xref: /openbmc/linux/net/ipv6/route.c (revision 7e9805696428113e34625a65a30dbc62cb78acc5)
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 {
69*7e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
70*7e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
71*7e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
72afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
73afc154e9SHannes Frederic Sowa };
74afc154e9SHannes Frederic Sowa 
751716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
7621efcfa0SEric Dumazet 				    const struct in6_addr *dest);
771da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
780dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
79ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
831da177e4SLinus Torvalds 				       struct net_device *dev, int how);
84569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
871da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
887150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
897150aedeSKamala R static int		ip6_pkt_prohibit_out(struct sk_buff *skb);
901da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
916700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
926700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
936700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
946700c270SDavid S. Miller 					struct sk_buff *skb);
9552bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
961da177e4SLinus Torvalds 
9770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
98efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
99b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
100b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
10195c96174SEric Dumazet 					   unsigned int pref);
102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
103b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
104b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
10570ceb4f5SYOSHIFUJI Hideaki #endif
10670ceb4f5SYOSHIFUJI Hideaki 
10706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10806582540SDavid S. Miller {
10906582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
11006582540SDavid S. Miller 	struct inet_peer *peer;
11106582540SDavid S. Miller 	u32 *p = NULL;
11206582540SDavid S. Miller 
1138e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1148e2ec639SYan, Zheng 		return NULL;
1158e2ec639SYan, Zheng 
116fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
11706582540SDavid S. Miller 	if (peer) {
11806582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11906582540SDavid S. Miller 		unsigned long prev, new;
12006582540SDavid S. Miller 
12106582540SDavid S. Miller 		p = peer->metrics;
12206582540SDavid S. Miller 		if (inet_metrics_new(peer))
12306582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
12406582540SDavid S. Miller 
12506582540SDavid S. Miller 		new = (unsigned long) p;
12606582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
12706582540SDavid S. Miller 
12806582540SDavid S. Miller 		if (prev != old) {
12906582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
13006582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
13106582540SDavid S. Miller 				p = NULL;
13206582540SDavid S. Miller 		}
13306582540SDavid S. Miller 	}
13406582540SDavid S. Miller 	return p;
13506582540SDavid S. Miller }
13606582540SDavid S. Miller 
137f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
138f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
139f894cbf8SDavid S. Miller 					     const void *daddr)
14039232973SDavid S. Miller {
14139232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
14239232973SDavid S. Miller 
143a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
14439232973SDavid S. Miller 		return (const void *) p;
145f894cbf8SDavid S. Miller 	else if (skb)
146f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
14739232973SDavid S. Miller 	return daddr;
14839232973SDavid S. Miller }
14939232973SDavid S. Miller 
150f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
151f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
152f894cbf8SDavid S. Miller 					  const void *daddr)
153d3aaeb38SDavid S. Miller {
15439232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
15539232973SDavid S. Miller 	struct neighbour *n;
15639232973SDavid S. Miller 
157f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1588e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
159f83c7790SDavid S. Miller 	if (n)
160f83c7790SDavid S. Miller 		return n;
161f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
162f83c7790SDavid S. Miller }
163f83c7790SDavid S. Miller 
1649a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1651da177e4SLinus Torvalds 	.family			=	AF_INET6,
16609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1671da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1681da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1691da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1700dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
171ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
17206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1731da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1741da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1751da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1761da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1771da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1786e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1791ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
180d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1811da177e4SLinus Torvalds };
1821da177e4SLinus Torvalds 
183ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
184ec831ea7SRoland Dreier {
185618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
186618f9bc7SSteffen Klassert 
187618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
188ec831ea7SRoland Dreier }
189ec831ea7SRoland Dreier 
1906700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1916700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
19214e50e57SDavid S. Miller {
19314e50e57SDavid S. Miller }
19414e50e57SDavid S. Miller 
1956700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1966700c270SDavid S. Miller 				      struct sk_buff *skb)
197b587ee3bSDavid S. Miller {
198b587ee3bSDavid S. Miller }
199b587ee3bSDavid S. Miller 
2000972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
2010972ddb2SHeld Bernhard 					 unsigned long old)
2020972ddb2SHeld Bernhard {
2030972ddb2SHeld Bernhard 	return NULL;
2040972ddb2SHeld Bernhard }
2050972ddb2SHeld Bernhard 
20614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
20714e50e57SDavid S. Miller 	.family			=	AF_INET6,
20809640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20914e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
21014e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
211ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
212214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
21314e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
214b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2150972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
216d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
21714e50e57SDavid S. Miller };
21814e50e57SDavid S. Miller 
21962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
22014edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
22162fa8a84SDavid S. Miller };
22262fa8a84SDavid S. Miller 
223fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2241da177e4SLinus Torvalds 	.dst = {
2251da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2261da177e4SLinus Torvalds 		.__use		= 1,
2272c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2281da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2291da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2301da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2311da177e4SLinus Torvalds 	},
2321da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2334f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2341da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2351da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2361da177e4SLinus Torvalds };
2371da177e4SLinus Torvalds 
238101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
239101367c2SThomas Graf 
240fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
241101367c2SThomas Graf 	.dst = {
242101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
243101367c2SThomas Graf 		.__use		= 1,
2442c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
245101367c2SThomas Graf 		.error		= -EACCES,
2469ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2479ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
248101367c2SThomas Graf 	},
249101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2504f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
251101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
252101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
253101367c2SThomas Graf };
254101367c2SThomas Graf 
255fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
256101367c2SThomas Graf 	.dst = {
257101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
258101367c2SThomas Graf 		.__use		= 1,
2592c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
260101367c2SThomas Graf 		.error		= -EINVAL,
261352e512cSHerbert Xu 		.input		= dst_discard,
262352e512cSHerbert Xu 		.output		= dst_discard,
263101367c2SThomas Graf 	},
264101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2654f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
266101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
267101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
268101367c2SThomas Graf };
269101367c2SThomas Graf 
270101367c2SThomas Graf #endif
271101367c2SThomas Graf 
2721da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
27397bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
274957c665fSDavid S. Miller 					     struct net_device *dev,
2758b96d22dSDavid S. Miller 					     int flags,
2768b96d22dSDavid S. Miller 					     struct fib6_table *table)
2771da177e4SLinus Torvalds {
27897bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2796f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
280cf911662SDavid S. Miller 
28197bab73fSDavid S. Miller 	if (rt) {
2828104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2838104891bSSteffen Klassert 
2848104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2858b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
286ca4c3fc2Sfan.du 		rt->rt6i_genid = rt_genid_ipv6(net);
28751ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28897bab73fSDavid S. Miller 	}
289cf911662SDavid S. Miller 	return rt;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2951da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
296ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2971da177e4SLinus Torvalds 
2988e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2998e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3008e2ec639SYan, Zheng 
30138308473SDavid S. Miller 	if (idev) {
3021da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3031da177e4SLinus Torvalds 		in6_dev_put(idev);
3041da177e4SLinus Torvalds 	}
3051716a961SGao feng 
306ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
307ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3081716a961SGao feng 
30997bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
31097bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
311b3419363SDavid S. Miller 		inet_putpeer(peer);
312b3419363SDavid S. Miller 	}
313b3419363SDavid S. Miller }
314b3419363SDavid S. Miller 
315b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
316b3419363SDavid S. Miller {
31797bab73fSDavid S. Miller 	struct inet_peer_base *base;
318b3419363SDavid S. Miller 	struct inet_peer *peer;
319b3419363SDavid S. Miller 
32097bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
32197bab73fSDavid S. Miller 	if (!base)
32297bab73fSDavid S. Miller 		return;
32397bab73fSDavid S. Miller 
32497bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3257b34ca2aSDavid S. Miller 	if (peer) {
32697bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
327b3419363SDavid S. Miller 			inet_putpeer(peer);
3281da177e4SLinus Torvalds 	}
3297b34ca2aSDavid S. Miller }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3321da177e4SLinus Torvalds 			   int how)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3351da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3365a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
337c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3381da177e4SLinus Torvalds 
33997cac082SDavid S. Miller 	if (dev != loopback_dev) {
34097cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3415a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3425a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
34338308473SDavid S. Miller 			if (loopback_idev) {
3441da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3451da177e4SLinus Torvalds 				in6_dev_put(idev);
3461da177e4SLinus Torvalds 			}
3471da177e4SLinus Torvalds 		}
34897cac082SDavid S. Miller 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3521da177e4SLinus Torvalds {
3531716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3541716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
355a50feda5SEric Dumazet 			return true;
3561716a961SGao feng 	} else if (rt->dst.from) {
3573fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3581716a961SGao feng 	}
359a50feda5SEric Dumazet 	return false;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
363c71099acSThomas Graf {
364a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
365a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
366c71099acSThomas Graf }
367c71099acSThomas Graf 
36851ebd318SNicolas Dichtel /* Multipath route selection:
36951ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
37051ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
37151ebd318SNicolas Dichtel  */
37251ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
37351ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
37451ebd318SNicolas Dichtel {
37551ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
37651ebd318SNicolas Dichtel 
377c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
378c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
37951ebd318SNicolas Dichtel 
38051ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
38151ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
38251ebd318SNicolas Dichtel 	case IPPROTO_UDP:
38351ebd318SNicolas Dichtel 	case IPPROTO_TCP:
38451ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
385b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
386b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38751ebd318SNicolas Dichtel 		break;
38851ebd318SNicolas Dichtel 
38951ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
390b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
391b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
39251ebd318SNicolas Dichtel 		break;
39351ebd318SNicolas Dichtel 	}
39451ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
395b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
39651ebd318SNicolas Dichtel 
39751ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39851ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
39951ebd318SNicolas Dichtel 	return val % candidate_count;
40051ebd318SNicolas Dichtel }
40151ebd318SNicolas Dichtel 
40251ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
40352bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
40452bd4c0cSNicolas Dichtel 					     int strict)
40551ebd318SNicolas Dichtel {
40651ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
40751ebd318SNicolas Dichtel 	int route_choosen;
40851ebd318SNicolas Dichtel 
40951ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
41051ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
41151ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
41251ebd318SNicolas Dichtel 	 */
41351ebd318SNicolas Dichtel 	if (route_choosen)
41451ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
41551ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
41651ebd318SNicolas Dichtel 			route_choosen--;
41751ebd318SNicolas Dichtel 			if (route_choosen == 0) {
41852bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
41952bd4c0cSNicolas Dichtel 					break;
42051ebd318SNicolas Dichtel 				match = sibling;
42151ebd318SNicolas Dichtel 				break;
42251ebd318SNicolas Dichtel 			}
42351ebd318SNicolas Dichtel 		}
42451ebd318SNicolas Dichtel 	return match;
42551ebd318SNicolas Dichtel }
42651ebd318SNicolas Dichtel 
4271da177e4SLinus Torvalds /*
428c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds 
4318ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4328ed67789SDaniel Lezcano 						    struct rt6_info *rt,
433b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4341da177e4SLinus Torvalds 						    int oif,
435d420895eSYOSHIFUJI Hideaki 						    int flags)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4381da177e4SLinus Torvalds 	struct rt6_info *sprt;
4391da177e4SLinus Torvalds 
440dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
441dd3abc4eSYOSHIFUJI Hideaki 		goto out;
442dd3abc4eSYOSHIFUJI Hideaki 
443d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
444d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
445dd3abc4eSYOSHIFUJI Hideaki 
446dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4471da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4481da177e4SLinus Torvalds 				return sprt;
4491da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
45038308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4511da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
452d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4531da177e4SLinus Torvalds 						continue;
4541da177e4SLinus Torvalds 					if (local && (!oif ||
4551da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4561da177e4SLinus Torvalds 						continue;
4571da177e4SLinus Torvalds 				}
4581da177e4SLinus Torvalds 				local = sprt;
4591da177e4SLinus Torvalds 			}
460dd3abc4eSYOSHIFUJI Hideaki 		} else {
461dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
462dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
463dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
464dd3abc4eSYOSHIFUJI Hideaki 		}
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
467dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4681da177e4SLinus Torvalds 		if (local)
4691da177e4SLinus Torvalds 			return local;
4701da177e4SLinus Torvalds 
471d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4728ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4731da177e4SLinus Torvalds 	}
474dd3abc4eSYOSHIFUJI Hideaki out:
4751da177e4SLinus Torvalds 	return rt;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
47827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
479c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
480c2f17e82SHannes Frederic Sowa 	struct work_struct work;
481c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
482c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
483c2f17e82SHannes Frederic Sowa };
484c2f17e82SHannes Frederic Sowa 
485c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
486c2f17e82SHannes Frederic Sowa {
487c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
488c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
489c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
490c2f17e82SHannes Frederic Sowa 
491c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
492c2f17e82SHannes Frederic Sowa 	ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
493c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
494c2f17e82SHannes Frederic Sowa 	kfree(w);
495c2f17e82SHannes Frederic Sowa }
496c2f17e82SHannes Frederic Sowa 
49727097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
49827097255SYOSHIFUJI Hideaki {
499f2c31e32SEric Dumazet 	struct neighbour *neigh;
50027097255SYOSHIFUJI Hideaki 	/*
50127097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
50227097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
50327097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
50427097255SYOSHIFUJI Hideaki 	 *
50527097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
50627097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
50727097255SYOSHIFUJI Hideaki 	 */
5082152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
509fdd6681dSAmerigo Wang 		return;
5102152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5112152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
5122152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5132152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
5142152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
5152152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
5167ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
5172152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5182152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
51952e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
520c2f17e82SHannes Frederic Sowa 		struct __rt6_probe_work *work;
52127097255SYOSHIFUJI Hideaki 
522c2f17e82SHannes Frederic Sowa 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
523c2f17e82SHannes Frederic Sowa 
524c2f17e82SHannes Frederic Sowa 		if (neigh && work)
525*7e980569SJiri Benc 			__neigh_set_probe_once(neigh);
5262152caeaSYOSHIFUJI Hideaki / 吉藤英明 
527c2f17e82SHannes Frederic Sowa 		if (neigh)
528c2f17e82SHannes Frederic Sowa 			write_unlock(&neigh->lock);
529c2f17e82SHannes Frederic Sowa 
530c2f17e82SHannes Frederic Sowa 		if (work) {
531c2f17e82SHannes Frederic Sowa 			INIT_WORK(&work->work, rt6_probe_deferred);
532c2f17e82SHannes Frederic Sowa 			work->target = rt->rt6i_gateway;
533c2f17e82SHannes Frederic Sowa 			dev_hold(rt->dst.dev);
534c2f17e82SHannes Frederic Sowa 			work->dev = rt->dst.dev;
535c2f17e82SHannes Frederic Sowa 			schedule_work(&work->work);
536c2f17e82SHannes Frederic Sowa 		}
537f2c31e32SEric Dumazet 	} else {
5382152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5392152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
54027097255SYOSHIFUJI Hideaki 	}
5412152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
542f2c31e32SEric Dumazet }
54327097255SYOSHIFUJI Hideaki #else
54427097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
54527097255SYOSHIFUJI Hideaki {
54627097255SYOSHIFUJI Hideaki }
54727097255SYOSHIFUJI Hideaki #endif
54827097255SYOSHIFUJI Hideaki 
5491da177e4SLinus Torvalds /*
550554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5511da177e4SLinus Torvalds  */
552b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5531da177e4SLinus Torvalds {
554d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
555161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
556554cfb7eSYOSHIFUJI Hideaki 		return 2;
557161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
558161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
559161980f4SDavid S. Miller 		return 1;
560554cfb7eSYOSHIFUJI Hideaki 	return 0;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
563afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5641da177e4SLinus Torvalds {
565f2c31e32SEric Dumazet 	struct neighbour *neigh;
566afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
567f2c31e32SEric Dumazet 
5684d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5694d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
570afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
571145a3621SYOSHIFUJI Hideaki / 吉藤英明 
572145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
573145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
574145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
575145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
576554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
577afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
578398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
579a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
580afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
581*7e980569SJiri Benc 		else
582*7e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
583398bcbebSYOSHIFUJI Hideaki #endif
584145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
585afc154e9SHannes Frederic Sowa 	} else {
586afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
587*7e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
588a5a81f0bSPaul Marks 	}
589145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
590145a3621SYOSHIFUJI Hideaki / 吉藤英明 
591a5a81f0bSPaul Marks 	return ret;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
595554cfb7eSYOSHIFUJI Hideaki 			   int strict)
596554cfb7eSYOSHIFUJI Hideaki {
597a5a81f0bSPaul Marks 	int m;
5984d0c5911SYOSHIFUJI Hideaki 
5994d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
60077d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
601afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
602ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
603ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
604ebacaaa0SYOSHIFUJI Hideaki #endif
605afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
606afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
607afc154e9SHannes Frederic Sowa 		if (n < 0)
608afc154e9SHannes Frederic Sowa 			return n;
609afc154e9SHannes Frederic Sowa 	}
610554cfb7eSYOSHIFUJI Hideaki 	return m;
611554cfb7eSYOSHIFUJI Hideaki }
612554cfb7eSYOSHIFUJI Hideaki 
613f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
614afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
615afc154e9SHannes Frederic Sowa 				   bool *do_rr)
616554cfb7eSYOSHIFUJI Hideaki {
617554cfb7eSYOSHIFUJI Hideaki 	int m;
618afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
619554cfb7eSYOSHIFUJI Hideaki 
620554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
621f11e6659SDavid S. Miller 		goto out;
622554cfb7eSYOSHIFUJI Hideaki 
623554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
624*7e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
625afc154e9SHannes Frederic Sowa 		match_do_rr = true;
626afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
627*7e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
628f11e6659SDavid S. Miller 		goto out;
6291da177e4SLinus Torvalds 	}
630f11e6659SDavid S. Miller 
631afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
632afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
633afc154e9SHannes Frederic Sowa 
634*7e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
635afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
636afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
637afc154e9SHannes Frederic Sowa 		*mpri = m;
638afc154e9SHannes Frederic Sowa 		match = rt;
639afc154e9SHannes Frederic Sowa 	}
640f11e6659SDavid S. Miller out:
641f11e6659SDavid S. Miller 	return match;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
644f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
645f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
646afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
647afc154e9SHannes Frederic Sowa 				     bool *do_rr)
648f11e6659SDavid S. Miller {
649f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
650f11e6659SDavid S. Miller 	int mpri = -1;
651f11e6659SDavid S. Miller 
652f11e6659SDavid S. Miller 	match = NULL;
653f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
654d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
655afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
656f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
657d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
658afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
659f11e6659SDavid S. Miller 
660f11e6659SDavid S. Miller 	return match;
661f11e6659SDavid S. Miller }
662f11e6659SDavid S. Miller 
663f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
664f11e6659SDavid S. Miller {
665f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6668ed67789SDaniel Lezcano 	struct net *net;
667afc154e9SHannes Frederic Sowa 	bool do_rr = false;
668f11e6659SDavid S. Miller 
669f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
670f11e6659SDavid S. Miller 	if (!rt0)
671f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
672f11e6659SDavid S. Miller 
673afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
674afc154e9SHannes Frederic Sowa 			     &do_rr);
675f11e6659SDavid S. Miller 
676afc154e9SHannes Frederic Sowa 	if (do_rr) {
677d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
678f11e6659SDavid S. Miller 
679554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
680f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
681f11e6659SDavid S. Miller 			next = fn->leaf;
682f11e6659SDavid S. Miller 
683f11e6659SDavid S. Miller 		if (next != rt0)
684f11e6659SDavid S. Miller 			fn->rr_ptr = next;
685554cfb7eSYOSHIFUJI Hideaki 	}
686554cfb7eSYOSHIFUJI Hideaki 
687d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
688a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
69170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
69270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
693b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
69470ceb4f5SYOSHIFUJI Hideaki {
695c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
69670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
69770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
69870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6994bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
70070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
70170ceb4f5SYOSHIFUJI Hideaki 
70270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
70370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
70470ceb4f5SYOSHIFUJI Hideaki 	}
70570ceb4f5SYOSHIFUJI Hideaki 
70670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
70770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
70870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
70970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
71070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
71170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
71270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
71370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
71470ceb4f5SYOSHIFUJI Hideaki 		}
71570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
71670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
71770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
71870ceb4f5SYOSHIFUJI Hideaki 		}
71970ceb4f5SYOSHIFUJI Hideaki 	}
72070ceb4f5SYOSHIFUJI Hideaki 
72170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
72270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
7233933fc95SJens Rosenboom 		return -EINVAL;
72470ceb4f5SYOSHIFUJI Hideaki 
7254bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
72670ceb4f5SYOSHIFUJI Hideaki 
72770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
72870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
72970ceb4f5SYOSHIFUJI Hideaki 	else {
73070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
73170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
73270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
73370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
73470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
73570ceb4f5SYOSHIFUJI Hideaki 	}
73670ceb4f5SYOSHIFUJI Hideaki 
737f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
738f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
739f104a567SDuan Jiong 	else
740f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
741f104a567SDuan Jiong 					gwaddr, dev->ifindex);
74270ceb4f5SYOSHIFUJI Hideaki 
74370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
744e0a1ad73SThomas Graf 		ip6_del_rt(rt);
74570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
74670ceb4f5SYOSHIFUJI Hideaki 	}
74770ceb4f5SYOSHIFUJI Hideaki 
74870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
749efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
75070ceb4f5SYOSHIFUJI Hideaki 					pref);
75170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
75270ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
75370ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
75470ceb4f5SYOSHIFUJI Hideaki 
75570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7561716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7571716a961SGao feng 			rt6_clean_expires(rt);
7581716a961SGao feng 		else
7591716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7601716a961SGao feng 
76194e187c0SAmerigo Wang 		ip6_rt_put(rt);
76270ceb4f5SYOSHIFUJI Hideaki 	}
76370ceb4f5SYOSHIFUJI Hideaki 	return 0;
76470ceb4f5SYOSHIFUJI Hideaki }
76570ceb4f5SYOSHIFUJI Hideaki #endif
76670ceb4f5SYOSHIFUJI Hideaki 
7678ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
768982f56f3SYOSHIFUJI Hideaki do { \
7698ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
770982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
771e0eda7bbSVille Nuorvala 		while (1) { \
772982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
773c71099acSThomas Graf 				goto out; \
774982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
775982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7768bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
777982f56f3SYOSHIFUJI Hideaki 			else \
778982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
779c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
780c71099acSThomas Graf 				goto restart; \
781c71099acSThomas Graf 		} \
782982f56f3SYOSHIFUJI Hideaki 	} \
783982f56f3SYOSHIFUJI Hideaki } while (0)
784c71099acSThomas Graf 
7858ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7868ed67789SDaniel Lezcano 					     struct fib6_table *table,
7874c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	struct fib6_node *fn;
7901da177e4SLinus Torvalds 	struct rt6_info *rt;
7911da177e4SLinus Torvalds 
792c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7934c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
794c71099acSThomas Graf restart:
795c71099acSThomas Graf 	rt = fn->leaf;
7964c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
79751ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
79852bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
7994c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
800c71099acSThomas Graf out:
801d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
802c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8031da177e4SLinus Torvalds 	return rt;
804c71099acSThomas Graf 
805c71099acSThomas Graf }
806c71099acSThomas Graf 
807ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
808ea6e574eSFlorian Westphal 				    int flags)
809ea6e574eSFlorian Westphal {
810ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
811ea6e574eSFlorian Westphal }
812ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
813ea6e574eSFlorian Westphal 
8149acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
8159acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
816c71099acSThomas Graf {
8174c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8184c9483b2SDavid S. Miller 		.flowi6_oif = oif,
8194c9483b2SDavid S. Miller 		.daddr = *daddr,
820c71099acSThomas Graf 	};
821c71099acSThomas Graf 	struct dst_entry *dst;
82277d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
823c71099acSThomas Graf 
824adaa70bbSThomas Graf 	if (saddr) {
8254c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
826adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
827adaa70bbSThomas Graf 	}
828adaa70bbSThomas Graf 
8294c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
830c71099acSThomas Graf 	if (dst->error == 0)
831c71099acSThomas Graf 		return (struct rt6_info *) dst;
832c71099acSThomas Graf 
833c71099acSThomas Graf 	dst_release(dst);
834c71099acSThomas Graf 
8351da177e4SLinus Torvalds 	return NULL;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8387159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8397159039aSYOSHIFUJI Hideaki 
840c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8411da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8421da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8431da177e4SLinus Torvalds    be destroyed.
8441da177e4SLinus Torvalds  */
8451da177e4SLinus Torvalds 
84686872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	int err;
849c71099acSThomas Graf 	struct fib6_table *table;
8501da177e4SLinus Torvalds 
851c71099acSThomas Graf 	table = rt->rt6i_table;
852c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
85386872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
854c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	return err;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
85940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
86040e22e8fSThomas Graf {
8614d1169c1SDenis V. Lunev 	struct nl_info info = {
862d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8634d1169c1SDenis V. Lunev 	};
864528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
86540e22e8fSThomas Graf }
86640e22e8fSThomas Graf 
8671716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
86821efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
869b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	struct rt6_info *rt;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/*
8741da177e4SLinus Torvalds 	 *	Clone the route.
8751da177e4SLinus Torvalds 	 */
8761da177e4SLinus Torvalds 
87721efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	if (rt) {
880bb3c3686SDavid S. Miller 		if (ort->rt6i_dst.plen != 128 &&
88121efcfa0SEric Dumazet 		    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
88258c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8871da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8884e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8891da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds #endif
89295a9a5baSYOSHIFUJI Hideaki 	}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return rt;
8951da177e4SLinus Torvalds }
89695a9a5baSYOSHIFUJI Hideaki 
89721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
89821efcfa0SEric Dumazet 					const struct in6_addr *daddr)
899299d9939SYOSHIFUJI Hideaki {
90021efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
90121efcfa0SEric Dumazet 
902887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
903299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
904299d9939SYOSHIFUJI Hideaki 	return rt;
905299d9939SYOSHIFUJI Hideaki }
906299d9939SYOSHIFUJI Hideaki 
9078ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
9084c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	struct fib6_node *fn;
911519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
912c71099acSThomas Graf 	int strict = 0;
9131da177e4SLinus Torvalds 	int attempts = 3;
914519fbd87SYOSHIFUJI Hideaki 	int err;
91553b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
9161da177e4SLinus Torvalds 
91777d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds relookup:
920c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
9211da177e4SLinus Torvalds 
9228238dd06SYOSHIFUJI Hideaki restart_2:
9234c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds restart:
9264acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
92752bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
92852bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
9294c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9308ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9318238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 
934d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
935c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9361da177e4SLinus Torvalds 
937c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
9384c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9397343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9404c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9417343ff31SDavid S. Miller 	else
9427343ff31SDavid S. Miller 		goto out2;
9431da177e4SLinus Torvalds 
94494e187c0SAmerigo Wang 	ip6_rt_put(rt);
9458ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9461da177e4SLinus Torvalds 
947d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
948e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
94940e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
950e40cf353SYOSHIFUJI Hideaki 		if (!err)
951e40cf353SYOSHIFUJI Hideaki 			goto out2;
952e40cf353SYOSHIFUJI Hideaki 	}
953e40cf353SYOSHIFUJI Hideaki 
954e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9551da177e4SLinus Torvalds 		goto out2;
9561da177e4SLinus Torvalds 
957519fbd87SYOSHIFUJI Hideaki 	/*
958c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
959519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9601da177e4SLinus Torvalds 	 */
96194e187c0SAmerigo Wang 	ip6_rt_put(rt);
9621da177e4SLinus Torvalds 	goto relookup;
963e40cf353SYOSHIFUJI Hideaki 
964519fbd87SYOSHIFUJI Hideaki out:
9658238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9668238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9678238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9688238dd06SYOSHIFUJI Hideaki 	}
969d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
970c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9711da177e4SLinus Torvalds out2:
972d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
973d8d1f30bSChangli Gao 	rt->dst.__use++;
974c71099acSThomas Graf 
975c71099acSThomas Graf 	return rt;
976c71099acSThomas Graf }
977c71099acSThomas Graf 
9788ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9794c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9804acad72dSPavel Emelyanov {
9814c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9824acad72dSPavel Emelyanov }
9834acad72dSPavel Emelyanov 
98472331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
98572331bc0SShmulik Ladkani 						struct net_device *dev,
98672331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
98772331bc0SShmulik Ladkani {
98872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
98972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
99072331bc0SShmulik Ladkani 
99172331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
99272331bc0SShmulik Ladkani }
99372331bc0SShmulik Ladkani 
994c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
995c71099acSThomas Graf {
996b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
997c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
998adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9994c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
10004c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
10014c9483b2SDavid S. Miller 		.daddr = iph->daddr,
10024c9483b2SDavid S. Miller 		.saddr = iph->saddr,
10036502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
10044c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
10054c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1006c71099acSThomas Graf 	};
1007adaa70bbSThomas Graf 
100872331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1009c71099acSThomas Graf }
1010c71099acSThomas Graf 
10118ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
10124c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1013c71099acSThomas Graf {
10144c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1015c71099acSThomas Graf }
1016c71099acSThomas Graf 
10179c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
10184c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
1019c71099acSThomas Graf {
1020c71099acSThomas Graf 	int flags = 0;
1021c71099acSThomas Graf 
10221fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
10234dc27d1cSDavid McCullough 
10244c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
102577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1026c71099acSThomas Graf 
10274c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1028adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10290c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10300c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1031adaa70bbSThomas Graf 
10324c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10357159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10361da177e4SLinus Torvalds 
10372774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
103814e50e57SDavid S. Miller {
10395c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
104014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
104114e50e57SDavid S. Miller 
1042f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
104314e50e57SDavid S. Miller 	if (rt) {
1044d8d1f30bSChangli Gao 		new = &rt->dst;
104514e50e57SDavid S. Miller 
10468104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10478104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10488104891bSSteffen Klassert 
104914e50e57SDavid S. Miller 		new->__use = 1;
1050352e512cSHerbert Xu 		new->input = dst_discard;
1051352e512cSHerbert Xu 		new->output = dst_discard;
105214e50e57SDavid S. Miller 
105321efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
105421efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
105521efcfa0SEric Dumazet 		else
1056defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
105714e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
105814e50e57SDavid S. Miller 		if (rt->rt6i_idev)
105914e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
106014e50e57SDavid S. Miller 
10614e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10621716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
106314e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
106414e50e57SDavid S. Miller 
106514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
106614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
106714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
106814e50e57SDavid S. Miller #endif
106914e50e57SDavid S. Miller 
107014e50e57SDavid S. Miller 		dst_free(new);
107114e50e57SDavid S. Miller 	}
107214e50e57SDavid S. Miller 
107369ead7afSDavid S. Miller 	dst_release(dst_orig);
107469ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
107514e50e57SDavid S. Miller }
107614e50e57SDavid S. Miller 
10771da177e4SLinus Torvalds /*
10781da177e4SLinus Torvalds  *	Destination cache support functions
10791da177e4SLinus Torvalds  */
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	struct rt6_info *rt;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10861da177e4SLinus Torvalds 
10876f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10886f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10896f3118b5SNicolas Dichtel 	 * into this function always.
10906f3118b5SNicolas Dichtel 	 */
1091ca4c3fc2Sfan.du 	if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev)))
10926f3118b5SNicolas Dichtel 		return NULL;
10936f3118b5SNicolas Dichtel 
1094e3bc10bdSHannes Frederic Sowa 	if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
10951da177e4SLinus Torvalds 		return NULL;
1096e3bc10bdSHannes Frederic Sowa 
1097e3bc10bdSHannes Frederic Sowa 	if (rt6_check_expired(rt))
1098e3bc10bdSHannes Frederic Sowa 		return NULL;
1099e3bc10bdSHannes Frederic Sowa 
1100e3bc10bdSHannes Frederic Sowa 	return dst;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
11041da177e4SLinus Torvalds {
11051da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	if (rt) {
110854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
110954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1110e0a1ad73SThomas Graf 				ip6_del_rt(rt);
111154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
11121da177e4SLinus Torvalds 			}
111354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
111454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
111554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
111654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
111754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
111854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct rt6_info *rt;
11241da177e4SLinus Torvalds 
11253ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11261da177e4SLinus Torvalds 
1127adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11281da177e4SLinus Torvalds 	if (rt) {
11291eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11301eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11311eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11321eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11331eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11341da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11351da177e4SLinus Torvalds 		}
11361da177e4SLinus Torvalds 	}
11371eb4f758SHannes Frederic Sowa }
11381da177e4SLinus Torvalds 
11396700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11406700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11431da177e4SLinus Torvalds 
114481aded24SDavid S. Miller 	dst_confirm(dst);
11451da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
114681aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
114781aded24SDavid S. Miller 
11481da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11491da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1150defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11511da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1152defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1153defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11541da177e4SLinus Torvalds 		}
1155defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
115681aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
116042ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
116142ae66c8SDavid S. Miller 		     int oif, u32 mark)
116281aded24SDavid S. Miller {
116381aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
116481aded24SDavid S. Miller 	struct dst_entry *dst;
116581aded24SDavid S. Miller 	struct flowi6 fl6;
116681aded24SDavid S. Miller 
116781aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
116881aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
116981aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
117081aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
117181aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11726502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
117381aded24SDavid S. Miller 
117481aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
117581aded24SDavid S. Miller 	if (!dst->error)
11766700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
117781aded24SDavid S. Miller 	dst_release(dst);
117881aded24SDavid S. Miller }
117981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
118081aded24SDavid S. Miller 
118181aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
118281aded24SDavid S. Miller {
118381aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
118481aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
118581aded24SDavid S. Miller }
118681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
118781aded24SDavid S. Miller 
1188b55b76b2SDuan Jiong /* Handle redirects */
1189b55b76b2SDuan Jiong struct ip6rd_flowi {
1190b55b76b2SDuan Jiong 	struct flowi6 fl6;
1191b55b76b2SDuan Jiong 	struct in6_addr gateway;
1192b55b76b2SDuan Jiong };
1193b55b76b2SDuan Jiong 
1194b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
1195b55b76b2SDuan Jiong 					     struct fib6_table *table,
1196b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
1197b55b76b2SDuan Jiong 					     int flags)
1198b55b76b2SDuan Jiong {
1199b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1200b55b76b2SDuan Jiong 	struct rt6_info *rt;
1201b55b76b2SDuan Jiong 	struct fib6_node *fn;
1202b55b76b2SDuan Jiong 
1203b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
1204b55b76b2SDuan Jiong 	 * check if the redirect has come from approriate router.
1205b55b76b2SDuan Jiong 	 *
1206b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
1207b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
1208b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
1209b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
1210b55b76b2SDuan Jiong 	 * routes.
1211b55b76b2SDuan Jiong 	 */
1212b55b76b2SDuan Jiong 
1213b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
1214b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1215b55b76b2SDuan Jiong restart:
1216b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1217b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
1218b55b76b2SDuan Jiong 			continue;
1219b55b76b2SDuan Jiong 		if (rt->dst.error)
1220b55b76b2SDuan Jiong 			break;
1221b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1222b55b76b2SDuan Jiong 			continue;
1223b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1224b55b76b2SDuan Jiong 			continue;
1225b55b76b2SDuan Jiong 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1226b55b76b2SDuan Jiong 			continue;
1227b55b76b2SDuan Jiong 		break;
1228b55b76b2SDuan Jiong 	}
1229b55b76b2SDuan Jiong 
1230b55b76b2SDuan Jiong 	if (!rt)
1231b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1232b55b76b2SDuan Jiong 	else if (rt->dst.error) {
1233b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1234b55b76b2SDuan Jiong 		goto out;
1235b55b76b2SDuan Jiong 	}
1236b55b76b2SDuan Jiong 	BACKTRACK(net, &fl6->saddr);
1237b55b76b2SDuan Jiong out:
1238b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
1239b55b76b2SDuan Jiong 
1240b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
1241b55b76b2SDuan Jiong 
1242b55b76b2SDuan Jiong 	return rt;
1243b55b76b2SDuan Jiong };
1244b55b76b2SDuan Jiong 
1245b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
1246b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
1247b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
1248b55b76b2SDuan Jiong {
1249b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1250b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
1251b55b76b2SDuan Jiong 
1252b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
1253b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
1254b55b76b2SDuan Jiong 
1255b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
1256b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
1257b55b76b2SDuan Jiong }
1258b55b76b2SDuan Jiong 
12593a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
12603a5ad2eeSDavid S. Miller {
12613a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
12623a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12633a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12643a5ad2eeSDavid S. Miller 
12653a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
12663a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12673a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12683a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12693a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12706502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
12713a5ad2eeSDavid S. Miller 
1272b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
12736700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
12743a5ad2eeSDavid S. Miller 	dst_release(dst);
12753a5ad2eeSDavid S. Miller }
12763a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12773a5ad2eeSDavid S. Miller 
1278c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1279c92a59ecSDuan Jiong 			    u32 mark)
1280c92a59ecSDuan Jiong {
1281c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1282c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1283c92a59ecSDuan Jiong 	struct dst_entry *dst;
1284c92a59ecSDuan Jiong 	struct flowi6 fl6;
1285c92a59ecSDuan Jiong 
1286c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
1287c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
1288c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
1289c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
1290c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
1291c92a59ecSDuan Jiong 
1292b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1293c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
1294c92a59ecSDuan Jiong 	dst_release(dst);
1295c92a59ecSDuan Jiong }
1296c92a59ecSDuan Jiong 
12973a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
12983a5ad2eeSDavid S. Miller {
12993a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
13003a5ad2eeSDavid S. Miller }
13013a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
13023a5ad2eeSDavid S. Miller 
13030dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
13041da177e4SLinus Torvalds {
13050dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
13060dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
13070dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
13080dbaee3bSDavid S. Miller 
13091da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
13101da177e4SLinus Torvalds 
13115578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
13125578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	/*
13151da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
13161da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
13171da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
13181da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
13191da177e4SLinus Torvalds 	 */
13201da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
13211da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
13221da177e4SLinus Torvalds 	return mtu;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
1325ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1326d33e4553SDavid S. Miller {
1327d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1328618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1329618f9bc7SSteffen Klassert 
1330618f9bc7SSteffen Klassert 	if (mtu)
1331618f9bc7SSteffen Klassert 		return mtu;
1332618f9bc7SSteffen Klassert 
1333618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1334d33e4553SDavid S. Miller 
1335d33e4553SDavid S. Miller 	rcu_read_lock();
1336d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1337d33e4553SDavid S. Miller 	if (idev)
1338d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1339d33e4553SDavid S. Miller 	rcu_read_unlock();
1340d33e4553SDavid S. Miller 
1341d33e4553SDavid S. Miller 	return mtu;
1342d33e4553SDavid S. Miller }
1343d33e4553SDavid S. Miller 
13443b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
13453b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
13465d0bbeebSThomas Graf 
13473b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
134887a11578SDavid S. Miller 				  struct flowi6 *fl6)
13491da177e4SLinus Torvalds {
135087a11578SDavid S. Miller 	struct dst_entry *dst;
13511da177e4SLinus Torvalds 	struct rt6_info *rt;
13521da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1353c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
13541da177e4SLinus Torvalds 
135538308473SDavid S. Miller 	if (unlikely(!idev))
1356122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
13571da177e4SLinus Torvalds 
13588b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
135938308473SDavid S. Miller 	if (unlikely(!rt)) {
13601da177e4SLinus Torvalds 		in6_dev_put(idev);
136187a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
13621da177e4SLinus Torvalds 		goto out;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
13658e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
13668e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1367d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1368550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
136987a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13708e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13718e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
137214edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
13731da177e4SLinus Torvalds 
13743b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1375d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1376d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13773b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13781da177e4SLinus Torvalds 
13795578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13801da177e4SLinus Torvalds 
138187a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
138287a11578SDavid S. Miller 
13831da177e4SLinus Torvalds out:
138487a11578SDavid S. Miller 	return dst;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13873d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
13881da177e4SLinus Torvalds {
1389e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
13903d0f24a7SStephen Hemminger 	int more = 0;
13911da177e4SLinus Torvalds 
13923b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
13933b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
13945d0bbeebSThomas Graf 
13951da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
13961da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
13971da177e4SLinus Torvalds 			*pprev = dst->next;
13981da177e4SLinus Torvalds 			dst_free(dst);
13991da177e4SLinus Torvalds 		} else {
14001da177e4SLinus Torvalds 			pprev = &dst->next;
14013d0f24a7SStephen Hemminger 			++more;
14021da177e4SLinus Torvalds 		}
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14053b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
14065d0bbeebSThomas Graf 
14073d0f24a7SStephen Hemminger 	return more;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
14111e493d19SDavid S. Miller 			    void *arg)
14121e493d19SDavid S. Miller {
14131e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
14141e493d19SDavid S. Miller 
14151e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
14161e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
14171e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
14181e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
14191e493d19SDavid S. Miller 		if (func(rt, arg)) {
14201e493d19SDavid S. Miller 			*pprev = dst->next;
14211e493d19SDavid S. Miller 			dst_free(dst);
14221e493d19SDavid S. Miller 		} else {
14231e493d19SDavid S. Miller 			pprev = &dst->next;
14241e493d19SDavid S. Miller 		}
14251e493d19SDavid S. Miller 	}
14261e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
14271e493d19SDavid S. Miller }
14281e493d19SDavid S. Miller 
1429569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
14301da177e4SLinus Torvalds {
143186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
14327019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
14337019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
14347019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
14357019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
14367019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1437fc66f95cSEric Dumazet 	int entries;
14381da177e4SLinus Torvalds 
1439fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
144049a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
1441fc66f95cSEric Dumazet 	    entries <= rt_max_size)
14421da177e4SLinus Torvalds 		goto out;
14431da177e4SLinus Torvalds 
14446891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
14452ac3ac8fSMichal Kubeček 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, entries > rt_max_size);
1446fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1447fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
14487019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
14491da177e4SLinus Torvalds out:
14507019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1451fc66f95cSEric Dumazet 	return entries > rt_max_size;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds /*
14551da177e4SLinus Torvalds  *
14561da177e4SLinus Torvalds  */
14571da177e4SLinus Torvalds 
145886872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	int err;
14615578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
14621da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
14631da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14641da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1465c71099acSThomas Graf 	struct fib6_table *table;
14661da177e4SLinus Torvalds 	int addr_type;
14671da177e4SLinus Torvalds 
146886872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
14691da177e4SLinus Torvalds 		return -EINVAL;
14701da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
147186872cb5SThomas Graf 	if (cfg->fc_src_len)
14721da177e4SLinus Torvalds 		return -EINVAL;
14731da177e4SLinus Torvalds #endif
147486872cb5SThomas Graf 	if (cfg->fc_ifindex) {
14751da177e4SLinus Torvalds 		err = -ENODEV;
14765578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
14771da177e4SLinus Torvalds 		if (!dev)
14781da177e4SLinus Torvalds 			goto out;
14791da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
14801da177e4SLinus Torvalds 		if (!idev)
14811da177e4SLinus Torvalds 			goto out;
14821da177e4SLinus Torvalds 	}
14831da177e4SLinus Torvalds 
148486872cb5SThomas Graf 	if (cfg->fc_metric == 0)
148586872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
14861da177e4SLinus Torvalds 
1487c71099acSThomas Graf 	err = -ENOBUFS;
148838308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1489d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1490d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
149138308473SDavid S. Miller 		if (!table) {
1492f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1493d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1494d71314b4SMatti Vaittinen 		}
1495d71314b4SMatti Vaittinen 	} else {
1496d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1497d71314b4SMatti Vaittinen 	}
149838308473SDavid S. Miller 
149938308473SDavid S. Miller 	if (!table)
1500c71099acSThomas Graf 		goto out;
1501c71099acSThomas Graf 
15028b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
15031da177e4SLinus Torvalds 
150438308473SDavid S. Miller 	if (!rt) {
15051da177e4SLinus Torvalds 		err = -ENOMEM;
15061da177e4SLinus Torvalds 		goto out;
15071da177e4SLinus Torvalds 	}
15081da177e4SLinus Torvalds 
15091716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
15101716a961SGao feng 		rt6_set_expires(rt, jiffies +
15111716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
15121716a961SGao feng 	else
15131716a961SGao feng 		rt6_clean_expires(rt);
15141da177e4SLinus Torvalds 
151586872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
151686872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
151786872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
151886872cb5SThomas Graf 
151986872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1522d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1523ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1524ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
15251da177e4SLinus Torvalds 	else
1526d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
15271da177e4SLinus Torvalds 
1528d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
15291da177e4SLinus Torvalds 
153086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
153186872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
15321da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
153311d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
15341da177e4SLinus Torvalds 
15358e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
15368e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
15378e2ec639SYan, Zheng 		if (!metrics) {
15388e2ec639SYan, Zheng 			err = -ENOMEM;
15398e2ec639SYan, Zheng 			goto out;
15408e2ec639SYan, Zheng 		}
15418e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
15428e2ec639SYan, Zheng 	}
15431da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
154486872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
154586872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
15461da177e4SLinus Torvalds #endif
15471da177e4SLinus Torvalds 
154886872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
15511da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
15521da177e4SLinus Torvalds 	 */
155386872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
155438308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
155538308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
155638308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
15571da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
15585578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
15591da177e4SLinus Torvalds 			if (dev) {
15601da177e4SLinus Torvalds 				dev_put(dev);
15611da177e4SLinus Torvalds 				in6_dev_put(idev);
15621da177e4SLinus Torvalds 			}
15635578689aSDaniel Lezcano 			dev = net->loopback_dev;
15641da177e4SLinus Torvalds 			dev_hold(dev);
15651da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
15661da177e4SLinus Torvalds 			if (!idev) {
15671da177e4SLinus Torvalds 				err = -ENODEV;
15681da177e4SLinus Torvalds 				goto out;
15691da177e4SLinus Torvalds 			}
15701da177e4SLinus Torvalds 		}
15711da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1572ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1573ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1574ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
15757150aedeSKamala R 			rt->dst.output = dst_discard;
15767150aedeSKamala R 			rt->dst.input = dst_discard;
1577ef2c7d7bSNicolas Dichtel 			break;
1578ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1579ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
15807150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
15817150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
1582ef2c7d7bSNicolas Dichtel 			break;
1583b4949ab2SNicolas Dichtel 		case RTN_THROW:
1584ef2c7d7bSNicolas Dichtel 		default:
15857150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
15867150aedeSKamala R 					: -ENETUNREACH;
15877150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
15887150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
1589ef2c7d7bSNicolas Dichtel 			break;
1590ef2c7d7bSNicolas Dichtel 		}
15911da177e4SLinus Torvalds 		goto install_route;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
159486872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1595b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
15961da177e4SLinus Torvalds 		int gwa_type;
15971da177e4SLinus Torvalds 
159886872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
15994e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
16001da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
16031da177e4SLinus Torvalds 			struct rt6_info *grt;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
16061da177e4SLinus Torvalds 			   addresses as nexthop address.
16071da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
16081da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
16091da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
16101da177e4SLinus Torvalds 			   some exceptions. --ANK
16111da177e4SLinus Torvalds 			 */
16121da177e4SLinus Torvalds 			err = -EINVAL;
16131da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
16141da177e4SLinus Torvalds 				goto out;
16151da177e4SLinus Torvalds 
16165578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
161938308473SDavid S. Miller 			if (!grt)
16201da177e4SLinus Torvalds 				goto out;
16211da177e4SLinus Torvalds 			if (dev) {
1622d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
162394e187c0SAmerigo Wang 					ip6_rt_put(grt);
16241da177e4SLinus Torvalds 					goto out;
16251da177e4SLinus Torvalds 				}
16261da177e4SLinus Torvalds 			} else {
1627d1918542SDavid S. Miller 				dev = grt->dst.dev;
16281da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
16291da177e4SLinus Torvalds 				dev_hold(dev);
16301da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
16311da177e4SLinus Torvalds 			}
16321da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
16331da177e4SLinus Torvalds 				err = 0;
163494e187c0SAmerigo Wang 			ip6_rt_put(grt);
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 			if (err)
16371da177e4SLinus Torvalds 				goto out;
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 		err = -EINVAL;
164038308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
16411da177e4SLinus Torvalds 			goto out;
16421da177e4SLinus Torvalds 	}
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	err = -ENODEV;
164538308473SDavid S. Miller 	if (!dev)
16461da177e4SLinus Torvalds 		goto out;
16471da177e4SLinus Torvalds 
1648c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1649c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1650c3968a85SDaniel Walter 			err = -EINVAL;
1651c3968a85SDaniel Walter 			goto out;
1652c3968a85SDaniel Walter 		}
16534e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1654c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1655c3968a85SDaniel Walter 	} else
1656c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1657c3968a85SDaniel Walter 
165886872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds install_route:
166186872cb5SThomas Graf 	if (cfg->fc_mx) {
166286872cb5SThomas Graf 		struct nlattr *nla;
166386872cb5SThomas Graf 		int remaining;
16641da177e4SLinus Torvalds 
166586872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
16668f4c1f9bSThomas Graf 			int type = nla_type(nla);
166786872cb5SThomas Graf 
166886872cb5SThomas Graf 			if (type) {
166986872cb5SThomas Graf 				if (type > RTAX_MAX) {
16701da177e4SLinus Torvalds 					err = -EINVAL;
16711da177e4SLinus Torvalds 					goto out;
16721da177e4SLinus Torvalds 				}
167386872cb5SThomas Graf 
1674defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
16751da177e4SLinus Torvalds 			}
16761da177e4SLinus Torvalds 		}
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 
1679d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16801da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1681c71099acSThomas Graf 	rt->rt6i_table = table;
168263152fc0SDaniel Lezcano 
1683c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
168463152fc0SDaniel Lezcano 
168586872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds out:
16881da177e4SLinus Torvalds 	if (dev)
16891da177e4SLinus Torvalds 		dev_put(dev);
16901da177e4SLinus Torvalds 	if (idev)
16911da177e4SLinus Torvalds 		in6_dev_put(idev);
16921da177e4SLinus Torvalds 	if (rt)
1693d8d1f30bSChangli Gao 		dst_free(&rt->dst);
16941da177e4SLinus Torvalds 	return err;
16951da177e4SLinus Torvalds }
16961da177e4SLinus Torvalds 
169786872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
16981da177e4SLinus Torvalds {
16991da177e4SLinus Torvalds 	int err;
1700c71099acSThomas Graf 	struct fib6_table *table;
1701d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
17021da177e4SLinus Torvalds 
17036825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
17046825a26cSGao feng 		err = -ENOENT;
17056825a26cSGao feng 		goto out;
17066825a26cSGao feng 	}
17076c813a72SPatrick McHardy 
1708c71099acSThomas Graf 	table = rt->rt6i_table;
1709c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
171086872cb5SThomas Graf 	err = fib6_del(rt, info);
1711c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
17121da177e4SLinus Torvalds 
17136825a26cSGao feng out:
171494e187c0SAmerigo Wang 	ip6_rt_put(rt);
17151da177e4SLinus Torvalds 	return err;
17161da177e4SLinus Torvalds }
17171da177e4SLinus Torvalds 
1718e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1719e0a1ad73SThomas Graf {
17204d1169c1SDenis V. Lunev 	struct nl_info info = {
1721d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
17224d1169c1SDenis V. Lunev 	};
1723528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1724e0a1ad73SThomas Graf }
1725e0a1ad73SThomas Graf 
172686872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
17271da177e4SLinus Torvalds {
1728c71099acSThomas Graf 	struct fib6_table *table;
17291da177e4SLinus Torvalds 	struct fib6_node *fn;
17301da177e4SLinus Torvalds 	struct rt6_info *rt;
17311da177e4SLinus Torvalds 	int err = -ESRCH;
17321da177e4SLinus Torvalds 
17335578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
173438308473SDavid S. Miller 	if (!table)
1735c71099acSThomas Graf 		return err;
17361da177e4SLinus Torvalds 
1737c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1738c71099acSThomas Graf 
1739c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
174086872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
174186872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	if (fn) {
1744d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
174586872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1746d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1747d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17481da177e4SLinus Torvalds 				continue;
174986872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
175086872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17511da177e4SLinus Torvalds 				continue;
175286872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17531da177e4SLinus Torvalds 				continue;
1754d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1755c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
17561da177e4SLinus Torvalds 
175786872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
17581da177e4SLinus Torvalds 		}
17591da177e4SLinus Torvalds 	}
1760c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	return err;
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17656700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1766a6279458SYOSHIFUJI Hideaki {
1767e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1768a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1769e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1770e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1771e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1772e8599ff4SDavid S. Miller 	struct neighbour *neigh;
177371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
17746e157b6aSDavid S. Miller 	int optlen, on_link;
17756e157b6aSDavid S. Miller 	u8 *lladdr;
1776e8599ff4SDavid S. Miller 
177729a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
177871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1779e8599ff4SDavid S. Miller 
1780e8599ff4SDavid S. Miller 	if (optlen < 0) {
17816e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1782e8599ff4SDavid S. Miller 		return;
1783e8599ff4SDavid S. Miller 	}
1784e8599ff4SDavid S. Miller 
178571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1786e8599ff4SDavid S. Miller 
178771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
17886e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1789e8599ff4SDavid S. Miller 		return;
1790e8599ff4SDavid S. Miller 	}
1791e8599ff4SDavid S. Miller 
17926e157b6aSDavid S. Miller 	on_link = 0;
179371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1794e8599ff4SDavid S. Miller 		on_link = 1;
179571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1796e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
17976e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1798e8599ff4SDavid S. Miller 		return;
1799e8599ff4SDavid S. Miller 	}
1800e8599ff4SDavid S. Miller 
1801e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1802e8599ff4SDavid S. Miller 	if (!in6_dev)
1803e8599ff4SDavid S. Miller 		return;
1804e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1805e8599ff4SDavid S. Miller 		return;
1806e8599ff4SDavid S. Miller 
1807e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1808e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1809e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1810e8599ff4SDavid S. Miller 	 */
1811e8599ff4SDavid S. Miller 
181271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1813e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1814e8599ff4SDavid S. Miller 		return;
1815e8599ff4SDavid S. Miller 	}
18166e157b6aSDavid S. Miller 
18176e157b6aSDavid S. Miller 	lladdr = NULL;
1818e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1819e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1820e8599ff4SDavid S. Miller 					     skb->dev);
1821e8599ff4SDavid S. Miller 		if (!lladdr) {
1822e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1823e8599ff4SDavid S. Miller 			return;
1824e8599ff4SDavid S. Miller 		}
1825e8599ff4SDavid S. Miller 	}
1826e8599ff4SDavid S. Miller 
18276e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
18286e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
18296e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
18306e157b6aSDavid S. Miller 		return;
18316e157b6aSDavid S. Miller 	}
18326e157b6aSDavid S. Miller 
18336e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18346e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18356e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18366e157b6aSDavid S. Miller 	 */
18376e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18386e157b6aSDavid S. Miller 
183971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1840e8599ff4SDavid S. Miller 	if (!neigh)
1841e8599ff4SDavid S. Miller 		return;
1842e8599ff4SDavid S. Miller 
18431da177e4SLinus Torvalds 	/*
18441da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18451da177e4SLinus Torvalds 	 */
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18481da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18491da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18501da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18511da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18521da177e4SLinus Torvalds 		     );
18531da177e4SLinus Torvalds 
185471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
185538308473SDavid S. Miller 	if (!nrt)
18561da177e4SLinus Torvalds 		goto out;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18591da177e4SLinus Torvalds 	if (on_link)
18601da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18611da177e4SLinus Torvalds 
18624e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
18631da177e4SLinus Torvalds 
186440e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18651da177e4SLinus Torvalds 		goto out;
18661da177e4SLinus Torvalds 
1867d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1868d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
186971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
187060592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
18718d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18728d71740cSTom Tucker 
18731da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18746e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1875e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18761da177e4SLinus Torvalds 	}
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds out:
1879e8599ff4SDavid S. Miller 	neigh_release(neigh);
18806e157b6aSDavid S. Miller }
18816e157b6aSDavid S. Miller 
18821da177e4SLinus Torvalds /*
18831da177e4SLinus Torvalds  *	Misc support functions
18841da177e4SLinus Torvalds  */
18851da177e4SLinus Torvalds 
18861716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
188721efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18881da177e4SLinus Torvalds {
1889d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18908b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18918b96d22dSDavid S. Miller 					    ort->rt6i_table);
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	if (rt) {
1894d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1895d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18968e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18971da177e4SLinus Torvalds 
18984e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18998e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1900defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1901d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
19021da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
19031da177e4SLinus Torvalds 		if (rt->rt6i_idev)
19041da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1905d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
19061da177e4SLinus Torvalds 
1907550bab42SJulian Anastasov 		if (ort->rt6i_flags & RTF_GATEWAY)
19084e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = ort->rt6i_gateway;
1909550bab42SJulian Anastasov 		else
1910550bab42SJulian Anastasov 			rt->rt6i_gateway = *dest;
19111716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
19121716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
19131716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
19141716a961SGao feng 			rt6_set_from(rt, ort);
19151da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
19181da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
19191da177e4SLinus Torvalds #endif
19200f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1921c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 	return rt;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
192670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1927efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1928b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1929b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
193070ceb4f5SYOSHIFUJI Hideaki {
193170ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
193270ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1933c71099acSThomas Graf 	struct fib6_table *table;
193470ceb4f5SYOSHIFUJI Hideaki 
1935efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
193638308473SDavid S. Miller 	if (!table)
1937c71099acSThomas Graf 		return NULL;
1938c71099acSThomas Graf 
19395744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1940c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
194170ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
194270ceb4f5SYOSHIFUJI Hideaki 		goto out;
194370ceb4f5SYOSHIFUJI Hideaki 
1944d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1945d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
194670ceb4f5SYOSHIFUJI Hideaki 			continue;
194770ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
194870ceb4f5SYOSHIFUJI Hideaki 			continue;
194970ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
195070ceb4f5SYOSHIFUJI Hideaki 			continue;
1951d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
195270ceb4f5SYOSHIFUJI Hideaki 		break;
195370ceb4f5SYOSHIFUJI Hideaki 	}
195470ceb4f5SYOSHIFUJI Hideaki out:
19555744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
195670ceb4f5SYOSHIFUJI Hideaki 	return rt;
195770ceb4f5SYOSHIFUJI Hideaki }
195870ceb4f5SYOSHIFUJI Hideaki 
1959efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1960b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1961b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
196295c96174SEric Dumazet 					   unsigned int pref)
196370ceb4f5SYOSHIFUJI Hideaki {
196486872cb5SThomas Graf 	struct fib6_config cfg = {
196586872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1966238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
196786872cb5SThomas Graf 		.fc_ifindex	= ifindex,
196886872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
196986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
197086872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
197115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1972efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1973efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
197486872cb5SThomas Graf 	};
197570ceb4f5SYOSHIFUJI Hideaki 
19764e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19774e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
197886872cb5SThomas Graf 
1979e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1980e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
198186872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
198270ceb4f5SYOSHIFUJI Hideaki 
198386872cb5SThomas Graf 	ip6_route_add(&cfg);
198470ceb4f5SYOSHIFUJI Hideaki 
1985efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
198670ceb4f5SYOSHIFUJI Hideaki }
198770ceb4f5SYOSHIFUJI Hideaki #endif
198870ceb4f5SYOSHIFUJI Hideaki 
1989b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	struct rt6_info *rt;
1992c71099acSThomas Graf 	struct fib6_table *table;
19931da177e4SLinus Torvalds 
1994c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
199538308473SDavid S. Miller 	if (!table)
1996c71099acSThomas Graf 		return NULL;
19971da177e4SLinus Torvalds 
19985744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1999d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
2000d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
2001045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
20021da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
20031da177e4SLinus Torvalds 			break;
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 	if (rt)
2006d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
20075744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
20081da177e4SLinus Torvalds 	return rt;
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
2011b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
2012ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
2013ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
20141da177e4SLinus Torvalds {
201586872cb5SThomas Graf 	struct fib6_config cfg = {
201686872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
2017238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
201886872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
201986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
202086872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
202115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
20225578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2023c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
202486872cb5SThomas Graf 	};
20251da177e4SLinus Torvalds 
20264e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
20271da177e4SLinus Torvalds 
202886872cb5SThomas Graf 	ip6_route_add(&cfg);
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
20311da177e4SLinus Torvalds }
20321da177e4SLinus Torvalds 
20337b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	struct rt6_info *rt;
2036c71099acSThomas Graf 	struct fib6_table *table;
2037c71099acSThomas Graf 
2038c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20397b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
204038308473SDavid S. Miller 	if (!table)
2041c71099acSThomas Graf 		return;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds restart:
2044c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2045d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20463e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
20473e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
2048d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2049c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2050e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20511da177e4SLinus Torvalds 			goto restart;
20521da177e4SLinus Torvalds 		}
20531da177e4SLinus Torvalds 	}
2054c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
20575578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20585578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
205986872cb5SThomas Graf 				 struct fib6_config *cfg)
206086872cb5SThomas Graf {
206186872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
206286872cb5SThomas Graf 
206386872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
206486872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
206586872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
206686872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
206786872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
206886872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
206986872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
207086872cb5SThomas Graf 
20715578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2072f1243c2dSBenjamin Thery 
20734e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20744e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20754e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
207686872cb5SThomas Graf }
207786872cb5SThomas Graf 
20785578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20791da177e4SLinus Torvalds {
208086872cb5SThomas Graf 	struct fib6_config cfg;
20811da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20821da177e4SLinus Torvalds 	int err;
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	switch(cmd) {
20851da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20861da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2087af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
20881da177e4SLinus Torvalds 			return -EPERM;
20891da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20901da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20911da177e4SLinus Torvalds 		if (err)
20921da177e4SLinus Torvalds 			return -EFAULT;
20931da177e4SLinus Torvalds 
20945578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
209586872cb5SThomas Graf 
20961da177e4SLinus Torvalds 		rtnl_lock();
20971da177e4SLinus Torvalds 		switch (cmd) {
20981da177e4SLinus Torvalds 		case SIOCADDRT:
209986872cb5SThomas Graf 			err = ip6_route_add(&cfg);
21001da177e4SLinus Torvalds 			break;
21011da177e4SLinus Torvalds 		case SIOCDELRT:
210286872cb5SThomas Graf 			err = ip6_route_del(&cfg);
21031da177e4SLinus Torvalds 			break;
21041da177e4SLinus Torvalds 		default:
21051da177e4SLinus Torvalds 			err = -EINVAL;
21061da177e4SLinus Torvalds 		}
21071da177e4SLinus Torvalds 		rtnl_unlock();
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 		return err;
21103ff50b79SStephen Hemminger 	}
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	return -EINVAL;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds /*
21161da177e4SLinus Torvalds  *	Drop the packet on the floor
21171da177e4SLinus Torvalds  */
21181da177e4SLinus Torvalds 
2119d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
21201da177e4SLinus Torvalds {
2121612f09e8SYOSHIFUJI Hideaki 	int type;
2122adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2123612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2124612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
21250660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
212645bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
21273bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21283bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2129612f09e8SYOSHIFUJI Hideaki 			break;
2130612f09e8SYOSHIFUJI Hideaki 		}
2131612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2132612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
21333bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21343bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2135612f09e8SYOSHIFUJI Hideaki 		break;
2136612f09e8SYOSHIFUJI Hideaki 	}
21373ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21381da177e4SLinus Torvalds 	kfree_skb(skb);
21391da177e4SLinus Torvalds 	return 0;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21429ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21439ce8ade0SThomas Graf {
2144612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21459ce8ade0SThomas Graf }
21469ce8ade0SThomas Graf 
214720380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
21481da177e4SLinus Torvalds {
2149adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2150612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
21539ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21549ce8ade0SThomas Graf {
2155612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21569ce8ade0SThomas Graf }
21579ce8ade0SThomas Graf 
21589ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21599ce8ade0SThomas Graf {
2160adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2161612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21629ce8ade0SThomas Graf }
21639ce8ade0SThomas Graf 
21641da177e4SLinus Torvalds /*
21651da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21661da177e4SLinus Torvalds  */
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21691da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21708f031519SDavid S. Miller 				    bool anycast)
21711da177e4SLinus Torvalds {
2172c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21738b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
21741da177e4SLinus Torvalds 
217538308473SDavid S. Miller 	if (!rt) {
2176f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21771da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
217840385653SBen Greear 	}
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	in6_dev_hold(idev);
21811da177e4SLinus Torvalds 
218211d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2183d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2184d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21851da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
218858c4fb86SYOSHIFUJI Hideaki 	if (anycast)
218958c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
219058c4fb86SYOSHIFUJI Hideaki 	else
21911da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21921da177e4SLinus Torvalds 
2193550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
21944e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21951da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21965578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21971da177e4SLinus Torvalds 
2198d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	return rt;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
2203c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2204c3968a85SDaniel Walter 			struct rt6_info *rt,
2205b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2206c3968a85SDaniel Walter 			unsigned int prefs,
2207c3968a85SDaniel Walter 			struct in6_addr *saddr)
2208c3968a85SDaniel Walter {
2209c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2210c3968a85SDaniel Walter 	int err = 0;
2211c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
22124e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2213c3968a85SDaniel Walter 	else
2214c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2215c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2216c3968a85SDaniel Walter 	return err;
2217c3968a85SDaniel Walter }
2218c3968a85SDaniel Walter 
2219c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2220c3968a85SDaniel Walter struct arg_dev_net_ip {
2221c3968a85SDaniel Walter 	struct net_device *dev;
2222c3968a85SDaniel Walter 	struct net *net;
2223c3968a85SDaniel Walter 	struct in6_addr *addr;
2224c3968a85SDaniel Walter };
2225c3968a85SDaniel Walter 
2226c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2227c3968a85SDaniel Walter {
2228c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2229c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2230c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2231c3968a85SDaniel Walter 
2232d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2233c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2234c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2235c3968a85SDaniel Walter 		/* remove prefsrc entry */
2236c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2237c3968a85SDaniel Walter 	}
2238c3968a85SDaniel Walter 	return 0;
2239c3968a85SDaniel Walter }
2240c3968a85SDaniel Walter 
2241c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2242c3968a85SDaniel Walter {
2243c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2244c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2245c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2246c3968a85SDaniel Walter 		.net = net,
2247c3968a85SDaniel Walter 		.addr = &ifp->addr,
2248c3968a85SDaniel Walter 	};
2249c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2250c3968a85SDaniel Walter }
2251c3968a85SDaniel Walter 
22528ed67789SDaniel Lezcano struct arg_dev_net {
22538ed67789SDaniel Lezcano 	struct net_device *dev;
22548ed67789SDaniel Lezcano 	struct net *net;
22558ed67789SDaniel Lezcano };
22568ed67789SDaniel Lezcano 
22571da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22581da177e4SLinus Torvalds {
2259bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2260bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22618ed67789SDaniel Lezcano 
2262d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2263c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22641da177e4SLinus Torvalds 		return -1;
2265c159d30cSDavid S. Miller 
22661da177e4SLinus Torvalds 	return 0;
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds 
2269f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22701da177e4SLinus Torvalds {
22718ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22728ed67789SDaniel Lezcano 		.dev = dev,
22738ed67789SDaniel Lezcano 		.net = net,
22748ed67789SDaniel Lezcano 	};
22758ed67789SDaniel Lezcano 
22768ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22771e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
228095c96174SEric Dumazet struct rt6_mtu_change_arg {
22811da177e4SLinus Torvalds 	struct net_device *dev;
228295c96174SEric Dumazet 	unsigned int mtu;
22831da177e4SLinus Torvalds };
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22861da177e4SLinus Torvalds {
22871da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22881da177e4SLinus Torvalds 	struct inet6_dev *idev;
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22911da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22921da177e4SLinus Torvalds 	   We still use this lock to block changes
22931da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22941da177e4SLinus Torvalds 	*/
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
229738308473SDavid S. Miller 	if (!idev)
22981da177e4SLinus Torvalds 		return 0;
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
23011da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
23021da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
23031da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
23041da177e4SLinus Torvalds 	 */
23051da177e4SLinus Torvalds 	/*
23061da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
23071da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
23081da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
23091da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
23101da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
23111da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
23121da177e4SLinus Torvalds 	   PMTU discouvery.
23131da177e4SLinus Torvalds 	 */
2314d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2315d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2316d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2317d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2318d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2319defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2320566cfd8fSSimon Arlott 	}
23211da177e4SLinus Torvalds 	return 0;
23221da177e4SLinus Torvalds }
23231da177e4SLinus Torvalds 
232495c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
23251da177e4SLinus Torvalds {
2326c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2327c71099acSThomas Graf 		.dev = dev,
2328c71099acSThomas Graf 		.mtu = mtu,
2329c71099acSThomas Graf 	};
23301da177e4SLinus Torvalds 
2331c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
2334ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23355176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
233686872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2337ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
233886872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
233986872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
234051ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
234186872cb5SThomas Graf };
234286872cb5SThomas Graf 
234386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
234486872cb5SThomas Graf 			      struct fib6_config *cfg)
23451da177e4SLinus Torvalds {
234686872cb5SThomas Graf 	struct rtmsg *rtm;
234786872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
234886872cb5SThomas Graf 	int err;
23491da177e4SLinus Torvalds 
235086872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
235186872cb5SThomas Graf 	if (err < 0)
235286872cb5SThomas Graf 		goto errout;
23531da177e4SLinus Torvalds 
235486872cb5SThomas Graf 	err = -EINVAL;
235586872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
235686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
235786872cb5SThomas Graf 
235886872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
235986872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
236086872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
236186872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
236286872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2363ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
236486872cb5SThomas Graf 
2365ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2366ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2367b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2368b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
236986872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
237086872cb5SThomas Graf 
2371ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2372ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2373ab79ad14SMaciej Żenczykowski 
237415e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
237586872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23763b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
237786872cb5SThomas Graf 
237886872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
237986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
238086872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23811da177e4SLinus Torvalds 	}
238286872cb5SThomas Graf 
238386872cb5SThomas Graf 	if (tb[RTA_DST]) {
238486872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
238586872cb5SThomas Graf 
238686872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
238786872cb5SThomas Graf 			goto errout;
238886872cb5SThomas Graf 
238986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23901da177e4SLinus Torvalds 	}
239186872cb5SThomas Graf 
239286872cb5SThomas Graf 	if (tb[RTA_SRC]) {
239386872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
239486872cb5SThomas Graf 
239586872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
239686872cb5SThomas Graf 			goto errout;
239786872cb5SThomas Graf 
239886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23991da177e4SLinus Torvalds 	}
240086872cb5SThomas Graf 
2401c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2402c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2403c3968a85SDaniel Walter 
240486872cb5SThomas Graf 	if (tb[RTA_OIF])
240586872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
240686872cb5SThomas Graf 
240786872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
240886872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
240986872cb5SThomas Graf 
241086872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
241186872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
241286872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
24131da177e4SLinus Torvalds 	}
241486872cb5SThomas Graf 
241586872cb5SThomas Graf 	if (tb[RTA_TABLE])
241686872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
241786872cb5SThomas Graf 
241851ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
241951ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
242051ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
242151ebd318SNicolas Dichtel 	}
242251ebd318SNicolas Dichtel 
242386872cb5SThomas Graf 	err = 0;
242486872cb5SThomas Graf errout:
242586872cb5SThomas Graf 	return err;
24261da177e4SLinus Torvalds }
24271da177e4SLinus Torvalds 
242851ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
242951ebd318SNicolas Dichtel {
243051ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
243151ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
243251ebd318SNicolas Dichtel 	int remaining;
243351ebd318SNicolas Dichtel 	int attrlen;
243451ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
243551ebd318SNicolas Dichtel 
243651ebd318SNicolas Dichtel beginning:
243751ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
243851ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
243951ebd318SNicolas Dichtel 
244051ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
244151ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
244251ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
244351ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
244451ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
244551ebd318SNicolas Dichtel 
244651ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
244751ebd318SNicolas Dichtel 		if (attrlen > 0) {
244851ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
244951ebd318SNicolas Dichtel 
245051ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
245151ebd318SNicolas Dichtel 			if (nla) {
245251ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
245351ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
245451ebd318SNicolas Dichtel 			}
245551ebd318SNicolas Dichtel 		}
245651ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
245751ebd318SNicolas Dichtel 		if (err) {
245851ebd318SNicolas Dichtel 			last_err = err;
245951ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
246051ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
246151ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
246251ebd318SNicolas Dichtel 			 */
246351ebd318SNicolas Dichtel 			if (add) {
246451ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
246551ebd318SNicolas Dichtel 				 * next hops that have been already added.
246651ebd318SNicolas Dichtel 				 */
246751ebd318SNicolas Dichtel 				add = 0;
246851ebd318SNicolas Dichtel 				goto beginning;
246951ebd318SNicolas Dichtel 			}
247051ebd318SNicolas Dichtel 		}
24711a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
24721a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
24731a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
24741a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
24751a72418bSNicolas Dichtel 		 */
24761a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
247751ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
247851ebd318SNicolas Dichtel 	}
247951ebd318SNicolas Dichtel 
248051ebd318SNicolas Dichtel 	return last_err;
248151ebd318SNicolas Dichtel }
248251ebd318SNicolas Dichtel 
2483661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24841da177e4SLinus Torvalds {
248586872cb5SThomas Graf 	struct fib6_config cfg;
248686872cb5SThomas Graf 	int err;
24871da177e4SLinus Torvalds 
248886872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
248986872cb5SThomas Graf 	if (err < 0)
249086872cb5SThomas Graf 		return err;
249186872cb5SThomas Graf 
249251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
249351ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
249451ebd318SNicolas Dichtel 	else
249586872cb5SThomas Graf 		return ip6_route_del(&cfg);
24961da177e4SLinus Torvalds }
24971da177e4SLinus Torvalds 
2498661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24991da177e4SLinus Torvalds {
250086872cb5SThomas Graf 	struct fib6_config cfg;
250186872cb5SThomas Graf 	int err;
25021da177e4SLinus Torvalds 
250386872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
250486872cb5SThomas Graf 	if (err < 0)
250586872cb5SThomas Graf 		return err;
250686872cb5SThomas Graf 
250751ebd318SNicolas Dichtel 	if (cfg.fc_mp)
250851ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
250951ebd318SNicolas Dichtel 	else
251086872cb5SThomas Graf 		return ip6_route_add(&cfg);
25111da177e4SLinus Torvalds }
25121da177e4SLinus Torvalds 
2513339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2514339bf98fSThomas Graf {
2515339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2516339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2517339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2518339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2519339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2520339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2521339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2522339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2523339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
25246a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2525339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2526339bf98fSThomas Graf }
2527339bf98fSThomas Graf 
2528191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2529191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
25300d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
253115e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
25327bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
25331da177e4SLinus Torvalds {
25341da177e4SLinus Torvalds 	struct rtmsg *rtm;
25351da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2536e3703b3dSThomas Graf 	long expires;
25379e762a4aSPatrick McHardy 	u32 table;
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
25401da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
25411da177e4SLinus Torvalds 			/* success since this is not a prefix route */
25421da177e4SLinus Torvalds 			return 1;
25431da177e4SLinus Torvalds 		}
25441da177e4SLinus Torvalds 	}
25451da177e4SLinus Torvalds 
254615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
254738308473SDavid S. Miller 	if (!nlh)
254826932566SPatrick McHardy 		return -EMSGSIZE;
25492d7202bfSThomas Graf 
25502d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
25511da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
25521da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
25531da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
25541da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2555c71099acSThomas Graf 	if (rt->rt6i_table)
25569e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2557c71099acSThomas Graf 	else
25589e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
25599e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2560c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2561c78679e8SDavid S. Miller 		goto nla_put_failure;
2562ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2563ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2564ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2565ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2566ef2c7d7bSNicolas Dichtel 			break;
2567ef2c7d7bSNicolas Dichtel 		case -EACCES:
2568ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2569ef2c7d7bSNicolas Dichtel 			break;
2570b4949ab2SNicolas Dichtel 		case -EAGAIN:
2571b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2572b4949ab2SNicolas Dichtel 			break;
2573ef2c7d7bSNicolas Dichtel 		default:
25741da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2575ef2c7d7bSNicolas Dichtel 			break;
2576ef2c7d7bSNicolas Dichtel 		}
2577ef2c7d7bSNicolas Dichtel 	}
2578ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2579ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2580d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
25811da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
25821da177e4SLinus Torvalds 	else
25831da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
25841da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
25851da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
25861da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
25871da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
25881da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2589f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2590f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
25911da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2592f0396f60SDenis Ovsienko 		else
2593f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2594f0396f60SDenis Ovsienko 	}
25951da177e4SLinus Torvalds 
25961da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
25971da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	if (dst) {
2600c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2601c78679e8SDavid S. Miller 			goto nla_put_failure;
26021da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
26031da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2604c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2605c78679e8SDavid S. Miller 			goto nla_put_failure;
26061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26071da177e4SLinus Torvalds 	if (src) {
2608c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2609c78679e8SDavid S. Miller 			goto nla_put_failure;
26101da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2611c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2612c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2613c78679e8SDavid S. Miller 		goto nla_put_failure;
26141da177e4SLinus Torvalds #endif
26157bc570c8SYOSHIFUJI Hideaki 	if (iif) {
26167bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
26177bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
26188229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
26197bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
26207bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
26217bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
26227bc570c8SYOSHIFUJI Hideaki 						return 0;
26237bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
26247bc570c8SYOSHIFUJI Hideaki 				} else {
26257bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
26267bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
26277bc570c8SYOSHIFUJI Hideaki 				}
26287bc570c8SYOSHIFUJI Hideaki 			}
26297bc570c8SYOSHIFUJI Hideaki 		} else
26307bc570c8SYOSHIFUJI Hideaki #endif
2631c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2632c78679e8SDavid S. Miller 				goto nla_put_failure;
26337bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
26341da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2635c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2636c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2637c78679e8SDavid S. Miller 			goto nla_put_failure;
2638c3968a85SDaniel Walter 	}
2639c3968a85SDaniel Walter 
2640c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2641c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
26424e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2643c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2644c78679e8SDavid S. Miller 			goto nla_put_failure;
26451da177e4SLinus Torvalds 	}
26462d7202bfSThomas Graf 
2647defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
26482d7202bfSThomas Graf 		goto nla_put_failure;
26492d7202bfSThomas Graf 
2650dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2651dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
265294f826b8SEric Dumazet 			goto nla_put_failure;
265394f826b8SEric Dumazet 	}
26542d7202bfSThomas Graf 
2655c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2656c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2657c78679e8SDavid S. Miller 		goto nla_put_failure;
2658c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2659c78679e8SDavid S. Miller 		goto nla_put_failure;
26608253947eSLi Wei 
26618253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
266269cdf8f9SYOSHIFUJI Hideaki 
266387a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2664e3703b3dSThomas Graf 		goto nla_put_failure;
26651da177e4SLinus Torvalds 
26662d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
26672d7202bfSThomas Graf 
26682d7202bfSThomas Graf nla_put_failure:
266926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
267026932566SPatrick McHardy 	return -EMSGSIZE;
26711da177e4SLinus Torvalds }
26721da177e4SLinus Torvalds 
26731b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
26741da177e4SLinus Torvalds {
26751da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
26761da177e4SLinus Torvalds 	int prefix;
26771da177e4SLinus Torvalds 
26782d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
26792d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
26801da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
26811da177e4SLinus Torvalds 	} else
26821da177e4SLinus Torvalds 		prefix = 0;
26831da177e4SLinus Torvalds 
2684191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2685191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
268615e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
26877bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
2690661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
26911da177e4SLinus Torvalds {
26923b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2693ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26941da177e4SLinus Torvalds 	struct rt6_info *rt;
2695ab364a6fSThomas Graf 	struct sk_buff *skb;
2696ab364a6fSThomas Graf 	struct rtmsg *rtm;
26974c9483b2SDavid S. Miller 	struct flowi6 fl6;
269872331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2699ab364a6fSThomas Graf 
2700ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2701ab364a6fSThomas Graf 	if (err < 0)
2702ab364a6fSThomas Graf 		goto errout;
2703ab364a6fSThomas Graf 
2704ab364a6fSThomas Graf 	err = -EINVAL;
27054c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2706ab364a6fSThomas Graf 
2707ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2708ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2709ab364a6fSThomas Graf 			goto errout;
2710ab364a6fSThomas Graf 
27114e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2712ab364a6fSThomas Graf 	}
2713ab364a6fSThomas Graf 
2714ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2715ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2716ab364a6fSThomas Graf 			goto errout;
2717ab364a6fSThomas Graf 
27184e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2719ab364a6fSThomas Graf 	}
2720ab364a6fSThomas Graf 
2721ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2722ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2723ab364a6fSThomas Graf 
2724ab364a6fSThomas Graf 	if (tb[RTA_OIF])
272572331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2726ab364a6fSThomas Graf 
2727ab364a6fSThomas Graf 	if (iif) {
2728ab364a6fSThomas Graf 		struct net_device *dev;
272972331bc0SShmulik Ladkani 		int flags = 0;
273072331bc0SShmulik Ladkani 
27315578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2732ab364a6fSThomas Graf 		if (!dev) {
2733ab364a6fSThomas Graf 			err = -ENODEV;
2734ab364a6fSThomas Graf 			goto errout;
2735ab364a6fSThomas Graf 		}
273672331bc0SShmulik Ladkani 
273772331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
273872331bc0SShmulik Ladkani 
273972331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
274072331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
274172331bc0SShmulik Ladkani 
274272331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
274372331bc0SShmulik Ladkani 							       flags);
274472331bc0SShmulik Ladkani 	} else {
274572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
274672331bc0SShmulik Ladkani 
274772331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2748ab364a6fSThomas Graf 	}
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
275138308473SDavid S. Miller 	if (!skb) {
275294e187c0SAmerigo Wang 		ip6_rt_put(rt);
2753ab364a6fSThomas Graf 		err = -ENOBUFS;
2754ab364a6fSThomas Graf 		goto errout;
2755ab364a6fSThomas Graf 	}
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27581da177e4SLinus Torvalds 	   through good chunk of routing engine.
27591da177e4SLinus Torvalds 	 */
2760459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
27611da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
27621da177e4SLinus Torvalds 
2763d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
27641da177e4SLinus Torvalds 
27654c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
276615e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
27677bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
27681da177e4SLinus Torvalds 	if (err < 0) {
2769ab364a6fSThomas Graf 		kfree_skb(skb);
2770ab364a6fSThomas Graf 		goto errout;
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 
277315e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2774ab364a6fSThomas Graf errout:
27751da177e4SLinus Torvalds 	return err;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
277886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
27791da177e4SLinus Torvalds {
27801da177e4SLinus Torvalds 	struct sk_buff *skb;
27815578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2782528c4cebSDenis V. Lunev 	u32 seq;
2783528c4cebSDenis V. Lunev 	int err;
27840d51aa80SJamal Hadi Salim 
2785528c4cebSDenis V. Lunev 	err = -ENOBUFS;
278638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
278786872cb5SThomas Graf 
2788339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
278938308473SDavid S. Miller 	if (!skb)
279021713ebcSThomas Graf 		goto errout;
27911da177e4SLinus Torvalds 
2792191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
279315e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
279426932566SPatrick McHardy 	if (err < 0) {
279526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
279626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
279726932566SPatrick McHardy 		kfree_skb(skb);
279826932566SPatrick McHardy 		goto errout;
279926932566SPatrick McHardy 	}
280015e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
28015578689aSDaniel Lezcano 		    info->nlh, gfp_any());
28021ce85fe4SPablo Neira Ayuso 	return;
280321713ebcSThomas Graf errout:
280421713ebcSThomas Graf 	if (err < 0)
28055578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28088ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2809351638e7SJiri Pirko 				unsigned long event, void *ptr)
28108ed67789SDaniel Lezcano {
2811351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2812c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
28138ed67789SDaniel Lezcano 
28148ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2815d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
28168ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
28178ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2818d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
28198ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2820d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
28218ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
28228ed67789SDaniel Lezcano #endif
28238ed67789SDaniel Lezcano 	}
28248ed67789SDaniel Lezcano 
28258ed67789SDaniel Lezcano 	return NOTIFY_OK;
28268ed67789SDaniel Lezcano }
28278ed67789SDaniel Lezcano 
28281da177e4SLinus Torvalds /*
28291da177e4SLinus Torvalds  *	/proc
28301da177e4SLinus Torvalds  */
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28331da177e4SLinus Torvalds 
283433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
283533120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
283633120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
283733120b30SAlexey Dobriyan 	.read		= seq_read,
283833120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
28398d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
284033120b30SAlexey Dobriyan };
284133120b30SAlexey Dobriyan 
28421da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
28431da177e4SLinus Torvalds {
284469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
28451da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
284669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
284769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
284869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
284969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
285069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2851fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
285269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 	return 0;
28551da177e4SLinus Torvalds }
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28581da177e4SLinus Torvalds {
2859de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
286069ddb805SDaniel Lezcano }
286169ddb805SDaniel Lezcano 
28629a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28631da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28641da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28651da177e4SLinus Torvalds 	.read	 = seq_read,
28661da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2867b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28681da177e4SLinus Torvalds };
28691da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds static
2874fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
28751da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28761da177e4SLinus Torvalds {
2877c486da34SLucian Adrian Grijincu 	struct net *net;
2878c486da34SLucian Adrian Grijincu 	int delay;
2879c486da34SLucian Adrian Grijincu 	if (!write)
2880c486da34SLucian Adrian Grijincu 		return -EINVAL;
2881c486da34SLucian Adrian Grijincu 
2882c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2883c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28848d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28852ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
28861da177e4SLinus Torvalds 	return 0;
28871da177e4SLinus Torvalds }
28881da177e4SLinus Torvalds 
2889fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
28901da177e4SLinus Torvalds 	{
28911da177e4SLinus Torvalds 		.procname	=	"flush",
28924990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28931da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
289489c8b3a1SDave Jones 		.mode		=	0200,
28956d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28961da177e4SLinus Torvalds 	},
28971da177e4SLinus Torvalds 	{
28981da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28999a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
29001da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29011da177e4SLinus Torvalds 		.mode		=	0644,
29026d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29031da177e4SLinus Torvalds 	},
29041da177e4SLinus Torvalds 	{
29051da177e4SLinus Torvalds 		.procname	=	"max_size",
29064990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
29071da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29081da177e4SLinus Torvalds 		.mode		=	0644,
29096d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29101da177e4SLinus Torvalds 	},
29111da177e4SLinus Torvalds 	{
29121da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
29134990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29141da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29151da177e4SLinus Torvalds 		.mode		=	0644,
29166d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29171da177e4SLinus Torvalds 	},
29181da177e4SLinus Torvalds 	{
29191da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
29204990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
29211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29221da177e4SLinus Torvalds 		.mode		=	0644,
29236d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29241da177e4SLinus Torvalds 	},
29251da177e4SLinus Torvalds 	{
29261da177e4SLinus Torvalds 		.procname	=	"gc_interval",
29274990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
29281da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29291da177e4SLinus Torvalds 		.mode		=	0644,
29306d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29311da177e4SLinus Torvalds 	},
29321da177e4SLinus Torvalds 	{
29331da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
29344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
29351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29361da177e4SLinus Torvalds 		.mode		=	0644,
2937f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29381da177e4SLinus Torvalds 	},
29391da177e4SLinus Torvalds 	{
29401da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
29414990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
29421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29431da177e4SLinus Torvalds 		.mode		=	0644,
29446d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29451da177e4SLinus Torvalds 	},
29461da177e4SLinus Torvalds 	{
29471da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
29484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
29491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29501da177e4SLinus Torvalds 		.mode		=	0644,
2951f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29521da177e4SLinus Torvalds 	},
29531da177e4SLinus Torvalds 	{
29541da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29554990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29561da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29571da177e4SLinus Torvalds 		.mode		=	0644,
29586d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29591da177e4SLinus Torvalds 	},
2960f8572d8fSEric W. Biederman 	{ }
29611da177e4SLinus Torvalds };
29621da177e4SLinus Torvalds 
29632c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2964760f2d01SDaniel Lezcano {
2965760f2d01SDaniel Lezcano 	struct ctl_table *table;
2966760f2d01SDaniel Lezcano 
2967760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2968760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2969760f2d01SDaniel Lezcano 			GFP_KERNEL);
29705ee09105SYOSHIFUJI Hideaki 
29715ee09105SYOSHIFUJI Hideaki 	if (table) {
29725ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2973c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
297486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29755ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29765ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29775ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29785ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29795ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29805ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29815ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29829c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2983464dc801SEric W. Biederman 
2984464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2985464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2986464dc801SEric W. Biederman 			table[0].procname = NULL;
29875ee09105SYOSHIFUJI Hideaki 	}
29885ee09105SYOSHIFUJI Hideaki 
2989760f2d01SDaniel Lezcano 	return table;
2990760f2d01SDaniel Lezcano }
29911da177e4SLinus Torvalds #endif
29921da177e4SLinus Torvalds 
29932c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2994cdb18761SDaniel Lezcano {
2995633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29968ed67789SDaniel Lezcano 
299786393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
299886393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2999f2fc6a54SBenjamin Thery 
3000fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3001fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
3002fc66f95cSEric Dumazet 
30038ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
30048ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
30058ed67789SDaniel Lezcano 					   GFP_KERNEL);
30068ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
3007fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
3008d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
30098ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
3010d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
301162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
301262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30138ed67789SDaniel Lezcano 
30148ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30158ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
30168ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
30178ed67789SDaniel Lezcano 					       GFP_KERNEL);
301868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
301968fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3020d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
30218ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3022d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
302362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
302462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30258ed67789SDaniel Lezcano 
30268ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
30278ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
30288ed67789SDaniel Lezcano 					       GFP_KERNEL);
302968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
303068fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3031d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
30328ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3033d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
303462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
303562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30368ed67789SDaniel Lezcano #endif
30378ed67789SDaniel Lezcano 
3038b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3039b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3040b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3041b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3042b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3043b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3044b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3045b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3046b339a47cSPeter Zijlstra 
30476891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
30486891a346SBenjamin Thery 
30498ed67789SDaniel Lezcano 	ret = 0;
30508ed67789SDaniel Lezcano out:
30518ed67789SDaniel Lezcano 	return ret;
3052f2fc6a54SBenjamin Thery 
305368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
305468fffc67SPeter Zijlstra out_ip6_prohibit_entry:
305568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
305668fffc67SPeter Zijlstra out_ip6_null_entry:
305768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
305868fffc67SPeter Zijlstra #endif
3059fc66f95cSEric Dumazet out_ip6_dst_entries:
3060fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3061f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3062f2fc6a54SBenjamin Thery 	goto out;
3063cdb18761SDaniel Lezcano }
3064cdb18761SDaniel Lezcano 
30652c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3066cdb18761SDaniel Lezcano {
30678ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30688ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30698ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30708ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30718ed67789SDaniel Lezcano #endif
307241bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3073cdb18761SDaniel Lezcano }
3074cdb18761SDaniel Lezcano 
3075d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3076d189634eSThomas Graf {
3077d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3078d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3079d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3080d189634eSThomas Graf #endif
3081d189634eSThomas Graf 	return 0;
3082d189634eSThomas Graf }
3083d189634eSThomas Graf 
3084d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3085d189634eSThomas Graf {
3086d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3087ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3088ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3089d189634eSThomas Graf #endif
3090d189634eSThomas Graf }
3091d189634eSThomas Graf 
3092cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3093cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3094cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3095cdb18761SDaniel Lezcano };
3096cdb18761SDaniel Lezcano 
3097c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3098c3426b47SDavid S. Miller {
3099c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3100c3426b47SDavid S. Miller 
3101c3426b47SDavid S. Miller 	if (!bp)
3102c3426b47SDavid S. Miller 		return -ENOMEM;
3103c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3104c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3105c3426b47SDavid S. Miller 	return 0;
3106c3426b47SDavid S. Miller }
3107c3426b47SDavid S. Miller 
3108c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3109c3426b47SDavid S. Miller {
3110c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3111c3426b47SDavid S. Miller 
3112c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
311356a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3114c3426b47SDavid S. Miller 	kfree(bp);
3115c3426b47SDavid S. Miller }
3116c3426b47SDavid S. Miller 
31172b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3118c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3119c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3120c3426b47SDavid S. Miller };
3121c3426b47SDavid S. Miller 
3122d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3123d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3124d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3125d189634eSThomas Graf };
3126d189634eSThomas Graf 
31278ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
31288ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
31298ed67789SDaniel Lezcano 	.priority = 0,
31308ed67789SDaniel Lezcano };
31318ed67789SDaniel Lezcano 
3132433d49c3SDaniel Lezcano int __init ip6_route_init(void)
31331da177e4SLinus Torvalds {
3134433d49c3SDaniel Lezcano 	int ret;
3135433d49c3SDaniel Lezcano 
31369a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
31379a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
31389a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
31399a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
31409a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3141c19a28e1SFernando Carrijo 		goto out;
314214e50e57SDavid S. Miller 
3143fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
31448ed67789SDaniel Lezcano 	if (ret)
3145bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3146bdb3289fSDaniel Lezcano 
3147c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3148c3426b47SDavid S. Miller 	if (ret)
3149e8803b6cSDavid S. Miller 		goto out_dst_entries;
31502a0c451aSThomas Graf 
31517e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
31527e52b33bSDavid S. Miller 	if (ret)
31537e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3154c3426b47SDavid S. Miller 
31555dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31565dc121e9SArnaud Ebalard 
31578ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31588ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31598ed67789SDaniel Lezcano 	 * manually for init_net */
3160d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31618ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3162bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3163d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31648ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3165d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31668ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3167bdb3289fSDaniel Lezcano   #endif
3168e8803b6cSDavid S. Miller 	ret = fib6_init();
3169433d49c3SDaniel Lezcano 	if (ret)
31708ed67789SDaniel Lezcano 		goto out_register_subsys;
3171433d49c3SDaniel Lezcano 
3172433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3173433d49c3SDaniel Lezcano 	if (ret)
3174e8803b6cSDavid S. Miller 		goto out_fib6_init;
3175c35b7e72SDaniel Lezcano 
3176433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3177433d49c3SDaniel Lezcano 	if (ret)
3178433d49c3SDaniel Lezcano 		goto xfrm6_init;
31797e5449c2SDaniel Lezcano 
3180d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3181d189634eSThomas Graf 	if (ret)
3182d189634eSThomas Graf 		goto fib6_rules_init;
3183d189634eSThomas Graf 
3184433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3185c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3186c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3187c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3188d189634eSThomas Graf 		goto out_register_late_subsys;
3189433d49c3SDaniel Lezcano 
31908ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3191cdb18761SDaniel Lezcano 	if (ret)
3192d189634eSThomas Graf 		goto out_register_late_subsys;
31938ed67789SDaniel Lezcano 
3194433d49c3SDaniel Lezcano out:
3195433d49c3SDaniel Lezcano 	return ret;
3196433d49c3SDaniel Lezcano 
3197d189634eSThomas Graf out_register_late_subsys:
3198d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3199433d49c3SDaniel Lezcano fib6_rules_init:
3200433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3201433d49c3SDaniel Lezcano xfrm6_init:
3202433d49c3SDaniel Lezcano 	xfrm6_fini();
32032a0c451aSThomas Graf out_fib6_init:
32042a0c451aSThomas Graf 	fib6_gc_cleanup();
32058ed67789SDaniel Lezcano out_register_subsys:
32068ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
32077e52b33bSDavid S. Miller out_register_inetpeer:
32087e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3209fc66f95cSEric Dumazet out_dst_entries:
3210fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3211433d49c3SDaniel Lezcano out_kmem_cache:
3212f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3213433d49c3SDaniel Lezcano 	goto out;
32141da177e4SLinus Torvalds }
32151da177e4SLinus Torvalds 
32161da177e4SLinus Torvalds void ip6_route_cleanup(void)
32171da177e4SLinus Torvalds {
32188ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3219d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3220101367c2SThomas Graf 	fib6_rules_cleanup();
32211da177e4SLinus Torvalds 	xfrm6_fini();
32221da177e4SLinus Torvalds 	fib6_gc_cleanup();
3223c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
32248ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
322541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3226f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
32271da177e4SLinus Torvalds }
3228