xref: /openbmc/linux/net/ipv6/route.c (revision 1f56a01f4ed1bbb36ff2a97f75a6e6231d790cff)
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);
87aad88724SEric Dumazet static int		ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb);
887150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
89aad88724SEric Dumazet static int		ip6_pkt_prohibit_out(struct sock *sk, 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))
1443b471175SMartin KaFai Lau 		return dst_cow_metrics_generic(dst, old);
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,
1971da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1981da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1991da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2000dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
201ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
20206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
2031da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2041da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2051da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2061da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2071da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2086e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2091ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
210d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
213ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
214ec831ea7SRoland Dreier {
215618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
216618f9bc7SSteffen Klassert 
217618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
218ec831ea7SRoland Dreier }
219ec831ea7SRoland Dreier 
2206700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2216700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
22214e50e57SDavid S. Miller {
22314e50e57SDavid S. Miller }
22414e50e57SDavid S. Miller 
2256700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2266700c270SDavid S. Miller 				      struct sk_buff *skb)
227b587ee3bSDavid S. Miller {
228b587ee3bSDavid S. Miller }
229b587ee3bSDavid S. Miller 
2300972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
2310972ddb2SHeld Bernhard 					 unsigned long old)
2320972ddb2SHeld Bernhard {
2330972ddb2SHeld Bernhard 	return NULL;
2340972ddb2SHeld Bernhard }
2350972ddb2SHeld Bernhard 
23614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
23714e50e57SDavid S. Miller 	.family			=	AF_INET6,
23814e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
23914e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
240ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
241214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
24214e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
243b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2440972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
245d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
24614e50e57SDavid S. Miller };
24714e50e57SDavid S. Miller 
24862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
24914edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
25062fa8a84SDavid S. Miller };
25162fa8a84SDavid S. Miller 
252fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2531da177e4SLinus Torvalds 	.dst = {
2541da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2551da177e4SLinus Torvalds 		.__use		= 1,
2562c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2571da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2581da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2591da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2601da177e4SLinus Torvalds 	},
2611da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2624f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2631da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2641da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2651da177e4SLinus Torvalds };
2661da177e4SLinus Torvalds 
267101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
268101367c2SThomas Graf 
269fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
270101367c2SThomas Graf 	.dst = {
271101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
272101367c2SThomas Graf 		.__use		= 1,
2732c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
274101367c2SThomas Graf 		.error		= -EACCES,
2759ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2769ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
277101367c2SThomas Graf 	},
278101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2794f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
280101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
281101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
282101367c2SThomas Graf };
283101367c2SThomas Graf 
284fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
285101367c2SThomas Graf 	.dst = {
286101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
287101367c2SThomas Graf 		.__use		= 1,
2882c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
289101367c2SThomas Graf 		.error		= -EINVAL,
290352e512cSHerbert Xu 		.input		= dst_discard,
291aad88724SEric Dumazet 		.output		= dst_discard_sk,
292101367c2SThomas Graf 	},
293101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2944f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
295101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
296101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
297101367c2SThomas Graf };
298101367c2SThomas Graf 
299101367c2SThomas Graf #endif
300101367c2SThomas Graf 
3011da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
30297bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
303957c665fSDavid S. Miller 					     struct net_device *dev,
3048b96d22dSDavid S. Miller 					     int flags,
3058b96d22dSDavid S. Miller 					     struct fib6_table *table)
3061da177e4SLinus Torvalds {
30797bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
3086f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
309cf911662SDavid S. Miller 
31097bab73fSDavid S. Miller 	if (rt) {
3118104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
3128104891bSSteffen Klassert 
3138104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
3148b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
31551ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
31697bab73fSDavid S. Miller 	}
317cf911662SDavid S. Miller 	return rt;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3231da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
324ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
3251da177e4SLinus Torvalds 
3268e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
3278e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3288e2ec639SYan, Zheng 
32938308473SDavid S. Miller 	if (idev) {
3301da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3311da177e4SLinus Torvalds 		in6_dev_put(idev);
3321da177e4SLinus Torvalds 	}
3331716a961SGao feng 
334ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
335ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3361716a961SGao feng 
33797bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
33897bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
339b3419363SDavid S. Miller 		inet_putpeer(peer);
340b3419363SDavid S. Miller 	}
341b3419363SDavid S. Miller }
342b3419363SDavid S. Miller 
3431da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3441da177e4SLinus Torvalds 			   int how)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3471da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3485a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
349c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3501da177e4SLinus Torvalds 
35197cac082SDavid S. Miller 	if (dev != loopback_dev) {
35297cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3535a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3545a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
35538308473SDavid S. Miller 			if (loopback_idev) {
3561da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3571da177e4SLinus Torvalds 				in6_dev_put(idev);
3581da177e4SLinus Torvalds 			}
3591da177e4SLinus Torvalds 		}
36097cac082SDavid S. Miller 	}
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3641da177e4SLinus Torvalds {
3651716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3661716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
367a50feda5SEric Dumazet 			return true;
3681716a961SGao feng 	} else if (rt->dst.from) {
3693fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3701716a961SGao feng 	}
371a50feda5SEric Dumazet 	return false;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
37451ebd318SNicolas Dichtel /* Multipath route selection:
37551ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
37651ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
37751ebd318SNicolas Dichtel  */
37851ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
37951ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
38051ebd318SNicolas Dichtel {
38151ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
38251ebd318SNicolas Dichtel 
383c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
384c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
38551ebd318SNicolas Dichtel 
38651ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
38751ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
38851ebd318SNicolas Dichtel 	case IPPROTO_UDP:
38951ebd318SNicolas Dichtel 	case IPPROTO_TCP:
39051ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
391b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
392b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
39351ebd318SNicolas Dichtel 		break;
39451ebd318SNicolas Dichtel 
39551ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
396b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
397b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
39851ebd318SNicolas Dichtel 		break;
39951ebd318SNicolas Dichtel 	}
40051ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
401b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
40251ebd318SNicolas Dichtel 
40351ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
40451ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
40551ebd318SNicolas Dichtel 	return val % candidate_count;
40651ebd318SNicolas Dichtel }
40751ebd318SNicolas Dichtel 
40851ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
40952bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
41052bd4c0cSNicolas Dichtel 					     int strict)
41151ebd318SNicolas Dichtel {
41251ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
41351ebd318SNicolas Dichtel 	int route_choosen;
41451ebd318SNicolas Dichtel 
41551ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
41651ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
41751ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
41851ebd318SNicolas Dichtel 	 */
41951ebd318SNicolas Dichtel 	if (route_choosen)
42051ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
42151ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
42251ebd318SNicolas Dichtel 			route_choosen--;
42351ebd318SNicolas Dichtel 			if (route_choosen == 0) {
42452bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
42552bd4c0cSNicolas Dichtel 					break;
42651ebd318SNicolas Dichtel 				match = sibling;
42751ebd318SNicolas Dichtel 				break;
42851ebd318SNicolas Dichtel 			}
42951ebd318SNicolas Dichtel 		}
43051ebd318SNicolas Dichtel 	return match;
43151ebd318SNicolas Dichtel }
43251ebd318SNicolas Dichtel 
4331da177e4SLinus Torvalds /*
434c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds 
4378ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4388ed67789SDaniel Lezcano 						    struct rt6_info *rt,
439b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4401da177e4SLinus Torvalds 						    int oif,
441d420895eSYOSHIFUJI Hideaki 						    int flags)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4441da177e4SLinus Torvalds 	struct rt6_info *sprt;
4451da177e4SLinus Torvalds 
446dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
447dd3abc4eSYOSHIFUJI Hideaki 		goto out;
448dd3abc4eSYOSHIFUJI Hideaki 
449d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
450d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
451dd3abc4eSYOSHIFUJI Hideaki 
452dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4531da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4541da177e4SLinus Torvalds 				return sprt;
4551da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
45638308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4571da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
458d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4591da177e4SLinus Torvalds 						continue;
4601da177e4SLinus Torvalds 					if (local && (!oif ||
4611da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4621da177e4SLinus Torvalds 						continue;
4631da177e4SLinus Torvalds 				}
4641da177e4SLinus Torvalds 				local = sprt;
4651da177e4SLinus Torvalds 			}
466dd3abc4eSYOSHIFUJI Hideaki 		} else {
467dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
468dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
469dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
470dd3abc4eSYOSHIFUJI Hideaki 		}
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
473dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4741da177e4SLinus Torvalds 		if (local)
4751da177e4SLinus Torvalds 			return local;
4761da177e4SLinus Torvalds 
477d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4788ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4791da177e4SLinus Torvalds 	}
480dd3abc4eSYOSHIFUJI Hideaki out:
4811da177e4SLinus Torvalds 	return rt;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
48427097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
485c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
486c2f17e82SHannes Frederic Sowa 	struct work_struct work;
487c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
488c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
489c2f17e82SHannes Frederic Sowa };
490c2f17e82SHannes Frederic Sowa 
491c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
492c2f17e82SHannes Frederic Sowa {
493c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
494c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
495c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
496c2f17e82SHannes Frederic Sowa 
497c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
498c2f17e82SHannes Frederic Sowa 	ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
499c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
500662f5533SMichael Büsch 	kfree(work);
501c2f17e82SHannes Frederic Sowa }
502c2f17e82SHannes Frederic Sowa 
50327097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
50427097255SYOSHIFUJI Hideaki {
505f2c31e32SEric Dumazet 	struct neighbour *neigh;
50627097255SYOSHIFUJI Hideaki 	/*
50727097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
50827097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
50927097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
51027097255SYOSHIFUJI Hideaki 	 *
51127097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
51227097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
51327097255SYOSHIFUJI Hideaki 	 */
5142152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
515fdd6681dSAmerigo Wang 		return;
5162152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5172152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
5182152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5192152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
5202152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
5212152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
5227ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
5232152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5242152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
52552e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
526c2f17e82SHannes Frederic Sowa 		struct __rt6_probe_work *work;
52727097255SYOSHIFUJI Hideaki 
528c2f17e82SHannes Frederic Sowa 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
529c2f17e82SHannes Frederic Sowa 
530c2f17e82SHannes Frederic Sowa 		if (neigh && work)
5317e980569SJiri Benc 			__neigh_set_probe_once(neigh);
5322152caeaSYOSHIFUJI Hideaki / 吉藤英明 
533c2f17e82SHannes Frederic Sowa 		if (neigh)
534c2f17e82SHannes Frederic Sowa 			write_unlock(&neigh->lock);
535c2f17e82SHannes Frederic Sowa 
536c2f17e82SHannes Frederic Sowa 		if (work) {
537c2f17e82SHannes Frederic Sowa 			INIT_WORK(&work->work, rt6_probe_deferred);
538c2f17e82SHannes Frederic Sowa 			work->target = rt->rt6i_gateway;
539c2f17e82SHannes Frederic Sowa 			dev_hold(rt->dst.dev);
540c2f17e82SHannes Frederic Sowa 			work->dev = rt->dst.dev;
541c2f17e82SHannes Frederic Sowa 			schedule_work(&work->work);
542c2f17e82SHannes Frederic Sowa 		}
543f2c31e32SEric Dumazet 	} else {
5442152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5452152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
54627097255SYOSHIFUJI Hideaki 	}
5472152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
548f2c31e32SEric Dumazet }
54927097255SYOSHIFUJI Hideaki #else
55027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
55127097255SYOSHIFUJI Hideaki {
55227097255SYOSHIFUJI Hideaki }
55327097255SYOSHIFUJI Hideaki #endif
55427097255SYOSHIFUJI Hideaki 
5551da177e4SLinus Torvalds /*
556554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5571da177e4SLinus Torvalds  */
558b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5591da177e4SLinus Torvalds {
560d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
561161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
562554cfb7eSYOSHIFUJI Hideaki 		return 2;
563161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
564161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
565161980f4SDavid S. Miller 		return 1;
566554cfb7eSYOSHIFUJI Hideaki 	return 0;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
569afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5701da177e4SLinus Torvalds {
571f2c31e32SEric Dumazet 	struct neighbour *neigh;
572afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
573f2c31e32SEric Dumazet 
5744d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5754d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
576afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
577145a3621SYOSHIFUJI Hideaki / 吉藤英明 
578145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
579145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
580145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
581145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
582554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
583afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
584398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
585a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
586afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
5877e980569SJiri Benc 		else
5887e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
589398bcbebSYOSHIFUJI Hideaki #endif
590145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
591afc154e9SHannes Frederic Sowa 	} else {
592afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
5937e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
594a5a81f0bSPaul Marks 	}
595145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
596145a3621SYOSHIFUJI Hideaki / 吉藤英明 
597a5a81f0bSPaul Marks 	return ret;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
600554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
601554cfb7eSYOSHIFUJI Hideaki 			   int strict)
602554cfb7eSYOSHIFUJI Hideaki {
603a5a81f0bSPaul Marks 	int m;
6044d0c5911SYOSHIFUJI Hideaki 
6054d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
60677d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
607afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
608ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
609ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
610ebacaaa0SYOSHIFUJI Hideaki #endif
611afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
612afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
613afc154e9SHannes Frederic Sowa 		if (n < 0)
614afc154e9SHannes Frederic Sowa 			return n;
615afc154e9SHannes Frederic Sowa 	}
616554cfb7eSYOSHIFUJI Hideaki 	return m;
617554cfb7eSYOSHIFUJI Hideaki }
618554cfb7eSYOSHIFUJI Hideaki 
619f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
620afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
621afc154e9SHannes Frederic Sowa 				   bool *do_rr)
622554cfb7eSYOSHIFUJI Hideaki {
623554cfb7eSYOSHIFUJI Hideaki 	int m;
624afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
625554cfb7eSYOSHIFUJI Hideaki 
626554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
627f11e6659SDavid S. Miller 		goto out;
628554cfb7eSYOSHIFUJI Hideaki 
629554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6307e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
631afc154e9SHannes Frederic Sowa 		match_do_rr = true;
632afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6337e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
634f11e6659SDavid S. Miller 		goto out;
6351da177e4SLinus Torvalds 	}
636f11e6659SDavid S. Miller 
637afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
638afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
639afc154e9SHannes Frederic Sowa 
6407e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
641afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
642afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
643afc154e9SHannes Frederic Sowa 		*mpri = m;
644afc154e9SHannes Frederic Sowa 		match = rt;
645afc154e9SHannes Frederic Sowa 	}
646f11e6659SDavid S. Miller out:
647f11e6659SDavid S. Miller 	return match;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
650f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
651f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
652afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
653afc154e9SHannes Frederic Sowa 				     bool *do_rr)
654f11e6659SDavid S. Miller {
655f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
656f11e6659SDavid S. Miller 	int mpri = -1;
657f11e6659SDavid S. Miller 
658f11e6659SDavid S. Miller 	match = NULL;
659f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
660d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
661afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
662f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && 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 
666f11e6659SDavid S. Miller 	return match;
667f11e6659SDavid S. Miller }
668f11e6659SDavid S. Miller 
669f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
670f11e6659SDavid S. Miller {
671f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6728ed67789SDaniel Lezcano 	struct net *net;
673afc154e9SHannes Frederic Sowa 	bool do_rr = false;
674f11e6659SDavid S. Miller 
675f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
676f11e6659SDavid S. Miller 	if (!rt0)
677f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
678f11e6659SDavid S. Miller 
679afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
680afc154e9SHannes Frederic Sowa 			     &do_rr);
681f11e6659SDavid S. Miller 
682afc154e9SHannes Frederic Sowa 	if (do_rr) {
683d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
684f11e6659SDavid S. Miller 
685554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
686f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
687f11e6659SDavid S. Miller 			next = fn->leaf;
688f11e6659SDavid S. Miller 
689f11e6659SDavid S. Miller 		if (next != rt0)
690f11e6659SDavid S. Miller 			fn->rr_ptr = next;
691554cfb7eSYOSHIFUJI Hideaki 	}
692554cfb7eSYOSHIFUJI Hideaki 
693d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
694a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
69770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
69870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
699b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
70070ceb4f5SYOSHIFUJI Hideaki {
701c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
70270ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
70370ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
70470ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7054bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
70670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
70770ceb4f5SYOSHIFUJI Hideaki 
70870ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
70970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71070ceb4f5SYOSHIFUJI Hideaki 	}
71170ceb4f5SYOSHIFUJI Hideaki 
71270ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
71370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
71470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
71670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
71870ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
71970ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
72070ceb4f5SYOSHIFUJI Hideaki 		}
72170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
72270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
72370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
72470ceb4f5SYOSHIFUJI Hideaki 		}
72570ceb4f5SYOSHIFUJI Hideaki 	}
72670ceb4f5SYOSHIFUJI Hideaki 
72770ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
72870ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
7293933fc95SJens Rosenboom 		return -EINVAL;
73070ceb4f5SYOSHIFUJI Hideaki 
7314bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
73270ceb4f5SYOSHIFUJI Hideaki 
73370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
73470ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
73570ceb4f5SYOSHIFUJI Hideaki 	else {
73670ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
73770ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
73870ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
73970ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
74070ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
74170ceb4f5SYOSHIFUJI Hideaki 	}
74270ceb4f5SYOSHIFUJI Hideaki 
743f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
744f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
745f104a567SDuan Jiong 	else
746f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
747f104a567SDuan Jiong 					gwaddr, dev->ifindex);
74870ceb4f5SYOSHIFUJI Hideaki 
74970ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
750e0a1ad73SThomas Graf 		ip6_del_rt(rt);
75170ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
75270ceb4f5SYOSHIFUJI Hideaki 	}
75370ceb4f5SYOSHIFUJI Hideaki 
75470ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
755efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
75670ceb4f5SYOSHIFUJI Hideaki 					pref);
75770ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
75870ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
75970ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
76070ceb4f5SYOSHIFUJI Hideaki 
76170ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7621716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7631716a961SGao feng 			rt6_clean_expires(rt);
7641716a961SGao feng 		else
7651716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7661716a961SGao feng 
76794e187c0SAmerigo Wang 		ip6_rt_put(rt);
76870ceb4f5SYOSHIFUJI Hideaki 	}
76970ceb4f5SYOSHIFUJI Hideaki 	return 0;
77070ceb4f5SYOSHIFUJI Hideaki }
77170ceb4f5SYOSHIFUJI Hideaki #endif
77270ceb4f5SYOSHIFUJI Hideaki 
773a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
774a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
775a3c00e46SMartin KaFai Lau {
776a3c00e46SMartin KaFai Lau 	struct fib6_node *pn;
777a3c00e46SMartin KaFai Lau 	while (1) {
778a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
779a3c00e46SMartin KaFai Lau 			return NULL;
780a3c00e46SMartin KaFai Lau 		pn = fn->parent;
781a3c00e46SMartin KaFai Lau 		if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
782a3c00e46SMartin KaFai Lau 			fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
783a3c00e46SMartin KaFai Lau 		else
784a3c00e46SMartin KaFai Lau 			fn = pn;
785a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
786a3c00e46SMartin KaFai Lau 			return fn;
787a3c00e46SMartin KaFai Lau 	}
788a3c00e46SMartin KaFai Lau }
789c71099acSThomas Graf 
7908ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7918ed67789SDaniel Lezcano 					     struct fib6_table *table,
7924c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	struct fib6_node *fn;
7951da177e4SLinus Torvalds 	struct rt6_info *rt;
7961da177e4SLinus Torvalds 
797c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7984c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
799c71099acSThomas Graf restart:
800c71099acSThomas Graf 	rt = fn->leaf;
8014c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
80251ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
80352bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
804a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
805a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
806a3c00e46SMartin KaFai Lau 		if (fn)
807a3c00e46SMartin KaFai Lau 			goto restart;
808a3c00e46SMartin KaFai Lau 	}
809d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
810c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8111da177e4SLinus Torvalds 	return rt;
812c71099acSThomas Graf 
813c71099acSThomas Graf }
814c71099acSThomas Graf 
815ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
816ea6e574eSFlorian Westphal 				    int flags)
817ea6e574eSFlorian Westphal {
818ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
819ea6e574eSFlorian Westphal }
820ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
821ea6e574eSFlorian Westphal 
8229acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
8239acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
824c71099acSThomas Graf {
8254c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8264c9483b2SDavid S. Miller 		.flowi6_oif = oif,
8274c9483b2SDavid S. Miller 		.daddr = *daddr,
828c71099acSThomas Graf 	};
829c71099acSThomas Graf 	struct dst_entry *dst;
83077d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
831c71099acSThomas Graf 
832adaa70bbSThomas Graf 	if (saddr) {
8334c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
834adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
835adaa70bbSThomas Graf 	}
836adaa70bbSThomas Graf 
8374c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
838c71099acSThomas Graf 	if (dst->error == 0)
839c71099acSThomas Graf 		return (struct rt6_info *) dst;
840c71099acSThomas Graf 
841c71099acSThomas Graf 	dst_release(dst);
842c71099acSThomas Graf 
8431da177e4SLinus Torvalds 	return NULL;
8441da177e4SLinus Torvalds }
8457159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8467159039aSYOSHIFUJI Hideaki 
847c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8481da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8491da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8501da177e4SLinus Torvalds    be destroyed.
8511da177e4SLinus Torvalds  */
8521da177e4SLinus Torvalds 
853e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
854e715b6d3SFlorian Westphal 			struct mx6_config *mxc)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	int err;
857c71099acSThomas Graf 	struct fib6_table *table;
8581da177e4SLinus Torvalds 
859c71099acSThomas Graf 	table = rt->rt6i_table;
860c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
861e715b6d3SFlorian Westphal 	err = fib6_add(&table->tb6_root, rt, info, mxc);
862c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	return err;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
86740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
86840e22e8fSThomas Graf {
869e715b6d3SFlorian Westphal 	struct nl_info info = {	.nl_net = dev_net(rt->dst.dev), };
870e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
871e715b6d3SFlorian Westphal 
872e715b6d3SFlorian Westphal 	return __ip6_ins_rt(rt, &info, &mxc);
87340e22e8fSThomas Graf }
87440e22e8fSThomas Graf 
8751716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
87621efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
877b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	struct rt6_info *rt;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	/*
8821da177e4SLinus Torvalds 	 *	Clone the route.
8831da177e4SLinus Torvalds 	 */
8841da177e4SLinus Torvalds 
88521efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (rt) {
888bb3c3686SDavid S. Miller 		if (ort->rt6i_dst.plen != 128 &&
88921efcfa0SEric Dumazet 		    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
89058c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8951da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8964e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8971da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds #endif
90095a9a5baSYOSHIFUJI Hideaki 	}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	return rt;
9031da177e4SLinus Torvalds }
90495a9a5baSYOSHIFUJI Hideaki 
90521efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
90621efcfa0SEric Dumazet 					const struct in6_addr *daddr)
907299d9939SYOSHIFUJI Hideaki {
90821efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
90921efcfa0SEric Dumazet 
910887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
911299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
912299d9939SYOSHIFUJI Hideaki 	return rt;
913299d9939SYOSHIFUJI Hideaki }
914299d9939SYOSHIFUJI Hideaki 
9158ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
9164c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
9171da177e4SLinus Torvalds {
918367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
919519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
920c71099acSThomas Graf 	int strict = 0;
9211da177e4SLinus Torvalds 	int attempts = 3;
922519fbd87SYOSHIFUJI Hideaki 	int err;
9231da177e4SLinus Torvalds 
92477d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
925367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
926367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
9271da177e4SLinus Torvalds 
928a3c00e46SMartin KaFai Lau redo_fib6_lookup_lock:
929c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
9301da177e4SLinus Torvalds 
9314c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
932367efcb9SMartin KaFai Lau 	saved_fn = fn;
9331da177e4SLinus Torvalds 
934a3c00e46SMartin KaFai Lau redo_rt6_select:
935367efcb9SMartin KaFai Lau 	rt = rt6_select(fn, oif, strict);
93652bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
937367efcb9SMartin KaFai Lau 		rt = rt6_multipath_select(rt, fl6, oif, strict);
938a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
939a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
940a3c00e46SMartin KaFai Lau 		if (fn)
941a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
942367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
943367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
944367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
945367efcb9SMartin KaFai Lau 			fn = saved_fn;
946367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
947367efcb9SMartin KaFai Lau 		} else {
948367efcb9SMartin KaFai Lau 			dst_hold(&rt->dst);
949367efcb9SMartin KaFai Lau 			read_unlock_bh(&table->tb6_lock);
950367efcb9SMartin KaFai Lau 			goto out2;
951367efcb9SMartin KaFai Lau 		}
952a3c00e46SMartin KaFai Lau 	}
953a3c00e46SMartin KaFai Lau 
954d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
955c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9561da177e4SLinus Torvalds 
95794c77bb4SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_CACHE)
95894c77bb4SMartin KaFai Lau 		goto out2;
95994c77bb4SMartin KaFai Lau 
960c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
9614c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9627343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9634c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9647343ff31SDavid S. Miller 	else
9657343ff31SDavid S. Miller 		goto out2;
9661da177e4SLinus Torvalds 
96794e187c0SAmerigo Wang 	ip6_rt_put(rt);
9688ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9691da177e4SLinus Torvalds 
970d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
971e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
97240e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
973e40cf353SYOSHIFUJI Hideaki 		if (!err)
974e40cf353SYOSHIFUJI Hideaki 			goto out2;
975e40cf353SYOSHIFUJI Hideaki 	}
976e40cf353SYOSHIFUJI Hideaki 
977e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9781da177e4SLinus Torvalds 		goto out2;
9791da177e4SLinus Torvalds 
980519fbd87SYOSHIFUJI Hideaki 	/*
981c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
982519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9831da177e4SLinus Torvalds 	 */
98494e187c0SAmerigo Wang 	ip6_rt_put(rt);
985a3c00e46SMartin KaFai Lau 	goto redo_fib6_lookup_lock;
986e40cf353SYOSHIFUJI Hideaki 
9871da177e4SLinus Torvalds out2:
988d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
989d8d1f30bSChangli Gao 	rt->dst.__use++;
990c71099acSThomas Graf 
991c71099acSThomas Graf 	return rt;
992c71099acSThomas Graf }
993c71099acSThomas Graf 
9948ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9954c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9964acad72dSPavel Emelyanov {
9974c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9984acad72dSPavel Emelyanov }
9994acad72dSPavel Emelyanov 
100072331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
100172331bc0SShmulik Ladkani 						struct net_device *dev,
100272331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
100372331bc0SShmulik Ladkani {
100472331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
100572331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
100672331bc0SShmulik Ladkani 
100772331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
100872331bc0SShmulik Ladkani }
100972331bc0SShmulik Ladkani 
1010c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1011c71099acSThomas Graf {
1012b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1013c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1014adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
10154c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
10164c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
10174c9483b2SDavid S. Miller 		.daddr = iph->daddr,
10184c9483b2SDavid S. Miller 		.saddr = iph->saddr,
10196502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
10204c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
10214c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1022c71099acSThomas Graf 	};
1023adaa70bbSThomas Graf 
102472331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1025c71099acSThomas Graf }
1026c71099acSThomas Graf 
10278ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
10284c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1029c71099acSThomas Graf {
10304c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1031c71099acSThomas Graf }
1032c71099acSThomas Graf 
10339c7a4f9cSFlorian Westphal struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
10344c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
1035c71099acSThomas Graf {
1036c71099acSThomas Graf 	int flags = 0;
1037c71099acSThomas Graf 
10381fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
10394dc27d1cSDavid McCullough 
10404c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
104177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1042c71099acSThomas Graf 
10434c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1044adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10450c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10460c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1047adaa70bbSThomas Graf 
10484c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10491da177e4SLinus Torvalds }
10507159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10511da177e4SLinus Torvalds 
10522774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
105314e50e57SDavid S. Miller {
10545c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
105514e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
105614e50e57SDavid S. Miller 
1057f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
105814e50e57SDavid S. Miller 	if (rt) {
1059d8d1f30bSChangli Gao 		new = &rt->dst;
106014e50e57SDavid S. Miller 
10618104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10628104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10638104891bSSteffen Klassert 
106414e50e57SDavid S. Miller 		new->__use = 1;
1065352e512cSHerbert Xu 		new->input = dst_discard;
1066aad88724SEric Dumazet 		new->output = dst_discard_sk;
106714e50e57SDavid S. Miller 
106821efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
106921efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
107021efcfa0SEric Dumazet 		else
1071defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
107214e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
107314e50e57SDavid S. Miller 		if (rt->rt6i_idev)
107414e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
107514e50e57SDavid S. Miller 
10764e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10771716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
107814e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
107914e50e57SDavid S. Miller 
108014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
108114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
108214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
108314e50e57SDavid S. Miller #endif
108414e50e57SDavid S. Miller 
108514e50e57SDavid S. Miller 		dst_free(new);
108614e50e57SDavid S. Miller 	}
108714e50e57SDavid S. Miller 
108869ead7afSDavid S. Miller 	dst_release(dst_orig);
108969ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
109014e50e57SDavid S. Miller }
109114e50e57SDavid S. Miller 
10921da177e4SLinus Torvalds /*
10931da177e4SLinus Torvalds  *	Destination cache support functions
10941da177e4SLinus Torvalds  */
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10971da177e4SLinus Torvalds {
10981da177e4SLinus Torvalds 	struct rt6_info *rt;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
11011da177e4SLinus Torvalds 
11026f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
11036f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
11046f3118b5SNicolas Dichtel 	 * into this function always.
11056f3118b5SNicolas Dichtel 	 */
1106e3bc10bdSHannes Frederic Sowa 	if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
11071da177e4SLinus Torvalds 		return NULL;
1108e3bc10bdSHannes Frederic Sowa 
1109e3bc10bdSHannes Frederic Sowa 	if (rt6_check_expired(rt))
1110e3bc10bdSHannes Frederic Sowa 		return NULL;
1111e3bc10bdSHannes Frederic Sowa 
1112e3bc10bdSHannes Frederic Sowa 	return dst;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	if (rt) {
112054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
112154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1122e0a1ad73SThomas Graf 				ip6_del_rt(rt);
112354c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
11241da177e4SLinus Torvalds 			}
112554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
112654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
112754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
112854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
112954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
113054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
11341da177e4SLinus Torvalds {
11351da177e4SLinus Torvalds 	struct rt6_info *rt;
11361da177e4SLinus Torvalds 
11373ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11381da177e4SLinus Torvalds 
1139adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11401da177e4SLinus Torvalds 	if (rt) {
11411eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11421eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11431eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11441eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11451eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11461da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11471da177e4SLinus Torvalds 		}
11481da177e4SLinus Torvalds 	}
11491eb4f758SHannes Frederic Sowa }
11501da177e4SLinus Torvalds 
11516700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11526700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
11551da177e4SLinus Torvalds 
115681aded24SDavid S. Miller 	dst_confirm(dst);
11571da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
115881aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
115981aded24SDavid S. Miller 
11601da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11619d289715SHagen Paul Pfeifer 		if (mtu < IPV6_MIN_MTU)
11621da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
11639d289715SHagen Paul Pfeifer 
1164defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
116581aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
116942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
117042ae66c8SDavid S. Miller 		     int oif, u32 mark)
117181aded24SDavid S. Miller {
117281aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
117381aded24SDavid S. Miller 	struct dst_entry *dst;
117481aded24SDavid S. Miller 	struct flowi6 fl6;
117581aded24SDavid S. Miller 
117681aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
117781aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
11781b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
117981aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
118081aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11816502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
118281aded24SDavid S. Miller 
118381aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
118481aded24SDavid S. Miller 	if (!dst->error)
11856700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
118681aded24SDavid S. Miller 	dst_release(dst);
118781aded24SDavid S. Miller }
118881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
118981aded24SDavid S. Miller 
119081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
119181aded24SDavid S. Miller {
119281aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
119381aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
119481aded24SDavid S. Miller }
119581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
119681aded24SDavid S. Miller 
1197b55b76b2SDuan Jiong /* Handle redirects */
1198b55b76b2SDuan Jiong struct ip6rd_flowi {
1199b55b76b2SDuan Jiong 	struct flowi6 fl6;
1200b55b76b2SDuan Jiong 	struct in6_addr gateway;
1201b55b76b2SDuan Jiong };
1202b55b76b2SDuan Jiong 
1203b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
1204b55b76b2SDuan Jiong 					     struct fib6_table *table,
1205b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
1206b55b76b2SDuan Jiong 					     int flags)
1207b55b76b2SDuan Jiong {
1208b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1209b55b76b2SDuan Jiong 	struct rt6_info *rt;
1210b55b76b2SDuan Jiong 	struct fib6_node *fn;
1211b55b76b2SDuan Jiong 
1212b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
1213b55b76b2SDuan Jiong 	 * check if the redirect has come from approriate router.
1214b55b76b2SDuan Jiong 	 *
1215b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
1216b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
1217b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
1218b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
1219b55b76b2SDuan Jiong 	 * routes.
1220b55b76b2SDuan Jiong 	 */
1221b55b76b2SDuan Jiong 
1222b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
1223b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1224b55b76b2SDuan Jiong restart:
1225b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1226b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
1227b55b76b2SDuan Jiong 			continue;
1228b55b76b2SDuan Jiong 		if (rt->dst.error)
1229b55b76b2SDuan Jiong 			break;
1230b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1231b55b76b2SDuan Jiong 			continue;
1232b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1233b55b76b2SDuan Jiong 			continue;
1234b55b76b2SDuan Jiong 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1235b55b76b2SDuan Jiong 			continue;
1236b55b76b2SDuan Jiong 		break;
1237b55b76b2SDuan Jiong 	}
1238b55b76b2SDuan Jiong 
1239b55b76b2SDuan Jiong 	if (!rt)
1240b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1241b55b76b2SDuan Jiong 	else if (rt->dst.error) {
1242b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1243b0a1ba59SMartin KaFai Lau 		goto out;
1244b0a1ba59SMartin KaFai Lau 	}
1245b0a1ba59SMartin KaFai Lau 
1246b0a1ba59SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
1247a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1248a3c00e46SMartin KaFai Lau 		if (fn)
1249a3c00e46SMartin KaFai Lau 			goto restart;
1250b55b76b2SDuan Jiong 	}
1251a3c00e46SMartin KaFai Lau 
1252b0a1ba59SMartin KaFai Lau out:
1253b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
1254b55b76b2SDuan Jiong 
1255b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
1256b55b76b2SDuan Jiong 
1257b55b76b2SDuan Jiong 	return rt;
1258b55b76b2SDuan Jiong };
1259b55b76b2SDuan Jiong 
1260b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
1261b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
1262b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
1263b55b76b2SDuan Jiong {
1264b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1265b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
1266b55b76b2SDuan Jiong 
1267b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
1268b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
1269b55b76b2SDuan Jiong 
1270b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
1271b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
1272b55b76b2SDuan Jiong }
1273b55b76b2SDuan Jiong 
12743a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
12753a5ad2eeSDavid S. Miller {
12763a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
12773a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12783a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12793a5ad2eeSDavid S. Miller 
12803a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1281e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
12823a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12833a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12843a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12853a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12866502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
12873a5ad2eeSDavid S. Miller 
1288b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
12896700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
12903a5ad2eeSDavid S. Miller 	dst_release(dst);
12913a5ad2eeSDavid S. Miller }
12923a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12933a5ad2eeSDavid S. Miller 
1294c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1295c92a59ecSDuan Jiong 			    u32 mark)
1296c92a59ecSDuan Jiong {
1297c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1298c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1299c92a59ecSDuan Jiong 	struct dst_entry *dst;
1300c92a59ecSDuan Jiong 	struct flowi6 fl6;
1301c92a59ecSDuan Jiong 
1302c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
1303e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
1304c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
1305c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
1306c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
1307c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
1308c92a59ecSDuan Jiong 
1309b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1310c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
1311c92a59ecSDuan Jiong 	dst_release(dst);
1312c92a59ecSDuan Jiong }
1313c92a59ecSDuan Jiong 
13143a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
13153a5ad2eeSDavid S. Miller {
13163a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
13173a5ad2eeSDavid S. Miller }
13183a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
13193a5ad2eeSDavid S. Miller 
13200dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
13211da177e4SLinus Torvalds {
13220dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
13230dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
13240dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
13250dbaee3bSDavid S. Miller 
13261da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
13271da177e4SLinus Torvalds 
13285578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
13295578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	/*
13321da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
13331da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
13341da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
13351da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
13361da177e4SLinus Torvalds 	 */
13371da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
13381da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
13391da177e4SLinus Torvalds 	return mtu;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1343d33e4553SDavid S. Miller {
1344d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1345618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1346618f9bc7SSteffen Klassert 
1347618f9bc7SSteffen Klassert 	if (mtu)
134830f78d8eSEric Dumazet 		goto out;
1349618f9bc7SSteffen Klassert 
1350618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1351d33e4553SDavid S. Miller 
1352d33e4553SDavid S. Miller 	rcu_read_lock();
1353d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1354d33e4553SDavid S. Miller 	if (idev)
1355d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1356d33e4553SDavid S. Miller 	rcu_read_unlock();
1357d33e4553SDavid S. Miller 
135830f78d8eSEric Dumazet out:
135930f78d8eSEric Dumazet 	return min_t(unsigned int, mtu, IP6_MAX_MTU);
1360d33e4553SDavid S. Miller }
1361d33e4553SDavid S. Miller 
13623b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
13633b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
13645d0bbeebSThomas Graf 
13653b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
136687a11578SDavid S. Miller 				  struct flowi6 *fl6)
13671da177e4SLinus Torvalds {
136887a11578SDavid S. Miller 	struct dst_entry *dst;
13691da177e4SLinus Torvalds 	struct rt6_info *rt;
13701da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1371c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
13721da177e4SLinus Torvalds 
137338308473SDavid S. Miller 	if (unlikely(!idev))
1374122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
13751da177e4SLinus Torvalds 
13768b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
137738308473SDavid S. Miller 	if (unlikely(!rt)) {
13781da177e4SLinus Torvalds 		in6_dev_put(idev);
137987a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
13801da177e4SLinus Torvalds 		goto out;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
13838e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
13848e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1385d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1386550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
138787a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13888e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13898e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
139014edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
13911da177e4SLinus Torvalds 
13923b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1393d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1394d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13953b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13961da177e4SLinus Torvalds 
13975578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13981da177e4SLinus Torvalds 
139987a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
140087a11578SDavid S. Miller 
14011da177e4SLinus Torvalds out:
140287a11578SDavid S. Miller 	return dst;
14031da177e4SLinus Torvalds }
14041da177e4SLinus Torvalds 
14053d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
14061da177e4SLinus Torvalds {
1407e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
14083d0f24a7SStephen Hemminger 	int more = 0;
14091da177e4SLinus Torvalds 
14103b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
14113b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
14125d0bbeebSThomas Graf 
14131da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
14141da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
14151da177e4SLinus Torvalds 			*pprev = dst->next;
14161da177e4SLinus Torvalds 			dst_free(dst);
14171da177e4SLinus Torvalds 		} else {
14181da177e4SLinus Torvalds 			pprev = &dst->next;
14193d0f24a7SStephen Hemminger 			++more;
14201da177e4SLinus Torvalds 		}
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14233b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
14245d0bbeebSThomas Graf 
14253d0f24a7SStephen Hemminger 	return more;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
14291e493d19SDavid S. Miller 			    void *arg)
14301e493d19SDavid S. Miller {
14311e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
14321e493d19SDavid S. Miller 
14331e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
14341e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
14351e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
14361e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
14371e493d19SDavid S. Miller 		if (func(rt, arg)) {
14381e493d19SDavid S. Miller 			*pprev = dst->next;
14391e493d19SDavid S. Miller 			dst_free(dst);
14401e493d19SDavid S. Miller 		} else {
14411e493d19SDavid S. Miller 			pprev = &dst->next;
14421e493d19SDavid S. Miller 		}
14431e493d19SDavid S. Miller 	}
14441e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
14451e493d19SDavid S. Miller }
14461e493d19SDavid S. Miller 
1447569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
14481da177e4SLinus Torvalds {
144986393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
14507019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
14517019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
14527019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
14537019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
14547019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1455fc66f95cSEric Dumazet 	int entries;
14561da177e4SLinus Torvalds 
1457fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
145849a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
1459fc66f95cSEric Dumazet 	    entries <= rt_max_size)
14601da177e4SLinus Torvalds 		goto out;
14611da177e4SLinus Torvalds 
14626891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
146314956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
1464fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1465fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
14667019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
14671da177e4SLinus Torvalds out:
14687019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1469fc66f95cSEric Dumazet 	return entries > rt_max_size;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
1472e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc,
1473e715b6d3SFlorian Westphal 			       const struct fib6_config *cfg)
1474e715b6d3SFlorian Westphal {
1475e715b6d3SFlorian Westphal 	struct nlattr *nla;
1476e715b6d3SFlorian Westphal 	int remaining;
1477e715b6d3SFlorian Westphal 	u32 *mp;
1478e715b6d3SFlorian Westphal 
147963159f29SIan Morris 	if (!cfg->fc_mx)
1480e715b6d3SFlorian Westphal 		return 0;
1481e715b6d3SFlorian Westphal 
1482e715b6d3SFlorian Westphal 	mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1483e715b6d3SFlorian Westphal 	if (unlikely(!mp))
1484e715b6d3SFlorian Westphal 		return -ENOMEM;
1485e715b6d3SFlorian Westphal 
1486e715b6d3SFlorian Westphal 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1487e715b6d3SFlorian Westphal 		int type = nla_type(nla);
1488e715b6d3SFlorian Westphal 
1489e715b6d3SFlorian Westphal 		if (type) {
1490ea697639SDaniel Borkmann 			u32 val;
1491ea697639SDaniel Borkmann 
1492e715b6d3SFlorian Westphal 			if (unlikely(type > RTAX_MAX))
1493e715b6d3SFlorian Westphal 				goto err;
1494ea697639SDaniel Borkmann 			if (type == RTAX_CC_ALGO) {
1495ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX];
1496e715b6d3SFlorian Westphal 
1497ea697639SDaniel Borkmann 				nla_strlcpy(tmp, nla, sizeof(tmp));
1498ea697639SDaniel Borkmann 				val = tcp_ca_get_key_by_name(tmp);
1499ea697639SDaniel Borkmann 				if (val == TCP_CA_UNSPEC)
1500ea697639SDaniel Borkmann 					goto err;
1501ea697639SDaniel Borkmann 			} else {
1502ea697639SDaniel Borkmann 				val = nla_get_u32(nla);
1503ea697639SDaniel Borkmann 			}
1504ea697639SDaniel Borkmann 
1505ea697639SDaniel Borkmann 			mp[type - 1] = val;
1506e715b6d3SFlorian Westphal 			__set_bit(type - 1, mxc->mx_valid);
1507e715b6d3SFlorian Westphal 		}
1508e715b6d3SFlorian Westphal 	}
1509e715b6d3SFlorian Westphal 
1510e715b6d3SFlorian Westphal 	mxc->mx = mp;
1511e715b6d3SFlorian Westphal 
1512e715b6d3SFlorian Westphal 	return 0;
1513e715b6d3SFlorian Westphal  err:
1514e715b6d3SFlorian Westphal 	kfree(mp);
1515e715b6d3SFlorian Westphal 	return -EINVAL;
1516e715b6d3SFlorian Westphal }
15171da177e4SLinus Torvalds 
151886872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds 	int err;
15215578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
15221da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
15231da177e4SLinus Torvalds 	struct net_device *dev = NULL;
15241da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1525c71099acSThomas Graf 	struct fib6_table *table;
1526e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
15271da177e4SLinus Torvalds 	int addr_type;
15281da177e4SLinus Torvalds 
152986872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
15301da177e4SLinus Torvalds 		return -EINVAL;
15311da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
153286872cb5SThomas Graf 	if (cfg->fc_src_len)
15331da177e4SLinus Torvalds 		return -EINVAL;
15341da177e4SLinus Torvalds #endif
153586872cb5SThomas Graf 	if (cfg->fc_ifindex) {
15361da177e4SLinus Torvalds 		err = -ENODEV;
15375578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
15381da177e4SLinus Torvalds 		if (!dev)
15391da177e4SLinus Torvalds 			goto out;
15401da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
15411da177e4SLinus Torvalds 		if (!idev)
15421da177e4SLinus Torvalds 			goto out;
15431da177e4SLinus Torvalds 	}
15441da177e4SLinus Torvalds 
154586872cb5SThomas Graf 	if (cfg->fc_metric == 0)
154686872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
15471da177e4SLinus Torvalds 
1548c71099acSThomas Graf 	err = -ENOBUFS;
154938308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1550d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1551d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
155238308473SDavid S. Miller 		if (!table) {
1553f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1554d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1555d71314b4SMatti Vaittinen 		}
1556d71314b4SMatti Vaittinen 	} else {
1557d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1558d71314b4SMatti Vaittinen 	}
155938308473SDavid S. Miller 
156038308473SDavid S. Miller 	if (!table)
1561c71099acSThomas Graf 		goto out;
1562c71099acSThomas Graf 
1563c88507fbSSabrina Dubroca 	rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
15641da177e4SLinus Torvalds 
156538308473SDavid S. Miller 	if (!rt) {
15661da177e4SLinus Torvalds 		err = -ENOMEM;
15671da177e4SLinus Torvalds 		goto out;
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
15701716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
15711716a961SGao feng 		rt6_set_expires(rt, jiffies +
15721716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
15731716a961SGao feng 	else
15741716a961SGao feng 		rt6_clean_expires(rt);
15751da177e4SLinus Torvalds 
157686872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
157786872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
157886872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
157986872cb5SThomas Graf 
158086872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1583d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1584ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1585ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
15861da177e4SLinus Torvalds 	else
1587d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
15881da177e4SLinus Torvalds 
1589d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
15901da177e4SLinus Torvalds 
159186872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
159286872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
1593e5fd387aSMichal Kubeček 	if (rt->rt6i_dst.plen == 128) {
159411d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
1595e5fd387aSMichal Kubeček 		dst_metrics_set_force_overwrite(&rt->dst);
1596e5fd387aSMichal Kubeček 	}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
159986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
160086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
16011da177e4SLinus Torvalds #endif
16021da177e4SLinus Torvalds 
160386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
16061da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
16071da177e4SLinus Torvalds 	 */
160886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
160938308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
161038308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
161138308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
16121da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
16135578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
16141da177e4SLinus Torvalds 			if (dev) {
16151da177e4SLinus Torvalds 				dev_put(dev);
16161da177e4SLinus Torvalds 				in6_dev_put(idev);
16171da177e4SLinus Torvalds 			}
16185578689aSDaniel Lezcano 			dev = net->loopback_dev;
16191da177e4SLinus Torvalds 			dev_hold(dev);
16201da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
16211da177e4SLinus Torvalds 			if (!idev) {
16221da177e4SLinus Torvalds 				err = -ENODEV;
16231da177e4SLinus Torvalds 				goto out;
16241da177e4SLinus Torvalds 			}
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1627ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1628ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1629ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1630aad88724SEric Dumazet 			rt->dst.output = dst_discard_sk;
16317150aedeSKamala R 			rt->dst.input = dst_discard;
1632ef2c7d7bSNicolas Dichtel 			break;
1633ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1634ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
16357150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
16367150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
1637ef2c7d7bSNicolas Dichtel 			break;
1638b4949ab2SNicolas Dichtel 		case RTN_THROW:
1639ef2c7d7bSNicolas Dichtel 		default:
16407150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
16417150aedeSKamala R 					: -ENETUNREACH;
16427150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
16437150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
1644ef2c7d7bSNicolas Dichtel 			break;
1645ef2c7d7bSNicolas Dichtel 		}
16461da177e4SLinus Torvalds 		goto install_route;
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
164986872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1650b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
16511da177e4SLinus Torvalds 		int gwa_type;
16521da177e4SLinus Torvalds 
165386872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
16544e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
16551da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
16581da177e4SLinus Torvalds 			struct rt6_info *grt;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
16611da177e4SLinus Torvalds 			   addresses as nexthop address.
16621da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
16631da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
16641da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
16651da177e4SLinus Torvalds 			   some exceptions. --ANK
16661da177e4SLinus Torvalds 			 */
16671da177e4SLinus Torvalds 			err = -EINVAL;
16681da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
16691da177e4SLinus Torvalds 				goto out;
16701da177e4SLinus Torvalds 
16715578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
167438308473SDavid S. Miller 			if (!grt)
16751da177e4SLinus Torvalds 				goto out;
16761da177e4SLinus Torvalds 			if (dev) {
1677d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
167894e187c0SAmerigo Wang 					ip6_rt_put(grt);
16791da177e4SLinus Torvalds 					goto out;
16801da177e4SLinus Torvalds 				}
16811da177e4SLinus Torvalds 			} else {
1682d1918542SDavid S. Miller 				dev = grt->dst.dev;
16831da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
16841da177e4SLinus Torvalds 				dev_hold(dev);
16851da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
16861da177e4SLinus Torvalds 			}
16871da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
16881da177e4SLinus Torvalds 				err = 0;
168994e187c0SAmerigo Wang 			ip6_rt_put(grt);
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 			if (err)
16921da177e4SLinus Torvalds 				goto out;
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 		err = -EINVAL;
169538308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
16961da177e4SLinus Torvalds 			goto out;
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	err = -ENODEV;
170038308473SDavid S. Miller 	if (!dev)
17011da177e4SLinus Torvalds 		goto out;
17021da177e4SLinus Torvalds 
1703c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1704c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1705c3968a85SDaniel Walter 			err = -EINVAL;
1706c3968a85SDaniel Walter 			goto out;
1707c3968a85SDaniel Walter 		}
17084e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1709c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1710c3968a85SDaniel Walter 	} else
1711c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1712c3968a85SDaniel Walter 
171386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds install_route:
1716d8d1f30bSChangli Gao 	rt->dst.dev = dev;
17171da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1718c71099acSThomas Graf 	rt->rt6i_table = table;
171963152fc0SDaniel Lezcano 
1720c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
172163152fc0SDaniel Lezcano 
1722e715b6d3SFlorian Westphal 	err = ip6_convert_metrics(&mxc, cfg);
1723e715b6d3SFlorian Westphal 	if (err)
1724e715b6d3SFlorian Westphal 		goto out;
17251da177e4SLinus Torvalds 
1726e715b6d3SFlorian Westphal 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1727e715b6d3SFlorian Westphal 
1728e715b6d3SFlorian Westphal 	kfree(mxc.mx);
1729e715b6d3SFlorian Westphal 	return err;
17301da177e4SLinus Torvalds out:
17311da177e4SLinus Torvalds 	if (dev)
17321da177e4SLinus Torvalds 		dev_put(dev);
17331da177e4SLinus Torvalds 	if (idev)
17341da177e4SLinus Torvalds 		in6_dev_put(idev);
17351da177e4SLinus Torvalds 	if (rt)
1736d8d1f30bSChangli Gao 		dst_free(&rt->dst);
17371da177e4SLinus Torvalds 	return err;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
174086872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	int err;
1743c71099acSThomas Graf 	struct fib6_table *table;
1744d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
17451da177e4SLinus Torvalds 
17466825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
17476825a26cSGao feng 		err = -ENOENT;
17486825a26cSGao feng 		goto out;
17496825a26cSGao feng 	}
17506c813a72SPatrick McHardy 
1751c71099acSThomas Graf 	table = rt->rt6i_table;
1752c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
175386872cb5SThomas Graf 	err = fib6_del(rt, info);
1754c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
17551da177e4SLinus Torvalds 
17566825a26cSGao feng out:
175794e187c0SAmerigo Wang 	ip6_rt_put(rt);
17581da177e4SLinus Torvalds 	return err;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
1761e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1762e0a1ad73SThomas Graf {
17634d1169c1SDenis V. Lunev 	struct nl_info info = {
1764d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
17654d1169c1SDenis V. Lunev 	};
1766528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1767e0a1ad73SThomas Graf }
1768e0a1ad73SThomas Graf 
176986872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
17701da177e4SLinus Torvalds {
1771c71099acSThomas Graf 	struct fib6_table *table;
17721da177e4SLinus Torvalds 	struct fib6_node *fn;
17731da177e4SLinus Torvalds 	struct rt6_info *rt;
17741da177e4SLinus Torvalds 	int err = -ESRCH;
17751da177e4SLinus Torvalds 
17765578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
177738308473SDavid S. Miller 	if (!table)
1778c71099acSThomas Graf 		return err;
17791da177e4SLinus Torvalds 
1780c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1781c71099acSThomas Graf 
1782c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
178386872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
178486872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	if (fn) {
1787d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1788*1f56a01fSMartin KaFai Lau 			if ((rt->rt6i_flags & RTF_CACHE) &&
1789*1f56a01fSMartin KaFai Lau 			    !(cfg->fc_flags & RTF_CACHE))
1790*1f56a01fSMartin KaFai Lau 				continue;
179186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1792d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1793d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17941da177e4SLinus Torvalds 				continue;
179586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
179686872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17971da177e4SLinus Torvalds 				continue;
179886872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17991da177e4SLinus Torvalds 				continue;
1800d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1801c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
18021da177e4SLinus Torvalds 
180386872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
18041da177e4SLinus Torvalds 		}
18051da177e4SLinus Torvalds 	}
1806c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	return err;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
18116700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1812a6279458SYOSHIFUJI Hideaki {
1813e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1814a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1815e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1816e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1817e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1818e8599ff4SDavid S. Miller 	struct neighbour *neigh;
181971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
18206e157b6aSDavid S. Miller 	int optlen, on_link;
18216e157b6aSDavid S. Miller 	u8 *lladdr;
1822e8599ff4SDavid S. Miller 
182329a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
182471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1825e8599ff4SDavid S. Miller 
1826e8599ff4SDavid S. Miller 	if (optlen < 0) {
18276e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1828e8599ff4SDavid S. Miller 		return;
1829e8599ff4SDavid S. Miller 	}
1830e8599ff4SDavid S. Miller 
183171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1832e8599ff4SDavid S. Miller 
183371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
18346e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1835e8599ff4SDavid S. Miller 		return;
1836e8599ff4SDavid S. Miller 	}
1837e8599ff4SDavid S. Miller 
18386e157b6aSDavid S. Miller 	on_link = 0;
183971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1840e8599ff4SDavid S. Miller 		on_link = 1;
184171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1842e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
18436e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1844e8599ff4SDavid S. Miller 		return;
1845e8599ff4SDavid S. Miller 	}
1846e8599ff4SDavid S. Miller 
1847e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1848e8599ff4SDavid S. Miller 	if (!in6_dev)
1849e8599ff4SDavid S. Miller 		return;
1850e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1851e8599ff4SDavid S. Miller 		return;
1852e8599ff4SDavid S. Miller 
1853e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1854e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1855e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1856e8599ff4SDavid S. Miller 	 */
1857e8599ff4SDavid S. Miller 
185871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1859e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1860e8599ff4SDavid S. Miller 		return;
1861e8599ff4SDavid S. Miller 	}
18626e157b6aSDavid S. Miller 
18636e157b6aSDavid S. Miller 	lladdr = NULL;
1864e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1865e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1866e8599ff4SDavid S. Miller 					     skb->dev);
1867e8599ff4SDavid S. Miller 		if (!lladdr) {
1868e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1869e8599ff4SDavid S. Miller 			return;
1870e8599ff4SDavid S. Miller 		}
1871e8599ff4SDavid S. Miller 	}
1872e8599ff4SDavid S. Miller 
18736e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
18746e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
18756e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
18766e157b6aSDavid S. Miller 		return;
18776e157b6aSDavid S. Miller 	}
18786e157b6aSDavid S. Miller 
18796e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18806e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18816e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18826e157b6aSDavid S. Miller 	 */
18836e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18846e157b6aSDavid S. Miller 
188571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1886e8599ff4SDavid S. Miller 	if (!neigh)
1887e8599ff4SDavid S. Miller 		return;
1888e8599ff4SDavid S. Miller 
18891da177e4SLinus Torvalds 	/*
18901da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18911da177e4SLinus Torvalds 	 */
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18941da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18951da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18961da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18971da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18981da177e4SLinus Torvalds 		     );
18991da177e4SLinus Torvalds 
190071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
190138308473SDavid S. Miller 	if (!nrt)
19021da177e4SLinus Torvalds 		goto out;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
19051da177e4SLinus Torvalds 	if (on_link)
19061da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
19071da177e4SLinus Torvalds 
19084e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
19091da177e4SLinus Torvalds 
191040e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
19111da177e4SLinus Torvalds 		goto out;
19121da177e4SLinus Torvalds 
1913d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1914d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
191571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
191660592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
19178d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
19188d71740cSTom Tucker 
19191da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
19206e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1921e0a1ad73SThomas Graf 		ip6_del_rt(rt);
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds out:
1925e8599ff4SDavid S. Miller 	neigh_release(neigh);
19266e157b6aSDavid S. Miller }
19276e157b6aSDavid S. Miller 
19281da177e4SLinus Torvalds /*
19291da177e4SLinus Torvalds  *	Misc support functions
19301da177e4SLinus Torvalds  */
19311da177e4SLinus Torvalds 
19321716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
193321efcfa0SEric Dumazet 				    const struct in6_addr *dest)
19341da177e4SLinus Torvalds {
1935d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
19368b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
19378b96d22dSDavid S. Miller 					    ort->rt6i_table);
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	if (rt) {
1940d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1941d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
19428e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
19431da177e4SLinus Torvalds 
19444e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
19458e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1946defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1947d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
19481da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
19491da177e4SLinus Torvalds 		if (rt->rt6i_idev)
19501da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1951d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
19521da177e4SLinus Torvalds 
1953550bab42SJulian Anastasov 		if (ort->rt6i_flags & RTF_GATEWAY)
19544e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = ort->rt6i_gateway;
1955550bab42SJulian Anastasov 		else
1956550bab42SJulian Anastasov 			rt->rt6i_gateway = *dest;
19571716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
19581716a961SGao feng 		rt6_set_from(rt, ort);
19591da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
19621da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
19631da177e4SLinus Torvalds #endif
19640f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1965c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 	return rt;
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
197070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1971efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1972b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1973b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
197470ceb4f5SYOSHIFUJI Hideaki {
197570ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
197670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1977c71099acSThomas Graf 	struct fib6_table *table;
197870ceb4f5SYOSHIFUJI Hideaki 
1979efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
198038308473SDavid S. Miller 	if (!table)
1981c71099acSThomas Graf 		return NULL;
1982c71099acSThomas Graf 
19835744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1984c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
198570ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
198670ceb4f5SYOSHIFUJI Hideaki 		goto out;
198770ceb4f5SYOSHIFUJI Hideaki 
1988d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1989d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
199070ceb4f5SYOSHIFUJI Hideaki 			continue;
199170ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
199270ceb4f5SYOSHIFUJI Hideaki 			continue;
199370ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
199470ceb4f5SYOSHIFUJI Hideaki 			continue;
1995d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
199670ceb4f5SYOSHIFUJI Hideaki 		break;
199770ceb4f5SYOSHIFUJI Hideaki 	}
199870ceb4f5SYOSHIFUJI Hideaki out:
19995744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
200070ceb4f5SYOSHIFUJI Hideaki 	return rt;
200170ceb4f5SYOSHIFUJI Hideaki }
200270ceb4f5SYOSHIFUJI Hideaki 
2003efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
2004b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
2005b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
200695c96174SEric Dumazet 					   unsigned int pref)
200770ceb4f5SYOSHIFUJI Hideaki {
200886872cb5SThomas Graf 	struct fib6_config cfg = {
200986872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
2010238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
201186872cb5SThomas Graf 		.fc_ifindex	= ifindex,
201286872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
201386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
201486872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
201515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
2016efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2017efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
201886872cb5SThomas Graf 	};
201970ceb4f5SYOSHIFUJI Hideaki 
20204e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
20214e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
202286872cb5SThomas Graf 
2023e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
2024e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
202586872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
202670ceb4f5SYOSHIFUJI Hideaki 
202786872cb5SThomas Graf 	ip6_route_add(&cfg);
202870ceb4f5SYOSHIFUJI Hideaki 
2029efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
203070ceb4f5SYOSHIFUJI Hideaki }
203170ceb4f5SYOSHIFUJI Hideaki #endif
203270ceb4f5SYOSHIFUJI Hideaki 
2033b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	struct rt6_info *rt;
2036c71099acSThomas Graf 	struct fib6_table *table;
20371da177e4SLinus Torvalds 
2038c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
203938308473SDavid S. Miller 	if (!table)
2040c71099acSThomas Graf 		return NULL;
20411da177e4SLinus Torvalds 
20425744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
2043d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
2044d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
2045045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
20461da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
20471da177e4SLinus Torvalds 			break;
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 	if (rt)
2050d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
20515744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
20521da177e4SLinus Torvalds 	return rt;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
2055b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
2056ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
2057ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
20581da177e4SLinus Torvalds {
205986872cb5SThomas Graf 	struct fib6_config cfg = {
206086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
2061238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
206286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
206386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
206486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
206515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
20665578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2067c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
206886872cb5SThomas Graf 	};
20691da177e4SLinus Torvalds 
20704e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
20711da177e4SLinus Torvalds 
207286872cb5SThomas Graf 	ip6_route_add(&cfg);
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
20777b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	struct rt6_info *rt;
2080c71099acSThomas Graf 	struct fib6_table *table;
2081c71099acSThomas Graf 
2082c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20837b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
208438308473SDavid S. Miller 	if (!table)
2085c71099acSThomas Graf 		return;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds restart:
2088c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2089d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20903e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
20913e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
2092d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2093c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2094e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20951da177e4SLinus Torvalds 			goto restart;
20961da177e4SLinus Torvalds 		}
20971da177e4SLinus Torvalds 	}
2098c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
21015578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
21025578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
210386872cb5SThomas Graf 				 struct fib6_config *cfg)
210486872cb5SThomas Graf {
210586872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
210686872cb5SThomas Graf 
210786872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
210886872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
210986872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
211086872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
211186872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
211286872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
211386872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
211486872cb5SThomas Graf 
21155578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2116f1243c2dSBenjamin Thery 
21174e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
21184e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
21194e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
212086872cb5SThomas Graf }
212186872cb5SThomas Graf 
21225578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
21231da177e4SLinus Torvalds {
212486872cb5SThomas Graf 	struct fib6_config cfg;
21251da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
21261da177e4SLinus Torvalds 	int err;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	switch (cmd) {
21291da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
21301da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2131af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
21321da177e4SLinus Torvalds 			return -EPERM;
21331da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
21341da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
21351da177e4SLinus Torvalds 		if (err)
21361da177e4SLinus Torvalds 			return -EFAULT;
21371da177e4SLinus Torvalds 
21385578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
213986872cb5SThomas Graf 
21401da177e4SLinus Torvalds 		rtnl_lock();
21411da177e4SLinus Torvalds 		switch (cmd) {
21421da177e4SLinus Torvalds 		case SIOCADDRT:
214386872cb5SThomas Graf 			err = ip6_route_add(&cfg);
21441da177e4SLinus Torvalds 			break;
21451da177e4SLinus Torvalds 		case SIOCDELRT:
214686872cb5SThomas Graf 			err = ip6_route_del(&cfg);
21471da177e4SLinus Torvalds 			break;
21481da177e4SLinus Torvalds 		default:
21491da177e4SLinus Torvalds 			err = -EINVAL;
21501da177e4SLinus Torvalds 		}
21511da177e4SLinus Torvalds 		rtnl_unlock();
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 		return err;
21543ff50b79SStephen Hemminger 	}
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	return -EINVAL;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds /*
21601da177e4SLinus Torvalds  *	Drop the packet on the floor
21611da177e4SLinus Torvalds  */
21621da177e4SLinus Torvalds 
2163d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
21641da177e4SLinus Torvalds {
2165612f09e8SYOSHIFUJI Hideaki 	int type;
2166adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2167612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2168612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
21690660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
217045bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
21713bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21723bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2173612f09e8SYOSHIFUJI Hideaki 			break;
2174612f09e8SYOSHIFUJI Hideaki 		}
2175612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2176612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
21773bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21783bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2179612f09e8SYOSHIFUJI Hideaki 		break;
2180612f09e8SYOSHIFUJI Hideaki 	}
21813ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21821da177e4SLinus Torvalds 	kfree_skb(skb);
21831da177e4SLinus Torvalds 	return 0;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21869ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21879ce8ade0SThomas Graf {
2188612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21899ce8ade0SThomas Graf }
21909ce8ade0SThomas Graf 
2191aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
21921da177e4SLinus Torvalds {
2193adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2194612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
21979ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21989ce8ade0SThomas Graf {
2199612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
22009ce8ade0SThomas Graf }
22019ce8ade0SThomas Graf 
2202aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
22039ce8ade0SThomas Graf {
2204adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2205612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
22069ce8ade0SThomas Graf }
22079ce8ade0SThomas Graf 
22081da177e4SLinus Torvalds /*
22091da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
22101da177e4SLinus Torvalds  */
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
22131da177e4SLinus Torvalds 				    const struct in6_addr *addr,
22148f031519SDavid S. Miller 				    bool anycast)
22151da177e4SLinus Torvalds {
2216c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2217a3300ef4SHannes Frederic Sowa 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2218a3300ef4SHannes Frederic Sowa 					    DST_NOCOUNT, NULL);
2219a3300ef4SHannes Frederic Sowa 	if (!rt)
22201da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	in6_dev_hold(idev);
22231da177e4SLinus Torvalds 
222411d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2225d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2226d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
22271da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
223058c4fb86SYOSHIFUJI Hideaki 	if (anycast)
223158c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
223258c4fb86SYOSHIFUJI Hideaki 	else
22331da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
22341da177e4SLinus Torvalds 
2235550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
22364e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
22371da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
22385578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
22391da177e4SLinus Torvalds 
2240d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	return rt;
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
2245c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2246c3968a85SDaniel Walter 			struct rt6_info *rt,
2247b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2248c3968a85SDaniel Walter 			unsigned int prefs,
2249c3968a85SDaniel Walter 			struct in6_addr *saddr)
2250c3968a85SDaniel Walter {
2251c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
2252c3968a85SDaniel Walter 	int err = 0;
2253c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
22544e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2255c3968a85SDaniel Walter 	else
2256c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2257c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2258c3968a85SDaniel Walter 	return err;
2259c3968a85SDaniel Walter }
2260c3968a85SDaniel Walter 
2261c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2262c3968a85SDaniel Walter struct arg_dev_net_ip {
2263c3968a85SDaniel Walter 	struct net_device *dev;
2264c3968a85SDaniel Walter 	struct net *net;
2265c3968a85SDaniel Walter 	struct in6_addr *addr;
2266c3968a85SDaniel Walter };
2267c3968a85SDaniel Walter 
2268c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2269c3968a85SDaniel Walter {
2270c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2271c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2272c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2273c3968a85SDaniel Walter 
2274d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2275c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2276c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2277c3968a85SDaniel Walter 		/* remove prefsrc entry */
2278c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2279c3968a85SDaniel Walter 	}
2280c3968a85SDaniel Walter 	return 0;
2281c3968a85SDaniel Walter }
2282c3968a85SDaniel Walter 
2283c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2284c3968a85SDaniel Walter {
2285c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2286c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2287c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2288c3968a85SDaniel Walter 		.net = net,
2289c3968a85SDaniel Walter 		.addr = &ifp->addr,
2290c3968a85SDaniel Walter 	};
22910c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
2292c3968a85SDaniel Walter }
2293c3968a85SDaniel Walter 
2294be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2295be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2296be7a010dSDuan Jiong 
2297be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
2298be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2299be7a010dSDuan Jiong {
2300be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
2301be7a010dSDuan Jiong 
2302be7a010dSDuan Jiong 	if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2303be7a010dSDuan Jiong 	     ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2304be7a010dSDuan Jiong 	     ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2305be7a010dSDuan Jiong 		return -1;
2306be7a010dSDuan Jiong 	}
2307be7a010dSDuan Jiong 	return 0;
2308be7a010dSDuan Jiong }
2309be7a010dSDuan Jiong 
2310be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2311be7a010dSDuan Jiong {
2312be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
2313be7a010dSDuan Jiong }
2314be7a010dSDuan Jiong 
23158ed67789SDaniel Lezcano struct arg_dev_net {
23168ed67789SDaniel Lezcano 	struct net_device *dev;
23178ed67789SDaniel Lezcano 	struct net *net;
23188ed67789SDaniel Lezcano };
23198ed67789SDaniel Lezcano 
23201da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
23211da177e4SLinus Torvalds {
2322bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2323bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
23248ed67789SDaniel Lezcano 
2325d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2326c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
23271da177e4SLinus Torvalds 		return -1;
2328c159d30cSDavid S. Miller 
23291da177e4SLinus Torvalds 	return 0;
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
2332f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
23331da177e4SLinus Torvalds {
23348ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
23358ed67789SDaniel Lezcano 		.dev = dev,
23368ed67789SDaniel Lezcano 		.net = net,
23378ed67789SDaniel Lezcano 	};
23388ed67789SDaniel Lezcano 
23390c3584d5SLi RongQing 	fib6_clean_all(net, fib6_ifdown, &adn);
23401e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
234395c96174SEric Dumazet struct rt6_mtu_change_arg {
23441da177e4SLinus Torvalds 	struct net_device *dev;
234595c96174SEric Dumazet 	unsigned int mtu;
23461da177e4SLinus Torvalds };
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
23491da177e4SLinus Torvalds {
23501da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
23511da177e4SLinus Torvalds 	struct inet6_dev *idev;
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
23541da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
23551da177e4SLinus Torvalds 	   We still use this lock to block changes
23561da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
23571da177e4SLinus Torvalds 	*/
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
236038308473SDavid S. Miller 	if (!idev)
23611da177e4SLinus Torvalds 		return 0;
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
23641da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
23651da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
23661da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
23671da177e4SLinus Torvalds 	 */
23681da177e4SLinus Torvalds 	/*
23691da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
23701da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
23711da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
23721da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
23731da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
23741da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
23751da177e4SLinus Torvalds 	   PMTU discouvery.
23761da177e4SLinus Torvalds 	 */
2377d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2378d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2379d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2380d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2381d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2382defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2383566cfd8fSSimon Arlott 	}
23841da177e4SLinus Torvalds 	return 0;
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
238795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
23881da177e4SLinus Torvalds {
2389c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2390c71099acSThomas Graf 		.dev = dev,
2391c71099acSThomas Graf 		.mtu = mtu,
2392c71099acSThomas Graf 	};
23931da177e4SLinus Torvalds 
23940c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
2397ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23985176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
239986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2400ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
240186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
240286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
240351ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
2404c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
240586872cb5SThomas Graf };
240686872cb5SThomas Graf 
240786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
240886872cb5SThomas Graf 			      struct fib6_config *cfg)
24091da177e4SLinus Torvalds {
241086872cb5SThomas Graf 	struct rtmsg *rtm;
241186872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
2412c78ba6d6SLubomir Rintel 	unsigned int pref;
241386872cb5SThomas Graf 	int err;
24141da177e4SLinus Torvalds 
241586872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
241686872cb5SThomas Graf 	if (err < 0)
241786872cb5SThomas Graf 		goto errout;
24181da177e4SLinus Torvalds 
241986872cb5SThomas Graf 	err = -EINVAL;
242086872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
242186872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
242286872cb5SThomas Graf 
242386872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
242486872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
242586872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
242686872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
242786872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2428ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
242986872cb5SThomas Graf 
2430ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2431ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2432b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2433b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
243486872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
243586872cb5SThomas Graf 
2436ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2437ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2438ab79ad14SMaciej Żenczykowski 
2439*1f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
2440*1f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
2441*1f56a01fSMartin KaFai Lau 
244215e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
244386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
24443b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
244586872cb5SThomas Graf 
244686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
244767b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
244886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
24491da177e4SLinus Torvalds 	}
245086872cb5SThomas Graf 
245186872cb5SThomas Graf 	if (tb[RTA_DST]) {
245286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
245386872cb5SThomas Graf 
245486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
245586872cb5SThomas Graf 			goto errout;
245686872cb5SThomas Graf 
245786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
24581da177e4SLinus Torvalds 	}
245986872cb5SThomas Graf 
246086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
246186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
246286872cb5SThomas Graf 
246386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
246486872cb5SThomas Graf 			goto errout;
246586872cb5SThomas Graf 
246686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
24671da177e4SLinus Torvalds 	}
246886872cb5SThomas Graf 
2469c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
247067b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
2471c3968a85SDaniel Walter 
247286872cb5SThomas Graf 	if (tb[RTA_OIF])
247386872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
247486872cb5SThomas Graf 
247586872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
247686872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
247786872cb5SThomas Graf 
247886872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
247986872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
248086872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
24811da177e4SLinus Torvalds 	}
248286872cb5SThomas Graf 
248386872cb5SThomas Graf 	if (tb[RTA_TABLE])
248486872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
248586872cb5SThomas Graf 
248651ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
248751ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
248851ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
248951ebd318SNicolas Dichtel 	}
249051ebd318SNicolas Dichtel 
2491c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
2492c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
2493c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
2494c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
2495c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
2496c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
2497c78ba6d6SLubomir Rintel 	}
2498c78ba6d6SLubomir Rintel 
249986872cb5SThomas Graf 	err = 0;
250086872cb5SThomas Graf errout:
250186872cb5SThomas Graf 	return err;
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
250451ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
250551ebd318SNicolas Dichtel {
250651ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
250751ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
250851ebd318SNicolas Dichtel 	int remaining;
250951ebd318SNicolas Dichtel 	int attrlen;
251051ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
251151ebd318SNicolas Dichtel 
251251ebd318SNicolas Dichtel beginning:
251351ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
251451ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
251551ebd318SNicolas Dichtel 
251651ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
251751ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
251851ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
251951ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
252051ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
252151ebd318SNicolas Dichtel 
252251ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
252351ebd318SNicolas Dichtel 		if (attrlen > 0) {
252451ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
252551ebd318SNicolas Dichtel 
252651ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
252751ebd318SNicolas Dichtel 			if (nla) {
252867b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
252951ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
253051ebd318SNicolas Dichtel 			}
253151ebd318SNicolas Dichtel 		}
253251ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
253351ebd318SNicolas Dichtel 		if (err) {
253451ebd318SNicolas Dichtel 			last_err = err;
253551ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
253651ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
253751ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
253851ebd318SNicolas Dichtel 			 */
253951ebd318SNicolas Dichtel 			if (add) {
254051ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
254151ebd318SNicolas Dichtel 				 * next hops that have been already added.
254251ebd318SNicolas Dichtel 				 */
254351ebd318SNicolas Dichtel 				add = 0;
254451ebd318SNicolas Dichtel 				goto beginning;
254551ebd318SNicolas Dichtel 			}
254651ebd318SNicolas Dichtel 		}
25471a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
25481a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
25491a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
25501a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
25511a72418bSNicolas Dichtel 		 */
25521a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
255351ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
255451ebd318SNicolas Dichtel 	}
255551ebd318SNicolas Dichtel 
255651ebd318SNicolas Dichtel 	return last_err;
255751ebd318SNicolas Dichtel }
255851ebd318SNicolas Dichtel 
2559661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
25601da177e4SLinus Torvalds {
256186872cb5SThomas Graf 	struct fib6_config cfg;
256286872cb5SThomas Graf 	int err;
25631da177e4SLinus Torvalds 
256486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
256586872cb5SThomas Graf 	if (err < 0)
256686872cb5SThomas Graf 		return err;
256786872cb5SThomas Graf 
256851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
256951ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
257051ebd318SNicolas Dichtel 	else
257186872cb5SThomas Graf 		return ip6_route_del(&cfg);
25721da177e4SLinus Torvalds }
25731da177e4SLinus Torvalds 
2574661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
25751da177e4SLinus Torvalds {
257686872cb5SThomas Graf 	struct fib6_config cfg;
257786872cb5SThomas Graf 	int err;
25781da177e4SLinus Torvalds 
257986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
258086872cb5SThomas Graf 	if (err < 0)
258186872cb5SThomas Graf 		return err;
258286872cb5SThomas Graf 
258351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
258451ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
258551ebd318SNicolas Dichtel 	else
258686872cb5SThomas Graf 		return ip6_route_add(&cfg);
25871da177e4SLinus Torvalds }
25881da177e4SLinus Torvalds 
2589339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2590339bf98fSThomas Graf {
2591339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2592339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2593339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2594339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2595339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2596339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2597339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2598339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2599339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
26006a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2601ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
2602c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2603c78ba6d6SLubomir Rintel 	       + nla_total_size(1); /* RTA_PREF */
2604339bf98fSThomas Graf }
2605339bf98fSThomas Graf 
2606191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2607191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
26080d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
260915e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
26107bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
26111da177e4SLinus Torvalds {
26121da177e4SLinus Torvalds 	struct rtmsg *rtm;
26131da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2614e3703b3dSThomas Graf 	long expires;
26159e762a4aSPatrick McHardy 	u32 table;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
26181da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
26191da177e4SLinus Torvalds 			/* success since this is not a prefix route */
26201da177e4SLinus Torvalds 			return 1;
26211da177e4SLinus Torvalds 		}
26221da177e4SLinus Torvalds 	}
26231da177e4SLinus Torvalds 
262415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
262538308473SDavid S. Miller 	if (!nlh)
262626932566SPatrick McHardy 		return -EMSGSIZE;
26272d7202bfSThomas Graf 
26282d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
26291da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
26301da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
26311da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
26321da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2633c71099acSThomas Graf 	if (rt->rt6i_table)
26349e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2635c71099acSThomas Graf 	else
26369e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
26379e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2638c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2639c78679e8SDavid S. Miller 		goto nla_put_failure;
2640ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2641ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2642ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2643ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2644ef2c7d7bSNicolas Dichtel 			break;
2645ef2c7d7bSNicolas Dichtel 		case -EACCES:
2646ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2647ef2c7d7bSNicolas Dichtel 			break;
2648b4949ab2SNicolas Dichtel 		case -EAGAIN:
2649b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2650b4949ab2SNicolas Dichtel 			break;
2651ef2c7d7bSNicolas Dichtel 		default:
26521da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2653ef2c7d7bSNicolas Dichtel 			break;
2654ef2c7d7bSNicolas Dichtel 		}
2655ef2c7d7bSNicolas Dichtel 	}
2656ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2657ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2658d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
26591da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
26601da177e4SLinus Torvalds 	else
26611da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
26621da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
26631da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
26641da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
26651da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
26661da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2667f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2668f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
26691da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2670f0396f60SDenis Ovsienko 		else
2671f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2672f0396f60SDenis Ovsienko 	}
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
26751da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds 	if (dst) {
2678930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, dst))
2679c78679e8SDavid S. Miller 			goto nla_put_failure;
26801da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
26811da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2682930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
2683c78679e8SDavid S. Miller 			goto nla_put_failure;
26841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26851da177e4SLinus Torvalds 	if (src) {
2686930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
2687c78679e8SDavid S. Miller 			goto nla_put_failure;
26881da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2689c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2690930345eaSJiri Benc 		   nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
2691c78679e8SDavid S. Miller 		goto nla_put_failure;
26921da177e4SLinus Torvalds #endif
26937bc570c8SYOSHIFUJI Hideaki 	if (iif) {
26947bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
26957bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
26968229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
26977bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
26987bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
26997bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
27007bc570c8SYOSHIFUJI Hideaki 						return 0;
27017bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
27027bc570c8SYOSHIFUJI Hideaki 				} else {
27037bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
27047bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
27057bc570c8SYOSHIFUJI Hideaki 				}
27067bc570c8SYOSHIFUJI Hideaki 			}
27077bc570c8SYOSHIFUJI Hideaki 		} else
27087bc570c8SYOSHIFUJI Hideaki #endif
2709c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2710c78679e8SDavid S. Miller 				goto nla_put_failure;
27117bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
27121da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2713c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2714930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2715c78679e8SDavid S. Miller 			goto nla_put_failure;
2716c3968a85SDaniel Walter 	}
2717c3968a85SDaniel Walter 
2718c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2719c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
27204e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2721930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2722c78679e8SDavid S. Miller 			goto nla_put_failure;
27231da177e4SLinus Torvalds 	}
27242d7202bfSThomas Graf 
2725defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
27262d7202bfSThomas Graf 		goto nla_put_failure;
27272d7202bfSThomas Graf 
2728dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2729930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
273094f826b8SEric Dumazet 			goto nla_put_failure;
273194f826b8SEric Dumazet 	}
27322d7202bfSThomas Graf 
2733c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2734c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2735c78679e8SDavid S. Miller 		goto nla_put_failure;
2736c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2737c78679e8SDavid S. Miller 		goto nla_put_failure;
27388253947eSLi Wei 
27398253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
274069cdf8f9SYOSHIFUJI Hideaki 
274187a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2742e3703b3dSThomas Graf 		goto nla_put_failure;
27431da177e4SLinus Torvalds 
2744c78ba6d6SLubomir Rintel 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2745c78ba6d6SLubomir Rintel 		goto nla_put_failure;
2746c78ba6d6SLubomir Rintel 
2747053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2748053c095aSJohannes Berg 	return 0;
27492d7202bfSThomas Graf 
27502d7202bfSThomas Graf nla_put_failure:
275126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
275226932566SPatrick McHardy 	return -EMSGSIZE;
27531da177e4SLinus Torvalds }
27541da177e4SLinus Torvalds 
27551b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
27561da177e4SLinus Torvalds {
27571da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
27581da177e4SLinus Torvalds 	int prefix;
27591da177e4SLinus Torvalds 
27602d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
27612d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
27621da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
27631da177e4SLinus Torvalds 	} else
27641da177e4SLinus Torvalds 		prefix = 0;
27651da177e4SLinus Torvalds 
2766191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2767191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
276815e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
27697bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
2772661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
27731da177e4SLinus Torvalds {
27743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2775ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
27761da177e4SLinus Torvalds 	struct rt6_info *rt;
2777ab364a6fSThomas Graf 	struct sk_buff *skb;
2778ab364a6fSThomas Graf 	struct rtmsg *rtm;
27794c9483b2SDavid S. Miller 	struct flowi6 fl6;
278072331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2781ab364a6fSThomas Graf 
2782ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2783ab364a6fSThomas Graf 	if (err < 0)
2784ab364a6fSThomas Graf 		goto errout;
2785ab364a6fSThomas Graf 
2786ab364a6fSThomas Graf 	err = -EINVAL;
27874c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2788ab364a6fSThomas Graf 
2789ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2790ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2791ab364a6fSThomas Graf 			goto errout;
2792ab364a6fSThomas Graf 
27934e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2794ab364a6fSThomas Graf 	}
2795ab364a6fSThomas Graf 
2796ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2797ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2798ab364a6fSThomas Graf 			goto errout;
2799ab364a6fSThomas Graf 
28004e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2801ab364a6fSThomas Graf 	}
2802ab364a6fSThomas Graf 
2803ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2804ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2805ab364a6fSThomas Graf 
2806ab364a6fSThomas Graf 	if (tb[RTA_OIF])
280772331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2808ab364a6fSThomas Graf 
28092e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
28102e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
28112e47b291SLorenzo Colitti 
2812ab364a6fSThomas Graf 	if (iif) {
2813ab364a6fSThomas Graf 		struct net_device *dev;
281472331bc0SShmulik Ladkani 		int flags = 0;
281572331bc0SShmulik Ladkani 
28165578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2817ab364a6fSThomas Graf 		if (!dev) {
2818ab364a6fSThomas Graf 			err = -ENODEV;
2819ab364a6fSThomas Graf 			goto errout;
2820ab364a6fSThomas Graf 		}
282172331bc0SShmulik Ladkani 
282272331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
282372331bc0SShmulik Ladkani 
282472331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
282572331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
282672331bc0SShmulik Ladkani 
282772331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
282872331bc0SShmulik Ladkani 							       flags);
282972331bc0SShmulik Ladkani 	} else {
283072331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
283172331bc0SShmulik Ladkani 
283272331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2833ab364a6fSThomas Graf 	}
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
283638308473SDavid S. Miller 	if (!skb) {
283794e187c0SAmerigo Wang 		ip6_rt_put(rt);
2838ab364a6fSThomas Graf 		err = -ENOBUFS;
2839ab364a6fSThomas Graf 		goto errout;
2840ab364a6fSThomas Graf 	}
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
28431da177e4SLinus Torvalds 	   through good chunk of routing engine.
28441da177e4SLinus Torvalds 	 */
2845459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
28461da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
28471da177e4SLinus Torvalds 
2848d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
28491da177e4SLinus Torvalds 
28504c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
285115e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
28527bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
28531da177e4SLinus Torvalds 	if (err < 0) {
2854ab364a6fSThomas Graf 		kfree_skb(skb);
2855ab364a6fSThomas Graf 		goto errout;
28561da177e4SLinus Torvalds 	}
28571da177e4SLinus Torvalds 
285815e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2859ab364a6fSThomas Graf errout:
28601da177e4SLinus Torvalds 	return err;
28611da177e4SLinus Torvalds }
28621da177e4SLinus Torvalds 
286386872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
28641da177e4SLinus Torvalds {
28651da177e4SLinus Torvalds 	struct sk_buff *skb;
28665578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2867528c4cebSDenis V. Lunev 	u32 seq;
2868528c4cebSDenis V. Lunev 	int err;
28690d51aa80SJamal Hadi Salim 
2870528c4cebSDenis V. Lunev 	err = -ENOBUFS;
287138308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
287286872cb5SThomas Graf 
2873339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
287438308473SDavid S. Miller 	if (!skb)
287521713ebcSThomas Graf 		goto errout;
28761da177e4SLinus Torvalds 
2877191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
287815e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
287926932566SPatrick McHardy 	if (err < 0) {
288026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
288126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
288226932566SPatrick McHardy 		kfree_skb(skb);
288326932566SPatrick McHardy 		goto errout;
288426932566SPatrick McHardy 	}
288515e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
28865578689aSDaniel Lezcano 		    info->nlh, gfp_any());
28871ce85fe4SPablo Neira Ayuso 	return;
288821713ebcSThomas Graf errout:
288921713ebcSThomas Graf 	if (err < 0)
28905578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
28911da177e4SLinus Torvalds }
28921da177e4SLinus Torvalds 
28938ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2894351638e7SJiri Pirko 				unsigned long event, void *ptr)
28958ed67789SDaniel Lezcano {
2896351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2897c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
28988ed67789SDaniel Lezcano 
28998ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2900d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
29018ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
29028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2903d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
29048ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2905d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
29068ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
29078ed67789SDaniel Lezcano #endif
29088ed67789SDaniel Lezcano 	}
29098ed67789SDaniel Lezcano 
29108ed67789SDaniel Lezcano 	return NOTIFY_OK;
29118ed67789SDaniel Lezcano }
29128ed67789SDaniel Lezcano 
29131da177e4SLinus Torvalds /*
29141da177e4SLinus Torvalds  *	/proc
29151da177e4SLinus Torvalds  */
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29181da177e4SLinus Torvalds 
291933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
292033120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
292133120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
292233120b30SAlexey Dobriyan 	.read		= seq_read,
292333120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
29248d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
292533120b30SAlexey Dobriyan };
292633120b30SAlexey Dobriyan 
29271da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
29281da177e4SLinus Torvalds {
292969ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
29301da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
293169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
293269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
293369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
293469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
293569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2936fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
293769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	return 0;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
29431da177e4SLinus Torvalds {
2944de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
294569ddb805SDaniel Lezcano }
294669ddb805SDaniel Lezcano 
29479a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
29481da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
29491da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
29501da177e4SLinus Torvalds 	.read	 = seq_read,
29511da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2952b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
29531da177e4SLinus Torvalds };
29541da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds static
2959fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
29601da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
29611da177e4SLinus Torvalds {
2962c486da34SLucian Adrian Grijincu 	struct net *net;
2963c486da34SLucian Adrian Grijincu 	int delay;
2964c486da34SLucian Adrian Grijincu 	if (!write)
2965c486da34SLucian Adrian Grijincu 		return -EINVAL;
2966c486da34SLucian Adrian Grijincu 
2967c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2968c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
29698d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
29702ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
29711da177e4SLinus Torvalds 	return 0;
29721da177e4SLinus Torvalds }
29731da177e4SLinus Torvalds 
2974fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
29751da177e4SLinus Torvalds 	{
29761da177e4SLinus Torvalds 		.procname	=	"flush",
29774990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
29781da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
297989c8b3a1SDave Jones 		.mode		=	0200,
29806d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
29811da177e4SLinus Torvalds 	},
29821da177e4SLinus Torvalds 	{
29831da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
29849a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
29851da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29861da177e4SLinus Torvalds 		.mode		=	0644,
29876d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29881da177e4SLinus Torvalds 	},
29891da177e4SLinus Torvalds 	{
29901da177e4SLinus Torvalds 		.procname	=	"max_size",
29914990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
29921da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29931da177e4SLinus Torvalds 		.mode		=	0644,
29946d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29951da177e4SLinus Torvalds 	},
29961da177e4SLinus Torvalds 	{
29971da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
29984990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29991da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30001da177e4SLinus Torvalds 		.mode		=	0644,
30016d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30021da177e4SLinus Torvalds 	},
30031da177e4SLinus Torvalds 	{
30041da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
30054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
30061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30071da177e4SLinus Torvalds 		.mode		=	0644,
30086d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30091da177e4SLinus Torvalds 	},
30101da177e4SLinus Torvalds 	{
30111da177e4SLinus Torvalds 		.procname	=	"gc_interval",
30124990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
30131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30141da177e4SLinus Torvalds 		.mode		=	0644,
30156d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30161da177e4SLinus Torvalds 	},
30171da177e4SLinus Torvalds 	{
30181da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
30194990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
30201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30211da177e4SLinus Torvalds 		.mode		=	0644,
3022f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
30231da177e4SLinus Torvalds 	},
30241da177e4SLinus Torvalds 	{
30251da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
30264990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
30271da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30281da177e4SLinus Torvalds 		.mode		=	0644,
30296d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30301da177e4SLinus Torvalds 	},
30311da177e4SLinus Torvalds 	{
30321da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
30334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
30341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30351da177e4SLinus Torvalds 		.mode		=	0644,
3036f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
30371da177e4SLinus Torvalds 	},
30381da177e4SLinus Torvalds 	{
30391da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
30404990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
30411da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30421da177e4SLinus Torvalds 		.mode		=	0644,
30436d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
30441da177e4SLinus Torvalds 	},
3045f8572d8fSEric W. Biederman 	{ }
30461da177e4SLinus Torvalds };
30471da177e4SLinus Torvalds 
30482c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
3049760f2d01SDaniel Lezcano {
3050760f2d01SDaniel Lezcano 	struct ctl_table *table;
3051760f2d01SDaniel Lezcano 
3052760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
3053760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
3054760f2d01SDaniel Lezcano 			GFP_KERNEL);
30555ee09105SYOSHIFUJI Hideaki 
30565ee09105SYOSHIFUJI Hideaki 	if (table) {
30575ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
3058c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
305986393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
30605ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
30615ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
30625ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
30635ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
30645ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
30655ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
30665ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
30679c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3068464dc801SEric W. Biederman 
3069464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
3070464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
3071464dc801SEric W. Biederman 			table[0].procname = NULL;
30725ee09105SYOSHIFUJI Hideaki 	}
30735ee09105SYOSHIFUJI Hideaki 
3074760f2d01SDaniel Lezcano 	return table;
3075760f2d01SDaniel Lezcano }
30761da177e4SLinus Torvalds #endif
30771da177e4SLinus Torvalds 
30782c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
3079cdb18761SDaniel Lezcano {
3080633d424bSPavel Emelyanov 	int ret = -ENOMEM;
30818ed67789SDaniel Lezcano 
308286393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
308386393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
3084f2fc6a54SBenjamin Thery 
3085fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3086fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
3087fc66f95cSEric Dumazet 
30888ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
30898ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
30908ed67789SDaniel Lezcano 					   GFP_KERNEL);
30918ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
3092fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
3093d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
30948ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
3095d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
309662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
309762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30988ed67789SDaniel Lezcano 
30998ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
31008ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
31018ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
31028ed67789SDaniel Lezcano 					       GFP_KERNEL);
310368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
310468fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3105d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
31068ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3107d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
310862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
310962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
31108ed67789SDaniel Lezcano 
31118ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
31128ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
31138ed67789SDaniel Lezcano 					       GFP_KERNEL);
311468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
311568fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3116d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
31178ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3118d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
311962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
312062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
31218ed67789SDaniel Lezcano #endif
31228ed67789SDaniel Lezcano 
3123b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3124b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3125b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3126b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3127b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3128b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3129b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3130b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3131b339a47cSPeter Zijlstra 
31326891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
31336891a346SBenjamin Thery 
31348ed67789SDaniel Lezcano 	ret = 0;
31358ed67789SDaniel Lezcano out:
31368ed67789SDaniel Lezcano 	return ret;
3137f2fc6a54SBenjamin Thery 
313868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
313968fffc67SPeter Zijlstra out_ip6_prohibit_entry:
314068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
314168fffc67SPeter Zijlstra out_ip6_null_entry:
314268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
314368fffc67SPeter Zijlstra #endif
3144fc66f95cSEric Dumazet out_ip6_dst_entries:
3145fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3146f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3147f2fc6a54SBenjamin Thery 	goto out;
3148cdb18761SDaniel Lezcano }
3149cdb18761SDaniel Lezcano 
31502c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3151cdb18761SDaniel Lezcano {
31528ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
31538ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
31548ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
31558ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
31568ed67789SDaniel Lezcano #endif
315741bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3158cdb18761SDaniel Lezcano }
3159cdb18761SDaniel Lezcano 
3160d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3161d189634eSThomas Graf {
3162d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3163d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3164d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3165d189634eSThomas Graf #endif
3166d189634eSThomas Graf 	return 0;
3167d189634eSThomas Graf }
3168d189634eSThomas Graf 
3169d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3170d189634eSThomas Graf {
3171d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3172ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3173ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3174d189634eSThomas Graf #endif
3175d189634eSThomas Graf }
3176d189634eSThomas Graf 
3177cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3178cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3179cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3180cdb18761SDaniel Lezcano };
3181cdb18761SDaniel Lezcano 
3182c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3183c3426b47SDavid S. Miller {
3184c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3185c3426b47SDavid S. Miller 
3186c3426b47SDavid S. Miller 	if (!bp)
3187c3426b47SDavid S. Miller 		return -ENOMEM;
3188c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3189c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3190c3426b47SDavid S. Miller 	return 0;
3191c3426b47SDavid S. Miller }
3192c3426b47SDavid S. Miller 
3193c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3194c3426b47SDavid S. Miller {
3195c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3196c3426b47SDavid S. Miller 
3197c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
319856a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3199c3426b47SDavid S. Miller 	kfree(bp);
3200c3426b47SDavid S. Miller }
3201c3426b47SDavid S. Miller 
32022b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3203c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3204c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3205c3426b47SDavid S. Miller };
3206c3426b47SDavid S. Miller 
3207d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3208d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3209d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3210d189634eSThomas Graf };
3211d189634eSThomas Graf 
32128ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
32138ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
32148ed67789SDaniel Lezcano 	.priority = 0,
32158ed67789SDaniel Lezcano };
32168ed67789SDaniel Lezcano 
3217433d49c3SDaniel Lezcano int __init ip6_route_init(void)
32181da177e4SLinus Torvalds {
3219433d49c3SDaniel Lezcano 	int ret;
3220433d49c3SDaniel Lezcano 
32219a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
32229a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
32239a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
32249a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
32259a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3226c19a28e1SFernando Carrijo 		goto out;
322714e50e57SDavid S. Miller 
3228fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
32298ed67789SDaniel Lezcano 	if (ret)
3230bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3231bdb3289fSDaniel Lezcano 
3232c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3233c3426b47SDavid S. Miller 	if (ret)
3234e8803b6cSDavid S. Miller 		goto out_dst_entries;
32352a0c451aSThomas Graf 
32367e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
32377e52b33bSDavid S. Miller 	if (ret)
32387e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3239c3426b47SDavid S. Miller 
32405dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
32415dc121e9SArnaud Ebalard 
32428ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
32438ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
32448ed67789SDaniel Lezcano 	 * manually for init_net */
3245d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
32468ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3247bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3248d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
32498ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3250d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
32518ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3252bdb3289fSDaniel Lezcano   #endif
3253e8803b6cSDavid S. Miller 	ret = fib6_init();
3254433d49c3SDaniel Lezcano 	if (ret)
32558ed67789SDaniel Lezcano 		goto out_register_subsys;
3256433d49c3SDaniel Lezcano 
3257433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3258433d49c3SDaniel Lezcano 	if (ret)
3259e8803b6cSDavid S. Miller 		goto out_fib6_init;
3260c35b7e72SDaniel Lezcano 
3261433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3262433d49c3SDaniel Lezcano 	if (ret)
3263433d49c3SDaniel Lezcano 		goto xfrm6_init;
32647e5449c2SDaniel Lezcano 
3265d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3266d189634eSThomas Graf 	if (ret)
3267d189634eSThomas Graf 		goto fib6_rules_init;
3268d189634eSThomas Graf 
3269433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3270c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3271c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3272c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3273d189634eSThomas Graf 		goto out_register_late_subsys;
3274433d49c3SDaniel Lezcano 
32758ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3276cdb18761SDaniel Lezcano 	if (ret)
3277d189634eSThomas Graf 		goto out_register_late_subsys;
32788ed67789SDaniel Lezcano 
3279433d49c3SDaniel Lezcano out:
3280433d49c3SDaniel Lezcano 	return ret;
3281433d49c3SDaniel Lezcano 
3282d189634eSThomas Graf out_register_late_subsys:
3283d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3284433d49c3SDaniel Lezcano fib6_rules_init:
3285433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3286433d49c3SDaniel Lezcano xfrm6_init:
3287433d49c3SDaniel Lezcano 	xfrm6_fini();
32882a0c451aSThomas Graf out_fib6_init:
32892a0c451aSThomas Graf 	fib6_gc_cleanup();
32908ed67789SDaniel Lezcano out_register_subsys:
32918ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
32927e52b33bSDavid S. Miller out_register_inetpeer:
32937e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3294fc66f95cSEric Dumazet out_dst_entries:
3295fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3296433d49c3SDaniel Lezcano out_kmem_cache:
3297f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3298433d49c3SDaniel Lezcano 	goto out;
32991da177e4SLinus Torvalds }
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds void ip6_route_cleanup(void)
33021da177e4SLinus Torvalds {
33038ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3304d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3305101367c2SThomas Graf 	fib6_rules_cleanup();
33061da177e4SLinus Torvalds 	xfrm6_fini();
33071da177e4SLinus Torvalds 	fib6_gc_cleanup();
3308c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
33098ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
331041bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3311f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
33121da177e4SLinus Torvalds }
3313