xref: /openbmc/linux/net/ipv6/route.c (revision 8d2ca1d7b5c3e63b3a8a81ae99015b615c5f2bf7)
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 
68afc154e9SHannes Frederic Sowa enum rt6_nud_state {
69afc154e9SHannes Frederic Sowa 	RT6_NUD_FAIL_HARD = -2,
70afc154e9SHannes Frederic Sowa 	RT6_NUD_FAIL_SOFT = -1,
71afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
72afc154e9SHannes Frederic Sowa };
73afc154e9SHannes 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);
286ca4c3fc2Sfan.du 		rt->rt6i_genid = rt_genid_ipv6(net);
28751ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
28897bab73fSDavid S. Miller 	}
289cf911662SDavid S. Miller 	return rt;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2951da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
296ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2971da177e4SLinus Torvalds 
2988e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2998e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3008e2ec639SYan, Zheng 
30138308473SDavid S. Miller 	if (idev) {
3021da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3031da177e4SLinus Torvalds 		in6_dev_put(idev);
3041da177e4SLinus Torvalds 	}
3051716a961SGao feng 
306ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
307ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
3081716a961SGao feng 
30997bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
31097bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
311b3419363SDavid S. Miller 		inet_putpeer(peer);
312b3419363SDavid S. Miller 	}
313b3419363SDavid S. Miller }
314b3419363SDavid S. Miller 
315b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
316b3419363SDavid S. Miller {
31797bab73fSDavid S. Miller 	struct inet_peer_base *base;
318b3419363SDavid S. Miller 	struct inet_peer *peer;
319b3419363SDavid S. Miller 
32097bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
32197bab73fSDavid S. Miller 	if (!base)
32297bab73fSDavid S. Miller 		return;
32397bab73fSDavid S. Miller 
32497bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3257b34ca2aSDavid S. Miller 	if (peer) {
32697bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
327b3419363SDavid S. Miller 			inet_putpeer(peer);
3281da177e4SLinus Torvalds 	}
3297b34ca2aSDavid S. Miller }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3321da177e4SLinus Torvalds 			   int how)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3351da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3365a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
337c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3381da177e4SLinus Torvalds 
33997cac082SDavid S. Miller 	if (dev != loopback_dev) {
34097cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3415a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3425a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
34338308473SDavid S. Miller 			if (loopback_idev) {
3441da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3451da177e4SLinus Torvalds 				in6_dev_put(idev);
3461da177e4SLinus Torvalds 			}
3471da177e4SLinus Torvalds 		}
34897cac082SDavid S. Miller 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3521da177e4SLinus Torvalds {
3531716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3541716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
355a50feda5SEric Dumazet 			return true;
3561716a961SGao feng 	} else if (rt->dst.from) {
3573fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3581716a961SGao feng 	}
359a50feda5SEric Dumazet 	return false;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
363c71099acSThomas Graf {
364a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
365a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
366c71099acSThomas Graf }
367c71099acSThomas Graf 
36851ebd318SNicolas Dichtel /* Multipath route selection:
36951ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
37051ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
37151ebd318SNicolas Dichtel  */
37251ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
37351ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
37451ebd318SNicolas Dichtel {
37551ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
37651ebd318SNicolas Dichtel 
377c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
378c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
37951ebd318SNicolas Dichtel 
38051ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
38151ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
38251ebd318SNicolas Dichtel 	case IPPROTO_UDP:
38351ebd318SNicolas Dichtel 	case IPPROTO_TCP:
38451ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
385b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
386b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
38751ebd318SNicolas Dichtel 		break;
38851ebd318SNicolas Dichtel 
38951ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
390b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
391b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
39251ebd318SNicolas Dichtel 		break;
39351ebd318SNicolas Dichtel 	}
39451ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
395b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
39651ebd318SNicolas Dichtel 
39751ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
39851ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
39951ebd318SNicolas Dichtel 	return val % candidate_count;
40051ebd318SNicolas Dichtel }
40151ebd318SNicolas Dichtel 
40251ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
40352bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
40452bd4c0cSNicolas Dichtel 					     int strict)
40551ebd318SNicolas Dichtel {
40651ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
40751ebd318SNicolas Dichtel 	int route_choosen;
40851ebd318SNicolas Dichtel 
40951ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
41051ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
41151ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
41251ebd318SNicolas Dichtel 	 */
41351ebd318SNicolas Dichtel 	if (route_choosen)
41451ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
41551ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
41651ebd318SNicolas Dichtel 			route_choosen--;
41751ebd318SNicolas Dichtel 			if (route_choosen == 0) {
41852bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
41952bd4c0cSNicolas Dichtel 					break;
42051ebd318SNicolas Dichtel 				match = sibling;
42151ebd318SNicolas Dichtel 				break;
42251ebd318SNicolas Dichtel 			}
42351ebd318SNicolas Dichtel 		}
42451ebd318SNicolas Dichtel 	return match;
42551ebd318SNicolas Dichtel }
42651ebd318SNicolas Dichtel 
4271da177e4SLinus Torvalds /*
428c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds 
4318ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4328ed67789SDaniel Lezcano 						    struct rt6_info *rt,
433b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4341da177e4SLinus Torvalds 						    int oif,
435d420895eSYOSHIFUJI Hideaki 						    int flags)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4381da177e4SLinus Torvalds 	struct rt6_info *sprt;
4391da177e4SLinus Torvalds 
440dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
441dd3abc4eSYOSHIFUJI Hideaki 		goto out;
442dd3abc4eSYOSHIFUJI Hideaki 
443d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
444d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
445dd3abc4eSYOSHIFUJI Hideaki 
446dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4471da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4481da177e4SLinus Torvalds 				return sprt;
4491da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
45038308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4511da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
452d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4531da177e4SLinus Torvalds 						continue;
4541da177e4SLinus Torvalds 					if (local && (!oif ||
4551da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4561da177e4SLinus Torvalds 						continue;
4571da177e4SLinus Torvalds 				}
4581da177e4SLinus Torvalds 				local = sprt;
4591da177e4SLinus Torvalds 			}
460dd3abc4eSYOSHIFUJI Hideaki 		} else {
461dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
462dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
463dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
464dd3abc4eSYOSHIFUJI Hideaki 		}
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
467dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4681da177e4SLinus Torvalds 		if (local)
4691da177e4SLinus Torvalds 			return local;
4701da177e4SLinus Torvalds 
471d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4728ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4731da177e4SLinus Torvalds 	}
474dd3abc4eSYOSHIFUJI Hideaki out:
4751da177e4SLinus Torvalds 	return rt;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
47827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
47927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
48027097255SYOSHIFUJI Hideaki {
481f2c31e32SEric Dumazet 	struct neighbour *neigh;
48227097255SYOSHIFUJI Hideaki 	/*
48327097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
48427097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
48527097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
48627097255SYOSHIFUJI Hideaki 	 *
48727097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
48827097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
48927097255SYOSHIFUJI Hideaki 	 */
4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
491fdd6681dSAmerigo Wang 		return;
4922152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4932152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4942152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4952152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4962152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4972152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4987ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
4992152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5002152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
50152e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
50227097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
50327097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
50427097255SYOSHIFUJI Hideaki 
505b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh) {
50627097255SYOSHIFUJI Hideaki 			neigh->updated = jiffies;
5072152caeaSYOSHIFUJI Hideaki / 吉藤英明 			write_unlock(&neigh->lock);
508b820bb6bSYOSHIFUJI Hideaki / 吉藤英明 		}
5092152caeaSYOSHIFUJI Hideaki / 吉藤英明 
5102152caeaSYOSHIFUJI Hideaki / 吉藤英明 		target = (struct in6_addr *)&rt->rt6i_gateway;
51127097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
512d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
513f2c31e32SEric Dumazet 	} else {
5142152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
5152152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
51627097255SYOSHIFUJI Hideaki 	}
5172152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
518f2c31e32SEric Dumazet }
51927097255SYOSHIFUJI Hideaki #else
52027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
52127097255SYOSHIFUJI Hideaki {
52227097255SYOSHIFUJI Hideaki }
52327097255SYOSHIFUJI Hideaki #endif
52427097255SYOSHIFUJI Hideaki 
5251da177e4SLinus Torvalds /*
526554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5271da177e4SLinus Torvalds  */
528b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5291da177e4SLinus Torvalds {
530d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
531161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
532554cfb7eSYOSHIFUJI Hideaki 		return 2;
533161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
534161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
535161980f4SDavid S. Miller 		return 1;
536554cfb7eSYOSHIFUJI Hideaki 	return 0;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
539afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5401da177e4SLinus Torvalds {
541f2c31e32SEric Dumazet 	struct neighbour *neigh;
542afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
543f2c31e32SEric Dumazet 
5444d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5454d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
546afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
547145a3621SYOSHIFUJI Hideaki / 吉藤英明 
548145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
549145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
550145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
551145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
552554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
553afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
554398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
555a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
556afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
557398bcbebSYOSHIFUJI Hideaki #endif
558145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
559afc154e9SHannes Frederic Sowa 	} else {
560afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
561afc154e9SHannes Frederic Sowa 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_SOFT;
562a5a81f0bSPaul Marks 	}
563145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
564145a3621SYOSHIFUJI Hideaki / 吉藤英明 
565a5a81f0bSPaul Marks 	return ret;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
568554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
569554cfb7eSYOSHIFUJI Hideaki 			   int strict)
570554cfb7eSYOSHIFUJI Hideaki {
571a5a81f0bSPaul Marks 	int m;
5724d0c5911SYOSHIFUJI Hideaki 
5734d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
57477d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
575afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
576ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
577ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
578ebacaaa0SYOSHIFUJI Hideaki #endif
579afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
580afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
581afc154e9SHannes Frederic Sowa 		if (n < 0)
582afc154e9SHannes Frederic Sowa 			return n;
583afc154e9SHannes Frederic Sowa 	}
584554cfb7eSYOSHIFUJI Hideaki 	return m;
585554cfb7eSYOSHIFUJI Hideaki }
586554cfb7eSYOSHIFUJI Hideaki 
587f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
588afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
589afc154e9SHannes Frederic Sowa 				   bool *do_rr)
590554cfb7eSYOSHIFUJI Hideaki {
591554cfb7eSYOSHIFUJI Hideaki 	int m;
592afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
593554cfb7eSYOSHIFUJI Hideaki 
594554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
595f11e6659SDavid S. Miller 		goto out;
596554cfb7eSYOSHIFUJI Hideaki 
597554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
598afc154e9SHannes Frederic Sowa 	if (m == RT6_NUD_FAIL_SOFT && !IS_ENABLED(CONFIG_IPV6_ROUTER_PREF)) {
599afc154e9SHannes Frederic Sowa 		match_do_rr = true;
600afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
601afc154e9SHannes Frederic Sowa 	} else if (m < 0) {
602f11e6659SDavid S. Miller 		goto out;
6031da177e4SLinus Torvalds 	}
604f11e6659SDavid S. Miller 
605afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
606afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
607afc154e9SHannes Frederic Sowa 
608afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
609afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
610afc154e9SHannes Frederic Sowa 		*mpri = m;
611afc154e9SHannes Frederic Sowa 		match = rt;
612afc154e9SHannes Frederic Sowa 	}
613f11e6659SDavid S. Miller out:
614f11e6659SDavid S. Miller 	return match;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
617f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
618f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
619afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
620afc154e9SHannes Frederic Sowa 				     bool *do_rr)
621f11e6659SDavid S. Miller {
622f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
623f11e6659SDavid S. Miller 	int mpri = -1;
624f11e6659SDavid S. Miller 
625f11e6659SDavid S. Miller 	match = NULL;
626f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
627d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
628afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
629f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
630d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
631afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
632f11e6659SDavid S. Miller 
633f11e6659SDavid S. Miller 	return match;
634f11e6659SDavid S. Miller }
635f11e6659SDavid S. Miller 
636f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
637f11e6659SDavid S. Miller {
638f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6398ed67789SDaniel Lezcano 	struct net *net;
640afc154e9SHannes Frederic Sowa 	bool do_rr = false;
641f11e6659SDavid S. Miller 
642f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
643f11e6659SDavid S. Miller 	if (!rt0)
644f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
645f11e6659SDavid S. Miller 
646afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
647afc154e9SHannes Frederic Sowa 			     &do_rr);
648f11e6659SDavid S. Miller 
649afc154e9SHannes Frederic Sowa 	if (do_rr) {
650d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
651f11e6659SDavid S. Miller 
652554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
653f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
654f11e6659SDavid S. Miller 			next = fn->leaf;
655f11e6659SDavid S. Miller 
656f11e6659SDavid S. Miller 		if (next != rt0)
657f11e6659SDavid S. Miller 			fn->rr_ptr = next;
658554cfb7eSYOSHIFUJI Hideaki 	}
659554cfb7eSYOSHIFUJI Hideaki 
660d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
661a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
66470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
66570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
666b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
66770ceb4f5SYOSHIFUJI Hideaki {
668c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
66970ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
67070ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
67170ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6724bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
67370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
67470ceb4f5SYOSHIFUJI Hideaki 
67570ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
67670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
67770ceb4f5SYOSHIFUJI Hideaki 	}
67870ceb4f5SYOSHIFUJI Hideaki 
67970ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
68070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
68170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
68370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
68570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
68670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
68770ceb4f5SYOSHIFUJI Hideaki 		}
68870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
68970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
69070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
69170ceb4f5SYOSHIFUJI Hideaki 		}
69270ceb4f5SYOSHIFUJI Hideaki 	}
69370ceb4f5SYOSHIFUJI Hideaki 
69470ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
69570ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6963933fc95SJens Rosenboom 		return -EINVAL;
69770ceb4f5SYOSHIFUJI Hideaki 
6984bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
69970ceb4f5SYOSHIFUJI Hideaki 
70070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
70170ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
70270ceb4f5SYOSHIFUJI Hideaki 	else {
70370ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
70470ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
70570ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
70670ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
70770ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
70870ceb4f5SYOSHIFUJI Hideaki 	}
70970ceb4f5SYOSHIFUJI Hideaki 
710efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
711efa2cea0SDaniel Lezcano 				dev->ifindex);
71270ceb4f5SYOSHIFUJI Hideaki 
71370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
714e0a1ad73SThomas Graf 		ip6_del_rt(rt);
71570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
71670ceb4f5SYOSHIFUJI Hideaki 	}
71770ceb4f5SYOSHIFUJI Hideaki 
71870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
719efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
72070ceb4f5SYOSHIFUJI Hideaki 					pref);
72170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
72270ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
72370ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
72470ceb4f5SYOSHIFUJI Hideaki 
72570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7261716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7271716a961SGao feng 			rt6_clean_expires(rt);
7281716a961SGao feng 		else
7291716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7301716a961SGao feng 
73194e187c0SAmerigo Wang 		ip6_rt_put(rt);
73270ceb4f5SYOSHIFUJI Hideaki 	}
73370ceb4f5SYOSHIFUJI Hideaki 	return 0;
73470ceb4f5SYOSHIFUJI Hideaki }
73570ceb4f5SYOSHIFUJI Hideaki #endif
73670ceb4f5SYOSHIFUJI Hideaki 
7378ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
738982f56f3SYOSHIFUJI Hideaki do { \
7398ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
740982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
741e0eda7bbSVille Nuorvala 		while (1) { \
742982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
743c71099acSThomas Graf 				goto out; \
744982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
745982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7468bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
747982f56f3SYOSHIFUJI Hideaki 			else \
748982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
749c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
750c71099acSThomas Graf 				goto restart; \
751c71099acSThomas Graf 		} \
752982f56f3SYOSHIFUJI Hideaki 	} \
753982f56f3SYOSHIFUJI Hideaki } while (0)
754c71099acSThomas Graf 
7558ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7568ed67789SDaniel Lezcano 					     struct fib6_table *table,
7574c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct fib6_node *fn;
7601da177e4SLinus Torvalds 	struct rt6_info *rt;
7611da177e4SLinus Torvalds 
762c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7634c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
764c71099acSThomas Graf restart:
765c71099acSThomas Graf 	rt = fn->leaf;
7664c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
76751ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
76852bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
7694c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
770c71099acSThomas Graf out:
771d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
772c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7731da177e4SLinus Torvalds 	return rt;
774c71099acSThomas Graf 
775c71099acSThomas Graf }
776c71099acSThomas Graf 
777ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
778ea6e574eSFlorian Westphal 				    int flags)
779ea6e574eSFlorian Westphal {
780ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
781ea6e574eSFlorian Westphal }
782ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
783ea6e574eSFlorian Westphal 
7849acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7859acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
786c71099acSThomas Graf {
7874c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7884c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7894c9483b2SDavid S. Miller 		.daddr = *daddr,
790c71099acSThomas Graf 	};
791c71099acSThomas Graf 	struct dst_entry *dst;
79277d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
793c71099acSThomas Graf 
794adaa70bbSThomas Graf 	if (saddr) {
7954c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
796adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
797adaa70bbSThomas Graf 	}
798adaa70bbSThomas Graf 
7994c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
800c71099acSThomas Graf 	if (dst->error == 0)
801c71099acSThomas Graf 		return (struct rt6_info *) dst;
802c71099acSThomas Graf 
803c71099acSThomas Graf 	dst_release(dst);
804c71099acSThomas Graf 
8051da177e4SLinus Torvalds 	return NULL;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8087159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8097159039aSYOSHIFUJI Hideaki 
810c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8111da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8121da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8131da177e4SLinus Torvalds    be destroyed.
8141da177e4SLinus Torvalds  */
8151da177e4SLinus Torvalds 
81686872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	int err;
819c71099acSThomas Graf 	struct fib6_table *table;
8201da177e4SLinus Torvalds 
821c71099acSThomas Graf 	table = rt->rt6i_table;
822c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
82386872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
824c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	return err;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
82940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
83040e22e8fSThomas Graf {
8314d1169c1SDenis V. Lunev 	struct nl_info info = {
832d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8334d1169c1SDenis V. Lunev 	};
834528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
83540e22e8fSThomas Graf }
83640e22e8fSThomas Graf 
8371716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
83821efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
839b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	struct rt6_info *rt;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	/*
8441da177e4SLinus Torvalds 	 *	Clone the route.
8451da177e4SLinus Torvalds 	 */
8461da177e4SLinus Torvalds 
84721efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (rt) {
85058c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
851bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
85221efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
85358c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8544e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
85558c4fb86SYOSHIFUJI Hideaki 		}
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8601da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8614e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8621da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds #endif
86595a9a5baSYOSHIFUJI Hideaki 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	return rt;
8681da177e4SLinus Torvalds }
86995a9a5baSYOSHIFUJI Hideaki 
87021efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
87121efcfa0SEric Dumazet 					const struct in6_addr *daddr)
872299d9939SYOSHIFUJI Hideaki {
87321efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
87421efcfa0SEric Dumazet 
875887c95ccSYOSHIFUJI Hideaki / 吉藤英明 	if (rt)
876299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
877299d9939SYOSHIFUJI Hideaki 	return rt;
878299d9939SYOSHIFUJI Hideaki }
879299d9939SYOSHIFUJI Hideaki 
8808ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8814c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8821da177e4SLinus Torvalds {
8831da177e4SLinus Torvalds 	struct fib6_node *fn;
884519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
885c71099acSThomas Graf 	int strict = 0;
8861da177e4SLinus Torvalds 	int attempts = 3;
887519fbd87SYOSHIFUJI Hideaki 	int err;
88853b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8891da177e4SLinus Torvalds 
89077d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds relookup:
893c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8941da177e4SLinus Torvalds 
8958238dd06SYOSHIFUJI Hideaki restart_2:
8964c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds restart:
8994acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
90052bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
90152bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, oif, strict | reachable);
9024c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9038ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9048238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9051da177e4SLinus Torvalds 		goto out;
9061da177e4SLinus Torvalds 
907d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
908c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9091da177e4SLinus Torvalds 
910c440f160SYOSHIFUJI Hideaki / 吉藤英明 	if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
9114c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9127343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9134c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9147343ff31SDavid S. Miller 	else
9157343ff31SDavid S. Miller 		goto out2;
9161da177e4SLinus Torvalds 
91794e187c0SAmerigo Wang 	ip6_rt_put(rt);
9188ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9191da177e4SLinus Torvalds 
920d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
921e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
92240e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
923e40cf353SYOSHIFUJI Hideaki 		if (!err)
924e40cf353SYOSHIFUJI Hideaki 			goto out2;
925e40cf353SYOSHIFUJI Hideaki 	}
926e40cf353SYOSHIFUJI Hideaki 
927e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9281da177e4SLinus Torvalds 		goto out2;
9291da177e4SLinus Torvalds 
930519fbd87SYOSHIFUJI Hideaki 	/*
931c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
932519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9331da177e4SLinus Torvalds 	 */
93494e187c0SAmerigo Wang 	ip6_rt_put(rt);
9351da177e4SLinus Torvalds 	goto relookup;
936e40cf353SYOSHIFUJI Hideaki 
937519fbd87SYOSHIFUJI Hideaki out:
9388238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9398238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9408238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9418238dd06SYOSHIFUJI Hideaki 	}
942d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
943c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9441da177e4SLinus Torvalds out2:
945d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
946d8d1f30bSChangli Gao 	rt->dst.__use++;
947c71099acSThomas Graf 
948c71099acSThomas Graf 	return rt;
949c71099acSThomas Graf }
950c71099acSThomas Graf 
9518ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9524c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9534acad72dSPavel Emelyanov {
9544c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9554acad72dSPavel Emelyanov }
9564acad72dSPavel Emelyanov 
95772331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
95872331bc0SShmulik Ladkani 						struct net_device *dev,
95972331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
96072331bc0SShmulik Ladkani {
96172331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
96272331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
96372331bc0SShmulik Ladkani 
96472331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
96572331bc0SShmulik Ladkani }
96672331bc0SShmulik Ladkani 
967c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
968c71099acSThomas Graf {
969b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
970c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
971adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9724c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9734c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9744c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9754c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9766502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9774c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9784c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
979c71099acSThomas Graf 	};
980adaa70bbSThomas Graf 
98172331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
982c71099acSThomas Graf }
983c71099acSThomas Graf 
9848ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9854c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
986c71099acSThomas Graf {
9874c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
988c71099acSThomas Graf }
989c71099acSThomas Graf 
9909c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9914c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
992c71099acSThomas Graf {
993c71099acSThomas Graf 	int flags = 0;
994c71099acSThomas Graf 
9951fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9964dc27d1cSDavid McCullough 
9974c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
99877d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
999c71099acSThomas Graf 
10004c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1001adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10020c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1004adaa70bbSThomas Graf 
10054c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10087159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10091da177e4SLinus Torvalds 
10102774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
101114e50e57SDavid S. Miller {
10125c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
101314e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
101414e50e57SDavid S. Miller 
1015f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
101614e50e57SDavid S. Miller 	if (rt) {
1017d8d1f30bSChangli Gao 		new = &rt->dst;
101814e50e57SDavid S. Miller 
10198104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10208104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10218104891bSSteffen Klassert 
102214e50e57SDavid S. Miller 		new->__use = 1;
1023352e512cSHerbert Xu 		new->input = dst_discard;
1024352e512cSHerbert Xu 		new->output = dst_discard;
102514e50e57SDavid S. Miller 
102621efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
102721efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
102821efcfa0SEric Dumazet 		else
1029defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
103014e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
103114e50e57SDavid S. Miller 		if (rt->rt6i_idev)
103214e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
103314e50e57SDavid S. Miller 
10344e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10351716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
103614e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
103714e50e57SDavid S. Miller 
103814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
103914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
104014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
104114e50e57SDavid S. Miller #endif
104214e50e57SDavid S. Miller 
104314e50e57SDavid S. Miller 		dst_free(new);
104414e50e57SDavid S. Miller 	}
104514e50e57SDavid S. Miller 
104669ead7afSDavid S. Miller 	dst_release(dst_orig);
104769ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
104814e50e57SDavid S. Miller }
104914e50e57SDavid S. Miller 
10501da177e4SLinus Torvalds /*
10511da177e4SLinus Torvalds  *	Destination cache support functions
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct rt6_info *rt;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10591da177e4SLinus Torvalds 
10606f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10616f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10626f3118b5SNicolas Dichtel 	 * into this function always.
10636f3118b5SNicolas Dichtel 	 */
1064ca4c3fc2Sfan.du 	if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev)))
10656f3118b5SNicolas Dichtel 		return NULL;
10666f3118b5SNicolas Dichtel 
1067a4477c4dSLi RongQing 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
10681da177e4SLinus Torvalds 		return dst;
1069a4477c4dSLi RongQing 
10701da177e4SLinus Torvalds 	return NULL;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10741da177e4SLinus Torvalds {
10751da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	if (rt) {
107854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
107954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1080e0a1ad73SThomas Graf 				ip6_del_rt(rt);
108154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10821da177e4SLinus Torvalds 			}
108354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
108454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
108554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
108654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
108754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
108854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	struct rt6_info *rt;
10941da177e4SLinus Torvalds 
10953ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10961da177e4SLinus Torvalds 
1097adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10981da177e4SLinus Torvalds 	if (rt) {
10991eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11001eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11011eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11021eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11031eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11041da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 	}
11071eb4f758SHannes Frederic Sowa }
11081da177e4SLinus Torvalds 
11096700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11106700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11131da177e4SLinus Torvalds 
111481aded24SDavid S. Miller 	dst_confirm(dst);
11151da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
111681aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
111781aded24SDavid S. Miller 
11181da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11191da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1120defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11211da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1122defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1123defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11241da177e4SLinus Torvalds 		}
1125defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
112681aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
113042ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
113142ae66c8SDavid S. Miller 		     int oif, u32 mark)
113281aded24SDavid S. Miller {
113381aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
113481aded24SDavid S. Miller 	struct dst_entry *dst;
113581aded24SDavid S. Miller 	struct flowi6 fl6;
113681aded24SDavid S. Miller 
113781aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
113881aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
113981aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11403e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
114181aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
114281aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11436502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
114481aded24SDavid S. Miller 
114581aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
114681aded24SDavid S. Miller 	if (!dst->error)
11476700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
114881aded24SDavid S. Miller 	dst_release(dst);
114981aded24SDavid S. Miller }
115081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
115181aded24SDavid S. Miller 
115281aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
115381aded24SDavid S. Miller {
115481aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
115581aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
115681aded24SDavid S. Miller }
115781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
115881aded24SDavid S. Miller 
1159b55b76b2SDuan Jiong /* Handle redirects */
1160b55b76b2SDuan Jiong struct ip6rd_flowi {
1161b55b76b2SDuan Jiong 	struct flowi6 fl6;
1162b55b76b2SDuan Jiong 	struct in6_addr gateway;
1163b55b76b2SDuan Jiong };
1164b55b76b2SDuan Jiong 
1165b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
1166b55b76b2SDuan Jiong 					     struct fib6_table *table,
1167b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
1168b55b76b2SDuan Jiong 					     int flags)
1169b55b76b2SDuan Jiong {
1170b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1171b55b76b2SDuan Jiong 	struct rt6_info *rt;
1172b55b76b2SDuan Jiong 	struct fib6_node *fn;
1173b55b76b2SDuan Jiong 
1174b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
1175b55b76b2SDuan Jiong 	 * check if the redirect has come from approriate router.
1176b55b76b2SDuan Jiong 	 *
1177b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
1178b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
1179b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
1180b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
1181b55b76b2SDuan Jiong 	 * routes.
1182b55b76b2SDuan Jiong 	 */
1183b55b76b2SDuan Jiong 
1184b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
1185b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1186b55b76b2SDuan Jiong restart:
1187b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1188b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
1189b55b76b2SDuan Jiong 			continue;
1190b55b76b2SDuan Jiong 		if (rt->dst.error)
1191b55b76b2SDuan Jiong 			break;
1192b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1193b55b76b2SDuan Jiong 			continue;
1194b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1195b55b76b2SDuan Jiong 			continue;
1196b55b76b2SDuan Jiong 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1197b55b76b2SDuan Jiong 			continue;
1198b55b76b2SDuan Jiong 		break;
1199b55b76b2SDuan Jiong 	}
1200b55b76b2SDuan Jiong 
1201b55b76b2SDuan Jiong 	if (!rt)
1202b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1203b55b76b2SDuan Jiong 	else if (rt->dst.error) {
1204b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1205b55b76b2SDuan Jiong 		goto out;
1206b55b76b2SDuan Jiong 	}
1207b55b76b2SDuan Jiong 	BACKTRACK(net, &fl6->saddr);
1208b55b76b2SDuan Jiong out:
1209b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
1210b55b76b2SDuan Jiong 
1211b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
1212b55b76b2SDuan Jiong 
1213b55b76b2SDuan Jiong 	return rt;
1214b55b76b2SDuan Jiong };
1215b55b76b2SDuan Jiong 
1216b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
1217b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
1218b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
1219b55b76b2SDuan Jiong {
1220b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1221b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
1222b55b76b2SDuan Jiong 
1223b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
1224b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
1225b55b76b2SDuan Jiong 
1226b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
1227b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
1228b55b76b2SDuan Jiong }
1229b55b76b2SDuan Jiong 
12303a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
12313a5ad2eeSDavid S. Miller {
12323a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
12333a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12343a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12353a5ad2eeSDavid S. Miller 
12363a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
12373a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12383a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12393a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
12403a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12413a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12426502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
12433a5ad2eeSDavid S. Miller 
1244b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
12456700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
12463a5ad2eeSDavid S. Miller 	dst_release(dst);
12473a5ad2eeSDavid S. Miller }
12483a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12493a5ad2eeSDavid S. Miller 
1250c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1251c92a59ecSDuan Jiong 			    u32 mark)
1252c92a59ecSDuan Jiong {
1253c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1254c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1255c92a59ecSDuan Jiong 	struct dst_entry *dst;
1256c92a59ecSDuan Jiong 	struct flowi6 fl6;
1257c92a59ecSDuan Jiong 
1258c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
1259c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
1260c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
1261c92a59ecSDuan Jiong 	fl6.flowi6_flags = 0;
1262c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
1263c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
1264c92a59ecSDuan Jiong 
1265b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1266c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
1267c92a59ecSDuan Jiong 	dst_release(dst);
1268c92a59ecSDuan Jiong }
1269c92a59ecSDuan Jiong 
12703a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
12713a5ad2eeSDavid S. Miller {
12723a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
12733a5ad2eeSDavid S. Miller }
12743a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
12753a5ad2eeSDavid S. Miller 
12760dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
12771da177e4SLinus Torvalds {
12780dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
12790dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
12800dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
12810dbaee3bSDavid S. Miller 
12821da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
12831da177e4SLinus Torvalds 
12845578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
12855578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	/*
12881da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
12891da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
12901da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
12911da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
12921da177e4SLinus Torvalds 	 */
12931da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
12941da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
12951da177e4SLinus Torvalds 	return mtu;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
1298ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1299d33e4553SDavid S. Miller {
1300d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1301618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1302618f9bc7SSteffen Klassert 
1303618f9bc7SSteffen Klassert 	if (mtu)
1304618f9bc7SSteffen Klassert 		return mtu;
1305618f9bc7SSteffen Klassert 
1306618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1307d33e4553SDavid S. Miller 
1308d33e4553SDavid S. Miller 	rcu_read_lock();
1309d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1310d33e4553SDavid S. Miller 	if (idev)
1311d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1312d33e4553SDavid S. Miller 	rcu_read_unlock();
1313d33e4553SDavid S. Miller 
1314d33e4553SDavid S. Miller 	return mtu;
1315d33e4553SDavid S. Miller }
1316d33e4553SDavid S. Miller 
13173b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
13183b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
13195d0bbeebSThomas Graf 
13203b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
132187a11578SDavid S. Miller 				  struct flowi6 *fl6)
13221da177e4SLinus Torvalds {
132387a11578SDavid S. Miller 	struct dst_entry *dst;
13241da177e4SLinus Torvalds 	struct rt6_info *rt;
13251da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1326c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
13271da177e4SLinus Torvalds 
132838308473SDavid S. Miller 	if (unlikely(!idev))
1329122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
13301da177e4SLinus Torvalds 
13318b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
133238308473SDavid S. Miller 	if (unlikely(!rt)) {
13331da177e4SLinus Torvalds 		in6_dev_put(idev);
133487a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
13351da177e4SLinus Torvalds 		goto out;
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 
13388e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
13398e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1340d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
134187a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13428e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13438e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
134414edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
13451da177e4SLinus Torvalds 
13463b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1347d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1348d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13493b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13501da177e4SLinus Torvalds 
13515578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13521da177e4SLinus Torvalds 
135387a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
135487a11578SDavid S. Miller 
13551da177e4SLinus Torvalds out:
135687a11578SDavid S. Miller 	return dst;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
13593d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
13601da177e4SLinus Torvalds {
1361e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
13623d0f24a7SStephen Hemminger 	int more = 0;
13631da177e4SLinus Torvalds 
13643b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
13653b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
13665d0bbeebSThomas Graf 
13671da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
13681da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
13691da177e4SLinus Torvalds 			*pprev = dst->next;
13701da177e4SLinus Torvalds 			dst_free(dst);
13711da177e4SLinus Torvalds 		} else {
13721da177e4SLinus Torvalds 			pprev = &dst->next;
13733d0f24a7SStephen Hemminger 			++more;
13741da177e4SLinus Torvalds 		}
13751da177e4SLinus Torvalds 	}
13761da177e4SLinus Torvalds 
13773b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13785d0bbeebSThomas Graf 
13793d0f24a7SStephen Hemminger 	return more;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
13831e493d19SDavid S. Miller 			    void *arg)
13841e493d19SDavid S. Miller {
13851e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
13861e493d19SDavid S. Miller 
13871e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
13881e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
13891e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
13901e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
13911e493d19SDavid S. Miller 		if (func(rt, arg)) {
13921e493d19SDavid S. Miller 			*pprev = dst->next;
13931e493d19SDavid S. Miller 			dst_free(dst);
13941e493d19SDavid S. Miller 		} else {
13951e493d19SDavid S. Miller 			pprev = &dst->next;
13961e493d19SDavid S. Miller 		}
13971e493d19SDavid S. Miller 	}
13981e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
13991e493d19SDavid S. Miller }
14001e493d19SDavid S. Miller 
1401569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
14021da177e4SLinus Torvalds {
140386393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
14047019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
14057019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
14067019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
14077019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
14087019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1409fc66f95cSEric Dumazet 	int entries;
14101da177e4SLinus Torvalds 
1411fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
141249a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
1413fc66f95cSEric Dumazet 	    entries <= rt_max_size)
14141da177e4SLinus Torvalds 		goto out;
14151da177e4SLinus Torvalds 
14166891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
14172ac3ac8fSMichal Kubeček 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, entries > rt_max_size);
1418fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1419fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
14207019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
14211da177e4SLinus Torvalds out:
14227019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1423fc66f95cSEric Dumazet 	return entries > rt_max_size;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds /*
14271da177e4SLinus Torvalds  *
14281da177e4SLinus Torvalds  */
14291da177e4SLinus Torvalds 
143086872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
14311da177e4SLinus Torvalds {
14321da177e4SLinus Torvalds 	int err;
14335578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
14341da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
14351da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14361da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1437c71099acSThomas Graf 	struct fib6_table *table;
14381da177e4SLinus Torvalds 	int addr_type;
14391da177e4SLinus Torvalds 
144086872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
14411da177e4SLinus Torvalds 		return -EINVAL;
14421da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
144386872cb5SThomas Graf 	if (cfg->fc_src_len)
14441da177e4SLinus Torvalds 		return -EINVAL;
14451da177e4SLinus Torvalds #endif
144686872cb5SThomas Graf 	if (cfg->fc_ifindex) {
14471da177e4SLinus Torvalds 		err = -ENODEV;
14485578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
14491da177e4SLinus Torvalds 		if (!dev)
14501da177e4SLinus Torvalds 			goto out;
14511da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
14521da177e4SLinus Torvalds 		if (!idev)
14531da177e4SLinus Torvalds 			goto out;
14541da177e4SLinus Torvalds 	}
14551da177e4SLinus Torvalds 
145686872cb5SThomas Graf 	if (cfg->fc_metric == 0)
145786872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
14581da177e4SLinus Torvalds 
1459c71099acSThomas Graf 	err = -ENOBUFS;
146038308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1461d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1462d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
146338308473SDavid S. Miller 		if (!table) {
1464f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1465d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1466d71314b4SMatti Vaittinen 		}
1467d71314b4SMatti Vaittinen 	} else {
1468d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1469d71314b4SMatti Vaittinen 	}
147038308473SDavid S. Miller 
147138308473SDavid S. Miller 	if (!table)
1472c71099acSThomas Graf 		goto out;
1473c71099acSThomas Graf 
14748b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
14751da177e4SLinus Torvalds 
147638308473SDavid S. Miller 	if (!rt) {
14771da177e4SLinus Torvalds 		err = -ENOMEM;
14781da177e4SLinus Torvalds 		goto out;
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
14811716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
14821716a961SGao feng 		rt6_set_expires(rt, jiffies +
14831716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
14841716a961SGao feng 	else
14851716a961SGao feng 		rt6_clean_expires(rt);
14861da177e4SLinus Torvalds 
148786872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
148886872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
148986872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
149086872cb5SThomas Graf 
149186872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1494d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1495ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1496ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14971da177e4SLinus Torvalds 	else
1498d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14991da177e4SLinus Torvalds 
1500d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
15011da177e4SLinus Torvalds 
150286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
150386872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
15041da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
150511d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
15061da177e4SLinus Torvalds 
15078e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
15088e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
15098e2ec639SYan, Zheng 		if (!metrics) {
15108e2ec639SYan, Zheng 			err = -ENOMEM;
15118e2ec639SYan, Zheng 			goto out;
15128e2ec639SYan, Zheng 		}
15138e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
15148e2ec639SYan, Zheng 	}
15151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
151686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
151786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
15181da177e4SLinus Torvalds #endif
15191da177e4SLinus Torvalds 
152086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
15231da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
15241da177e4SLinus Torvalds 	 */
152586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
152638308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
152738308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
152838308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
15291da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
15305578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
15311da177e4SLinus Torvalds 			if (dev) {
15321da177e4SLinus Torvalds 				dev_put(dev);
15331da177e4SLinus Torvalds 				in6_dev_put(idev);
15341da177e4SLinus Torvalds 			}
15355578689aSDaniel Lezcano 			dev = net->loopback_dev;
15361da177e4SLinus Torvalds 			dev_hold(dev);
15371da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
15381da177e4SLinus Torvalds 			if (!idev) {
15391da177e4SLinus Torvalds 				err = -ENODEV;
15401da177e4SLinus Torvalds 				goto out;
15411da177e4SLinus Torvalds 			}
15421da177e4SLinus Torvalds 		}
1543d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1544d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
15451da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1546ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1547ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1548ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1549ef2c7d7bSNicolas Dichtel 			break;
1550ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1551ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1552ef2c7d7bSNicolas Dichtel 			break;
1553b4949ab2SNicolas Dichtel 		case RTN_THROW:
1554b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1555b4949ab2SNicolas Dichtel 			break;
1556ef2c7d7bSNicolas Dichtel 		default:
1557ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1558ef2c7d7bSNicolas Dichtel 			break;
1559ef2c7d7bSNicolas Dichtel 		}
15601da177e4SLinus Torvalds 		goto install_route;
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 
156386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1564b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
15651da177e4SLinus Torvalds 		int gwa_type;
15661da177e4SLinus Torvalds 
156786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
15684e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
15691da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
15721da177e4SLinus Torvalds 			struct rt6_info *grt;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
15751da177e4SLinus Torvalds 			   addresses as nexthop address.
15761da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
15771da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
15781da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
15791da177e4SLinus Torvalds 			   some exceptions. --ANK
15801da177e4SLinus Torvalds 			 */
15811da177e4SLinus Torvalds 			err = -EINVAL;
15821da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
15831da177e4SLinus Torvalds 				goto out;
15841da177e4SLinus Torvalds 
15855578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
158838308473SDavid S. Miller 			if (!grt)
15891da177e4SLinus Torvalds 				goto out;
15901da177e4SLinus Torvalds 			if (dev) {
1591d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
159294e187c0SAmerigo Wang 					ip6_rt_put(grt);
15931da177e4SLinus Torvalds 					goto out;
15941da177e4SLinus Torvalds 				}
15951da177e4SLinus Torvalds 			} else {
1596d1918542SDavid S. Miller 				dev = grt->dst.dev;
15971da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15981da177e4SLinus Torvalds 				dev_hold(dev);
15991da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
16001da177e4SLinus Torvalds 			}
16011da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
16021da177e4SLinus Torvalds 				err = 0;
160394e187c0SAmerigo Wang 			ip6_rt_put(grt);
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 			if (err)
16061da177e4SLinus Torvalds 				goto out;
16071da177e4SLinus Torvalds 		}
16081da177e4SLinus Torvalds 		err = -EINVAL;
160938308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
16101da177e4SLinus Torvalds 			goto out;
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	err = -ENODEV;
161438308473SDavid S. Miller 	if (!dev)
16151da177e4SLinus Torvalds 		goto out;
16161da177e4SLinus Torvalds 
1617c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1618c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1619c3968a85SDaniel Walter 			err = -EINVAL;
1620c3968a85SDaniel Walter 			goto out;
1621c3968a85SDaniel Walter 		}
16224e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1623c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1624c3968a85SDaniel Walter 	} else
1625c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1626c3968a85SDaniel Walter 
162786872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds install_route:
163086872cb5SThomas Graf 	if (cfg->fc_mx) {
163186872cb5SThomas Graf 		struct nlattr *nla;
163286872cb5SThomas Graf 		int remaining;
16331da177e4SLinus Torvalds 
163486872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
16358f4c1f9bSThomas Graf 			int type = nla_type(nla);
163686872cb5SThomas Graf 
163786872cb5SThomas Graf 			if (type) {
163886872cb5SThomas Graf 				if (type > RTAX_MAX) {
16391da177e4SLinus Torvalds 					err = -EINVAL;
16401da177e4SLinus Torvalds 					goto out;
16411da177e4SLinus Torvalds 				}
164286872cb5SThomas Graf 
1643defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
16441da177e4SLinus Torvalds 			}
16451da177e4SLinus Torvalds 		}
16461da177e4SLinus Torvalds 	}
16471da177e4SLinus Torvalds 
1648d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16491da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1650c71099acSThomas Graf 	rt->rt6i_table = table;
165163152fc0SDaniel Lezcano 
1652c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
165363152fc0SDaniel Lezcano 
165486872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds out:
16571da177e4SLinus Torvalds 	if (dev)
16581da177e4SLinus Torvalds 		dev_put(dev);
16591da177e4SLinus Torvalds 	if (idev)
16601da177e4SLinus Torvalds 		in6_dev_put(idev);
16611da177e4SLinus Torvalds 	if (rt)
1662d8d1f30bSChangli Gao 		dst_free(&rt->dst);
16631da177e4SLinus Torvalds 	return err;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
166686872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	int err;
1669c71099acSThomas Graf 	struct fib6_table *table;
1670d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
16711da177e4SLinus Torvalds 
16726825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
16736825a26cSGao feng 		err = -ENOENT;
16746825a26cSGao feng 		goto out;
16756825a26cSGao feng 	}
16766c813a72SPatrick McHardy 
1677c71099acSThomas Graf 	table = rt->rt6i_table;
1678c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
167986872cb5SThomas Graf 	err = fib6_del(rt, info);
1680c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16811da177e4SLinus Torvalds 
16826825a26cSGao feng out:
168394e187c0SAmerigo Wang 	ip6_rt_put(rt);
16841da177e4SLinus Torvalds 	return err;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
1687e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1688e0a1ad73SThomas Graf {
16894d1169c1SDenis V. Lunev 	struct nl_info info = {
1690d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
16914d1169c1SDenis V. Lunev 	};
1692528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1693e0a1ad73SThomas Graf }
1694e0a1ad73SThomas Graf 
169586872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16961da177e4SLinus Torvalds {
1697c71099acSThomas Graf 	struct fib6_table *table;
16981da177e4SLinus Torvalds 	struct fib6_node *fn;
16991da177e4SLinus Torvalds 	struct rt6_info *rt;
17001da177e4SLinus Torvalds 	int err = -ESRCH;
17011da177e4SLinus Torvalds 
17025578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
170338308473SDavid S. Miller 	if (!table)
1704c71099acSThomas Graf 		return err;
17051da177e4SLinus Torvalds 
1706c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1707c71099acSThomas Graf 
1708c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
170986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
171086872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (fn) {
1713d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
171486872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1715d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1716d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17171da177e4SLinus Torvalds 				continue;
171886872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
171986872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17201da177e4SLinus Torvalds 				continue;
172186872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17221da177e4SLinus Torvalds 				continue;
1723d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1724c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
17251da177e4SLinus Torvalds 
172686872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
17271da177e4SLinus Torvalds 		}
17281da177e4SLinus Torvalds 	}
1729c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	return err;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
17346700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1735a6279458SYOSHIFUJI Hideaki {
1736e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1737a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1738e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1739e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1740e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1741e8599ff4SDavid S. Miller 	struct neighbour *neigh;
174271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
17436e157b6aSDavid S. Miller 	int optlen, on_link;
17446e157b6aSDavid S. Miller 	u8 *lladdr;
1745e8599ff4SDavid S. Miller 
174629a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
174771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1748e8599ff4SDavid S. Miller 
1749e8599ff4SDavid S. Miller 	if (optlen < 0) {
17506e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1751e8599ff4SDavid S. Miller 		return;
1752e8599ff4SDavid S. Miller 	}
1753e8599ff4SDavid S. Miller 
175471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1755e8599ff4SDavid S. Miller 
175671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
17576e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1758e8599ff4SDavid S. Miller 		return;
1759e8599ff4SDavid S. Miller 	}
1760e8599ff4SDavid S. Miller 
17616e157b6aSDavid S. Miller 	on_link = 0;
176271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1763e8599ff4SDavid S. Miller 		on_link = 1;
176471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1765e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
17666e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1767e8599ff4SDavid S. Miller 		return;
1768e8599ff4SDavid S. Miller 	}
1769e8599ff4SDavid S. Miller 
1770e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1771e8599ff4SDavid S. Miller 	if (!in6_dev)
1772e8599ff4SDavid S. Miller 		return;
1773e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1774e8599ff4SDavid S. Miller 		return;
1775e8599ff4SDavid S. Miller 
1776e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1777e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1778e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1779e8599ff4SDavid S. Miller 	 */
1780e8599ff4SDavid S. Miller 
178171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1782e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1783e8599ff4SDavid S. Miller 		return;
1784e8599ff4SDavid S. Miller 	}
17856e157b6aSDavid S. Miller 
17866e157b6aSDavid S. Miller 	lladdr = NULL;
1787e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1788e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1789e8599ff4SDavid S. Miller 					     skb->dev);
1790e8599ff4SDavid S. Miller 		if (!lladdr) {
1791e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1792e8599ff4SDavid S. Miller 			return;
1793e8599ff4SDavid S. Miller 		}
1794e8599ff4SDavid S. Miller 	}
1795e8599ff4SDavid S. Miller 
17966e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17976e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17986e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17996e157b6aSDavid S. Miller 		return;
18006e157b6aSDavid S. Miller 	}
18016e157b6aSDavid S. Miller 
18026e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18036e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18046e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18056e157b6aSDavid S. Miller 	 */
18066e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18076e157b6aSDavid S. Miller 
180871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1809e8599ff4SDavid S. Miller 	if (!neigh)
1810e8599ff4SDavid S. Miller 		return;
1811e8599ff4SDavid S. Miller 
18121da177e4SLinus Torvalds 	/*
18131da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18141da177e4SLinus Torvalds 	 */
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18171da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18181da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18191da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18201da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18211da177e4SLinus Torvalds 		     );
18221da177e4SLinus Torvalds 
182371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
182438308473SDavid S. Miller 	if (!nrt)
18251da177e4SLinus Torvalds 		goto out;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18281da177e4SLinus Torvalds 	if (on_link)
18291da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18301da177e4SLinus Torvalds 
18314e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
18321da177e4SLinus Torvalds 
183340e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18341da177e4SLinus Torvalds 		goto out;
18351da177e4SLinus Torvalds 
1836d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1837d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
183871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
183960592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
18408d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18418d71740cSTom Tucker 
18421da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18436e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1844e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18451da177e4SLinus Torvalds 	}
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds out:
1848e8599ff4SDavid S. Miller 	neigh_release(neigh);
18496e157b6aSDavid S. Miller }
18506e157b6aSDavid S. Miller 
18511da177e4SLinus Torvalds /*
18521da177e4SLinus Torvalds  *	Misc support functions
18531da177e4SLinus Torvalds  */
18541da177e4SLinus Torvalds 
18551716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
185621efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18571da177e4SLinus Torvalds {
1858d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18598b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18608b96d22dSDavid S. Miller 					    ort->rt6i_table);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	if (rt) {
1863d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1864d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18658e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18661da177e4SLinus Torvalds 
18674e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18688e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1869defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1870d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18711da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18721da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18731da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1874d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18751da177e4SLinus Torvalds 
18764e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18771716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18781716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18791716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18801716a961SGao feng 			rt6_set_from(rt, ort);
18811da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18841da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18851da177e4SLinus Torvalds #endif
18860f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1887c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 	return rt;
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
189270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1893efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1894b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1895b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
189670ceb4f5SYOSHIFUJI Hideaki {
189770ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
189870ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1899c71099acSThomas Graf 	struct fib6_table *table;
190070ceb4f5SYOSHIFUJI Hideaki 
1901efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
190238308473SDavid S. Miller 	if (!table)
1903c71099acSThomas Graf 		return NULL;
1904c71099acSThomas Graf 
19055744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1906c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
190770ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
190870ceb4f5SYOSHIFUJI Hideaki 		goto out;
190970ceb4f5SYOSHIFUJI Hideaki 
1910d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1911d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
191270ceb4f5SYOSHIFUJI Hideaki 			continue;
191370ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
191470ceb4f5SYOSHIFUJI Hideaki 			continue;
191570ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
191670ceb4f5SYOSHIFUJI Hideaki 			continue;
1917d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
191870ceb4f5SYOSHIFUJI Hideaki 		break;
191970ceb4f5SYOSHIFUJI Hideaki 	}
192070ceb4f5SYOSHIFUJI Hideaki out:
19215744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
192270ceb4f5SYOSHIFUJI Hideaki 	return rt;
192370ceb4f5SYOSHIFUJI Hideaki }
192470ceb4f5SYOSHIFUJI Hideaki 
1925efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1926b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1927b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
192895c96174SEric Dumazet 					   unsigned int pref)
192970ceb4f5SYOSHIFUJI Hideaki {
193086872cb5SThomas Graf 	struct fib6_config cfg = {
193186872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1932238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
193386872cb5SThomas Graf 		.fc_ifindex	= ifindex,
193486872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
193586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
193686872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
193715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1938efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1939efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
194086872cb5SThomas Graf 	};
194170ceb4f5SYOSHIFUJI Hideaki 
19424e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19434e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
194486872cb5SThomas Graf 
1945e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1946e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
194786872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
194870ceb4f5SYOSHIFUJI Hideaki 
194986872cb5SThomas Graf 	ip6_route_add(&cfg);
195070ceb4f5SYOSHIFUJI Hideaki 
1951efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
195270ceb4f5SYOSHIFUJI Hideaki }
195370ceb4f5SYOSHIFUJI Hideaki #endif
195470ceb4f5SYOSHIFUJI Hideaki 
1955b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19561da177e4SLinus Torvalds {
19571da177e4SLinus Torvalds 	struct rt6_info *rt;
1958c71099acSThomas Graf 	struct fib6_table *table;
19591da177e4SLinus Torvalds 
1960c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
196138308473SDavid S. Miller 	if (!table)
1962c71099acSThomas Graf 		return NULL;
19631da177e4SLinus Torvalds 
19645744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1965d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1966d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1967045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19681da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19691da177e4SLinus Torvalds 			break;
19701da177e4SLinus Torvalds 	}
19711da177e4SLinus Torvalds 	if (rt)
1972d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
19735744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
19741da177e4SLinus Torvalds 	return rt;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
1977b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1978ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1979ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19801da177e4SLinus Torvalds {
198186872cb5SThomas Graf 	struct fib6_config cfg = {
198286872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1983238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
198486872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
198586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
198686872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
198715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
19885578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1989c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
199086872cb5SThomas Graf 	};
19911da177e4SLinus Torvalds 
19924e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19931da177e4SLinus Torvalds 
199486872cb5SThomas Graf 	ip6_route_add(&cfg);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19997b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	struct rt6_info *rt;
2002c71099acSThomas Graf 	struct fib6_table *table;
2003c71099acSThomas Graf 
2004c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20057b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
200638308473SDavid S. Miller 	if (!table)
2007c71099acSThomas Graf 		return;
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds restart:
2010c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2011d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20123e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
20133e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
2014d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2015c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2016e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20171da177e4SLinus Torvalds 			goto restart;
20181da177e4SLinus Torvalds 		}
20191da177e4SLinus Torvalds 	}
2020c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
20235578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20245578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
202586872cb5SThomas Graf 				 struct fib6_config *cfg)
202686872cb5SThomas Graf {
202786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
202886872cb5SThomas Graf 
202986872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
203086872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
203186872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
203286872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
203386872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
203486872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
203586872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
203686872cb5SThomas Graf 
20375578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2038f1243c2dSBenjamin Thery 
20394e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20404e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20414e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
204286872cb5SThomas Graf }
204386872cb5SThomas Graf 
20445578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20451da177e4SLinus Torvalds {
204686872cb5SThomas Graf 	struct fib6_config cfg;
20471da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20481da177e4SLinus Torvalds 	int err;
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	switch(cmd) {
20511da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20521da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2053af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
20541da177e4SLinus Torvalds 			return -EPERM;
20551da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20561da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20571da177e4SLinus Torvalds 		if (err)
20581da177e4SLinus Torvalds 			return -EFAULT;
20591da177e4SLinus Torvalds 
20605578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
206186872cb5SThomas Graf 
20621da177e4SLinus Torvalds 		rtnl_lock();
20631da177e4SLinus Torvalds 		switch (cmd) {
20641da177e4SLinus Torvalds 		case SIOCADDRT:
206586872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20661da177e4SLinus Torvalds 			break;
20671da177e4SLinus Torvalds 		case SIOCDELRT:
206886872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20691da177e4SLinus Torvalds 			break;
20701da177e4SLinus Torvalds 		default:
20711da177e4SLinus Torvalds 			err = -EINVAL;
20721da177e4SLinus Torvalds 		}
20731da177e4SLinus Torvalds 		rtnl_unlock();
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 		return err;
20763ff50b79SStephen Hemminger 	}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	return -EINVAL;
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds /*
20821da177e4SLinus Torvalds  *	Drop the packet on the floor
20831da177e4SLinus Torvalds  */
20841da177e4SLinus Torvalds 
2085d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20861da177e4SLinus Torvalds {
2087612f09e8SYOSHIFUJI Hideaki 	int type;
2088adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2089612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2090612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20910660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
209245bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20933bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20943bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2095612f09e8SYOSHIFUJI Hideaki 			break;
2096612f09e8SYOSHIFUJI Hideaki 		}
2097612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2098612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20993bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21003bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2101612f09e8SYOSHIFUJI Hideaki 		break;
2102612f09e8SYOSHIFUJI Hideaki 	}
21033ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21041da177e4SLinus Torvalds 	kfree_skb(skb);
21051da177e4SLinus Torvalds 	return 0;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
21089ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21099ce8ade0SThomas Graf {
2110612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21119ce8ade0SThomas Graf }
21129ce8ade0SThomas Graf 
211320380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
21141da177e4SLinus Torvalds {
2115adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2116612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21196723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
21206723ab54SDavid S. Miller 
21219ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21229ce8ade0SThomas Graf {
2123612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21249ce8ade0SThomas Graf }
21259ce8ade0SThomas Graf 
21269ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21279ce8ade0SThomas Graf {
2128adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2129612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21309ce8ade0SThomas Graf }
21319ce8ade0SThomas Graf 
21326723ab54SDavid S. Miller #endif
21336723ab54SDavid S. Miller 
21341da177e4SLinus Torvalds /*
21351da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21361da177e4SLinus Torvalds  */
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21391da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21408f031519SDavid S. Miller 				    bool anycast)
21411da177e4SLinus Torvalds {
2142c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21438b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
21441da177e4SLinus Torvalds 
214538308473SDavid S. Miller 	if (!rt) {
2146f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21471da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
214840385653SBen Greear 	}
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	in6_dev_hold(idev);
21511da177e4SLinus Torvalds 
215211d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2153d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2154d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21551da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
215858c4fb86SYOSHIFUJI Hideaki 	if (anycast)
215958c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
216058c4fb86SYOSHIFUJI Hideaki 	else
21611da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21621da177e4SLinus Torvalds 
21634e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21641da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21655578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21661da177e4SLinus Torvalds 
2167d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	return rt;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
2172c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2173c3968a85SDaniel Walter 			struct rt6_info *rt,
2174b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2175c3968a85SDaniel Walter 			unsigned int prefs,
2176c3968a85SDaniel Walter 			struct in6_addr *saddr)
2177c3968a85SDaniel Walter {
2178c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2179c3968a85SDaniel Walter 	int err = 0;
2180c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21814e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2182c3968a85SDaniel Walter 	else
2183c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2184c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2185c3968a85SDaniel Walter 	return err;
2186c3968a85SDaniel Walter }
2187c3968a85SDaniel Walter 
2188c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2189c3968a85SDaniel Walter struct arg_dev_net_ip {
2190c3968a85SDaniel Walter 	struct net_device *dev;
2191c3968a85SDaniel Walter 	struct net *net;
2192c3968a85SDaniel Walter 	struct in6_addr *addr;
2193c3968a85SDaniel Walter };
2194c3968a85SDaniel Walter 
2195c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2196c3968a85SDaniel Walter {
2197c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2198c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2199c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2200c3968a85SDaniel Walter 
2201d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2202c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2203c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2204c3968a85SDaniel Walter 		/* remove prefsrc entry */
2205c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2206c3968a85SDaniel Walter 	}
2207c3968a85SDaniel Walter 	return 0;
2208c3968a85SDaniel Walter }
2209c3968a85SDaniel Walter 
2210c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2211c3968a85SDaniel Walter {
2212c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2213c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2214c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2215c3968a85SDaniel Walter 		.net = net,
2216c3968a85SDaniel Walter 		.addr = &ifp->addr,
2217c3968a85SDaniel Walter 	};
2218c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2219c3968a85SDaniel Walter }
2220c3968a85SDaniel Walter 
22218ed67789SDaniel Lezcano struct arg_dev_net {
22228ed67789SDaniel Lezcano 	struct net_device *dev;
22238ed67789SDaniel Lezcano 	struct net *net;
22248ed67789SDaniel Lezcano };
22258ed67789SDaniel Lezcano 
22261da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22271da177e4SLinus Torvalds {
2228bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2229bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22308ed67789SDaniel Lezcano 
2231d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2232c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22331da177e4SLinus Torvalds 		return -1;
2234c159d30cSDavid S. Miller 
22351da177e4SLinus Torvalds 	return 0;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
2238f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22391da177e4SLinus Torvalds {
22408ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22418ed67789SDaniel Lezcano 		.dev = dev,
22428ed67789SDaniel Lezcano 		.net = net,
22438ed67789SDaniel Lezcano 	};
22448ed67789SDaniel Lezcano 
22458ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22461e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds 
224995c96174SEric Dumazet struct rt6_mtu_change_arg {
22501da177e4SLinus Torvalds 	struct net_device *dev;
225195c96174SEric Dumazet 	unsigned int mtu;
22521da177e4SLinus Torvalds };
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22571da177e4SLinus Torvalds 	struct inet6_dev *idev;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22601da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22611da177e4SLinus Torvalds 	   We still use this lock to block changes
22621da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22631da177e4SLinus Torvalds 	*/
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
226638308473SDavid S. Miller 	if (!idev)
22671da177e4SLinus Torvalds 		return 0;
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22701da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22711da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22721da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22731da177e4SLinus Torvalds 	 */
22741da177e4SLinus Torvalds 	/*
22751da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22761da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22771da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22781da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22791da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22801da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22811da177e4SLinus Torvalds 	   PMTU discouvery.
22821da177e4SLinus Torvalds 	 */
2283d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2284d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2285d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2286d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2287d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2288defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2289566cfd8fSSimon Arlott 	}
22901da177e4SLinus Torvalds 	return 0;
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
229395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22941da177e4SLinus Torvalds {
2295c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2296c71099acSThomas Graf 		.dev = dev,
2297c71099acSThomas Graf 		.mtu = mtu,
2298c71099acSThomas Graf 	};
22991da177e4SLinus Torvalds 
2300c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds 
2303ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23045176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
230586872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2306ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
230786872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
230886872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
230951ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
231086872cb5SThomas Graf };
231186872cb5SThomas Graf 
231286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
231386872cb5SThomas Graf 			      struct fib6_config *cfg)
23141da177e4SLinus Torvalds {
231586872cb5SThomas Graf 	struct rtmsg *rtm;
231686872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
231786872cb5SThomas Graf 	int err;
23181da177e4SLinus Torvalds 
231986872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
232086872cb5SThomas Graf 	if (err < 0)
232186872cb5SThomas Graf 		goto errout;
23221da177e4SLinus Torvalds 
232386872cb5SThomas Graf 	err = -EINVAL;
232486872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
232586872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
232686872cb5SThomas Graf 
232786872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
232886872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
232986872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
233086872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
233186872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2332ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
233386872cb5SThomas Graf 
2334ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2335ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2336b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2337b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
233886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
233986872cb5SThomas Graf 
2340ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2341ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2342ab79ad14SMaciej Żenczykowski 
234315e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
234486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23453b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
234686872cb5SThomas Graf 
234786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
234886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
234986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23501da177e4SLinus Torvalds 	}
235186872cb5SThomas Graf 
235286872cb5SThomas Graf 	if (tb[RTA_DST]) {
235386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
235486872cb5SThomas Graf 
235586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
235686872cb5SThomas Graf 			goto errout;
235786872cb5SThomas Graf 
235886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23591da177e4SLinus Torvalds 	}
236086872cb5SThomas Graf 
236186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
236286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
236386872cb5SThomas Graf 
236486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
236586872cb5SThomas Graf 			goto errout;
236686872cb5SThomas Graf 
236786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23681da177e4SLinus Torvalds 	}
236986872cb5SThomas Graf 
2370c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2371c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2372c3968a85SDaniel Walter 
237386872cb5SThomas Graf 	if (tb[RTA_OIF])
237486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
237586872cb5SThomas Graf 
237686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
237786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
237886872cb5SThomas Graf 
237986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
238086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
238186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23821da177e4SLinus Torvalds 	}
238386872cb5SThomas Graf 
238486872cb5SThomas Graf 	if (tb[RTA_TABLE])
238586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
238686872cb5SThomas Graf 
238751ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
238851ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
238951ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
239051ebd318SNicolas Dichtel 	}
239151ebd318SNicolas Dichtel 
239286872cb5SThomas Graf 	err = 0;
239386872cb5SThomas Graf errout:
239486872cb5SThomas Graf 	return err;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
239751ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
239851ebd318SNicolas Dichtel {
239951ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
240051ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
240151ebd318SNicolas Dichtel 	int remaining;
240251ebd318SNicolas Dichtel 	int attrlen;
240351ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
240451ebd318SNicolas Dichtel 
240551ebd318SNicolas Dichtel beginning:
240651ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
240751ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
240851ebd318SNicolas Dichtel 
240951ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
241051ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
241151ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
241251ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
241351ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
241451ebd318SNicolas Dichtel 
241551ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
241651ebd318SNicolas Dichtel 		if (attrlen > 0) {
241751ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
241851ebd318SNicolas Dichtel 
241951ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
242051ebd318SNicolas Dichtel 			if (nla) {
242151ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
242251ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
242351ebd318SNicolas Dichtel 			}
242451ebd318SNicolas Dichtel 		}
242551ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
242651ebd318SNicolas Dichtel 		if (err) {
242751ebd318SNicolas Dichtel 			last_err = err;
242851ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
242951ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
243051ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
243151ebd318SNicolas Dichtel 			 */
243251ebd318SNicolas Dichtel 			if (add) {
243351ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
243451ebd318SNicolas Dichtel 				 * next hops that have been already added.
243551ebd318SNicolas Dichtel 				 */
243651ebd318SNicolas Dichtel 				add = 0;
243751ebd318SNicolas Dichtel 				goto beginning;
243851ebd318SNicolas Dichtel 			}
243951ebd318SNicolas Dichtel 		}
24401a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
24411a72418bSNicolas Dichtel 		 * this flag after the first nexthop (if there is a collision,
24421a72418bSNicolas Dichtel 		 * we have already fail to add the first nexthop:
24431a72418bSNicolas Dichtel 		 * fib6_add_rt2node() has reject it).
24441a72418bSNicolas Dichtel 		 */
24451a72418bSNicolas Dichtel 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
244651ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
244751ebd318SNicolas Dichtel 	}
244851ebd318SNicolas Dichtel 
244951ebd318SNicolas Dichtel 	return last_err;
245051ebd318SNicolas Dichtel }
245151ebd318SNicolas Dichtel 
2452661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24531da177e4SLinus Torvalds {
245486872cb5SThomas Graf 	struct fib6_config cfg;
245586872cb5SThomas Graf 	int err;
24561da177e4SLinus Torvalds 
245786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
245886872cb5SThomas Graf 	if (err < 0)
245986872cb5SThomas Graf 		return err;
246086872cb5SThomas Graf 
246151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
246251ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
246351ebd318SNicolas Dichtel 	else
246486872cb5SThomas Graf 		return ip6_route_del(&cfg);
24651da177e4SLinus Torvalds }
24661da177e4SLinus Torvalds 
2467661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
24681da177e4SLinus Torvalds {
246986872cb5SThomas Graf 	struct fib6_config cfg;
247086872cb5SThomas Graf 	int err;
24711da177e4SLinus Torvalds 
247286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
247386872cb5SThomas Graf 	if (err < 0)
247486872cb5SThomas Graf 		return err;
247586872cb5SThomas Graf 
247651ebd318SNicolas Dichtel 	if (cfg.fc_mp)
247751ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
247851ebd318SNicolas Dichtel 	else
247986872cb5SThomas Graf 		return ip6_route_add(&cfg);
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
2482339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2483339bf98fSThomas Graf {
2484339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2485339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2486339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2487339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2488339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2489339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2490339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2491339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2492339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24936a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2494339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2495339bf98fSThomas Graf }
2496339bf98fSThomas Graf 
2497191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2498191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
24990d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
250015e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
25017bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
25021da177e4SLinus Torvalds {
25031da177e4SLinus Torvalds 	struct rtmsg *rtm;
25041da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2505e3703b3dSThomas Graf 	long expires;
25069e762a4aSPatrick McHardy 	u32 table;
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
25091da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
25101da177e4SLinus Torvalds 			/* success since this is not a prefix route */
25111da177e4SLinus Torvalds 			return 1;
25121da177e4SLinus Torvalds 		}
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 
251515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
251638308473SDavid S. Miller 	if (!nlh)
251726932566SPatrick McHardy 		return -EMSGSIZE;
25182d7202bfSThomas Graf 
25192d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
25201da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
25211da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
25221da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
25231da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2524c71099acSThomas Graf 	if (rt->rt6i_table)
25259e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2526c71099acSThomas Graf 	else
25279e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
25289e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2529c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2530c78679e8SDavid S. Miller 		goto nla_put_failure;
2531ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2532ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2533ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2534ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2535ef2c7d7bSNicolas Dichtel 			break;
2536ef2c7d7bSNicolas Dichtel 		case -EACCES:
2537ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2538ef2c7d7bSNicolas Dichtel 			break;
2539b4949ab2SNicolas Dichtel 		case -EAGAIN:
2540b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2541b4949ab2SNicolas Dichtel 			break;
2542ef2c7d7bSNicolas Dichtel 		default:
25431da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2544ef2c7d7bSNicolas Dichtel 			break;
2545ef2c7d7bSNicolas Dichtel 		}
2546ef2c7d7bSNicolas Dichtel 	}
2547ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2548ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2549d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
25501da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
25511da177e4SLinus Torvalds 	else
25521da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
25531da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
25541da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
25551da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
25561da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
25571da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2558f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2559f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
25601da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2561f0396f60SDenis Ovsienko 		else
2562f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2563f0396f60SDenis Ovsienko 	}
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
25661da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	if (dst) {
2569c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2570c78679e8SDavid S. Miller 			goto nla_put_failure;
25711da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
25721da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2573c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2574c78679e8SDavid S. Miller 			goto nla_put_failure;
25751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25761da177e4SLinus Torvalds 	if (src) {
2577c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2578c78679e8SDavid S. Miller 			goto nla_put_failure;
25791da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2580c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2581c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2582c78679e8SDavid S. Miller 		goto nla_put_failure;
25831da177e4SLinus Torvalds #endif
25847bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25857bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
25867bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
25878229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
25887bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
25897bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25907bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25917bc570c8SYOSHIFUJI Hideaki 						return 0;
25927bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
25937bc570c8SYOSHIFUJI Hideaki 				} else {
25947bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
25957bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
25967bc570c8SYOSHIFUJI Hideaki 				}
25977bc570c8SYOSHIFUJI Hideaki 			}
25987bc570c8SYOSHIFUJI Hideaki 		} else
25997bc570c8SYOSHIFUJI Hideaki #endif
2600c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2601c78679e8SDavid S. Miller 				goto nla_put_failure;
26027bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
26031da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2604c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2605c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2606c78679e8SDavid S. Miller 			goto nla_put_failure;
2607c3968a85SDaniel Walter 	}
2608c3968a85SDaniel Walter 
2609c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2610c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
26114e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2612c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2613c78679e8SDavid S. Miller 			goto nla_put_failure;
26141da177e4SLinus Torvalds 	}
26152d7202bfSThomas Graf 
2616defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
26172d7202bfSThomas Graf 		goto nla_put_failure;
26182d7202bfSThomas Graf 
2619dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2620dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 		if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
262194f826b8SEric Dumazet 			goto nla_put_failure;
262294f826b8SEric Dumazet 	}
26232d7202bfSThomas Graf 
2624c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2625c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2626c78679e8SDavid S. Miller 		goto nla_put_failure;
2627c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2628c78679e8SDavid S. Miller 		goto nla_put_failure;
26298253947eSLi Wei 
26308253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
263169cdf8f9SYOSHIFUJI Hideaki 
263287a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2633e3703b3dSThomas Graf 		goto nla_put_failure;
26341da177e4SLinus Torvalds 
26352d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
26362d7202bfSThomas Graf 
26372d7202bfSThomas Graf nla_put_failure:
263826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
263926932566SPatrick McHardy 	return -EMSGSIZE;
26401da177e4SLinus Torvalds }
26411da177e4SLinus Torvalds 
26421b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
26431da177e4SLinus Torvalds {
26441da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
26451da177e4SLinus Torvalds 	int prefix;
26461da177e4SLinus Torvalds 
26472d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
26482d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
26491da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
26501da177e4SLinus Torvalds 	} else
26511da177e4SLinus Torvalds 		prefix = 0;
26521da177e4SLinus Torvalds 
2653191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2654191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
265515e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
26567bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
2659661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
26601da177e4SLinus Torvalds {
26613b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2662ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26631da177e4SLinus Torvalds 	struct rt6_info *rt;
2664ab364a6fSThomas Graf 	struct sk_buff *skb;
2665ab364a6fSThomas Graf 	struct rtmsg *rtm;
26664c9483b2SDavid S. Miller 	struct flowi6 fl6;
266772331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2668ab364a6fSThomas Graf 
2669ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2670ab364a6fSThomas Graf 	if (err < 0)
2671ab364a6fSThomas Graf 		goto errout;
2672ab364a6fSThomas Graf 
2673ab364a6fSThomas Graf 	err = -EINVAL;
26744c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2675ab364a6fSThomas Graf 
2676ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2677ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2678ab364a6fSThomas Graf 			goto errout;
2679ab364a6fSThomas Graf 
26804e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2681ab364a6fSThomas Graf 	}
2682ab364a6fSThomas Graf 
2683ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2684ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2685ab364a6fSThomas Graf 			goto errout;
2686ab364a6fSThomas Graf 
26874e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2688ab364a6fSThomas Graf 	}
2689ab364a6fSThomas Graf 
2690ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2691ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2692ab364a6fSThomas Graf 
2693ab364a6fSThomas Graf 	if (tb[RTA_OIF])
269472331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2695ab364a6fSThomas Graf 
2696ab364a6fSThomas Graf 	if (iif) {
2697ab364a6fSThomas Graf 		struct net_device *dev;
269872331bc0SShmulik Ladkani 		int flags = 0;
269972331bc0SShmulik Ladkani 
27005578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2701ab364a6fSThomas Graf 		if (!dev) {
2702ab364a6fSThomas Graf 			err = -ENODEV;
2703ab364a6fSThomas Graf 			goto errout;
2704ab364a6fSThomas Graf 		}
270572331bc0SShmulik Ladkani 
270672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
270772331bc0SShmulik Ladkani 
270872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
270972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
271072331bc0SShmulik Ladkani 
271172331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
271272331bc0SShmulik Ladkani 							       flags);
271372331bc0SShmulik Ladkani 	} else {
271472331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
271572331bc0SShmulik Ladkani 
271672331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2717ab364a6fSThomas Graf 	}
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
272038308473SDavid S. Miller 	if (!skb) {
272194e187c0SAmerigo Wang 		ip6_rt_put(rt);
2722ab364a6fSThomas Graf 		err = -ENOBUFS;
2723ab364a6fSThomas Graf 		goto errout;
2724ab364a6fSThomas Graf 	}
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27271da177e4SLinus Torvalds 	   through good chunk of routing engine.
27281da177e4SLinus Torvalds 	 */
2729459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
27301da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
27311da177e4SLinus Torvalds 
2732d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
27331da177e4SLinus Torvalds 
27344c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
273515e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
27367bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
27371da177e4SLinus Torvalds 	if (err < 0) {
2738ab364a6fSThomas Graf 		kfree_skb(skb);
2739ab364a6fSThomas Graf 		goto errout;
27401da177e4SLinus Torvalds 	}
27411da177e4SLinus Torvalds 
274215e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2743ab364a6fSThomas Graf errout:
27441da177e4SLinus Torvalds 	return err;
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
274786872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
27481da177e4SLinus Torvalds {
27491da177e4SLinus Torvalds 	struct sk_buff *skb;
27505578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2751528c4cebSDenis V. Lunev 	u32 seq;
2752528c4cebSDenis V. Lunev 	int err;
27530d51aa80SJamal Hadi Salim 
2754528c4cebSDenis V. Lunev 	err = -ENOBUFS;
275538308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
275686872cb5SThomas Graf 
2757339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
275838308473SDavid S. Miller 	if (!skb)
275921713ebcSThomas Graf 		goto errout;
27601da177e4SLinus Torvalds 
2761191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
276215e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
276326932566SPatrick McHardy 	if (err < 0) {
276426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
276526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
276626932566SPatrick McHardy 		kfree_skb(skb);
276726932566SPatrick McHardy 		goto errout;
276826932566SPatrick McHardy 	}
276915e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
27705578689aSDaniel Lezcano 		    info->nlh, gfp_any());
27711ce85fe4SPablo Neira Ayuso 	return;
277221713ebcSThomas Graf errout:
277321713ebcSThomas Graf 	if (err < 0)
27745578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27778ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2778351638e7SJiri Pirko 				unsigned long event, void *ptr)
27798ed67789SDaniel Lezcano {
2780351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2781c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27828ed67789SDaniel Lezcano 
27838ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2784d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27858ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
27868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2787d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
27888ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2789d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27908ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27918ed67789SDaniel Lezcano #endif
27928ed67789SDaniel Lezcano 	}
27938ed67789SDaniel Lezcano 
27948ed67789SDaniel Lezcano 	return NOTIFY_OK;
27958ed67789SDaniel Lezcano }
27968ed67789SDaniel Lezcano 
27971da177e4SLinus Torvalds /*
27981da177e4SLinus Torvalds  *	/proc
27991da177e4SLinus Torvalds  */
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28021da177e4SLinus Torvalds 
280333120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
280433120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
280533120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
280633120b30SAlexey Dobriyan 	.read		= seq_read,
280733120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2808*8d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
280933120b30SAlexey Dobriyan };
281033120b30SAlexey Dobriyan 
28111da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
28121da177e4SLinus Torvalds {
281369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
28141da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
281569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
281669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
281769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
281869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
281969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2820fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
282169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	return 0;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28271da177e4SLinus Torvalds {
2828de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
282969ddb805SDaniel Lezcano }
283069ddb805SDaniel Lezcano 
28319a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28321da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28331da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28341da177e4SLinus Torvalds 	.read	 = seq_read,
28351da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2836b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28371da177e4SLinus Torvalds };
28381da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds static
2843fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
28441da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28451da177e4SLinus Torvalds {
2846c486da34SLucian Adrian Grijincu 	struct net *net;
2847c486da34SLucian Adrian Grijincu 	int delay;
2848c486da34SLucian Adrian Grijincu 	if (!write)
2849c486da34SLucian Adrian Grijincu 		return -EINVAL;
2850c486da34SLucian Adrian Grijincu 
2851c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2852c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28538d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28542ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
28551da177e4SLinus Torvalds 	return 0;
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
2858fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
28591da177e4SLinus Torvalds 	{
28601da177e4SLinus Torvalds 		.procname	=	"flush",
28614990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28621da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
286389c8b3a1SDave Jones 		.mode		=	0200,
28646d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28651da177e4SLinus Torvalds 	},
28661da177e4SLinus Torvalds 	{
28671da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28689a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28691da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28701da177e4SLinus Torvalds 		.mode		=	0644,
28716d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28721da177e4SLinus Torvalds 	},
28731da177e4SLinus Torvalds 	{
28741da177e4SLinus Torvalds 		.procname	=	"max_size",
28754990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28761da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28771da177e4SLinus Torvalds 		.mode		=	0644,
28786d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28791da177e4SLinus Torvalds 	},
28801da177e4SLinus Torvalds 	{
28811da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28824990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28831da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28841da177e4SLinus Torvalds 		.mode		=	0644,
28856d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28861da177e4SLinus Torvalds 	},
28871da177e4SLinus Torvalds 	{
28881da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28894990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28901da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28911da177e4SLinus Torvalds 		.mode		=	0644,
28926d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28931da177e4SLinus Torvalds 	},
28941da177e4SLinus Torvalds 	{
28951da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28964990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28971da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28981da177e4SLinus Torvalds 		.mode		=	0644,
28996d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29001da177e4SLinus Torvalds 	},
29011da177e4SLinus Torvalds 	{
29021da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
29034990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
29041da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29051da177e4SLinus Torvalds 		.mode		=	0644,
2906f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29071da177e4SLinus Torvalds 	},
29081da177e4SLinus Torvalds 	{
29091da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
29104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
29111da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29121da177e4SLinus Torvalds 		.mode		=	0644,
29136d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29141da177e4SLinus Torvalds 	},
29151da177e4SLinus Torvalds 	{
29161da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
29174990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
29181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29191da177e4SLinus Torvalds 		.mode		=	0644,
2920f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29211da177e4SLinus Torvalds 	},
29221da177e4SLinus Torvalds 	{
29231da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29261da177e4SLinus Torvalds 		.mode		=	0644,
29276d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29281da177e4SLinus Torvalds 	},
2929f8572d8fSEric W. Biederman 	{ }
29301da177e4SLinus Torvalds };
29311da177e4SLinus Torvalds 
29322c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2933760f2d01SDaniel Lezcano {
2934760f2d01SDaniel Lezcano 	struct ctl_table *table;
2935760f2d01SDaniel Lezcano 
2936760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2937760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2938760f2d01SDaniel Lezcano 			GFP_KERNEL);
29395ee09105SYOSHIFUJI Hideaki 
29405ee09105SYOSHIFUJI Hideaki 	if (table) {
29415ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2942c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
294386393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29445ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29455ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29465ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
29475ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
29485ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
29495ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
29505ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
29519c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2952464dc801SEric W. Biederman 
2953464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
2954464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
2955464dc801SEric W. Biederman 			table[0].procname = NULL;
29565ee09105SYOSHIFUJI Hideaki 	}
29575ee09105SYOSHIFUJI Hideaki 
2958760f2d01SDaniel Lezcano 	return table;
2959760f2d01SDaniel Lezcano }
29601da177e4SLinus Torvalds #endif
29611da177e4SLinus Torvalds 
29622c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2963cdb18761SDaniel Lezcano {
2964633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29658ed67789SDaniel Lezcano 
296686393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
296786393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2968f2fc6a54SBenjamin Thery 
2969fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2970fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2971fc66f95cSEric Dumazet 
29728ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29738ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29748ed67789SDaniel Lezcano 					   GFP_KERNEL);
29758ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2976fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2977d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29788ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2979d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
298062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
298162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29828ed67789SDaniel Lezcano 
29838ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29848ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29858ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29868ed67789SDaniel Lezcano 					       GFP_KERNEL);
298768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
298868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2989d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29908ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2991d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
299262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
299362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29948ed67789SDaniel Lezcano 
29958ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29968ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29978ed67789SDaniel Lezcano 					       GFP_KERNEL);
299868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
299968fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3000d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
30018ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3002d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
300362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
300462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30058ed67789SDaniel Lezcano #endif
30068ed67789SDaniel Lezcano 
3007b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3008b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3009b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3010b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3011b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3012b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3013b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3014b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3015b339a47cSPeter Zijlstra 
30166891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
30176891a346SBenjamin Thery 
30188ed67789SDaniel Lezcano 	ret = 0;
30198ed67789SDaniel Lezcano out:
30208ed67789SDaniel Lezcano 	return ret;
3021f2fc6a54SBenjamin Thery 
302268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
302368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
302468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
302568fffc67SPeter Zijlstra out_ip6_null_entry:
302668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
302768fffc67SPeter Zijlstra #endif
3028fc66f95cSEric Dumazet out_ip6_dst_entries:
3029fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3030f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3031f2fc6a54SBenjamin Thery 	goto out;
3032cdb18761SDaniel Lezcano }
3033cdb18761SDaniel Lezcano 
30342c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3035cdb18761SDaniel Lezcano {
30368ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30378ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30388ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30398ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30408ed67789SDaniel Lezcano #endif
304141bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3042cdb18761SDaniel Lezcano }
3043cdb18761SDaniel Lezcano 
3044d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3045d189634eSThomas Graf {
3046d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3047d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3048d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3049d189634eSThomas Graf #endif
3050d189634eSThomas Graf 	return 0;
3051d189634eSThomas Graf }
3052d189634eSThomas Graf 
3053d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3054d189634eSThomas Graf {
3055d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3056ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3057ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3058d189634eSThomas Graf #endif
3059d189634eSThomas Graf }
3060d189634eSThomas Graf 
3061cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3062cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3063cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3064cdb18761SDaniel Lezcano };
3065cdb18761SDaniel Lezcano 
3066c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3067c3426b47SDavid S. Miller {
3068c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3069c3426b47SDavid S. Miller 
3070c3426b47SDavid S. Miller 	if (!bp)
3071c3426b47SDavid S. Miller 		return -ENOMEM;
3072c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3073c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3074c3426b47SDavid S. Miller 	return 0;
3075c3426b47SDavid S. Miller }
3076c3426b47SDavid S. Miller 
3077c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3078c3426b47SDavid S. Miller {
3079c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3080c3426b47SDavid S. Miller 
3081c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
308256a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3083c3426b47SDavid S. Miller 	kfree(bp);
3084c3426b47SDavid S. Miller }
3085c3426b47SDavid S. Miller 
30862b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3087c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3088c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3089c3426b47SDavid S. Miller };
3090c3426b47SDavid S. Miller 
3091d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3092d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3093d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3094d189634eSThomas Graf };
3095d189634eSThomas Graf 
30968ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30978ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30988ed67789SDaniel Lezcano 	.priority = 0,
30998ed67789SDaniel Lezcano };
31008ed67789SDaniel Lezcano 
3101433d49c3SDaniel Lezcano int __init ip6_route_init(void)
31021da177e4SLinus Torvalds {
3103433d49c3SDaniel Lezcano 	int ret;
3104433d49c3SDaniel Lezcano 
31059a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
31069a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
31079a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
31089a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
31099a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3110c19a28e1SFernando Carrijo 		goto out;
311114e50e57SDavid S. Miller 
3112fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
31138ed67789SDaniel Lezcano 	if (ret)
3114bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3115bdb3289fSDaniel Lezcano 
3116c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3117c3426b47SDavid S. Miller 	if (ret)
3118e8803b6cSDavid S. Miller 		goto out_dst_entries;
31192a0c451aSThomas Graf 
31207e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
31217e52b33bSDavid S. Miller 	if (ret)
31227e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3123c3426b47SDavid S. Miller 
31245dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31255dc121e9SArnaud Ebalard 
31268ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31278ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31288ed67789SDaniel Lezcano 	 * manually for init_net */
3129d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31308ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3131bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3132d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31338ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3134d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31358ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3136bdb3289fSDaniel Lezcano   #endif
3137e8803b6cSDavid S. Miller 	ret = fib6_init();
3138433d49c3SDaniel Lezcano 	if (ret)
31398ed67789SDaniel Lezcano 		goto out_register_subsys;
3140433d49c3SDaniel Lezcano 
3141433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3142433d49c3SDaniel Lezcano 	if (ret)
3143e8803b6cSDavid S. Miller 		goto out_fib6_init;
3144c35b7e72SDaniel Lezcano 
3145433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3146433d49c3SDaniel Lezcano 	if (ret)
3147433d49c3SDaniel Lezcano 		goto xfrm6_init;
31487e5449c2SDaniel Lezcano 
3149d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3150d189634eSThomas Graf 	if (ret)
3151d189634eSThomas Graf 		goto fib6_rules_init;
3152d189634eSThomas Graf 
3153433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3154c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3155c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3156c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3157d189634eSThomas Graf 		goto out_register_late_subsys;
3158433d49c3SDaniel Lezcano 
31598ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3160cdb18761SDaniel Lezcano 	if (ret)
3161d189634eSThomas Graf 		goto out_register_late_subsys;
31628ed67789SDaniel Lezcano 
3163433d49c3SDaniel Lezcano out:
3164433d49c3SDaniel Lezcano 	return ret;
3165433d49c3SDaniel Lezcano 
3166d189634eSThomas Graf out_register_late_subsys:
3167d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3168433d49c3SDaniel Lezcano fib6_rules_init:
3169433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3170433d49c3SDaniel Lezcano xfrm6_init:
3171433d49c3SDaniel Lezcano 	xfrm6_fini();
31722a0c451aSThomas Graf out_fib6_init:
31732a0c451aSThomas Graf 	fib6_gc_cleanup();
31748ed67789SDaniel Lezcano out_register_subsys:
31758ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
31767e52b33bSDavid S. Miller out_register_inetpeer:
31777e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3178fc66f95cSEric Dumazet out_dst_entries:
3179fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3180433d49c3SDaniel Lezcano out_kmem_cache:
3181f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3182433d49c3SDaniel Lezcano 	goto out;
31831da177e4SLinus Torvalds }
31841da177e4SLinus Torvalds 
31851da177e4SLinus Torvalds void ip6_route_cleanup(void)
31861da177e4SLinus Torvalds {
31878ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3188d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3189101367c2SThomas Graf 	fib6_rules_cleanup();
31901da177e4SLinus Torvalds 	xfrm6_fini();
31911da177e4SLinus Torvalds 	fib6_gc_cleanup();
3192c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
31938ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
319441bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3195f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
31961da177e4SLinus Torvalds }
3197