xref: /openbmc/linux/net/ipv6/route.c (revision 6e157b6ac61aa7758ccd643d4aafdf3cc17b9f04)
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);
82*6e157b6aSDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sk_buff *skb);
831da177e4SLinus Torvalds 
8470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
85efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
86b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
87b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
8895c96174SEric Dumazet 					   unsigned int pref);
89efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
90b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
91b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9270ceb4f5SYOSHIFUJI Hideaki #endif
9370ceb4f5SYOSHIFUJI Hideaki 
9406582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9506582540SDavid S. Miller {
9606582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
9706582540SDavid S. Miller 	struct inet_peer *peer;
9806582540SDavid S. Miller 	u32 *p = NULL;
9906582540SDavid S. Miller 
1008e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1018e2ec639SYan, Zheng 		return NULL;
1028e2ec639SYan, Zheng 
103fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10406582540SDavid S. Miller 	if (peer) {
10506582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10606582540SDavid S. Miller 		unsigned long prev, new;
10706582540SDavid S. Miller 
10806582540SDavid S. Miller 		p = peer->metrics;
10906582540SDavid S. Miller 		if (inet_metrics_new(peer))
11006582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11106582540SDavid S. Miller 
11206582540SDavid S. Miller 		new = (unsigned long) p;
11306582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11406582540SDavid S. Miller 
11506582540SDavid S. Miller 		if (prev != old) {
11606582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
11706582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
11806582540SDavid S. Miller 				p = NULL;
11906582540SDavid S. Miller 		}
12006582540SDavid S. Miller 	}
12106582540SDavid S. Miller 	return p;
12206582540SDavid S. Miller }
12306582540SDavid S. Miller 
124f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
125f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
126f894cbf8SDavid S. Miller 					     const void *daddr)
12739232973SDavid S. Miller {
12839232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
12939232973SDavid S. Miller 
130a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13139232973SDavid S. Miller 		return (const void *) p;
132f894cbf8SDavid S. Miller 	else if (skb)
133f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13439232973SDavid S. Miller 	return daddr;
13539232973SDavid S. Miller }
13639232973SDavid S. Miller 
137f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
138f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
139f894cbf8SDavid S. Miller 					  const void *daddr)
140d3aaeb38SDavid S. Miller {
14139232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14239232973SDavid S. Miller 	struct neighbour *n;
14339232973SDavid S. Miller 
144f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
14539232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
146f83c7790SDavid S. Miller 	if (n)
147f83c7790SDavid S. Miller 		return n;
148f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
149f83c7790SDavid S. Miller }
150f83c7790SDavid S. Miller 
1518ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
152f83c7790SDavid S. Miller {
1538ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1548ade06c6SDavid S. Miller 	if (!n) {
1558ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
156f83c7790SDavid S. Miller 		if (IS_ERR(n))
157f83c7790SDavid S. Miller 			return PTR_ERR(n);
1588ade06c6SDavid S. Miller 	}
15997cac082SDavid S. Miller 	rt->n = n;
160f83c7790SDavid S. Miller 
161f83c7790SDavid S. Miller 	return 0;
162d3aaeb38SDavid S. Miller }
163d3aaeb38SDavid S. Miller 
1649a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1651da177e4SLinus Torvalds 	.family			=	AF_INET6,
16609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1671da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1681da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1691da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1700dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
171ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
17206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1731da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1741da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1751da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1761da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1771da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
178*6e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1791ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
180d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1811da177e4SLinus Torvalds };
1821da177e4SLinus Torvalds 
183ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
184ec831ea7SRoland Dreier {
185618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
186618f9bc7SSteffen Klassert 
187618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
188ec831ea7SRoland Dreier }
189ec831ea7SRoland Dreier 
19014e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
19114e50e57SDavid S. Miller {
19214e50e57SDavid S. Miller }
19314e50e57SDavid S. Miller 
1940972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1950972ddb2SHeld Bernhard 					 unsigned long old)
1960972ddb2SHeld Bernhard {
1970972ddb2SHeld Bernhard 	return NULL;
1980972ddb2SHeld Bernhard }
1990972ddb2SHeld Bernhard 
20014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
20114e50e57SDavid S. Miller 	.family			=	AF_INET6,
20209640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20314e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20414e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
205ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
206214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20714e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
2080972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
209d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
21014e50e57SDavid S. Miller };
21114e50e57SDavid S. Miller 
21262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21362fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
21462fa8a84SDavid S. Miller };
21562fa8a84SDavid S. Miller 
216bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
2171da177e4SLinus Torvalds 	.dst = {
2181da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2191da177e4SLinus Torvalds 		.__use		= 1,
2201da177e4SLinus Torvalds 		.obsolete	= -1,
2211da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2221da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2231da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2241da177e4SLinus Torvalds 	},
2251da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2264f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2271da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2281da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
231101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
232101367c2SThomas Graf 
2336723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2346723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2356723ab54SDavid S. Miller 
236280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
237101367c2SThomas Graf 	.dst = {
238101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
239101367c2SThomas Graf 		.__use		= 1,
240101367c2SThomas Graf 		.obsolete	= -1,
241101367c2SThomas Graf 		.error		= -EACCES,
2429ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2439ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
244101367c2SThomas Graf 	},
245101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2464f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
247101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
248101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
249101367c2SThomas Graf };
250101367c2SThomas Graf 
251bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
252101367c2SThomas Graf 	.dst = {
253101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
254101367c2SThomas Graf 		.__use		= 1,
255101367c2SThomas Graf 		.obsolete	= -1,
256101367c2SThomas Graf 		.error		= -EINVAL,
257352e512cSHerbert Xu 		.input		= dst_discard,
258352e512cSHerbert Xu 		.output		= dst_discard,
259101367c2SThomas Graf 	},
260101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2614f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
262101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
263101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
264101367c2SThomas Graf };
265101367c2SThomas Graf 
266101367c2SThomas Graf #endif
267101367c2SThomas Graf 
2681da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26997bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
270957c665fSDavid S. Miller 					     struct net_device *dev,
2718b96d22dSDavid S. Miller 					     int flags,
2728b96d22dSDavid S. Miller 					     struct fib6_table *table)
2731da177e4SLinus Torvalds {
27497bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
27597bab73fSDavid S. Miller 					0, 0, flags);
276cf911662SDavid S. Miller 
27797bab73fSDavid S. Miller 	if (rt) {
278a2de86f6SSteffen Klassert 		memset(&rt->n, 0,
279fbe58186SMadalin Bucur 		       sizeof(*rt) - sizeof(struct dst_entry));
2808b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
28197bab73fSDavid S. Miller 	}
282cf911662SDavid S. Miller 	return rt;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2881da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2891da177e4SLinus Torvalds 
29097cac082SDavid S. Miller 	if (rt->n)
29197cac082SDavid S. Miller 		neigh_release(rt->n);
29297cac082SDavid S. Miller 
2938e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2948e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2958e2ec639SYan, Zheng 
29638308473SDavid S. Miller 	if (idev) {
2971da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2981da177e4SLinus Torvalds 		in6_dev_put(idev);
2991da177e4SLinus Torvalds 	}
3001716a961SGao feng 
3011716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
3021716a961SGao feng 		dst_release(dst->from);
3031716a961SGao feng 
30497bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
30597bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
306b3419363SDavid S. Miller 		inet_putpeer(peer);
307b3419363SDavid S. Miller 	}
308b3419363SDavid S. Miller }
309b3419363SDavid S. Miller 
3106431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3116431cbc2SDavid S. Miller 
3126431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3136431cbc2SDavid S. Miller {
3146431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3156431cbc2SDavid S. Miller }
3166431cbc2SDavid S. Miller 
317b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
318b3419363SDavid S. Miller {
31997bab73fSDavid S. Miller 	struct inet_peer_base *base;
320b3419363SDavid S. Miller 	struct inet_peer *peer;
321b3419363SDavid S. Miller 
32297bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
32397bab73fSDavid S. Miller 	if (!base)
32497bab73fSDavid S. Miller 		return;
32597bab73fSDavid S. Miller 
32697bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3277b34ca2aSDavid S. Miller 	if (peer) {
32897bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
329b3419363SDavid S. Miller 			inet_putpeer(peer);
3306431cbc2SDavid S. Miller 		else
3316431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
3321da177e4SLinus Torvalds 	}
3337b34ca2aSDavid S. Miller }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3361da177e4SLinus Torvalds 			   int how)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3391da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3405a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
341c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3421da177e4SLinus Torvalds 
34397cac082SDavid S. Miller 	if (dev != loopback_dev) {
34497cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3455a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3465a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
34738308473SDavid S. Miller 			if (loopback_idev) {
3481da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3491da177e4SLinus Torvalds 				in6_dev_put(idev);
3501da177e4SLinus Torvalds 			}
3511da177e4SLinus Torvalds 		}
35297cac082SDavid S. Miller 		if (rt->n && rt->n->dev == dev) {
35397cac082SDavid S. Miller 			rt->n->dev = loopback_dev;
35497cac082SDavid S. Miller 			dev_hold(loopback_dev);
35597cac082SDavid S. Miller 			dev_put(dev);
35697cac082SDavid S. Miller 		}
35797cac082SDavid S. Miller 	}
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
360a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3611da177e4SLinus Torvalds {
3621716a961SGao feng 	struct rt6_info *ort = NULL;
3631716a961SGao feng 
3641716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3651716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
366a50feda5SEric Dumazet 			return true;
3671716a961SGao feng 	} else if (rt->dst.from) {
3681716a961SGao feng 		ort = (struct rt6_info *) rt->dst.from;
3691716a961SGao feng 		return (ort->rt6i_flags & RTF_EXPIRES) &&
3701716a961SGao feng 			time_after(jiffies, ort->dst.expires);
3711716a961SGao feng 	}
372a50feda5SEric Dumazet 	return false;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
375a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
376c71099acSThomas Graf {
377a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
378a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
379c71099acSThomas Graf }
380c71099acSThomas Graf 
3811da177e4SLinus Torvalds /*
382c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3831da177e4SLinus Torvalds  */
3841da177e4SLinus Torvalds 
3858ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3868ed67789SDaniel Lezcano 						    struct rt6_info *rt,
387b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3881da177e4SLinus Torvalds 						    int oif,
389d420895eSYOSHIFUJI Hideaki 						    int flags)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3921da177e4SLinus Torvalds 	struct rt6_info *sprt;
3931da177e4SLinus Torvalds 
394dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
395dd3abc4eSYOSHIFUJI Hideaki 		goto out;
396dd3abc4eSYOSHIFUJI Hideaki 
397d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
398d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
399dd3abc4eSYOSHIFUJI Hideaki 
400dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4011da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4021da177e4SLinus Torvalds 				return sprt;
4031da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
40438308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4051da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
406d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4071da177e4SLinus Torvalds 						continue;
4081da177e4SLinus Torvalds 					if (local && (!oif ||
4091da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4101da177e4SLinus Torvalds 						continue;
4111da177e4SLinus Torvalds 				}
4121da177e4SLinus Torvalds 				local = sprt;
4131da177e4SLinus Torvalds 			}
414dd3abc4eSYOSHIFUJI Hideaki 		} else {
415dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
416dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
417dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
418dd3abc4eSYOSHIFUJI Hideaki 		}
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
421dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4221da177e4SLinus Torvalds 		if (local)
4231da177e4SLinus Torvalds 			return local;
4241da177e4SLinus Torvalds 
425d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4268ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4271da177e4SLinus Torvalds 	}
428dd3abc4eSYOSHIFUJI Hideaki out:
4291da177e4SLinus Torvalds 	return rt;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
43227097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
43327097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
43427097255SYOSHIFUJI Hideaki {
435f2c31e32SEric Dumazet 	struct neighbour *neigh;
43627097255SYOSHIFUJI Hideaki 	/*
43727097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
43827097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
43927097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
44027097255SYOSHIFUJI Hideaki 	 *
44127097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
44227097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
44327097255SYOSHIFUJI Hideaki 	 */
444f2c31e32SEric Dumazet 	rcu_read_lock();
44597cac082SDavid S. Miller 	neigh = rt ? rt->n : NULL;
44627097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
447f2c31e32SEric Dumazet 		goto out;
44827097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
44927097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
45052e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
45127097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
45227097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
45327097255SYOSHIFUJI Hideaki 
45427097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
45527097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
45627097255SYOSHIFUJI Hideaki 
45727097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
45827097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
459d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
460f2c31e32SEric Dumazet 	} else {
46127097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
46227097255SYOSHIFUJI Hideaki 	}
463f2c31e32SEric Dumazet out:
464f2c31e32SEric Dumazet 	rcu_read_unlock();
465f2c31e32SEric Dumazet }
46627097255SYOSHIFUJI Hideaki #else
46727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
46827097255SYOSHIFUJI Hideaki {
46927097255SYOSHIFUJI Hideaki }
47027097255SYOSHIFUJI Hideaki #endif
47127097255SYOSHIFUJI Hideaki 
4721da177e4SLinus Torvalds /*
473554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4741da177e4SLinus Torvalds  */
475b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4761da177e4SLinus Torvalds {
477d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
478161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
479554cfb7eSYOSHIFUJI Hideaki 		return 2;
480161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
481161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
482161980f4SDavid S. Miller 		return 1;
483554cfb7eSYOSHIFUJI Hideaki 	return 0;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
486b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4871da177e4SLinus Torvalds {
488f2c31e32SEric Dumazet 	struct neighbour *neigh;
489398bcbebSYOSHIFUJI Hideaki 	int m;
490f2c31e32SEric Dumazet 
491f2c31e32SEric Dumazet 	rcu_read_lock();
49297cac082SDavid S. Miller 	neigh = rt->n;
4934d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4944d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4954d0c5911SYOSHIFUJI Hideaki 		m = 1;
4964d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4971da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
498554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4994d0c5911SYOSHIFUJI Hideaki 			m = 2;
500398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
501398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
502398bcbebSYOSHIFUJI Hideaki 			m = 0;
503398bcbebSYOSHIFUJI Hideaki #endif
504398bcbebSYOSHIFUJI Hideaki 		else
505ea73ee23SYOSHIFUJI Hideaki 			m = 1;
5061da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
507398bcbebSYOSHIFUJI Hideaki 	} else
508398bcbebSYOSHIFUJI Hideaki 		m = 0;
509f2c31e32SEric Dumazet 	rcu_read_unlock();
510554cfb7eSYOSHIFUJI Hideaki 	return m;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
513554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
514554cfb7eSYOSHIFUJI Hideaki 			   int strict)
515554cfb7eSYOSHIFUJI Hideaki {
5164d0c5911SYOSHIFUJI Hideaki 	int m, n;
5174d0c5911SYOSHIFUJI Hideaki 
5184d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
51977d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
520554cfb7eSYOSHIFUJI Hideaki 		return -1;
521ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
522ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
523ebacaaa0SYOSHIFUJI Hideaki #endif
5244d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
525557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
526554cfb7eSYOSHIFUJI Hideaki 		return -1;
527554cfb7eSYOSHIFUJI Hideaki 	return m;
528554cfb7eSYOSHIFUJI Hideaki }
529554cfb7eSYOSHIFUJI Hideaki 
530f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
531f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
532554cfb7eSYOSHIFUJI Hideaki {
533554cfb7eSYOSHIFUJI Hideaki 	int m;
534554cfb7eSYOSHIFUJI Hideaki 
535554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
536f11e6659SDavid S. Miller 		goto out;
537554cfb7eSYOSHIFUJI Hideaki 
538554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
539554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
540f11e6659SDavid S. Miller 		goto out;
541554cfb7eSYOSHIFUJI Hideaki 
542f11e6659SDavid S. Miller 	if (m > *mpri) {
543ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
54427097255SYOSHIFUJI Hideaki 			rt6_probe(match);
545f11e6659SDavid S. Miller 		*mpri = m;
546554cfb7eSYOSHIFUJI Hideaki 		match = rt;
547ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
54827097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5491da177e4SLinus Torvalds 	}
550f11e6659SDavid S. Miller 
551f11e6659SDavid S. Miller out:
552f11e6659SDavid S. Miller 	return match;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
555f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
556f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
557f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
558f11e6659SDavid S. Miller {
559f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
560f11e6659SDavid S. Miller 	int mpri = -1;
561f11e6659SDavid S. Miller 
562f11e6659SDavid S. Miller 	match = NULL;
563f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
564d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
565f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
566f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
567d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
568f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
569f11e6659SDavid S. Miller 
570f11e6659SDavid S. Miller 	return match;
571f11e6659SDavid S. Miller }
572f11e6659SDavid S. Miller 
573f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
574f11e6659SDavid S. Miller {
575f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5768ed67789SDaniel Lezcano 	struct net *net;
577f11e6659SDavid S. Miller 
578f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
579f11e6659SDavid S. Miller 	if (!rt0)
580f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
581f11e6659SDavid S. Miller 
582f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
583f11e6659SDavid S. Miller 
584554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
585f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
586d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
587f11e6659SDavid S. Miller 
588554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
589f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
590f11e6659SDavid S. Miller 			next = fn->leaf;
591f11e6659SDavid S. Miller 
592f11e6659SDavid S. Miller 		if (next != rt0)
593f11e6659SDavid S. Miller 			fn->rr_ptr = next;
594554cfb7eSYOSHIFUJI Hideaki 	}
595554cfb7eSYOSHIFUJI Hideaki 
596d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
597a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
60070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
60170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
602b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
60370ceb4f5SYOSHIFUJI Hideaki {
604c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
60570ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
60670ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
60770ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6084bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
60970ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
61070ceb4f5SYOSHIFUJI Hideaki 
61170ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
61270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
61370ceb4f5SYOSHIFUJI Hideaki 	}
61470ceb4f5SYOSHIFUJI Hideaki 
61570ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
61670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
61770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
61870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
61970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
62070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
62170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
62270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
62370ceb4f5SYOSHIFUJI Hideaki 		}
62470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
62570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
62670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
62770ceb4f5SYOSHIFUJI Hideaki 		}
62870ceb4f5SYOSHIFUJI Hideaki 	}
62970ceb4f5SYOSHIFUJI Hideaki 
63070ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
63170ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6323933fc95SJens Rosenboom 		return -EINVAL;
63370ceb4f5SYOSHIFUJI Hideaki 
6344bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
63570ceb4f5SYOSHIFUJI Hideaki 
63670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
63770ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
63870ceb4f5SYOSHIFUJI Hideaki 	else {
63970ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
64070ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
64170ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
64270ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
64370ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
64470ceb4f5SYOSHIFUJI Hideaki 	}
64570ceb4f5SYOSHIFUJI Hideaki 
646efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
647efa2cea0SDaniel Lezcano 				dev->ifindex);
64870ceb4f5SYOSHIFUJI Hideaki 
64970ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
650e0a1ad73SThomas Graf 		ip6_del_rt(rt);
65170ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
65270ceb4f5SYOSHIFUJI Hideaki 	}
65370ceb4f5SYOSHIFUJI Hideaki 
65470ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
655efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
65670ceb4f5SYOSHIFUJI Hideaki 					pref);
65770ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
65870ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
65970ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
66070ceb4f5SYOSHIFUJI Hideaki 
66170ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6621716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
6631716a961SGao feng 			rt6_clean_expires(rt);
6641716a961SGao feng 		else
6651716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
6661716a961SGao feng 
667d8d1f30bSChangli Gao 		dst_release(&rt->dst);
66870ceb4f5SYOSHIFUJI Hideaki 	}
66970ceb4f5SYOSHIFUJI Hideaki 	return 0;
67070ceb4f5SYOSHIFUJI Hideaki }
67170ceb4f5SYOSHIFUJI Hideaki #endif
67270ceb4f5SYOSHIFUJI Hideaki 
6738ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
674982f56f3SYOSHIFUJI Hideaki do { \
6758ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
676982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
677e0eda7bbSVille Nuorvala 		while (1) { \
678982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
679c71099acSThomas Graf 				goto out; \
680982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
681982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6828bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
683982f56f3SYOSHIFUJI Hideaki 			else \
684982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
685c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
686c71099acSThomas Graf 				goto restart; \
687c71099acSThomas Graf 		} \
688982f56f3SYOSHIFUJI Hideaki 	} \
689982f56f3SYOSHIFUJI Hideaki } while (0)
690c71099acSThomas Graf 
6918ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6928ed67789SDaniel Lezcano 					     struct fib6_table *table,
6934c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	struct fib6_node *fn;
6961da177e4SLinus Torvalds 	struct rt6_info *rt;
6971da177e4SLinus Torvalds 
698c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6994c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
700c71099acSThomas Graf restart:
701c71099acSThomas Graf 	rt = fn->leaf;
7024c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
7034c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
704c71099acSThomas Graf out:
705d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
706c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7071da177e4SLinus Torvalds 	return rt;
708c71099acSThomas Graf 
709c71099acSThomas Graf }
710c71099acSThomas Graf 
711ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
712ea6e574eSFlorian Westphal 				    int flags)
713ea6e574eSFlorian Westphal {
714ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
715ea6e574eSFlorian Westphal }
716ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
717ea6e574eSFlorian Westphal 
7189acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7199acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
720c71099acSThomas Graf {
7214c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7224c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7234c9483b2SDavid S. Miller 		.daddr = *daddr,
724c71099acSThomas Graf 	};
725c71099acSThomas Graf 	struct dst_entry *dst;
72677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
727c71099acSThomas Graf 
728adaa70bbSThomas Graf 	if (saddr) {
7294c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
730adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
731adaa70bbSThomas Graf 	}
732adaa70bbSThomas Graf 
7334c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
734c71099acSThomas Graf 	if (dst->error == 0)
735c71099acSThomas Graf 		return (struct rt6_info *) dst;
736c71099acSThomas Graf 
737c71099acSThomas Graf 	dst_release(dst);
738c71099acSThomas Graf 
7391da177e4SLinus Torvalds 	return NULL;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7437159039aSYOSHIFUJI Hideaki 
744c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7451da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7461da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7471da177e4SLinus Torvalds    be destroyed.
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds 
75086872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	int err;
753c71099acSThomas Graf 	struct fib6_table *table;
7541da177e4SLinus Torvalds 
755c71099acSThomas Graf 	table = rt->rt6i_table;
756c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
75786872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
758c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	return err;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
76340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
76440e22e8fSThomas Graf {
7654d1169c1SDenis V. Lunev 	struct nl_info info = {
766d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
7674d1169c1SDenis V. Lunev 	};
768528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
76940e22e8fSThomas Graf }
77040e22e8fSThomas Graf 
7711716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
77221efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
773b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct rt6_info *rt;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	/*
7781da177e4SLinus Torvalds 	 *	Clone the route.
7791da177e4SLinus Torvalds 	 */
7801da177e4SLinus Torvalds 
78121efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	if (rt) {
78414deae41SDavid S. Miller 		int attempts = !in_softirq();
78514deae41SDavid S. Miller 
78658c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
787bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
78821efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
78958c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7904e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
79158c4fb86SYOSHIFUJI Hideaki 		}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7961da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7974e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
7981da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7991da177e4SLinus Torvalds 		}
8001da177e4SLinus Torvalds #endif
8011da177e4SLinus Torvalds 
80214deae41SDavid S. Miller 	retry:
8038ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
804d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
80514deae41SDavid S. Miller 			int saved_rt_min_interval =
80614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
80714deae41SDavid S. Miller 			int saved_rt_elasticity =
80814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
80914deae41SDavid S. Miller 
81014deae41SDavid S. Miller 			if (attempts-- > 0) {
81114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
81214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
81314deae41SDavid S. Miller 
81486393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
81514deae41SDavid S. Miller 
81614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
81714deae41SDavid S. Miller 					saved_rt_elasticity;
81814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
81914deae41SDavid S. Miller 					saved_rt_min_interval;
82014deae41SDavid S. Miller 				goto retry;
82114deae41SDavid S. Miller 			}
82214deae41SDavid S. Miller 
823f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
824d8d1f30bSChangli Gao 			dst_free(&rt->dst);
82514deae41SDavid S. Miller 			return NULL;
82614deae41SDavid S. Miller 		}
82795a9a5baSYOSHIFUJI Hideaki 	}
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	return rt;
8301da177e4SLinus Torvalds }
83195a9a5baSYOSHIFUJI Hideaki 
83221efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
83321efcfa0SEric Dumazet 					const struct in6_addr *daddr)
834299d9939SYOSHIFUJI Hideaki {
83521efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
83621efcfa0SEric Dumazet 
837299d9939SYOSHIFUJI Hideaki 	if (rt) {
838299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
83997cac082SDavid S. Miller 		rt->n = neigh_clone(ort->n);
840299d9939SYOSHIFUJI Hideaki 	}
841299d9939SYOSHIFUJI Hideaki 	return rt;
842299d9939SYOSHIFUJI Hideaki }
843299d9939SYOSHIFUJI Hideaki 
8448ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8454c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	struct fib6_node *fn;
848519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
849c71099acSThomas Graf 	int strict = 0;
8501da177e4SLinus Torvalds 	int attempts = 3;
851519fbd87SYOSHIFUJI Hideaki 	int err;
85253b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8531da177e4SLinus Torvalds 
85477d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds relookup:
857c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8581da177e4SLinus Torvalds 
8598238dd06SYOSHIFUJI Hideaki restart_2:
8604c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds restart:
8634acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8648ed67789SDaniel Lezcano 
8654c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8668ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8678238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8681da177e4SLinus Torvalds 		goto out;
8691da177e4SLinus Torvalds 
870d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
871c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8721da177e4SLinus Torvalds 
87397cac082SDavid S. Miller 	if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP))
8744c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8757343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8764c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8777343ff31SDavid S. Miller 	else
8787343ff31SDavid S. Miller 		goto out2;
8791da177e4SLinus Torvalds 
880d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8818ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8821da177e4SLinus Torvalds 
883d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
884e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
88540e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
886e40cf353SYOSHIFUJI Hideaki 		if (!err)
887e40cf353SYOSHIFUJI Hideaki 			goto out2;
888e40cf353SYOSHIFUJI Hideaki 	}
889e40cf353SYOSHIFUJI Hideaki 
890e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8911da177e4SLinus Torvalds 		goto out2;
8921da177e4SLinus Torvalds 
893519fbd87SYOSHIFUJI Hideaki 	/*
894c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
895519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8961da177e4SLinus Torvalds 	 */
897d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8981da177e4SLinus Torvalds 	goto relookup;
899e40cf353SYOSHIFUJI Hideaki 
900519fbd87SYOSHIFUJI Hideaki out:
9018238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9028238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9038238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9048238dd06SYOSHIFUJI Hideaki 	}
905d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
906c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9071da177e4SLinus Torvalds out2:
908d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
909d8d1f30bSChangli Gao 	rt->dst.__use++;
910c71099acSThomas Graf 
911c71099acSThomas Graf 	return rt;
912c71099acSThomas Graf }
913c71099acSThomas Graf 
9148ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9154c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9164acad72dSPavel Emelyanov {
9174c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9184acad72dSPavel Emelyanov }
9194acad72dSPavel Emelyanov 
92072331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
92172331bc0SShmulik Ladkani 						struct net_device *dev,
92272331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
92372331bc0SShmulik Ladkani {
92472331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
92572331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
92672331bc0SShmulik Ladkani 
92772331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
92872331bc0SShmulik Ladkani }
92972331bc0SShmulik Ladkani 
930c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
931c71099acSThomas Graf {
932b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
933c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
934adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9354c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9364c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9374c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9384c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9394c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
9404c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9414c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
942c71099acSThomas Graf 	};
943adaa70bbSThomas Graf 
94472331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
945c71099acSThomas Graf }
946c71099acSThomas Graf 
9478ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9484c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
949c71099acSThomas Graf {
9504c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
951c71099acSThomas Graf }
952c71099acSThomas Graf 
9539c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9544c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
955c71099acSThomas Graf {
956c71099acSThomas Graf 	int flags = 0;
957c71099acSThomas Graf 
9584dc27d1cSDavid McCullough 	fl6->flowi6_iif = net->loopback_dev->ifindex;
9594dc27d1cSDavid McCullough 
9604c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
96177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
962c71099acSThomas Graf 
9634c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
964adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9650c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9660c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
967adaa70bbSThomas Graf 
9684c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9717159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9721da177e4SLinus Torvalds 
9732774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
97414e50e57SDavid S. Miller {
9755c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
97614e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
97714e50e57SDavid S. Miller 
9785c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
97914e50e57SDavid S. Miller 	if (rt) {
980cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
98197bab73fSDavid S. Miller 		rt6_init_peer(rt, net->ipv6.peers);
982cf911662SDavid S. Miller 
983d8d1f30bSChangli Gao 		new = &rt->dst;
98414e50e57SDavid S. Miller 
98514e50e57SDavid S. Miller 		new->__use = 1;
986352e512cSHerbert Xu 		new->input = dst_discard;
987352e512cSHerbert Xu 		new->output = dst_discard;
98814e50e57SDavid S. Miller 
98921efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
99021efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
99121efcfa0SEric Dumazet 		else
992defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
99314e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
99414e50e57SDavid S. Miller 		if (rt->rt6i_idev)
99514e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
99614e50e57SDavid S. Miller 
9974e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
9981716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
9991716a961SGao feng 		rt6_clean_expires(rt);
100014e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
100114e50e57SDavid S. Miller 
100214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
100314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
100414e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
100514e50e57SDavid S. Miller #endif
100614e50e57SDavid S. Miller 
100714e50e57SDavid S. Miller 		dst_free(new);
100814e50e57SDavid S. Miller 	}
100914e50e57SDavid S. Miller 
101069ead7afSDavid S. Miller 	dst_release(dst_orig);
101169ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
101214e50e57SDavid S. Miller }
101314e50e57SDavid S. Miller 
10141da177e4SLinus Torvalds /*
10151da177e4SLinus Torvalds  *	Destination cache support functions
10161da177e4SLinus Torvalds  */
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	struct rt6_info *rt;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10231da177e4SLinus Torvalds 
10246431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
10256431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
102697bab73fSDavid S. Miller 			if (!rt6_has_peer(rt))
10276431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
10286431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
10296431cbc2SDavid S. Miller 		}
10301da177e4SLinus Torvalds 		return dst;
10316431cbc2SDavid S. Miller 	}
10321da177e4SLinus Torvalds 	return NULL;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	if (rt) {
104054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
104154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1042e0a1ad73SThomas Graf 				ip6_del_rt(rt);
104354c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10441da177e4SLinus Torvalds 			}
104554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
104654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
104754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
104854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
104954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
105054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	struct rt6_info *rt;
10561da177e4SLinus Torvalds 
10573ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10581da177e4SLinus Torvalds 
1059adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10601da177e4SLinus Torvalds 	if (rt) {
10611716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10621716a961SGao feng 			rt6_update_expires(rt, 0);
10631716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10641da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10711da177e4SLinus Torvalds 
107281aded24SDavid S. Miller 	dst_confirm(dst);
10731da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
107481aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
107581aded24SDavid S. Miller 
10761da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10771da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1078defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10791da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1080defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1081defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10821da177e4SLinus Torvalds 		}
1083defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
108481aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
108842ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
108942ae66c8SDavid S. Miller 		     int oif, u32 mark)
109081aded24SDavid S. Miller {
109181aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
109281aded24SDavid S. Miller 	struct dst_entry *dst;
109381aded24SDavid S. Miller 	struct flowi6 fl6;
109481aded24SDavid S. Miller 
109581aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
109681aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
109781aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
10983e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
109981aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
110081aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
110181aded24SDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
110281aded24SDavid S. Miller 
110381aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
110481aded24SDavid S. Miller 	if (!dst->error)
110581aded24SDavid S. Miller 		ip6_rt_update_pmtu(dst, ntohl(mtu));
110681aded24SDavid S. Miller 	dst_release(dst);
110781aded24SDavid S. Miller }
110881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
110981aded24SDavid S. Miller 
111081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
111181aded24SDavid S. Miller {
111281aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
111381aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
111481aded24SDavid S. Miller }
111581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
111681aded24SDavid S. Miller 
11170dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11181da177e4SLinus Torvalds {
11190dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11200dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11210dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11220dbaee3bSDavid S. Miller 
11231da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11241da177e4SLinus Torvalds 
11255578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11265578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	/*
11291da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11301da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11311da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11321da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11331da177e4SLinus Torvalds 	 */
11341da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11351da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11361da177e4SLinus Torvalds 	return mtu;
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
1139ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1140d33e4553SDavid S. Miller {
1141d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1142618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1143618f9bc7SSteffen Klassert 
1144618f9bc7SSteffen Klassert 	if (mtu)
1145618f9bc7SSteffen Klassert 		return mtu;
1146618f9bc7SSteffen Klassert 
1147618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1148d33e4553SDavid S. Miller 
1149d33e4553SDavid S. Miller 	rcu_read_lock();
1150d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1151d33e4553SDavid S. Miller 	if (idev)
1152d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1153d33e4553SDavid S. Miller 	rcu_read_unlock();
1154d33e4553SDavid S. Miller 
1155d33e4553SDavid S. Miller 	return mtu;
1156d33e4553SDavid S. Miller }
1157d33e4553SDavid S. Miller 
11583b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
11593b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
11605d0bbeebSThomas Graf 
11613b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
11621da177e4SLinus Torvalds 				  struct neighbour *neigh,
116387a11578SDavid S. Miller 				  struct flowi6 *fl6)
11641da177e4SLinus Torvalds {
116587a11578SDavid S. Miller 	struct dst_entry *dst;
11661da177e4SLinus Torvalds 	struct rt6_info *rt;
11671da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1168c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
11691da177e4SLinus Torvalds 
117038308473SDavid S. Miller 	if (unlikely(!idev))
1171122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
11721da177e4SLinus Torvalds 
11738b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
117438308473SDavid S. Miller 	if (unlikely(!rt)) {
11751da177e4SLinus Torvalds 		in6_dev_put(idev);
117687a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
11771da177e4SLinus Torvalds 		goto out;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	if (neigh)
11811da177e4SLinus Torvalds 		neigh_hold(neigh);
118214deae41SDavid S. Miller 	else {
1183f894cbf8SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
1184b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1185252c3d84SRongQing.Li 			in6_dev_put(idev);
1186b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1187b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1188b43faac6SDavid S. Miller 		}
118914deae41SDavid S. Miller 	}
11901da177e4SLinus Torvalds 
11918e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
11928e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
119397cac082SDavid S. Miller 	rt->n = neigh;
1194d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
119587a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
11968e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
11978e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
11987011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
11991da177e4SLinus Torvalds 
12003b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1201d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1202d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12033b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12041da177e4SLinus Torvalds 
12055578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12061da177e4SLinus Torvalds 
120787a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
120887a11578SDavid S. Miller 
12091da177e4SLinus Torvalds out:
121087a11578SDavid S. Miller 	return dst;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
12133d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12141da177e4SLinus Torvalds {
1215e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12163d0f24a7SStephen Hemminger 	int more = 0;
12171da177e4SLinus Torvalds 
12183b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12193b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12205d0bbeebSThomas Graf 
12211da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12221da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12231da177e4SLinus Torvalds 			*pprev = dst->next;
12241da177e4SLinus Torvalds 			dst_free(dst);
12251da177e4SLinus Torvalds 		} else {
12261da177e4SLinus Torvalds 			pprev = &dst->next;
12273d0f24a7SStephen Hemminger 			++more;
12281da177e4SLinus Torvalds 		}
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12313b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12325d0bbeebSThomas Graf 
12333d0f24a7SStephen Hemminger 	return more;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12371e493d19SDavid S. Miller 			    void *arg)
12381e493d19SDavid S. Miller {
12391e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12401e493d19SDavid S. Miller 
12411e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12421e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12431e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12441e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12451e493d19SDavid S. Miller 		if (func(rt, arg)) {
12461e493d19SDavid S. Miller 			*pprev = dst->next;
12471e493d19SDavid S. Miller 			dst_free(dst);
12481e493d19SDavid S. Miller 		} else {
12491e493d19SDavid S. Miller 			pprev = &dst->next;
12501e493d19SDavid S. Miller 		}
12511e493d19SDavid S. Miller 	}
12521e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12531e493d19SDavid S. Miller }
12541e493d19SDavid S. Miller 
1255569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	unsigned long now = jiffies;
125886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12597019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12607019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12617019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12627019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12637019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1264fc66f95cSEric Dumazet 	int entries;
12651da177e4SLinus Torvalds 
1266fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12677019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1268fc66f95cSEric Dumazet 	    entries <= rt_max_size)
12691da177e4SLinus Torvalds 		goto out;
12701da177e4SLinus Torvalds 
12716891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
12726891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
12736891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1274fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1275fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
12767019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
12771da177e4SLinus Torvalds out:
12787019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1279fc66f95cSEric Dumazet 	return entries > rt_max_size;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
12831da177e4SLinus Torvalds    but results in cleaner routing tables.
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds    Remove it only when all the things will work!
12861da177e4SLinus Torvalds  */
12871da177e4SLinus Torvalds 
12886b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
12891da177e4SLinus Torvalds {
12905170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1291a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
12926b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1293c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1294c68f24ccSEric Dumazet 
1295c68f24ccSEric Dumazet 		rcu_read_lock();
1296c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1297c68f24ccSEric Dumazet 		if (idev)
12981da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1299c68f24ccSEric Dumazet 		else
130053b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1301c68f24ccSEric Dumazet 		rcu_read_unlock();
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 	return hoplimit;
13041da177e4SLinus Torvalds }
1305abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /*
13081da177e4SLinus Torvalds  *
13091da177e4SLinus Torvalds  */
13101da177e4SLinus Torvalds 
131186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	int err;
13145578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13151da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13161da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13171da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1318c71099acSThomas Graf 	struct fib6_table *table;
13191da177e4SLinus Torvalds 	int addr_type;
13201da177e4SLinus Torvalds 
132186872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13221da177e4SLinus Torvalds 		return -EINVAL;
13231da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
132486872cb5SThomas Graf 	if (cfg->fc_src_len)
13251da177e4SLinus Torvalds 		return -EINVAL;
13261da177e4SLinus Torvalds #endif
132786872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13281da177e4SLinus Torvalds 		err = -ENODEV;
13295578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13301da177e4SLinus Torvalds 		if (!dev)
13311da177e4SLinus Torvalds 			goto out;
13321da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13331da177e4SLinus Torvalds 		if (!idev)
13341da177e4SLinus Torvalds 			goto out;
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 
133786872cb5SThomas Graf 	if (cfg->fc_metric == 0)
133886872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13391da177e4SLinus Torvalds 
1340c71099acSThomas Graf 	err = -ENOBUFS;
134138308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1342d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1343d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
134438308473SDavid S. Miller 		if (!table) {
1345f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1346d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1347d71314b4SMatti Vaittinen 		}
1348d71314b4SMatti Vaittinen 	} else {
1349d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1350d71314b4SMatti Vaittinen 	}
135138308473SDavid S. Miller 
135238308473SDavid S. Miller 	if (!table)
1353c71099acSThomas Graf 		goto out;
1354c71099acSThomas Graf 
13558b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13561da177e4SLinus Torvalds 
135738308473SDavid S. Miller 	if (!rt) {
13581da177e4SLinus Torvalds 		err = -ENOMEM;
13591da177e4SLinus Torvalds 		goto out;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
1362d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
13631716a961SGao feng 
13641716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13651716a961SGao feng 		rt6_set_expires(rt, jiffies +
13661716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13671716a961SGao feng 	else
13681716a961SGao feng 		rt6_clean_expires(rt);
13691da177e4SLinus Torvalds 
137086872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
137186872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
137286872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
137386872cb5SThomas Graf 
137486872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1377d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1378ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1379ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
13801da177e4SLinus Torvalds 	else
1381d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
13821da177e4SLinus Torvalds 
1383d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
13841da177e4SLinus Torvalds 
138586872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
138686872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
13871da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
138811d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
13891da177e4SLinus Torvalds 
13908e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
13918e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
13928e2ec639SYan, Zheng 		if (!metrics) {
13938e2ec639SYan, Zheng 			err = -ENOMEM;
13948e2ec639SYan, Zheng 			goto out;
13958e2ec639SYan, Zheng 		}
13968e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
13978e2ec639SYan, Zheng 	}
13981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
139986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
140086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14011da177e4SLinus Torvalds #endif
14021da177e4SLinus Torvalds 
140386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14061da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14071da177e4SLinus Torvalds 	 */
140886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
140938308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
141038308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
141138308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14121da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14135578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14141da177e4SLinus Torvalds 			if (dev) {
14151da177e4SLinus Torvalds 				dev_put(dev);
14161da177e4SLinus Torvalds 				in6_dev_put(idev);
14171da177e4SLinus Torvalds 			}
14185578689aSDaniel Lezcano 			dev = net->loopback_dev;
14191da177e4SLinus Torvalds 			dev_hold(dev);
14201da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14211da177e4SLinus Torvalds 			if (!idev) {
14221da177e4SLinus Torvalds 				err = -ENODEV;
14231da177e4SLinus Torvalds 				goto out;
14241da177e4SLinus Torvalds 			}
14251da177e4SLinus Torvalds 		}
1426d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1427d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1428d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
14291da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
14301da177e4SLinus Torvalds 		goto install_route;
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 
143386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1434b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14351da177e4SLinus Torvalds 		int gwa_type;
14361da177e4SLinus Torvalds 
143786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14384e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14391da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14421da177e4SLinus Torvalds 			struct rt6_info *grt;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14451da177e4SLinus Torvalds 			   addresses as nexthop address.
14461da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14471da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14481da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14491da177e4SLinus Torvalds 			   some exceptions. --ANK
14501da177e4SLinus Torvalds 			 */
14511da177e4SLinus Torvalds 			err = -EINVAL;
14521da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14531da177e4SLinus Torvalds 				goto out;
14541da177e4SLinus Torvalds 
14555578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
145838308473SDavid S. Miller 			if (!grt)
14591da177e4SLinus Torvalds 				goto out;
14601da177e4SLinus Torvalds 			if (dev) {
1461d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1462d8d1f30bSChangli Gao 					dst_release(&grt->dst);
14631da177e4SLinus Torvalds 					goto out;
14641da177e4SLinus Torvalds 				}
14651da177e4SLinus Torvalds 			} else {
1466d1918542SDavid S. Miller 				dev = grt->dst.dev;
14671da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
14681da177e4SLinus Torvalds 				dev_hold(dev);
14691da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
14701da177e4SLinus Torvalds 			}
14711da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
14721da177e4SLinus Torvalds 				err = 0;
1473d8d1f30bSChangli Gao 			dst_release(&grt->dst);
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 			if (err)
14761da177e4SLinus Torvalds 				goto out;
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 		err = -EINVAL;
147938308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
14801da177e4SLinus Torvalds 			goto out;
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	err = -ENODEV;
148438308473SDavid S. Miller 	if (!dev)
14851da177e4SLinus Torvalds 		goto out;
14861da177e4SLinus Torvalds 
1487c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1488c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1489c3968a85SDaniel Walter 			err = -EINVAL;
1490c3968a85SDaniel Walter 			goto out;
1491c3968a85SDaniel Walter 		}
14924e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1493c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1494c3968a85SDaniel Walter 	} else
1495c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1496c3968a85SDaniel Walter 
149786872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
14988ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1499f83c7790SDavid S. Miller 		if (err)
15001da177e4SLinus Torvalds 			goto out;
15011da177e4SLinus Torvalds 	}
15021da177e4SLinus Torvalds 
150386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds install_route:
150686872cb5SThomas Graf 	if (cfg->fc_mx) {
150786872cb5SThomas Graf 		struct nlattr *nla;
150886872cb5SThomas Graf 		int remaining;
15091da177e4SLinus Torvalds 
151086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15118f4c1f9bSThomas Graf 			int type = nla_type(nla);
151286872cb5SThomas Graf 
151386872cb5SThomas Graf 			if (type) {
151486872cb5SThomas Graf 				if (type > RTAX_MAX) {
15151da177e4SLinus Torvalds 					err = -EINVAL;
15161da177e4SLinus Torvalds 					goto out;
15171da177e4SLinus Torvalds 				}
151886872cb5SThomas Graf 
1519defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15201da177e4SLinus Torvalds 			}
15211da177e4SLinus Torvalds 		}
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds 
1524d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15251da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1526c71099acSThomas Graf 	rt->rt6i_table = table;
152763152fc0SDaniel Lezcano 
1528c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
152963152fc0SDaniel Lezcano 
153086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds out:
15331da177e4SLinus Torvalds 	if (dev)
15341da177e4SLinus Torvalds 		dev_put(dev);
15351da177e4SLinus Torvalds 	if (idev)
15361da177e4SLinus Torvalds 		in6_dev_put(idev);
15371da177e4SLinus Torvalds 	if (rt)
1538d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15391da177e4SLinus Torvalds 	return err;
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
154286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15431da177e4SLinus Torvalds {
15441da177e4SLinus Torvalds 	int err;
1545c71099acSThomas Graf 	struct fib6_table *table;
1546d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15471da177e4SLinus Torvalds 
15488ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
15496c813a72SPatrick McHardy 		return -ENOENT;
15506c813a72SPatrick McHardy 
1551c71099acSThomas Graf 	table = rt->rt6i_table;
1552c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
15531da177e4SLinus Torvalds 
155486872cb5SThomas Graf 	err = fib6_del(rt, info);
1555d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15561da177e4SLinus Torvalds 
1557c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	return err;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
1562e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1563e0a1ad73SThomas Graf {
15644d1169c1SDenis V. Lunev 	struct nl_info info = {
1565d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15664d1169c1SDenis V. Lunev 	};
1567528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1568e0a1ad73SThomas Graf }
1569e0a1ad73SThomas Graf 
157086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
15711da177e4SLinus Torvalds {
1572c71099acSThomas Graf 	struct fib6_table *table;
15731da177e4SLinus Torvalds 	struct fib6_node *fn;
15741da177e4SLinus Torvalds 	struct rt6_info *rt;
15751da177e4SLinus Torvalds 	int err = -ESRCH;
15761da177e4SLinus Torvalds 
15775578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
157838308473SDavid S. Miller 	if (!table)
1579c71099acSThomas Graf 		return err;
15801da177e4SLinus Torvalds 
1581c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1582c71099acSThomas Graf 
1583c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
158486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
158586872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	if (fn) {
1588d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
158986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1590d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1591d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
15921da177e4SLinus Torvalds 				continue;
159386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
159486872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
15951da177e4SLinus Torvalds 				continue;
159686872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
15971da177e4SLinus Torvalds 				continue;
1598d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1599c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16001da177e4SLinus Torvalds 
160186872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16021da177e4SLinus Torvalds 		}
16031da177e4SLinus Torvalds 	}
1604c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	return err;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds /*
16101da177e4SLinus Torvalds  *	Handle redirects
16111da177e4SLinus Torvalds  */
1612a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
16134c9483b2SDavid S. Miller 	struct flowi6 fl6;
1614a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1615a6279458SYOSHIFUJI Hideaki };
16161da177e4SLinus Torvalds 
16178ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
16188ed67789SDaniel Lezcano 					     struct fib6_table *table,
16194c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1620a6279458SYOSHIFUJI Hideaki 					     int flags)
1621a6279458SYOSHIFUJI Hideaki {
16224c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1623a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1624a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1625c71099acSThomas Graf 
1626e843b9e1SYOSHIFUJI Hideaki 	/*
1627e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1628e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1629e843b9e1SYOSHIFUJI Hideaki 	 *
1630e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1631e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1632e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1633e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1634e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1635e843b9e1SYOSHIFUJI Hideaki 	 */
16361da177e4SLinus Torvalds 
1637c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
16384c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1639e843b9e1SYOSHIFUJI Hideaki restart:
1640d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
16411da177e4SLinus Torvalds 		/*
16421da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
16431da177e4SLinus Torvalds 		 *
16441da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
16451da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
16461da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
16471da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
16481da177e4SLinus Torvalds 		 */
1649e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1650e843b9e1SYOSHIFUJI Hideaki 			continue;
16511da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1652e843b9e1SYOSHIFUJI Hideaki 			continue;
1653d1918542SDavid S. Miller 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1654e843b9e1SYOSHIFUJI Hideaki 			continue;
1655a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1656e843b9e1SYOSHIFUJI Hideaki 			continue;
1657e843b9e1SYOSHIFUJI Hideaki 		break;
1658e843b9e1SYOSHIFUJI Hideaki 	}
1659a6279458SYOSHIFUJI Hideaki 
1660cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
16618ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
16624c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1663cb15d9c2SYOSHIFUJI Hideaki out:
1664d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1665a6279458SYOSHIFUJI Hideaki 
1666c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16671da177e4SLinus Torvalds 
1668a6279458SYOSHIFUJI Hideaki 	return rt;
1669a6279458SYOSHIFUJI Hideaki };
1670a6279458SYOSHIFUJI Hideaki 
1671b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1672b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1673b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1674a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1675a6279458SYOSHIFUJI Hideaki {
1676adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1677c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1678a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
16794c9483b2SDavid S. Miller 		.fl6 = {
16804c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
16814c9483b2SDavid S. Miller 			.daddr = *dest,
16824c9483b2SDavid S. Miller 			.saddr = *src,
1683a6279458SYOSHIFUJI Hideaki 		},
1684a6279458SYOSHIFUJI Hideaki 	};
1685adaa70bbSThomas Graf 
16864e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
168786c36ce4SBrian Haley 
1688adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1689adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1690a6279458SYOSHIFUJI Hideaki 
16914c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
169258f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1693a6279458SYOSHIFUJI Hideaki }
1694a6279458SYOSHIFUJI Hideaki 
1695*6e157b6aSDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sk_buff *skb)
1696a6279458SYOSHIFUJI Hideaki {
1697e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1698a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1699e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1700e8599ff4SDavid S. Miller 	const struct in6_addr *target;
1701e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1702*6e157b6aSDavid S. Miller 	const struct in6_addr *dest;
1703*6e157b6aSDavid S. Miller 	struct neighbour *old_neigh;
1704e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1705e8599ff4SDavid S. Miller 	struct neighbour *neigh;
1706e8599ff4SDavid S. Miller 	struct icmp6hdr *icmph;
1707*6e157b6aSDavid S. Miller 	int optlen, on_link;
1708*6e157b6aSDavid S. Miller 	u8 *lladdr;
1709e8599ff4SDavid S. Miller 
1710e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
1711e8599ff4SDavid S. Miller 	optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1712e8599ff4SDavid S. Miller 
1713e8599ff4SDavid S. Miller 	if (optlen < 0) {
1714*6e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1715e8599ff4SDavid S. Miller 		return;
1716e8599ff4SDavid S. Miller 	}
1717e8599ff4SDavid S. Miller 
1718e8599ff4SDavid S. Miller 	icmph = icmp6_hdr(skb);
1719e8599ff4SDavid S. Miller 	target = (const struct in6_addr *) (icmph + 1);
1720e8599ff4SDavid S. Miller 	dest = target + 1;
1721e8599ff4SDavid S. Miller 
1722e8599ff4SDavid S. Miller 	if (ipv6_addr_is_multicast(dest)) {
1723*6e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1724e8599ff4SDavid S. Miller 		return;
1725e8599ff4SDavid S. Miller 	}
1726e8599ff4SDavid S. Miller 
1727*6e157b6aSDavid S. Miller 	on_link = 0;
1728e8599ff4SDavid S. Miller 	if (ipv6_addr_equal(dest, target)) {
1729e8599ff4SDavid S. Miller 		on_link = 1;
1730e8599ff4SDavid S. Miller 	} else if (ipv6_addr_type(target) !=
1731e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1732*6e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1733e8599ff4SDavid S. Miller 		return;
1734e8599ff4SDavid S. Miller 	}
1735e8599ff4SDavid S. Miller 
1736e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1737e8599ff4SDavid S. Miller 	if (!in6_dev)
1738e8599ff4SDavid S. Miller 		return;
1739e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1740e8599ff4SDavid S. Miller 		return;
1741e8599ff4SDavid S. Miller 
1742e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1743e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1744e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1745e8599ff4SDavid S. Miller 	 */
1746e8599ff4SDavid S. Miller 
1747e8599ff4SDavid S. Miller 	if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
1748e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1749e8599ff4SDavid S. Miller 		return;
1750e8599ff4SDavid S. Miller 	}
1751*6e157b6aSDavid S. Miller 
1752*6e157b6aSDavid S. Miller 	lladdr = NULL;
1753e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1754e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1755e8599ff4SDavid S. Miller 					     skb->dev);
1756e8599ff4SDavid S. Miller 		if (!lladdr) {
1757e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1758e8599ff4SDavid S. Miller 			return;
1759e8599ff4SDavid S. Miller 		}
1760e8599ff4SDavid S. Miller 	}
1761e8599ff4SDavid S. Miller 
1762*6e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
1763*6e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
1764*6e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1765*6e157b6aSDavid S. Miller 		return;
1766*6e157b6aSDavid S. Miller 	}
1767*6e157b6aSDavid S. Miller 
1768*6e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
1769*6e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
1770*6e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
1771*6e157b6aSDavid S. Miller 	 */
1772*6e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
1773*6e157b6aSDavid S. Miller 
1774e8599ff4SDavid S. Miller 	neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
1775e8599ff4SDavid S. Miller 	if (!neigh)
1776e8599ff4SDavid S. Miller 		return;
1777e8599ff4SDavid S. Miller 
1778*6e157b6aSDavid S. Miller 	/* Duplicate redirect: silently ignore. */
1779*6e157b6aSDavid S. Miller 	old_neigh = rt->n;
1780*6e157b6aSDavid S. Miller 	if (neigh == old_neigh)
1781a6279458SYOSHIFUJI Hideaki 		goto out;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	/*
17841da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17851da177e4SLinus Torvalds 	 */
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17881da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17891da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17901da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17911da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17921da177e4SLinus Torvalds 		     );
17931da177e4SLinus Torvalds 
179421efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
179538308473SDavid S. Miller 	if (!nrt)
17961da177e4SLinus Torvalds 		goto out;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17991da177e4SLinus Torvalds 	if (on_link)
18001da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18011da177e4SLinus Torvalds 
18024e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
180397cac082SDavid S. Miller 	nrt->n = neigh_clone(neigh);
18041da177e4SLinus Torvalds 
180540e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18061da177e4SLinus Torvalds 		goto out;
18071da177e4SLinus Torvalds 
1808d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
18091d248b1cSDavid S. Miller 	netevent.old_neigh = old_neigh;
1810d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
18111d248b1cSDavid S. Miller 	netevent.new_neigh = neigh;
18121d248b1cSDavid S. Miller 	netevent.daddr = dest;
18138d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18148d71740cSTom Tucker 
18151da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1816*6e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1817e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds out:
1821e8599ff4SDavid S. Miller 	neigh_release(neigh);
1822*6e157b6aSDavid S. Miller }
1823*6e157b6aSDavid S. Miller 
1824*6e157b6aSDavid S. Miller void rt6_redirect(struct sk_buff *skb)
1825*6e157b6aSDavid S. Miller {
1826*6e157b6aSDavid S. Miller 	const struct in6_addr *target;
1827*6e157b6aSDavid S. Miller 	const struct in6_addr *dest;
1828*6e157b6aSDavid S. Miller 	const struct in6_addr *src;
1829*6e157b6aSDavid S. Miller 	const struct in6_addr *saddr;
1830*6e157b6aSDavid S. Miller 	struct icmp6hdr *icmph;
1831*6e157b6aSDavid S. Miller 	struct rt6_info *rt;
1832*6e157b6aSDavid S. Miller 
1833*6e157b6aSDavid S. Miller 	icmph = icmp6_hdr(skb);
1834*6e157b6aSDavid S. Miller 	target = (const struct in6_addr *) (icmph + 1);
1835*6e157b6aSDavid S. Miller 	dest = target + 1;
1836*6e157b6aSDavid S. Miller 
1837*6e157b6aSDavid S. Miller 	src = &ipv6_hdr(skb)->daddr;
1838*6e157b6aSDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
1839*6e157b6aSDavid S. Miller 
1840*6e157b6aSDavid S. Miller 	rt = ip6_route_redirect(dest, src, saddr, skb->dev);
1841*6e157b6aSDavid S. Miller 	rt6_do_redirect(&rt->dst, skb);
1842d8d1f30bSChangli Gao 	dst_release(&rt->dst);
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /*
18461da177e4SLinus Torvalds  *	Misc support functions
18471da177e4SLinus Torvalds  */
18481da177e4SLinus Torvalds 
18491716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
185021efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18511da177e4SLinus Torvalds {
1852d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18538b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18548b96d22dSDavid S. Miller 					    ort->rt6i_table);
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	if (rt) {
1857d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1858d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18598e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18601da177e4SLinus Torvalds 
18614e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18628e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1863defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1864d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18651da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18661da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18671da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1868d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18691da177e4SLinus Torvalds 
18704e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18711716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18721716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18731716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18741716a961SGao feng 			rt6_set_from(rt, ort);
18751716a961SGao feng 		else
18761716a961SGao feng 			rt6_clean_expires(rt);
18771da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18801da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18811da177e4SLinus Torvalds #endif
18820f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1883c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 	return rt;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
188870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1889efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1890b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1891b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
189270ceb4f5SYOSHIFUJI Hideaki {
189370ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
189470ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1895c71099acSThomas Graf 	struct fib6_table *table;
189670ceb4f5SYOSHIFUJI Hideaki 
1897efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
189838308473SDavid S. Miller 	if (!table)
1899c71099acSThomas Graf 		return NULL;
1900c71099acSThomas Graf 
1901c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1902c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
190370ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
190470ceb4f5SYOSHIFUJI Hideaki 		goto out;
190570ceb4f5SYOSHIFUJI Hideaki 
1906d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1907d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
190870ceb4f5SYOSHIFUJI Hideaki 			continue;
190970ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
191070ceb4f5SYOSHIFUJI Hideaki 			continue;
191170ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
191270ceb4f5SYOSHIFUJI Hideaki 			continue;
1913d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
191470ceb4f5SYOSHIFUJI Hideaki 		break;
191570ceb4f5SYOSHIFUJI Hideaki 	}
191670ceb4f5SYOSHIFUJI Hideaki out:
1917c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
191870ceb4f5SYOSHIFUJI Hideaki 	return rt;
191970ceb4f5SYOSHIFUJI Hideaki }
192070ceb4f5SYOSHIFUJI Hideaki 
1921efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1922b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1923b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
192495c96174SEric Dumazet 					   unsigned int pref)
192570ceb4f5SYOSHIFUJI Hideaki {
192686872cb5SThomas Graf 	struct fib6_config cfg = {
192786872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1928238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
192986872cb5SThomas Graf 		.fc_ifindex	= ifindex,
193086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
193186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
193286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1933efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1934efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1935efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
193686872cb5SThomas Graf 	};
193770ceb4f5SYOSHIFUJI Hideaki 
19384e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19394e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
194086872cb5SThomas Graf 
1941e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1942e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
194386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
194470ceb4f5SYOSHIFUJI Hideaki 
194586872cb5SThomas Graf 	ip6_route_add(&cfg);
194670ceb4f5SYOSHIFUJI Hideaki 
1947efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
194870ceb4f5SYOSHIFUJI Hideaki }
194970ceb4f5SYOSHIFUJI Hideaki #endif
195070ceb4f5SYOSHIFUJI Hideaki 
1951b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19521da177e4SLinus Torvalds {
19531da177e4SLinus Torvalds 	struct rt6_info *rt;
1954c71099acSThomas Graf 	struct fib6_table *table;
19551da177e4SLinus Torvalds 
1956c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
195738308473SDavid S. Miller 	if (!table)
1958c71099acSThomas Graf 		return NULL;
19591da177e4SLinus Torvalds 
1960c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1961d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1962d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1963045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19641da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19651da177e4SLinus Torvalds 			break;
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 	if (rt)
1968d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1969c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
19701da177e4SLinus Torvalds 	return rt;
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
1973b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1974ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1975ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19761da177e4SLinus Torvalds {
197786872cb5SThomas Graf 	struct fib6_config cfg = {
197886872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1979238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
198086872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
198186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
198286872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
19835578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
19845578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1985c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
198686872cb5SThomas Graf 	};
19871da177e4SLinus Torvalds 
19884e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19891da177e4SLinus Torvalds 
199086872cb5SThomas Graf 	ip6_route_add(&cfg);
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19957b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19961da177e4SLinus Torvalds {
19971da177e4SLinus Torvalds 	struct rt6_info *rt;
1998c71099acSThomas Graf 	struct fib6_table *table;
1999c71099acSThomas Graf 
2000c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20017b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
200238308473SDavid S. Miller 	if (!table)
2003c71099acSThomas Graf 		return;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds restart:
2006c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2007d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20081da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
2009d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2010c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2011e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20121da177e4SLinus Torvalds 			goto restart;
20131da177e4SLinus Torvalds 		}
20141da177e4SLinus Torvalds 	}
2015c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
20185578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20195578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
202086872cb5SThomas Graf 				 struct fib6_config *cfg)
202186872cb5SThomas Graf {
202286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
202386872cb5SThomas Graf 
202486872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
202586872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
202686872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
202786872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
202886872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
202986872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
203086872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
203186872cb5SThomas Graf 
20325578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2033f1243c2dSBenjamin Thery 
20344e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20354e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20364e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
203786872cb5SThomas Graf }
203886872cb5SThomas Graf 
20395578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20401da177e4SLinus Torvalds {
204186872cb5SThomas Graf 	struct fib6_config cfg;
20421da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20431da177e4SLinus Torvalds 	int err;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	switch(cmd) {
20461da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20471da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
20481da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
20491da177e4SLinus Torvalds 			return -EPERM;
20501da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20511da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20521da177e4SLinus Torvalds 		if (err)
20531da177e4SLinus Torvalds 			return -EFAULT;
20541da177e4SLinus Torvalds 
20555578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
205686872cb5SThomas Graf 
20571da177e4SLinus Torvalds 		rtnl_lock();
20581da177e4SLinus Torvalds 		switch (cmd) {
20591da177e4SLinus Torvalds 		case SIOCADDRT:
206086872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20611da177e4SLinus Torvalds 			break;
20621da177e4SLinus Torvalds 		case SIOCDELRT:
206386872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20641da177e4SLinus Torvalds 			break;
20651da177e4SLinus Torvalds 		default:
20661da177e4SLinus Torvalds 			err = -EINVAL;
20671da177e4SLinus Torvalds 		}
20681da177e4SLinus Torvalds 		rtnl_unlock();
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 		return err;
20713ff50b79SStephen Hemminger 	}
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	return -EINVAL;
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds /*
20771da177e4SLinus Torvalds  *	Drop the packet on the floor
20781da177e4SLinus Torvalds  */
20791da177e4SLinus Torvalds 
2080d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20811da177e4SLinus Torvalds {
2082612f09e8SYOSHIFUJI Hideaki 	int type;
2083adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2084612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2085612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20860660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
208745bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20883bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20893bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2090612f09e8SYOSHIFUJI Hideaki 			break;
2091612f09e8SYOSHIFUJI Hideaki 		}
2092612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2093612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20943bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20953bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2096612f09e8SYOSHIFUJI Hideaki 		break;
2097612f09e8SYOSHIFUJI Hideaki 	}
20983ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20991da177e4SLinus Torvalds 	kfree_skb(skb);
21001da177e4SLinus Torvalds 	return 0;
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
21039ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21049ce8ade0SThomas Graf {
2105612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21069ce8ade0SThomas Graf }
21079ce8ade0SThomas Graf 
210820380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
21091da177e4SLinus Torvalds {
2110adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2111612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21146723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
21156723ab54SDavid S. Miller 
21169ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21179ce8ade0SThomas Graf {
2118612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21199ce8ade0SThomas Graf }
21209ce8ade0SThomas Graf 
21219ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21229ce8ade0SThomas Graf {
2123adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2124612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21259ce8ade0SThomas Graf }
21269ce8ade0SThomas Graf 
21276723ab54SDavid S. Miller #endif
21286723ab54SDavid S. Miller 
21291da177e4SLinus Torvalds /*
21301da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21311da177e4SLinus Torvalds  */
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21341da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21358f031519SDavid S. Miller 				    bool anycast)
21361da177e4SLinus Torvalds {
2137c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21388b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2139f83c7790SDavid S. Miller 	int err;
21401da177e4SLinus Torvalds 
214138308473SDavid S. Miller 	if (!rt) {
2142f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21431da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
214440385653SBen Greear 	}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	in6_dev_hold(idev);
21471da177e4SLinus Torvalds 
214811d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2149d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2150d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21511da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2152d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
215558c4fb86SYOSHIFUJI Hideaki 	if (anycast)
215658c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
215758c4fb86SYOSHIFUJI Hideaki 	else
21581da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21598ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2160f83c7790SDavid S. Miller 	if (err) {
2161d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2162f83c7790SDavid S. Miller 		return ERR_PTR(err);
21631da177e4SLinus Torvalds 	}
21641da177e4SLinus Torvalds 
21654e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21661da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21675578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21681da177e4SLinus Torvalds 
2169d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	return rt;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
2174c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2175c3968a85SDaniel Walter 			struct rt6_info *rt,
2176b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2177c3968a85SDaniel Walter 			unsigned int prefs,
2178c3968a85SDaniel Walter 			struct in6_addr *saddr)
2179c3968a85SDaniel Walter {
2180c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2181c3968a85SDaniel Walter 	int err = 0;
2182c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21834e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2184c3968a85SDaniel Walter 	else
2185c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2186c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2187c3968a85SDaniel Walter 	return err;
2188c3968a85SDaniel Walter }
2189c3968a85SDaniel Walter 
2190c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2191c3968a85SDaniel Walter struct arg_dev_net_ip {
2192c3968a85SDaniel Walter 	struct net_device *dev;
2193c3968a85SDaniel Walter 	struct net *net;
2194c3968a85SDaniel Walter 	struct in6_addr *addr;
2195c3968a85SDaniel Walter };
2196c3968a85SDaniel Walter 
2197c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2198c3968a85SDaniel Walter {
2199c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2200c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2201c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2202c3968a85SDaniel Walter 
2203d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2204c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2205c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2206c3968a85SDaniel Walter 		/* remove prefsrc entry */
2207c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2208c3968a85SDaniel Walter 	}
2209c3968a85SDaniel Walter 	return 0;
2210c3968a85SDaniel Walter }
2211c3968a85SDaniel Walter 
2212c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2213c3968a85SDaniel Walter {
2214c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2215c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2216c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2217c3968a85SDaniel Walter 		.net = net,
2218c3968a85SDaniel Walter 		.addr = &ifp->addr,
2219c3968a85SDaniel Walter 	};
2220c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2221c3968a85SDaniel Walter }
2222c3968a85SDaniel Walter 
22238ed67789SDaniel Lezcano struct arg_dev_net {
22248ed67789SDaniel Lezcano 	struct net_device *dev;
22258ed67789SDaniel Lezcano 	struct net *net;
22268ed67789SDaniel Lezcano };
22278ed67789SDaniel Lezcano 
22281da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22291da177e4SLinus Torvalds {
2230bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2231bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22328ed67789SDaniel Lezcano 
2233d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2234c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22351da177e4SLinus Torvalds 		return -1;
2236c159d30cSDavid S. Miller 
22371da177e4SLinus Torvalds 	return 0;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
2240f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22411da177e4SLinus Torvalds {
22428ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22438ed67789SDaniel Lezcano 		.dev = dev,
22448ed67789SDaniel Lezcano 		.net = net,
22458ed67789SDaniel Lezcano 	};
22468ed67789SDaniel Lezcano 
22478ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22481e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds 
225195c96174SEric Dumazet struct rt6_mtu_change_arg {
22521da177e4SLinus Torvalds 	struct net_device *dev;
225395c96174SEric Dumazet 	unsigned int mtu;
22541da177e4SLinus Torvalds };
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22571da177e4SLinus Torvalds {
22581da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22591da177e4SLinus Torvalds 	struct inet6_dev *idev;
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22621da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22631da177e4SLinus Torvalds 	   We still use this lock to block changes
22641da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22651da177e4SLinus Torvalds 	*/
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
226838308473SDavid S. Miller 	if (!idev)
22691da177e4SLinus Torvalds 		return 0;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22721da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22731da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22741da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22751da177e4SLinus Torvalds 	 */
22761da177e4SLinus Torvalds 	/*
22771da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22781da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22791da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22801da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22811da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22821da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22831da177e4SLinus Torvalds 	   PMTU discouvery.
22841da177e4SLinus Torvalds 	 */
2285d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2286d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2287d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2288d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2289d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2290defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2291566cfd8fSSimon Arlott 	}
22921da177e4SLinus Torvalds 	return 0;
22931da177e4SLinus Torvalds }
22941da177e4SLinus Torvalds 
229595c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22961da177e4SLinus Torvalds {
2297c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2298c71099acSThomas Graf 		.dev = dev,
2299c71099acSThomas Graf 		.mtu = mtu,
2300c71099acSThomas Graf 	};
23011da177e4SLinus Torvalds 
2302c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
2305ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23065176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
230786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2308ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
230986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
231086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
231186872cb5SThomas Graf };
231286872cb5SThomas Graf 
231386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
231486872cb5SThomas Graf 			      struct fib6_config *cfg)
23151da177e4SLinus Torvalds {
231686872cb5SThomas Graf 	struct rtmsg *rtm;
231786872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
231886872cb5SThomas Graf 	int err;
23191da177e4SLinus Torvalds 
232086872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
232186872cb5SThomas Graf 	if (err < 0)
232286872cb5SThomas Graf 		goto errout;
23231da177e4SLinus Torvalds 
232486872cb5SThomas Graf 	err = -EINVAL;
232586872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
232686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
232786872cb5SThomas Graf 
232886872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
232986872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
233086872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
233186872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
233286872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
233386872cb5SThomas Graf 
233486872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
233586872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
233686872cb5SThomas Graf 
2337ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2338ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2339ab79ad14SMaciej Żenczykowski 
234086872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
234186872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23423b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
234386872cb5SThomas Graf 
234486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
234586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
234686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23471da177e4SLinus Torvalds 	}
234886872cb5SThomas Graf 
234986872cb5SThomas Graf 	if (tb[RTA_DST]) {
235086872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
235186872cb5SThomas Graf 
235286872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
235386872cb5SThomas Graf 			goto errout;
235486872cb5SThomas Graf 
235586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23561da177e4SLinus Torvalds 	}
235786872cb5SThomas Graf 
235886872cb5SThomas Graf 	if (tb[RTA_SRC]) {
235986872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
236086872cb5SThomas Graf 
236186872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
236286872cb5SThomas Graf 			goto errout;
236386872cb5SThomas Graf 
236486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23651da177e4SLinus Torvalds 	}
236686872cb5SThomas Graf 
2367c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2368c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2369c3968a85SDaniel Walter 
237086872cb5SThomas Graf 	if (tb[RTA_OIF])
237186872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
237286872cb5SThomas Graf 
237386872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
237486872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
237586872cb5SThomas Graf 
237686872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
237786872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
237886872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23791da177e4SLinus Torvalds 	}
238086872cb5SThomas Graf 
238186872cb5SThomas Graf 	if (tb[RTA_TABLE])
238286872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
238386872cb5SThomas Graf 
238486872cb5SThomas Graf 	err = 0;
238586872cb5SThomas Graf errout:
238686872cb5SThomas Graf 	return err;
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
2389c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23901da177e4SLinus Torvalds {
239186872cb5SThomas Graf 	struct fib6_config cfg;
239286872cb5SThomas Graf 	int err;
23931da177e4SLinus Torvalds 
239486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
239586872cb5SThomas Graf 	if (err < 0)
239686872cb5SThomas Graf 		return err;
239786872cb5SThomas Graf 
239886872cb5SThomas Graf 	return ip6_route_del(&cfg);
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
2401c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
24021da177e4SLinus Torvalds {
240386872cb5SThomas Graf 	struct fib6_config cfg;
240486872cb5SThomas Graf 	int err;
24051da177e4SLinus Torvalds 
240686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
240786872cb5SThomas Graf 	if (err < 0)
240886872cb5SThomas Graf 		return err;
240986872cb5SThomas Graf 
241086872cb5SThomas Graf 	return ip6_route_add(&cfg);
24111da177e4SLinus Torvalds }
24121da177e4SLinus Torvalds 
2413339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2414339bf98fSThomas Graf {
2415339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2416339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2417339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2418339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2419339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2420339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2421339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2422339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2423339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24246a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2425339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2426339bf98fSThomas Graf }
2427339bf98fSThomas Graf 
2428191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2429191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24300d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
24310d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
24327bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	struct rtmsg *rtm;
24351da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2436e3703b3dSThomas Graf 	long expires;
24379e762a4aSPatrick McHardy 	u32 table;
2438f2c31e32SEric Dumazet 	struct neighbour *n;
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24411da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24421da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24431da177e4SLinus Torvalds 			return 1;
24441da177e4SLinus Torvalds 		}
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 
24472d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
244838308473SDavid S. Miller 	if (!nlh)
244926932566SPatrick McHardy 		return -EMSGSIZE;
24502d7202bfSThomas Graf 
24512d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24521da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24531da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24541da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24551da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2456c71099acSThomas Graf 	if (rt->rt6i_table)
24579e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2458c71099acSThomas Graf 	else
24599e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24609e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2461c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2462c78679e8SDavid S. Miller 		goto nla_put_failure;
24631da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
24641da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2465ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2466ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2467d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24681da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24691da177e4SLinus Torvalds 	else
24701da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24711da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24721da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24731da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24741da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24751da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
24761da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
24771da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
24781da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
24791da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24821da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	if (dst) {
2485c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2486c78679e8SDavid S. Miller 			goto nla_put_failure;
24871da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24881da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2489c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2490c78679e8SDavid S. Miller 			goto nla_put_failure;
24911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24921da177e4SLinus Torvalds 	if (src) {
2493c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2494c78679e8SDavid S. Miller 			goto nla_put_failure;
24951da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2496c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2497c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2498c78679e8SDavid S. Miller 		goto nla_put_failure;
24991da177e4SLinus Torvalds #endif
25007bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25017bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
25027bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
25038229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
25047bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
25057bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25067bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25077bc570c8SYOSHIFUJI Hideaki 						return 0;
25087bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25097bc570c8SYOSHIFUJI Hideaki 				} else {
25107bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25117bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25127bc570c8SYOSHIFUJI Hideaki 				}
25137bc570c8SYOSHIFUJI Hideaki 			}
25147bc570c8SYOSHIFUJI Hideaki 		} else
25157bc570c8SYOSHIFUJI Hideaki #endif
2516c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2517c78679e8SDavid S. Miller 				goto nla_put_failure;
25187bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25191da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2520c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2521c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2522c78679e8SDavid S. Miller 			goto nla_put_failure;
2523c3968a85SDaniel Walter 	}
2524c3968a85SDaniel Walter 
2525c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2526c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25274e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2528c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2529c78679e8SDavid S. Miller 			goto nla_put_failure;
25301da177e4SLinus Torvalds 	}
25312d7202bfSThomas Graf 
2532defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25332d7202bfSThomas Graf 		goto nla_put_failure;
25342d7202bfSThomas Graf 
2535f2c31e32SEric Dumazet 	rcu_read_lock();
253697cac082SDavid S. Miller 	n = rt->n;
253794f826b8SEric Dumazet 	if (n) {
253894f826b8SEric Dumazet 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) {
253994f826b8SEric Dumazet 			rcu_read_unlock();
254094f826b8SEric Dumazet 			goto nla_put_failure;
254194f826b8SEric Dumazet 		}
254294f826b8SEric Dumazet 	}
2543f2c31e32SEric Dumazet 	rcu_read_unlock();
25442d7202bfSThomas Graf 
2545c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2546c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2547c78679e8SDavid S. Miller 		goto nla_put_failure;
2548c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2549c78679e8SDavid S. Miller 		goto nla_put_failure;
255036e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
255136e3deaeSYOSHIFUJI Hideaki 		expires = 0;
2552d1918542SDavid S. Miller 	else if (rt->dst.expires - jiffies < INT_MAX)
2553d1918542SDavid S. Miller 		expires = rt->dst.expires - jiffies;
255436e3deaeSYOSHIFUJI Hideaki 	else
255536e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
255669cdf8f9SYOSHIFUJI Hideaki 
255787a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2558e3703b3dSThomas Graf 		goto nla_put_failure;
25591da177e4SLinus Torvalds 
25602d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25612d7202bfSThomas Graf 
25622d7202bfSThomas Graf nla_put_failure:
256326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
256426932566SPatrick McHardy 	return -EMSGSIZE;
25651da177e4SLinus Torvalds }
25661da177e4SLinus Torvalds 
25671b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25681da177e4SLinus Torvalds {
25691da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25701da177e4SLinus Torvalds 	int prefix;
25711da177e4SLinus Torvalds 
25722d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25732d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25741da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25751da177e4SLinus Torvalds 	} else
25761da177e4SLinus Torvalds 		prefix = 0;
25771da177e4SLinus Torvalds 
2578191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2579191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
25801da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
25817bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
2584c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25851da177e4SLinus Torvalds {
25863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2587ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25881da177e4SLinus Torvalds 	struct rt6_info *rt;
2589ab364a6fSThomas Graf 	struct sk_buff *skb;
2590ab364a6fSThomas Graf 	struct rtmsg *rtm;
25914c9483b2SDavid S. Miller 	struct flowi6 fl6;
259272331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2593ab364a6fSThomas Graf 
2594ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2595ab364a6fSThomas Graf 	if (err < 0)
2596ab364a6fSThomas Graf 		goto errout;
2597ab364a6fSThomas Graf 
2598ab364a6fSThomas Graf 	err = -EINVAL;
25994c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2600ab364a6fSThomas Graf 
2601ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2602ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2603ab364a6fSThomas Graf 			goto errout;
2604ab364a6fSThomas Graf 
26054e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2606ab364a6fSThomas Graf 	}
2607ab364a6fSThomas Graf 
2608ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2609ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2610ab364a6fSThomas Graf 			goto errout;
2611ab364a6fSThomas Graf 
26124e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2613ab364a6fSThomas Graf 	}
2614ab364a6fSThomas Graf 
2615ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2616ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2617ab364a6fSThomas Graf 
2618ab364a6fSThomas Graf 	if (tb[RTA_OIF])
261972331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2620ab364a6fSThomas Graf 
2621ab364a6fSThomas Graf 	if (iif) {
2622ab364a6fSThomas Graf 		struct net_device *dev;
262372331bc0SShmulik Ladkani 		int flags = 0;
262472331bc0SShmulik Ladkani 
26255578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2626ab364a6fSThomas Graf 		if (!dev) {
2627ab364a6fSThomas Graf 			err = -ENODEV;
2628ab364a6fSThomas Graf 			goto errout;
2629ab364a6fSThomas Graf 		}
263072331bc0SShmulik Ladkani 
263172331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
263272331bc0SShmulik Ladkani 
263372331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
263472331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
263572331bc0SShmulik Ladkani 
263672331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
263772331bc0SShmulik Ladkani 							       flags);
263872331bc0SShmulik Ladkani 	} else {
263972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
264072331bc0SShmulik Ladkani 
264172331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2642ab364a6fSThomas Graf 	}
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
264538308473SDavid S. Miller 	if (!skb) {
26462173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2647ab364a6fSThomas Graf 		err = -ENOBUFS;
2648ab364a6fSThomas Graf 		goto errout;
2649ab364a6fSThomas Graf 	}
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26521da177e4SLinus Torvalds 	   through good chunk of routing engine.
26531da177e4SLinus Torvalds 	 */
2654459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26551da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26561da177e4SLinus Torvalds 
2657d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26581da177e4SLinus Torvalds 
26594c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
26601da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
26617bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26621da177e4SLinus Torvalds 	if (err < 0) {
2663ab364a6fSThomas Graf 		kfree_skb(skb);
2664ab364a6fSThomas Graf 		goto errout;
26651da177e4SLinus Torvalds 	}
26661da177e4SLinus Torvalds 
26675578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2668ab364a6fSThomas Graf errout:
26691da177e4SLinus Torvalds 	return err;
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
267286872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26731da177e4SLinus Torvalds {
26741da177e4SLinus Torvalds 	struct sk_buff *skb;
26755578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2676528c4cebSDenis V. Lunev 	u32 seq;
2677528c4cebSDenis V. Lunev 	int err;
26780d51aa80SJamal Hadi Salim 
2679528c4cebSDenis V. Lunev 	err = -ENOBUFS;
268038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
268186872cb5SThomas Graf 
2682339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
268338308473SDavid S. Miller 	if (!skb)
268421713ebcSThomas Graf 		goto errout;
26851da177e4SLinus Torvalds 
2686191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
26877bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
268826932566SPatrick McHardy 	if (err < 0) {
268926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
269026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
269126932566SPatrick McHardy 		kfree_skb(skb);
269226932566SPatrick McHardy 		goto errout;
269326932566SPatrick McHardy 	}
26941ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26955578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26961ce85fe4SPablo Neira Ayuso 	return;
269721713ebcSThomas Graf errout:
269821713ebcSThomas Graf 	if (err < 0)
26995578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27001da177e4SLinus Torvalds }
27011da177e4SLinus Torvalds 
27028ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
27038ed67789SDaniel Lezcano 				unsigned long event, void *data)
27048ed67789SDaniel Lezcano {
27058ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2706c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27078ed67789SDaniel Lezcano 
27088ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2709d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27108ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
27118ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2712d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
27138ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2714d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27158ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27168ed67789SDaniel Lezcano #endif
27178ed67789SDaniel Lezcano 	}
27188ed67789SDaniel Lezcano 
27198ed67789SDaniel Lezcano 	return NOTIFY_OK;
27208ed67789SDaniel Lezcano }
27218ed67789SDaniel Lezcano 
27221da177e4SLinus Torvalds /*
27231da177e4SLinus Torvalds  *	/proc
27241da177e4SLinus Torvalds  */
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds struct rt6_proc_arg
27291da177e4SLinus Torvalds {
27301da177e4SLinus Torvalds 	char *buffer;
27311da177e4SLinus Torvalds 	int offset;
27321da177e4SLinus Torvalds 	int length;
27331da177e4SLinus Torvalds 	int skip;
27341da177e4SLinus Torvalds 	int len;
27351da177e4SLinus Torvalds };
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27381da177e4SLinus Torvalds {
273933120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
274069cce1d1SDavid S. Miller 	struct neighbour *n;
27411da177e4SLinus Torvalds 
27424b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27454b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27461da177e4SLinus Torvalds #else
274733120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27481da177e4SLinus Torvalds #endif
2749f2c31e32SEric Dumazet 	rcu_read_lock();
275097cac082SDavid S. Miller 	n = rt->n;
275169cce1d1SDavid S. Miller 	if (n) {
275269cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
27531da177e4SLinus Torvalds 	} else {
275433120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27551da177e4SLinus Torvalds 	}
2756f2c31e32SEric Dumazet 	rcu_read_unlock();
275733120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2758d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2759d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2760d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27611da177e4SLinus Torvalds 	return 0;
27621da177e4SLinus Torvalds }
27631da177e4SLinus Torvalds 
276433120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27651da177e4SLinus Torvalds {
2766f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
276732b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
276833120b30SAlexey Dobriyan 	return 0;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
277133120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
277233120b30SAlexey Dobriyan {
2773de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2774f3db4851SDaniel Lezcano }
2775f3db4851SDaniel Lezcano 
277633120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
277733120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
277833120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
277933120b30SAlexey Dobriyan 	.read		= seq_read,
278033120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2781b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
278233120b30SAlexey Dobriyan };
278333120b30SAlexey Dobriyan 
27841da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27851da177e4SLinus Torvalds {
278669ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27871da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
278869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
278969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
279069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
279169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
279269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2793fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
279469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 	return 0;
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28001da177e4SLinus Torvalds {
2801de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
280269ddb805SDaniel Lezcano }
280369ddb805SDaniel Lezcano 
28049a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28051da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28061da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28071da177e4SLinus Torvalds 	.read	 = seq_read,
28081da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2809b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28101da177e4SLinus Torvalds };
28111da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds static
28168d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
28171da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28181da177e4SLinus Torvalds {
2819c486da34SLucian Adrian Grijincu 	struct net *net;
2820c486da34SLucian Adrian Grijincu 	int delay;
2821c486da34SLucian Adrian Grijincu 	if (!write)
2822c486da34SLucian Adrian Grijincu 		return -EINVAL;
2823c486da34SLucian Adrian Grijincu 
2824c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2825c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28268d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28275b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28281da177e4SLinus Torvalds 	return 0;
28291da177e4SLinus Torvalds }
28301da177e4SLinus Torvalds 
2831760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
28321da177e4SLinus Torvalds 	{
28331da177e4SLinus Torvalds 		.procname	=	"flush",
28344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
283689c8b3a1SDave Jones 		.mode		=	0200,
28376d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28381da177e4SLinus Torvalds 	},
28391da177e4SLinus Torvalds 	{
28401da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28419a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28431da177e4SLinus Torvalds 		.mode		=	0644,
28446d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28451da177e4SLinus Torvalds 	},
28461da177e4SLinus Torvalds 	{
28471da177e4SLinus Torvalds 		.procname	=	"max_size",
28484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28501da177e4SLinus Torvalds 		.mode		=	0644,
28516d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28521da177e4SLinus Torvalds 	},
28531da177e4SLinus Torvalds 	{
28541da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28554990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28561da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28571da177e4SLinus Torvalds 		.mode		=	0644,
28586d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28591da177e4SLinus Torvalds 	},
28601da177e4SLinus Torvalds 	{
28611da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28624990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28631da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28641da177e4SLinus Torvalds 		.mode		=	0644,
28656d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28661da177e4SLinus Torvalds 	},
28671da177e4SLinus Torvalds 	{
28681da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28694990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28701da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28711da177e4SLinus Torvalds 		.mode		=	0644,
28726d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28731da177e4SLinus Torvalds 	},
28741da177e4SLinus Torvalds 	{
28751da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28764990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28771da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28781da177e4SLinus Torvalds 		.mode		=	0644,
2879f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28801da177e4SLinus Torvalds 	},
28811da177e4SLinus Torvalds 	{
28821da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28834990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28841da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28851da177e4SLinus Torvalds 		.mode		=	0644,
28866d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28871da177e4SLinus Torvalds 	},
28881da177e4SLinus Torvalds 	{
28891da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28904990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28911da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28921da177e4SLinus Torvalds 		.mode		=	0644,
2893f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28941da177e4SLinus Torvalds 	},
28951da177e4SLinus Torvalds 	{
28961da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28974990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28981da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28991da177e4SLinus Torvalds 		.mode		=	0644,
29006d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29011da177e4SLinus Torvalds 	},
2902f8572d8fSEric W. Biederman 	{ }
29031da177e4SLinus Torvalds };
29041da177e4SLinus Torvalds 
29052c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2906760f2d01SDaniel Lezcano {
2907760f2d01SDaniel Lezcano 	struct ctl_table *table;
2908760f2d01SDaniel Lezcano 
2909760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2910760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2911760f2d01SDaniel Lezcano 			GFP_KERNEL);
29125ee09105SYOSHIFUJI Hideaki 
29135ee09105SYOSHIFUJI Hideaki 	if (table) {
29145ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2915c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
291686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29175ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29185ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29195ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29205ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29215ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29225ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29235ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29249c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29255ee09105SYOSHIFUJI Hideaki 	}
29265ee09105SYOSHIFUJI Hideaki 
2927760f2d01SDaniel Lezcano 	return table;
2928760f2d01SDaniel Lezcano }
29291da177e4SLinus Torvalds #endif
29301da177e4SLinus Torvalds 
29312c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2932cdb18761SDaniel Lezcano {
2933633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29348ed67789SDaniel Lezcano 
293586393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
293686393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2937f2fc6a54SBenjamin Thery 
2938fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2939fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2940fc66f95cSEric Dumazet 
29418ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29428ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29438ed67789SDaniel Lezcano 					   GFP_KERNEL);
29448ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2945fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2946d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29478ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2948d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
294962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
295062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29518ed67789SDaniel Lezcano 
29528ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29538ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29548ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29558ed67789SDaniel Lezcano 					       GFP_KERNEL);
295668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
295768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2958d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29598ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2960d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
296162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
296262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29638ed67789SDaniel Lezcano 
29648ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29658ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29668ed67789SDaniel Lezcano 					       GFP_KERNEL);
296768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
296868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2969d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29708ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2971d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
297262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
297362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29748ed67789SDaniel Lezcano #endif
29758ed67789SDaniel Lezcano 
2976b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2977b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2978b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2979b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2980b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2981b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2982b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2983b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2984b339a47cSPeter Zijlstra 
29856891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29866891a346SBenjamin Thery 
29878ed67789SDaniel Lezcano 	ret = 0;
29888ed67789SDaniel Lezcano out:
29898ed67789SDaniel Lezcano 	return ret;
2990f2fc6a54SBenjamin Thery 
299168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
299268fffc67SPeter Zijlstra out_ip6_prohibit_entry:
299368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
299468fffc67SPeter Zijlstra out_ip6_null_entry:
299568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
299668fffc67SPeter Zijlstra #endif
2997fc66f95cSEric Dumazet out_ip6_dst_entries:
2998fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2999f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3000f2fc6a54SBenjamin Thery 	goto out;
3001cdb18761SDaniel Lezcano }
3002cdb18761SDaniel Lezcano 
30032c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3004cdb18761SDaniel Lezcano {
30058ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30068ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30078ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30088ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30098ed67789SDaniel Lezcano #endif
301041bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3011cdb18761SDaniel Lezcano }
3012cdb18761SDaniel Lezcano 
3013d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3014d189634eSThomas Graf {
3015d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3016d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
3017d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
3018d189634eSThomas Graf #endif
3019d189634eSThomas Graf 	return 0;
3020d189634eSThomas Graf }
3021d189634eSThomas Graf 
3022d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3023d189634eSThomas Graf {
3024d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3025d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
3026d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
3027d189634eSThomas Graf #endif
3028d189634eSThomas Graf }
3029d189634eSThomas Graf 
3030cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3031cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3032cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3033cdb18761SDaniel Lezcano };
3034cdb18761SDaniel Lezcano 
3035c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3036c3426b47SDavid S. Miller {
3037c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3038c3426b47SDavid S. Miller 
3039c3426b47SDavid S. Miller 	if (!bp)
3040c3426b47SDavid S. Miller 		return -ENOMEM;
3041c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3042c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3043c3426b47SDavid S. Miller 	return 0;
3044c3426b47SDavid S. Miller }
3045c3426b47SDavid S. Miller 
3046c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3047c3426b47SDavid S. Miller {
3048c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3049c3426b47SDavid S. Miller 
3050c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
305156a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3052c3426b47SDavid S. Miller 	kfree(bp);
3053c3426b47SDavid S. Miller }
3054c3426b47SDavid S. Miller 
30552b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3056c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3057c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3058c3426b47SDavid S. Miller };
3059c3426b47SDavid S. Miller 
3060d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3061d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3062d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3063d189634eSThomas Graf };
3064d189634eSThomas Graf 
30658ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30668ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30678ed67789SDaniel Lezcano 	.priority = 0,
30688ed67789SDaniel Lezcano };
30698ed67789SDaniel Lezcano 
3070433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30711da177e4SLinus Torvalds {
3072433d49c3SDaniel Lezcano 	int ret;
3073433d49c3SDaniel Lezcano 
30749a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30759a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30769a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30779a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30789a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3079c19a28e1SFernando Carrijo 		goto out;
308014e50e57SDavid S. Miller 
3081fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30828ed67789SDaniel Lezcano 	if (ret)
3083bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3084bdb3289fSDaniel Lezcano 
3085c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3086c3426b47SDavid S. Miller 	if (ret)
3087e8803b6cSDavid S. Miller 		goto out_dst_entries;
30882a0c451aSThomas Graf 
30897e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30907e52b33bSDavid S. Miller 	if (ret)
30917e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3092c3426b47SDavid S. Miller 
30935dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30945dc121e9SArnaud Ebalard 
30958ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30968ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30978ed67789SDaniel Lezcano 	 * manually for init_net */
3098d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30998ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3100bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3101d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31028ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3103d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31048ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3105bdb3289fSDaniel Lezcano   #endif
3106e8803b6cSDavid S. Miller 	ret = fib6_init();
3107433d49c3SDaniel Lezcano 	if (ret)
31088ed67789SDaniel Lezcano 		goto out_register_subsys;
3109433d49c3SDaniel Lezcano 
3110433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3111433d49c3SDaniel Lezcano 	if (ret)
3112e8803b6cSDavid S. Miller 		goto out_fib6_init;
3113c35b7e72SDaniel Lezcano 
3114433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3115433d49c3SDaniel Lezcano 	if (ret)
3116433d49c3SDaniel Lezcano 		goto xfrm6_init;
31177e5449c2SDaniel Lezcano 
3118d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3119d189634eSThomas Graf 	if (ret)
3120d189634eSThomas Graf 		goto fib6_rules_init;
3121d189634eSThomas Graf 
3122433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3123c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3124c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3125c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3126d189634eSThomas Graf 		goto out_register_late_subsys;
3127433d49c3SDaniel Lezcano 
31288ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3129cdb18761SDaniel Lezcano 	if (ret)
3130d189634eSThomas Graf 		goto out_register_late_subsys;
31318ed67789SDaniel Lezcano 
3132433d49c3SDaniel Lezcano out:
3133433d49c3SDaniel Lezcano 	return ret;
3134433d49c3SDaniel Lezcano 
3135d189634eSThomas Graf out_register_late_subsys:
3136d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3137433d49c3SDaniel Lezcano fib6_rules_init:
3138433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3139433d49c3SDaniel Lezcano xfrm6_init:
3140433d49c3SDaniel Lezcano 	xfrm6_fini();
31412a0c451aSThomas Graf out_fib6_init:
31422a0c451aSThomas Graf 	fib6_gc_cleanup();
31438ed67789SDaniel Lezcano out_register_subsys:
31448ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31457e52b33bSDavid S. Miller out_register_inetpeer:
31467e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3147fc66f95cSEric Dumazet out_dst_entries:
3148fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3149433d49c3SDaniel Lezcano out_kmem_cache:
3150f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3151433d49c3SDaniel Lezcano 	goto out;
31521da177e4SLinus Torvalds }
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds void ip6_route_cleanup(void)
31551da177e4SLinus Torvalds {
31568ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3157d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3158101367c2SThomas Graf 	fib6_rules_cleanup();
31591da177e4SLinus Torvalds 	xfrm6_fini();
31601da177e4SLinus Torvalds 	fib6_gc_cleanup();
3161c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31628ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
316341bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3164f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31651da177e4SLinus Torvalds }
3166