xref: /openbmc/linux/net/ipv6/route.c (revision 30f78d8ebf7f514801e71b88a10c948275168518)
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 
68afc154e9SHannes Frederic Sowa enum rt6_nud_state {
697e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
707e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
717e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
72afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
73afc154e9SHannes Frederic Sowa };
74afc154e9SHannes Frederic Sowa 
751716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
7621efcfa0SEric Dumazet 				    const struct in6_addr *dest);
771da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
780dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
79ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
831da177e4SLinus Torvalds 				       struct net_device *dev, int how);
84569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
871da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
887150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
897150aedeSKamala R static int		ip6_pkt_prohibit_out(struct sk_buff *skb);
901da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
916700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
926700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
936700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
946700c270SDavid S. Miller 					struct sk_buff *skb);
9552bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
961da177e4SLinus Torvalds 
9770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
98efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
99b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
100b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
10195c96174SEric Dumazet 					   unsigned int pref);
102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
103b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
104b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
10570ceb4f5SYOSHIFUJI Hideaki #endif
10670ceb4f5SYOSHIFUJI Hideaki 
107e8243534Sstephen hemminger static void rt6_bind_peer(struct rt6_info *rt, int create)
108e8243534Sstephen hemminger {
109e8243534Sstephen hemminger 	struct inet_peer_base *base;
110e8243534Sstephen hemminger 	struct inet_peer *peer;
111e8243534Sstephen hemminger 
112e8243534Sstephen hemminger 	base = inetpeer_base_ptr(rt->_rt6i_peer);
113e8243534Sstephen hemminger 	if (!base)
114e8243534Sstephen hemminger 		return;
115e8243534Sstephen hemminger 
116e8243534Sstephen hemminger 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
117e8243534Sstephen hemminger 	if (peer) {
118e8243534Sstephen hemminger 		if (!rt6_set_peer(rt, peer))
119e8243534Sstephen hemminger 			inet_putpeer(peer);
120e8243534Sstephen hemminger 	}
121e8243534Sstephen hemminger }
122e8243534Sstephen hemminger 
123e8243534Sstephen hemminger static struct inet_peer *__rt6_get_peer(struct rt6_info *rt, int create)
124e8243534Sstephen hemminger {
125e8243534Sstephen hemminger 	if (rt6_has_peer(rt))
126e8243534Sstephen hemminger 		return rt6_peer_ptr(rt);
127e8243534Sstephen hemminger 
128e8243534Sstephen hemminger 	rt6_bind_peer(rt, create);
129e8243534Sstephen hemminger 	return (rt6_has_peer(rt) ? rt6_peer_ptr(rt) : NULL);
130e8243534Sstephen hemminger }
131e8243534Sstephen hemminger 
132e8243534Sstephen hemminger static struct inet_peer *rt6_get_peer_create(struct rt6_info *rt)
133e8243534Sstephen hemminger {
134e8243534Sstephen hemminger 	return __rt6_get_peer(rt, 1);
135e8243534Sstephen hemminger }
136e8243534Sstephen hemminger 
13706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
13806582540SDavid S. Miller {
13906582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14006582540SDavid S. Miller 	struct inet_peer *peer;
14106582540SDavid S. Miller 	u32 *p = NULL;
14206582540SDavid S. Miller 
1438e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1448e2ec639SYan, Zheng 		return NULL;
1458e2ec639SYan, Zheng 
146fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
14706582540SDavid S. Miller 	if (peer) {
14806582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
14906582540SDavid S. Miller 		unsigned long prev, new;
15006582540SDavid S. Miller 
15106582540SDavid S. Miller 		p = peer->metrics;
152e5fd387aSMichal Kubeček 		if (inet_metrics_new(peer) ||
153e5fd387aSMichal Kubeček 		    (old & DST_METRICS_FORCE_OVERWRITE))
15406582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
15506582540SDavid S. Miller 
15606582540SDavid S. Miller 		new = (unsigned long) p;
15706582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
15806582540SDavid S. Miller 
15906582540SDavid S. Miller 		if (prev != old) {
16006582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
16106582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
16206582540SDavid S. Miller 				p = NULL;
16306582540SDavid S. Miller 		}
16406582540SDavid S. Miller 	}
16506582540SDavid S. Miller 	return p;
16606582540SDavid S. Miller }
16706582540SDavid S. Miller 
168f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
169f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
170f894cbf8SDavid S. Miller 					     const void *daddr)
17139232973SDavid S. Miller {
17239232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
17339232973SDavid S. Miller 
174a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
17539232973SDavid S. Miller 		return (const void *) p;
176f894cbf8SDavid S. Miller 	else if (skb)
177f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
17839232973SDavid S. Miller 	return daddr;
17939232973SDavid S. Miller }
18039232973SDavid S. Miller 
181f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
182f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
183f894cbf8SDavid S. Miller 					  const void *daddr)
184d3aaeb38SDavid S. Miller {
18539232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
18639232973SDavid S. Miller 	struct neighbour *n;
18739232973SDavid S. Miller 
188f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1898e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
190f83c7790SDavid S. Miller 	if (n)
191f83c7790SDavid S. Miller 		return n;
192f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
193f83c7790SDavid S. Miller }
194f83c7790SDavid S. Miller 
1959a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1961da177e4SLinus Torvalds 	.family			=	AF_INET6,
19709640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1981da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1991da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2001da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2010dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
202ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
20306582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
2041da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2051da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2061da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2071da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2081da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2096e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2101ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
211d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
2121da177e4SLinus Torvalds };
2131da177e4SLinus Torvalds 
214ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
215ec831ea7SRoland Dreier {
216618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
217618f9bc7SSteffen Klassert 
218618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
219ec831ea7SRoland Dreier }
220ec831ea7SRoland Dreier 
2216700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2226700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
22314e50e57SDavid S. Miller {
22414e50e57SDavid S. Miller }
22514e50e57SDavid S. Miller 
2266700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2276700c270SDavid S. Miller 				      struct sk_buff *skb)
228b587ee3bSDavid S. Miller {
229b587ee3bSDavid S. Miller }
230b587ee3bSDavid S. Miller 
2310972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
2320972ddb2SHeld Bernhard 					 unsigned long old)
2330972ddb2SHeld Bernhard {
2340972ddb2SHeld Bernhard 	return NULL;
2350972ddb2SHeld Bernhard }
2360972ddb2SHeld Bernhard 
23714e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
23814e50e57SDavid S. Miller 	.family			=	AF_INET6,
23909640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
24014e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
24114e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
242ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
243214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
24414e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
245b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2460972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
247d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
24814e50e57SDavid S. Miller };
24914e50e57SDavid S. Miller 
25062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
25114edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
25262fa8a84SDavid S. Miller };
25362fa8a84SDavid S. Miller 
254fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2551da177e4SLinus Torvalds 	.dst = {
2561da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2571da177e4SLinus Torvalds 		.__use		= 1,
2582c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2591da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2601da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2611da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2621da177e4SLinus Torvalds 	},
2631da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2644f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2651da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2661da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2671da177e4SLinus Torvalds };
2681da177e4SLinus Torvalds 
269101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
270101367c2SThomas Graf 
271fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
272101367c2SThomas Graf 	.dst = {
273101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
274101367c2SThomas Graf 		.__use		= 1,
2752c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
276101367c2SThomas Graf 		.error		= -EACCES,
2779ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2789ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
279101367c2SThomas Graf 	},
280101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2814f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
282101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
283101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
284101367c2SThomas Graf };
285101367c2SThomas Graf 
286fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
287101367c2SThomas Graf 	.dst = {
288101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
289101367c2SThomas Graf 		.__use		= 1,
2902c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
291101367c2SThomas Graf 		.error		= -EINVAL,
292352e512cSHerbert Xu 		.input		= dst_discard,
293352e512cSHerbert Xu 		.output		= dst_discard,
294101367c2SThomas Graf 	},
295101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2964f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
297101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
298101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
299101367c2SThomas Graf };
300101367c2SThomas Graf 
301101367c2SThomas Graf #endif
302101367c2SThomas Graf 
3031da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
30497bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
305957c665fSDavid S. Miller 					     struct net_device *dev,
3068b96d22dSDavid S. Miller 					     int flags,
3078b96d22dSDavid S. Miller 					     struct fib6_table *table)
3081da177e4SLinus Torvalds {
30997bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
3106f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
311cf911662SDavid S. Miller 
31297bab73fSDavid S. Miller 	if (rt) {
3138104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
3148104891bSSteffen Klassert 
3158104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
3168b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
317ca4c3fc2Sfan.du 		rt->rt6i_genid = rt_genid_ipv6(net);
31851ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
31997bab73fSDavid S. Miller 	}
320cf911662SDavid S. Miller 	return rt;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3261da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
327ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
3281da177e4SLinus Torvalds 
3298e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
3308e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3318e2ec639SYan, Zheng 
33238308473SDavid S. Miller 	if (idev) {
3331da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3341da177e4SLinus Torvalds 		in6_dev_put(idev);
3351da177e4SLinus Torvalds 	}
3361716a961SGao feng 
337ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
338ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3391716a961SGao feng 
34097bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
34197bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
342b3419363SDavid S. Miller 		inet_putpeer(peer);
343b3419363SDavid S. Miller 	}
344b3419363SDavid S. Miller }
345b3419363SDavid S. Miller 
3461da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3471da177e4SLinus Torvalds 			   int how)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3501da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3515a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
352c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3531da177e4SLinus Torvalds 
35497cac082SDavid S. Miller 	if (dev != loopback_dev) {
35597cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3565a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3575a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
35838308473SDavid S. Miller 			if (loopback_idev) {
3591da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3601da177e4SLinus Torvalds 				in6_dev_put(idev);
3611da177e4SLinus Torvalds 			}
3621da177e4SLinus Torvalds 		}
36397cac082SDavid S. Miller 	}
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
366a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3671da177e4SLinus Torvalds {
3681716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3691716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
370a50feda5SEric Dumazet 			return true;
3711716a961SGao feng 	} else if (rt->dst.from) {
3723fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3731716a961SGao feng 	}
374a50feda5SEric Dumazet 	return false;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
37751ebd318SNicolas Dichtel /* Multipath route selection:
37851ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
37951ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
38051ebd318SNicolas Dichtel  */
38151ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
38251ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
38351ebd318SNicolas Dichtel {
38451ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
38551ebd318SNicolas Dichtel 
386c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
387c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
38851ebd318SNicolas Dichtel 
38951ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
39051ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
39151ebd318SNicolas Dichtel 	case IPPROTO_UDP:
39251ebd318SNicolas Dichtel 	case IPPROTO_TCP:
39351ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
394b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
395b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
39651ebd318SNicolas Dichtel 		break;
39751ebd318SNicolas Dichtel 
39851ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
399b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
400b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
40151ebd318SNicolas Dichtel 		break;
40251ebd318SNicolas Dichtel 	}
40351ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
404b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
40551ebd318SNicolas Dichtel 
40651ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
40751ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
40851ebd318SNicolas Dichtel 	return val % candidate_count;
40951ebd318SNicolas Dichtel }
41051ebd318SNicolas Dichtel 
41151ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
41252bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
41352bd4c0cSNicolas Dichtel 					     int strict)
41451ebd318SNicolas Dichtel {
41551ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
41651ebd318SNicolas Dichtel 	int route_choosen;
41751ebd318SNicolas Dichtel 
41851ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
41951ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
42051ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
42151ebd318SNicolas Dichtel 	 */
42251ebd318SNicolas Dichtel 	if (route_choosen)
42351ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
42451ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
42551ebd318SNicolas Dichtel 			route_choosen--;
42651ebd318SNicolas Dichtel 			if (route_choosen == 0) {
42752bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
42852bd4c0cSNicolas Dichtel 					break;
42951ebd318SNicolas Dichtel 				match = sibling;
43051ebd318SNicolas Dichtel 				break;
43151ebd318SNicolas Dichtel 			}
43251ebd318SNicolas Dichtel 		}
43351ebd318SNicolas Dichtel 	return match;
43451ebd318SNicolas Dichtel }
43551ebd318SNicolas Dichtel 
4361da177e4SLinus Torvalds /*
437c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4381da177e4SLinus Torvalds  */
4391da177e4SLinus Torvalds 
4408ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4418ed67789SDaniel Lezcano 						    struct rt6_info *rt,
442b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4431da177e4SLinus Torvalds 						    int oif,
444d420895eSYOSHIFUJI Hideaki 						    int flags)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4471da177e4SLinus Torvalds 	struct rt6_info *sprt;
4481da177e4SLinus Torvalds 
449dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
450dd3abc4eSYOSHIFUJI Hideaki 		goto out;
451dd3abc4eSYOSHIFUJI Hideaki 
452d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
453d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
454dd3abc4eSYOSHIFUJI Hideaki 
455dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4561da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4571da177e4SLinus Torvalds 				return sprt;
4581da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
45938308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4601da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
461d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4621da177e4SLinus Torvalds 						continue;
4631da177e4SLinus Torvalds 					if (local && (!oif ||
4641da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4651da177e4SLinus Torvalds 						continue;
4661da177e4SLinus Torvalds 				}
4671da177e4SLinus Torvalds 				local = sprt;
4681da177e4SLinus Torvalds 			}
469dd3abc4eSYOSHIFUJI Hideaki 		} else {
470dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
471dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
472dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
473dd3abc4eSYOSHIFUJI Hideaki 		}
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
476dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4771da177e4SLinus Torvalds 		if (local)
4781da177e4SLinus Torvalds 			return local;
4791da177e4SLinus Torvalds 
480d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4818ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4821da177e4SLinus Torvalds 	}
483dd3abc4eSYOSHIFUJI Hideaki out:
4841da177e4SLinus Torvalds 	return rt;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
48727097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
488c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
489c2f17e82SHannes Frederic Sowa 	struct work_struct work;
490c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
491c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
492c2f17e82SHannes Frederic Sowa };
493c2f17e82SHannes Frederic Sowa 
494c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
495c2f17e82SHannes Frederic Sowa {
496c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
497c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
498c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
499c2f17e82SHannes Frederic Sowa 
500c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
501c2f17e82SHannes Frederic Sowa 	ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
502c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
503c2f17e82SHannes Frederic Sowa 	kfree(w);
504c2f17e82SHannes Frederic Sowa }
505c2f17e82SHannes Frederic Sowa 
50627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
50727097255SYOSHIFUJI Hideaki {
508f2c31e32SEric Dumazet 	struct neighbour *neigh;
50927097255SYOSHIFUJI Hideaki 	/*
51027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
51127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
51227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
51327097255SYOSHIFUJI Hideaki 	 *
51427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
51527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
51627097255SYOSHIFUJI Hideaki 	 */
5172152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
518fdd6681dSAmerigo Wang 		return;
5192152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5202152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
5212152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5222152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
5232152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
5242152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
5257ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
5262152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5272152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
52852e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
529c2f17e82SHannes Frederic Sowa 		struct __rt6_probe_work *work;
53027097255SYOSHIFUJI Hideaki 
531c2f17e82SHannes Frederic Sowa 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
532c2f17e82SHannes Frederic Sowa 
533c2f17e82SHannes Frederic Sowa 		if (neigh && work)
5347e980569SJiri Benc 			__neigh_set_probe_once(neigh);
5352152caeaSYOSHIFUJI Hideaki / 吉藤英明 
536c2f17e82SHannes Frederic Sowa 		if (neigh)
537c2f17e82SHannes Frederic Sowa 			write_unlock(&neigh->lock);
538c2f17e82SHannes Frederic Sowa 
539c2f17e82SHannes Frederic Sowa 		if (work) {
540c2f17e82SHannes Frederic Sowa 			INIT_WORK(&work->work, rt6_probe_deferred);
541c2f17e82SHannes Frederic Sowa 			work->target = rt->rt6i_gateway;
542c2f17e82SHannes Frederic Sowa 			dev_hold(rt->dst.dev);
543c2f17e82SHannes Frederic Sowa 			work->dev = rt->dst.dev;
544c2f17e82SHannes Frederic Sowa 			schedule_work(&work->work);
545c2f17e82SHannes Frederic Sowa 		}
546f2c31e32SEric Dumazet 	} else {
5472152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5482152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
54927097255SYOSHIFUJI Hideaki 	}
5502152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
551f2c31e32SEric Dumazet }
55227097255SYOSHIFUJI Hideaki #else
55327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
55427097255SYOSHIFUJI Hideaki {
55527097255SYOSHIFUJI Hideaki }
55627097255SYOSHIFUJI Hideaki #endif
55727097255SYOSHIFUJI Hideaki 
5581da177e4SLinus Torvalds /*
559554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5601da177e4SLinus Torvalds  */
561b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5621da177e4SLinus Torvalds {
563d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
564161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
565554cfb7eSYOSHIFUJI Hideaki 		return 2;
566161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
567161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
568161980f4SDavid S. Miller 		return 1;
569554cfb7eSYOSHIFUJI Hideaki 	return 0;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5731da177e4SLinus Torvalds {
574f2c31e32SEric Dumazet 	struct neighbour *neigh;
575afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
576f2c31e32SEric Dumazet 
5774d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5784d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
579afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
580145a3621SYOSHIFUJI Hideaki / 吉藤英明 
581145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
582145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
583145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
584145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
585554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
586afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
587398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
588a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
589afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
5907e980569SJiri Benc 		else
5917e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
592398bcbebSYOSHIFUJI Hideaki #endif
593145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
594afc154e9SHannes Frederic Sowa 	} else {
595afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
5967e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
597a5a81f0bSPaul Marks 	}
598145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
599145a3621SYOSHIFUJI Hideaki / 吉藤英明 
600a5a81f0bSPaul Marks 	return ret;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
603554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
604554cfb7eSYOSHIFUJI Hideaki 			   int strict)
605554cfb7eSYOSHIFUJI Hideaki {
606a5a81f0bSPaul Marks 	int m;
6074d0c5911SYOSHIFUJI Hideaki 
6084d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
60977d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
610afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
611ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
612ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
613ebacaaa0SYOSHIFUJI Hideaki #endif
614afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
615afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
616afc154e9SHannes Frederic Sowa 		if (n < 0)
617afc154e9SHannes Frederic Sowa 			return n;
618afc154e9SHannes Frederic Sowa 	}
619554cfb7eSYOSHIFUJI Hideaki 	return m;
620554cfb7eSYOSHIFUJI Hideaki }
621554cfb7eSYOSHIFUJI Hideaki 
622f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
623afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
624afc154e9SHannes Frederic Sowa 				   bool *do_rr)
625554cfb7eSYOSHIFUJI Hideaki {
626554cfb7eSYOSHIFUJI Hideaki 	int m;
627afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
628554cfb7eSYOSHIFUJI Hideaki 
629554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
630f11e6659SDavid S. Miller 		goto out;
631554cfb7eSYOSHIFUJI Hideaki 
632554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6337e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
634afc154e9SHannes Frederic Sowa 		match_do_rr = true;
635afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6367e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
637f11e6659SDavid S. Miller 		goto out;
6381da177e4SLinus Torvalds 	}
639f11e6659SDavid S. Miller 
640afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
641afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
642afc154e9SHannes Frederic Sowa 
6437e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
644afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
645afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
646afc154e9SHannes Frederic Sowa 		*mpri = m;
647afc154e9SHannes Frederic Sowa 		match = rt;
648afc154e9SHannes Frederic Sowa 	}
649f11e6659SDavid S. Miller out:
650f11e6659SDavid S. Miller 	return match;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
653f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
654f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
655afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
656afc154e9SHannes Frederic Sowa 				     bool *do_rr)
657f11e6659SDavid S. Miller {
658f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
659f11e6659SDavid S. Miller 	int mpri = -1;
660f11e6659SDavid S. Miller 
661f11e6659SDavid S. Miller 	match = NULL;
662f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
663d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
664afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
665f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
666d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
667afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
668f11e6659SDavid S. Miller 
669f11e6659SDavid S. Miller 	return match;
670f11e6659SDavid S. Miller }
671f11e6659SDavid S. Miller 
672f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
673f11e6659SDavid S. Miller {
674f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6758ed67789SDaniel Lezcano 	struct net *net;
676afc154e9SHannes Frederic Sowa 	bool do_rr = false;
677f11e6659SDavid S. Miller 
678f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
679f11e6659SDavid S. Miller 	if (!rt0)
680f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
681f11e6659SDavid S. Miller 
682afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
683afc154e9SHannes Frederic Sowa 			     &do_rr);
684f11e6659SDavid S. Miller 
685afc154e9SHannes Frederic Sowa 	if (do_rr) {
686d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
687f11e6659SDavid S. Miller 
688554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
689f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
690f11e6659SDavid S. Miller 			next = fn->leaf;
691f11e6659SDavid S. Miller 
692f11e6659SDavid S. Miller 		if (next != rt0)
693f11e6659SDavid S. Miller 			fn->rr_ptr = next;
694554cfb7eSYOSHIFUJI Hideaki 	}
695554cfb7eSYOSHIFUJI Hideaki 
696d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
697a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
70070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
70170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
702b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
70370ceb4f5SYOSHIFUJI Hideaki {
704c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
70570ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
70670ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
70770ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7084bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
70970ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
71070ceb4f5SYOSHIFUJI Hideaki 
71170ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
71270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71370ceb4f5SYOSHIFUJI Hideaki 	}
71470ceb4f5SYOSHIFUJI Hideaki 
71570ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
71670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
71770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
71970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
72070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
72170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
72270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
72370ceb4f5SYOSHIFUJI Hideaki 		}
72470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
72570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
72670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
72770ceb4f5SYOSHIFUJI Hideaki 		}
72870ceb4f5SYOSHIFUJI Hideaki 	}
72970ceb4f5SYOSHIFUJI Hideaki 
73070ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
73170ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
7323933fc95SJens Rosenboom 		return -EINVAL;
73370ceb4f5SYOSHIFUJI Hideaki 
7344bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
73570ceb4f5SYOSHIFUJI Hideaki 
73670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
73770ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
73870ceb4f5SYOSHIFUJI Hideaki 	else {
73970ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
74070ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
74170ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
74270ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
74370ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
74470ceb4f5SYOSHIFUJI Hideaki 	}
74570ceb4f5SYOSHIFUJI Hideaki 
746f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
747f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
748f104a567SDuan Jiong 	else
749f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
750f104a567SDuan Jiong 					gwaddr, dev->ifindex);
75170ceb4f5SYOSHIFUJI Hideaki 
75270ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
753e0a1ad73SThomas Graf 		ip6_del_rt(rt);
75470ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
75570ceb4f5SYOSHIFUJI Hideaki 	}
75670ceb4f5SYOSHIFUJI Hideaki 
75770ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
758efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
75970ceb4f5SYOSHIFUJI Hideaki 					pref);
76070ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
76170ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
76270ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
76370ceb4f5SYOSHIFUJI Hideaki 
76470ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7651716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7661716a961SGao feng 			rt6_clean_expires(rt);
7671716a961SGao feng 		else
7681716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7691716a961SGao feng 
77094e187c0SAmerigo Wang 		ip6_rt_put(rt);
77170ceb4f5SYOSHIFUJI Hideaki 	}
77270ceb4f5SYOSHIFUJI Hideaki 	return 0;
77370ceb4f5SYOSHIFUJI Hideaki }
77470ceb4f5SYOSHIFUJI Hideaki #endif
77570ceb4f5SYOSHIFUJI Hideaki 
7768ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
777982f56f3SYOSHIFUJI Hideaki do { \
7788ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
779982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
780e0eda7bbSVille Nuorvala 		while (1) { \
781982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
782c71099acSThomas Graf 				goto out; \
783982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
784982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7858bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
786982f56f3SYOSHIFUJI Hideaki 			else \
787982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
788c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
789c71099acSThomas Graf 				goto restart; \
790c71099acSThomas Graf 		} \
791982f56f3SYOSHIFUJI Hideaki 	} \
792982f56f3SYOSHIFUJI Hideaki } while (0)
793c71099acSThomas Graf 
7948ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7958ed67789SDaniel Lezcano 					     struct fib6_table *table,
7964c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	struct fib6_node *fn;
7991da177e4SLinus Torvalds 	struct rt6_info *rt;
8001da177e4SLinus Torvalds 
801c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8024c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
803c71099acSThomas Graf restart:
804c71099acSThomas Graf 	rt = fn->leaf;
8054c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
80651ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
80752bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
8084c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
809c71099acSThomas Graf out:
810d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
811c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8121da177e4SLinus Torvalds 	return rt;
813c71099acSThomas Graf 
814c71099acSThomas Graf }
815c71099acSThomas Graf 
816ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
817ea6e574eSFlorian Westphal 				    int flags)
818ea6e574eSFlorian Westphal {
819ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
820ea6e574eSFlorian Westphal }
821ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
822ea6e574eSFlorian Westphal 
8239acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
8249acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
825c71099acSThomas Graf {
8264c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8274c9483b2SDavid S. Miller 		.flowi6_oif = oif,
8284c9483b2SDavid S. Miller 		.daddr = *daddr,
829c71099acSThomas Graf 	};
830c71099acSThomas Graf 	struct dst_entry *dst;
83177d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
832c71099acSThomas Graf 
833adaa70bbSThomas Graf 	if (saddr) {
8344c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
835adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
836adaa70bbSThomas Graf 	}
837adaa70bbSThomas Graf 
8384c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
839c71099acSThomas Graf 	if (dst->error == 0)
840c71099acSThomas Graf 		return (struct rt6_info *) dst;
841c71099acSThomas Graf 
842c71099acSThomas Graf 	dst_release(dst);
843c71099acSThomas Graf 
8441da177e4SLinus Torvalds 	return NULL;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8477159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8487159039aSYOSHIFUJI Hideaki 
849c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8501da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8511da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8521da177e4SLinus Torvalds    be destroyed.
8531da177e4SLinus Torvalds  */
8541da177e4SLinus Torvalds 
855e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
856e5fd387aSMichal Kubeček 			struct nlattr *mx, int mx_len)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	int err;
859c71099acSThomas Graf 	struct fib6_table *table;
8601da177e4SLinus Torvalds 
861c71099acSThomas Graf 	table = rt->rt6i_table;
862c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
863e5fd387aSMichal Kubeček 	err = fib6_add(&table->tb6_root, rt, info, mx, mx_len);
864c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	return err;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
86940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
87040e22e8fSThomas Graf {
8714d1169c1SDenis V. Lunev 	struct nl_info info = {
872d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8734d1169c1SDenis V. Lunev 	};
874e5fd387aSMichal Kubeček 	return __ip6_ins_rt(rt, &info, NULL, 0);
87540e22e8fSThomas Graf }
87640e22e8fSThomas Graf 
8771716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
87821efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
879b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	struct rt6_info *rt;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/*
8841da177e4SLinus Torvalds 	 *	Clone the route.
8851da177e4SLinus Torvalds 	 */
8861da177e4SLinus Torvalds 
88721efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	if (rt) {
890bb3c3686SDavid S. Miller 		if (ort->rt6i_dst.plen != 128 &&
89121efcfa0SEric Dumazet 		    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
89258c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8971da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8984e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8991da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
9001da177e4SLinus Torvalds 		}
9011da177e4SLinus Torvalds #endif
90295a9a5baSYOSHIFUJI Hideaki 	}
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	return rt;
9051da177e4SLinus Torvalds }
90695a9a5baSYOSHIFUJI Hideaki 
90721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
90821efcfa0SEric Dumazet 					const struct in6_addr *daddr)
909299d9939SYOSHIFUJI Hideaki {
91021efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
91121efcfa0SEric Dumazet 
912887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
913299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
914299d9939SYOSHIFUJI Hideaki 	return rt;
915299d9939SYOSHIFUJI Hideaki }
916299d9939SYOSHIFUJI Hideaki 
9178ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
9184c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	struct fib6_node *fn;
921519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
922c71099acSThomas Graf 	int strict = 0;
9231da177e4SLinus Torvalds 	int attempts = 3;
924519fbd87SYOSHIFUJI Hideaki 	int err;
92553b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
9261da177e4SLinus Torvalds 
92777d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds relookup:
930c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
9311da177e4SLinus Torvalds 
9328238dd06SYOSHIFUJI Hideaki restart_2:
9334c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds restart:
9364acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
93752bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
93852bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
9394c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9408ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9418238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9421da177e4SLinus Torvalds 		goto out;
9431da177e4SLinus Torvalds 
944d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
945c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9461da177e4SLinus Torvalds 
947c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
9484c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9497343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9504c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9517343ff31SDavid S. Miller 	else
9527343ff31SDavid S. Miller 		goto out2;
9531da177e4SLinus Torvalds 
95494e187c0SAmerigo Wang 	ip6_rt_put(rt);
9558ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9561da177e4SLinus Torvalds 
957d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
958e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
95940e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
960e40cf353SYOSHIFUJI Hideaki 		if (!err)
961e40cf353SYOSHIFUJI Hideaki 			goto out2;
962e40cf353SYOSHIFUJI Hideaki 	}
963e40cf353SYOSHIFUJI Hideaki 
964e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9651da177e4SLinus Torvalds 		goto out2;
9661da177e4SLinus Torvalds 
967519fbd87SYOSHIFUJI Hideaki 	/*
968c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
969519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9701da177e4SLinus Torvalds 	 */
97194e187c0SAmerigo Wang 	ip6_rt_put(rt);
9721da177e4SLinus Torvalds 	goto relookup;
973e40cf353SYOSHIFUJI Hideaki 
974519fbd87SYOSHIFUJI Hideaki out:
9758238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9768238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9778238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9788238dd06SYOSHIFUJI Hideaki 	}
979d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
980c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9811da177e4SLinus Torvalds out2:
982d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
983d8d1f30bSChangli Gao 	rt->dst.__use++;
984c71099acSThomas Graf 
985c71099acSThomas Graf 	return rt;
986c71099acSThomas Graf }
987c71099acSThomas Graf 
9888ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9894c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9904acad72dSPavel Emelyanov {
9914c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9924acad72dSPavel Emelyanov }
9934acad72dSPavel Emelyanov 
99472331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
99572331bc0SShmulik Ladkani 						struct net_device *dev,
99672331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
99772331bc0SShmulik Ladkani {
99872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
99972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
100072331bc0SShmulik Ladkani 
100172331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
100272331bc0SShmulik Ladkani }
100372331bc0SShmulik Ladkani 
1004c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1005c71099acSThomas Graf {
1006b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1007c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1008adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
10094c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
10104c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
10114c9483b2SDavid S. Miller 		.daddr = iph->daddr,
10124c9483b2SDavid S. Miller 		.saddr = iph->saddr,
10136502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
10144c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
10154c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1016c71099acSThomas Graf 	};
1017adaa70bbSThomas Graf 
101872331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1019c71099acSThomas Graf }
1020c71099acSThomas Graf 
10218ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
10224c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1023c71099acSThomas Graf {
10244c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1025c71099acSThomas Graf }
1026c71099acSThomas Graf 
10279c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
10284c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
1029c71099acSThomas Graf {
1030c71099acSThomas Graf 	int flags = 0;
1031c71099acSThomas Graf 
10321fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
10334dc27d1cSDavid McCullough 
10344c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
103577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1036c71099acSThomas Graf 
10374c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1038adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10390c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10400c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1041adaa70bbSThomas Graf 
10424c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10457159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10461da177e4SLinus Torvalds 
10472774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
104814e50e57SDavid S. Miller {
10495c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
105014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
105114e50e57SDavid S. Miller 
1052f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
105314e50e57SDavid S. Miller 	if (rt) {
1054d8d1f30bSChangli Gao 		new = &rt->dst;
105514e50e57SDavid S. Miller 
10568104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10578104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10588104891bSSteffen Klassert 
105914e50e57SDavid S. Miller 		new->__use = 1;
1060352e512cSHerbert Xu 		new->input = dst_discard;
1061352e512cSHerbert Xu 		new->output = dst_discard;
106214e50e57SDavid S. Miller 
106321efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
106421efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
106521efcfa0SEric Dumazet 		else
1066defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
106714e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
106814e50e57SDavid S. Miller 		if (rt->rt6i_idev)
106914e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
107014e50e57SDavid S. Miller 
10714e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10721716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
107314e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
107414e50e57SDavid S. Miller 
107514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
107614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
107714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
107814e50e57SDavid S. Miller #endif
107914e50e57SDavid S. Miller 
108014e50e57SDavid S. Miller 		dst_free(new);
108114e50e57SDavid S. Miller 	}
108214e50e57SDavid S. Miller 
108369ead7afSDavid S. Miller 	dst_release(dst_orig);
108469ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
108514e50e57SDavid S. Miller }
108614e50e57SDavid S. Miller 
10871da177e4SLinus Torvalds /*
10881da177e4SLinus Torvalds  *	Destination cache support functions
10891da177e4SLinus Torvalds  */
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	struct rt6_info *rt;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10961da177e4SLinus Torvalds 
10976f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10986f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10996f3118b5SNicolas Dichtel 	 * into this function always.
11006f3118b5SNicolas Dichtel 	 */
1101ca4c3fc2Sfan.du 	if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev)))
11026f3118b5SNicolas Dichtel 		return NULL;
11036f3118b5SNicolas Dichtel 
1104e3bc10bdSHannes Frederic Sowa 	if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
11051da177e4SLinus Torvalds 		return NULL;
1106e3bc10bdSHannes Frederic Sowa 
1107e3bc10bdSHannes Frederic Sowa 	if (rt6_check_expired(rt))
1108e3bc10bdSHannes Frederic Sowa 		return NULL;
1109e3bc10bdSHannes Frederic Sowa 
1110e3bc10bdSHannes Frederic Sowa 	return dst;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (rt) {
111854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
111954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1120e0a1ad73SThomas Graf 				ip6_del_rt(rt);
112154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
11221da177e4SLinus Torvalds 			}
112354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
112454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
112554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
112654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
112754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
112854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	struct rt6_info *rt;
11341da177e4SLinus Torvalds 
11353ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11361da177e4SLinus Torvalds 
1137adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11381da177e4SLinus Torvalds 	if (rt) {
11391eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11401eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11411eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11421eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11431eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11441da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11451da177e4SLinus Torvalds 		}
11461da177e4SLinus Torvalds 	}
11471eb4f758SHannes Frederic Sowa }
11481da177e4SLinus Torvalds 
11496700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11506700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11531da177e4SLinus Torvalds 
115481aded24SDavid S. Miller 	dst_confirm(dst);
11551da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
115681aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
115781aded24SDavid S. Miller 
11581da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11591da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1160defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11611da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1162defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1163defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11641da177e4SLinus Torvalds 		}
1165defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
116681aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
117042ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
117142ae66c8SDavid S. Miller 		     int oif, u32 mark)
117281aded24SDavid S. Miller {
117381aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
117481aded24SDavid S. Miller 	struct dst_entry *dst;
117581aded24SDavid S. Miller 	struct flowi6 fl6;
117681aded24SDavid S. Miller 
117781aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
117881aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
117981aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
118081aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
118181aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11826502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
118381aded24SDavid S. Miller 
118481aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
118581aded24SDavid S. Miller 	if (!dst->error)
11866700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
118781aded24SDavid S. Miller 	dst_release(dst);
118881aded24SDavid S. Miller }
118981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
119081aded24SDavid S. Miller 
119181aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
119281aded24SDavid S. Miller {
119381aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
119481aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
119581aded24SDavid S. Miller }
119681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
119781aded24SDavid S. Miller 
1198b55b76b2SDuan Jiong /* Handle redirects */
1199b55b76b2SDuan Jiong struct ip6rd_flowi {
1200b55b76b2SDuan Jiong 	struct flowi6 fl6;
1201b55b76b2SDuan Jiong 	struct in6_addr gateway;
1202b55b76b2SDuan Jiong };
1203b55b76b2SDuan Jiong 
1204b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
1205b55b76b2SDuan Jiong 					     struct fib6_table *table,
1206b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
1207b55b76b2SDuan Jiong 					     int flags)
1208b55b76b2SDuan Jiong {
1209b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1210b55b76b2SDuan Jiong 	struct rt6_info *rt;
1211b55b76b2SDuan Jiong 	struct fib6_node *fn;
1212b55b76b2SDuan Jiong 
1213b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
1214b55b76b2SDuan Jiong 	 * check if the redirect has come from approriate router.
1215b55b76b2SDuan Jiong 	 *
1216b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
1217b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
1218b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
1219b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
1220b55b76b2SDuan Jiong 	 * routes.
1221b55b76b2SDuan Jiong 	 */
1222b55b76b2SDuan Jiong 
1223b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
1224b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1225b55b76b2SDuan Jiong restart:
1226b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1227b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
1228b55b76b2SDuan Jiong 			continue;
1229b55b76b2SDuan Jiong 		if (rt->dst.error)
1230b55b76b2SDuan Jiong 			break;
1231b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1232b55b76b2SDuan Jiong 			continue;
1233b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1234b55b76b2SDuan Jiong 			continue;
1235b55b76b2SDuan Jiong 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1236b55b76b2SDuan Jiong 			continue;
1237b55b76b2SDuan Jiong 		break;
1238b55b76b2SDuan Jiong 	}
1239b55b76b2SDuan Jiong 
1240b55b76b2SDuan Jiong 	if (!rt)
1241b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1242b55b76b2SDuan Jiong 	else if (rt->dst.error) {
1243b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1244b55b76b2SDuan Jiong 		goto out;
1245b55b76b2SDuan Jiong 	}
1246b55b76b2SDuan Jiong 	BACKTRACK(net, &fl6->saddr);
1247b55b76b2SDuan Jiong out:
1248b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
1249b55b76b2SDuan Jiong 
1250b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
1251b55b76b2SDuan Jiong 
1252b55b76b2SDuan Jiong 	return rt;
1253b55b76b2SDuan Jiong };
1254b55b76b2SDuan Jiong 
1255b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
1256b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
1257b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
1258b55b76b2SDuan Jiong {
1259b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1260b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
1261b55b76b2SDuan Jiong 
1262b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
1263b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
1264b55b76b2SDuan Jiong 
1265b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
1266b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
1267b55b76b2SDuan Jiong }
1268b55b76b2SDuan Jiong 
12693a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
12703a5ad2eeSDavid S. Miller {
12713a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
12723a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12733a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12743a5ad2eeSDavid S. Miller 
12753a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
12763a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12773a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12783a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12793a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12806502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
12813a5ad2eeSDavid S. Miller 
1282b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
12836700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
12843a5ad2eeSDavid S. Miller 	dst_release(dst);
12853a5ad2eeSDavid S. Miller }
12863a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12873a5ad2eeSDavid S. Miller 
1288c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1289c92a59ecSDuan Jiong 			    u32 mark)
1290c92a59ecSDuan Jiong {
1291c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1292c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1293c92a59ecSDuan Jiong 	struct dst_entry *dst;
1294c92a59ecSDuan Jiong 	struct flowi6 fl6;
1295c92a59ecSDuan Jiong 
1296c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
1297c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
1298c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
1299c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
1300c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
1301c92a59ecSDuan Jiong 
1302b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1303c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
1304c92a59ecSDuan Jiong 	dst_release(dst);
1305c92a59ecSDuan Jiong }
1306c92a59ecSDuan Jiong 
13073a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
13083a5ad2eeSDavid S. Miller {
13093a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
13103a5ad2eeSDavid S. Miller }
13113a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
13123a5ad2eeSDavid S. Miller 
13130dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
13141da177e4SLinus Torvalds {
13150dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
13160dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
13170dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
13180dbaee3bSDavid S. Miller 
13191da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
13201da177e4SLinus Torvalds 
13215578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
13225578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/*
13251da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
13261da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
13271da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
13281da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
13291da177e4SLinus Torvalds 	 */
13301da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
13311da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
13321da177e4SLinus Torvalds 	return mtu;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
1335ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1336d33e4553SDavid S. Miller {
1337d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1338618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1339618f9bc7SSteffen Klassert 
1340618f9bc7SSteffen Klassert 	if (mtu)
1341*30f78d8eSEric Dumazet 		goto out;
1342618f9bc7SSteffen Klassert 
1343618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1344d33e4553SDavid S. Miller 
1345d33e4553SDavid S. Miller 	rcu_read_lock();
1346d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1347d33e4553SDavid S. Miller 	if (idev)
1348d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1349d33e4553SDavid S. Miller 	rcu_read_unlock();
1350d33e4553SDavid S. Miller 
1351*30f78d8eSEric Dumazet out:
1352*30f78d8eSEric Dumazet 	return min_t(unsigned int, mtu, IP6_MAX_MTU);
1353d33e4553SDavid S. Miller }
1354d33e4553SDavid S. Miller 
13553b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
13563b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
13575d0bbeebSThomas Graf 
13583b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
135987a11578SDavid S. Miller 				  struct flowi6 *fl6)
13601da177e4SLinus Torvalds {
136187a11578SDavid S. Miller 	struct dst_entry *dst;
13621da177e4SLinus Torvalds 	struct rt6_info *rt;
13631da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1364c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
13651da177e4SLinus Torvalds 
136638308473SDavid S. Miller 	if (unlikely(!idev))
1367122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
13681da177e4SLinus Torvalds 
13698b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
137038308473SDavid S. Miller 	if (unlikely(!rt)) {
13711da177e4SLinus Torvalds 		in6_dev_put(idev);
137287a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
13731da177e4SLinus Torvalds 		goto out;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13768e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
13778e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1378d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1379550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
138087a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13818e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13828e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
138314edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
13841da177e4SLinus Torvalds 
13853b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1386d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1387d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13883b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13891da177e4SLinus Torvalds 
13905578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13911da177e4SLinus Torvalds 
139287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
139387a11578SDavid S. Miller 
13941da177e4SLinus Torvalds out:
139587a11578SDavid S. Miller 	return dst;
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13983d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
13991da177e4SLinus Torvalds {
1400e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
14013d0f24a7SStephen Hemminger 	int more = 0;
14021da177e4SLinus Torvalds 
14033b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
14043b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
14055d0bbeebSThomas Graf 
14061da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
14071da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
14081da177e4SLinus Torvalds 			*pprev = dst->next;
14091da177e4SLinus Torvalds 			dst_free(dst);
14101da177e4SLinus Torvalds 		} else {
14111da177e4SLinus Torvalds 			pprev = &dst->next;
14123d0f24a7SStephen Hemminger 			++more;
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
14163b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
14175d0bbeebSThomas Graf 
14183d0f24a7SStephen Hemminger 	return more;
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
14221e493d19SDavid S. Miller 			    void *arg)
14231e493d19SDavid S. Miller {
14241e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
14251e493d19SDavid S. Miller 
14261e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
14271e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
14281e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
14291e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
14301e493d19SDavid S. Miller 		if (func(rt, arg)) {
14311e493d19SDavid S. Miller 			*pprev = dst->next;
14321e493d19SDavid S. Miller 			dst_free(dst);
14331e493d19SDavid S. Miller 		} else {
14341e493d19SDavid S. Miller 			pprev = &dst->next;
14351e493d19SDavid S. Miller 		}
14361e493d19SDavid S. Miller 	}
14371e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
14381e493d19SDavid S. Miller }
14391e493d19SDavid S. Miller 
1440569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
14411da177e4SLinus Torvalds {
144286393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
14437019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
14447019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
14457019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
14467019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
14477019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1448fc66f95cSEric Dumazet 	int entries;
14491da177e4SLinus Torvalds 
1450fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
145149a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
1452fc66f95cSEric Dumazet 	    entries <= rt_max_size)
14531da177e4SLinus Torvalds 		goto out;
14541da177e4SLinus Torvalds 
14556891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
14562ac3ac8fSMichal Kubeček 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, entries > rt_max_size);
1457fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1458fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
14597019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
14601da177e4SLinus Torvalds out:
14617019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1462fc66f95cSEric Dumazet 	return entries > rt_max_size;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds /*
14661da177e4SLinus Torvalds  *
14671da177e4SLinus Torvalds  */
14681da177e4SLinus Torvalds 
146986872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
14701da177e4SLinus Torvalds {
14711da177e4SLinus Torvalds 	int err;
14725578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
14731da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
14741da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14751da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1476c71099acSThomas Graf 	struct fib6_table *table;
14771da177e4SLinus Torvalds 	int addr_type;
14781da177e4SLinus Torvalds 
147986872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
14801da177e4SLinus Torvalds 		return -EINVAL;
14811da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
148286872cb5SThomas Graf 	if (cfg->fc_src_len)
14831da177e4SLinus Torvalds 		return -EINVAL;
14841da177e4SLinus Torvalds #endif
148586872cb5SThomas Graf 	if (cfg->fc_ifindex) {
14861da177e4SLinus Torvalds 		err = -ENODEV;
14875578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
14881da177e4SLinus Torvalds 		if (!dev)
14891da177e4SLinus Torvalds 			goto out;
14901da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
14911da177e4SLinus Torvalds 		if (!idev)
14921da177e4SLinus Torvalds 			goto out;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 
149586872cb5SThomas Graf 	if (cfg->fc_metric == 0)
149686872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
14971da177e4SLinus Torvalds 
1498c71099acSThomas Graf 	err = -ENOBUFS;
149938308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1500d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1501d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
150238308473SDavid S. Miller 		if (!table) {
1503f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1504d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1505d71314b4SMatti Vaittinen 		}
1506d71314b4SMatti Vaittinen 	} else {
1507d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1508d71314b4SMatti Vaittinen 	}
150938308473SDavid S. Miller 
151038308473SDavid S. Miller 	if (!table)
1511c71099acSThomas Graf 		goto out;
1512c71099acSThomas Graf 
1513c88507fbSSabrina Dubroca 	rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
15141da177e4SLinus Torvalds 
151538308473SDavid S. Miller 	if (!rt) {
15161da177e4SLinus Torvalds 		err = -ENOMEM;
15171da177e4SLinus Torvalds 		goto out;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
15201716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
15211716a961SGao feng 		rt6_set_expires(rt, jiffies +
15221716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
15231716a961SGao feng 	else
15241716a961SGao feng 		rt6_clean_expires(rt);
15251da177e4SLinus Torvalds 
152686872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
152786872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
152886872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
152986872cb5SThomas Graf 
153086872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1533d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1534ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1535ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
15361da177e4SLinus Torvalds 	else
1537d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
15381da177e4SLinus Torvalds 
1539d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
15401da177e4SLinus Torvalds 
154186872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
154286872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
1543e5fd387aSMichal Kubeček 	if (rt->rt6i_dst.plen == 128) {
154411d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
1545e5fd387aSMichal Kubeček 		dst_metrics_set_force_overwrite(&rt->dst);
1546e5fd387aSMichal Kubeček 	}
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
154986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
155086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
15511da177e4SLinus Torvalds #endif
15521da177e4SLinus Torvalds 
155386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
15561da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
15571da177e4SLinus Torvalds 	 */
155886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
155938308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
156038308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
156138308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
15621da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
15635578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
15641da177e4SLinus Torvalds 			if (dev) {
15651da177e4SLinus Torvalds 				dev_put(dev);
15661da177e4SLinus Torvalds 				in6_dev_put(idev);
15671da177e4SLinus Torvalds 			}
15685578689aSDaniel Lezcano 			dev = net->loopback_dev;
15691da177e4SLinus Torvalds 			dev_hold(dev);
15701da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
15711da177e4SLinus Torvalds 			if (!idev) {
15721da177e4SLinus Torvalds 				err = -ENODEV;
15731da177e4SLinus Torvalds 				goto out;
15741da177e4SLinus Torvalds 			}
15751da177e4SLinus Torvalds 		}
15761da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1577ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1578ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1579ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
15807150aedeSKamala R 			rt->dst.output = dst_discard;
15817150aedeSKamala R 			rt->dst.input = dst_discard;
1582ef2c7d7bSNicolas Dichtel 			break;
1583ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1584ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
15857150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
15867150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
1587ef2c7d7bSNicolas Dichtel 			break;
1588b4949ab2SNicolas Dichtel 		case RTN_THROW:
1589ef2c7d7bSNicolas Dichtel 		default:
15907150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
15917150aedeSKamala R 					: -ENETUNREACH;
15927150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
15937150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
1594ef2c7d7bSNicolas Dichtel 			break;
1595ef2c7d7bSNicolas Dichtel 		}
15961da177e4SLinus Torvalds 		goto install_route;
15971da177e4SLinus Torvalds 	}
15981da177e4SLinus Torvalds 
159986872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1600b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
16011da177e4SLinus Torvalds 		int gwa_type;
16021da177e4SLinus Torvalds 
160386872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
16044e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
16051da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
16081da177e4SLinus Torvalds 			struct rt6_info *grt;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
16111da177e4SLinus Torvalds 			   addresses as nexthop address.
16121da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
16131da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
16141da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
16151da177e4SLinus Torvalds 			   some exceptions. --ANK
16161da177e4SLinus Torvalds 			 */
16171da177e4SLinus Torvalds 			err = -EINVAL;
16181da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
16191da177e4SLinus Torvalds 				goto out;
16201da177e4SLinus Torvalds 
16215578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
162438308473SDavid S. Miller 			if (!grt)
16251da177e4SLinus Torvalds 				goto out;
16261da177e4SLinus Torvalds 			if (dev) {
1627d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
162894e187c0SAmerigo Wang 					ip6_rt_put(grt);
16291da177e4SLinus Torvalds 					goto out;
16301da177e4SLinus Torvalds 				}
16311da177e4SLinus Torvalds 			} else {
1632d1918542SDavid S. Miller 				dev = grt->dst.dev;
16331da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
16341da177e4SLinus Torvalds 				dev_hold(dev);
16351da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
16361da177e4SLinus Torvalds 			}
16371da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
16381da177e4SLinus Torvalds 				err = 0;
163994e187c0SAmerigo Wang 			ip6_rt_put(grt);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 			if (err)
16421da177e4SLinus Torvalds 				goto out;
16431da177e4SLinus Torvalds 		}
16441da177e4SLinus Torvalds 		err = -EINVAL;
164538308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
16461da177e4SLinus Torvalds 			goto out;
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	err = -ENODEV;
165038308473SDavid S. Miller 	if (!dev)
16511da177e4SLinus Torvalds 		goto out;
16521da177e4SLinus Torvalds 
1653c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1654c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1655c3968a85SDaniel Walter 			err = -EINVAL;
1656c3968a85SDaniel Walter 			goto out;
1657c3968a85SDaniel Walter 		}
16584e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1659c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1660c3968a85SDaniel Walter 	} else
1661c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1662c3968a85SDaniel Walter 
166386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds install_route:
1666d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16671da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1668c71099acSThomas Graf 	rt->rt6i_table = table;
166963152fc0SDaniel Lezcano 
1670c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
167163152fc0SDaniel Lezcano 
1672e5fd387aSMichal Kubeček 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo, cfg->fc_mx, cfg->fc_mx_len);
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds out:
16751da177e4SLinus Torvalds 	if (dev)
16761da177e4SLinus Torvalds 		dev_put(dev);
16771da177e4SLinus Torvalds 	if (idev)
16781da177e4SLinus Torvalds 		in6_dev_put(idev);
16791da177e4SLinus Torvalds 	if (rt)
1680d8d1f30bSChangli Gao 		dst_free(&rt->dst);
16811da177e4SLinus Torvalds 	return err;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
168486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	int err;
1687c71099acSThomas Graf 	struct fib6_table *table;
1688d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
16891da177e4SLinus Torvalds 
16906825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
16916825a26cSGao feng 		err = -ENOENT;
16926825a26cSGao feng 		goto out;
16936825a26cSGao feng 	}
16946c813a72SPatrick McHardy 
1695c71099acSThomas Graf 	table = rt->rt6i_table;
1696c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
169786872cb5SThomas Graf 	err = fib6_del(rt, info);
1698c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16991da177e4SLinus Torvalds 
17006825a26cSGao feng out:
170194e187c0SAmerigo Wang 	ip6_rt_put(rt);
17021da177e4SLinus Torvalds 	return err;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
1705e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1706e0a1ad73SThomas Graf {
17074d1169c1SDenis V. Lunev 	struct nl_info info = {
1708d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
17094d1169c1SDenis V. Lunev 	};
1710528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1711e0a1ad73SThomas Graf }
1712e0a1ad73SThomas Graf 
171386872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
17141da177e4SLinus Torvalds {
1715c71099acSThomas Graf 	struct fib6_table *table;
17161da177e4SLinus Torvalds 	struct fib6_node *fn;
17171da177e4SLinus Torvalds 	struct rt6_info *rt;
17181da177e4SLinus Torvalds 	int err = -ESRCH;
17191da177e4SLinus Torvalds 
17205578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
172138308473SDavid S. Miller 	if (!table)
1722c71099acSThomas Graf 		return err;
17231da177e4SLinus Torvalds 
1724c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1725c71099acSThomas Graf 
1726c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
172786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
172886872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (fn) {
1731d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
173286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1733d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1734d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17351da177e4SLinus Torvalds 				continue;
173686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
173786872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17381da177e4SLinus Torvalds 				continue;
173986872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17401da177e4SLinus Torvalds 				continue;
1741d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1742c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
17431da177e4SLinus Torvalds 
174486872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
17451da177e4SLinus Torvalds 		}
17461da177e4SLinus Torvalds 	}
1747c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	return err;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17526700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1753a6279458SYOSHIFUJI Hideaki {
1754e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1755a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1756e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1757e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1758e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1759e8599ff4SDavid S. Miller 	struct neighbour *neigh;
176071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
17616e157b6aSDavid S. Miller 	int optlen, on_link;
17626e157b6aSDavid S. Miller 	u8 *lladdr;
1763e8599ff4SDavid S. Miller 
176429a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
176571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1766e8599ff4SDavid S. Miller 
1767e8599ff4SDavid S. Miller 	if (optlen < 0) {
17686e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1769e8599ff4SDavid S. Miller 		return;
1770e8599ff4SDavid S. Miller 	}
1771e8599ff4SDavid S. Miller 
177271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1773e8599ff4SDavid S. Miller 
177471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
17756e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1776e8599ff4SDavid S. Miller 		return;
1777e8599ff4SDavid S. Miller 	}
1778e8599ff4SDavid S. Miller 
17796e157b6aSDavid S. Miller 	on_link = 0;
178071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1781e8599ff4SDavid S. Miller 		on_link = 1;
178271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1783e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
17846e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1785e8599ff4SDavid S. Miller 		return;
1786e8599ff4SDavid S. Miller 	}
1787e8599ff4SDavid S. Miller 
1788e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1789e8599ff4SDavid S. Miller 	if (!in6_dev)
1790e8599ff4SDavid S. Miller 		return;
1791e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1792e8599ff4SDavid S. Miller 		return;
1793e8599ff4SDavid S. Miller 
1794e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1795e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1796e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1797e8599ff4SDavid S. Miller 	 */
1798e8599ff4SDavid S. Miller 
179971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1800e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1801e8599ff4SDavid S. Miller 		return;
1802e8599ff4SDavid S. Miller 	}
18036e157b6aSDavid S. Miller 
18046e157b6aSDavid S. Miller 	lladdr = NULL;
1805e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1806e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1807e8599ff4SDavid S. Miller 					     skb->dev);
1808e8599ff4SDavid S. Miller 		if (!lladdr) {
1809e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1810e8599ff4SDavid S. Miller 			return;
1811e8599ff4SDavid S. Miller 		}
1812e8599ff4SDavid S. Miller 	}
1813e8599ff4SDavid S. Miller 
18146e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
18156e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
18166e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
18176e157b6aSDavid S. Miller 		return;
18186e157b6aSDavid S. Miller 	}
18196e157b6aSDavid S. Miller 
18206e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18216e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18226e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18236e157b6aSDavid S. Miller 	 */
18246e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18256e157b6aSDavid S. Miller 
182671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1827e8599ff4SDavid S. Miller 	if (!neigh)
1828e8599ff4SDavid S. Miller 		return;
1829e8599ff4SDavid S. Miller 
18301da177e4SLinus Torvalds 	/*
18311da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18321da177e4SLinus Torvalds 	 */
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18351da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18361da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18371da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18381da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18391da177e4SLinus Torvalds 		     );
18401da177e4SLinus Torvalds 
184171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
184238308473SDavid S. Miller 	if (!nrt)
18431da177e4SLinus Torvalds 		goto out;
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18461da177e4SLinus Torvalds 	if (on_link)
18471da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18481da177e4SLinus Torvalds 
18494e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
18501da177e4SLinus Torvalds 
185140e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18521da177e4SLinus Torvalds 		goto out;
18531da177e4SLinus Torvalds 
1854d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1855d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
185671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
185760592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
18588d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18598d71740cSTom Tucker 
18601da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18616e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1862e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18631da177e4SLinus Torvalds 	}
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds out:
1866e8599ff4SDavid S. Miller 	neigh_release(neigh);
18676e157b6aSDavid S. Miller }
18686e157b6aSDavid S. Miller 
18691da177e4SLinus Torvalds /*
18701da177e4SLinus Torvalds  *	Misc support functions
18711da177e4SLinus Torvalds  */
18721da177e4SLinus Torvalds 
18731716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
187421efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18751da177e4SLinus Torvalds {
1876d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18778b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18788b96d22dSDavid S. Miller 					    ort->rt6i_table);
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	if (rt) {
1881d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1882d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18838e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18841da177e4SLinus Torvalds 
18854e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18868e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1887defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1888d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18891da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18901da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18911da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1892d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18931da177e4SLinus Torvalds 
1894550bab42SJulian Anastasov 		if (ort->rt6i_flags & RTF_GATEWAY)
18954e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = ort->rt6i_gateway;
1896550bab42SJulian Anastasov 		else
1897550bab42SJulian Anastasov 			rt->rt6i_gateway = *dest;
18981716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18991716a961SGao feng 		rt6_set_from(rt, ort);
19001da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
19031da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
19041da177e4SLinus Torvalds #endif
19050f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1906c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
19071da177e4SLinus Torvalds 	}
19081da177e4SLinus Torvalds 	return rt;
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
191170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1912efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1913b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1914b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
191570ceb4f5SYOSHIFUJI Hideaki {
191670ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
191770ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1918c71099acSThomas Graf 	struct fib6_table *table;
191970ceb4f5SYOSHIFUJI Hideaki 
1920efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
192138308473SDavid S. Miller 	if (!table)
1922c71099acSThomas Graf 		return NULL;
1923c71099acSThomas Graf 
19245744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1925c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
192670ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
192770ceb4f5SYOSHIFUJI Hideaki 		goto out;
192870ceb4f5SYOSHIFUJI Hideaki 
1929d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1930d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
193170ceb4f5SYOSHIFUJI Hideaki 			continue;
193270ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
193370ceb4f5SYOSHIFUJI Hideaki 			continue;
193470ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
193570ceb4f5SYOSHIFUJI Hideaki 			continue;
1936d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
193770ceb4f5SYOSHIFUJI Hideaki 		break;
193870ceb4f5SYOSHIFUJI Hideaki 	}
193970ceb4f5SYOSHIFUJI Hideaki out:
19405744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
194170ceb4f5SYOSHIFUJI Hideaki 	return rt;
194270ceb4f5SYOSHIFUJI Hideaki }
194370ceb4f5SYOSHIFUJI Hideaki 
1944efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1945b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1946b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
194795c96174SEric Dumazet 					   unsigned int pref)
194870ceb4f5SYOSHIFUJI Hideaki {
194986872cb5SThomas Graf 	struct fib6_config cfg = {
195086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1951238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
195286872cb5SThomas Graf 		.fc_ifindex	= ifindex,
195386872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
195486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
195586872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
195615e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1957efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1958efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
195986872cb5SThomas Graf 	};
196070ceb4f5SYOSHIFUJI Hideaki 
19614e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19624e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
196386872cb5SThomas Graf 
1964e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1965e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
196686872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
196770ceb4f5SYOSHIFUJI Hideaki 
196886872cb5SThomas Graf 	ip6_route_add(&cfg);
196970ceb4f5SYOSHIFUJI Hideaki 
1970efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
197170ceb4f5SYOSHIFUJI Hideaki }
197270ceb4f5SYOSHIFUJI Hideaki #endif
197370ceb4f5SYOSHIFUJI Hideaki 
1974b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19751da177e4SLinus Torvalds {
19761da177e4SLinus Torvalds 	struct rt6_info *rt;
1977c71099acSThomas Graf 	struct fib6_table *table;
19781da177e4SLinus Torvalds 
1979c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
198038308473SDavid S. Miller 	if (!table)
1981c71099acSThomas Graf 		return NULL;
19821da177e4SLinus Torvalds 
19835744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1984d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1985d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1986045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19871da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19881da177e4SLinus Torvalds 			break;
19891da177e4SLinus Torvalds 	}
19901da177e4SLinus Torvalds 	if (rt)
1991d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
19925744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
19931da177e4SLinus Torvalds 	return rt;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
1996b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1997ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1998ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19991da177e4SLinus Torvalds {
200086872cb5SThomas Graf 	struct fib6_config cfg = {
200186872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
2002238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
200386872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
200486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
200586872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
200615e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
20075578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2008c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
200986872cb5SThomas Graf 	};
20101da177e4SLinus Torvalds 
20114e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
20121da177e4SLinus Torvalds 
201386872cb5SThomas Graf 	ip6_route_add(&cfg);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
20187b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20191da177e4SLinus Torvalds {
20201da177e4SLinus Torvalds 	struct rt6_info *rt;
2021c71099acSThomas Graf 	struct fib6_table *table;
2022c71099acSThomas Graf 
2023c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20247b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
202538308473SDavid S. Miller 	if (!table)
2026c71099acSThomas Graf 		return;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds restart:
2029c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2030d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20313e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
20323e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
2033d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2034c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2035e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20361da177e4SLinus Torvalds 			goto restart;
20371da177e4SLinus Torvalds 		}
20381da177e4SLinus Torvalds 	}
2039c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds 
20425578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20435578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
204486872cb5SThomas Graf 				 struct fib6_config *cfg)
204586872cb5SThomas Graf {
204686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
204786872cb5SThomas Graf 
204886872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
204986872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
205086872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
205186872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
205286872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
205386872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
205486872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
205586872cb5SThomas Graf 
20565578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2057f1243c2dSBenjamin Thery 
20584e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20594e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20604e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
206186872cb5SThomas Graf }
206286872cb5SThomas Graf 
20635578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20641da177e4SLinus Torvalds {
206586872cb5SThomas Graf 	struct fib6_config cfg;
20661da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20671da177e4SLinus Torvalds 	int err;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	switch(cmd) {
20701da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20711da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2072af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
20731da177e4SLinus Torvalds 			return -EPERM;
20741da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20751da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20761da177e4SLinus Torvalds 		if (err)
20771da177e4SLinus Torvalds 			return -EFAULT;
20781da177e4SLinus Torvalds 
20795578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
208086872cb5SThomas Graf 
20811da177e4SLinus Torvalds 		rtnl_lock();
20821da177e4SLinus Torvalds 		switch (cmd) {
20831da177e4SLinus Torvalds 		case SIOCADDRT:
208486872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20851da177e4SLinus Torvalds 			break;
20861da177e4SLinus Torvalds 		case SIOCDELRT:
208786872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20881da177e4SLinus Torvalds 			break;
20891da177e4SLinus Torvalds 		default:
20901da177e4SLinus Torvalds 			err = -EINVAL;
20911da177e4SLinus Torvalds 		}
20921da177e4SLinus Torvalds 		rtnl_unlock();
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 		return err;
20953ff50b79SStephen Hemminger 	}
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	return -EINVAL;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds /*
21011da177e4SLinus Torvalds  *	Drop the packet on the floor
21021da177e4SLinus Torvalds  */
21031da177e4SLinus Torvalds 
2104d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
21051da177e4SLinus Torvalds {
2106612f09e8SYOSHIFUJI Hideaki 	int type;
2107adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2108612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2109612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
21100660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
211145bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
21123bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21133bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2114612f09e8SYOSHIFUJI Hideaki 			break;
2115612f09e8SYOSHIFUJI Hideaki 		}
2116612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2117612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
21183bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21193bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2120612f09e8SYOSHIFUJI Hideaki 		break;
2121612f09e8SYOSHIFUJI Hideaki 	}
21223ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21231da177e4SLinus Torvalds 	kfree_skb(skb);
21241da177e4SLinus Torvalds 	return 0;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21279ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21289ce8ade0SThomas Graf {
2129612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21309ce8ade0SThomas Graf }
21319ce8ade0SThomas Graf 
213220380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
21331da177e4SLinus Torvalds {
2134adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2135612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21361da177e4SLinus Torvalds }
21371da177e4SLinus Torvalds 
21389ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21399ce8ade0SThomas Graf {
2140612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21419ce8ade0SThomas Graf }
21429ce8ade0SThomas Graf 
21439ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21449ce8ade0SThomas Graf {
2145adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2146612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21479ce8ade0SThomas Graf }
21489ce8ade0SThomas Graf 
21491da177e4SLinus Torvalds /*
21501da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21511da177e4SLinus Torvalds  */
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21541da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21558f031519SDavid S. Miller 				    bool anycast)
21561da177e4SLinus Torvalds {
2157c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2158a3300ef4SHannes Frederic Sowa 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2159a3300ef4SHannes Frederic Sowa 					    DST_NOCOUNT, NULL);
2160a3300ef4SHannes Frederic Sowa 	if (!rt)
21611da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	in6_dev_hold(idev);
21641da177e4SLinus Torvalds 
216511d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2166d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2167d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21681da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
217158c4fb86SYOSHIFUJI Hideaki 	if (anycast)
217258c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
217358c4fb86SYOSHIFUJI Hideaki 	else
21741da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21751da177e4SLinus Torvalds 
2176550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
21774e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21781da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21795578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21801da177e4SLinus Torvalds 
2181d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	return rt;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
2186c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2187c3968a85SDaniel Walter 			struct rt6_info *rt,
2188b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2189c3968a85SDaniel Walter 			unsigned int prefs,
2190c3968a85SDaniel Walter 			struct in6_addr *saddr)
2191c3968a85SDaniel Walter {
2192c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2193c3968a85SDaniel Walter 	int err = 0;
2194c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21954e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2196c3968a85SDaniel Walter 	else
2197c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2198c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2199c3968a85SDaniel Walter 	return err;
2200c3968a85SDaniel Walter }
2201c3968a85SDaniel Walter 
2202c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2203c3968a85SDaniel Walter struct arg_dev_net_ip {
2204c3968a85SDaniel Walter 	struct net_device *dev;
2205c3968a85SDaniel Walter 	struct net *net;
2206c3968a85SDaniel Walter 	struct in6_addr *addr;
2207c3968a85SDaniel Walter };
2208c3968a85SDaniel Walter 
2209c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2210c3968a85SDaniel Walter {
2211c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2212c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2213c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2214c3968a85SDaniel Walter 
2215d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2216c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2217c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2218c3968a85SDaniel Walter 		/* remove prefsrc entry */
2219c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2220c3968a85SDaniel Walter 	}
2221c3968a85SDaniel Walter 	return 0;
2222c3968a85SDaniel Walter }
2223c3968a85SDaniel Walter 
2224c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2225c3968a85SDaniel Walter {
2226c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2227c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2228c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2229c3968a85SDaniel Walter 		.net = net,
2230c3968a85SDaniel Walter 		.addr = &ifp->addr,
2231c3968a85SDaniel Walter 	};
22320c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
2233c3968a85SDaniel Walter }
2234c3968a85SDaniel Walter 
22358ed67789SDaniel Lezcano struct arg_dev_net {
22368ed67789SDaniel Lezcano 	struct net_device *dev;
22378ed67789SDaniel Lezcano 	struct net *net;
22388ed67789SDaniel Lezcano };
22398ed67789SDaniel Lezcano 
22401da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22411da177e4SLinus Torvalds {
2242bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2243bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22448ed67789SDaniel Lezcano 
2245d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2246c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22471da177e4SLinus Torvalds 		return -1;
2248c159d30cSDavid S. Miller 
22491da177e4SLinus Torvalds 	return 0;
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
2252f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22531da177e4SLinus Torvalds {
22548ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22558ed67789SDaniel Lezcano 		.dev = dev,
22568ed67789SDaniel Lezcano 		.net = net,
22578ed67789SDaniel Lezcano 	};
22588ed67789SDaniel Lezcano 
22590c3584d5SLi RongQing 	fib6_clean_all(net, fib6_ifdown, &adn);
22601e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
226395c96174SEric Dumazet struct rt6_mtu_change_arg {
22641da177e4SLinus Torvalds 	struct net_device *dev;
226595c96174SEric Dumazet 	unsigned int mtu;
22661da177e4SLinus Torvalds };
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22711da177e4SLinus Torvalds 	struct inet6_dev *idev;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22741da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22751da177e4SLinus Torvalds 	   We still use this lock to block changes
22761da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22771da177e4SLinus Torvalds 	*/
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
228038308473SDavid S. Miller 	if (!idev)
22811da177e4SLinus Torvalds 		return 0;
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22841da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22851da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22861da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22871da177e4SLinus Torvalds 	 */
22881da177e4SLinus Torvalds 	/*
22891da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22901da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22911da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22921da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22931da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22941da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22951da177e4SLinus Torvalds 	   PMTU discouvery.
22961da177e4SLinus Torvalds 	 */
2297d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2298d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2299d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2300d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2301d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2302defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2303566cfd8fSSimon Arlott 	}
23041da177e4SLinus Torvalds 	return 0;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
230795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
23081da177e4SLinus Torvalds {
2309c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2310c71099acSThomas Graf 		.dev = dev,
2311c71099acSThomas Graf 		.mtu = mtu,
2312c71099acSThomas Graf 	};
23131da177e4SLinus Torvalds 
23140c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
2317ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23185176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
231986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2320ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
232186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
232286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
232351ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
232486872cb5SThomas Graf };
232586872cb5SThomas Graf 
232686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
232786872cb5SThomas Graf 			      struct fib6_config *cfg)
23281da177e4SLinus Torvalds {
232986872cb5SThomas Graf 	struct rtmsg *rtm;
233086872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
233186872cb5SThomas Graf 	int err;
23321da177e4SLinus Torvalds 
233386872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
233486872cb5SThomas Graf 	if (err < 0)
233586872cb5SThomas Graf 		goto errout;
23361da177e4SLinus Torvalds 
233786872cb5SThomas Graf 	err = -EINVAL;
233886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
233986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
234086872cb5SThomas Graf 
234186872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
234286872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
234386872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
234486872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
234586872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2346ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
234786872cb5SThomas Graf 
2348ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2349ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2350b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2351b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
235286872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
235386872cb5SThomas Graf 
2354ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2355ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2356ab79ad14SMaciej Żenczykowski 
235715e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
235886872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23593b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
236086872cb5SThomas Graf 
236186872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
236286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
236386872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23641da177e4SLinus Torvalds 	}
236586872cb5SThomas Graf 
236686872cb5SThomas Graf 	if (tb[RTA_DST]) {
236786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
236886872cb5SThomas Graf 
236986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
237086872cb5SThomas Graf 			goto errout;
237186872cb5SThomas Graf 
237286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23731da177e4SLinus Torvalds 	}
237486872cb5SThomas Graf 
237586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
237686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
237786872cb5SThomas Graf 
237886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
237986872cb5SThomas Graf 			goto errout;
238086872cb5SThomas Graf 
238186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23821da177e4SLinus Torvalds 	}
238386872cb5SThomas Graf 
2384c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2385c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2386c3968a85SDaniel Walter 
238786872cb5SThomas Graf 	if (tb[RTA_OIF])
238886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
238986872cb5SThomas Graf 
239086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
239186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
239286872cb5SThomas Graf 
239386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
239486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
239586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23961da177e4SLinus Torvalds 	}
239786872cb5SThomas Graf 
239886872cb5SThomas Graf 	if (tb[RTA_TABLE])
239986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
240086872cb5SThomas Graf 
240151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
240251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
240351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
240451ebd318SNicolas Dichtel 	}
240551ebd318SNicolas Dichtel 
240686872cb5SThomas Graf 	err = 0;
240786872cb5SThomas Graf errout:
240886872cb5SThomas Graf 	return err;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
241151ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
241251ebd318SNicolas Dichtel {
241351ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
241451ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
241551ebd318SNicolas Dichtel 	int remaining;
241651ebd318SNicolas Dichtel 	int attrlen;
241751ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
241851ebd318SNicolas Dichtel 
241951ebd318SNicolas Dichtel beginning:
242051ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
242151ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
242251ebd318SNicolas Dichtel 
242351ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
242451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
242551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
242651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
242751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
242851ebd318SNicolas Dichtel 
242951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
243051ebd318SNicolas Dichtel 		if (attrlen > 0) {
243151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
243251ebd318SNicolas Dichtel 
243351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
243451ebd318SNicolas Dichtel 			if (nla) {
243551ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
243651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
243751ebd318SNicolas Dichtel 			}
243851ebd318SNicolas Dichtel 		}
243951ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
244051ebd318SNicolas Dichtel 		if (err) {
244151ebd318SNicolas Dichtel 			last_err = err;
244251ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
244351ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
244451ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
244551ebd318SNicolas Dichtel 			 */
244651ebd318SNicolas Dichtel 			if (add) {
244751ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
244851ebd318SNicolas Dichtel 				 * next hops that have been already added.
244951ebd318SNicolas Dichtel 				 */
245051ebd318SNicolas Dichtel 				add = 0;
245151ebd318SNicolas Dichtel 				goto beginning;
245251ebd318SNicolas Dichtel 			}
245351ebd318SNicolas Dichtel 		}
24541a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
24551a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
24561a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
24571a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
24581a72418bSNicolas Dichtel 		 */
24591a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
246051ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
246151ebd318SNicolas Dichtel 	}
246251ebd318SNicolas Dichtel 
246351ebd318SNicolas Dichtel 	return last_err;
246451ebd318SNicolas Dichtel }
246551ebd318SNicolas Dichtel 
2466661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24671da177e4SLinus Torvalds {
246886872cb5SThomas Graf 	struct fib6_config cfg;
246986872cb5SThomas Graf 	int err;
24701da177e4SLinus Torvalds 
247186872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
247286872cb5SThomas Graf 	if (err < 0)
247386872cb5SThomas Graf 		return err;
247486872cb5SThomas Graf 
247551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
247651ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
247751ebd318SNicolas Dichtel 	else
247886872cb5SThomas Graf 		return ip6_route_del(&cfg);
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds 
2481661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24821da177e4SLinus Torvalds {
248386872cb5SThomas Graf 	struct fib6_config cfg;
248486872cb5SThomas Graf 	int err;
24851da177e4SLinus Torvalds 
248686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
248786872cb5SThomas Graf 	if (err < 0)
248886872cb5SThomas Graf 		return err;
248986872cb5SThomas Graf 
249051ebd318SNicolas Dichtel 	if (cfg.fc_mp)
249151ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
249251ebd318SNicolas Dichtel 	else
249386872cb5SThomas Graf 		return ip6_route_add(&cfg);
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
2496339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2497339bf98fSThomas Graf {
2498339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2499339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2500339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2501339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2502339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2503339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2504339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2505339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2506339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
25076a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2508339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2509339bf98fSThomas Graf }
2510339bf98fSThomas Graf 
2511191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2512191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
25130d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
251415e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
25157bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct rtmsg *rtm;
25181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2519e3703b3dSThomas Graf 	long expires;
25209e762a4aSPatrick McHardy 	u32 table;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
25231da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
25241da177e4SLinus Torvalds 			/* success since this is not a prefix route */
25251da177e4SLinus Torvalds 			return 1;
25261da177e4SLinus Torvalds 		}
25271da177e4SLinus Torvalds 	}
25281da177e4SLinus Torvalds 
252915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
253038308473SDavid S. Miller 	if (!nlh)
253126932566SPatrick McHardy 		return -EMSGSIZE;
25322d7202bfSThomas Graf 
25332d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
25341da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
25351da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
25361da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
25371da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2538c71099acSThomas Graf 	if (rt->rt6i_table)
25399e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2540c71099acSThomas Graf 	else
25419e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
25429e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2543c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2544c78679e8SDavid S. Miller 		goto nla_put_failure;
2545ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2546ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2547ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2548ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2549ef2c7d7bSNicolas Dichtel 			break;
2550ef2c7d7bSNicolas Dichtel 		case -EACCES:
2551ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2552ef2c7d7bSNicolas Dichtel 			break;
2553b4949ab2SNicolas Dichtel 		case -EAGAIN:
2554b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2555b4949ab2SNicolas Dichtel 			break;
2556ef2c7d7bSNicolas Dichtel 		default:
25571da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2558ef2c7d7bSNicolas Dichtel 			break;
2559ef2c7d7bSNicolas Dichtel 		}
2560ef2c7d7bSNicolas Dichtel 	}
2561ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2562ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2563d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
25641da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
25651da177e4SLinus Torvalds 	else
25661da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
25671da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
25681da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
25691da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
25701da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
25711da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2572f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2573f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
25741da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2575f0396f60SDenis Ovsienko 		else
2576f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2577f0396f60SDenis Ovsienko 	}
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
25801da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	if (dst) {
2583c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2584c78679e8SDavid S. Miller 			goto nla_put_failure;
25851da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
25861da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2587c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2588c78679e8SDavid S. Miller 			goto nla_put_failure;
25891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25901da177e4SLinus Torvalds 	if (src) {
2591c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2592c78679e8SDavid S. Miller 			goto nla_put_failure;
25931da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2594c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2595c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2596c78679e8SDavid S. Miller 		goto nla_put_failure;
25971da177e4SLinus Torvalds #endif
25987bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25997bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
26007bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
26018229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
26027bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
26037bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
26047bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
26057bc570c8SYOSHIFUJI Hideaki 						return 0;
26067bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
26077bc570c8SYOSHIFUJI Hideaki 				} else {
26087bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
26097bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
26107bc570c8SYOSHIFUJI Hideaki 				}
26117bc570c8SYOSHIFUJI Hideaki 			}
26127bc570c8SYOSHIFUJI Hideaki 		} else
26137bc570c8SYOSHIFUJI Hideaki #endif
2614c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2615c78679e8SDavid S. Miller 				goto nla_put_failure;
26167bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
26171da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2618c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2619c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2620c78679e8SDavid S. Miller 			goto nla_put_failure;
2621c3968a85SDaniel Walter 	}
2622c3968a85SDaniel Walter 
2623c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2624c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
26254e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2626c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2627c78679e8SDavid S. Miller 			goto nla_put_failure;
26281da177e4SLinus Torvalds 	}
26292d7202bfSThomas Graf 
2630defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
26312d7202bfSThomas Graf 		goto nla_put_failure;
26322d7202bfSThomas Graf 
2633dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2634dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
263594f826b8SEric Dumazet 			goto nla_put_failure;
263694f826b8SEric Dumazet 	}
26372d7202bfSThomas Graf 
2638c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2639c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2640c78679e8SDavid S. Miller 		goto nla_put_failure;
2641c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2642c78679e8SDavid S. Miller 		goto nla_put_failure;
26438253947eSLi Wei 
26448253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
264569cdf8f9SYOSHIFUJI Hideaki 
264687a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2647e3703b3dSThomas Graf 		goto nla_put_failure;
26481da177e4SLinus Torvalds 
26492d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
26502d7202bfSThomas Graf 
26512d7202bfSThomas Graf nla_put_failure:
265226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
265326932566SPatrick McHardy 	return -EMSGSIZE;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
26591da177e4SLinus Torvalds 	int prefix;
26601da177e4SLinus Torvalds 
26612d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
26622d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
26631da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
26641da177e4SLinus Torvalds 	} else
26651da177e4SLinus Torvalds 		prefix = 0;
26661da177e4SLinus Torvalds 
2667191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2668191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
266915e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
26707bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
26711da177e4SLinus Torvalds }
26721da177e4SLinus Torvalds 
2673661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
26741da177e4SLinus Torvalds {
26753b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2676ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26771da177e4SLinus Torvalds 	struct rt6_info *rt;
2678ab364a6fSThomas Graf 	struct sk_buff *skb;
2679ab364a6fSThomas Graf 	struct rtmsg *rtm;
26804c9483b2SDavid S. Miller 	struct flowi6 fl6;
268172331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2682ab364a6fSThomas Graf 
2683ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2684ab364a6fSThomas Graf 	if (err < 0)
2685ab364a6fSThomas Graf 		goto errout;
2686ab364a6fSThomas Graf 
2687ab364a6fSThomas Graf 	err = -EINVAL;
26884c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2689ab364a6fSThomas Graf 
2690ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2691ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2692ab364a6fSThomas Graf 			goto errout;
2693ab364a6fSThomas Graf 
26944e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2695ab364a6fSThomas Graf 	}
2696ab364a6fSThomas Graf 
2697ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2698ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2699ab364a6fSThomas Graf 			goto errout;
2700ab364a6fSThomas Graf 
27014e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2702ab364a6fSThomas Graf 	}
2703ab364a6fSThomas Graf 
2704ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2705ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2706ab364a6fSThomas Graf 
2707ab364a6fSThomas Graf 	if (tb[RTA_OIF])
270872331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2709ab364a6fSThomas Graf 
2710ab364a6fSThomas Graf 	if (iif) {
2711ab364a6fSThomas Graf 		struct net_device *dev;
271272331bc0SShmulik Ladkani 		int flags = 0;
271372331bc0SShmulik Ladkani 
27145578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2715ab364a6fSThomas Graf 		if (!dev) {
2716ab364a6fSThomas Graf 			err = -ENODEV;
2717ab364a6fSThomas Graf 			goto errout;
2718ab364a6fSThomas Graf 		}
271972331bc0SShmulik Ladkani 
272072331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
272172331bc0SShmulik Ladkani 
272272331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
272372331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
272472331bc0SShmulik Ladkani 
272572331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
272672331bc0SShmulik Ladkani 							       flags);
272772331bc0SShmulik Ladkani 	} else {
272872331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
272972331bc0SShmulik Ladkani 
273072331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2731ab364a6fSThomas Graf 	}
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
273438308473SDavid S. Miller 	if (!skb) {
273594e187c0SAmerigo Wang 		ip6_rt_put(rt);
2736ab364a6fSThomas Graf 		err = -ENOBUFS;
2737ab364a6fSThomas Graf 		goto errout;
2738ab364a6fSThomas Graf 	}
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27411da177e4SLinus Torvalds 	   through good chunk of routing engine.
27421da177e4SLinus Torvalds 	 */
2743459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
27441da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
27451da177e4SLinus Torvalds 
2746d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
27471da177e4SLinus Torvalds 
27484c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
274915e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
27507bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
27511da177e4SLinus Torvalds 	if (err < 0) {
2752ab364a6fSThomas Graf 		kfree_skb(skb);
2753ab364a6fSThomas Graf 		goto errout;
27541da177e4SLinus Torvalds 	}
27551da177e4SLinus Torvalds 
275615e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2757ab364a6fSThomas Graf errout:
27581da177e4SLinus Torvalds 	return err;
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
276186872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
27621da177e4SLinus Torvalds {
27631da177e4SLinus Torvalds 	struct sk_buff *skb;
27645578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2765528c4cebSDenis V. Lunev 	u32 seq;
2766528c4cebSDenis V. Lunev 	int err;
27670d51aa80SJamal Hadi Salim 
2768528c4cebSDenis V. Lunev 	err = -ENOBUFS;
276938308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
277086872cb5SThomas Graf 
2771339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
277238308473SDavid S. Miller 	if (!skb)
277321713ebcSThomas Graf 		goto errout;
27741da177e4SLinus Torvalds 
2775191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
277615e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
277726932566SPatrick McHardy 	if (err < 0) {
277826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
277926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
278026932566SPatrick McHardy 		kfree_skb(skb);
278126932566SPatrick McHardy 		goto errout;
278226932566SPatrick McHardy 	}
278315e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
27845578689aSDaniel Lezcano 		    info->nlh, gfp_any());
27851ce85fe4SPablo Neira Ayuso 	return;
278621713ebcSThomas Graf errout:
278721713ebcSThomas Graf 	if (err < 0)
27885578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
27918ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2792351638e7SJiri Pirko 				unsigned long event, void *ptr)
27938ed67789SDaniel Lezcano {
2794351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2795c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27968ed67789SDaniel Lezcano 
27978ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2798d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27998ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
28008ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2801d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
28028ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2803d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
28048ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
28058ed67789SDaniel Lezcano #endif
28068ed67789SDaniel Lezcano 	}
28078ed67789SDaniel Lezcano 
28088ed67789SDaniel Lezcano 	return NOTIFY_OK;
28098ed67789SDaniel Lezcano }
28108ed67789SDaniel Lezcano 
28111da177e4SLinus Torvalds /*
28121da177e4SLinus Torvalds  *	/proc
28131da177e4SLinus Torvalds  */
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28161da177e4SLinus Torvalds 
281733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
281833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
281933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
282033120b30SAlexey Dobriyan 	.read		= seq_read,
282133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
28228d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
282333120b30SAlexey Dobriyan };
282433120b30SAlexey Dobriyan 
28251da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
28261da177e4SLinus Torvalds {
282769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
28281da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
282969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
283069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
283169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
283269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
283369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2834fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
283569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 	return 0;
28381da177e4SLinus Torvalds }
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28411da177e4SLinus Torvalds {
2842de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
284369ddb805SDaniel Lezcano }
284469ddb805SDaniel Lezcano 
28459a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28461da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28471da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28481da177e4SLinus Torvalds 	.read	 = seq_read,
28491da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2850b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28511da177e4SLinus Torvalds };
28521da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds static
2857fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
28581da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28591da177e4SLinus Torvalds {
2860c486da34SLucian Adrian Grijincu 	struct net *net;
2861c486da34SLucian Adrian Grijincu 	int delay;
2862c486da34SLucian Adrian Grijincu 	if (!write)
2863c486da34SLucian Adrian Grijincu 		return -EINVAL;
2864c486da34SLucian Adrian Grijincu 
2865c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2866c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28678d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28682ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
28691da177e4SLinus Torvalds 	return 0;
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
2872fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
28731da177e4SLinus Torvalds 	{
28741da177e4SLinus Torvalds 		.procname	=	"flush",
28754990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28761da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
287789c8b3a1SDave Jones 		.mode		=	0200,
28786d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28791da177e4SLinus Torvalds 	},
28801da177e4SLinus Torvalds 	{
28811da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28829a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28831da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28841da177e4SLinus Torvalds 		.mode		=	0644,
28856d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28861da177e4SLinus Torvalds 	},
28871da177e4SLinus Torvalds 	{
28881da177e4SLinus Torvalds 		.procname	=	"max_size",
28894990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28901da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28911da177e4SLinus Torvalds 		.mode		=	0644,
28926d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28931da177e4SLinus Torvalds 	},
28941da177e4SLinus Torvalds 	{
28951da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28964990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28971da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28981da177e4SLinus Torvalds 		.mode		=	0644,
28996d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29001da177e4SLinus Torvalds 	},
29011da177e4SLinus Torvalds 	{
29021da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
29034990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
29041da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29051da177e4SLinus Torvalds 		.mode		=	0644,
29066d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29071da177e4SLinus Torvalds 	},
29081da177e4SLinus Torvalds 	{
29091da177e4SLinus Torvalds 		.procname	=	"gc_interval",
29104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_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_elasticity",
29174990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
29181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29191da177e4SLinus Torvalds 		.mode		=	0644,
2920f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29211da177e4SLinus Torvalds 	},
29221da177e4SLinus Torvalds 	{
29231da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
29244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
29251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29261da177e4SLinus Torvalds 		.mode		=	0644,
29276d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29281da177e4SLinus Torvalds 	},
29291da177e4SLinus Torvalds 	{
29301da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
29314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
29321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29331da177e4SLinus Torvalds 		.mode		=	0644,
2934f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29351da177e4SLinus Torvalds 	},
29361da177e4SLinus Torvalds 	{
29371da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29401da177e4SLinus Torvalds 		.mode		=	0644,
29416d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29421da177e4SLinus Torvalds 	},
2943f8572d8fSEric W. Biederman 	{ }
29441da177e4SLinus Torvalds };
29451da177e4SLinus Torvalds 
29462c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2947760f2d01SDaniel Lezcano {
2948760f2d01SDaniel Lezcano 	struct ctl_table *table;
2949760f2d01SDaniel Lezcano 
2950760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2951760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2952760f2d01SDaniel Lezcano 			GFP_KERNEL);
29535ee09105SYOSHIFUJI Hideaki 
29545ee09105SYOSHIFUJI Hideaki 	if (table) {
29555ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2956c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
295786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29585ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29595ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29605ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29615ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29625ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29635ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29645ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29659c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2966464dc801SEric W. Biederman 
2967464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2968464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2969464dc801SEric W. Biederman 			table[0].procname = NULL;
29705ee09105SYOSHIFUJI Hideaki 	}
29715ee09105SYOSHIFUJI Hideaki 
2972760f2d01SDaniel Lezcano 	return table;
2973760f2d01SDaniel Lezcano }
29741da177e4SLinus Torvalds #endif
29751da177e4SLinus Torvalds 
29762c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2977cdb18761SDaniel Lezcano {
2978633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29798ed67789SDaniel Lezcano 
298086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
298186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2982f2fc6a54SBenjamin Thery 
2983fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2984fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2985fc66f95cSEric Dumazet 
29868ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29878ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29888ed67789SDaniel Lezcano 					   GFP_KERNEL);
29898ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2990fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2991d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29928ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2993d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
299462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
299562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29968ed67789SDaniel Lezcano 
29978ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29988ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29998ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
30008ed67789SDaniel Lezcano 					       GFP_KERNEL);
300168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
300268fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3003d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
30048ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3005d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
300662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
300762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30088ed67789SDaniel Lezcano 
30098ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
30108ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
30118ed67789SDaniel Lezcano 					       GFP_KERNEL);
301268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
301368fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3014d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
30158ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3016d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
301762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
301862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30198ed67789SDaniel Lezcano #endif
30208ed67789SDaniel Lezcano 
3021b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3022b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3023b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3024b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3025b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3026b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3027b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3028b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3029b339a47cSPeter Zijlstra 
30306891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
30316891a346SBenjamin Thery 
30328ed67789SDaniel Lezcano 	ret = 0;
30338ed67789SDaniel Lezcano out:
30348ed67789SDaniel Lezcano 	return ret;
3035f2fc6a54SBenjamin Thery 
303668fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
303768fffc67SPeter Zijlstra out_ip6_prohibit_entry:
303868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
303968fffc67SPeter Zijlstra out_ip6_null_entry:
304068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
304168fffc67SPeter Zijlstra #endif
3042fc66f95cSEric Dumazet out_ip6_dst_entries:
3043fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3044f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3045f2fc6a54SBenjamin Thery 	goto out;
3046cdb18761SDaniel Lezcano }
3047cdb18761SDaniel Lezcano 
30482c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3049cdb18761SDaniel Lezcano {
30508ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30518ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30528ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30538ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30548ed67789SDaniel Lezcano #endif
305541bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3056cdb18761SDaniel Lezcano }
3057cdb18761SDaniel Lezcano 
3058d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3059d189634eSThomas Graf {
3060d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3061d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3062d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3063d189634eSThomas Graf #endif
3064d189634eSThomas Graf 	return 0;
3065d189634eSThomas Graf }
3066d189634eSThomas Graf 
3067d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3068d189634eSThomas Graf {
3069d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3070ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3071ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3072d189634eSThomas Graf #endif
3073d189634eSThomas Graf }
3074d189634eSThomas Graf 
3075cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3076cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3077cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3078cdb18761SDaniel Lezcano };
3079cdb18761SDaniel Lezcano 
3080c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3081c3426b47SDavid S. Miller {
3082c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3083c3426b47SDavid S. Miller 
3084c3426b47SDavid S. Miller 	if (!bp)
3085c3426b47SDavid S. Miller 		return -ENOMEM;
3086c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3087c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3088c3426b47SDavid S. Miller 	return 0;
3089c3426b47SDavid S. Miller }
3090c3426b47SDavid S. Miller 
3091c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3092c3426b47SDavid S. Miller {
3093c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3094c3426b47SDavid S. Miller 
3095c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
309656a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3097c3426b47SDavid S. Miller 	kfree(bp);
3098c3426b47SDavid S. Miller }
3099c3426b47SDavid S. Miller 
31002b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3101c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3102c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3103c3426b47SDavid S. Miller };
3104c3426b47SDavid S. Miller 
3105d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3106d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3107d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3108d189634eSThomas Graf };
3109d189634eSThomas Graf 
31108ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
31118ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
31128ed67789SDaniel Lezcano 	.priority = 0,
31138ed67789SDaniel Lezcano };
31148ed67789SDaniel Lezcano 
3115433d49c3SDaniel Lezcano int __init ip6_route_init(void)
31161da177e4SLinus Torvalds {
3117433d49c3SDaniel Lezcano 	int ret;
3118433d49c3SDaniel Lezcano 
31199a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
31209a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
31219a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
31229a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
31239a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3124c19a28e1SFernando Carrijo 		goto out;
312514e50e57SDavid S. Miller 
3126fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
31278ed67789SDaniel Lezcano 	if (ret)
3128bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3129bdb3289fSDaniel Lezcano 
3130c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3131c3426b47SDavid S. Miller 	if (ret)
3132e8803b6cSDavid S. Miller 		goto out_dst_entries;
31332a0c451aSThomas Graf 
31347e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
31357e52b33bSDavid S. Miller 	if (ret)
31367e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3137c3426b47SDavid S. Miller 
31385dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31395dc121e9SArnaud Ebalard 
31408ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31418ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31428ed67789SDaniel Lezcano 	 * manually for init_net */
3143d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31448ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3145bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3146d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31478ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3148d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31498ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3150bdb3289fSDaniel Lezcano   #endif
3151e8803b6cSDavid S. Miller 	ret = fib6_init();
3152433d49c3SDaniel Lezcano 	if (ret)
31538ed67789SDaniel Lezcano 		goto out_register_subsys;
3154433d49c3SDaniel Lezcano 
3155433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3156433d49c3SDaniel Lezcano 	if (ret)
3157e8803b6cSDavid S. Miller 		goto out_fib6_init;
3158c35b7e72SDaniel Lezcano 
3159433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3160433d49c3SDaniel Lezcano 	if (ret)
3161433d49c3SDaniel Lezcano 		goto xfrm6_init;
31627e5449c2SDaniel Lezcano 
3163d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3164d189634eSThomas Graf 	if (ret)
3165d189634eSThomas Graf 		goto fib6_rules_init;
3166d189634eSThomas Graf 
3167433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3168c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3169c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3170c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3171d189634eSThomas Graf 		goto out_register_late_subsys;
3172433d49c3SDaniel Lezcano 
31738ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3174cdb18761SDaniel Lezcano 	if (ret)
3175d189634eSThomas Graf 		goto out_register_late_subsys;
31768ed67789SDaniel Lezcano 
3177433d49c3SDaniel Lezcano out:
3178433d49c3SDaniel Lezcano 	return ret;
3179433d49c3SDaniel Lezcano 
3180d189634eSThomas Graf out_register_late_subsys:
3181d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3182433d49c3SDaniel Lezcano fib6_rules_init:
3183433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3184433d49c3SDaniel Lezcano xfrm6_init:
3185433d49c3SDaniel Lezcano 	xfrm6_fini();
31862a0c451aSThomas Graf out_fib6_init:
31872a0c451aSThomas Graf 	fib6_gc_cleanup();
31888ed67789SDaniel Lezcano out_register_subsys:
31898ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31907e52b33bSDavid S. Miller out_register_inetpeer:
31917e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3192fc66f95cSEric Dumazet out_dst_entries:
3193fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3194433d49c3SDaniel Lezcano out_kmem_cache:
3195f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3196433d49c3SDaniel Lezcano 	goto out;
31971da177e4SLinus Torvalds }
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds void ip6_route_cleanup(void)
32001da177e4SLinus Torvalds {
32018ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3202d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3203101367c2SThomas Graf 	fib6_rules_cleanup();
32041da177e4SLinus Torvalds 	xfrm6_fini();
32051da177e4SLinus Torvalds 	fib6_gc_cleanup();
3206c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
32078ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
320841bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3209f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
32101da177e4SLinus Torvalds }
3211