xref: /openbmc/linux/net/ipv6/route.c (revision 50eb431d6e98189eb40606fcd4d03ecd8e168afa)
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.
25c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
26c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
311da177e4SLinus Torvalds #include <linux/types.h>
321da177e4SLinus Torvalds #include <linux/times.h>
331da177e4SLinus Torvalds #include <linux/socket.h>
341da177e4SLinus Torvalds #include <linux/sockios.h>
351da177e4SLinus Torvalds #include <linux/net.h>
361da177e4SLinus Torvalds #include <linux/route.h>
371da177e4SLinus Torvalds #include <linux/netdevice.h>
381da177e4SLinus Torvalds #include <linux/in6.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/if_arp.h>
411da177e4SLinus Torvalds #include <linux/proc_fs.h>
421da177e4SLinus Torvalds #include <linux/seq_file.h>
43457c4cbcSEric W. Biederman #include <net/net_namespace.h>
441da177e4SLinus Torvalds #include <net/snmp.h>
451da177e4SLinus Torvalds #include <net/ipv6.h>
461da177e4SLinus Torvalds #include <net/ip6_fib.h>
471da177e4SLinus Torvalds #include <net/ip6_route.h>
481da177e4SLinus Torvalds #include <net/ndisc.h>
491da177e4SLinus Torvalds #include <net/addrconf.h>
501da177e4SLinus Torvalds #include <net/tcp.h>
511da177e4SLinus Torvalds #include <linux/rtnetlink.h>
521da177e4SLinus Torvalds #include <net/dst.h>
531da177e4SLinus Torvalds #include <net/xfrm.h>
548d71740cSTom Tucker #include <net/netevent.h>
5521713ebcSThomas Graf #include <net/netlink.h>
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #include <asm/uaccess.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
601da177e4SLinus Torvalds #include <linux/sysctl.h>
611da177e4SLinus Torvalds #endif
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Set to 3 to get tracing. */
641da177e4SLinus Torvalds #define RT6_DEBUG 2
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #if RT6_DEBUG >= 3
671da177e4SLinus Torvalds #define RDBG(x) printk x
681da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
691da177e4SLinus Torvalds #else
701da177e4SLinus Torvalds #define RDBG(x)
711da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
74519fbd87SYOSHIFUJI Hideaki #define CLONE_OFFLINK_ROUTE 0
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
771da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
781da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
791da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
801da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
811da177e4SLinus Torvalds 				       struct net_device *dev, int how);
821da177e4SLinus Torvalds static int		 ip6_dst_gc(void);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
851da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
861da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
871da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
881da177e4SLinus Torvalds 
8970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
9070ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
9170ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
9270ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
9370ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
9470ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex);
9570ceb4f5SYOSHIFUJI Hideaki #endif
9670ceb4f5SYOSHIFUJI Hideaki 
971da177e4SLinus Torvalds static struct dst_ops ip6_dst_ops = {
981da177e4SLinus Torvalds 	.family			=	AF_INET6,
991da177e4SLinus Torvalds 	.protocol		=	__constant_htons(ETH_P_IPV6),
1001da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1011da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1021da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1031da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1041da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1051da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1061da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1071da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
108862b82c6SHerbert Xu 	.local_out		=	ip6_local_out,
1091da177e4SLinus Torvalds 	.entry_size		=	sizeof(struct rt6_info),
1101da177e4SLinus Torvalds };
1111da177e4SLinus Torvalds 
11214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
11314e50e57SDavid S. Miller {
11414e50e57SDavid S. Miller }
11514e50e57SDavid S. Miller 
11614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
11714e50e57SDavid S. Miller 	.family			=	AF_INET6,
11814e50e57SDavid S. Miller 	.protocol		=	__constant_htons(ETH_P_IPV6),
11914e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
12014e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
12114e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
12214e50e57SDavid S. Miller 	.entry_size		=	sizeof(struct rt6_info),
12314e50e57SDavid S. Miller };
12414e50e57SDavid S. Miller 
1251da177e4SLinus Torvalds struct rt6_info ip6_null_entry = {
1261da177e4SLinus Torvalds 	.u = {
1271da177e4SLinus Torvalds 		.dst = {
1281da177e4SLinus Torvalds 			.__refcnt	= ATOMIC_INIT(1),
1291da177e4SLinus Torvalds 			.__use		= 1,
1301da177e4SLinus Torvalds 			.obsolete	= -1,
1311da177e4SLinus Torvalds 			.error		= -ENETUNREACH,
1321da177e4SLinus Torvalds 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1331da177e4SLinus Torvalds 			.input		= ip6_pkt_discard,
1341da177e4SLinus Torvalds 			.output		= ip6_pkt_discard_out,
1351da177e4SLinus Torvalds 			.ops		= &ip6_dst_ops,
1361da177e4SLinus Torvalds 			.path		= (struct dst_entry*)&ip6_null_entry,
1371da177e4SLinus Torvalds 		}
1381da177e4SLinus Torvalds 	},
1391da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1401da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1411da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
144101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
145101367c2SThomas Graf 
1466723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
1476723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
1486723ab54SDavid S. Miller 
149101367c2SThomas Graf struct rt6_info ip6_prohibit_entry = {
150101367c2SThomas Graf 	.u = {
151101367c2SThomas Graf 		.dst = {
152101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
153101367c2SThomas Graf 			.__use		= 1,
154101367c2SThomas Graf 			.obsolete	= -1,
155101367c2SThomas Graf 			.error		= -EACCES,
156101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1579ce8ade0SThomas Graf 			.input		= ip6_pkt_prohibit,
1589ce8ade0SThomas Graf 			.output		= ip6_pkt_prohibit_out,
159101367c2SThomas Graf 			.ops		= &ip6_dst_ops,
160101367c2SThomas Graf 			.path		= (struct dst_entry*)&ip6_prohibit_entry,
161101367c2SThomas Graf 		}
162101367c2SThomas Graf 	},
163101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
164101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
165101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
166101367c2SThomas Graf };
167101367c2SThomas Graf 
168101367c2SThomas Graf struct rt6_info ip6_blk_hole_entry = {
169101367c2SThomas Graf 	.u = {
170101367c2SThomas Graf 		.dst = {
171101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
172101367c2SThomas Graf 			.__use		= 1,
173101367c2SThomas Graf 			.obsolete	= -1,
174101367c2SThomas Graf 			.error		= -EINVAL,
175101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
176352e512cSHerbert Xu 			.input		= dst_discard,
177352e512cSHerbert Xu 			.output		= dst_discard,
178101367c2SThomas Graf 			.ops		= &ip6_dst_ops,
179101367c2SThomas Graf 			.path		= (struct dst_entry*)&ip6_blk_hole_entry,
180101367c2SThomas Graf 		}
181101367c2SThomas Graf 	},
182101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
183101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
184101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
185101367c2SThomas Graf };
186101367c2SThomas Graf 
187101367c2SThomas Graf #endif
188101367c2SThomas Graf 
1891da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
1901da177e4SLinus Torvalds static __inline__ struct rt6_info *ip6_dst_alloc(void)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	return (struct rt6_info *)dst_alloc(&ip6_dst_ops);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
1981da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (idev != NULL) {
2011da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2021da177e4SLinus Torvalds 		in6_dev_put(idev);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2071da177e4SLinus Torvalds 			   int how)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2101da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2115a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
2125a3e55d6SDenis V. Lunev 		dev->nd_net->loopback_dev;
2131da177e4SLinus Torvalds 
2145a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
2155a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
2165a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
2171da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
2181da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
2191da177e4SLinus Torvalds 			in6_dev_put(idev);
2201da177e4SLinus Torvalds 		}
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	return (rt->rt6i_flags & RTF_EXPIRES &&
2271da177e4SLinus Torvalds 		time_after(jiffies, rt->rt6i_expires));
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230c71099acSThomas Graf static inline int rt6_need_strict(struct in6_addr *daddr)
231c71099acSThomas Graf {
232c71099acSThomas Graf 	return (ipv6_addr_type(daddr) &
233c71099acSThomas Graf 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
234c71099acSThomas Graf }
235c71099acSThomas Graf 
2361da177e4SLinus Torvalds /*
237c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
2381da177e4SLinus Torvalds  */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static __inline__ struct rt6_info *rt6_device_match(struct rt6_info *rt,
2411da177e4SLinus Torvalds 						    int oif,
2421da177e4SLinus Torvalds 						    int strict)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
2451da177e4SLinus Torvalds 	struct rt6_info *sprt;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	if (oif) {
2487cc48263SEric Dumazet 		for (sprt = rt; sprt; sprt = sprt->u.dst.rt6_next) {
2491da177e4SLinus Torvalds 			struct net_device *dev = sprt->rt6i_dev;
2501da177e4SLinus Torvalds 			if (dev->ifindex == oif)
2511da177e4SLinus Torvalds 				return sprt;
2521da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
2531da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
2541da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
2551da177e4SLinus Torvalds 					if (strict && oif)
2561da177e4SLinus Torvalds 						continue;
2571da177e4SLinus Torvalds 					if (local && (!oif ||
2581da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
2591da177e4SLinus Torvalds 						continue;
2601da177e4SLinus Torvalds 				}
2611da177e4SLinus Torvalds 				local = sprt;
2621da177e4SLinus Torvalds 			}
2631da177e4SLinus Torvalds 		}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 		if (local)
2661da177e4SLinus Torvalds 			return local;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		if (strict)
2691da177e4SLinus Torvalds 			return &ip6_null_entry;
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 	return rt;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
27427097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
27527097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
27627097255SYOSHIFUJI Hideaki {
27727097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
27827097255SYOSHIFUJI Hideaki 	/*
27927097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
28027097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
28127097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
28227097255SYOSHIFUJI Hideaki 	 *
28327097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
28427097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
28527097255SYOSHIFUJI Hideaki 	 */
28627097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
28727097255SYOSHIFUJI Hideaki 		return;
28827097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
28927097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
29052e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
29127097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
29227097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
29327097255SYOSHIFUJI Hideaki 
29427097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
29527097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
29627097255SYOSHIFUJI Hideaki 
29727097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
29827097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
29927097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
30027097255SYOSHIFUJI Hideaki 	} else
30127097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
30227097255SYOSHIFUJI Hideaki }
30327097255SYOSHIFUJI Hideaki #else
30427097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
30527097255SYOSHIFUJI Hideaki {
30627097255SYOSHIFUJI Hideaki 	return;
30727097255SYOSHIFUJI Hideaki }
30827097255SYOSHIFUJI Hideaki #endif
30927097255SYOSHIFUJI Hideaki 
3101da177e4SLinus Torvalds /*
311554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3121da177e4SLinus Torvalds  */
313b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
3141da177e4SLinus Torvalds {
315554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
316161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
317554cfb7eSYOSHIFUJI Hideaki 		return 2;
318161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
319161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
320161980f4SDavid S. Miller 		return 1;
321554cfb7eSYOSHIFUJI Hideaki 	return 0;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
3251da177e4SLinus Torvalds {
326554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
327398bcbebSYOSHIFUJI Hideaki 	int m;
3284d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
3294d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
3304d0c5911SYOSHIFUJI Hideaki 		m = 1;
3314d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
3321da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
333554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
3344d0c5911SYOSHIFUJI Hideaki 			m = 2;
335398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
336398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
337398bcbebSYOSHIFUJI Hideaki 			m = 0;
338398bcbebSYOSHIFUJI Hideaki #endif
339398bcbebSYOSHIFUJI Hideaki 		else
340ea73ee23SYOSHIFUJI Hideaki 			m = 1;
3411da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
342398bcbebSYOSHIFUJI Hideaki 	} else
343398bcbebSYOSHIFUJI Hideaki 		m = 0;
344554cfb7eSYOSHIFUJI Hideaki 	return m;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
348554cfb7eSYOSHIFUJI Hideaki 			   int strict)
349554cfb7eSYOSHIFUJI Hideaki {
3504d0c5911SYOSHIFUJI Hideaki 	int m, n;
3514d0c5911SYOSHIFUJI Hideaki 
3524d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
35377d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
354554cfb7eSYOSHIFUJI Hideaki 		return -1;
355ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
356ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
357ebacaaa0SYOSHIFUJI Hideaki #endif
3584d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
359557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
360554cfb7eSYOSHIFUJI Hideaki 		return -1;
361554cfb7eSYOSHIFUJI Hideaki 	return m;
362554cfb7eSYOSHIFUJI Hideaki }
363554cfb7eSYOSHIFUJI Hideaki 
364f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
365f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
366554cfb7eSYOSHIFUJI Hideaki {
367554cfb7eSYOSHIFUJI Hideaki 	int m;
368554cfb7eSYOSHIFUJI Hideaki 
369554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
370f11e6659SDavid S. Miller 		goto out;
371554cfb7eSYOSHIFUJI Hideaki 
372554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
373554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
374f11e6659SDavid S. Miller 		goto out;
375554cfb7eSYOSHIFUJI Hideaki 
376f11e6659SDavid S. Miller 	if (m > *mpri) {
377ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
37827097255SYOSHIFUJI Hideaki 			rt6_probe(match);
379f11e6659SDavid S. Miller 		*mpri = m;
380554cfb7eSYOSHIFUJI Hideaki 		match = rt;
381ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
38227097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
3831da177e4SLinus Torvalds 	}
384f11e6659SDavid S. Miller 
385f11e6659SDavid S. Miller out:
386f11e6659SDavid S. Miller 	return match;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
390f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
391f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
392f11e6659SDavid S. Miller {
393f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
394f11e6659SDavid S. Miller 	int mpri = -1;
395f11e6659SDavid S. Miller 
396f11e6659SDavid S. Miller 	match = NULL;
397f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
398f11e6659SDavid S. Miller 	     rt = rt->u.dst.rt6_next)
399f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
400f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
401f11e6659SDavid S. Miller 	     rt = rt->u.dst.rt6_next)
402f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
403f11e6659SDavid S. Miller 
404f11e6659SDavid S. Miller 	return match;
405f11e6659SDavid S. Miller }
406f11e6659SDavid S. Miller 
407f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
408f11e6659SDavid S. Miller {
409f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
410f11e6659SDavid S. Miller 
411f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
412f11e6659SDavid S. Miller 		  __FUNCTION__, fn->leaf, oif);
413f11e6659SDavid S. Miller 
414f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
415f11e6659SDavid S. Miller 	if (!rt0)
416f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
417f11e6659SDavid S. Miller 
418f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
419f11e6659SDavid S. Miller 
420554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
421f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
422f11e6659SDavid S. Miller 		struct rt6_info *next = rt0->u.dst.rt6_next;
423f11e6659SDavid S. Miller 
424554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
425f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
426f11e6659SDavid S. Miller 			next = fn->leaf;
427f11e6659SDavid S. Miller 
428f11e6659SDavid S. Miller 		if (next != rt0)
429f11e6659SDavid S. Miller 			fn->rr_ptr = next;
430554cfb7eSYOSHIFUJI Hideaki 	}
431554cfb7eSYOSHIFUJI Hideaki 
432f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
433f11e6659SDavid S. Miller 		  __FUNCTION__, match);
434554cfb7eSYOSHIFUJI Hideaki 
435554cfb7eSYOSHIFUJI Hideaki 	return (match ? match : &ip6_null_entry);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
43870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
43970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
44070ceb4f5SYOSHIFUJI Hideaki 		  struct in6_addr *gwaddr)
44170ceb4f5SYOSHIFUJI Hideaki {
44270ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
44370ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
44470ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
44570ceb4f5SYOSHIFUJI Hideaki 	u32 lifetime;
44670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
44770ceb4f5SYOSHIFUJI Hideaki 
44870ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
44970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45070ceb4f5SYOSHIFUJI Hideaki 	}
45170ceb4f5SYOSHIFUJI Hideaki 
45270ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
45370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
45470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
45670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
45870ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
45970ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
46070ceb4f5SYOSHIFUJI Hideaki 		}
46170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
46270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
46370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
46470ceb4f5SYOSHIFUJI Hideaki 		}
46570ceb4f5SYOSHIFUJI Hideaki 	}
46670ceb4f5SYOSHIFUJI Hideaki 
46770ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
46870ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
46970ceb4f5SYOSHIFUJI Hideaki 		pref = ICMPV6_ROUTER_PREF_MEDIUM;
47070ceb4f5SYOSHIFUJI Hideaki 
471e69a4adcSAl Viro 	lifetime = ntohl(rinfo->lifetime);
47270ceb4f5SYOSHIFUJI Hideaki 	if (lifetime == 0xffffffff) {
47370ceb4f5SYOSHIFUJI Hideaki 		/* infinity */
47470ceb4f5SYOSHIFUJI Hideaki 	} else if (lifetime > 0x7fffffff/HZ) {
47570ceb4f5SYOSHIFUJI Hideaki 		/* Avoid arithmetic overflow */
47670ceb4f5SYOSHIFUJI Hideaki 		lifetime = 0x7fffffff/HZ - 1;
47770ceb4f5SYOSHIFUJI Hideaki 	}
47870ceb4f5SYOSHIFUJI Hideaki 
47970ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
48070ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
48170ceb4f5SYOSHIFUJI Hideaki 	else {
48270ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
48370ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
48470ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
48570ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
48670ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
48770ceb4f5SYOSHIFUJI Hideaki 	}
48870ceb4f5SYOSHIFUJI Hideaki 
48970ceb4f5SYOSHIFUJI Hideaki 	rt = rt6_get_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex);
49070ceb4f5SYOSHIFUJI Hideaki 
49170ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
492e0a1ad73SThomas Graf 		ip6_del_rt(rt);
49370ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
49470ceb4f5SYOSHIFUJI Hideaki 	}
49570ceb4f5SYOSHIFUJI Hideaki 
49670ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
49770ceb4f5SYOSHIFUJI Hideaki 		rt = rt6_add_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
49870ceb4f5SYOSHIFUJI Hideaki 					pref);
49970ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
50070ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
50170ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
50270ceb4f5SYOSHIFUJI Hideaki 
50370ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
50470ceb4f5SYOSHIFUJI Hideaki 		if (lifetime == 0xffffffff) {
50570ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
50670ceb4f5SYOSHIFUJI Hideaki 		} else {
50770ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
50870ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
50970ceb4f5SYOSHIFUJI Hideaki 		}
51070ceb4f5SYOSHIFUJI Hideaki 		dst_release(&rt->u.dst);
51170ceb4f5SYOSHIFUJI Hideaki 	}
51270ceb4f5SYOSHIFUJI Hideaki 	return 0;
51370ceb4f5SYOSHIFUJI Hideaki }
51470ceb4f5SYOSHIFUJI Hideaki #endif
51570ceb4f5SYOSHIFUJI Hideaki 
516982f56f3SYOSHIFUJI Hideaki #define BACKTRACK(saddr) \
517982f56f3SYOSHIFUJI Hideaki do { \
518982f56f3SYOSHIFUJI Hideaki 	if (rt == &ip6_null_entry) { \
519982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
520e0eda7bbSVille Nuorvala 		while (1) { \
521982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
522c71099acSThomas Graf 				goto out; \
523982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
524982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
5258bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
526982f56f3SYOSHIFUJI Hideaki 			else \
527982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
528c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
529c71099acSThomas Graf 				goto restart; \
530c71099acSThomas Graf 		} \
531982f56f3SYOSHIFUJI Hideaki 	} \
532982f56f3SYOSHIFUJI Hideaki } while(0)
533c71099acSThomas Graf 
534c71099acSThomas Graf static struct rt6_info *ip6_pol_route_lookup(struct fib6_table *table,
535c71099acSThomas Graf 					     struct flowi *fl, int flags)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct fib6_node *fn;
5381da177e4SLinus Torvalds 	struct rt6_info *rt;
5391da177e4SLinus Torvalds 
540c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
541c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
542c71099acSThomas Graf restart:
543c71099acSThomas Graf 	rt = fn->leaf;
54477d16f45SYOSHIFUJI Hideaki 	rt = rt6_device_match(rt, fl->oif, flags);
545982f56f3SYOSHIFUJI Hideaki 	BACKTRACK(&fl->fl6_src);
546c71099acSThomas Graf out:
54703f49f34SPavel Emelyanov 	dst_use(&rt->u.dst, jiffies);
548c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
5491da177e4SLinus Torvalds 	return rt;
550c71099acSThomas Graf 
551c71099acSThomas Graf }
552c71099acSThomas Graf 
553c71099acSThomas Graf struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr,
554c71099acSThomas Graf 			    int oif, int strict)
555c71099acSThomas Graf {
556c71099acSThomas Graf 	struct flowi fl = {
557c71099acSThomas Graf 		.oif = oif,
558c71099acSThomas Graf 		.nl_u = {
559c71099acSThomas Graf 			.ip6_u = {
560c71099acSThomas Graf 				.daddr = *daddr,
561c71099acSThomas Graf 			},
562c71099acSThomas Graf 		},
563c71099acSThomas Graf 	};
564c71099acSThomas Graf 	struct dst_entry *dst;
56577d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
566c71099acSThomas Graf 
567adaa70bbSThomas Graf 	if (saddr) {
568adaa70bbSThomas Graf 		memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
569adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
570adaa70bbSThomas Graf 	}
571adaa70bbSThomas Graf 
572c71099acSThomas Graf 	dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_lookup);
573c71099acSThomas Graf 	if (dst->error == 0)
574c71099acSThomas Graf 		return (struct rt6_info *) dst;
575c71099acSThomas Graf 
576c71099acSThomas Graf 	dst_release(dst);
577c71099acSThomas Graf 
5781da177e4SLinus Torvalds 	return NULL;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5817159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
5827159039aSYOSHIFUJI Hideaki 
583c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
5841da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
5851da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
5861da177e4SLinus Torvalds    be destroyed.
5871da177e4SLinus Torvalds  */
5881da177e4SLinus Torvalds 
58986872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	int err;
592c71099acSThomas Graf 	struct fib6_table *table;
5931da177e4SLinus Torvalds 
594c71099acSThomas Graf 	table = rt->rt6i_table;
595c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
59686872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
597c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	return err;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
60240e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
60340e22e8fSThomas Graf {
6044d1169c1SDenis V. Lunev 	struct nl_info info = {
6054d1169c1SDenis V. Lunev 		.nl_net = &init_net,
6064d1169c1SDenis V. Lunev 	};
607528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
60840e22e8fSThomas Graf }
60940e22e8fSThomas Graf 
61095a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
61195a9a5baSYOSHIFUJI Hideaki 				      struct in6_addr *saddr)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	struct rt6_info *rt;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	/*
6161da177e4SLinus Torvalds 	 *	Clone the route.
6171da177e4SLinus Torvalds 	 */
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (rt) {
62258c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
62358c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
62458c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
62558c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
6261da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
62758c4fb86SYOSHIFUJI Hideaki 		}
6281da177e4SLinus Torvalds 
62958c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
6301da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
6311da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
6321da177e4SLinus Torvalds 		rt->u.dst.flags |= DST_HOST;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
6351da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
6361da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
6371da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
6381da177e4SLinus Torvalds 		}
6391da177e4SLinus Torvalds #endif
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
6421da177e4SLinus Torvalds 
64395a9a5baSYOSHIFUJI Hideaki 	}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	return rt;
6461da177e4SLinus Torvalds }
64795a9a5baSYOSHIFUJI Hideaki 
648299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
649299d9939SYOSHIFUJI Hideaki {
650299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
651299d9939SYOSHIFUJI Hideaki 	if (rt) {
652299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
653299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
654299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
655299d9939SYOSHIFUJI Hideaki 		rt->u.dst.flags |= DST_HOST;
656299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
657299d9939SYOSHIFUJI Hideaki 	}
658299d9939SYOSHIFUJI Hideaki 	return rt;
659299d9939SYOSHIFUJI Hideaki }
660299d9939SYOSHIFUJI Hideaki 
6614acad72dSPavel Emelyanov static struct rt6_info *ip6_pol_route(struct fib6_table *table, int oif,
6628ce11e6aSAdrian Bunk 					    struct flowi *fl, int flags)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct fib6_node *fn;
665519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
666c71099acSThomas Graf 	int strict = 0;
6671da177e4SLinus Torvalds 	int attempts = 3;
668519fbd87SYOSHIFUJI Hideaki 	int err;
669ea659e07SYOSHIFUJI Hideaki 	int reachable = ipv6_devconf.forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
6701da177e4SLinus Torvalds 
67177d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds relookup:
674c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6751da177e4SLinus Torvalds 
6768238dd06SYOSHIFUJI Hideaki restart_2:
677c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds restart:
6804acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
681982f56f3SYOSHIFUJI Hideaki 	BACKTRACK(&fl->fl6_src);
6828238dd06SYOSHIFUJI Hideaki 	if (rt == &ip6_null_entry ||
6838238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
6841da177e4SLinus Torvalds 		goto out;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	dst_hold(&rt->u.dst);
687c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6881da177e4SLinus Torvalds 
689519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
690e40cf353SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
691519fbd87SYOSHIFUJI Hideaki 	else {
692519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE
693519fbd87SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
694519fbd87SYOSHIFUJI Hideaki #else
695519fbd87SYOSHIFUJI Hideaki 		goto out2;
696519fbd87SYOSHIFUJI Hideaki #endif
697519fbd87SYOSHIFUJI Hideaki 	}
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
700e40cf353SYOSHIFUJI Hideaki 	rt = nrt ? : &ip6_null_entry;
7011da177e4SLinus Torvalds 
702e40cf353SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
703e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
70440e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
705e40cf353SYOSHIFUJI Hideaki 		if (!err)
706e40cf353SYOSHIFUJI Hideaki 			goto out2;
707e40cf353SYOSHIFUJI Hideaki 	}
708e40cf353SYOSHIFUJI Hideaki 
709e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
7101da177e4SLinus Torvalds 		goto out2;
7111da177e4SLinus Torvalds 
712519fbd87SYOSHIFUJI Hideaki 	/*
713c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
714519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
7151da177e4SLinus Torvalds 	 */
7161da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
7171da177e4SLinus Torvalds 	goto relookup;
718e40cf353SYOSHIFUJI Hideaki 
719519fbd87SYOSHIFUJI Hideaki out:
7208238dd06SYOSHIFUJI Hideaki 	if (reachable) {
7218238dd06SYOSHIFUJI Hideaki 		reachable = 0;
7228238dd06SYOSHIFUJI Hideaki 		goto restart_2;
7238238dd06SYOSHIFUJI Hideaki 	}
724519fbd87SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
725c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7261da177e4SLinus Torvalds out2:
7271da177e4SLinus Torvalds 	rt->u.dst.lastuse = jiffies;
7281da177e4SLinus Torvalds 	rt->u.dst.__use++;
729c71099acSThomas Graf 
730c71099acSThomas Graf 	return rt;
731c71099acSThomas Graf }
732c71099acSThomas Graf 
7334acad72dSPavel Emelyanov static struct rt6_info *ip6_pol_route_input(struct fib6_table *table,
7344acad72dSPavel Emelyanov 					    struct flowi *fl, int flags)
7354acad72dSPavel Emelyanov {
7364acad72dSPavel Emelyanov 	return ip6_pol_route(table, fl->iif, fl, flags);
7374acad72dSPavel Emelyanov }
7384acad72dSPavel Emelyanov 
739c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
740c71099acSThomas Graf {
7410660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
742adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
743c71099acSThomas Graf 	struct flowi fl = {
744c71099acSThomas Graf 		.iif = skb->dev->ifindex,
745c71099acSThomas Graf 		.nl_u = {
746c71099acSThomas Graf 			.ip6_u = {
747c71099acSThomas Graf 				.daddr = iph->daddr,
748c71099acSThomas Graf 				.saddr = iph->saddr,
74990bcaf7bSAl Viro 				.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
750c71099acSThomas Graf 			},
751c71099acSThomas Graf 		},
75247dcf0cbSThomas Graf 		.mark = skb->mark,
753c71099acSThomas Graf 		.proto = iph->nexthdr,
754c71099acSThomas Graf 	};
755adaa70bbSThomas Graf 
756adaa70bbSThomas Graf 	if (rt6_need_strict(&iph->daddr))
757adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
758c71099acSThomas Graf 
759c71099acSThomas Graf 	skb->dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_input);
760c71099acSThomas Graf }
761c71099acSThomas Graf 
762c71099acSThomas Graf static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,
763c71099acSThomas Graf 					     struct flowi *fl, int flags)
764c71099acSThomas Graf {
7654acad72dSPavel Emelyanov 	return ip6_pol_route(table, fl->oif, fl, flags);
766c71099acSThomas Graf }
767c71099acSThomas Graf 
768c71099acSThomas Graf struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl)
769c71099acSThomas Graf {
770c71099acSThomas Graf 	int flags = 0;
771c71099acSThomas Graf 
772c71099acSThomas Graf 	if (rt6_need_strict(&fl->fl6_dst))
77377d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
774c71099acSThomas Graf 
775adaa70bbSThomas Graf 	if (!ipv6_addr_any(&fl->fl6_src))
776adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
777adaa70bbSThomas Graf 
778c71099acSThomas Graf 	return fib6_rule_lookup(fl, flags, ip6_pol_route_output);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7817159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
7821da177e4SLinus Torvalds 
78314e50e57SDavid S. Miller int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
78414e50e57SDavid S. Miller {
78514e50e57SDavid S. Miller 	struct rt6_info *ort = (struct rt6_info *) *dstp;
78614e50e57SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)
78714e50e57SDavid S. Miller 		dst_alloc(&ip6_dst_blackhole_ops);
78814e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
78914e50e57SDavid S. Miller 
79014e50e57SDavid S. Miller 	if (rt) {
79114e50e57SDavid S. Miller 		new = &rt->u.dst;
79214e50e57SDavid S. Miller 
79314e50e57SDavid S. Miller 		atomic_set(&new->__refcnt, 1);
79414e50e57SDavid S. Miller 		new->__use = 1;
795352e512cSHerbert Xu 		new->input = dst_discard;
796352e512cSHerbert Xu 		new->output = dst_discard;
79714e50e57SDavid S. Miller 
79814e50e57SDavid S. Miller 		memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
79914e50e57SDavid S. Miller 		new->dev = ort->u.dst.dev;
80014e50e57SDavid S. Miller 		if (new->dev)
80114e50e57SDavid S. Miller 			dev_hold(new->dev);
80214e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
80314e50e57SDavid S. Miller 		if (rt->rt6i_idev)
80414e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
80514e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
80614e50e57SDavid S. Miller 
80714e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
80814e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
80914e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
81014e50e57SDavid S. Miller 
81114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
81214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
81314e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
81414e50e57SDavid S. Miller #endif
81514e50e57SDavid S. Miller 
81614e50e57SDavid S. Miller 		dst_free(new);
81714e50e57SDavid S. Miller 	}
81814e50e57SDavid S. Miller 
81914e50e57SDavid S. Miller 	dst_release(*dstp);
82014e50e57SDavid S. Miller 	*dstp = new;
82114e50e57SDavid S. Miller 	return (new ? 0 : -ENOMEM);
82214e50e57SDavid S. Miller }
82314e50e57SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
82414e50e57SDavid S. Miller 
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds  *	Destination cache support functions
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	struct rt6_info *rt;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
8361da177e4SLinus Torvalds 		return dst;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	return NULL;
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (rt) {
8461da177e4SLinus Torvalds 		if (rt->rt6i_flags & RTF_CACHE)
847e0a1ad73SThomas Graf 			ip6_del_rt(rt);
8481da177e4SLinus Torvalds 		else
8491da177e4SLinus Torvalds 			dst_release(dst);
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	return NULL;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct rt6_info *rt;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	rt = (struct rt6_info *) skb->dst;
8611da177e4SLinus Torvalds 	if (rt) {
8621da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
8631da177e4SLinus Torvalds 			dst_set_expires(&rt->u.dst, 0);
8641da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
8651da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
8661da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
8751da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
8761da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
8771da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
8781da177e4SLinus Torvalds 			dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
8791da177e4SLinus Torvalds 		}
8801da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
8818d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds static inline unsigned int ipv6_advmss(unsigned int mtu)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
8901da177e4SLinus Torvalds 
8914990509fSDaniel Lezcano 	if (mtu < init_net.ipv6.sysctl.ip6_rt_min_advmss)
8924990509fSDaniel Lezcano 		mtu = init_net.ipv6.sysctl.ip6_rt_min_advmss;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
8961da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
8971da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
8981da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
8991da177e4SLinus Torvalds 	 */
9001da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
9011da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
9021da177e4SLinus Torvalds 	return mtu;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9055d0bbeebSThomas Graf static struct dst_entry *ndisc_dst_gc_list;
9068ce11e6aSAdrian Bunk static DEFINE_SPINLOCK(ndisc_lock);
9075d0bbeebSThomas Graf 
9081da177e4SLinus Torvalds struct dst_entry *ndisc_dst_alloc(struct net_device *dev,
9091da177e4SLinus Torvalds 				  struct neighbour *neigh,
9101da177e4SLinus Torvalds 				  struct in6_addr *addr,
9111da177e4SLinus Torvalds 				  int (*output)(struct sk_buff *))
9121da177e4SLinus Torvalds {
9131da177e4SLinus Torvalds 	struct rt6_info *rt;
9141da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
9171da177e4SLinus Torvalds 		return NULL;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	rt = ip6_dst_alloc();
9201da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
9211da177e4SLinus Torvalds 		in6_dev_put(idev);
9221da177e4SLinus Torvalds 		goto out;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	dev_hold(dev);
9261da177e4SLinus Torvalds 	if (neigh)
9271da177e4SLinus Torvalds 		neigh_hold(neigh);
9281da177e4SLinus Torvalds 	else
9291da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	rt->rt6i_dev	  = dev;
9321da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
9331da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
9341da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
9351da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
9361da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
9371da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
9381da177e4SLinus Torvalds 	rt->u.dst.output  = output;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
9411da177e4SLinus Torvalds 	rt->u.dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
9421da177e4SLinus Torvalds 				? DST_HOST
9431da177e4SLinus Torvalds 				: 0;
9441da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
9451da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
9461da177e4SLinus Torvalds #endif
9471da177e4SLinus Torvalds 
9485d0bbeebSThomas Graf 	spin_lock_bh(&ndisc_lock);
9491da177e4SLinus Torvalds 	rt->u.dst.next = ndisc_dst_gc_list;
9501da177e4SLinus Torvalds 	ndisc_dst_gc_list = &rt->u.dst;
9515d0bbeebSThomas Graf 	spin_unlock_bh(&ndisc_lock);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	fib6_force_start_gc();
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds out:
95640aa7b90SYOSHIFUJI Hideaki 	return &rt->u.dst;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds int ndisc_dst_gc(int *more)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	struct dst_entry *dst, *next, **pprev;
9621da177e4SLinus Torvalds 	int freed;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	next = NULL;
9651da177e4SLinus Torvalds 	freed = 0;
9665d0bbeebSThomas Graf 
9675d0bbeebSThomas Graf 	spin_lock_bh(&ndisc_lock);
9685d0bbeebSThomas Graf 	pprev = &ndisc_dst_gc_list;
9695d0bbeebSThomas Graf 
9701da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
9711da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
9721da177e4SLinus Torvalds 			*pprev = dst->next;
9731da177e4SLinus Torvalds 			dst_free(dst);
9741da177e4SLinus Torvalds 			freed++;
9751da177e4SLinus Torvalds 		} else {
9761da177e4SLinus Torvalds 			pprev = &dst->next;
9771da177e4SLinus Torvalds 			(*more)++;
9781da177e4SLinus Torvalds 		}
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9815d0bbeebSThomas Graf 	spin_unlock_bh(&ndisc_lock);
9825d0bbeebSThomas Graf 
9831da177e4SLinus Torvalds 	return freed;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds static int ip6_dst_gc(void)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	static unsigned expire = 30*HZ;
9891da177e4SLinus Torvalds 	static unsigned long last_gc;
9901da177e4SLinus Torvalds 	unsigned long now = jiffies;
9911da177e4SLinus Torvalds 
9924990509fSDaniel Lezcano 	if (time_after(last_gc + init_net.ipv6.sysctl.ip6_rt_gc_min_interval, now) &&
9934990509fSDaniel Lezcano 	    atomic_read(&ip6_dst_ops.entries) <= init_net.ipv6.sysctl.ip6_rt_max_size)
9941da177e4SLinus Torvalds 		goto out;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	expire++;
9971da177e4SLinus Torvalds 	fib6_run_gc(expire);
9981da177e4SLinus Torvalds 	last_gc = now;
9991da177e4SLinus Torvalds 	if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh)
10004990509fSDaniel Lezcano 		expire = init_net.ipv6.sysctl.ip6_rt_gc_timeout>>1;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds out:
10034990509fSDaniel Lezcano 	expire -= expire>>init_net.ipv6.sysctl.ip6_rt_gc_elasticity;
10044990509fSDaniel Lezcano 	return (atomic_read(&ip6_dst_ops.entries) > init_net.ipv6.sysctl.ip6_rt_max_size);
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
10081da177e4SLinus Torvalds    but results in cleaner routing tables.
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds    Remove it only when all the things will work!
10111da177e4SLinus Torvalds  */
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	int mtu = IPV6_MIN_MTU;
10161da177e4SLinus Torvalds 	struct inet6_dev *idev;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	idev = in6_dev_get(dev);
10191da177e4SLinus Torvalds 	if (idev) {
10201da177e4SLinus Torvalds 		mtu = idev->cnf.mtu6;
10211da177e4SLinus Torvalds 		in6_dev_put(idev);
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 	return mtu;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds int ipv6_get_hoplimit(struct net_device *dev)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	int hoplimit = ipv6_devconf.hop_limit;
10291da177e4SLinus Torvalds 	struct inet6_dev *idev;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	idev = in6_dev_get(dev);
10321da177e4SLinus Torvalds 	if (idev) {
10331da177e4SLinus Torvalds 		hoplimit = idev->cnf.hop_limit;
10341da177e4SLinus Torvalds 		in6_dev_put(idev);
10351da177e4SLinus Torvalds 	}
10361da177e4SLinus Torvalds 	return hoplimit;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds /*
10401da177e4SLinus Torvalds  *
10411da177e4SLinus Torvalds  */
10421da177e4SLinus Torvalds 
104386872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
10441da177e4SLinus Torvalds {
10451da177e4SLinus Torvalds 	int err;
10461da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
10471da177e4SLinus Torvalds 	struct net_device *dev = NULL;
10481da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1049c71099acSThomas Graf 	struct fib6_table *table;
10501da177e4SLinus Torvalds 	int addr_type;
10511da177e4SLinus Torvalds 
105286872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
10531da177e4SLinus Torvalds 		return -EINVAL;
10541da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
105586872cb5SThomas Graf 	if (cfg->fc_src_len)
10561da177e4SLinus Torvalds 		return -EINVAL;
10571da177e4SLinus Torvalds #endif
105886872cb5SThomas Graf 	if (cfg->fc_ifindex) {
10591da177e4SLinus Torvalds 		err = -ENODEV;
1060881d966bSEric W. Biederman 		dev = dev_get_by_index(&init_net, cfg->fc_ifindex);
10611da177e4SLinus Torvalds 		if (!dev)
10621da177e4SLinus Torvalds 			goto out;
10631da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
10641da177e4SLinus Torvalds 		if (!idev)
10651da177e4SLinus Torvalds 			goto out;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
106886872cb5SThomas Graf 	if (cfg->fc_metric == 0)
106986872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
10701da177e4SLinus Torvalds 
107186872cb5SThomas Graf 	table = fib6_new_table(cfg->fc_table);
1072c71099acSThomas Graf 	if (table == NULL) {
1073c71099acSThomas Graf 		err = -ENOBUFS;
1074c71099acSThomas Graf 		goto out;
1075c71099acSThomas Graf 	}
1076c71099acSThomas Graf 
10771da177e4SLinus Torvalds 	rt = ip6_dst_alloc();
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	if (rt == NULL) {
10801da177e4SLinus Torvalds 		err = -ENOMEM;
10811da177e4SLinus Torvalds 		goto out;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
108586872cb5SThomas Graf 	rt->rt6i_expires = jiffies + clock_t_to_jiffies(cfg->fc_expires);
10861da177e4SLinus Torvalds 
108786872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
108886872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
108986872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
109086872cb5SThomas Graf 
109186872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
10941da177e4SLinus Torvalds 		rt->u.dst.input = ip6_mc_input;
10951da177e4SLinus Torvalds 	else
10961da177e4SLinus Torvalds 		rt->u.dst.input = ip6_forward;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
10991da177e4SLinus Torvalds 
110086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
110186872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
11021da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
11031da177e4SLinus Torvalds 	       rt->u.dst.flags = DST_HOST;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
110686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
110786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
11081da177e4SLinus Torvalds #endif
11091da177e4SLinus Torvalds 
111086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
11131da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
11141da177e4SLinus Torvalds 	 */
111586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
11161da177e4SLinus Torvalds 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
11171da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
11182774c7abSEric W. Biederman 		if (dev != init_net.loopback_dev) {
11191da177e4SLinus Torvalds 			if (dev) {
11201da177e4SLinus Torvalds 				dev_put(dev);
11211da177e4SLinus Torvalds 				in6_dev_put(idev);
11221da177e4SLinus Torvalds 			}
11232774c7abSEric W. Biederman 			dev = init_net.loopback_dev;
11241da177e4SLinus Torvalds 			dev_hold(dev);
11251da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
11261da177e4SLinus Torvalds 			if (!idev) {
11271da177e4SLinus Torvalds 				err = -ENODEV;
11281da177e4SLinus Torvalds 				goto out;
11291da177e4SLinus Torvalds 			}
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 		rt->u.dst.output = ip6_pkt_discard_out;
11321da177e4SLinus Torvalds 		rt->u.dst.input = ip6_pkt_discard;
11331da177e4SLinus Torvalds 		rt->u.dst.error = -ENETUNREACH;
11341da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
11351da177e4SLinus Torvalds 		goto install_route;
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
113886872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
11391da177e4SLinus Torvalds 		struct in6_addr *gw_addr;
11401da177e4SLinus Torvalds 		int gwa_type;
11411da177e4SLinus Torvalds 
114286872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
114386872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
11441da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
11471da177e4SLinus Torvalds 			struct rt6_info *grt;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
11501da177e4SLinus Torvalds 			   addresses as nexthop address.
11511da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
11521da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
11531da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
11541da177e4SLinus Torvalds 			   some exceptions. --ANK
11551da177e4SLinus Torvalds 			 */
11561da177e4SLinus Torvalds 			err = -EINVAL;
11571da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
11581da177e4SLinus Torvalds 				goto out;
11591da177e4SLinus Torvalds 
116086872cb5SThomas Graf 			grt = rt6_lookup(gw_addr, NULL, cfg->fc_ifindex, 1);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
11631da177e4SLinus Torvalds 			if (grt == NULL)
11641da177e4SLinus Torvalds 				goto out;
11651da177e4SLinus Torvalds 			if (dev) {
11661da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
11671da177e4SLinus Torvalds 					dst_release(&grt->u.dst);
11681da177e4SLinus Torvalds 					goto out;
11691da177e4SLinus Torvalds 				}
11701da177e4SLinus Torvalds 			} else {
11711da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
11721da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
11731da177e4SLinus Torvalds 				dev_hold(dev);
11741da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
11751da177e4SLinus Torvalds 			}
11761da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
11771da177e4SLinus Torvalds 				err = 0;
11781da177e4SLinus Torvalds 			dst_release(&grt->u.dst);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 			if (err)
11811da177e4SLinus Torvalds 				goto out;
11821da177e4SLinus Torvalds 		}
11831da177e4SLinus Torvalds 		err = -EINVAL;
11841da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
11851da177e4SLinus Torvalds 			goto out;
11861da177e4SLinus Torvalds 	}
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	err = -ENODEV;
11891da177e4SLinus Torvalds 	if (dev == NULL)
11901da177e4SLinus Torvalds 		goto out;
11911da177e4SLinus Torvalds 
119286872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
11931da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
11941da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
11951da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
11961da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
11971da177e4SLinus Torvalds 			goto out;
11981da177e4SLinus Torvalds 		}
11991da177e4SLinus Torvalds 	}
12001da177e4SLinus Torvalds 
120186872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds install_route:
120486872cb5SThomas Graf 	if (cfg->fc_mx) {
120586872cb5SThomas Graf 		struct nlattr *nla;
120686872cb5SThomas Graf 		int remaining;
12071da177e4SLinus Torvalds 
120886872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
12098f4c1f9bSThomas Graf 			int type = nla_type(nla);
121086872cb5SThomas Graf 
121186872cb5SThomas Graf 			if (type) {
121286872cb5SThomas Graf 				if (type > RTAX_MAX) {
12131da177e4SLinus Torvalds 					err = -EINVAL;
12141da177e4SLinus Torvalds 					goto out;
12151da177e4SLinus Torvalds 				}
121686872cb5SThomas Graf 
121786872cb5SThomas Graf 				rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
12181da177e4SLinus Torvalds 			}
12191da177e4SLinus Torvalds 		}
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
12231da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
12241da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_MTU-1])
12251da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
12261da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
12271da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
12281da177e4SLinus Torvalds 	rt->u.dst.dev = dev;
12291da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1230c71099acSThomas Graf 	rt->rt6i_table = table;
123186872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds out:
12341da177e4SLinus Torvalds 	if (dev)
12351da177e4SLinus Torvalds 		dev_put(dev);
12361da177e4SLinus Torvalds 	if (idev)
12371da177e4SLinus Torvalds 		in6_dev_put(idev);
12381da177e4SLinus Torvalds 	if (rt)
123940aa7b90SYOSHIFUJI Hideaki 		dst_free(&rt->u.dst);
12401da177e4SLinus Torvalds 	return err;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
124386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	int err;
1246c71099acSThomas Graf 	struct fib6_table *table;
12471da177e4SLinus Torvalds 
12486c813a72SPatrick McHardy 	if (rt == &ip6_null_entry)
12496c813a72SPatrick McHardy 		return -ENOENT;
12506c813a72SPatrick McHardy 
1251c71099acSThomas Graf 	table = rt->rt6i_table;
1252c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
12531da177e4SLinus Torvalds 
125486872cb5SThomas Graf 	err = fib6_del(rt, info);
12551da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
12561da177e4SLinus Torvalds 
1257c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	return err;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
1262e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1263e0a1ad73SThomas Graf {
12644d1169c1SDenis V. Lunev 	struct nl_info info = {
12654d1169c1SDenis V. Lunev 		.nl_net = &init_net,
12664d1169c1SDenis V. Lunev 	};
1267528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1268e0a1ad73SThomas Graf }
1269e0a1ad73SThomas Graf 
127086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
12711da177e4SLinus Torvalds {
1272c71099acSThomas Graf 	struct fib6_table *table;
12731da177e4SLinus Torvalds 	struct fib6_node *fn;
12741da177e4SLinus Torvalds 	struct rt6_info *rt;
12751da177e4SLinus Torvalds 	int err = -ESRCH;
12761da177e4SLinus Torvalds 
127786872cb5SThomas Graf 	table = fib6_get_table(cfg->fc_table);
1278c71099acSThomas Graf 	if (table == NULL)
1279c71099acSThomas Graf 		return err;
12801da177e4SLinus Torvalds 
1281c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1282c71099acSThomas Graf 
1283c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
128486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
128586872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if (fn) {
12887cc48263SEric Dumazet 		for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
128986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
12901da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
129186872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
12921da177e4SLinus Torvalds 				continue;
129386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
129486872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
12951da177e4SLinus Torvalds 				continue;
129686872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
12971da177e4SLinus Torvalds 				continue;
12981da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1299c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
13001da177e4SLinus Torvalds 
130186872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 	}
1304c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	return err;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds /*
13101da177e4SLinus Torvalds  *	Handle redirects
13111da177e4SLinus Torvalds  */
1312a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
1313a6279458SYOSHIFUJI Hideaki 	struct flowi fl;
1314a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1315a6279458SYOSHIFUJI Hideaki };
13161da177e4SLinus Torvalds 
1317a6279458SYOSHIFUJI Hideaki static struct rt6_info *__ip6_route_redirect(struct fib6_table *table,
1318a6279458SYOSHIFUJI Hideaki 					     struct flowi *fl,
1319a6279458SYOSHIFUJI Hideaki 					     int flags)
1320a6279458SYOSHIFUJI Hideaki {
1321a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1322a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1323a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1324c71099acSThomas Graf 
1325e843b9e1SYOSHIFUJI Hideaki 	/*
1326e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1327e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1328e843b9e1SYOSHIFUJI Hideaki 	 *
1329e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1330e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1331e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1332e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1333e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1334e843b9e1SYOSHIFUJI Hideaki 	 */
13351da177e4SLinus Torvalds 
1336c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1337a6279458SYOSHIFUJI Hideaki 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1338e843b9e1SYOSHIFUJI Hideaki restart:
13397cc48263SEric Dumazet 	for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
13401da177e4SLinus Torvalds 		/*
13411da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
13421da177e4SLinus Torvalds 		 *
13431da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
13441da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
13451da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
13461da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
13471da177e4SLinus Torvalds 		 */
1348e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1349e843b9e1SYOSHIFUJI Hideaki 			continue;
13501da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1351e843b9e1SYOSHIFUJI Hideaki 			continue;
1352a6279458SYOSHIFUJI Hideaki 		if (fl->oif != rt->rt6i_dev->ifindex)
1353e843b9e1SYOSHIFUJI Hideaki 			continue;
1354a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1355e843b9e1SYOSHIFUJI Hideaki 			continue;
1356e843b9e1SYOSHIFUJI Hideaki 		break;
1357e843b9e1SYOSHIFUJI Hideaki 	}
1358a6279458SYOSHIFUJI Hideaki 
1359cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
1360a6279458SYOSHIFUJI Hideaki 		rt = &ip6_null_entry;
1361cb15d9c2SYOSHIFUJI Hideaki 	BACKTRACK(&fl->fl6_src);
1362cb15d9c2SYOSHIFUJI Hideaki out:
1363a6279458SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
1364a6279458SYOSHIFUJI Hideaki 
1365c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13661da177e4SLinus Torvalds 
1367a6279458SYOSHIFUJI Hideaki 	return rt;
1368a6279458SYOSHIFUJI Hideaki };
1369a6279458SYOSHIFUJI Hideaki 
1370a6279458SYOSHIFUJI Hideaki static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1371a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *src,
1372a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *gateway,
1373a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1374a6279458SYOSHIFUJI Hideaki {
1375adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1376a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
1377a6279458SYOSHIFUJI Hideaki 		.fl = {
1378a6279458SYOSHIFUJI Hideaki 			.oif = dev->ifindex,
1379a6279458SYOSHIFUJI Hideaki 			.nl_u = {
1380a6279458SYOSHIFUJI Hideaki 				.ip6_u = {
1381a6279458SYOSHIFUJI Hideaki 					.daddr = *dest,
1382a6279458SYOSHIFUJI Hideaki 					.saddr = *src,
1383a6279458SYOSHIFUJI Hideaki 				},
1384a6279458SYOSHIFUJI Hideaki 			},
1385a6279458SYOSHIFUJI Hideaki 		},
1386a6279458SYOSHIFUJI Hideaki 		.gateway = *gateway,
1387a6279458SYOSHIFUJI Hideaki 	};
1388adaa70bbSThomas Graf 
1389adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1390adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1391a6279458SYOSHIFUJI Hideaki 
1392a6279458SYOSHIFUJI Hideaki 	return (struct rt6_info *)fib6_rule_lookup((struct flowi *)&rdfl, flags, __ip6_route_redirect);
1393a6279458SYOSHIFUJI Hideaki }
1394a6279458SYOSHIFUJI Hideaki 
1395a6279458SYOSHIFUJI Hideaki void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1396a6279458SYOSHIFUJI Hideaki 		  struct in6_addr *saddr,
1397a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1398a6279458SYOSHIFUJI Hideaki {
1399a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1400a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1401a6279458SYOSHIFUJI Hideaki 
1402a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1403a6279458SYOSHIFUJI Hideaki 
1404a6279458SYOSHIFUJI Hideaki 	if (rt == &ip6_null_entry) {
14051da177e4SLinus Torvalds 		if (net_ratelimit())
14061da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
14071da177e4SLinus Torvalds 			       "for redirect target\n");
1408a6279458SYOSHIFUJI Hideaki 		goto out;
14091da177e4SLinus Torvalds 	}
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	/*
14121da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
14131da177e4SLinus Torvalds 	 */
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
14161da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
14171da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
14181da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
14191da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
14201da177e4SLinus Torvalds 		     );
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	/*
14231da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
14241da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
14251da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
14261da177e4SLinus Torvalds 	 */
14271da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
14301da177e4SLinus Torvalds 	if (neigh == rt->u.dst.neighbour)
14311da177e4SLinus Torvalds 		goto out;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
14341da177e4SLinus Torvalds 	if (nrt == NULL)
14351da177e4SLinus Torvalds 		goto out;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
14381da177e4SLinus Torvalds 	if (on_link)
14391da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
14421da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
14431da177e4SLinus Torvalds 	nrt->u.dst.flags |= DST_HOST;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
14461da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
14471da177e4SLinus Torvalds 	/* Reset pmtu, it may be better */
14481da177e4SLinus Torvalds 	nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
14491da177e4SLinus Torvalds 	nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&nrt->u.dst));
14501da177e4SLinus Torvalds 
145140e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
14521da177e4SLinus Torvalds 		goto out;
14531da177e4SLinus Torvalds 
14548d71740cSTom Tucker 	netevent.old = &rt->u.dst;
14558d71740cSTom Tucker 	netevent.new = &nrt->u.dst;
14568d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
14578d71740cSTom Tucker 
14581da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1459e0a1ad73SThomas Graf 		ip6_del_rt(rt);
14601da177e4SLinus Torvalds 		return;
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds out:
14641da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
14651da177e4SLinus Torvalds 	return;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds /*
14691da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
14701da177e4SLinus Torvalds  *	i.e. Path MTU discovery
14711da177e4SLinus Torvalds  */
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
14741da177e4SLinus Torvalds 			struct net_device *dev, u32 pmtu)
14751da177e4SLinus Torvalds {
14761da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
14771da177e4SLinus Torvalds 	int allfrag = 0;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	rt = rt6_lookup(daddr, saddr, dev->ifindex, 0);
14801da177e4SLinus Torvalds 	if (rt == NULL)
14811da177e4SLinus Torvalds 		return;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	if (pmtu >= dst_mtu(&rt->u.dst))
14841da177e4SLinus Torvalds 		goto out;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
14871da177e4SLinus Torvalds 		/*
14881da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
14891da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
14901da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
14911da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
14921da177e4SLinus Torvalds 		 */
14931da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
14941da177e4SLinus Torvalds 		allfrag = 1;
14951da177e4SLinus Torvalds 	}
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
14981da177e4SLinus Torvalds 	   They are sent only in response to data packets,
14991da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
15001da177e4SLinus Torvalds 	 */
15011da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
15041da177e4SLinus Torvalds 	   not to override it, but add new one, so that
15051da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
15061da177e4SLinus Torvalds 	   would return automatically.
15071da177e4SLinus Torvalds 	 */
15081da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
15091da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
15101da177e4SLinus Torvalds 		if (allfrag)
15111da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
15124990509fSDaniel Lezcano 		dst_set_expires(&rt->u.dst, init_net.ipv6.sysctl.ip6_rt_mtu_expires);
15131da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
15141da177e4SLinus Torvalds 		goto out;
15151da177e4SLinus Torvalds 	}
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	/* Network route.
15181da177e4SLinus Torvalds 	   Two cases are possible:
15191da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
15201da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
15211da177e4SLinus Torvalds 	 */
1522d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1523a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1524d5315b50SYOSHIFUJI Hideaki 	else
1525d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1526a1e78363SYOSHIFUJI Hideaki 
1527d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
15281da177e4SLinus Torvalds 		nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
15291da177e4SLinus Torvalds 		if (allfrag)
15301da177e4SLinus Torvalds 			nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1531a1e78363SYOSHIFUJI Hideaki 
15321da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1533a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1534a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1535a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1536a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
15371da177e4SLinus Torvalds 		 */
15384990509fSDaniel Lezcano 		dst_set_expires(&nrt->u.dst, init_net.ipv6.sysctl.ip6_rt_mtu_expires);
15391da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1540a1e78363SYOSHIFUJI Hideaki 
154140e22e8fSThomas Graf 		ip6_ins_rt(nrt);
15421da177e4SLinus Torvalds 	}
15431da177e4SLinus Torvalds out:
15441da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds /*
15481da177e4SLinus Torvalds  *	Misc support functions
15491da177e4SLinus Torvalds  */
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
15521da177e4SLinus Torvalds {
15531da177e4SLinus Torvalds 	struct rt6_info *rt = ip6_dst_alloc();
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	if (rt) {
15561da177e4SLinus Torvalds 		rt->u.dst.input = ort->u.dst.input;
15571da177e4SLinus Torvalds 		rt->u.dst.output = ort->u.dst.output;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
156022e1e4d8SVille Nuorvala 		rt->u.dst.error = ort->u.dst.error;
15611da177e4SLinus Torvalds 		rt->u.dst.dev = ort->u.dst.dev;
15621da177e4SLinus Torvalds 		if (rt->u.dst.dev)
15631da177e4SLinus Torvalds 			dev_hold(rt->u.dst.dev);
15641da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
15651da177e4SLinus Torvalds 		if (rt->rt6i_idev)
15661da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
15671da177e4SLinus Torvalds 		rt->u.dst.lastuse = jiffies;
15681da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
15711da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
15721da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
15751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
15761da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
15771da177e4SLinus Torvalds #endif
1578c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
15791da177e4SLinus Torvalds 	}
15801da177e4SLinus Torvalds 	return rt;
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
158370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
158470ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
158570ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex)
158670ceb4f5SYOSHIFUJI Hideaki {
158770ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
158870ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1589c71099acSThomas Graf 	struct fib6_table *table;
159070ceb4f5SYOSHIFUJI Hideaki 
1591c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_INFO);
1592c71099acSThomas Graf 	if (table == NULL)
1593c71099acSThomas Graf 		return NULL;
1594c71099acSThomas Graf 
1595c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1596c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
159770ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
159870ceb4f5SYOSHIFUJI Hideaki 		goto out;
159970ceb4f5SYOSHIFUJI Hideaki 
16007cc48263SEric Dumazet 	for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
160170ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
160270ceb4f5SYOSHIFUJI Hideaki 			continue;
160370ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
160470ceb4f5SYOSHIFUJI Hideaki 			continue;
160570ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
160670ceb4f5SYOSHIFUJI Hideaki 			continue;
160770ceb4f5SYOSHIFUJI Hideaki 		dst_hold(&rt->u.dst);
160870ceb4f5SYOSHIFUJI Hideaki 		break;
160970ceb4f5SYOSHIFUJI Hideaki 	}
161070ceb4f5SYOSHIFUJI Hideaki out:
1611c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
161270ceb4f5SYOSHIFUJI Hideaki 	return rt;
161370ceb4f5SYOSHIFUJI Hideaki }
161470ceb4f5SYOSHIFUJI Hideaki 
161570ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
161670ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
161770ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
161870ceb4f5SYOSHIFUJI Hideaki {
161986872cb5SThomas Graf 	struct fib6_config cfg = {
162086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
162186872cb5SThomas Graf 		.fc_metric	= 1024,
162286872cb5SThomas Graf 		.fc_ifindex	= ifindex,
162386872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
162486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
162586872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
162686872cb5SThomas Graf 	};
162770ceb4f5SYOSHIFUJI Hideaki 
162886872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
162986872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
163086872cb5SThomas Graf 
1631e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1632e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
163386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
163470ceb4f5SYOSHIFUJI Hideaki 
163586872cb5SThomas Graf 	ip6_route_add(&cfg);
163670ceb4f5SYOSHIFUJI Hideaki 
163770ceb4f5SYOSHIFUJI Hideaki 	return rt6_get_route_info(prefix, prefixlen, gwaddr, ifindex);
163870ceb4f5SYOSHIFUJI Hideaki }
163970ceb4f5SYOSHIFUJI Hideaki #endif
164070ceb4f5SYOSHIFUJI Hideaki 
16411da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds 	struct rt6_info *rt;
1644c71099acSThomas Graf 	struct fib6_table *table;
16451da177e4SLinus Torvalds 
1646c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_DFLT);
1647c71099acSThomas Graf 	if (table == NULL)
1648c71099acSThomas Graf 		return NULL;
16491da177e4SLinus Torvalds 
1650c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
16517cc48263SEric Dumazet 	for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
16521da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1653045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
16541da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
16551da177e4SLinus Torvalds 			break;
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 	if (rt)
16581da177e4SLinus Torvalds 		dst_hold(&rt->u.dst);
1659c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16601da177e4SLinus Torvalds 	return rt;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
1663c7dc89c0SFred L. Templin EXPORT_SYMBOL(rt6_get_dflt_router);
1664c7dc89c0SFred L. Templin 
16651da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1666ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1667ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
16681da177e4SLinus Torvalds {
166986872cb5SThomas Graf 	struct fib6_config cfg = {
167086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
167186872cb5SThomas Graf 		.fc_metric	= 1024,
167286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
167386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
167486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
167586872cb5SThomas Graf 	};
16761da177e4SLinus Torvalds 
167786872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
16781da177e4SLinus Torvalds 
167986872cb5SThomas Graf 	ip6_route_add(&cfg);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds void rt6_purge_dflt_routers(void)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	struct rt6_info *rt;
1687c71099acSThomas Graf 	struct fib6_table *table;
1688c71099acSThomas Graf 
1689c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
1690c71099acSThomas Graf 	table = fib6_get_table(RT6_TABLE_DFLT);
1691c71099acSThomas Graf 	if (table == NULL)
1692c71099acSThomas Graf 		return;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds restart:
1695c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
16967cc48263SEric Dumazet 	for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
16971da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
16981da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1699c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1700e0a1ad73SThomas Graf 			ip6_del_rt(rt);
17011da177e4SLinus Torvalds 			goto restart;
17021da177e4SLinus Torvalds 		}
17031da177e4SLinus Torvalds 	}
1704c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
170786872cb5SThomas Graf static void rtmsg_to_fib6_config(struct in6_rtmsg *rtmsg,
170886872cb5SThomas Graf 				 struct fib6_config *cfg)
170986872cb5SThomas Graf {
171086872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
171186872cb5SThomas Graf 
171286872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
171386872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
171486872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
171586872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
171686872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
171786872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
171886872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
171986872cb5SThomas Graf 
172086872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
172186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
172286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
172386872cb5SThomas Graf }
172486872cb5SThomas Graf 
17251da177e4SLinus Torvalds int ipv6_route_ioctl(unsigned int cmd, void __user *arg)
17261da177e4SLinus Torvalds {
172786872cb5SThomas Graf 	struct fib6_config cfg;
17281da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
17291da177e4SLinus Torvalds 	int err;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	switch(cmd) {
17321da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
17331da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
17341da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
17351da177e4SLinus Torvalds 			return -EPERM;
17361da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
17371da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
17381da177e4SLinus Torvalds 		if (err)
17391da177e4SLinus Torvalds 			return -EFAULT;
17401da177e4SLinus Torvalds 
174186872cb5SThomas Graf 		rtmsg_to_fib6_config(&rtmsg, &cfg);
174286872cb5SThomas Graf 
17431da177e4SLinus Torvalds 		rtnl_lock();
17441da177e4SLinus Torvalds 		switch (cmd) {
17451da177e4SLinus Torvalds 		case SIOCADDRT:
174686872cb5SThomas Graf 			err = ip6_route_add(&cfg);
17471da177e4SLinus Torvalds 			break;
17481da177e4SLinus Torvalds 		case SIOCDELRT:
174986872cb5SThomas Graf 			err = ip6_route_del(&cfg);
17501da177e4SLinus Torvalds 			break;
17511da177e4SLinus Torvalds 		default:
17521da177e4SLinus Torvalds 			err = -EINVAL;
17531da177e4SLinus Torvalds 		}
17541da177e4SLinus Torvalds 		rtnl_unlock();
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 		return err;
17573ff50b79SStephen Hemminger 	}
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	return -EINVAL;
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds /*
17631da177e4SLinus Torvalds  *	Drop the packet on the floor
17641da177e4SLinus Torvalds  */
17651da177e4SLinus Torvalds 
1766*50eb431dSIlpo Järvinen static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
17671da177e4SLinus Torvalds {
1768612f09e8SYOSHIFUJI Hideaki 	int type;
1769612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1770612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
17710660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1772612f09e8SYOSHIFUJI Hideaki 		if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
1773a11d206dSYOSHIFUJI Hideaki 			IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INADDRERRORS);
1774612f09e8SYOSHIFUJI Hideaki 			break;
1775612f09e8SYOSHIFUJI Hideaki 		}
1776612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1777612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
1778612f09e8SYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), ipstats_mib_noroutes);
1779612f09e8SYOSHIFUJI Hideaki 		break;
1780612f09e8SYOSHIFUJI Hideaki 	}
17819ce8ade0SThomas Graf 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
17821da177e4SLinus Torvalds 	kfree_skb(skb);
17831da177e4SLinus Torvalds 	return 0;
17841da177e4SLinus Torvalds }
17851da177e4SLinus Torvalds 
17869ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
17879ce8ade0SThomas Graf {
1788612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
17899ce8ade0SThomas Graf }
17909ce8ade0SThomas Graf 
179120380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	skb->dev = skb->dst->dev;
1794612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds 
17976723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
17986723ab54SDavid S. Miller 
17999ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
18009ce8ade0SThomas Graf {
1801612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
18029ce8ade0SThomas Graf }
18039ce8ade0SThomas Graf 
18049ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
18059ce8ade0SThomas Graf {
18069ce8ade0SThomas Graf 	skb->dev = skb->dst->dev;
1807612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
18089ce8ade0SThomas Graf }
18099ce8ade0SThomas Graf 
18106723ab54SDavid S. Miller #endif
18116723ab54SDavid S. Miller 
18121da177e4SLinus Torvalds /*
18131da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
18141da177e4SLinus Torvalds  */
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
18171da177e4SLinus Torvalds 				    const struct in6_addr *addr,
18181da177e4SLinus Torvalds 				    int anycast)
18191da177e4SLinus Torvalds {
18201da177e4SLinus Torvalds 	struct rt6_info *rt = ip6_dst_alloc();
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	if (rt == NULL)
18231da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18241da177e4SLinus Torvalds 
18252774c7abSEric W. Biederman 	dev_hold(init_net.loopback_dev);
18261da177e4SLinus Torvalds 	in6_dev_hold(idev);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	rt->u.dst.flags = DST_HOST;
18291da177e4SLinus Torvalds 	rt->u.dst.input = ip6_input;
18301da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
18312774c7abSEric W. Biederman 	rt->rt6i_dev = init_net.loopback_dev;
18321da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
18331da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
18341da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
18351da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
18361da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
183958c4fb86SYOSHIFUJI Hideaki 	if (anycast)
184058c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
184158c4fb86SYOSHIFUJI Hideaki 	else
18421da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
18431da177e4SLinus Torvalds 	rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
18441da177e4SLinus Torvalds 	if (rt->rt6i_nexthop == NULL) {
184540aa7b90SYOSHIFUJI Hideaki 		dst_free(&rt->u.dst);
18461da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
18501da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
1851c71099acSThomas Graf 	rt->rt6i_table = fib6_get_table(RT6_TABLE_LOCAL);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	return rt;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	if (((void*)rt->rt6i_dev == arg || arg == NULL) &&
18611da177e4SLinus Torvalds 	    rt != &ip6_null_entry) {
18621da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
18631da177e4SLinus Torvalds 		return -1;
18641da177e4SLinus Torvalds 	}
18651da177e4SLinus Torvalds 	return 0;
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds void rt6_ifdown(struct net_device *dev)
18691da177e4SLinus Torvalds {
1870c71099acSThomas Graf 	fib6_clean_all(fib6_ifdown, 0, dev);
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds struct rt6_mtu_change_arg
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds 	struct net_device *dev;
18761da177e4SLinus Torvalds 	unsigned mtu;
18771da177e4SLinus Torvalds };
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
18821da177e4SLinus Torvalds 	struct inet6_dev *idev;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
18851da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
18861da177e4SLinus Torvalds 	   We still use this lock to block changes
18871da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
18881da177e4SLinus Torvalds 	*/
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
18911da177e4SLinus Torvalds 	if (idev == NULL)
18921da177e4SLinus Torvalds 		return 0;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
18951da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
18961da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
18971da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
18981da177e4SLinus Torvalds 	 */
18991da177e4SLinus Torvalds 	/*
19001da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
19011da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
19021da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
19031da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
19041da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
19051da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
19061da177e4SLinus Torvalds 	   PMTU discouvery.
19071da177e4SLinus Torvalds 	 */
19081da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
19091da177e4SLinus Torvalds 	    !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
19101da177e4SLinus Torvalds 	    (dst_mtu(&rt->u.dst) > arg->mtu ||
19111da177e4SLinus Torvalds 	     (dst_mtu(&rt->u.dst) < arg->mtu &&
1912566cfd8fSSimon Arlott 	      dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
19131da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
19141da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(arg->mtu);
1915566cfd8fSSimon Arlott 	}
19161da177e4SLinus Torvalds 	return 0;
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
19201da177e4SLinus Torvalds {
1921c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
1922c71099acSThomas Graf 		.dev = dev,
1923c71099acSThomas Graf 		.mtu = mtu,
1924c71099acSThomas Graf 	};
19251da177e4SLinus Torvalds 
1926c71099acSThomas Graf 	fib6_clean_all(rt6_mtu_change_route, 0, &arg);
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
1929ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
19305176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
193186872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
1932ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
193386872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
193486872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
193586872cb5SThomas Graf };
193686872cb5SThomas Graf 
193786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
193886872cb5SThomas Graf 			      struct fib6_config *cfg)
19391da177e4SLinus Torvalds {
194086872cb5SThomas Graf 	struct rtmsg *rtm;
194186872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
194286872cb5SThomas Graf 	int err;
19431da177e4SLinus Torvalds 
194486872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
194586872cb5SThomas Graf 	if (err < 0)
194686872cb5SThomas Graf 		goto errout;
19471da177e4SLinus Torvalds 
194886872cb5SThomas Graf 	err = -EINVAL;
194986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
195086872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
195186872cb5SThomas Graf 
195286872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
195386872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
195486872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
195586872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
195686872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
195786872cb5SThomas Graf 
195886872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
195986872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
196086872cb5SThomas Graf 
196186872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
196286872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
196386872cb5SThomas Graf 
196486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
196586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
196686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
19671da177e4SLinus Torvalds 	}
196886872cb5SThomas Graf 
196986872cb5SThomas Graf 	if (tb[RTA_DST]) {
197086872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
197186872cb5SThomas Graf 
197286872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
197386872cb5SThomas Graf 			goto errout;
197486872cb5SThomas Graf 
197586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
19761da177e4SLinus Torvalds 	}
197786872cb5SThomas Graf 
197886872cb5SThomas Graf 	if (tb[RTA_SRC]) {
197986872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
198086872cb5SThomas Graf 
198186872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
198286872cb5SThomas Graf 			goto errout;
198386872cb5SThomas Graf 
198486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
19851da177e4SLinus Torvalds 	}
198686872cb5SThomas Graf 
198786872cb5SThomas Graf 	if (tb[RTA_OIF])
198886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
198986872cb5SThomas Graf 
199086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
199186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
199286872cb5SThomas Graf 
199386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
199486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
199586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
19961da177e4SLinus Torvalds 	}
199786872cb5SThomas Graf 
199886872cb5SThomas Graf 	if (tb[RTA_TABLE])
199986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
200086872cb5SThomas Graf 
200186872cb5SThomas Graf 	err = 0;
200286872cb5SThomas Graf errout:
200386872cb5SThomas Graf 	return err;
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
2006c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
20071da177e4SLinus Torvalds {
2008b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
200986872cb5SThomas Graf 	struct fib6_config cfg;
201086872cb5SThomas Graf 	int err;
20111da177e4SLinus Torvalds 
2012b854272bSDenis V. Lunev 	if (net != &init_net)
2013b854272bSDenis V. Lunev 		return -EINVAL;
2014b854272bSDenis V. Lunev 
201586872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
201686872cb5SThomas Graf 	if (err < 0)
201786872cb5SThomas Graf 		return err;
201886872cb5SThomas Graf 
201986872cb5SThomas Graf 	return ip6_route_del(&cfg);
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
2022c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
20231da177e4SLinus Torvalds {
2024b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
202586872cb5SThomas Graf 	struct fib6_config cfg;
202686872cb5SThomas Graf 	int err;
20271da177e4SLinus Torvalds 
2028b854272bSDenis V. Lunev 	if (net != &init_net)
2029b854272bSDenis V. Lunev 		return -EINVAL;
2030b854272bSDenis V. Lunev 
203186872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
203286872cb5SThomas Graf 	if (err < 0)
203386872cb5SThomas Graf 		return err;
203486872cb5SThomas Graf 
203586872cb5SThomas Graf 	return ip6_route_add(&cfg);
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
2038339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2039339bf98fSThomas Graf {
2040339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2041339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2042339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2043339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2044339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2045339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2046339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2047339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2048339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
20496a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2050339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2051339bf98fSThomas Graf }
2052339bf98fSThomas Graf 
20531da177e4SLinus Torvalds static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
20540d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
20550d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
20560d51aa80SJamal Hadi Salim 			 int prefix, unsigned int flags)
20571da177e4SLinus Torvalds {
20581da177e4SLinus Torvalds 	struct rtmsg *rtm;
20591da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2060e3703b3dSThomas Graf 	long expires;
20619e762a4aSPatrick McHardy 	u32 table;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
20641da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
20651da177e4SLinus Torvalds 			/* success since this is not a prefix route */
20661da177e4SLinus Torvalds 			return 1;
20671da177e4SLinus Torvalds 		}
20681da177e4SLinus Torvalds 	}
20691da177e4SLinus Torvalds 
20702d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
20712d7202bfSThomas Graf 	if (nlh == NULL)
207226932566SPatrick McHardy 		return -EMSGSIZE;
20732d7202bfSThomas Graf 
20742d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
20751da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
20761da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
20771da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
20781da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2079c71099acSThomas Graf 	if (rt->rt6i_table)
20809e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2081c71099acSThomas Graf 	else
20829e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
20839e762a4aSPatrick McHardy 	rtm->rtm_table = table;
20842d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
20851da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
20861da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
20871da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
20881da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
20891da177e4SLinus Torvalds 	else
20901da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
20911da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
20921da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
20931da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
20941da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
20951da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
20961da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
20971da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
20981da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
20991da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
21021da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	if (dst) {
21052d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
21061da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
21071da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
21082d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
21091da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
21101da177e4SLinus Torvalds 	if (src) {
21112d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
21121da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
21131da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
21142d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
21151da177e4SLinus Torvalds #endif
21161da177e4SLinus Torvalds 	if (iif)
21172d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_IIF, iif);
21181da177e4SLinus Torvalds 	else if (dst) {
21191da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
21201da177e4SLinus Torvalds 		if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0)
21212d7202bfSThomas Graf 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
21221da177e4SLinus Torvalds 	}
21232d7202bfSThomas Graf 
21241da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
21252d7202bfSThomas Graf 		goto nla_put_failure;
21262d7202bfSThomas Graf 
21271da177e4SLinus Torvalds 	if (rt->u.dst.neighbour)
21282d7202bfSThomas Graf 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
21292d7202bfSThomas Graf 
21301da177e4SLinus Torvalds 	if (rt->u.dst.dev)
21312d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
21322d7202bfSThomas Graf 
21332d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2134e3703b3dSThomas Graf 
2135e3703b3dSThomas Graf 	expires = rt->rt6i_expires ? rt->rt6i_expires - jiffies : 0;
2136e3703b3dSThomas Graf 	if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2137e3703b3dSThomas Graf 			       expires, rt->u.dst.error) < 0)
2138e3703b3dSThomas Graf 		goto nla_put_failure;
21391da177e4SLinus Torvalds 
21402d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
21412d7202bfSThomas Graf 
21422d7202bfSThomas Graf nla_put_failure:
214326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
214426932566SPatrick McHardy 	return -EMSGSIZE;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21471b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
21501da177e4SLinus Torvalds 	int prefix;
21511da177e4SLinus Torvalds 
21522d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
21532d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
21541da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
21551da177e4SLinus Torvalds 	} else
21561da177e4SLinus Torvalds 		prefix = 0;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
21591da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
21600d51aa80SJamal Hadi Salim 		     prefix, NLM_F_MULTI);
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
2163c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
21641da177e4SLinus Torvalds {
2165b854272bSDenis V. Lunev 	struct net *net = in_skb->sk->sk_net;
2166ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
21671da177e4SLinus Torvalds 	struct rt6_info *rt;
2168ab364a6fSThomas Graf 	struct sk_buff *skb;
2169ab364a6fSThomas Graf 	struct rtmsg *rtm;
2170ab364a6fSThomas Graf 	struct flowi fl;
2171ab364a6fSThomas Graf 	int err, iif = 0;
2172ab364a6fSThomas Graf 
2173b854272bSDenis V. Lunev 	if (net != &init_net)
2174b854272bSDenis V. Lunev 		return -EINVAL;
2175b854272bSDenis V. Lunev 
2176ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2177ab364a6fSThomas Graf 	if (err < 0)
2178ab364a6fSThomas Graf 		goto errout;
2179ab364a6fSThomas Graf 
2180ab364a6fSThomas Graf 	err = -EINVAL;
2181ab364a6fSThomas Graf 	memset(&fl, 0, sizeof(fl));
2182ab364a6fSThomas Graf 
2183ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2184ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2185ab364a6fSThomas Graf 			goto errout;
2186ab364a6fSThomas Graf 
2187ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2188ab364a6fSThomas Graf 	}
2189ab364a6fSThomas Graf 
2190ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2191ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2192ab364a6fSThomas Graf 			goto errout;
2193ab364a6fSThomas Graf 
2194ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2195ab364a6fSThomas Graf 	}
2196ab364a6fSThomas Graf 
2197ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2198ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2199ab364a6fSThomas Graf 
2200ab364a6fSThomas Graf 	if (tb[RTA_OIF])
2201ab364a6fSThomas Graf 		fl.oif = nla_get_u32(tb[RTA_OIF]);
2202ab364a6fSThomas Graf 
2203ab364a6fSThomas Graf 	if (iif) {
2204ab364a6fSThomas Graf 		struct net_device *dev;
2205881d966bSEric W. Biederman 		dev = __dev_get_by_index(&init_net, iif);
2206ab364a6fSThomas Graf 		if (!dev) {
2207ab364a6fSThomas Graf 			err = -ENODEV;
2208ab364a6fSThomas Graf 			goto errout;
2209ab364a6fSThomas Graf 		}
2210ab364a6fSThomas Graf 	}
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2213ab364a6fSThomas Graf 	if (skb == NULL) {
2214ab364a6fSThomas Graf 		err = -ENOBUFS;
2215ab364a6fSThomas Graf 		goto errout;
2216ab364a6fSThomas Graf 	}
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
22191da177e4SLinus Torvalds 	   through good chunk of routing engine.
22201da177e4SLinus Torvalds 	 */
2221459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
22221da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	rt = (struct rt6_info*) ip6_route_output(NULL, &fl);
22251da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
22261da177e4SLinus Torvalds 
2227ab364a6fSThomas Graf 	err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
22281da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
22290d51aa80SJamal Hadi Salim 			    nlh->nlmsg_seq, 0, 0);
22301da177e4SLinus Torvalds 	if (err < 0) {
2231ab364a6fSThomas Graf 		kfree_skb(skb);
2232ab364a6fSThomas Graf 		goto errout;
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 
223597c53cacSDenis V. Lunev 	err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
2236ab364a6fSThomas Graf errout:
22371da177e4SLinus Torvalds 	return err;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
224086872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	struct sk_buff *skb;
2243528c4cebSDenis V. Lunev 	u32 seq;
2244528c4cebSDenis V. Lunev 	int err;
22450d51aa80SJamal Hadi Salim 
2246528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2247528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
224886872cb5SThomas Graf 
2249339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
225021713ebcSThomas Graf 	if (skb == NULL)
225121713ebcSThomas Graf 		goto errout;
22521da177e4SLinus Torvalds 
2253528c4cebSDenis V. Lunev 	err = rt6_fill_node(skb, rt, NULL, NULL, 0,
2254528c4cebSDenis V. Lunev 				event, info->pid, seq, 0, 0);
225526932566SPatrick McHardy 	if (err < 0) {
225626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
225726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
225826932566SPatrick McHardy 		kfree_skb(skb);
225926932566SPatrick McHardy 		goto errout;
226026932566SPatrick McHardy 	}
2261528c4cebSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, info->pid,
2262528c4cebSDenis V. Lunev 				RTNLGRP_IPV6_ROUTE, info->nlh, gfp_any());
226321713ebcSThomas Graf errout:
226421713ebcSThomas Graf 	if (err < 0)
226597c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_ROUTE, err);
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds /*
22691da177e4SLinus Torvalds  *	/proc
22701da177e4SLinus Torvalds  */
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds struct rt6_proc_arg
22771da177e4SLinus Torvalds {
22781da177e4SLinus Torvalds 	char *buffer;
22791da177e4SLinus Torvalds 	int offset;
22801da177e4SLinus Torvalds 	int length;
22811da177e4SLinus Torvalds 	int skip;
22821da177e4SLinus Torvalds 	int len;
22831da177e4SLinus Torvalds };
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
22861da177e4SLinus Torvalds {
228733120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
22881da177e4SLinus Torvalds 
228933120b30SAlexey Dobriyan 	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
22901da177e4SLinus Torvalds 		   rt->rt6i_dst.plen);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
229333120b30SAlexey Dobriyan 	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
22941da177e4SLinus Torvalds 		   rt->rt6i_src.plen);
22951da177e4SLinus Torvalds #else
229633120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
22971da177e4SLinus Torvalds #endif
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
230033120b30SAlexey Dobriyan 		seq_printf(m, NIP6_SEQFMT,
230133e93c96SYOSHIFUJI Hideaki 			   NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
23021da177e4SLinus Torvalds 	} else {
230333120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
23041da177e4SLinus Torvalds 	}
230533120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
23061da177e4SLinus Torvalds 		   rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
23071da177e4SLinus Torvalds 		   rt->u.dst.__use, rt->rt6i_flags,
23081da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
23091da177e4SLinus Torvalds 	return 0;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
231233120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
23131da177e4SLinus Torvalds {
231433120b30SAlexey Dobriyan 	fib6_clean_all(rt6_info_route, 0, m);
231533120b30SAlexey Dobriyan 	return 0;
23161da177e4SLinus Torvalds }
23171da177e4SLinus Torvalds 
231833120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
231933120b30SAlexey Dobriyan {
232033120b30SAlexey Dobriyan 	return single_open(file, ipv6_route_show, NULL);
232133120b30SAlexey Dobriyan }
232233120b30SAlexey Dobriyan 
232333120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
232433120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
232533120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
232633120b30SAlexey Dobriyan 	.read		= seq_read,
232733120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
232833120b30SAlexey Dobriyan 	.release	= single_release,
232933120b30SAlexey Dobriyan };
233033120b30SAlexey Dobriyan 
23311da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
23321da177e4SLinus Torvalds {
23331da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
23341da177e4SLinus Torvalds 		      rt6_stats.fib_nodes, rt6_stats.fib_route_nodes,
23351da177e4SLinus Torvalds 		      rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries,
23361da177e4SLinus Torvalds 		      rt6_stats.fib_rt_cache,
23371da177e4SLinus Torvalds 		      atomic_read(&ip6_dst_ops.entries),
23381da177e4SLinus Torvalds 		      rt6_stats.fib_discarded_routes);
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	return 0;
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
23441da177e4SLinus Torvalds {
23451da177e4SLinus Torvalds 	return single_open(file, rt6_stats_seq_show, NULL);
23461da177e4SLinus Torvalds }
23471da177e4SLinus Torvalds 
23489a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
23491da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
23501da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
23511da177e4SLinus Torvalds 	.read	 = seq_read,
23521da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
23531da177e4SLinus Torvalds 	.release = single_release,
23541da177e4SLinus Torvalds };
235575314fb3SDaniel Lezcano 
235675314fb3SDaniel Lezcano static int ipv6_route_proc_init(struct net *net)
235775314fb3SDaniel Lezcano {
235875314fb3SDaniel Lezcano 	int ret = -ENOMEM;
235975314fb3SDaniel Lezcano 	if (!proc_net_fops_create(net, "ipv6_route",
236075314fb3SDaniel Lezcano 				  0, &ipv6_route_proc_fops))
236175314fb3SDaniel Lezcano 		goto out;
236275314fb3SDaniel Lezcano 
236375314fb3SDaniel Lezcano 	if (!proc_net_fops_create(net, "rt6_stats",
236475314fb3SDaniel Lezcano 				  S_IRUGO, &rt6_stats_seq_fops))
236575314fb3SDaniel Lezcano 		goto out_ipv6_route;
236675314fb3SDaniel Lezcano 
236775314fb3SDaniel Lezcano 	ret = 0;
236875314fb3SDaniel Lezcano out:
236975314fb3SDaniel Lezcano 	return ret;
237075314fb3SDaniel Lezcano out_ipv6_route:
237175314fb3SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
237275314fb3SDaniel Lezcano 	goto out;
237375314fb3SDaniel Lezcano }
237475314fb3SDaniel Lezcano 
237575314fb3SDaniel Lezcano static void ipv6_route_proc_fini(struct net *net)
237675314fb3SDaniel Lezcano {
237775314fb3SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
237875314fb3SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
237975314fb3SDaniel Lezcano }
238075314fb3SDaniel Lezcano #else
238175314fb3SDaniel Lezcano static inline int ipv6_route_proc_init(struct net *net)
238275314fb3SDaniel Lezcano {
238375314fb3SDaniel Lezcano 	return 0;
238475314fb3SDaniel Lezcano }
238575314fb3SDaniel Lezcano static inline void ipv6_route_proc_fini(struct net *net)
238675314fb3SDaniel Lezcano {
238775314fb3SDaniel Lezcano 	return ;
238875314fb3SDaniel Lezcano }
23891da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds static
23941da177e4SLinus Torvalds int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
23951da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
23961da177e4SLinus Torvalds {
23974990509fSDaniel Lezcano 	int delay = init_net.ipv6.sysctl.flush_delay;
23981da177e4SLinus Torvalds 	if (write) {
23991da177e4SLinus Torvalds 		proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
24004990509fSDaniel Lezcano 		fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay);
24011da177e4SLinus Torvalds 		return 0;
24021da177e4SLinus Torvalds 	} else
24031da177e4SLinus Torvalds 		return -EINVAL;
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds 
2406760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
24071da177e4SLinus Torvalds 	{
24081da177e4SLinus Torvalds 		.procname	=	"flush",
24094990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
24101da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
241189c8b3a1SDave Jones 		.mode		=	0200,
24121da177e4SLinus Torvalds 		.proc_handler	=	&ipv6_sysctl_rtcache_flush
24131da177e4SLinus Torvalds 	},
24141da177e4SLinus Torvalds 	{
24151da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_THRESH,
24161da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
24171da177e4SLinus Torvalds 		.data		=	&ip6_dst_ops.gc_thresh,
24181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24191da177e4SLinus Torvalds 		.mode		=	0644,
24201da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec,
24211da177e4SLinus Torvalds 	},
24221da177e4SLinus Torvalds 	{
24231da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MAX_SIZE,
24241da177e4SLinus Torvalds 		.procname	=	"max_size",
24254990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
24261da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24271da177e4SLinus Torvalds 		.mode		=	0644,
24281da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec,
24291da177e4SLinus Torvalds 	},
24301da177e4SLinus Torvalds 	{
24311da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL,
24321da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
24334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
24341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24351da177e4SLinus Torvalds 		.mode		=	0644,
24361da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24371da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24381da177e4SLinus Torvalds 	},
24391da177e4SLinus Torvalds 	{
24401da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_TIMEOUT,
24411da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
24424990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
24431da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24441da177e4SLinus Torvalds 		.mode		=	0644,
24451da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24461da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24471da177e4SLinus Torvalds 	},
24481da177e4SLinus Torvalds 	{
24491da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_INTERVAL,
24501da177e4SLinus Torvalds 		.procname	=	"gc_interval",
24514990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
24521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24531da177e4SLinus Torvalds 		.mode		=	0644,
24541da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24551da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24561da177e4SLinus Torvalds 	},
24571da177e4SLinus Torvalds 	{
24581da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_ELASTICITY,
24591da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
24604990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
24611da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24621da177e4SLinus Torvalds 		.mode		=	0644,
24631da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24641da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24651da177e4SLinus Torvalds 	},
24661da177e4SLinus Torvalds 	{
24671da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MTU_EXPIRES,
24681da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
24694990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
24701da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24711da177e4SLinus Torvalds 		.mode		=	0644,
24721da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24731da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24741da177e4SLinus Torvalds 	},
24751da177e4SLinus Torvalds 	{
24761da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MIN_ADVMSS,
24771da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
24784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
24791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24801da177e4SLinus Torvalds 		.mode		=	0644,
24811da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
24821da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
24831da177e4SLinus Torvalds 	},
24841da177e4SLinus Torvalds 	{
24851da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
24861da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
24874990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
24881da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
24891da177e4SLinus Torvalds 		.mode		=	0644,
24901da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_ms_jiffies,
24911da177e4SLinus Torvalds 		.strategy	=	&sysctl_ms_jiffies,
24921da177e4SLinus Torvalds 	},
24931da177e4SLinus Torvalds 	{ .ctl_name = 0 }
24941da177e4SLinus Torvalds };
24951da177e4SLinus Torvalds 
2496760f2d01SDaniel Lezcano struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2497760f2d01SDaniel Lezcano {
2498760f2d01SDaniel Lezcano 	struct ctl_table *table;
2499760f2d01SDaniel Lezcano 
2500760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2501760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2502760f2d01SDaniel Lezcano 			GFP_KERNEL);
2503760f2d01SDaniel Lezcano 	return table;
2504760f2d01SDaniel Lezcano }
25051da177e4SLinus Torvalds #endif
25061da177e4SLinus Torvalds 
2507433d49c3SDaniel Lezcano int __init ip6_route_init(void)
25081da177e4SLinus Torvalds {
2509433d49c3SDaniel Lezcano 	int ret;
2510433d49c3SDaniel Lezcano 
2511e5d679f3SAlexey Dobriyan 	ip6_dst_ops.kmem_cachep =
2512e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2513f845ab6bSDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
2514f845ab6bSDaniel Lezcano 	if (!ip6_dst_ops.kmem_cachep)
2515f845ab6bSDaniel Lezcano 		return -ENOMEM;
2516f845ab6bSDaniel Lezcano 
251714e50e57SDavid S. Miller 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops.kmem_cachep;
251814e50e57SDavid S. Miller 
2519433d49c3SDaniel Lezcano 	ret = fib6_init();
2520433d49c3SDaniel Lezcano 	if (ret)
2521433d49c3SDaniel Lezcano 		goto out_kmem_cache;
2522433d49c3SDaniel Lezcano 
252375314fb3SDaniel Lezcano 	ret = ipv6_route_proc_init(&init_net);
252475314fb3SDaniel Lezcano 	if (ret)
2525433d49c3SDaniel Lezcano 		goto out_fib6_init;
2526433d49c3SDaniel Lezcano 
2527433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2528433d49c3SDaniel Lezcano 	if (ret)
252975314fb3SDaniel Lezcano 		goto out_proc_init;
2530c35b7e72SDaniel Lezcano 
2531433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2532433d49c3SDaniel Lezcano 	if (ret)
2533433d49c3SDaniel Lezcano 		goto xfrm6_init;
25347e5449c2SDaniel Lezcano 
2535433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2536433d49c3SDaniel Lezcano 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2537433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2538433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2539433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2540433d49c3SDaniel Lezcano 
2541433d49c3SDaniel Lezcano 	ret = 0;
2542433d49c3SDaniel Lezcano out:
2543433d49c3SDaniel Lezcano 	return ret;
2544433d49c3SDaniel Lezcano 
2545433d49c3SDaniel Lezcano fib6_rules_init:
2546433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2547433d49c3SDaniel Lezcano xfrm6_init:
2548433d49c3SDaniel Lezcano 	xfrm6_fini();
254975314fb3SDaniel Lezcano out_proc_init:
255075314fb3SDaniel Lezcano 	ipv6_route_proc_fini(&init_net);
2551433d49c3SDaniel Lezcano out_fib6_init:
2552433d49c3SDaniel Lezcano 	rt6_ifdown(NULL);
2553433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
2554433d49c3SDaniel Lezcano out_kmem_cache:
2555433d49c3SDaniel Lezcano 	kmem_cache_destroy(ip6_dst_ops.kmem_cachep);
2556433d49c3SDaniel Lezcano 	goto out;
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds void ip6_route_cleanup(void)
25601da177e4SLinus Torvalds {
2561101367c2SThomas Graf 	fib6_rules_cleanup();
256275314fb3SDaniel Lezcano 	ipv6_route_proc_fini(&init_net);
25631da177e4SLinus Torvalds 	xfrm6_fini();
25641da177e4SLinus Torvalds 	rt6_ifdown(NULL);
25651da177e4SLinus Torvalds 	fib6_gc_cleanup();
25661da177e4SLinus Torvalds 	kmem_cache_destroy(ip6_dst_ops.kmem_cachep);
25671da177e4SLinus Torvalds }
2568