xref: /openbmc/linux/net/ipv6/route.c (revision f894cbf847c9bea1955095bf37aca6c050553167)
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>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <asm/uaccess.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
641da177e4SLinus Torvalds #include <linux/sysctl.h>
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6821efcfa0SEric Dumazet 				    const struct in6_addr *dest);
691da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
700dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
71ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
721da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
731da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
751da177e4SLinus Torvalds 				       struct net_device *dev, int how);
76569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
791da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
801da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
821da177e4SLinus Torvalds 
8370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
84efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
85b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
86b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
8795c96174SEric Dumazet 					   unsigned int pref);
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9170ceb4f5SYOSHIFUJI Hideaki #endif
9270ceb4f5SYOSHIFUJI Hideaki 
9306582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9406582540SDavid S. Miller {
9506582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
9606582540SDavid S. Miller 	struct inet_peer *peer;
9706582540SDavid S. Miller 	u32 *p = NULL;
9806582540SDavid S. Miller 
998e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1008e2ec639SYan, Zheng 		return NULL;
1018e2ec639SYan, Zheng 
102fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10306582540SDavid S. Miller 	if (peer) {
10406582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10506582540SDavid S. Miller 		unsigned long prev, new;
10606582540SDavid S. Miller 
10706582540SDavid S. Miller 		p = peer->metrics;
10806582540SDavid S. Miller 		if (inet_metrics_new(peer))
10906582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11006582540SDavid S. Miller 
11106582540SDavid S. Miller 		new = (unsigned long) p;
11206582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11306582540SDavid S. Miller 
11406582540SDavid S. Miller 		if (prev != old) {
11506582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
11606582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
11706582540SDavid S. Miller 				p = NULL;
11806582540SDavid S. Miller 		}
11906582540SDavid S. Miller 	}
12006582540SDavid S. Miller 	return p;
12106582540SDavid S. Miller }
12206582540SDavid S. Miller 
123*f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
124*f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
125*f894cbf8SDavid S. Miller 					     const void *daddr)
12639232973SDavid S. Miller {
12739232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
12839232973SDavid S. Miller 
129a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13039232973SDavid S. Miller 		return (const void *) p;
131*f894cbf8SDavid S. Miller 	else if (skb)
132*f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13339232973SDavid S. Miller 	return daddr;
13439232973SDavid S. Miller }
13539232973SDavid S. Miller 
136*f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
137*f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
138*f894cbf8SDavid S. Miller 					  const void *daddr)
139d3aaeb38SDavid S. Miller {
14039232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14139232973SDavid S. Miller 	struct neighbour *n;
14239232973SDavid S. Miller 
143*f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
14439232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
145f83c7790SDavid S. Miller 	if (n)
146f83c7790SDavid S. Miller 		return n;
147f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
148f83c7790SDavid S. Miller }
149f83c7790SDavid S. Miller 
1508ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
151f83c7790SDavid S. Miller {
1528ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1538ade06c6SDavid S. Miller 	if (!n) {
1548ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
155f83c7790SDavid S. Miller 		if (IS_ERR(n))
156f83c7790SDavid S. Miller 			return PTR_ERR(n);
1578ade06c6SDavid S. Miller 	}
158f83c7790SDavid S. Miller 	dst_set_neighbour(&rt->dst, n);
159f83c7790SDavid S. Miller 
160f83c7790SDavid S. Miller 	return 0;
161d3aaeb38SDavid S. Miller }
162d3aaeb38SDavid S. Miller 
1639a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1641da177e4SLinus Torvalds 	.family			=	AF_INET6,
16509640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1661da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1671da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1681da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1690dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
170ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
17106582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1721da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1731da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1741da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1751da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1761da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1771ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
178d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1791da177e4SLinus Torvalds };
1801da177e4SLinus Torvalds 
181ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
182ec831ea7SRoland Dreier {
183618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
184618f9bc7SSteffen Klassert 
185618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
186ec831ea7SRoland Dreier }
187ec831ea7SRoland Dreier 
18814e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
18914e50e57SDavid S. Miller {
19014e50e57SDavid S. Miller }
19114e50e57SDavid S. Miller 
1920972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1930972ddb2SHeld Bernhard 					 unsigned long old)
1940972ddb2SHeld Bernhard {
1950972ddb2SHeld Bernhard 	return NULL;
1960972ddb2SHeld Bernhard }
1970972ddb2SHeld Bernhard 
19814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19914e50e57SDavid S. Miller 	.family			=	AF_INET6,
20009640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20114e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20214e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
203ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
204214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20514e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
2060972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
207d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20814e50e57SDavid S. Miller };
20914e50e57SDavid S. Miller 
21062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21162fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
21262fa8a84SDavid S. Miller };
21362fa8a84SDavid S. Miller 
214bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
2151da177e4SLinus Torvalds 	.dst = {
2161da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2171da177e4SLinus Torvalds 		.__use		= 1,
2181da177e4SLinus Torvalds 		.obsolete	= -1,
2191da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2201da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2211da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2221da177e4SLinus Torvalds 	},
2231da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2244f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2251da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2261da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2271da177e4SLinus Torvalds };
2281da177e4SLinus Torvalds 
229101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
230101367c2SThomas Graf 
2316723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2326723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2336723ab54SDavid S. Miller 
234280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
235101367c2SThomas Graf 	.dst = {
236101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
237101367c2SThomas Graf 		.__use		= 1,
238101367c2SThomas Graf 		.obsolete	= -1,
239101367c2SThomas Graf 		.error		= -EACCES,
2409ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2419ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
242101367c2SThomas Graf 	},
243101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2444f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
245101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
246101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
247101367c2SThomas Graf };
248101367c2SThomas Graf 
249bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
250101367c2SThomas Graf 	.dst = {
251101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
252101367c2SThomas Graf 		.__use		= 1,
253101367c2SThomas Graf 		.obsolete	= -1,
254101367c2SThomas Graf 		.error		= -EINVAL,
255352e512cSHerbert Xu 		.input		= dst_discard,
256352e512cSHerbert Xu 		.output		= dst_discard,
257101367c2SThomas Graf 	},
258101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2594f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
260101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
261101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
262101367c2SThomas Graf };
263101367c2SThomas Graf 
264101367c2SThomas Graf #endif
265101367c2SThomas Graf 
2661da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26797bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
268957c665fSDavid S. Miller 					     struct net_device *dev,
2698b96d22dSDavid S. Miller 					     int flags,
2708b96d22dSDavid S. Miller 					     struct fib6_table *table)
2711da177e4SLinus Torvalds {
27297bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
27397bab73fSDavid S. Miller 					0, 0, flags);
274cf911662SDavid S. Miller 
27597bab73fSDavid S. Miller 	if (rt) {
276fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
277fbe58186SMadalin Bucur 		       sizeof(*rt) - sizeof(struct dst_entry));
2788b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
27997bab73fSDavid S. Miller 	}
280cf911662SDavid S. Miller 	return rt;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2861da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2871da177e4SLinus Torvalds 
2888e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2898e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2908e2ec639SYan, Zheng 
29138308473SDavid S. Miller 	if (idev) {
2921da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2931da177e4SLinus Torvalds 		in6_dev_put(idev);
2941da177e4SLinus Torvalds 	}
2951716a961SGao feng 
2961716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
2971716a961SGao feng 		dst_release(dst->from);
2981716a961SGao feng 
29997bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
30097bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
301b3419363SDavid S. Miller 		inet_putpeer(peer);
302b3419363SDavid S. Miller 	}
303b3419363SDavid S. Miller }
304b3419363SDavid S. Miller 
3056431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3066431cbc2SDavid S. Miller 
3076431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3086431cbc2SDavid S. Miller {
3096431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3106431cbc2SDavid S. Miller }
3116431cbc2SDavid S. Miller 
312b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
313b3419363SDavid S. Miller {
31497bab73fSDavid S. Miller 	struct inet_peer_base *base;
315b3419363SDavid S. Miller 	struct inet_peer *peer;
316b3419363SDavid S. Miller 
31797bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
31897bab73fSDavid S. Miller 	if (!base)
31997bab73fSDavid S. Miller 		return;
32097bab73fSDavid S. Miller 
32197bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3227b34ca2aSDavid S. Miller 	if (peer) {
32397bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
324b3419363SDavid S. Miller 			inet_putpeer(peer);
3256431cbc2SDavid S. Miller 		else
3266431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
3271da177e4SLinus Torvalds 	}
3287b34ca2aSDavid S. Miller }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3311da177e4SLinus Torvalds 			   int how)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3341da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3355a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
336c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3371da177e4SLinus Torvalds 
33838308473SDavid S. Miller 	if (dev != loopback_dev && idev && idev->dev == dev) {
3395a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3405a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
34138308473SDavid S. Miller 		if (loopback_idev) {
3421da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3431da177e4SLinus Torvalds 			in6_dev_put(idev);
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3491da177e4SLinus Torvalds {
3501716a961SGao feng 	struct rt6_info *ort = NULL;
3511716a961SGao feng 
3521716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3531716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
354a50feda5SEric Dumazet 			return true;
3551716a961SGao feng 	} else if (rt->dst.from) {
3561716a961SGao feng 		ort = (struct rt6_info *) rt->dst.from;
3571716a961SGao feng 		return (ort->rt6i_flags & RTF_EXPIRES) &&
3581716a961SGao feng 			time_after(jiffies, ort->dst.expires);
3591716a961SGao feng 	}
360a50feda5SEric Dumazet 	return false;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
364c71099acSThomas Graf {
365a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
366a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
367c71099acSThomas Graf }
368c71099acSThomas Graf 
3691da177e4SLinus Torvalds /*
370c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3711da177e4SLinus Torvalds  */
3721da177e4SLinus Torvalds 
3738ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3748ed67789SDaniel Lezcano 						    struct rt6_info *rt,
375b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3761da177e4SLinus Torvalds 						    int oif,
377d420895eSYOSHIFUJI Hideaki 						    int flags)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3801da177e4SLinus Torvalds 	struct rt6_info *sprt;
3811da177e4SLinus Torvalds 
382dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
383dd3abc4eSYOSHIFUJI Hideaki 		goto out;
384dd3abc4eSYOSHIFUJI Hideaki 
385d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
386d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
387dd3abc4eSYOSHIFUJI Hideaki 
388dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3891da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3901da177e4SLinus Torvalds 				return sprt;
3911da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
39238308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
3931da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
394d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3951da177e4SLinus Torvalds 						continue;
3961da177e4SLinus Torvalds 					if (local && (!oif ||
3971da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3981da177e4SLinus Torvalds 						continue;
3991da177e4SLinus Torvalds 				}
4001da177e4SLinus Torvalds 				local = sprt;
4011da177e4SLinus Torvalds 			}
402dd3abc4eSYOSHIFUJI Hideaki 		} else {
403dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
404dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
405dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
406dd3abc4eSYOSHIFUJI Hideaki 		}
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
409dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4101da177e4SLinus Torvalds 		if (local)
4111da177e4SLinus Torvalds 			return local;
4121da177e4SLinus Torvalds 
413d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4148ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4151da177e4SLinus Torvalds 	}
416dd3abc4eSYOSHIFUJI Hideaki out:
4171da177e4SLinus Torvalds 	return rt;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
42027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
42127097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
42227097255SYOSHIFUJI Hideaki {
423f2c31e32SEric Dumazet 	struct neighbour *neigh;
42427097255SYOSHIFUJI Hideaki 	/*
42527097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
42627097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
42727097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
42827097255SYOSHIFUJI Hideaki 	 *
42927097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
43027097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
43127097255SYOSHIFUJI Hideaki 	 */
432f2c31e32SEric Dumazet 	rcu_read_lock();
43327217455SDavid Miller 	neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL;
43427097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
435f2c31e32SEric Dumazet 		goto out;
43627097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
43727097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
43852e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
43927097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
44027097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
44127097255SYOSHIFUJI Hideaki 
44227097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
44327097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
44427097255SYOSHIFUJI Hideaki 
44527097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
44627097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
447d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
448f2c31e32SEric Dumazet 	} else {
44927097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
45027097255SYOSHIFUJI Hideaki 	}
451f2c31e32SEric Dumazet out:
452f2c31e32SEric Dumazet 	rcu_read_unlock();
453f2c31e32SEric Dumazet }
45427097255SYOSHIFUJI Hideaki #else
45527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
45627097255SYOSHIFUJI Hideaki {
45727097255SYOSHIFUJI Hideaki }
45827097255SYOSHIFUJI Hideaki #endif
45927097255SYOSHIFUJI Hideaki 
4601da177e4SLinus Torvalds /*
461554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4621da177e4SLinus Torvalds  */
463b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4641da177e4SLinus Torvalds {
465d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
466161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
467554cfb7eSYOSHIFUJI Hideaki 		return 2;
468161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
469161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
470161980f4SDavid S. Miller 		return 1;
471554cfb7eSYOSHIFUJI Hideaki 	return 0;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
474b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4751da177e4SLinus Torvalds {
476f2c31e32SEric Dumazet 	struct neighbour *neigh;
477398bcbebSYOSHIFUJI Hideaki 	int m;
478f2c31e32SEric Dumazet 
479f2c31e32SEric Dumazet 	rcu_read_lock();
48027217455SDavid Miller 	neigh = dst_get_neighbour_noref(&rt->dst);
4814d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4824d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4834d0c5911SYOSHIFUJI Hideaki 		m = 1;
4844d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4851da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
486554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4874d0c5911SYOSHIFUJI Hideaki 			m = 2;
488398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
489398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
490398bcbebSYOSHIFUJI Hideaki 			m = 0;
491398bcbebSYOSHIFUJI Hideaki #endif
492398bcbebSYOSHIFUJI Hideaki 		else
493ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4941da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
495398bcbebSYOSHIFUJI Hideaki 	} else
496398bcbebSYOSHIFUJI Hideaki 		m = 0;
497f2c31e32SEric Dumazet 	rcu_read_unlock();
498554cfb7eSYOSHIFUJI Hideaki 	return m;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
501554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
502554cfb7eSYOSHIFUJI Hideaki 			   int strict)
503554cfb7eSYOSHIFUJI Hideaki {
5044d0c5911SYOSHIFUJI Hideaki 	int m, n;
5054d0c5911SYOSHIFUJI Hideaki 
5064d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
50777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
508554cfb7eSYOSHIFUJI Hideaki 		return -1;
509ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
510ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
511ebacaaa0SYOSHIFUJI Hideaki #endif
5124d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
513557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
514554cfb7eSYOSHIFUJI Hideaki 		return -1;
515554cfb7eSYOSHIFUJI Hideaki 	return m;
516554cfb7eSYOSHIFUJI Hideaki }
517554cfb7eSYOSHIFUJI Hideaki 
518f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
519f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
520554cfb7eSYOSHIFUJI Hideaki {
521554cfb7eSYOSHIFUJI Hideaki 	int m;
522554cfb7eSYOSHIFUJI Hideaki 
523554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
524f11e6659SDavid S. Miller 		goto out;
525554cfb7eSYOSHIFUJI Hideaki 
526554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
527554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
528f11e6659SDavid S. Miller 		goto out;
529554cfb7eSYOSHIFUJI Hideaki 
530f11e6659SDavid S. Miller 	if (m > *mpri) {
531ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
53227097255SYOSHIFUJI Hideaki 			rt6_probe(match);
533f11e6659SDavid S. Miller 		*mpri = m;
534554cfb7eSYOSHIFUJI Hideaki 		match = rt;
535ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
53627097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5371da177e4SLinus Torvalds 	}
538f11e6659SDavid S. Miller 
539f11e6659SDavid S. Miller out:
540f11e6659SDavid S. Miller 	return match;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
544f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
545f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
546f11e6659SDavid S. Miller {
547f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
548f11e6659SDavid S. Miller 	int mpri = -1;
549f11e6659SDavid S. Miller 
550f11e6659SDavid S. Miller 	match = NULL;
551f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
552d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
553f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
554f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
555d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
556f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
557f11e6659SDavid S. Miller 
558f11e6659SDavid S. Miller 	return match;
559f11e6659SDavid S. Miller }
560f11e6659SDavid S. Miller 
561f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
562f11e6659SDavid S. Miller {
563f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5648ed67789SDaniel Lezcano 	struct net *net;
565f11e6659SDavid S. Miller 
566f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
567f11e6659SDavid S. Miller 	if (!rt0)
568f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
569f11e6659SDavid S. Miller 
570f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
571f11e6659SDavid S. Miller 
572554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
573f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
574d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
575f11e6659SDavid S. Miller 
576554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
577f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
578f11e6659SDavid S. Miller 			next = fn->leaf;
579f11e6659SDavid S. Miller 
580f11e6659SDavid S. Miller 		if (next != rt0)
581f11e6659SDavid S. Miller 			fn->rr_ptr = next;
582554cfb7eSYOSHIFUJI Hideaki 	}
583554cfb7eSYOSHIFUJI Hideaki 
584d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
585a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
58870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
58970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
590b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
59170ceb4f5SYOSHIFUJI Hideaki {
592c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
59370ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
59470ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
59570ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5964bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
59770ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
59870ceb4f5SYOSHIFUJI Hideaki 
59970ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
60070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
60170ceb4f5SYOSHIFUJI Hideaki 	}
60270ceb4f5SYOSHIFUJI Hideaki 
60370ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
60470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
60570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
60670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
60770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
60870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
60970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
61070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
61170ceb4f5SYOSHIFUJI Hideaki 		}
61270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
61370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
61470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
61570ceb4f5SYOSHIFUJI Hideaki 		}
61670ceb4f5SYOSHIFUJI Hideaki 	}
61770ceb4f5SYOSHIFUJI Hideaki 
61870ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
61970ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6203933fc95SJens Rosenboom 		return -EINVAL;
62170ceb4f5SYOSHIFUJI Hideaki 
6224bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
62370ceb4f5SYOSHIFUJI Hideaki 
62470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
62570ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
62670ceb4f5SYOSHIFUJI Hideaki 	else {
62770ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
62870ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
62970ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
63070ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
63170ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
63270ceb4f5SYOSHIFUJI Hideaki 	}
63370ceb4f5SYOSHIFUJI Hideaki 
634efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
635efa2cea0SDaniel Lezcano 				dev->ifindex);
63670ceb4f5SYOSHIFUJI Hideaki 
63770ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
638e0a1ad73SThomas Graf 		ip6_del_rt(rt);
63970ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
64070ceb4f5SYOSHIFUJI Hideaki 	}
64170ceb4f5SYOSHIFUJI Hideaki 
64270ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
643efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
64470ceb4f5SYOSHIFUJI Hideaki 					pref);
64570ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
64670ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
64770ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
64870ceb4f5SYOSHIFUJI Hideaki 
64970ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6501716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
6511716a961SGao feng 			rt6_clean_expires(rt);
6521716a961SGao feng 		else
6531716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
6541716a961SGao feng 
655d8d1f30bSChangli Gao 		dst_release(&rt->dst);
65670ceb4f5SYOSHIFUJI Hideaki 	}
65770ceb4f5SYOSHIFUJI Hideaki 	return 0;
65870ceb4f5SYOSHIFUJI Hideaki }
65970ceb4f5SYOSHIFUJI Hideaki #endif
66070ceb4f5SYOSHIFUJI Hideaki 
6618ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
662982f56f3SYOSHIFUJI Hideaki do { \
6638ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
664982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
665e0eda7bbSVille Nuorvala 		while (1) { \
666982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
667c71099acSThomas Graf 				goto out; \
668982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
669982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6708bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
671982f56f3SYOSHIFUJI Hideaki 			else \
672982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
673c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
674c71099acSThomas Graf 				goto restart; \
675c71099acSThomas Graf 		} \
676982f56f3SYOSHIFUJI Hideaki 	} \
677982f56f3SYOSHIFUJI Hideaki } while (0)
678c71099acSThomas Graf 
6798ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6808ed67789SDaniel Lezcano 					     struct fib6_table *table,
6814c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct fib6_node *fn;
6841da177e4SLinus Torvalds 	struct rt6_info *rt;
6851da177e4SLinus Torvalds 
686c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6874c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
688c71099acSThomas Graf restart:
689c71099acSThomas Graf 	rt = fn->leaf;
6904c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6914c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
692c71099acSThomas Graf out:
693d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
694c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6951da177e4SLinus Torvalds 	return rt;
696c71099acSThomas Graf 
697c71099acSThomas Graf }
698c71099acSThomas Graf 
699ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
700ea6e574eSFlorian Westphal 				    int flags)
701ea6e574eSFlorian Westphal {
702ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
703ea6e574eSFlorian Westphal }
704ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
705ea6e574eSFlorian Westphal 
7069acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7079acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
708c71099acSThomas Graf {
7094c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7104c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7114c9483b2SDavid S. Miller 		.daddr = *daddr,
712c71099acSThomas Graf 	};
713c71099acSThomas Graf 	struct dst_entry *dst;
71477d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
715c71099acSThomas Graf 
716adaa70bbSThomas Graf 	if (saddr) {
7174c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
718adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
719adaa70bbSThomas Graf 	}
720adaa70bbSThomas Graf 
7214c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
722c71099acSThomas Graf 	if (dst->error == 0)
723c71099acSThomas Graf 		return (struct rt6_info *) dst;
724c71099acSThomas Graf 
725c71099acSThomas Graf 	dst_release(dst);
726c71099acSThomas Graf 
7271da177e4SLinus Torvalds 	return NULL;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7317159039aSYOSHIFUJI Hideaki 
732c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7331da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7341da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7351da177e4SLinus Torvalds    be destroyed.
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds 
73886872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	int err;
741c71099acSThomas Graf 	struct fib6_table *table;
7421da177e4SLinus Torvalds 
743c71099acSThomas Graf 	table = rt->rt6i_table;
744c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
74586872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
746c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	return err;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
75140e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
75240e22e8fSThomas Graf {
7534d1169c1SDenis V. Lunev 	struct nl_info info = {
754d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
7554d1169c1SDenis V. Lunev 	};
756528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
75740e22e8fSThomas Graf }
75840e22e8fSThomas Graf 
7591716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
76021efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
761b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct rt6_info *rt;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/*
7661da177e4SLinus Torvalds 	 *	Clone the route.
7671da177e4SLinus Torvalds 	 */
7681da177e4SLinus Torvalds 
76921efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	if (rt) {
77214deae41SDavid S. Miller 		int attempts = !in_softirq();
77314deae41SDavid S. Miller 
77458c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
775bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
77621efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
77758c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7784e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
77958c4fb86SYOSHIFUJI Hideaki 		}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7841da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7854e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
7861da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7871da177e4SLinus Torvalds 		}
7881da177e4SLinus Torvalds #endif
7891da177e4SLinus Torvalds 
79014deae41SDavid S. Miller 	retry:
7918ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
792d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
79314deae41SDavid S. Miller 			int saved_rt_min_interval =
79414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
79514deae41SDavid S. Miller 			int saved_rt_elasticity =
79614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
79714deae41SDavid S. Miller 
79814deae41SDavid S. Miller 			if (attempts-- > 0) {
79914deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
80014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
80114deae41SDavid S. Miller 
80286393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
80314deae41SDavid S. Miller 
80414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
80514deae41SDavid S. Miller 					saved_rt_elasticity;
80614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
80714deae41SDavid S. Miller 					saved_rt_min_interval;
80814deae41SDavid S. Miller 				goto retry;
80914deae41SDavid S. Miller 			}
81014deae41SDavid S. Miller 
811f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
812d8d1f30bSChangli Gao 			dst_free(&rt->dst);
81314deae41SDavid S. Miller 			return NULL;
81414deae41SDavid S. Miller 		}
81595a9a5baSYOSHIFUJI Hideaki 	}
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	return rt;
8181da177e4SLinus Torvalds }
81995a9a5baSYOSHIFUJI Hideaki 
82021efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
82121efcfa0SEric Dumazet 					const struct in6_addr *daddr)
822299d9939SYOSHIFUJI Hideaki {
82321efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
82421efcfa0SEric Dumazet 
825299d9939SYOSHIFUJI Hideaki 	if (rt) {
826299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
82727217455SDavid Miller 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst)));
828299d9939SYOSHIFUJI Hideaki 	}
829299d9939SYOSHIFUJI Hideaki 	return rt;
830299d9939SYOSHIFUJI Hideaki }
831299d9939SYOSHIFUJI Hideaki 
8328ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8334c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	struct fib6_node *fn;
836519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
837c71099acSThomas Graf 	int strict = 0;
8381da177e4SLinus Torvalds 	int attempts = 3;
839519fbd87SYOSHIFUJI Hideaki 	int err;
84053b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8411da177e4SLinus Torvalds 
84277d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds relookup:
845c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8461da177e4SLinus Torvalds 
8478238dd06SYOSHIFUJI Hideaki restart_2:
8484c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds restart:
8514acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8528ed67789SDaniel Lezcano 
8534c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8548ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8558238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8561da177e4SLinus Torvalds 		goto out;
8571da177e4SLinus Torvalds 
858d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
859c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8601da177e4SLinus Torvalds 
86127217455SDavid Miller 	if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8624c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8637343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8644c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8657343ff31SDavid S. Miller 	else
8667343ff31SDavid S. Miller 		goto out2;
8671da177e4SLinus Torvalds 
868d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8698ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8701da177e4SLinus Torvalds 
871d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
872e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
87340e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
874e40cf353SYOSHIFUJI Hideaki 		if (!err)
875e40cf353SYOSHIFUJI Hideaki 			goto out2;
876e40cf353SYOSHIFUJI Hideaki 	}
877e40cf353SYOSHIFUJI Hideaki 
878e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8791da177e4SLinus Torvalds 		goto out2;
8801da177e4SLinus Torvalds 
881519fbd87SYOSHIFUJI Hideaki 	/*
882c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
883519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8841da177e4SLinus Torvalds 	 */
885d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8861da177e4SLinus Torvalds 	goto relookup;
887e40cf353SYOSHIFUJI Hideaki 
888519fbd87SYOSHIFUJI Hideaki out:
8898238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8908238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8918238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8928238dd06SYOSHIFUJI Hideaki 	}
893d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
894c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8951da177e4SLinus Torvalds out2:
896d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
897d8d1f30bSChangli Gao 	rt->dst.__use++;
898c71099acSThomas Graf 
899c71099acSThomas Graf 	return rt;
900c71099acSThomas Graf }
901c71099acSThomas Graf 
9028ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9034c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9044acad72dSPavel Emelyanov {
9054c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9064acad72dSPavel Emelyanov }
9074acad72dSPavel Emelyanov 
90872331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
90972331bc0SShmulik Ladkani 						struct net_device *dev,
91072331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
91172331bc0SShmulik Ladkani {
91272331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
91372331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
91472331bc0SShmulik Ladkani 
91572331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
91672331bc0SShmulik Ladkani }
91772331bc0SShmulik Ladkani 
918c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
919c71099acSThomas Graf {
920b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
921c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
922adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9234c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9244c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9254c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9264c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9274c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
9284c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9294c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
930c71099acSThomas Graf 	};
931adaa70bbSThomas Graf 
93272331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
933c71099acSThomas Graf }
934c71099acSThomas Graf 
9358ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9364c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
937c71099acSThomas Graf {
9384c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
939c71099acSThomas Graf }
940c71099acSThomas Graf 
9419c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9424c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
943c71099acSThomas Graf {
944c71099acSThomas Graf 	int flags = 0;
945c71099acSThomas Graf 
9464dc27d1cSDavid McCullough 	fl6->flowi6_iif = net->loopback_dev->ifindex;
9474dc27d1cSDavid McCullough 
9484c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
94977d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
950c71099acSThomas Graf 
9514c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
952adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9530c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9540c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
955adaa70bbSThomas Graf 
9564c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9597159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9601da177e4SLinus Torvalds 
9612774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
96214e50e57SDavid S. Miller {
9635c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
96414e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
96514e50e57SDavid S. Miller 
9665c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
96714e50e57SDavid S. Miller 	if (rt) {
968cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
96997bab73fSDavid S. Miller 		rt6_init_peer(rt, net->ipv6.peers);
970cf911662SDavid S. Miller 
971d8d1f30bSChangli Gao 		new = &rt->dst;
97214e50e57SDavid S. Miller 
97314e50e57SDavid S. Miller 		new->__use = 1;
974352e512cSHerbert Xu 		new->input = dst_discard;
975352e512cSHerbert Xu 		new->output = dst_discard;
97614e50e57SDavid S. Miller 
97721efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
97821efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
97921efcfa0SEric Dumazet 		else
980defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
98114e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
98214e50e57SDavid S. Miller 		if (rt->rt6i_idev)
98314e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
98414e50e57SDavid S. Miller 
9854e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
9861716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
9871716a961SGao feng 		rt6_clean_expires(rt);
98814e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
98914e50e57SDavid S. Miller 
99014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
99114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
99214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
99314e50e57SDavid S. Miller #endif
99414e50e57SDavid S. Miller 
99514e50e57SDavid S. Miller 		dst_free(new);
99614e50e57SDavid S. Miller 	}
99714e50e57SDavid S. Miller 
99869ead7afSDavid S. Miller 	dst_release(dst_orig);
99969ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
100014e50e57SDavid S. Miller }
100114e50e57SDavid S. Miller 
10021da177e4SLinus Torvalds /*
10031da177e4SLinus Torvalds  *	Destination cache support functions
10041da177e4SLinus Torvalds  */
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct rt6_info *rt;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10111da177e4SLinus Torvalds 
10126431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
10136431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
101497bab73fSDavid S. Miller 			if (!rt6_has_peer(rt))
10156431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
10166431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
10176431cbc2SDavid S. Miller 		}
10181da177e4SLinus Torvalds 		return dst;
10196431cbc2SDavid S. Miller 	}
10201da177e4SLinus Torvalds 	return NULL;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	if (rt) {
102854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
102954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1030e0a1ad73SThomas Graf 				ip6_del_rt(rt);
103154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10321da177e4SLinus Torvalds 			}
103354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
103454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
103554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
103654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
103754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
103854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct rt6_info *rt;
10441da177e4SLinus Torvalds 
10453ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10461da177e4SLinus Torvalds 
1047adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10481da177e4SLinus Torvalds 	if (rt) {
10491716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10501716a961SGao feng 			rt6_update_expires(rt, 0);
10511716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10521da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10571da177e4SLinus Torvalds {
10581da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10591da177e4SLinus Torvalds 
106081aded24SDavid S. Miller 	dst_confirm(dst);
10611da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
106281aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
106381aded24SDavid S. Miller 
10641da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10651da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1066defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10671da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1068defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1069defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10701da177e4SLinus Torvalds 		}
1071defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
107281aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
107642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
107742ae66c8SDavid S. Miller 		     int oif, u32 mark)
107881aded24SDavid S. Miller {
107981aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
108081aded24SDavid S. Miller 	struct dst_entry *dst;
108181aded24SDavid S. Miller 	struct flowi6 fl6;
108281aded24SDavid S. Miller 
108381aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
108481aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
108581aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
108681aded24SDavid S. Miller 	fl6.flowi6_flags = FLOWI_FLAG_PRECOW_METRICS;
108781aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
108881aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
108981aded24SDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
109081aded24SDavid S. Miller 
109181aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
109281aded24SDavid S. Miller 	if (!dst->error)
109381aded24SDavid S. Miller 		ip6_rt_update_pmtu(dst, ntohl(mtu));
109481aded24SDavid S. Miller 	dst_release(dst);
109581aded24SDavid S. Miller }
109681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
109781aded24SDavid S. Miller 
109881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
109981aded24SDavid S. Miller {
110081aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
110181aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
110281aded24SDavid S. Miller }
110381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
110481aded24SDavid S. Miller 
11050dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11061da177e4SLinus Torvalds {
11070dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11080dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11090dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11100dbaee3bSDavid S. Miller 
11111da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11121da177e4SLinus Torvalds 
11135578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11145578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	/*
11171da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11181da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11191da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11201da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11211da177e4SLinus Torvalds 	 */
11221da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11231da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11241da177e4SLinus Torvalds 	return mtu;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
1127ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1128d33e4553SDavid S. Miller {
1129d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1130618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1131618f9bc7SSteffen Klassert 
1132618f9bc7SSteffen Klassert 	if (mtu)
1133618f9bc7SSteffen Klassert 		return mtu;
1134618f9bc7SSteffen Klassert 
1135618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1136d33e4553SDavid S. Miller 
1137d33e4553SDavid S. Miller 	rcu_read_lock();
1138d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1139d33e4553SDavid S. Miller 	if (idev)
1140d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1141d33e4553SDavid S. Miller 	rcu_read_unlock();
1142d33e4553SDavid S. Miller 
1143d33e4553SDavid S. Miller 	return mtu;
1144d33e4553SDavid S. Miller }
1145d33e4553SDavid S. Miller 
11463b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
11473b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
11485d0bbeebSThomas Graf 
11493b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
11501da177e4SLinus Torvalds 				  struct neighbour *neigh,
115187a11578SDavid S. Miller 				  struct flowi6 *fl6)
11521da177e4SLinus Torvalds {
115387a11578SDavid S. Miller 	struct dst_entry *dst;
11541da177e4SLinus Torvalds 	struct rt6_info *rt;
11551da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1156c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
11571da177e4SLinus Torvalds 
115838308473SDavid S. Miller 	if (unlikely(!idev))
1159122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
11601da177e4SLinus Torvalds 
11618b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
116238308473SDavid S. Miller 	if (unlikely(!rt)) {
11631da177e4SLinus Torvalds 		in6_dev_put(idev);
116487a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
11651da177e4SLinus Torvalds 		goto out;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	if (neigh)
11691da177e4SLinus Torvalds 		neigh_hold(neigh);
117014deae41SDavid S. Miller 	else {
1171*f894cbf8SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
1172b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1173252c3d84SRongQing.Li 			in6_dev_put(idev);
1174b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1175b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1176b43faac6SDavid S. Miller 		}
117714deae41SDavid S. Miller 	}
11781da177e4SLinus Torvalds 
11798e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
11808e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
118169cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1182d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
118387a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
11848e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
11858e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
11867011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
11871da177e4SLinus Torvalds 
11883b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1189d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1190d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
11913b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11921da177e4SLinus Torvalds 
11935578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11941da177e4SLinus Torvalds 
119587a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
119687a11578SDavid S. Miller 
11971da177e4SLinus Torvalds out:
119887a11578SDavid S. Miller 	return dst;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12013d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12021da177e4SLinus Torvalds {
1203e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12043d0f24a7SStephen Hemminger 	int more = 0;
12051da177e4SLinus Torvalds 
12063b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12073b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12085d0bbeebSThomas Graf 
12091da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12101da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12111da177e4SLinus Torvalds 			*pprev = dst->next;
12121da177e4SLinus Torvalds 			dst_free(dst);
12131da177e4SLinus Torvalds 		} else {
12141da177e4SLinus Torvalds 			pprev = &dst->next;
12153d0f24a7SStephen Hemminger 			++more;
12161da177e4SLinus Torvalds 		}
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 
12193b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12205d0bbeebSThomas Graf 
12213d0f24a7SStephen Hemminger 	return more;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12251e493d19SDavid S. Miller 			    void *arg)
12261e493d19SDavid S. Miller {
12271e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12281e493d19SDavid S. Miller 
12291e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12301e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12311e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12321e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12331e493d19SDavid S. Miller 		if (func(rt, arg)) {
12341e493d19SDavid S. Miller 			*pprev = dst->next;
12351e493d19SDavid S. Miller 			dst_free(dst);
12361e493d19SDavid S. Miller 		} else {
12371e493d19SDavid S. Miller 			pprev = &dst->next;
12381e493d19SDavid S. Miller 		}
12391e493d19SDavid S. Miller 	}
12401e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12411e493d19SDavid S. Miller }
12421e493d19SDavid S. Miller 
1243569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	unsigned long now = jiffies;
124686393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12477019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12487019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12497019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12507019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12517019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1252fc66f95cSEric Dumazet 	int entries;
12531da177e4SLinus Torvalds 
1254fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12557019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1256fc66f95cSEric Dumazet 	    entries <= rt_max_size)
12571da177e4SLinus Torvalds 		goto out;
12581da177e4SLinus Torvalds 
12596891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
12606891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
12616891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1262fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1263fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
12647019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
12651da177e4SLinus Torvalds out:
12667019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1267fc66f95cSEric Dumazet 	return entries > rt_max_size;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
12711da177e4SLinus Torvalds    but results in cleaner routing tables.
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds    Remove it only when all the things will work!
12741da177e4SLinus Torvalds  */
12751da177e4SLinus Torvalds 
12766b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
12771da177e4SLinus Torvalds {
12785170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1279a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
12806b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1281c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1282c68f24ccSEric Dumazet 
1283c68f24ccSEric Dumazet 		rcu_read_lock();
1284c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1285c68f24ccSEric Dumazet 		if (idev)
12861da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1287c68f24ccSEric Dumazet 		else
128853b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1289c68f24ccSEric Dumazet 		rcu_read_unlock();
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 	return hoplimit;
12921da177e4SLinus Torvalds }
1293abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds /*
12961da177e4SLinus Torvalds  *
12971da177e4SLinus Torvalds  */
12981da177e4SLinus Torvalds 
129986872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	int err;
13025578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13031da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13041da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13051da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1306c71099acSThomas Graf 	struct fib6_table *table;
13071da177e4SLinus Torvalds 	int addr_type;
13081da177e4SLinus Torvalds 
130986872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13101da177e4SLinus Torvalds 		return -EINVAL;
13111da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
131286872cb5SThomas Graf 	if (cfg->fc_src_len)
13131da177e4SLinus Torvalds 		return -EINVAL;
13141da177e4SLinus Torvalds #endif
131586872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13161da177e4SLinus Torvalds 		err = -ENODEV;
13175578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13181da177e4SLinus Torvalds 		if (!dev)
13191da177e4SLinus Torvalds 			goto out;
13201da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13211da177e4SLinus Torvalds 		if (!idev)
13221da177e4SLinus Torvalds 			goto out;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
132586872cb5SThomas Graf 	if (cfg->fc_metric == 0)
132686872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13271da177e4SLinus Torvalds 
1328c71099acSThomas Graf 	err = -ENOBUFS;
132938308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1330d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1331d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
133238308473SDavid S. Miller 		if (!table) {
1333f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1334d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1335d71314b4SMatti Vaittinen 		}
1336d71314b4SMatti Vaittinen 	} else {
1337d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1338d71314b4SMatti Vaittinen 	}
133938308473SDavid S. Miller 
134038308473SDavid S. Miller 	if (!table)
1341c71099acSThomas Graf 		goto out;
1342c71099acSThomas Graf 
13438b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13441da177e4SLinus Torvalds 
134538308473SDavid S. Miller 	if (!rt) {
13461da177e4SLinus Torvalds 		err = -ENOMEM;
13471da177e4SLinus Torvalds 		goto out;
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 
1350d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
13511716a961SGao feng 
13521716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13531716a961SGao feng 		rt6_set_expires(rt, jiffies +
13541716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13551716a961SGao feng 	else
13561716a961SGao feng 		rt6_clean_expires(rt);
13571da177e4SLinus Torvalds 
135886872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
135986872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
136086872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
136186872cb5SThomas Graf 
136286872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1365d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1366ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1367ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
13681da177e4SLinus Torvalds 	else
1369d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
13701da177e4SLinus Torvalds 
1371d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
13721da177e4SLinus Torvalds 
137386872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
137486872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
13751da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
137611d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
13771da177e4SLinus Torvalds 
13788e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
13798e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
13808e2ec639SYan, Zheng 		if (!metrics) {
13818e2ec639SYan, Zheng 			err = -ENOMEM;
13828e2ec639SYan, Zheng 			goto out;
13838e2ec639SYan, Zheng 		}
13848e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
13858e2ec639SYan, Zheng 	}
13861da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
138786872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
138886872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
13891da177e4SLinus Torvalds #endif
13901da177e4SLinus Torvalds 
139186872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
13941da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
13951da177e4SLinus Torvalds 	 */
139686872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
139738308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
139838308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
139938308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14001da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14015578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14021da177e4SLinus Torvalds 			if (dev) {
14031da177e4SLinus Torvalds 				dev_put(dev);
14041da177e4SLinus Torvalds 				in6_dev_put(idev);
14051da177e4SLinus Torvalds 			}
14065578689aSDaniel Lezcano 			dev = net->loopback_dev;
14071da177e4SLinus Torvalds 			dev_hold(dev);
14081da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14091da177e4SLinus Torvalds 			if (!idev) {
14101da177e4SLinus Torvalds 				err = -ENODEV;
14111da177e4SLinus Torvalds 				goto out;
14121da177e4SLinus Torvalds 			}
14131da177e4SLinus Torvalds 		}
1414d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1415d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1416d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
14171da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
14181da177e4SLinus Torvalds 		goto install_route;
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
142186872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1422b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14231da177e4SLinus Torvalds 		int gwa_type;
14241da177e4SLinus Torvalds 
142586872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14264e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14271da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14301da177e4SLinus Torvalds 			struct rt6_info *grt;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14331da177e4SLinus Torvalds 			   addresses as nexthop address.
14341da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14351da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14361da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14371da177e4SLinus Torvalds 			   some exceptions. --ANK
14381da177e4SLinus Torvalds 			 */
14391da177e4SLinus Torvalds 			err = -EINVAL;
14401da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14411da177e4SLinus Torvalds 				goto out;
14421da177e4SLinus Torvalds 
14435578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
144638308473SDavid S. Miller 			if (!grt)
14471da177e4SLinus Torvalds 				goto out;
14481da177e4SLinus Torvalds 			if (dev) {
1449d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1450d8d1f30bSChangli Gao 					dst_release(&grt->dst);
14511da177e4SLinus Torvalds 					goto out;
14521da177e4SLinus Torvalds 				}
14531da177e4SLinus Torvalds 			} else {
1454d1918542SDavid S. Miller 				dev = grt->dst.dev;
14551da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
14561da177e4SLinus Torvalds 				dev_hold(dev);
14571da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
14581da177e4SLinus Torvalds 			}
14591da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
14601da177e4SLinus Torvalds 				err = 0;
1461d8d1f30bSChangli Gao 			dst_release(&grt->dst);
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 			if (err)
14641da177e4SLinus Torvalds 				goto out;
14651da177e4SLinus Torvalds 		}
14661da177e4SLinus Torvalds 		err = -EINVAL;
146738308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
14681da177e4SLinus Torvalds 			goto out;
14691da177e4SLinus Torvalds 	}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	err = -ENODEV;
147238308473SDavid S. Miller 	if (!dev)
14731da177e4SLinus Torvalds 		goto out;
14741da177e4SLinus Torvalds 
1475c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1476c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1477c3968a85SDaniel Walter 			err = -EINVAL;
1478c3968a85SDaniel Walter 			goto out;
1479c3968a85SDaniel Walter 		}
14804e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1481c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1482c3968a85SDaniel Walter 	} else
1483c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1484c3968a85SDaniel Walter 
148586872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
14868ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1487f83c7790SDavid S. Miller 		if (err)
14881da177e4SLinus Torvalds 			goto out;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
149186872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds install_route:
149486872cb5SThomas Graf 	if (cfg->fc_mx) {
149586872cb5SThomas Graf 		struct nlattr *nla;
149686872cb5SThomas Graf 		int remaining;
14971da177e4SLinus Torvalds 
149886872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
14998f4c1f9bSThomas Graf 			int type = nla_type(nla);
150086872cb5SThomas Graf 
150186872cb5SThomas Graf 			if (type) {
150286872cb5SThomas Graf 				if (type > RTAX_MAX) {
15031da177e4SLinus Torvalds 					err = -EINVAL;
15041da177e4SLinus Torvalds 					goto out;
15051da177e4SLinus Torvalds 				}
150686872cb5SThomas Graf 
1507defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15081da177e4SLinus Torvalds 			}
15091da177e4SLinus Torvalds 		}
15101da177e4SLinus Torvalds 	}
15111da177e4SLinus Torvalds 
1512d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15131da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1514c71099acSThomas Graf 	rt->rt6i_table = table;
151563152fc0SDaniel Lezcano 
1516c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
151763152fc0SDaniel Lezcano 
151886872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds out:
15211da177e4SLinus Torvalds 	if (dev)
15221da177e4SLinus Torvalds 		dev_put(dev);
15231da177e4SLinus Torvalds 	if (idev)
15241da177e4SLinus Torvalds 		in6_dev_put(idev);
15251da177e4SLinus Torvalds 	if (rt)
1526d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15271da177e4SLinus Torvalds 	return err;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
153086872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15311da177e4SLinus Torvalds {
15321da177e4SLinus Torvalds 	int err;
1533c71099acSThomas Graf 	struct fib6_table *table;
1534d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15351da177e4SLinus Torvalds 
15368ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
15376c813a72SPatrick McHardy 		return -ENOENT;
15386c813a72SPatrick McHardy 
1539c71099acSThomas Graf 	table = rt->rt6i_table;
1540c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
15411da177e4SLinus Torvalds 
154286872cb5SThomas Graf 	err = fib6_del(rt, info);
1543d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15441da177e4SLinus Torvalds 
1545c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	return err;
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
1550e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1551e0a1ad73SThomas Graf {
15524d1169c1SDenis V. Lunev 	struct nl_info info = {
1553d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15544d1169c1SDenis V. Lunev 	};
1555528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1556e0a1ad73SThomas Graf }
1557e0a1ad73SThomas Graf 
155886872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
15591da177e4SLinus Torvalds {
1560c71099acSThomas Graf 	struct fib6_table *table;
15611da177e4SLinus Torvalds 	struct fib6_node *fn;
15621da177e4SLinus Torvalds 	struct rt6_info *rt;
15631da177e4SLinus Torvalds 	int err = -ESRCH;
15641da177e4SLinus Torvalds 
15655578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
156638308473SDavid S. Miller 	if (!table)
1567c71099acSThomas Graf 		return err;
15681da177e4SLinus Torvalds 
1569c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1570c71099acSThomas Graf 
1571c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
157286872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
157386872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	if (fn) {
1576d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
157786872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1578d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1579d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
15801da177e4SLinus Torvalds 				continue;
158186872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
158286872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
15831da177e4SLinus Torvalds 				continue;
158486872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
15851da177e4SLinus Torvalds 				continue;
1586d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1587c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
15881da177e4SLinus Torvalds 
158986872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
15901da177e4SLinus Torvalds 		}
15911da177e4SLinus Torvalds 	}
1592c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	return err;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds /*
15981da177e4SLinus Torvalds  *	Handle redirects
15991da177e4SLinus Torvalds  */
1600a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
16014c9483b2SDavid S. Miller 	struct flowi6 fl6;
1602a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1603a6279458SYOSHIFUJI Hideaki };
16041da177e4SLinus Torvalds 
16058ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
16068ed67789SDaniel Lezcano 					     struct fib6_table *table,
16074c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1608a6279458SYOSHIFUJI Hideaki 					     int flags)
1609a6279458SYOSHIFUJI Hideaki {
16104c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1611a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1612a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1613c71099acSThomas Graf 
1614e843b9e1SYOSHIFUJI Hideaki 	/*
1615e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1616e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1617e843b9e1SYOSHIFUJI Hideaki 	 *
1618e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1619e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1620e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1621e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1622e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1623e843b9e1SYOSHIFUJI Hideaki 	 */
16241da177e4SLinus Torvalds 
1625c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
16264c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1627e843b9e1SYOSHIFUJI Hideaki restart:
1628d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
16291da177e4SLinus Torvalds 		/*
16301da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
16311da177e4SLinus Torvalds 		 *
16321da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
16331da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
16341da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
16351da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
16361da177e4SLinus Torvalds 		 */
1637e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1638e843b9e1SYOSHIFUJI Hideaki 			continue;
16391da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1640e843b9e1SYOSHIFUJI Hideaki 			continue;
1641d1918542SDavid S. Miller 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1642e843b9e1SYOSHIFUJI Hideaki 			continue;
1643a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1644e843b9e1SYOSHIFUJI Hideaki 			continue;
1645e843b9e1SYOSHIFUJI Hideaki 		break;
1646e843b9e1SYOSHIFUJI Hideaki 	}
1647a6279458SYOSHIFUJI Hideaki 
1648cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
16498ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
16504c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1651cb15d9c2SYOSHIFUJI Hideaki out:
1652d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1653a6279458SYOSHIFUJI Hideaki 
1654c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16551da177e4SLinus Torvalds 
1656a6279458SYOSHIFUJI Hideaki 	return rt;
1657a6279458SYOSHIFUJI Hideaki };
1658a6279458SYOSHIFUJI Hideaki 
1659b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1660b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1661b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1662a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1663a6279458SYOSHIFUJI Hideaki {
1664adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1665c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1666a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
16674c9483b2SDavid S. Miller 		.fl6 = {
16684c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
16694c9483b2SDavid S. Miller 			.daddr = *dest,
16704c9483b2SDavid S. Miller 			.saddr = *src,
1671a6279458SYOSHIFUJI Hideaki 		},
1672a6279458SYOSHIFUJI Hideaki 	};
1673adaa70bbSThomas Graf 
16744e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
167586c36ce4SBrian Haley 
1676adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1677adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1678a6279458SYOSHIFUJI Hideaki 
16794c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
168058f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1681a6279458SYOSHIFUJI Hideaki }
1682a6279458SYOSHIFUJI Hideaki 
1683b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1684b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1685a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1686a6279458SYOSHIFUJI Hideaki {
1687a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1688a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1689c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1690a6279458SYOSHIFUJI Hideaki 
1691a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1692a6279458SYOSHIFUJI Hideaki 
16938ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
1694e87cc472SJoe Perches 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1695a6279458SYOSHIFUJI Hideaki 		goto out;
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	/*
16991da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17001da177e4SLinus Torvalds 	 */
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17031da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17041da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17051da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17061da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17071da177e4SLinus Torvalds 		     );
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	/*
17101da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
17111da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
17121da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
17131da177e4SLinus Torvalds 	 */
1714d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
171727217455SDavid Miller 	if (neigh == dst_get_neighbour_noref_raw(&rt->dst))
17181da177e4SLinus Torvalds 		goto out;
17191da177e4SLinus Torvalds 
172021efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
172138308473SDavid S. Miller 	if (!nrt)
17221da177e4SLinus Torvalds 		goto out;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17251da177e4SLinus Torvalds 	if (on_link)
17261da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17271da177e4SLinus Torvalds 
17284e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
172969cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
17301da177e4SLinus Torvalds 
173140e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17321da177e4SLinus Torvalds 		goto out;
17331da177e4SLinus Torvalds 
1734d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1735d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
17368d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17378d71740cSTom Tucker 
17381da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1739e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17401da177e4SLinus Torvalds 		return;
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds out:
1744d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds /*
17481da177e4SLinus Torvalds  *	Misc support functions
17491da177e4SLinus Torvalds  */
17501da177e4SLinus Torvalds 
17511716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
175221efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17531da177e4SLinus Torvalds {
1754d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17558b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17568b96d22dSDavid S. Miller 					    ort->rt6i_table);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	if (rt) {
1759d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1760d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17618e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17621da177e4SLinus Torvalds 
17634e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17648e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1765defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1766d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17671da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17681da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17691da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1770d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17711da177e4SLinus Torvalds 
17724e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17731716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17741716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17751716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
17761716a961SGao feng 			rt6_set_from(rt, ort);
17771716a961SGao feng 		else
17781716a961SGao feng 			rt6_clean_expires(rt);
17791da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17821da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17831da177e4SLinus Torvalds #endif
17840f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1785c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds 	return rt;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
179070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1791efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1792b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1793b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
179470ceb4f5SYOSHIFUJI Hideaki {
179570ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
179670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1797c71099acSThomas Graf 	struct fib6_table *table;
179870ceb4f5SYOSHIFUJI Hideaki 
1799efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
180038308473SDavid S. Miller 	if (!table)
1801c71099acSThomas Graf 		return NULL;
1802c71099acSThomas Graf 
1803c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1804c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
180570ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
180670ceb4f5SYOSHIFUJI Hideaki 		goto out;
180770ceb4f5SYOSHIFUJI Hideaki 
1808d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1809d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
181070ceb4f5SYOSHIFUJI Hideaki 			continue;
181170ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
181270ceb4f5SYOSHIFUJI Hideaki 			continue;
181370ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
181470ceb4f5SYOSHIFUJI Hideaki 			continue;
1815d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
181670ceb4f5SYOSHIFUJI Hideaki 		break;
181770ceb4f5SYOSHIFUJI Hideaki 	}
181870ceb4f5SYOSHIFUJI Hideaki out:
1819c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
182070ceb4f5SYOSHIFUJI Hideaki 	return rt;
182170ceb4f5SYOSHIFUJI Hideaki }
182270ceb4f5SYOSHIFUJI Hideaki 
1823efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1824b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1825b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
182695c96174SEric Dumazet 					   unsigned int pref)
182770ceb4f5SYOSHIFUJI Hideaki {
182886872cb5SThomas Graf 	struct fib6_config cfg = {
182986872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1830238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
183186872cb5SThomas Graf 		.fc_ifindex	= ifindex,
183286872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
183386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
183486872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1835efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1836efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1837efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
183886872cb5SThomas Graf 	};
183970ceb4f5SYOSHIFUJI Hideaki 
18404e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18414e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
184286872cb5SThomas Graf 
1843e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1844e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
184586872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
184670ceb4f5SYOSHIFUJI Hideaki 
184786872cb5SThomas Graf 	ip6_route_add(&cfg);
184870ceb4f5SYOSHIFUJI Hideaki 
1849efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
185070ceb4f5SYOSHIFUJI Hideaki }
185170ceb4f5SYOSHIFUJI Hideaki #endif
185270ceb4f5SYOSHIFUJI Hideaki 
1853b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18541da177e4SLinus Torvalds {
18551da177e4SLinus Torvalds 	struct rt6_info *rt;
1856c71099acSThomas Graf 	struct fib6_table *table;
18571da177e4SLinus Torvalds 
1858c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
185938308473SDavid S. Miller 	if (!table)
1860c71099acSThomas Graf 		return NULL;
18611da177e4SLinus Torvalds 
1862c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1863d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1864d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1865045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18661da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18671da177e4SLinus Torvalds 			break;
18681da177e4SLinus Torvalds 	}
18691da177e4SLinus Torvalds 	if (rt)
1870d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1871c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18721da177e4SLinus Torvalds 	return rt;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
1875b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1876ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1877ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18781da177e4SLinus Torvalds {
187986872cb5SThomas Graf 	struct fib6_config cfg = {
188086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1881238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
188286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
188386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
188486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18855578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18865578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1887c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
188886872cb5SThomas Graf 	};
18891da177e4SLinus Torvalds 
18904e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
18911da177e4SLinus Torvalds 
189286872cb5SThomas Graf 	ip6_route_add(&cfg);
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
18977b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
18981da177e4SLinus Torvalds {
18991da177e4SLinus Torvalds 	struct rt6_info *rt;
1900c71099acSThomas Graf 	struct fib6_table *table;
1901c71099acSThomas Graf 
1902c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19037b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
190438308473SDavid S. Miller 	if (!table)
1905c71099acSThomas Graf 		return;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds restart:
1908c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1909d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19101da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1911d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1912c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1913e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19141da177e4SLinus Torvalds 			goto restart;
19151da177e4SLinus Torvalds 		}
19161da177e4SLinus Torvalds 	}
1917c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19205578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19215578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
192286872cb5SThomas Graf 				 struct fib6_config *cfg)
192386872cb5SThomas Graf {
192486872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
192586872cb5SThomas Graf 
192686872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
192786872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
192886872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
192986872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
193086872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
193186872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
193286872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
193386872cb5SThomas Graf 
19345578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1935f1243c2dSBenjamin Thery 
19364e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19374e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19384e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
193986872cb5SThomas Graf }
194086872cb5SThomas Graf 
19415578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19421da177e4SLinus Torvalds {
194386872cb5SThomas Graf 	struct fib6_config cfg;
19441da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19451da177e4SLinus Torvalds 	int err;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	switch(cmd) {
19481da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19491da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19501da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19511da177e4SLinus Torvalds 			return -EPERM;
19521da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19531da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19541da177e4SLinus Torvalds 		if (err)
19551da177e4SLinus Torvalds 			return -EFAULT;
19561da177e4SLinus Torvalds 
19575578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
195886872cb5SThomas Graf 
19591da177e4SLinus Torvalds 		rtnl_lock();
19601da177e4SLinus Torvalds 		switch (cmd) {
19611da177e4SLinus Torvalds 		case SIOCADDRT:
196286872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19631da177e4SLinus Torvalds 			break;
19641da177e4SLinus Torvalds 		case SIOCDELRT:
196586872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19661da177e4SLinus Torvalds 			break;
19671da177e4SLinus Torvalds 		default:
19681da177e4SLinus Torvalds 			err = -EINVAL;
19691da177e4SLinus Torvalds 		}
19701da177e4SLinus Torvalds 		rtnl_unlock();
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 		return err;
19733ff50b79SStephen Hemminger 	}
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	return -EINVAL;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds /*
19791da177e4SLinus Torvalds  *	Drop the packet on the floor
19801da177e4SLinus Torvalds  */
19811da177e4SLinus Torvalds 
1982d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19831da177e4SLinus Torvalds {
1984612f09e8SYOSHIFUJI Hideaki 	int type;
1985adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1986612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1987612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19880660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
198945bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19903bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19913bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1992612f09e8SYOSHIFUJI Hideaki 			break;
1993612f09e8SYOSHIFUJI Hideaki 		}
1994612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1995612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
19963bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19973bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
1998612f09e8SYOSHIFUJI Hideaki 		break;
1999612f09e8SYOSHIFUJI Hideaki 	}
20003ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20011da177e4SLinus Torvalds 	kfree_skb(skb);
20021da177e4SLinus Torvalds 	return 0;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
20059ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20069ce8ade0SThomas Graf {
2007612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20089ce8ade0SThomas Graf }
20099ce8ade0SThomas Graf 
201020380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20111da177e4SLinus Torvalds {
2012adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2013612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
20166723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20176723ab54SDavid S. Miller 
20189ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20199ce8ade0SThomas Graf {
2020612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20219ce8ade0SThomas Graf }
20229ce8ade0SThomas Graf 
20239ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20249ce8ade0SThomas Graf {
2025adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2026612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20279ce8ade0SThomas Graf }
20289ce8ade0SThomas Graf 
20296723ab54SDavid S. Miller #endif
20306723ab54SDavid S. Miller 
20311da177e4SLinus Torvalds /*
20321da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20331da177e4SLinus Torvalds  */
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20361da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20378f031519SDavid S. Miller 				    bool anycast)
20381da177e4SLinus Torvalds {
2039c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20408b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2041f83c7790SDavid S. Miller 	int err;
20421da177e4SLinus Torvalds 
204338308473SDavid S. Miller 	if (!rt) {
2044f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20451da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
204640385653SBen Greear 	}
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	in6_dev_hold(idev);
20491da177e4SLinus Torvalds 
205011d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2051d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2052d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20531da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2054d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
205758c4fb86SYOSHIFUJI Hideaki 	if (anycast)
205858c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
205958c4fb86SYOSHIFUJI Hideaki 	else
20601da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20618ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2062f83c7790SDavid S. Miller 	if (err) {
2063d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2064f83c7790SDavid S. Miller 		return ERR_PTR(err);
20651da177e4SLinus Torvalds 	}
20661da177e4SLinus Torvalds 
20674e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20681da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20695578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20701da177e4SLinus Torvalds 
2071d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	return rt;
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds 
2076c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2077c3968a85SDaniel Walter 			struct rt6_info *rt,
2078b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2079c3968a85SDaniel Walter 			unsigned int prefs,
2080c3968a85SDaniel Walter 			struct in6_addr *saddr)
2081c3968a85SDaniel Walter {
2082c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2083c3968a85SDaniel Walter 	int err = 0;
2084c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
20854e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2086c3968a85SDaniel Walter 	else
2087c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2088c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2089c3968a85SDaniel Walter 	return err;
2090c3968a85SDaniel Walter }
2091c3968a85SDaniel Walter 
2092c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2093c3968a85SDaniel Walter struct arg_dev_net_ip {
2094c3968a85SDaniel Walter 	struct net_device *dev;
2095c3968a85SDaniel Walter 	struct net *net;
2096c3968a85SDaniel Walter 	struct in6_addr *addr;
2097c3968a85SDaniel Walter };
2098c3968a85SDaniel Walter 
2099c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2100c3968a85SDaniel Walter {
2101c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2102c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2103c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2104c3968a85SDaniel Walter 
2105d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2106c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2107c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2108c3968a85SDaniel Walter 		/* remove prefsrc entry */
2109c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2110c3968a85SDaniel Walter 	}
2111c3968a85SDaniel Walter 	return 0;
2112c3968a85SDaniel Walter }
2113c3968a85SDaniel Walter 
2114c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2115c3968a85SDaniel Walter {
2116c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2117c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2118c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2119c3968a85SDaniel Walter 		.net = net,
2120c3968a85SDaniel Walter 		.addr = &ifp->addr,
2121c3968a85SDaniel Walter 	};
2122c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2123c3968a85SDaniel Walter }
2124c3968a85SDaniel Walter 
21258ed67789SDaniel Lezcano struct arg_dev_net {
21268ed67789SDaniel Lezcano 	struct net_device *dev;
21278ed67789SDaniel Lezcano 	struct net *net;
21288ed67789SDaniel Lezcano };
21298ed67789SDaniel Lezcano 
21301da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21311da177e4SLinus Torvalds {
2132bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2133bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21348ed67789SDaniel Lezcano 
2135d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2136c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21371da177e4SLinus Torvalds 		return -1;
2138c159d30cSDavid S. Miller 
21391da177e4SLinus Torvalds 	return 0;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
2142f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21431da177e4SLinus Torvalds {
21448ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21458ed67789SDaniel Lezcano 		.dev = dev,
21468ed67789SDaniel Lezcano 		.net = net,
21478ed67789SDaniel Lezcano 	};
21488ed67789SDaniel Lezcano 
21498ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21501e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
215395c96174SEric Dumazet struct rt6_mtu_change_arg {
21541da177e4SLinus Torvalds 	struct net_device *dev;
215595c96174SEric Dumazet 	unsigned int mtu;
21561da177e4SLinus Torvalds };
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21591da177e4SLinus Torvalds {
21601da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21611da177e4SLinus Torvalds 	struct inet6_dev *idev;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21641da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21651da177e4SLinus Torvalds 	   We still use this lock to block changes
21661da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21671da177e4SLinus Torvalds 	*/
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
217038308473SDavid S. Miller 	if (!idev)
21711da177e4SLinus Torvalds 		return 0;
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21741da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21751da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21761da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21771da177e4SLinus Torvalds 	 */
21781da177e4SLinus Torvalds 	/*
21791da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21801da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21811da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21821da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21831da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21841da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21851da177e4SLinus Torvalds 	   PMTU discouvery.
21861da177e4SLinus Torvalds 	 */
2187d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2188d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2189d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2190d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2191d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2192defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2193566cfd8fSSimon Arlott 	}
21941da177e4SLinus Torvalds 	return 0;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
219795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
21981da177e4SLinus Torvalds {
2199c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2200c71099acSThomas Graf 		.dev = dev,
2201c71099acSThomas Graf 		.mtu = mtu,
2202c71099acSThomas Graf 	};
22031da177e4SLinus Torvalds 
2204c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
2207ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22085176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
220986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2210ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
221186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
221286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
221386872cb5SThomas Graf };
221486872cb5SThomas Graf 
221586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
221686872cb5SThomas Graf 			      struct fib6_config *cfg)
22171da177e4SLinus Torvalds {
221886872cb5SThomas Graf 	struct rtmsg *rtm;
221986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
222086872cb5SThomas Graf 	int err;
22211da177e4SLinus Torvalds 
222286872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
222386872cb5SThomas Graf 	if (err < 0)
222486872cb5SThomas Graf 		goto errout;
22251da177e4SLinus Torvalds 
222686872cb5SThomas Graf 	err = -EINVAL;
222786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
222886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
222986872cb5SThomas Graf 
223086872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
223186872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
223286872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
223386872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
223486872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
223586872cb5SThomas Graf 
223686872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
223786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
223886872cb5SThomas Graf 
2239ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2240ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2241ab79ad14SMaciej Żenczykowski 
224286872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
224386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22443b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
224586872cb5SThomas Graf 
224686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
224786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
224886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22491da177e4SLinus Torvalds 	}
225086872cb5SThomas Graf 
225186872cb5SThomas Graf 	if (tb[RTA_DST]) {
225286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
225386872cb5SThomas Graf 
225486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
225586872cb5SThomas Graf 			goto errout;
225686872cb5SThomas Graf 
225786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22581da177e4SLinus Torvalds 	}
225986872cb5SThomas Graf 
226086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
226186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
226286872cb5SThomas Graf 
226386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
226486872cb5SThomas Graf 			goto errout;
226586872cb5SThomas Graf 
226686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22671da177e4SLinus Torvalds 	}
226886872cb5SThomas Graf 
2269c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2270c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2271c3968a85SDaniel Walter 
227286872cb5SThomas Graf 	if (tb[RTA_OIF])
227386872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
227486872cb5SThomas Graf 
227586872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
227686872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
227786872cb5SThomas Graf 
227886872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
227986872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
228086872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22811da177e4SLinus Torvalds 	}
228286872cb5SThomas Graf 
228386872cb5SThomas Graf 	if (tb[RTA_TABLE])
228486872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
228586872cb5SThomas Graf 
228686872cb5SThomas Graf 	err = 0;
228786872cb5SThomas Graf errout:
228886872cb5SThomas Graf 	return err;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
2291c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22921da177e4SLinus Torvalds {
229386872cb5SThomas Graf 	struct fib6_config cfg;
229486872cb5SThomas Graf 	int err;
22951da177e4SLinus Torvalds 
229686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
229786872cb5SThomas Graf 	if (err < 0)
229886872cb5SThomas Graf 		return err;
229986872cb5SThomas Graf 
230086872cb5SThomas Graf 	return ip6_route_del(&cfg);
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds 
2303c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23041da177e4SLinus Torvalds {
230586872cb5SThomas Graf 	struct fib6_config cfg;
230686872cb5SThomas Graf 	int err;
23071da177e4SLinus Torvalds 
230886872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
230986872cb5SThomas Graf 	if (err < 0)
231086872cb5SThomas Graf 		return err;
231186872cb5SThomas Graf 
231286872cb5SThomas Graf 	return ip6_route_add(&cfg);
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
2315339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2316339bf98fSThomas Graf {
2317339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2318339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2319339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2320339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2321339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2322339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2323339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2324339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2325339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23266a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2327339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2328339bf98fSThomas Graf }
2329339bf98fSThomas Graf 
2330191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2331191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23320d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23330d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23347bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23351da177e4SLinus Torvalds {
2336346f870bSDavid S. Miller 	const struct inet_peer *peer;
23371da177e4SLinus Torvalds 	struct rtmsg *rtm;
23381da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2339e3703b3dSThomas Graf 	long expires;
23409e762a4aSPatrick McHardy 	u32 table;
2341f2c31e32SEric Dumazet 	struct neighbour *n;
2342346f870bSDavid S. Miller 	u32 ts, tsage;
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23451da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23461da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23471da177e4SLinus Torvalds 			return 1;
23481da177e4SLinus Torvalds 		}
23491da177e4SLinus Torvalds 	}
23501da177e4SLinus Torvalds 
23512d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
235238308473SDavid S. Miller 	if (!nlh)
235326932566SPatrick McHardy 		return -EMSGSIZE;
23542d7202bfSThomas Graf 
23552d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23561da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23571da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23581da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23591da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2360c71099acSThomas Graf 	if (rt->rt6i_table)
23619e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2362c71099acSThomas Graf 	else
23639e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23649e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2365c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2366c78679e8SDavid S. Miller 		goto nla_put_failure;
23671da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
23681da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2369ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2370ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2371d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
23721da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23731da177e4SLinus Torvalds 	else
23741da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23751da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23761da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23771da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23781da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
23791da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23801da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23811da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23821da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
23831da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
23861da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 	if (dst) {
2389c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2390c78679e8SDavid S. Miller 			goto nla_put_failure;
23911da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
23921da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2393c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2394c78679e8SDavid S. Miller 			goto nla_put_failure;
23951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
23961da177e4SLinus Torvalds 	if (src) {
2397c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2398c78679e8SDavid S. Miller 			goto nla_put_failure;
23991da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2400c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2401c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2402c78679e8SDavid S. Miller 		goto nla_put_failure;
24031da177e4SLinus Torvalds #endif
24047bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24057bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24067bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24078229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24087bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24097bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24107bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24117bc570c8SYOSHIFUJI Hideaki 						return 0;
24127bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24137bc570c8SYOSHIFUJI Hideaki 				} else {
24147bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24157bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24167bc570c8SYOSHIFUJI Hideaki 				}
24177bc570c8SYOSHIFUJI Hideaki 			}
24187bc570c8SYOSHIFUJI Hideaki 		} else
24197bc570c8SYOSHIFUJI Hideaki #endif
2420c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2421c78679e8SDavid S. Miller 				goto nla_put_failure;
24227bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24231da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2424c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2425c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2426c78679e8SDavid S. Miller 			goto nla_put_failure;
2427c3968a85SDaniel Walter 	}
2428c3968a85SDaniel Walter 
2429c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2430c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
24314e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2432c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2433c78679e8SDavid S. Miller 			goto nla_put_failure;
24341da177e4SLinus Torvalds 	}
24352d7202bfSThomas Graf 
2436defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24372d7202bfSThomas Graf 		goto nla_put_failure;
24382d7202bfSThomas Graf 
2439f2c31e32SEric Dumazet 	rcu_read_lock();
244027217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
244194f826b8SEric Dumazet 	if (n) {
244294f826b8SEric Dumazet 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) {
244394f826b8SEric Dumazet 			rcu_read_unlock();
244494f826b8SEric Dumazet 			goto nla_put_failure;
244594f826b8SEric Dumazet 		}
244694f826b8SEric Dumazet 	}
2447f2c31e32SEric Dumazet 	rcu_read_unlock();
24482d7202bfSThomas Graf 
2449c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2450c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2451c78679e8SDavid S. Miller 		goto nla_put_failure;
2452c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2453c78679e8SDavid S. Miller 		goto nla_put_failure;
245436e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
245536e3deaeSYOSHIFUJI Hideaki 		expires = 0;
2456d1918542SDavid S. Miller 	else if (rt->dst.expires - jiffies < INT_MAX)
2457d1918542SDavid S. Miller 		expires = rt->dst.expires - jiffies;
245836e3deaeSYOSHIFUJI Hideaki 	else
245936e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
246069cdf8f9SYOSHIFUJI Hideaki 
246197bab73fSDavid S. Miller 	peer = NULL;
246297bab73fSDavid S. Miller 	if (rt6_has_peer(rt))
246397bab73fSDavid S. Miller 		peer = rt6_peer_ptr(rt);
2464346f870bSDavid S. Miller 	ts = tsage = 0;
2465346f870bSDavid S. Miller 	if (peer && peer->tcp_ts_stamp) {
2466346f870bSDavid S. Miller 		ts = peer->tcp_ts;
2467346f870bSDavid S. Miller 		tsage = get_seconds() - peer->tcp_ts_stamp;
2468346f870bSDavid S. Miller 	}
2469346f870bSDavid S. Miller 
2470346f870bSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
2471d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2472e3703b3dSThomas Graf 		goto nla_put_failure;
24731da177e4SLinus Torvalds 
24742d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24752d7202bfSThomas Graf 
24762d7202bfSThomas Graf nla_put_failure:
247726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
247826932566SPatrick McHardy 	return -EMSGSIZE;
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds 
24811b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24841da177e4SLinus Torvalds 	int prefix;
24851da177e4SLinus Torvalds 
24862d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24872d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24881da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24891da177e4SLinus Torvalds 	} else
24901da177e4SLinus Torvalds 		prefix = 0;
24911da177e4SLinus Torvalds 
2492191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2493191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24941da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24957bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24961da177e4SLinus Torvalds }
24971da177e4SLinus Torvalds 
2498c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24991da177e4SLinus Torvalds {
25003b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2501ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25021da177e4SLinus Torvalds 	struct rt6_info *rt;
2503ab364a6fSThomas Graf 	struct sk_buff *skb;
2504ab364a6fSThomas Graf 	struct rtmsg *rtm;
25054c9483b2SDavid S. Miller 	struct flowi6 fl6;
250672331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2507ab364a6fSThomas Graf 
2508ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2509ab364a6fSThomas Graf 	if (err < 0)
2510ab364a6fSThomas Graf 		goto errout;
2511ab364a6fSThomas Graf 
2512ab364a6fSThomas Graf 	err = -EINVAL;
25134c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2514ab364a6fSThomas Graf 
2515ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2516ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2517ab364a6fSThomas Graf 			goto errout;
2518ab364a6fSThomas Graf 
25194e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2520ab364a6fSThomas Graf 	}
2521ab364a6fSThomas Graf 
2522ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2523ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2524ab364a6fSThomas Graf 			goto errout;
2525ab364a6fSThomas Graf 
25264e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2527ab364a6fSThomas Graf 	}
2528ab364a6fSThomas Graf 
2529ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2530ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2531ab364a6fSThomas Graf 
2532ab364a6fSThomas Graf 	if (tb[RTA_OIF])
253372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2534ab364a6fSThomas Graf 
2535ab364a6fSThomas Graf 	if (iif) {
2536ab364a6fSThomas Graf 		struct net_device *dev;
253772331bc0SShmulik Ladkani 		int flags = 0;
253872331bc0SShmulik Ladkani 
25395578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2540ab364a6fSThomas Graf 		if (!dev) {
2541ab364a6fSThomas Graf 			err = -ENODEV;
2542ab364a6fSThomas Graf 			goto errout;
2543ab364a6fSThomas Graf 		}
254472331bc0SShmulik Ladkani 
254572331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
254672331bc0SShmulik Ladkani 
254772331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
254872331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
254972331bc0SShmulik Ladkani 
255072331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
255172331bc0SShmulik Ladkani 							       flags);
255272331bc0SShmulik Ladkani 	} else {
255372331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
255472331bc0SShmulik Ladkani 
255572331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2556ab364a6fSThomas Graf 	}
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
255938308473SDavid S. Miller 	if (!skb) {
25602173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2561ab364a6fSThomas Graf 		err = -ENOBUFS;
2562ab364a6fSThomas Graf 		goto errout;
2563ab364a6fSThomas Graf 	}
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25661da177e4SLinus Torvalds 	   through good chunk of routing engine.
25671da177e4SLinus Torvalds 	 */
2568459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25691da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25701da177e4SLinus Torvalds 
2571d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25721da177e4SLinus Torvalds 
25734c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25741da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25757bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25761da177e4SLinus Torvalds 	if (err < 0) {
2577ab364a6fSThomas Graf 		kfree_skb(skb);
2578ab364a6fSThomas Graf 		goto errout;
25791da177e4SLinus Torvalds 	}
25801da177e4SLinus Torvalds 
25815578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2582ab364a6fSThomas Graf errout:
25831da177e4SLinus Torvalds 	return err;
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
258686872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25871da177e4SLinus Torvalds {
25881da177e4SLinus Torvalds 	struct sk_buff *skb;
25895578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2590528c4cebSDenis V. Lunev 	u32 seq;
2591528c4cebSDenis V. Lunev 	int err;
25920d51aa80SJamal Hadi Salim 
2593528c4cebSDenis V. Lunev 	err = -ENOBUFS;
259438308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
259586872cb5SThomas Graf 
2596339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
259738308473SDavid S. Miller 	if (!skb)
259821713ebcSThomas Graf 		goto errout;
25991da177e4SLinus Torvalds 
2600191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
26017bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
260226932566SPatrick McHardy 	if (err < 0) {
260326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
260426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
260526932566SPatrick McHardy 		kfree_skb(skb);
260626932566SPatrick McHardy 		goto errout;
260726932566SPatrick McHardy 	}
26081ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26095578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26101ce85fe4SPablo Neira Ayuso 	return;
261121713ebcSThomas Graf errout:
261221713ebcSThomas Graf 	if (err < 0)
26135578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
26168ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26178ed67789SDaniel Lezcano 				unsigned long event, void *data)
26188ed67789SDaniel Lezcano {
26198ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2620c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26218ed67789SDaniel Lezcano 
26228ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2623d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26248ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26258ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2626d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26278ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2628d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26298ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26308ed67789SDaniel Lezcano #endif
26318ed67789SDaniel Lezcano 	}
26328ed67789SDaniel Lezcano 
26338ed67789SDaniel Lezcano 	return NOTIFY_OK;
26348ed67789SDaniel Lezcano }
26358ed67789SDaniel Lezcano 
26361da177e4SLinus Torvalds /*
26371da177e4SLinus Torvalds  *	/proc
26381da177e4SLinus Torvalds  */
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds struct rt6_proc_arg
26431da177e4SLinus Torvalds {
26441da177e4SLinus Torvalds 	char *buffer;
26451da177e4SLinus Torvalds 	int offset;
26461da177e4SLinus Torvalds 	int length;
26471da177e4SLinus Torvalds 	int skip;
26481da177e4SLinus Torvalds 	int len;
26491da177e4SLinus Torvalds };
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26521da177e4SLinus Torvalds {
265333120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
265469cce1d1SDavid S. Miller 	struct neighbour *n;
26551da177e4SLinus Torvalds 
26564b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26594b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26601da177e4SLinus Torvalds #else
266133120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26621da177e4SLinus Torvalds #endif
2663f2c31e32SEric Dumazet 	rcu_read_lock();
266427217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
266569cce1d1SDavid S. Miller 	if (n) {
266669cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26671da177e4SLinus Torvalds 	} else {
266833120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26691da177e4SLinus Torvalds 	}
2670f2c31e32SEric Dumazet 	rcu_read_unlock();
267133120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2672d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2673d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2674d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
26751da177e4SLinus Torvalds 	return 0;
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds 
267833120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26791da177e4SLinus Torvalds {
2680f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
268132b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
268233120b30SAlexey Dobriyan 	return 0;
26831da177e4SLinus Torvalds }
26841da177e4SLinus Torvalds 
268533120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
268633120b30SAlexey Dobriyan {
2687de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2688f3db4851SDaniel Lezcano }
2689f3db4851SDaniel Lezcano 
269033120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
269133120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
269233120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
269333120b30SAlexey Dobriyan 	.read		= seq_read,
269433120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2695b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
269633120b30SAlexey Dobriyan };
269733120b30SAlexey Dobriyan 
26981da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26991da177e4SLinus Torvalds {
270069ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27011da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
270269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
270369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
270469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
270569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
270669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2707fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
270869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	return 0;
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27141da177e4SLinus Torvalds {
2715de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
271669ddb805SDaniel Lezcano }
271769ddb805SDaniel Lezcano 
27189a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27191da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27201da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27211da177e4SLinus Torvalds 	.read	 = seq_read,
27221da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2723b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27241da177e4SLinus Torvalds };
27251da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds static
27308d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27311da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27321da177e4SLinus Torvalds {
2733c486da34SLucian Adrian Grijincu 	struct net *net;
2734c486da34SLucian Adrian Grijincu 	int delay;
2735c486da34SLucian Adrian Grijincu 	if (!write)
2736c486da34SLucian Adrian Grijincu 		return -EINVAL;
2737c486da34SLucian Adrian Grijincu 
2738c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2739c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27408d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27415b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27421da177e4SLinus Torvalds 	return 0;
27431da177e4SLinus Torvalds }
27441da177e4SLinus Torvalds 
2745760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27461da177e4SLinus Torvalds 	{
27471da177e4SLinus Torvalds 		.procname	=	"flush",
27484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
275089c8b3a1SDave Jones 		.mode		=	0200,
27516d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27521da177e4SLinus Torvalds 	},
27531da177e4SLinus Torvalds 	{
27541da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27559a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27561da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27571da177e4SLinus Torvalds 		.mode		=	0644,
27586d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27591da177e4SLinus Torvalds 	},
27601da177e4SLinus Torvalds 	{
27611da177e4SLinus Torvalds 		.procname	=	"max_size",
27624990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27631da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27641da177e4SLinus Torvalds 		.mode		=	0644,
27656d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27661da177e4SLinus Torvalds 	},
27671da177e4SLinus Torvalds 	{
27681da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27694990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27701da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27711da177e4SLinus Torvalds 		.mode		=	0644,
27726d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27731da177e4SLinus Torvalds 	},
27741da177e4SLinus Torvalds 	{
27751da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27764990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27771da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27781da177e4SLinus Torvalds 		.mode		=	0644,
27796d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27801da177e4SLinus Torvalds 	},
27811da177e4SLinus Torvalds 	{
27821da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27834990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27841da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27851da177e4SLinus Torvalds 		.mode		=	0644,
27866d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27871da177e4SLinus Torvalds 	},
27881da177e4SLinus Torvalds 	{
27891da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27904990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27911da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27921da177e4SLinus Torvalds 		.mode		=	0644,
2793f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27941da177e4SLinus Torvalds 	},
27951da177e4SLinus Torvalds 	{
27961da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27974990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27981da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27991da177e4SLinus Torvalds 		.mode		=	0644,
28006d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28011da177e4SLinus Torvalds 	},
28021da177e4SLinus Torvalds 	{
28031da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28044990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28051da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28061da177e4SLinus Torvalds 		.mode		=	0644,
2807f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28081da177e4SLinus Torvalds 	},
28091da177e4SLinus Torvalds 	{
28101da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28114990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28121da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28131da177e4SLinus Torvalds 		.mode		=	0644,
28146d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28151da177e4SLinus Torvalds 	},
2816f8572d8fSEric W. Biederman 	{ }
28171da177e4SLinus Torvalds };
28181da177e4SLinus Torvalds 
28192c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2820760f2d01SDaniel Lezcano {
2821760f2d01SDaniel Lezcano 	struct ctl_table *table;
2822760f2d01SDaniel Lezcano 
2823760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2824760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2825760f2d01SDaniel Lezcano 			GFP_KERNEL);
28265ee09105SYOSHIFUJI Hideaki 
28275ee09105SYOSHIFUJI Hideaki 	if (table) {
28285ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2829c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
283086393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28315ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28325ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28335ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28345ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28355ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28365ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28375ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28389c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28395ee09105SYOSHIFUJI Hideaki 	}
28405ee09105SYOSHIFUJI Hideaki 
2841760f2d01SDaniel Lezcano 	return table;
2842760f2d01SDaniel Lezcano }
28431da177e4SLinus Torvalds #endif
28441da177e4SLinus Torvalds 
28452c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2846cdb18761SDaniel Lezcano {
2847633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28488ed67789SDaniel Lezcano 
284986393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
285086393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2851f2fc6a54SBenjamin Thery 
2852fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2853fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2854fc66f95cSEric Dumazet 
28558ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28568ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28578ed67789SDaniel Lezcano 					   GFP_KERNEL);
28588ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2859fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2860d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28618ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2862d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
286462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28658ed67789SDaniel Lezcano 
28668ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28678ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28688ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28698ed67789SDaniel Lezcano 					       GFP_KERNEL);
287068fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
287168fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2872d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28738ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2874d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
287562fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
287662fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28778ed67789SDaniel Lezcano 
28788ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28798ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28808ed67789SDaniel Lezcano 					       GFP_KERNEL);
288168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
288268fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2883d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28848ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2885d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
288662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
288762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28888ed67789SDaniel Lezcano #endif
28898ed67789SDaniel Lezcano 
2890b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2891b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2892b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2893b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2894b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2895b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2896b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2897b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2898b339a47cSPeter Zijlstra 
28996891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29006891a346SBenjamin Thery 
29018ed67789SDaniel Lezcano 	ret = 0;
29028ed67789SDaniel Lezcano out:
29038ed67789SDaniel Lezcano 	return ret;
2904f2fc6a54SBenjamin Thery 
290568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
290668fffc67SPeter Zijlstra out_ip6_prohibit_entry:
290768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
290868fffc67SPeter Zijlstra out_ip6_null_entry:
290968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
291068fffc67SPeter Zijlstra #endif
2911fc66f95cSEric Dumazet out_ip6_dst_entries:
2912fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2913f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2914f2fc6a54SBenjamin Thery 	goto out;
2915cdb18761SDaniel Lezcano }
2916cdb18761SDaniel Lezcano 
29172c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2918cdb18761SDaniel Lezcano {
29198ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29208ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29218ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29228ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29238ed67789SDaniel Lezcano #endif
292441bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2925cdb18761SDaniel Lezcano }
2926cdb18761SDaniel Lezcano 
2927d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2928d189634eSThomas Graf {
2929d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2930d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2931d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2932d189634eSThomas Graf #endif
2933d189634eSThomas Graf 	return 0;
2934d189634eSThomas Graf }
2935d189634eSThomas Graf 
2936d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
2937d189634eSThomas Graf {
2938d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2939d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
2940d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
2941d189634eSThomas Graf #endif
2942d189634eSThomas Graf }
2943d189634eSThomas Graf 
2944cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2945cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2946cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2947cdb18761SDaniel Lezcano };
2948cdb18761SDaniel Lezcano 
2949c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
2950c3426b47SDavid S. Miller {
2951c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
2952c3426b47SDavid S. Miller 
2953c3426b47SDavid S. Miller 	if (!bp)
2954c3426b47SDavid S. Miller 		return -ENOMEM;
2955c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
2956c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
2957c3426b47SDavid S. Miller 	return 0;
2958c3426b47SDavid S. Miller }
2959c3426b47SDavid S. Miller 
2960c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
2961c3426b47SDavid S. Miller {
2962c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
2963c3426b47SDavid S. Miller 
2964c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
296556a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
2966c3426b47SDavid S. Miller 	kfree(bp);
2967c3426b47SDavid S. Miller }
2968c3426b47SDavid S. Miller 
29692b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
2970c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
2971c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
2972c3426b47SDavid S. Miller };
2973c3426b47SDavid S. Miller 
2974d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
2975d189634eSThomas Graf 	.init = ip6_route_net_init_late,
2976d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
2977d189634eSThomas Graf };
2978d189634eSThomas Graf 
29798ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29808ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29818ed67789SDaniel Lezcano 	.priority = 0,
29828ed67789SDaniel Lezcano };
29838ed67789SDaniel Lezcano 
2984433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29851da177e4SLinus Torvalds {
2986433d49c3SDaniel Lezcano 	int ret;
2987433d49c3SDaniel Lezcano 
29889a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29899a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29909a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29919a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29929a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2993c19a28e1SFernando Carrijo 		goto out;
299414e50e57SDavid S. Miller 
2995fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29968ed67789SDaniel Lezcano 	if (ret)
2997bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2998bdb3289fSDaniel Lezcano 
2999c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3000c3426b47SDavid S. Miller 	if (ret)
3001e8803b6cSDavid S. Miller 		goto out_dst_entries;
30022a0c451aSThomas Graf 
30037e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30047e52b33bSDavid S. Miller 	if (ret)
30057e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3006c3426b47SDavid S. Miller 
30075dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30085dc121e9SArnaud Ebalard 
30098ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30108ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30118ed67789SDaniel Lezcano 	 * manually for init_net */
3012d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30138ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3014bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3015d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30168ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3017d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30188ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3019bdb3289fSDaniel Lezcano   #endif
3020e8803b6cSDavid S. Miller 	ret = fib6_init();
3021433d49c3SDaniel Lezcano 	if (ret)
30228ed67789SDaniel Lezcano 		goto out_register_subsys;
3023433d49c3SDaniel Lezcano 
3024433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3025433d49c3SDaniel Lezcano 	if (ret)
3026e8803b6cSDavid S. Miller 		goto out_fib6_init;
3027c35b7e72SDaniel Lezcano 
3028433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3029433d49c3SDaniel Lezcano 	if (ret)
3030433d49c3SDaniel Lezcano 		goto xfrm6_init;
30317e5449c2SDaniel Lezcano 
3032d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3033d189634eSThomas Graf 	if (ret)
3034d189634eSThomas Graf 		goto fib6_rules_init;
3035d189634eSThomas Graf 
3036433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3037c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3038c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3039c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3040d189634eSThomas Graf 		goto out_register_late_subsys;
3041433d49c3SDaniel Lezcano 
30428ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3043cdb18761SDaniel Lezcano 	if (ret)
3044d189634eSThomas Graf 		goto out_register_late_subsys;
30458ed67789SDaniel Lezcano 
3046433d49c3SDaniel Lezcano out:
3047433d49c3SDaniel Lezcano 	return ret;
3048433d49c3SDaniel Lezcano 
3049d189634eSThomas Graf out_register_late_subsys:
3050d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3051433d49c3SDaniel Lezcano fib6_rules_init:
3052433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3053433d49c3SDaniel Lezcano xfrm6_init:
3054433d49c3SDaniel Lezcano 	xfrm6_fini();
30552a0c451aSThomas Graf out_fib6_init:
30562a0c451aSThomas Graf 	fib6_gc_cleanup();
30578ed67789SDaniel Lezcano out_register_subsys:
30588ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
30597e52b33bSDavid S. Miller out_register_inetpeer:
30607e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3061fc66f95cSEric Dumazet out_dst_entries:
3062fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3063433d49c3SDaniel Lezcano out_kmem_cache:
3064f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3065433d49c3SDaniel Lezcano 	goto out;
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds void ip6_route_cleanup(void)
30691da177e4SLinus Torvalds {
30708ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3071d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3072101367c2SThomas Graf 	fib6_rules_cleanup();
30731da177e4SLinus Torvalds 	xfrm6_fini();
30741da177e4SLinus Torvalds 	fib6_gc_cleanup();
3075c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
30768ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
307741bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3078f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30791da177e4SLinus Torvalds }
3080