xref: /openbmc/linux/net/ipv6/route.c (revision 41bb78b4b9adb21cf2c395b6b880aaae99c788b7)
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 
274fc268d2SRandy Dunlap #include <linux/capability.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/times.h>
311da177e4SLinus Torvalds #include <linux/socket.h>
321da177e4SLinus Torvalds #include <linux/sockios.h>
331da177e4SLinus Torvalds #include <linux/net.h>
341da177e4SLinus Torvalds #include <linux/route.h>
351da177e4SLinus Torvalds #include <linux/netdevice.h>
361da177e4SLinus Torvalds #include <linux/in6.h>
377bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
381da177e4SLinus Torvalds #include <linux/init.h>
391da177e4SLinus Torvalds #include <linux/if_arp.h>
401da177e4SLinus Torvalds #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/seq_file.h>
425b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
435a0e3ad6STejun Heo #include <linux/slab.h>
44457c4cbcSEric W. Biederman #include <net/net_namespace.h>
451da177e4SLinus Torvalds #include <net/snmp.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/ip6_fib.h>
481da177e4SLinus Torvalds #include <net/ip6_route.h>
491da177e4SLinus Torvalds #include <net/ndisc.h>
501da177e4SLinus Torvalds #include <net/addrconf.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
521da177e4SLinus Torvalds #include <linux/rtnetlink.h>
531da177e4SLinus Torvalds #include <net/dst.h>
541da177e4SLinus Torvalds #include <net/xfrm.h>
558d71740cSTom Tucker #include <net/netevent.h>
5621713ebcSThomas Graf #include <net/netlink.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <asm/uaccess.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
611da177e4SLinus Torvalds #include <linux/sysctl.h>
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Set to 3 to get tracing. */
651da177e4SLinus Torvalds #define RT6_DEBUG 2
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #if RT6_DEBUG >= 3
681da177e4SLinus Torvalds #define RDBG(x) printk x
691da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
701da177e4SLinus Torvalds #else
711da177e4SLinus Torvalds #define RDBG(x)
721da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds 
75519fbd87SYOSHIFUJI Hideaki #define CLONE_OFFLINK_ROUTE 0
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
781da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
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);
881da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
891da177e4SLinus Torvalds 
9070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
91efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
92efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9370ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
9470ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
95efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
96efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9770ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex);
9870ceb4f5SYOSHIFUJI Hideaki #endif
9970ceb4f5SYOSHIFUJI Hideaki 
1009a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1011da177e4SLinus Torvalds 	.family			=	AF_INET6,
10209640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1031da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1041da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1051da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1061da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1071da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1081da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1091da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1101da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1111ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds 
11414e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
11514e50e57SDavid S. Miller {
11614e50e57SDavid S. Miller }
11714e50e57SDavid S. Miller 
11814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
11914e50e57SDavid S. Miller 	.family			=	AF_INET6,
12009640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
12114e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
12214e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
12314e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
12414e50e57SDavid S. Miller };
12514e50e57SDavid S. Miller 
126bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1271da177e4SLinus Torvalds 	.dst = {
1281da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1291da177e4SLinus Torvalds 		.__use		= 1,
1301da177e4SLinus Torvalds 		.obsolete	= -1,
1311da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1321da177e4SLinus Torvalds 		.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1331da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1341da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1351da177e4SLinus Torvalds 	},
1361da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1374f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
1381da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1391da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
142101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
143101367c2SThomas Graf 
1446723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
1456723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
1466723ab54SDavid S. Miller 
147280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
148101367c2SThomas Graf 	.dst = {
149101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
150101367c2SThomas Graf 		.__use		= 1,
151101367c2SThomas Graf 		.obsolete	= -1,
152101367c2SThomas Graf 		.error		= -EACCES,
153101367c2SThomas Graf 		.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1549ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
1559ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
156101367c2SThomas Graf 	},
157101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1584f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
159101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
160101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
161101367c2SThomas Graf };
162101367c2SThomas Graf 
163bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
164101367c2SThomas Graf 	.dst = {
165101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
166101367c2SThomas Graf 		.__use		= 1,
167101367c2SThomas Graf 		.obsolete	= -1,
168101367c2SThomas Graf 		.error		= -EINVAL,
169101367c2SThomas Graf 		.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
170352e512cSHerbert Xu 		.input		= dst_discard,
171352e512cSHerbert Xu 		.output		= dst_discard,
172101367c2SThomas Graf 	},
173101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1744f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
175101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
176101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
177101367c2SThomas Graf };
178101367c2SThomas Graf 
179101367c2SThomas Graf #endif
180101367c2SThomas Graf 
1811da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
182f2fc6a54SBenjamin Thery static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
1831da177e4SLinus Torvalds {
184f2fc6a54SBenjamin Thery 	return (struct rt6_info *)dst_alloc(ops);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
1901da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	if (idev != NULL) {
1931da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
1941da177e4SLinus Torvalds 		in6_dev_put(idev);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
1991da177e4SLinus Torvalds 			   int how)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2021da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2035a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
204c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2051da177e4SLinus Torvalds 
2065a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
2075a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
2085a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
2091da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
2101da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
2111da177e4SLinus Torvalds 			in6_dev_put(idev);
2121da177e4SLinus Torvalds 		}
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
2171da177e4SLinus Torvalds {
218a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
219a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
222c71099acSThomas Graf static inline int rt6_need_strict(struct in6_addr *daddr)
223c71099acSThomas Graf {
224a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
225a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
226c71099acSThomas Graf }
227c71099acSThomas Graf 
2281da177e4SLinus Torvalds /*
229c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
2301da177e4SLinus Torvalds  */
2311da177e4SLinus Torvalds 
2328ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
2338ed67789SDaniel Lezcano 						    struct rt6_info *rt,
234dd3abc4eSYOSHIFUJI Hideaki 						    struct in6_addr *saddr,
2351da177e4SLinus Torvalds 						    int oif,
236d420895eSYOSHIFUJI Hideaki 						    int flags)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
2391da177e4SLinus Torvalds 	struct rt6_info *sprt;
2401da177e4SLinus Torvalds 
241dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
242dd3abc4eSYOSHIFUJI Hideaki 		goto out;
243dd3abc4eSYOSHIFUJI Hideaki 
244d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
2451da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
246dd3abc4eSYOSHIFUJI Hideaki 
247dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
2481da177e4SLinus Torvalds 			if (dev->ifindex == oif)
2491da177e4SLinus Torvalds 				return sprt;
2501da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
2511da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
2521da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
253d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
2541da177e4SLinus Torvalds 						continue;
2551da177e4SLinus Torvalds 					if (local && (!oif ||
2561da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
2571da177e4SLinus Torvalds 						continue;
2581da177e4SLinus Torvalds 				}
2591da177e4SLinus Torvalds 				local = sprt;
2601da177e4SLinus Torvalds 			}
261dd3abc4eSYOSHIFUJI Hideaki 		} else {
262dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
263dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
264dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
265dd3abc4eSYOSHIFUJI Hideaki 		}
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 
268dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
2691da177e4SLinus Torvalds 		if (local)
2701da177e4SLinus Torvalds 			return local;
2711da177e4SLinus Torvalds 
272d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
2738ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
2741da177e4SLinus Torvalds 	}
275dd3abc4eSYOSHIFUJI Hideaki out:
2761da177e4SLinus Torvalds 	return rt;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
27927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
28027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
28127097255SYOSHIFUJI Hideaki {
28227097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
28327097255SYOSHIFUJI Hideaki 	/*
28427097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
28527097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
28627097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
28727097255SYOSHIFUJI Hideaki 	 *
28827097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
28927097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
29027097255SYOSHIFUJI Hideaki 	 */
29127097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
29227097255SYOSHIFUJI Hideaki 		return;
29327097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
29427097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
29552e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
29627097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
29727097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
29827097255SYOSHIFUJI Hideaki 
29927097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
30027097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
30127097255SYOSHIFUJI Hideaki 
30227097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
30327097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
30427097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
30527097255SYOSHIFUJI Hideaki 	} else
30627097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
30727097255SYOSHIFUJI Hideaki }
30827097255SYOSHIFUJI Hideaki #else
30927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
31027097255SYOSHIFUJI Hideaki {
31127097255SYOSHIFUJI Hideaki }
31227097255SYOSHIFUJI Hideaki #endif
31327097255SYOSHIFUJI Hideaki 
3141da177e4SLinus Torvalds /*
315554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3161da177e4SLinus Torvalds  */
317b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
3181da177e4SLinus Torvalds {
319554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
320161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
321554cfb7eSYOSHIFUJI Hideaki 		return 2;
322161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
323161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
324161980f4SDavid S. Miller 		return 1;
325554cfb7eSYOSHIFUJI Hideaki 	return 0;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
3291da177e4SLinus Torvalds {
330554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
331398bcbebSYOSHIFUJI Hideaki 	int m;
3324d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
3334d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
3344d0c5911SYOSHIFUJI Hideaki 		m = 1;
3354d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
3361da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
337554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
3384d0c5911SYOSHIFUJI Hideaki 			m = 2;
339398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
340398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
341398bcbebSYOSHIFUJI Hideaki 			m = 0;
342398bcbebSYOSHIFUJI Hideaki #endif
343398bcbebSYOSHIFUJI Hideaki 		else
344ea73ee23SYOSHIFUJI Hideaki 			m = 1;
3451da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
346398bcbebSYOSHIFUJI Hideaki 	} else
347398bcbebSYOSHIFUJI Hideaki 		m = 0;
348554cfb7eSYOSHIFUJI Hideaki 	return m;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
352554cfb7eSYOSHIFUJI Hideaki 			   int strict)
353554cfb7eSYOSHIFUJI Hideaki {
3544d0c5911SYOSHIFUJI Hideaki 	int m, n;
3554d0c5911SYOSHIFUJI Hideaki 
3564d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
35777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
358554cfb7eSYOSHIFUJI Hideaki 		return -1;
359ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
360ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
361ebacaaa0SYOSHIFUJI Hideaki #endif
3624d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
363557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
364554cfb7eSYOSHIFUJI Hideaki 		return -1;
365554cfb7eSYOSHIFUJI Hideaki 	return m;
366554cfb7eSYOSHIFUJI Hideaki }
367554cfb7eSYOSHIFUJI Hideaki 
368f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
369f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
370554cfb7eSYOSHIFUJI Hideaki {
371554cfb7eSYOSHIFUJI Hideaki 	int m;
372554cfb7eSYOSHIFUJI Hideaki 
373554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
374f11e6659SDavid S. Miller 		goto out;
375554cfb7eSYOSHIFUJI Hideaki 
376554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
377554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
378f11e6659SDavid S. Miller 		goto out;
379554cfb7eSYOSHIFUJI Hideaki 
380f11e6659SDavid S. Miller 	if (m > *mpri) {
381ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
38227097255SYOSHIFUJI Hideaki 			rt6_probe(match);
383f11e6659SDavid S. Miller 		*mpri = m;
384554cfb7eSYOSHIFUJI Hideaki 		match = rt;
385ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
38627097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
3871da177e4SLinus Torvalds 	}
388f11e6659SDavid S. Miller 
389f11e6659SDavid S. Miller out:
390f11e6659SDavid S. Miller 	return match;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
394f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
395f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
396f11e6659SDavid S. Miller {
397f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
398f11e6659SDavid S. Miller 	int mpri = -1;
399f11e6659SDavid S. Miller 
400f11e6659SDavid S. Miller 	match = NULL;
401f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
402d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
403f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
404f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
405d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
406f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
407f11e6659SDavid S. Miller 
408f11e6659SDavid S. Miller 	return match;
409f11e6659SDavid S. Miller }
410f11e6659SDavid S. Miller 
411f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
412f11e6659SDavid S. Miller {
413f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
4148ed67789SDaniel Lezcano 	struct net *net;
415f11e6659SDavid S. Miller 
416f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
4170dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
418f11e6659SDavid S. Miller 
419f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
420f11e6659SDavid S. Miller 	if (!rt0)
421f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
422f11e6659SDavid S. Miller 
423f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
424f11e6659SDavid S. Miller 
425554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
426f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
427d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
428f11e6659SDavid S. Miller 
429554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
430f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
431f11e6659SDavid S. Miller 			next = fn->leaf;
432f11e6659SDavid S. Miller 
433f11e6659SDavid S. Miller 		if (next != rt0)
434f11e6659SDavid S. Miller 			fn->rr_ptr = next;
435554cfb7eSYOSHIFUJI Hideaki 	}
436554cfb7eSYOSHIFUJI Hideaki 
437f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
4380dc47877SHarvey Harrison 		  __func__, match);
439554cfb7eSYOSHIFUJI Hideaki 
440c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
441a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
44470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
44570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
44670ceb4f5SYOSHIFUJI Hideaki 		  struct in6_addr *gwaddr)
44770ceb4f5SYOSHIFUJI Hideaki {
448c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
44970ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
45070ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
45170ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
4524bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
45370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
45470ceb4f5SYOSHIFUJI Hideaki 
45570ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
45670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45770ceb4f5SYOSHIFUJI Hideaki 	}
45870ceb4f5SYOSHIFUJI Hideaki 
45970ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
46070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
46170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
46270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
46370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
46470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
46570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
46670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
46770ceb4f5SYOSHIFUJI Hideaki 		}
46870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
46970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
47070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
47170ceb4f5SYOSHIFUJI Hideaki 		}
47270ceb4f5SYOSHIFUJI Hideaki 	}
47370ceb4f5SYOSHIFUJI Hideaki 
47470ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
47570ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
4763933fc95SJens Rosenboom 		return -EINVAL;
47770ceb4f5SYOSHIFUJI Hideaki 
4784bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
47970ceb4f5SYOSHIFUJI Hideaki 
48070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
48170ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
48270ceb4f5SYOSHIFUJI Hideaki 	else {
48370ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
48470ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
48570ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
48670ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
48770ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
48870ceb4f5SYOSHIFUJI Hideaki 	}
48970ceb4f5SYOSHIFUJI Hideaki 
490efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
491efa2cea0SDaniel Lezcano 				dev->ifindex);
49270ceb4f5SYOSHIFUJI Hideaki 
49370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
494e0a1ad73SThomas Graf 		ip6_del_rt(rt);
49570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
49670ceb4f5SYOSHIFUJI Hideaki 	}
49770ceb4f5SYOSHIFUJI Hideaki 
49870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
499efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
50070ceb4f5SYOSHIFUJI Hideaki 					pref);
50170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
50270ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
50370ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
50470ceb4f5SYOSHIFUJI Hideaki 
50570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
5064bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
50770ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
50870ceb4f5SYOSHIFUJI Hideaki 		} else {
50970ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
51070ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
51170ceb4f5SYOSHIFUJI Hideaki 		}
512d8d1f30bSChangli Gao 		dst_release(&rt->dst);
51370ceb4f5SYOSHIFUJI Hideaki 	}
51470ceb4f5SYOSHIFUJI Hideaki 	return 0;
51570ceb4f5SYOSHIFUJI Hideaki }
51670ceb4f5SYOSHIFUJI Hideaki #endif
51770ceb4f5SYOSHIFUJI Hideaki 
5188ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
519982f56f3SYOSHIFUJI Hideaki do { \
5208ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
521982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
522e0eda7bbSVille Nuorvala 		while (1) { \
523982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
524c71099acSThomas Graf 				goto out; \
525982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
526982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
5278bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
528982f56f3SYOSHIFUJI Hideaki 			else \
529982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
530c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
531c71099acSThomas Graf 				goto restart; \
532c71099acSThomas Graf 		} \
533982f56f3SYOSHIFUJI Hideaki 	} \
534982f56f3SYOSHIFUJI Hideaki } while(0)
535c71099acSThomas Graf 
5368ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
5378ed67789SDaniel Lezcano 					     struct fib6_table *table,
538c71099acSThomas Graf 					     struct flowi *fl, int flags)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct fib6_node *fn;
5411da177e4SLinus Torvalds 	struct rt6_info *rt;
5421da177e4SLinus Torvalds 
543c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
544c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
545c71099acSThomas Graf restart:
546c71099acSThomas Graf 	rt = fn->leaf;
547dd3abc4eSYOSHIFUJI Hideaki 	rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
5488ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
549c71099acSThomas Graf out:
550d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
551c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
5521da177e4SLinus Torvalds 	return rt;
553c71099acSThomas Graf 
554c71099acSThomas Graf }
555c71099acSThomas Graf 
5569acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
5579acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
558c71099acSThomas Graf {
559c71099acSThomas Graf 	struct flowi fl = {
560c71099acSThomas Graf 		.oif = oif,
561c71099acSThomas Graf 		.nl_u = {
562c71099acSThomas Graf 			.ip6_u = {
563c71099acSThomas Graf 				.daddr = *daddr,
564c71099acSThomas Graf 			},
565c71099acSThomas Graf 		},
566c71099acSThomas Graf 	};
567c71099acSThomas Graf 	struct dst_entry *dst;
56877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
569c71099acSThomas Graf 
570adaa70bbSThomas Graf 	if (saddr) {
571adaa70bbSThomas Graf 		memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
572adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
573adaa70bbSThomas Graf 	}
574adaa70bbSThomas Graf 
575606a2b48SDaniel Lezcano 	dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
576c71099acSThomas Graf 	if (dst->error == 0)
577c71099acSThomas Graf 		return (struct rt6_info *) dst;
578c71099acSThomas Graf 
579c71099acSThomas Graf 	dst_release(dst);
580c71099acSThomas Graf 
5811da177e4SLinus Torvalds 	return NULL;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5847159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
5857159039aSYOSHIFUJI Hideaki 
586c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
5871da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
5881da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
5891da177e4SLinus Torvalds    be destroyed.
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds 
59286872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	int err;
595c71099acSThomas Graf 	struct fib6_table *table;
5961da177e4SLinus Torvalds 
597c71099acSThomas Graf 	table = rt->rt6i_table;
598c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
59986872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
600c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	return err;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
60540e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
60640e22e8fSThomas Graf {
6074d1169c1SDenis V. Lunev 	struct nl_info info = {
608c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
6094d1169c1SDenis V. Lunev 	};
610528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
61140e22e8fSThomas Graf }
61240e22e8fSThomas Graf 
61395a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
61495a9a5baSYOSHIFUJI Hideaki 				      struct in6_addr *saddr)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct rt6_info *rt;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/*
6191da177e4SLinus Torvalds 	 *	Clone the route.
6201da177e4SLinus Torvalds 	 */
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (rt) {
62514deae41SDavid S. Miller 		struct neighbour *neigh;
62614deae41SDavid S. Miller 		int attempts = !in_softirq();
62714deae41SDavid S. Miller 
62858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
62958c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
63058c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
63158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
6321da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
63358c4fb86SYOSHIFUJI Hideaki 		}
6341da177e4SLinus Torvalds 
63558c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
6361da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
6371da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
638d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
6411da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
6421da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
6431da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
6441da177e4SLinus Torvalds 		}
6451da177e4SLinus Torvalds #endif
6461da177e4SLinus Torvalds 
64714deae41SDavid S. Miller 	retry:
64814deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
64914deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
65014deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
65114deae41SDavid S. Miller 			int saved_rt_min_interval =
65214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
65314deae41SDavid S. Miller 			int saved_rt_elasticity =
65414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
65514deae41SDavid S. Miller 
65614deae41SDavid S. Miller 			if (attempts-- > 0) {
65714deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
65814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
65914deae41SDavid S. Miller 
66086393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
66114deae41SDavid S. Miller 
66214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
66314deae41SDavid S. Miller 					saved_rt_elasticity;
66414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
66514deae41SDavid S. Miller 					saved_rt_min_interval;
66614deae41SDavid S. Miller 				goto retry;
66714deae41SDavid S. Miller 			}
66814deae41SDavid S. Miller 
66914deae41SDavid S. Miller 			if (net_ratelimit())
67014deae41SDavid S. Miller 				printk(KERN_WARNING
6717e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
672d8d1f30bSChangli Gao 			dst_free(&rt->dst);
67314deae41SDavid S. Miller 			return NULL;
67414deae41SDavid S. Miller 		}
67514deae41SDavid S. Miller 		rt->rt6i_nexthop = neigh;
6761da177e4SLinus Torvalds 
67795a9a5baSYOSHIFUJI Hideaki 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	return rt;
6801da177e4SLinus Torvalds }
68195a9a5baSYOSHIFUJI Hideaki 
682299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
683299d9939SYOSHIFUJI Hideaki {
684299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
685299d9939SYOSHIFUJI Hideaki 	if (rt) {
686299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
687299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
688299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
689d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
690299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
691299d9939SYOSHIFUJI Hideaki 	}
692299d9939SYOSHIFUJI Hideaki 	return rt;
693299d9939SYOSHIFUJI Hideaki }
694299d9939SYOSHIFUJI Hideaki 
6958ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
6968ce11e6aSAdrian Bunk 				      struct flowi *fl, int flags)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct fib6_node *fn;
699519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
700c71099acSThomas Graf 	int strict = 0;
7011da177e4SLinus Torvalds 	int attempts = 3;
702519fbd87SYOSHIFUJI Hideaki 	int err;
70353b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
7041da177e4SLinus Torvalds 
70577d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds relookup:
708c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7091da177e4SLinus Torvalds 
7108238dd06SYOSHIFUJI Hideaki restart_2:
711c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds restart:
7144acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
7158ed67789SDaniel Lezcano 
7168ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
7178ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
7188238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
7191da177e4SLinus Torvalds 		goto out;
7201da177e4SLinus Torvalds 
721d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
722c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7231da177e4SLinus Torvalds 
724519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
725e40cf353SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
726519fbd87SYOSHIFUJI Hideaki 	else {
727519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE
728519fbd87SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
729519fbd87SYOSHIFUJI Hideaki #else
730519fbd87SYOSHIFUJI Hideaki 		goto out2;
731519fbd87SYOSHIFUJI Hideaki #endif
732519fbd87SYOSHIFUJI Hideaki 	}
7331da177e4SLinus Torvalds 
734d8d1f30bSChangli Gao 	dst_release(&rt->dst);
7358ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
7361da177e4SLinus Torvalds 
737d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
738e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
73940e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
740e40cf353SYOSHIFUJI Hideaki 		if (!err)
741e40cf353SYOSHIFUJI Hideaki 			goto out2;
742e40cf353SYOSHIFUJI Hideaki 	}
743e40cf353SYOSHIFUJI Hideaki 
744e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
7451da177e4SLinus Torvalds 		goto out2;
7461da177e4SLinus Torvalds 
747519fbd87SYOSHIFUJI Hideaki 	/*
748c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
749519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
7501da177e4SLinus Torvalds 	 */
751d8d1f30bSChangli Gao 	dst_release(&rt->dst);
7521da177e4SLinus Torvalds 	goto relookup;
753e40cf353SYOSHIFUJI Hideaki 
754519fbd87SYOSHIFUJI Hideaki out:
7558238dd06SYOSHIFUJI Hideaki 	if (reachable) {
7568238dd06SYOSHIFUJI Hideaki 		reachable = 0;
7578238dd06SYOSHIFUJI Hideaki 		goto restart_2;
7588238dd06SYOSHIFUJI Hideaki 	}
759d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
760c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7611da177e4SLinus Torvalds out2:
762d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
763d8d1f30bSChangli Gao 	rt->dst.__use++;
764c71099acSThomas Graf 
765c71099acSThomas Graf 	return rt;
766c71099acSThomas Graf }
767c71099acSThomas Graf 
7688ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
7694acad72dSPavel Emelyanov 					    struct flowi *fl, int flags)
7704acad72dSPavel Emelyanov {
7718ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->iif, fl, flags);
7724acad72dSPavel Emelyanov }
7734acad72dSPavel Emelyanov 
774c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
775c71099acSThomas Graf {
7760660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
777c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
778adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
779c71099acSThomas Graf 	struct flowi fl = {
780c71099acSThomas Graf 		.iif = skb->dev->ifindex,
781c71099acSThomas Graf 		.nl_u = {
782c71099acSThomas Graf 			.ip6_u = {
783c71099acSThomas Graf 				.daddr = iph->daddr,
784c71099acSThomas Graf 				.saddr = iph->saddr,
78590bcaf7bSAl Viro 				.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
786c71099acSThomas Graf 			},
787c71099acSThomas Graf 		},
78847dcf0cbSThomas Graf 		.mark = skb->mark,
789c71099acSThomas Graf 		.proto = iph->nexthdr,
790c71099acSThomas Graf 	};
791adaa70bbSThomas Graf 
7921d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
793adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
794c71099acSThomas Graf 
795adf30907SEric Dumazet 	skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input));
796c71099acSThomas Graf }
797c71099acSThomas Graf 
7988ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
799c71099acSThomas Graf 					     struct flowi *fl, int flags)
800c71099acSThomas Graf {
8018ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->oif, fl, flags);
802c71099acSThomas Graf }
803c71099acSThomas Graf 
8044591db4fSDaniel Lezcano struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
8054591db4fSDaniel Lezcano 				    struct flowi *fl)
806c71099acSThomas Graf {
807c71099acSThomas Graf 	int flags = 0;
808c71099acSThomas Graf 
8096057fd78SBrian Haley 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst))
81077d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
811c71099acSThomas Graf 
812adaa70bbSThomas Graf 	if (!ipv6_addr_any(&fl->fl6_src))
813adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
8140c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
8150c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
816adaa70bbSThomas Graf 
8174591db4fSDaniel Lezcano 	return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8207159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
8211da177e4SLinus Torvalds 
82214e50e57SDavid S. Miller int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
82314e50e57SDavid S. Miller {
82414e50e57SDavid S. Miller 	struct rt6_info *ort = (struct rt6_info *) *dstp;
82514e50e57SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)
82614e50e57SDavid S. Miller 		dst_alloc(&ip6_dst_blackhole_ops);
82714e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
82814e50e57SDavid S. Miller 
82914e50e57SDavid S. Miller 	if (rt) {
830d8d1f30bSChangli Gao 		new = &rt->dst;
83114e50e57SDavid S. Miller 
83214e50e57SDavid S. Miller 		atomic_set(&new->__refcnt, 1);
83314e50e57SDavid S. Miller 		new->__use = 1;
834352e512cSHerbert Xu 		new->input = dst_discard;
835352e512cSHerbert Xu 		new->output = dst_discard;
83614e50e57SDavid S. Miller 
837d8d1f30bSChangli Gao 		memcpy(new->metrics, ort->dst.metrics, RTAX_MAX*sizeof(u32));
838d8d1f30bSChangli Gao 		new->dev = ort->dst.dev;
83914e50e57SDavid S. Miller 		if (new->dev)
84014e50e57SDavid S. Miller 			dev_hold(new->dev);
84114e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
84214e50e57SDavid S. Miller 		if (rt->rt6i_idev)
84314e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
84414e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
84514e50e57SDavid S. Miller 
84614e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
84714e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
84814e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
84914e50e57SDavid S. Miller 
85014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
85114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
85214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
85314e50e57SDavid S. Miller #endif
85414e50e57SDavid S. Miller 
85514e50e57SDavid S. Miller 		dst_free(new);
85614e50e57SDavid S. Miller 	}
85714e50e57SDavid S. Miller 
85814e50e57SDavid S. Miller 	dst_release(*dstp);
85914e50e57SDavid S. Miller 	*dstp = new;
860a02cec21SEric Dumazet 	return new ? 0 : -ENOMEM;
86114e50e57SDavid S. Miller }
86214e50e57SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
86314e50e57SDavid S. Miller 
8641da177e4SLinus Torvalds /*
8651da177e4SLinus Torvalds  *	Destination cache support functions
8661da177e4SLinus Torvalds  */
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	struct rt6_info *rt;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
8731da177e4SLinus Torvalds 
87410414444SHerbert Xu 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
8751da177e4SLinus Torvalds 		return dst;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	return NULL;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (rt) {
88554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
88654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
887e0a1ad73SThomas Graf 				ip6_del_rt(rt);
88854c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
8891da177e4SLinus Torvalds 			}
89054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
89154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
89254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
89354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
89454c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
89554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	struct rt6_info *rt;
9011da177e4SLinus Torvalds 
9023ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9031da177e4SLinus Torvalds 
904adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9051da177e4SLinus Torvalds 	if (rt) {
9061da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
907d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
9081da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
9091da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
9101da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
9191da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
9201da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
9211da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
9221da177e4SLinus Torvalds 			dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
9231da177e4SLinus Torvalds 		}
9241da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
9258d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev);
9301da177e4SLinus Torvalds 
9315578689aSDaniel Lezcano static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
9341da177e4SLinus Torvalds 
9355578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
9365578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/*
9391da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
9401da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
9411da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
9421da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
9431da177e4SLinus Torvalds 	 */
9441da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
9451da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
9461da177e4SLinus Torvalds 	return mtu;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9493b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
9503b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
9515d0bbeebSThomas Graf 
9523b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
9531da177e4SLinus Torvalds 				  struct neighbour *neigh,
9549acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	struct rt6_info *rt;
9571da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
958c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
9611da177e4SLinus Torvalds 		return NULL;
9621da177e4SLinus Torvalds 
96386393e52SAlexey Dobriyan 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
9641da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
9651da177e4SLinus Torvalds 		in6_dev_put(idev);
9661da177e4SLinus Torvalds 		goto out;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	dev_hold(dev);
9701da177e4SLinus Torvalds 	if (neigh)
9711da177e4SLinus Torvalds 		neigh_hold(neigh);
97214deae41SDavid S. Miller 	else {
9731da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
97414deae41SDavid S. Miller 		if (IS_ERR(neigh))
97514deae41SDavid S. Miller 			neigh = NULL;
97614deae41SDavid S. Miller 	}
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	rt->rt6i_dev	  = dev;
9791da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
9801da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
981d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
982d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_HOPLIMIT-1] = 255;
983d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
984d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->dst));
985d8d1f30bSChangli Gao 	rt->dst.output  = ip6_output;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
988d8d1f30bSChangli Gao 	rt->dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
9891da177e4SLinus Torvalds 				? DST_HOST
9901da177e4SLinus Torvalds 				: 0;
9911da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
9921da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
9931da177e4SLinus Torvalds #endif
9941da177e4SLinus Torvalds 
9953b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
996d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
997d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
9983b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
9991da177e4SLinus Torvalds 
10005578689aSDaniel Lezcano 	fib6_force_start_gc(net);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds out:
1003d8d1f30bSChangli Gao 	return &rt->dst;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10063d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct dst_entry *dst, *next, **pprev;
10093d0f24a7SStephen Hemminger 	int more = 0;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	next = NULL;
10125d0bbeebSThomas Graf 
10133b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
10143b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
10155d0bbeebSThomas Graf 
10161da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
10171da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
10181da177e4SLinus Torvalds 			*pprev = dst->next;
10191da177e4SLinus Torvalds 			dst_free(dst);
10201da177e4SLinus Torvalds 		} else {
10211da177e4SLinus Torvalds 			pprev = &dst->next;
10223d0f24a7SStephen Hemminger 			++more;
10231da177e4SLinus Torvalds 		}
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10263b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10275d0bbeebSThomas Graf 
10283d0f24a7SStephen Hemminger 	return more;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
10321e493d19SDavid S. Miller 			    void *arg)
10331e493d19SDavid S. Miller {
10341e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
10351e493d19SDavid S. Miller 
10361e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
10371e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
10381e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
10391e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
10401e493d19SDavid S. Miller 		if (func(rt, arg)) {
10411e493d19SDavid S. Miller 			*pprev = dst->next;
10421e493d19SDavid S. Miller 			dst_free(dst);
10431e493d19SDavid S. Miller 		} else {
10441e493d19SDavid S. Miller 			pprev = &dst->next;
10451e493d19SDavid S. Miller 		}
10461e493d19SDavid S. Miller 	}
10471e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
10481e493d19SDavid S. Miller }
10491e493d19SDavid S. Miller 
1050569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
10511da177e4SLinus Torvalds {
10521da177e4SLinus Torvalds 	unsigned long now = jiffies;
105386393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
10547019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
10557019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
10567019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
10577019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
10587019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1059fc66f95cSEric Dumazet 	int entries;
10601da177e4SLinus Torvalds 
1061fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
10627019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1063fc66f95cSEric Dumazet 	    entries <= rt_max_size)
10641da177e4SLinus Torvalds 		goto out;
10651da177e4SLinus Torvalds 
10666891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
10676891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
10686891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1069fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1070fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
10717019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
10721da177e4SLinus Torvalds out:
10737019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1074fc66f95cSEric Dumazet 	return entries > rt_max_size;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
10781da177e4SLinus Torvalds    but results in cleaner routing tables.
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds    Remove it only when all the things will work!
10811da177e4SLinus Torvalds  */
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	int mtu = IPV6_MIN_MTU;
10861da177e4SLinus Torvalds 	struct inet6_dev *idev;
10871da177e4SLinus Torvalds 
1088c68f24ccSEric Dumazet 	rcu_read_lock();
1089c68f24ccSEric Dumazet 	idev = __in6_dev_get(dev);
1090c68f24ccSEric Dumazet 	if (idev)
10911da177e4SLinus Torvalds 		mtu = idev->cnf.mtu6;
1092c68f24ccSEric Dumazet 	rcu_read_unlock();
10931da177e4SLinus Torvalds 	return mtu;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
10966b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
10971da177e4SLinus Torvalds {
10986b75d090SYOSHIFUJI Hideaki 	int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
10996b75d090SYOSHIFUJI Hideaki 	if (hoplimit < 0) {
11006b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1101c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1102c68f24ccSEric Dumazet 
1103c68f24ccSEric Dumazet 		rcu_read_lock();
1104c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1105c68f24ccSEric Dumazet 		if (idev)
11061da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1107c68f24ccSEric Dumazet 		else
110853b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1109c68f24ccSEric Dumazet 		rcu_read_unlock();
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 	return hoplimit;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds /*
11151da177e4SLinus Torvalds  *
11161da177e4SLinus Torvalds  */
11171da177e4SLinus Torvalds 
111886872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	int err;
11215578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
11221da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
11231da177e4SLinus Torvalds 	struct net_device *dev = NULL;
11241da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1125c71099acSThomas Graf 	struct fib6_table *table;
11261da177e4SLinus Torvalds 	int addr_type;
11271da177e4SLinus Torvalds 
112886872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
11291da177e4SLinus Torvalds 		return -EINVAL;
11301da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
113186872cb5SThomas Graf 	if (cfg->fc_src_len)
11321da177e4SLinus Torvalds 		return -EINVAL;
11331da177e4SLinus Torvalds #endif
113486872cb5SThomas Graf 	if (cfg->fc_ifindex) {
11351da177e4SLinus Torvalds 		err = -ENODEV;
11365578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
11371da177e4SLinus Torvalds 		if (!dev)
11381da177e4SLinus Torvalds 			goto out;
11391da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
11401da177e4SLinus Torvalds 		if (!idev)
11411da177e4SLinus Torvalds 			goto out;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
114486872cb5SThomas Graf 	if (cfg->fc_metric == 0)
114586872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
11461da177e4SLinus Torvalds 
11475578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1148c71099acSThomas Graf 	if (table == NULL) {
1149c71099acSThomas Graf 		err = -ENOBUFS;
1150c71099acSThomas Graf 		goto out;
1151c71099acSThomas Graf 	}
1152c71099acSThomas Graf 
115386393e52SAlexey Dobriyan 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	if (rt == NULL) {
11561da177e4SLinus Torvalds 		err = -ENOMEM;
11571da177e4SLinus Torvalds 		goto out;
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds 
1160d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
11616f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
11626f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
11636f704992SYOSHIFUJI Hideaki 				0;
11641da177e4SLinus Torvalds 
116586872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
116686872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
116786872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
116886872cb5SThomas Graf 
116986872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1172d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1173ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1174ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
11751da177e4SLinus Torvalds 	else
1176d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
11771da177e4SLinus Torvalds 
1178d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
11791da177e4SLinus Torvalds 
118086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
118186872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
11821da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
1183d8d1f30bSChangli Gao 	       rt->dst.flags = DST_HOST;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
118686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
118786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
11881da177e4SLinus Torvalds #endif
11891da177e4SLinus Torvalds 
119086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
11931da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
11941da177e4SLinus Torvalds 	 */
119586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1196ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1197ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
11981da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
11995578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
12001da177e4SLinus Torvalds 			if (dev) {
12011da177e4SLinus Torvalds 				dev_put(dev);
12021da177e4SLinus Torvalds 				in6_dev_put(idev);
12031da177e4SLinus Torvalds 			}
12045578689aSDaniel Lezcano 			dev = net->loopback_dev;
12051da177e4SLinus Torvalds 			dev_hold(dev);
12061da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
12071da177e4SLinus Torvalds 			if (!idev) {
12081da177e4SLinus Torvalds 				err = -ENODEV;
12091da177e4SLinus Torvalds 				goto out;
12101da177e4SLinus Torvalds 			}
12111da177e4SLinus Torvalds 		}
1212d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1213d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1214d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
12151da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
12161da177e4SLinus Torvalds 		goto install_route;
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 
121986872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
12201da177e4SLinus Torvalds 		struct in6_addr *gw_addr;
12211da177e4SLinus Torvalds 		int gwa_type;
12221da177e4SLinus Torvalds 
122386872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
122486872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
12251da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
12281da177e4SLinus Torvalds 			struct rt6_info *grt;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
12311da177e4SLinus Torvalds 			   addresses as nexthop address.
12321da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
12331da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
12341da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
12351da177e4SLinus Torvalds 			   some exceptions. --ANK
12361da177e4SLinus Torvalds 			 */
12371da177e4SLinus Torvalds 			err = -EINVAL;
12381da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
12391da177e4SLinus Torvalds 				goto out;
12401da177e4SLinus Torvalds 
12415578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
12441da177e4SLinus Torvalds 			if (grt == NULL)
12451da177e4SLinus Torvalds 				goto out;
12461da177e4SLinus Torvalds 			if (dev) {
12471da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1248d8d1f30bSChangli Gao 					dst_release(&grt->dst);
12491da177e4SLinus Torvalds 					goto out;
12501da177e4SLinus Torvalds 				}
12511da177e4SLinus Torvalds 			} else {
12521da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
12531da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
12541da177e4SLinus Torvalds 				dev_hold(dev);
12551da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
12561da177e4SLinus Torvalds 			}
12571da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
12581da177e4SLinus Torvalds 				err = 0;
1259d8d1f30bSChangli Gao 			dst_release(&grt->dst);
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 			if (err)
12621da177e4SLinus Torvalds 				goto out;
12631da177e4SLinus Torvalds 		}
12641da177e4SLinus Torvalds 		err = -EINVAL;
12651da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
12661da177e4SLinus Torvalds 			goto out;
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	err = -ENODEV;
12701da177e4SLinus Torvalds 	if (dev == NULL)
12711da177e4SLinus Torvalds 		goto out;
12721da177e4SLinus Torvalds 
127386872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
12741da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
12751da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
12761da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
12771da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
12781da177e4SLinus Torvalds 			goto out;
12791da177e4SLinus Torvalds 		}
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
128286872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds install_route:
128586872cb5SThomas Graf 	if (cfg->fc_mx) {
128686872cb5SThomas Graf 		struct nlattr *nla;
128786872cb5SThomas Graf 		int remaining;
12881da177e4SLinus Torvalds 
128986872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
12908f4c1f9bSThomas Graf 			int type = nla_type(nla);
129186872cb5SThomas Graf 
129286872cb5SThomas Graf 			if (type) {
129386872cb5SThomas Graf 				if (type > RTAX_MAX) {
12941da177e4SLinus Torvalds 					err = -EINVAL;
12951da177e4SLinus Torvalds 					goto out;
12961da177e4SLinus Torvalds 				}
129786872cb5SThomas Graf 
1298d8d1f30bSChangli Gao 				rt->dst.metrics[type - 1] = nla_get_u32(nla);
12991da177e4SLinus Torvalds 			}
13001da177e4SLinus Torvalds 		}
13011da177e4SLinus Torvalds 	}
13021da177e4SLinus Torvalds 
1303d8d1f30bSChangli Gao 	if (dst_metric(&rt->dst, RTAX_HOPLIMIT) == 0)
1304d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_HOPLIMIT-1] = -1;
1305d8d1f30bSChangli Gao 	if (!dst_mtu(&rt->dst))
1306d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
1307d8d1f30bSChangli Gao 	if (!dst_metric(&rt->dst, RTAX_ADVMSS))
1308d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->dst));
1309d8d1f30bSChangli Gao 	rt->dst.dev = dev;
13101da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1311c71099acSThomas Graf 	rt->rt6i_table = table;
131263152fc0SDaniel Lezcano 
1313c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
131463152fc0SDaniel Lezcano 
131586872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds out:
13181da177e4SLinus Torvalds 	if (dev)
13191da177e4SLinus Torvalds 		dev_put(dev);
13201da177e4SLinus Torvalds 	if (idev)
13211da177e4SLinus Torvalds 		in6_dev_put(idev);
13221da177e4SLinus Torvalds 	if (rt)
1323d8d1f30bSChangli Gao 		dst_free(&rt->dst);
13241da177e4SLinus Torvalds 	return err;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
132786872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	int err;
1330c71099acSThomas Graf 	struct fib6_table *table;
1331c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
13321da177e4SLinus Torvalds 
13338ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
13346c813a72SPatrick McHardy 		return -ENOENT;
13356c813a72SPatrick McHardy 
1336c71099acSThomas Graf 	table = rt->rt6i_table;
1337c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
13381da177e4SLinus Torvalds 
133986872cb5SThomas Graf 	err = fib6_del(rt, info);
1340d8d1f30bSChangli Gao 	dst_release(&rt->dst);
13411da177e4SLinus Torvalds 
1342c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	return err;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
1347e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1348e0a1ad73SThomas Graf {
13494d1169c1SDenis V. Lunev 	struct nl_info info = {
1350c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
13514d1169c1SDenis V. Lunev 	};
1352528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1353e0a1ad73SThomas Graf }
1354e0a1ad73SThomas Graf 
135586872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
13561da177e4SLinus Torvalds {
1357c71099acSThomas Graf 	struct fib6_table *table;
13581da177e4SLinus Torvalds 	struct fib6_node *fn;
13591da177e4SLinus Torvalds 	struct rt6_info *rt;
13601da177e4SLinus Torvalds 	int err = -ESRCH;
13611da177e4SLinus Torvalds 
13625578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1363c71099acSThomas Graf 	if (table == NULL)
1364c71099acSThomas Graf 		return err;
13651da177e4SLinus Torvalds 
1366c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1367c71099acSThomas Graf 
1368c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
136986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
137086872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	if (fn) {
1373d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
137486872cb5SThomas Graf 			if (cfg->fc_ifindex &&
13751da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
137686872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
13771da177e4SLinus Torvalds 				continue;
137886872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
137986872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
13801da177e4SLinus Torvalds 				continue;
138186872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
13821da177e4SLinus Torvalds 				continue;
1383d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1384c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
13851da177e4SLinus Torvalds 
138686872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
13871da177e4SLinus Torvalds 		}
13881da177e4SLinus Torvalds 	}
1389c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	return err;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /*
13951da177e4SLinus Torvalds  *	Handle redirects
13961da177e4SLinus Torvalds  */
1397a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
1398a6279458SYOSHIFUJI Hideaki 	struct flowi fl;
1399a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1400a6279458SYOSHIFUJI Hideaki };
14011da177e4SLinus Torvalds 
14028ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
14038ed67789SDaniel Lezcano 					     struct fib6_table *table,
1404a6279458SYOSHIFUJI Hideaki 					     struct flowi *fl,
1405a6279458SYOSHIFUJI Hideaki 					     int flags)
1406a6279458SYOSHIFUJI Hideaki {
1407a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1408a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1409a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1410c71099acSThomas Graf 
1411e843b9e1SYOSHIFUJI Hideaki 	/*
1412e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1413e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1414e843b9e1SYOSHIFUJI Hideaki 	 *
1415e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1416e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1417e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1418e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1419e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1420e843b9e1SYOSHIFUJI Hideaki 	 */
14211da177e4SLinus Torvalds 
1422c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1423a6279458SYOSHIFUJI Hideaki 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1424e843b9e1SYOSHIFUJI Hideaki restart:
1425d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
14261da177e4SLinus Torvalds 		/*
14271da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
14281da177e4SLinus Torvalds 		 *
14291da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
14301da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
14311da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
14321da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
14331da177e4SLinus Torvalds 		 */
1434e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1435e843b9e1SYOSHIFUJI Hideaki 			continue;
14361da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1437e843b9e1SYOSHIFUJI Hideaki 			continue;
1438a6279458SYOSHIFUJI Hideaki 		if (fl->oif != rt->rt6i_dev->ifindex)
1439e843b9e1SYOSHIFUJI Hideaki 			continue;
1440a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1441e843b9e1SYOSHIFUJI Hideaki 			continue;
1442e843b9e1SYOSHIFUJI Hideaki 		break;
1443e843b9e1SYOSHIFUJI Hideaki 	}
1444a6279458SYOSHIFUJI Hideaki 
1445cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
14468ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
14478ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
1448cb15d9c2SYOSHIFUJI Hideaki out:
1449d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1450a6279458SYOSHIFUJI Hideaki 
1451c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14521da177e4SLinus Torvalds 
1453a6279458SYOSHIFUJI Hideaki 	return rt;
1454a6279458SYOSHIFUJI Hideaki };
1455a6279458SYOSHIFUJI Hideaki 
1456a6279458SYOSHIFUJI Hideaki static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1457a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *src,
1458a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *gateway,
1459a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1460a6279458SYOSHIFUJI Hideaki {
1461adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1462c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1463a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
1464a6279458SYOSHIFUJI Hideaki 		.fl = {
1465a6279458SYOSHIFUJI Hideaki 			.oif = dev->ifindex,
1466a6279458SYOSHIFUJI Hideaki 			.nl_u = {
1467a6279458SYOSHIFUJI Hideaki 				.ip6_u = {
1468a6279458SYOSHIFUJI Hideaki 					.daddr = *dest,
1469a6279458SYOSHIFUJI Hideaki 					.saddr = *src,
1470a6279458SYOSHIFUJI Hideaki 				},
1471a6279458SYOSHIFUJI Hideaki 			},
1472a6279458SYOSHIFUJI Hideaki 		},
1473a6279458SYOSHIFUJI Hideaki 	};
1474adaa70bbSThomas Graf 
147586c36ce4SBrian Haley 	ipv6_addr_copy(&rdfl.gateway, gateway);
147686c36ce4SBrian Haley 
1477adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1478adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1479a6279458SYOSHIFUJI Hideaki 
14805578689aSDaniel Lezcano 	return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
148158f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1482a6279458SYOSHIFUJI Hideaki }
1483a6279458SYOSHIFUJI Hideaki 
1484a6279458SYOSHIFUJI Hideaki void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1485a6279458SYOSHIFUJI Hideaki 		  struct in6_addr *saddr,
1486a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1487a6279458SYOSHIFUJI Hideaki {
1488a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1489a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1490c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1491a6279458SYOSHIFUJI Hideaki 
1492a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1493a6279458SYOSHIFUJI Hideaki 
14948ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
14951da177e4SLinus Torvalds 		if (net_ratelimit())
14961da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
14971da177e4SLinus Torvalds 			       "for redirect target\n");
1498a6279458SYOSHIFUJI Hideaki 		goto out;
14991da177e4SLinus Torvalds 	}
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	/*
15021da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
15031da177e4SLinus Torvalds 	 */
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
15061da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
15071da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
15081da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
15091da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
15101da177e4SLinus Torvalds 		     );
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	/*
15131da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
15141da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
15151da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
15161da177e4SLinus Torvalds 	 */
1517d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1520d8d1f30bSChangli Gao 	if (neigh == rt->dst.neighbour)
15211da177e4SLinus Torvalds 		goto out;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
15241da177e4SLinus Torvalds 	if (nrt == NULL)
15251da177e4SLinus Torvalds 		goto out;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
15281da177e4SLinus Torvalds 	if (on_link)
15291da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
15321da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
1533d8d1f30bSChangli Gao 	nrt->dst.flags |= DST_HOST;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
15361da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
15371da177e4SLinus Torvalds 	/* Reset pmtu, it may be better */
1538d8d1f30bSChangli Gao 	nrt->dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
1539d8d1f30bSChangli Gao 	nrt->dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
1540d8d1f30bSChangli Gao 							dst_mtu(&nrt->dst));
15411da177e4SLinus Torvalds 
154240e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
15431da177e4SLinus Torvalds 		goto out;
15441da177e4SLinus Torvalds 
1545d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1546d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
15478d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
15488d71740cSTom Tucker 
15491da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1550e0a1ad73SThomas Graf 		ip6_del_rt(rt);
15511da177e4SLinus Torvalds 		return;
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds out:
1555d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds /*
15591da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
15601da177e4SLinus Torvalds  *	i.e. Path MTU discovery
15611da177e4SLinus Torvalds  */
15621da177e4SLinus Torvalds 
1563ae878ae2SMaciej Żenczykowski static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
1564ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
15671da177e4SLinus Torvalds 	int allfrag = 0;
15681da177e4SLinus Torvalds 
1569ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
15701da177e4SLinus Torvalds 	if (rt == NULL)
15711da177e4SLinus Torvalds 		return;
15721da177e4SLinus Torvalds 
1573d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
15741da177e4SLinus Torvalds 		goto out;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
15771da177e4SLinus Torvalds 		/*
15781da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
15791da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
15801da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
15811da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
15821da177e4SLinus Torvalds 		 */
15831da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
15841da177e4SLinus Torvalds 		allfrag = 1;
15851da177e4SLinus Torvalds 	}
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
15881da177e4SLinus Torvalds 	   They are sent only in response to data packets,
15891da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
15901da177e4SLinus Torvalds 	 */
1591d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
15941da177e4SLinus Torvalds 	   not to override it, but add new one, so that
15951da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
15961da177e4SLinus Torvalds 	   would return automatically.
15971da177e4SLinus Torvalds 	 */
15981da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1599d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_MTU-1] = pmtu;
16001da177e4SLinus Torvalds 		if (allfrag)
1601d8d1f30bSChangli Gao 			rt->dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1602d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16031da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
16041da177e4SLinus Torvalds 		goto out;
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	/* Network route.
16081da177e4SLinus Torvalds 	   Two cases are possible:
16091da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
16101da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
16111da177e4SLinus Torvalds 	 */
1612d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1613a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1614d5315b50SYOSHIFUJI Hideaki 	else
1615d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1616a1e78363SYOSHIFUJI Hideaki 
1617d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1618d8d1f30bSChangli Gao 		nrt->dst.metrics[RTAX_MTU-1] = pmtu;
16191da177e4SLinus Torvalds 		if (allfrag)
1620d8d1f30bSChangli Gao 			nrt->dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1621a1e78363SYOSHIFUJI Hideaki 
16221da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1623a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1624a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1625a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1626a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
16271da177e4SLinus Torvalds 		 */
1628d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16291da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1630a1e78363SYOSHIFUJI Hideaki 
163140e22e8fSThomas Graf 		ip6_ins_rt(nrt);
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds out:
1634d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
1637ae878ae2SMaciej Żenczykowski void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1638ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1639ae878ae2SMaciej Żenczykowski {
1640ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1641ae878ae2SMaciej Żenczykowski 
1642ae878ae2SMaciej Żenczykowski 	/*
1643ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1644ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1645ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1646ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1647ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1648ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1649ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1650ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1651ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1652ae878ae2SMaciej Żenczykowski 	 * interfaces.
1653ae878ae2SMaciej Żenczykowski 	 */
1654ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1655ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1656ae878ae2SMaciej Żenczykowski }
1657ae878ae2SMaciej Żenczykowski 
16581da177e4SLinus Torvalds /*
16591da177e4SLinus Torvalds  *	Misc support functions
16601da177e4SLinus Torvalds  */
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
16631da177e4SLinus Torvalds {
1664c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
166586393e52SAlexey Dobriyan 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	if (rt) {
1668d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1669d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
16701da177e4SLinus Torvalds 
1671d8d1f30bSChangli Gao 		memcpy(rt->dst.metrics, ort->dst.metrics, RTAX_MAX*sizeof(u32));
1672d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
1673d8d1f30bSChangli Gao 		rt->dst.dev = ort->dst.dev;
1674d8d1f30bSChangli Gao 		if (rt->dst.dev)
1675d8d1f30bSChangli Gao 			dev_hold(rt->dst.dev);
16761da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
16771da177e4SLinus Torvalds 		if (rt->rt6i_idev)
16781da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1679d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
16801da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
16831da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
16841da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
16871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
16881da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
16891da177e4SLinus Torvalds #endif
1690c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
16911da177e4SLinus Torvalds 	}
16921da177e4SLinus Torvalds 	return rt;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
169570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1696efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1697efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
169870ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex)
169970ceb4f5SYOSHIFUJI Hideaki {
170070ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
170170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1702c71099acSThomas Graf 	struct fib6_table *table;
170370ceb4f5SYOSHIFUJI Hideaki 
1704efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1705c71099acSThomas Graf 	if (table == NULL)
1706c71099acSThomas Graf 		return NULL;
1707c71099acSThomas Graf 
1708c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1709c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
171070ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
171170ceb4f5SYOSHIFUJI Hideaki 		goto out;
171270ceb4f5SYOSHIFUJI Hideaki 
1713d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
171470ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
171570ceb4f5SYOSHIFUJI Hideaki 			continue;
171670ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
171770ceb4f5SYOSHIFUJI Hideaki 			continue;
171870ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
171970ceb4f5SYOSHIFUJI Hideaki 			continue;
1720d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
172170ceb4f5SYOSHIFUJI Hideaki 		break;
172270ceb4f5SYOSHIFUJI Hideaki 	}
172370ceb4f5SYOSHIFUJI Hideaki out:
1724c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
172570ceb4f5SYOSHIFUJI Hideaki 	return rt;
172670ceb4f5SYOSHIFUJI Hideaki }
172770ceb4f5SYOSHIFUJI Hideaki 
1728efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1729efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
173070ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
173170ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
173270ceb4f5SYOSHIFUJI Hideaki {
173386872cb5SThomas Graf 	struct fib6_config cfg = {
173486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1735238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
173686872cb5SThomas Graf 		.fc_ifindex	= ifindex,
173786872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
173886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
173986872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1740efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1741efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1742efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
174386872cb5SThomas Graf 	};
174470ceb4f5SYOSHIFUJI Hideaki 
174586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
174686872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
174786872cb5SThomas Graf 
1748e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1749e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
175086872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
175170ceb4f5SYOSHIFUJI Hideaki 
175286872cb5SThomas Graf 	ip6_route_add(&cfg);
175370ceb4f5SYOSHIFUJI Hideaki 
1754efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
175570ceb4f5SYOSHIFUJI Hideaki }
175670ceb4f5SYOSHIFUJI Hideaki #endif
175770ceb4f5SYOSHIFUJI Hideaki 
17581da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
17591da177e4SLinus Torvalds {
17601da177e4SLinus Torvalds 	struct rt6_info *rt;
1761c71099acSThomas Graf 	struct fib6_table *table;
17621da177e4SLinus Torvalds 
1763c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1764c71099acSThomas Graf 	if (table == NULL)
1765c71099acSThomas Graf 		return NULL;
17661da177e4SLinus Torvalds 
1767c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1768d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
17691da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1770045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
17711da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
17721da177e4SLinus Torvalds 			break;
17731da177e4SLinus Torvalds 	}
17741da177e4SLinus Torvalds 	if (rt)
1775d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1776c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
17771da177e4SLinus Torvalds 	return rt;
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1781ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1782ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
17831da177e4SLinus Torvalds {
178486872cb5SThomas Graf 	struct fib6_config cfg = {
178586872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1786238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
178786872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
178886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
178986872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
17905578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
17915578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1792c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
179386872cb5SThomas Graf 	};
17941da177e4SLinus Torvalds 
179586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
17961da177e4SLinus Torvalds 
179786872cb5SThomas Graf 	ip6_route_add(&cfg);
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
18027b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
18031da177e4SLinus Torvalds {
18041da177e4SLinus Torvalds 	struct rt6_info *rt;
1805c71099acSThomas Graf 	struct fib6_table *table;
1806c71099acSThomas Graf 
1807c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
18087b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1809c71099acSThomas Graf 	if (table == NULL)
1810c71099acSThomas Graf 		return;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds restart:
1813c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1814d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
18151da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1816d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1817c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1818e0a1ad73SThomas Graf 			ip6_del_rt(rt);
18191da177e4SLinus Torvalds 			goto restart;
18201da177e4SLinus Torvalds 		}
18211da177e4SLinus Torvalds 	}
1822c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
18255578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
18265578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
182786872cb5SThomas Graf 				 struct fib6_config *cfg)
182886872cb5SThomas Graf {
182986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
183086872cb5SThomas Graf 
183186872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
183286872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
183386872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
183486872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
183586872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
183686872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
183786872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
183886872cb5SThomas Graf 
18395578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1840f1243c2dSBenjamin Thery 
184186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
184286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
184386872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
184486872cb5SThomas Graf }
184586872cb5SThomas Graf 
18465578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
18471da177e4SLinus Torvalds {
184886872cb5SThomas Graf 	struct fib6_config cfg;
18491da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
18501da177e4SLinus Torvalds 	int err;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	switch(cmd) {
18531da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
18541da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
18551da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
18561da177e4SLinus Torvalds 			return -EPERM;
18571da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
18581da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
18591da177e4SLinus Torvalds 		if (err)
18601da177e4SLinus Torvalds 			return -EFAULT;
18611da177e4SLinus Torvalds 
18625578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
186386872cb5SThomas Graf 
18641da177e4SLinus Torvalds 		rtnl_lock();
18651da177e4SLinus Torvalds 		switch (cmd) {
18661da177e4SLinus Torvalds 		case SIOCADDRT:
186786872cb5SThomas Graf 			err = ip6_route_add(&cfg);
18681da177e4SLinus Torvalds 			break;
18691da177e4SLinus Torvalds 		case SIOCDELRT:
187086872cb5SThomas Graf 			err = ip6_route_del(&cfg);
18711da177e4SLinus Torvalds 			break;
18721da177e4SLinus Torvalds 		default:
18731da177e4SLinus Torvalds 			err = -EINVAL;
18741da177e4SLinus Torvalds 		}
18751da177e4SLinus Torvalds 		rtnl_unlock();
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 		return err;
18783ff50b79SStephen Hemminger 	}
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	return -EINVAL;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /*
18841da177e4SLinus Torvalds  *	Drop the packet on the floor
18851da177e4SLinus Torvalds  */
18861da177e4SLinus Torvalds 
1887d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
18881da177e4SLinus Torvalds {
1889612f09e8SYOSHIFUJI Hideaki 	int type;
1890adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1891612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1892612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
18930660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
189445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
18953bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
18963bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1897612f09e8SYOSHIFUJI Hideaki 			break;
1898612f09e8SYOSHIFUJI Hideaki 		}
1899612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1900612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
19013bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19023bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
1903612f09e8SYOSHIFUJI Hideaki 		break;
1904612f09e8SYOSHIFUJI Hideaki 	}
19053ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
19061da177e4SLinus Torvalds 	kfree_skb(skb);
19071da177e4SLinus Torvalds 	return 0;
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
19109ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
19119ce8ade0SThomas Graf {
1912612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
19139ce8ade0SThomas Graf }
19149ce8ade0SThomas Graf 
191520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
19161da177e4SLinus Torvalds {
1917adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
1918612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19216723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
19226723ab54SDavid S. Miller 
19239ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
19249ce8ade0SThomas Graf {
1925612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
19269ce8ade0SThomas Graf }
19279ce8ade0SThomas Graf 
19289ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
19299ce8ade0SThomas Graf {
1930adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
1931612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
19329ce8ade0SThomas Graf }
19339ce8ade0SThomas Graf 
19346723ab54SDavid S. Miller #endif
19356723ab54SDavid S. Miller 
19361da177e4SLinus Torvalds /*
19371da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
19381da177e4SLinus Torvalds  */
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
19411da177e4SLinus Torvalds 				    const struct in6_addr *addr,
19421da177e4SLinus Torvalds 				    int anycast)
19431da177e4SLinus Torvalds {
1944c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
194586393e52SAlexey Dobriyan 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
194614deae41SDavid S. Miller 	struct neighbour *neigh;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	if (rt == NULL)
19491da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
19501da177e4SLinus Torvalds 
19515578689aSDaniel Lezcano 	dev_hold(net->loopback_dev);
19521da177e4SLinus Torvalds 	in6_dev_hold(idev);
19531da177e4SLinus Torvalds 
1954d8d1f30bSChangli Gao 	rt->dst.flags = DST_HOST;
1955d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
1956d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
19575578689aSDaniel Lezcano 	rt->rt6i_dev = net->loopback_dev;
19581da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1959d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
1960d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->dst));
1961d8d1f30bSChangli Gao 	rt->dst.metrics[RTAX_HOPLIMIT-1] = -1;
1962d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
196558c4fb86SYOSHIFUJI Hideaki 	if (anycast)
196658c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
196758c4fb86SYOSHIFUJI Hideaki 	else
19681da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
196914deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
197014deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
1971d8d1f30bSChangli Gao 		dst_free(&rt->dst);
197214deae41SDavid S. Miller 
197314deae41SDavid S. Miller 		/* We are casting this because that is the return
197414deae41SDavid S. Miller 		 * value type.  But an errno encoded pointer is the
197514deae41SDavid S. Miller 		 * same regardless of the underlying pointer type,
197614deae41SDavid S. Miller 		 * and that's what we are returning.  So this is OK.
197714deae41SDavid S. Miller 		 */
197814deae41SDavid S. Miller 		return (struct rt6_info *) neigh;
19791da177e4SLinus Torvalds 	}
198014deae41SDavid S. Miller 	rt->rt6i_nexthop = neigh;
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
19831da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
19845578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
19851da177e4SLinus Torvalds 
1986d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	return rt;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
19918ed67789SDaniel Lezcano struct arg_dev_net {
19928ed67789SDaniel Lezcano 	struct net_device *dev;
19938ed67789SDaniel Lezcano 	struct net *net;
19948ed67789SDaniel Lezcano };
19958ed67789SDaniel Lezcano 
19961da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
19971da177e4SLinus Torvalds {
19988ed67789SDaniel Lezcano 	struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
19998ed67789SDaniel Lezcano 	struct net *net = ((struct arg_dev_net *)arg)->net;
20008ed67789SDaniel Lezcano 
20018ed67789SDaniel Lezcano 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
20028ed67789SDaniel Lezcano 	    rt != net->ipv6.ip6_null_entry) {
20031da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
20041da177e4SLinus Torvalds 		return -1;
20051da177e4SLinus Torvalds 	}
20061da177e4SLinus Torvalds 	return 0;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
2009f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
20101da177e4SLinus Torvalds {
20118ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
20128ed67789SDaniel Lezcano 		.dev = dev,
20138ed67789SDaniel Lezcano 		.net = net,
20148ed67789SDaniel Lezcano 	};
20158ed67789SDaniel Lezcano 
20168ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
20171e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds struct rt6_mtu_change_arg
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	struct net_device *dev;
20231da177e4SLinus Torvalds 	unsigned mtu;
20241da177e4SLinus Torvalds };
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
20271da177e4SLinus Torvalds {
20281da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
20291da177e4SLinus Torvalds 	struct inet6_dev *idev;
2030c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(arg->dev);
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
20331da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
20341da177e4SLinus Torvalds 	   We still use this lock to block changes
20351da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
20361da177e4SLinus Torvalds 	*/
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
20391da177e4SLinus Torvalds 	if (idev == NULL)
20401da177e4SLinus Torvalds 		return 0;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
20431da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
20441da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
20451da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
20461da177e4SLinus Torvalds 	 */
20471da177e4SLinus Torvalds 	/*
20481da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
20491da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
20501da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
20511da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
20521da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
20531da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
20541da177e4SLinus Torvalds 	   PMTU discouvery.
20551da177e4SLinus Torvalds 	 */
20561da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2057d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2058d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2059d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2060d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2061d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_MTU-1] = arg->mtu;
2062d8d1f30bSChangli Gao 		rt->dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
2063566cfd8fSSimon Arlott 	}
20641da177e4SLinus Torvalds 	return 0;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
20681da177e4SLinus Torvalds {
2069c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2070c71099acSThomas Graf 		.dev = dev,
2071c71099acSThomas Graf 		.mtu = mtu,
2072c71099acSThomas Graf 	};
20731da177e4SLinus Torvalds 
2074c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
2077ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
20785176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
207986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2080ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
208186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
208286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
208386872cb5SThomas Graf };
208486872cb5SThomas Graf 
208586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
208686872cb5SThomas Graf 			      struct fib6_config *cfg)
20871da177e4SLinus Torvalds {
208886872cb5SThomas Graf 	struct rtmsg *rtm;
208986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
209086872cb5SThomas Graf 	int err;
20911da177e4SLinus Torvalds 
209286872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
209386872cb5SThomas Graf 	if (err < 0)
209486872cb5SThomas Graf 		goto errout;
20951da177e4SLinus Torvalds 
209686872cb5SThomas Graf 	err = -EINVAL;
209786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
209886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
209986872cb5SThomas Graf 
210086872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
210186872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
210286872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
210386872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
210486872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
210586872cb5SThomas Graf 
210686872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
210786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
210886872cb5SThomas Graf 
2109ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2110ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2111ab79ad14SMaciej Żenczykowski 
211286872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
211386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
21143b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
211586872cb5SThomas Graf 
211686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
211786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
211886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
21191da177e4SLinus Torvalds 	}
212086872cb5SThomas Graf 
212186872cb5SThomas Graf 	if (tb[RTA_DST]) {
212286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
212386872cb5SThomas Graf 
212486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
212586872cb5SThomas Graf 			goto errout;
212686872cb5SThomas Graf 
212786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
21281da177e4SLinus Torvalds 	}
212986872cb5SThomas Graf 
213086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
213186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
213286872cb5SThomas Graf 
213386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
213486872cb5SThomas Graf 			goto errout;
213586872cb5SThomas Graf 
213686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
21371da177e4SLinus Torvalds 	}
213886872cb5SThomas Graf 
213986872cb5SThomas Graf 	if (tb[RTA_OIF])
214086872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
214186872cb5SThomas Graf 
214286872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
214386872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
214486872cb5SThomas Graf 
214586872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
214686872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
214786872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
21481da177e4SLinus Torvalds 	}
214986872cb5SThomas Graf 
215086872cb5SThomas Graf 	if (tb[RTA_TABLE])
215186872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
215286872cb5SThomas Graf 
215386872cb5SThomas Graf 	err = 0;
215486872cb5SThomas Graf errout:
215586872cb5SThomas Graf 	return err;
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
2158c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
21591da177e4SLinus Torvalds {
216086872cb5SThomas Graf 	struct fib6_config cfg;
216186872cb5SThomas Graf 	int err;
21621da177e4SLinus Torvalds 
216386872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
216486872cb5SThomas Graf 	if (err < 0)
216586872cb5SThomas Graf 		return err;
216686872cb5SThomas Graf 
216786872cb5SThomas Graf 	return ip6_route_del(&cfg);
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
2170c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
21711da177e4SLinus Torvalds {
217286872cb5SThomas Graf 	struct fib6_config cfg;
217386872cb5SThomas Graf 	int err;
21741da177e4SLinus Torvalds 
217586872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
217686872cb5SThomas Graf 	if (err < 0)
217786872cb5SThomas Graf 		return err;
217886872cb5SThomas Graf 
217986872cb5SThomas Graf 	return ip6_route_add(&cfg);
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
2182339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2183339bf98fSThomas Graf {
2184339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2185339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2186339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2187339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2188339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2189339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2190339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2191339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2192339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
21936a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2194339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2195339bf98fSThomas Graf }
2196339bf98fSThomas Graf 
2197191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2198191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
21990d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
22000d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
22017bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct rtmsg *rtm;
22041da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2205e3703b3dSThomas Graf 	long expires;
22069e762a4aSPatrick McHardy 	u32 table;
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
22091da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
22101da177e4SLinus Torvalds 			/* success since this is not a prefix route */
22111da177e4SLinus Torvalds 			return 1;
22121da177e4SLinus Torvalds 		}
22131da177e4SLinus Torvalds 	}
22141da177e4SLinus Torvalds 
22152d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
22162d7202bfSThomas Graf 	if (nlh == NULL)
221726932566SPatrick McHardy 		return -EMSGSIZE;
22182d7202bfSThomas Graf 
22192d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
22201da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
22211da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
22221da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
22231da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2224c71099acSThomas Graf 	if (rt->rt6i_table)
22259e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2226c71099acSThomas Graf 	else
22279e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
22289e762a4aSPatrick McHardy 	rtm->rtm_table = table;
22292d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
22301da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
22311da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2232ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2233ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
22341da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
22351da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
22361da177e4SLinus Torvalds 	else
22371da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
22381da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
22391da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
22401da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
22411da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
22421da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
22431da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
22441da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
22451da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
22461da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
22491da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	if (dst) {
22522d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
22531da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
22541da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
22552d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
22561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
22571da177e4SLinus Torvalds 	if (src) {
22582d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
22591da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
22601da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
22612d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
22621da177e4SLinus Torvalds #endif
22637bc570c8SYOSHIFUJI Hideaki 	if (iif) {
22647bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
22657bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
22668229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
22677bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
22687bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
22697bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
22707bc570c8SYOSHIFUJI Hideaki 						return 0;
22717bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
22727bc570c8SYOSHIFUJI Hideaki 				} else {
22737bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
22747bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
22757bc570c8SYOSHIFUJI Hideaki 				}
22767bc570c8SYOSHIFUJI Hideaki 			}
22777bc570c8SYOSHIFUJI Hideaki 		} else
22787bc570c8SYOSHIFUJI Hideaki #endif
22792d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
22807bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
2281d8d1f30bSChangli Gao 		struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
22821da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2283191cd582SBrian Haley 		if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
22847cbca67cSYOSHIFUJI Hideaki 				       dst, 0, &saddr_buf) == 0)
22852d7202bfSThomas Graf 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
22861da177e4SLinus Torvalds 	}
22872d7202bfSThomas Graf 
2288d8d1f30bSChangli Gao 	if (rtnetlink_put_metrics(skb, rt->dst.metrics) < 0)
22892d7202bfSThomas Graf 		goto nla_put_failure;
22902d7202bfSThomas Graf 
2291d8d1f30bSChangli Gao 	if (rt->dst.neighbour)
2292d8d1f30bSChangli Gao 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
22932d7202bfSThomas Graf 
2294d8d1f30bSChangli Gao 	if (rt->dst.dev)
22952d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
22962d7202bfSThomas Graf 
22972d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2298e3703b3dSThomas Graf 
229936e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
230036e3deaeSYOSHIFUJI Hideaki 		expires = 0;
230136e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
230236e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
230336e3deaeSYOSHIFUJI Hideaki 	else
230436e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
230569cdf8f9SYOSHIFUJI Hideaki 
2306d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2307d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2308e3703b3dSThomas Graf 		goto nla_put_failure;
23091da177e4SLinus Torvalds 
23102d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
23112d7202bfSThomas Graf 
23122d7202bfSThomas Graf nla_put_failure:
231326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
231426932566SPatrick McHardy 	return -EMSGSIZE;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23171b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
23181da177e4SLinus Torvalds {
23191da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
23201da177e4SLinus Torvalds 	int prefix;
23211da177e4SLinus Torvalds 
23222d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
23232d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
23241da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
23251da177e4SLinus Torvalds 	} else
23261da177e4SLinus Torvalds 		prefix = 0;
23271da177e4SLinus Torvalds 
2328191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2329191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
23301da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
23317bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
2334c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
23351da177e4SLinus Torvalds {
23363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2337ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
23381da177e4SLinus Torvalds 	struct rt6_info *rt;
2339ab364a6fSThomas Graf 	struct sk_buff *skb;
2340ab364a6fSThomas Graf 	struct rtmsg *rtm;
2341ab364a6fSThomas Graf 	struct flowi fl;
2342ab364a6fSThomas Graf 	int err, iif = 0;
2343ab364a6fSThomas Graf 
2344ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2345ab364a6fSThomas Graf 	if (err < 0)
2346ab364a6fSThomas Graf 		goto errout;
2347ab364a6fSThomas Graf 
2348ab364a6fSThomas Graf 	err = -EINVAL;
2349ab364a6fSThomas Graf 	memset(&fl, 0, sizeof(fl));
2350ab364a6fSThomas Graf 
2351ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2352ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2353ab364a6fSThomas Graf 			goto errout;
2354ab364a6fSThomas Graf 
2355ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2356ab364a6fSThomas Graf 	}
2357ab364a6fSThomas Graf 
2358ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2359ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2360ab364a6fSThomas Graf 			goto errout;
2361ab364a6fSThomas Graf 
2362ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2363ab364a6fSThomas Graf 	}
2364ab364a6fSThomas Graf 
2365ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2366ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2367ab364a6fSThomas Graf 
2368ab364a6fSThomas Graf 	if (tb[RTA_OIF])
2369ab364a6fSThomas Graf 		fl.oif = nla_get_u32(tb[RTA_OIF]);
2370ab364a6fSThomas Graf 
2371ab364a6fSThomas Graf 	if (iif) {
2372ab364a6fSThomas Graf 		struct net_device *dev;
23735578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2374ab364a6fSThomas Graf 		if (!dev) {
2375ab364a6fSThomas Graf 			err = -ENODEV;
2376ab364a6fSThomas Graf 			goto errout;
2377ab364a6fSThomas Graf 		}
2378ab364a6fSThomas Graf 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2381ab364a6fSThomas Graf 	if (skb == NULL) {
2382ab364a6fSThomas Graf 		err = -ENOBUFS;
2383ab364a6fSThomas Graf 		goto errout;
2384ab364a6fSThomas Graf 	}
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
23871da177e4SLinus Torvalds 	   through good chunk of routing engine.
23881da177e4SLinus Torvalds 	 */
2389459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
23901da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
23911da177e4SLinus Torvalds 
23928a3edd80SDaniel Lezcano 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
2393d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
23941da177e4SLinus Torvalds 
2395191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
23961da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
23977bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
23981da177e4SLinus Torvalds 	if (err < 0) {
2399ab364a6fSThomas Graf 		kfree_skb(skb);
2400ab364a6fSThomas Graf 		goto errout;
24011da177e4SLinus Torvalds 	}
24021da177e4SLinus Torvalds 
24035578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2404ab364a6fSThomas Graf errout:
24051da177e4SLinus Torvalds 	return err;
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
240886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	struct sk_buff *skb;
24115578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2412528c4cebSDenis V. Lunev 	u32 seq;
2413528c4cebSDenis V. Lunev 	int err;
24140d51aa80SJamal Hadi Salim 
2415528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2416528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
241786872cb5SThomas Graf 
2418339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
241921713ebcSThomas Graf 	if (skb == NULL)
242021713ebcSThomas Graf 		goto errout;
24211da177e4SLinus Torvalds 
2422191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
24237bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
242426932566SPatrick McHardy 	if (err < 0) {
242526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
242626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
242726932566SPatrick McHardy 		kfree_skb(skb);
242826932566SPatrick McHardy 		goto errout;
242926932566SPatrick McHardy 	}
24301ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
24315578689aSDaniel Lezcano 		    info->nlh, gfp_any());
24321ce85fe4SPablo Neira Ayuso 	return;
243321713ebcSThomas Graf errout:
243421713ebcSThomas Graf 	if (err < 0)
24355578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
24388ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
24398ed67789SDaniel Lezcano 				unsigned long event, void *data)
24408ed67789SDaniel Lezcano {
24418ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2442c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
24438ed67789SDaniel Lezcano 
24448ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2445d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
24468ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
24478ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2448d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
24498ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2450d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
24518ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
24528ed67789SDaniel Lezcano #endif
24538ed67789SDaniel Lezcano 	}
24548ed67789SDaniel Lezcano 
24558ed67789SDaniel Lezcano 	return NOTIFY_OK;
24568ed67789SDaniel Lezcano }
24578ed67789SDaniel Lezcano 
24581da177e4SLinus Torvalds /*
24591da177e4SLinus Torvalds  *	/proc
24601da177e4SLinus Torvalds  */
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds struct rt6_proc_arg
24671da177e4SLinus Torvalds {
24681da177e4SLinus Torvalds 	char *buffer;
24691da177e4SLinus Torvalds 	int offset;
24701da177e4SLinus Torvalds 	int length;
24711da177e4SLinus Torvalds 	int skip;
24721da177e4SLinus Torvalds 	int len;
24731da177e4SLinus Torvalds };
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
24761da177e4SLinus Torvalds {
247733120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
24781da177e4SLinus Torvalds 
24794b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24824b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
24831da177e4SLinus Torvalds #else
248433120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
24851da177e4SLinus Torvalds #endif
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
24884b7a4274SHarvey Harrison 		seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
24891da177e4SLinus Torvalds 	} else {
249033120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
24911da177e4SLinus Torvalds 	}
249233120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2493d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2494d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
24951da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
24961da177e4SLinus Torvalds 	return 0;
24971da177e4SLinus Torvalds }
24981da177e4SLinus Torvalds 
249933120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
25001da177e4SLinus Torvalds {
2501f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2502f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
250333120b30SAlexey Dobriyan 	return 0;
25041da177e4SLinus Torvalds }
25051da177e4SLinus Torvalds 
250633120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
250733120b30SAlexey Dobriyan {
2508de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2509f3db4851SDaniel Lezcano }
2510f3db4851SDaniel Lezcano 
251133120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
251233120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
251333120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
251433120b30SAlexey Dobriyan 	.read		= seq_read,
251533120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2516b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
251733120b30SAlexey Dobriyan };
251833120b30SAlexey Dobriyan 
25191da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
25201da177e4SLinus Torvalds {
252169ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
25221da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
252369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
252469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
252569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
252669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
252769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2528fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
252969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	return 0;
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
25351da177e4SLinus Torvalds {
2536de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
253769ddb805SDaniel Lezcano }
253869ddb805SDaniel Lezcano 
25399a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
25401da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
25411da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
25421da177e4SLinus Torvalds 	.read	 = seq_read,
25431da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2544b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
25451da177e4SLinus Torvalds };
25461da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static
25518d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
25521da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
25531da177e4SLinus Torvalds {
25545b7c931dSDaniel Lezcano 	struct net *net = current->nsproxy->net_ns;
25555b7c931dSDaniel Lezcano 	int delay = net->ipv6.sysctl.flush_delay;
25561da177e4SLinus Torvalds 	if (write) {
25578d65af78SAlexey Dobriyan 		proc_dointvec(ctl, write, buffer, lenp, ppos);
25585b7c931dSDaniel Lezcano 		fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
25591da177e4SLinus Torvalds 		return 0;
25601da177e4SLinus Torvalds 	} else
25611da177e4SLinus Torvalds 		return -EINVAL;
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
2564760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
25651da177e4SLinus Torvalds 	{
25661da177e4SLinus Torvalds 		.procname	=	"flush",
25674990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
25681da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
256989c8b3a1SDave Jones 		.mode		=	0200,
25706d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
25711da177e4SLinus Torvalds 	},
25721da177e4SLinus Torvalds 	{
25731da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
25749a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
25751da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25761da177e4SLinus Torvalds 		.mode		=	0644,
25776d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
25781da177e4SLinus Torvalds 	},
25791da177e4SLinus Torvalds 	{
25801da177e4SLinus Torvalds 		.procname	=	"max_size",
25814990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
25821da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25831da177e4SLinus Torvalds 		.mode		=	0644,
25846d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
25851da177e4SLinus Torvalds 	},
25861da177e4SLinus Torvalds 	{
25871da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
25884990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
25891da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25901da177e4SLinus Torvalds 		.mode		=	0644,
25916d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
25921da177e4SLinus Torvalds 	},
25931da177e4SLinus Torvalds 	{
25941da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
25954990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
25961da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25971da177e4SLinus Torvalds 		.mode		=	0644,
25986d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
25991da177e4SLinus Torvalds 	},
26001da177e4SLinus Torvalds 	{
26011da177e4SLinus Torvalds 		.procname	=	"gc_interval",
26024990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
26031da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26041da177e4SLinus Torvalds 		.mode		=	0644,
26056d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26061da177e4SLinus Torvalds 	},
26071da177e4SLinus Torvalds 	{
26081da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
26094990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
26101da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26111da177e4SLinus Torvalds 		.mode		=	0644,
2612f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
26131da177e4SLinus Torvalds 	},
26141da177e4SLinus Torvalds 	{
26151da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
26164990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
26171da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26181da177e4SLinus Torvalds 		.mode		=	0644,
26196d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26201da177e4SLinus Torvalds 	},
26211da177e4SLinus Torvalds 	{
26221da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
26234990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
26241da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26251da177e4SLinus Torvalds 		.mode		=	0644,
2626f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
26271da177e4SLinus Torvalds 	},
26281da177e4SLinus Torvalds 	{
26291da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
26304990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
26311da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26321da177e4SLinus Torvalds 		.mode		=	0644,
26336d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
26341da177e4SLinus Torvalds 	},
2635f8572d8fSEric W. Biederman 	{ }
26361da177e4SLinus Torvalds };
26371da177e4SLinus Torvalds 
26382c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2639760f2d01SDaniel Lezcano {
2640760f2d01SDaniel Lezcano 	struct ctl_table *table;
2641760f2d01SDaniel Lezcano 
2642760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2643760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2644760f2d01SDaniel Lezcano 			GFP_KERNEL);
26455ee09105SYOSHIFUJI Hideaki 
26465ee09105SYOSHIFUJI Hideaki 	if (table) {
26475ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
264886393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
26495ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
26505ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
26515ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
26525ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
26535ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
26545ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
26555ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
26569c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
26575ee09105SYOSHIFUJI Hideaki 	}
26585ee09105SYOSHIFUJI Hideaki 
2659760f2d01SDaniel Lezcano 	return table;
2660760f2d01SDaniel Lezcano }
26611da177e4SLinus Torvalds #endif
26621da177e4SLinus Torvalds 
26632c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2664cdb18761SDaniel Lezcano {
2665633d424bSPavel Emelyanov 	int ret = -ENOMEM;
26668ed67789SDaniel Lezcano 
266786393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
266886393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2669f2fc6a54SBenjamin Thery 
2670fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2671fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2672fc66f95cSEric Dumazet 
26738ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
26748ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
26758ed67789SDaniel Lezcano 					   GFP_KERNEL);
26768ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2677fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2678d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
26798ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2680d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
26818ed67789SDaniel Lezcano 
26828ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
26838ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
26848ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
26858ed67789SDaniel Lezcano 					       GFP_KERNEL);
268668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
268768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2688d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
26898ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2690d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
26918ed67789SDaniel Lezcano 
26928ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
26938ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
26948ed67789SDaniel Lezcano 					       GFP_KERNEL);
269568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
269668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2697d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
26988ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2699d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
27008ed67789SDaniel Lezcano #endif
27018ed67789SDaniel Lezcano 
2702b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2703b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2704b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2705b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2706b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2707b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2708b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2709b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2710b339a47cSPeter Zijlstra 
2711cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2712cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2713cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2714cdb18761SDaniel Lezcano #endif
27156891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
27166891a346SBenjamin Thery 
27178ed67789SDaniel Lezcano 	ret = 0;
27188ed67789SDaniel Lezcano out:
27198ed67789SDaniel Lezcano 	return ret;
2720f2fc6a54SBenjamin Thery 
272168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
272268fffc67SPeter Zijlstra out_ip6_prohibit_entry:
272368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
272468fffc67SPeter Zijlstra out_ip6_null_entry:
272568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
272668fffc67SPeter Zijlstra #endif
2727fc66f95cSEric Dumazet out_ip6_dst_entries:
2728fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2729f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2730f2fc6a54SBenjamin Thery 	goto out;
2731cdb18761SDaniel Lezcano }
2732cdb18761SDaniel Lezcano 
27332c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2734cdb18761SDaniel Lezcano {
2735cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2736cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2737cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2738cdb18761SDaniel Lezcano #endif
27398ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
27408ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27418ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
27428ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
27438ed67789SDaniel Lezcano #endif
2744*41bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2745cdb18761SDaniel Lezcano }
2746cdb18761SDaniel Lezcano 
2747cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2748cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2749cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2750cdb18761SDaniel Lezcano };
2751cdb18761SDaniel Lezcano 
27528ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
27538ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
27548ed67789SDaniel Lezcano 	.priority = 0,
27558ed67789SDaniel Lezcano };
27568ed67789SDaniel Lezcano 
2757433d49c3SDaniel Lezcano int __init ip6_route_init(void)
27581da177e4SLinus Torvalds {
2759433d49c3SDaniel Lezcano 	int ret;
2760433d49c3SDaniel Lezcano 
27619a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
27629a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
27639a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
27649a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
27659a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2766c19a28e1SFernando Carrijo 		goto out;
276714e50e57SDavid S. Miller 
2768fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
27698ed67789SDaniel Lezcano 	if (ret)
2770bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2771bdb3289fSDaniel Lezcano 
2772fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2773fc66f95cSEric Dumazet 	if (ret)
2774fc66f95cSEric Dumazet 		goto out_dst_entries;
2775fc66f95cSEric Dumazet 
27765dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
27775dc121e9SArnaud Ebalard 
27788ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
27798ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
27808ed67789SDaniel Lezcano 	 * manually for init_net */
2781d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
27828ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2783bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2784d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
27858ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2786d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
27878ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2788bdb3289fSDaniel Lezcano   #endif
2789433d49c3SDaniel Lezcano 	ret = fib6_init();
2790433d49c3SDaniel Lezcano 	if (ret)
27918ed67789SDaniel Lezcano 		goto out_register_subsys;
2792433d49c3SDaniel Lezcano 
2793433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2794433d49c3SDaniel Lezcano 	if (ret)
2795cdb18761SDaniel Lezcano 		goto out_fib6_init;
2796c35b7e72SDaniel Lezcano 
2797433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2798433d49c3SDaniel Lezcano 	if (ret)
2799433d49c3SDaniel Lezcano 		goto xfrm6_init;
28007e5449c2SDaniel Lezcano 
2801433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2802433d49c3SDaniel Lezcano 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2803433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2804433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2805433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2806433d49c3SDaniel Lezcano 
28078ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2808cdb18761SDaniel Lezcano 	if (ret)
2809cdb18761SDaniel Lezcano 		goto fib6_rules_init;
28108ed67789SDaniel Lezcano 
2811433d49c3SDaniel Lezcano out:
2812433d49c3SDaniel Lezcano 	return ret;
2813433d49c3SDaniel Lezcano 
2814433d49c3SDaniel Lezcano fib6_rules_init:
2815433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2816433d49c3SDaniel Lezcano xfrm6_init:
2817433d49c3SDaniel Lezcano 	xfrm6_fini();
2818433d49c3SDaniel Lezcano out_fib6_init:
2819433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
28208ed67789SDaniel Lezcano out_register_subsys:
28218ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2822fc66f95cSEric Dumazet out_dst_entries:
2823fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2824433d49c3SDaniel Lezcano out_kmem_cache:
2825f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2826433d49c3SDaniel Lezcano 	goto out;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds void ip6_route_cleanup(void)
28301da177e4SLinus Torvalds {
28318ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2832101367c2SThomas Graf 	fib6_rules_cleanup();
28331da177e4SLinus Torvalds 	xfrm6_fini();
28341da177e4SLinus Torvalds 	fib6_gc_cleanup();
28358ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2836*41bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2837f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
28381da177e4SLinus Torvalds }
2839