xref: /openbmc/linux/net/ipv6/route.c (revision ece31ffd539e8e2b586b1ca5f50bc4f4591e3893)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47457c4cbcSEric W. Biederman #include <net/net_namespace.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds #include <net/ipv6.h>
501da177e4SLinus Torvalds #include <net/ip6_fib.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/ndisc.h>
531da177e4SLinus Torvalds #include <net/addrconf.h>
541da177e4SLinus Torvalds #include <net/tcp.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <net/dst.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
588d71740cSTom Tucker #include <net/netevent.h>
5921713ebcSThomas Graf #include <net/netlink.h>
6051ebd318SNicolas Dichtel #include <net/nexthop.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
651da177e4SLinus Torvalds #include <linux/sysctl.h>
661da177e4SLinus Torvalds #endif
671da177e4SLinus Torvalds 
681716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6921efcfa0SEric Dumazet 				    const struct in6_addr *dest);
701da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
710dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
72ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
751da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
761da177e4SLinus Torvalds 				       struct net_device *dev, int how);
77569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
801da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
826700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
836700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
846700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
856700c270SDavid S. Miller 					struct sk_buff *skb);
861da177e4SLinus Torvalds 
8770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9195c96174SEric Dumazet 					   unsigned int pref);
92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
93b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
94b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9570ceb4f5SYOSHIFUJI Hideaki #endif
9670ceb4f5SYOSHIFUJI Hideaki 
9706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9806582540SDavid S. Miller {
9906582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10006582540SDavid S. Miller 	struct inet_peer *peer;
10106582540SDavid S. Miller 	u32 *p = NULL;
10206582540SDavid S. Miller 
1038e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1048e2ec639SYan, Zheng 		return NULL;
1058e2ec639SYan, Zheng 
106fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10706582540SDavid S. Miller 	if (peer) {
10806582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10906582540SDavid S. Miller 		unsigned long prev, new;
11006582540SDavid S. Miller 
11106582540SDavid S. Miller 		p = peer->metrics;
11206582540SDavid S. Miller 		if (inet_metrics_new(peer))
11306582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11406582540SDavid S. Miller 
11506582540SDavid S. Miller 		new = (unsigned long) p;
11606582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		if (prev != old) {
11906582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12006582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12106582540SDavid S. Miller 				p = NULL;
12206582540SDavid S. Miller 		}
12306582540SDavid S. Miller 	}
12406582540SDavid S. Miller 	return p;
12506582540SDavid S. Miller }
12606582540SDavid S. Miller 
127f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
128f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
129f894cbf8SDavid S. Miller 					     const void *daddr)
13039232973SDavid S. Miller {
13139232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
13239232973SDavid S. Miller 
133a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13439232973SDavid S. Miller 		return (const void *) p;
135f894cbf8SDavid S. Miller 	else if (skb)
136f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13739232973SDavid S. Miller 	return daddr;
13839232973SDavid S. Miller }
13939232973SDavid S. Miller 
140f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
141f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
142f894cbf8SDavid S. Miller 					  const void *daddr)
143d3aaeb38SDavid S. Miller {
14439232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14539232973SDavid S. Miller 	struct neighbour *n;
14639232973SDavid S. Miller 
147f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1488e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
149f83c7790SDavid S. Miller 	if (n)
150f83c7790SDavid S. Miller 		return n;
151f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
152f83c7790SDavid S. Miller }
153f83c7790SDavid S. Miller 
1549a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1551da177e4SLinus Torvalds 	.family			=	AF_INET6,
15609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1571da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1581da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1591da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1600dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
161ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1631da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1641da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1651da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1661da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1671da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1686e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1691ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
170d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1711da177e4SLinus Torvalds };
1721da177e4SLinus Torvalds 
173ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
174ec831ea7SRoland Dreier {
175618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
176618f9bc7SSteffen Klassert 
177618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
178ec831ea7SRoland Dreier }
179ec831ea7SRoland Dreier 
1806700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1816700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
18214e50e57SDavid S. Miller {
18314e50e57SDavid S. Miller }
18414e50e57SDavid S. Miller 
1856700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1866700c270SDavid S. Miller 				      struct sk_buff *skb)
187b587ee3bSDavid S. Miller {
188b587ee3bSDavid S. Miller }
189b587ee3bSDavid S. Miller 
1900972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1910972ddb2SHeld Bernhard 					 unsigned long old)
1920972ddb2SHeld Bernhard {
1930972ddb2SHeld Bernhard 	return NULL;
1940972ddb2SHeld Bernhard }
1950972ddb2SHeld Bernhard 
19614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19714e50e57SDavid S. Miller 	.family			=	AF_INET6,
19809640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
19914e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20014e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
201ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
202214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20314e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
204b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2050972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
206d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20714e50e57SDavid S. Miller };
20814e50e57SDavid S. Miller 
20962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21014edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
21162fa8a84SDavid S. Miller };
21262fa8a84SDavid S. Miller 
213fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2141da177e4SLinus Torvalds 	.dst = {
2151da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2161da177e4SLinus Torvalds 		.__use		= 1,
2172c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2181da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2191da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2201da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2211da177e4SLinus Torvalds 	},
2221da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2234f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2241da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2251da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
228101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
229101367c2SThomas Graf 
2306723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2316723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2326723ab54SDavid S. Miller 
233fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
234101367c2SThomas Graf 	.dst = {
235101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
236101367c2SThomas Graf 		.__use		= 1,
2372c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
238101367c2SThomas Graf 		.error		= -EACCES,
2399ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2409ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
241101367c2SThomas Graf 	},
242101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2434f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
244101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
245101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
246101367c2SThomas Graf };
247101367c2SThomas Graf 
248fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
249101367c2SThomas Graf 	.dst = {
250101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
251101367c2SThomas Graf 		.__use		= 1,
2522c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
253101367c2SThomas Graf 		.error		= -EINVAL,
254352e512cSHerbert Xu 		.input		= dst_discard,
255352e512cSHerbert Xu 		.output		= dst_discard,
256101367c2SThomas Graf 	},
257101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2584f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
259101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
260101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
261101367c2SThomas Graf };
262101367c2SThomas Graf 
263101367c2SThomas Graf #endif
264101367c2SThomas Graf 
2651da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26697bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
267957c665fSDavid S. Miller 					     struct net_device *dev,
2688b96d22dSDavid S. Miller 					     int flags,
2698b96d22dSDavid S. Miller 					     struct fib6_table *table)
2701da177e4SLinus Torvalds {
27197bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2726f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
273cf911662SDavid S. Miller 
27497bab73fSDavid S. Miller 	if (rt) {
2758104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2768104891bSSteffen Klassert 
2778104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2788b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
2796f3118b5SNicolas Dichtel 		rt->rt6i_genid = rt_genid(net);
28051ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28151ebd318SNicolas Dichtel 		rt->rt6i_nsiblings = 0;
28297bab73fSDavid S. Miller 	}
283cf911662SDavid S. Miller 	return rt;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2891da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2901da177e4SLinus Torvalds 
2918e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2928e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2938e2ec639SYan, Zheng 
29438308473SDavid S. Miller 	if (idev) {
2951da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2961da177e4SLinus Torvalds 		in6_dev_put(idev);
2971da177e4SLinus Torvalds 	}
2981716a961SGao feng 
2991716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
3001716a961SGao feng 		dst_release(dst->from);
3011716a961SGao feng 
30297bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
30397bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
304b3419363SDavid S. Miller 		inet_putpeer(peer);
305b3419363SDavid S. Miller 	}
306b3419363SDavid S. Miller }
307b3419363SDavid S. Miller 
308b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
309b3419363SDavid S. Miller {
31097bab73fSDavid S. Miller 	struct inet_peer_base *base;
311b3419363SDavid S. Miller 	struct inet_peer *peer;
312b3419363SDavid S. Miller 
31397bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
31497bab73fSDavid S. Miller 	if (!base)
31597bab73fSDavid S. Miller 		return;
31697bab73fSDavid S. Miller 
31797bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3187b34ca2aSDavid S. Miller 	if (peer) {
31997bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
320b3419363SDavid S. Miller 			inet_putpeer(peer);
3211da177e4SLinus Torvalds 	}
3227b34ca2aSDavid S. Miller }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3251da177e4SLinus Torvalds 			   int how)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3281da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3295a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
330c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3311da177e4SLinus Torvalds 
33297cac082SDavid S. Miller 	if (dev != loopback_dev) {
33397cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3345a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3355a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
33638308473SDavid S. Miller 			if (loopback_idev) {
3371da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3381da177e4SLinus Torvalds 				in6_dev_put(idev);
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 		}
34197cac082SDavid S. Miller 	}
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
344a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3451da177e4SLinus Torvalds {
3461716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3471716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
348a50feda5SEric Dumazet 			return true;
3491716a961SGao feng 	} else if (rt->dst.from) {
3503fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3511716a961SGao feng 	}
352a50feda5SEric Dumazet 	return false;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
355a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
356c71099acSThomas Graf {
357a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
358a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
359c71099acSThomas Graf }
360c71099acSThomas Graf 
36151ebd318SNicolas Dichtel /* Multipath route selection:
36251ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
36351ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
36451ebd318SNicolas Dichtel  */
36551ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
36651ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
36751ebd318SNicolas Dichtel {
36851ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
36951ebd318SNicolas Dichtel 
370c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
371c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
37251ebd318SNicolas Dichtel 
37351ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
37451ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
37551ebd318SNicolas Dichtel 	case IPPROTO_UDP:
37651ebd318SNicolas Dichtel 	case IPPROTO_TCP:
37751ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
378b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
379b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38051ebd318SNicolas Dichtel 		break;
38151ebd318SNicolas Dichtel 
38251ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
383b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
384b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
38551ebd318SNicolas Dichtel 		break;
38651ebd318SNicolas Dichtel 	}
38751ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
388b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
38951ebd318SNicolas Dichtel 
39051ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39151ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
39251ebd318SNicolas Dichtel 	return val % candidate_count;
39351ebd318SNicolas Dichtel }
39451ebd318SNicolas Dichtel 
39551ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
39651ebd318SNicolas Dichtel 					     struct flowi6 *fl6)
39751ebd318SNicolas Dichtel {
39851ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
39951ebd318SNicolas Dichtel 	int route_choosen;
40051ebd318SNicolas Dichtel 
40151ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
40251ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
40351ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
40451ebd318SNicolas Dichtel 	 */
40551ebd318SNicolas Dichtel 	if (route_choosen)
40651ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
40751ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
40851ebd318SNicolas Dichtel 			route_choosen--;
40951ebd318SNicolas Dichtel 			if (route_choosen == 0) {
41051ebd318SNicolas Dichtel 				match = sibling;
41151ebd318SNicolas Dichtel 				break;
41251ebd318SNicolas Dichtel 			}
41351ebd318SNicolas Dichtel 		}
41451ebd318SNicolas Dichtel 	return match;
41551ebd318SNicolas Dichtel }
41651ebd318SNicolas Dichtel 
4171da177e4SLinus Torvalds /*
418c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4191da177e4SLinus Torvalds  */
4201da177e4SLinus Torvalds 
4218ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4228ed67789SDaniel Lezcano 						    struct rt6_info *rt,
423b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4241da177e4SLinus Torvalds 						    int oif,
425d420895eSYOSHIFUJI Hideaki 						    int flags)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4281da177e4SLinus Torvalds 	struct rt6_info *sprt;
4291da177e4SLinus Torvalds 
430dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
431dd3abc4eSYOSHIFUJI Hideaki 		goto out;
432dd3abc4eSYOSHIFUJI Hideaki 
433d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
434d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
435dd3abc4eSYOSHIFUJI Hideaki 
436dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4371da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4381da177e4SLinus Torvalds 				return sprt;
4391da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
44038308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4411da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
442d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4431da177e4SLinus Torvalds 						continue;
4441da177e4SLinus Torvalds 					if (local && (!oif ||
4451da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4461da177e4SLinus Torvalds 						continue;
4471da177e4SLinus Torvalds 				}
4481da177e4SLinus Torvalds 				local = sprt;
4491da177e4SLinus Torvalds 			}
450dd3abc4eSYOSHIFUJI Hideaki 		} else {
451dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
452dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
453dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
454dd3abc4eSYOSHIFUJI Hideaki 		}
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 
457dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4581da177e4SLinus Torvalds 		if (local)
4591da177e4SLinus Torvalds 			return local;
4601da177e4SLinus Torvalds 
461d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4628ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4631da177e4SLinus Torvalds 	}
464dd3abc4eSYOSHIFUJI Hideaki out:
4651da177e4SLinus Torvalds 	return rt;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
46827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
46927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
47027097255SYOSHIFUJI Hideaki {
471f2c31e32SEric Dumazet 	struct neighbour *neigh;
47227097255SYOSHIFUJI Hideaki 	/*
47327097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
47427097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
47527097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
47627097255SYOSHIFUJI Hideaki 	 *
47727097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
47827097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
47927097255SYOSHIFUJI Hideaki 	 */
4802152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
481fdd6681dSAmerigo Wang 		return;
4822152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4832152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4842152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4852152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4862152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4872152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4887ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
4892152caeaSYOSHIFUJI Hideaki / 吉藤英明 
4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
49152e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
49227097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
49327097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
49427097255SYOSHIFUJI Hideaki 
495b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh) {
49627097255SYOSHIFUJI Hideaki 			neigh->updated = jiffies;
4972152caeaSYOSHIFUJI Hideaki / 吉藤英明 			write_unlock(&neigh->lock);
498b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		}
4992152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5002152caeaSYOSHIFUJI Hideaki / 吉藤英明 		target = (struct in6_addr *)&rt->rt6i_gateway;
50127097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
502d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
503f2c31e32SEric Dumazet 	} else {
5042152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5052152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
50627097255SYOSHIFUJI Hideaki 	}
5072152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
508f2c31e32SEric Dumazet }
50927097255SYOSHIFUJI Hideaki #else
51027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
51127097255SYOSHIFUJI Hideaki {
51227097255SYOSHIFUJI Hideaki }
51327097255SYOSHIFUJI Hideaki #endif
51427097255SYOSHIFUJI Hideaki 
5151da177e4SLinus Torvalds /*
516554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5171da177e4SLinus Torvalds  */
518b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5191da177e4SLinus Torvalds {
520d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
521161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
522554cfb7eSYOSHIFUJI Hideaki 		return 2;
523161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
524161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
525161980f4SDavid S. Miller 		return 1;
526554cfb7eSYOSHIFUJI Hideaki 	return 0;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
529a5a81f0bSPaul Marks static inline bool rt6_check_neigh(struct rt6_info *rt)
5301da177e4SLinus Torvalds {
531f2c31e32SEric Dumazet 	struct neighbour *neigh;
532a5a81f0bSPaul Marks 	bool ret = false;
533f2c31e32SEric Dumazet 
5344d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5354d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
536145a3621SYOSHIFUJI Hideaki / 吉藤英明 		return true;
537145a3621SYOSHIFUJI Hideaki / 吉藤英明 
538145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
539145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
540145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
541145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
542554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
543a5a81f0bSPaul Marks 			ret = true;
544398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
545a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
546a5a81f0bSPaul Marks 			ret = true;
547398bcbebSYOSHIFUJI Hideaki #endif
548145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
549a5a81f0bSPaul Marks 	}
550145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
551145a3621SYOSHIFUJI Hideaki / 吉藤英明 
552a5a81f0bSPaul Marks 	return ret;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
555554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
556554cfb7eSYOSHIFUJI Hideaki 			   int strict)
557554cfb7eSYOSHIFUJI Hideaki {
558a5a81f0bSPaul Marks 	int m;
5594d0c5911SYOSHIFUJI Hideaki 
5604d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
56177d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
562554cfb7eSYOSHIFUJI Hideaki 		return -1;
563ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
564ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
565ebacaaa0SYOSHIFUJI Hideaki #endif
566a5a81f0bSPaul Marks 	if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
567554cfb7eSYOSHIFUJI Hideaki 		return -1;
568554cfb7eSYOSHIFUJI Hideaki 	return m;
569554cfb7eSYOSHIFUJI Hideaki }
570554cfb7eSYOSHIFUJI Hideaki 
571f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
572f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
573554cfb7eSYOSHIFUJI Hideaki {
574554cfb7eSYOSHIFUJI Hideaki 	int m;
575554cfb7eSYOSHIFUJI Hideaki 
576554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
577f11e6659SDavid S. Miller 		goto out;
578554cfb7eSYOSHIFUJI Hideaki 
579554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
580554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
581f11e6659SDavid S. Miller 		goto out;
582554cfb7eSYOSHIFUJI Hideaki 
583f11e6659SDavid S. Miller 	if (m > *mpri) {
584ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
58527097255SYOSHIFUJI Hideaki 			rt6_probe(match);
586f11e6659SDavid S. Miller 		*mpri = m;
587554cfb7eSYOSHIFUJI Hideaki 		match = rt;
588ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
58927097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5901da177e4SLinus Torvalds 	}
591f11e6659SDavid S. Miller 
592f11e6659SDavid S. Miller out:
593f11e6659SDavid S. Miller 	return match;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
596f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
597f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
598f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
599f11e6659SDavid S. Miller {
600f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
601f11e6659SDavid S. Miller 	int mpri = -1;
602f11e6659SDavid S. Miller 
603f11e6659SDavid S. Miller 	match = NULL;
604f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
605d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
606f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
607f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
608d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
609f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
610f11e6659SDavid S. Miller 
611f11e6659SDavid S. Miller 	return match;
612f11e6659SDavid S. Miller }
613f11e6659SDavid S. Miller 
614f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
615f11e6659SDavid S. Miller {
616f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6178ed67789SDaniel Lezcano 	struct net *net;
618f11e6659SDavid S. Miller 
619f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
620f11e6659SDavid S. Miller 	if (!rt0)
621f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
622f11e6659SDavid S. Miller 
623f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
624f11e6659SDavid S. Miller 
625554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
626f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
627d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
628f11e6659SDavid S. Miller 
629554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
630f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
631f11e6659SDavid S. Miller 			next = fn->leaf;
632f11e6659SDavid S. Miller 
633f11e6659SDavid S. Miller 		if (next != rt0)
634f11e6659SDavid S. Miller 			fn->rr_ptr = next;
635554cfb7eSYOSHIFUJI Hideaki 	}
636554cfb7eSYOSHIFUJI Hideaki 
637d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
638a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
64170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
64270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
643b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
64470ceb4f5SYOSHIFUJI Hideaki {
645c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
64670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
64770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
64870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6494bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
65070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
65170ceb4f5SYOSHIFUJI Hideaki 
65270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
65370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
65470ceb4f5SYOSHIFUJI Hideaki 	}
65570ceb4f5SYOSHIFUJI Hideaki 
65670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
65770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
65870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
65970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
66070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
66270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
66370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
66470ceb4f5SYOSHIFUJI Hideaki 		}
66570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
66670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
66770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
66870ceb4f5SYOSHIFUJI Hideaki 		}
66970ceb4f5SYOSHIFUJI Hideaki 	}
67070ceb4f5SYOSHIFUJI Hideaki 
67170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
67270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6733933fc95SJens Rosenboom 		return -EINVAL;
67470ceb4f5SYOSHIFUJI Hideaki 
6754bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
67670ceb4f5SYOSHIFUJI Hideaki 
67770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
67870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
67970ceb4f5SYOSHIFUJI Hideaki 	else {
68070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
68170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
68270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
68370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
68470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
68570ceb4f5SYOSHIFUJI Hideaki 	}
68670ceb4f5SYOSHIFUJI Hideaki 
687efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
688efa2cea0SDaniel Lezcano 				dev->ifindex);
68970ceb4f5SYOSHIFUJI Hideaki 
69070ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
691e0a1ad73SThomas Graf 		ip6_del_rt(rt);
69270ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
69370ceb4f5SYOSHIFUJI Hideaki 	}
69470ceb4f5SYOSHIFUJI Hideaki 
69570ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
696efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
69770ceb4f5SYOSHIFUJI Hideaki 					pref);
69870ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
69970ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
70070ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
70170ceb4f5SYOSHIFUJI Hideaki 
70270ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7031716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7041716a961SGao feng 			rt6_clean_expires(rt);
7051716a961SGao feng 		else
7061716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7071716a961SGao feng 
70894e187c0SAmerigo Wang 		ip6_rt_put(rt);
70970ceb4f5SYOSHIFUJI Hideaki 	}
71070ceb4f5SYOSHIFUJI Hideaki 	return 0;
71170ceb4f5SYOSHIFUJI Hideaki }
71270ceb4f5SYOSHIFUJI Hideaki #endif
71370ceb4f5SYOSHIFUJI Hideaki 
7148ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
715982f56f3SYOSHIFUJI Hideaki do { \
7168ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
717982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
718e0eda7bbSVille Nuorvala 		while (1) { \
719982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
720c71099acSThomas Graf 				goto out; \
721982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
722982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7238bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
724982f56f3SYOSHIFUJI Hideaki 			else \
725982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
726c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
727c71099acSThomas Graf 				goto restart; \
728c71099acSThomas Graf 		} \
729982f56f3SYOSHIFUJI Hideaki 	} \
730982f56f3SYOSHIFUJI Hideaki } while (0)
731c71099acSThomas Graf 
7328ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7338ed67789SDaniel Lezcano 					     struct fib6_table *table,
7344c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct fib6_node *fn;
7371da177e4SLinus Torvalds 	struct rt6_info *rt;
7381da177e4SLinus Torvalds 
739c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7404c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
741c71099acSThomas Graf restart:
742c71099acSThomas Graf 	rt = fn->leaf;
7434c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
74451ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
74551ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
7464c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
747c71099acSThomas Graf out:
748d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
749c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7501da177e4SLinus Torvalds 	return rt;
751c71099acSThomas Graf 
752c71099acSThomas Graf }
753c71099acSThomas Graf 
754ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
755ea6e574eSFlorian Westphal 				    int flags)
756ea6e574eSFlorian Westphal {
757ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
758ea6e574eSFlorian Westphal }
759ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
760ea6e574eSFlorian Westphal 
7619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7629acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
763c71099acSThomas Graf {
7644c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7654c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7664c9483b2SDavid S. Miller 		.daddr = *daddr,
767c71099acSThomas Graf 	};
768c71099acSThomas Graf 	struct dst_entry *dst;
76977d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
770c71099acSThomas Graf 
771adaa70bbSThomas Graf 	if (saddr) {
7724c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
773adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
774adaa70bbSThomas Graf 	}
775adaa70bbSThomas Graf 
7764c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
777c71099acSThomas Graf 	if (dst->error == 0)
778c71099acSThomas Graf 		return (struct rt6_info *) dst;
779c71099acSThomas Graf 
780c71099acSThomas Graf 	dst_release(dst);
781c71099acSThomas Graf 
7821da177e4SLinus Torvalds 	return NULL;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7867159039aSYOSHIFUJI Hideaki 
787c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7881da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7891da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7901da177e4SLinus Torvalds    be destroyed.
7911da177e4SLinus Torvalds  */
7921da177e4SLinus Torvalds 
79386872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	int err;
796c71099acSThomas Graf 	struct fib6_table *table;
7971da177e4SLinus Torvalds 
798c71099acSThomas Graf 	table = rt->rt6i_table;
799c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
80086872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
801c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	return err;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
80640e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
80740e22e8fSThomas Graf {
8084d1169c1SDenis V. Lunev 	struct nl_info info = {
809d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8104d1169c1SDenis V. Lunev 	};
811528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
81240e22e8fSThomas Graf }
81340e22e8fSThomas Graf 
8141716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
81521efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
816b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct rt6_info *rt;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/*
8211da177e4SLinus Torvalds 	 *	Clone the route.
8221da177e4SLinus Torvalds 	 */
8231da177e4SLinus Torvalds 
82421efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if (rt) {
82758c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
828bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
82921efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
83058c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8314e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
83258c4fb86SYOSHIFUJI Hideaki 		}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8371da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8384e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8391da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8401da177e4SLinus Torvalds 		}
8411da177e4SLinus Torvalds #endif
84295a9a5baSYOSHIFUJI Hideaki 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	return rt;
8451da177e4SLinus Torvalds }
84695a9a5baSYOSHIFUJI Hideaki 
84721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
84821efcfa0SEric Dumazet 					const struct in6_addr *daddr)
849299d9939SYOSHIFUJI Hideaki {
85021efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
85121efcfa0SEric Dumazet 
852887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
853299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
854299d9939SYOSHIFUJI Hideaki 	return rt;
855299d9939SYOSHIFUJI Hideaki }
856299d9939SYOSHIFUJI Hideaki 
8578ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8584c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	struct fib6_node *fn;
861519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
862c71099acSThomas Graf 	int strict = 0;
8631da177e4SLinus Torvalds 	int attempts = 3;
864519fbd87SYOSHIFUJI Hideaki 	int err;
86553b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8661da177e4SLinus Torvalds 
86777d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds relookup:
870c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8711da177e4SLinus Torvalds 
8728238dd06SYOSHIFUJI Hideaki restart_2:
8734c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds restart:
8764acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
87751ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && oif == 0)
87851ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
8794c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8808ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8818238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8821da177e4SLinus Torvalds 		goto out;
8831da177e4SLinus Torvalds 
884d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
885c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8861da177e4SLinus Torvalds 
887c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
8884c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8897343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8904c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8917343ff31SDavid S. Miller 	else
8927343ff31SDavid S. Miller 		goto out2;
8931da177e4SLinus Torvalds 
89494e187c0SAmerigo Wang 	ip6_rt_put(rt);
8958ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8961da177e4SLinus Torvalds 
897d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
898e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
89940e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
900e40cf353SYOSHIFUJI Hideaki 		if (!err)
901e40cf353SYOSHIFUJI Hideaki 			goto out2;
902e40cf353SYOSHIFUJI Hideaki 	}
903e40cf353SYOSHIFUJI Hideaki 
904e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9051da177e4SLinus Torvalds 		goto out2;
9061da177e4SLinus Torvalds 
907519fbd87SYOSHIFUJI Hideaki 	/*
908c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
909519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9101da177e4SLinus Torvalds 	 */
91194e187c0SAmerigo Wang 	ip6_rt_put(rt);
9121da177e4SLinus Torvalds 	goto relookup;
913e40cf353SYOSHIFUJI Hideaki 
914519fbd87SYOSHIFUJI Hideaki out:
9158238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9168238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9178238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9188238dd06SYOSHIFUJI Hideaki 	}
919d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
920c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9211da177e4SLinus Torvalds out2:
922d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
923d8d1f30bSChangli Gao 	rt->dst.__use++;
924c71099acSThomas Graf 
925c71099acSThomas Graf 	return rt;
926c71099acSThomas Graf }
927c71099acSThomas Graf 
9288ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9294c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9304acad72dSPavel Emelyanov {
9314c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9324acad72dSPavel Emelyanov }
9334acad72dSPavel Emelyanov 
93472331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
93572331bc0SShmulik Ladkani 						struct net_device *dev,
93672331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
93772331bc0SShmulik Ladkani {
93872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
93972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
94072331bc0SShmulik Ladkani 
94172331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
94272331bc0SShmulik Ladkani }
94372331bc0SShmulik Ladkani 
944c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
945c71099acSThomas Graf {
946b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
947c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
948adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9494c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9504c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9514c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9524c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9536502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9544c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9554c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
956c71099acSThomas Graf 	};
957adaa70bbSThomas Graf 
95872331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
959c71099acSThomas Graf }
960c71099acSThomas Graf 
9618ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9624c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
963c71099acSThomas Graf {
9644c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
965c71099acSThomas Graf }
966c71099acSThomas Graf 
9679c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9684c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
969c71099acSThomas Graf {
970c71099acSThomas Graf 	int flags = 0;
971c71099acSThomas Graf 
9721fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9734dc27d1cSDavid McCullough 
9744c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
97577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
976c71099acSThomas Graf 
9774c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
978adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9790c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9800c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
981adaa70bbSThomas Graf 
9824c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
9857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9861da177e4SLinus Torvalds 
9872774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
98814e50e57SDavid S. Miller {
9895c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
99014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
99114e50e57SDavid S. Miller 
992f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
99314e50e57SDavid S. Miller 	if (rt) {
994d8d1f30bSChangli Gao 		new = &rt->dst;
99514e50e57SDavid S. Miller 
9968104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
9978104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
9988104891bSSteffen Klassert 
99914e50e57SDavid S. Miller 		new->__use = 1;
1000352e512cSHerbert Xu 		new->input = dst_discard;
1001352e512cSHerbert Xu 		new->output = dst_discard;
100214e50e57SDavid S. Miller 
100321efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
100421efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
100521efcfa0SEric Dumazet 		else
1006defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
100714e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
100814e50e57SDavid S. Miller 		if (rt->rt6i_idev)
100914e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
101014e50e57SDavid S. Miller 
10114e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10121716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
10131716a961SGao feng 		rt6_clean_expires(rt);
101414e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
101514e50e57SDavid S. Miller 
101614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
101714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
101814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
101914e50e57SDavid S. Miller #endif
102014e50e57SDavid S. Miller 
102114e50e57SDavid S. Miller 		dst_free(new);
102214e50e57SDavid S. Miller 	}
102314e50e57SDavid S. Miller 
102469ead7afSDavid S. Miller 	dst_release(dst_orig);
102569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
102614e50e57SDavid S. Miller }
102714e50e57SDavid S. Miller 
10281da177e4SLinus Torvalds /*
10291da177e4SLinus Torvalds  *	Destination cache support functions
10301da177e4SLinus Torvalds  */
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	struct rt6_info *rt;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10371da177e4SLinus Torvalds 
10386f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10396f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10406f3118b5SNicolas Dichtel 	 * into this function always.
10416f3118b5SNicolas Dichtel 	 */
10426f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
10436f3118b5SNicolas Dichtel 		return NULL;
10446f3118b5SNicolas Dichtel 
1045a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10461da177e4SLinus Torvalds 		return dst;
1047a4477c4dSLi RongQing 
10481da177e4SLinus Torvalds 	return NULL;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (rt) {
105654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
105754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1058e0a1ad73SThomas Graf 				ip6_del_rt(rt);
105954c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10601da177e4SLinus Torvalds 			}
106154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
106254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
106354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
106454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
106554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
106654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	struct rt6_info *rt;
10721da177e4SLinus Torvalds 
10733ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10741da177e4SLinus Torvalds 
1075adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10761da177e4SLinus Torvalds 	if (rt) {
10771716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10781716a961SGao feng 			rt6_update_expires(rt, 0);
10791716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10801da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10846700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
10856700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10881da177e4SLinus Torvalds 
108981aded24SDavid S. Miller 	dst_confirm(dst);
10901da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
109181aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
109281aded24SDavid S. Miller 
10931da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10941da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1095defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10961da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1097defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1098defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10991da177e4SLinus Torvalds 		}
1100defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
110181aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
110542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
110642ae66c8SDavid S. Miller 		     int oif, u32 mark)
110781aded24SDavid S. Miller {
110881aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
110981aded24SDavid S. Miller 	struct dst_entry *dst;
111081aded24SDavid S. Miller 	struct flowi6 fl6;
111181aded24SDavid S. Miller 
111281aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
111381aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
111481aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11153e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
111681aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
111781aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11186502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
111981aded24SDavid S. Miller 
112081aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
112181aded24SDavid S. Miller 	if (!dst->error)
11226700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
112381aded24SDavid S. Miller 	dst_release(dst);
112481aded24SDavid S. Miller }
112581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
112681aded24SDavid S. Miller 
112781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
112881aded24SDavid S. Miller {
112981aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
113081aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
113181aded24SDavid S. Miller }
113281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
113381aded24SDavid S. Miller 
11343a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11353a5ad2eeSDavid S. Miller {
11363a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11373a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11383a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11393a5ad2eeSDavid S. Miller 
11403a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11413a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11423a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11433a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11443a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11453a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11466502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
11473a5ad2eeSDavid S. Miller 
11483a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11493a5ad2eeSDavid S. Miller 	if (!dst->error)
11506700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
11513a5ad2eeSDavid S. Miller 	dst_release(dst);
11523a5ad2eeSDavid S. Miller }
11533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11543a5ad2eeSDavid S. Miller 
11553a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11563a5ad2eeSDavid S. Miller {
11573a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11583a5ad2eeSDavid S. Miller }
11593a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
11603a5ad2eeSDavid S. Miller 
11610dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11621da177e4SLinus Torvalds {
11630dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11640dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11650dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11660dbaee3bSDavid S. Miller 
11671da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11681da177e4SLinus Torvalds 
11695578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11705578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/*
11731da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11741da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11751da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11761da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11791da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11801da177e4SLinus Torvalds 	return mtu;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
1183ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1184d33e4553SDavid S. Miller {
1185d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1186618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1187618f9bc7SSteffen Klassert 
1188618f9bc7SSteffen Klassert 	if (mtu)
1189618f9bc7SSteffen Klassert 		return mtu;
1190618f9bc7SSteffen Klassert 
1191618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1192d33e4553SDavid S. Miller 
1193d33e4553SDavid S. Miller 	rcu_read_lock();
1194d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1195d33e4553SDavid S. Miller 	if (idev)
1196d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1197d33e4553SDavid S. Miller 	rcu_read_unlock();
1198d33e4553SDavid S. Miller 
1199d33e4553SDavid S. Miller 	return mtu;
1200d33e4553SDavid S. Miller }
1201d33e4553SDavid S. Miller 
12023b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12033b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12045d0bbeebSThomas Graf 
12053b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
120687a11578SDavid S. Miller 				  struct flowi6 *fl6)
12071da177e4SLinus Torvalds {
120887a11578SDavid S. Miller 	struct dst_entry *dst;
12091da177e4SLinus Torvalds 	struct rt6_info *rt;
12101da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1211c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12121da177e4SLinus Torvalds 
121338308473SDavid S. Miller 	if (unlikely(!idev))
1214122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12151da177e4SLinus Torvalds 
12168b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
121738308473SDavid S. Miller 	if (unlikely(!rt)) {
12181da177e4SLinus Torvalds 		in6_dev_put(idev);
121987a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12201da177e4SLinus Torvalds 		goto out;
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
12238e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12248e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1225d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
122687a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12278e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12288e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
122914edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
12301da177e4SLinus Torvalds 
12313b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1232d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1233d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12343b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12351da177e4SLinus Torvalds 
12365578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12371da177e4SLinus Torvalds 
123887a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
123987a11578SDavid S. Miller 
12401da177e4SLinus Torvalds out:
124187a11578SDavid S. Miller 	return dst;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12443d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12451da177e4SLinus Torvalds {
1246e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12473d0f24a7SStephen Hemminger 	int more = 0;
12481da177e4SLinus Torvalds 
12493b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12503b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12515d0bbeebSThomas Graf 
12521da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12531da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12541da177e4SLinus Torvalds 			*pprev = dst->next;
12551da177e4SLinus Torvalds 			dst_free(dst);
12561da177e4SLinus Torvalds 		} else {
12571da177e4SLinus Torvalds 			pprev = &dst->next;
12583d0f24a7SStephen Hemminger 			++more;
12591da177e4SLinus Torvalds 		}
12601da177e4SLinus Torvalds 	}
12611da177e4SLinus Torvalds 
12623b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12635d0bbeebSThomas Graf 
12643d0f24a7SStephen Hemminger 	return more;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
12671e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12681e493d19SDavid S. Miller 			    void *arg)
12691e493d19SDavid S. Miller {
12701e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12711e493d19SDavid S. Miller 
12721e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12731e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12741e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12751e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12761e493d19SDavid S. Miller 		if (func(rt, arg)) {
12771e493d19SDavid S. Miller 			*pprev = dst->next;
12781e493d19SDavid S. Miller 			dst_free(dst);
12791e493d19SDavid S. Miller 		} else {
12801e493d19SDavid S. Miller 			pprev = &dst->next;
12811e493d19SDavid S. Miller 		}
12821e493d19SDavid S. Miller 	}
12831e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12841e493d19SDavid S. Miller }
12851e493d19SDavid S. Miller 
1286569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	unsigned long now = jiffies;
128986393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12907019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12917019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12927019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12937019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12947019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1295fc66f95cSEric Dumazet 	int entries;
12961da177e4SLinus Torvalds 
1297fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12987019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1299fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13001da177e4SLinus Torvalds 		goto out;
13011da177e4SLinus Torvalds 
13026891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13036891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13046891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1305fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1306fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13077019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13081da177e4SLinus Torvalds out:
13097019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1310fc66f95cSEric Dumazet 	return entries > rt_max_size;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13136b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13141da177e4SLinus Torvalds {
13155170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1316a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13176b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1318c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1319c68f24ccSEric Dumazet 
1320c68f24ccSEric Dumazet 		rcu_read_lock();
1321c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1322c68f24ccSEric Dumazet 		if (idev)
13231da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1324c68f24ccSEric Dumazet 		else
132553b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1326c68f24ccSEric Dumazet 		rcu_read_unlock();
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 	return hoplimit;
13291da177e4SLinus Torvalds }
1330abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  *
13341da177e4SLinus Torvalds  */
13351da177e4SLinus Torvalds 
133686872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	int err;
13395578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13401da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13411da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13421da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1343c71099acSThomas Graf 	struct fib6_table *table;
13441da177e4SLinus Torvalds 	int addr_type;
13451da177e4SLinus Torvalds 
134686872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13471da177e4SLinus Torvalds 		return -EINVAL;
13481da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
134986872cb5SThomas Graf 	if (cfg->fc_src_len)
13501da177e4SLinus Torvalds 		return -EINVAL;
13511da177e4SLinus Torvalds #endif
135286872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13531da177e4SLinus Torvalds 		err = -ENODEV;
13545578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13551da177e4SLinus Torvalds 		if (!dev)
13561da177e4SLinus Torvalds 			goto out;
13571da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13581da177e4SLinus Torvalds 		if (!idev)
13591da177e4SLinus Torvalds 			goto out;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
136286872cb5SThomas Graf 	if (cfg->fc_metric == 0)
136386872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13641da177e4SLinus Torvalds 
1365c71099acSThomas Graf 	err = -ENOBUFS;
136638308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1367d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1368d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
136938308473SDavid S. Miller 		if (!table) {
1370f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1371d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1372d71314b4SMatti Vaittinen 		}
1373d71314b4SMatti Vaittinen 	} else {
1374d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1375d71314b4SMatti Vaittinen 	}
137638308473SDavid S. Miller 
137738308473SDavid S. Miller 	if (!table)
1378c71099acSThomas Graf 		goto out;
1379c71099acSThomas Graf 
13808b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13811da177e4SLinus Torvalds 
138238308473SDavid S. Miller 	if (!rt) {
13831da177e4SLinus Torvalds 		err = -ENOMEM;
13841da177e4SLinus Torvalds 		goto out;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
13871716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13881716a961SGao feng 		rt6_set_expires(rt, jiffies +
13891716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13901716a961SGao feng 	else
13911716a961SGao feng 		rt6_clean_expires(rt);
13921da177e4SLinus Torvalds 
139386872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
139486872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
139586872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
139686872cb5SThomas Graf 
139786872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1400d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1401ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1402ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14031da177e4SLinus Torvalds 	else
1404d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14051da177e4SLinus Torvalds 
1406d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14071da177e4SLinus Torvalds 
140886872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
140986872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14101da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
141111d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14121da177e4SLinus Torvalds 
14138e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14148e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14158e2ec639SYan, Zheng 		if (!metrics) {
14168e2ec639SYan, Zheng 			err = -ENOMEM;
14178e2ec639SYan, Zheng 			goto out;
14188e2ec639SYan, Zheng 		}
14198e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14208e2ec639SYan, Zheng 	}
14211da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
142286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
142386872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14241da177e4SLinus Torvalds #endif
14251da177e4SLinus Torvalds 
142686872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14291da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14301da177e4SLinus Torvalds 	 */
143186872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
143238308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
143338308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
143438308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14351da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14365578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14371da177e4SLinus Torvalds 			if (dev) {
14381da177e4SLinus Torvalds 				dev_put(dev);
14391da177e4SLinus Torvalds 				in6_dev_put(idev);
14401da177e4SLinus Torvalds 			}
14415578689aSDaniel Lezcano 			dev = net->loopback_dev;
14421da177e4SLinus Torvalds 			dev_hold(dev);
14431da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14441da177e4SLinus Torvalds 			if (!idev) {
14451da177e4SLinus Torvalds 				err = -ENODEV;
14461da177e4SLinus Torvalds 				goto out;
14471da177e4SLinus Torvalds 			}
14481da177e4SLinus Torvalds 		}
1449d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1450d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
14511da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1452ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1453ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1454ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1455ef2c7d7bSNicolas Dichtel 			break;
1456ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1457ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1458ef2c7d7bSNicolas Dichtel 			break;
1459b4949ab2SNicolas Dichtel 		case RTN_THROW:
1460b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1461b4949ab2SNicolas Dichtel 			break;
1462ef2c7d7bSNicolas Dichtel 		default:
1463ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1464ef2c7d7bSNicolas Dichtel 			break;
1465ef2c7d7bSNicolas Dichtel 		}
14661da177e4SLinus Torvalds 		goto install_route;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 
146986872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1470b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14711da177e4SLinus Torvalds 		int gwa_type;
14721da177e4SLinus Torvalds 
147386872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14744e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14751da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14781da177e4SLinus Torvalds 			struct rt6_info *grt;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14811da177e4SLinus Torvalds 			   addresses as nexthop address.
14821da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14831da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14841da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14851da177e4SLinus Torvalds 			   some exceptions. --ANK
14861da177e4SLinus Torvalds 			 */
14871da177e4SLinus Torvalds 			err = -EINVAL;
14881da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14891da177e4SLinus Torvalds 				goto out;
14901da177e4SLinus Torvalds 
14915578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
149438308473SDavid S. Miller 			if (!grt)
14951da177e4SLinus Torvalds 				goto out;
14961da177e4SLinus Torvalds 			if (dev) {
1497d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
149894e187c0SAmerigo Wang 					ip6_rt_put(grt);
14991da177e4SLinus Torvalds 					goto out;
15001da177e4SLinus Torvalds 				}
15011da177e4SLinus Torvalds 			} else {
1502d1918542SDavid S. Miller 				dev = grt->dst.dev;
15031da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15041da177e4SLinus Torvalds 				dev_hold(dev);
15051da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15061da177e4SLinus Torvalds 			}
15071da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15081da177e4SLinus Torvalds 				err = 0;
150994e187c0SAmerigo Wang 			ip6_rt_put(grt);
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 			if (err)
15121da177e4SLinus Torvalds 				goto out;
15131da177e4SLinus Torvalds 		}
15141da177e4SLinus Torvalds 		err = -EINVAL;
151538308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15161da177e4SLinus Torvalds 			goto out;
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	err = -ENODEV;
152038308473SDavid S. Miller 	if (!dev)
15211da177e4SLinus Torvalds 		goto out;
15221da177e4SLinus Torvalds 
1523c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1524c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1525c3968a85SDaniel Walter 			err = -EINVAL;
1526c3968a85SDaniel Walter 			goto out;
1527c3968a85SDaniel Walter 		}
15284e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1529c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1530c3968a85SDaniel Walter 	} else
1531c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1532c3968a85SDaniel Walter 
153386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds install_route:
153686872cb5SThomas Graf 	if (cfg->fc_mx) {
153786872cb5SThomas Graf 		struct nlattr *nla;
153886872cb5SThomas Graf 		int remaining;
15391da177e4SLinus Torvalds 
154086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15418f4c1f9bSThomas Graf 			int type = nla_type(nla);
154286872cb5SThomas Graf 
154386872cb5SThomas Graf 			if (type) {
154486872cb5SThomas Graf 				if (type > RTAX_MAX) {
15451da177e4SLinus Torvalds 					err = -EINVAL;
15461da177e4SLinus Torvalds 					goto out;
15471da177e4SLinus Torvalds 				}
154886872cb5SThomas Graf 
1549defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15501da177e4SLinus Torvalds 			}
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
1554d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15551da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1556c71099acSThomas Graf 	rt->rt6i_table = table;
155763152fc0SDaniel Lezcano 
1558c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
155963152fc0SDaniel Lezcano 
156086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds out:
15631da177e4SLinus Torvalds 	if (dev)
15641da177e4SLinus Torvalds 		dev_put(dev);
15651da177e4SLinus Torvalds 	if (idev)
15661da177e4SLinus Torvalds 		in6_dev_put(idev);
15671da177e4SLinus Torvalds 	if (rt)
1568d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15691da177e4SLinus Torvalds 	return err;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
157286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds 	int err;
1575c71099acSThomas Graf 	struct fib6_table *table;
1576d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15771da177e4SLinus Torvalds 
15786825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
15796825a26cSGao feng 		err = -ENOENT;
15806825a26cSGao feng 		goto out;
15816825a26cSGao feng 	}
15826c813a72SPatrick McHardy 
1583c71099acSThomas Graf 	table = rt->rt6i_table;
1584c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
158586872cb5SThomas Graf 	err = fib6_del(rt, info);
1586c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15871da177e4SLinus Torvalds 
15886825a26cSGao feng out:
158994e187c0SAmerigo Wang 	ip6_rt_put(rt);
15901da177e4SLinus Torvalds 	return err;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
1593e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1594e0a1ad73SThomas Graf {
15954d1169c1SDenis V. Lunev 	struct nl_info info = {
1596d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15974d1169c1SDenis V. Lunev 	};
1598528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1599e0a1ad73SThomas Graf }
1600e0a1ad73SThomas Graf 
160186872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16021da177e4SLinus Torvalds {
1603c71099acSThomas Graf 	struct fib6_table *table;
16041da177e4SLinus Torvalds 	struct fib6_node *fn;
16051da177e4SLinus Torvalds 	struct rt6_info *rt;
16061da177e4SLinus Torvalds 	int err = -ESRCH;
16071da177e4SLinus Torvalds 
16085578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
160938308473SDavid S. Miller 	if (!table)
1610c71099acSThomas Graf 		return err;
16111da177e4SLinus Torvalds 
1612c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1613c71099acSThomas Graf 
1614c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
161586872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
161686872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	if (fn) {
1619d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
162086872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1621d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1622d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16231da177e4SLinus Torvalds 				continue;
162486872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
162586872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16261da177e4SLinus Torvalds 				continue;
162786872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16281da177e4SLinus Torvalds 				continue;
1629d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1630c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16311da177e4SLinus Torvalds 
163286872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16331da177e4SLinus Torvalds 		}
16341da177e4SLinus Torvalds 	}
1635c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	return err;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
16406700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1641a6279458SYOSHIFUJI Hideaki {
1642e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1643a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1644e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1645e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1646e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1647e8599ff4SDavid S. Miller 	struct neighbour *neigh;
164871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
16496e157b6aSDavid S. Miller 	int optlen, on_link;
16506e157b6aSDavid S. Miller 	u8 *lladdr;
1651e8599ff4SDavid S. Miller 
1652e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
165371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1654e8599ff4SDavid S. Miller 
1655e8599ff4SDavid S. Miller 	if (optlen < 0) {
16566e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1657e8599ff4SDavid S. Miller 		return;
1658e8599ff4SDavid S. Miller 	}
1659e8599ff4SDavid S. Miller 
166071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1661e8599ff4SDavid S. Miller 
166271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
16636e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1664e8599ff4SDavid S. Miller 		return;
1665e8599ff4SDavid S. Miller 	}
1666e8599ff4SDavid S. Miller 
16676e157b6aSDavid S. Miller 	on_link = 0;
166871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1669e8599ff4SDavid S. Miller 		on_link = 1;
167071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1671e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
16726e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1673e8599ff4SDavid S. Miller 		return;
1674e8599ff4SDavid S. Miller 	}
1675e8599ff4SDavid S. Miller 
1676e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1677e8599ff4SDavid S. Miller 	if (!in6_dev)
1678e8599ff4SDavid S. Miller 		return;
1679e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1680e8599ff4SDavid S. Miller 		return;
1681e8599ff4SDavid S. Miller 
1682e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1683e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1684e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1685e8599ff4SDavid S. Miller 	 */
1686e8599ff4SDavid S. Miller 
168771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1688e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1689e8599ff4SDavid S. Miller 		return;
1690e8599ff4SDavid S. Miller 	}
16916e157b6aSDavid S. Miller 
16926e157b6aSDavid S. Miller 	lladdr = NULL;
1693e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1694e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1695e8599ff4SDavid S. Miller 					     skb->dev);
1696e8599ff4SDavid S. Miller 		if (!lladdr) {
1697e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1698e8599ff4SDavid S. Miller 			return;
1699e8599ff4SDavid S. Miller 		}
1700e8599ff4SDavid S. Miller 	}
1701e8599ff4SDavid S. Miller 
17026e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17036e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17046e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17056e157b6aSDavid S. Miller 		return;
17066e157b6aSDavid S. Miller 	}
17076e157b6aSDavid S. Miller 
17086e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17096e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17106e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17116e157b6aSDavid S. Miller 	 */
17126e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17136e157b6aSDavid S. Miller 
171471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1715e8599ff4SDavid S. Miller 	if (!neigh)
1716e8599ff4SDavid S. Miller 		return;
1717e8599ff4SDavid S. Miller 
17181da177e4SLinus Torvalds 	/*
17191da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17201da177e4SLinus Torvalds 	 */
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17231da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17241da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17251da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17261da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17271da177e4SLinus Torvalds 		     );
17281da177e4SLinus Torvalds 
172971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
173038308473SDavid S. Miller 	if (!nrt)
17311da177e4SLinus Torvalds 		goto out;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17341da177e4SLinus Torvalds 	if (on_link)
17351da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17361da177e4SLinus Torvalds 
17374e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
17381da177e4SLinus Torvalds 
173940e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17401da177e4SLinus Torvalds 		goto out;
17411da177e4SLinus Torvalds 
1742d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1743d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
174471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
174560592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
17468d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17478d71740cSTom Tucker 
17481da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
17496e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1750e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds out:
1754e8599ff4SDavid S. Miller 	neigh_release(neigh);
17556e157b6aSDavid S. Miller }
17566e157b6aSDavid S. Miller 
17571da177e4SLinus Torvalds /*
17581da177e4SLinus Torvalds  *	Misc support functions
17591da177e4SLinus Torvalds  */
17601da177e4SLinus Torvalds 
17611716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
176221efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17631da177e4SLinus Torvalds {
1764d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17658b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17668b96d22dSDavid S. Miller 					    ort->rt6i_table);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	if (rt) {
1769d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1770d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17718e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17721da177e4SLinus Torvalds 
17734e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17748e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1775defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1776d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17771da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17781da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17791da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1780d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17811da177e4SLinus Torvalds 
17824e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17831716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17841716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17851716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
17861716a961SGao feng 			rt6_set_from(rt, ort);
17871716a961SGao feng 		else
17881716a961SGao feng 			rt6_clean_expires(rt);
17891da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17921da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17931da177e4SLinus Torvalds #endif
17940f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1795c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 	return rt;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
180070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1801efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1802b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1803b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
180470ceb4f5SYOSHIFUJI Hideaki {
180570ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1807c71099acSThomas Graf 	struct fib6_table *table;
180870ceb4f5SYOSHIFUJI Hideaki 
1809efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
181038308473SDavid S. Miller 	if (!table)
1811c71099acSThomas Graf 		return NULL;
1812c71099acSThomas Graf 
18135744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1814c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
181570ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181670ceb4f5SYOSHIFUJI Hideaki 		goto out;
181770ceb4f5SYOSHIFUJI Hideaki 
1818d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1819d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
182070ceb4f5SYOSHIFUJI Hideaki 			continue;
182170ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
182270ceb4f5SYOSHIFUJI Hideaki 			continue;
182370ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
182470ceb4f5SYOSHIFUJI Hideaki 			continue;
1825d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182670ceb4f5SYOSHIFUJI Hideaki 		break;
182770ceb4f5SYOSHIFUJI Hideaki 	}
182870ceb4f5SYOSHIFUJI Hideaki out:
18295744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
183070ceb4f5SYOSHIFUJI Hideaki 	return rt;
183170ceb4f5SYOSHIFUJI Hideaki }
183270ceb4f5SYOSHIFUJI Hideaki 
1833efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1834b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1835b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183695c96174SEric Dumazet 					   unsigned int pref)
183770ceb4f5SYOSHIFUJI Hideaki {
183886872cb5SThomas Graf 	struct fib6_config cfg = {
183986872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1840238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
184186872cb5SThomas Graf 		.fc_ifindex	= ifindex,
184286872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
184386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
184486872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
184515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1846efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1847efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
184886872cb5SThomas Graf 	};
184970ceb4f5SYOSHIFUJI Hideaki 
18504e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18514e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
185286872cb5SThomas Graf 
1853e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1854e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
185586872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185670ceb4f5SYOSHIFUJI Hideaki 
185786872cb5SThomas Graf 	ip6_route_add(&cfg);
185870ceb4f5SYOSHIFUJI Hideaki 
1859efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
186070ceb4f5SYOSHIFUJI Hideaki }
186170ceb4f5SYOSHIFUJI Hideaki #endif
186270ceb4f5SYOSHIFUJI Hideaki 
1863b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	struct rt6_info *rt;
1866c71099acSThomas Graf 	struct fib6_table *table;
18671da177e4SLinus Torvalds 
1868c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
186938308473SDavid S. Miller 	if (!table)
1870c71099acSThomas Graf 		return NULL;
18711da177e4SLinus Torvalds 
18725744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1873d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1874d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1875045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18761da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18771da177e4SLinus Torvalds 			break;
18781da177e4SLinus Torvalds 	}
18791da177e4SLinus Torvalds 	if (rt)
1880d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
18815744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
18821da177e4SLinus Torvalds 	return rt;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
1885b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1886ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1887ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18881da177e4SLinus Torvalds {
188986872cb5SThomas Graf 	struct fib6_config cfg = {
189086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1891238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
189286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
189386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
189486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
189515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
18965578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1897c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
189886872cb5SThomas Graf 	};
18991da177e4SLinus Torvalds 
19004e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19011da177e4SLinus Torvalds 
190286872cb5SThomas Graf 	ip6_route_add(&cfg);
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19077b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19081da177e4SLinus Torvalds {
19091da177e4SLinus Torvalds 	struct rt6_info *rt;
1910c71099acSThomas Graf 	struct fib6_table *table;
1911c71099acSThomas Graf 
1912c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19137b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
191438308473SDavid S. Miller 	if (!table)
1915c71099acSThomas Graf 		return;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds restart:
1918c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1919d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19201da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1921d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1922c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1923e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19241da177e4SLinus Torvalds 			goto restart;
19251da177e4SLinus Torvalds 		}
19261da177e4SLinus Torvalds 	}
1927c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
19305578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19315578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
193286872cb5SThomas Graf 				 struct fib6_config *cfg)
193386872cb5SThomas Graf {
193486872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
193586872cb5SThomas Graf 
193686872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
193786872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
193886872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
193986872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
194086872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
194186872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
194286872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
194386872cb5SThomas Graf 
19445578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1945f1243c2dSBenjamin Thery 
19464e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19474e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19484e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
194986872cb5SThomas Graf }
195086872cb5SThomas Graf 
19515578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19521da177e4SLinus Torvalds {
195386872cb5SThomas Graf 	struct fib6_config cfg;
19541da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19551da177e4SLinus Torvalds 	int err;
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 	switch(cmd) {
19581da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19591da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
1960af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
19611da177e4SLinus Torvalds 			return -EPERM;
19621da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19631da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19641da177e4SLinus Torvalds 		if (err)
19651da177e4SLinus Torvalds 			return -EFAULT;
19661da177e4SLinus Torvalds 
19675578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
196886872cb5SThomas Graf 
19691da177e4SLinus Torvalds 		rtnl_lock();
19701da177e4SLinus Torvalds 		switch (cmd) {
19711da177e4SLinus Torvalds 		case SIOCADDRT:
197286872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19731da177e4SLinus Torvalds 			break;
19741da177e4SLinus Torvalds 		case SIOCDELRT:
197586872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19761da177e4SLinus Torvalds 			break;
19771da177e4SLinus Torvalds 		default:
19781da177e4SLinus Torvalds 			err = -EINVAL;
19791da177e4SLinus Torvalds 		}
19801da177e4SLinus Torvalds 		rtnl_unlock();
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 		return err;
19833ff50b79SStephen Hemminger 	}
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	return -EINVAL;
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds /*
19891da177e4SLinus Torvalds  *	Drop the packet on the floor
19901da177e4SLinus Torvalds  */
19911da177e4SLinus Torvalds 
1992d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19931da177e4SLinus Torvalds {
1994612f09e8SYOSHIFUJI Hideaki 	int type;
1995adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1996612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1997612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19980660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
199945bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20003bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20013bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2002612f09e8SYOSHIFUJI Hideaki 			break;
2003612f09e8SYOSHIFUJI Hideaki 		}
2004612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2005612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20063bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20073bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2008612f09e8SYOSHIFUJI Hideaki 		break;
2009612f09e8SYOSHIFUJI Hideaki 	}
20103ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20111da177e4SLinus Torvalds 	kfree_skb(skb);
20121da177e4SLinus Torvalds 	return 0;
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
20159ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20169ce8ade0SThomas Graf {
2017612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20189ce8ade0SThomas Graf }
20199ce8ade0SThomas Graf 
202020380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20211da177e4SLinus Torvalds {
2022adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2023612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
20266723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20276723ab54SDavid S. Miller 
20289ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20299ce8ade0SThomas Graf {
2030612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20319ce8ade0SThomas Graf }
20329ce8ade0SThomas Graf 
20339ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20349ce8ade0SThomas Graf {
2035adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2036612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20379ce8ade0SThomas Graf }
20389ce8ade0SThomas Graf 
20396723ab54SDavid S. Miller #endif
20406723ab54SDavid S. Miller 
20411da177e4SLinus Torvalds /*
20421da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20431da177e4SLinus Torvalds  */
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20461da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20478f031519SDavid S. Miller 				    bool anycast)
20481da177e4SLinus Torvalds {
2049c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20508b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
20511da177e4SLinus Torvalds 
205238308473SDavid S. Miller 	if (!rt) {
2053f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20541da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
205540385653SBen Greear 	}
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	in6_dev_hold(idev);
20581da177e4SLinus Torvalds 
205911d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2060d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2061d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20621da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
206558c4fb86SYOSHIFUJI Hideaki 	if (anycast)
206658c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
206758c4fb86SYOSHIFUJI Hideaki 	else
20681da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20691da177e4SLinus Torvalds 
20704e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20711da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20725578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20731da177e4SLinus Torvalds 
2074d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	return rt;
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds 
2079c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2080c3968a85SDaniel Walter 			struct rt6_info *rt,
2081b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2082c3968a85SDaniel Walter 			unsigned int prefs,
2083c3968a85SDaniel Walter 			struct in6_addr *saddr)
2084c3968a85SDaniel Walter {
2085c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2086c3968a85SDaniel Walter 	int err = 0;
2087c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
20884e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2089c3968a85SDaniel Walter 	else
2090c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2091c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2092c3968a85SDaniel Walter 	return err;
2093c3968a85SDaniel Walter }
2094c3968a85SDaniel Walter 
2095c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2096c3968a85SDaniel Walter struct arg_dev_net_ip {
2097c3968a85SDaniel Walter 	struct net_device *dev;
2098c3968a85SDaniel Walter 	struct net *net;
2099c3968a85SDaniel Walter 	struct in6_addr *addr;
2100c3968a85SDaniel Walter };
2101c3968a85SDaniel Walter 
2102c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2103c3968a85SDaniel Walter {
2104c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2105c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2106c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2107c3968a85SDaniel Walter 
2108d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2109c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2110c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2111c3968a85SDaniel Walter 		/* remove prefsrc entry */
2112c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2113c3968a85SDaniel Walter 	}
2114c3968a85SDaniel Walter 	return 0;
2115c3968a85SDaniel Walter }
2116c3968a85SDaniel Walter 
2117c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2118c3968a85SDaniel Walter {
2119c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2120c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2121c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2122c3968a85SDaniel Walter 		.net = net,
2123c3968a85SDaniel Walter 		.addr = &ifp->addr,
2124c3968a85SDaniel Walter 	};
2125c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2126c3968a85SDaniel Walter }
2127c3968a85SDaniel Walter 
21288ed67789SDaniel Lezcano struct arg_dev_net {
21298ed67789SDaniel Lezcano 	struct net_device *dev;
21308ed67789SDaniel Lezcano 	struct net *net;
21318ed67789SDaniel Lezcano };
21328ed67789SDaniel Lezcano 
21331da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21341da177e4SLinus Torvalds {
2135bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2136bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21378ed67789SDaniel Lezcano 
2138d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2139c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21401da177e4SLinus Torvalds 		return -1;
2141c159d30cSDavid S. Miller 
21421da177e4SLinus Torvalds 	return 0;
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds 
2145f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21461da177e4SLinus Torvalds {
21478ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21488ed67789SDaniel Lezcano 		.dev = dev,
21498ed67789SDaniel Lezcano 		.net = net,
21508ed67789SDaniel Lezcano 	};
21518ed67789SDaniel Lezcano 
21528ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21531e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
215695c96174SEric Dumazet struct rt6_mtu_change_arg {
21571da177e4SLinus Torvalds 	struct net_device *dev;
215895c96174SEric Dumazet 	unsigned int mtu;
21591da177e4SLinus Torvalds };
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21641da177e4SLinus Torvalds 	struct inet6_dev *idev;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21671da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21681da177e4SLinus Torvalds 	   We still use this lock to block changes
21691da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21701da177e4SLinus Torvalds 	*/
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
217338308473SDavid S. Miller 	if (!idev)
21741da177e4SLinus Torvalds 		return 0;
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21771da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21781da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21791da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21801da177e4SLinus Torvalds 	 */
21811da177e4SLinus Torvalds 	/*
21821da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21831da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21841da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21851da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21861da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21871da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21881da177e4SLinus Torvalds 	   PMTU discouvery.
21891da177e4SLinus Torvalds 	 */
2190d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2191d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2192d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2193d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2194d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2195defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2196566cfd8fSSimon Arlott 	}
21971da177e4SLinus Torvalds 	return 0;
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
220095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22011da177e4SLinus Torvalds {
2202c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2203c71099acSThomas Graf 		.dev = dev,
2204c71099acSThomas Graf 		.mtu = mtu,
2205c71099acSThomas Graf 	};
22061da177e4SLinus Torvalds 
2207c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
2210ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22115176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
221286872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2213ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
221486872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
221586872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
221651ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
221786872cb5SThomas Graf };
221886872cb5SThomas Graf 
221986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
222086872cb5SThomas Graf 			      struct fib6_config *cfg)
22211da177e4SLinus Torvalds {
222286872cb5SThomas Graf 	struct rtmsg *rtm;
222386872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
222486872cb5SThomas Graf 	int err;
22251da177e4SLinus Torvalds 
222686872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
222786872cb5SThomas Graf 	if (err < 0)
222886872cb5SThomas Graf 		goto errout;
22291da177e4SLinus Torvalds 
223086872cb5SThomas Graf 	err = -EINVAL;
223186872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
223286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
223386872cb5SThomas Graf 
223486872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
223586872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
223686872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
223786872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
223886872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2239ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
224086872cb5SThomas Graf 
2241ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2242ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2243b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2244b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
224586872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
224686872cb5SThomas Graf 
2247ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2248ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2249ab79ad14SMaciej Żenczykowski 
225015e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
225186872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22523b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
225386872cb5SThomas Graf 
225486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
225586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
225686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22571da177e4SLinus Torvalds 	}
225886872cb5SThomas Graf 
225986872cb5SThomas Graf 	if (tb[RTA_DST]) {
226086872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
226186872cb5SThomas Graf 
226286872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
226386872cb5SThomas Graf 			goto errout;
226486872cb5SThomas Graf 
226586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22661da177e4SLinus Torvalds 	}
226786872cb5SThomas Graf 
226886872cb5SThomas Graf 	if (tb[RTA_SRC]) {
226986872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
227086872cb5SThomas Graf 
227186872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
227286872cb5SThomas Graf 			goto errout;
227386872cb5SThomas Graf 
227486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22751da177e4SLinus Torvalds 	}
227686872cb5SThomas Graf 
2277c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2278c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2279c3968a85SDaniel Walter 
228086872cb5SThomas Graf 	if (tb[RTA_OIF])
228186872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
228286872cb5SThomas Graf 
228386872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
228486872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
228586872cb5SThomas Graf 
228686872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
228786872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
228886872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22891da177e4SLinus Torvalds 	}
229086872cb5SThomas Graf 
229186872cb5SThomas Graf 	if (tb[RTA_TABLE])
229286872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
229386872cb5SThomas Graf 
229451ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
229551ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
229651ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
229751ebd318SNicolas Dichtel 	}
229851ebd318SNicolas Dichtel 
229986872cb5SThomas Graf 	err = 0;
230086872cb5SThomas Graf errout:
230186872cb5SThomas Graf 	return err;
23021da177e4SLinus Torvalds }
23031da177e4SLinus Torvalds 
230451ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
230551ebd318SNicolas Dichtel {
230651ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
230751ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
230851ebd318SNicolas Dichtel 	int remaining;
230951ebd318SNicolas Dichtel 	int attrlen;
231051ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
231151ebd318SNicolas Dichtel 
231251ebd318SNicolas Dichtel beginning:
231351ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
231451ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
231551ebd318SNicolas Dichtel 
231651ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
231751ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
231851ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
231951ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
232051ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
232151ebd318SNicolas Dichtel 
232251ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
232351ebd318SNicolas Dichtel 		if (attrlen > 0) {
232451ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
232551ebd318SNicolas Dichtel 
232651ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
232751ebd318SNicolas Dichtel 			if (nla) {
232851ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
232951ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
233051ebd318SNicolas Dichtel 			}
233151ebd318SNicolas Dichtel 		}
233251ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
233351ebd318SNicolas Dichtel 		if (err) {
233451ebd318SNicolas Dichtel 			last_err = err;
233551ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
233651ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
233751ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
233851ebd318SNicolas Dichtel 			 */
233951ebd318SNicolas Dichtel 			if (add) {
234051ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
234151ebd318SNicolas Dichtel 				 * next hops that have been already added.
234251ebd318SNicolas Dichtel 				 */
234351ebd318SNicolas Dichtel 				add = 0;
234451ebd318SNicolas Dichtel 				goto beginning;
234551ebd318SNicolas Dichtel 			}
234651ebd318SNicolas Dichtel 		}
23471a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
23481a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
23491a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
23501a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
23511a72418bSNicolas Dichtel 		 */
23521a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
235351ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
235451ebd318SNicolas Dichtel 	}
235551ebd318SNicolas Dichtel 
235651ebd318SNicolas Dichtel 	return last_err;
235751ebd318SNicolas Dichtel }
235851ebd318SNicolas Dichtel 
2359c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23601da177e4SLinus Torvalds {
236186872cb5SThomas Graf 	struct fib6_config cfg;
236286872cb5SThomas Graf 	int err;
23631da177e4SLinus Torvalds 
236486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
236586872cb5SThomas Graf 	if (err < 0)
236686872cb5SThomas Graf 		return err;
236786872cb5SThomas Graf 
236851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
236951ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
237051ebd318SNicolas Dichtel 	else
237186872cb5SThomas Graf 		return ip6_route_del(&cfg);
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
2374c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23751da177e4SLinus Torvalds {
237686872cb5SThomas Graf 	struct fib6_config cfg;
237786872cb5SThomas Graf 	int err;
23781da177e4SLinus Torvalds 
237986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
238086872cb5SThomas Graf 	if (err < 0)
238186872cb5SThomas Graf 		return err;
238286872cb5SThomas Graf 
238351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
238451ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
238551ebd318SNicolas Dichtel 	else
238686872cb5SThomas Graf 		return ip6_route_add(&cfg);
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
2389339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2390339bf98fSThomas Graf {
2391339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2392339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2393339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2394339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2395339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2396339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2397339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2398339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2399339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24006a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2401339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2402339bf98fSThomas Graf }
2403339bf98fSThomas Graf 
2404191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2405191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24060d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
240715e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
24087bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	struct rtmsg *rtm;
24111da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2412e3703b3dSThomas Graf 	long expires;
24139e762a4aSPatrick McHardy 	u32 table;
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24161da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24171da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24181da177e4SLinus Torvalds 			return 1;
24191da177e4SLinus Torvalds 		}
24201da177e4SLinus Torvalds 	}
24211da177e4SLinus Torvalds 
242215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
242338308473SDavid S. Miller 	if (!nlh)
242426932566SPatrick McHardy 		return -EMSGSIZE;
24252d7202bfSThomas Graf 
24262d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24271da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24281da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24291da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24301da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2431c71099acSThomas Graf 	if (rt->rt6i_table)
24329e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2433c71099acSThomas Graf 	else
24349e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24359e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2436c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2437c78679e8SDavid S. Miller 		goto nla_put_failure;
2438ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2439ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2440ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2441ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2442ef2c7d7bSNicolas Dichtel 			break;
2443ef2c7d7bSNicolas Dichtel 		case -EACCES:
2444ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2445ef2c7d7bSNicolas Dichtel 			break;
2446b4949ab2SNicolas Dichtel 		case -EAGAIN:
2447b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2448b4949ab2SNicolas Dichtel 			break;
2449ef2c7d7bSNicolas Dichtel 		default:
24501da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2451ef2c7d7bSNicolas Dichtel 			break;
2452ef2c7d7bSNicolas Dichtel 		}
2453ef2c7d7bSNicolas Dichtel 	}
2454ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2455ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2456d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24571da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24581da177e4SLinus Torvalds 	else
24591da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24601da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24611da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24621da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24631da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24641da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2465f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2466f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
24671da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2468f0396f60SDenis Ovsienko 		else
2469f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2470f0396f60SDenis Ovsienko 	}
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24731da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	if (dst) {
2476c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2477c78679e8SDavid S. Miller 			goto nla_put_failure;
24781da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24791da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2480c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2481c78679e8SDavid S. Miller 			goto nla_put_failure;
24821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24831da177e4SLinus Torvalds 	if (src) {
2484c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2485c78679e8SDavid S. Miller 			goto nla_put_failure;
24861da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2487c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2488c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2489c78679e8SDavid S. Miller 		goto nla_put_failure;
24901da177e4SLinus Torvalds #endif
24917bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24927bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24937bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24948229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24957bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24967bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24977bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24987bc570c8SYOSHIFUJI Hideaki 						return 0;
24997bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25007bc570c8SYOSHIFUJI Hideaki 				} else {
25017bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25027bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25037bc570c8SYOSHIFUJI Hideaki 				}
25047bc570c8SYOSHIFUJI Hideaki 			}
25057bc570c8SYOSHIFUJI Hideaki 		} else
25067bc570c8SYOSHIFUJI Hideaki #endif
2507c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2508c78679e8SDavid S. Miller 				goto nla_put_failure;
25097bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25101da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2511c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2512c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2513c78679e8SDavid S. Miller 			goto nla_put_failure;
2514c3968a85SDaniel Walter 	}
2515c3968a85SDaniel Walter 
2516c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2517c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25184e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2519c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2520c78679e8SDavid S. Miller 			goto nla_put_failure;
25211da177e4SLinus Torvalds 	}
25222d7202bfSThomas Graf 
2523defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25242d7202bfSThomas Graf 		goto nla_put_failure;
25252d7202bfSThomas Graf 
2526dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2527dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
252894f826b8SEric Dumazet 			goto nla_put_failure;
252994f826b8SEric Dumazet 	}
25302d7202bfSThomas Graf 
2531c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2532c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2533c78679e8SDavid S. Miller 		goto nla_put_failure;
2534c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2535c78679e8SDavid S. Miller 		goto nla_put_failure;
25368253947eSLi Wei 
25378253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
253869cdf8f9SYOSHIFUJI Hideaki 
253987a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2540e3703b3dSThomas Graf 		goto nla_put_failure;
25411da177e4SLinus Torvalds 
25422d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25432d7202bfSThomas Graf 
25442d7202bfSThomas Graf nla_put_failure:
254526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
254626932566SPatrick McHardy 	return -EMSGSIZE;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25501da177e4SLinus Torvalds {
25511da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25521da177e4SLinus Torvalds 	int prefix;
25531da177e4SLinus Torvalds 
25542d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25552d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25561da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25571da177e4SLinus Torvalds 	} else
25581da177e4SLinus Torvalds 		prefix = 0;
25591da177e4SLinus Torvalds 
2560191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2561191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
256215e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
25637bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
2566c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25671da177e4SLinus Torvalds {
25683b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2569ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25701da177e4SLinus Torvalds 	struct rt6_info *rt;
2571ab364a6fSThomas Graf 	struct sk_buff *skb;
2572ab364a6fSThomas Graf 	struct rtmsg *rtm;
25734c9483b2SDavid S. Miller 	struct flowi6 fl6;
257472331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2575ab364a6fSThomas Graf 
2576ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2577ab364a6fSThomas Graf 	if (err < 0)
2578ab364a6fSThomas Graf 		goto errout;
2579ab364a6fSThomas Graf 
2580ab364a6fSThomas Graf 	err = -EINVAL;
25814c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2582ab364a6fSThomas Graf 
2583ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2584ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2585ab364a6fSThomas Graf 			goto errout;
2586ab364a6fSThomas Graf 
25874e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2588ab364a6fSThomas Graf 	}
2589ab364a6fSThomas Graf 
2590ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2591ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2592ab364a6fSThomas Graf 			goto errout;
2593ab364a6fSThomas Graf 
25944e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2595ab364a6fSThomas Graf 	}
2596ab364a6fSThomas Graf 
2597ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2598ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2599ab364a6fSThomas Graf 
2600ab364a6fSThomas Graf 	if (tb[RTA_OIF])
260172331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2602ab364a6fSThomas Graf 
2603ab364a6fSThomas Graf 	if (iif) {
2604ab364a6fSThomas Graf 		struct net_device *dev;
260572331bc0SShmulik Ladkani 		int flags = 0;
260672331bc0SShmulik Ladkani 
26075578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2608ab364a6fSThomas Graf 		if (!dev) {
2609ab364a6fSThomas Graf 			err = -ENODEV;
2610ab364a6fSThomas Graf 			goto errout;
2611ab364a6fSThomas Graf 		}
261272331bc0SShmulik Ladkani 
261372331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
261472331bc0SShmulik Ladkani 
261572331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
261672331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
261772331bc0SShmulik Ladkani 
261872331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
261972331bc0SShmulik Ladkani 							       flags);
262072331bc0SShmulik Ladkani 	} else {
262172331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
262272331bc0SShmulik Ladkani 
262372331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2624ab364a6fSThomas Graf 	}
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
262738308473SDavid S. Miller 	if (!skb) {
262894e187c0SAmerigo Wang 		ip6_rt_put(rt);
2629ab364a6fSThomas Graf 		err = -ENOBUFS;
2630ab364a6fSThomas Graf 		goto errout;
2631ab364a6fSThomas Graf 	}
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26341da177e4SLinus Torvalds 	   through good chunk of routing engine.
26351da177e4SLinus Torvalds 	 */
2636459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26371da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26381da177e4SLinus Torvalds 
2639d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26401da177e4SLinus Torvalds 
26414c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
264215e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
26437bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26441da177e4SLinus Torvalds 	if (err < 0) {
2645ab364a6fSThomas Graf 		kfree_skb(skb);
2646ab364a6fSThomas Graf 		goto errout;
26471da177e4SLinus Torvalds 	}
26481da177e4SLinus Torvalds 
264915e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2650ab364a6fSThomas Graf errout:
26511da177e4SLinus Torvalds 	return err;
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
265486872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26551da177e4SLinus Torvalds {
26561da177e4SLinus Torvalds 	struct sk_buff *skb;
26575578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2658528c4cebSDenis V. Lunev 	u32 seq;
2659528c4cebSDenis V. Lunev 	int err;
26600d51aa80SJamal Hadi Salim 
2661528c4cebSDenis V. Lunev 	err = -ENOBUFS;
266238308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
266386872cb5SThomas Graf 
2664339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
266538308473SDavid S. Miller 	if (!skb)
266621713ebcSThomas Graf 		goto errout;
26671da177e4SLinus Torvalds 
2668191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
266915e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
267026932566SPatrick McHardy 	if (err < 0) {
267126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
267226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
267326932566SPatrick McHardy 		kfree_skb(skb);
267426932566SPatrick McHardy 		goto errout;
267526932566SPatrick McHardy 	}
267615e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
26775578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26781ce85fe4SPablo Neira Ayuso 	return;
267921713ebcSThomas Graf errout:
268021713ebcSThomas Graf 	if (err < 0)
26815578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26848ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26858ed67789SDaniel Lezcano 				unsigned long event, void *data)
26868ed67789SDaniel Lezcano {
26878ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2688c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26898ed67789SDaniel Lezcano 
26908ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2691d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26928ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26938ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2694d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26958ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2696d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26978ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26988ed67789SDaniel Lezcano #endif
26998ed67789SDaniel Lezcano 	}
27008ed67789SDaniel Lezcano 
27018ed67789SDaniel Lezcano 	return NOTIFY_OK;
27028ed67789SDaniel Lezcano }
27038ed67789SDaniel Lezcano 
27041da177e4SLinus Torvalds /*
27051da177e4SLinus Torvalds  *	/proc
27061da177e4SLinus Torvalds  */
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds struct rt6_proc_arg
27111da177e4SLinus Torvalds {
27121da177e4SLinus Torvalds 	char *buffer;
27131da177e4SLinus Torvalds 	int offset;
27141da177e4SLinus Torvalds 	int length;
27151da177e4SLinus Torvalds 	int skip;
27161da177e4SLinus Torvalds 	int len;
27171da177e4SLinus Torvalds };
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27201da177e4SLinus Torvalds {
272133120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
27221da177e4SLinus Torvalds 
27234b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27264b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27271da177e4SLinus Torvalds #else
272833120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27291da177e4SLinus Torvalds #endif
2730dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2731dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		seq_printf(m, "%pi6", &rt->rt6i_gateway);
27321da177e4SLinus Torvalds 	} else {
273333120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27341da177e4SLinus Torvalds 	}
273533120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2736d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2737d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2738d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27391da177e4SLinus Torvalds 	return 0;
27401da177e4SLinus Torvalds }
27411da177e4SLinus Torvalds 
274233120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27431da177e4SLinus Torvalds {
2744f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
274532b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
274633120b30SAlexey Dobriyan 	return 0;
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
274933120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
275033120b30SAlexey Dobriyan {
2751de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2752f3db4851SDaniel Lezcano }
2753f3db4851SDaniel Lezcano 
275433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
275533120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
275633120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
275733120b30SAlexey Dobriyan 	.read		= seq_read,
275833120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2759b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
276033120b30SAlexey Dobriyan };
276133120b30SAlexey Dobriyan 
27621da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27631da177e4SLinus Torvalds {
276469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27651da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
276669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
276769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
276869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
276969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
277069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2771fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
277269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	return 0;
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27781da177e4SLinus Torvalds {
2779de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
278069ddb805SDaniel Lezcano }
278169ddb805SDaniel Lezcano 
27829a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27831da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27841da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27851da177e4SLinus Torvalds 	.read	 = seq_read,
27861da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2787b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27881da177e4SLinus Torvalds };
27891da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds static
27948d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27951da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27961da177e4SLinus Torvalds {
2797c486da34SLucian Adrian Grijincu 	struct net *net;
2798c486da34SLucian Adrian Grijincu 	int delay;
2799c486da34SLucian Adrian Grijincu 	if (!write)
2800c486da34SLucian Adrian Grijincu 		return -EINVAL;
2801c486da34SLucian Adrian Grijincu 
2802c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2803c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28048d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28055b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28061da177e4SLinus Torvalds 	return 0;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
2809760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
28101da177e4SLinus Torvalds 	{
28111da177e4SLinus Torvalds 		.procname	=	"flush",
28124990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
281489c8b3a1SDave Jones 		.mode		=	0200,
28156d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28161da177e4SLinus Torvalds 	},
28171da177e4SLinus Torvalds 	{
28181da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28199a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28211da177e4SLinus Torvalds 		.mode		=	0644,
28226d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28231da177e4SLinus Torvalds 	},
28241da177e4SLinus Torvalds 	{
28251da177e4SLinus Torvalds 		.procname	=	"max_size",
28264990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28271da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28281da177e4SLinus Torvalds 		.mode		=	0644,
28296d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28301da177e4SLinus Torvalds 	},
28311da177e4SLinus Torvalds 	{
28321da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28351da177e4SLinus Torvalds 		.mode		=	0644,
28366d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28371da177e4SLinus Torvalds 	},
28381da177e4SLinus Torvalds 	{
28391da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28404990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28411da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28421da177e4SLinus Torvalds 		.mode		=	0644,
28436d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28441da177e4SLinus Torvalds 	},
28451da177e4SLinus Torvalds 	{
28461da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28474990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28481da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28491da177e4SLinus Torvalds 		.mode		=	0644,
28506d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28511da177e4SLinus Torvalds 	},
28521da177e4SLinus Torvalds 	{
28531da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28544990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28551da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28561da177e4SLinus Torvalds 		.mode		=	0644,
2857f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28581da177e4SLinus Torvalds 	},
28591da177e4SLinus Torvalds 	{
28601da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28614990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28621da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28631da177e4SLinus Torvalds 		.mode		=	0644,
28646d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28651da177e4SLinus Torvalds 	},
28661da177e4SLinus Torvalds 	{
28671da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28684990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28691da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28701da177e4SLinus Torvalds 		.mode		=	0644,
2871f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28721da177e4SLinus Torvalds 	},
28731da177e4SLinus Torvalds 	{
28741da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28754990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28761da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28771da177e4SLinus Torvalds 		.mode		=	0644,
28786d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28791da177e4SLinus Torvalds 	},
2880f8572d8fSEric W. Biederman 	{ }
28811da177e4SLinus Torvalds };
28821da177e4SLinus Torvalds 
28832c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2884760f2d01SDaniel Lezcano {
2885760f2d01SDaniel Lezcano 	struct ctl_table *table;
2886760f2d01SDaniel Lezcano 
2887760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2888760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2889760f2d01SDaniel Lezcano 			GFP_KERNEL);
28905ee09105SYOSHIFUJI Hideaki 
28915ee09105SYOSHIFUJI Hideaki 	if (table) {
28925ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2893c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
289486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28955ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28965ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28975ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28985ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28995ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29005ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29015ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29029c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2903464dc801SEric W. Biederman 
2904464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2905464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2906464dc801SEric W. Biederman 			table[0].procname = NULL;
29075ee09105SYOSHIFUJI Hideaki 	}
29085ee09105SYOSHIFUJI Hideaki 
2909760f2d01SDaniel Lezcano 	return table;
2910760f2d01SDaniel Lezcano }
29111da177e4SLinus Torvalds #endif
29121da177e4SLinus Torvalds 
29132c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2914cdb18761SDaniel Lezcano {
2915633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29168ed67789SDaniel Lezcano 
291786393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
291886393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2919f2fc6a54SBenjamin Thery 
2920fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2921fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2922fc66f95cSEric Dumazet 
29238ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29248ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29258ed67789SDaniel Lezcano 					   GFP_KERNEL);
29268ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2927fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2928d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29298ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2930d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
293162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
293262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29338ed67789SDaniel Lezcano 
29348ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29358ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29368ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29378ed67789SDaniel Lezcano 					       GFP_KERNEL);
293868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
293968fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2940d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29418ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2942d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
294362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
294462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29458ed67789SDaniel Lezcano 
29468ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29478ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29488ed67789SDaniel Lezcano 					       GFP_KERNEL);
294968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
295068fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2951d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29528ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2953d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
295462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
295562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29568ed67789SDaniel Lezcano #endif
29578ed67789SDaniel Lezcano 
2958b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2959b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2960b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2961b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2962b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2963b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2964b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2965b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2966b339a47cSPeter Zijlstra 
29676891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29686891a346SBenjamin Thery 
29698ed67789SDaniel Lezcano 	ret = 0;
29708ed67789SDaniel Lezcano out:
29718ed67789SDaniel Lezcano 	return ret;
2972f2fc6a54SBenjamin Thery 
297368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
297468fffc67SPeter Zijlstra out_ip6_prohibit_entry:
297568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
297668fffc67SPeter Zijlstra out_ip6_null_entry:
297768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
297868fffc67SPeter Zijlstra #endif
2979fc66f95cSEric Dumazet out_ip6_dst_entries:
2980fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2981f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2982f2fc6a54SBenjamin Thery 	goto out;
2983cdb18761SDaniel Lezcano }
2984cdb18761SDaniel Lezcano 
29852c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2986cdb18761SDaniel Lezcano {
29878ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29888ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29898ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29908ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29918ed67789SDaniel Lezcano #endif
299241bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2993cdb18761SDaniel Lezcano }
2994cdb18761SDaniel Lezcano 
2995d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2996d189634eSThomas Graf {
2997d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2998d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
2999d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3000d189634eSThomas Graf #endif
3001d189634eSThomas Graf 	return 0;
3002d189634eSThomas Graf }
3003d189634eSThomas Graf 
3004d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3005d189634eSThomas Graf {
3006d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3007*ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3008*ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3009d189634eSThomas Graf #endif
3010d189634eSThomas Graf }
3011d189634eSThomas Graf 
3012cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3013cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3014cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3015cdb18761SDaniel Lezcano };
3016cdb18761SDaniel Lezcano 
3017c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3018c3426b47SDavid S. Miller {
3019c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3020c3426b47SDavid S. Miller 
3021c3426b47SDavid S. Miller 	if (!bp)
3022c3426b47SDavid S. Miller 		return -ENOMEM;
3023c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3024c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3025c3426b47SDavid S. Miller 	return 0;
3026c3426b47SDavid S. Miller }
3027c3426b47SDavid S. Miller 
3028c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3029c3426b47SDavid S. Miller {
3030c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3031c3426b47SDavid S. Miller 
3032c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
303356a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3034c3426b47SDavid S. Miller 	kfree(bp);
3035c3426b47SDavid S. Miller }
3036c3426b47SDavid S. Miller 
30372b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3038c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3039c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3040c3426b47SDavid S. Miller };
3041c3426b47SDavid S. Miller 
3042d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3043d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3044d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3045d189634eSThomas Graf };
3046d189634eSThomas Graf 
30478ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30488ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30498ed67789SDaniel Lezcano 	.priority = 0,
30508ed67789SDaniel Lezcano };
30518ed67789SDaniel Lezcano 
3052433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30531da177e4SLinus Torvalds {
3054433d49c3SDaniel Lezcano 	int ret;
3055433d49c3SDaniel Lezcano 
30569a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30579a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30589a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30599a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30609a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3061c19a28e1SFernando Carrijo 		goto out;
306214e50e57SDavid S. Miller 
3063fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30648ed67789SDaniel Lezcano 	if (ret)
3065bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3066bdb3289fSDaniel Lezcano 
3067c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3068c3426b47SDavid S. Miller 	if (ret)
3069e8803b6cSDavid S. Miller 		goto out_dst_entries;
30702a0c451aSThomas Graf 
30717e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30727e52b33bSDavid S. Miller 	if (ret)
30737e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3074c3426b47SDavid S. Miller 
30755dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30765dc121e9SArnaud Ebalard 
30778ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30788ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30798ed67789SDaniel Lezcano 	 * manually for init_net */
3080d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30818ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3082bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3083d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30848ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3085d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30868ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3087bdb3289fSDaniel Lezcano   #endif
3088e8803b6cSDavid S. Miller 	ret = fib6_init();
3089433d49c3SDaniel Lezcano 	if (ret)
30908ed67789SDaniel Lezcano 		goto out_register_subsys;
3091433d49c3SDaniel Lezcano 
3092433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3093433d49c3SDaniel Lezcano 	if (ret)
3094e8803b6cSDavid S. Miller 		goto out_fib6_init;
3095c35b7e72SDaniel Lezcano 
3096433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3097433d49c3SDaniel Lezcano 	if (ret)
3098433d49c3SDaniel Lezcano 		goto xfrm6_init;
30997e5449c2SDaniel Lezcano 
3100d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3101d189634eSThomas Graf 	if (ret)
3102d189634eSThomas Graf 		goto fib6_rules_init;
3103d189634eSThomas Graf 
3104433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3105c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3106c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3107c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3108d189634eSThomas Graf 		goto out_register_late_subsys;
3109433d49c3SDaniel Lezcano 
31108ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3111cdb18761SDaniel Lezcano 	if (ret)
3112d189634eSThomas Graf 		goto out_register_late_subsys;
31138ed67789SDaniel Lezcano 
3114433d49c3SDaniel Lezcano out:
3115433d49c3SDaniel Lezcano 	return ret;
3116433d49c3SDaniel Lezcano 
3117d189634eSThomas Graf out_register_late_subsys:
3118d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3119433d49c3SDaniel Lezcano fib6_rules_init:
3120433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3121433d49c3SDaniel Lezcano xfrm6_init:
3122433d49c3SDaniel Lezcano 	xfrm6_fini();
31232a0c451aSThomas Graf out_fib6_init:
31242a0c451aSThomas Graf 	fib6_gc_cleanup();
31258ed67789SDaniel Lezcano out_register_subsys:
31268ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31277e52b33bSDavid S. Miller out_register_inetpeer:
31287e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3129fc66f95cSEric Dumazet out_dst_entries:
3130fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3131433d49c3SDaniel Lezcano out_kmem_cache:
3132f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3133433d49c3SDaniel Lezcano 	goto out;
31341da177e4SLinus Torvalds }
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds void ip6_route_cleanup(void)
31371da177e4SLinus Torvalds {
31388ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3139d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3140101367c2SThomas Graf 	fib6_rules_cleanup();
31411da177e4SLinus Torvalds 	xfrm6_fini();
31421da177e4SLinus Torvalds 	fib6_gc_cleanup();
3143c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31448ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
314541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3146f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31471da177e4SLinus Torvalds }
3148