xref: /openbmc/linux/net/ipv6/route.c (revision 633d424bf33dab99e77b36210fbd1b996e7823df)
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>
397bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds #include <linux/if_arp.h>
421da177e4SLinus Torvalds #include <linux/proc_fs.h>
431da177e4SLinus Torvalds #include <linux/seq_file.h>
445b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
45457c4cbcSEric W. Biederman #include <net/net_namespace.h>
461da177e4SLinus Torvalds #include <net/snmp.h>
471da177e4SLinus Torvalds #include <net/ipv6.h>
481da177e4SLinus Torvalds #include <net/ip6_fib.h>
491da177e4SLinus Torvalds #include <net/ip6_route.h>
501da177e4SLinus Torvalds #include <net/ndisc.h>
511da177e4SLinus Torvalds #include <net/addrconf.h>
521da177e4SLinus Torvalds #include <net/tcp.h>
531da177e4SLinus Torvalds #include <linux/rtnetlink.h>
541da177e4SLinus Torvalds #include <net/dst.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
568d71740cSTom Tucker #include <net/netevent.h>
5721713ebcSThomas Graf #include <net/netlink.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/uaccess.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Set to 3 to get tracing. */
661da177e4SLinus Torvalds #define RT6_DEBUG 2
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #if RT6_DEBUG >= 3
691da177e4SLinus Torvalds #define RDBG(x) printk x
701da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
711da177e4SLinus Torvalds #else
721da177e4SLinus Torvalds #define RDBG(x)
731da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
76519fbd87SYOSHIFUJI Hideaki #define CLONE_OFFLINK_ROUTE 0
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
791da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
831da177e4SLinus Torvalds 				       struct net_device *dev, int how);
84569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
871da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
881da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
891da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
901da177e4SLinus Torvalds 
9170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
93efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9470ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
9570ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
96efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
97efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9870ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex);
9970ceb4f5SYOSHIFUJI Hideaki #endif
10070ceb4f5SYOSHIFUJI Hideaki 
1019a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1021da177e4SLinus Torvalds 	.family			=	AF_INET6,
1031da177e4SLinus Torvalds 	.protocol		=	__constant_htons(ETH_P_IPV6),
1041da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1051da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1061da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1071da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1081da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1091da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1101da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1111da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
112862b82c6SHerbert Xu 	.local_out		=	ip6_local_out,
1131da177e4SLinus Torvalds 	.entry_size		=	sizeof(struct rt6_info),
114e2422970SEric Dumazet 	.entries		=	ATOMIC_INIT(0),
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
11714e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
11814e50e57SDavid S. Miller {
11914e50e57SDavid S. Miller }
12014e50e57SDavid S. Miller 
12114e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
12214e50e57SDavid S. Miller 	.family			=	AF_INET6,
12314e50e57SDavid S. Miller 	.protocol		=	__constant_htons(ETH_P_IPV6),
12414e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
12514e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
12614e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
12714e50e57SDavid S. Miller 	.entry_size		=	sizeof(struct rt6_info),
128e2422970SEric Dumazet 	.entries		=	ATOMIC_INIT(0),
12914e50e57SDavid S. Miller };
13014e50e57SDavid S. Miller 
131bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1321da177e4SLinus Torvalds 	.u = {
1331da177e4SLinus Torvalds 		.dst = {
1341da177e4SLinus Torvalds 			.__refcnt	= ATOMIC_INIT(1),
1351da177e4SLinus Torvalds 			.__use		= 1,
1361da177e4SLinus Torvalds 			.obsolete	= -1,
1371da177e4SLinus Torvalds 			.error		= -ENETUNREACH,
1381da177e4SLinus Torvalds 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1391da177e4SLinus Torvalds 			.input		= ip6_pkt_discard,
1401da177e4SLinus Torvalds 			.output		= ip6_pkt_discard_out,
1411da177e4SLinus Torvalds 		}
1421da177e4SLinus Torvalds 	},
1431da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1441da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1451da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1461da177e4SLinus Torvalds };
1471da177e4SLinus Torvalds 
148101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
149101367c2SThomas Graf 
1506723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
1516723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
1526723ab54SDavid S. Miller 
153280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
154101367c2SThomas Graf 	.u = {
155101367c2SThomas Graf 		.dst = {
156101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
157101367c2SThomas Graf 			.__use		= 1,
158101367c2SThomas Graf 			.obsolete	= -1,
159101367c2SThomas Graf 			.error		= -EACCES,
160101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
1619ce8ade0SThomas Graf 			.input		= ip6_pkt_prohibit,
1629ce8ade0SThomas Graf 			.output		= ip6_pkt_prohibit_out,
163101367c2SThomas Graf 		}
164101367c2SThomas Graf 	},
165101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
166101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
167101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
168101367c2SThomas Graf };
169101367c2SThomas Graf 
170bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
171101367c2SThomas Graf 	.u = {
172101367c2SThomas Graf 		.dst = {
173101367c2SThomas Graf 			.__refcnt	= ATOMIC_INIT(1),
174101367c2SThomas Graf 			.__use		= 1,
175101367c2SThomas Graf 			.obsolete	= -1,
176101367c2SThomas Graf 			.error		= -EINVAL,
177101367c2SThomas Graf 			.metrics	= { [RTAX_HOPLIMIT - 1] = 255, },
178352e512cSHerbert Xu 			.input		= dst_discard,
179352e512cSHerbert Xu 			.output		= dst_discard,
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 */
190f2fc6a54SBenjamin Thery static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
1911da177e4SLinus Torvalds {
192f2fc6a54SBenjamin Thery 	return (struct rt6_info *)dst_alloc(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 =
212c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->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 
2408ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
2418ed67789SDaniel Lezcano 						    struct rt6_info *rt,
2421da177e4SLinus Torvalds 						    int oif,
2431da177e4SLinus Torvalds 						    int strict)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
2461da177e4SLinus Torvalds 	struct rt6_info *sprt;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	if (oif) {
2497cc48263SEric Dumazet 		for (sprt = rt; sprt; sprt = sprt->u.dst.rt6_next) {
2501da177e4SLinus Torvalds 			struct net_device *dev = sprt->rt6i_dev;
2511da177e4SLinus Torvalds 			if (dev->ifindex == oif)
2521da177e4SLinus Torvalds 				return sprt;
2531da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
2541da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
2551da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
2561da177e4SLinus Torvalds 					if (strict && oif)
2571da177e4SLinus Torvalds 						continue;
2581da177e4SLinus Torvalds 					if (local && (!oif ||
2591da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
2601da177e4SLinus Torvalds 						continue;
2611da177e4SLinus Torvalds 				}
2621da177e4SLinus Torvalds 				local = sprt;
2631da177e4SLinus Torvalds 			}
2641da177e4SLinus Torvalds 		}
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 		if (local)
2671da177e4SLinus Torvalds 			return local;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (strict)
2708ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	return rt;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
27527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
27627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
27727097255SYOSHIFUJI Hideaki {
27827097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
27927097255SYOSHIFUJI Hideaki 	/*
28027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
28127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
28227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
28327097255SYOSHIFUJI Hideaki 	 *
28427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
28527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
28627097255SYOSHIFUJI Hideaki 	 */
28727097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
28827097255SYOSHIFUJI Hideaki 		return;
28927097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
29027097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
29152e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
29227097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
29327097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
29427097255SYOSHIFUJI Hideaki 
29527097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
29627097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
29727097255SYOSHIFUJI Hideaki 
29827097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
29927097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
30027097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
30127097255SYOSHIFUJI Hideaki 	} else
30227097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
30327097255SYOSHIFUJI Hideaki }
30427097255SYOSHIFUJI Hideaki #else
30527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
30627097255SYOSHIFUJI Hideaki {
30727097255SYOSHIFUJI Hideaki 	return;
30827097255SYOSHIFUJI Hideaki }
30927097255SYOSHIFUJI Hideaki #endif
31027097255SYOSHIFUJI Hideaki 
3111da177e4SLinus Torvalds /*
312554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3131da177e4SLinus Torvalds  */
314b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
3151da177e4SLinus Torvalds {
316554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
317161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
318554cfb7eSYOSHIFUJI Hideaki 		return 2;
319161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
320161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
321161980f4SDavid S. Miller 		return 1;
322554cfb7eSYOSHIFUJI Hideaki 	return 0;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
3261da177e4SLinus Torvalds {
327554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
328398bcbebSYOSHIFUJI Hideaki 	int m;
3294d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
3304d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
3314d0c5911SYOSHIFUJI Hideaki 		m = 1;
3324d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
3331da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
334554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
3354d0c5911SYOSHIFUJI Hideaki 			m = 2;
336398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
337398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
338398bcbebSYOSHIFUJI Hideaki 			m = 0;
339398bcbebSYOSHIFUJI Hideaki #endif
340398bcbebSYOSHIFUJI Hideaki 		else
341ea73ee23SYOSHIFUJI Hideaki 			m = 1;
3421da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
343398bcbebSYOSHIFUJI Hideaki 	} else
344398bcbebSYOSHIFUJI Hideaki 		m = 0;
345554cfb7eSYOSHIFUJI Hideaki 	return m;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
349554cfb7eSYOSHIFUJI Hideaki 			   int strict)
350554cfb7eSYOSHIFUJI Hideaki {
3514d0c5911SYOSHIFUJI Hideaki 	int m, n;
3524d0c5911SYOSHIFUJI Hideaki 
3534d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
35477d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
355554cfb7eSYOSHIFUJI Hideaki 		return -1;
356ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
357ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
358ebacaaa0SYOSHIFUJI Hideaki #endif
3594d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
360557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
361554cfb7eSYOSHIFUJI Hideaki 		return -1;
362554cfb7eSYOSHIFUJI Hideaki 	return m;
363554cfb7eSYOSHIFUJI Hideaki }
364554cfb7eSYOSHIFUJI Hideaki 
365f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
366f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
367554cfb7eSYOSHIFUJI Hideaki {
368554cfb7eSYOSHIFUJI Hideaki 	int m;
369554cfb7eSYOSHIFUJI Hideaki 
370554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
371f11e6659SDavid S. Miller 		goto out;
372554cfb7eSYOSHIFUJI Hideaki 
373554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
374554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
375f11e6659SDavid S. Miller 		goto out;
376554cfb7eSYOSHIFUJI Hideaki 
377f11e6659SDavid S. Miller 	if (m > *mpri) {
378ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
37927097255SYOSHIFUJI Hideaki 			rt6_probe(match);
380f11e6659SDavid S. Miller 		*mpri = m;
381554cfb7eSYOSHIFUJI Hideaki 		match = rt;
382ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
38327097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
3841da177e4SLinus Torvalds 	}
385f11e6659SDavid S. Miller 
386f11e6659SDavid S. Miller out:
387f11e6659SDavid S. Miller 	return match;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
390f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
391f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
392f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
393f11e6659SDavid S. Miller {
394f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
395f11e6659SDavid S. Miller 	int mpri = -1;
396f11e6659SDavid S. Miller 
397f11e6659SDavid S. Miller 	match = NULL;
398f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
399f11e6659SDavid S. Miller 	     rt = rt->u.dst.rt6_next)
400f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
401f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
402f11e6659SDavid S. Miller 	     rt = rt->u.dst.rt6_next)
403f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
404f11e6659SDavid S. Miller 
405f11e6659SDavid S. Miller 	return match;
406f11e6659SDavid S. Miller }
407f11e6659SDavid S. Miller 
408f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
409f11e6659SDavid S. Miller {
410f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
4118ed67789SDaniel Lezcano 	struct net *net;
412f11e6659SDavid S. Miller 
413f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
4140dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
415f11e6659SDavid S. Miller 
416f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
417f11e6659SDavid S. Miller 	if (!rt0)
418f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
419f11e6659SDavid S. Miller 
420f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
421f11e6659SDavid S. Miller 
422554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
423f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
424f11e6659SDavid S. Miller 		struct rt6_info *next = rt0->u.dst.rt6_next;
425f11e6659SDavid S. Miller 
426554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
427f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
428f11e6659SDavid S. Miller 			next = fn->leaf;
429f11e6659SDavid S. Miller 
430f11e6659SDavid S. Miller 		if (next != rt0)
431f11e6659SDavid S. Miller 			fn->rr_ptr = next;
432554cfb7eSYOSHIFUJI Hideaki 	}
433554cfb7eSYOSHIFUJI Hideaki 
434f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
4350dc47877SHarvey Harrison 		  __func__, match);
436554cfb7eSYOSHIFUJI Hideaki 
437c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
4388ed67789SDaniel Lezcano 	return (match ? match : net->ipv6.ip6_null_entry);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
44170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
44270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
44370ceb4f5SYOSHIFUJI Hideaki 		  struct in6_addr *gwaddr)
44470ceb4f5SYOSHIFUJI Hideaki {
445c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
44670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
44770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
44870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
44970ceb4f5SYOSHIFUJI Hideaki 	u32 lifetime;
45070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
45170ceb4f5SYOSHIFUJI Hideaki 
45270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
45370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45470ceb4f5SYOSHIFUJI Hideaki 	}
45570ceb4f5SYOSHIFUJI Hideaki 
45670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
45770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
45870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
45970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
46070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
46170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
46270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
46370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
46470ceb4f5SYOSHIFUJI Hideaki 		}
46570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
46670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
46770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
46870ceb4f5SYOSHIFUJI Hideaki 		}
46970ceb4f5SYOSHIFUJI Hideaki 	}
47070ceb4f5SYOSHIFUJI Hideaki 
47170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
47270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
47370ceb4f5SYOSHIFUJI Hideaki 		pref = ICMPV6_ROUTER_PREF_MEDIUM;
47470ceb4f5SYOSHIFUJI Hideaki 
475e69a4adcSAl Viro 	lifetime = ntohl(rinfo->lifetime);
47670ceb4f5SYOSHIFUJI Hideaki 	if (lifetime == 0xffffffff) {
47770ceb4f5SYOSHIFUJI Hideaki 		/* infinity */
47870ceb4f5SYOSHIFUJI Hideaki 	} else if (lifetime > 0x7fffffff/HZ) {
47970ceb4f5SYOSHIFUJI Hideaki 		/* Avoid arithmetic overflow */
48070ceb4f5SYOSHIFUJI Hideaki 		lifetime = 0x7fffffff/HZ - 1;
48170ceb4f5SYOSHIFUJI Hideaki 	}
48270ceb4f5SYOSHIFUJI Hideaki 
48370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
48470ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
48570ceb4f5SYOSHIFUJI Hideaki 	else {
48670ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
48770ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
48870ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
48970ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
49070ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
49170ceb4f5SYOSHIFUJI Hideaki 	}
49270ceb4f5SYOSHIFUJI Hideaki 
493efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
494efa2cea0SDaniel Lezcano 				dev->ifindex);
49570ceb4f5SYOSHIFUJI Hideaki 
49670ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
497e0a1ad73SThomas Graf 		ip6_del_rt(rt);
49870ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
49970ceb4f5SYOSHIFUJI Hideaki 	}
50070ceb4f5SYOSHIFUJI Hideaki 
50170ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
502efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
50370ceb4f5SYOSHIFUJI Hideaki 					pref);
50470ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
50570ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
50670ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
50770ceb4f5SYOSHIFUJI Hideaki 
50870ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
50970ceb4f5SYOSHIFUJI Hideaki 		if (lifetime == 0xffffffff) {
51070ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
51170ceb4f5SYOSHIFUJI Hideaki 		} else {
51270ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
51370ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
51470ceb4f5SYOSHIFUJI Hideaki 		}
51570ceb4f5SYOSHIFUJI Hideaki 		dst_release(&rt->u.dst);
51670ceb4f5SYOSHIFUJI Hideaki 	}
51770ceb4f5SYOSHIFUJI Hideaki 	return 0;
51870ceb4f5SYOSHIFUJI Hideaki }
51970ceb4f5SYOSHIFUJI Hideaki #endif
52070ceb4f5SYOSHIFUJI Hideaki 
5218ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
522982f56f3SYOSHIFUJI Hideaki do { \
5238ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
524982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
525e0eda7bbSVille Nuorvala 		while (1) { \
526982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
527c71099acSThomas Graf 				goto out; \
528982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
529982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
5308bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
531982f56f3SYOSHIFUJI Hideaki 			else \
532982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
533c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
534c71099acSThomas Graf 				goto restart; \
535c71099acSThomas Graf 		} \
536982f56f3SYOSHIFUJI Hideaki 	} \
537982f56f3SYOSHIFUJI Hideaki } while(0)
538c71099acSThomas Graf 
5398ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
5408ed67789SDaniel Lezcano 					     struct fib6_table *table,
541c71099acSThomas Graf 					     struct flowi *fl, int flags)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	struct fib6_node *fn;
5441da177e4SLinus Torvalds 	struct rt6_info *rt;
5451da177e4SLinus Torvalds 
546c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
547c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
548c71099acSThomas Graf restart:
549c71099acSThomas Graf 	rt = fn->leaf;
5508ed67789SDaniel Lezcano 	rt = rt6_device_match(net, rt, fl->oif, flags);
5518ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
552c71099acSThomas Graf out:
55303f49f34SPavel Emelyanov 	dst_use(&rt->u.dst, jiffies);
554c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
5551da177e4SLinus Torvalds 	return rt;
556c71099acSThomas Graf 
557c71099acSThomas Graf }
558c71099acSThomas Graf 
5599acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
5609acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
561c71099acSThomas Graf {
562c71099acSThomas Graf 	struct flowi fl = {
563c71099acSThomas Graf 		.oif = oif,
564c71099acSThomas Graf 		.nl_u = {
565c71099acSThomas Graf 			.ip6_u = {
566c71099acSThomas Graf 				.daddr = *daddr,
567c71099acSThomas Graf 			},
568c71099acSThomas Graf 		},
569c71099acSThomas Graf 	};
570c71099acSThomas Graf 	struct dst_entry *dst;
57177d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
572c71099acSThomas Graf 
573adaa70bbSThomas Graf 	if (saddr) {
574adaa70bbSThomas Graf 		memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
575adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
576adaa70bbSThomas Graf 	}
577adaa70bbSThomas Graf 
578606a2b48SDaniel Lezcano 	dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
579c71099acSThomas Graf 	if (dst->error == 0)
580c71099acSThomas Graf 		return (struct rt6_info *) dst;
581c71099acSThomas Graf 
582c71099acSThomas Graf 	dst_release(dst);
583c71099acSThomas Graf 
5841da177e4SLinus Torvalds 	return NULL;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5877159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
5887159039aSYOSHIFUJI Hideaki 
589c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
5901da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
5911da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
5921da177e4SLinus Torvalds    be destroyed.
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds 
59586872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	int err;
598c71099acSThomas Graf 	struct fib6_table *table;
5991da177e4SLinus Torvalds 
600c71099acSThomas Graf 	table = rt->rt6i_table;
601c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
60286872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
603c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return err;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
60840e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
60940e22e8fSThomas Graf {
6104d1169c1SDenis V. Lunev 	struct nl_info info = {
611c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
6124d1169c1SDenis V. Lunev 	};
613528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
61440e22e8fSThomas Graf }
61540e22e8fSThomas Graf 
61695a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
61795a9a5baSYOSHIFUJI Hideaki 				      struct in6_addr *saddr)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	struct rt6_info *rt;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	/*
6221da177e4SLinus Torvalds 	 *	Clone the route.
6231da177e4SLinus Torvalds 	 */
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	if (rt) {
62858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
62958c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
63058c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
63158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
6321da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
63358c4fb86SYOSHIFUJI Hideaki 		}
6341da177e4SLinus Torvalds 
63558c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
6361da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
6371da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
6381da177e4SLinus Torvalds 		rt->u.dst.flags |= DST_HOST;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
6411da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
6421da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
6431da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
6441da177e4SLinus Torvalds 		}
6451da177e4SLinus Torvalds #endif
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
6481da177e4SLinus Torvalds 
64995a9a5baSYOSHIFUJI Hideaki 	}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	return rt;
6521da177e4SLinus Torvalds }
65395a9a5baSYOSHIFUJI Hideaki 
654299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
655299d9939SYOSHIFUJI Hideaki {
656299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
657299d9939SYOSHIFUJI Hideaki 	if (rt) {
658299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
659299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
660299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
661299d9939SYOSHIFUJI Hideaki 		rt->u.dst.flags |= DST_HOST;
662299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
663299d9939SYOSHIFUJI Hideaki 	}
664299d9939SYOSHIFUJI Hideaki 	return rt;
665299d9939SYOSHIFUJI Hideaki }
666299d9939SYOSHIFUJI Hideaki 
6678ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
6688ce11e6aSAdrian Bunk 				      struct flowi *fl, int flags)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	struct fib6_node *fn;
671519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
672c71099acSThomas Graf 	int strict = 0;
6731da177e4SLinus Torvalds 	int attempts = 3;
674519fbd87SYOSHIFUJI Hideaki 	int err;
675ea659e07SYOSHIFUJI Hideaki 	int reachable = ipv6_devconf.forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
6761da177e4SLinus Torvalds 
67777d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds relookup:
680c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6811da177e4SLinus Torvalds 
6828238dd06SYOSHIFUJI Hideaki restart_2:
683c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds restart:
6864acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
6878ed67789SDaniel Lezcano 
6888ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
6898ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
6908238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
6911da177e4SLinus Torvalds 		goto out;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	dst_hold(&rt->u.dst);
694c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6951da177e4SLinus Torvalds 
696519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
697e40cf353SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
698519fbd87SYOSHIFUJI Hideaki 	else {
699519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE
700519fbd87SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
701519fbd87SYOSHIFUJI Hideaki #else
702519fbd87SYOSHIFUJI Hideaki 		goto out2;
703519fbd87SYOSHIFUJI Hideaki #endif
704519fbd87SYOSHIFUJI Hideaki 	}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
7078ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
7081da177e4SLinus Torvalds 
709e40cf353SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
710e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
71140e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
712e40cf353SYOSHIFUJI Hideaki 		if (!err)
713e40cf353SYOSHIFUJI Hideaki 			goto out2;
714e40cf353SYOSHIFUJI Hideaki 	}
715e40cf353SYOSHIFUJI Hideaki 
716e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
7171da177e4SLinus Torvalds 		goto out2;
7181da177e4SLinus Torvalds 
719519fbd87SYOSHIFUJI Hideaki 	/*
720c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
721519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
7221da177e4SLinus Torvalds 	 */
7231da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
7241da177e4SLinus Torvalds 	goto relookup;
725e40cf353SYOSHIFUJI Hideaki 
726519fbd87SYOSHIFUJI Hideaki out:
7278238dd06SYOSHIFUJI Hideaki 	if (reachable) {
7288238dd06SYOSHIFUJI Hideaki 		reachable = 0;
7298238dd06SYOSHIFUJI Hideaki 		goto restart_2;
7308238dd06SYOSHIFUJI Hideaki 	}
731519fbd87SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
732c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7331da177e4SLinus Torvalds out2:
7341da177e4SLinus Torvalds 	rt->u.dst.lastuse = jiffies;
7351da177e4SLinus Torvalds 	rt->u.dst.__use++;
736c71099acSThomas Graf 
737c71099acSThomas Graf 	return rt;
738c71099acSThomas Graf }
739c71099acSThomas Graf 
7408ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
7414acad72dSPavel Emelyanov 					    struct flowi *fl, int flags)
7424acad72dSPavel Emelyanov {
7438ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->iif, fl, flags);
7444acad72dSPavel Emelyanov }
7454acad72dSPavel Emelyanov 
746c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
747c71099acSThomas Graf {
7480660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
749c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
750adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
751c71099acSThomas Graf 	struct flowi fl = {
752c71099acSThomas Graf 		.iif = skb->dev->ifindex,
753c71099acSThomas Graf 		.nl_u = {
754c71099acSThomas Graf 			.ip6_u = {
755c71099acSThomas Graf 				.daddr = iph->daddr,
756c71099acSThomas Graf 				.saddr = iph->saddr,
75790bcaf7bSAl Viro 				.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
758c71099acSThomas Graf 			},
759c71099acSThomas Graf 		},
76047dcf0cbSThomas Graf 		.mark = skb->mark,
761c71099acSThomas Graf 		.proto = iph->nexthdr,
762c71099acSThomas Graf 	};
763adaa70bbSThomas Graf 
764adaa70bbSThomas Graf 	if (rt6_need_strict(&iph->daddr))
765adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
766c71099acSThomas Graf 
7675578689aSDaniel Lezcano 	skb->dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input);
768c71099acSThomas Graf }
769c71099acSThomas Graf 
7708ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
771c71099acSThomas Graf 					     struct flowi *fl, int flags)
772c71099acSThomas Graf {
7738ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->oif, fl, flags);
774c71099acSThomas Graf }
775c71099acSThomas Graf 
7764591db4fSDaniel Lezcano struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
7774591db4fSDaniel Lezcano 				    struct flowi *fl)
778c71099acSThomas Graf {
779c71099acSThomas Graf 	int flags = 0;
780c71099acSThomas Graf 
781c71099acSThomas Graf 	if (rt6_need_strict(&fl->fl6_dst))
78277d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
783c71099acSThomas Graf 
784adaa70bbSThomas Graf 	if (!ipv6_addr_any(&fl->fl6_src))
785adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
7867cbca67cSYOSHIFUJI Hideaki 	else if (sk) {
7877cbca67cSYOSHIFUJI Hideaki 		unsigned int prefs = inet6_sk(sk)->srcprefs;
7887cbca67cSYOSHIFUJI Hideaki 		if (prefs & IPV6_PREFER_SRC_TMP)
7897cbca67cSYOSHIFUJI Hideaki 			flags |= RT6_LOOKUP_F_SRCPREF_TMP;
7907cbca67cSYOSHIFUJI Hideaki 		if (prefs & IPV6_PREFER_SRC_PUBLIC)
7917cbca67cSYOSHIFUJI Hideaki 			flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
7927cbca67cSYOSHIFUJI Hideaki 		if (prefs & IPV6_PREFER_SRC_COA)
7937cbca67cSYOSHIFUJI Hideaki 			flags |= RT6_LOOKUP_F_SRCPREF_COA;
7947cbca67cSYOSHIFUJI Hideaki 	}
795adaa70bbSThomas Graf 
7964591db4fSDaniel Lezcano 	return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7997159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
8001da177e4SLinus Torvalds 
80114e50e57SDavid S. Miller int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
80214e50e57SDavid S. Miller {
80314e50e57SDavid S. Miller 	struct rt6_info *ort = (struct rt6_info *) *dstp;
80414e50e57SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)
80514e50e57SDavid S. Miller 		dst_alloc(&ip6_dst_blackhole_ops);
80614e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
80714e50e57SDavid S. Miller 
80814e50e57SDavid S. Miller 	if (rt) {
80914e50e57SDavid S. Miller 		new = &rt->u.dst;
81014e50e57SDavid S. Miller 
81114e50e57SDavid S. Miller 		atomic_set(&new->__refcnt, 1);
81214e50e57SDavid S. Miller 		new->__use = 1;
813352e512cSHerbert Xu 		new->input = dst_discard;
814352e512cSHerbert Xu 		new->output = dst_discard;
81514e50e57SDavid S. Miller 
81614e50e57SDavid S. Miller 		memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
81714e50e57SDavid S. Miller 		new->dev = ort->u.dst.dev;
81814e50e57SDavid S. Miller 		if (new->dev)
81914e50e57SDavid S. Miller 			dev_hold(new->dev);
82014e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
82114e50e57SDavid S. Miller 		if (rt->rt6i_idev)
82214e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
82314e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
82414e50e57SDavid S. Miller 
82514e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
82614e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
82714e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
82814e50e57SDavid S. Miller 
82914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
83014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
83114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
83214e50e57SDavid S. Miller #endif
83314e50e57SDavid S. Miller 
83414e50e57SDavid S. Miller 		dst_free(new);
83514e50e57SDavid S. Miller 	}
83614e50e57SDavid S. Miller 
83714e50e57SDavid S. Miller 	dst_release(*dstp);
83814e50e57SDavid S. Miller 	*dstp = new;
83914e50e57SDavid S. Miller 	return (new ? 0 : -ENOMEM);
84014e50e57SDavid S. Miller }
84114e50e57SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
84214e50e57SDavid S. Miller 
8431da177e4SLinus Torvalds /*
8441da177e4SLinus Torvalds  *	Destination cache support functions
8451da177e4SLinus Torvalds  */
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	struct rt6_info *rt;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
8541da177e4SLinus Torvalds 		return dst;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	return NULL;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	if (rt) {
8641da177e4SLinus Torvalds 		if (rt->rt6i_flags & RTF_CACHE)
865e0a1ad73SThomas Graf 			ip6_del_rt(rt);
8661da177e4SLinus Torvalds 		else
8671da177e4SLinus Torvalds 			dst_release(dst);
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 	return NULL;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	struct rt6_info *rt;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	rt = (struct rt6_info *) skb->dst;
8791da177e4SLinus Torvalds 	if (rt) {
8801da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
8811da177e4SLinus Torvalds 			dst_set_expires(&rt->u.dst, 0);
8821da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
8831da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
8841da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
8931da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
8941da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
8951da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
8961da177e4SLinus Torvalds 			dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
8971da177e4SLinus Torvalds 		}
8981da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
8998d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev);
9041da177e4SLinus Torvalds 
9055578689aSDaniel Lezcano static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
9081da177e4SLinus Torvalds 
9095578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
9105578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	/*
9131da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
9141da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
9151da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
9161da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
9171da177e4SLinus Torvalds 	 */
9181da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
9191da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
9201da177e4SLinus Torvalds 	return mtu;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9233b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
9243b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
9255d0bbeebSThomas Graf 
9263b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
9271da177e4SLinus Torvalds 				  struct neighbour *neigh,
9289acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct rt6_info *rt;
9311da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
932c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
9351da177e4SLinus Torvalds 		return NULL;
9361da177e4SLinus Torvalds 
937f2fc6a54SBenjamin Thery 	rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
9381da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
9391da177e4SLinus Torvalds 		in6_dev_put(idev);
9401da177e4SLinus Torvalds 		goto out;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	dev_hold(dev);
9441da177e4SLinus Torvalds 	if (neigh)
9451da177e4SLinus Torvalds 		neigh_hold(neigh);
9461da177e4SLinus Torvalds 	else
9471da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	rt->rt6i_dev	  = dev;
9501da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
9511da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
9521da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
9531da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
9541da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
9555578689aSDaniel Lezcano 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
9563b00944cSYOSHIFUJI Hideaki 	rt->u.dst.output  = ip6_output;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
9591da177e4SLinus Torvalds 	rt->u.dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
9601da177e4SLinus Torvalds 				? DST_HOST
9611da177e4SLinus Torvalds 				: 0;
9621da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
9631da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
9641da177e4SLinus Torvalds #endif
9651da177e4SLinus Torvalds 
9663b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
9673b00944cSYOSHIFUJI Hideaki 	rt->u.dst.next = icmp6_dst_gc_list;
9683b00944cSYOSHIFUJI Hideaki 	icmp6_dst_gc_list = &rt->u.dst;
9693b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
9701da177e4SLinus Torvalds 
9715578689aSDaniel Lezcano 	fib6_force_start_gc(net);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds out:
97440aa7b90SYOSHIFUJI Hideaki 	return &rt->u.dst;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9773b00944cSYOSHIFUJI Hideaki int icmp6_dst_gc(int *more)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	struct dst_entry *dst, *next, **pprev;
9801da177e4SLinus Torvalds 	int freed;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	next = NULL;
9831da177e4SLinus Torvalds 	freed = 0;
9845d0bbeebSThomas Graf 
9853b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
9863b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
9875d0bbeebSThomas Graf 
9881da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
9891da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
9901da177e4SLinus Torvalds 			*pprev = dst->next;
9911da177e4SLinus Torvalds 			dst_free(dst);
9921da177e4SLinus Torvalds 			freed++;
9931da177e4SLinus Torvalds 		} else {
9941da177e4SLinus Torvalds 			pprev = &dst->next;
9951da177e4SLinus Torvalds 			(*more)++;
9961da177e4SLinus Torvalds 		}
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds 
9993b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10005d0bbeebSThomas Graf 
10011da177e4SLinus Torvalds 	return freed;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
1004569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	unsigned long now = jiffies;
10077019b78eSDaniel Lezcano 	struct net *net = ops->dst_net;
10087019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
10097019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
10107019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
10117019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
10127019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
10131da177e4SLinus Torvalds 
10147019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
10157019b78eSDaniel Lezcano 	    atomic_read(&ops->entries) <= rt_max_size)
10161da177e4SLinus Torvalds 		goto out;
10171da177e4SLinus Torvalds 
10186891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
10196891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
10206891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
10217019b78eSDaniel Lezcano 	if (atomic_read(&ops->entries) < ops->gc_thresh)
10227019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
10231da177e4SLinus Torvalds out:
10247019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
10257019b78eSDaniel Lezcano 	return (atomic_read(&ops->entries) > rt_max_size);
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
10291da177e4SLinus Torvalds    but results in cleaner routing tables.
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds    Remove it only when all the things will work!
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	int mtu = IPV6_MIN_MTU;
10371da177e4SLinus Torvalds 	struct inet6_dev *idev;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	idev = in6_dev_get(dev);
10401da177e4SLinus Torvalds 	if (idev) {
10411da177e4SLinus Torvalds 		mtu = idev->cnf.mtu6;
10421da177e4SLinus Torvalds 		in6_dev_put(idev);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 	return mtu;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10476b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
10481da177e4SLinus Torvalds {
10496b75d090SYOSHIFUJI Hideaki 	int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
10506b75d090SYOSHIFUJI Hideaki 	if (hoplimit < 0) {
10516b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
10526b75d090SYOSHIFUJI Hideaki 		struct inet6_dev *idev = in6_dev_get(dev);
10531da177e4SLinus Torvalds 		if (idev) {
10541da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
10551da177e4SLinus Torvalds 			in6_dev_put(idev);
10566b75d090SYOSHIFUJI Hideaki 		} else
10576b75d090SYOSHIFUJI Hideaki 			hoplimit = ipv6_devconf.hop_limit;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 	return hoplimit;
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  */
10651da177e4SLinus Torvalds 
106686872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	int err;
10695578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
10701da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
10711da177e4SLinus Torvalds 	struct net_device *dev = NULL;
10721da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1073c71099acSThomas Graf 	struct fib6_table *table;
10741da177e4SLinus Torvalds 	int addr_type;
10751da177e4SLinus Torvalds 
107686872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
10771da177e4SLinus Torvalds 		return -EINVAL;
10781da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
107986872cb5SThomas Graf 	if (cfg->fc_src_len)
10801da177e4SLinus Torvalds 		return -EINVAL;
10811da177e4SLinus Torvalds #endif
108286872cb5SThomas Graf 	if (cfg->fc_ifindex) {
10831da177e4SLinus Torvalds 		err = -ENODEV;
10845578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
10851da177e4SLinus Torvalds 		if (!dev)
10861da177e4SLinus Torvalds 			goto out;
10871da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
10881da177e4SLinus Torvalds 		if (!idev)
10891da177e4SLinus Torvalds 			goto out;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
109286872cb5SThomas Graf 	if (cfg->fc_metric == 0)
109386872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
10941da177e4SLinus Torvalds 
10955578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1096c71099acSThomas Graf 	if (table == NULL) {
1097c71099acSThomas Graf 		err = -ENOBUFS;
1098c71099acSThomas Graf 		goto out;
1099c71099acSThomas Graf 	}
1100c71099acSThomas Graf 
1101f2fc6a54SBenjamin Thery 	rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	if (rt == NULL) {
11041da177e4SLinus Torvalds 		err = -ENOMEM;
11051da177e4SLinus Torvalds 		goto out;
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
110986872cb5SThomas Graf 	rt->rt6i_expires = jiffies + clock_t_to_jiffies(cfg->fc_expires);
11101da177e4SLinus Torvalds 
111186872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
111286872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
111386872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
111486872cb5SThomas Graf 
111586872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
11181da177e4SLinus Torvalds 		rt->u.dst.input = ip6_mc_input;
11191da177e4SLinus Torvalds 	else
11201da177e4SLinus Torvalds 		rt->u.dst.input = ip6_forward;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
11231da177e4SLinus Torvalds 
112486872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
112586872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
11261da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
11271da177e4SLinus Torvalds 	       rt->u.dst.flags = DST_HOST;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
113086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
113186872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
11321da177e4SLinus Torvalds #endif
11331da177e4SLinus Torvalds 
113486872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
11371da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
11381da177e4SLinus Torvalds 	 */
113986872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
11401da177e4SLinus Torvalds 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
11411da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
11425578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
11431da177e4SLinus Torvalds 			if (dev) {
11441da177e4SLinus Torvalds 				dev_put(dev);
11451da177e4SLinus Torvalds 				in6_dev_put(idev);
11461da177e4SLinus Torvalds 			}
11475578689aSDaniel Lezcano 			dev = net->loopback_dev;
11481da177e4SLinus Torvalds 			dev_hold(dev);
11491da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
11501da177e4SLinus Torvalds 			if (!idev) {
11511da177e4SLinus Torvalds 				err = -ENODEV;
11521da177e4SLinus Torvalds 				goto out;
11531da177e4SLinus Torvalds 			}
11541da177e4SLinus Torvalds 		}
11551da177e4SLinus Torvalds 		rt->u.dst.output = ip6_pkt_discard_out;
11561da177e4SLinus Torvalds 		rt->u.dst.input = ip6_pkt_discard;
11571da177e4SLinus Torvalds 		rt->u.dst.error = -ENETUNREACH;
11581da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
11591da177e4SLinus Torvalds 		goto install_route;
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds 
116286872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
11631da177e4SLinus Torvalds 		struct in6_addr *gw_addr;
11641da177e4SLinus Torvalds 		int gwa_type;
11651da177e4SLinus Torvalds 
116686872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
116786872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
11681da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
11711da177e4SLinus Torvalds 			struct rt6_info *grt;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
11741da177e4SLinus Torvalds 			   addresses as nexthop address.
11751da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
11761da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
11771da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
11781da177e4SLinus Torvalds 			   some exceptions. --ANK
11791da177e4SLinus Torvalds 			 */
11801da177e4SLinus Torvalds 			err = -EINVAL;
11811da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
11821da177e4SLinus Torvalds 				goto out;
11831da177e4SLinus Torvalds 
11845578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
11871da177e4SLinus Torvalds 			if (grt == NULL)
11881da177e4SLinus Torvalds 				goto out;
11891da177e4SLinus Torvalds 			if (dev) {
11901da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
11911da177e4SLinus Torvalds 					dst_release(&grt->u.dst);
11921da177e4SLinus Torvalds 					goto out;
11931da177e4SLinus Torvalds 				}
11941da177e4SLinus Torvalds 			} else {
11951da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
11961da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
11971da177e4SLinus Torvalds 				dev_hold(dev);
11981da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
11991da177e4SLinus Torvalds 			}
12001da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
12011da177e4SLinus Torvalds 				err = 0;
12021da177e4SLinus Torvalds 			dst_release(&grt->u.dst);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 			if (err)
12051da177e4SLinus Torvalds 				goto out;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 		err = -EINVAL;
12081da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
12091da177e4SLinus Torvalds 			goto out;
12101da177e4SLinus Torvalds 	}
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	err = -ENODEV;
12131da177e4SLinus Torvalds 	if (dev == NULL)
12141da177e4SLinus Torvalds 		goto out;
12151da177e4SLinus Torvalds 
121686872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
12171da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
12181da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
12191da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
12201da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
12211da177e4SLinus Torvalds 			goto out;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 
122586872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds install_route:
122886872cb5SThomas Graf 	if (cfg->fc_mx) {
122986872cb5SThomas Graf 		struct nlattr *nla;
123086872cb5SThomas Graf 		int remaining;
12311da177e4SLinus Torvalds 
123286872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
12338f4c1f9bSThomas Graf 			int type = nla_type(nla);
123486872cb5SThomas Graf 
123586872cb5SThomas Graf 			if (type) {
123686872cb5SThomas Graf 				if (type > RTAX_MAX) {
12371da177e4SLinus Torvalds 					err = -EINVAL;
12381da177e4SLinus Torvalds 					goto out;
12391da177e4SLinus Torvalds 				}
124086872cb5SThomas Graf 
124186872cb5SThomas Graf 				rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
12421da177e4SLinus Torvalds 			}
12431da177e4SLinus Torvalds 		}
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
12471da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
12481da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_MTU-1])
12491da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
12501da177e4SLinus Torvalds 	if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
12515578689aSDaniel Lezcano 		rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
12521da177e4SLinus Torvalds 	rt->u.dst.dev = dev;
12531da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1254c71099acSThomas Graf 	rt->rt6i_table = table;
125563152fc0SDaniel Lezcano 
1256c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
125763152fc0SDaniel Lezcano 
125886872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds out:
12611da177e4SLinus Torvalds 	if (dev)
12621da177e4SLinus Torvalds 		dev_put(dev);
12631da177e4SLinus Torvalds 	if (idev)
12641da177e4SLinus Torvalds 		in6_dev_put(idev);
12651da177e4SLinus Torvalds 	if (rt)
126640aa7b90SYOSHIFUJI Hideaki 		dst_free(&rt->u.dst);
12671da177e4SLinus Torvalds 	return err;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
127086872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	int err;
1273c71099acSThomas Graf 	struct fib6_table *table;
1274c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
12751da177e4SLinus Torvalds 
12768ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
12776c813a72SPatrick McHardy 		return -ENOENT;
12786c813a72SPatrick McHardy 
1279c71099acSThomas Graf 	table = rt->rt6i_table;
1280c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
12811da177e4SLinus Torvalds 
128286872cb5SThomas Graf 	err = fib6_del(rt, info);
12831da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
12841da177e4SLinus Torvalds 
1285c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	return err;
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
1290e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1291e0a1ad73SThomas Graf {
12924d1169c1SDenis V. Lunev 	struct nl_info info = {
1293c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
12944d1169c1SDenis V. Lunev 	};
1295528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1296e0a1ad73SThomas Graf }
1297e0a1ad73SThomas Graf 
129886872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
12991da177e4SLinus Torvalds {
1300c71099acSThomas Graf 	struct fib6_table *table;
13011da177e4SLinus Torvalds 	struct fib6_node *fn;
13021da177e4SLinus Torvalds 	struct rt6_info *rt;
13031da177e4SLinus Torvalds 	int err = -ESRCH;
13041da177e4SLinus Torvalds 
13055578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1306c71099acSThomas Graf 	if (table == NULL)
1307c71099acSThomas Graf 		return err;
13081da177e4SLinus Torvalds 
1309c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1310c71099acSThomas Graf 
1311c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
131286872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
131386872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	if (fn) {
13167cc48263SEric Dumazet 		for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
131786872cb5SThomas Graf 			if (cfg->fc_ifindex &&
13181da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
131986872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
13201da177e4SLinus Torvalds 				continue;
132186872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
132286872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
13231da177e4SLinus Torvalds 				continue;
132486872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
13251da177e4SLinus Torvalds 				continue;
13261da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1327c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
13281da177e4SLinus Torvalds 
132986872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
13301da177e4SLinus Torvalds 		}
13311da177e4SLinus Torvalds 	}
1332c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	return err;
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds /*
13381da177e4SLinus Torvalds  *	Handle redirects
13391da177e4SLinus Torvalds  */
1340a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
1341a6279458SYOSHIFUJI Hideaki 	struct flowi fl;
1342a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1343a6279458SYOSHIFUJI Hideaki };
13441da177e4SLinus Torvalds 
13458ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
13468ed67789SDaniel Lezcano 					     struct fib6_table *table,
1347a6279458SYOSHIFUJI Hideaki 					     struct flowi *fl,
1348a6279458SYOSHIFUJI Hideaki 					     int flags)
1349a6279458SYOSHIFUJI Hideaki {
1350a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1351a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1352a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1353c71099acSThomas Graf 
1354e843b9e1SYOSHIFUJI Hideaki 	/*
1355e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1356e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1357e843b9e1SYOSHIFUJI Hideaki 	 *
1358e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1359e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1360e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1361e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1362e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1363e843b9e1SYOSHIFUJI Hideaki 	 */
13641da177e4SLinus Torvalds 
1365c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1366a6279458SYOSHIFUJI Hideaki 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1367e843b9e1SYOSHIFUJI Hideaki restart:
13687cc48263SEric Dumazet 	for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
13691da177e4SLinus Torvalds 		/*
13701da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
13711da177e4SLinus Torvalds 		 *
13721da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
13731da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
13741da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
13751da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
13761da177e4SLinus Torvalds 		 */
1377e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1378e843b9e1SYOSHIFUJI Hideaki 			continue;
13791da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1380e843b9e1SYOSHIFUJI Hideaki 			continue;
1381a6279458SYOSHIFUJI Hideaki 		if (fl->oif != rt->rt6i_dev->ifindex)
1382e843b9e1SYOSHIFUJI Hideaki 			continue;
1383a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1384e843b9e1SYOSHIFUJI Hideaki 			continue;
1385e843b9e1SYOSHIFUJI Hideaki 		break;
1386e843b9e1SYOSHIFUJI Hideaki 	}
1387a6279458SYOSHIFUJI Hideaki 
1388cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
13898ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
13908ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
1391cb15d9c2SYOSHIFUJI Hideaki out:
1392a6279458SYOSHIFUJI Hideaki 	dst_hold(&rt->u.dst);
1393a6279458SYOSHIFUJI Hideaki 
1394c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
13951da177e4SLinus Torvalds 
1396a6279458SYOSHIFUJI Hideaki 	return rt;
1397a6279458SYOSHIFUJI Hideaki };
1398a6279458SYOSHIFUJI Hideaki 
1399a6279458SYOSHIFUJI Hideaki static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1400a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *src,
1401a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *gateway,
1402a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1403a6279458SYOSHIFUJI Hideaki {
1404adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1405c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1406a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
1407a6279458SYOSHIFUJI Hideaki 		.fl = {
1408a6279458SYOSHIFUJI Hideaki 			.oif = dev->ifindex,
1409a6279458SYOSHIFUJI Hideaki 			.nl_u = {
1410a6279458SYOSHIFUJI Hideaki 				.ip6_u = {
1411a6279458SYOSHIFUJI Hideaki 					.daddr = *dest,
1412a6279458SYOSHIFUJI Hideaki 					.saddr = *src,
1413a6279458SYOSHIFUJI Hideaki 				},
1414a6279458SYOSHIFUJI Hideaki 			},
1415a6279458SYOSHIFUJI Hideaki 		},
1416a6279458SYOSHIFUJI Hideaki 		.gateway = *gateway,
1417a6279458SYOSHIFUJI Hideaki 	};
1418adaa70bbSThomas Graf 
1419adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1420adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1421a6279458SYOSHIFUJI Hideaki 
14225578689aSDaniel Lezcano 	return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
142358f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1424a6279458SYOSHIFUJI Hideaki }
1425a6279458SYOSHIFUJI Hideaki 
1426a6279458SYOSHIFUJI Hideaki void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1427a6279458SYOSHIFUJI Hideaki 		  struct in6_addr *saddr,
1428a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1429a6279458SYOSHIFUJI Hideaki {
1430a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1431a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1432c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1433a6279458SYOSHIFUJI Hideaki 
1434a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1435a6279458SYOSHIFUJI Hideaki 
14368ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
14371da177e4SLinus Torvalds 		if (net_ratelimit())
14381da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
14391da177e4SLinus Torvalds 			       "for redirect target\n");
1440a6279458SYOSHIFUJI Hideaki 		goto out;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	/*
14441da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
14451da177e4SLinus Torvalds 	 */
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
14481da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
14491da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
14501da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
14511da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
14521da177e4SLinus Torvalds 		     );
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	/*
14551da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
14561da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
14571da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
14581da177e4SLinus Torvalds 	 */
14591da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
14621da177e4SLinus Torvalds 	if (neigh == rt->u.dst.neighbour)
14631da177e4SLinus Torvalds 		goto out;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
14661da177e4SLinus Torvalds 	if (nrt == NULL)
14671da177e4SLinus Torvalds 		goto out;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
14701da177e4SLinus Torvalds 	if (on_link)
14711da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
14741da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
14751da177e4SLinus Torvalds 	nrt->u.dst.flags |= DST_HOST;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
14781da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
14791da177e4SLinus Torvalds 	/* Reset pmtu, it may be better */
14801da177e4SLinus Torvalds 	nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
1481c346dca1SYOSHIFUJI Hideaki 	nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
14825578689aSDaniel Lezcano 							dst_mtu(&nrt->u.dst));
14831da177e4SLinus Torvalds 
148440e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
14851da177e4SLinus Torvalds 		goto out;
14861da177e4SLinus Torvalds 
14878d71740cSTom Tucker 	netevent.old = &rt->u.dst;
14888d71740cSTom Tucker 	netevent.new = &nrt->u.dst;
14898d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
14908d71740cSTom Tucker 
14911da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1492e0a1ad73SThomas Graf 		ip6_del_rt(rt);
14931da177e4SLinus Torvalds 		return;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds out:
14971da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
14981da177e4SLinus Torvalds 	return;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds /*
15021da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
15031da177e4SLinus Torvalds  *	i.e. Path MTU discovery
15041da177e4SLinus Torvalds  */
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
15071da177e4SLinus Torvalds 			struct net_device *dev, u32 pmtu)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
1510c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
15111da177e4SLinus Torvalds 	int allfrag = 0;
15121da177e4SLinus Torvalds 
15135578689aSDaniel Lezcano 	rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
15141da177e4SLinus Torvalds 	if (rt == NULL)
15151da177e4SLinus Torvalds 		return;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	if (pmtu >= dst_mtu(&rt->u.dst))
15181da177e4SLinus Torvalds 		goto out;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
15211da177e4SLinus Torvalds 		/*
15221da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
15231da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
15241da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
15251da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
15261da177e4SLinus Torvalds 		 */
15271da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
15281da177e4SLinus Torvalds 		allfrag = 1;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
15321da177e4SLinus Torvalds 	   They are sent only in response to data packets,
15331da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
15341da177e4SLinus Torvalds 	 */
15351da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
15381da177e4SLinus Torvalds 	   not to override it, but add new one, so that
15391da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
15401da177e4SLinus Torvalds 	   would return automatically.
15411da177e4SLinus Torvalds 	 */
15421da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
15431da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
15441da177e4SLinus Torvalds 		if (allfrag)
15451da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
15465578689aSDaniel Lezcano 		dst_set_expires(&rt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
15471da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
15481da177e4SLinus Torvalds 		goto out;
15491da177e4SLinus Torvalds 	}
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	/* Network route.
15521da177e4SLinus Torvalds 	   Two cases are possible:
15531da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
15541da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
15551da177e4SLinus Torvalds 	 */
1556d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1557a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1558d5315b50SYOSHIFUJI Hideaki 	else
1559d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1560a1e78363SYOSHIFUJI Hideaki 
1561d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
15621da177e4SLinus Torvalds 		nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
15631da177e4SLinus Torvalds 		if (allfrag)
15641da177e4SLinus Torvalds 			nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1565a1e78363SYOSHIFUJI Hideaki 
15661da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1567a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1568a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1569a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1570a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
15711da177e4SLinus Torvalds 		 */
15725578689aSDaniel Lezcano 		dst_set_expires(&nrt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
15731da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1574a1e78363SYOSHIFUJI Hideaki 
157540e22e8fSThomas Graf 		ip6_ins_rt(nrt);
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds out:
15781da177e4SLinus Torvalds 	dst_release(&rt->u.dst);
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds /*
15821da177e4SLinus Torvalds  *	Misc support functions
15831da177e4SLinus Torvalds  */
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
15861da177e4SLinus Torvalds {
1587c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
1588f2fc6a54SBenjamin Thery 	struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	if (rt) {
15911da177e4SLinus Torvalds 		rt->u.dst.input = ort->u.dst.input;
15921da177e4SLinus Torvalds 		rt->u.dst.output = ort->u.dst.output;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
159522e1e4d8SVille Nuorvala 		rt->u.dst.error = ort->u.dst.error;
15961da177e4SLinus Torvalds 		rt->u.dst.dev = ort->u.dst.dev;
15971da177e4SLinus Torvalds 		if (rt->u.dst.dev)
15981da177e4SLinus Torvalds 			dev_hold(rt->u.dst.dev);
15991da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
16001da177e4SLinus Torvalds 		if (rt->rt6i_idev)
16011da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
16021da177e4SLinus Torvalds 		rt->u.dst.lastuse = jiffies;
16031da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
16061da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
16071da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
16101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
16111da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
16121da177e4SLinus Torvalds #endif
1613c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 	return rt;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
161870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1619efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1620efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
162170ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex)
162270ceb4f5SYOSHIFUJI Hideaki {
162370ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
162470ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1625c71099acSThomas Graf 	struct fib6_table *table;
162670ceb4f5SYOSHIFUJI Hideaki 
1627efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1628c71099acSThomas Graf 	if (table == NULL)
1629c71099acSThomas Graf 		return NULL;
1630c71099acSThomas Graf 
1631c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1632c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
163370ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
163470ceb4f5SYOSHIFUJI Hideaki 		goto out;
163570ceb4f5SYOSHIFUJI Hideaki 
16367cc48263SEric Dumazet 	for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
163770ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
163870ceb4f5SYOSHIFUJI Hideaki 			continue;
163970ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
164070ceb4f5SYOSHIFUJI Hideaki 			continue;
164170ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
164270ceb4f5SYOSHIFUJI Hideaki 			continue;
164370ceb4f5SYOSHIFUJI Hideaki 		dst_hold(&rt->u.dst);
164470ceb4f5SYOSHIFUJI Hideaki 		break;
164570ceb4f5SYOSHIFUJI Hideaki 	}
164670ceb4f5SYOSHIFUJI Hideaki out:
1647c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
164870ceb4f5SYOSHIFUJI Hideaki 	return rt;
164970ceb4f5SYOSHIFUJI Hideaki }
165070ceb4f5SYOSHIFUJI Hideaki 
1651efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1652efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
165370ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
165470ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
165570ceb4f5SYOSHIFUJI Hideaki {
165686872cb5SThomas Graf 	struct fib6_config cfg = {
165786872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1658238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
165986872cb5SThomas Graf 		.fc_ifindex	= ifindex,
166086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
166186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
166286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1663efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1664efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1665efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
166686872cb5SThomas Graf 	};
166770ceb4f5SYOSHIFUJI Hideaki 
166886872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
166986872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
167086872cb5SThomas Graf 
1671e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1672e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
167386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
167470ceb4f5SYOSHIFUJI Hideaki 
167586872cb5SThomas Graf 	ip6_route_add(&cfg);
167670ceb4f5SYOSHIFUJI Hideaki 
1677efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
167870ceb4f5SYOSHIFUJI Hideaki }
167970ceb4f5SYOSHIFUJI Hideaki #endif
168070ceb4f5SYOSHIFUJI Hideaki 
16811da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	struct rt6_info *rt;
1684c71099acSThomas Graf 	struct fib6_table *table;
16851da177e4SLinus Torvalds 
1686c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1687c71099acSThomas Graf 	if (table == NULL)
1688c71099acSThomas Graf 		return NULL;
16891da177e4SLinus Torvalds 
1690c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
16917cc48263SEric Dumazet 	for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
16921da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1693045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
16941da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
16951da177e4SLinus Torvalds 			break;
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 	if (rt)
16981da177e4SLinus Torvalds 		dst_hold(&rt->u.dst);
1699c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
17001da177e4SLinus Torvalds 	return rt;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1704ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1705ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
17061da177e4SLinus Torvalds {
170786872cb5SThomas Graf 	struct fib6_config cfg = {
170886872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1709238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
171086872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
171186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
171286872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
17135578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
17145578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1715c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
171686872cb5SThomas Graf 	};
17171da177e4SLinus Torvalds 
171886872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
17191da177e4SLinus Torvalds 
172086872cb5SThomas Graf 	ip6_route_add(&cfg);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
17231da177e4SLinus Torvalds }
17241da177e4SLinus Torvalds 
17257b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
17261da177e4SLinus Torvalds {
17271da177e4SLinus Torvalds 	struct rt6_info *rt;
1728c71099acSThomas Graf 	struct fib6_table *table;
1729c71099acSThomas Graf 
1730c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
17317b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1732c71099acSThomas Graf 	if (table == NULL)
1733c71099acSThomas Graf 		return;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds restart:
1736c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
17377cc48263SEric Dumazet 	for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
17381da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
17391da177e4SLinus Torvalds 			dst_hold(&rt->u.dst);
1740c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1741e0a1ad73SThomas Graf 			ip6_del_rt(rt);
17421da177e4SLinus Torvalds 			goto restart;
17431da177e4SLinus Torvalds 		}
17441da177e4SLinus Torvalds 	}
1745c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
17485578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
17495578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
175086872cb5SThomas Graf 				 struct fib6_config *cfg)
175186872cb5SThomas Graf {
175286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
175386872cb5SThomas Graf 
175486872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
175586872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
175686872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
175786872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
175886872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
175986872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
176086872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
176186872cb5SThomas Graf 
17625578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1763f1243c2dSBenjamin Thery 
176486872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
176586872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
176686872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
176786872cb5SThomas Graf }
176886872cb5SThomas Graf 
17695578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
17701da177e4SLinus Torvalds {
177186872cb5SThomas Graf 	struct fib6_config cfg;
17721da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
17731da177e4SLinus Torvalds 	int err;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	switch(cmd) {
17761da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
17771da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
17781da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
17791da177e4SLinus Torvalds 			return -EPERM;
17801da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
17811da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
17821da177e4SLinus Torvalds 		if (err)
17831da177e4SLinus Torvalds 			return -EFAULT;
17841da177e4SLinus Torvalds 
17855578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
178686872cb5SThomas Graf 
17871da177e4SLinus Torvalds 		rtnl_lock();
17881da177e4SLinus Torvalds 		switch (cmd) {
17891da177e4SLinus Torvalds 		case SIOCADDRT:
179086872cb5SThomas Graf 			err = ip6_route_add(&cfg);
17911da177e4SLinus Torvalds 			break;
17921da177e4SLinus Torvalds 		case SIOCDELRT:
179386872cb5SThomas Graf 			err = ip6_route_del(&cfg);
17941da177e4SLinus Torvalds 			break;
17951da177e4SLinus Torvalds 		default:
17961da177e4SLinus Torvalds 			err = -EINVAL;
17971da177e4SLinus Torvalds 		}
17981da177e4SLinus Torvalds 		rtnl_unlock();
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 		return err;
18013ff50b79SStephen Hemminger 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	return -EINVAL;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds /*
18071da177e4SLinus Torvalds  *	Drop the packet on the floor
18081da177e4SLinus Torvalds  */
18091da177e4SLinus Torvalds 
181050eb431dSIlpo Järvinen static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
18111da177e4SLinus Torvalds {
1812612f09e8SYOSHIFUJI Hideaki 	int type;
1813612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1814612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
18150660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1816612f09e8SYOSHIFUJI Hideaki 		if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
1817a11d206dSYOSHIFUJI Hideaki 			IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INADDRERRORS);
1818612f09e8SYOSHIFUJI Hideaki 			break;
1819612f09e8SYOSHIFUJI Hideaki 		}
1820612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1821612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
1822612f09e8SYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), ipstats_mib_noroutes);
1823612f09e8SYOSHIFUJI Hideaki 		break;
1824612f09e8SYOSHIFUJI Hideaki 	}
18259ce8ade0SThomas Graf 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
18261da177e4SLinus Torvalds 	kfree_skb(skb);
18271da177e4SLinus Torvalds 	return 0;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
18309ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
18319ce8ade0SThomas Graf {
1832612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
18339ce8ade0SThomas Graf }
18349ce8ade0SThomas Graf 
183520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
18361da177e4SLinus Torvalds {
18371da177e4SLinus Torvalds 	skb->dev = skb->dst->dev;
1838612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
18416723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
18426723ab54SDavid S. Miller 
18439ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
18449ce8ade0SThomas Graf {
1845612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
18469ce8ade0SThomas Graf }
18479ce8ade0SThomas Graf 
18489ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
18499ce8ade0SThomas Graf {
18509ce8ade0SThomas Graf 	skb->dev = skb->dst->dev;
1851612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
18529ce8ade0SThomas Graf }
18539ce8ade0SThomas Graf 
18546723ab54SDavid S. Miller #endif
18556723ab54SDavid S. Miller 
18561da177e4SLinus Torvalds /*
18571da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
18581da177e4SLinus Torvalds  */
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
18611da177e4SLinus Torvalds 				    const struct in6_addr *addr,
18621da177e4SLinus Torvalds 				    int anycast)
18631da177e4SLinus Torvalds {
1864c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
1865f2fc6a54SBenjamin Thery 	struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	if (rt == NULL)
18681da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18691da177e4SLinus Torvalds 
18705578689aSDaniel Lezcano 	dev_hold(net->loopback_dev);
18711da177e4SLinus Torvalds 	in6_dev_hold(idev);
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	rt->u.dst.flags = DST_HOST;
18741da177e4SLinus Torvalds 	rt->u.dst.input = ip6_input;
18751da177e4SLinus Torvalds 	rt->u.dst.output = ip6_output;
18765578689aSDaniel Lezcano 	rt->rt6i_dev = net->loopback_dev;
18771da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
18781da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
18795578689aSDaniel Lezcano 	rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
18801da177e4SLinus Torvalds 	rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
18811da177e4SLinus Torvalds 	rt->u.dst.obsolete = -1;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
188458c4fb86SYOSHIFUJI Hideaki 	if (anycast)
188558c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
188658c4fb86SYOSHIFUJI Hideaki 	else
18871da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
18881da177e4SLinus Torvalds 	rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
18891da177e4SLinus Torvalds 	if (rt->rt6i_nexthop == NULL) {
189040aa7b90SYOSHIFUJI Hideaki 		dst_free(&rt->u.dst);
18911da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
18951da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
18965578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	atomic_set(&rt->u.dst.__refcnt, 1);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	return rt;
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds 
19038ed67789SDaniel Lezcano struct arg_dev_net {
19048ed67789SDaniel Lezcano 	struct net_device *dev;
19058ed67789SDaniel Lezcano 	struct net *net;
19068ed67789SDaniel Lezcano };
19078ed67789SDaniel Lezcano 
19081da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
19091da177e4SLinus Torvalds {
19108ed67789SDaniel Lezcano 	struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
19118ed67789SDaniel Lezcano 	struct net *net = ((struct arg_dev_net *)arg)->net;
19128ed67789SDaniel Lezcano 
19138ed67789SDaniel Lezcano 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
19148ed67789SDaniel Lezcano 	    rt != net->ipv6.ip6_null_entry) {
19151da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
19161da177e4SLinus Torvalds 		return -1;
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
1921f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
19221da177e4SLinus Torvalds {
19238ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
19248ed67789SDaniel Lezcano 		.dev = dev,
19258ed67789SDaniel Lezcano 		.net = net,
19268ed67789SDaniel Lezcano 	};
19278ed67789SDaniel Lezcano 
19288ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds struct rt6_mtu_change_arg
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	struct net_device *dev;
19341da177e4SLinus Torvalds 	unsigned mtu;
19351da177e4SLinus Torvalds };
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
19381da177e4SLinus Torvalds {
19391da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
19401da177e4SLinus Torvalds 	struct inet6_dev *idev;
1941c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(arg->dev);
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
19441da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
19451da177e4SLinus Torvalds 	   We still use this lock to block changes
19461da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
19471da177e4SLinus Torvalds 	*/
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
19501da177e4SLinus Torvalds 	if (idev == NULL)
19511da177e4SLinus Torvalds 		return 0;
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
19541da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
19551da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
19561da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
19571da177e4SLinus Torvalds 	 */
19581da177e4SLinus Torvalds 	/*
19591da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
19601da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
19611da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
19621da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
19631da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
19641da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
19651da177e4SLinus Torvalds 	   PMTU discouvery.
19661da177e4SLinus Torvalds 	 */
19671da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
19681da177e4SLinus Torvalds 	    !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
196923717795SJim Paris 	    (dst_mtu(&rt->u.dst) >= arg->mtu ||
19701da177e4SLinus Torvalds 	     (dst_mtu(&rt->u.dst) < arg->mtu &&
1971566cfd8fSSimon Arlott 	      dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
19721da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
19735578689aSDaniel Lezcano 		rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
1974566cfd8fSSimon Arlott 	}
19751da177e4SLinus Torvalds 	return 0;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
19791da177e4SLinus Torvalds {
1980c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
1981c71099acSThomas Graf 		.dev = dev,
1982c71099acSThomas Graf 		.mtu = mtu,
1983c71099acSThomas Graf 	};
19841da177e4SLinus Torvalds 
1985c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
1988ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
19895176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
199086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
1991ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
199286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
199386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
199486872cb5SThomas Graf };
199586872cb5SThomas Graf 
199686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
199786872cb5SThomas Graf 			      struct fib6_config *cfg)
19981da177e4SLinus Torvalds {
199986872cb5SThomas Graf 	struct rtmsg *rtm;
200086872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
200186872cb5SThomas Graf 	int err;
20021da177e4SLinus Torvalds 
200386872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
200486872cb5SThomas Graf 	if (err < 0)
200586872cb5SThomas Graf 		goto errout;
20061da177e4SLinus Torvalds 
200786872cb5SThomas Graf 	err = -EINVAL;
200886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
200986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
201086872cb5SThomas Graf 
201186872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
201286872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
201386872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
201486872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
201586872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
201686872cb5SThomas Graf 
201786872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
201886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
201986872cb5SThomas Graf 
202086872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
202186872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
20223b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
202386872cb5SThomas Graf 
202486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
202586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
202686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
20271da177e4SLinus Torvalds 	}
202886872cb5SThomas Graf 
202986872cb5SThomas Graf 	if (tb[RTA_DST]) {
203086872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
203186872cb5SThomas Graf 
203286872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
203386872cb5SThomas Graf 			goto errout;
203486872cb5SThomas Graf 
203586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
20361da177e4SLinus Torvalds 	}
203786872cb5SThomas Graf 
203886872cb5SThomas Graf 	if (tb[RTA_SRC]) {
203986872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
204086872cb5SThomas Graf 
204186872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
204286872cb5SThomas Graf 			goto errout;
204386872cb5SThomas Graf 
204486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
20451da177e4SLinus Torvalds 	}
204686872cb5SThomas Graf 
204786872cb5SThomas Graf 	if (tb[RTA_OIF])
204886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
204986872cb5SThomas Graf 
205086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
205186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
205286872cb5SThomas Graf 
205386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
205486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
205586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
20561da177e4SLinus Torvalds 	}
205786872cb5SThomas Graf 
205886872cb5SThomas Graf 	if (tb[RTA_TABLE])
205986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
206086872cb5SThomas Graf 
206186872cb5SThomas Graf 	err = 0;
206286872cb5SThomas Graf errout:
206386872cb5SThomas Graf 	return err;
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
2066c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
20671da177e4SLinus Torvalds {
206886872cb5SThomas Graf 	struct fib6_config cfg;
206986872cb5SThomas Graf 	int err;
20701da177e4SLinus Torvalds 
207186872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
207286872cb5SThomas Graf 	if (err < 0)
207386872cb5SThomas Graf 		return err;
207486872cb5SThomas Graf 
207586872cb5SThomas Graf 	return ip6_route_del(&cfg);
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
2078c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
20791da177e4SLinus Torvalds {
208086872cb5SThomas Graf 	struct fib6_config cfg;
208186872cb5SThomas Graf 	int err;
20821da177e4SLinus Torvalds 
208386872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
208486872cb5SThomas Graf 	if (err < 0)
208586872cb5SThomas Graf 		return err;
208686872cb5SThomas Graf 
208786872cb5SThomas Graf 	return ip6_route_add(&cfg);
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
2090339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2091339bf98fSThomas Graf {
2092339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2093339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2094339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2095339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2096339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2097339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2098339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2099339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2100339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
21016a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2102339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2103339bf98fSThomas Graf }
2104339bf98fSThomas Graf 
21051da177e4SLinus Torvalds static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
21060d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
21070d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
21087bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
21091da177e4SLinus Torvalds {
21101da177e4SLinus Torvalds 	struct rtmsg *rtm;
21111da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2112e3703b3dSThomas Graf 	long expires;
21139e762a4aSPatrick McHardy 	u32 table;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
21161da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
21171da177e4SLinus Torvalds 			/* success since this is not a prefix route */
21181da177e4SLinus Torvalds 			return 1;
21191da177e4SLinus Torvalds 		}
21201da177e4SLinus Torvalds 	}
21211da177e4SLinus Torvalds 
21222d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
21232d7202bfSThomas Graf 	if (nlh == NULL)
212426932566SPatrick McHardy 		return -EMSGSIZE;
21252d7202bfSThomas Graf 
21262d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
21271da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
21281da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
21291da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
21301da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2131c71099acSThomas Graf 	if (rt->rt6i_table)
21329e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2133c71099acSThomas Graf 	else
21349e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
21359e762a4aSPatrick McHardy 	rtm->rtm_table = table;
21362d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
21371da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
21381da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
21391da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
21401da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
21411da177e4SLinus Torvalds 	else
21421da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
21431da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
21441da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
21451da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
21461da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
21471da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
21481da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
21491da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
21501da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
21511da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
21541da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	if (dst) {
21572d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
21581da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
21591da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
21602d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
21611da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
21621da177e4SLinus Torvalds 	if (src) {
21632d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
21641da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
21651da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
21662d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
21671da177e4SLinus Torvalds #endif
21687bc570c8SYOSHIFUJI Hideaki 	if (iif) {
21697bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
21707bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
21717bc570c8SYOSHIFUJI Hideaki 			int err = ip6mr_get_route(skb, rtm, nowait);
21727bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
21737bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
21747bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
21757bc570c8SYOSHIFUJI Hideaki 						return 0;
21767bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
21777bc570c8SYOSHIFUJI Hideaki 				} else {
21787bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
21797bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
21807bc570c8SYOSHIFUJI Hideaki 				}
21817bc570c8SYOSHIFUJI Hideaki 			}
21827bc570c8SYOSHIFUJI Hideaki 		} else
21837bc570c8SYOSHIFUJI Hideaki #endif
21842d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
21857bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
21861da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
21875e5f3f0fSYOSHIFUJI Hideaki 		if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
21887cbca67cSYOSHIFUJI Hideaki 				       dst, 0, &saddr_buf) == 0)
21892d7202bfSThomas Graf 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
21901da177e4SLinus Torvalds 	}
21912d7202bfSThomas Graf 
21921da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
21932d7202bfSThomas Graf 		goto nla_put_failure;
21942d7202bfSThomas Graf 
21951da177e4SLinus Torvalds 	if (rt->u.dst.neighbour)
21962d7202bfSThomas Graf 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
21972d7202bfSThomas Graf 
21981da177e4SLinus Torvalds 	if (rt->u.dst.dev)
21992d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
22002d7202bfSThomas Graf 
22012d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2202e3703b3dSThomas Graf 
2203e3703b3dSThomas Graf 	expires = rt->rt6i_expires ? rt->rt6i_expires - jiffies : 0;
2204e3703b3dSThomas Graf 	if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2205e3703b3dSThomas Graf 			       expires, rt->u.dst.error) < 0)
2206e3703b3dSThomas Graf 		goto nla_put_failure;
22071da177e4SLinus Torvalds 
22082d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
22092d7202bfSThomas Graf 
22102d7202bfSThomas Graf nla_put_failure:
221126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
221226932566SPatrick McHardy 	return -EMSGSIZE;
22131da177e4SLinus Torvalds }
22141da177e4SLinus Torvalds 
22151b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
22161da177e4SLinus Torvalds {
22171da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
22181da177e4SLinus Torvalds 	int prefix;
22191da177e4SLinus Torvalds 
22202d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
22212d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
22221da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
22231da177e4SLinus Torvalds 	} else
22241da177e4SLinus Torvalds 		prefix = 0;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
22271da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
22287bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
2231c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
22321da177e4SLinus Torvalds {
22333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2234ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
22351da177e4SLinus Torvalds 	struct rt6_info *rt;
2236ab364a6fSThomas Graf 	struct sk_buff *skb;
2237ab364a6fSThomas Graf 	struct rtmsg *rtm;
2238ab364a6fSThomas Graf 	struct flowi fl;
2239ab364a6fSThomas Graf 	int err, iif = 0;
2240ab364a6fSThomas Graf 
2241ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2242ab364a6fSThomas Graf 	if (err < 0)
2243ab364a6fSThomas Graf 		goto errout;
2244ab364a6fSThomas Graf 
2245ab364a6fSThomas Graf 	err = -EINVAL;
2246ab364a6fSThomas Graf 	memset(&fl, 0, sizeof(fl));
2247ab364a6fSThomas Graf 
2248ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2249ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2250ab364a6fSThomas Graf 			goto errout;
2251ab364a6fSThomas Graf 
2252ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2253ab364a6fSThomas Graf 	}
2254ab364a6fSThomas Graf 
2255ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2256ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2257ab364a6fSThomas Graf 			goto errout;
2258ab364a6fSThomas Graf 
2259ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2260ab364a6fSThomas Graf 	}
2261ab364a6fSThomas Graf 
2262ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2263ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2264ab364a6fSThomas Graf 
2265ab364a6fSThomas Graf 	if (tb[RTA_OIF])
2266ab364a6fSThomas Graf 		fl.oif = nla_get_u32(tb[RTA_OIF]);
2267ab364a6fSThomas Graf 
2268ab364a6fSThomas Graf 	if (iif) {
2269ab364a6fSThomas Graf 		struct net_device *dev;
22705578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2271ab364a6fSThomas Graf 		if (!dev) {
2272ab364a6fSThomas Graf 			err = -ENODEV;
2273ab364a6fSThomas Graf 			goto errout;
2274ab364a6fSThomas Graf 		}
2275ab364a6fSThomas Graf 	}
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2278ab364a6fSThomas Graf 	if (skb == NULL) {
2279ab364a6fSThomas Graf 		err = -ENOBUFS;
2280ab364a6fSThomas Graf 		goto errout;
2281ab364a6fSThomas Graf 	}
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
22841da177e4SLinus Torvalds 	   through good chunk of routing engine.
22851da177e4SLinus Torvalds 	 */
2286459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
22871da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
22881da177e4SLinus Torvalds 
22898a3edd80SDaniel Lezcano 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
22901da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
22911da177e4SLinus Torvalds 
2292ab364a6fSThomas Graf 	err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
22931da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
22947bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
22951da177e4SLinus Torvalds 	if (err < 0) {
2296ab364a6fSThomas Graf 		kfree_skb(skb);
2297ab364a6fSThomas Graf 		goto errout;
22981da177e4SLinus Torvalds 	}
22991da177e4SLinus Torvalds 
23005578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2301ab364a6fSThomas Graf errout:
23021da177e4SLinus Torvalds 	return err;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
230586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
23061da177e4SLinus Torvalds {
23071da177e4SLinus Torvalds 	struct sk_buff *skb;
23085578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2309528c4cebSDenis V. Lunev 	u32 seq;
2310528c4cebSDenis V. Lunev 	int err;
23110d51aa80SJamal Hadi Salim 
2312528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2313528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
231486872cb5SThomas Graf 
2315339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
231621713ebcSThomas Graf 	if (skb == NULL)
231721713ebcSThomas Graf 		goto errout;
23181da177e4SLinus Torvalds 
2319528c4cebSDenis V. Lunev 	err = rt6_fill_node(skb, rt, NULL, NULL, 0,
23207bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
232126932566SPatrick McHardy 	if (err < 0) {
232226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
232326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
232426932566SPatrick McHardy 		kfree_skb(skb);
232526932566SPatrick McHardy 		goto errout;
232626932566SPatrick McHardy 	}
23275578689aSDaniel Lezcano 	err = rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
23285578689aSDaniel Lezcano 			  info->nlh, gfp_any());
232921713ebcSThomas Graf errout:
233021713ebcSThomas Graf 	if (err < 0)
23315578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23348ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
23358ed67789SDaniel Lezcano 				unsigned long event, void *data)
23368ed67789SDaniel Lezcano {
23378ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2338c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
23398ed67789SDaniel Lezcano 
23408ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
23418ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->u.dst.dev = dev;
23428ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
23438ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
23448ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->u.dst.dev = dev;
23458ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
23468ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->u.dst.dev = dev;
23478ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
23488ed67789SDaniel Lezcano #endif
23498ed67789SDaniel Lezcano 	}
23508ed67789SDaniel Lezcano 
23518ed67789SDaniel Lezcano 	return NOTIFY_OK;
23528ed67789SDaniel Lezcano }
23538ed67789SDaniel Lezcano 
23541da177e4SLinus Torvalds /*
23551da177e4SLinus Torvalds  *	/proc
23561da177e4SLinus Torvalds  */
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds struct rt6_proc_arg
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	char *buffer;
23651da177e4SLinus Torvalds 	int offset;
23661da177e4SLinus Torvalds 	int length;
23671da177e4SLinus Torvalds 	int skip;
23681da177e4SLinus Torvalds 	int len;
23691da177e4SLinus Torvalds };
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
23721da177e4SLinus Torvalds {
237333120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
23741da177e4SLinus Torvalds 
237533120b30SAlexey Dobriyan 	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
23761da177e4SLinus Torvalds 		   rt->rt6i_dst.plen);
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
237933120b30SAlexey Dobriyan 	seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
23801da177e4SLinus Torvalds 		   rt->rt6i_src.plen);
23811da177e4SLinus Torvalds #else
238233120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
23831da177e4SLinus Torvalds #endif
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
238633120b30SAlexey Dobriyan 		seq_printf(m, NIP6_SEQFMT,
238733e93c96SYOSHIFUJI Hideaki 			   NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
23881da177e4SLinus Torvalds 	} else {
238933120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
23901da177e4SLinus Torvalds 	}
239133120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
23921da177e4SLinus Torvalds 		   rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
23931da177e4SLinus Torvalds 		   rt->u.dst.__use, rt->rt6i_flags,
23941da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
23951da177e4SLinus Torvalds 	return 0;
23961da177e4SLinus Torvalds }
23971da177e4SLinus Torvalds 
239833120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
23991da177e4SLinus Torvalds {
2400f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2401f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
240233120b30SAlexey Dobriyan 	return 0;
24031da177e4SLinus Torvalds }
24041da177e4SLinus Torvalds 
240533120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
240633120b30SAlexey Dobriyan {
2407a2333525SPavel Emelyanov 	int err;
2408f3db4851SDaniel Lezcano 	struct net *net = get_proc_net(inode);
2409f3db4851SDaniel Lezcano 	if (!net)
2410f3db4851SDaniel Lezcano 		return -ENXIO;
2411a2333525SPavel Emelyanov 
2412a2333525SPavel Emelyanov 	err = single_open(file, ipv6_route_show, net);
2413a2333525SPavel Emelyanov 	if (err < 0) {
2414a2333525SPavel Emelyanov 		put_net(net);
2415a2333525SPavel Emelyanov 		return err;
2416a2333525SPavel Emelyanov 	}
2417a2333525SPavel Emelyanov 
2418a2333525SPavel Emelyanov 	return 0;
2419f3db4851SDaniel Lezcano }
2420f3db4851SDaniel Lezcano 
2421f3db4851SDaniel Lezcano static int ipv6_route_release(struct inode *inode, struct file *file)
2422f3db4851SDaniel Lezcano {
2423f3db4851SDaniel Lezcano 	struct seq_file *seq = file->private_data;
2424f3db4851SDaniel Lezcano 	struct net *net = seq->private;
2425f3db4851SDaniel Lezcano 	put_net(net);
2426f3db4851SDaniel Lezcano 	return single_release(inode, file);
242733120b30SAlexey Dobriyan }
242833120b30SAlexey Dobriyan 
242933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
243033120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
243133120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
243233120b30SAlexey Dobriyan 	.read		= seq_read,
243333120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2434f3db4851SDaniel Lezcano 	.release	= ipv6_route_release,
243533120b30SAlexey Dobriyan };
243633120b30SAlexey Dobriyan 
24371da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
24381da177e4SLinus Torvalds {
243969ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
24401da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
244169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
244269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
244369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
244469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
244569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2446f2fc6a54SBenjamin Thery 		   atomic_read(&net->ipv6.ip6_dst_ops->entries),
244769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 	return 0;
24501da177e4SLinus Torvalds }
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
24531da177e4SLinus Torvalds {
2454a2333525SPavel Emelyanov 	int err;
245569ddb805SDaniel Lezcano 	struct net *net = get_proc_net(inode);
2456a2333525SPavel Emelyanov 	if (!net)
2457a2333525SPavel Emelyanov 		return -ENXIO;
2458a2333525SPavel Emelyanov 
2459a2333525SPavel Emelyanov 	err = single_open(file, rt6_stats_seq_show, net);
2460a2333525SPavel Emelyanov 	if (err < 0) {
2461a2333525SPavel Emelyanov 		put_net(net);
2462a2333525SPavel Emelyanov 		return err;
2463a2333525SPavel Emelyanov 	}
2464a2333525SPavel Emelyanov 
2465a2333525SPavel Emelyanov 	return 0;
246669ddb805SDaniel Lezcano }
246769ddb805SDaniel Lezcano 
246869ddb805SDaniel Lezcano static int rt6_stats_seq_release(struct inode *inode, struct file *file)
246969ddb805SDaniel Lezcano {
247069ddb805SDaniel Lezcano 	struct seq_file *seq = file->private_data;
247169ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
247269ddb805SDaniel Lezcano 	put_net(net);
247369ddb805SDaniel Lezcano 	return single_release(inode, file);
24741da177e4SLinus Torvalds }
24751da177e4SLinus Torvalds 
24769a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
24771da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
24781da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
24791da177e4SLinus Torvalds 	.read	 = seq_read,
24801da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
248169ddb805SDaniel Lezcano 	.release = rt6_stats_seq_release,
24821da177e4SLinus Torvalds };
24831da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds static
24881da177e4SLinus Torvalds int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
24891da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
24901da177e4SLinus Torvalds {
24915b7c931dSDaniel Lezcano 	struct net *net = current->nsproxy->net_ns;
24925b7c931dSDaniel Lezcano 	int delay = net->ipv6.sysctl.flush_delay;
24931da177e4SLinus Torvalds 	if (write) {
24941da177e4SLinus Torvalds 		proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
24955b7c931dSDaniel Lezcano 		fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
24961da177e4SLinus Torvalds 		return 0;
24971da177e4SLinus Torvalds 	} else
24981da177e4SLinus Torvalds 		return -EINVAL;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
2501760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
25021da177e4SLinus Torvalds 	{
25031da177e4SLinus Torvalds 		.procname	=	"flush",
25044990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
25051da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
250689c8b3a1SDave Jones 		.mode		=	0200,
25071da177e4SLinus Torvalds 		.proc_handler	=	&ipv6_sysctl_rtcache_flush
25081da177e4SLinus Torvalds 	},
25091da177e4SLinus Torvalds 	{
25101da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_THRESH,
25111da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
25129a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
25131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25141da177e4SLinus Torvalds 		.mode		=	0644,
25151da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec,
25161da177e4SLinus Torvalds 	},
25171da177e4SLinus Torvalds 	{
25181da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MAX_SIZE,
25191da177e4SLinus Torvalds 		.procname	=	"max_size",
25204990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
25211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25221da177e4SLinus Torvalds 		.mode		=	0644,
25231da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec,
25241da177e4SLinus Torvalds 	},
25251da177e4SLinus Torvalds 	{
25261da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL,
25271da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
25284990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
25291da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25301da177e4SLinus Torvalds 		.mode		=	0644,
25311da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25321da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25331da177e4SLinus Torvalds 	},
25341da177e4SLinus Torvalds 	{
25351da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_TIMEOUT,
25361da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
25374990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
25381da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25391da177e4SLinus Torvalds 		.mode		=	0644,
25401da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25411da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25421da177e4SLinus Torvalds 	},
25431da177e4SLinus Torvalds 	{
25441da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_INTERVAL,
25451da177e4SLinus Torvalds 		.procname	=	"gc_interval",
25464990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
25471da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25481da177e4SLinus Torvalds 		.mode		=	0644,
25491da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25501da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25511da177e4SLinus Torvalds 	},
25521da177e4SLinus Torvalds 	{
25531da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_ELASTICITY,
25541da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
25554990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
25561da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25571da177e4SLinus Torvalds 		.mode		=	0644,
25581da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25591da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25601da177e4SLinus Torvalds 	},
25611da177e4SLinus Torvalds 	{
25621da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MTU_EXPIRES,
25631da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
25644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
25651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25661da177e4SLinus Torvalds 		.mode		=	0644,
25671da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25681da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25691da177e4SLinus Torvalds 	},
25701da177e4SLinus Torvalds 	{
25711da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_MIN_ADVMSS,
25721da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
25734990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
25741da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25751da177e4SLinus Torvalds 		.mode		=	0644,
25761da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_jiffies,
25771da177e4SLinus Torvalds 		.strategy	=	&sysctl_jiffies,
25781da177e4SLinus Torvalds 	},
25791da177e4SLinus Torvalds 	{
25801da177e4SLinus Torvalds 		.ctl_name	=	NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
25811da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
25824990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
25831da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
25841da177e4SLinus Torvalds 		.mode		=	0644,
25851da177e4SLinus Torvalds 		.proc_handler	=	&proc_dointvec_ms_jiffies,
25861da177e4SLinus Torvalds 		.strategy	=	&sysctl_ms_jiffies,
25871da177e4SLinus Torvalds 	},
25881da177e4SLinus Torvalds 	{ .ctl_name = 0 }
25891da177e4SLinus Torvalds };
25901da177e4SLinus Torvalds 
2591760f2d01SDaniel Lezcano struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2592760f2d01SDaniel Lezcano {
2593760f2d01SDaniel Lezcano 	struct ctl_table *table;
2594760f2d01SDaniel Lezcano 
2595760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2596760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2597760f2d01SDaniel Lezcano 			GFP_KERNEL);
25985ee09105SYOSHIFUJI Hideaki 
25995ee09105SYOSHIFUJI Hideaki 	if (table) {
26005ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2601f2fc6a54SBenjamin Thery 		table[1].data = &net->ipv6.ip6_dst_ops->gc_thresh;
26025ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
26035ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
26045ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
26055ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
26065ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
26075ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
26085ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
26095ee09105SYOSHIFUJI Hideaki 	}
26105ee09105SYOSHIFUJI Hideaki 
2611760f2d01SDaniel Lezcano 	return table;
2612760f2d01SDaniel Lezcano }
26131da177e4SLinus Torvalds #endif
26141da177e4SLinus Torvalds 
2615cdb18761SDaniel Lezcano static int ip6_route_net_init(struct net *net)
2616cdb18761SDaniel Lezcano {
2617*633d424bSPavel Emelyanov 	int ret = -ENOMEM;
26188ed67789SDaniel Lezcano 
2619f2fc6a54SBenjamin Thery 	net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
2620f2fc6a54SBenjamin Thery 					sizeof(*net->ipv6.ip6_dst_ops),
2621f2fc6a54SBenjamin Thery 					GFP_KERNEL);
2622f2fc6a54SBenjamin Thery 	if (!net->ipv6.ip6_dst_ops)
2623f2fc6a54SBenjamin Thery 		goto out;
262448115becSDenis V. Lunev 	net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
2625f2fc6a54SBenjamin Thery 
26268ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
26278ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
26288ed67789SDaniel Lezcano 					   GFP_KERNEL);
26298ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2630f2fc6a54SBenjamin Thery 		goto out_ip6_dst_ops;
26318ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry->u.dst.path =
26328ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2633f2fc6a54SBenjamin Thery 	net->ipv6.ip6_null_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
26348ed67789SDaniel Lezcano 
26358ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
26368ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
26378ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
26388ed67789SDaniel Lezcano 					       GFP_KERNEL);
26398ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_prohibit_entry) {
26408ed67789SDaniel Lezcano 		kfree(net->ipv6.ip6_null_entry);
26418ed67789SDaniel Lezcano 		goto out;
26428ed67789SDaniel Lezcano 	}
26438ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry->u.dst.path =
26448ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2645f2fc6a54SBenjamin Thery 	net->ipv6.ip6_prohibit_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
26468ed67789SDaniel Lezcano 
26478ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
26488ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
26498ed67789SDaniel Lezcano 					       GFP_KERNEL);
26508ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_blk_hole_entry) {
26518ed67789SDaniel Lezcano 		kfree(net->ipv6.ip6_null_entry);
26528ed67789SDaniel Lezcano 		kfree(net->ipv6.ip6_prohibit_entry);
26538ed67789SDaniel Lezcano 		goto out;
26548ed67789SDaniel Lezcano 	}
26558ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry->u.dst.path =
26568ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2657f2fc6a54SBenjamin Thery 	net->ipv6.ip6_blk_hole_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
26588ed67789SDaniel Lezcano #endif
26598ed67789SDaniel Lezcano 
2660cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2661cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2662cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2663cdb18761SDaniel Lezcano #endif
26646891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
26656891a346SBenjamin Thery 
26668ed67789SDaniel Lezcano 	ret = 0;
26678ed67789SDaniel Lezcano out:
26688ed67789SDaniel Lezcano 	return ret;
2669f2fc6a54SBenjamin Thery 
2670f2fc6a54SBenjamin Thery out_ip6_dst_ops:
267148115becSDenis V. Lunev 	release_net(net->ipv6.ip6_dst_ops->dst_net);
2672f2fc6a54SBenjamin Thery 	kfree(net->ipv6.ip6_dst_ops);
2673f2fc6a54SBenjamin Thery 	goto out;
2674cdb18761SDaniel Lezcano }
2675cdb18761SDaniel Lezcano 
2676cdb18761SDaniel Lezcano static void ip6_route_net_exit(struct net *net)
2677cdb18761SDaniel Lezcano {
2678cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2679cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2680cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2681cdb18761SDaniel Lezcano #endif
26828ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
26838ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
26848ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
26858ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
26868ed67789SDaniel Lezcano #endif
268748115becSDenis V. Lunev 	release_net(net->ipv6.ip6_dst_ops->dst_net);
2688f2fc6a54SBenjamin Thery 	kfree(net->ipv6.ip6_dst_ops);
2689cdb18761SDaniel Lezcano }
2690cdb18761SDaniel Lezcano 
2691cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2692cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2693cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2694cdb18761SDaniel Lezcano };
2695cdb18761SDaniel Lezcano 
26968ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
26978ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
26988ed67789SDaniel Lezcano 	.priority = 0,
26998ed67789SDaniel Lezcano };
27008ed67789SDaniel Lezcano 
2701433d49c3SDaniel Lezcano int __init ip6_route_init(void)
27021da177e4SLinus Torvalds {
2703433d49c3SDaniel Lezcano 	int ret;
2704433d49c3SDaniel Lezcano 
27059a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
27069a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
27079a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
27089a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
27099a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2710f2fc6a54SBenjamin Thery 		goto out;;
271114e50e57SDavid S. Miller 
27128ed67789SDaniel Lezcano 	ret = register_pernet_subsys(&ip6_route_net_ops);
27138ed67789SDaniel Lezcano 	if (ret)
2714bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2715bdb3289fSDaniel Lezcano 
27168ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
27178ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
27188ed67789SDaniel Lezcano 	 * manually for init_net */
27198ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->u.dst.dev = init_net.loopback_dev;
27208ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2721bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27228ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->u.dst.dev = init_net.loopback_dev;
27238ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
27248ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->u.dst.dev = init_net.loopback_dev;
27258ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2726bdb3289fSDaniel Lezcano   #endif
2727433d49c3SDaniel Lezcano 	ret = fib6_init();
2728433d49c3SDaniel Lezcano 	if (ret)
27298ed67789SDaniel Lezcano 		goto out_register_subsys;
2730433d49c3SDaniel Lezcano 
2731433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2732433d49c3SDaniel Lezcano 	if (ret)
2733cdb18761SDaniel Lezcano 		goto out_fib6_init;
2734c35b7e72SDaniel Lezcano 
2735433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2736433d49c3SDaniel Lezcano 	if (ret)
2737433d49c3SDaniel Lezcano 		goto xfrm6_init;
27387e5449c2SDaniel Lezcano 
2739433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2740433d49c3SDaniel Lezcano 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2741433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2742433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2743433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2744433d49c3SDaniel Lezcano 
27458ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2746cdb18761SDaniel Lezcano 	if (ret)
2747cdb18761SDaniel Lezcano 		goto fib6_rules_init;
27488ed67789SDaniel Lezcano 
2749433d49c3SDaniel Lezcano out:
2750433d49c3SDaniel Lezcano 	return ret;
2751433d49c3SDaniel Lezcano 
2752433d49c3SDaniel Lezcano fib6_rules_init:
2753433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2754433d49c3SDaniel Lezcano xfrm6_init:
2755433d49c3SDaniel Lezcano 	xfrm6_fini();
2756433d49c3SDaniel Lezcano out_fib6_init:
2757433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
27588ed67789SDaniel Lezcano out_register_subsys:
27598ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2760433d49c3SDaniel Lezcano out_kmem_cache:
2761f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2762433d49c3SDaniel Lezcano 	goto out;
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds void ip6_route_cleanup(void)
27661da177e4SLinus Torvalds {
27678ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2768101367c2SThomas Graf 	fib6_rules_cleanup();
27691da177e4SLinus Torvalds 	xfrm6_fini();
27701da177e4SLinus Torvalds 	fib6_gc_cleanup();
27718ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2772f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
27731da177e4SLinus Torvalds }
2774