xref: /openbmc/linux/net/ipv6/route.c (revision ecd9883724b78cc72ed92c98bcb1a46c764fff21)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47457c4cbcSEric W. Biederman #include <net/net_namespace.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds #include <net/ipv6.h>
501da177e4SLinus Torvalds #include <net/ip6_fib.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/ndisc.h>
531da177e4SLinus Torvalds #include <net/addrconf.h>
541da177e4SLinus Torvalds #include <net/tcp.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <net/dst.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
588d71740cSTom Tucker #include <net/netevent.h>
5921713ebcSThomas Graf #include <net/netlink.h>
6051ebd318SNicolas Dichtel #include <net/nexthop.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
651da177e4SLinus Torvalds #include <linux/sysctl.h>
661da177e4SLinus Torvalds #endif
671da177e4SLinus Torvalds 
681716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6921efcfa0SEric Dumazet 				    const struct in6_addr *dest);
701da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
710dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
72ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
751da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
761da177e4SLinus Torvalds 				       struct net_device *dev, int how);
77569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
801da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
826700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
836700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
846700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
856700c270SDavid S. Miller 					struct sk_buff *skb);
861da177e4SLinus Torvalds 
8770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9195c96174SEric Dumazet 					   unsigned int pref);
92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
93b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
94b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9570ceb4f5SYOSHIFUJI Hideaki #endif
9670ceb4f5SYOSHIFUJI Hideaki 
9706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9806582540SDavid S. Miller {
9906582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10006582540SDavid S. Miller 	struct inet_peer *peer;
10106582540SDavid S. Miller 	u32 *p = NULL;
10206582540SDavid S. Miller 
1038e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1048e2ec639SYan, Zheng 		return NULL;
1058e2ec639SYan, Zheng 
106fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10706582540SDavid S. Miller 	if (peer) {
10806582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10906582540SDavid S. Miller 		unsigned long prev, new;
11006582540SDavid S. Miller 
11106582540SDavid S. Miller 		p = peer->metrics;
11206582540SDavid S. Miller 		if (inet_metrics_new(peer))
11306582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11406582540SDavid S. Miller 
11506582540SDavid S. Miller 		new = (unsigned long) p;
11606582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		if (prev != old) {
11906582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12006582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12106582540SDavid S. Miller 				p = NULL;
12206582540SDavid S. Miller 		}
12306582540SDavid S. Miller 	}
12406582540SDavid S. Miller 	return p;
12506582540SDavid S. Miller }
12606582540SDavid S. Miller 
127f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
128f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
129f894cbf8SDavid S. Miller 					     const void *daddr)
13039232973SDavid S. Miller {
13139232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
13239232973SDavid S. Miller 
133a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13439232973SDavid S. Miller 		return (const void *) p;
135f894cbf8SDavid S. Miller 	else if (skb)
136f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13739232973SDavid S. Miller 	return daddr;
13839232973SDavid S. Miller }
13939232973SDavid S. Miller 
140f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
141f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
142f894cbf8SDavid S. Miller 					  const void *daddr)
143d3aaeb38SDavid S. Miller {
14439232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14539232973SDavid S. Miller 	struct neighbour *n;
14639232973SDavid S. Miller 
147f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1488e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
149f83c7790SDavid S. Miller 	if (n)
150f83c7790SDavid S. Miller 		return n;
151f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
152f83c7790SDavid S. Miller }
153f83c7790SDavid S. Miller 
1549a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1551da177e4SLinus Torvalds 	.family			=	AF_INET6,
15609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1571da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1581da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1591da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1600dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
161ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1631da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1641da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1651da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1661da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1671da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1686e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1691ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
170d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1711da177e4SLinus Torvalds };
1721da177e4SLinus Torvalds 
173ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
174ec831ea7SRoland Dreier {
175618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
176618f9bc7SSteffen Klassert 
177618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
178ec831ea7SRoland Dreier }
179ec831ea7SRoland Dreier 
1806700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1816700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
18214e50e57SDavid S. Miller {
18314e50e57SDavid S. Miller }
18414e50e57SDavid S. Miller 
1856700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1866700c270SDavid S. Miller 				      struct sk_buff *skb)
187b587ee3bSDavid S. Miller {
188b587ee3bSDavid S. Miller }
189b587ee3bSDavid S. Miller 
1900972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1910972ddb2SHeld Bernhard 					 unsigned long old)
1920972ddb2SHeld Bernhard {
1930972ddb2SHeld Bernhard 	return NULL;
1940972ddb2SHeld Bernhard }
1950972ddb2SHeld Bernhard 
19614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19714e50e57SDavid S. Miller 	.family			=	AF_INET6,
19809640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
19914e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20014e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
201ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
202214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20314e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
204b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2050972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
206d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20714e50e57SDavid S. Miller };
20814e50e57SDavid S. Miller 
20962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21014edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
21162fa8a84SDavid S. Miller };
21262fa8a84SDavid S. Miller 
213fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2141da177e4SLinus Torvalds 	.dst = {
2151da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2161da177e4SLinus Torvalds 		.__use		= 1,
2172c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2181da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2191da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2201da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2211da177e4SLinus Torvalds 	},
2221da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2234f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2241da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2251da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
228101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
229101367c2SThomas Graf 
2306723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2316723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2326723ab54SDavid S. Miller 
233fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
234101367c2SThomas Graf 	.dst = {
235101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
236101367c2SThomas Graf 		.__use		= 1,
2372c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
238101367c2SThomas Graf 		.error		= -EACCES,
2399ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2409ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
241101367c2SThomas Graf 	},
242101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2434f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
244101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
245101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
246101367c2SThomas Graf };
247101367c2SThomas Graf 
248fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
249101367c2SThomas Graf 	.dst = {
250101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
251101367c2SThomas Graf 		.__use		= 1,
2522c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
253101367c2SThomas Graf 		.error		= -EINVAL,
254352e512cSHerbert Xu 		.input		= dst_discard,
255352e512cSHerbert Xu 		.output		= dst_discard,
256101367c2SThomas Graf 	},
257101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2584f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
259101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
260101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
261101367c2SThomas Graf };
262101367c2SThomas Graf 
263101367c2SThomas Graf #endif
264101367c2SThomas Graf 
2651da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26697bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
267957c665fSDavid S. Miller 					     struct net_device *dev,
2688b96d22dSDavid S. Miller 					     int flags,
2698b96d22dSDavid S. Miller 					     struct fib6_table *table)
2701da177e4SLinus Torvalds {
27197bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2726f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
273cf911662SDavid S. Miller 
27497bab73fSDavid S. Miller 	if (rt) {
2758104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2768104891bSSteffen Klassert 
2778104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2788b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
2796f3118b5SNicolas Dichtel 		rt->rt6i_genid = rt_genid(net);
28051ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28151ebd318SNicolas Dichtel 		rt->rt6i_nsiblings = 0;
28297bab73fSDavid S. Miller 	}
283cf911662SDavid S. Miller 	return rt;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2891da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
290*ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2911da177e4SLinus Torvalds 
2928e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2938e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2948e2ec639SYan, Zheng 
29538308473SDavid S. Miller 	if (idev) {
2961da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2971da177e4SLinus Torvalds 		in6_dev_put(idev);
2981da177e4SLinus Torvalds 	}
2991716a961SGao feng 
300*ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
301*ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3021716a961SGao feng 
30397bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
30497bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
305b3419363SDavid S. Miller 		inet_putpeer(peer);
306b3419363SDavid S. Miller 	}
307b3419363SDavid S. Miller }
308b3419363SDavid S. Miller 
309b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
310b3419363SDavid S. Miller {
31197bab73fSDavid S. Miller 	struct inet_peer_base *base;
312b3419363SDavid S. Miller 	struct inet_peer *peer;
313b3419363SDavid S. Miller 
31497bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
31597bab73fSDavid S. Miller 	if (!base)
31697bab73fSDavid S. Miller 		return;
31797bab73fSDavid S. Miller 
31897bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3197b34ca2aSDavid S. Miller 	if (peer) {
32097bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
321b3419363SDavid S. Miller 			inet_putpeer(peer);
3221da177e4SLinus Torvalds 	}
3237b34ca2aSDavid S. Miller }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3261da177e4SLinus Torvalds 			   int how)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3291da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3305a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
331c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3321da177e4SLinus Torvalds 
33397cac082SDavid S. Miller 	if (dev != loopback_dev) {
33497cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3355a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3365a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
33738308473SDavid S. Miller 			if (loopback_idev) {
3381da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3391da177e4SLinus Torvalds 				in6_dev_put(idev);
3401da177e4SLinus Torvalds 			}
3411da177e4SLinus Torvalds 		}
34297cac082SDavid S. Miller 	}
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
345a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3461da177e4SLinus Torvalds {
3471716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3481716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
349a50feda5SEric Dumazet 			return true;
3501716a961SGao feng 	} else if (rt->dst.from) {
3513fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3521716a961SGao feng 	}
353a50feda5SEric Dumazet 	return false;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
357c71099acSThomas Graf {
358a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
359a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
360c71099acSThomas Graf }
361c71099acSThomas Graf 
36251ebd318SNicolas Dichtel /* Multipath route selection:
36351ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
36451ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
36551ebd318SNicolas Dichtel  */
36651ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
36751ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
36851ebd318SNicolas Dichtel {
36951ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
37051ebd318SNicolas Dichtel 
371c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
372c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
37351ebd318SNicolas Dichtel 
37451ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
37551ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
37651ebd318SNicolas Dichtel 	case IPPROTO_UDP:
37751ebd318SNicolas Dichtel 	case IPPROTO_TCP:
37851ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
379b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
380b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38151ebd318SNicolas Dichtel 		break;
38251ebd318SNicolas Dichtel 
38351ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
384b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
385b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
38651ebd318SNicolas Dichtel 		break;
38751ebd318SNicolas Dichtel 	}
38851ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
389b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
39051ebd318SNicolas Dichtel 
39151ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39251ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
39351ebd318SNicolas Dichtel 	return val % candidate_count;
39451ebd318SNicolas Dichtel }
39551ebd318SNicolas Dichtel 
39651ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
39751ebd318SNicolas Dichtel 					     struct flowi6 *fl6)
39851ebd318SNicolas Dichtel {
39951ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
40051ebd318SNicolas Dichtel 	int route_choosen;
40151ebd318SNicolas Dichtel 
40251ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
40351ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
40451ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
40551ebd318SNicolas Dichtel 	 */
40651ebd318SNicolas Dichtel 	if (route_choosen)
40751ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
40851ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
40951ebd318SNicolas Dichtel 			route_choosen--;
41051ebd318SNicolas Dichtel 			if (route_choosen == 0) {
41151ebd318SNicolas Dichtel 				match = sibling;
41251ebd318SNicolas Dichtel 				break;
41351ebd318SNicolas Dichtel 			}
41451ebd318SNicolas Dichtel 		}
41551ebd318SNicolas Dichtel 	return match;
41651ebd318SNicolas Dichtel }
41751ebd318SNicolas Dichtel 
4181da177e4SLinus Torvalds /*
419c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4201da177e4SLinus Torvalds  */
4211da177e4SLinus Torvalds 
4228ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4238ed67789SDaniel Lezcano 						    struct rt6_info *rt,
424b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4251da177e4SLinus Torvalds 						    int oif,
426d420895eSYOSHIFUJI Hideaki 						    int flags)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4291da177e4SLinus Torvalds 	struct rt6_info *sprt;
4301da177e4SLinus Torvalds 
431dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
432dd3abc4eSYOSHIFUJI Hideaki 		goto out;
433dd3abc4eSYOSHIFUJI Hideaki 
434d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
435d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
436dd3abc4eSYOSHIFUJI Hideaki 
437dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4381da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4391da177e4SLinus Torvalds 				return sprt;
4401da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
44138308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4421da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
443d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4441da177e4SLinus Torvalds 						continue;
4451da177e4SLinus Torvalds 					if (local && (!oif ||
4461da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4471da177e4SLinus Torvalds 						continue;
4481da177e4SLinus Torvalds 				}
4491da177e4SLinus Torvalds 				local = sprt;
4501da177e4SLinus Torvalds 			}
451dd3abc4eSYOSHIFUJI Hideaki 		} else {
452dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
453dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
454dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
455dd3abc4eSYOSHIFUJI Hideaki 		}
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
458dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4591da177e4SLinus Torvalds 		if (local)
4601da177e4SLinus Torvalds 			return local;
4611da177e4SLinus Torvalds 
462d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4638ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4641da177e4SLinus Torvalds 	}
465dd3abc4eSYOSHIFUJI Hideaki out:
4661da177e4SLinus Torvalds 	return rt;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
46927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
47027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
47127097255SYOSHIFUJI Hideaki {
472f2c31e32SEric Dumazet 	struct neighbour *neigh;
47327097255SYOSHIFUJI Hideaki 	/*
47427097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
47527097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
47627097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
47727097255SYOSHIFUJI Hideaki 	 *
47827097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
47927097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
48027097255SYOSHIFUJI Hideaki 	 */
4812152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
482fdd6681dSAmerigo Wang 		return;
4832152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4842152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4852152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4862152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4872152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4882152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4897ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 
4912152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
49252e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
49327097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
49427097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
49527097255SYOSHIFUJI Hideaki 
496b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh) {
49727097255SYOSHIFUJI Hideaki 			neigh->updated = jiffies;
4982152caeaSYOSHIFUJI Hideaki / 吉藤英明 			write_unlock(&neigh->lock);
499b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		}
5002152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5012152caeaSYOSHIFUJI Hideaki / 吉藤英明 		target = (struct in6_addr *)&rt->rt6i_gateway;
50227097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
503d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
504f2c31e32SEric Dumazet 	} else {
5052152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5062152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
50727097255SYOSHIFUJI Hideaki 	}
5082152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
509f2c31e32SEric Dumazet }
51027097255SYOSHIFUJI Hideaki #else
51127097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
51227097255SYOSHIFUJI Hideaki {
51327097255SYOSHIFUJI Hideaki }
51427097255SYOSHIFUJI Hideaki #endif
51527097255SYOSHIFUJI Hideaki 
5161da177e4SLinus Torvalds /*
517554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5181da177e4SLinus Torvalds  */
519b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5201da177e4SLinus Torvalds {
521d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
522161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
523554cfb7eSYOSHIFUJI Hideaki 		return 2;
524161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
525161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
526161980f4SDavid S. Miller 		return 1;
527554cfb7eSYOSHIFUJI Hideaki 	return 0;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
530a5a81f0bSPaul Marks static inline bool rt6_check_neigh(struct rt6_info *rt)
5311da177e4SLinus Torvalds {
532f2c31e32SEric Dumazet 	struct neighbour *neigh;
533a5a81f0bSPaul Marks 	bool ret = false;
534f2c31e32SEric Dumazet 
5354d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5364d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
537145a3621SYOSHIFUJI Hideaki / 吉藤英明 		return true;
538145a3621SYOSHIFUJI Hideaki / 吉藤英明 
539145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
540145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
541145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
542145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
543554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
544a5a81f0bSPaul Marks 			ret = true;
545398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
546a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
547a5a81f0bSPaul Marks 			ret = true;
548398bcbebSYOSHIFUJI Hideaki #endif
549145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
550a5a81f0bSPaul Marks 	}
551145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
552145a3621SYOSHIFUJI Hideaki / 吉藤英明 
553a5a81f0bSPaul Marks 	return ret;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
556554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
557554cfb7eSYOSHIFUJI Hideaki 			   int strict)
558554cfb7eSYOSHIFUJI Hideaki {
559a5a81f0bSPaul Marks 	int m;
5604d0c5911SYOSHIFUJI Hideaki 
5614d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
56277d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
563554cfb7eSYOSHIFUJI Hideaki 		return -1;
564ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
565ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
566ebacaaa0SYOSHIFUJI Hideaki #endif
567a5a81f0bSPaul Marks 	if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
568554cfb7eSYOSHIFUJI Hideaki 		return -1;
569554cfb7eSYOSHIFUJI Hideaki 	return m;
570554cfb7eSYOSHIFUJI Hideaki }
571554cfb7eSYOSHIFUJI Hideaki 
572f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
573f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
574554cfb7eSYOSHIFUJI Hideaki {
575554cfb7eSYOSHIFUJI Hideaki 	int m;
576554cfb7eSYOSHIFUJI Hideaki 
577554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
578f11e6659SDavid S. Miller 		goto out;
579554cfb7eSYOSHIFUJI Hideaki 
580554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
581554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
582f11e6659SDavid S. Miller 		goto out;
583554cfb7eSYOSHIFUJI Hideaki 
584f11e6659SDavid S. Miller 	if (m > *mpri) {
585ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
58627097255SYOSHIFUJI Hideaki 			rt6_probe(match);
587f11e6659SDavid S. Miller 		*mpri = m;
588554cfb7eSYOSHIFUJI Hideaki 		match = rt;
589ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
59027097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5911da177e4SLinus Torvalds 	}
592f11e6659SDavid S. Miller 
593f11e6659SDavid S. Miller out:
594f11e6659SDavid S. Miller 	return match;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
597f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
598f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
599f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
600f11e6659SDavid S. Miller {
601f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
602f11e6659SDavid S. Miller 	int mpri = -1;
603f11e6659SDavid S. Miller 
604f11e6659SDavid S. Miller 	match = NULL;
605f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
606d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
607f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
608f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
609d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
610f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
611f11e6659SDavid S. Miller 
612f11e6659SDavid S. Miller 	return match;
613f11e6659SDavid S. Miller }
614f11e6659SDavid S. Miller 
615f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
616f11e6659SDavid S. Miller {
617f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6188ed67789SDaniel Lezcano 	struct net *net;
619f11e6659SDavid S. Miller 
620f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
621f11e6659SDavid S. Miller 	if (!rt0)
622f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
623f11e6659SDavid S. Miller 
624f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
625f11e6659SDavid S. Miller 
626554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
627f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
628d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
629f11e6659SDavid S. Miller 
630554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
631f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
632f11e6659SDavid S. Miller 			next = fn->leaf;
633f11e6659SDavid S. Miller 
634f11e6659SDavid S. Miller 		if (next != rt0)
635f11e6659SDavid S. Miller 			fn->rr_ptr = next;
636554cfb7eSYOSHIFUJI Hideaki 	}
637554cfb7eSYOSHIFUJI Hideaki 
638d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
639a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
64270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
64370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
644b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
64570ceb4f5SYOSHIFUJI Hideaki {
646c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
64770ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
64870ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
64970ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6504bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
65170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
65270ceb4f5SYOSHIFUJI Hideaki 
65370ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
65470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
65570ceb4f5SYOSHIFUJI Hideaki 	}
65670ceb4f5SYOSHIFUJI Hideaki 
65770ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
65870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
65970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
66170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
66270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
66370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
66470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
66570ceb4f5SYOSHIFUJI Hideaki 		}
66670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
66770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
66870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
66970ceb4f5SYOSHIFUJI Hideaki 		}
67070ceb4f5SYOSHIFUJI Hideaki 	}
67170ceb4f5SYOSHIFUJI Hideaki 
67270ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
67370ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6743933fc95SJens Rosenboom 		return -EINVAL;
67570ceb4f5SYOSHIFUJI Hideaki 
6764bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
67770ceb4f5SYOSHIFUJI Hideaki 
67870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
67970ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
68070ceb4f5SYOSHIFUJI Hideaki 	else {
68170ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
68270ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
68370ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
68470ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
68570ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
68670ceb4f5SYOSHIFUJI Hideaki 	}
68770ceb4f5SYOSHIFUJI Hideaki 
688efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
689efa2cea0SDaniel Lezcano 				dev->ifindex);
69070ceb4f5SYOSHIFUJI Hideaki 
69170ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
692e0a1ad73SThomas Graf 		ip6_del_rt(rt);
69370ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
69470ceb4f5SYOSHIFUJI Hideaki 	}
69570ceb4f5SYOSHIFUJI Hideaki 
69670ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
697efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
69870ceb4f5SYOSHIFUJI Hideaki 					pref);
69970ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
70070ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
70170ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
70270ceb4f5SYOSHIFUJI Hideaki 
70370ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7041716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7051716a961SGao feng 			rt6_clean_expires(rt);
7061716a961SGao feng 		else
7071716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7081716a961SGao feng 
70994e187c0SAmerigo Wang 		ip6_rt_put(rt);
71070ceb4f5SYOSHIFUJI Hideaki 	}
71170ceb4f5SYOSHIFUJI Hideaki 	return 0;
71270ceb4f5SYOSHIFUJI Hideaki }
71370ceb4f5SYOSHIFUJI Hideaki #endif
71470ceb4f5SYOSHIFUJI Hideaki 
7158ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
716982f56f3SYOSHIFUJI Hideaki do { \
7178ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
718982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
719e0eda7bbSVille Nuorvala 		while (1) { \
720982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
721c71099acSThomas Graf 				goto out; \
722982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
723982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7248bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
725982f56f3SYOSHIFUJI Hideaki 			else \
726982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
727c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
728c71099acSThomas Graf 				goto restart; \
729c71099acSThomas Graf 		} \
730982f56f3SYOSHIFUJI Hideaki 	} \
731982f56f3SYOSHIFUJI Hideaki } while (0)
732c71099acSThomas Graf 
7338ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7348ed67789SDaniel Lezcano 					     struct fib6_table *table,
7354c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	struct fib6_node *fn;
7381da177e4SLinus Torvalds 	struct rt6_info *rt;
7391da177e4SLinus Torvalds 
740c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7414c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
742c71099acSThomas Graf restart:
743c71099acSThomas Graf 	rt = fn->leaf;
7444c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
74551ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
74651ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
7474c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
748c71099acSThomas Graf out:
749d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
750c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7511da177e4SLinus Torvalds 	return rt;
752c71099acSThomas Graf 
753c71099acSThomas Graf }
754c71099acSThomas Graf 
755ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
756ea6e574eSFlorian Westphal 				    int flags)
757ea6e574eSFlorian Westphal {
758ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
759ea6e574eSFlorian Westphal }
760ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
761ea6e574eSFlorian Westphal 
7629acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7639acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
764c71099acSThomas Graf {
7654c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7664c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7674c9483b2SDavid S. Miller 		.daddr = *daddr,
768c71099acSThomas Graf 	};
769c71099acSThomas Graf 	struct dst_entry *dst;
77077d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
771c71099acSThomas Graf 
772adaa70bbSThomas Graf 	if (saddr) {
7734c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
774adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
775adaa70bbSThomas Graf 	}
776adaa70bbSThomas Graf 
7774c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
778c71099acSThomas Graf 	if (dst->error == 0)
779c71099acSThomas Graf 		return (struct rt6_info *) dst;
780c71099acSThomas Graf 
781c71099acSThomas Graf 	dst_release(dst);
782c71099acSThomas Graf 
7831da177e4SLinus Torvalds 	return NULL;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7867159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7877159039aSYOSHIFUJI Hideaki 
788c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7891da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7901da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7911da177e4SLinus Torvalds    be destroyed.
7921da177e4SLinus Torvalds  */
7931da177e4SLinus Torvalds 
79486872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	int err;
797c71099acSThomas Graf 	struct fib6_table *table;
7981da177e4SLinus Torvalds 
799c71099acSThomas Graf 	table = rt->rt6i_table;
800c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
80186872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
802c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	return err;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
80740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
80840e22e8fSThomas Graf {
8094d1169c1SDenis V. Lunev 	struct nl_info info = {
810d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8114d1169c1SDenis V. Lunev 	};
812528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
81340e22e8fSThomas Graf }
81440e22e8fSThomas Graf 
8151716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
81621efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
817b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	struct rt6_info *rt;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/*
8221da177e4SLinus Torvalds 	 *	Clone the route.
8231da177e4SLinus Torvalds 	 */
8241da177e4SLinus Torvalds 
82521efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	if (rt) {
82858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
829bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
83021efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
83158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8324e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
83358c4fb86SYOSHIFUJI Hideaki 		}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8381da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8394e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8401da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds #endif
84395a9a5baSYOSHIFUJI Hideaki 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	return rt;
8461da177e4SLinus Torvalds }
84795a9a5baSYOSHIFUJI Hideaki 
84821efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
84921efcfa0SEric Dumazet 					const struct in6_addr *daddr)
850299d9939SYOSHIFUJI Hideaki {
85121efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
85221efcfa0SEric Dumazet 
853887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
854299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
855299d9939SYOSHIFUJI Hideaki 	return rt;
856299d9939SYOSHIFUJI Hideaki }
857299d9939SYOSHIFUJI Hideaki 
8588ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8594c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct fib6_node *fn;
862519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
863c71099acSThomas Graf 	int strict = 0;
8641da177e4SLinus Torvalds 	int attempts = 3;
865519fbd87SYOSHIFUJI Hideaki 	int err;
86653b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8671da177e4SLinus Torvalds 
86877d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds relookup:
871c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8721da177e4SLinus Torvalds 
8738238dd06SYOSHIFUJI Hideaki restart_2:
8744c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds restart:
8774acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
87851ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && oif == 0)
87951ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
8804c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8818ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8828238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8831da177e4SLinus Torvalds 		goto out;
8841da177e4SLinus Torvalds 
885d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
886c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8871da177e4SLinus Torvalds 
888c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
8894c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8907343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8914c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8927343ff31SDavid S. Miller 	else
8937343ff31SDavid S. Miller 		goto out2;
8941da177e4SLinus Torvalds 
89594e187c0SAmerigo Wang 	ip6_rt_put(rt);
8968ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8971da177e4SLinus Torvalds 
898d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
899e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
90040e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
901e40cf353SYOSHIFUJI Hideaki 		if (!err)
902e40cf353SYOSHIFUJI Hideaki 			goto out2;
903e40cf353SYOSHIFUJI Hideaki 	}
904e40cf353SYOSHIFUJI Hideaki 
905e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9061da177e4SLinus Torvalds 		goto out2;
9071da177e4SLinus Torvalds 
908519fbd87SYOSHIFUJI Hideaki 	/*
909c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
910519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9111da177e4SLinus Torvalds 	 */
91294e187c0SAmerigo Wang 	ip6_rt_put(rt);
9131da177e4SLinus Torvalds 	goto relookup;
914e40cf353SYOSHIFUJI Hideaki 
915519fbd87SYOSHIFUJI Hideaki out:
9168238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9178238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9188238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9198238dd06SYOSHIFUJI Hideaki 	}
920d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
921c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9221da177e4SLinus Torvalds out2:
923d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
924d8d1f30bSChangli Gao 	rt->dst.__use++;
925c71099acSThomas Graf 
926c71099acSThomas Graf 	return rt;
927c71099acSThomas Graf }
928c71099acSThomas Graf 
9298ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9304c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9314acad72dSPavel Emelyanov {
9324c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9334acad72dSPavel Emelyanov }
9344acad72dSPavel Emelyanov 
93572331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
93672331bc0SShmulik Ladkani 						struct net_device *dev,
93772331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
93872331bc0SShmulik Ladkani {
93972331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
94072331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
94172331bc0SShmulik Ladkani 
94272331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
94372331bc0SShmulik Ladkani }
94472331bc0SShmulik Ladkani 
945c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
946c71099acSThomas Graf {
947b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
948c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
949adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9504c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9514c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9524c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9534c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9546502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9554c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9564c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
957c71099acSThomas Graf 	};
958adaa70bbSThomas Graf 
95972331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
960c71099acSThomas Graf }
961c71099acSThomas Graf 
9628ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9634c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
964c71099acSThomas Graf {
9654c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
966c71099acSThomas Graf }
967c71099acSThomas Graf 
9689c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9694c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
970c71099acSThomas Graf {
971c71099acSThomas Graf 	int flags = 0;
972c71099acSThomas Graf 
9731fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9744dc27d1cSDavid McCullough 
9754c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
97677d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
977c71099acSThomas Graf 
9784c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
979adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9800c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9810c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
982adaa70bbSThomas Graf 
9834c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9867159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9871da177e4SLinus Torvalds 
9882774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
98914e50e57SDavid S. Miller {
9905c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
99114e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
99214e50e57SDavid S. Miller 
993f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
99414e50e57SDavid S. Miller 	if (rt) {
995d8d1f30bSChangli Gao 		new = &rt->dst;
99614e50e57SDavid S. Miller 
9978104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
9988104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
9998104891bSSteffen Klassert 
100014e50e57SDavid S. Miller 		new->__use = 1;
1001352e512cSHerbert Xu 		new->input = dst_discard;
1002352e512cSHerbert Xu 		new->output = dst_discard;
100314e50e57SDavid S. Miller 
100421efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
100521efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
100621efcfa0SEric Dumazet 		else
1007defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
100814e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
100914e50e57SDavid S. Miller 		if (rt->rt6i_idev)
101014e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
101114e50e57SDavid S. Miller 
10124e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10131716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
101414e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
101514e50e57SDavid S. Miller 
101614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
101714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
101814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
101914e50e57SDavid S. Miller #endif
102014e50e57SDavid S. Miller 
102114e50e57SDavid S. Miller 		dst_free(new);
102214e50e57SDavid S. Miller 	}
102314e50e57SDavid S. Miller 
102469ead7afSDavid S. Miller 	dst_release(dst_orig);
102569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
102614e50e57SDavid S. Miller }
102714e50e57SDavid S. Miller 
10281da177e4SLinus Torvalds /*
10291da177e4SLinus Torvalds  *	Destination cache support functions
10301da177e4SLinus Torvalds  */
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	struct rt6_info *rt;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10371da177e4SLinus Torvalds 
10386f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10396f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10406f3118b5SNicolas Dichtel 	 * into this function always.
10416f3118b5SNicolas Dichtel 	 */
10426f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
10436f3118b5SNicolas Dichtel 		return NULL;
10446f3118b5SNicolas Dichtel 
1045a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10461da177e4SLinus Torvalds 		return dst;
1047a4477c4dSLi RongQing 
10481da177e4SLinus Torvalds 	return NULL;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (rt) {
105654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
105754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1058e0a1ad73SThomas Graf 				ip6_del_rt(rt);
105954c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10601da177e4SLinus Torvalds 			}
106154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
106254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
106354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
106454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
106554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
106654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	struct rt6_info *rt;
10721da177e4SLinus Torvalds 
10733ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10741da177e4SLinus Torvalds 
1075adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10761da177e4SLinus Torvalds 	if (rt) {
10771716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10781716a961SGao feng 			rt6_update_expires(rt, 0);
10791716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10801da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10846700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
10856700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10881da177e4SLinus Torvalds 
108981aded24SDavid S. Miller 	dst_confirm(dst);
10901da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
109181aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
109281aded24SDavid S. Miller 
10931da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10941da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1095defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10961da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1097defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1098defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10991da177e4SLinus Torvalds 		}
1100defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
110181aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
110542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
110642ae66c8SDavid S. Miller 		     int oif, u32 mark)
110781aded24SDavid S. Miller {
110881aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
110981aded24SDavid S. Miller 	struct dst_entry *dst;
111081aded24SDavid S. Miller 	struct flowi6 fl6;
111181aded24SDavid S. Miller 
111281aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
111381aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
111481aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11153e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
111681aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
111781aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11186502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
111981aded24SDavid S. Miller 
112081aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
112181aded24SDavid S. Miller 	if (!dst->error)
11226700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
112381aded24SDavid S. Miller 	dst_release(dst);
112481aded24SDavid S. Miller }
112581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
112681aded24SDavid S. Miller 
112781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
112881aded24SDavid S. Miller {
112981aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
113081aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
113181aded24SDavid S. Miller }
113281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
113381aded24SDavid S. Miller 
11343a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11353a5ad2eeSDavid S. Miller {
11363a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11373a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11383a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11393a5ad2eeSDavid S. Miller 
11403a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11413a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11423a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11433a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11443a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11453a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11466502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
11473a5ad2eeSDavid S. Miller 
11483a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11493a5ad2eeSDavid S. Miller 	if (!dst->error)
11506700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
11513a5ad2eeSDavid S. Miller 	dst_release(dst);
11523a5ad2eeSDavid S. Miller }
11533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11543a5ad2eeSDavid S. Miller 
11553a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11563a5ad2eeSDavid S. Miller {
11573a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11583a5ad2eeSDavid S. Miller }
11593a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
11603a5ad2eeSDavid S. Miller 
11610dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11621da177e4SLinus Torvalds {
11630dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11640dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11650dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11660dbaee3bSDavid S. Miller 
11671da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11681da177e4SLinus Torvalds 
11695578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11705578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/*
11731da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11741da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11751da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11761da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11791da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11801da177e4SLinus Torvalds 	return mtu;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
1183ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1184d33e4553SDavid S. Miller {
1185d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1186618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1187618f9bc7SSteffen Klassert 
1188618f9bc7SSteffen Klassert 	if (mtu)
1189618f9bc7SSteffen Klassert 		return mtu;
1190618f9bc7SSteffen Klassert 
1191618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1192d33e4553SDavid S. Miller 
1193d33e4553SDavid S. Miller 	rcu_read_lock();
1194d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1195d33e4553SDavid S. Miller 	if (idev)
1196d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1197d33e4553SDavid S. Miller 	rcu_read_unlock();
1198d33e4553SDavid S. Miller 
1199d33e4553SDavid S. Miller 	return mtu;
1200d33e4553SDavid S. Miller }
1201d33e4553SDavid S. Miller 
12023b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12033b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12045d0bbeebSThomas Graf 
12053b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
120687a11578SDavid S. Miller 				  struct flowi6 *fl6)
12071da177e4SLinus Torvalds {
120887a11578SDavid S. Miller 	struct dst_entry *dst;
12091da177e4SLinus Torvalds 	struct rt6_info *rt;
12101da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1211c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12121da177e4SLinus Torvalds 
121338308473SDavid S. Miller 	if (unlikely(!idev))
1214122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12151da177e4SLinus Torvalds 
12168b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
121738308473SDavid S. Miller 	if (unlikely(!rt)) {
12181da177e4SLinus Torvalds 		in6_dev_put(idev);
121987a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12201da177e4SLinus Torvalds 		goto out;
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
12238e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12248e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1225d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
122687a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12278e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12288e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
122914edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
12301da177e4SLinus Torvalds 
12313b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1232d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1233d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12343b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12351da177e4SLinus Torvalds 
12365578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12371da177e4SLinus Torvalds 
123887a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
123987a11578SDavid S. Miller 
12401da177e4SLinus Torvalds out:
124187a11578SDavid S. Miller 	return dst;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12443d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12451da177e4SLinus Torvalds {
1246e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12473d0f24a7SStephen Hemminger 	int more = 0;
12481da177e4SLinus Torvalds 
12493b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12503b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12515d0bbeebSThomas Graf 
12521da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12531da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12541da177e4SLinus Torvalds 			*pprev = dst->next;
12551da177e4SLinus Torvalds 			dst_free(dst);
12561da177e4SLinus Torvalds 		} else {
12571da177e4SLinus Torvalds 			pprev = &dst->next;
12583d0f24a7SStephen Hemminger 			++more;
12591da177e4SLinus Torvalds 		}
12601da177e4SLinus Torvalds 	}
12611da177e4SLinus Torvalds 
12623b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12635d0bbeebSThomas Graf 
12643d0f24a7SStephen Hemminger 	return more;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
12671e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12681e493d19SDavid S. Miller 			    void *arg)
12691e493d19SDavid S. Miller {
12701e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12711e493d19SDavid S. Miller 
12721e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12731e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12741e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12751e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12761e493d19SDavid S. Miller 		if (func(rt, arg)) {
12771e493d19SDavid S. Miller 			*pprev = dst->next;
12781e493d19SDavid S. Miller 			dst_free(dst);
12791e493d19SDavid S. Miller 		} else {
12801e493d19SDavid S. Miller 			pprev = &dst->next;
12811e493d19SDavid S. Miller 		}
12821e493d19SDavid S. Miller 	}
12831e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12841e493d19SDavid S. Miller }
12851e493d19SDavid S. Miller 
1286569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	unsigned long now = jiffies;
128986393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12907019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12917019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12927019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12937019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12947019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1295fc66f95cSEric Dumazet 	int entries;
12961da177e4SLinus Torvalds 
1297fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12987019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1299fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13001da177e4SLinus Torvalds 		goto out;
13011da177e4SLinus Torvalds 
13026891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13036891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13046891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1305fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1306fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13077019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13081da177e4SLinus Torvalds out:
13097019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1310fc66f95cSEric Dumazet 	return entries > rt_max_size;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13136b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13141da177e4SLinus Torvalds {
13155170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1316a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13176b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1318c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1319c68f24ccSEric Dumazet 
1320c68f24ccSEric Dumazet 		rcu_read_lock();
1321c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1322c68f24ccSEric Dumazet 		if (idev)
13231da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1324c68f24ccSEric Dumazet 		else
132553b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1326c68f24ccSEric Dumazet 		rcu_read_unlock();
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 	return hoplimit;
13291da177e4SLinus Torvalds }
1330abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  *
13341da177e4SLinus Torvalds  */
13351da177e4SLinus Torvalds 
133686872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	int err;
13395578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13401da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13411da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13421da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1343c71099acSThomas Graf 	struct fib6_table *table;
13441da177e4SLinus Torvalds 	int addr_type;
13451da177e4SLinus Torvalds 
134686872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13471da177e4SLinus Torvalds 		return -EINVAL;
13481da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
134986872cb5SThomas Graf 	if (cfg->fc_src_len)
13501da177e4SLinus Torvalds 		return -EINVAL;
13511da177e4SLinus Torvalds #endif
135286872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13531da177e4SLinus Torvalds 		err = -ENODEV;
13545578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13551da177e4SLinus Torvalds 		if (!dev)
13561da177e4SLinus Torvalds 			goto out;
13571da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13581da177e4SLinus Torvalds 		if (!idev)
13591da177e4SLinus Torvalds 			goto out;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
136286872cb5SThomas Graf 	if (cfg->fc_metric == 0)
136386872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13641da177e4SLinus Torvalds 
1365c71099acSThomas Graf 	err = -ENOBUFS;
136638308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1367d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1368d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
136938308473SDavid S. Miller 		if (!table) {
1370f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1371d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1372d71314b4SMatti Vaittinen 		}
1373d71314b4SMatti Vaittinen 	} else {
1374d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1375d71314b4SMatti Vaittinen 	}
137638308473SDavid S. Miller 
137738308473SDavid S. Miller 	if (!table)
1378c71099acSThomas Graf 		goto out;
1379c71099acSThomas Graf 
13808b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13811da177e4SLinus Torvalds 
138238308473SDavid S. Miller 	if (!rt) {
13831da177e4SLinus Torvalds 		err = -ENOMEM;
13841da177e4SLinus Torvalds 		goto out;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
13871716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13881716a961SGao feng 		rt6_set_expires(rt, jiffies +
13891716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13901716a961SGao feng 	else
13911716a961SGao feng 		rt6_clean_expires(rt);
13921da177e4SLinus Torvalds 
139386872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
139486872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
139586872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
139686872cb5SThomas Graf 
139786872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1400d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1401ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1402ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14031da177e4SLinus Torvalds 	else
1404d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14051da177e4SLinus Torvalds 
1406d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14071da177e4SLinus Torvalds 
140886872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
140986872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14101da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
141111d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14121da177e4SLinus Torvalds 
14138e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14148e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14158e2ec639SYan, Zheng 		if (!metrics) {
14168e2ec639SYan, Zheng 			err = -ENOMEM;
14178e2ec639SYan, Zheng 			goto out;
14188e2ec639SYan, Zheng 		}
14198e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14208e2ec639SYan, Zheng 	}
14211da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
142286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
142386872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14241da177e4SLinus Torvalds #endif
14251da177e4SLinus Torvalds 
142686872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14291da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14301da177e4SLinus Torvalds 	 */
143186872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
143238308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
143338308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
143438308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14351da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14365578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14371da177e4SLinus Torvalds 			if (dev) {
14381da177e4SLinus Torvalds 				dev_put(dev);
14391da177e4SLinus Torvalds 				in6_dev_put(idev);
14401da177e4SLinus Torvalds 			}
14415578689aSDaniel Lezcano 			dev = net->loopback_dev;
14421da177e4SLinus Torvalds 			dev_hold(dev);
14431da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14441da177e4SLinus Torvalds 			if (!idev) {
14451da177e4SLinus Torvalds 				err = -ENODEV;
14461da177e4SLinus Torvalds 				goto out;
14471da177e4SLinus Torvalds 			}
14481da177e4SLinus Torvalds 		}
1449d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1450d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
14511da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1452ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1453ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1454ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1455ef2c7d7bSNicolas Dichtel 			break;
1456ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1457ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1458ef2c7d7bSNicolas Dichtel 			break;
1459b4949ab2SNicolas Dichtel 		case RTN_THROW:
1460b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1461b4949ab2SNicolas Dichtel 			break;
1462ef2c7d7bSNicolas Dichtel 		default:
1463ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1464ef2c7d7bSNicolas Dichtel 			break;
1465ef2c7d7bSNicolas Dichtel 		}
14661da177e4SLinus Torvalds 		goto install_route;
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 
146986872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1470b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14711da177e4SLinus Torvalds 		int gwa_type;
14721da177e4SLinus Torvalds 
147386872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14744e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14751da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14781da177e4SLinus Torvalds 			struct rt6_info *grt;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14811da177e4SLinus Torvalds 			   addresses as nexthop address.
14821da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14831da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14841da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14851da177e4SLinus Torvalds 			   some exceptions. --ANK
14861da177e4SLinus Torvalds 			 */
14871da177e4SLinus Torvalds 			err = -EINVAL;
14881da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14891da177e4SLinus Torvalds 				goto out;
14901da177e4SLinus Torvalds 
14915578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
149438308473SDavid S. Miller 			if (!grt)
14951da177e4SLinus Torvalds 				goto out;
14961da177e4SLinus Torvalds 			if (dev) {
1497d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
149894e187c0SAmerigo Wang 					ip6_rt_put(grt);
14991da177e4SLinus Torvalds 					goto out;
15001da177e4SLinus Torvalds 				}
15011da177e4SLinus Torvalds 			} else {
1502d1918542SDavid S. Miller 				dev = grt->dst.dev;
15031da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15041da177e4SLinus Torvalds 				dev_hold(dev);
15051da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15061da177e4SLinus Torvalds 			}
15071da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15081da177e4SLinus Torvalds 				err = 0;
150994e187c0SAmerigo Wang 			ip6_rt_put(grt);
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 			if (err)
15121da177e4SLinus Torvalds 				goto out;
15131da177e4SLinus Torvalds 		}
15141da177e4SLinus Torvalds 		err = -EINVAL;
151538308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15161da177e4SLinus Torvalds 			goto out;
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	err = -ENODEV;
152038308473SDavid S. Miller 	if (!dev)
15211da177e4SLinus Torvalds 		goto out;
15221da177e4SLinus Torvalds 
1523c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1524c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1525c3968a85SDaniel Walter 			err = -EINVAL;
1526c3968a85SDaniel Walter 			goto out;
1527c3968a85SDaniel Walter 		}
15284e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1529c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1530c3968a85SDaniel Walter 	} else
1531c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1532c3968a85SDaniel Walter 
153386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds install_route:
153686872cb5SThomas Graf 	if (cfg->fc_mx) {
153786872cb5SThomas Graf 		struct nlattr *nla;
153886872cb5SThomas Graf 		int remaining;
15391da177e4SLinus Torvalds 
154086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15418f4c1f9bSThomas Graf 			int type = nla_type(nla);
154286872cb5SThomas Graf 
154386872cb5SThomas Graf 			if (type) {
154486872cb5SThomas Graf 				if (type > RTAX_MAX) {
15451da177e4SLinus Torvalds 					err = -EINVAL;
15461da177e4SLinus Torvalds 					goto out;
15471da177e4SLinus Torvalds 				}
154886872cb5SThomas Graf 
1549defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15501da177e4SLinus Torvalds 			}
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
1554d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15551da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1556c71099acSThomas Graf 	rt->rt6i_table = table;
155763152fc0SDaniel Lezcano 
1558c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
155963152fc0SDaniel Lezcano 
156086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds out:
15631da177e4SLinus Torvalds 	if (dev)
15641da177e4SLinus Torvalds 		dev_put(dev);
15651da177e4SLinus Torvalds 	if (idev)
15661da177e4SLinus Torvalds 		in6_dev_put(idev);
15671da177e4SLinus Torvalds 	if (rt)
1568d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15691da177e4SLinus Torvalds 	return err;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
157286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds 	int err;
1575c71099acSThomas Graf 	struct fib6_table *table;
1576d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15771da177e4SLinus Torvalds 
15786825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
15796825a26cSGao feng 		err = -ENOENT;
15806825a26cSGao feng 		goto out;
15816825a26cSGao feng 	}
15826c813a72SPatrick McHardy 
1583c71099acSThomas Graf 	table = rt->rt6i_table;
1584c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
158586872cb5SThomas Graf 	err = fib6_del(rt, info);
1586c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15871da177e4SLinus Torvalds 
15886825a26cSGao feng out:
158994e187c0SAmerigo Wang 	ip6_rt_put(rt);
15901da177e4SLinus Torvalds 	return err;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
1593e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1594e0a1ad73SThomas Graf {
15954d1169c1SDenis V. Lunev 	struct nl_info info = {
1596d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15974d1169c1SDenis V. Lunev 	};
1598528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1599e0a1ad73SThomas Graf }
1600e0a1ad73SThomas Graf 
160186872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16021da177e4SLinus Torvalds {
1603c71099acSThomas Graf 	struct fib6_table *table;
16041da177e4SLinus Torvalds 	struct fib6_node *fn;
16051da177e4SLinus Torvalds 	struct rt6_info *rt;
16061da177e4SLinus Torvalds 	int err = -ESRCH;
16071da177e4SLinus Torvalds 
16085578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
160938308473SDavid S. Miller 	if (!table)
1610c71099acSThomas Graf 		return err;
16111da177e4SLinus Torvalds 
1612c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1613c71099acSThomas Graf 
1614c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
161586872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
161686872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	if (fn) {
1619d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
162086872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1621d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1622d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16231da177e4SLinus Torvalds 				continue;
162486872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
162586872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16261da177e4SLinus Torvalds 				continue;
162786872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16281da177e4SLinus Torvalds 				continue;
1629d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1630c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16311da177e4SLinus Torvalds 
163286872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16331da177e4SLinus Torvalds 		}
16341da177e4SLinus Torvalds 	}
1635c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	return err;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
16406700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1641a6279458SYOSHIFUJI Hideaki {
1642e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1643a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1644e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1645e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1646e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1647e8599ff4SDavid S. Miller 	struct neighbour *neigh;
164871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
16496e157b6aSDavid S. Miller 	int optlen, on_link;
16506e157b6aSDavid S. Miller 	u8 *lladdr;
1651e8599ff4SDavid S. Miller 
1652e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
165371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1654e8599ff4SDavid S. Miller 
1655e8599ff4SDavid S. Miller 	if (optlen < 0) {
16566e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1657e8599ff4SDavid S. Miller 		return;
1658e8599ff4SDavid S. Miller 	}
1659e8599ff4SDavid S. Miller 
166071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1661e8599ff4SDavid S. Miller 
166271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
16636e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1664e8599ff4SDavid S. Miller 		return;
1665e8599ff4SDavid S. Miller 	}
1666e8599ff4SDavid S. Miller 
16676e157b6aSDavid S. Miller 	on_link = 0;
166871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1669e8599ff4SDavid S. Miller 		on_link = 1;
167071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1671e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
16726e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1673e8599ff4SDavid S. Miller 		return;
1674e8599ff4SDavid S. Miller 	}
1675e8599ff4SDavid S. Miller 
1676e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1677e8599ff4SDavid S. Miller 	if (!in6_dev)
1678e8599ff4SDavid S. Miller 		return;
1679e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1680e8599ff4SDavid S. Miller 		return;
1681e8599ff4SDavid S. Miller 
1682e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1683e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1684e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1685e8599ff4SDavid S. Miller 	 */
1686e8599ff4SDavid S. Miller 
168771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1688e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1689e8599ff4SDavid S. Miller 		return;
1690e8599ff4SDavid S. Miller 	}
16916e157b6aSDavid S. Miller 
16926e157b6aSDavid S. Miller 	lladdr = NULL;
1693e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1694e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1695e8599ff4SDavid S. Miller 					     skb->dev);
1696e8599ff4SDavid S. Miller 		if (!lladdr) {
1697e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1698e8599ff4SDavid S. Miller 			return;
1699e8599ff4SDavid S. Miller 		}
1700e8599ff4SDavid S. Miller 	}
1701e8599ff4SDavid S. Miller 
17026e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17036e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17046e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17056e157b6aSDavid S. Miller 		return;
17066e157b6aSDavid S. Miller 	}
17076e157b6aSDavid S. Miller 
17086e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17096e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17106e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17116e157b6aSDavid S. Miller 	 */
17126e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17136e157b6aSDavid S. Miller 
171471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1715e8599ff4SDavid S. Miller 	if (!neigh)
1716e8599ff4SDavid S. Miller 		return;
1717e8599ff4SDavid S. Miller 
17181da177e4SLinus Torvalds 	/*
17191da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17201da177e4SLinus Torvalds 	 */
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17231da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17241da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17251da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17261da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17271da177e4SLinus Torvalds 		     );
17281da177e4SLinus Torvalds 
172971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
173038308473SDavid S. Miller 	if (!nrt)
17311da177e4SLinus Torvalds 		goto out;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17341da177e4SLinus Torvalds 	if (on_link)
17351da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17361da177e4SLinus Torvalds 
17374e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
17381da177e4SLinus Torvalds 
173940e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17401da177e4SLinus Torvalds 		goto out;
17411da177e4SLinus Torvalds 
1742d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1743d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
174471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
174560592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
17468d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17478d71740cSTom Tucker 
17481da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
17496e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1750e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds out:
1754e8599ff4SDavid S. Miller 	neigh_release(neigh);
17556e157b6aSDavid S. Miller }
17566e157b6aSDavid S. Miller 
17571da177e4SLinus Torvalds /*
17581da177e4SLinus Torvalds  *	Misc support functions
17591da177e4SLinus Torvalds  */
17601da177e4SLinus Torvalds 
17611716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
176221efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17631da177e4SLinus Torvalds {
1764d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17658b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17668b96d22dSDavid S. Miller 					    ort->rt6i_table);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	if (rt) {
1769d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1770d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17718e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17721da177e4SLinus Torvalds 
17734e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17748e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1775defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1776d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17771da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17781da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17791da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1780d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17811da177e4SLinus Torvalds 
17824e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17831716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17841716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17851716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
17861716a961SGao feng 			rt6_set_from(rt, ort);
17871da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17901da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17911da177e4SLinus Torvalds #endif
17920f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1793c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17941da177e4SLinus Torvalds 	}
17951da177e4SLinus Torvalds 	return rt;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
179870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1799efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1800b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1801b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
180270ceb4f5SYOSHIFUJI Hideaki {
180370ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180470ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1805c71099acSThomas Graf 	struct fib6_table *table;
180670ceb4f5SYOSHIFUJI Hideaki 
1807efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
180838308473SDavid S. Miller 	if (!table)
1809c71099acSThomas Graf 		return NULL;
1810c71099acSThomas Graf 
18115744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1812c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
181370ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181470ceb4f5SYOSHIFUJI Hideaki 		goto out;
181570ceb4f5SYOSHIFUJI Hideaki 
1816d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1817d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
181870ceb4f5SYOSHIFUJI Hideaki 			continue;
181970ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
182070ceb4f5SYOSHIFUJI Hideaki 			continue;
182170ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
182270ceb4f5SYOSHIFUJI Hideaki 			continue;
1823d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182470ceb4f5SYOSHIFUJI Hideaki 		break;
182570ceb4f5SYOSHIFUJI Hideaki 	}
182670ceb4f5SYOSHIFUJI Hideaki out:
18275744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
182870ceb4f5SYOSHIFUJI Hideaki 	return rt;
182970ceb4f5SYOSHIFUJI Hideaki }
183070ceb4f5SYOSHIFUJI Hideaki 
1831efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1832b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1833b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183495c96174SEric Dumazet 					   unsigned int pref)
183570ceb4f5SYOSHIFUJI Hideaki {
183686872cb5SThomas Graf 	struct fib6_config cfg = {
183786872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1838238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
183986872cb5SThomas Graf 		.fc_ifindex	= ifindex,
184086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
184186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
184286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
184315e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1844efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1845efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
184686872cb5SThomas Graf 	};
184770ceb4f5SYOSHIFUJI Hideaki 
18484e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18494e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
185086872cb5SThomas Graf 
1851e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1852e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
185386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185470ceb4f5SYOSHIFUJI Hideaki 
185586872cb5SThomas Graf 	ip6_route_add(&cfg);
185670ceb4f5SYOSHIFUJI Hideaki 
1857efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
185870ceb4f5SYOSHIFUJI Hideaki }
185970ceb4f5SYOSHIFUJI Hideaki #endif
186070ceb4f5SYOSHIFUJI Hideaki 
1861b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	struct rt6_info *rt;
1864c71099acSThomas Graf 	struct fib6_table *table;
18651da177e4SLinus Torvalds 
1866c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
186738308473SDavid S. Miller 	if (!table)
1868c71099acSThomas Graf 		return NULL;
18691da177e4SLinus Torvalds 
18705744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1871d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1872d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1873045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18741da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18751da177e4SLinus Torvalds 			break;
18761da177e4SLinus Torvalds 	}
18771da177e4SLinus Torvalds 	if (rt)
1878d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
18795744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
18801da177e4SLinus Torvalds 	return rt;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
1883b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1884ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1885ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18861da177e4SLinus Torvalds {
188786872cb5SThomas Graf 	struct fib6_config cfg = {
188886872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1889238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
189086872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
189186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
189286872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
189315e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
18945578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1895c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
189686872cb5SThomas Graf 	};
18971da177e4SLinus Torvalds 
18984e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
18991da177e4SLinus Torvalds 
190086872cb5SThomas Graf 	ip6_route_add(&cfg);
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
19057b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	struct rt6_info *rt;
1908c71099acSThomas Graf 	struct fib6_table *table;
1909c71099acSThomas Graf 
1910c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19117b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
191238308473SDavid S. Miller 	if (!table)
1913c71099acSThomas Graf 		return;
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds restart:
1916c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1917d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19181da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1919d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1920c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1921e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19221da177e4SLinus Torvalds 			goto restart;
19231da177e4SLinus Torvalds 		}
19241da177e4SLinus Torvalds 	}
1925c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19285578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19295578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
193086872cb5SThomas Graf 				 struct fib6_config *cfg)
193186872cb5SThomas Graf {
193286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
193386872cb5SThomas Graf 
193486872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
193586872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
193686872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
193786872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
193886872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
193986872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
194086872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
194186872cb5SThomas Graf 
19425578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1943f1243c2dSBenjamin Thery 
19444e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19454e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19464e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
194786872cb5SThomas Graf }
194886872cb5SThomas Graf 
19495578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19501da177e4SLinus Torvalds {
195186872cb5SThomas Graf 	struct fib6_config cfg;
19521da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19531da177e4SLinus Torvalds 	int err;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	switch(cmd) {
19561da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19571da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
1958af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
19591da177e4SLinus Torvalds 			return -EPERM;
19601da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19611da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19621da177e4SLinus Torvalds 		if (err)
19631da177e4SLinus Torvalds 			return -EFAULT;
19641da177e4SLinus Torvalds 
19655578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
196686872cb5SThomas Graf 
19671da177e4SLinus Torvalds 		rtnl_lock();
19681da177e4SLinus Torvalds 		switch (cmd) {
19691da177e4SLinus Torvalds 		case SIOCADDRT:
197086872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19711da177e4SLinus Torvalds 			break;
19721da177e4SLinus Torvalds 		case SIOCDELRT:
197386872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19741da177e4SLinus Torvalds 			break;
19751da177e4SLinus Torvalds 		default:
19761da177e4SLinus Torvalds 			err = -EINVAL;
19771da177e4SLinus Torvalds 		}
19781da177e4SLinus Torvalds 		rtnl_unlock();
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 		return err;
19813ff50b79SStephen Hemminger 	}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	return -EINVAL;
19841da177e4SLinus Torvalds }
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds /*
19871da177e4SLinus Torvalds  *	Drop the packet on the floor
19881da177e4SLinus Torvalds  */
19891da177e4SLinus Torvalds 
1990d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19911da177e4SLinus Torvalds {
1992612f09e8SYOSHIFUJI Hideaki 	int type;
1993adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1994612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1995612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19960660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
199745bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19983bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19993bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2000612f09e8SYOSHIFUJI Hideaki 			break;
2001612f09e8SYOSHIFUJI Hideaki 		}
2002612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2003612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20043bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20053bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2006612f09e8SYOSHIFUJI Hideaki 		break;
2007612f09e8SYOSHIFUJI Hideaki 	}
20083ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20091da177e4SLinus Torvalds 	kfree_skb(skb);
20101da177e4SLinus Torvalds 	return 0;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20139ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20149ce8ade0SThomas Graf {
2015612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20169ce8ade0SThomas Graf }
20179ce8ade0SThomas Graf 
201820380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20191da177e4SLinus Torvalds {
2020adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2021612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
20246723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20256723ab54SDavid S. Miller 
20269ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20279ce8ade0SThomas Graf {
2028612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20299ce8ade0SThomas Graf }
20309ce8ade0SThomas Graf 
20319ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20329ce8ade0SThomas Graf {
2033adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2034612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20359ce8ade0SThomas Graf }
20369ce8ade0SThomas Graf 
20376723ab54SDavid S. Miller #endif
20386723ab54SDavid S. Miller 
20391da177e4SLinus Torvalds /*
20401da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20411da177e4SLinus Torvalds  */
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20441da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20458f031519SDavid S. Miller 				    bool anycast)
20461da177e4SLinus Torvalds {
2047c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20488b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
20491da177e4SLinus Torvalds 
205038308473SDavid S. Miller 	if (!rt) {
2051f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20521da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
205340385653SBen Greear 	}
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	in6_dev_hold(idev);
20561da177e4SLinus Torvalds 
205711d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2058d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2059d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20601da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
206358c4fb86SYOSHIFUJI Hideaki 	if (anycast)
206458c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
206558c4fb86SYOSHIFUJI Hideaki 	else
20661da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20671da177e4SLinus Torvalds 
20684e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20691da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20705578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20711da177e4SLinus Torvalds 
2072d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	return rt;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
2077c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2078c3968a85SDaniel Walter 			struct rt6_info *rt,
2079b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2080c3968a85SDaniel Walter 			unsigned int prefs,
2081c3968a85SDaniel Walter 			struct in6_addr *saddr)
2082c3968a85SDaniel Walter {
2083c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2084c3968a85SDaniel Walter 	int err = 0;
2085c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
20864e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2087c3968a85SDaniel Walter 	else
2088c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2089c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2090c3968a85SDaniel Walter 	return err;
2091c3968a85SDaniel Walter }
2092c3968a85SDaniel Walter 
2093c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2094c3968a85SDaniel Walter struct arg_dev_net_ip {
2095c3968a85SDaniel Walter 	struct net_device *dev;
2096c3968a85SDaniel Walter 	struct net *net;
2097c3968a85SDaniel Walter 	struct in6_addr *addr;
2098c3968a85SDaniel Walter };
2099c3968a85SDaniel Walter 
2100c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2101c3968a85SDaniel Walter {
2102c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2103c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2104c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2105c3968a85SDaniel Walter 
2106d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2107c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2108c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2109c3968a85SDaniel Walter 		/* remove prefsrc entry */
2110c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2111c3968a85SDaniel Walter 	}
2112c3968a85SDaniel Walter 	return 0;
2113c3968a85SDaniel Walter }
2114c3968a85SDaniel Walter 
2115c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2116c3968a85SDaniel Walter {
2117c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2118c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2119c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2120c3968a85SDaniel Walter 		.net = net,
2121c3968a85SDaniel Walter 		.addr = &ifp->addr,
2122c3968a85SDaniel Walter 	};
2123c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2124c3968a85SDaniel Walter }
2125c3968a85SDaniel Walter 
21268ed67789SDaniel Lezcano struct arg_dev_net {
21278ed67789SDaniel Lezcano 	struct net_device *dev;
21288ed67789SDaniel Lezcano 	struct net *net;
21298ed67789SDaniel Lezcano };
21308ed67789SDaniel Lezcano 
21311da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21321da177e4SLinus Torvalds {
2133bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2134bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21358ed67789SDaniel Lezcano 
2136d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2137c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21381da177e4SLinus Torvalds 		return -1;
2139c159d30cSDavid S. Miller 
21401da177e4SLinus Torvalds 	return 0;
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
2143f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21441da177e4SLinus Torvalds {
21458ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21468ed67789SDaniel Lezcano 		.dev = dev,
21478ed67789SDaniel Lezcano 		.net = net,
21488ed67789SDaniel Lezcano 	};
21498ed67789SDaniel Lezcano 
21508ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21511e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
215495c96174SEric Dumazet struct rt6_mtu_change_arg {
21551da177e4SLinus Torvalds 	struct net_device *dev;
215695c96174SEric Dumazet 	unsigned int mtu;
21571da177e4SLinus Torvalds };
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21601da177e4SLinus Torvalds {
21611da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21621da177e4SLinus Torvalds 	struct inet6_dev *idev;
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21651da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21661da177e4SLinus Torvalds 	   We still use this lock to block changes
21671da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21681da177e4SLinus Torvalds 	*/
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
217138308473SDavid S. Miller 	if (!idev)
21721da177e4SLinus Torvalds 		return 0;
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21751da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21761da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21771da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21781da177e4SLinus Torvalds 	 */
21791da177e4SLinus Torvalds 	/*
21801da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21811da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21821da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21831da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21841da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21851da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21861da177e4SLinus Torvalds 	   PMTU discouvery.
21871da177e4SLinus Torvalds 	 */
2188d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2189d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2190d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2191d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2192d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2193defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2194566cfd8fSSimon Arlott 	}
21951da177e4SLinus Torvalds 	return 0;
21961da177e4SLinus Torvalds }
21971da177e4SLinus Torvalds 
219895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
21991da177e4SLinus Torvalds {
2200c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2201c71099acSThomas Graf 		.dev = dev,
2202c71099acSThomas Graf 		.mtu = mtu,
2203c71099acSThomas Graf 	};
22041da177e4SLinus Torvalds 
2205c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
2208ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22095176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
221086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2211ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
221286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
221386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
221451ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
221586872cb5SThomas Graf };
221686872cb5SThomas Graf 
221786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
221886872cb5SThomas Graf 			      struct fib6_config *cfg)
22191da177e4SLinus Torvalds {
222086872cb5SThomas Graf 	struct rtmsg *rtm;
222186872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
222286872cb5SThomas Graf 	int err;
22231da177e4SLinus Torvalds 
222486872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
222586872cb5SThomas Graf 	if (err < 0)
222686872cb5SThomas Graf 		goto errout;
22271da177e4SLinus Torvalds 
222886872cb5SThomas Graf 	err = -EINVAL;
222986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
223086872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
223186872cb5SThomas Graf 
223286872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
223386872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
223486872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
223586872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
223686872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2237ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
223886872cb5SThomas Graf 
2239ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2240ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2241b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2242b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
224386872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
224486872cb5SThomas Graf 
2245ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2246ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2247ab79ad14SMaciej Żenczykowski 
224815e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
224986872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22503b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
225186872cb5SThomas Graf 
225286872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
225386872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
225486872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22551da177e4SLinus Torvalds 	}
225686872cb5SThomas Graf 
225786872cb5SThomas Graf 	if (tb[RTA_DST]) {
225886872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
225986872cb5SThomas Graf 
226086872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
226186872cb5SThomas Graf 			goto errout;
226286872cb5SThomas Graf 
226386872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22641da177e4SLinus Torvalds 	}
226586872cb5SThomas Graf 
226686872cb5SThomas Graf 	if (tb[RTA_SRC]) {
226786872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
226886872cb5SThomas Graf 
226986872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
227086872cb5SThomas Graf 			goto errout;
227186872cb5SThomas Graf 
227286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22731da177e4SLinus Torvalds 	}
227486872cb5SThomas Graf 
2275c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2276c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2277c3968a85SDaniel Walter 
227886872cb5SThomas Graf 	if (tb[RTA_OIF])
227986872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
228086872cb5SThomas Graf 
228186872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
228286872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
228386872cb5SThomas Graf 
228486872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
228586872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
228686872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22871da177e4SLinus Torvalds 	}
228886872cb5SThomas Graf 
228986872cb5SThomas Graf 	if (tb[RTA_TABLE])
229086872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
229186872cb5SThomas Graf 
229251ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
229351ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
229451ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
229551ebd318SNicolas Dichtel 	}
229651ebd318SNicolas Dichtel 
229786872cb5SThomas Graf 	err = 0;
229886872cb5SThomas Graf errout:
229986872cb5SThomas Graf 	return err;
23001da177e4SLinus Torvalds }
23011da177e4SLinus Torvalds 
230251ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
230351ebd318SNicolas Dichtel {
230451ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
230551ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
230651ebd318SNicolas Dichtel 	int remaining;
230751ebd318SNicolas Dichtel 	int attrlen;
230851ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
230951ebd318SNicolas Dichtel 
231051ebd318SNicolas Dichtel beginning:
231151ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
231251ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
231351ebd318SNicolas Dichtel 
231451ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
231551ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
231651ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
231751ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
231851ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
231951ebd318SNicolas Dichtel 
232051ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
232151ebd318SNicolas Dichtel 		if (attrlen > 0) {
232251ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
232351ebd318SNicolas Dichtel 
232451ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
232551ebd318SNicolas Dichtel 			if (nla) {
232651ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
232751ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
232851ebd318SNicolas Dichtel 			}
232951ebd318SNicolas Dichtel 		}
233051ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
233151ebd318SNicolas Dichtel 		if (err) {
233251ebd318SNicolas Dichtel 			last_err = err;
233351ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
233451ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
233551ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
233651ebd318SNicolas Dichtel 			 */
233751ebd318SNicolas Dichtel 			if (add) {
233851ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
233951ebd318SNicolas Dichtel 				 * next hops that have been already added.
234051ebd318SNicolas Dichtel 				 */
234151ebd318SNicolas Dichtel 				add = 0;
234251ebd318SNicolas Dichtel 				goto beginning;
234351ebd318SNicolas Dichtel 			}
234451ebd318SNicolas Dichtel 		}
23451a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
23461a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
23471a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
23481a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
23491a72418bSNicolas Dichtel 		 */
23501a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
235151ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
235251ebd318SNicolas Dichtel 	}
235351ebd318SNicolas Dichtel 
235451ebd318SNicolas Dichtel 	return last_err;
235551ebd318SNicolas Dichtel }
235651ebd318SNicolas Dichtel 
2357c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23581da177e4SLinus Torvalds {
235986872cb5SThomas Graf 	struct fib6_config cfg;
236086872cb5SThomas Graf 	int err;
23611da177e4SLinus Torvalds 
236286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
236386872cb5SThomas Graf 	if (err < 0)
236486872cb5SThomas Graf 		return err;
236586872cb5SThomas Graf 
236651ebd318SNicolas Dichtel 	if (cfg.fc_mp)
236751ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
236851ebd318SNicolas Dichtel 	else
236986872cb5SThomas Graf 		return ip6_route_del(&cfg);
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds 
2372c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23731da177e4SLinus Torvalds {
237486872cb5SThomas Graf 	struct fib6_config cfg;
237586872cb5SThomas Graf 	int err;
23761da177e4SLinus Torvalds 
237786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
237886872cb5SThomas Graf 	if (err < 0)
237986872cb5SThomas Graf 		return err;
238086872cb5SThomas Graf 
238151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
238251ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
238351ebd318SNicolas Dichtel 	else
238486872cb5SThomas Graf 		return ip6_route_add(&cfg);
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
2387339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2388339bf98fSThomas Graf {
2389339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2390339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2391339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2392339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2393339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2394339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2395339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2396339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2397339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23986a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2399339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2400339bf98fSThomas Graf }
2401339bf98fSThomas Graf 
2402191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2403191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24040d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
240515e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
24067bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24071da177e4SLinus Torvalds {
24081da177e4SLinus Torvalds 	struct rtmsg *rtm;
24091da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2410e3703b3dSThomas Graf 	long expires;
24119e762a4aSPatrick McHardy 	u32 table;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24141da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24151da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24161da177e4SLinus Torvalds 			return 1;
24171da177e4SLinus Torvalds 		}
24181da177e4SLinus Torvalds 	}
24191da177e4SLinus Torvalds 
242015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
242138308473SDavid S. Miller 	if (!nlh)
242226932566SPatrick McHardy 		return -EMSGSIZE;
24232d7202bfSThomas Graf 
24242d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24251da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24261da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24271da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24281da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2429c71099acSThomas Graf 	if (rt->rt6i_table)
24309e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2431c71099acSThomas Graf 	else
24329e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24339e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2434c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2435c78679e8SDavid S. Miller 		goto nla_put_failure;
2436ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2437ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2438ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2439ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2440ef2c7d7bSNicolas Dichtel 			break;
2441ef2c7d7bSNicolas Dichtel 		case -EACCES:
2442ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2443ef2c7d7bSNicolas Dichtel 			break;
2444b4949ab2SNicolas Dichtel 		case -EAGAIN:
2445b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2446b4949ab2SNicolas Dichtel 			break;
2447ef2c7d7bSNicolas Dichtel 		default:
24481da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2449ef2c7d7bSNicolas Dichtel 			break;
2450ef2c7d7bSNicolas Dichtel 		}
2451ef2c7d7bSNicolas Dichtel 	}
2452ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2453ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2454d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24551da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24561da177e4SLinus Torvalds 	else
24571da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24581da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24591da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24601da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24611da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24621da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2463f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2464f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
24651da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2466f0396f60SDenis Ovsienko 		else
2467f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2468f0396f60SDenis Ovsienko 	}
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24711da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	if (dst) {
2474c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2475c78679e8SDavid S. Miller 			goto nla_put_failure;
24761da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24771da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2478c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2479c78679e8SDavid S. Miller 			goto nla_put_failure;
24801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24811da177e4SLinus Torvalds 	if (src) {
2482c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2483c78679e8SDavid S. Miller 			goto nla_put_failure;
24841da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2485c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2486c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2487c78679e8SDavid S. Miller 		goto nla_put_failure;
24881da177e4SLinus Torvalds #endif
24897bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24907bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24917bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24928229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24937bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24947bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24957bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24967bc570c8SYOSHIFUJI Hideaki 						return 0;
24977bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24987bc570c8SYOSHIFUJI Hideaki 				} else {
24997bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25007bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25017bc570c8SYOSHIFUJI Hideaki 				}
25027bc570c8SYOSHIFUJI Hideaki 			}
25037bc570c8SYOSHIFUJI Hideaki 		} else
25047bc570c8SYOSHIFUJI Hideaki #endif
2505c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2506c78679e8SDavid S. Miller 				goto nla_put_failure;
25077bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25081da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2509c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2510c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2511c78679e8SDavid S. Miller 			goto nla_put_failure;
2512c3968a85SDaniel Walter 	}
2513c3968a85SDaniel Walter 
2514c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2515c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25164e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2517c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2518c78679e8SDavid S. Miller 			goto nla_put_failure;
25191da177e4SLinus Torvalds 	}
25202d7202bfSThomas Graf 
2521defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25222d7202bfSThomas Graf 		goto nla_put_failure;
25232d7202bfSThomas Graf 
2524dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2525dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
252694f826b8SEric Dumazet 			goto nla_put_failure;
252794f826b8SEric Dumazet 	}
25282d7202bfSThomas Graf 
2529c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2530c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2531c78679e8SDavid S. Miller 		goto nla_put_failure;
2532c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2533c78679e8SDavid S. Miller 		goto nla_put_failure;
25348253947eSLi Wei 
25358253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
253669cdf8f9SYOSHIFUJI Hideaki 
253787a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2538e3703b3dSThomas Graf 		goto nla_put_failure;
25391da177e4SLinus Torvalds 
25402d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25412d7202bfSThomas Graf 
25422d7202bfSThomas Graf nla_put_failure:
254326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
254426932566SPatrick McHardy 	return -EMSGSIZE;
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
25471b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25481da177e4SLinus Torvalds {
25491da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25501da177e4SLinus Torvalds 	int prefix;
25511da177e4SLinus Torvalds 
25522d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25532d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25541da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25551da177e4SLinus Torvalds 	} else
25561da177e4SLinus Torvalds 		prefix = 0;
25571da177e4SLinus Torvalds 
2558191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2559191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
256015e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
25617bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
2564c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25651da177e4SLinus Torvalds {
25663b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2567ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25681da177e4SLinus Torvalds 	struct rt6_info *rt;
2569ab364a6fSThomas Graf 	struct sk_buff *skb;
2570ab364a6fSThomas Graf 	struct rtmsg *rtm;
25714c9483b2SDavid S. Miller 	struct flowi6 fl6;
257272331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2573ab364a6fSThomas Graf 
2574ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2575ab364a6fSThomas Graf 	if (err < 0)
2576ab364a6fSThomas Graf 		goto errout;
2577ab364a6fSThomas Graf 
2578ab364a6fSThomas Graf 	err = -EINVAL;
25794c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2580ab364a6fSThomas Graf 
2581ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2582ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2583ab364a6fSThomas Graf 			goto errout;
2584ab364a6fSThomas Graf 
25854e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2586ab364a6fSThomas Graf 	}
2587ab364a6fSThomas Graf 
2588ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2589ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2590ab364a6fSThomas Graf 			goto errout;
2591ab364a6fSThomas Graf 
25924e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2593ab364a6fSThomas Graf 	}
2594ab364a6fSThomas Graf 
2595ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2596ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2597ab364a6fSThomas Graf 
2598ab364a6fSThomas Graf 	if (tb[RTA_OIF])
259972331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2600ab364a6fSThomas Graf 
2601ab364a6fSThomas Graf 	if (iif) {
2602ab364a6fSThomas Graf 		struct net_device *dev;
260372331bc0SShmulik Ladkani 		int flags = 0;
260472331bc0SShmulik Ladkani 
26055578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2606ab364a6fSThomas Graf 		if (!dev) {
2607ab364a6fSThomas Graf 			err = -ENODEV;
2608ab364a6fSThomas Graf 			goto errout;
2609ab364a6fSThomas Graf 		}
261072331bc0SShmulik Ladkani 
261172331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
261272331bc0SShmulik Ladkani 
261372331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
261472331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
261572331bc0SShmulik Ladkani 
261672331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
261772331bc0SShmulik Ladkani 							       flags);
261872331bc0SShmulik Ladkani 	} else {
261972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
262072331bc0SShmulik Ladkani 
262172331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2622ab364a6fSThomas Graf 	}
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
262538308473SDavid S. Miller 	if (!skb) {
262694e187c0SAmerigo Wang 		ip6_rt_put(rt);
2627ab364a6fSThomas Graf 		err = -ENOBUFS;
2628ab364a6fSThomas Graf 		goto errout;
2629ab364a6fSThomas Graf 	}
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26321da177e4SLinus Torvalds 	   through good chunk of routing engine.
26331da177e4SLinus Torvalds 	 */
2634459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26351da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26361da177e4SLinus Torvalds 
2637d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26381da177e4SLinus Torvalds 
26394c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
264015e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
26417bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26421da177e4SLinus Torvalds 	if (err < 0) {
2643ab364a6fSThomas Graf 		kfree_skb(skb);
2644ab364a6fSThomas Graf 		goto errout;
26451da177e4SLinus Torvalds 	}
26461da177e4SLinus Torvalds 
264715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2648ab364a6fSThomas Graf errout:
26491da177e4SLinus Torvalds 	return err;
26501da177e4SLinus Torvalds }
26511da177e4SLinus Torvalds 
265286872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26531da177e4SLinus Torvalds {
26541da177e4SLinus Torvalds 	struct sk_buff *skb;
26555578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2656528c4cebSDenis V. Lunev 	u32 seq;
2657528c4cebSDenis V. Lunev 	int err;
26580d51aa80SJamal Hadi Salim 
2659528c4cebSDenis V. Lunev 	err = -ENOBUFS;
266038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
266186872cb5SThomas Graf 
2662339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
266338308473SDavid S. Miller 	if (!skb)
266421713ebcSThomas Graf 		goto errout;
26651da177e4SLinus Torvalds 
2666191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
266715e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
266826932566SPatrick McHardy 	if (err < 0) {
266926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
267026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
267126932566SPatrick McHardy 		kfree_skb(skb);
267226932566SPatrick McHardy 		goto errout;
267326932566SPatrick McHardy 	}
267415e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
26755578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26761ce85fe4SPablo Neira Ayuso 	return;
267721713ebcSThomas Graf errout:
267821713ebcSThomas Graf 	if (err < 0)
26795578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds 
26828ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26838ed67789SDaniel Lezcano 				unsigned long event, void *data)
26848ed67789SDaniel Lezcano {
26858ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2686c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26878ed67789SDaniel Lezcano 
26888ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2689d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26908ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26918ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2692d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26938ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2694d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26958ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26968ed67789SDaniel Lezcano #endif
26978ed67789SDaniel Lezcano 	}
26988ed67789SDaniel Lezcano 
26998ed67789SDaniel Lezcano 	return NOTIFY_OK;
27008ed67789SDaniel Lezcano }
27018ed67789SDaniel Lezcano 
27021da177e4SLinus Torvalds /*
27031da177e4SLinus Torvalds  *	/proc
27041da177e4SLinus Torvalds  */
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds struct rt6_proc_arg
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	char *buffer;
27111da177e4SLinus Torvalds 	int offset;
27121da177e4SLinus Torvalds 	int length;
27131da177e4SLinus Torvalds 	int skip;
27141da177e4SLinus Torvalds 	int len;
27151da177e4SLinus Torvalds };
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27181da177e4SLinus Torvalds {
271933120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
27201da177e4SLinus Torvalds 
27214b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27244b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27251da177e4SLinus Torvalds #else
272633120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27271da177e4SLinus Torvalds #endif
2728dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2729dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		seq_printf(m, "%pi6", &rt->rt6i_gateway);
27301da177e4SLinus Torvalds 	} else {
273133120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27321da177e4SLinus Torvalds 	}
273333120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2734d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2735d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2736d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27371da177e4SLinus Torvalds 	return 0;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
274033120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27411da177e4SLinus Torvalds {
2742f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
274332b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
274433120b30SAlexey Dobriyan 	return 0;
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
274733120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
274833120b30SAlexey Dobriyan {
2749de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2750f3db4851SDaniel Lezcano }
2751f3db4851SDaniel Lezcano 
275233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
275333120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
275433120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
275533120b30SAlexey Dobriyan 	.read		= seq_read,
275633120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2757b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
275833120b30SAlexey Dobriyan };
275933120b30SAlexey Dobriyan 
27601da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27611da177e4SLinus Torvalds {
276269ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27631da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
276469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
276569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
276669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
276769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
276869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2769fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
277069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 	return 0;
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27761da177e4SLinus Torvalds {
2777de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
277869ddb805SDaniel Lezcano }
277969ddb805SDaniel Lezcano 
27809a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27811da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27821da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27831da177e4SLinus Torvalds 	.read	 = seq_read,
27841da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2785b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27861da177e4SLinus Torvalds };
27871da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds static
27928d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27931da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27941da177e4SLinus Torvalds {
2795c486da34SLucian Adrian Grijincu 	struct net *net;
2796c486da34SLucian Adrian Grijincu 	int delay;
2797c486da34SLucian Adrian Grijincu 	if (!write)
2798c486da34SLucian Adrian Grijincu 		return -EINVAL;
2799c486da34SLucian Adrian Grijincu 
2800c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2801c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28028d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28035b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28041da177e4SLinus Torvalds 	return 0;
28051da177e4SLinus Torvalds }
28061da177e4SLinus Torvalds 
2807760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
28081da177e4SLinus Torvalds 	{
28091da177e4SLinus Torvalds 		.procname	=	"flush",
28104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28111da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
281289c8b3a1SDave Jones 		.mode		=	0200,
28136d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28141da177e4SLinus Torvalds 	},
28151da177e4SLinus Torvalds 	{
28161da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28179a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28191da177e4SLinus Torvalds 		.mode		=	0644,
28206d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28211da177e4SLinus Torvalds 	},
28221da177e4SLinus Torvalds 	{
28231da177e4SLinus Torvalds 		.procname	=	"max_size",
28244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28261da177e4SLinus Torvalds 		.mode		=	0644,
28276d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28281da177e4SLinus Torvalds 	},
28291da177e4SLinus Torvalds 	{
28301da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28331da177e4SLinus Torvalds 		.mode		=	0644,
28346d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28351da177e4SLinus Torvalds 	},
28361da177e4SLinus Torvalds 	{
28371da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28401da177e4SLinus Torvalds 		.mode		=	0644,
28416d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28421da177e4SLinus Torvalds 	},
28431da177e4SLinus Torvalds 	{
28441da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28454990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28471da177e4SLinus Torvalds 		.mode		=	0644,
28486d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28491da177e4SLinus Torvalds 	},
28501da177e4SLinus Torvalds 	{
28511da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28524990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28531da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28541da177e4SLinus Torvalds 		.mode		=	0644,
2855f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28561da177e4SLinus Torvalds 	},
28571da177e4SLinus Torvalds 	{
28581da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28594990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28601da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28611da177e4SLinus Torvalds 		.mode		=	0644,
28626d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28631da177e4SLinus Torvalds 	},
28641da177e4SLinus Torvalds 	{
28651da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28664990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28671da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28681da177e4SLinus Torvalds 		.mode		=	0644,
2869f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28701da177e4SLinus Torvalds 	},
28711da177e4SLinus Torvalds 	{
28721da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28734990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28741da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28751da177e4SLinus Torvalds 		.mode		=	0644,
28766d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28771da177e4SLinus Torvalds 	},
2878f8572d8fSEric W. Biederman 	{ }
28791da177e4SLinus Torvalds };
28801da177e4SLinus Torvalds 
28812c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2882760f2d01SDaniel Lezcano {
2883760f2d01SDaniel Lezcano 	struct ctl_table *table;
2884760f2d01SDaniel Lezcano 
2885760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2886760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2887760f2d01SDaniel Lezcano 			GFP_KERNEL);
28885ee09105SYOSHIFUJI Hideaki 
28895ee09105SYOSHIFUJI Hideaki 	if (table) {
28905ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2891c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
289286393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28935ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28945ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28955ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28965ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28975ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28985ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28995ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29009c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2901464dc801SEric W. Biederman 
2902464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2903464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2904464dc801SEric W. Biederman 			table[0].procname = NULL;
29055ee09105SYOSHIFUJI Hideaki 	}
29065ee09105SYOSHIFUJI Hideaki 
2907760f2d01SDaniel Lezcano 	return table;
2908760f2d01SDaniel Lezcano }
29091da177e4SLinus Torvalds #endif
29101da177e4SLinus Torvalds 
29112c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2912cdb18761SDaniel Lezcano {
2913633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29148ed67789SDaniel Lezcano 
291586393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
291686393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2917f2fc6a54SBenjamin Thery 
2918fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2919fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2920fc66f95cSEric Dumazet 
29218ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29228ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29238ed67789SDaniel Lezcano 					   GFP_KERNEL);
29248ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2925fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2926d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29278ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2928d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
292962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
293062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29318ed67789SDaniel Lezcano 
29328ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29338ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29348ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29358ed67789SDaniel Lezcano 					       GFP_KERNEL);
293668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
293768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2938d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29398ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2940d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
294162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
294262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29438ed67789SDaniel Lezcano 
29448ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29458ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29468ed67789SDaniel Lezcano 					       GFP_KERNEL);
294768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
294868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2949d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29508ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2951d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
295262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
295362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29548ed67789SDaniel Lezcano #endif
29558ed67789SDaniel Lezcano 
2956b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2957b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2958b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2959b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2960b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2961b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2962b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2963b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2964b339a47cSPeter Zijlstra 
29656891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29666891a346SBenjamin Thery 
29678ed67789SDaniel Lezcano 	ret = 0;
29688ed67789SDaniel Lezcano out:
29698ed67789SDaniel Lezcano 	return ret;
2970f2fc6a54SBenjamin Thery 
297168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
297268fffc67SPeter Zijlstra out_ip6_prohibit_entry:
297368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
297468fffc67SPeter Zijlstra out_ip6_null_entry:
297568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
297668fffc67SPeter Zijlstra #endif
2977fc66f95cSEric Dumazet out_ip6_dst_entries:
2978fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2979f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2980f2fc6a54SBenjamin Thery 	goto out;
2981cdb18761SDaniel Lezcano }
2982cdb18761SDaniel Lezcano 
29832c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2984cdb18761SDaniel Lezcano {
29858ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29878ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29888ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29898ed67789SDaniel Lezcano #endif
299041bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2991cdb18761SDaniel Lezcano }
2992cdb18761SDaniel Lezcano 
2993d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2994d189634eSThomas Graf {
2995d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2996d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
2997d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
2998d189634eSThomas Graf #endif
2999d189634eSThomas Graf 	return 0;
3000d189634eSThomas Graf }
3001d189634eSThomas Graf 
3002d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3003d189634eSThomas Graf {
3004d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3005ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3006ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3007d189634eSThomas Graf #endif
3008d189634eSThomas Graf }
3009d189634eSThomas Graf 
3010cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3011cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3012cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3013cdb18761SDaniel Lezcano };
3014cdb18761SDaniel Lezcano 
3015c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3016c3426b47SDavid S. Miller {
3017c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3018c3426b47SDavid S. Miller 
3019c3426b47SDavid S. Miller 	if (!bp)
3020c3426b47SDavid S. Miller 		return -ENOMEM;
3021c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3022c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3023c3426b47SDavid S. Miller 	return 0;
3024c3426b47SDavid S. Miller }
3025c3426b47SDavid S. Miller 
3026c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3027c3426b47SDavid S. Miller {
3028c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3029c3426b47SDavid S. Miller 
3030c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
303156a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3032c3426b47SDavid S. Miller 	kfree(bp);
3033c3426b47SDavid S. Miller }
3034c3426b47SDavid S. Miller 
30352b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3036c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3037c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3038c3426b47SDavid S. Miller };
3039c3426b47SDavid S. Miller 
3040d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3041d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3042d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3043d189634eSThomas Graf };
3044d189634eSThomas Graf 
30458ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30468ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30478ed67789SDaniel Lezcano 	.priority = 0,
30488ed67789SDaniel Lezcano };
30498ed67789SDaniel Lezcano 
3050433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30511da177e4SLinus Torvalds {
3052433d49c3SDaniel Lezcano 	int ret;
3053433d49c3SDaniel Lezcano 
30549a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30559a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30569a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30579a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30589a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3059c19a28e1SFernando Carrijo 		goto out;
306014e50e57SDavid S. Miller 
3061fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30628ed67789SDaniel Lezcano 	if (ret)
3063bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3064bdb3289fSDaniel Lezcano 
3065c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3066c3426b47SDavid S. Miller 	if (ret)
3067e8803b6cSDavid S. Miller 		goto out_dst_entries;
30682a0c451aSThomas Graf 
30697e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30707e52b33bSDavid S. Miller 	if (ret)
30717e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3072c3426b47SDavid S. Miller 
30735dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30745dc121e9SArnaud Ebalard 
30758ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30768ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30778ed67789SDaniel Lezcano 	 * manually for init_net */
3078d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30798ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3080bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3081d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30828ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3083d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30848ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3085bdb3289fSDaniel Lezcano   #endif
3086e8803b6cSDavid S. Miller 	ret = fib6_init();
3087433d49c3SDaniel Lezcano 	if (ret)
30888ed67789SDaniel Lezcano 		goto out_register_subsys;
3089433d49c3SDaniel Lezcano 
3090433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3091433d49c3SDaniel Lezcano 	if (ret)
3092e8803b6cSDavid S. Miller 		goto out_fib6_init;
3093c35b7e72SDaniel Lezcano 
3094433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3095433d49c3SDaniel Lezcano 	if (ret)
3096433d49c3SDaniel Lezcano 		goto xfrm6_init;
30977e5449c2SDaniel Lezcano 
3098d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3099d189634eSThomas Graf 	if (ret)
3100d189634eSThomas Graf 		goto fib6_rules_init;
3101d189634eSThomas Graf 
3102433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3103c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3104c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3105c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3106d189634eSThomas Graf 		goto out_register_late_subsys;
3107433d49c3SDaniel Lezcano 
31088ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3109cdb18761SDaniel Lezcano 	if (ret)
3110d189634eSThomas Graf 		goto out_register_late_subsys;
31118ed67789SDaniel Lezcano 
3112433d49c3SDaniel Lezcano out:
3113433d49c3SDaniel Lezcano 	return ret;
3114433d49c3SDaniel Lezcano 
3115d189634eSThomas Graf out_register_late_subsys:
3116d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3117433d49c3SDaniel Lezcano fib6_rules_init:
3118433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3119433d49c3SDaniel Lezcano xfrm6_init:
3120433d49c3SDaniel Lezcano 	xfrm6_fini();
31212a0c451aSThomas Graf out_fib6_init:
31222a0c451aSThomas Graf 	fib6_gc_cleanup();
31238ed67789SDaniel Lezcano out_register_subsys:
31248ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31257e52b33bSDavid S. Miller out_register_inetpeer:
31267e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3127fc66f95cSEric Dumazet out_dst_entries:
3128fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3129433d49c3SDaniel Lezcano out_kmem_cache:
3130f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3131433d49c3SDaniel Lezcano 	goto out;
31321da177e4SLinus Torvalds }
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds void ip6_route_cleanup(void)
31351da177e4SLinus Torvalds {
31368ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3137d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3138101367c2SThomas Graf 	fib6_rules_cleanup();
31391da177e4SLinus Torvalds 	xfrm6_fini();
31401da177e4SLinus Torvalds 	fib6_gc_cleanup();
3141c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31428ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
314341bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3144f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31451da177e4SLinus Torvalds }
3146