xref: /openbmc/linux/net/ipv6/route.c (revision afc154e978de1eb11c555bc8bcec1552f75ebc43)
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 
68*afc154e9SHannes Frederic Sowa enum rt6_nud_state {
69*afc154e9SHannes Frederic Sowa 	RT6_NUD_FAIL_HARD = -2,
70*afc154e9SHannes Frederic Sowa 	RT6_NUD_FAIL_SOFT = -1,
71*afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
72*afc154e9SHannes Frederic Sowa };
73*afc154e9SHannes Frederic Sowa 
741716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
7521efcfa0SEric Dumazet 				    const struct in6_addr *dest);
761da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
770dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
78ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
791da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
801da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
821da177e4SLinus Torvalds 				       struct net_device *dev, int how);
83569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
861da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
871da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
886700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
896700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
906700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
916700c270SDavid S. Miller 					struct sk_buff *skb);
9252bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
931da177e4SLinus Torvalds 
9470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
95efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
96b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
97b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9895c96174SEric Dumazet 					   unsigned int pref);
99efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
100b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
101b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
10270ceb4f5SYOSHIFUJI Hideaki #endif
10370ceb4f5SYOSHIFUJI Hideaki 
10406582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10506582540SDavid S. Miller {
10606582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10706582540SDavid S. Miller 	struct inet_peer *peer;
10806582540SDavid S. Miller 	u32 *p = NULL;
10906582540SDavid S. Miller 
1108e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1118e2ec639SYan, Zheng 		return NULL;
1128e2ec639SYan, Zheng 
113fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
11406582540SDavid S. Miller 	if (peer) {
11506582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11606582540SDavid S. Miller 		unsigned long prev, new;
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		p = peer->metrics;
11906582540SDavid S. Miller 		if (inet_metrics_new(peer))
12006582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
12106582540SDavid S. Miller 
12206582540SDavid S. Miller 		new = (unsigned long) p;
12306582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
12406582540SDavid S. Miller 
12506582540SDavid S. Miller 		if (prev != old) {
12606582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12706582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12806582540SDavid S. Miller 				p = NULL;
12906582540SDavid S. Miller 		}
13006582540SDavid S. Miller 	}
13106582540SDavid S. Miller 	return p;
13206582540SDavid S. Miller }
13306582540SDavid S. Miller 
134f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
135f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
136f894cbf8SDavid S. Miller 					     const void *daddr)
13739232973SDavid S. Miller {
13839232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
13939232973SDavid S. Miller 
140a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
14139232973SDavid S. Miller 		return (const void *) p;
142f894cbf8SDavid S. Miller 	else if (skb)
143f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
14439232973SDavid S. Miller 	return daddr;
14539232973SDavid S. Miller }
14639232973SDavid S. Miller 
147f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
148f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
149f894cbf8SDavid S. Miller 					  const void *daddr)
150d3aaeb38SDavid S. Miller {
15139232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
15239232973SDavid S. Miller 	struct neighbour *n;
15339232973SDavid S. Miller 
154f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1558e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
156f83c7790SDavid S. Miller 	if (n)
157f83c7790SDavid S. Miller 		return n;
158f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
159f83c7790SDavid S. Miller }
160f83c7790SDavid S. Miller 
1619a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1621da177e4SLinus Torvalds 	.family			=	AF_INET6,
16309640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1641da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1651da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1661da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1670dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
168ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16906582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1701da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1711da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1721da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1731da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1741da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1756e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1761ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
177d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1781da177e4SLinus Torvalds };
1791da177e4SLinus Torvalds 
180ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
181ec831ea7SRoland Dreier {
182618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
183618f9bc7SSteffen Klassert 
184618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
185ec831ea7SRoland Dreier }
186ec831ea7SRoland Dreier 
1876700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1886700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
18914e50e57SDavid S. Miller {
19014e50e57SDavid S. Miller }
19114e50e57SDavid S. Miller 
1926700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1936700c270SDavid S. Miller 				      struct sk_buff *skb)
194b587ee3bSDavid S. Miller {
195b587ee3bSDavid S. Miller }
196b587ee3bSDavid S. Miller 
1970972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1980972ddb2SHeld Bernhard 					 unsigned long old)
1990972ddb2SHeld Bernhard {
2000972ddb2SHeld Bernhard 	return NULL;
2010972ddb2SHeld Bernhard }
2020972ddb2SHeld Bernhard 
20314e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
20414e50e57SDavid S. Miller 	.family			=	AF_INET6,
20509640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20614e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20714e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
208ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
209214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
21014e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
211b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2120972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
213d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
21414e50e57SDavid S. Miller };
21514e50e57SDavid S. Miller 
21662fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21714edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
21862fa8a84SDavid S. Miller };
21962fa8a84SDavid S. Miller 
220fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2211da177e4SLinus Torvalds 	.dst = {
2221da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2231da177e4SLinus Torvalds 		.__use		= 1,
2242c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2251da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2261da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2271da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2281da177e4SLinus Torvalds 	},
2291da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2304f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2311da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2321da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2331da177e4SLinus Torvalds };
2341da177e4SLinus Torvalds 
235101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
236101367c2SThomas Graf 
2376723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2386723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2396723ab54SDavid S. Miller 
240fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
241101367c2SThomas Graf 	.dst = {
242101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
243101367c2SThomas Graf 		.__use		= 1,
2442c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
245101367c2SThomas Graf 		.error		= -EACCES,
2469ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2479ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
248101367c2SThomas Graf 	},
249101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2504f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
251101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
252101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
253101367c2SThomas Graf };
254101367c2SThomas Graf 
255fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
256101367c2SThomas Graf 	.dst = {
257101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
258101367c2SThomas Graf 		.__use		= 1,
2592c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
260101367c2SThomas Graf 		.error		= -EINVAL,
261352e512cSHerbert Xu 		.input		= dst_discard,
262352e512cSHerbert Xu 		.output		= dst_discard,
263101367c2SThomas Graf 	},
264101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2654f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
266101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
267101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
268101367c2SThomas Graf };
269101367c2SThomas Graf 
270101367c2SThomas Graf #endif
271101367c2SThomas Graf 
2721da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
27397bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
274957c665fSDavid S. Miller 					     struct net_device *dev,
2758b96d22dSDavid S. Miller 					     int flags,
2768b96d22dSDavid S. Miller 					     struct fib6_table *table)
2771da177e4SLinus Torvalds {
27897bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2796f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
280cf911662SDavid S. Miller 
28197bab73fSDavid S. Miller 	if (rt) {
2828104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2838104891bSSteffen Klassert 
2848104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2858b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
2866f3118b5SNicolas Dichtel 		rt->rt6i_genid = rt_genid(net);
28751ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28851ebd318SNicolas Dichtel 		rt->rt6i_nsiblings = 0;
28997bab73fSDavid S. Miller 	}
290cf911662SDavid S. Miller 	return rt;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2961da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
297ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2981da177e4SLinus Torvalds 
2998e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
3008e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3018e2ec639SYan, Zheng 
30238308473SDavid S. Miller 	if (idev) {
3031da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3041da177e4SLinus Torvalds 		in6_dev_put(idev);
3051da177e4SLinus Torvalds 	}
3061716a961SGao feng 
307ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
308ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3091716a961SGao feng 
31097bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
31197bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
312b3419363SDavid S. Miller 		inet_putpeer(peer);
313b3419363SDavid S. Miller 	}
314b3419363SDavid S. Miller }
315b3419363SDavid S. Miller 
316b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
317b3419363SDavid S. Miller {
31897bab73fSDavid S. Miller 	struct inet_peer_base *base;
319b3419363SDavid S. Miller 	struct inet_peer *peer;
320b3419363SDavid S. Miller 
32197bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
32297bab73fSDavid S. Miller 	if (!base)
32397bab73fSDavid S. Miller 		return;
32497bab73fSDavid S. Miller 
32597bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3267b34ca2aSDavid S. Miller 	if (peer) {
32797bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
328b3419363SDavid S. Miller 			inet_putpeer(peer);
3291da177e4SLinus Torvalds 	}
3307b34ca2aSDavid S. Miller }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3331da177e4SLinus Torvalds 			   int how)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3361da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3375a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
338c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3391da177e4SLinus Torvalds 
34097cac082SDavid S. Miller 	if (dev != loopback_dev) {
34197cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3425a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3435a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
34438308473SDavid S. Miller 			if (loopback_idev) {
3451da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3461da177e4SLinus Torvalds 				in6_dev_put(idev);
3471da177e4SLinus Torvalds 			}
3481da177e4SLinus Torvalds 		}
34997cac082SDavid S. Miller 	}
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3531da177e4SLinus Torvalds {
3541716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3551716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
356a50feda5SEric Dumazet 			return true;
3571716a961SGao feng 	} else if (rt->dst.from) {
3583fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3591716a961SGao feng 	}
360a50feda5SEric Dumazet 	return false;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
364c71099acSThomas Graf {
365a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
366a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
367c71099acSThomas Graf }
368c71099acSThomas Graf 
36951ebd318SNicolas Dichtel /* Multipath route selection:
37051ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
37151ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
37251ebd318SNicolas Dichtel  */
37351ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
37451ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
37551ebd318SNicolas Dichtel {
37651ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
37751ebd318SNicolas Dichtel 
378c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
379c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
38051ebd318SNicolas Dichtel 
38151ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
38251ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
38351ebd318SNicolas Dichtel 	case IPPROTO_UDP:
38451ebd318SNicolas Dichtel 	case IPPROTO_TCP:
38551ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
386b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
387b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38851ebd318SNicolas Dichtel 		break;
38951ebd318SNicolas Dichtel 
39051ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
391b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
392b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
39351ebd318SNicolas Dichtel 		break;
39451ebd318SNicolas Dichtel 	}
39551ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
396b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
39751ebd318SNicolas Dichtel 
39851ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39951ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
40051ebd318SNicolas Dichtel 	return val % candidate_count;
40151ebd318SNicolas Dichtel }
40251ebd318SNicolas Dichtel 
40351ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
40452bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
40552bd4c0cSNicolas Dichtel 					     int strict)
40651ebd318SNicolas Dichtel {
40751ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
40851ebd318SNicolas Dichtel 	int route_choosen;
40951ebd318SNicolas Dichtel 
41051ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
41151ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
41251ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
41351ebd318SNicolas Dichtel 	 */
41451ebd318SNicolas Dichtel 	if (route_choosen)
41551ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
41651ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
41751ebd318SNicolas Dichtel 			route_choosen--;
41851ebd318SNicolas Dichtel 			if (route_choosen == 0) {
41952bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
42052bd4c0cSNicolas Dichtel 					break;
42151ebd318SNicolas Dichtel 				match = sibling;
42251ebd318SNicolas Dichtel 				break;
42351ebd318SNicolas Dichtel 			}
42451ebd318SNicolas Dichtel 		}
42551ebd318SNicolas Dichtel 	return match;
42651ebd318SNicolas Dichtel }
42751ebd318SNicolas Dichtel 
4281da177e4SLinus Torvalds /*
429c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4301da177e4SLinus Torvalds  */
4311da177e4SLinus Torvalds 
4328ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4338ed67789SDaniel Lezcano 						    struct rt6_info *rt,
434b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4351da177e4SLinus Torvalds 						    int oif,
436d420895eSYOSHIFUJI Hideaki 						    int flags)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4391da177e4SLinus Torvalds 	struct rt6_info *sprt;
4401da177e4SLinus Torvalds 
441dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
442dd3abc4eSYOSHIFUJI Hideaki 		goto out;
443dd3abc4eSYOSHIFUJI Hideaki 
444d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
445d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
446dd3abc4eSYOSHIFUJI Hideaki 
447dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4481da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4491da177e4SLinus Torvalds 				return sprt;
4501da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
45138308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4521da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
453d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4541da177e4SLinus Torvalds 						continue;
4551da177e4SLinus Torvalds 					if (local && (!oif ||
4561da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4571da177e4SLinus Torvalds 						continue;
4581da177e4SLinus Torvalds 				}
4591da177e4SLinus Torvalds 				local = sprt;
4601da177e4SLinus Torvalds 			}
461dd3abc4eSYOSHIFUJI Hideaki 		} else {
462dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
463dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
464dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
465dd3abc4eSYOSHIFUJI Hideaki 		}
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 
468dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4691da177e4SLinus Torvalds 		if (local)
4701da177e4SLinus Torvalds 			return local;
4711da177e4SLinus Torvalds 
472d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4738ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4741da177e4SLinus Torvalds 	}
475dd3abc4eSYOSHIFUJI Hideaki out:
4761da177e4SLinus Torvalds 	return rt;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
47927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
48027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
48127097255SYOSHIFUJI Hideaki {
482f2c31e32SEric Dumazet 	struct neighbour *neigh;
48327097255SYOSHIFUJI Hideaki 	/*
48427097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
48527097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
48627097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
48727097255SYOSHIFUJI Hideaki 	 *
48827097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
48927097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
49027097255SYOSHIFUJI Hideaki 	 */
4912152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
492fdd6681dSAmerigo Wang 		return;
4932152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4942152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4952152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4962152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4972152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4982152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4997ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
5002152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5012152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
50252e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
50327097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
50427097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
50527097255SYOSHIFUJI Hideaki 
506b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh) {
50727097255SYOSHIFUJI Hideaki 			neigh->updated = jiffies;
5082152caeaSYOSHIFUJI Hideaki / 吉藤英明 			write_unlock(&neigh->lock);
509b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		}
5102152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5112152caeaSYOSHIFUJI Hideaki / 吉藤英明 		target = (struct in6_addr *)&rt->rt6i_gateway;
51227097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
513d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
514f2c31e32SEric Dumazet 	} else {
5152152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5162152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
51727097255SYOSHIFUJI Hideaki 	}
5182152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
519f2c31e32SEric Dumazet }
52027097255SYOSHIFUJI Hideaki #else
52127097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
52227097255SYOSHIFUJI Hideaki {
52327097255SYOSHIFUJI Hideaki }
52427097255SYOSHIFUJI Hideaki #endif
52527097255SYOSHIFUJI Hideaki 
5261da177e4SLinus Torvalds /*
527554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5281da177e4SLinus Torvalds  */
529b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5301da177e4SLinus Torvalds {
531d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
532161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
533554cfb7eSYOSHIFUJI Hideaki 		return 2;
534161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
535161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
536161980f4SDavid S. Miller 		return 1;
537554cfb7eSYOSHIFUJI Hideaki 	return 0;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
540*afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5411da177e4SLinus Torvalds {
542f2c31e32SEric Dumazet 	struct neighbour *neigh;
543*afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
544f2c31e32SEric Dumazet 
5454d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5464d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
547*afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
548145a3621SYOSHIFUJI Hideaki / 吉藤英明 
549145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
550145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
551145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
552145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
553554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
554*afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
555398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
556a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
557*afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
558398bcbebSYOSHIFUJI Hideaki #endif
559145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
560*afc154e9SHannes Frederic Sowa 	} else {
561*afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
562*afc154e9SHannes Frederic Sowa 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_SOFT;
563a5a81f0bSPaul Marks 	}
564145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
565145a3621SYOSHIFUJI Hideaki / 吉藤英明 
566a5a81f0bSPaul Marks 	return ret;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
569554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
570554cfb7eSYOSHIFUJI Hideaki 			   int strict)
571554cfb7eSYOSHIFUJI Hideaki {
572a5a81f0bSPaul Marks 	int m;
5734d0c5911SYOSHIFUJI Hideaki 
5744d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
57577d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
576*afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
577ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
578ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
579ebacaaa0SYOSHIFUJI Hideaki #endif
580*afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
581*afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
582*afc154e9SHannes Frederic Sowa 		if (n < 0)
583*afc154e9SHannes Frederic Sowa 			return n;
584*afc154e9SHannes Frederic Sowa 	}
585554cfb7eSYOSHIFUJI Hideaki 	return m;
586554cfb7eSYOSHIFUJI Hideaki }
587554cfb7eSYOSHIFUJI Hideaki 
588f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
589*afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
590*afc154e9SHannes Frederic Sowa 				   bool *do_rr)
591554cfb7eSYOSHIFUJI Hideaki {
592554cfb7eSYOSHIFUJI Hideaki 	int m;
593*afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
594554cfb7eSYOSHIFUJI Hideaki 
595554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
596f11e6659SDavid S. Miller 		goto out;
597554cfb7eSYOSHIFUJI Hideaki 
598554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
599*afc154e9SHannes Frederic Sowa 	if (m == RT6_NUD_FAIL_SOFT && !IS_ENABLED(CONFIG_IPV6_ROUTER_PREF)) {
600*afc154e9SHannes Frederic Sowa 		match_do_rr = true;
601*afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
602*afc154e9SHannes Frederic Sowa 	} else if (m < 0) {
603f11e6659SDavid S. Miller 		goto out;
6041da177e4SLinus Torvalds 	}
605f11e6659SDavid S. Miller 
606*afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
607*afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
608*afc154e9SHannes Frederic Sowa 
609*afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
610*afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
611*afc154e9SHannes Frederic Sowa 		*mpri = m;
612*afc154e9SHannes Frederic Sowa 		match = rt;
613*afc154e9SHannes Frederic Sowa 	}
614f11e6659SDavid S. Miller out:
615f11e6659SDavid S. Miller 	return match;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
619f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
620*afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
621*afc154e9SHannes Frederic Sowa 				     bool *do_rr)
622f11e6659SDavid S. Miller {
623f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
624f11e6659SDavid S. Miller 	int mpri = -1;
625f11e6659SDavid S. Miller 
626f11e6659SDavid S. Miller 	match = NULL;
627f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
628d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
629*afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
630f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
631d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
632*afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
633f11e6659SDavid S. Miller 
634f11e6659SDavid S. Miller 	return match;
635f11e6659SDavid S. Miller }
636f11e6659SDavid S. Miller 
637f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
638f11e6659SDavid S. Miller {
639f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6408ed67789SDaniel Lezcano 	struct net *net;
641*afc154e9SHannes Frederic Sowa 	bool do_rr = false;
642f11e6659SDavid S. Miller 
643f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
644f11e6659SDavid S. Miller 	if (!rt0)
645f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
646f11e6659SDavid S. Miller 
647*afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
648*afc154e9SHannes Frederic Sowa 			     &do_rr);
649f11e6659SDavid S. Miller 
650*afc154e9SHannes Frederic Sowa 	if (do_rr) {
651d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
652f11e6659SDavid S. Miller 
653554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
654f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
655f11e6659SDavid S. Miller 			next = fn->leaf;
656f11e6659SDavid S. Miller 
657f11e6659SDavid S. Miller 		if (next != rt0)
658f11e6659SDavid S. Miller 			fn->rr_ptr = next;
659554cfb7eSYOSHIFUJI Hideaki 	}
660554cfb7eSYOSHIFUJI Hideaki 
661d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
662a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
66570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
66670ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
667b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
66870ceb4f5SYOSHIFUJI Hideaki {
669c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
67070ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
67170ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
67270ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6734bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
67470ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
67570ceb4f5SYOSHIFUJI Hideaki 
67670ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
67770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
67870ceb4f5SYOSHIFUJI Hideaki 	}
67970ceb4f5SYOSHIFUJI Hideaki 
68070ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
68170ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
68270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68370ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
68470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
68670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
68770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
68870ceb4f5SYOSHIFUJI Hideaki 		}
68970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
69070ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
69170ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
69270ceb4f5SYOSHIFUJI Hideaki 		}
69370ceb4f5SYOSHIFUJI Hideaki 	}
69470ceb4f5SYOSHIFUJI Hideaki 
69570ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
69670ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6973933fc95SJens Rosenboom 		return -EINVAL;
69870ceb4f5SYOSHIFUJI Hideaki 
6994bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
70070ceb4f5SYOSHIFUJI Hideaki 
70170ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
70270ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
70370ceb4f5SYOSHIFUJI Hideaki 	else {
70470ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
70570ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
70670ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
70770ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
70870ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
70970ceb4f5SYOSHIFUJI Hideaki 	}
71070ceb4f5SYOSHIFUJI Hideaki 
711efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
712efa2cea0SDaniel Lezcano 				dev->ifindex);
71370ceb4f5SYOSHIFUJI Hideaki 
71470ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
715e0a1ad73SThomas Graf 		ip6_del_rt(rt);
71670ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
71770ceb4f5SYOSHIFUJI Hideaki 	}
71870ceb4f5SYOSHIFUJI Hideaki 
71970ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
720efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
72170ceb4f5SYOSHIFUJI Hideaki 					pref);
72270ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
72370ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
72470ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
72570ceb4f5SYOSHIFUJI Hideaki 
72670ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7271716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7281716a961SGao feng 			rt6_clean_expires(rt);
7291716a961SGao feng 		else
7301716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7311716a961SGao feng 
73294e187c0SAmerigo Wang 		ip6_rt_put(rt);
73370ceb4f5SYOSHIFUJI Hideaki 	}
73470ceb4f5SYOSHIFUJI Hideaki 	return 0;
73570ceb4f5SYOSHIFUJI Hideaki }
73670ceb4f5SYOSHIFUJI Hideaki #endif
73770ceb4f5SYOSHIFUJI Hideaki 
7388ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
739982f56f3SYOSHIFUJI Hideaki do { \
7408ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
741982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
742e0eda7bbSVille Nuorvala 		while (1) { \
743982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
744c71099acSThomas Graf 				goto out; \
745982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
746982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7478bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
748982f56f3SYOSHIFUJI Hideaki 			else \
749982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
750c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
751c71099acSThomas Graf 				goto restart; \
752c71099acSThomas Graf 		} \
753982f56f3SYOSHIFUJI Hideaki 	} \
754982f56f3SYOSHIFUJI Hideaki } while (0)
755c71099acSThomas Graf 
7568ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7578ed67789SDaniel Lezcano 					     struct fib6_table *table,
7584c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	struct fib6_node *fn;
7611da177e4SLinus Torvalds 	struct rt6_info *rt;
7621da177e4SLinus Torvalds 
763c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7644c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
765c71099acSThomas Graf restart:
766c71099acSThomas Graf 	rt = fn->leaf;
7674c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
76851ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
76952bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
7704c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
771c71099acSThomas Graf out:
772d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
773c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7741da177e4SLinus Torvalds 	return rt;
775c71099acSThomas Graf 
776c71099acSThomas Graf }
777c71099acSThomas Graf 
778ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
779ea6e574eSFlorian Westphal 				    int flags)
780ea6e574eSFlorian Westphal {
781ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
782ea6e574eSFlorian Westphal }
783ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
784ea6e574eSFlorian Westphal 
7859acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7869acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
787c71099acSThomas Graf {
7884c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7894c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7904c9483b2SDavid S. Miller 		.daddr = *daddr,
791c71099acSThomas Graf 	};
792c71099acSThomas Graf 	struct dst_entry *dst;
79377d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
794c71099acSThomas Graf 
795adaa70bbSThomas Graf 	if (saddr) {
7964c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
797adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
798adaa70bbSThomas Graf 	}
799adaa70bbSThomas Graf 
8004c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
801c71099acSThomas Graf 	if (dst->error == 0)
802c71099acSThomas Graf 		return (struct rt6_info *) dst;
803c71099acSThomas Graf 
804c71099acSThomas Graf 	dst_release(dst);
805c71099acSThomas Graf 
8061da177e4SLinus Torvalds 	return NULL;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8097159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8107159039aSYOSHIFUJI Hideaki 
811c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8121da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8131da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8141da177e4SLinus Torvalds    be destroyed.
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds 
81786872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	int err;
820c71099acSThomas Graf 	struct fib6_table *table;
8211da177e4SLinus Torvalds 
822c71099acSThomas Graf 	table = rt->rt6i_table;
823c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
82486872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
825c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	return err;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
83040e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
83140e22e8fSThomas Graf {
8324d1169c1SDenis V. Lunev 	struct nl_info info = {
833d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8344d1169c1SDenis V. Lunev 	};
835528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
83640e22e8fSThomas Graf }
83740e22e8fSThomas Graf 
8381716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
83921efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
840b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	struct rt6_info *rt;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/*
8451da177e4SLinus Torvalds 	 *	Clone the route.
8461da177e4SLinus Torvalds 	 */
8471da177e4SLinus Torvalds 
84821efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	if (rt) {
85158c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
852bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
85321efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
85458c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8554e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
85658c4fb86SYOSHIFUJI Hideaki 		}
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8611da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8624e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8631da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds #endif
86695a9a5baSYOSHIFUJI Hideaki 	}
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	return rt;
8691da177e4SLinus Torvalds }
87095a9a5baSYOSHIFUJI Hideaki 
87121efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
87221efcfa0SEric Dumazet 					const struct in6_addr *daddr)
873299d9939SYOSHIFUJI Hideaki {
87421efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
87521efcfa0SEric Dumazet 
876887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
877299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
878299d9939SYOSHIFUJI Hideaki 	return rt;
879299d9939SYOSHIFUJI Hideaki }
880299d9939SYOSHIFUJI Hideaki 
8818ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8824c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct fib6_node *fn;
885519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
886c71099acSThomas Graf 	int strict = 0;
8871da177e4SLinus Torvalds 	int attempts = 3;
888519fbd87SYOSHIFUJI Hideaki 	int err;
88953b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8901da177e4SLinus Torvalds 
89177d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds relookup:
894c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8951da177e4SLinus Torvalds 
8968238dd06SYOSHIFUJI Hideaki restart_2:
8974c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds restart:
9004acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
90152bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
90252bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
9034c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9048ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9058238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9061da177e4SLinus Torvalds 		goto out;
9071da177e4SLinus Torvalds 
908d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
909c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9101da177e4SLinus Torvalds 
911c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
9124c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9137343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9144c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9157343ff31SDavid S. Miller 	else
9167343ff31SDavid S. Miller 		goto out2;
9171da177e4SLinus Torvalds 
91894e187c0SAmerigo Wang 	ip6_rt_put(rt);
9198ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9201da177e4SLinus Torvalds 
921d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
922e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
92340e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
924e40cf353SYOSHIFUJI Hideaki 		if (!err)
925e40cf353SYOSHIFUJI Hideaki 			goto out2;
926e40cf353SYOSHIFUJI Hideaki 	}
927e40cf353SYOSHIFUJI Hideaki 
928e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9291da177e4SLinus Torvalds 		goto out2;
9301da177e4SLinus Torvalds 
931519fbd87SYOSHIFUJI Hideaki 	/*
932c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
933519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9341da177e4SLinus Torvalds 	 */
93594e187c0SAmerigo Wang 	ip6_rt_put(rt);
9361da177e4SLinus Torvalds 	goto relookup;
937e40cf353SYOSHIFUJI Hideaki 
938519fbd87SYOSHIFUJI Hideaki out:
9398238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9408238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9418238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9428238dd06SYOSHIFUJI Hideaki 	}
943d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
944c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9451da177e4SLinus Torvalds out2:
946d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
947d8d1f30bSChangli Gao 	rt->dst.__use++;
948c71099acSThomas Graf 
949c71099acSThomas Graf 	return rt;
950c71099acSThomas Graf }
951c71099acSThomas Graf 
9528ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9534c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9544acad72dSPavel Emelyanov {
9554c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9564acad72dSPavel Emelyanov }
9574acad72dSPavel Emelyanov 
95872331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
95972331bc0SShmulik Ladkani 						struct net_device *dev,
96072331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
96172331bc0SShmulik Ladkani {
96272331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
96372331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
96472331bc0SShmulik Ladkani 
96572331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
96672331bc0SShmulik Ladkani }
96772331bc0SShmulik Ladkani 
968c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
969c71099acSThomas Graf {
970b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
971c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
972adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9734c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9744c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9754c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9764c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9776502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9784c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9794c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
980c71099acSThomas Graf 	};
981adaa70bbSThomas Graf 
98272331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
983c71099acSThomas Graf }
984c71099acSThomas Graf 
9858ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9864c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
987c71099acSThomas Graf {
9884c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
989c71099acSThomas Graf }
990c71099acSThomas Graf 
9919c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9924c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
993c71099acSThomas Graf {
994c71099acSThomas Graf 	int flags = 0;
995c71099acSThomas Graf 
9961fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9974dc27d1cSDavid McCullough 
9984c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
99977d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1000c71099acSThomas Graf 
10014c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1002adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10040c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1005adaa70bbSThomas Graf 
10064c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10097159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10101da177e4SLinus Torvalds 
10112774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
101214e50e57SDavid S. Miller {
10135c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
101414e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
101514e50e57SDavid S. Miller 
1016f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
101714e50e57SDavid S. Miller 	if (rt) {
1018d8d1f30bSChangli Gao 		new = &rt->dst;
101914e50e57SDavid S. Miller 
10208104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10218104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10228104891bSSteffen Klassert 
102314e50e57SDavid S. Miller 		new->__use = 1;
1024352e512cSHerbert Xu 		new->input = dst_discard;
1025352e512cSHerbert Xu 		new->output = dst_discard;
102614e50e57SDavid S. Miller 
102721efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
102821efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
102921efcfa0SEric Dumazet 		else
1030defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
103114e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
103214e50e57SDavid S. Miller 		if (rt->rt6i_idev)
103314e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
103414e50e57SDavid S. Miller 
10354e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10361716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
103714e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
103814e50e57SDavid S. Miller 
103914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
104014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
104114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
104214e50e57SDavid S. Miller #endif
104314e50e57SDavid S. Miller 
104414e50e57SDavid S. Miller 		dst_free(new);
104514e50e57SDavid S. Miller 	}
104614e50e57SDavid S. Miller 
104769ead7afSDavid S. Miller 	dst_release(dst_orig);
104869ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
104914e50e57SDavid S. Miller }
105014e50e57SDavid S. Miller 
10511da177e4SLinus Torvalds /*
10521da177e4SLinus Torvalds  *	Destination cache support functions
10531da177e4SLinus Torvalds  */
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct rt6_info *rt;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10601da177e4SLinus Torvalds 
10616f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10626f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10636f3118b5SNicolas Dichtel 	 * into this function always.
10646f3118b5SNicolas Dichtel 	 */
10656f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
10666f3118b5SNicolas Dichtel 		return NULL;
10676f3118b5SNicolas Dichtel 
1068a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10691da177e4SLinus Torvalds 		return dst;
1070a4477c4dSLi RongQing 
10711da177e4SLinus Torvalds 	return NULL;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (rt) {
107954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
108054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1081e0a1ad73SThomas Graf 				ip6_del_rt(rt);
108254c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10831da177e4SLinus Torvalds 			}
108454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
108554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
108654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
108754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
108854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
108954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10931da177e4SLinus Torvalds {
10941da177e4SLinus Torvalds 	struct rt6_info *rt;
10951da177e4SLinus Torvalds 
10963ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10971da177e4SLinus Torvalds 
1098adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10991da177e4SLinus Torvalds 	if (rt) {
11001eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11011eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11021eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11031eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11041eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11051da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11061da177e4SLinus Torvalds 		}
11071da177e4SLinus Torvalds 	}
11081eb4f758SHannes Frederic Sowa }
11091da177e4SLinus Torvalds 
11106700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11116700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11141da177e4SLinus Torvalds 
111581aded24SDavid S. Miller 	dst_confirm(dst);
11161da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
111781aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
111881aded24SDavid S. Miller 
11191da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11201da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1121defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11221da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1123defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1124defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11251da177e4SLinus Torvalds 		}
1126defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
112781aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
113142ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
113242ae66c8SDavid S. Miller 		     int oif, u32 mark)
113381aded24SDavid S. Miller {
113481aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
113581aded24SDavid S. Miller 	struct dst_entry *dst;
113681aded24SDavid S. Miller 	struct flowi6 fl6;
113781aded24SDavid S. Miller 
113881aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
113981aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
114081aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11413e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
114281aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
114381aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11446502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
114581aded24SDavid S. Miller 
114681aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
114781aded24SDavid S. Miller 	if (!dst->error)
11486700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
114981aded24SDavid S. Miller 	dst_release(dst);
115081aded24SDavid S. Miller }
115181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
115281aded24SDavid S. Miller 
115381aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
115481aded24SDavid S. Miller {
115581aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
115681aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
115781aded24SDavid S. Miller }
115881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
115981aded24SDavid S. Miller 
11603a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11613a5ad2eeSDavid S. Miller {
11623a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11633a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11643a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11653a5ad2eeSDavid S. Miller 
11663a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11673a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11683a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11693a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11703a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11713a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11726502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
11733a5ad2eeSDavid S. Miller 
11743a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11753a5ad2eeSDavid S. Miller 	if (!dst->error)
11766700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
11773a5ad2eeSDavid S. Miller 	dst_release(dst);
11783a5ad2eeSDavid S. Miller }
11793a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11803a5ad2eeSDavid S. Miller 
11813a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11823a5ad2eeSDavid S. Miller {
11833a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11843a5ad2eeSDavid S. Miller }
11853a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
11863a5ad2eeSDavid S. Miller 
11870dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11881da177e4SLinus Torvalds {
11890dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11900dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11910dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11920dbaee3bSDavid S. Miller 
11931da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11941da177e4SLinus Torvalds 
11955578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11965578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	/*
11991da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
12001da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
12011da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
12021da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
12031da177e4SLinus Torvalds 	 */
12041da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
12051da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
12061da177e4SLinus Torvalds 	return mtu;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
1209ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1210d33e4553SDavid S. Miller {
1211d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1212618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1213618f9bc7SSteffen Klassert 
1214618f9bc7SSteffen Klassert 	if (mtu)
1215618f9bc7SSteffen Klassert 		return mtu;
1216618f9bc7SSteffen Klassert 
1217618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1218d33e4553SDavid S. Miller 
1219d33e4553SDavid S. Miller 	rcu_read_lock();
1220d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1221d33e4553SDavid S. Miller 	if (idev)
1222d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1223d33e4553SDavid S. Miller 	rcu_read_unlock();
1224d33e4553SDavid S. Miller 
1225d33e4553SDavid S. Miller 	return mtu;
1226d33e4553SDavid S. Miller }
1227d33e4553SDavid S. Miller 
12283b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12293b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12305d0bbeebSThomas Graf 
12313b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
123287a11578SDavid S. Miller 				  struct flowi6 *fl6)
12331da177e4SLinus Torvalds {
123487a11578SDavid S. Miller 	struct dst_entry *dst;
12351da177e4SLinus Torvalds 	struct rt6_info *rt;
12361da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1237c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12381da177e4SLinus Torvalds 
123938308473SDavid S. Miller 	if (unlikely(!idev))
1240122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12411da177e4SLinus Torvalds 
12428b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
124338308473SDavid S. Miller 	if (unlikely(!rt)) {
12441da177e4SLinus Torvalds 		in6_dev_put(idev);
124587a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12461da177e4SLinus Torvalds 		goto out;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12498e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12508e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1251d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
125287a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12538e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12548e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
125514edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
12561da177e4SLinus Torvalds 
12573b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1258d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1259d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12603b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12611da177e4SLinus Torvalds 
12625578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12631da177e4SLinus Torvalds 
126487a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
126587a11578SDavid S. Miller 
12661da177e4SLinus Torvalds out:
126787a11578SDavid S. Miller 	return dst;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12703d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12711da177e4SLinus Torvalds {
1272e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12733d0f24a7SStephen Hemminger 	int more = 0;
12741da177e4SLinus Torvalds 
12753b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12763b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12775d0bbeebSThomas Graf 
12781da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12791da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12801da177e4SLinus Torvalds 			*pprev = dst->next;
12811da177e4SLinus Torvalds 			dst_free(dst);
12821da177e4SLinus Torvalds 		} else {
12831da177e4SLinus Torvalds 			pprev = &dst->next;
12843d0f24a7SStephen Hemminger 			++more;
12851da177e4SLinus Torvalds 		}
12861da177e4SLinus Torvalds 	}
12871da177e4SLinus Torvalds 
12883b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12895d0bbeebSThomas Graf 
12903d0f24a7SStephen Hemminger 	return more;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12941e493d19SDavid S. Miller 			    void *arg)
12951e493d19SDavid S. Miller {
12961e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12971e493d19SDavid S. Miller 
12981e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12991e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
13001e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
13011e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
13021e493d19SDavid S. Miller 		if (func(rt, arg)) {
13031e493d19SDavid S. Miller 			*pprev = dst->next;
13041e493d19SDavid S. Miller 			dst_free(dst);
13051e493d19SDavid S. Miller 		} else {
13061e493d19SDavid S. Miller 			pprev = &dst->next;
13071e493d19SDavid S. Miller 		}
13081e493d19SDavid S. Miller 	}
13091e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
13101e493d19SDavid S. Miller }
13111e493d19SDavid S. Miller 
1312569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
13131da177e4SLinus Torvalds {
13141da177e4SLinus Torvalds 	unsigned long now = jiffies;
131586393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
13167019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
13177019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
13187019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
13197019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
13207019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1321fc66f95cSEric Dumazet 	int entries;
13221da177e4SLinus Torvalds 
1323fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
13247019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1325fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13261da177e4SLinus Torvalds 		goto out;
13271da177e4SLinus Torvalds 
13286891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13296891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13306891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1331fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1332fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13337019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13341da177e4SLinus Torvalds out:
13357019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1336fc66f95cSEric Dumazet 	return entries > rt_max_size;
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
13396b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13401da177e4SLinus Torvalds {
13415170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1342a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13436b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1344c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1345c68f24ccSEric Dumazet 
1346c68f24ccSEric Dumazet 		rcu_read_lock();
1347c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1348c68f24ccSEric Dumazet 		if (idev)
13491da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1350c68f24ccSEric Dumazet 		else
135153b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1352c68f24ccSEric Dumazet 		rcu_read_unlock();
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 	return hoplimit;
13551da177e4SLinus Torvalds }
1356abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds /*
13591da177e4SLinus Torvalds  *
13601da177e4SLinus Torvalds  */
13611da177e4SLinus Torvalds 
136286872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13631da177e4SLinus Torvalds {
13641da177e4SLinus Torvalds 	int err;
13655578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13661da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13671da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13681da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1369c71099acSThomas Graf 	struct fib6_table *table;
13701da177e4SLinus Torvalds 	int addr_type;
13711da177e4SLinus Torvalds 
137286872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13731da177e4SLinus Torvalds 		return -EINVAL;
13741da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
137586872cb5SThomas Graf 	if (cfg->fc_src_len)
13761da177e4SLinus Torvalds 		return -EINVAL;
13771da177e4SLinus Torvalds #endif
137886872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13791da177e4SLinus Torvalds 		err = -ENODEV;
13805578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13811da177e4SLinus Torvalds 		if (!dev)
13821da177e4SLinus Torvalds 			goto out;
13831da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13841da177e4SLinus Torvalds 		if (!idev)
13851da177e4SLinus Torvalds 			goto out;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
138886872cb5SThomas Graf 	if (cfg->fc_metric == 0)
138986872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13901da177e4SLinus Torvalds 
1391c71099acSThomas Graf 	err = -ENOBUFS;
139238308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1393d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1394d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
139538308473SDavid S. Miller 		if (!table) {
1396f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1397d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1398d71314b4SMatti Vaittinen 		}
1399d71314b4SMatti Vaittinen 	} else {
1400d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1401d71314b4SMatti Vaittinen 	}
140238308473SDavid S. Miller 
140338308473SDavid S. Miller 	if (!table)
1404c71099acSThomas Graf 		goto out;
1405c71099acSThomas Graf 
14068b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
14071da177e4SLinus Torvalds 
140838308473SDavid S. Miller 	if (!rt) {
14091da177e4SLinus Torvalds 		err = -ENOMEM;
14101da177e4SLinus Torvalds 		goto out;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 
14131716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
14141716a961SGao feng 		rt6_set_expires(rt, jiffies +
14151716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
14161716a961SGao feng 	else
14171716a961SGao feng 		rt6_clean_expires(rt);
14181da177e4SLinus Torvalds 
141986872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
142086872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
142186872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
142286872cb5SThomas Graf 
142386872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1426d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1427ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1428ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14291da177e4SLinus Torvalds 	else
1430d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14311da177e4SLinus Torvalds 
1432d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14331da177e4SLinus Torvalds 
143486872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
143586872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14361da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
143711d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14381da177e4SLinus Torvalds 
14398e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14408e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14418e2ec639SYan, Zheng 		if (!metrics) {
14428e2ec639SYan, Zheng 			err = -ENOMEM;
14438e2ec639SYan, Zheng 			goto out;
14448e2ec639SYan, Zheng 		}
14458e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14468e2ec639SYan, Zheng 	}
14471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
144886872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
144986872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14501da177e4SLinus Torvalds #endif
14511da177e4SLinus Torvalds 
145286872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14551da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14561da177e4SLinus Torvalds 	 */
145786872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
145838308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
145938308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
146038308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14611da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14625578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14631da177e4SLinus Torvalds 			if (dev) {
14641da177e4SLinus Torvalds 				dev_put(dev);
14651da177e4SLinus Torvalds 				in6_dev_put(idev);
14661da177e4SLinus Torvalds 			}
14675578689aSDaniel Lezcano 			dev = net->loopback_dev;
14681da177e4SLinus Torvalds 			dev_hold(dev);
14691da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14701da177e4SLinus Torvalds 			if (!idev) {
14711da177e4SLinus Torvalds 				err = -ENODEV;
14721da177e4SLinus Torvalds 				goto out;
14731da177e4SLinus Torvalds 			}
14741da177e4SLinus Torvalds 		}
1475d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1476d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
14771da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1478ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1479ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1480ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1481ef2c7d7bSNicolas Dichtel 			break;
1482ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1483ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1484ef2c7d7bSNicolas Dichtel 			break;
1485b4949ab2SNicolas Dichtel 		case RTN_THROW:
1486b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1487b4949ab2SNicolas Dichtel 			break;
1488ef2c7d7bSNicolas Dichtel 		default:
1489ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1490ef2c7d7bSNicolas Dichtel 			break;
1491ef2c7d7bSNicolas Dichtel 		}
14921da177e4SLinus Torvalds 		goto install_route;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 
149586872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1496b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14971da177e4SLinus Torvalds 		int gwa_type;
14981da177e4SLinus Torvalds 
149986872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
15004e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
15011da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
15041da177e4SLinus Torvalds 			struct rt6_info *grt;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
15071da177e4SLinus Torvalds 			   addresses as nexthop address.
15081da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
15091da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
15101da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
15111da177e4SLinus Torvalds 			   some exceptions. --ANK
15121da177e4SLinus Torvalds 			 */
15131da177e4SLinus Torvalds 			err = -EINVAL;
15141da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
15151da177e4SLinus Torvalds 				goto out;
15161da177e4SLinus Torvalds 
15175578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
152038308473SDavid S. Miller 			if (!grt)
15211da177e4SLinus Torvalds 				goto out;
15221da177e4SLinus Torvalds 			if (dev) {
1523d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
152494e187c0SAmerigo Wang 					ip6_rt_put(grt);
15251da177e4SLinus Torvalds 					goto out;
15261da177e4SLinus Torvalds 				}
15271da177e4SLinus Torvalds 			} else {
1528d1918542SDavid S. Miller 				dev = grt->dst.dev;
15291da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15301da177e4SLinus Torvalds 				dev_hold(dev);
15311da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15321da177e4SLinus Torvalds 			}
15331da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15341da177e4SLinus Torvalds 				err = 0;
153594e187c0SAmerigo Wang 			ip6_rt_put(grt);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 			if (err)
15381da177e4SLinus Torvalds 				goto out;
15391da177e4SLinus Torvalds 		}
15401da177e4SLinus Torvalds 		err = -EINVAL;
154138308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15421da177e4SLinus Torvalds 			goto out;
15431da177e4SLinus Torvalds 	}
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	err = -ENODEV;
154638308473SDavid S. Miller 	if (!dev)
15471da177e4SLinus Torvalds 		goto out;
15481da177e4SLinus Torvalds 
1549c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1550c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1551c3968a85SDaniel Walter 			err = -EINVAL;
1552c3968a85SDaniel Walter 			goto out;
1553c3968a85SDaniel Walter 		}
15544e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1555c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1556c3968a85SDaniel Walter 	} else
1557c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1558c3968a85SDaniel Walter 
155986872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds install_route:
156286872cb5SThomas Graf 	if (cfg->fc_mx) {
156386872cb5SThomas Graf 		struct nlattr *nla;
156486872cb5SThomas Graf 		int remaining;
15651da177e4SLinus Torvalds 
156686872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15678f4c1f9bSThomas Graf 			int type = nla_type(nla);
156886872cb5SThomas Graf 
156986872cb5SThomas Graf 			if (type) {
157086872cb5SThomas Graf 				if (type > RTAX_MAX) {
15711da177e4SLinus Torvalds 					err = -EINVAL;
15721da177e4SLinus Torvalds 					goto out;
15731da177e4SLinus Torvalds 				}
157486872cb5SThomas Graf 
1575defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15761da177e4SLinus Torvalds 			}
15771da177e4SLinus Torvalds 		}
15781da177e4SLinus Torvalds 	}
15791da177e4SLinus Torvalds 
1580d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15811da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1582c71099acSThomas Graf 	rt->rt6i_table = table;
158363152fc0SDaniel Lezcano 
1584c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
158563152fc0SDaniel Lezcano 
158686872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds out:
15891da177e4SLinus Torvalds 	if (dev)
15901da177e4SLinus Torvalds 		dev_put(dev);
15911da177e4SLinus Torvalds 	if (idev)
15921da177e4SLinus Torvalds 		in6_dev_put(idev);
15931da177e4SLinus Torvalds 	if (rt)
1594d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15951da177e4SLinus Torvalds 	return err;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
159886872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15991da177e4SLinus Torvalds {
16001da177e4SLinus Torvalds 	int err;
1601c71099acSThomas Graf 	struct fib6_table *table;
1602d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
16031da177e4SLinus Torvalds 
16046825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
16056825a26cSGao feng 		err = -ENOENT;
16066825a26cSGao feng 		goto out;
16076825a26cSGao feng 	}
16086c813a72SPatrick McHardy 
1609c71099acSThomas Graf 	table = rt->rt6i_table;
1610c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
161186872cb5SThomas Graf 	err = fib6_del(rt, info);
1612c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16131da177e4SLinus Torvalds 
16146825a26cSGao feng out:
161594e187c0SAmerigo Wang 	ip6_rt_put(rt);
16161da177e4SLinus Torvalds 	return err;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
1619e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1620e0a1ad73SThomas Graf {
16214d1169c1SDenis V. Lunev 	struct nl_info info = {
1622d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
16234d1169c1SDenis V. Lunev 	};
1624528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1625e0a1ad73SThomas Graf }
1626e0a1ad73SThomas Graf 
162786872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16281da177e4SLinus Torvalds {
1629c71099acSThomas Graf 	struct fib6_table *table;
16301da177e4SLinus Torvalds 	struct fib6_node *fn;
16311da177e4SLinus Torvalds 	struct rt6_info *rt;
16321da177e4SLinus Torvalds 	int err = -ESRCH;
16331da177e4SLinus Torvalds 
16345578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
163538308473SDavid S. Miller 	if (!table)
1636c71099acSThomas Graf 		return err;
16371da177e4SLinus Torvalds 
1638c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1639c71099acSThomas Graf 
1640c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
164186872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
164286872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (fn) {
1645d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
164686872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1647d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1648d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16491da177e4SLinus Torvalds 				continue;
165086872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
165186872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16521da177e4SLinus Torvalds 				continue;
165386872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16541da177e4SLinus Torvalds 				continue;
1655d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1656c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16571da177e4SLinus Torvalds 
165886872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16591da177e4SLinus Torvalds 		}
16601da177e4SLinus Torvalds 	}
1661c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	return err;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
16666700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1667a6279458SYOSHIFUJI Hideaki {
1668e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1669a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1670e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1671e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1672e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1673e8599ff4SDavid S. Miller 	struct neighbour *neigh;
167471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
16756e157b6aSDavid S. Miller 	int optlen, on_link;
16766e157b6aSDavid S. Miller 	u8 *lladdr;
1677e8599ff4SDavid S. Miller 
167829a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
167971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1680e8599ff4SDavid S. Miller 
1681e8599ff4SDavid S. Miller 	if (optlen < 0) {
16826e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1683e8599ff4SDavid S. Miller 		return;
1684e8599ff4SDavid S. Miller 	}
1685e8599ff4SDavid S. Miller 
168671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1687e8599ff4SDavid S. Miller 
168871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
16896e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1690e8599ff4SDavid S. Miller 		return;
1691e8599ff4SDavid S. Miller 	}
1692e8599ff4SDavid S. Miller 
16936e157b6aSDavid S. Miller 	on_link = 0;
169471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1695e8599ff4SDavid S. Miller 		on_link = 1;
169671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1697e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
16986e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1699e8599ff4SDavid S. Miller 		return;
1700e8599ff4SDavid S. Miller 	}
1701e8599ff4SDavid S. Miller 
1702e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1703e8599ff4SDavid S. Miller 	if (!in6_dev)
1704e8599ff4SDavid S. Miller 		return;
1705e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1706e8599ff4SDavid S. Miller 		return;
1707e8599ff4SDavid S. Miller 
1708e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1709e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1710e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1711e8599ff4SDavid S. Miller 	 */
1712e8599ff4SDavid S. Miller 
171371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1714e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1715e8599ff4SDavid S. Miller 		return;
1716e8599ff4SDavid S. Miller 	}
17176e157b6aSDavid S. Miller 
17186e157b6aSDavid S. Miller 	lladdr = NULL;
1719e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1720e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1721e8599ff4SDavid S. Miller 					     skb->dev);
1722e8599ff4SDavid S. Miller 		if (!lladdr) {
1723e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1724e8599ff4SDavid S. Miller 			return;
1725e8599ff4SDavid S. Miller 		}
1726e8599ff4SDavid S. Miller 	}
1727e8599ff4SDavid S. Miller 
17286e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17296e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17306e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17316e157b6aSDavid S. Miller 		return;
17326e157b6aSDavid S. Miller 	}
17336e157b6aSDavid S. Miller 
17346e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17356e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17366e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17376e157b6aSDavid S. Miller 	 */
17386e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17396e157b6aSDavid S. Miller 
174071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1741e8599ff4SDavid S. Miller 	if (!neigh)
1742e8599ff4SDavid S. Miller 		return;
1743e8599ff4SDavid S. Miller 
17441da177e4SLinus Torvalds 	/*
17451da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17461da177e4SLinus Torvalds 	 */
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17491da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17501da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17511da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17521da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17531da177e4SLinus Torvalds 		     );
17541da177e4SLinus Torvalds 
175571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
175638308473SDavid S. Miller 	if (!nrt)
17571da177e4SLinus Torvalds 		goto out;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17601da177e4SLinus Torvalds 	if (on_link)
17611da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17621da177e4SLinus Torvalds 
17634e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
17641da177e4SLinus Torvalds 
176540e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17661da177e4SLinus Torvalds 		goto out;
17671da177e4SLinus Torvalds 
1768d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1769d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
177071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
177160592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
17728d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17738d71740cSTom Tucker 
17741da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
17756e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1776e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds out:
1780e8599ff4SDavid S. Miller 	neigh_release(neigh);
17816e157b6aSDavid S. Miller }
17826e157b6aSDavid S. Miller 
17831da177e4SLinus Torvalds /*
17841da177e4SLinus Torvalds  *	Misc support functions
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds 
17871716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
178821efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17891da177e4SLinus Torvalds {
1790d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17918b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17928b96d22dSDavid S. Miller 					    ort->rt6i_table);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	if (rt) {
1795d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1796d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17978e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17981da177e4SLinus Torvalds 
17994e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18008e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1801defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1802d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18031da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18041da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18051da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1806d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18071da177e4SLinus Torvalds 
18084e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18091716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18101716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18111716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18121716a961SGao feng 			rt6_set_from(rt, ort);
18131da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18161da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18171da177e4SLinus Torvalds #endif
18180f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1819c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 	return rt;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
182470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1825efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1826b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1827b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
182870ceb4f5SYOSHIFUJI Hideaki {
182970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
183070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1831c71099acSThomas Graf 	struct fib6_table *table;
183270ceb4f5SYOSHIFUJI Hideaki 
1833efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
183438308473SDavid S. Miller 	if (!table)
1835c71099acSThomas Graf 		return NULL;
1836c71099acSThomas Graf 
18375744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1838c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
183970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
184070ceb4f5SYOSHIFUJI Hideaki 		goto out;
184170ceb4f5SYOSHIFUJI Hideaki 
1842d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1843d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
184470ceb4f5SYOSHIFUJI Hideaki 			continue;
184570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
184670ceb4f5SYOSHIFUJI Hideaki 			continue;
184770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
184870ceb4f5SYOSHIFUJI Hideaki 			continue;
1849d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
185070ceb4f5SYOSHIFUJI Hideaki 		break;
185170ceb4f5SYOSHIFUJI Hideaki 	}
185270ceb4f5SYOSHIFUJI Hideaki out:
18535744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
185470ceb4f5SYOSHIFUJI Hideaki 	return rt;
185570ceb4f5SYOSHIFUJI Hideaki }
185670ceb4f5SYOSHIFUJI Hideaki 
1857efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1858b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1859b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
186095c96174SEric Dumazet 					   unsigned int pref)
186170ceb4f5SYOSHIFUJI Hideaki {
186286872cb5SThomas Graf 	struct fib6_config cfg = {
186386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1864238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
186586872cb5SThomas Graf 		.fc_ifindex	= ifindex,
186686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
186786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
186886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
186915e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1870efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1871efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
187286872cb5SThomas Graf 	};
187370ceb4f5SYOSHIFUJI Hideaki 
18744e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18754e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
187686872cb5SThomas Graf 
1877e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1878e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
187986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
188070ceb4f5SYOSHIFUJI Hideaki 
188186872cb5SThomas Graf 	ip6_route_add(&cfg);
188270ceb4f5SYOSHIFUJI Hideaki 
1883efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
188470ceb4f5SYOSHIFUJI Hideaki }
188570ceb4f5SYOSHIFUJI Hideaki #endif
188670ceb4f5SYOSHIFUJI Hideaki 
1887b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18881da177e4SLinus Torvalds {
18891da177e4SLinus Torvalds 	struct rt6_info *rt;
1890c71099acSThomas Graf 	struct fib6_table *table;
18911da177e4SLinus Torvalds 
1892c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
189338308473SDavid S. Miller 	if (!table)
1894c71099acSThomas Graf 		return NULL;
18951da177e4SLinus Torvalds 
18965744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1897d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1898d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1899045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19001da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19011da177e4SLinus Torvalds 			break;
19021da177e4SLinus Torvalds 	}
19031da177e4SLinus Torvalds 	if (rt)
1904d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
19055744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
19061da177e4SLinus Torvalds 	return rt;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
1909b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1910ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1911ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19121da177e4SLinus Torvalds {
191386872cb5SThomas Graf 	struct fib6_config cfg = {
191486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1915238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
191686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
191786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
191886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
191915e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
19205578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1921c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
192286872cb5SThomas Graf 	};
19231da177e4SLinus Torvalds 
19244e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19251da177e4SLinus Torvalds 
192686872cb5SThomas Graf 	ip6_route_add(&cfg);
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19317b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	struct rt6_info *rt;
1934c71099acSThomas Graf 	struct fib6_table *table;
1935c71099acSThomas Graf 
1936c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19377b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
193838308473SDavid S. Miller 	if (!table)
1939c71099acSThomas Graf 		return;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds restart:
1942c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1943d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19443e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
19453e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
1946d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1947c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1948e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19491da177e4SLinus Torvalds 			goto restart;
19501da177e4SLinus Torvalds 		}
19511da177e4SLinus Torvalds 	}
1952c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19531da177e4SLinus Torvalds }
19541da177e4SLinus Torvalds 
19555578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19565578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
195786872cb5SThomas Graf 				 struct fib6_config *cfg)
195886872cb5SThomas Graf {
195986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
196086872cb5SThomas Graf 
196186872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
196286872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
196386872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
196486872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
196586872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
196686872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
196786872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
196886872cb5SThomas Graf 
19695578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1970f1243c2dSBenjamin Thery 
19714e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19724e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19734e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
197486872cb5SThomas Graf }
197586872cb5SThomas Graf 
19765578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19771da177e4SLinus Torvalds {
197886872cb5SThomas Graf 	struct fib6_config cfg;
19791da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19801da177e4SLinus Torvalds 	int err;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	switch(cmd) {
19831da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19841da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
1985af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
19861da177e4SLinus Torvalds 			return -EPERM;
19871da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19881da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19891da177e4SLinus Torvalds 		if (err)
19901da177e4SLinus Torvalds 			return -EFAULT;
19911da177e4SLinus Torvalds 
19925578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
199386872cb5SThomas Graf 
19941da177e4SLinus Torvalds 		rtnl_lock();
19951da177e4SLinus Torvalds 		switch (cmd) {
19961da177e4SLinus Torvalds 		case SIOCADDRT:
199786872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19981da177e4SLinus Torvalds 			break;
19991da177e4SLinus Torvalds 		case SIOCDELRT:
200086872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20011da177e4SLinus Torvalds 			break;
20021da177e4SLinus Torvalds 		default:
20031da177e4SLinus Torvalds 			err = -EINVAL;
20041da177e4SLinus Torvalds 		}
20051da177e4SLinus Torvalds 		rtnl_unlock();
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 		return err;
20083ff50b79SStephen Hemminger 	}
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	return -EINVAL;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds /*
20141da177e4SLinus Torvalds  *	Drop the packet on the floor
20151da177e4SLinus Torvalds  */
20161da177e4SLinus Torvalds 
2017d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20181da177e4SLinus Torvalds {
2019612f09e8SYOSHIFUJI Hideaki 	int type;
2020adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2021612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2022612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20230660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
202445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20253bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20263bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2027612f09e8SYOSHIFUJI Hideaki 			break;
2028612f09e8SYOSHIFUJI Hideaki 		}
2029612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2030612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20313bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20323bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2033612f09e8SYOSHIFUJI Hideaki 		break;
2034612f09e8SYOSHIFUJI Hideaki 	}
20353ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20361da177e4SLinus Torvalds 	kfree_skb(skb);
20371da177e4SLinus Torvalds 	return 0;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20409ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20419ce8ade0SThomas Graf {
2042612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20439ce8ade0SThomas Graf }
20449ce8ade0SThomas Graf 
204520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20461da177e4SLinus Torvalds {
2047adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2048612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
20516723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20526723ab54SDavid S. Miller 
20539ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20549ce8ade0SThomas Graf {
2055612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20569ce8ade0SThomas Graf }
20579ce8ade0SThomas Graf 
20589ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20599ce8ade0SThomas Graf {
2060adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2061612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20629ce8ade0SThomas Graf }
20639ce8ade0SThomas Graf 
20646723ab54SDavid S. Miller #endif
20656723ab54SDavid S. Miller 
20661da177e4SLinus Torvalds /*
20671da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20681da177e4SLinus Torvalds  */
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20711da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20728f031519SDavid S. Miller 				    bool anycast)
20731da177e4SLinus Torvalds {
2074c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20758b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
20761da177e4SLinus Torvalds 
207738308473SDavid S. Miller 	if (!rt) {
2078f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20791da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
208040385653SBen Greear 	}
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	in6_dev_hold(idev);
20831da177e4SLinus Torvalds 
208411d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2085d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2086d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20871da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
209058c4fb86SYOSHIFUJI Hideaki 	if (anycast)
209158c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
209258c4fb86SYOSHIFUJI Hideaki 	else
20931da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20941da177e4SLinus Torvalds 
20954e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20961da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20975578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20981da177e4SLinus Torvalds 
2099d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	return rt;
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
2104c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2105c3968a85SDaniel Walter 			struct rt6_info *rt,
2106b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2107c3968a85SDaniel Walter 			unsigned int prefs,
2108c3968a85SDaniel Walter 			struct in6_addr *saddr)
2109c3968a85SDaniel Walter {
2110c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2111c3968a85SDaniel Walter 	int err = 0;
2112c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21134e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2114c3968a85SDaniel Walter 	else
2115c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2116c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2117c3968a85SDaniel Walter 	return err;
2118c3968a85SDaniel Walter }
2119c3968a85SDaniel Walter 
2120c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2121c3968a85SDaniel Walter struct arg_dev_net_ip {
2122c3968a85SDaniel Walter 	struct net_device *dev;
2123c3968a85SDaniel Walter 	struct net *net;
2124c3968a85SDaniel Walter 	struct in6_addr *addr;
2125c3968a85SDaniel Walter };
2126c3968a85SDaniel Walter 
2127c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2128c3968a85SDaniel Walter {
2129c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2130c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2131c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2132c3968a85SDaniel Walter 
2133d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2134c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2135c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2136c3968a85SDaniel Walter 		/* remove prefsrc entry */
2137c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2138c3968a85SDaniel Walter 	}
2139c3968a85SDaniel Walter 	return 0;
2140c3968a85SDaniel Walter }
2141c3968a85SDaniel Walter 
2142c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2143c3968a85SDaniel Walter {
2144c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2145c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2146c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2147c3968a85SDaniel Walter 		.net = net,
2148c3968a85SDaniel Walter 		.addr = &ifp->addr,
2149c3968a85SDaniel Walter 	};
2150c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2151c3968a85SDaniel Walter }
2152c3968a85SDaniel Walter 
21538ed67789SDaniel Lezcano struct arg_dev_net {
21548ed67789SDaniel Lezcano 	struct net_device *dev;
21558ed67789SDaniel Lezcano 	struct net *net;
21568ed67789SDaniel Lezcano };
21578ed67789SDaniel Lezcano 
21581da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21591da177e4SLinus Torvalds {
2160bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2161bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21628ed67789SDaniel Lezcano 
2163d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2164c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21651da177e4SLinus Torvalds 		return -1;
2166c159d30cSDavid S. Miller 
21671da177e4SLinus Torvalds 	return 0;
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
2170f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21711da177e4SLinus Torvalds {
21728ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21738ed67789SDaniel Lezcano 		.dev = dev,
21748ed67789SDaniel Lezcano 		.net = net,
21758ed67789SDaniel Lezcano 	};
21768ed67789SDaniel Lezcano 
21778ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21781e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
218195c96174SEric Dumazet struct rt6_mtu_change_arg {
21821da177e4SLinus Torvalds 	struct net_device *dev;
218395c96174SEric Dumazet 	unsigned int mtu;
21841da177e4SLinus Torvalds };
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21871da177e4SLinus Torvalds {
21881da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21891da177e4SLinus Torvalds 	struct inet6_dev *idev;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21921da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21931da177e4SLinus Torvalds 	   We still use this lock to block changes
21941da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21951da177e4SLinus Torvalds 	*/
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
219838308473SDavid S. Miller 	if (!idev)
21991da177e4SLinus Torvalds 		return 0;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22021da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22031da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22041da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22051da177e4SLinus Torvalds 	 */
22061da177e4SLinus Torvalds 	/*
22071da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22081da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22091da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22101da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22111da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22121da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22131da177e4SLinus Torvalds 	   PMTU discouvery.
22141da177e4SLinus Torvalds 	 */
2215d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2216d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2217d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2218d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2219d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2220defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2221566cfd8fSSimon Arlott 	}
22221da177e4SLinus Torvalds 	return 0;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
222595c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22261da177e4SLinus Torvalds {
2227c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2228c71099acSThomas Graf 		.dev = dev,
2229c71099acSThomas Graf 		.mtu = mtu,
2230c71099acSThomas Graf 	};
22311da177e4SLinus Torvalds 
2232c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
2235ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22365176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
223786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2238ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
223986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
224086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
224151ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
224286872cb5SThomas Graf };
224386872cb5SThomas Graf 
224486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
224586872cb5SThomas Graf 			      struct fib6_config *cfg)
22461da177e4SLinus Torvalds {
224786872cb5SThomas Graf 	struct rtmsg *rtm;
224886872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
224986872cb5SThomas Graf 	int err;
22501da177e4SLinus Torvalds 
225186872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
225286872cb5SThomas Graf 	if (err < 0)
225386872cb5SThomas Graf 		goto errout;
22541da177e4SLinus Torvalds 
225586872cb5SThomas Graf 	err = -EINVAL;
225686872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
225786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
225886872cb5SThomas Graf 
225986872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
226086872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
226186872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
226286872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
226386872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2264ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
226586872cb5SThomas Graf 
2266ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2267ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2268b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2269b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
227086872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
227186872cb5SThomas Graf 
2272ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2273ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2274ab79ad14SMaciej Żenczykowski 
227515e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
227686872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22773b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
227886872cb5SThomas Graf 
227986872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
228086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
228186872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22821da177e4SLinus Torvalds 	}
228386872cb5SThomas Graf 
228486872cb5SThomas Graf 	if (tb[RTA_DST]) {
228586872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
228686872cb5SThomas Graf 
228786872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
228886872cb5SThomas Graf 			goto errout;
228986872cb5SThomas Graf 
229086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22911da177e4SLinus Torvalds 	}
229286872cb5SThomas Graf 
229386872cb5SThomas Graf 	if (tb[RTA_SRC]) {
229486872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
229586872cb5SThomas Graf 
229686872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
229786872cb5SThomas Graf 			goto errout;
229886872cb5SThomas Graf 
229986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23001da177e4SLinus Torvalds 	}
230186872cb5SThomas Graf 
2302c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2303c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2304c3968a85SDaniel Walter 
230586872cb5SThomas Graf 	if (tb[RTA_OIF])
230686872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
230786872cb5SThomas Graf 
230886872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
230986872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
231086872cb5SThomas Graf 
231186872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
231286872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
231386872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23141da177e4SLinus Torvalds 	}
231586872cb5SThomas Graf 
231686872cb5SThomas Graf 	if (tb[RTA_TABLE])
231786872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
231886872cb5SThomas Graf 
231951ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
232051ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
232151ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
232251ebd318SNicolas Dichtel 	}
232351ebd318SNicolas Dichtel 
232486872cb5SThomas Graf 	err = 0;
232586872cb5SThomas Graf errout:
232686872cb5SThomas Graf 	return err;
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
232951ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
233051ebd318SNicolas Dichtel {
233151ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
233251ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
233351ebd318SNicolas Dichtel 	int remaining;
233451ebd318SNicolas Dichtel 	int attrlen;
233551ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
233651ebd318SNicolas Dichtel 
233751ebd318SNicolas Dichtel beginning:
233851ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
233951ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
234051ebd318SNicolas Dichtel 
234151ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
234251ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
234351ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
234451ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
234551ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
234651ebd318SNicolas Dichtel 
234751ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
234851ebd318SNicolas Dichtel 		if (attrlen > 0) {
234951ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
235051ebd318SNicolas Dichtel 
235151ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
235251ebd318SNicolas Dichtel 			if (nla) {
235351ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
235451ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
235551ebd318SNicolas Dichtel 			}
235651ebd318SNicolas Dichtel 		}
235751ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
235851ebd318SNicolas Dichtel 		if (err) {
235951ebd318SNicolas Dichtel 			last_err = err;
236051ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
236151ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
236251ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
236351ebd318SNicolas Dichtel 			 */
236451ebd318SNicolas Dichtel 			if (add) {
236551ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
236651ebd318SNicolas Dichtel 				 * next hops that have been already added.
236751ebd318SNicolas Dichtel 				 */
236851ebd318SNicolas Dichtel 				add = 0;
236951ebd318SNicolas Dichtel 				goto beginning;
237051ebd318SNicolas Dichtel 			}
237151ebd318SNicolas Dichtel 		}
23721a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
23731a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
23741a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
23751a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
23761a72418bSNicolas Dichtel 		 */
23771a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
237851ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
237951ebd318SNicolas Dichtel 	}
238051ebd318SNicolas Dichtel 
238151ebd318SNicolas Dichtel 	return last_err;
238251ebd318SNicolas Dichtel }
238351ebd318SNicolas Dichtel 
2384661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
23851da177e4SLinus Torvalds {
238686872cb5SThomas Graf 	struct fib6_config cfg;
238786872cb5SThomas Graf 	int err;
23881da177e4SLinus Torvalds 
238986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
239086872cb5SThomas Graf 	if (err < 0)
239186872cb5SThomas Graf 		return err;
239286872cb5SThomas Graf 
239351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
239451ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
239551ebd318SNicolas Dichtel 	else
239686872cb5SThomas Graf 		return ip6_route_del(&cfg);
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
2399661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24001da177e4SLinus Torvalds {
240186872cb5SThomas Graf 	struct fib6_config cfg;
240286872cb5SThomas Graf 	int err;
24031da177e4SLinus Torvalds 
240486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
240586872cb5SThomas Graf 	if (err < 0)
240686872cb5SThomas Graf 		return err;
240786872cb5SThomas Graf 
240851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
240951ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
241051ebd318SNicolas Dichtel 	else
241186872cb5SThomas Graf 		return ip6_route_add(&cfg);
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
2414339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2415339bf98fSThomas Graf {
2416339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2417339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2418339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2419339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2420339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2421339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2422339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2423339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2424339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24256a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2426339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2427339bf98fSThomas Graf }
2428339bf98fSThomas Graf 
2429191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2430191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24310d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
243215e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
24337bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
24341da177e4SLinus Torvalds {
24351da177e4SLinus Torvalds 	struct rtmsg *rtm;
24361da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2437e3703b3dSThomas Graf 	long expires;
24389e762a4aSPatrick McHardy 	u32 table;
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24411da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24421da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24431da177e4SLinus Torvalds 			return 1;
24441da177e4SLinus Torvalds 		}
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 
244715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
244838308473SDavid S. Miller 	if (!nlh)
244926932566SPatrick McHardy 		return -EMSGSIZE;
24502d7202bfSThomas Graf 
24512d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24521da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24531da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24541da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24551da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2456c71099acSThomas Graf 	if (rt->rt6i_table)
24579e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2458c71099acSThomas Graf 	else
24599e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24609e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2461c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2462c78679e8SDavid S. Miller 		goto nla_put_failure;
2463ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2464ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2465ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2466ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2467ef2c7d7bSNicolas Dichtel 			break;
2468ef2c7d7bSNicolas Dichtel 		case -EACCES:
2469ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2470ef2c7d7bSNicolas Dichtel 			break;
2471b4949ab2SNicolas Dichtel 		case -EAGAIN:
2472b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2473b4949ab2SNicolas Dichtel 			break;
2474ef2c7d7bSNicolas Dichtel 		default:
24751da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2476ef2c7d7bSNicolas Dichtel 			break;
2477ef2c7d7bSNicolas Dichtel 		}
2478ef2c7d7bSNicolas Dichtel 	}
2479ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2480ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2481d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24821da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24831da177e4SLinus Torvalds 	else
24841da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24851da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24861da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24871da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24881da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24891da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2490f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2491f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
24921da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2493f0396f60SDenis Ovsienko 		else
2494f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2495f0396f60SDenis Ovsienko 	}
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24981da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 	if (dst) {
2501c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2502c78679e8SDavid S. Miller 			goto nla_put_failure;
25031da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
25041da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2505c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2506c78679e8SDavid S. Miller 			goto nla_put_failure;
25071da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25081da177e4SLinus Torvalds 	if (src) {
2509c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2510c78679e8SDavid S. Miller 			goto nla_put_failure;
25111da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2512c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2513c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2514c78679e8SDavid S. Miller 		goto nla_put_failure;
25151da177e4SLinus Torvalds #endif
25167bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25177bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
25187bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
25198229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
25207bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
25217bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25227bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25237bc570c8SYOSHIFUJI Hideaki 						return 0;
25247bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25257bc570c8SYOSHIFUJI Hideaki 				} else {
25267bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25277bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25287bc570c8SYOSHIFUJI Hideaki 				}
25297bc570c8SYOSHIFUJI Hideaki 			}
25307bc570c8SYOSHIFUJI Hideaki 		} else
25317bc570c8SYOSHIFUJI Hideaki #endif
2532c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2533c78679e8SDavid S. Miller 				goto nla_put_failure;
25347bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
25351da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2536c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2537c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2538c78679e8SDavid S. Miller 			goto nla_put_failure;
2539c3968a85SDaniel Walter 	}
2540c3968a85SDaniel Walter 
2541c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2542c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
25434e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2544c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2545c78679e8SDavid S. Miller 			goto nla_put_failure;
25461da177e4SLinus Torvalds 	}
25472d7202bfSThomas Graf 
2548defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25492d7202bfSThomas Graf 		goto nla_put_failure;
25502d7202bfSThomas Graf 
2551dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2552dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
255394f826b8SEric Dumazet 			goto nla_put_failure;
255494f826b8SEric Dumazet 	}
25552d7202bfSThomas Graf 
2556c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2557c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2558c78679e8SDavid S. Miller 		goto nla_put_failure;
2559c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2560c78679e8SDavid S. Miller 		goto nla_put_failure;
25618253947eSLi Wei 
25628253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
256369cdf8f9SYOSHIFUJI Hideaki 
256487a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2565e3703b3dSThomas Graf 		goto nla_put_failure;
25661da177e4SLinus Torvalds 
25672d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25682d7202bfSThomas Graf 
25692d7202bfSThomas Graf nla_put_failure:
257026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
257126932566SPatrick McHardy 	return -EMSGSIZE;
25721da177e4SLinus Torvalds }
25731da177e4SLinus Torvalds 
25741b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25751da177e4SLinus Torvalds {
25761da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25771da177e4SLinus Torvalds 	int prefix;
25781da177e4SLinus Torvalds 
25792d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25802d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25811da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25821da177e4SLinus Torvalds 	} else
25831da177e4SLinus Torvalds 		prefix = 0;
25841da177e4SLinus Torvalds 
2585191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2586191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
258715e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
25887bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds 
2591661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
25921da177e4SLinus Torvalds {
25933b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2594ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25951da177e4SLinus Torvalds 	struct rt6_info *rt;
2596ab364a6fSThomas Graf 	struct sk_buff *skb;
2597ab364a6fSThomas Graf 	struct rtmsg *rtm;
25984c9483b2SDavid S. Miller 	struct flowi6 fl6;
259972331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2600ab364a6fSThomas Graf 
2601ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2602ab364a6fSThomas Graf 	if (err < 0)
2603ab364a6fSThomas Graf 		goto errout;
2604ab364a6fSThomas Graf 
2605ab364a6fSThomas Graf 	err = -EINVAL;
26064c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2607ab364a6fSThomas Graf 
2608ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2609ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2610ab364a6fSThomas Graf 			goto errout;
2611ab364a6fSThomas Graf 
26124e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2613ab364a6fSThomas Graf 	}
2614ab364a6fSThomas Graf 
2615ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2616ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2617ab364a6fSThomas Graf 			goto errout;
2618ab364a6fSThomas Graf 
26194e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2620ab364a6fSThomas Graf 	}
2621ab364a6fSThomas Graf 
2622ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2623ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2624ab364a6fSThomas Graf 
2625ab364a6fSThomas Graf 	if (tb[RTA_OIF])
262672331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2627ab364a6fSThomas Graf 
2628ab364a6fSThomas Graf 	if (iif) {
2629ab364a6fSThomas Graf 		struct net_device *dev;
263072331bc0SShmulik Ladkani 		int flags = 0;
263172331bc0SShmulik Ladkani 
26325578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2633ab364a6fSThomas Graf 		if (!dev) {
2634ab364a6fSThomas Graf 			err = -ENODEV;
2635ab364a6fSThomas Graf 			goto errout;
2636ab364a6fSThomas Graf 		}
263772331bc0SShmulik Ladkani 
263872331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
263972331bc0SShmulik Ladkani 
264072331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
264172331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
264272331bc0SShmulik Ladkani 
264372331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
264472331bc0SShmulik Ladkani 							       flags);
264572331bc0SShmulik Ladkani 	} else {
264672331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
264772331bc0SShmulik Ladkani 
264872331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2649ab364a6fSThomas Graf 	}
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
265238308473SDavid S. Miller 	if (!skb) {
265394e187c0SAmerigo Wang 		ip6_rt_put(rt);
2654ab364a6fSThomas Graf 		err = -ENOBUFS;
2655ab364a6fSThomas Graf 		goto errout;
2656ab364a6fSThomas Graf 	}
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26591da177e4SLinus Torvalds 	   through good chunk of routing engine.
26601da177e4SLinus Torvalds 	 */
2661459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26621da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26631da177e4SLinus Torvalds 
2664d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26651da177e4SLinus Torvalds 
26664c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
266715e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
26687bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26691da177e4SLinus Torvalds 	if (err < 0) {
2670ab364a6fSThomas Graf 		kfree_skb(skb);
2671ab364a6fSThomas Graf 		goto errout;
26721da177e4SLinus Torvalds 	}
26731da177e4SLinus Torvalds 
267415e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2675ab364a6fSThomas Graf errout:
26761da177e4SLinus Torvalds 	return err;
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds 
267986872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26801da177e4SLinus Torvalds {
26811da177e4SLinus Torvalds 	struct sk_buff *skb;
26825578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2683528c4cebSDenis V. Lunev 	u32 seq;
2684528c4cebSDenis V. Lunev 	int err;
26850d51aa80SJamal Hadi Salim 
2686528c4cebSDenis V. Lunev 	err = -ENOBUFS;
268738308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
268886872cb5SThomas Graf 
2689339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
269038308473SDavid S. Miller 	if (!skb)
269121713ebcSThomas Graf 		goto errout;
26921da177e4SLinus Torvalds 
2693191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
269415e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
269526932566SPatrick McHardy 	if (err < 0) {
269626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
269726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
269826932566SPatrick McHardy 		kfree_skb(skb);
269926932566SPatrick McHardy 		goto errout;
270026932566SPatrick McHardy 	}
270115e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
27025578689aSDaniel Lezcano 		    info->nlh, gfp_any());
27031ce85fe4SPablo Neira Ayuso 	return;
270421713ebcSThomas Graf errout:
270521713ebcSThomas Graf 	if (err < 0)
27065578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27098ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2710351638e7SJiri Pirko 				unsigned long event, void *ptr)
27118ed67789SDaniel Lezcano {
2712351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2713c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27148ed67789SDaniel Lezcano 
27158ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2716d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27178ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
27188ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2719d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
27208ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2721d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27228ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27238ed67789SDaniel Lezcano #endif
27248ed67789SDaniel Lezcano 	}
27258ed67789SDaniel Lezcano 
27268ed67789SDaniel Lezcano 	return NOTIFY_OK;
27278ed67789SDaniel Lezcano }
27288ed67789SDaniel Lezcano 
27291da177e4SLinus Torvalds /*
27301da177e4SLinus Torvalds  *	/proc
27311da177e4SLinus Torvalds  */
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds struct rt6_proc_arg
27361da177e4SLinus Torvalds {
27371da177e4SLinus Torvalds 	char *buffer;
27381da177e4SLinus Torvalds 	int offset;
27391da177e4SLinus Torvalds 	int length;
27401da177e4SLinus Torvalds 	int skip;
27411da177e4SLinus Torvalds 	int len;
27421da177e4SLinus Torvalds };
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27451da177e4SLinus Torvalds {
274633120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
27471da177e4SLinus Torvalds 
27484b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27514b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27521da177e4SLinus Torvalds #else
275333120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27541da177e4SLinus Torvalds #endif
2755dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2756dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		seq_printf(m, "%pi6", &rt->rt6i_gateway);
27571da177e4SLinus Torvalds 	} else {
275833120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27591da177e4SLinus Torvalds 	}
276033120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2761d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2762d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2763d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27641da177e4SLinus Torvalds 	return 0;
27651da177e4SLinus Torvalds }
27661da177e4SLinus Torvalds 
276733120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27681da177e4SLinus Torvalds {
2769f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
277032b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
277133120b30SAlexey Dobriyan 	return 0;
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
277433120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
277533120b30SAlexey Dobriyan {
2776de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2777f3db4851SDaniel Lezcano }
2778f3db4851SDaniel Lezcano 
277933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
278033120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
278133120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
278233120b30SAlexey Dobriyan 	.read		= seq_read,
278333120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2784b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
278533120b30SAlexey Dobriyan };
278633120b30SAlexey Dobriyan 
27871da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27881da177e4SLinus Torvalds {
278969ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27901da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
279169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
279269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
279369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
279469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
279569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2796fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
279769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	return 0;
28001da177e4SLinus Torvalds }
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28031da177e4SLinus Torvalds {
2804de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
280569ddb805SDaniel Lezcano }
280669ddb805SDaniel Lezcano 
28079a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28081da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28091da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28101da177e4SLinus Torvalds 	.read	 = seq_read,
28111da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2812b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28131da177e4SLinus Torvalds };
28141da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds static
2819fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
28201da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28211da177e4SLinus Torvalds {
2822c486da34SLucian Adrian Grijincu 	struct net *net;
2823c486da34SLucian Adrian Grijincu 	int delay;
2824c486da34SLucian Adrian Grijincu 	if (!write)
2825c486da34SLucian Adrian Grijincu 		return -EINVAL;
2826c486da34SLucian Adrian Grijincu 
2827c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2828c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28298d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28305b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28311da177e4SLinus Torvalds 	return 0;
28321da177e4SLinus Torvalds }
28331da177e4SLinus Torvalds 
2834fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
28351da177e4SLinus Torvalds 	{
28361da177e4SLinus Torvalds 		.procname	=	"flush",
28374990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28381da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
283989c8b3a1SDave Jones 		.mode		=	0200,
28406d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28411da177e4SLinus Torvalds 	},
28421da177e4SLinus Torvalds 	{
28431da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28449a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28451da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28461da177e4SLinus Torvalds 		.mode		=	0644,
28476d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28481da177e4SLinus Torvalds 	},
28491da177e4SLinus Torvalds 	{
28501da177e4SLinus Torvalds 		.procname	=	"max_size",
28514990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28531da177e4SLinus Torvalds 		.mode		=	0644,
28546d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28551da177e4SLinus Torvalds 	},
28561da177e4SLinus Torvalds 	{
28571da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28584990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28591da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28601da177e4SLinus Torvalds 		.mode		=	0644,
28616d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28621da177e4SLinus Torvalds 	},
28631da177e4SLinus Torvalds 	{
28641da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28654990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28671da177e4SLinus Torvalds 		.mode		=	0644,
28686d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28691da177e4SLinus Torvalds 	},
28701da177e4SLinus Torvalds 	{
28711da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28741da177e4SLinus Torvalds 		.mode		=	0644,
28756d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28761da177e4SLinus Torvalds 	},
28771da177e4SLinus Torvalds 	{
28781da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28794990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28801da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28811da177e4SLinus Torvalds 		.mode		=	0644,
2882f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28831da177e4SLinus Torvalds 	},
28841da177e4SLinus Torvalds 	{
28851da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28864990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28871da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28881da177e4SLinus Torvalds 		.mode		=	0644,
28896d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28901da177e4SLinus Torvalds 	},
28911da177e4SLinus Torvalds 	{
28921da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28934990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28941da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28951da177e4SLinus Torvalds 		.mode		=	0644,
2896f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28971da177e4SLinus Torvalds 	},
28981da177e4SLinus Torvalds 	{
28991da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29004990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29011da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29021da177e4SLinus Torvalds 		.mode		=	0644,
29036d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29041da177e4SLinus Torvalds 	},
2905f8572d8fSEric W. Biederman 	{ }
29061da177e4SLinus Torvalds };
29071da177e4SLinus Torvalds 
29082c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2909760f2d01SDaniel Lezcano {
2910760f2d01SDaniel Lezcano 	struct ctl_table *table;
2911760f2d01SDaniel Lezcano 
2912760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2913760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2914760f2d01SDaniel Lezcano 			GFP_KERNEL);
29155ee09105SYOSHIFUJI Hideaki 
29165ee09105SYOSHIFUJI Hideaki 	if (table) {
29175ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2918c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
291986393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29205ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29215ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29225ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29235ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29245ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29255ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29265ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29279c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2928464dc801SEric W. Biederman 
2929464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2930464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2931464dc801SEric W. Biederman 			table[0].procname = NULL;
29325ee09105SYOSHIFUJI Hideaki 	}
29335ee09105SYOSHIFUJI Hideaki 
2934760f2d01SDaniel Lezcano 	return table;
2935760f2d01SDaniel Lezcano }
29361da177e4SLinus Torvalds #endif
29371da177e4SLinus Torvalds 
29382c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2939cdb18761SDaniel Lezcano {
2940633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29418ed67789SDaniel Lezcano 
294286393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
294386393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2944f2fc6a54SBenjamin Thery 
2945fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2946fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2947fc66f95cSEric Dumazet 
29488ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29498ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29508ed67789SDaniel Lezcano 					   GFP_KERNEL);
29518ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2952fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2953d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29548ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2955d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
295662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
295762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29588ed67789SDaniel Lezcano 
29598ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29608ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29618ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29628ed67789SDaniel Lezcano 					       GFP_KERNEL);
296368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
296468fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2965d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29668ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2967d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
296862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
296962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29708ed67789SDaniel Lezcano 
29718ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29728ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29738ed67789SDaniel Lezcano 					       GFP_KERNEL);
297468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
297568fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2976d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29778ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2978d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
297962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
298062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29818ed67789SDaniel Lezcano #endif
29828ed67789SDaniel Lezcano 
2983b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2984b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2985b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2986b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2987b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2988b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2989b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2990b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2991b339a47cSPeter Zijlstra 
29926891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29936891a346SBenjamin Thery 
29948ed67789SDaniel Lezcano 	ret = 0;
29958ed67789SDaniel Lezcano out:
29968ed67789SDaniel Lezcano 	return ret;
2997f2fc6a54SBenjamin Thery 
299868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
299968fffc67SPeter Zijlstra out_ip6_prohibit_entry:
300068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
300168fffc67SPeter Zijlstra out_ip6_null_entry:
300268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
300368fffc67SPeter Zijlstra #endif
3004fc66f95cSEric Dumazet out_ip6_dst_entries:
3005fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3006f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3007f2fc6a54SBenjamin Thery 	goto out;
3008cdb18761SDaniel Lezcano }
3009cdb18761SDaniel Lezcano 
30102c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3011cdb18761SDaniel Lezcano {
30128ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30138ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30148ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30158ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30168ed67789SDaniel Lezcano #endif
301741bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3018cdb18761SDaniel Lezcano }
3019cdb18761SDaniel Lezcano 
3020d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3021d189634eSThomas Graf {
3022d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3023d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3024d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3025d189634eSThomas Graf #endif
3026d189634eSThomas Graf 	return 0;
3027d189634eSThomas Graf }
3028d189634eSThomas Graf 
3029d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3030d189634eSThomas Graf {
3031d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3032ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3033ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3034d189634eSThomas Graf #endif
3035d189634eSThomas Graf }
3036d189634eSThomas Graf 
3037cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3038cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3039cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3040cdb18761SDaniel Lezcano };
3041cdb18761SDaniel Lezcano 
3042c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3043c3426b47SDavid S. Miller {
3044c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3045c3426b47SDavid S. Miller 
3046c3426b47SDavid S. Miller 	if (!bp)
3047c3426b47SDavid S. Miller 		return -ENOMEM;
3048c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3049c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3050c3426b47SDavid S. Miller 	return 0;
3051c3426b47SDavid S. Miller }
3052c3426b47SDavid S. Miller 
3053c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3054c3426b47SDavid S. Miller {
3055c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3056c3426b47SDavid S. Miller 
3057c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
305856a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3059c3426b47SDavid S. Miller 	kfree(bp);
3060c3426b47SDavid S. Miller }
3061c3426b47SDavid S. Miller 
30622b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3063c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3064c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3065c3426b47SDavid S. Miller };
3066c3426b47SDavid S. Miller 
3067d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3068d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3069d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3070d189634eSThomas Graf };
3071d189634eSThomas Graf 
30728ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30738ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30748ed67789SDaniel Lezcano 	.priority = 0,
30758ed67789SDaniel Lezcano };
30768ed67789SDaniel Lezcano 
3077433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30781da177e4SLinus Torvalds {
3079433d49c3SDaniel Lezcano 	int ret;
3080433d49c3SDaniel Lezcano 
30819a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30829a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30839a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30849a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30859a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3086c19a28e1SFernando Carrijo 		goto out;
308714e50e57SDavid S. Miller 
3088fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30898ed67789SDaniel Lezcano 	if (ret)
3090bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3091bdb3289fSDaniel Lezcano 
3092c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3093c3426b47SDavid S. Miller 	if (ret)
3094e8803b6cSDavid S. Miller 		goto out_dst_entries;
30952a0c451aSThomas Graf 
30967e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30977e52b33bSDavid S. Miller 	if (ret)
30987e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3099c3426b47SDavid S. Miller 
31005dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31015dc121e9SArnaud Ebalard 
31028ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31038ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31048ed67789SDaniel Lezcano 	 * manually for init_net */
3105d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31068ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3107bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3108d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31098ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3110d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31118ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3112bdb3289fSDaniel Lezcano   #endif
3113e8803b6cSDavid S. Miller 	ret = fib6_init();
3114433d49c3SDaniel Lezcano 	if (ret)
31158ed67789SDaniel Lezcano 		goto out_register_subsys;
3116433d49c3SDaniel Lezcano 
3117433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3118433d49c3SDaniel Lezcano 	if (ret)
3119e8803b6cSDavid S. Miller 		goto out_fib6_init;
3120c35b7e72SDaniel Lezcano 
3121433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3122433d49c3SDaniel Lezcano 	if (ret)
3123433d49c3SDaniel Lezcano 		goto xfrm6_init;
31247e5449c2SDaniel Lezcano 
3125d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3126d189634eSThomas Graf 	if (ret)
3127d189634eSThomas Graf 		goto fib6_rules_init;
3128d189634eSThomas Graf 
3129433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3130c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3131c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3132c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3133d189634eSThomas Graf 		goto out_register_late_subsys;
3134433d49c3SDaniel Lezcano 
31358ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3136cdb18761SDaniel Lezcano 	if (ret)
3137d189634eSThomas Graf 		goto out_register_late_subsys;
31388ed67789SDaniel Lezcano 
3139433d49c3SDaniel Lezcano out:
3140433d49c3SDaniel Lezcano 	return ret;
3141433d49c3SDaniel Lezcano 
3142d189634eSThomas Graf out_register_late_subsys:
3143d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3144433d49c3SDaniel Lezcano fib6_rules_init:
3145433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3146433d49c3SDaniel Lezcano xfrm6_init:
3147433d49c3SDaniel Lezcano 	xfrm6_fini();
31482a0c451aSThomas Graf out_fib6_init:
31492a0c451aSThomas Graf 	fib6_gc_cleanup();
31508ed67789SDaniel Lezcano out_register_subsys:
31518ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31527e52b33bSDavid S. Miller out_register_inetpeer:
31537e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3154fc66f95cSEric Dumazet out_dst_entries:
3155fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3156433d49c3SDaniel Lezcano out_kmem_cache:
3157f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3158433d49c3SDaniel Lezcano 	goto out;
31591da177e4SLinus Torvalds }
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds void ip6_route_cleanup(void)
31621da177e4SLinus Torvalds {
31638ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3164d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3165101367c2SThomas Graf 	fib6_rules_cleanup();
31661da177e4SLinus Torvalds 	xfrm6_fini();
31671da177e4SLinus Torvalds 	fib6_gc_cleanup();
3168c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31698ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
317041bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3171f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31721da177e4SLinus Torvalds }
3173