xref: /openbmc/linux/net/ipv6/route.c (revision 52bd4c0c1551daa2efa7bb9e01a2f4ea6d1311bb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47457c4cbcSEric W. Biederman #include <net/net_namespace.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds #include <net/ipv6.h>
501da177e4SLinus Torvalds #include <net/ip6_fib.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/ndisc.h>
531da177e4SLinus Torvalds #include <net/addrconf.h>
541da177e4SLinus Torvalds #include <net/tcp.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <net/dst.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
588d71740cSTom Tucker #include <net/netevent.h>
5921713ebcSThomas Graf #include <net/netlink.h>
6051ebd318SNicolas Dichtel #include <net/nexthop.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
651da177e4SLinus Torvalds #include <linux/sysctl.h>
661da177e4SLinus Torvalds #endif
671da177e4SLinus Torvalds 
681716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6921efcfa0SEric Dumazet 				    const struct in6_addr *dest);
701da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
710dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
72ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
751da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
761da177e4SLinus Torvalds 				       struct net_device *dev, int how);
77569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
801da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
826700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
836700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
846700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
856700c270SDavid S. Miller 					struct sk_buff *skb);
86*52bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
871da177e4SLinus Torvalds 
8870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
89efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
90b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
91b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9295c96174SEric Dumazet 					   unsigned int pref);
93efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
94b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
95b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9670ceb4f5SYOSHIFUJI Hideaki #endif
9770ceb4f5SYOSHIFUJI Hideaki 
9806582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9906582540SDavid S. Miller {
10006582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10106582540SDavid S. Miller 	struct inet_peer *peer;
10206582540SDavid S. Miller 	u32 *p = NULL;
10306582540SDavid S. Miller 
1048e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1058e2ec639SYan, Zheng 		return NULL;
1068e2ec639SYan, Zheng 
107fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10806582540SDavid S. Miller 	if (peer) {
10906582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11006582540SDavid S. Miller 		unsigned long prev, new;
11106582540SDavid S. Miller 
11206582540SDavid S. Miller 		p = peer->metrics;
11306582540SDavid S. Miller 		if (inet_metrics_new(peer))
11406582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11506582540SDavid S. Miller 
11606582540SDavid S. Miller 		new = (unsigned long) p;
11706582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11806582540SDavid S. Miller 
11906582540SDavid S. Miller 		if (prev != old) {
12006582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12106582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12206582540SDavid S. Miller 				p = NULL;
12306582540SDavid S. Miller 		}
12406582540SDavid S. Miller 	}
12506582540SDavid S. Miller 	return p;
12606582540SDavid S. Miller }
12706582540SDavid S. Miller 
128f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
129f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
130f894cbf8SDavid S. Miller 					     const void *daddr)
13139232973SDavid S. Miller {
13239232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
13339232973SDavid S. Miller 
134a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13539232973SDavid S. Miller 		return (const void *) p;
136f894cbf8SDavid S. Miller 	else if (skb)
137f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13839232973SDavid S. Miller 	return daddr;
13939232973SDavid S. Miller }
14039232973SDavid S. Miller 
141f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
142f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
143f894cbf8SDavid S. Miller 					  const void *daddr)
144d3aaeb38SDavid S. Miller {
14539232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14639232973SDavid S. Miller 	struct neighbour *n;
14739232973SDavid S. Miller 
148f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1498e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
150f83c7790SDavid S. Miller 	if (n)
151f83c7790SDavid S. Miller 		return n;
152f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
153f83c7790SDavid S. Miller }
154f83c7790SDavid S. Miller 
1559a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1561da177e4SLinus Torvalds 	.family			=	AF_INET6,
15709640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1581da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1591da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1601da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1610dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
162ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16306582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1641da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1651da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1661da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1671da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1681da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1696e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1701ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
171d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1721da177e4SLinus Torvalds };
1731da177e4SLinus Torvalds 
174ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
175ec831ea7SRoland Dreier {
176618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
177618f9bc7SSteffen Klassert 
178618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
179ec831ea7SRoland Dreier }
180ec831ea7SRoland Dreier 
1816700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1826700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
18314e50e57SDavid S. Miller {
18414e50e57SDavid S. Miller }
18514e50e57SDavid S. Miller 
1866700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1876700c270SDavid S. Miller 				      struct sk_buff *skb)
188b587ee3bSDavid S. Miller {
189b587ee3bSDavid S. Miller }
190b587ee3bSDavid S. Miller 
1910972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1920972ddb2SHeld Bernhard 					 unsigned long old)
1930972ddb2SHeld Bernhard {
1940972ddb2SHeld Bernhard 	return NULL;
1950972ddb2SHeld Bernhard }
1960972ddb2SHeld Bernhard 
19714e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19814e50e57SDavid S. Miller 	.family			=	AF_INET6,
19909640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20014e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20114e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
202ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
203214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20414e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
205b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2060972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
207d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20814e50e57SDavid S. Miller };
20914e50e57SDavid S. Miller 
21062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21114edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
21262fa8a84SDavid S. Miller };
21362fa8a84SDavid S. Miller 
214fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2151da177e4SLinus Torvalds 	.dst = {
2161da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2171da177e4SLinus Torvalds 		.__use		= 1,
2182c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2191da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2201da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2211da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2221da177e4SLinus Torvalds 	},
2231da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2244f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2251da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2261da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2271da177e4SLinus Torvalds };
2281da177e4SLinus Torvalds 
229101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
230101367c2SThomas Graf 
2316723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2326723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2336723ab54SDavid S. Miller 
234fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
235101367c2SThomas Graf 	.dst = {
236101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
237101367c2SThomas Graf 		.__use		= 1,
2382c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
239101367c2SThomas Graf 		.error		= -EACCES,
2409ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2419ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
242101367c2SThomas Graf 	},
243101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2444f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
245101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
246101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
247101367c2SThomas Graf };
248101367c2SThomas Graf 
249fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
250101367c2SThomas Graf 	.dst = {
251101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
252101367c2SThomas Graf 		.__use		= 1,
2532c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
254101367c2SThomas Graf 		.error		= -EINVAL,
255352e512cSHerbert Xu 		.input		= dst_discard,
256352e512cSHerbert Xu 		.output		= dst_discard,
257101367c2SThomas Graf 	},
258101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2594f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
260101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
261101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
262101367c2SThomas Graf };
263101367c2SThomas Graf 
264101367c2SThomas Graf #endif
265101367c2SThomas Graf 
2661da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26797bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
268957c665fSDavid S. Miller 					     struct net_device *dev,
2698b96d22dSDavid S. Miller 					     int flags,
2708b96d22dSDavid S. Miller 					     struct fib6_table *table)
2711da177e4SLinus Torvalds {
27297bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2736f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
274cf911662SDavid S. Miller 
27597bab73fSDavid S. Miller 	if (rt) {
2768104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2778104891bSSteffen Klassert 
2788104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2798b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
2806f3118b5SNicolas Dichtel 		rt->rt6i_genid = rt_genid(net);
28151ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28251ebd318SNicolas Dichtel 		rt->rt6i_nsiblings = 0;
28397bab73fSDavid S. Miller 	}
284cf911662SDavid S. Miller 	return rt;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2901da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
291ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2921da177e4SLinus Torvalds 
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 
301ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
302ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(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 
310b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
311b3419363SDavid S. Miller {
31297bab73fSDavid S. Miller 	struct inet_peer_base *base;
313b3419363SDavid S. Miller 	struct inet_peer *peer;
314b3419363SDavid S. Miller 
31597bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
31697bab73fSDavid S. Miller 	if (!base)
31797bab73fSDavid S. Miller 		return;
31897bab73fSDavid S. Miller 
31997bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3207b34ca2aSDavid S. Miller 	if (peer) {
32197bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
322b3419363SDavid S. Miller 			inet_putpeer(peer);
3231da177e4SLinus Torvalds 	}
3247b34ca2aSDavid S. Miller }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3271da177e4SLinus Torvalds 			   int how)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3301da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3315a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
332c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3331da177e4SLinus Torvalds 
33497cac082SDavid S. Miller 	if (dev != loopback_dev) {
33597cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3365a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3375a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
33838308473SDavid S. Miller 			if (loopback_idev) {
3391da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3401da177e4SLinus Torvalds 				in6_dev_put(idev);
3411da177e4SLinus Torvalds 			}
3421da177e4SLinus Torvalds 		}
34397cac082SDavid S. Miller 	}
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
346a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3471da177e4SLinus Torvalds {
3481716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3491716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
350a50feda5SEric Dumazet 			return true;
3511716a961SGao feng 	} else if (rt->dst.from) {
3523fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3531716a961SGao feng 	}
354a50feda5SEric Dumazet 	return false;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
358c71099acSThomas Graf {
359a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
360a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
361c71099acSThomas Graf }
362c71099acSThomas Graf 
36351ebd318SNicolas Dichtel /* Multipath route selection:
36451ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
36551ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
36651ebd318SNicolas Dichtel  */
36751ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
36851ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
36951ebd318SNicolas Dichtel {
37051ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
37151ebd318SNicolas Dichtel 
372c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
373c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
37451ebd318SNicolas Dichtel 
37551ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
37651ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
37751ebd318SNicolas Dichtel 	case IPPROTO_UDP:
37851ebd318SNicolas Dichtel 	case IPPROTO_TCP:
37951ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
380b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
381b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38251ebd318SNicolas Dichtel 		break;
38351ebd318SNicolas Dichtel 
38451ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
385b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
386b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
38751ebd318SNicolas Dichtel 		break;
38851ebd318SNicolas Dichtel 	}
38951ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
390b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
39151ebd318SNicolas Dichtel 
39251ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39351ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
39451ebd318SNicolas Dichtel 	return val % candidate_count;
39551ebd318SNicolas Dichtel }
39651ebd318SNicolas Dichtel 
39751ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
398*52bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
399*52bd4c0cSNicolas Dichtel 					     int strict)
40051ebd318SNicolas Dichtel {
40151ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
40251ebd318SNicolas Dichtel 	int route_choosen;
40351ebd318SNicolas Dichtel 
40451ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
40551ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
40651ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
40751ebd318SNicolas Dichtel 	 */
40851ebd318SNicolas Dichtel 	if (route_choosen)
40951ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
41051ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
41151ebd318SNicolas Dichtel 			route_choosen--;
41251ebd318SNicolas Dichtel 			if (route_choosen == 0) {
413*52bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
414*52bd4c0cSNicolas Dichtel 					break;
41551ebd318SNicolas Dichtel 				match = sibling;
41651ebd318SNicolas Dichtel 				break;
41751ebd318SNicolas Dichtel 			}
41851ebd318SNicolas Dichtel 		}
41951ebd318SNicolas Dichtel 	return match;
42051ebd318SNicolas Dichtel }
42151ebd318SNicolas Dichtel 
4221da177e4SLinus Torvalds /*
423c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds 
4268ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4278ed67789SDaniel Lezcano 						    struct rt6_info *rt,
428b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4291da177e4SLinus Torvalds 						    int oif,
430d420895eSYOSHIFUJI Hideaki 						    int flags)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4331da177e4SLinus Torvalds 	struct rt6_info *sprt;
4341da177e4SLinus Torvalds 
435dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
436dd3abc4eSYOSHIFUJI Hideaki 		goto out;
437dd3abc4eSYOSHIFUJI Hideaki 
438d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
439d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
440dd3abc4eSYOSHIFUJI Hideaki 
441dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4421da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4431da177e4SLinus Torvalds 				return sprt;
4441da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
44538308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4461da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
447d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4481da177e4SLinus Torvalds 						continue;
4491da177e4SLinus Torvalds 					if (local && (!oif ||
4501da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4511da177e4SLinus Torvalds 						continue;
4521da177e4SLinus Torvalds 				}
4531da177e4SLinus Torvalds 				local = sprt;
4541da177e4SLinus Torvalds 			}
455dd3abc4eSYOSHIFUJI Hideaki 		} else {
456dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
457dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
458dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
459dd3abc4eSYOSHIFUJI Hideaki 		}
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
462dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4631da177e4SLinus Torvalds 		if (local)
4641da177e4SLinus Torvalds 			return local;
4651da177e4SLinus Torvalds 
466d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4678ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4681da177e4SLinus Torvalds 	}
469dd3abc4eSYOSHIFUJI Hideaki out:
4701da177e4SLinus Torvalds 	return rt;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
47327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
47427097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
47527097255SYOSHIFUJI Hideaki {
476f2c31e32SEric Dumazet 	struct neighbour *neigh;
47727097255SYOSHIFUJI Hideaki 	/*
47827097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
47927097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
48027097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
48127097255SYOSHIFUJI Hideaki 	 *
48227097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
48327097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
48427097255SYOSHIFUJI Hideaki 	 */
4852152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
486fdd6681dSAmerigo Wang 		return;
4872152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4882152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4892152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4912152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4922152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4937ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
4942152caeaSYOSHIFUJI Hideaki / 吉藤英明 
4952152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
49652e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
49727097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
49827097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
49927097255SYOSHIFUJI Hideaki 
500b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh) {
50127097255SYOSHIFUJI Hideaki 			neigh->updated = jiffies;
5022152caeaSYOSHIFUJI Hideaki / 吉藤英明 			write_unlock(&neigh->lock);
503b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		}
5042152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5052152caeaSYOSHIFUJI Hideaki / 吉藤英明 		target = (struct in6_addr *)&rt->rt6i_gateway;
50627097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
507d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
508f2c31e32SEric Dumazet 	} else {
5092152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5102152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
51127097255SYOSHIFUJI Hideaki 	}
5122152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
513f2c31e32SEric Dumazet }
51427097255SYOSHIFUJI Hideaki #else
51527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
51627097255SYOSHIFUJI Hideaki {
51727097255SYOSHIFUJI Hideaki }
51827097255SYOSHIFUJI Hideaki #endif
51927097255SYOSHIFUJI Hideaki 
5201da177e4SLinus Torvalds /*
521554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5221da177e4SLinus Torvalds  */
523b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5241da177e4SLinus Torvalds {
525d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
526161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
527554cfb7eSYOSHIFUJI Hideaki 		return 2;
528161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
529161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
530161980f4SDavid S. Miller 		return 1;
531554cfb7eSYOSHIFUJI Hideaki 	return 0;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
534a5a81f0bSPaul Marks static inline bool rt6_check_neigh(struct rt6_info *rt)
5351da177e4SLinus Torvalds {
536f2c31e32SEric Dumazet 	struct neighbour *neigh;
537a5a81f0bSPaul Marks 	bool ret = false;
538f2c31e32SEric Dumazet 
5394d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5404d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
541145a3621SYOSHIFUJI Hideaki / 吉藤英明 		return true;
542145a3621SYOSHIFUJI Hideaki / 吉藤英明 
543145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
544145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
545145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
546145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
547554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
548a5a81f0bSPaul Marks 			ret = true;
549398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
550a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
551a5a81f0bSPaul Marks 			ret = true;
552398bcbebSYOSHIFUJI Hideaki #endif
553145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
554a5a81f0bSPaul Marks 	}
555145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
556145a3621SYOSHIFUJI Hideaki / 吉藤英明 
557a5a81f0bSPaul Marks 	return ret;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
560554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
561554cfb7eSYOSHIFUJI Hideaki 			   int strict)
562554cfb7eSYOSHIFUJI Hideaki {
563a5a81f0bSPaul Marks 	int m;
5644d0c5911SYOSHIFUJI Hideaki 
5654d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
56677d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
567554cfb7eSYOSHIFUJI Hideaki 		return -1;
568ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
569ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
570ebacaaa0SYOSHIFUJI Hideaki #endif
571a5a81f0bSPaul Marks 	if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
572554cfb7eSYOSHIFUJI Hideaki 		return -1;
573554cfb7eSYOSHIFUJI Hideaki 	return m;
574554cfb7eSYOSHIFUJI Hideaki }
575554cfb7eSYOSHIFUJI Hideaki 
576f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
577f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
578554cfb7eSYOSHIFUJI Hideaki {
579554cfb7eSYOSHIFUJI Hideaki 	int m;
580554cfb7eSYOSHIFUJI Hideaki 
581554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
582f11e6659SDavid S. Miller 		goto out;
583554cfb7eSYOSHIFUJI Hideaki 
584554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
585554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
586f11e6659SDavid S. Miller 		goto out;
587554cfb7eSYOSHIFUJI Hideaki 
588f11e6659SDavid S. Miller 	if (m > *mpri) {
589ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
59027097255SYOSHIFUJI Hideaki 			rt6_probe(match);
591f11e6659SDavid S. Miller 		*mpri = m;
592554cfb7eSYOSHIFUJI Hideaki 		match = rt;
593ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
59427097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5951da177e4SLinus Torvalds 	}
596f11e6659SDavid S. Miller 
597f11e6659SDavid S. Miller out:
598f11e6659SDavid S. Miller 	return match;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
601f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
602f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
603f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
604f11e6659SDavid S. Miller {
605f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
606f11e6659SDavid S. Miller 	int mpri = -1;
607f11e6659SDavid S. Miller 
608f11e6659SDavid S. Miller 	match = NULL;
609f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
610d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
611f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
612f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
613d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
614f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
615f11e6659SDavid S. Miller 
616f11e6659SDavid S. Miller 	return match;
617f11e6659SDavid S. Miller }
618f11e6659SDavid S. Miller 
619f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
620f11e6659SDavid S. Miller {
621f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6228ed67789SDaniel Lezcano 	struct net *net;
623f11e6659SDavid S. Miller 
624f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
625f11e6659SDavid S. Miller 	if (!rt0)
626f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
627f11e6659SDavid S. Miller 
628f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
629f11e6659SDavid S. Miller 
630554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
631f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
632d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
633f11e6659SDavid S. Miller 
634554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
635f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
636f11e6659SDavid S. Miller 			next = fn->leaf;
637f11e6659SDavid S. Miller 
638f11e6659SDavid S. Miller 		if (next != rt0)
639f11e6659SDavid S. Miller 			fn->rr_ptr = next;
640554cfb7eSYOSHIFUJI Hideaki 	}
641554cfb7eSYOSHIFUJI Hideaki 
642d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
643a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
64670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
64770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
648b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
64970ceb4f5SYOSHIFUJI Hideaki {
650c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
65170ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
65270ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
65370ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6544bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
65570ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
65670ceb4f5SYOSHIFUJI Hideaki 
65770ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
65870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
65970ceb4f5SYOSHIFUJI Hideaki 	}
66070ceb4f5SYOSHIFUJI Hideaki 
66170ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
66270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
66370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
66570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
66770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
66870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
66970ceb4f5SYOSHIFUJI Hideaki 		}
67070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
67170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
67270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
67370ceb4f5SYOSHIFUJI Hideaki 		}
67470ceb4f5SYOSHIFUJI Hideaki 	}
67570ceb4f5SYOSHIFUJI Hideaki 
67670ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
67770ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6783933fc95SJens Rosenboom 		return -EINVAL;
67970ceb4f5SYOSHIFUJI Hideaki 
6804bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
68170ceb4f5SYOSHIFUJI Hideaki 
68270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
68370ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
68470ceb4f5SYOSHIFUJI Hideaki 	else {
68570ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
68670ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
68770ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
68870ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
68970ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
69070ceb4f5SYOSHIFUJI Hideaki 	}
69170ceb4f5SYOSHIFUJI Hideaki 
692efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
693efa2cea0SDaniel Lezcano 				dev->ifindex);
69470ceb4f5SYOSHIFUJI Hideaki 
69570ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
696e0a1ad73SThomas Graf 		ip6_del_rt(rt);
69770ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
69870ceb4f5SYOSHIFUJI Hideaki 	}
69970ceb4f5SYOSHIFUJI Hideaki 
70070ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
701efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
70270ceb4f5SYOSHIFUJI Hideaki 					pref);
70370ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
70470ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
70570ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
70670ceb4f5SYOSHIFUJI Hideaki 
70770ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7081716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7091716a961SGao feng 			rt6_clean_expires(rt);
7101716a961SGao feng 		else
7111716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7121716a961SGao feng 
71394e187c0SAmerigo Wang 		ip6_rt_put(rt);
71470ceb4f5SYOSHIFUJI Hideaki 	}
71570ceb4f5SYOSHIFUJI Hideaki 	return 0;
71670ceb4f5SYOSHIFUJI Hideaki }
71770ceb4f5SYOSHIFUJI Hideaki #endif
71870ceb4f5SYOSHIFUJI Hideaki 
7198ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
720982f56f3SYOSHIFUJI Hideaki do { \
7218ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
722982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
723e0eda7bbSVille Nuorvala 		while (1) { \
724982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
725c71099acSThomas Graf 				goto out; \
726982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
727982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7288bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
729982f56f3SYOSHIFUJI Hideaki 			else \
730982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
731c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
732c71099acSThomas Graf 				goto restart; \
733c71099acSThomas Graf 		} \
734982f56f3SYOSHIFUJI Hideaki 	} \
735982f56f3SYOSHIFUJI Hideaki } while (0)
736c71099acSThomas Graf 
7378ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7388ed67789SDaniel Lezcano 					     struct fib6_table *table,
7394c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	struct fib6_node *fn;
7421da177e4SLinus Torvalds 	struct rt6_info *rt;
7431da177e4SLinus Torvalds 
744c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7454c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
746c71099acSThomas Graf restart:
747c71099acSThomas Graf 	rt = fn->leaf;
7484c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
74951ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
750*52bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
7514c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
752c71099acSThomas Graf out:
753d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
754c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7551da177e4SLinus Torvalds 	return rt;
756c71099acSThomas Graf 
757c71099acSThomas Graf }
758c71099acSThomas Graf 
759ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
760ea6e574eSFlorian Westphal 				    int flags)
761ea6e574eSFlorian Westphal {
762ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
763ea6e574eSFlorian Westphal }
764ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
765ea6e574eSFlorian Westphal 
7669acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7679acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
768c71099acSThomas Graf {
7694c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7704c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7714c9483b2SDavid S. Miller 		.daddr = *daddr,
772c71099acSThomas Graf 	};
773c71099acSThomas Graf 	struct dst_entry *dst;
77477d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
775c71099acSThomas Graf 
776adaa70bbSThomas Graf 	if (saddr) {
7774c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
778adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
779adaa70bbSThomas Graf 	}
780adaa70bbSThomas Graf 
7814c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
782c71099acSThomas Graf 	if (dst->error == 0)
783c71099acSThomas Graf 		return (struct rt6_info *) dst;
784c71099acSThomas Graf 
785c71099acSThomas Graf 	dst_release(dst);
786c71099acSThomas Graf 
7871da177e4SLinus Torvalds 	return NULL;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7907159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7917159039aSYOSHIFUJI Hideaki 
792c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7931da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7941da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7951da177e4SLinus Torvalds    be destroyed.
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds 
79886872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7991da177e4SLinus Torvalds {
8001da177e4SLinus Torvalds 	int err;
801c71099acSThomas Graf 	struct fib6_table *table;
8021da177e4SLinus Torvalds 
803c71099acSThomas Graf 	table = rt->rt6i_table;
804c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
80586872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
806c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	return err;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
81140e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
81240e22e8fSThomas Graf {
8134d1169c1SDenis V. Lunev 	struct nl_info info = {
814d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8154d1169c1SDenis V. Lunev 	};
816528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
81740e22e8fSThomas Graf }
81840e22e8fSThomas Graf 
8191716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
82021efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
821b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct rt6_info *rt;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	/*
8261da177e4SLinus Torvalds 	 *	Clone the route.
8271da177e4SLinus Torvalds 	 */
8281da177e4SLinus Torvalds 
82921efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if (rt) {
83258c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
833bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
83421efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
83558c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8364e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
83758c4fb86SYOSHIFUJI Hideaki 		}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8421da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8434e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8441da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8451da177e4SLinus Torvalds 		}
8461da177e4SLinus Torvalds #endif
84795a9a5baSYOSHIFUJI Hideaki 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	return rt;
8501da177e4SLinus Torvalds }
85195a9a5baSYOSHIFUJI Hideaki 
85221efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
85321efcfa0SEric Dumazet 					const struct in6_addr *daddr)
854299d9939SYOSHIFUJI Hideaki {
85521efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
85621efcfa0SEric Dumazet 
857887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
858299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
859299d9939SYOSHIFUJI Hideaki 	return rt;
860299d9939SYOSHIFUJI Hideaki }
861299d9939SYOSHIFUJI Hideaki 
8628ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8634c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8641da177e4SLinus Torvalds {
8651da177e4SLinus Torvalds 	struct fib6_node *fn;
866519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
867c71099acSThomas Graf 	int strict = 0;
8681da177e4SLinus Torvalds 	int attempts = 3;
869519fbd87SYOSHIFUJI Hideaki 	int err;
87053b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8711da177e4SLinus Torvalds 
87277d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds relookup:
875c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8761da177e4SLinus Torvalds 
8778238dd06SYOSHIFUJI Hideaki restart_2:
8784c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds restart:
8814acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
882*52bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
883*52bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
8844c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8858ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8868238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8871da177e4SLinus Torvalds 		goto out;
8881da177e4SLinus Torvalds 
889d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
890c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8911da177e4SLinus Torvalds 
892c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
8934c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8947343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8954c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8967343ff31SDavid S. Miller 	else
8977343ff31SDavid S. Miller 		goto out2;
8981da177e4SLinus Torvalds 
89994e187c0SAmerigo Wang 	ip6_rt_put(rt);
9008ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9011da177e4SLinus Torvalds 
902d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
903e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
90440e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
905e40cf353SYOSHIFUJI Hideaki 		if (!err)
906e40cf353SYOSHIFUJI Hideaki 			goto out2;
907e40cf353SYOSHIFUJI Hideaki 	}
908e40cf353SYOSHIFUJI Hideaki 
909e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9101da177e4SLinus Torvalds 		goto out2;
9111da177e4SLinus Torvalds 
912519fbd87SYOSHIFUJI Hideaki 	/*
913c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
914519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9151da177e4SLinus Torvalds 	 */
91694e187c0SAmerigo Wang 	ip6_rt_put(rt);
9171da177e4SLinus Torvalds 	goto relookup;
918e40cf353SYOSHIFUJI Hideaki 
919519fbd87SYOSHIFUJI Hideaki out:
9208238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9218238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9228238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9238238dd06SYOSHIFUJI Hideaki 	}
924d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
925c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9261da177e4SLinus Torvalds out2:
927d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
928d8d1f30bSChangli Gao 	rt->dst.__use++;
929c71099acSThomas Graf 
930c71099acSThomas Graf 	return rt;
931c71099acSThomas Graf }
932c71099acSThomas Graf 
9338ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9344c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9354acad72dSPavel Emelyanov {
9364c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9374acad72dSPavel Emelyanov }
9384acad72dSPavel Emelyanov 
93972331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
94072331bc0SShmulik Ladkani 						struct net_device *dev,
94172331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
94272331bc0SShmulik Ladkani {
94372331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
94472331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
94572331bc0SShmulik Ladkani 
94672331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
94772331bc0SShmulik Ladkani }
94872331bc0SShmulik Ladkani 
949c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
950c71099acSThomas Graf {
951b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
952c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
953adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9544c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9554c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9564c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9574c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9586502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9594c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9604c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
961c71099acSThomas Graf 	};
962adaa70bbSThomas Graf 
96372331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
964c71099acSThomas Graf }
965c71099acSThomas Graf 
9668ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9674c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
968c71099acSThomas Graf {
9694c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
970c71099acSThomas Graf }
971c71099acSThomas Graf 
9729c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9734c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
974c71099acSThomas Graf {
975c71099acSThomas Graf 	int flags = 0;
976c71099acSThomas Graf 
9771fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9784dc27d1cSDavid McCullough 
9794c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
98077d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
981c71099acSThomas Graf 
9824c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
983adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9840c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9850c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
986adaa70bbSThomas Graf 
9874c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9907159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9911da177e4SLinus Torvalds 
9922774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
99314e50e57SDavid S. Miller {
9945c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
99514e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
99614e50e57SDavid S. Miller 
997f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
99814e50e57SDavid S. Miller 	if (rt) {
999d8d1f30bSChangli Gao 		new = &rt->dst;
100014e50e57SDavid S. Miller 
10018104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10028104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10038104891bSSteffen Klassert 
100414e50e57SDavid S. Miller 		new->__use = 1;
1005352e512cSHerbert Xu 		new->input = dst_discard;
1006352e512cSHerbert Xu 		new->output = dst_discard;
100714e50e57SDavid S. Miller 
100821efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
100921efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
101021efcfa0SEric Dumazet 		else
1011defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
101214e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
101314e50e57SDavid S. Miller 		if (rt->rt6i_idev)
101414e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
101514e50e57SDavid S. Miller 
10164e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10171716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
101814e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
101914e50e57SDavid S. Miller 
102014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
102114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
102214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
102314e50e57SDavid S. Miller #endif
102414e50e57SDavid S. Miller 
102514e50e57SDavid S. Miller 		dst_free(new);
102614e50e57SDavid S. Miller 	}
102714e50e57SDavid S. Miller 
102869ead7afSDavid S. Miller 	dst_release(dst_orig);
102969ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
103014e50e57SDavid S. Miller }
103114e50e57SDavid S. Miller 
10321da177e4SLinus Torvalds /*
10331da177e4SLinus Torvalds  *	Destination cache support functions
10341da177e4SLinus Torvalds  */
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct rt6_info *rt;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10411da177e4SLinus Torvalds 
10426f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10436f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10446f3118b5SNicolas Dichtel 	 * into this function always.
10456f3118b5SNicolas Dichtel 	 */
10466f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
10476f3118b5SNicolas Dichtel 		return NULL;
10486f3118b5SNicolas Dichtel 
1049a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10501da177e4SLinus Torvalds 		return dst;
1051a4477c4dSLi RongQing 
10521da177e4SLinus Torvalds 	return NULL;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	if (rt) {
106054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
106154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1062e0a1ad73SThomas Graf 				ip6_del_rt(rt);
106354c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10641da177e4SLinus Torvalds 			}
106554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
106654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
106754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
106854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
106954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
107054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct rt6_info *rt;
10761da177e4SLinus Torvalds 
10773ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10781da177e4SLinus Torvalds 
1079adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10801da177e4SLinus Torvalds 	if (rt) {
10811716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10821716a961SGao feng 			rt6_update_expires(rt, 0);
10831716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10841da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10886700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
10896700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10921da177e4SLinus Torvalds 
109381aded24SDavid S. Miller 	dst_confirm(dst);
10941da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
109581aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
109681aded24SDavid S. Miller 
10971da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10981da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1099defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11001da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1101defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1102defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11031da177e4SLinus Torvalds 		}
1104defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
110581aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
110942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
111042ae66c8SDavid S. Miller 		     int oif, u32 mark)
111181aded24SDavid S. Miller {
111281aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
111381aded24SDavid S. Miller 	struct dst_entry *dst;
111481aded24SDavid S. Miller 	struct flowi6 fl6;
111581aded24SDavid S. Miller 
111681aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
111781aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
111881aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11193e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
112081aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
112181aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11226502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
112381aded24SDavid S. Miller 
112481aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
112581aded24SDavid S. Miller 	if (!dst->error)
11266700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
112781aded24SDavid S. Miller 	dst_release(dst);
112881aded24SDavid S. Miller }
112981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
113081aded24SDavid S. Miller 
113181aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
113281aded24SDavid S. Miller {
113381aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
113481aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
113581aded24SDavid S. Miller }
113681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
113781aded24SDavid S. Miller 
11383a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11393a5ad2eeSDavid S. Miller {
11403a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11413a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11423a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11433a5ad2eeSDavid S. Miller 
11443a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11453a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11463a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11473a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11483a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11493a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11506502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
11513a5ad2eeSDavid S. Miller 
11523a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11533a5ad2eeSDavid S. Miller 	if (!dst->error)
11546700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
11553a5ad2eeSDavid S. Miller 	dst_release(dst);
11563a5ad2eeSDavid S. Miller }
11573a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11583a5ad2eeSDavid S. Miller 
11593a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11603a5ad2eeSDavid S. Miller {
11613a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11623a5ad2eeSDavid S. Miller }
11633a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
11643a5ad2eeSDavid S. Miller 
11650dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11661da177e4SLinus Torvalds {
11670dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11680dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11690dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11700dbaee3bSDavid S. Miller 
11711da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11721da177e4SLinus Torvalds 
11735578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11745578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	/*
11771da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11781da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11791da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11801da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11831da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11841da177e4SLinus Torvalds 	return mtu;
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
1187ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1188d33e4553SDavid S. Miller {
1189d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1190618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1191618f9bc7SSteffen Klassert 
1192618f9bc7SSteffen Klassert 	if (mtu)
1193618f9bc7SSteffen Klassert 		return mtu;
1194618f9bc7SSteffen Klassert 
1195618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1196d33e4553SDavid S. Miller 
1197d33e4553SDavid S. Miller 	rcu_read_lock();
1198d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1199d33e4553SDavid S. Miller 	if (idev)
1200d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1201d33e4553SDavid S. Miller 	rcu_read_unlock();
1202d33e4553SDavid S. Miller 
1203d33e4553SDavid S. Miller 	return mtu;
1204d33e4553SDavid S. Miller }
1205d33e4553SDavid S. Miller 
12063b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12073b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12085d0bbeebSThomas Graf 
12093b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
121087a11578SDavid S. Miller 				  struct flowi6 *fl6)
12111da177e4SLinus Torvalds {
121287a11578SDavid S. Miller 	struct dst_entry *dst;
12131da177e4SLinus Torvalds 	struct rt6_info *rt;
12141da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1215c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12161da177e4SLinus Torvalds 
121738308473SDavid S. Miller 	if (unlikely(!idev))
1218122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12191da177e4SLinus Torvalds 
12208b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
122138308473SDavid S. Miller 	if (unlikely(!rt)) {
12221da177e4SLinus Torvalds 		in6_dev_put(idev);
122387a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12241da177e4SLinus Torvalds 		goto out;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12278e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12288e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1229d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
123087a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12318e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12328e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
123314edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
12341da177e4SLinus Torvalds 
12353b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1236d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1237d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12383b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12391da177e4SLinus Torvalds 
12405578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12411da177e4SLinus Torvalds 
124287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
124387a11578SDavid S. Miller 
12441da177e4SLinus Torvalds out:
124587a11578SDavid S. Miller 	return dst;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12483d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12491da177e4SLinus Torvalds {
1250e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12513d0f24a7SStephen Hemminger 	int more = 0;
12521da177e4SLinus Torvalds 
12533b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12543b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12555d0bbeebSThomas Graf 
12561da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12571da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12581da177e4SLinus Torvalds 			*pprev = dst->next;
12591da177e4SLinus Torvalds 			dst_free(dst);
12601da177e4SLinus Torvalds 		} else {
12611da177e4SLinus Torvalds 			pprev = &dst->next;
12623d0f24a7SStephen Hemminger 			++more;
12631da177e4SLinus Torvalds 		}
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 
12663b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12675d0bbeebSThomas Graf 
12683d0f24a7SStephen Hemminger 	return more;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12721e493d19SDavid S. Miller 			    void *arg)
12731e493d19SDavid S. Miller {
12741e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12751e493d19SDavid S. Miller 
12761e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12771e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12781e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12791e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12801e493d19SDavid S. Miller 		if (func(rt, arg)) {
12811e493d19SDavid S. Miller 			*pprev = dst->next;
12821e493d19SDavid S. Miller 			dst_free(dst);
12831e493d19SDavid S. Miller 		} else {
12841e493d19SDavid S. Miller 			pprev = &dst->next;
12851e493d19SDavid S. Miller 		}
12861e493d19SDavid S. Miller 	}
12871e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12881e493d19SDavid S. Miller }
12891e493d19SDavid S. Miller 
1290569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	unsigned long now = jiffies;
129386393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12947019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12957019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12967019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12977019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12987019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1299fc66f95cSEric Dumazet 	int entries;
13001da177e4SLinus Torvalds 
1301fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
13027019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1303fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13041da177e4SLinus Torvalds 		goto out;
13051da177e4SLinus Torvalds 
13066891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13076891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13086891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1309fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1310fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13117019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13121da177e4SLinus Torvalds out:
13137019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1314fc66f95cSEric Dumazet 	return entries > rt_max_size;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
13176b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13181da177e4SLinus Torvalds {
13195170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1320a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13216b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1322c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1323c68f24ccSEric Dumazet 
1324c68f24ccSEric Dumazet 		rcu_read_lock();
1325c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1326c68f24ccSEric Dumazet 		if (idev)
13271da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1328c68f24ccSEric Dumazet 		else
132953b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1330c68f24ccSEric Dumazet 		rcu_read_unlock();
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds 	return hoplimit;
13331da177e4SLinus Torvalds }
1334abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds /*
13371da177e4SLinus Torvalds  *
13381da177e4SLinus Torvalds  */
13391da177e4SLinus Torvalds 
134086872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	int err;
13435578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13441da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13451da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13461da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1347c71099acSThomas Graf 	struct fib6_table *table;
13481da177e4SLinus Torvalds 	int addr_type;
13491da177e4SLinus Torvalds 
135086872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13511da177e4SLinus Torvalds 		return -EINVAL;
13521da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
135386872cb5SThomas Graf 	if (cfg->fc_src_len)
13541da177e4SLinus Torvalds 		return -EINVAL;
13551da177e4SLinus Torvalds #endif
135686872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13571da177e4SLinus Torvalds 		err = -ENODEV;
13585578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13591da177e4SLinus Torvalds 		if (!dev)
13601da177e4SLinus Torvalds 			goto out;
13611da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13621da177e4SLinus Torvalds 		if (!idev)
13631da177e4SLinus Torvalds 			goto out;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
136686872cb5SThomas Graf 	if (cfg->fc_metric == 0)
136786872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13681da177e4SLinus Torvalds 
1369c71099acSThomas Graf 	err = -ENOBUFS;
137038308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1371d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1372d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
137338308473SDavid S. Miller 		if (!table) {
1374f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1375d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1376d71314b4SMatti Vaittinen 		}
1377d71314b4SMatti Vaittinen 	} else {
1378d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1379d71314b4SMatti Vaittinen 	}
138038308473SDavid S. Miller 
138138308473SDavid S. Miller 	if (!table)
1382c71099acSThomas Graf 		goto out;
1383c71099acSThomas Graf 
13848b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13851da177e4SLinus Torvalds 
138638308473SDavid S. Miller 	if (!rt) {
13871da177e4SLinus Torvalds 		err = -ENOMEM;
13881da177e4SLinus Torvalds 		goto out;
13891da177e4SLinus Torvalds 	}
13901da177e4SLinus Torvalds 
13911716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13921716a961SGao feng 		rt6_set_expires(rt, jiffies +
13931716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13941716a961SGao feng 	else
13951716a961SGao feng 		rt6_clean_expires(rt);
13961da177e4SLinus Torvalds 
139786872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
139886872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
139986872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
140086872cb5SThomas Graf 
140186872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1404d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1405ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1406ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14071da177e4SLinus Torvalds 	else
1408d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14091da177e4SLinus Torvalds 
1410d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14111da177e4SLinus Torvalds 
141286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
141386872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14141da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
141511d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14161da177e4SLinus Torvalds 
14178e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14188e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14198e2ec639SYan, Zheng 		if (!metrics) {
14208e2ec639SYan, Zheng 			err = -ENOMEM;
14218e2ec639SYan, Zheng 			goto out;
14228e2ec639SYan, Zheng 		}
14238e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14248e2ec639SYan, Zheng 	}
14251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
142686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
142786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14281da177e4SLinus Torvalds #endif
14291da177e4SLinus Torvalds 
143086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14331da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14341da177e4SLinus Torvalds 	 */
143586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
143638308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
143738308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
143838308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14391da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14405578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14411da177e4SLinus Torvalds 			if (dev) {
14421da177e4SLinus Torvalds 				dev_put(dev);
14431da177e4SLinus Torvalds 				in6_dev_put(idev);
14441da177e4SLinus Torvalds 			}
14455578689aSDaniel Lezcano 			dev = net->loopback_dev;
14461da177e4SLinus Torvalds 			dev_hold(dev);
14471da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14481da177e4SLinus Torvalds 			if (!idev) {
14491da177e4SLinus Torvalds 				err = -ENODEV;
14501da177e4SLinus Torvalds 				goto out;
14511da177e4SLinus Torvalds 			}
14521da177e4SLinus Torvalds 		}
1453d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1454d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
14551da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1456ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1457ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1458ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1459ef2c7d7bSNicolas Dichtel 			break;
1460ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1461ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1462ef2c7d7bSNicolas Dichtel 			break;
1463b4949ab2SNicolas Dichtel 		case RTN_THROW:
1464b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1465b4949ab2SNicolas Dichtel 			break;
1466ef2c7d7bSNicolas Dichtel 		default:
1467ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1468ef2c7d7bSNicolas Dichtel 			break;
1469ef2c7d7bSNicolas Dichtel 		}
14701da177e4SLinus Torvalds 		goto install_route;
14711da177e4SLinus Torvalds 	}
14721da177e4SLinus Torvalds 
147386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1474b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14751da177e4SLinus Torvalds 		int gwa_type;
14761da177e4SLinus Torvalds 
147786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14784e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14791da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14821da177e4SLinus Torvalds 			struct rt6_info *grt;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14851da177e4SLinus Torvalds 			   addresses as nexthop address.
14861da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14871da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14881da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14891da177e4SLinus Torvalds 			   some exceptions. --ANK
14901da177e4SLinus Torvalds 			 */
14911da177e4SLinus Torvalds 			err = -EINVAL;
14921da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14931da177e4SLinus Torvalds 				goto out;
14941da177e4SLinus Torvalds 
14955578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
149838308473SDavid S. Miller 			if (!grt)
14991da177e4SLinus Torvalds 				goto out;
15001da177e4SLinus Torvalds 			if (dev) {
1501d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
150294e187c0SAmerigo Wang 					ip6_rt_put(grt);
15031da177e4SLinus Torvalds 					goto out;
15041da177e4SLinus Torvalds 				}
15051da177e4SLinus Torvalds 			} else {
1506d1918542SDavid S. Miller 				dev = grt->dst.dev;
15071da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15081da177e4SLinus Torvalds 				dev_hold(dev);
15091da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15101da177e4SLinus Torvalds 			}
15111da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15121da177e4SLinus Torvalds 				err = 0;
151394e187c0SAmerigo Wang 			ip6_rt_put(grt);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 			if (err)
15161da177e4SLinus Torvalds 				goto out;
15171da177e4SLinus Torvalds 		}
15181da177e4SLinus Torvalds 		err = -EINVAL;
151938308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15201da177e4SLinus Torvalds 			goto out;
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	err = -ENODEV;
152438308473SDavid S. Miller 	if (!dev)
15251da177e4SLinus Torvalds 		goto out;
15261da177e4SLinus Torvalds 
1527c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1528c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1529c3968a85SDaniel Walter 			err = -EINVAL;
1530c3968a85SDaniel Walter 			goto out;
1531c3968a85SDaniel Walter 		}
15324e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1533c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1534c3968a85SDaniel Walter 	} else
1535c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1536c3968a85SDaniel Walter 
153786872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds install_route:
154086872cb5SThomas Graf 	if (cfg->fc_mx) {
154186872cb5SThomas Graf 		struct nlattr *nla;
154286872cb5SThomas Graf 		int remaining;
15431da177e4SLinus Torvalds 
154486872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15458f4c1f9bSThomas Graf 			int type = nla_type(nla);
154686872cb5SThomas Graf 
154786872cb5SThomas Graf 			if (type) {
154886872cb5SThomas Graf 				if (type > RTAX_MAX) {
15491da177e4SLinus Torvalds 					err = -EINVAL;
15501da177e4SLinus Torvalds 					goto out;
15511da177e4SLinus Torvalds 				}
155286872cb5SThomas Graf 
1553defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15541da177e4SLinus Torvalds 			}
15551da177e4SLinus Torvalds 		}
15561da177e4SLinus Torvalds 	}
15571da177e4SLinus Torvalds 
1558d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15591da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1560c71099acSThomas Graf 	rt->rt6i_table = table;
156163152fc0SDaniel Lezcano 
1562c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
156363152fc0SDaniel Lezcano 
156486872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds out:
15671da177e4SLinus Torvalds 	if (dev)
15681da177e4SLinus Torvalds 		dev_put(dev);
15691da177e4SLinus Torvalds 	if (idev)
15701da177e4SLinus Torvalds 		in6_dev_put(idev);
15711da177e4SLinus Torvalds 	if (rt)
1572d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15731da177e4SLinus Torvalds 	return err;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
157686872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	int err;
1579c71099acSThomas Graf 	struct fib6_table *table;
1580d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15811da177e4SLinus Torvalds 
15826825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
15836825a26cSGao feng 		err = -ENOENT;
15846825a26cSGao feng 		goto out;
15856825a26cSGao feng 	}
15866c813a72SPatrick McHardy 
1587c71099acSThomas Graf 	table = rt->rt6i_table;
1588c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
158986872cb5SThomas Graf 	err = fib6_del(rt, info);
1590c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15911da177e4SLinus Torvalds 
15926825a26cSGao feng out:
159394e187c0SAmerigo Wang 	ip6_rt_put(rt);
15941da177e4SLinus Torvalds 	return err;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
1597e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1598e0a1ad73SThomas Graf {
15994d1169c1SDenis V. Lunev 	struct nl_info info = {
1600d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
16014d1169c1SDenis V. Lunev 	};
1602528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1603e0a1ad73SThomas Graf }
1604e0a1ad73SThomas Graf 
160586872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16061da177e4SLinus Torvalds {
1607c71099acSThomas Graf 	struct fib6_table *table;
16081da177e4SLinus Torvalds 	struct fib6_node *fn;
16091da177e4SLinus Torvalds 	struct rt6_info *rt;
16101da177e4SLinus Torvalds 	int err = -ESRCH;
16111da177e4SLinus Torvalds 
16125578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
161338308473SDavid S. Miller 	if (!table)
1614c71099acSThomas Graf 		return err;
16151da177e4SLinus Torvalds 
1616c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1617c71099acSThomas Graf 
1618c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
161986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
162086872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	if (fn) {
1623d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
162486872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1625d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1626d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16271da177e4SLinus Torvalds 				continue;
162886872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
162986872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16301da177e4SLinus Torvalds 				continue;
163186872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16321da177e4SLinus Torvalds 				continue;
1633d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1634c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16351da177e4SLinus Torvalds 
163686872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16371da177e4SLinus Torvalds 		}
16381da177e4SLinus Torvalds 	}
1639c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	return err;
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
16446700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1645a6279458SYOSHIFUJI Hideaki {
1646e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1647a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1648e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1649e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1650e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1651e8599ff4SDavid S. Miller 	struct neighbour *neigh;
165271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
16536e157b6aSDavid S. Miller 	int optlen, on_link;
16546e157b6aSDavid S. Miller 	u8 *lladdr;
1655e8599ff4SDavid S. Miller 
165629a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
165771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1658e8599ff4SDavid S. Miller 
1659e8599ff4SDavid S. Miller 	if (optlen < 0) {
16606e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1661e8599ff4SDavid S. Miller 		return;
1662e8599ff4SDavid S. Miller 	}
1663e8599ff4SDavid S. Miller 
166471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1665e8599ff4SDavid S. Miller 
166671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
16676e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1668e8599ff4SDavid S. Miller 		return;
1669e8599ff4SDavid S. Miller 	}
1670e8599ff4SDavid S. Miller 
16716e157b6aSDavid S. Miller 	on_link = 0;
167271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1673e8599ff4SDavid S. Miller 		on_link = 1;
167471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1675e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
16766e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1677e8599ff4SDavid S. Miller 		return;
1678e8599ff4SDavid S. Miller 	}
1679e8599ff4SDavid S. Miller 
1680e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1681e8599ff4SDavid S. Miller 	if (!in6_dev)
1682e8599ff4SDavid S. Miller 		return;
1683e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1684e8599ff4SDavid S. Miller 		return;
1685e8599ff4SDavid S. Miller 
1686e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1687e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1688e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1689e8599ff4SDavid S. Miller 	 */
1690e8599ff4SDavid S. Miller 
169171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1692e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1693e8599ff4SDavid S. Miller 		return;
1694e8599ff4SDavid S. Miller 	}
16956e157b6aSDavid S. Miller 
16966e157b6aSDavid S. Miller 	lladdr = NULL;
1697e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1698e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1699e8599ff4SDavid S. Miller 					     skb->dev);
1700e8599ff4SDavid S. Miller 		if (!lladdr) {
1701e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1702e8599ff4SDavid S. Miller 			return;
1703e8599ff4SDavid S. Miller 		}
1704e8599ff4SDavid S. Miller 	}
1705e8599ff4SDavid S. Miller 
17066e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17076e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17086e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17096e157b6aSDavid S. Miller 		return;
17106e157b6aSDavid S. Miller 	}
17116e157b6aSDavid S. Miller 
17126e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17136e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17146e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17156e157b6aSDavid S. Miller 	 */
17166e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17176e157b6aSDavid S. Miller 
171871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1719e8599ff4SDavid S. Miller 	if (!neigh)
1720e8599ff4SDavid S. Miller 		return;
1721e8599ff4SDavid S. Miller 
17221da177e4SLinus Torvalds 	/*
17231da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17241da177e4SLinus Torvalds 	 */
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17271da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17281da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17291da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17301da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17311da177e4SLinus Torvalds 		     );
17321da177e4SLinus Torvalds 
173371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
173438308473SDavid S. Miller 	if (!nrt)
17351da177e4SLinus Torvalds 		goto out;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17381da177e4SLinus Torvalds 	if (on_link)
17391da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17401da177e4SLinus Torvalds 
17414e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
17421da177e4SLinus Torvalds 
174340e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17441da177e4SLinus Torvalds 		goto out;
17451da177e4SLinus Torvalds 
1746d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1747d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
174871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
174960592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
17508d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17518d71740cSTom Tucker 
17521da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
17536e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1754e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds out:
1758e8599ff4SDavid S. Miller 	neigh_release(neigh);
17596e157b6aSDavid S. Miller }
17606e157b6aSDavid S. Miller 
17611da177e4SLinus Torvalds /*
17621da177e4SLinus Torvalds  *	Misc support functions
17631da177e4SLinus Torvalds  */
17641da177e4SLinus Torvalds 
17651716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
176621efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17671da177e4SLinus Torvalds {
1768d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17698b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17708b96d22dSDavid S. Miller 					    ort->rt6i_table);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	if (rt) {
1773d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1774d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17758e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17761da177e4SLinus Torvalds 
17774e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17788e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1779defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1780d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17811da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17821da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17831da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1784d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17851da177e4SLinus Torvalds 
17864e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17871716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17881716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17891716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
17901716a961SGao feng 			rt6_set_from(rt, ort);
17911da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17941da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17951da177e4SLinus Torvalds #endif
17960f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1797c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17981da177e4SLinus Torvalds 	}
17991da177e4SLinus Torvalds 	return rt;
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
180270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1803efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1804b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1805b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
180670ceb4f5SYOSHIFUJI Hideaki {
180770ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180870ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1809c71099acSThomas Graf 	struct fib6_table *table;
181070ceb4f5SYOSHIFUJI Hideaki 
1811efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
181238308473SDavid S. Miller 	if (!table)
1813c71099acSThomas Graf 		return NULL;
1814c71099acSThomas Graf 
18155744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1816c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
181770ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181870ceb4f5SYOSHIFUJI Hideaki 		goto out;
181970ceb4f5SYOSHIFUJI Hideaki 
1820d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1821d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
182270ceb4f5SYOSHIFUJI Hideaki 			continue;
182370ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
182470ceb4f5SYOSHIFUJI Hideaki 			continue;
182570ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
182670ceb4f5SYOSHIFUJI Hideaki 			continue;
1827d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182870ceb4f5SYOSHIFUJI Hideaki 		break;
182970ceb4f5SYOSHIFUJI Hideaki 	}
183070ceb4f5SYOSHIFUJI Hideaki out:
18315744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
183270ceb4f5SYOSHIFUJI Hideaki 	return rt;
183370ceb4f5SYOSHIFUJI Hideaki }
183470ceb4f5SYOSHIFUJI Hideaki 
1835efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1836b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1837b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183895c96174SEric Dumazet 					   unsigned int pref)
183970ceb4f5SYOSHIFUJI Hideaki {
184086872cb5SThomas Graf 	struct fib6_config cfg = {
184186872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1842238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
184386872cb5SThomas Graf 		.fc_ifindex	= ifindex,
184486872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
184586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
184686872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
184715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1848efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1849efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
185086872cb5SThomas Graf 	};
185170ceb4f5SYOSHIFUJI Hideaki 
18524e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18534e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
185486872cb5SThomas Graf 
1855e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1856e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
185786872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185870ceb4f5SYOSHIFUJI Hideaki 
185986872cb5SThomas Graf 	ip6_route_add(&cfg);
186070ceb4f5SYOSHIFUJI Hideaki 
1861efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
186270ceb4f5SYOSHIFUJI Hideaki }
186370ceb4f5SYOSHIFUJI Hideaki #endif
186470ceb4f5SYOSHIFUJI Hideaki 
1865b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	struct rt6_info *rt;
1868c71099acSThomas Graf 	struct fib6_table *table;
18691da177e4SLinus Torvalds 
1870c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
187138308473SDavid S. Miller 	if (!table)
1872c71099acSThomas Graf 		return NULL;
18731da177e4SLinus Torvalds 
18745744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1875d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1876d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1877045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18781da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18791da177e4SLinus Torvalds 			break;
18801da177e4SLinus Torvalds 	}
18811da177e4SLinus Torvalds 	if (rt)
1882d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
18835744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
18841da177e4SLinus Torvalds 	return rt;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
1887b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1888ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1889ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18901da177e4SLinus Torvalds {
189186872cb5SThomas Graf 	struct fib6_config cfg = {
189286872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1893238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
189486872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
189586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
189686872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
189715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
18985578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1899c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
190086872cb5SThomas Graf 	};
19011da177e4SLinus Torvalds 
19024e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19031da177e4SLinus Torvalds 
190486872cb5SThomas Graf 	ip6_route_add(&cfg);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19097b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	struct rt6_info *rt;
1912c71099acSThomas Graf 	struct fib6_table *table;
1913c71099acSThomas Graf 
1914c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19157b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
191638308473SDavid S. Miller 	if (!table)
1917c71099acSThomas Graf 		return;
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds restart:
1920c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1921d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19223e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
19233e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
1924d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1925c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1926e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19271da177e4SLinus Torvalds 			goto restart;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 	}
1930c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19335578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19345578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
193586872cb5SThomas Graf 				 struct fib6_config *cfg)
193686872cb5SThomas Graf {
193786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
193886872cb5SThomas Graf 
193986872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
194086872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
194186872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
194286872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
194386872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
194486872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
194586872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
194686872cb5SThomas Graf 
19475578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1948f1243c2dSBenjamin Thery 
19494e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19504e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19514e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
195286872cb5SThomas Graf }
195386872cb5SThomas Graf 
19545578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19551da177e4SLinus Torvalds {
195686872cb5SThomas Graf 	struct fib6_config cfg;
19571da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19581da177e4SLinus Torvalds 	int err;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	switch(cmd) {
19611da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19621da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
1963af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
19641da177e4SLinus Torvalds 			return -EPERM;
19651da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19661da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19671da177e4SLinus Torvalds 		if (err)
19681da177e4SLinus Torvalds 			return -EFAULT;
19691da177e4SLinus Torvalds 
19705578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
197186872cb5SThomas Graf 
19721da177e4SLinus Torvalds 		rtnl_lock();
19731da177e4SLinus Torvalds 		switch (cmd) {
19741da177e4SLinus Torvalds 		case SIOCADDRT:
197586872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19761da177e4SLinus Torvalds 			break;
19771da177e4SLinus Torvalds 		case SIOCDELRT:
197886872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19791da177e4SLinus Torvalds 			break;
19801da177e4SLinus Torvalds 		default:
19811da177e4SLinus Torvalds 			err = -EINVAL;
19821da177e4SLinus Torvalds 		}
19831da177e4SLinus Torvalds 		rtnl_unlock();
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 		return err;
19863ff50b79SStephen Hemminger 	}
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	return -EINVAL;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds /*
19921da177e4SLinus Torvalds  *	Drop the packet on the floor
19931da177e4SLinus Torvalds  */
19941da177e4SLinus Torvalds 
1995d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19961da177e4SLinus Torvalds {
1997612f09e8SYOSHIFUJI Hideaki 	int type;
1998adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1999612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2000612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20010660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
200245bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20033bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20043bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2005612f09e8SYOSHIFUJI Hideaki 			break;
2006612f09e8SYOSHIFUJI Hideaki 		}
2007612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2008612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20093bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20103bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2011612f09e8SYOSHIFUJI Hideaki 		break;
2012612f09e8SYOSHIFUJI Hideaki 	}
20133ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20141da177e4SLinus Torvalds 	kfree_skb(skb);
20151da177e4SLinus Torvalds 	return 0;
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
20189ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20199ce8ade0SThomas Graf {
2020612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20219ce8ade0SThomas Graf }
20229ce8ade0SThomas Graf 
202320380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20241da177e4SLinus Torvalds {
2025adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2026612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20296723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20306723ab54SDavid S. Miller 
20319ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20329ce8ade0SThomas Graf {
2033612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20349ce8ade0SThomas Graf }
20359ce8ade0SThomas Graf 
20369ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20379ce8ade0SThomas Graf {
2038adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2039612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20409ce8ade0SThomas Graf }
20419ce8ade0SThomas Graf 
20426723ab54SDavid S. Miller #endif
20436723ab54SDavid S. Miller 
20441da177e4SLinus Torvalds /*
20451da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20461da177e4SLinus Torvalds  */
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20491da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20508f031519SDavid S. Miller 				    bool anycast)
20511da177e4SLinus Torvalds {
2052c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20538b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
20541da177e4SLinus Torvalds 
205538308473SDavid S. Miller 	if (!rt) {
2056f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20571da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
205840385653SBen Greear 	}
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	in6_dev_hold(idev);
20611da177e4SLinus Torvalds 
206211d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2063d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2064d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20651da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
206858c4fb86SYOSHIFUJI Hideaki 	if (anycast)
206958c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
207058c4fb86SYOSHIFUJI Hideaki 	else
20711da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20721da177e4SLinus Torvalds 
20734e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20741da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20755578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20761da177e4SLinus Torvalds 
2077d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	return rt;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
2082c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2083c3968a85SDaniel Walter 			struct rt6_info *rt,
2084b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2085c3968a85SDaniel Walter 			unsigned int prefs,
2086c3968a85SDaniel Walter 			struct in6_addr *saddr)
2087c3968a85SDaniel Walter {
2088c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2089c3968a85SDaniel Walter 	int err = 0;
2090c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
20914e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2092c3968a85SDaniel Walter 	else
2093c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2094c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2095c3968a85SDaniel Walter 	return err;
2096c3968a85SDaniel Walter }
2097c3968a85SDaniel Walter 
2098c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2099c3968a85SDaniel Walter struct arg_dev_net_ip {
2100c3968a85SDaniel Walter 	struct net_device *dev;
2101c3968a85SDaniel Walter 	struct net *net;
2102c3968a85SDaniel Walter 	struct in6_addr *addr;
2103c3968a85SDaniel Walter };
2104c3968a85SDaniel Walter 
2105c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2106c3968a85SDaniel Walter {
2107c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2108c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2109c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2110c3968a85SDaniel Walter 
2111d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2112c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2113c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2114c3968a85SDaniel Walter 		/* remove prefsrc entry */
2115c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2116c3968a85SDaniel Walter 	}
2117c3968a85SDaniel Walter 	return 0;
2118c3968a85SDaniel Walter }
2119c3968a85SDaniel Walter 
2120c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2121c3968a85SDaniel Walter {
2122c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2123c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2124c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2125c3968a85SDaniel Walter 		.net = net,
2126c3968a85SDaniel Walter 		.addr = &ifp->addr,
2127c3968a85SDaniel Walter 	};
2128c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2129c3968a85SDaniel Walter }
2130c3968a85SDaniel Walter 
21318ed67789SDaniel Lezcano struct arg_dev_net {
21328ed67789SDaniel Lezcano 	struct net_device *dev;
21338ed67789SDaniel Lezcano 	struct net *net;
21348ed67789SDaniel Lezcano };
21358ed67789SDaniel Lezcano 
21361da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21371da177e4SLinus Torvalds {
2138bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2139bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21408ed67789SDaniel Lezcano 
2141d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2142c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21431da177e4SLinus Torvalds 		return -1;
2144c159d30cSDavid S. Miller 
21451da177e4SLinus Torvalds 	return 0;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
2148f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21491da177e4SLinus Torvalds {
21508ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21518ed67789SDaniel Lezcano 		.dev = dev,
21528ed67789SDaniel Lezcano 		.net = net,
21538ed67789SDaniel Lezcano 	};
21548ed67789SDaniel Lezcano 
21558ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21561e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
215995c96174SEric Dumazet struct rt6_mtu_change_arg {
21601da177e4SLinus Torvalds 	struct net_device *dev;
216195c96174SEric Dumazet 	unsigned int mtu;
21621da177e4SLinus Torvalds };
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21651da177e4SLinus Torvalds {
21661da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21671da177e4SLinus Torvalds 	struct inet6_dev *idev;
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21701da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21711da177e4SLinus Torvalds 	   We still use this lock to block changes
21721da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21731da177e4SLinus Torvalds 	*/
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
217638308473SDavid S. Miller 	if (!idev)
21771da177e4SLinus Torvalds 		return 0;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21801da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21811da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21821da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21831da177e4SLinus Torvalds 	 */
21841da177e4SLinus Torvalds 	/*
21851da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21861da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21871da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21881da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21891da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21901da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21911da177e4SLinus Torvalds 	   PMTU discouvery.
21921da177e4SLinus Torvalds 	 */
2193d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2194d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2195d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2196d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2197d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2198defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2199566cfd8fSSimon Arlott 	}
22001da177e4SLinus Torvalds 	return 0;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
220395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22041da177e4SLinus Torvalds {
2205c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2206c71099acSThomas Graf 		.dev = dev,
2207c71099acSThomas Graf 		.mtu = mtu,
2208c71099acSThomas Graf 	};
22091da177e4SLinus Torvalds 
2210c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
2213ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22145176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
221586872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2216ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
221786872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
221886872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
221951ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
222086872cb5SThomas Graf };
222186872cb5SThomas Graf 
222286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
222386872cb5SThomas Graf 			      struct fib6_config *cfg)
22241da177e4SLinus Torvalds {
222586872cb5SThomas Graf 	struct rtmsg *rtm;
222686872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
222786872cb5SThomas Graf 	int err;
22281da177e4SLinus Torvalds 
222986872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
223086872cb5SThomas Graf 	if (err < 0)
223186872cb5SThomas Graf 		goto errout;
22321da177e4SLinus Torvalds 
223386872cb5SThomas Graf 	err = -EINVAL;
223486872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
223586872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
223686872cb5SThomas Graf 
223786872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
223886872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
223986872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
224086872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
224186872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2242ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
224386872cb5SThomas Graf 
2244ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2245ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2246b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2247b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
224886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
224986872cb5SThomas Graf 
2250ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2251ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2252ab79ad14SMaciej Żenczykowski 
225315e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
225486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22553b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
225686872cb5SThomas Graf 
225786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
225886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
225986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22601da177e4SLinus Torvalds 	}
226186872cb5SThomas Graf 
226286872cb5SThomas Graf 	if (tb[RTA_DST]) {
226386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
226486872cb5SThomas Graf 
226586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
226686872cb5SThomas Graf 			goto errout;
226786872cb5SThomas Graf 
226886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22691da177e4SLinus Torvalds 	}
227086872cb5SThomas Graf 
227186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
227286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
227386872cb5SThomas Graf 
227486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
227586872cb5SThomas Graf 			goto errout;
227686872cb5SThomas Graf 
227786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22781da177e4SLinus Torvalds 	}
227986872cb5SThomas Graf 
2280c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2281c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2282c3968a85SDaniel Walter 
228386872cb5SThomas Graf 	if (tb[RTA_OIF])
228486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
228586872cb5SThomas Graf 
228686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
228786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
228886872cb5SThomas Graf 
228986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
229086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
229186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22921da177e4SLinus Torvalds 	}
229386872cb5SThomas Graf 
229486872cb5SThomas Graf 	if (tb[RTA_TABLE])
229586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
229686872cb5SThomas Graf 
229751ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
229851ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
229951ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
230051ebd318SNicolas Dichtel 	}
230151ebd318SNicolas Dichtel 
230286872cb5SThomas Graf 	err = 0;
230386872cb5SThomas Graf errout:
230486872cb5SThomas Graf 	return err;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
230751ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
230851ebd318SNicolas Dichtel {
230951ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
231051ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
231151ebd318SNicolas Dichtel 	int remaining;
231251ebd318SNicolas Dichtel 	int attrlen;
231351ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
231451ebd318SNicolas Dichtel 
231551ebd318SNicolas Dichtel beginning:
231651ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
231751ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
231851ebd318SNicolas Dichtel 
231951ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
232051ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
232151ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
232251ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
232351ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
232451ebd318SNicolas Dichtel 
232551ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
232651ebd318SNicolas Dichtel 		if (attrlen > 0) {
232751ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
232851ebd318SNicolas Dichtel 
232951ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
233051ebd318SNicolas Dichtel 			if (nla) {
233151ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
233251ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
233351ebd318SNicolas Dichtel 			}
233451ebd318SNicolas Dichtel 		}
233551ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
233651ebd318SNicolas Dichtel 		if (err) {
233751ebd318SNicolas Dichtel 			last_err = err;
233851ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
233951ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
234051ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
234151ebd318SNicolas Dichtel 			 */
234251ebd318SNicolas Dichtel 			if (add) {
234351ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
234451ebd318SNicolas Dichtel 				 * next hops that have been already added.
234551ebd318SNicolas Dichtel 				 */
234651ebd318SNicolas Dichtel 				add = 0;
234751ebd318SNicolas Dichtel 				goto beginning;
234851ebd318SNicolas Dichtel 			}
234951ebd318SNicolas Dichtel 		}
23501a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
23511a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
23521a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
23531a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
23541a72418bSNicolas Dichtel 		 */
23551a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
235651ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
235751ebd318SNicolas Dichtel 	}
235851ebd318SNicolas Dichtel 
235951ebd318SNicolas Dichtel 	return last_err;
236051ebd318SNicolas Dichtel }
236151ebd318SNicolas Dichtel 
2362661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
23631da177e4SLinus Torvalds {
236486872cb5SThomas Graf 	struct fib6_config cfg;
236586872cb5SThomas Graf 	int err;
23661da177e4SLinus Torvalds 
236786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
236886872cb5SThomas Graf 	if (err < 0)
236986872cb5SThomas Graf 		return err;
237086872cb5SThomas Graf 
237151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
237251ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
237351ebd318SNicolas Dichtel 	else
237486872cb5SThomas Graf 		return ip6_route_del(&cfg);
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
2377661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
23781da177e4SLinus Torvalds {
237986872cb5SThomas Graf 	struct fib6_config cfg;
238086872cb5SThomas Graf 	int err;
23811da177e4SLinus Torvalds 
238286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
238386872cb5SThomas Graf 	if (err < 0)
238486872cb5SThomas Graf 		return err;
238586872cb5SThomas Graf 
238651ebd318SNicolas Dichtel 	if (cfg.fc_mp)
238751ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
238851ebd318SNicolas Dichtel 	else
238986872cb5SThomas Graf 		return ip6_route_add(&cfg);
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
2392339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2393339bf98fSThomas Graf {
2394339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2395339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2396339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2397339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2398339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2399339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2400339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2401339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2402339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24036a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2404339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2405339bf98fSThomas Graf }
2406339bf98fSThomas Graf 
2407191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2408191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24090d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
241015e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
24117bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24121da177e4SLinus Torvalds {
24131da177e4SLinus Torvalds 	struct rtmsg *rtm;
24141da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2415e3703b3dSThomas Graf 	long expires;
24169e762a4aSPatrick McHardy 	u32 table;
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24191da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24201da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24211da177e4SLinus Torvalds 			return 1;
24221da177e4SLinus Torvalds 		}
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 
242515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
242638308473SDavid S. Miller 	if (!nlh)
242726932566SPatrick McHardy 		return -EMSGSIZE;
24282d7202bfSThomas Graf 
24292d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24301da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24311da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24321da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24331da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2434c71099acSThomas Graf 	if (rt->rt6i_table)
24359e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2436c71099acSThomas Graf 	else
24379e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24389e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2439c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2440c78679e8SDavid S. Miller 		goto nla_put_failure;
2441ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2442ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2443ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2444ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2445ef2c7d7bSNicolas Dichtel 			break;
2446ef2c7d7bSNicolas Dichtel 		case -EACCES:
2447ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2448ef2c7d7bSNicolas Dichtel 			break;
2449b4949ab2SNicolas Dichtel 		case -EAGAIN:
2450b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2451b4949ab2SNicolas Dichtel 			break;
2452ef2c7d7bSNicolas Dichtel 		default:
24531da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2454ef2c7d7bSNicolas Dichtel 			break;
2455ef2c7d7bSNicolas Dichtel 		}
2456ef2c7d7bSNicolas Dichtel 	}
2457ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2458ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2459d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24601da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24611da177e4SLinus Torvalds 	else
24621da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24631da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24641da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24651da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24661da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24671da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2468f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2469f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
24701da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2471f0396f60SDenis Ovsienko 		else
2472f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2473f0396f60SDenis Ovsienko 	}
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24761da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	if (dst) {
2479c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2480c78679e8SDavid S. Miller 			goto nla_put_failure;
24811da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24821da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2483c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2484c78679e8SDavid S. Miller 			goto nla_put_failure;
24851da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24861da177e4SLinus Torvalds 	if (src) {
2487c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2488c78679e8SDavid S. Miller 			goto nla_put_failure;
24891da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2490c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2491c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2492c78679e8SDavid S. Miller 		goto nla_put_failure;
24931da177e4SLinus Torvalds #endif
24947bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24957bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24967bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24978229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24987bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24997bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25007bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25017bc570c8SYOSHIFUJI Hideaki 						return 0;
25027bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25037bc570c8SYOSHIFUJI Hideaki 				} else {
25047bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25057bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25067bc570c8SYOSHIFUJI Hideaki 				}
25077bc570c8SYOSHIFUJI Hideaki 			}
25087bc570c8SYOSHIFUJI Hideaki 		} else
25097bc570c8SYOSHIFUJI Hideaki #endif
2510c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2511c78679e8SDavid S. Miller 				goto nla_put_failure;
25127bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25131da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2514c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2515c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2516c78679e8SDavid S. Miller 			goto nla_put_failure;
2517c3968a85SDaniel Walter 	}
2518c3968a85SDaniel Walter 
2519c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2520c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25214e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2522c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2523c78679e8SDavid S. Miller 			goto nla_put_failure;
25241da177e4SLinus Torvalds 	}
25252d7202bfSThomas Graf 
2526defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25272d7202bfSThomas Graf 		goto nla_put_failure;
25282d7202bfSThomas Graf 
2529dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2530dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
253194f826b8SEric Dumazet 			goto nla_put_failure;
253294f826b8SEric Dumazet 	}
25332d7202bfSThomas Graf 
2534c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2535c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2536c78679e8SDavid S. Miller 		goto nla_put_failure;
2537c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2538c78679e8SDavid S. Miller 		goto nla_put_failure;
25398253947eSLi Wei 
25408253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
254169cdf8f9SYOSHIFUJI Hideaki 
254287a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2543e3703b3dSThomas Graf 		goto nla_put_failure;
25441da177e4SLinus Torvalds 
25452d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25462d7202bfSThomas Graf 
25472d7202bfSThomas Graf nla_put_failure:
254826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
254926932566SPatrick McHardy 	return -EMSGSIZE;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25551da177e4SLinus Torvalds 	int prefix;
25561da177e4SLinus Torvalds 
25572d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25582d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25591da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25601da177e4SLinus Torvalds 	} else
25611da177e4SLinus Torvalds 		prefix = 0;
25621da177e4SLinus Torvalds 
2563191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2564191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
256515e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
25667bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
2569661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
25701da177e4SLinus Torvalds {
25713b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2572ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25731da177e4SLinus Torvalds 	struct rt6_info *rt;
2574ab364a6fSThomas Graf 	struct sk_buff *skb;
2575ab364a6fSThomas Graf 	struct rtmsg *rtm;
25764c9483b2SDavid S. Miller 	struct flowi6 fl6;
257772331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2578ab364a6fSThomas Graf 
2579ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2580ab364a6fSThomas Graf 	if (err < 0)
2581ab364a6fSThomas Graf 		goto errout;
2582ab364a6fSThomas Graf 
2583ab364a6fSThomas Graf 	err = -EINVAL;
25844c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2585ab364a6fSThomas Graf 
2586ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2587ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2588ab364a6fSThomas Graf 			goto errout;
2589ab364a6fSThomas Graf 
25904e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2591ab364a6fSThomas Graf 	}
2592ab364a6fSThomas Graf 
2593ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2594ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2595ab364a6fSThomas Graf 			goto errout;
2596ab364a6fSThomas Graf 
25974e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2598ab364a6fSThomas Graf 	}
2599ab364a6fSThomas Graf 
2600ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2601ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2602ab364a6fSThomas Graf 
2603ab364a6fSThomas Graf 	if (tb[RTA_OIF])
260472331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2605ab364a6fSThomas Graf 
2606ab364a6fSThomas Graf 	if (iif) {
2607ab364a6fSThomas Graf 		struct net_device *dev;
260872331bc0SShmulik Ladkani 		int flags = 0;
260972331bc0SShmulik Ladkani 
26105578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2611ab364a6fSThomas Graf 		if (!dev) {
2612ab364a6fSThomas Graf 			err = -ENODEV;
2613ab364a6fSThomas Graf 			goto errout;
2614ab364a6fSThomas Graf 		}
261572331bc0SShmulik Ladkani 
261672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
261772331bc0SShmulik Ladkani 
261872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
261972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
262072331bc0SShmulik Ladkani 
262172331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
262272331bc0SShmulik Ladkani 							       flags);
262372331bc0SShmulik Ladkani 	} else {
262472331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
262572331bc0SShmulik Ladkani 
262672331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2627ab364a6fSThomas Graf 	}
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
263038308473SDavid S. Miller 	if (!skb) {
263194e187c0SAmerigo Wang 		ip6_rt_put(rt);
2632ab364a6fSThomas Graf 		err = -ENOBUFS;
2633ab364a6fSThomas Graf 		goto errout;
2634ab364a6fSThomas Graf 	}
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26371da177e4SLinus Torvalds 	   through good chunk of routing engine.
26381da177e4SLinus Torvalds 	 */
2639459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26401da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26411da177e4SLinus Torvalds 
2642d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26431da177e4SLinus Torvalds 
26444c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
264515e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
26467bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26471da177e4SLinus Torvalds 	if (err < 0) {
2648ab364a6fSThomas Graf 		kfree_skb(skb);
2649ab364a6fSThomas Graf 		goto errout;
26501da177e4SLinus Torvalds 	}
26511da177e4SLinus Torvalds 
265215e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2653ab364a6fSThomas Graf errout:
26541da177e4SLinus Torvalds 	return err;
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
265786872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26581da177e4SLinus Torvalds {
26591da177e4SLinus Torvalds 	struct sk_buff *skb;
26605578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2661528c4cebSDenis V. Lunev 	u32 seq;
2662528c4cebSDenis V. Lunev 	int err;
26630d51aa80SJamal Hadi Salim 
2664528c4cebSDenis V. Lunev 	err = -ENOBUFS;
266538308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
266686872cb5SThomas Graf 
2667339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
266838308473SDavid S. Miller 	if (!skb)
266921713ebcSThomas Graf 		goto errout;
26701da177e4SLinus Torvalds 
2671191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
267215e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
267326932566SPatrick McHardy 	if (err < 0) {
267426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
267526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
267626932566SPatrick McHardy 		kfree_skb(skb);
267726932566SPatrick McHardy 		goto errout;
267826932566SPatrick McHardy 	}
267915e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
26805578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26811ce85fe4SPablo Neira Ayuso 	return;
268221713ebcSThomas Graf errout:
268321713ebcSThomas Graf 	if (err < 0)
26845578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26878ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2688351638e7SJiri Pirko 				unsigned long event, void *ptr)
26898ed67789SDaniel Lezcano {
2690351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2691c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26928ed67789SDaniel Lezcano 
26938ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2694d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26958ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26968ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2697d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26988ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2699d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27008ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27018ed67789SDaniel Lezcano #endif
27028ed67789SDaniel Lezcano 	}
27038ed67789SDaniel Lezcano 
27048ed67789SDaniel Lezcano 	return NOTIFY_OK;
27058ed67789SDaniel Lezcano }
27068ed67789SDaniel Lezcano 
27071da177e4SLinus Torvalds /*
27081da177e4SLinus Torvalds  *	/proc
27091da177e4SLinus Torvalds  */
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds struct rt6_proc_arg
27141da177e4SLinus Torvalds {
27151da177e4SLinus Torvalds 	char *buffer;
27161da177e4SLinus Torvalds 	int offset;
27171da177e4SLinus Torvalds 	int length;
27181da177e4SLinus Torvalds 	int skip;
27191da177e4SLinus Torvalds 	int len;
27201da177e4SLinus Torvalds };
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27231da177e4SLinus Torvalds {
272433120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
27251da177e4SLinus Torvalds 
27264b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27294b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27301da177e4SLinus Torvalds #else
273133120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27321da177e4SLinus Torvalds #endif
2733dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2734dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		seq_printf(m, "%pi6", &rt->rt6i_gateway);
27351da177e4SLinus Torvalds 	} else {
273633120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27371da177e4SLinus Torvalds 	}
273833120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2739d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2740d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2741d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27421da177e4SLinus Torvalds 	return 0;
27431da177e4SLinus Torvalds }
27441da177e4SLinus Torvalds 
274533120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27461da177e4SLinus Torvalds {
2747f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
274832b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
274933120b30SAlexey Dobriyan 	return 0;
27501da177e4SLinus Torvalds }
27511da177e4SLinus Torvalds 
275233120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
275333120b30SAlexey Dobriyan {
2754de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2755f3db4851SDaniel Lezcano }
2756f3db4851SDaniel Lezcano 
275733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
275833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
275933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
276033120b30SAlexey Dobriyan 	.read		= seq_read,
276133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2762b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
276333120b30SAlexey Dobriyan };
276433120b30SAlexey Dobriyan 
27651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27661da177e4SLinus Torvalds {
276769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27681da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
276969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
277069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
277169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
277269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
277369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2774fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
277569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	return 0;
27781da177e4SLinus Torvalds }
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27811da177e4SLinus Torvalds {
2782de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
278369ddb805SDaniel Lezcano }
278469ddb805SDaniel Lezcano 
27859a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27861da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27871da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27881da177e4SLinus Torvalds 	.read	 = seq_read,
27891da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2790b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27911da177e4SLinus Torvalds };
27921da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds static
2797fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
27981da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27991da177e4SLinus Torvalds {
2800c486da34SLucian Adrian Grijincu 	struct net *net;
2801c486da34SLucian Adrian Grijincu 	int delay;
2802c486da34SLucian Adrian Grijincu 	if (!write)
2803c486da34SLucian Adrian Grijincu 		return -EINVAL;
2804c486da34SLucian Adrian Grijincu 
2805c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2806c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28078d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28085b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28091da177e4SLinus Torvalds 	return 0;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
2812fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
28131da177e4SLinus Torvalds 	{
28141da177e4SLinus Torvalds 		.procname	=	"flush",
28154990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28161da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
281789c8b3a1SDave Jones 		.mode		=	0200,
28186d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28191da177e4SLinus Torvalds 	},
28201da177e4SLinus Torvalds 	{
28211da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28229a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28241da177e4SLinus Torvalds 		.mode		=	0644,
28256d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28261da177e4SLinus Torvalds 	},
28271da177e4SLinus Torvalds 	{
28281da177e4SLinus Torvalds 		.procname	=	"max_size",
28294990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28311da177e4SLinus Torvalds 		.mode		=	0644,
28326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28331da177e4SLinus Torvalds 	},
28341da177e4SLinus Torvalds 	{
28351da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28381da177e4SLinus Torvalds 		.mode		=	0644,
28396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28401da177e4SLinus Torvalds 	},
28411da177e4SLinus Torvalds 	{
28421da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28451da177e4SLinus Torvalds 		.mode		=	0644,
28466d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28471da177e4SLinus Torvalds 	},
28481da177e4SLinus Torvalds 	{
28491da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28521da177e4SLinus Torvalds 		.mode		=	0644,
28536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28541da177e4SLinus Torvalds 	},
28551da177e4SLinus Torvalds 	{
28561da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28591da177e4SLinus Torvalds 		.mode		=	0644,
2860f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28611da177e4SLinus Torvalds 	},
28621da177e4SLinus Torvalds 	{
28631da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28661da177e4SLinus Torvalds 		.mode		=	0644,
28676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28681da177e4SLinus Torvalds 	},
28691da177e4SLinus Torvalds 	{
28701da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28731da177e4SLinus Torvalds 		.mode		=	0644,
2874f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28751da177e4SLinus Torvalds 	},
28761da177e4SLinus Torvalds 	{
28771da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28801da177e4SLinus Torvalds 		.mode		=	0644,
28816d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28821da177e4SLinus Torvalds 	},
2883f8572d8fSEric W. Biederman 	{ }
28841da177e4SLinus Torvalds };
28851da177e4SLinus Torvalds 
28862c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2887760f2d01SDaniel Lezcano {
2888760f2d01SDaniel Lezcano 	struct ctl_table *table;
2889760f2d01SDaniel Lezcano 
2890760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2891760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2892760f2d01SDaniel Lezcano 			GFP_KERNEL);
28935ee09105SYOSHIFUJI Hideaki 
28945ee09105SYOSHIFUJI Hideaki 	if (table) {
28955ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2896c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
289786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28985ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28995ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29005ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29015ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29025ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29035ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29045ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29059c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2906464dc801SEric W. Biederman 
2907464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2908464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2909464dc801SEric W. Biederman 			table[0].procname = NULL;
29105ee09105SYOSHIFUJI Hideaki 	}
29115ee09105SYOSHIFUJI Hideaki 
2912760f2d01SDaniel Lezcano 	return table;
2913760f2d01SDaniel Lezcano }
29141da177e4SLinus Torvalds #endif
29151da177e4SLinus Torvalds 
29162c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2917cdb18761SDaniel Lezcano {
2918633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29198ed67789SDaniel Lezcano 
292086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
292186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2922f2fc6a54SBenjamin Thery 
2923fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2924fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2925fc66f95cSEric Dumazet 
29268ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29278ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29288ed67789SDaniel Lezcano 					   GFP_KERNEL);
29298ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2930fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2931d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29328ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2933d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
293462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
293562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29368ed67789SDaniel Lezcano 
29378ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29388ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29398ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29408ed67789SDaniel Lezcano 					       GFP_KERNEL);
294168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
294268fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2943d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29448ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2945d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
294662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
294762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29488ed67789SDaniel Lezcano 
29498ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29508ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29518ed67789SDaniel Lezcano 					       GFP_KERNEL);
295268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
295368fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2954d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29558ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2956d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
295762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
295862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29598ed67789SDaniel Lezcano #endif
29608ed67789SDaniel Lezcano 
2961b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2962b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2963b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2964b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2965b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2966b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2967b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2968b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2969b339a47cSPeter Zijlstra 
29706891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29716891a346SBenjamin Thery 
29728ed67789SDaniel Lezcano 	ret = 0;
29738ed67789SDaniel Lezcano out:
29748ed67789SDaniel Lezcano 	return ret;
2975f2fc6a54SBenjamin Thery 
297668fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
297768fffc67SPeter Zijlstra out_ip6_prohibit_entry:
297868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
297968fffc67SPeter Zijlstra out_ip6_null_entry:
298068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
298168fffc67SPeter Zijlstra #endif
2982fc66f95cSEric Dumazet out_ip6_dst_entries:
2983fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2984f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2985f2fc6a54SBenjamin Thery 	goto out;
2986cdb18761SDaniel Lezcano }
2987cdb18761SDaniel Lezcano 
29882c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2989cdb18761SDaniel Lezcano {
29908ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29918ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29928ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29938ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29948ed67789SDaniel Lezcano #endif
299541bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2996cdb18761SDaniel Lezcano }
2997cdb18761SDaniel Lezcano 
2998d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2999d189634eSThomas Graf {
3000d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3001d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3002d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3003d189634eSThomas Graf #endif
3004d189634eSThomas Graf 	return 0;
3005d189634eSThomas Graf }
3006d189634eSThomas Graf 
3007d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3008d189634eSThomas Graf {
3009d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3010ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3011ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3012d189634eSThomas Graf #endif
3013d189634eSThomas Graf }
3014d189634eSThomas Graf 
3015cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3016cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3017cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3018cdb18761SDaniel Lezcano };
3019cdb18761SDaniel Lezcano 
3020c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3021c3426b47SDavid S. Miller {
3022c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3023c3426b47SDavid S. Miller 
3024c3426b47SDavid S. Miller 	if (!bp)
3025c3426b47SDavid S. Miller 		return -ENOMEM;
3026c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3027c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3028c3426b47SDavid S. Miller 	return 0;
3029c3426b47SDavid S. Miller }
3030c3426b47SDavid S. Miller 
3031c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3032c3426b47SDavid S. Miller {
3033c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3034c3426b47SDavid S. Miller 
3035c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
303656a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3037c3426b47SDavid S. Miller 	kfree(bp);
3038c3426b47SDavid S. Miller }
3039c3426b47SDavid S. Miller 
30402b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3041c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3042c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3043c3426b47SDavid S. Miller };
3044c3426b47SDavid S. Miller 
3045d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3046d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3047d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3048d189634eSThomas Graf };
3049d189634eSThomas Graf 
30508ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30518ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30528ed67789SDaniel Lezcano 	.priority = 0,
30538ed67789SDaniel Lezcano };
30548ed67789SDaniel Lezcano 
3055433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30561da177e4SLinus Torvalds {
3057433d49c3SDaniel Lezcano 	int ret;
3058433d49c3SDaniel Lezcano 
30599a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30609a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30619a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30629a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30639a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3064c19a28e1SFernando Carrijo 		goto out;
306514e50e57SDavid S. Miller 
3066fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30678ed67789SDaniel Lezcano 	if (ret)
3068bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3069bdb3289fSDaniel Lezcano 
3070c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3071c3426b47SDavid S. Miller 	if (ret)
3072e8803b6cSDavid S. Miller 		goto out_dst_entries;
30732a0c451aSThomas Graf 
30747e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30757e52b33bSDavid S. Miller 	if (ret)
30767e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3077c3426b47SDavid S. Miller 
30785dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30795dc121e9SArnaud Ebalard 
30808ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30818ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30828ed67789SDaniel Lezcano 	 * manually for init_net */
3083d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30848ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3085bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3086d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30878ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3088d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30898ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3090bdb3289fSDaniel Lezcano   #endif
3091e8803b6cSDavid S. Miller 	ret = fib6_init();
3092433d49c3SDaniel Lezcano 	if (ret)
30938ed67789SDaniel Lezcano 		goto out_register_subsys;
3094433d49c3SDaniel Lezcano 
3095433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3096433d49c3SDaniel Lezcano 	if (ret)
3097e8803b6cSDavid S. Miller 		goto out_fib6_init;
3098c35b7e72SDaniel Lezcano 
3099433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3100433d49c3SDaniel Lezcano 	if (ret)
3101433d49c3SDaniel Lezcano 		goto xfrm6_init;
31027e5449c2SDaniel Lezcano 
3103d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3104d189634eSThomas Graf 	if (ret)
3105d189634eSThomas Graf 		goto fib6_rules_init;
3106d189634eSThomas Graf 
3107433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3108c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3109c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3110c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3111d189634eSThomas Graf 		goto out_register_late_subsys;
3112433d49c3SDaniel Lezcano 
31138ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3114cdb18761SDaniel Lezcano 	if (ret)
3115d189634eSThomas Graf 		goto out_register_late_subsys;
31168ed67789SDaniel Lezcano 
3117433d49c3SDaniel Lezcano out:
3118433d49c3SDaniel Lezcano 	return ret;
3119433d49c3SDaniel Lezcano 
3120d189634eSThomas Graf out_register_late_subsys:
3121d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3122433d49c3SDaniel Lezcano fib6_rules_init:
3123433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3124433d49c3SDaniel Lezcano xfrm6_init:
3125433d49c3SDaniel Lezcano 	xfrm6_fini();
31262a0c451aSThomas Graf out_fib6_init:
31272a0c451aSThomas Graf 	fib6_gc_cleanup();
31288ed67789SDaniel Lezcano out_register_subsys:
31298ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31307e52b33bSDavid S. Miller out_register_inetpeer:
31317e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3132fc66f95cSEric Dumazet out_dst_entries:
3133fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3134433d49c3SDaniel Lezcano out_kmem_cache:
3135f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3136433d49c3SDaniel Lezcano 	goto out;
31371da177e4SLinus Torvalds }
31381da177e4SLinus Torvalds 
31391da177e4SLinus Torvalds void ip6_route_cleanup(void)
31401da177e4SLinus Torvalds {
31418ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3142d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3143101367c2SThomas Graf 	fib6_rules_cleanup();
31441da177e4SLinus Torvalds 	xfrm6_fini();
31451da177e4SLinus Torvalds 	fib6_gc_cleanup();
3146c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31478ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
314841bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3149f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31501da177e4SLinus Torvalds }
3151