xref: /openbmc/linux/net/ipv6/route.c (revision 7ff74a596b6aa47ccc71f6b9a26006af69d0e33f)
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] = {
22314edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
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 
324b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
325b3419363SDavid S. Miller {
32697bab73fSDavid S. Miller 	struct inet_peer_base *base;
327b3419363SDavid S. Miller 	struct inet_peer *peer;
328b3419363SDavid S. Miller 
32997bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
33097bab73fSDavid S. Miller 	if (!base)
33197bab73fSDavid S. Miller 		return;
33297bab73fSDavid S. Miller 
33397bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3347b34ca2aSDavid S. Miller 	if (peer) {
33597bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
336b3419363SDavid S. Miller 			inet_putpeer(peer);
3371da177e4SLinus Torvalds 	}
3387b34ca2aSDavid S. Miller }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3411da177e4SLinus Torvalds 			   int how)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3441da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3455a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
346c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3471da177e4SLinus Torvalds 
34897cac082SDavid S. Miller 	if (dev != loopback_dev) {
34997cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3505a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3515a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
35238308473SDavid S. Miller 			if (loopback_idev) {
3531da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3541da177e4SLinus Torvalds 				in6_dev_put(idev);
3551da177e4SLinus Torvalds 			}
3561da177e4SLinus Torvalds 		}
35797cac082SDavid S. Miller 		if (rt->n && rt->n->dev == dev) {
35897cac082SDavid S. Miller 			rt->n->dev = loopback_dev;
35997cac082SDavid S. Miller 			dev_hold(loopback_dev);
36097cac082SDavid S. Miller 			dev_put(dev);
36197cac082SDavid S. Miller 		}
36297cac082SDavid S. Miller 	}
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
365a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3661da177e4SLinus Torvalds {
3671716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3681716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
369a50feda5SEric Dumazet 			return true;
3701716a961SGao feng 	} else if (rt->dst.from) {
3713fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3721716a961SGao feng 	}
373a50feda5SEric Dumazet 	return false;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
377c71099acSThomas Graf {
378a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
379a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
380c71099acSThomas Graf }
381c71099acSThomas Graf 
38251ebd318SNicolas Dichtel /* Multipath route selection:
38351ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
38451ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
38551ebd318SNicolas Dichtel  */
38651ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
38751ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
38851ebd318SNicolas Dichtel {
38951ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
39051ebd318SNicolas Dichtel 
391c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
392c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
39351ebd318SNicolas Dichtel 
39451ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
39551ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
39651ebd318SNicolas Dichtel 	case IPPROTO_UDP:
39751ebd318SNicolas Dichtel 	case IPPROTO_TCP:
39851ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
399b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
400b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
40151ebd318SNicolas Dichtel 		break;
40251ebd318SNicolas Dichtel 
40351ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
404b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
405b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
40651ebd318SNicolas Dichtel 		break;
40751ebd318SNicolas Dichtel 	}
40851ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
409b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
41051ebd318SNicolas Dichtel 
41151ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
41251ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
41351ebd318SNicolas Dichtel 	return val % candidate_count;
41451ebd318SNicolas Dichtel }
41551ebd318SNicolas Dichtel 
41651ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
41751ebd318SNicolas Dichtel 					     struct flowi6 *fl6)
41851ebd318SNicolas Dichtel {
41951ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
42051ebd318SNicolas Dichtel 	int route_choosen;
42151ebd318SNicolas Dichtel 
42251ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
42351ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
42451ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
42551ebd318SNicolas Dichtel 	 */
42651ebd318SNicolas Dichtel 	if (route_choosen)
42751ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
42851ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
42951ebd318SNicolas Dichtel 			route_choosen--;
43051ebd318SNicolas Dichtel 			if (route_choosen == 0) {
43151ebd318SNicolas Dichtel 				match = sibling;
43251ebd318SNicolas Dichtel 				break;
43351ebd318SNicolas Dichtel 			}
43451ebd318SNicolas Dichtel 		}
43551ebd318SNicolas Dichtel 	return match;
43651ebd318SNicolas Dichtel }
43751ebd318SNicolas Dichtel 
4381da177e4SLinus Torvalds /*
439c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds 
4428ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4438ed67789SDaniel Lezcano 						    struct rt6_info *rt,
444b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4451da177e4SLinus Torvalds 						    int oif,
446d420895eSYOSHIFUJI Hideaki 						    int flags)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4491da177e4SLinus Torvalds 	struct rt6_info *sprt;
4501da177e4SLinus Torvalds 
451dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
452dd3abc4eSYOSHIFUJI Hideaki 		goto out;
453dd3abc4eSYOSHIFUJI Hideaki 
454d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
455d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
456dd3abc4eSYOSHIFUJI Hideaki 
457dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4581da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4591da177e4SLinus Torvalds 				return sprt;
4601da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
46138308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4621da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
463d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4641da177e4SLinus Torvalds 						continue;
4651da177e4SLinus Torvalds 					if (local && (!oif ||
4661da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4671da177e4SLinus Torvalds 						continue;
4681da177e4SLinus Torvalds 				}
4691da177e4SLinus Torvalds 				local = sprt;
4701da177e4SLinus Torvalds 			}
471dd3abc4eSYOSHIFUJI Hideaki 		} else {
472dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
473dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
474dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
475dd3abc4eSYOSHIFUJI Hideaki 		}
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
478dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4791da177e4SLinus Torvalds 		if (local)
4801da177e4SLinus Torvalds 			return local;
4811da177e4SLinus Torvalds 
482d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4838ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4841da177e4SLinus Torvalds 	}
485dd3abc4eSYOSHIFUJI Hideaki out:
4861da177e4SLinus Torvalds 	return rt;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
48927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
49027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
49127097255SYOSHIFUJI Hideaki {
492f2c31e32SEric Dumazet 	struct neighbour *neigh;
49327097255SYOSHIFUJI Hideaki 	/*
49427097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
49527097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
49627097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
49727097255SYOSHIFUJI Hideaki 	 *
49827097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
49927097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
50027097255SYOSHIFUJI Hideaki 	 */
50197cac082SDavid S. Miller 	neigh = rt ? rt->n : NULL;
502*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh)
503fdd6681dSAmerigo Wang 		return;
504*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	write_lock_bh(&neigh->lock);
505*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh->nud_state & NUD_VALID) {
506*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 		write_unlock_bh(&neigh->lock);
507*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 		return;
508*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
50927097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
51052e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
51127097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
51227097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
51327097255SYOSHIFUJI Hideaki 
51427097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
515*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 		write_unlock_bh(&neigh->lock);
51627097255SYOSHIFUJI Hideaki 
51727097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
51827097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
519d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
520f2c31e32SEric Dumazet 	} else {
521*7ff74a59SYOSHIFUJI Hideaki / 吉藤英明 		write_unlock_bh(&neigh->lock);
52227097255SYOSHIFUJI Hideaki 	}
523f2c31e32SEric Dumazet }
52427097255SYOSHIFUJI Hideaki #else
52527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
52627097255SYOSHIFUJI Hideaki {
52727097255SYOSHIFUJI Hideaki }
52827097255SYOSHIFUJI Hideaki #endif
52927097255SYOSHIFUJI Hideaki 
5301da177e4SLinus Torvalds /*
531554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5321da177e4SLinus Torvalds  */
533b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5341da177e4SLinus Torvalds {
535d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
536161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
537554cfb7eSYOSHIFUJI Hideaki 		return 2;
538161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
539161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
540161980f4SDavid S. Miller 		return 1;
541554cfb7eSYOSHIFUJI Hideaki 	return 0;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
544a5a81f0bSPaul Marks static inline bool rt6_check_neigh(struct rt6_info *rt)
5451da177e4SLinus Torvalds {
546f2c31e32SEric Dumazet 	struct neighbour *neigh;
547a5a81f0bSPaul Marks 	bool ret = false;
548f2c31e32SEric Dumazet 
54997cac082SDavid S. Miller 	neigh = rt->n;
5504d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5514d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
552a5a81f0bSPaul Marks 		ret = true;
5534d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
5541da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
555554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
556a5a81f0bSPaul Marks 			ret = true;
557398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
558a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
559a5a81f0bSPaul Marks 			ret = true;
560398bcbebSYOSHIFUJI Hideaki #endif
5611da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
562a5a81f0bSPaul Marks 	}
563a5a81f0bSPaul Marks 	return ret;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
566554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
567554cfb7eSYOSHIFUJI Hideaki 			   int strict)
568554cfb7eSYOSHIFUJI Hideaki {
569a5a81f0bSPaul Marks 	int m;
5704d0c5911SYOSHIFUJI Hideaki 
5714d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
57277d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
573554cfb7eSYOSHIFUJI Hideaki 		return -1;
574ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
575ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
576ebacaaa0SYOSHIFUJI Hideaki #endif
577a5a81f0bSPaul Marks 	if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
578554cfb7eSYOSHIFUJI Hideaki 		return -1;
579554cfb7eSYOSHIFUJI Hideaki 	return m;
580554cfb7eSYOSHIFUJI Hideaki }
581554cfb7eSYOSHIFUJI Hideaki 
582f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
583f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
584554cfb7eSYOSHIFUJI Hideaki {
585554cfb7eSYOSHIFUJI Hideaki 	int m;
586554cfb7eSYOSHIFUJI Hideaki 
587554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
588f11e6659SDavid S. Miller 		goto out;
589554cfb7eSYOSHIFUJI Hideaki 
590554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
591554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
592f11e6659SDavid S. Miller 		goto out;
593554cfb7eSYOSHIFUJI Hideaki 
594f11e6659SDavid S. Miller 	if (m > *mpri) {
595ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
59627097255SYOSHIFUJI Hideaki 			rt6_probe(match);
597f11e6659SDavid S. Miller 		*mpri = m;
598554cfb7eSYOSHIFUJI Hideaki 		match = rt;
599ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
60027097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
6011da177e4SLinus Torvalds 	}
602f11e6659SDavid S. Miller 
603f11e6659SDavid S. Miller out:
604f11e6659SDavid S. Miller 	return match;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
607f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
608f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
609f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
610f11e6659SDavid S. Miller {
611f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
612f11e6659SDavid S. Miller 	int mpri = -1;
613f11e6659SDavid S. Miller 
614f11e6659SDavid S. Miller 	match = NULL;
615f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
616d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
617f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
618f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
619d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
620f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
621f11e6659SDavid S. Miller 
622f11e6659SDavid S. Miller 	return match;
623f11e6659SDavid S. Miller }
624f11e6659SDavid S. Miller 
625f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
626f11e6659SDavid S. Miller {
627f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6288ed67789SDaniel Lezcano 	struct net *net;
629f11e6659SDavid S. Miller 
630f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
631f11e6659SDavid S. Miller 	if (!rt0)
632f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
633f11e6659SDavid S. Miller 
634f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
635f11e6659SDavid S. Miller 
636554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
637f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
638d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
639f11e6659SDavid S. Miller 
640554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
641f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
642f11e6659SDavid S. Miller 			next = fn->leaf;
643f11e6659SDavid S. Miller 
644f11e6659SDavid S. Miller 		if (next != rt0)
645f11e6659SDavid S. Miller 			fn->rr_ptr = next;
646554cfb7eSYOSHIFUJI Hideaki 	}
647554cfb7eSYOSHIFUJI Hideaki 
648d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
649a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
65270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
65370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
654b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
65570ceb4f5SYOSHIFUJI Hideaki {
656c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
65770ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
65870ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
65970ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6604bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
66170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
66270ceb4f5SYOSHIFUJI Hideaki 
66370ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
66470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66570ceb4f5SYOSHIFUJI Hideaki 	}
66670ceb4f5SYOSHIFUJI Hideaki 
66770ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
66870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
66970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
67070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
67170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
67270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
67370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
67470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
67570ceb4f5SYOSHIFUJI Hideaki 		}
67670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
67770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
67870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
67970ceb4f5SYOSHIFUJI Hideaki 		}
68070ceb4f5SYOSHIFUJI Hideaki 	}
68170ceb4f5SYOSHIFUJI Hideaki 
68270ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
68370ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6843933fc95SJens Rosenboom 		return -EINVAL;
68570ceb4f5SYOSHIFUJI Hideaki 
6864bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
68770ceb4f5SYOSHIFUJI Hideaki 
68870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
68970ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
69070ceb4f5SYOSHIFUJI Hideaki 	else {
69170ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
69270ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
69370ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
69470ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
69570ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
69670ceb4f5SYOSHIFUJI Hideaki 	}
69770ceb4f5SYOSHIFUJI Hideaki 
698efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
699efa2cea0SDaniel Lezcano 				dev->ifindex);
70070ceb4f5SYOSHIFUJI Hideaki 
70170ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
702e0a1ad73SThomas Graf 		ip6_del_rt(rt);
70370ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
70470ceb4f5SYOSHIFUJI Hideaki 	}
70570ceb4f5SYOSHIFUJI Hideaki 
70670ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
707efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
70870ceb4f5SYOSHIFUJI Hideaki 					pref);
70970ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
71070ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
71170ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
71270ceb4f5SYOSHIFUJI Hideaki 
71370ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7141716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7151716a961SGao feng 			rt6_clean_expires(rt);
7161716a961SGao feng 		else
7171716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7181716a961SGao feng 
71994e187c0SAmerigo Wang 		ip6_rt_put(rt);
72070ceb4f5SYOSHIFUJI Hideaki 	}
72170ceb4f5SYOSHIFUJI Hideaki 	return 0;
72270ceb4f5SYOSHIFUJI Hideaki }
72370ceb4f5SYOSHIFUJI Hideaki #endif
72470ceb4f5SYOSHIFUJI Hideaki 
7258ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
726982f56f3SYOSHIFUJI Hideaki do { \
7278ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
728982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
729e0eda7bbSVille Nuorvala 		while (1) { \
730982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
731c71099acSThomas Graf 				goto out; \
732982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
733982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7348bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
735982f56f3SYOSHIFUJI Hideaki 			else \
736982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
737c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
738c71099acSThomas Graf 				goto restart; \
739c71099acSThomas Graf 		} \
740982f56f3SYOSHIFUJI Hideaki 	} \
741982f56f3SYOSHIFUJI Hideaki } while (0)
742c71099acSThomas Graf 
7438ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7448ed67789SDaniel Lezcano 					     struct fib6_table *table,
7454c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	struct fib6_node *fn;
7481da177e4SLinus Torvalds 	struct rt6_info *rt;
7491da177e4SLinus Torvalds 
750c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7514c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
752c71099acSThomas Graf restart:
753c71099acSThomas Graf 	rt = fn->leaf;
7544c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
75551ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
75651ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
7574c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
758c71099acSThomas Graf out:
759d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
760c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7611da177e4SLinus Torvalds 	return rt;
762c71099acSThomas Graf 
763c71099acSThomas Graf }
764c71099acSThomas Graf 
765ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
766ea6e574eSFlorian Westphal 				    int flags)
767ea6e574eSFlorian Westphal {
768ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
769ea6e574eSFlorian Westphal }
770ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
771ea6e574eSFlorian Westphal 
7729acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7739acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
774c71099acSThomas Graf {
7754c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7764c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7774c9483b2SDavid S. Miller 		.daddr = *daddr,
778c71099acSThomas Graf 	};
779c71099acSThomas Graf 	struct dst_entry *dst;
78077d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
781c71099acSThomas Graf 
782adaa70bbSThomas Graf 	if (saddr) {
7834c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
784adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
785adaa70bbSThomas Graf 	}
786adaa70bbSThomas Graf 
7874c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
788c71099acSThomas Graf 	if (dst->error == 0)
789c71099acSThomas Graf 		return (struct rt6_info *) dst;
790c71099acSThomas Graf 
791c71099acSThomas Graf 	dst_release(dst);
792c71099acSThomas Graf 
7931da177e4SLinus Torvalds 	return NULL;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7967159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7977159039aSYOSHIFUJI Hideaki 
798c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7991da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8001da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8011da177e4SLinus Torvalds    be destroyed.
8021da177e4SLinus Torvalds  */
8031da177e4SLinus Torvalds 
80486872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	int err;
807c71099acSThomas Graf 	struct fib6_table *table;
8081da177e4SLinus Torvalds 
809c71099acSThomas Graf 	table = rt->rt6i_table;
810c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
81186872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
812c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	return err;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
81740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
81840e22e8fSThomas Graf {
8194d1169c1SDenis V. Lunev 	struct nl_info info = {
820d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8214d1169c1SDenis V. Lunev 	};
822528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
82340e22e8fSThomas Graf }
82440e22e8fSThomas Graf 
8251716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
82621efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
827b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	struct rt6_info *rt;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	/*
8321da177e4SLinus Torvalds 	 *	Clone the route.
8331da177e4SLinus Torvalds 	 */
8341da177e4SLinus Torvalds 
83521efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (rt) {
83814deae41SDavid S. Miller 		int attempts = !in_softirq();
83914deae41SDavid S. Miller 
84058c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
841bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
84221efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
84358c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8444e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
84558c4fb86SYOSHIFUJI Hideaki 		}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8501da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8514e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8521da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8531da177e4SLinus Torvalds 		}
8541da177e4SLinus Torvalds #endif
8551da177e4SLinus Torvalds 
85614deae41SDavid S. Miller 	retry:
8578ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
858d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
85914deae41SDavid S. Miller 			int saved_rt_min_interval =
86014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
86114deae41SDavid S. Miller 			int saved_rt_elasticity =
86214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
86314deae41SDavid S. Miller 
86414deae41SDavid S. Miller 			if (attempts-- > 0) {
86514deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
86614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
86714deae41SDavid S. Miller 
86886393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
86914deae41SDavid S. Miller 
87014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
87114deae41SDavid S. Miller 					saved_rt_elasticity;
87214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
87314deae41SDavid S. Miller 					saved_rt_min_interval;
87414deae41SDavid S. Miller 				goto retry;
87514deae41SDavid S. Miller 			}
87614deae41SDavid S. Miller 
877f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
878d8d1f30bSChangli Gao 			dst_free(&rt->dst);
87914deae41SDavid S. Miller 			return NULL;
88014deae41SDavid S. Miller 		}
88195a9a5baSYOSHIFUJI Hideaki 	}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	return rt;
8841da177e4SLinus Torvalds }
88595a9a5baSYOSHIFUJI Hideaki 
88621efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
88721efcfa0SEric Dumazet 					const struct in6_addr *daddr)
888299d9939SYOSHIFUJI Hideaki {
88921efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
89021efcfa0SEric Dumazet 
891299d9939SYOSHIFUJI Hideaki 	if (rt) {
892299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
89397cac082SDavid S. Miller 		rt->n = neigh_clone(ort->n);
894299d9939SYOSHIFUJI Hideaki 	}
895299d9939SYOSHIFUJI Hideaki 	return rt;
896299d9939SYOSHIFUJI Hideaki }
897299d9939SYOSHIFUJI Hideaki 
8988ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8994c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	struct fib6_node *fn;
902519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
903c71099acSThomas Graf 	int strict = 0;
9041da177e4SLinus Torvalds 	int attempts = 3;
905519fbd87SYOSHIFUJI Hideaki 	int err;
90653b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
9071da177e4SLinus Torvalds 
90877d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds relookup:
911c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
9121da177e4SLinus Torvalds 
9138238dd06SYOSHIFUJI Hideaki restart_2:
9144c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds restart:
9174acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
91851ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && oif == 0)
91951ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
9204c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9218ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9228238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9231da177e4SLinus Torvalds 		goto out;
9241da177e4SLinus Torvalds 
925d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
926c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9271da177e4SLinus Torvalds 
92897cac082SDavid S. Miller 	if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP))
9294c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9307343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9314c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9327343ff31SDavid S. Miller 	else
9337343ff31SDavid S. Miller 		goto out2;
9341da177e4SLinus Torvalds 
93594e187c0SAmerigo Wang 	ip6_rt_put(rt);
9368ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9371da177e4SLinus Torvalds 
938d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
939e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
94040e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
941e40cf353SYOSHIFUJI Hideaki 		if (!err)
942e40cf353SYOSHIFUJI Hideaki 			goto out2;
943e40cf353SYOSHIFUJI Hideaki 	}
944e40cf353SYOSHIFUJI Hideaki 
945e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9461da177e4SLinus Torvalds 		goto out2;
9471da177e4SLinus Torvalds 
948519fbd87SYOSHIFUJI Hideaki 	/*
949c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
950519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9511da177e4SLinus Torvalds 	 */
95294e187c0SAmerigo Wang 	ip6_rt_put(rt);
9531da177e4SLinus Torvalds 	goto relookup;
954e40cf353SYOSHIFUJI Hideaki 
955519fbd87SYOSHIFUJI Hideaki out:
9568238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9578238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9588238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9598238dd06SYOSHIFUJI Hideaki 	}
960d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
961c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9621da177e4SLinus Torvalds out2:
963d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
964d8d1f30bSChangli Gao 	rt->dst.__use++;
965c71099acSThomas Graf 
966c71099acSThomas Graf 	return rt;
967c71099acSThomas Graf }
968c71099acSThomas Graf 
9698ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9704c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9714acad72dSPavel Emelyanov {
9724c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9734acad72dSPavel Emelyanov }
9744acad72dSPavel Emelyanov 
97572331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
97672331bc0SShmulik Ladkani 						struct net_device *dev,
97772331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
97872331bc0SShmulik Ladkani {
97972331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
98072331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
98172331bc0SShmulik Ladkani 
98272331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
98372331bc0SShmulik Ladkani }
98472331bc0SShmulik Ladkani 
985c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
986c71099acSThomas Graf {
987b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
988c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
989adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9904c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9914c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9924c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9934c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9946502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9954c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9964c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
997c71099acSThomas Graf 	};
998adaa70bbSThomas Graf 
99972331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1000c71099acSThomas Graf }
1001c71099acSThomas Graf 
10028ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
10034c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1004c71099acSThomas Graf {
10054c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1006c71099acSThomas Graf }
1007c71099acSThomas Graf 
10089c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
10094c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
1010c71099acSThomas Graf {
1011c71099acSThomas Graf 	int flags = 0;
1012c71099acSThomas Graf 
10131fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
10144dc27d1cSDavid McCullough 
10154c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
101677d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1017c71099acSThomas Graf 
10184c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1019adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10200c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10210c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1022adaa70bbSThomas Graf 
10234c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10267159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10271da177e4SLinus Torvalds 
10282774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
102914e50e57SDavid S. Miller {
10305c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
103114e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
103214e50e57SDavid S. Miller 
1033f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
103414e50e57SDavid S. Miller 	if (rt) {
1035d8d1f30bSChangli Gao 		new = &rt->dst;
103614e50e57SDavid S. Miller 
10378104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10388104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10398104891bSSteffen Klassert 
104014e50e57SDavid S. Miller 		new->__use = 1;
1041352e512cSHerbert Xu 		new->input = dst_discard;
1042352e512cSHerbert Xu 		new->output = dst_discard;
104314e50e57SDavid S. Miller 
104421efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
104521efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
104621efcfa0SEric Dumazet 		else
1047defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
104814e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
104914e50e57SDavid S. Miller 		if (rt->rt6i_idev)
105014e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
105114e50e57SDavid S. Miller 
10524e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10531716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
10541716a961SGao feng 		rt6_clean_expires(rt);
105514e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
105614e50e57SDavid S. Miller 
105714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
105814e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
105914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
106014e50e57SDavid S. Miller #endif
106114e50e57SDavid S. Miller 
106214e50e57SDavid S. Miller 		dst_free(new);
106314e50e57SDavid S. Miller 	}
106414e50e57SDavid S. Miller 
106569ead7afSDavid S. Miller 	dst_release(dst_orig);
106669ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
106714e50e57SDavid S. Miller }
106814e50e57SDavid S. Miller 
10691da177e4SLinus Torvalds /*
10701da177e4SLinus Torvalds  *	Destination cache support functions
10711da177e4SLinus Torvalds  */
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct rt6_info *rt;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10781da177e4SLinus Torvalds 
10796f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10806f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10816f3118b5SNicolas Dichtel 	 * into this function always.
10826f3118b5SNicolas Dichtel 	 */
10836f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
10846f3118b5SNicolas Dichtel 		return NULL;
10856f3118b5SNicolas Dichtel 
1086a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10871da177e4SLinus Torvalds 		return dst;
1088a4477c4dSLi RongQing 
10891da177e4SLinus Torvalds 	return NULL;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10931da177e4SLinus Torvalds {
10941da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (rt) {
109754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
109854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1099e0a1ad73SThomas Graf 				ip6_del_rt(rt);
110054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
11011da177e4SLinus Torvalds 			}
110254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
110354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
110454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
110554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
110654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
110754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	struct rt6_info *rt;
11131da177e4SLinus Torvalds 
11143ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11151da177e4SLinus Torvalds 
1116adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11171da177e4SLinus Torvalds 	if (rt) {
11181716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
11191716a961SGao feng 			rt6_update_expires(rt, 0);
11201716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
11211da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11256700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11266700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11291da177e4SLinus Torvalds 
113081aded24SDavid S. Miller 	dst_confirm(dst);
11311da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
113281aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
113381aded24SDavid S. Miller 
11341da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11351da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1136defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11371da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1138defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1139defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11401da177e4SLinus Torvalds 		}
1141defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
114281aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
114642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
114742ae66c8SDavid S. Miller 		     int oif, u32 mark)
114881aded24SDavid S. Miller {
114981aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
115081aded24SDavid S. Miller 	struct dst_entry *dst;
115181aded24SDavid S. Miller 	struct flowi6 fl6;
115281aded24SDavid S. Miller 
115381aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
115481aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
115581aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11563e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
115781aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
115881aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11596502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
116081aded24SDavid S. Miller 
116181aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
116281aded24SDavid S. Miller 	if (!dst->error)
11636700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
116481aded24SDavid S. Miller 	dst_release(dst);
116581aded24SDavid S. Miller }
116681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
116781aded24SDavid S. Miller 
116881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
116981aded24SDavid S. Miller {
117081aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
117181aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
117281aded24SDavid S. Miller }
117381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
117481aded24SDavid S. Miller 
11753a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11763a5ad2eeSDavid S. Miller {
11773a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11783a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11793a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11803a5ad2eeSDavid S. Miller 
11813a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11823a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11833a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11843a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11853a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11863a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11876502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
11883a5ad2eeSDavid S. Miller 
11893a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11903a5ad2eeSDavid S. Miller 	if (!dst->error)
11916700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
11923a5ad2eeSDavid S. Miller 	dst_release(dst);
11933a5ad2eeSDavid S. Miller }
11943a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11953a5ad2eeSDavid S. Miller 
11963a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11973a5ad2eeSDavid S. Miller {
11983a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11993a5ad2eeSDavid S. Miller }
12003a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
12013a5ad2eeSDavid S. Miller 
12020dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
12031da177e4SLinus Torvalds {
12040dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
12050dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
12060dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
12070dbaee3bSDavid S. Miller 
12081da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
12091da177e4SLinus Torvalds 
12105578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
12115578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	/*
12141da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
12151da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
12161da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
12171da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
12181da177e4SLinus Torvalds 	 */
12191da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
12201da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
12211da177e4SLinus Torvalds 	return mtu;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
1224ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1225d33e4553SDavid S. Miller {
1226d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1227618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1228618f9bc7SSteffen Klassert 
1229618f9bc7SSteffen Klassert 	if (mtu)
1230618f9bc7SSteffen Klassert 		return mtu;
1231618f9bc7SSteffen Klassert 
1232618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1233d33e4553SDavid S. Miller 
1234d33e4553SDavid S. Miller 	rcu_read_lock();
1235d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1236d33e4553SDavid S. Miller 	if (idev)
1237d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1238d33e4553SDavid S. Miller 	rcu_read_unlock();
1239d33e4553SDavid S. Miller 
1240d33e4553SDavid S. Miller 	return mtu;
1241d33e4553SDavid S. Miller }
1242d33e4553SDavid S. Miller 
12433b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12443b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12455d0bbeebSThomas Graf 
12463b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
12471da177e4SLinus Torvalds 				  struct neighbour *neigh,
124887a11578SDavid S. Miller 				  struct flowi6 *fl6)
12491da177e4SLinus Torvalds {
125087a11578SDavid S. Miller 	struct dst_entry *dst;
12511da177e4SLinus Torvalds 	struct rt6_info *rt;
12521da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1253c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12541da177e4SLinus Torvalds 
125538308473SDavid S. Miller 	if (unlikely(!idev))
1256122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12571da177e4SLinus Torvalds 
12588b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
125938308473SDavid S. Miller 	if (unlikely(!rt)) {
12601da177e4SLinus Torvalds 		in6_dev_put(idev);
126187a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12621da177e4SLinus Torvalds 		goto out;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	if (neigh)
12661da177e4SLinus Torvalds 		neigh_hold(neigh);
126714deae41SDavid S. Miller 	else {
1268f894cbf8SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
1269b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1270252c3d84SRongQing.Li 			in6_dev_put(idev);
1271b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1272b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1273b43faac6SDavid S. Miller 		}
127414deae41SDavid S. Miller 	}
12751da177e4SLinus Torvalds 
12768e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12778e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
127897cac082SDavid S. Miller 	rt->n = neigh;
1279d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
128087a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12818e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12828e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
128314edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
12841da177e4SLinus Torvalds 
12853b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1286d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1287d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12883b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12891da177e4SLinus Torvalds 
12905578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12911da177e4SLinus Torvalds 
129287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
129387a11578SDavid S. Miller 
12941da177e4SLinus Torvalds out:
129587a11578SDavid S. Miller 	return dst;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12983d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12991da177e4SLinus Torvalds {
1300e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
13013d0f24a7SStephen Hemminger 	int more = 0;
13021da177e4SLinus Torvalds 
13033b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
13043b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
13055d0bbeebSThomas Graf 
13061da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
13071da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
13081da177e4SLinus Torvalds 			*pprev = dst->next;
13091da177e4SLinus Torvalds 			dst_free(dst);
13101da177e4SLinus Torvalds 		} else {
13111da177e4SLinus Torvalds 			pprev = &dst->next;
13123d0f24a7SStephen Hemminger 			++more;
13131da177e4SLinus Torvalds 		}
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds 
13163b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13175d0bbeebSThomas Graf 
13183d0f24a7SStephen Hemminger 	return more;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
13221e493d19SDavid S. Miller 			    void *arg)
13231e493d19SDavid S. Miller {
13241e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
13251e493d19SDavid S. Miller 
13261e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
13271e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
13281e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
13291e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
13301e493d19SDavid S. Miller 		if (func(rt, arg)) {
13311e493d19SDavid S. Miller 			*pprev = dst->next;
13321e493d19SDavid S. Miller 			dst_free(dst);
13331e493d19SDavid S. Miller 		} else {
13341e493d19SDavid S. Miller 			pprev = &dst->next;
13351e493d19SDavid S. Miller 		}
13361e493d19SDavid S. Miller 	}
13371e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
13381e493d19SDavid S. Miller }
13391e493d19SDavid S. Miller 
1340569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	unsigned long now = jiffies;
134386393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
13447019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
13457019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
13467019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
13477019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
13487019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1349fc66f95cSEric Dumazet 	int entries;
13501da177e4SLinus Torvalds 
1351fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
13527019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1353fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13541da177e4SLinus Torvalds 		goto out;
13551da177e4SLinus Torvalds 
13566891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13576891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13586891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1359fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1360fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13617019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13621da177e4SLinus Torvalds out:
13637019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1364fc66f95cSEric Dumazet 	return entries > rt_max_size;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13676b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13681da177e4SLinus Torvalds {
13695170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1370a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13716b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1372c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1373c68f24ccSEric Dumazet 
1374c68f24ccSEric Dumazet 		rcu_read_lock();
1375c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1376c68f24ccSEric Dumazet 		if (idev)
13771da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1378c68f24ccSEric Dumazet 		else
137953b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1380c68f24ccSEric Dumazet 		rcu_read_unlock();
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 	return hoplimit;
13831da177e4SLinus Torvalds }
1384abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds /*
13871da177e4SLinus Torvalds  *
13881da177e4SLinus Torvalds  */
13891da177e4SLinus Torvalds 
139086872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13911da177e4SLinus Torvalds {
13921da177e4SLinus Torvalds 	int err;
13935578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13941da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13951da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13961da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1397c71099acSThomas Graf 	struct fib6_table *table;
13981da177e4SLinus Torvalds 	int addr_type;
13991da177e4SLinus Torvalds 
140086872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
14011da177e4SLinus Torvalds 		return -EINVAL;
14021da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
140386872cb5SThomas Graf 	if (cfg->fc_src_len)
14041da177e4SLinus Torvalds 		return -EINVAL;
14051da177e4SLinus Torvalds #endif
140686872cb5SThomas Graf 	if (cfg->fc_ifindex) {
14071da177e4SLinus Torvalds 		err = -ENODEV;
14085578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
14091da177e4SLinus Torvalds 		if (!dev)
14101da177e4SLinus Torvalds 			goto out;
14111da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
14121da177e4SLinus Torvalds 		if (!idev)
14131da177e4SLinus Torvalds 			goto out;
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
141686872cb5SThomas Graf 	if (cfg->fc_metric == 0)
141786872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
14181da177e4SLinus Torvalds 
1419c71099acSThomas Graf 	err = -ENOBUFS;
142038308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1421d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1422d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
142338308473SDavid S. Miller 		if (!table) {
1424f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1425d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1426d71314b4SMatti Vaittinen 		}
1427d71314b4SMatti Vaittinen 	} else {
1428d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1429d71314b4SMatti Vaittinen 	}
143038308473SDavid S. Miller 
143138308473SDavid S. Miller 	if (!table)
1432c71099acSThomas Graf 		goto out;
1433c71099acSThomas Graf 
14348b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
14351da177e4SLinus Torvalds 
143638308473SDavid S. Miller 	if (!rt) {
14371da177e4SLinus Torvalds 		err = -ENOMEM;
14381da177e4SLinus Torvalds 		goto out;
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 
14411716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
14421716a961SGao feng 		rt6_set_expires(rt, jiffies +
14431716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
14441716a961SGao feng 	else
14451716a961SGao feng 		rt6_clean_expires(rt);
14461da177e4SLinus Torvalds 
144786872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
144886872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
144986872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
145086872cb5SThomas Graf 
145186872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1454d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1455ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1456ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14571da177e4SLinus Torvalds 	else
1458d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14591da177e4SLinus Torvalds 
1460d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14611da177e4SLinus Torvalds 
146286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
146386872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14641da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
146511d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14661da177e4SLinus Torvalds 
14678e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14688e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14698e2ec639SYan, Zheng 		if (!metrics) {
14708e2ec639SYan, Zheng 			err = -ENOMEM;
14718e2ec639SYan, Zheng 			goto out;
14728e2ec639SYan, Zheng 		}
14738e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14748e2ec639SYan, Zheng 	}
14751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
147686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
147786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14781da177e4SLinus Torvalds #endif
14791da177e4SLinus Torvalds 
148086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14831da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14841da177e4SLinus Torvalds 	 */
148586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
148638308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
148738308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
148838308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14891da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14905578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14911da177e4SLinus Torvalds 			if (dev) {
14921da177e4SLinus Torvalds 				dev_put(dev);
14931da177e4SLinus Torvalds 				in6_dev_put(idev);
14941da177e4SLinus Torvalds 			}
14955578689aSDaniel Lezcano 			dev = net->loopback_dev;
14961da177e4SLinus Torvalds 			dev_hold(dev);
14971da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14981da177e4SLinus Torvalds 			if (!idev) {
14991da177e4SLinus Torvalds 				err = -ENODEV;
15001da177e4SLinus Torvalds 				goto out;
15011da177e4SLinus Torvalds 			}
15021da177e4SLinus Torvalds 		}
1503d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1504d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
15051da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1506ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1507ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1508ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1509ef2c7d7bSNicolas Dichtel 			break;
1510ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1511ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1512ef2c7d7bSNicolas Dichtel 			break;
1513b4949ab2SNicolas Dichtel 		case RTN_THROW:
1514b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1515b4949ab2SNicolas Dichtel 			break;
1516ef2c7d7bSNicolas Dichtel 		default:
1517ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1518ef2c7d7bSNicolas Dichtel 			break;
1519ef2c7d7bSNicolas Dichtel 		}
15201da177e4SLinus Torvalds 		goto install_route;
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
152386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1524b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
15251da177e4SLinus Torvalds 		int gwa_type;
15261da177e4SLinus Torvalds 
152786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
15284e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
15291da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
15321da177e4SLinus Torvalds 			struct rt6_info *grt;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
15351da177e4SLinus Torvalds 			   addresses as nexthop address.
15361da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
15371da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
15381da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
15391da177e4SLinus Torvalds 			   some exceptions. --ANK
15401da177e4SLinus Torvalds 			 */
15411da177e4SLinus Torvalds 			err = -EINVAL;
15421da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
15431da177e4SLinus Torvalds 				goto out;
15441da177e4SLinus Torvalds 
15455578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
154838308473SDavid S. Miller 			if (!grt)
15491da177e4SLinus Torvalds 				goto out;
15501da177e4SLinus Torvalds 			if (dev) {
1551d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
155294e187c0SAmerigo Wang 					ip6_rt_put(grt);
15531da177e4SLinus Torvalds 					goto out;
15541da177e4SLinus Torvalds 				}
15551da177e4SLinus Torvalds 			} else {
1556d1918542SDavid S. Miller 				dev = grt->dst.dev;
15571da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15581da177e4SLinus Torvalds 				dev_hold(dev);
15591da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15601da177e4SLinus Torvalds 			}
15611da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15621da177e4SLinus Torvalds 				err = 0;
156394e187c0SAmerigo Wang 			ip6_rt_put(grt);
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 			if (err)
15661da177e4SLinus Torvalds 				goto out;
15671da177e4SLinus Torvalds 		}
15681da177e4SLinus Torvalds 		err = -EINVAL;
156938308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15701da177e4SLinus Torvalds 			goto out;
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	err = -ENODEV;
157438308473SDavid S. Miller 	if (!dev)
15751da177e4SLinus Torvalds 		goto out;
15761da177e4SLinus Torvalds 
1577c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1578c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1579c3968a85SDaniel Walter 			err = -EINVAL;
1580c3968a85SDaniel Walter 			goto out;
1581c3968a85SDaniel Walter 		}
15824e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1583c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1584c3968a85SDaniel Walter 	} else
1585c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1586c3968a85SDaniel Walter 
158786872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
15888ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1589f83c7790SDavid S. Miller 		if (err)
15901da177e4SLinus Torvalds 			goto out;
15911da177e4SLinus Torvalds 	}
15921da177e4SLinus Torvalds 
159386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds install_route:
159686872cb5SThomas Graf 	if (cfg->fc_mx) {
159786872cb5SThomas Graf 		struct nlattr *nla;
159886872cb5SThomas Graf 		int remaining;
15991da177e4SLinus Torvalds 
160086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
16018f4c1f9bSThomas Graf 			int type = nla_type(nla);
160286872cb5SThomas Graf 
160386872cb5SThomas Graf 			if (type) {
160486872cb5SThomas Graf 				if (type > RTAX_MAX) {
16051da177e4SLinus Torvalds 					err = -EINVAL;
16061da177e4SLinus Torvalds 					goto out;
16071da177e4SLinus Torvalds 				}
160886872cb5SThomas Graf 
1609defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
16101da177e4SLinus Torvalds 			}
16111da177e4SLinus Torvalds 		}
16121da177e4SLinus Torvalds 	}
16131da177e4SLinus Torvalds 
1614d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16151da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1616c71099acSThomas Graf 	rt->rt6i_table = table;
161763152fc0SDaniel Lezcano 
1618c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
161963152fc0SDaniel Lezcano 
162086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds out:
16231da177e4SLinus Torvalds 	if (dev)
16241da177e4SLinus Torvalds 		dev_put(dev);
16251da177e4SLinus Torvalds 	if (idev)
16261da177e4SLinus Torvalds 		in6_dev_put(idev);
16271da177e4SLinus Torvalds 	if (rt)
1628d8d1f30bSChangli Gao 		dst_free(&rt->dst);
16291da177e4SLinus Torvalds 	return err;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
163286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
16331da177e4SLinus Torvalds {
16341da177e4SLinus Torvalds 	int err;
1635c71099acSThomas Graf 	struct fib6_table *table;
1636d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
16371da177e4SLinus Torvalds 
16386825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
16396825a26cSGao feng 		err = -ENOENT;
16406825a26cSGao feng 		goto out;
16416825a26cSGao feng 	}
16426c813a72SPatrick McHardy 
1643c71099acSThomas Graf 	table = rt->rt6i_table;
1644c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
164586872cb5SThomas Graf 	err = fib6_del(rt, info);
1646c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16471da177e4SLinus Torvalds 
16486825a26cSGao feng out:
164994e187c0SAmerigo Wang 	ip6_rt_put(rt);
16501da177e4SLinus Torvalds 	return err;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
1653e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1654e0a1ad73SThomas Graf {
16554d1169c1SDenis V. Lunev 	struct nl_info info = {
1656d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
16574d1169c1SDenis V. Lunev 	};
1658528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1659e0a1ad73SThomas Graf }
1660e0a1ad73SThomas Graf 
166186872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16621da177e4SLinus Torvalds {
1663c71099acSThomas Graf 	struct fib6_table *table;
16641da177e4SLinus Torvalds 	struct fib6_node *fn;
16651da177e4SLinus Torvalds 	struct rt6_info *rt;
16661da177e4SLinus Torvalds 	int err = -ESRCH;
16671da177e4SLinus Torvalds 
16685578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
166938308473SDavid S. Miller 	if (!table)
1670c71099acSThomas Graf 		return err;
16711da177e4SLinus Torvalds 
1672c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1673c71099acSThomas Graf 
1674c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
167586872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
167686872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	if (fn) {
1679d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
168086872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1681d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1682d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16831da177e4SLinus Torvalds 				continue;
168486872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
168586872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16861da177e4SLinus Torvalds 				continue;
168786872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16881da177e4SLinus Torvalds 				continue;
1689d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1690c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16911da177e4SLinus Torvalds 
169286872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 	}
1695c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	return err;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17006700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1701a6279458SYOSHIFUJI Hideaki {
1702e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1703a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1704e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1705e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
17066e157b6aSDavid S. Miller 	struct neighbour *old_neigh;
1707e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1708e8599ff4SDavid S. Miller 	struct neighbour *neigh;
170971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
17106e157b6aSDavid S. Miller 	int optlen, on_link;
17116e157b6aSDavid S. Miller 	u8 *lladdr;
1712e8599ff4SDavid S. Miller 
1713e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
171471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1715e8599ff4SDavid S. Miller 
1716e8599ff4SDavid S. Miller 	if (optlen < 0) {
17176e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1718e8599ff4SDavid S. Miller 		return;
1719e8599ff4SDavid S. Miller 	}
1720e8599ff4SDavid S. Miller 
172171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1722e8599ff4SDavid S. Miller 
172371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
17246e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1725e8599ff4SDavid S. Miller 		return;
1726e8599ff4SDavid S. Miller 	}
1727e8599ff4SDavid S. Miller 
17286e157b6aSDavid S. Miller 	on_link = 0;
172971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1730e8599ff4SDavid S. Miller 		on_link = 1;
173171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1732e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
17336e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1734e8599ff4SDavid S. Miller 		return;
1735e8599ff4SDavid S. Miller 	}
1736e8599ff4SDavid S. Miller 
1737e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1738e8599ff4SDavid S. Miller 	if (!in6_dev)
1739e8599ff4SDavid S. Miller 		return;
1740e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1741e8599ff4SDavid S. Miller 		return;
1742e8599ff4SDavid S. Miller 
1743e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1744e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1745e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1746e8599ff4SDavid S. Miller 	 */
1747e8599ff4SDavid S. Miller 
174871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1749e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1750e8599ff4SDavid S. Miller 		return;
1751e8599ff4SDavid S. Miller 	}
17526e157b6aSDavid S. Miller 
17536e157b6aSDavid S. Miller 	lladdr = NULL;
1754e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1755e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1756e8599ff4SDavid S. Miller 					     skb->dev);
1757e8599ff4SDavid S. Miller 		if (!lladdr) {
1758e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1759e8599ff4SDavid S. Miller 			return;
1760e8599ff4SDavid S. Miller 		}
1761e8599ff4SDavid S. Miller 	}
1762e8599ff4SDavid S. Miller 
17636e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17646e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17656e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17666e157b6aSDavid S. Miller 		return;
17676e157b6aSDavid S. Miller 	}
17686e157b6aSDavid S. Miller 
17696e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17706e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17716e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17726e157b6aSDavid S. Miller 	 */
17736e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17746e157b6aSDavid S. Miller 
177571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1776e8599ff4SDavid S. Miller 	if (!neigh)
1777e8599ff4SDavid S. Miller 		return;
1778e8599ff4SDavid S. Miller 
17796e157b6aSDavid S. Miller 	/* Duplicate redirect: silently ignore. */
17806e157b6aSDavid S. Miller 	old_neigh = rt->n;
17816e157b6aSDavid S. Miller 	if (neigh == old_neigh)
1782a6279458SYOSHIFUJI Hideaki 		goto out;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	/*
17851da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17861da177e4SLinus Torvalds 	 */
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17891da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17901da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17911da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17921da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17931da177e4SLinus Torvalds 		     );
17941da177e4SLinus Torvalds 
179571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
179638308473SDavid S. Miller 	if (!nrt)
17971da177e4SLinus Torvalds 		goto out;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18001da177e4SLinus Torvalds 	if (on_link)
18011da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18021da177e4SLinus Torvalds 
18034e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
180497cac082SDavid S. Miller 	nrt->n = neigh_clone(neigh);
18051da177e4SLinus Torvalds 
180640e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18071da177e4SLinus Torvalds 		goto out;
18081da177e4SLinus Torvalds 
1809d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1810d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
181171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
181260592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
18138d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18148d71740cSTom Tucker 
18151da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18166e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1817e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds out:
1821e8599ff4SDavid S. Miller 	neigh_release(neigh);
18226e157b6aSDavid S. Miller }
18236e157b6aSDavid S. Miller 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  *	Misc support functions
18261da177e4SLinus Torvalds  */
18271da177e4SLinus Torvalds 
18281716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
182921efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18301da177e4SLinus Torvalds {
1831d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18328b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18338b96d22dSDavid S. Miller 					    ort->rt6i_table);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (rt) {
1836d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1837d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18388e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18391da177e4SLinus Torvalds 
18404e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18418e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1842defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1843d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18441da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18451da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18461da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1847d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18481da177e4SLinus Torvalds 
18494e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18501716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18511716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18521716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18531716a961SGao feng 			rt6_set_from(rt, ort);
18541716a961SGao feng 		else
18551716a961SGao feng 			rt6_clean_expires(rt);
18561da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18591da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18601da177e4SLinus Torvalds #endif
18610f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1862c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18631da177e4SLinus Torvalds 	}
18641da177e4SLinus Torvalds 	return rt;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
186770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1868efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1869b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1870b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
187170ceb4f5SYOSHIFUJI Hideaki {
187270ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
187370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1874c71099acSThomas Graf 	struct fib6_table *table;
187570ceb4f5SYOSHIFUJI Hideaki 
1876efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
187738308473SDavid S. Miller 	if (!table)
1878c71099acSThomas Graf 		return NULL;
1879c71099acSThomas Graf 
18805744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1881c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
188270ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
188370ceb4f5SYOSHIFUJI Hideaki 		goto out;
188470ceb4f5SYOSHIFUJI Hideaki 
1885d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1886d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
188770ceb4f5SYOSHIFUJI Hideaki 			continue;
188870ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
188970ceb4f5SYOSHIFUJI Hideaki 			continue;
189070ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
189170ceb4f5SYOSHIFUJI Hideaki 			continue;
1892d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
189370ceb4f5SYOSHIFUJI Hideaki 		break;
189470ceb4f5SYOSHIFUJI Hideaki 	}
189570ceb4f5SYOSHIFUJI Hideaki out:
18965744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
189770ceb4f5SYOSHIFUJI Hideaki 	return rt;
189870ceb4f5SYOSHIFUJI Hideaki }
189970ceb4f5SYOSHIFUJI Hideaki 
1900efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1901b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1902b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
190395c96174SEric Dumazet 					   unsigned int pref)
190470ceb4f5SYOSHIFUJI Hideaki {
190586872cb5SThomas Graf 	struct fib6_config cfg = {
190686872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1907238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
190886872cb5SThomas Graf 		.fc_ifindex	= ifindex,
190986872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
191086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
191186872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
191215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1913efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1914efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
191586872cb5SThomas Graf 	};
191670ceb4f5SYOSHIFUJI Hideaki 
19174e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19184e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
191986872cb5SThomas Graf 
1920e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1921e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
192286872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
192370ceb4f5SYOSHIFUJI Hideaki 
192486872cb5SThomas Graf 	ip6_route_add(&cfg);
192570ceb4f5SYOSHIFUJI Hideaki 
1926efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
192770ceb4f5SYOSHIFUJI Hideaki }
192870ceb4f5SYOSHIFUJI Hideaki #endif
192970ceb4f5SYOSHIFUJI Hideaki 
1930b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	struct rt6_info *rt;
1933c71099acSThomas Graf 	struct fib6_table *table;
19341da177e4SLinus Torvalds 
1935c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
193638308473SDavid S. Miller 	if (!table)
1937c71099acSThomas Graf 		return NULL;
19381da177e4SLinus Torvalds 
19395744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1940d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1941d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1942045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19431da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19441da177e4SLinus Torvalds 			break;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 	if (rt)
1947d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
19485744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
19491da177e4SLinus Torvalds 	return rt;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
1952b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1953ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1954ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19551da177e4SLinus Torvalds {
195686872cb5SThomas Graf 	struct fib6_config cfg = {
195786872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1958238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
195986872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
196086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
196186872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
196215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
19635578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1964c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
196586872cb5SThomas Graf 	};
19661da177e4SLinus Torvalds 
19674e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19681da177e4SLinus Torvalds 
196986872cb5SThomas Graf 	ip6_route_add(&cfg);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
19747b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19751da177e4SLinus Torvalds {
19761da177e4SLinus Torvalds 	struct rt6_info *rt;
1977c71099acSThomas Graf 	struct fib6_table *table;
1978c71099acSThomas Graf 
1979c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19807b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
198138308473SDavid S. Miller 	if (!table)
1982c71099acSThomas Graf 		return;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds restart:
1985c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1986d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19871da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1988d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1989c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1990e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19911da177e4SLinus Torvalds 			goto restart;
19921da177e4SLinus Torvalds 		}
19931da177e4SLinus Torvalds 	}
1994c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19951da177e4SLinus Torvalds }
19961da177e4SLinus Torvalds 
19975578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19985578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
199986872cb5SThomas Graf 				 struct fib6_config *cfg)
200086872cb5SThomas Graf {
200186872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
200286872cb5SThomas Graf 
200386872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
200486872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
200586872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
200686872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
200786872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
200886872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
200986872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
201086872cb5SThomas Graf 
20115578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2012f1243c2dSBenjamin Thery 
20134e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20144e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20154e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
201686872cb5SThomas Graf }
201786872cb5SThomas Graf 
20185578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20191da177e4SLinus Torvalds {
202086872cb5SThomas Graf 	struct fib6_config cfg;
20211da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20221da177e4SLinus Torvalds 	int err;
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	switch(cmd) {
20251da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20261da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2027af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
20281da177e4SLinus Torvalds 			return -EPERM;
20291da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20301da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20311da177e4SLinus Torvalds 		if (err)
20321da177e4SLinus Torvalds 			return -EFAULT;
20331da177e4SLinus Torvalds 
20345578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
203586872cb5SThomas Graf 
20361da177e4SLinus Torvalds 		rtnl_lock();
20371da177e4SLinus Torvalds 		switch (cmd) {
20381da177e4SLinus Torvalds 		case SIOCADDRT:
203986872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20401da177e4SLinus Torvalds 			break;
20411da177e4SLinus Torvalds 		case SIOCDELRT:
204286872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20431da177e4SLinus Torvalds 			break;
20441da177e4SLinus Torvalds 		default:
20451da177e4SLinus Torvalds 			err = -EINVAL;
20461da177e4SLinus Torvalds 		}
20471da177e4SLinus Torvalds 		rtnl_unlock();
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 		return err;
20503ff50b79SStephen Hemminger 	}
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	return -EINVAL;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /*
20561da177e4SLinus Torvalds  *	Drop the packet on the floor
20571da177e4SLinus Torvalds  */
20581da177e4SLinus Torvalds 
2059d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20601da177e4SLinus Torvalds {
2061612f09e8SYOSHIFUJI Hideaki 	int type;
2062adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2063612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2064612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20650660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
206645bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20673bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20683bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2069612f09e8SYOSHIFUJI Hideaki 			break;
2070612f09e8SYOSHIFUJI Hideaki 		}
2071612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2072612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20733bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20743bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2075612f09e8SYOSHIFUJI Hideaki 		break;
2076612f09e8SYOSHIFUJI Hideaki 	}
20773ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20781da177e4SLinus Torvalds 	kfree_skb(skb);
20791da177e4SLinus Torvalds 	return 0;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
20829ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20839ce8ade0SThomas Graf {
2084612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20859ce8ade0SThomas Graf }
20869ce8ade0SThomas Graf 
208720380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20881da177e4SLinus Torvalds {
2089adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2090612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
20936723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20946723ab54SDavid S. Miller 
20959ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20969ce8ade0SThomas Graf {
2097612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20989ce8ade0SThomas Graf }
20999ce8ade0SThomas Graf 
21009ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21019ce8ade0SThomas Graf {
2102adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2103612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21049ce8ade0SThomas Graf }
21059ce8ade0SThomas Graf 
21066723ab54SDavid S. Miller #endif
21076723ab54SDavid S. Miller 
21081da177e4SLinus Torvalds /*
21091da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21101da177e4SLinus Torvalds  */
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21131da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21148f031519SDavid S. Miller 				    bool anycast)
21151da177e4SLinus Torvalds {
2116c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21178b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2118f83c7790SDavid S. Miller 	int err;
21191da177e4SLinus Torvalds 
212038308473SDavid S. Miller 	if (!rt) {
2121f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21221da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
212340385653SBen Greear 	}
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	in6_dev_hold(idev);
21261da177e4SLinus Torvalds 
212711d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2128d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2129d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21301da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
213358c4fb86SYOSHIFUJI Hideaki 	if (anycast)
213458c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
213558c4fb86SYOSHIFUJI Hideaki 	else
21361da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21378ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2138f83c7790SDavid S. Miller 	if (err) {
2139d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2140f83c7790SDavid S. Miller 		return ERR_PTR(err);
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 
21434e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21441da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21455578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21461da177e4SLinus Torvalds 
2147d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	return rt;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
2152c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2153c3968a85SDaniel Walter 			struct rt6_info *rt,
2154b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2155c3968a85SDaniel Walter 			unsigned int prefs,
2156c3968a85SDaniel Walter 			struct in6_addr *saddr)
2157c3968a85SDaniel Walter {
2158c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2159c3968a85SDaniel Walter 	int err = 0;
2160c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21614e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2162c3968a85SDaniel Walter 	else
2163c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2164c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2165c3968a85SDaniel Walter 	return err;
2166c3968a85SDaniel Walter }
2167c3968a85SDaniel Walter 
2168c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2169c3968a85SDaniel Walter struct arg_dev_net_ip {
2170c3968a85SDaniel Walter 	struct net_device *dev;
2171c3968a85SDaniel Walter 	struct net *net;
2172c3968a85SDaniel Walter 	struct in6_addr *addr;
2173c3968a85SDaniel Walter };
2174c3968a85SDaniel Walter 
2175c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2176c3968a85SDaniel Walter {
2177c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2178c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2179c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2180c3968a85SDaniel Walter 
2181d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2182c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2183c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2184c3968a85SDaniel Walter 		/* remove prefsrc entry */
2185c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2186c3968a85SDaniel Walter 	}
2187c3968a85SDaniel Walter 	return 0;
2188c3968a85SDaniel Walter }
2189c3968a85SDaniel Walter 
2190c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2191c3968a85SDaniel Walter {
2192c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2193c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2194c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2195c3968a85SDaniel Walter 		.net = net,
2196c3968a85SDaniel Walter 		.addr = &ifp->addr,
2197c3968a85SDaniel Walter 	};
2198c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2199c3968a85SDaniel Walter }
2200c3968a85SDaniel Walter 
22018ed67789SDaniel Lezcano struct arg_dev_net {
22028ed67789SDaniel Lezcano 	struct net_device *dev;
22038ed67789SDaniel Lezcano 	struct net *net;
22048ed67789SDaniel Lezcano };
22058ed67789SDaniel Lezcano 
22061da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22071da177e4SLinus Torvalds {
2208bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2209bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22108ed67789SDaniel Lezcano 
2211d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2212c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22131da177e4SLinus Torvalds 		return -1;
2214c159d30cSDavid S. Miller 
22151da177e4SLinus Torvalds 	return 0;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
2218f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22191da177e4SLinus Torvalds {
22208ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22218ed67789SDaniel Lezcano 		.dev = dev,
22228ed67789SDaniel Lezcano 		.net = net,
22238ed67789SDaniel Lezcano 	};
22248ed67789SDaniel Lezcano 
22258ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22261e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22271da177e4SLinus Torvalds }
22281da177e4SLinus Torvalds 
222995c96174SEric Dumazet struct rt6_mtu_change_arg {
22301da177e4SLinus Torvalds 	struct net_device *dev;
223195c96174SEric Dumazet 	unsigned int mtu;
22321da177e4SLinus Torvalds };
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22351da177e4SLinus Torvalds {
22361da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22371da177e4SLinus Torvalds 	struct inet6_dev *idev;
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22401da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22411da177e4SLinus Torvalds 	   We still use this lock to block changes
22421da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22431da177e4SLinus Torvalds 	*/
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
224638308473SDavid S. Miller 	if (!idev)
22471da177e4SLinus Torvalds 		return 0;
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22501da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22511da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22521da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22531da177e4SLinus Torvalds 	 */
22541da177e4SLinus Torvalds 	/*
22551da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22561da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22571da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22581da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22591da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22601da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22611da177e4SLinus Torvalds 	   PMTU discouvery.
22621da177e4SLinus Torvalds 	 */
2263d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2264d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2265d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2266d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2267d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2268defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2269566cfd8fSSimon Arlott 	}
22701da177e4SLinus Torvalds 	return 0;
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
227395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22741da177e4SLinus Torvalds {
2275c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2276c71099acSThomas Graf 		.dev = dev,
2277c71099acSThomas Graf 		.mtu = mtu,
2278c71099acSThomas Graf 	};
22791da177e4SLinus Torvalds 
2280c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
2283ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22845176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
228586872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2286ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
228786872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
228886872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
228951ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
229086872cb5SThomas Graf };
229186872cb5SThomas Graf 
229286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
229386872cb5SThomas Graf 			      struct fib6_config *cfg)
22941da177e4SLinus Torvalds {
229586872cb5SThomas Graf 	struct rtmsg *rtm;
229686872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
229786872cb5SThomas Graf 	int err;
22981da177e4SLinus Torvalds 
229986872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
230086872cb5SThomas Graf 	if (err < 0)
230186872cb5SThomas Graf 		goto errout;
23021da177e4SLinus Torvalds 
230386872cb5SThomas Graf 	err = -EINVAL;
230486872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
230586872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
230686872cb5SThomas Graf 
230786872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
230886872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
230986872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
231086872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
231186872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2312ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
231386872cb5SThomas Graf 
2314ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2315ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2316b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2317b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
231886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
231986872cb5SThomas Graf 
2320ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2321ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2322ab79ad14SMaciej Żenczykowski 
232315e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
232486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23253b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
232686872cb5SThomas Graf 
232786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
232886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
232986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23301da177e4SLinus Torvalds 	}
233186872cb5SThomas Graf 
233286872cb5SThomas Graf 	if (tb[RTA_DST]) {
233386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
233486872cb5SThomas Graf 
233586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
233686872cb5SThomas Graf 			goto errout;
233786872cb5SThomas Graf 
233886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23391da177e4SLinus Torvalds 	}
234086872cb5SThomas Graf 
234186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
234286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
234386872cb5SThomas Graf 
234486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
234586872cb5SThomas Graf 			goto errout;
234686872cb5SThomas Graf 
234786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23481da177e4SLinus Torvalds 	}
234986872cb5SThomas Graf 
2350c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2351c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2352c3968a85SDaniel Walter 
235386872cb5SThomas Graf 	if (tb[RTA_OIF])
235486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
235586872cb5SThomas Graf 
235686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
235786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
235886872cb5SThomas Graf 
235986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
236086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
236186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23621da177e4SLinus Torvalds 	}
236386872cb5SThomas Graf 
236486872cb5SThomas Graf 	if (tb[RTA_TABLE])
236586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
236686872cb5SThomas Graf 
236751ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
236851ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
236951ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
237051ebd318SNicolas Dichtel 	}
237151ebd318SNicolas Dichtel 
237286872cb5SThomas Graf 	err = 0;
237386872cb5SThomas Graf errout:
237486872cb5SThomas Graf 	return err;
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
237751ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
237851ebd318SNicolas Dichtel {
237951ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
238051ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
238151ebd318SNicolas Dichtel 	int remaining;
238251ebd318SNicolas Dichtel 	int attrlen;
238351ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
238451ebd318SNicolas Dichtel 
238551ebd318SNicolas Dichtel beginning:
238651ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
238751ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
238851ebd318SNicolas Dichtel 
238951ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
239051ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
239151ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
239251ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
239351ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
239451ebd318SNicolas Dichtel 
239551ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
239651ebd318SNicolas Dichtel 		if (attrlen > 0) {
239751ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
239851ebd318SNicolas Dichtel 
239951ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
240051ebd318SNicolas Dichtel 			if (nla) {
240151ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
240251ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
240351ebd318SNicolas Dichtel 			}
240451ebd318SNicolas Dichtel 		}
240551ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
240651ebd318SNicolas Dichtel 		if (err) {
240751ebd318SNicolas Dichtel 			last_err = err;
240851ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
240951ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
241051ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
241151ebd318SNicolas Dichtel 			 */
241251ebd318SNicolas Dichtel 			if (add) {
241351ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
241451ebd318SNicolas Dichtel 				 * next hops that have been already added.
241551ebd318SNicolas Dichtel 				 */
241651ebd318SNicolas Dichtel 				add = 0;
241751ebd318SNicolas Dichtel 				goto beginning;
241851ebd318SNicolas Dichtel 			}
241951ebd318SNicolas Dichtel 		}
24201a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
24211a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
24221a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
24231a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
24241a72418bSNicolas Dichtel 		 */
24251a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
242651ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
242751ebd318SNicolas Dichtel 	}
242851ebd318SNicolas Dichtel 
242951ebd318SNicolas Dichtel 	return last_err;
243051ebd318SNicolas Dichtel }
243151ebd318SNicolas Dichtel 
2432c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
24331da177e4SLinus Torvalds {
243486872cb5SThomas Graf 	struct fib6_config cfg;
243586872cb5SThomas Graf 	int err;
24361da177e4SLinus Torvalds 
243786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
243886872cb5SThomas Graf 	if (err < 0)
243986872cb5SThomas Graf 		return err;
244086872cb5SThomas Graf 
244151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
244251ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
244351ebd318SNicolas Dichtel 	else
244486872cb5SThomas Graf 		return ip6_route_del(&cfg);
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
2447c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
24481da177e4SLinus Torvalds {
244986872cb5SThomas Graf 	struct fib6_config cfg;
245086872cb5SThomas Graf 	int err;
24511da177e4SLinus Torvalds 
245286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
245386872cb5SThomas Graf 	if (err < 0)
245486872cb5SThomas Graf 		return err;
245586872cb5SThomas Graf 
245651ebd318SNicolas Dichtel 	if (cfg.fc_mp)
245751ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
245851ebd318SNicolas Dichtel 	else
245986872cb5SThomas Graf 		return ip6_route_add(&cfg);
24601da177e4SLinus Torvalds }
24611da177e4SLinus Torvalds 
2462339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2463339bf98fSThomas Graf {
2464339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2465339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2466339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2467339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2468339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2469339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2470339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2471339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2472339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24736a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2474339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2475339bf98fSThomas Graf }
2476339bf98fSThomas Graf 
2477191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2478191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24790d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
248015e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
24817bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	struct rtmsg *rtm;
24841da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2485e3703b3dSThomas Graf 	long expires;
24869e762a4aSPatrick McHardy 	u32 table;
2487f2c31e32SEric Dumazet 	struct neighbour *n;
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24901da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24911da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24921da177e4SLinus Torvalds 			return 1;
24931da177e4SLinus Torvalds 		}
24941da177e4SLinus Torvalds 	}
24951da177e4SLinus Torvalds 
249615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
249738308473SDavid S. Miller 	if (!nlh)
249826932566SPatrick McHardy 		return -EMSGSIZE;
24992d7202bfSThomas Graf 
25002d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
25011da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
25021da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
25031da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
25041da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2505c71099acSThomas Graf 	if (rt->rt6i_table)
25069e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2507c71099acSThomas Graf 	else
25089e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
25099e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2510c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2511c78679e8SDavid S. Miller 		goto nla_put_failure;
2512ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2513ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2514ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2515ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2516ef2c7d7bSNicolas Dichtel 			break;
2517ef2c7d7bSNicolas Dichtel 		case -EACCES:
2518ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2519ef2c7d7bSNicolas Dichtel 			break;
2520b4949ab2SNicolas Dichtel 		case -EAGAIN:
2521b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2522b4949ab2SNicolas Dichtel 			break;
2523ef2c7d7bSNicolas Dichtel 		default:
25241da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2525ef2c7d7bSNicolas Dichtel 			break;
2526ef2c7d7bSNicolas Dichtel 		}
2527ef2c7d7bSNicolas Dichtel 	}
2528ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2529ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2530d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
25311da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
25321da177e4SLinus Torvalds 	else
25331da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
25341da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
25351da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
25361da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
25371da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
25381da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2539f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2540f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
25411da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2542f0396f60SDenis Ovsienko 		else
2543f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2544f0396f60SDenis Ovsienko 	}
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
25471da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	if (dst) {
2550c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2551c78679e8SDavid S. Miller 			goto nla_put_failure;
25521da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
25531da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2554c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2555c78679e8SDavid S. Miller 			goto nla_put_failure;
25561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25571da177e4SLinus Torvalds 	if (src) {
2558c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2559c78679e8SDavid S. Miller 			goto nla_put_failure;
25601da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2561c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2562c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2563c78679e8SDavid S. Miller 		goto nla_put_failure;
25641da177e4SLinus Torvalds #endif
25657bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25667bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
25677bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
25688229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
25697bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
25707bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25717bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25727bc570c8SYOSHIFUJI Hideaki 						return 0;
25737bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25747bc570c8SYOSHIFUJI Hideaki 				} else {
25757bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25767bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25777bc570c8SYOSHIFUJI Hideaki 				}
25787bc570c8SYOSHIFUJI Hideaki 			}
25797bc570c8SYOSHIFUJI Hideaki 		} else
25807bc570c8SYOSHIFUJI Hideaki #endif
2581c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2582c78679e8SDavid S. Miller 				goto nla_put_failure;
25837bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25841da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2585c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2586c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2587c78679e8SDavid S. Miller 			goto nla_put_failure;
2588c3968a85SDaniel Walter 	}
2589c3968a85SDaniel Walter 
2590c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2591c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25924e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2593c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2594c78679e8SDavid S. Miller 			goto nla_put_failure;
25951da177e4SLinus Torvalds 	}
25962d7202bfSThomas Graf 
2597defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25982d7202bfSThomas Graf 		goto nla_put_failure;
25992d7202bfSThomas Graf 
260097cac082SDavid S. Miller 	n = rt->n;
260194f826b8SEric Dumazet 	if (n) {
2602fdd6681dSAmerigo Wang 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0)
260394f826b8SEric Dumazet 			goto nla_put_failure;
260494f826b8SEric Dumazet 	}
26052d7202bfSThomas Graf 
2606c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2607c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2608c78679e8SDavid S. Miller 		goto nla_put_failure;
2609c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2610c78679e8SDavid S. Miller 		goto nla_put_failure;
26118253947eSLi Wei 
26128253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
261369cdf8f9SYOSHIFUJI Hideaki 
261487a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2615e3703b3dSThomas Graf 		goto nla_put_failure;
26161da177e4SLinus Torvalds 
26172d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
26182d7202bfSThomas Graf 
26192d7202bfSThomas Graf nla_put_failure:
262026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
262126932566SPatrick McHardy 	return -EMSGSIZE;
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
26271da177e4SLinus Torvalds 	int prefix;
26281da177e4SLinus Torvalds 
26292d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
26302d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
26311da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
26321da177e4SLinus Torvalds 	} else
26331da177e4SLinus Torvalds 		prefix = 0;
26341da177e4SLinus Torvalds 
2635191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2636191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
263715e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
26387bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
2641c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
26421da177e4SLinus Torvalds {
26433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2644ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26451da177e4SLinus Torvalds 	struct rt6_info *rt;
2646ab364a6fSThomas Graf 	struct sk_buff *skb;
2647ab364a6fSThomas Graf 	struct rtmsg *rtm;
26484c9483b2SDavid S. Miller 	struct flowi6 fl6;
264972331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2650ab364a6fSThomas Graf 
2651ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2652ab364a6fSThomas Graf 	if (err < 0)
2653ab364a6fSThomas Graf 		goto errout;
2654ab364a6fSThomas Graf 
2655ab364a6fSThomas Graf 	err = -EINVAL;
26564c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2657ab364a6fSThomas Graf 
2658ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2659ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2660ab364a6fSThomas Graf 			goto errout;
2661ab364a6fSThomas Graf 
26624e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2663ab364a6fSThomas Graf 	}
2664ab364a6fSThomas Graf 
2665ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2666ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2667ab364a6fSThomas Graf 			goto errout;
2668ab364a6fSThomas Graf 
26694e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2670ab364a6fSThomas Graf 	}
2671ab364a6fSThomas Graf 
2672ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2673ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2674ab364a6fSThomas Graf 
2675ab364a6fSThomas Graf 	if (tb[RTA_OIF])
267672331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2677ab364a6fSThomas Graf 
2678ab364a6fSThomas Graf 	if (iif) {
2679ab364a6fSThomas Graf 		struct net_device *dev;
268072331bc0SShmulik Ladkani 		int flags = 0;
268172331bc0SShmulik Ladkani 
26825578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2683ab364a6fSThomas Graf 		if (!dev) {
2684ab364a6fSThomas Graf 			err = -ENODEV;
2685ab364a6fSThomas Graf 			goto errout;
2686ab364a6fSThomas Graf 		}
268772331bc0SShmulik Ladkani 
268872331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
268972331bc0SShmulik Ladkani 
269072331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
269172331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
269272331bc0SShmulik Ladkani 
269372331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
269472331bc0SShmulik Ladkani 							       flags);
269572331bc0SShmulik Ladkani 	} else {
269672331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
269772331bc0SShmulik Ladkani 
269872331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2699ab364a6fSThomas Graf 	}
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
270238308473SDavid S. Miller 	if (!skb) {
270394e187c0SAmerigo Wang 		ip6_rt_put(rt);
2704ab364a6fSThomas Graf 		err = -ENOBUFS;
2705ab364a6fSThomas Graf 		goto errout;
2706ab364a6fSThomas Graf 	}
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27091da177e4SLinus Torvalds 	   through good chunk of routing engine.
27101da177e4SLinus Torvalds 	 */
2711459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
27121da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
27131da177e4SLinus Torvalds 
2714d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
27151da177e4SLinus Torvalds 
27164c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
271715e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
27187bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
27191da177e4SLinus Torvalds 	if (err < 0) {
2720ab364a6fSThomas Graf 		kfree_skb(skb);
2721ab364a6fSThomas Graf 		goto errout;
27221da177e4SLinus Torvalds 	}
27231da177e4SLinus Torvalds 
272415e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2725ab364a6fSThomas Graf errout:
27261da177e4SLinus Torvalds 	return err;
27271da177e4SLinus Torvalds }
27281da177e4SLinus Torvalds 
272986872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
27301da177e4SLinus Torvalds {
27311da177e4SLinus Torvalds 	struct sk_buff *skb;
27325578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2733528c4cebSDenis V. Lunev 	u32 seq;
2734528c4cebSDenis V. Lunev 	int err;
27350d51aa80SJamal Hadi Salim 
2736528c4cebSDenis V. Lunev 	err = -ENOBUFS;
273738308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
273886872cb5SThomas Graf 
2739339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
274038308473SDavid S. Miller 	if (!skb)
274121713ebcSThomas Graf 		goto errout;
27421da177e4SLinus Torvalds 
2743191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
274415e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
274526932566SPatrick McHardy 	if (err < 0) {
274626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
274726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
274826932566SPatrick McHardy 		kfree_skb(skb);
274926932566SPatrick McHardy 		goto errout;
275026932566SPatrick McHardy 	}
275115e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
27525578689aSDaniel Lezcano 		    info->nlh, gfp_any());
27531ce85fe4SPablo Neira Ayuso 	return;
275421713ebcSThomas Graf errout:
275521713ebcSThomas Graf 	if (err < 0)
27565578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
27598ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
27608ed67789SDaniel Lezcano 				unsigned long event, void *data)
27618ed67789SDaniel Lezcano {
27628ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2763c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27648ed67789SDaniel Lezcano 
27658ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2766d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27678ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
27688ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2769d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
27708ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2771d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27728ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27738ed67789SDaniel Lezcano #endif
27748ed67789SDaniel Lezcano 	}
27758ed67789SDaniel Lezcano 
27768ed67789SDaniel Lezcano 	return NOTIFY_OK;
27778ed67789SDaniel Lezcano }
27788ed67789SDaniel Lezcano 
27791da177e4SLinus Torvalds /*
27801da177e4SLinus Torvalds  *	/proc
27811da177e4SLinus Torvalds  */
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds struct rt6_proc_arg
27861da177e4SLinus Torvalds {
27871da177e4SLinus Torvalds 	char *buffer;
27881da177e4SLinus Torvalds 	int offset;
27891da177e4SLinus Torvalds 	int length;
27901da177e4SLinus Torvalds 	int skip;
27911da177e4SLinus Torvalds 	int len;
27921da177e4SLinus Torvalds };
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27951da177e4SLinus Torvalds {
279633120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
279769cce1d1SDavid S. Miller 	struct neighbour *n;
27981da177e4SLinus Torvalds 
27994b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
28024b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
28031da177e4SLinus Torvalds #else
280433120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
28051da177e4SLinus Torvalds #endif
280697cac082SDavid S. Miller 	n = rt->n;
280769cce1d1SDavid S. Miller 	if (n) {
280869cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
28091da177e4SLinus Torvalds 	} else {
281033120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
28111da177e4SLinus Torvalds 	}
281233120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2813d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2814d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2815d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
28161da177e4SLinus Torvalds 	return 0;
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds 
281933120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
28201da177e4SLinus Torvalds {
2821f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
282232b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
282333120b30SAlexey Dobriyan 	return 0;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
282633120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
282733120b30SAlexey Dobriyan {
2828de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2829f3db4851SDaniel Lezcano }
2830f3db4851SDaniel Lezcano 
283133120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
283233120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
283333120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
283433120b30SAlexey Dobriyan 	.read		= seq_read,
283533120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2836b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
283733120b30SAlexey Dobriyan };
283833120b30SAlexey Dobriyan 
28391da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
28401da177e4SLinus Torvalds {
284169ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
28421da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
284369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
284469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
284569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
284669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
284769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2848fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
284969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds 	return 0;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28551da177e4SLinus Torvalds {
2856de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
285769ddb805SDaniel Lezcano }
285869ddb805SDaniel Lezcano 
28599a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28601da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28611da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28621da177e4SLinus Torvalds 	.read	 = seq_read,
28631da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2864b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28651da177e4SLinus Torvalds };
28661da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds static
28718d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
28721da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28731da177e4SLinus Torvalds {
2874c486da34SLucian Adrian Grijincu 	struct net *net;
2875c486da34SLucian Adrian Grijincu 	int delay;
2876c486da34SLucian Adrian Grijincu 	if (!write)
2877c486da34SLucian Adrian Grijincu 		return -EINVAL;
2878c486da34SLucian Adrian Grijincu 
2879c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2880c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28818d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28825b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28831da177e4SLinus Torvalds 	return 0;
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
2886760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
28871da177e4SLinus Torvalds 	{
28881da177e4SLinus Torvalds 		.procname	=	"flush",
28894990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28901da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
289189c8b3a1SDave Jones 		.mode		=	0200,
28926d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28931da177e4SLinus Torvalds 	},
28941da177e4SLinus Torvalds 	{
28951da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28969a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28971da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28981da177e4SLinus Torvalds 		.mode		=	0644,
28996d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29001da177e4SLinus Torvalds 	},
29011da177e4SLinus Torvalds 	{
29021da177e4SLinus Torvalds 		.procname	=	"max_size",
29034990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
29041da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29051da177e4SLinus Torvalds 		.mode		=	0644,
29066d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29071da177e4SLinus Torvalds 	},
29081da177e4SLinus Torvalds 	{
29091da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
29104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29111da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29121da177e4SLinus Torvalds 		.mode		=	0644,
29136d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29141da177e4SLinus Torvalds 	},
29151da177e4SLinus Torvalds 	{
29161da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
29174990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
29181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29191da177e4SLinus Torvalds 		.mode		=	0644,
29206d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29211da177e4SLinus Torvalds 	},
29221da177e4SLinus Torvalds 	{
29231da177e4SLinus Torvalds 		.procname	=	"gc_interval",
29244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
29251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29261da177e4SLinus Torvalds 		.mode		=	0644,
29276d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29281da177e4SLinus Torvalds 	},
29291da177e4SLinus Torvalds 	{
29301da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
29314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
29321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29331da177e4SLinus Torvalds 		.mode		=	0644,
2934f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29351da177e4SLinus Torvalds 	},
29361da177e4SLinus Torvalds 	{
29371da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
29384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
29391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29401da177e4SLinus Torvalds 		.mode		=	0644,
29416d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29421da177e4SLinus Torvalds 	},
29431da177e4SLinus Torvalds 	{
29441da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
29454990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
29461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29471da177e4SLinus Torvalds 		.mode		=	0644,
2948f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29491da177e4SLinus Torvalds 	},
29501da177e4SLinus Torvalds 	{
29511da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29524990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29531da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29541da177e4SLinus Torvalds 		.mode		=	0644,
29556d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29561da177e4SLinus Torvalds 	},
2957f8572d8fSEric W. Biederman 	{ }
29581da177e4SLinus Torvalds };
29591da177e4SLinus Torvalds 
29602c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2961760f2d01SDaniel Lezcano {
2962760f2d01SDaniel Lezcano 	struct ctl_table *table;
2963760f2d01SDaniel Lezcano 
2964760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2965760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2966760f2d01SDaniel Lezcano 			GFP_KERNEL);
29675ee09105SYOSHIFUJI Hideaki 
29685ee09105SYOSHIFUJI Hideaki 	if (table) {
29695ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2970c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
297186393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29725ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29735ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29745ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29755ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29765ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29775ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29785ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29799c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2980464dc801SEric W. Biederman 
2981464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2982464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2983464dc801SEric W. Biederman 			table[0].procname = NULL;
29845ee09105SYOSHIFUJI Hideaki 	}
29855ee09105SYOSHIFUJI Hideaki 
2986760f2d01SDaniel Lezcano 	return table;
2987760f2d01SDaniel Lezcano }
29881da177e4SLinus Torvalds #endif
29891da177e4SLinus Torvalds 
29902c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2991cdb18761SDaniel Lezcano {
2992633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29938ed67789SDaniel Lezcano 
299486393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
299586393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2996f2fc6a54SBenjamin Thery 
2997fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2998fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2999fc66f95cSEric Dumazet 
30008ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
30018ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
30028ed67789SDaniel Lezcano 					   GFP_KERNEL);
30038ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
3004fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
3005d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
30068ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
3007d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
300862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
300962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30108ed67789SDaniel Lezcano 
30118ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30128ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
30138ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
30148ed67789SDaniel Lezcano 					       GFP_KERNEL);
301568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
301668fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3017d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
30188ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3019d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
302062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
302162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30228ed67789SDaniel Lezcano 
30238ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
30248ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
30258ed67789SDaniel Lezcano 					       GFP_KERNEL);
302668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
302768fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3028d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
30298ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3030d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
303162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
303262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30338ed67789SDaniel Lezcano #endif
30348ed67789SDaniel Lezcano 
3035b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3036b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3037b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3038b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3039b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3040b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3041b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3042b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3043b339a47cSPeter Zijlstra 
30446891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
30456891a346SBenjamin Thery 
30468ed67789SDaniel Lezcano 	ret = 0;
30478ed67789SDaniel Lezcano out:
30488ed67789SDaniel Lezcano 	return ret;
3049f2fc6a54SBenjamin Thery 
305068fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
305168fffc67SPeter Zijlstra out_ip6_prohibit_entry:
305268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
305368fffc67SPeter Zijlstra out_ip6_null_entry:
305468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
305568fffc67SPeter Zijlstra #endif
3056fc66f95cSEric Dumazet out_ip6_dst_entries:
3057fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3058f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3059f2fc6a54SBenjamin Thery 	goto out;
3060cdb18761SDaniel Lezcano }
3061cdb18761SDaniel Lezcano 
30622c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3063cdb18761SDaniel Lezcano {
30648ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30658ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30668ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30678ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30688ed67789SDaniel Lezcano #endif
306941bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3070cdb18761SDaniel Lezcano }
3071cdb18761SDaniel Lezcano 
3072d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3073d189634eSThomas Graf {
3074d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3075d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
3076d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
3077d189634eSThomas Graf #endif
3078d189634eSThomas Graf 	return 0;
3079d189634eSThomas Graf }
3080d189634eSThomas Graf 
3081d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3082d189634eSThomas Graf {
3083d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3084d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
3085d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
3086d189634eSThomas Graf #endif
3087d189634eSThomas Graf }
3088d189634eSThomas Graf 
3089cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3090cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3091cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3092cdb18761SDaniel Lezcano };
3093cdb18761SDaniel Lezcano 
3094c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3095c3426b47SDavid S. Miller {
3096c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3097c3426b47SDavid S. Miller 
3098c3426b47SDavid S. Miller 	if (!bp)
3099c3426b47SDavid S. Miller 		return -ENOMEM;
3100c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3101c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3102c3426b47SDavid S. Miller 	return 0;
3103c3426b47SDavid S. Miller }
3104c3426b47SDavid S. Miller 
3105c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3106c3426b47SDavid S. Miller {
3107c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3108c3426b47SDavid S. Miller 
3109c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
311056a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3111c3426b47SDavid S. Miller 	kfree(bp);
3112c3426b47SDavid S. Miller }
3113c3426b47SDavid S. Miller 
31142b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3115c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3116c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3117c3426b47SDavid S. Miller };
3118c3426b47SDavid S. Miller 
3119d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3120d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3121d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3122d189634eSThomas Graf };
3123d189634eSThomas Graf 
31248ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
31258ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
31268ed67789SDaniel Lezcano 	.priority = 0,
31278ed67789SDaniel Lezcano };
31288ed67789SDaniel Lezcano 
3129433d49c3SDaniel Lezcano int __init ip6_route_init(void)
31301da177e4SLinus Torvalds {
3131433d49c3SDaniel Lezcano 	int ret;
3132433d49c3SDaniel Lezcano 
31339a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
31349a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
31359a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
31369a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
31379a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3138c19a28e1SFernando Carrijo 		goto out;
313914e50e57SDavid S. Miller 
3140fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
31418ed67789SDaniel Lezcano 	if (ret)
3142bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3143bdb3289fSDaniel Lezcano 
3144c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3145c3426b47SDavid S. Miller 	if (ret)
3146e8803b6cSDavid S. Miller 		goto out_dst_entries;
31472a0c451aSThomas Graf 
31487e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
31497e52b33bSDavid S. Miller 	if (ret)
31507e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3151c3426b47SDavid S. Miller 
31525dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31535dc121e9SArnaud Ebalard 
31548ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31558ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31568ed67789SDaniel Lezcano 	 * manually for init_net */
3157d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31588ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3159bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3160d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31618ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3162d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31638ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3164bdb3289fSDaniel Lezcano   #endif
3165e8803b6cSDavid S. Miller 	ret = fib6_init();
3166433d49c3SDaniel Lezcano 	if (ret)
31678ed67789SDaniel Lezcano 		goto out_register_subsys;
3168433d49c3SDaniel Lezcano 
3169433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3170433d49c3SDaniel Lezcano 	if (ret)
3171e8803b6cSDavid S. Miller 		goto out_fib6_init;
3172c35b7e72SDaniel Lezcano 
3173433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3174433d49c3SDaniel Lezcano 	if (ret)
3175433d49c3SDaniel Lezcano 		goto xfrm6_init;
31767e5449c2SDaniel Lezcano 
3177d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3178d189634eSThomas Graf 	if (ret)
3179d189634eSThomas Graf 		goto fib6_rules_init;
3180d189634eSThomas Graf 
3181433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3182c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3183c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3184c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3185d189634eSThomas Graf 		goto out_register_late_subsys;
3186433d49c3SDaniel Lezcano 
31878ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3188cdb18761SDaniel Lezcano 	if (ret)
3189d189634eSThomas Graf 		goto out_register_late_subsys;
31908ed67789SDaniel Lezcano 
3191433d49c3SDaniel Lezcano out:
3192433d49c3SDaniel Lezcano 	return ret;
3193433d49c3SDaniel Lezcano 
3194d189634eSThomas Graf out_register_late_subsys:
3195d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3196433d49c3SDaniel Lezcano fib6_rules_init:
3197433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3198433d49c3SDaniel Lezcano xfrm6_init:
3199433d49c3SDaniel Lezcano 	xfrm6_fini();
32002a0c451aSThomas Graf out_fib6_init:
32012a0c451aSThomas Graf 	fib6_gc_cleanup();
32028ed67789SDaniel Lezcano out_register_subsys:
32038ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
32047e52b33bSDavid S. Miller out_register_inetpeer:
32057e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3206fc66f95cSEric Dumazet out_dst_entries:
3207fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3208433d49c3SDaniel Lezcano out_kmem_cache:
3209f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3210433d49c3SDaniel Lezcano 	goto out;
32111da177e4SLinus Torvalds }
32121da177e4SLinus Torvalds 
32131da177e4SLinus Torvalds void ip6_route_cleanup(void)
32141da177e4SLinus Torvalds {
32158ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3216d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3217101367c2SThomas Graf 	fib6_rules_cleanup();
32181da177e4SLinus Torvalds 	xfrm6_fini();
32191da177e4SLinus Torvalds 	fib6_gc_cleanup();
3220c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
32218ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
322241bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3223f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
32241da177e4SLinus Torvalds }
3225