xref: /openbmc/linux/net/ipv6/route.c (revision 2d7202bfdd28687073f5efef8d2f51bbab0af867)
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  *	$Id: route.c,v 1.56 2001/10/31 21:55:55 davem Exp $
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
191da177e4SLinus Torvalds  *		reworked default router selection.
201da177e4SLinus Torvalds  *		- respect outgoing interface
211da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
221da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
231da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
241da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
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>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/if_arp.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #ifdef 	CONFIG_PROC_FS
411da177e4SLinus Torvalds #include <linux/proc_fs.h>
421da177e4SLinus Torvalds #include <linux/seq_file.h>
431da177e4SLinus Torvalds #endif
441da177e4SLinus Torvalds 
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 
77554cfb7eSYOSHIFUJI Hideaki #define RT6_SELECT_F_IFACE	0x1
78554cfb7eSYOSHIFUJI Hideaki #define RT6_SELECT_F_REACHABLE	0x2
79554cfb7eSYOSHIFUJI Hideaki 
801da177e4SLinus Torvalds static int ip6_rt_max_size = 4096;
811da177e4SLinus Torvalds static int ip6_rt_gc_min_interval = HZ / 2;
821da177e4SLinus Torvalds static int ip6_rt_gc_timeout = 60*HZ;
831da177e4SLinus Torvalds int ip6_rt_gc_interval = 30*HZ;
841da177e4SLinus Torvalds static int ip6_rt_gc_elasticity = 9;
851da177e4SLinus Torvalds static int ip6_rt_mtu_expires = 10*60*HZ;
861da177e4SLinus Torvalds static int ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
891da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
921da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
931da177e4SLinus Torvalds 				       struct net_device *dev, int how);
941da177e4SLinus Torvalds static int		 ip6_dst_gc(void);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
971da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
981da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
991da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
1001da177e4SLinus Torvalds 
10170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
10270ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
10370ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
10470ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
10570ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
10670ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex);
10770ceb4f5SYOSHIFUJI Hideaki #endif
10870ceb4f5SYOSHIFUJI Hideaki 
1091da177e4SLinus Torvalds static struct dst_ops ip6_dst_ops = {
1101da177e4SLinus Torvalds 	.family			=	AF_INET6,
1111da177e4SLinus Torvalds 	.protocol		=	__constant_htons(ETH_P_IPV6),
1121da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1131da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1141da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1151da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1161da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1171da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1181da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1191da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1201da177e4SLinus Torvalds 	.entry_size		=	sizeof(struct rt6_info),
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds struct rt6_info ip6_null_entry = {
1241da177e4SLinus Torvalds 	.u = {
1251da177e4SLinus Torvalds 		.dst = {
1261da177e4SLinus Torvalds 			.__refcnt	= ATOMIC_INIT(1),
1271da177e4SLinus Torvalds 			.__use		= 1,
1281da177e4SLinus Torvalds 			.dev		= &loopback_dev,
1291da177e4SLinus Torvalds 			.obsolete	= -1,
1301da177e4SLinus Torvalds 			.error		= -ENETUNREACH,
1311da177e4SLinus Torvalds 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1321da177e4SLinus Torvalds 			.input		= ip6_pkt_discard,
1331da177e4SLinus Torvalds 			.output		= ip6_pkt_discard_out,
1341da177e4SLinus Torvalds 			.ops		= &ip6_dst_ops,
1351da177e4SLinus Torvalds 			.path		= (struct dst_entry*)&ip6_null_entry,
1361da177e4SLinus Torvalds 		}
1371da177e4SLinus Torvalds 	},
1381da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1391da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1401da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds 
143101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
144101367c2SThomas Graf 
145101367c2SThomas Graf struct rt6_info ip6_prohibit_entry = {
146101367c2SThomas Graf 	.u = {
147101367c2SThomas Graf 		.dst = {
148101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
149101367c2SThomas Graf 			.__use		= 1,
150101367c2SThomas Graf 			.dev		= &loopback_dev,
151101367c2SThomas Graf 			.obsolete	= -1,
152101367c2SThomas Graf 			.error		= -EACCES,
153101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
154101367c2SThomas Graf 			.input		= ip6_pkt_discard,
155101367c2SThomas Graf 			.output		= ip6_pkt_discard_out,
156101367c2SThomas Graf 			.ops		= &ip6_dst_ops,
157101367c2SThomas Graf 			.path		= (struct dst_entry*)&ip6_prohibit_entry,
158101367c2SThomas Graf 		}
159101367c2SThomas Graf 	},
160101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
161101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
162101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
163101367c2SThomas Graf };
164101367c2SThomas Graf 
165101367c2SThomas Graf struct rt6_info ip6_blk_hole_entry = {
166101367c2SThomas Graf 	.u = {
167101367c2SThomas Graf 		.dst = {
168101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
169101367c2SThomas Graf 			.__use		= 1,
170101367c2SThomas Graf 			.dev		= &loopback_dev,
171101367c2SThomas Graf 			.obsolete	= -1,
172101367c2SThomas Graf 			.error		= -EINVAL,
173101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
174101367c2SThomas Graf 			.input		= ip6_pkt_discard,
175101367c2SThomas Graf 			.output		= ip6_pkt_discard_out,
176101367c2SThomas Graf 			.ops		= &ip6_dst_ops,
177101367c2SThomas Graf 			.path		= (struct dst_entry*)&ip6_blk_hole_entry,
178101367c2SThomas Graf 		}
179101367c2SThomas Graf 	},
180101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
181101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
182101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
183101367c2SThomas Graf };
184101367c2SThomas Graf 
185101367c2SThomas Graf #endif
186101367c2SThomas Graf 
1871da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
1881da177e4SLinus Torvalds static __inline__ struct rt6_info *ip6_dst_alloc(void)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	return (struct rt6_info *)dst_alloc(&ip6_dst_ops);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
1961da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (idev != NULL) {
1991da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2001da177e4SLinus Torvalds 		in6_dev_put(idev);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2051da177e4SLinus Torvalds 			   int how)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2081da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	if (dev != &loopback_dev && idev != NULL && idev->dev == dev) {
2111da177e4SLinus Torvalds 		struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
2121da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
2131da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
2141da177e4SLinus Torvalds 			in6_dev_put(idev);
2151da177e4SLinus Torvalds 		}
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	return (rt->rt6i_flags & RTF_EXPIRES &&
2221da177e4SLinus Torvalds 		time_after(jiffies, rt->rt6i_expires));
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225c71099acSThomas Graf static inline int rt6_need_strict(struct in6_addr *daddr)
226c71099acSThomas Graf {
227c71099acSThomas Graf 	return (ipv6_addr_type(daddr) &
228c71099acSThomas Graf 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
229c71099acSThomas Graf }
230c71099acSThomas Graf 
2311da177e4SLinus Torvalds /*
232c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static __inline__ struct rt6_info *rt6_device_match(struct rt6_info *rt,
2361da177e4SLinus Torvalds 						    int oif,
2371da177e4SLinus Torvalds 						    int strict)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
2401da177e4SLinus Torvalds 	struct rt6_info *sprt;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if (oif) {
2431da177e4SLinus Torvalds 		for (sprt = rt; sprt; sprt = sprt->u.next) {
2441da177e4SLinus Torvalds 			struct net_device *dev = sprt->rt6i_dev;
2451da177e4SLinus Torvalds 			if (dev->ifindex == oif)
2461da177e4SLinus Torvalds 				return sprt;
2471da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
2481da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
2491da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
2501da177e4SLinus Torvalds 					if (strict && oif)
2511da177e4SLinus Torvalds 						continue;
2521da177e4SLinus Torvalds 					if (local && (!oif ||
2531da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
2541da177e4SLinus Torvalds 						continue;
2551da177e4SLinus Torvalds 				}
2561da177e4SLinus Torvalds 				local = sprt;
2571da177e4SLinus Torvalds 			}
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 		if (local)
2611da177e4SLinus Torvalds 			return local;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		if (strict)
2641da177e4SLinus Torvalds 			return &ip6_null_entry;
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 	return rt;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
26927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
27027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
27127097255SYOSHIFUJI Hideaki {
27227097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
27327097255SYOSHIFUJI Hideaki 	/*
27427097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
27527097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
27627097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
27727097255SYOSHIFUJI Hideaki 	 *
27827097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
27927097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
28027097255SYOSHIFUJI Hideaki 	 */
28127097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
28227097255SYOSHIFUJI Hideaki 		return;
28327097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
28427097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
28552e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
28627097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
28727097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
28827097255SYOSHIFUJI Hideaki 
28927097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
29027097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
29127097255SYOSHIFUJI Hideaki 
29227097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
29327097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
29427097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
29527097255SYOSHIFUJI Hideaki 	} else
29627097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
29727097255SYOSHIFUJI Hideaki }
29827097255SYOSHIFUJI Hideaki #else
29927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
30027097255SYOSHIFUJI Hideaki {
30127097255SYOSHIFUJI Hideaki 	return;
30227097255SYOSHIFUJI Hideaki }
30327097255SYOSHIFUJI Hideaki #endif
30427097255SYOSHIFUJI Hideaki 
3051da177e4SLinus Torvalds /*
306554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3071da177e4SLinus Torvalds  */
308554cfb7eSYOSHIFUJI Hideaki static int inline rt6_check_dev(struct rt6_info *rt, int oif)
3091da177e4SLinus Torvalds {
310554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
311554cfb7eSYOSHIFUJI Hideaki 	if (!oif || dev->ifindex == oif)
312554cfb7eSYOSHIFUJI Hideaki 		return 2;
313554cfb7eSYOSHIFUJI Hideaki 	if ((dev->flags & IFF_LOOPBACK) &&
314554cfb7eSYOSHIFUJI Hideaki 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
315554cfb7eSYOSHIFUJI Hideaki 		return 1;
316554cfb7eSYOSHIFUJI Hideaki 	return 0;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
319554cfb7eSYOSHIFUJI Hideaki static int inline rt6_check_neigh(struct rt6_info *rt)
3201da177e4SLinus Torvalds {
321554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
3221da177e4SLinus Torvalds 	int m = 0;
3234d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
3244d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
3254d0c5911SYOSHIFUJI Hideaki 		m = 1;
3264d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
3271da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
328554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
3294d0c5911SYOSHIFUJI Hideaki 			m = 2;
3301da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
3311da177e4SLinus Torvalds 	}
332554cfb7eSYOSHIFUJI Hideaki 	return m;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
335554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
336554cfb7eSYOSHIFUJI Hideaki 			   int strict)
337554cfb7eSYOSHIFUJI Hideaki {
3384d0c5911SYOSHIFUJI Hideaki 	int m, n;
3394d0c5911SYOSHIFUJI Hideaki 
3404d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
341554cfb7eSYOSHIFUJI Hideaki 	if (!m && (strict & RT6_SELECT_F_IFACE))
342554cfb7eSYOSHIFUJI Hideaki 		return -1;
343ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
344ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
345ebacaaa0SYOSHIFUJI Hideaki #endif
3464d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
3474d0c5911SYOSHIFUJI Hideaki 	if (n > 1)
348ebacaaa0SYOSHIFUJI Hideaki 		m |= 16;
3494d0c5911SYOSHIFUJI Hideaki 	else if (!n && strict & RT6_SELECT_F_REACHABLE)
350554cfb7eSYOSHIFUJI Hideaki 		return -1;
351554cfb7eSYOSHIFUJI Hideaki 	return m;
352554cfb7eSYOSHIFUJI Hideaki }
353554cfb7eSYOSHIFUJI Hideaki 
354554cfb7eSYOSHIFUJI Hideaki static struct rt6_info *rt6_select(struct rt6_info **head, int oif,
355554cfb7eSYOSHIFUJI Hideaki 				   int strict)
356554cfb7eSYOSHIFUJI Hideaki {
357554cfb7eSYOSHIFUJI Hideaki 	struct rt6_info *match = NULL, *last = NULL;
358554cfb7eSYOSHIFUJI Hideaki 	struct rt6_info *rt, *rt0 = *head;
359554cfb7eSYOSHIFUJI Hideaki 	u32 metric;
360554cfb7eSYOSHIFUJI Hideaki 	int mpri = -1;
361554cfb7eSYOSHIFUJI Hideaki 
362554cfb7eSYOSHIFUJI Hideaki 	RT6_TRACE("%s(head=%p(*head=%p), oif=%d)\n",
363554cfb7eSYOSHIFUJI Hideaki 		  __FUNCTION__, head, head ? *head : NULL, oif);
364554cfb7eSYOSHIFUJI Hideaki 
365554cfb7eSYOSHIFUJI Hideaki 	for (rt = rt0, metric = rt0->rt6i_metric;
366c302e6d5SYOSHIFUJI Hideaki 	     rt && rt->rt6i_metric == metric && (!last || rt != rt0);
367554cfb7eSYOSHIFUJI Hideaki 	     rt = rt->u.next) {
368554cfb7eSYOSHIFUJI Hideaki 		int m;
369554cfb7eSYOSHIFUJI Hideaki 
370554cfb7eSYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
371554cfb7eSYOSHIFUJI Hideaki 			continue;
372554cfb7eSYOSHIFUJI Hideaki 
373554cfb7eSYOSHIFUJI Hideaki 		last = rt;
374554cfb7eSYOSHIFUJI Hideaki 
375554cfb7eSYOSHIFUJI Hideaki 		m = rt6_score_route(rt, oif, strict);
376554cfb7eSYOSHIFUJI Hideaki 		if (m < 0)
377554cfb7eSYOSHIFUJI Hideaki 			continue;
378554cfb7eSYOSHIFUJI Hideaki 
379554cfb7eSYOSHIFUJI Hideaki 		if (m > mpri) {
38027097255SYOSHIFUJI Hideaki 			rt6_probe(match);
381554cfb7eSYOSHIFUJI Hideaki 			match = rt;
3821da177e4SLinus Torvalds 			mpri = m;
38327097255SYOSHIFUJI Hideaki 		} else {
38427097255SYOSHIFUJI Hideaki 			rt6_probe(rt);
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
388554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
389554cfb7eSYOSHIFUJI Hideaki 	    (strict & RT6_SELECT_F_REACHABLE) &&
390554cfb7eSYOSHIFUJI Hideaki 	    last && last != rt0) {
391554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
39234af946aSIngo Molnar 		static DEFINE_SPINLOCK(lock);
393c302e6d5SYOSHIFUJI Hideaki 		spin_lock(&lock);
394554cfb7eSYOSHIFUJI Hideaki 		*head = rt0->u.next;
395554cfb7eSYOSHIFUJI Hideaki 		rt0->u.next = last->u.next;
396554cfb7eSYOSHIFUJI Hideaki 		last->u.next = rt0;
397c302e6d5SYOSHIFUJI Hideaki 		spin_unlock(&lock);
398554cfb7eSYOSHIFUJI Hideaki 	}
399554cfb7eSYOSHIFUJI Hideaki 
400554cfb7eSYOSHIFUJI Hideaki 	RT6_TRACE("%s() => %p, score=%d\n",
401554cfb7eSYOSHIFUJI Hideaki 		  __FUNCTION__, match, mpri);
402554cfb7eSYOSHIFUJI Hideaki 
403554cfb7eSYOSHIFUJI Hideaki 	return (match ? match : &ip6_null_entry);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
40670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
40770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
40870ceb4f5SYOSHIFUJI Hideaki 		  struct in6_addr *gwaddr)
40970ceb4f5SYOSHIFUJI Hideaki {
41070ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
41170ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
41270ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
41370ceb4f5SYOSHIFUJI Hideaki 	u32 lifetime;
41470ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
41570ceb4f5SYOSHIFUJI Hideaki 
41670ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
41770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
41870ceb4f5SYOSHIFUJI Hideaki 	}
41970ceb4f5SYOSHIFUJI Hideaki 
42070ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
42170ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
42270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
42370ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
42470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
42570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
42670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
42770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
42870ceb4f5SYOSHIFUJI Hideaki 		}
42970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
43070ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
43170ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
43270ceb4f5SYOSHIFUJI Hideaki 		}
43370ceb4f5SYOSHIFUJI Hideaki 	}
43470ceb4f5SYOSHIFUJI Hideaki 
43570ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
43670ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
43770ceb4f5SYOSHIFUJI Hideaki 		pref = ICMPV6_ROUTER_PREF_MEDIUM;
43870ceb4f5SYOSHIFUJI Hideaki 
43970ceb4f5SYOSHIFUJI Hideaki 	lifetime = htonl(rinfo->lifetime);
44070ceb4f5SYOSHIFUJI Hideaki 	if (lifetime == 0xffffffff) {
44170ceb4f5SYOSHIFUJI Hideaki 		/* infinity */
44270ceb4f5SYOSHIFUJI Hideaki 	} else if (lifetime > 0x7fffffff/HZ) {
44370ceb4f5SYOSHIFUJI Hideaki 		/* Avoid arithmetic overflow */
44470ceb4f5SYOSHIFUJI Hideaki 		lifetime = 0x7fffffff/HZ - 1;
44570ceb4f5SYOSHIFUJI Hideaki 	}
44670ceb4f5SYOSHIFUJI Hideaki 
44770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
44870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
44970ceb4f5SYOSHIFUJI Hideaki 	else {
45070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
45170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
45270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
45370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
45470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
45570ceb4f5SYOSHIFUJI Hideaki 	}
45670ceb4f5SYOSHIFUJI Hideaki 
45770ceb4f5SYOSHIFUJI Hideaki 	rt = rt6_get_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex);
45870ceb4f5SYOSHIFUJI Hideaki 
45970ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
460e0a1ad73SThomas Graf 		ip6_del_rt(rt);
46170ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
46270ceb4f5SYOSHIFUJI Hideaki 	}
46370ceb4f5SYOSHIFUJI Hideaki 
46470ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
46570ceb4f5SYOSHIFUJI Hideaki 		rt = rt6_add_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
46670ceb4f5SYOSHIFUJI Hideaki 					pref);
46770ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
46870ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
46970ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
47070ceb4f5SYOSHIFUJI Hideaki 
47170ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
47270ceb4f5SYOSHIFUJI Hideaki 		if (lifetime == 0xffffffff) {
47370ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
47470ceb4f5SYOSHIFUJI Hideaki 		} else {
47570ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
47670ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
47770ceb4f5SYOSHIFUJI Hideaki 		}
47870ceb4f5SYOSHIFUJI Hideaki 		dst_release(&rt->u.dst);
47970ceb4f5SYOSHIFUJI Hideaki 	}
48070ceb4f5SYOSHIFUJI Hideaki 	return 0;
48170ceb4f5SYOSHIFUJI Hideaki }
48270ceb4f5SYOSHIFUJI Hideaki #endif
48370ceb4f5SYOSHIFUJI Hideaki 
484c71099acSThomas Graf #define BACKTRACK() \
485c71099acSThomas Graf if (rt == &ip6_null_entry && flags & RT6_F_STRICT) { \
486c71099acSThomas Graf 	while ((fn = fn->parent) != NULL) { \
487c71099acSThomas Graf 		if (fn->fn_flags & RTN_TL_ROOT) { \
488c71099acSThomas Graf 			dst_hold(&rt->u.dst); \
489c71099acSThomas Graf 			goto out; \
490c71099acSThomas Graf 		} \
491c71099acSThomas Graf 		if (fn->fn_flags & RTN_RTINFO) \
492c71099acSThomas Graf 			goto restart; \
493c71099acSThomas Graf 	} \
494c71099acSThomas Graf }
495c71099acSThomas Graf 
496c71099acSThomas Graf static struct rt6_info *ip6_pol_route_lookup(struct fib6_table *table,
497c71099acSThomas Graf 					     struct flowi *fl, int flags)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct fib6_node *fn;
5001da177e4SLinus Torvalds 	struct rt6_info *rt;
5011da177e4SLinus Torvalds 
502c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
503c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
504c71099acSThomas Graf restart:
505c71099acSThomas Graf 	rt = fn->leaf;
506c71099acSThomas Graf 	rt = rt6_device_match(rt, fl->oif, flags & RT6_F_STRICT);
507c71099acSThomas Graf 	BACKTRACK();
5081da177e4SLinus Torvalds 	dst_hold(&rt->u.dst);
509c71099acSThomas Graf out:
510c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	rt->u.dst.lastuse = jiffies;
513c71099acSThomas Graf 	rt->u.dst.__use++;
514c71099acSThomas Graf 
5151da177e4SLinus Torvalds 	return rt;
516c71099acSThomas Graf 
517c71099acSThomas Graf }
518c71099acSThomas Graf 
519c71099acSThomas Graf struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr,
520c71099acSThomas Graf 			    int oif, int strict)
521c71099acSThomas Graf {
522c71099acSThomas Graf 	struct flowi fl = {
523c71099acSThomas Graf 		.oif = oif,
524c71099acSThomas Graf 		.nl_u = {
525c71099acSThomas Graf 			.ip6_u = {
526c71099acSThomas Graf 				.daddr = *daddr,
527c71099acSThomas Graf 				/* TODO: saddr */
528c71099acSThomas Graf 			},
529c71099acSThomas Graf 		},
530c71099acSThomas Graf 	};
531c71099acSThomas Graf 	struct dst_entry *dst;
532c71099acSThomas Graf 	int flags = strict ? RT6_F_STRICT : 0;
533c71099acSThomas Graf 
534c71099acSThomas Graf 	dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_lookup);
535c71099acSThomas Graf 	if (dst->error == 0)
536c71099acSThomas Graf 		return (struct rt6_info *) dst;
537c71099acSThomas Graf 
538c71099acSThomas Graf 	dst_release(dst);
539c71099acSThomas Graf 
5401da177e4SLinus Torvalds 	return NULL;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
5441da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
5451da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
5461da177e4SLinus Torvalds    be destroyed.
5471da177e4SLinus Torvalds  */
5481da177e4SLinus Torvalds 
54986872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	int err;
552c71099acSThomas Graf 	struct fib6_table *table;
5531da177e4SLinus Torvalds 
554c71099acSThomas Graf 	table = rt->rt6i_table;
555c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
55686872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
557c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	return err;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
56240e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
56340e22e8fSThomas Graf {
56486872cb5SThomas Graf 	return __ip6_ins_rt(rt, NULL);
56540e22e8fSThomas Graf }
56640e22e8fSThomas Graf 
56795a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
56895a9a5baSYOSHIFUJI Hideaki 				      struct in6_addr *saddr)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	struct rt6_info *rt;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/*
5731da177e4SLinus Torvalds 	 *	Clone the route.
5741da177e4SLinus Torvalds 	 */
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (rt) {
57958c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
58058c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
58158c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
58258c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
5831da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
58458c4fb86SYOSHIFUJI Hideaki 		}
5851da177e4SLinus Torvalds 
58658c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
5871da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
5881da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
5891da177e4SLinus Torvalds 		rt->u.dst.flags |= DST_HOST;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
5921da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
5931da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
5941da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds #endif
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 		rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
5991da177e4SLinus Torvalds 
60095a9a5baSYOSHIFUJI Hideaki 	}
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	return rt;
6031da177e4SLinus Torvalds }
60495a9a5baSYOSHIFUJI Hideaki 
605299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
606299d9939SYOSHIFUJI Hideaki {
607299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
608299d9939SYOSHIFUJI Hideaki 	if (rt) {
609299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
610299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
611299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
612299d9939SYOSHIFUJI Hideaki 		if (rt->rt6i_flags & RTF_REJECT)
613299d9939SYOSHIFUJI Hideaki 			rt->u.dst.error = ort->u.dst.error;
614299d9939SYOSHIFUJI Hideaki 		rt->u.dst.flags |= DST_HOST;
615299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
616299d9939SYOSHIFUJI Hideaki 	}
617299d9939SYOSHIFUJI Hideaki 	return rt;
618299d9939SYOSHIFUJI Hideaki }
619299d9939SYOSHIFUJI Hideaki 
6208ce11e6aSAdrian Bunk static struct rt6_info *ip6_pol_route_input(struct fib6_table *table,
6218ce11e6aSAdrian Bunk 					    struct flowi *fl, int flags)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	struct fib6_node *fn;
624519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
625c71099acSThomas Graf 	int strict = 0;
6261da177e4SLinus Torvalds 	int attempts = 3;
627519fbd87SYOSHIFUJI Hideaki 	int err;
6288238dd06SYOSHIFUJI Hideaki 	int reachable = RT6_SELECT_F_REACHABLE;
6291da177e4SLinus Torvalds 
630c71099acSThomas Graf 	if (flags & RT6_F_STRICT)
631c71099acSThomas Graf 		strict = RT6_SELECT_F_IFACE;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds relookup:
634c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6351da177e4SLinus Torvalds 
6368238dd06SYOSHIFUJI Hideaki restart_2:
637c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds restart:
640c71099acSThomas Graf 	rt = rt6_select(&fn->leaf, fl->iif, strict | reachable);
6411da177e4SLinus Torvalds 	BACKTRACK();
6428238dd06SYOSHIFUJI Hideaki 	if (rt == &ip6_null_entry ||
6438238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
6441da177e4SLinus Torvalds 		goto out;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	dst_hold(&rt->u.dst);
647c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6481da177e4SLinus Torvalds 
649519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
650e40cf353SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
651519fbd87SYOSHIFUJI Hideaki 	else {
652519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE
653519fbd87SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
654519fbd87SYOSHIFUJI Hideaki #else
655519fbd87SYOSHIFUJI Hideaki 		goto out2;
656519fbd87SYOSHIFUJI Hideaki #endif
657519fbd87SYOSHIFUJI Hideaki 	}
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
660e40cf353SYOSHIFUJI Hideaki 	rt = nrt ? : &ip6_null_entry;
6611da177e4SLinus Torvalds 
662e40cf353SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
663e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
66440e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
665e40cf353SYOSHIFUJI Hideaki 		if (!err)
666e40cf353SYOSHIFUJI Hideaki 			goto out2;
667e40cf353SYOSHIFUJI Hideaki 	}
668e40cf353SYOSHIFUJI Hideaki 
669e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
6701da177e4SLinus Torvalds 		goto out2;
6711da177e4SLinus Torvalds 
672519fbd87SYOSHIFUJI Hideaki 	/*
673c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
674519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
6751da177e4SLinus Torvalds 	 */
6761da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
6771da177e4SLinus Torvalds 	goto relookup;
678e40cf353SYOSHIFUJI Hideaki 
679519fbd87SYOSHIFUJI Hideaki out:
6808238dd06SYOSHIFUJI Hideaki 	if (reachable) {
6818238dd06SYOSHIFUJI Hideaki 		reachable = 0;
6828238dd06SYOSHIFUJI Hideaki 		goto restart_2;
6838238dd06SYOSHIFUJI Hideaki 	}
684519fbd87SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
685c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6861da177e4SLinus Torvalds out2:
6871da177e4SLinus Torvalds 	rt->u.dst.lastuse = jiffies;
6881da177e4SLinus Torvalds 	rt->u.dst.__use++;
689c71099acSThomas Graf 
690c71099acSThomas Graf 	return rt;
691c71099acSThomas Graf }
692c71099acSThomas Graf 
693c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
694c71099acSThomas Graf {
695c71099acSThomas Graf 	struct ipv6hdr *iph = skb->nh.ipv6h;
696c71099acSThomas Graf 	struct flowi fl = {
697c71099acSThomas Graf 		.iif = skb->dev->ifindex,
698c71099acSThomas Graf 		.nl_u = {
699c71099acSThomas Graf 			.ip6_u = {
700c71099acSThomas Graf 				.daddr = iph->daddr,
701c71099acSThomas Graf 				.saddr = iph->saddr,
702c71099acSThomas Graf 				.flowlabel = (* (u32 *) iph)&IPV6_FLOWINFO_MASK,
703c71099acSThomas Graf 			},
704c71099acSThomas Graf 		},
705c71099acSThomas Graf 		.proto = iph->nexthdr,
706c71099acSThomas Graf 	};
707c71099acSThomas Graf 	int flags = 0;
708c71099acSThomas Graf 
709c71099acSThomas Graf 	if (rt6_need_strict(&iph->daddr))
710c71099acSThomas Graf 		flags |= RT6_F_STRICT;
711c71099acSThomas Graf 
712c71099acSThomas Graf 	skb->dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_input);
713c71099acSThomas Graf }
714c71099acSThomas Graf 
715c71099acSThomas Graf static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,
716c71099acSThomas Graf 					     struct flowi *fl, int flags)
717c71099acSThomas Graf {
718c71099acSThomas Graf 	struct fib6_node *fn;
719c71099acSThomas Graf 	struct rt6_info *rt, *nrt;
720c71099acSThomas Graf 	int strict = 0;
721c71099acSThomas Graf 	int attempts = 3;
722c71099acSThomas Graf 	int err;
723c71099acSThomas Graf 	int reachable = RT6_SELECT_F_REACHABLE;
724c71099acSThomas Graf 
725c71099acSThomas Graf 	if (flags & RT6_F_STRICT)
726c71099acSThomas Graf 		strict = RT6_SELECT_F_IFACE;
727c71099acSThomas Graf 
728c71099acSThomas Graf relookup:
729c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
730c71099acSThomas Graf 
731c71099acSThomas Graf restart_2:
732c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
733c71099acSThomas Graf 
734c71099acSThomas Graf restart:
735c71099acSThomas Graf 	rt = rt6_select(&fn->leaf, fl->oif, strict | reachable);
736c71099acSThomas Graf 	BACKTRACK();
737c71099acSThomas Graf 	if (rt == &ip6_null_entry ||
738c71099acSThomas Graf 	    rt->rt6i_flags & RTF_CACHE)
739c71099acSThomas Graf 		goto out;
740c71099acSThomas Graf 
741c71099acSThomas Graf 	dst_hold(&rt->u.dst);
742c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
743c71099acSThomas Graf 
744c71099acSThomas Graf 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
745c71099acSThomas Graf 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
746c71099acSThomas Graf 	else {
747c71099acSThomas Graf #if CLONE_OFFLINK_ROUTE
748c71099acSThomas Graf 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
749c71099acSThomas Graf #else
750c71099acSThomas Graf 		goto out2;
751c71099acSThomas Graf #endif
752c71099acSThomas Graf 	}
753c71099acSThomas Graf 
754c71099acSThomas Graf 	dst_release(&rt->u.dst);
755c71099acSThomas Graf 	rt = nrt ? : &ip6_null_entry;
756c71099acSThomas Graf 
757c71099acSThomas Graf 	dst_hold(&rt->u.dst);
758c71099acSThomas Graf 	if (nrt) {
75940e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
760c71099acSThomas Graf 		if (!err)
761c71099acSThomas Graf 			goto out2;
762c71099acSThomas Graf 	}
763c71099acSThomas Graf 
764c71099acSThomas Graf 	if (--attempts <= 0)
765c71099acSThomas Graf 		goto out2;
766c71099acSThomas Graf 
767c71099acSThomas Graf 	/*
768c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
769c71099acSThomas Graf 	 * released someone could insert this route.  Relookup.
770c71099acSThomas Graf 	 */
771c71099acSThomas Graf 	dst_release(&rt->u.dst);
772c71099acSThomas Graf 	goto relookup;
773c71099acSThomas Graf 
774c71099acSThomas Graf out:
775c71099acSThomas Graf 	if (reachable) {
776c71099acSThomas Graf 		reachable = 0;
777c71099acSThomas Graf 		goto restart_2;
778c71099acSThomas Graf 	}
779c71099acSThomas Graf 	dst_hold(&rt->u.dst);
780c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
781c71099acSThomas Graf out2:
782c71099acSThomas Graf 	rt->u.dst.lastuse = jiffies;
783c71099acSThomas Graf 	rt->u.dst.__use++;
784c71099acSThomas Graf 	return rt;
785c71099acSThomas Graf }
786c71099acSThomas Graf 
787c71099acSThomas Graf struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl)
788c71099acSThomas Graf {
789c71099acSThomas Graf 	int flags = 0;
790c71099acSThomas Graf 
791c71099acSThomas Graf 	if (rt6_need_strict(&fl->fl6_dst))
792c71099acSThomas Graf 		flags |= RT6_F_STRICT;
793c71099acSThomas Graf 
794c71099acSThomas Graf 	return fib6_rule_lookup(fl, flags, ip6_pol_route_output);
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /*
7991da177e4SLinus Torvalds  *	Destination cache support functions
8001da177e4SLinus Torvalds  */
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	struct rt6_info *rt;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
8091da177e4SLinus Torvalds 		return dst;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	return NULL;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	if (rt) {
8191da177e4SLinus Torvalds 		if (rt->rt6i_flags & RTF_CACHE)
820e0a1ad73SThomas Graf 			ip6_del_rt(rt);
8211da177e4SLinus Torvalds 		else
8221da177e4SLinus Torvalds 			dst_release(dst);
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	return NULL;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	struct rt6_info *rt;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	rt = (struct rt6_info *) skb->dst;
8341da177e4SLinus Torvalds 	if (rt) {
8351da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
8361da177e4SLinus Torvalds 			dst_set_expires(&rt->u.dst, 0);
8371da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
8381da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
8391da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
8481da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
8491da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
8501da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
8511da177e4SLinus Torvalds 			dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
8521da177e4SLinus Torvalds 		}
8531da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
8548d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static inline unsigned int ipv6_advmss(unsigned int mtu)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (mtu < ip6_rt_min_advmss)
8651da177e4SLinus Torvalds 		mtu = ip6_rt_min_advmss;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	/*
8681da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
8691da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
8701da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
8711da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
8721da177e4SLinus Torvalds 	 */
8731da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
8741da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
8751da177e4SLinus Torvalds 	return mtu;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8785d0bbeebSThomas Graf static struct dst_entry *ndisc_dst_gc_list;
8798ce11e6aSAdrian Bunk static DEFINE_SPINLOCK(ndisc_lock);
8805d0bbeebSThomas Graf 
8811da177e4SLinus Torvalds struct dst_entry *ndisc_dst_alloc(struct net_device *dev,
8821da177e4SLinus Torvalds 				  struct neighbour *neigh,
8831da177e4SLinus Torvalds 				  struct in6_addr *addr,
8841da177e4SLinus Torvalds 				  int (*output)(struct sk_buff *))
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	struct rt6_info *rt;
8871da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
8901da177e4SLinus Torvalds 		return NULL;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	rt = ip6_dst_alloc();
8931da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
8941da177e4SLinus Torvalds 		in6_dev_put(idev);
8951da177e4SLinus Torvalds 		goto out;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	dev_hold(dev);
8991da177e4SLinus Torvalds 	if (neigh)
9001da177e4SLinus Torvalds 		neigh_hold(neigh);
9011da177e4SLinus Torvalds 	else
9021da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	rt->rt6i_dev	  = dev;
9051da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
9061da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
9071da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
9081da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
9091da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
9101da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
9111da177e4SLinus Torvalds 	rt->u.dst.output  = output;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
9141da177e4SLinus Torvalds 	rt->u.dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
9151da177e4SLinus Torvalds 				? DST_HOST
9161da177e4SLinus Torvalds 				: 0;
9171da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
9181da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
9191da177e4SLinus Torvalds #endif
9201da177e4SLinus Torvalds 
9215d0bbeebSThomas Graf 	spin_lock_bh(&ndisc_lock);
9221da177e4SLinus Torvalds 	rt->u.dst.next = ndisc_dst_gc_list;
9231da177e4SLinus Torvalds 	ndisc_dst_gc_list = &rt->u.dst;
9245d0bbeebSThomas Graf 	spin_unlock_bh(&ndisc_lock);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	fib6_force_start_gc();
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds out:
9291da177e4SLinus Torvalds 	return (struct dst_entry *)rt;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds int ndisc_dst_gc(int *more)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct dst_entry *dst, *next, **pprev;
9351da177e4SLinus Torvalds 	int freed;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	next = NULL;
9381da177e4SLinus Torvalds  	freed = 0;
9395d0bbeebSThomas Graf 
9405d0bbeebSThomas Graf 	spin_lock_bh(&ndisc_lock);
9415d0bbeebSThomas Graf 	pprev = &ndisc_dst_gc_list;
9425d0bbeebSThomas Graf 
9431da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
9441da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
9451da177e4SLinus Torvalds 			*pprev = dst->next;
9461da177e4SLinus Torvalds 			dst_free(dst);
9471da177e4SLinus Torvalds 			freed++;
9481da177e4SLinus Torvalds 		} else {
9491da177e4SLinus Torvalds 			pprev = &dst->next;
9501da177e4SLinus Torvalds 			(*more)++;
9511da177e4SLinus Torvalds 		}
9521da177e4SLinus Torvalds 	}
9531da177e4SLinus Torvalds 
9545d0bbeebSThomas Graf 	spin_unlock_bh(&ndisc_lock);
9555d0bbeebSThomas Graf 
9561da177e4SLinus Torvalds 	return freed;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds static int ip6_dst_gc(void)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	static unsigned expire = 30*HZ;
9621da177e4SLinus Torvalds 	static unsigned long last_gc;
9631da177e4SLinus Torvalds 	unsigned long now = jiffies;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (time_after(last_gc + ip6_rt_gc_min_interval, now) &&
9661da177e4SLinus Torvalds 	    atomic_read(&ip6_dst_ops.entries) <= ip6_rt_max_size)
9671da177e4SLinus Torvalds 		goto out;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	expire++;
9701da177e4SLinus Torvalds 	fib6_run_gc(expire);
9711da177e4SLinus Torvalds 	last_gc = now;
9721da177e4SLinus Torvalds 	if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh)
9731da177e4SLinus Torvalds 		expire = ip6_rt_gc_timeout>>1;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds out:
9761da177e4SLinus Torvalds 	expire -= expire>>ip6_rt_gc_elasticity;
9771da177e4SLinus Torvalds 	return (atomic_read(&ip6_dst_ops.entries) > ip6_rt_max_size);
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
9811da177e4SLinus Torvalds    but results in cleaner routing tables.
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds    Remove it only when all the things will work!
9841da177e4SLinus Torvalds  */
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	int mtu = IPV6_MIN_MTU;
9891da177e4SLinus Torvalds 	struct inet6_dev *idev;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	idev = in6_dev_get(dev);
9921da177e4SLinus Torvalds 	if (idev) {
9931da177e4SLinus Torvalds 		mtu = idev->cnf.mtu6;
9941da177e4SLinus Torvalds 		in6_dev_put(idev);
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	return mtu;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds int ipv6_get_hoplimit(struct net_device *dev)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	int hoplimit = ipv6_devconf.hop_limit;
10021da177e4SLinus Torvalds 	struct inet6_dev *idev;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	idev = in6_dev_get(dev);
10051da177e4SLinus Torvalds 	if (idev) {
10061da177e4SLinus Torvalds 		hoplimit = idev->cnf.hop_limit;
10071da177e4SLinus Torvalds 		in6_dev_put(idev);
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 	return hoplimit;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds /*
10131da177e4SLinus Torvalds  *
10141da177e4SLinus Torvalds  */
10151da177e4SLinus Torvalds 
101686872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	int err;
10191da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
10201da177e4SLinus Torvalds 	struct net_device *dev = NULL;
10211da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1022c71099acSThomas Graf 	struct fib6_table *table;
10231da177e4SLinus Torvalds 	int addr_type;
10241da177e4SLinus Torvalds 
102586872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
10261da177e4SLinus Torvalds 		return -EINVAL;
10271da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
102886872cb5SThomas Graf 	if (cfg->fc_src_len)
10291da177e4SLinus Torvalds 		return -EINVAL;
10301da177e4SLinus Torvalds #endif
103186872cb5SThomas Graf 	if (cfg->fc_ifindex) {
10321da177e4SLinus Torvalds 		err = -ENODEV;
103386872cb5SThomas Graf 		dev = dev_get_by_index(cfg->fc_ifindex);
10341da177e4SLinus Torvalds 		if (!dev)
10351da177e4SLinus Torvalds 			goto out;
10361da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
10371da177e4SLinus Torvalds 		if (!idev)
10381da177e4SLinus Torvalds 			goto out;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
104186872cb5SThomas Graf 	if (cfg->fc_metric == 0)
104286872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
10431da177e4SLinus Torvalds 
104486872cb5SThomas Graf 	table = fib6_new_table(cfg->fc_table);
1045c71099acSThomas Graf 	if (table == NULL) {
1046c71099acSThomas Graf 		err = -ENOBUFS;
1047c71099acSThomas Graf 		goto out;
1048c71099acSThomas Graf 	}
1049c71099acSThomas Graf 
10501da177e4SLinus Torvalds 	rt = ip6_dst_alloc();
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	if (rt == NULL) {
10531da177e4SLinus Torvalds 		err = -ENOMEM;
10541da177e4SLinus Torvalds 		goto out;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
105886872cb5SThomas Graf 	rt->rt6i_expires = jiffies + clock_t_to_jiffies(cfg->fc_expires);
10591da177e4SLinus Torvalds 
106086872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
106186872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
106286872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
106386872cb5SThomas Graf 
106486872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
10671da177e4SLinus Torvalds 		rt->u.dst.input = ip6_mc_input;
10681da177e4SLinus Torvalds 	else
10691da177e4SLinus Torvalds 		rt->u.dst.input = ip6_forward;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
10721da177e4SLinus Torvalds 
107386872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
107486872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
10751da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
10761da177e4SLinus Torvalds 	       rt->u.dst.flags = DST_HOST;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
107986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
108086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
10811da177e4SLinus Torvalds #endif
10821da177e4SLinus Torvalds 
108386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
10861da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
10871da177e4SLinus Torvalds 	 */
108886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
10891da177e4SLinus Torvalds 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
10901da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
10911da177e4SLinus Torvalds 		if (dev != &loopback_dev) {
10921da177e4SLinus Torvalds 			if (dev) {
10931da177e4SLinus Torvalds 				dev_put(dev);
10941da177e4SLinus Torvalds 				in6_dev_put(idev);
10951da177e4SLinus Torvalds 			}
10961da177e4SLinus Torvalds 			dev = &loopback_dev;
10971da177e4SLinus Torvalds 			dev_hold(dev);
10981da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
10991da177e4SLinus Torvalds 			if (!idev) {
11001da177e4SLinus Torvalds 				err = -ENODEV;
11011da177e4SLinus Torvalds 				goto out;
11021da177e4SLinus Torvalds 			}
11031da177e4SLinus Torvalds 		}
11041da177e4SLinus Torvalds 		rt->u.dst.output = ip6_pkt_discard_out;
11051da177e4SLinus Torvalds 		rt->u.dst.input = ip6_pkt_discard;
11061da177e4SLinus Torvalds 		rt->u.dst.error = -ENETUNREACH;
11071da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
11081da177e4SLinus Torvalds 		goto install_route;
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 
111186872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
11121da177e4SLinus Torvalds 		struct in6_addr *gw_addr;
11131da177e4SLinus Torvalds 		int gwa_type;
11141da177e4SLinus Torvalds 
111586872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
111686872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
11171da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
11201da177e4SLinus Torvalds 			struct rt6_info *grt;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
11231da177e4SLinus Torvalds 			   addresses as nexthop address.
11241da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
11251da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
11261da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
11271da177e4SLinus Torvalds 			   some exceptions. --ANK
11281da177e4SLinus Torvalds 			 */
11291da177e4SLinus Torvalds 			err = -EINVAL;
11301da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
11311da177e4SLinus Torvalds 				goto out;
11321da177e4SLinus Torvalds 
113386872cb5SThomas Graf 			grt = rt6_lookup(gw_addr, NULL, cfg->fc_ifindex, 1);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
11361da177e4SLinus Torvalds 			if (grt == NULL)
11371da177e4SLinus Torvalds 				goto out;
11381da177e4SLinus Torvalds 			if (dev) {
11391da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
11401da177e4SLinus Torvalds 					dst_release(&grt->u.dst);
11411da177e4SLinus Torvalds 					goto out;
11421da177e4SLinus Torvalds 				}
11431da177e4SLinus Torvalds 			} else {
11441da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
11451da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
11461da177e4SLinus Torvalds 				dev_hold(dev);
11471da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
11481da177e4SLinus Torvalds 			}
11491da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
11501da177e4SLinus Torvalds 				err = 0;
11511da177e4SLinus Torvalds 			dst_release(&grt->u.dst);
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 			if (err)
11541da177e4SLinus Torvalds 				goto out;
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 		err = -EINVAL;
11571da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
11581da177e4SLinus Torvalds 			goto out;
11591da177e4SLinus Torvalds 	}
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	err = -ENODEV;
11621da177e4SLinus Torvalds 	if (dev == NULL)
11631da177e4SLinus Torvalds 		goto out;
11641da177e4SLinus Torvalds 
116586872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
11661da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
11671da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
11681da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
11691da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
11701da177e4SLinus Torvalds 			goto out;
11711da177e4SLinus Torvalds 		}
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
117486872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds install_route:
117786872cb5SThomas Graf 	if (cfg->fc_mx) {
117886872cb5SThomas Graf 		struct nlattr *nla;
117986872cb5SThomas Graf 		int remaining;
11801da177e4SLinus Torvalds 
118186872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
118286872cb5SThomas Graf 			int type = nla->nla_type;
118386872cb5SThomas Graf 
118486872cb5SThomas Graf 			if (type) {
118586872cb5SThomas Graf 				if (type > RTAX_MAX) {
11861da177e4SLinus Torvalds 					err = -EINVAL;
11871da177e4SLinus Torvalds 					goto out;
11881da177e4SLinus Torvalds 				}
118986872cb5SThomas Graf 
119086872cb5SThomas Graf 				rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
11911da177e4SLinus Torvalds 			}
11921da177e4SLinus Torvalds 		}
11931da177e4SLinus Torvalds 	}
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
11961da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
11971da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_MTU-1])
11981da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
11991da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
12001da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
12011da177e4SLinus Torvalds 	rt->u.dst.dev = dev;
12021da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1203c71099acSThomas Graf 	rt->rt6i_table = table;
120486872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds out:
12071da177e4SLinus Torvalds 	if (dev)
12081da177e4SLinus Torvalds 		dev_put(dev);
12091da177e4SLinus Torvalds 	if (idev)
12101da177e4SLinus Torvalds 		in6_dev_put(idev);
12111da177e4SLinus Torvalds 	if (rt)
12121da177e4SLinus Torvalds 		dst_free((struct dst_entry *) rt);
12131da177e4SLinus Torvalds 	return err;
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
121686872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
12171da177e4SLinus Torvalds {
12181da177e4SLinus Torvalds 	int err;
1219c71099acSThomas Graf 	struct fib6_table *table;
12201da177e4SLinus Torvalds 
12216c813a72SPatrick McHardy 	if (rt == &ip6_null_entry)
12226c813a72SPatrick McHardy 		return -ENOENT;
12236c813a72SPatrick McHardy 
1224c71099acSThomas Graf 	table = rt->rt6i_table;
1225c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
12261da177e4SLinus Torvalds 
122786872cb5SThomas Graf 	err = fib6_del(rt, info);
12281da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
12291da177e4SLinus Torvalds 
1230c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	return err;
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
1235e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1236e0a1ad73SThomas Graf {
123786872cb5SThomas Graf 	return __ip6_del_rt(rt, NULL);
1238e0a1ad73SThomas Graf }
1239e0a1ad73SThomas Graf 
124086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
12411da177e4SLinus Torvalds {
1242c71099acSThomas Graf 	struct fib6_table *table;
12431da177e4SLinus Torvalds 	struct fib6_node *fn;
12441da177e4SLinus Torvalds 	struct rt6_info *rt;
12451da177e4SLinus Torvalds 	int err = -ESRCH;
12461da177e4SLinus Torvalds 
124786872cb5SThomas Graf 	table = fib6_get_table(cfg->fc_table);
1248c71099acSThomas Graf 	if (table == NULL)
1249c71099acSThomas Graf 		return err;
12501da177e4SLinus Torvalds 
1251c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1252c71099acSThomas Graf 
1253c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
125486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
125586872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	if (fn) {
12581da177e4SLinus Torvalds 		for (rt = fn->leaf; rt; rt = rt->u.next) {
125986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
12601da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
126186872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
12621da177e4SLinus Torvalds 				continue;
126386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
126486872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
12651da177e4SLinus Torvalds 				continue;
126686872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
12671da177e4SLinus Torvalds 				continue;
12681da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1269c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
12701da177e4SLinus Torvalds 
127186872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 	}
1274c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	return err;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /*
12801da177e4SLinus Torvalds  *	Handle redirects
12811da177e4SLinus Torvalds  */
12821da177e4SLinus Torvalds void rt6_redirect(struct in6_addr *dest, struct in6_addr *saddr,
12831da177e4SLinus Torvalds 		  struct neighbour *neigh, u8 *lladdr, int on_link)
12841da177e4SLinus Torvalds {
1285e843b9e1SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1286e843b9e1SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1287c71099acSThomas Graf 	struct fib6_table *table;
12888d71740cSTom Tucker 	struct netevent_redirect netevent;
12891da177e4SLinus Torvalds 
1290c71099acSThomas Graf 	/* TODO: Very lazy, might need to check all tables */
1291c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_MAIN);
1292c71099acSThomas Graf 	if (table == NULL)
1293c71099acSThomas Graf 		return;
1294c71099acSThomas Graf 
1295e843b9e1SYOSHIFUJI Hideaki 	/*
1296e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1297e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1298e843b9e1SYOSHIFUJI Hideaki 	 *
1299e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1300e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1301e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1302e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1303e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1304e843b9e1SYOSHIFUJI Hideaki 	 */
13051da177e4SLinus Torvalds 
1306c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1307c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, dest, NULL);
1308e843b9e1SYOSHIFUJI Hideaki restart:
1309e843b9e1SYOSHIFUJI Hideaki 	for (rt = fn->leaf; rt; rt = rt->u.next) {
13101da177e4SLinus Torvalds 		/*
13111da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
13121da177e4SLinus Torvalds 		 *
13131da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
13141da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
13151da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
13161da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
13171da177e4SLinus Torvalds 		 */
1318e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1319e843b9e1SYOSHIFUJI Hideaki 			continue;
13201da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1321e843b9e1SYOSHIFUJI Hideaki 			continue;
1322e843b9e1SYOSHIFUJI Hideaki 		if (neigh->dev != rt->rt6i_dev)
1323e843b9e1SYOSHIFUJI Hideaki 			continue;
1324e843b9e1SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(saddr, &rt->rt6i_gateway))
1325e843b9e1SYOSHIFUJI Hideaki 			continue;
1326e843b9e1SYOSHIFUJI Hideaki 		break;
1327e843b9e1SYOSHIFUJI Hideaki 	}
1328e843b9e1SYOSHIFUJI Hideaki 	if (rt)
1329e843b9e1SYOSHIFUJI Hideaki 		dst_hold(&rt->u.dst);
1330c71099acSThomas Graf 	else if (rt6_need_strict(dest)) {
1331e843b9e1SYOSHIFUJI Hideaki 		while ((fn = fn->parent) != NULL) {
1332e843b9e1SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_ROOT)
1333e843b9e1SYOSHIFUJI Hideaki 				break;
1334e843b9e1SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_RTINFO)
1335e843b9e1SYOSHIFUJI Hideaki 				goto restart;
1336e843b9e1SYOSHIFUJI Hideaki 		}
1337e843b9e1SYOSHIFUJI Hideaki 	}
1338c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13391da177e4SLinus Torvalds 
1340e843b9e1SYOSHIFUJI Hideaki 	if (!rt) {
13411da177e4SLinus Torvalds 		if (net_ratelimit())
13421da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
13431da177e4SLinus Torvalds 			       "for redirect target\n");
1344e843b9e1SYOSHIFUJI Hideaki 		return;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/*
13481da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
13491da177e4SLinus Torvalds 	 */
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
13521da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
13531da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
13541da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
13551da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
13561da177e4SLinus Torvalds 		     );
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	/*
13591da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
13601da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
13611da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
13621da177e4SLinus Torvalds 	 */
13631da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
13661da177e4SLinus Torvalds 	if (neigh == rt->u.dst.neighbour)
13671da177e4SLinus Torvalds 		goto out;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
13701da177e4SLinus Torvalds 	if (nrt == NULL)
13711da177e4SLinus Torvalds 		goto out;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
13741da177e4SLinus Torvalds 	if (on_link)
13751da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
13781da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
13791da177e4SLinus Torvalds 	nrt->u.dst.flags |= DST_HOST;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
13821da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
13831da177e4SLinus Torvalds 	/* Reset pmtu, it may be better */
13841da177e4SLinus Torvalds 	nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
13851da177e4SLinus Torvalds 	nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&nrt->u.dst));
13861da177e4SLinus Torvalds 
138740e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
13881da177e4SLinus Torvalds 		goto out;
13891da177e4SLinus Torvalds 
13908d71740cSTom Tucker 	netevent.old = &rt->u.dst;
13918d71740cSTom Tucker 	netevent.new = &nrt->u.dst;
13928d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
13938d71740cSTom Tucker 
13941da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1395e0a1ad73SThomas Graf 		ip6_del_rt(rt);
13961da177e4SLinus Torvalds 		return;
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds out:
14001da177e4SLinus Torvalds         dst_release(&rt->u.dst);
14011da177e4SLinus Torvalds 	return;
14021da177e4SLinus Torvalds }
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds /*
14051da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
14061da177e4SLinus Torvalds  *	i.e. Path MTU discovery
14071da177e4SLinus Torvalds  */
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
14101da177e4SLinus Torvalds 			struct net_device *dev, u32 pmtu)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
14131da177e4SLinus Torvalds 	int allfrag = 0;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	rt = rt6_lookup(daddr, saddr, dev->ifindex, 0);
14161da177e4SLinus Torvalds 	if (rt == NULL)
14171da177e4SLinus Torvalds 		return;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	if (pmtu >= dst_mtu(&rt->u.dst))
14201da177e4SLinus Torvalds 		goto out;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
14231da177e4SLinus Torvalds 		/*
14241da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
14251da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
14261da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
14271da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
14281da177e4SLinus Torvalds 		 */
14291da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
14301da177e4SLinus Torvalds 		allfrag = 1;
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
14341da177e4SLinus Torvalds 	   They are sent only in response to data packets,
14351da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
14361da177e4SLinus Torvalds 	 */
14371da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
14401da177e4SLinus Torvalds 	   not to override it, but add new one, so that
14411da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
14421da177e4SLinus Torvalds 	   would return automatically.
14431da177e4SLinus Torvalds 	 */
14441da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
14451da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
14461da177e4SLinus Torvalds 		if (allfrag)
14471da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
14481da177e4SLinus Torvalds 		dst_set_expires(&rt->u.dst, ip6_rt_mtu_expires);
14491da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
14501da177e4SLinus Torvalds 		goto out;
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	/* Network route.
14541da177e4SLinus Torvalds 	   Two cases are possible:
14551da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
14561da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
14571da177e4SLinus Torvalds 	 */
1458d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1459a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1460d5315b50SYOSHIFUJI Hideaki 	else
1461d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1462a1e78363SYOSHIFUJI Hideaki 
1463d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
14641da177e4SLinus Torvalds 		nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
14651da177e4SLinus Torvalds 		if (allfrag)
14661da177e4SLinus Torvalds 			nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1467a1e78363SYOSHIFUJI Hideaki 
14681da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1469a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1470a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1471a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1472a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
14731da177e4SLinus Torvalds 		 */
14741da177e4SLinus Torvalds 		dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires);
14751da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1476a1e78363SYOSHIFUJI Hideaki 
147740e22e8fSThomas Graf 		ip6_ins_rt(nrt);
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds out:
14801da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds /*
14841da177e4SLinus Torvalds  *	Misc support functions
14851da177e4SLinus Torvalds  */
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	struct rt6_info *rt = ip6_dst_alloc();
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	if (rt) {
14921da177e4SLinus Torvalds 		rt->u.dst.input = ort->u.dst.input;
14931da177e4SLinus Torvalds 		rt->u.dst.output = ort->u.dst.output;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
14961da177e4SLinus Torvalds 		rt->u.dst.dev = ort->u.dst.dev;
14971da177e4SLinus Torvalds 		if (rt->u.dst.dev)
14981da177e4SLinus Torvalds 			dev_hold(rt->u.dst.dev);
14991da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
15001da177e4SLinus Torvalds 		if (rt->rt6i_idev)
15011da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
15021da177e4SLinus Torvalds 		rt->u.dst.lastuse = jiffies;
15031da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
15061da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
15071da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
15101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
15111da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
15121da177e4SLinus Torvalds #endif
1513c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
15141da177e4SLinus Torvalds 	}
15151da177e4SLinus Torvalds 	return rt;
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
151870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
151970ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
152070ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex)
152170ceb4f5SYOSHIFUJI Hideaki {
152270ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
152370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1524c71099acSThomas Graf 	struct fib6_table *table;
152570ceb4f5SYOSHIFUJI Hideaki 
1526c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_INFO);
1527c71099acSThomas Graf 	if (table == NULL)
1528c71099acSThomas Graf 		return NULL;
1529c71099acSThomas Graf 
1530c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1531c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
153270ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
153370ceb4f5SYOSHIFUJI Hideaki 		goto out;
153470ceb4f5SYOSHIFUJI Hideaki 
153570ceb4f5SYOSHIFUJI Hideaki 	for (rt = fn->leaf; rt; rt = rt->u.next) {
153670ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
153770ceb4f5SYOSHIFUJI Hideaki 			continue;
153870ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
153970ceb4f5SYOSHIFUJI Hideaki 			continue;
154070ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
154170ceb4f5SYOSHIFUJI Hideaki 			continue;
154270ceb4f5SYOSHIFUJI Hideaki 		dst_hold(&rt->u.dst);
154370ceb4f5SYOSHIFUJI Hideaki 		break;
154470ceb4f5SYOSHIFUJI Hideaki 	}
154570ceb4f5SYOSHIFUJI Hideaki out:
1546c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
154770ceb4f5SYOSHIFUJI Hideaki 	return rt;
154870ceb4f5SYOSHIFUJI Hideaki }
154970ceb4f5SYOSHIFUJI Hideaki 
155070ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
155170ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
155270ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
155370ceb4f5SYOSHIFUJI Hideaki {
155486872cb5SThomas Graf 	struct fib6_config cfg = {
155586872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
155686872cb5SThomas Graf 		.fc_metric	= 1024,
155786872cb5SThomas Graf 		.fc_ifindex	= ifindex,
155886872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
155986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
156086872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
156186872cb5SThomas Graf 	};
156270ceb4f5SYOSHIFUJI Hideaki 
156386872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
156486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
156586872cb5SThomas Graf 
1566e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1567e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
156886872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
156970ceb4f5SYOSHIFUJI Hideaki 
157086872cb5SThomas Graf 	ip6_route_add(&cfg);
157170ceb4f5SYOSHIFUJI Hideaki 
157270ceb4f5SYOSHIFUJI Hideaki 	return rt6_get_route_info(prefix, prefixlen, gwaddr, ifindex);
157370ceb4f5SYOSHIFUJI Hideaki }
157470ceb4f5SYOSHIFUJI Hideaki #endif
157570ceb4f5SYOSHIFUJI Hideaki 
15761da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	struct rt6_info *rt;
1579c71099acSThomas Graf 	struct fib6_table *table;
15801da177e4SLinus Torvalds 
1581c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_DFLT);
1582c71099acSThomas Graf 	if (table == NULL)
1583c71099acSThomas Graf 		return NULL;
15841da177e4SLinus Torvalds 
1585c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1586c71099acSThomas Graf 	for (rt = table->tb6_root.leaf; rt; rt=rt->u.next) {
15871da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1588045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
15891da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
15901da177e4SLinus Torvalds 			break;
15911da177e4SLinus Torvalds 	}
15921da177e4SLinus Torvalds 	if (rt)
15931da177e4SLinus Torvalds 		dst_hold(&rt->u.dst);
1594c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15951da177e4SLinus Torvalds 	return rt;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1599ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1600ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
16011da177e4SLinus Torvalds {
160286872cb5SThomas Graf 	struct fib6_config cfg = {
160386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
160486872cb5SThomas Graf 		.fc_metric	= 1024,
160586872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
160686872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
160786872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
160886872cb5SThomas Graf 	};
16091da177e4SLinus Torvalds 
161086872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
16111da177e4SLinus Torvalds 
161286872cb5SThomas Graf 	ip6_route_add(&cfg);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds void rt6_purge_dflt_routers(void)
16181da177e4SLinus Torvalds {
16191da177e4SLinus Torvalds 	struct rt6_info *rt;
1620c71099acSThomas Graf 	struct fib6_table *table;
1621c71099acSThomas Graf 
1622c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
1623c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_DFLT);
1624c71099acSThomas Graf 	if (table == NULL)
1625c71099acSThomas Graf 		return;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds restart:
1628c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1629c71099acSThomas Graf 	for (rt = table->tb6_root.leaf; rt; rt = rt->u.next) {
16301da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
16311da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1632c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1633e0a1ad73SThomas Graf 			ip6_del_rt(rt);
16341da177e4SLinus Torvalds 			goto restart;
16351da177e4SLinus Torvalds 		}
16361da177e4SLinus Torvalds 	}
1637c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
164086872cb5SThomas Graf static void rtmsg_to_fib6_config(struct in6_rtmsg *rtmsg,
164186872cb5SThomas Graf 				 struct fib6_config *cfg)
164286872cb5SThomas Graf {
164386872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
164486872cb5SThomas Graf 
164586872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
164686872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
164786872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
164886872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
164986872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
165086872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
165186872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
165286872cb5SThomas Graf 
165386872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
165486872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
165586872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
165686872cb5SThomas Graf }
165786872cb5SThomas Graf 
16581da177e4SLinus Torvalds int ipv6_route_ioctl(unsigned int cmd, void __user *arg)
16591da177e4SLinus Torvalds {
166086872cb5SThomas Graf 	struct fib6_config cfg;
16611da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
16621da177e4SLinus Torvalds 	int err;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	switch(cmd) {
16651da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
16661da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
16671da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
16681da177e4SLinus Torvalds 			return -EPERM;
16691da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
16701da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
16711da177e4SLinus Torvalds 		if (err)
16721da177e4SLinus Torvalds 			return -EFAULT;
16731da177e4SLinus Torvalds 
167486872cb5SThomas Graf 		rtmsg_to_fib6_config(&rtmsg, &cfg);
167586872cb5SThomas Graf 
16761da177e4SLinus Torvalds 		rtnl_lock();
16771da177e4SLinus Torvalds 		switch (cmd) {
16781da177e4SLinus Torvalds 		case SIOCADDRT:
167986872cb5SThomas Graf 			err = ip6_route_add(&cfg);
16801da177e4SLinus Torvalds 			break;
16811da177e4SLinus Torvalds 		case SIOCDELRT:
168286872cb5SThomas Graf 			err = ip6_route_del(&cfg);
16831da177e4SLinus Torvalds 			break;
16841da177e4SLinus Torvalds 		default:
16851da177e4SLinus Torvalds 			err = -EINVAL;
16861da177e4SLinus Torvalds 		}
16871da177e4SLinus Torvalds 		rtnl_unlock();
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 		return err;
16901da177e4SLinus Torvalds 	};
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	return -EINVAL;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds /*
16961da177e4SLinus Torvalds  *	Drop the packet on the floor
16971da177e4SLinus Torvalds  */
16981da177e4SLinus Torvalds 
169920380731SArnaldo Carvalho de Melo static int ip6_pkt_discard(struct sk_buff *skb)
17001da177e4SLinus Torvalds {
170176d0cc1bSLv Liangying 	int type = ipv6_addr_type(&skb->nh.ipv6h->daddr);
170276d0cc1bSLv Liangying 	if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED)
170376d0cc1bSLv Liangying 		IP6_INC_STATS(IPSTATS_MIB_INADDRERRORS);
170476d0cc1bSLv Liangying 
17051da177e4SLinus Torvalds 	IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
17061da177e4SLinus Torvalds 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_NOROUTE, 0, skb->dev);
17071da177e4SLinus Torvalds 	kfree_skb(skb);
17081da177e4SLinus Torvalds 	return 0;
17091da177e4SLinus Torvalds }
17101da177e4SLinus Torvalds 
171120380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
17121da177e4SLinus Torvalds {
17131da177e4SLinus Torvalds 	skb->dev = skb->dst->dev;
17141da177e4SLinus Torvalds 	return ip6_pkt_discard(skb);
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds /*
17181da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
17191da177e4SLinus Torvalds  */
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
17221da177e4SLinus Torvalds 				    const struct in6_addr *addr,
17231da177e4SLinus Torvalds 				    int anycast)
17241da177e4SLinus Torvalds {
17251da177e4SLinus Torvalds 	struct rt6_info *rt = ip6_dst_alloc();
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	if (rt == NULL)
17281da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	dev_hold(&loopback_dev);
17311da177e4SLinus Torvalds 	in6_dev_hold(idev);
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	rt->u.dst.flags = DST_HOST;
17341da177e4SLinus Torvalds 	rt->u.dst.input = ip6_input;
17351da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
17361da177e4SLinus Torvalds 	rt->rt6i_dev = &loopback_dev;
17371da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
17381da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
17391da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
17401da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
17411da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
174458c4fb86SYOSHIFUJI Hideaki 	if (anycast)
174558c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
174658c4fb86SYOSHIFUJI Hideaki 	else
17471da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
17481da177e4SLinus Torvalds 	rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
17491da177e4SLinus Torvalds 	if (rt->rt6i_nexthop == NULL) {
17501da177e4SLinus Torvalds 		dst_free((struct dst_entry *) rt);
17511da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
17551da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
1756c71099acSThomas Graf 	rt->rt6i_table = fib6_get_table(RT6_TABLE_LOCAL);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	return rt;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
17641da177e4SLinus Torvalds {
17651da177e4SLinus Torvalds 	if (((void*)rt->rt6i_dev == arg || arg == NULL) &&
17661da177e4SLinus Torvalds 	    rt != &ip6_null_entry) {
17671da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
17681da177e4SLinus Torvalds 		return -1;
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 	return 0;
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds void rt6_ifdown(struct net_device *dev)
17741da177e4SLinus Torvalds {
1775c71099acSThomas Graf 	fib6_clean_all(fib6_ifdown, 0, dev);
17761da177e4SLinus Torvalds }
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds struct rt6_mtu_change_arg
17791da177e4SLinus Torvalds {
17801da177e4SLinus Torvalds 	struct net_device *dev;
17811da177e4SLinus Torvalds 	unsigned mtu;
17821da177e4SLinus Torvalds };
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
17851da177e4SLinus Torvalds {
17861da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
17871da177e4SLinus Torvalds 	struct inet6_dev *idev;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
17901da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
17911da177e4SLinus Torvalds 	   We still use this lock to block changes
17921da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
17931da177e4SLinus Torvalds 	*/
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
17961da177e4SLinus Torvalds 	if (idev == NULL)
17971da177e4SLinus Torvalds 		return 0;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
18001da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
18011da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
18021da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
18031da177e4SLinus Torvalds 	 */
18041da177e4SLinus Torvalds 	/*
18051da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
18061da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
18071da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
18081da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
18091da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
18101da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
18111da177e4SLinus Torvalds 	   PMTU discouvery.
18121da177e4SLinus Torvalds 	 */
18131da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
18141da177e4SLinus Torvalds 	    !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
18151da177e4SLinus Torvalds             (dst_mtu(&rt->u.dst) > arg->mtu ||
18161da177e4SLinus Torvalds              (dst_mtu(&rt->u.dst) < arg->mtu &&
18171da177e4SLinus Torvalds 	      dst_mtu(&rt->u.dst) == idev->cnf.mtu6)))
18181da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
18191da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(arg->mtu);
18201da177e4SLinus Torvalds 	return 0;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
18241da177e4SLinus Torvalds {
1825c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
1826c71099acSThomas Graf 		.dev = dev,
1827c71099acSThomas Graf 		.mtu = mtu,
1828c71099acSThomas Graf 	};
18291da177e4SLinus Torvalds 
1830c71099acSThomas Graf 	fib6_clean_all(rt6_mtu_change_route, 0, &arg);
18311da177e4SLinus Torvalds }
18321da177e4SLinus Torvalds 
183386872cb5SThomas Graf static struct nla_policy rtm_ipv6_policy[RTA_MAX+1] __read_mostly = {
183486872cb5SThomas Graf 	[RTA_GATEWAY]           = { .minlen = sizeof(struct in6_addr) },
183586872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
183686872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
183786872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
183886872cb5SThomas Graf };
183986872cb5SThomas Graf 
184086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
184186872cb5SThomas Graf 			      struct fib6_config *cfg)
18421da177e4SLinus Torvalds {
184386872cb5SThomas Graf 	struct rtmsg *rtm;
184486872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
184586872cb5SThomas Graf 	int err;
18461da177e4SLinus Torvalds 
184786872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
184886872cb5SThomas Graf 	if (err < 0)
184986872cb5SThomas Graf 		goto errout;
18501da177e4SLinus Torvalds 
185186872cb5SThomas Graf 	err = -EINVAL;
185286872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
185386872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
185486872cb5SThomas Graf 
185586872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
185686872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
185786872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
185886872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
185986872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
186086872cb5SThomas Graf 
186186872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
186286872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
186386872cb5SThomas Graf 
186486872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
186586872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
186686872cb5SThomas Graf 
186786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
186886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
186986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
18701da177e4SLinus Torvalds 	}
187186872cb5SThomas Graf 
187286872cb5SThomas Graf 	if (tb[RTA_DST]) {
187386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
187486872cb5SThomas Graf 
187586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
187686872cb5SThomas Graf 			goto errout;
187786872cb5SThomas Graf 
187886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
18791da177e4SLinus Torvalds 	}
188086872cb5SThomas Graf 
188186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
188286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
188386872cb5SThomas Graf 
188486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
188586872cb5SThomas Graf 			goto errout;
188686872cb5SThomas Graf 
188786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
18881da177e4SLinus Torvalds 	}
188986872cb5SThomas Graf 
189086872cb5SThomas Graf 	if (tb[RTA_OIF])
189186872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
189286872cb5SThomas Graf 
189386872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
189486872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
189586872cb5SThomas Graf 
189686872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
189786872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
189886872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
18991da177e4SLinus Torvalds 	}
190086872cb5SThomas Graf 
190186872cb5SThomas Graf 	if (tb[RTA_TABLE])
190286872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
190386872cb5SThomas Graf 
190486872cb5SThomas Graf 	err = 0;
190586872cb5SThomas Graf errout:
190686872cb5SThomas Graf 	return err;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
19101da177e4SLinus Torvalds {
191186872cb5SThomas Graf 	struct fib6_config cfg;
191286872cb5SThomas Graf 	int err;
19131da177e4SLinus Torvalds 
191486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
191586872cb5SThomas Graf 	if (err < 0)
191686872cb5SThomas Graf 		return err;
191786872cb5SThomas Graf 
191886872cb5SThomas Graf 	return ip6_route_del(&cfg);
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
19221da177e4SLinus Torvalds {
192386872cb5SThomas Graf 	struct fib6_config cfg;
192486872cb5SThomas Graf 	int err;
19251da177e4SLinus Torvalds 
192686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
192786872cb5SThomas Graf 	if (err < 0)
192886872cb5SThomas Graf 		return err;
192986872cb5SThomas Graf 
193086872cb5SThomas Graf 	return ip6_route_add(&cfg);
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
19340d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
19350d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
19360d51aa80SJamal Hadi Salim 			 int prefix, unsigned int flags)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	struct rtmsg *rtm;
19391da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
19401da177e4SLinus Torvalds 	struct rta_cacheinfo ci;
19419e762a4aSPatrick McHardy 	u32 table;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
19441da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
19451da177e4SLinus Torvalds 			/* success since this is not a prefix route */
19461da177e4SLinus Torvalds 			return 1;
19471da177e4SLinus Torvalds 		}
19481da177e4SLinus Torvalds 	}
19491da177e4SLinus Torvalds 
1950*2d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
1951*2d7202bfSThomas Graf 	if (nlh == NULL)
1952*2d7202bfSThomas Graf 		return -ENOBUFS;
1953*2d7202bfSThomas Graf 
1954*2d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
19551da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
19561da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
19571da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
19581da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
1959c71099acSThomas Graf 	if (rt->rt6i_table)
19609e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
1961c71099acSThomas Graf 	else
19629e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
19639e762a4aSPatrick McHardy 	rtm->rtm_table = table;
1964*2d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
19651da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
19661da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
19671da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
19681da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
19691da177e4SLinus Torvalds 	else
19701da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
19711da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
19721da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
19731da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
19741da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
19751da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
19761da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
19771da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
19781da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
19791da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
19821da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (dst) {
1985*2d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
19861da177e4SLinus Torvalds 	        rtm->rtm_dst_len = 128;
19871da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
1988*2d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
19891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
19901da177e4SLinus Torvalds 	if (src) {
1991*2d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
19921da177e4SLinus Torvalds 	        rtm->rtm_src_len = 128;
19931da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
1994*2d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
19951da177e4SLinus Torvalds #endif
19961da177e4SLinus Torvalds 	if (iif)
1997*2d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_IIF, iif);
19981da177e4SLinus Torvalds 	else if (dst) {
19991da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
20001da177e4SLinus Torvalds 		if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0)
2001*2d7202bfSThomas Graf 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
20021da177e4SLinus Torvalds 	}
2003*2d7202bfSThomas Graf 
20041da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2005*2d7202bfSThomas Graf 		goto nla_put_failure;
2006*2d7202bfSThomas Graf 
20071da177e4SLinus Torvalds 	if (rt->u.dst.neighbour)
2008*2d7202bfSThomas Graf 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
2009*2d7202bfSThomas Graf 
20101da177e4SLinus Torvalds 	if (rt->u.dst.dev)
2011*2d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2012*2d7202bfSThomas Graf 
2013*2d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
20141da177e4SLinus Torvalds 	ci.rta_lastuse = jiffies_to_clock_t(jiffies - rt->u.dst.lastuse);
20151da177e4SLinus Torvalds 	if (rt->rt6i_expires)
20161da177e4SLinus Torvalds 		ci.rta_expires = jiffies_to_clock_t(rt->rt6i_expires - jiffies);
20171da177e4SLinus Torvalds 	else
20181da177e4SLinus Torvalds 		ci.rta_expires = 0;
20191da177e4SLinus Torvalds 	ci.rta_used = rt->u.dst.__use;
20201da177e4SLinus Torvalds 	ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt);
20211da177e4SLinus Torvalds 	ci.rta_error = rt->u.dst.error;
20221da177e4SLinus Torvalds 	ci.rta_id = 0;
20231da177e4SLinus Torvalds 	ci.rta_ts = 0;
20241da177e4SLinus Torvalds 	ci.rta_tsage = 0;
2025*2d7202bfSThomas Graf 	NLA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
20261da177e4SLinus Torvalds 
2027*2d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
2028*2d7202bfSThomas Graf 
2029*2d7202bfSThomas Graf nla_put_failure:
2030*2d7202bfSThomas Graf 	return nlmsg_cancel(skb, nlh);
20311da177e4SLinus Torvalds }
20321da177e4SLinus Torvalds 
20331b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
20361da177e4SLinus Torvalds 	int prefix;
20371da177e4SLinus Torvalds 
2038*2d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2039*2d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
20401da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
20411da177e4SLinus Torvalds 	} else
20421da177e4SLinus Torvalds 		prefix = 0;
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds 	return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
20451da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
20460d51aa80SJamal Hadi Salim 		     prefix, NLM_F_MULTI);
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	struct rtattr **rta = arg;
20521da177e4SLinus Torvalds 	int iif = 0;
20531da177e4SLinus Torvalds 	int err = -ENOBUFS;
20541da177e4SLinus Torvalds 	struct sk_buff *skb;
20551da177e4SLinus Torvalds 	struct flowi fl;
20561da177e4SLinus Torvalds 	struct rt6_info *rt;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
20591da177e4SLinus Torvalds 	if (skb == NULL)
20601da177e4SLinus Torvalds 		goto out;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
20631da177e4SLinus Torvalds 	   through good chunk of routing engine.
20641da177e4SLinus Torvalds 	 */
20651da177e4SLinus Torvalds 	skb->mac.raw = skb->data;
20661da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	memset(&fl, 0, sizeof(fl));
20691da177e4SLinus Torvalds 	if (rta[RTA_SRC-1])
20701da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_src,
20711da177e4SLinus Torvalds 			       (struct in6_addr*)RTA_DATA(rta[RTA_SRC-1]));
20721da177e4SLinus Torvalds 	if (rta[RTA_DST-1])
20731da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst,
20741da177e4SLinus Torvalds 			       (struct in6_addr*)RTA_DATA(rta[RTA_DST-1]));
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	if (rta[RTA_IIF-1])
20771da177e4SLinus Torvalds 		memcpy(&iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	if (iif) {
20801da177e4SLinus Torvalds 		struct net_device *dev;
20811da177e4SLinus Torvalds 		dev = __dev_get_by_index(iif);
20821da177e4SLinus Torvalds 		if (!dev) {
20831da177e4SLinus Torvalds 			err = -ENODEV;
20841da177e4SLinus Torvalds 			goto out_free;
20851da177e4SLinus Torvalds 		}
20861da177e4SLinus Torvalds 	}
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	fl.oif = 0;
20891da177e4SLinus Torvalds 	if (rta[RTA_OIF-1])
20901da177e4SLinus Torvalds 		memcpy(&fl.oif, RTA_DATA(rta[RTA_OIF-1]), sizeof(int));
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	rt = (struct rt6_info*)ip6_route_output(NULL, &fl);
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
20971da177e4SLinus Torvalds 	err = rt6_fill_node(skb, rt,
20981da177e4SLinus Torvalds 			    &fl.fl6_dst, &fl.fl6_src,
20991da177e4SLinus Torvalds 			    iif,
21001da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
21010d51aa80SJamal Hadi Salim 			    nlh->nlmsg_seq, 0, 0);
21021da177e4SLinus Torvalds 	if (err < 0) {
21031da177e4SLinus Torvalds 		err = -EMSGSIZE;
21041da177e4SLinus Torvalds 		goto out_free;
21051da177e4SLinus Torvalds 	}
21061da177e4SLinus Torvalds 
21072942e900SThomas Graf 	err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
21081da177e4SLinus Torvalds out:
21091da177e4SLinus Torvalds 	return err;
21101da177e4SLinus Torvalds out_free:
21111da177e4SLinus Torvalds 	kfree_skb(skb);
21121da177e4SLinus Torvalds 	goto out;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
211586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
21161da177e4SLinus Torvalds {
21171da177e4SLinus Torvalds 	struct sk_buff *skb;
211886872cb5SThomas Graf 	u32 pid = 0, seq = 0;
211986872cb5SThomas Graf 	struct nlmsghdr *nlh = NULL;
212021713ebcSThomas Graf 	int payload = sizeof(struct rtmsg) + 256;
212121713ebcSThomas Graf 	int err = -ENOBUFS;
21220d51aa80SJamal Hadi Salim 
212386872cb5SThomas Graf 	if (info) {
212486872cb5SThomas Graf 		pid = info->pid;
212586872cb5SThomas Graf 		nlh = info->nlh;
212686872cb5SThomas Graf 		if (nlh)
212786872cb5SThomas Graf 			seq = nlh->nlmsg_seq;
212886872cb5SThomas Graf 	}
212986872cb5SThomas Graf 
213021713ebcSThomas Graf 	skb = nlmsg_new(nlmsg_total_size(payload), gfp_any());
213121713ebcSThomas Graf 	if (skb == NULL)
213221713ebcSThomas Graf 		goto errout;
21331da177e4SLinus Torvalds 
213421713ebcSThomas Graf 	err = rt6_fill_node(skb, rt, NULL, NULL, 0, event, pid, seq, 0, 0);
213521713ebcSThomas Graf 	if (err < 0) {
21361da177e4SLinus Torvalds 		kfree_skb(skb);
213721713ebcSThomas Graf 		goto errout;
21381da177e4SLinus Torvalds 	}
213921713ebcSThomas Graf 
214021713ebcSThomas Graf 	err = rtnl_notify(skb, pid, RTNLGRP_IPV6_ROUTE, nlh, gfp_any());
214121713ebcSThomas Graf errout:
214221713ebcSThomas Graf 	if (err < 0)
214321713ebcSThomas Graf 		rtnl_set_sk_err(RTNLGRP_IPV6_ROUTE, err);
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds /*
21471da177e4SLinus Torvalds  *	/proc
21481da177e4SLinus Torvalds  */
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds struct rt6_proc_arg
21551da177e4SLinus Torvalds {
21561da177e4SLinus Torvalds 	char *buffer;
21571da177e4SLinus Torvalds 	int offset;
21581da177e4SLinus Torvalds 	int length;
21591da177e4SLinus Torvalds 	int skip;
21601da177e4SLinus Torvalds 	int len;
21611da177e4SLinus Torvalds };
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
21661da177e4SLinus Torvalds 	int i;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	if (arg->skip < arg->offset / RT6_INFO_LEN) {
21691da177e4SLinus Torvalds 		arg->skip++;
21701da177e4SLinus Torvalds 		return 0;
21711da177e4SLinus Torvalds 	}
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	if (arg->len >= arg->length)
21741da177e4SLinus Torvalds 		return 0;
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	for (i=0; i<16; i++) {
21771da177e4SLinus Torvalds 		sprintf(arg->buffer + arg->len, "%02x",
21781da177e4SLinus Torvalds 			rt->rt6i_dst.addr.s6_addr[i]);
21791da177e4SLinus Torvalds 		arg->len += 2;
21801da177e4SLinus Torvalds 	}
21811da177e4SLinus Torvalds 	arg->len += sprintf(arg->buffer + arg->len, " %02x ",
21821da177e4SLinus Torvalds 			    rt->rt6i_dst.plen);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
21851da177e4SLinus Torvalds 	for (i=0; i<16; i++) {
21861da177e4SLinus Torvalds 		sprintf(arg->buffer + arg->len, "%02x",
21871da177e4SLinus Torvalds 			rt->rt6i_src.addr.s6_addr[i]);
21881da177e4SLinus Torvalds 		arg->len += 2;
21891da177e4SLinus Torvalds 	}
21901da177e4SLinus Torvalds 	arg->len += sprintf(arg->buffer + arg->len, " %02x ",
21911da177e4SLinus Torvalds 			    rt->rt6i_src.plen);
21921da177e4SLinus Torvalds #else
21931da177e4SLinus Torvalds 	sprintf(arg->buffer + arg->len,
21941da177e4SLinus Torvalds 		"00000000000000000000000000000000 00 ");
21951da177e4SLinus Torvalds 	arg->len += 36;
21961da177e4SLinus Torvalds #endif
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
21991da177e4SLinus Torvalds 		for (i=0; i<16; i++) {
22001da177e4SLinus Torvalds 			sprintf(arg->buffer + arg->len, "%02x",
22011da177e4SLinus Torvalds 				rt->rt6i_nexthop->primary_key[i]);
22021da177e4SLinus Torvalds 			arg->len += 2;
22031da177e4SLinus Torvalds 		}
22041da177e4SLinus Torvalds 	} else {
22051da177e4SLinus Torvalds 		sprintf(arg->buffer + arg->len,
22061da177e4SLinus Torvalds 			"00000000000000000000000000000000");
22071da177e4SLinus Torvalds 		arg->len += 32;
22081da177e4SLinus Torvalds 	}
22091da177e4SLinus Torvalds 	arg->len += sprintf(arg->buffer + arg->len,
22101da177e4SLinus Torvalds 			    " %08x %08x %08x %08x %8s\n",
22111da177e4SLinus Torvalds 			    rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
22121da177e4SLinus Torvalds 			    rt->u.dst.__use, rt->rt6i_flags,
22131da177e4SLinus Torvalds 			    rt->rt6i_dev ? rt->rt6i_dev->name : "");
22141da177e4SLinus Torvalds 	return 0;
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
22181da177e4SLinus Torvalds {
2219c71099acSThomas Graf 	struct rt6_proc_arg arg = {
2220c71099acSThomas Graf 		.buffer = buffer,
2221c71099acSThomas Graf 		.offset = offset,
2222c71099acSThomas Graf 		.length = length,
2223c71099acSThomas Graf 	};
22241da177e4SLinus Torvalds 
2225c71099acSThomas Graf 	fib6_clean_all(rt6_info_route, 0, &arg);
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	*start = buffer;
22281da177e4SLinus Torvalds 	if (offset)
22291da177e4SLinus Torvalds 		*start += offset % RT6_INFO_LEN;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	arg.len -= offset % RT6_INFO_LEN;
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	if (arg.len > length)
22341da177e4SLinus Torvalds 		arg.len = length;
22351da177e4SLinus Torvalds 	if (arg.len < 0)
22361da177e4SLinus Torvalds 		arg.len = 0;
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	return arg.len;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
22441da177e4SLinus Torvalds 		      rt6_stats.fib_nodes, rt6_stats.fib_route_nodes,
22451da177e4SLinus Torvalds 		      rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries,
22461da177e4SLinus Torvalds 		      rt6_stats.fib_rt_cache,
22471da177e4SLinus Torvalds 		      atomic_read(&ip6_dst_ops.entries),
22481da177e4SLinus Torvalds 		      rt6_stats.fib_discarded_routes);
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	return 0;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
22541da177e4SLinus Torvalds {
22551da177e4SLinus Torvalds 	return single_open(file, rt6_stats_seq_show, NULL);
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static struct file_operations rt6_stats_seq_fops = {
22591da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
22601da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
22611da177e4SLinus Torvalds 	.read	 = seq_read,
22621da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
22631da177e4SLinus Torvalds 	.release = single_release,
22641da177e4SLinus Torvalds };
22651da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds static int flush_delay;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds static
22721da177e4SLinus Torvalds int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
22731da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
22741da177e4SLinus Torvalds {
22751da177e4SLinus Torvalds 	if (write) {
22761da177e4SLinus Torvalds 		proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
22771da177e4SLinus Torvalds 		fib6_run_gc(flush_delay <= 0 ? ~0UL : (unsigned long)flush_delay);
22781da177e4SLinus Torvalds 		return 0;
22791da177e4SLinus Torvalds 	} else
22801da177e4SLinus Torvalds 		return -EINVAL;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds ctl_table ipv6_route_table[] = {
22841da177e4SLinus Torvalds         {
22851da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_FLUSH,
22861da177e4SLinus Torvalds 		.procname	=	"flush",
22871da177e4SLinus Torvalds          	.data		=	&flush_delay,
22881da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
228989c8b3a1SDave Jones 		.mode		=	0200,
22901da177e4SLinus Torvalds          	.proc_handler	=	&ipv6_sysctl_rtcache_flush
22911da177e4SLinus Torvalds 	},
22921da177e4SLinus Torvalds 	{
22931da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_THRESH,
22941da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
22951da177e4SLinus Torvalds          	.data		=	&ip6_dst_ops.gc_thresh,
22961da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
22971da177e4SLinus Torvalds 		.mode		=	0644,
22981da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec,
22991da177e4SLinus Torvalds 	},
23001da177e4SLinus Torvalds 	{
23011da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MAX_SIZE,
23021da177e4SLinus Torvalds 		.procname	=	"max_size",
23031da177e4SLinus Torvalds          	.data		=	&ip6_rt_max_size,
23041da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23051da177e4SLinus Torvalds 		.mode		=	0644,
23061da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec,
23071da177e4SLinus Torvalds 	},
23081da177e4SLinus Torvalds 	{
23091da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL,
23101da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
23111da177e4SLinus Torvalds          	.data		=	&ip6_rt_gc_min_interval,
23121da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23131da177e4SLinus Torvalds 		.mode		=	0644,
23141da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23151da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23161da177e4SLinus Torvalds 	},
23171da177e4SLinus Torvalds 	{
23181da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_TIMEOUT,
23191da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
23201da177e4SLinus Torvalds          	.data		=	&ip6_rt_gc_timeout,
23211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23221da177e4SLinus Torvalds 		.mode		=	0644,
23231da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23241da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23251da177e4SLinus Torvalds 	},
23261da177e4SLinus Torvalds 	{
23271da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_INTERVAL,
23281da177e4SLinus Torvalds 		.procname	=	"gc_interval",
23291da177e4SLinus Torvalds          	.data		=	&ip6_rt_gc_interval,
23301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23311da177e4SLinus Torvalds 		.mode		=	0644,
23321da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23331da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23341da177e4SLinus Torvalds 	},
23351da177e4SLinus Torvalds 	{
23361da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_ELASTICITY,
23371da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
23381da177e4SLinus Torvalds          	.data		=	&ip6_rt_gc_elasticity,
23391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23401da177e4SLinus Torvalds 		.mode		=	0644,
23411da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23421da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23431da177e4SLinus Torvalds 	},
23441da177e4SLinus Torvalds 	{
23451da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MTU_EXPIRES,
23461da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
23471da177e4SLinus Torvalds          	.data		=	&ip6_rt_mtu_expires,
23481da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23491da177e4SLinus Torvalds 		.mode		=	0644,
23501da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23511da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23521da177e4SLinus Torvalds 	},
23531da177e4SLinus Torvalds 	{
23541da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MIN_ADVMSS,
23551da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
23561da177e4SLinus Torvalds          	.data		=	&ip6_rt_min_advmss,
23571da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23581da177e4SLinus Torvalds 		.mode		=	0644,
23591da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_jiffies,
23601da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
23611da177e4SLinus Torvalds 	},
23621da177e4SLinus Torvalds 	{
23631da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
23641da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
23651da177e4SLinus Torvalds          	.data		=	&ip6_rt_gc_min_interval,
23661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
23671da177e4SLinus Torvalds 		.mode		=	0644,
23681da177e4SLinus Torvalds          	.proc_handler	=	&proc_dointvec_ms_jiffies,
23691da177e4SLinus Torvalds 		.strategy	=	&sysctl_ms_jiffies,
23701da177e4SLinus Torvalds 	},
23711da177e4SLinus Torvalds 	{ .ctl_name = 0 }
23721da177e4SLinus Torvalds };
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds #endif
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds void __init ip6_route_init(void)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	struct proc_dir_entry *p;
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	ip6_dst_ops.kmem_cachep = kmem_cache_create("ip6_dst_cache",
23811da177e4SLinus Torvalds 						     sizeof(struct rt6_info),
23821da177e4SLinus Torvalds 						     0, SLAB_HWCACHE_ALIGN,
23831da177e4SLinus Torvalds 						     NULL, NULL);
23841da177e4SLinus Torvalds 	if (!ip6_dst_ops.kmem_cachep)
23851da177e4SLinus Torvalds 		panic("cannot create ip6_dst_cache");
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	fib6_init();
23881da177e4SLinus Torvalds #ifdef 	CONFIG_PROC_FS
23891da177e4SLinus Torvalds 	p = proc_net_create("ipv6_route", 0, rt6_proc_info);
23901da177e4SLinus Torvalds 	if (p)
23911da177e4SLinus Torvalds 		p->owner = THIS_MODULE;
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	proc_net_fops_create("rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
23941da177e4SLinus Torvalds #endif
23951da177e4SLinus Torvalds #ifdef CONFIG_XFRM
23961da177e4SLinus Torvalds 	xfrm6_init();
23971da177e4SLinus Torvalds #endif
2398101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2399101367c2SThomas Graf 	fib6_rules_init();
2400101367c2SThomas Graf #endif
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds void ip6_route_cleanup(void)
24041da177e4SLinus Torvalds {
2405101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2406101367c2SThomas Graf 	fib6_rules_cleanup();
2407101367c2SThomas Graf #endif
24081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
24091da177e4SLinus Torvalds 	proc_net_remove("ipv6_route");
24101da177e4SLinus Torvalds 	proc_net_remove("rt6_stats");
24111da177e4SLinus Torvalds #endif
24121da177e4SLinus Torvalds #ifdef CONFIG_XFRM
24131da177e4SLinus Torvalds 	xfrm6_fini();
24141da177e4SLinus Torvalds #endif
24151da177e4SLinus Torvalds 	rt6_ifdown(NULL);
24161da177e4SLinus Torvalds 	fib6_gc_cleanup();
24171da177e4SLinus Torvalds 	kmem_cache_destroy(ip6_dst_ops.kmem_cachep);
24181da177e4SLinus Torvalds }
2419