xref: /linux/net/ipv4/route.c (revision cf8da764fc6959b7efb482f375dfef9830e98205)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		ROUTE - implementation of the IP router.
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
111da177e4SLinus Torvalds  *		Linus Torvalds, <Linus.Torvalds@helsinki.fi>
121da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Fixes:
151da177e4SLinus Torvalds  *		Alan Cox	:	Verify area fixes.
161da177e4SLinus Torvalds  *		Alan Cox	:	cli() protects routing changes
171da177e4SLinus Torvalds  *		Rui Oliveira	:	ICMP routing table updates
181da177e4SLinus Torvalds  *		(rco@di.uminho.pt)	Routing table insertion and update
191da177e4SLinus Torvalds  *		Linus Torvalds	:	Rewrote bits to be sensible
201da177e4SLinus Torvalds  *		Alan Cox	:	Added BSD route gw semantics
211da177e4SLinus Torvalds  *		Alan Cox	:	Super /proc >4K
221da177e4SLinus Torvalds  *		Alan Cox	:	MTU in route table
231da177e4SLinus Torvalds  *		Alan Cox	: 	MSS actually. Also added the window
241da177e4SLinus Torvalds  *					clamper.
251da177e4SLinus Torvalds  *		Sam Lantinga	:	Fixed route matching in rt_del()
261da177e4SLinus Torvalds  *		Alan Cox	:	Routing cache support.
271da177e4SLinus Torvalds  *		Alan Cox	:	Removed compatibility cruft.
281da177e4SLinus Torvalds  *		Alan Cox	:	RTF_REJECT support.
291da177e4SLinus Torvalds  *		Alan Cox	:	TCP irtt support.
301da177e4SLinus Torvalds  *		Jonathan Naylor	:	Added Metric support.
311da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	BSD API fixes.
321da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	Metrics.
331da177e4SLinus Torvalds  *		Alan Cox	:	Use __u32 properly
341da177e4SLinus Torvalds  *		Alan Cox	:	Aligned routing errors more closely with BSD
351da177e4SLinus Torvalds  *					our system is still very different.
361da177e4SLinus Torvalds  *		Alan Cox	:	Faster /proc handling
371da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Massive rework to support tree based routing,
381da177e4SLinus Torvalds  *					routing caches and better behaviour.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  *		Olaf Erb	:	irtt wasn't being copied right.
411da177e4SLinus Torvalds  *		Bjorn Ekwall	:	Kerneld route support.
421da177e4SLinus Torvalds  *		Alan Cox	:	Multicast fixed (I hope)
431da177e4SLinus Torvalds  * 		Pavel Krauz	:	Limited broadcast fixed
441da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
451da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	End of old history. Split to fib.c and
461da177e4SLinus Torvalds  *					route.c and rewritten from scratch.
471da177e4SLinus Torvalds  *		Andi Kleen	:	Load-limit warning messages.
481da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
491da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Race condition in ip_route_input_slow.
501da177e4SLinus Torvalds  *	Tobias Ringstrom	:	Uninitialized res.type in ip_route_output_slow.
511da177e4SLinus Torvalds  *	Vladimir V. Ivanov	:	IP rule info (flowid) is really useful.
521da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
531da177e4SLinus Torvalds  *	Robert Olsson		:	Added rt_cache statistics
541da177e4SLinus Torvalds  *	Arnaldo C. Melo		:	Convert proc stuff to seq_file
55bb1d23b0SEric Dumazet  *	Eric Dumazet		:	hashed spinlocks and rt_check_expire() fixes.
56cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Ignore TOS on PMTUD and Redirect
57cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Removed TOS from hash calculations
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
601da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
611da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
621da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <linux/module.h>
661da177e4SLinus Torvalds #include <asm/uaccess.h>
671da177e4SLinus Torvalds #include <asm/system.h>
681da177e4SLinus Torvalds #include <linux/bitops.h>
691da177e4SLinus Torvalds #include <linux/types.h>
701da177e4SLinus Torvalds #include <linux/kernel.h>
711da177e4SLinus Torvalds #include <linux/mm.h>
72424c4b70SEric Dumazet #include <linux/bootmem.h>
731da177e4SLinus Torvalds #include <linux/string.h>
741da177e4SLinus Torvalds #include <linux/socket.h>
751da177e4SLinus Torvalds #include <linux/sockios.h>
761da177e4SLinus Torvalds #include <linux/errno.h>
771da177e4SLinus Torvalds #include <linux/in.h>
781da177e4SLinus Torvalds #include <linux/inet.h>
791da177e4SLinus Torvalds #include <linux/netdevice.h>
801da177e4SLinus Torvalds #include <linux/proc_fs.h>
811da177e4SLinus Torvalds #include <linux/init.h>
8239c90eceSEric Dumazet #include <linux/workqueue.h>
831da177e4SLinus Torvalds #include <linux/skbuff.h>
841da177e4SLinus Torvalds #include <linux/inetdevice.h>
851da177e4SLinus Torvalds #include <linux/igmp.h>
861da177e4SLinus Torvalds #include <linux/pkt_sched.h>
871da177e4SLinus Torvalds #include <linux/mroute.h>
881da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
891da177e4SLinus Torvalds #include <linux/random.h>
901da177e4SLinus Torvalds #include <linux/jhash.h>
911da177e4SLinus Torvalds #include <linux/rcupdate.h>
921da177e4SLinus Torvalds #include <linux/times.h>
93352e512cSHerbert Xu #include <net/dst.h>
94457c4cbcSEric W. Biederman #include <net/net_namespace.h>
951da177e4SLinus Torvalds #include <net/protocol.h>
961da177e4SLinus Torvalds #include <net/ip.h>
971da177e4SLinus Torvalds #include <net/route.h>
981da177e4SLinus Torvalds #include <net/inetpeer.h>
991da177e4SLinus Torvalds #include <net/sock.h>
1001da177e4SLinus Torvalds #include <net/ip_fib.h>
1011da177e4SLinus Torvalds #include <net/arp.h>
1021da177e4SLinus Torvalds #include <net/tcp.h>
1031da177e4SLinus Torvalds #include <net/icmp.h>
1041da177e4SLinus Torvalds #include <net/xfrm.h>
1058d71740cSTom Tucker #include <net/netevent.h>
10663f3444fSThomas Graf #include <net/rtnetlink.h>
1071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1081da177e4SLinus Torvalds #include <linux/sysctl.h>
1091da177e4SLinus Torvalds #endif
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define RT_FL_TOS(oldflp) \
1121da177e4SLinus Torvalds     ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #define IP_MAX_MTU	0xFFF0
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static int ip_rt_max_size;
119817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly	= RT_GC_TIMEOUT;
120817bc4dbSStephen Hemminger static int ip_rt_gc_interval __read_mostly	= 60 * HZ;
121817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly	= HZ / 2;
122817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly	= 9;
123817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly	= HZ / 50;
124817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly	= ((HZ / 50) << (9 + 1));
125817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly	= HZ;
126817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly	= 5 * HZ;
127817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly	= 8;
128817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly	= 10 * 60 * HZ;
129817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly		= 512 + 20 + 20;
130817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly	= 256;
131817bc4dbSStephen Hemminger static int ip_rt_secret_interval __read_mostly	= 10 * 60 * HZ;
1321080d709SNeil Horman static int rt_chain_length_max __read_mostly	= 20;
1331da177e4SLinus Torvalds 
134beb659bdSEric Dumazet static void rt_worker_func(struct work_struct *work);
135beb659bdSEric Dumazet static DECLARE_DELAYED_WORK(expires_work, rt_worker_func);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  *	Interface to generic destination cache.
1391da177e4SLinus Torvalds  */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
1421da177e4SLinus Torvalds static void		 ipv4_dst_destroy(struct dst_entry *dst);
1431da177e4SLinus Torvalds static void		 ipv4_dst_ifdown(struct dst_entry *dst,
1441da177e4SLinus Torvalds 					 struct net_device *dev, int how);
1451da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
1461da177e4SLinus Torvalds static void		 ipv4_link_failure(struct sk_buff *skb);
1471da177e4SLinus Torvalds static void		 ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
148569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops);
1491080d709SNeil Horman static void rt_emergency_hash_rebuild(struct net *net);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1531da177e4SLinus Torvalds 	.family =		AF_INET,
15409640e63SHarvey Harrison 	.protocol =		cpu_to_be16(ETH_P_IP),
1551da177e4SLinus Torvalds 	.gc =			rt_garbage_collect,
1561da177e4SLinus Torvalds 	.check =		ipv4_dst_check,
1571da177e4SLinus Torvalds 	.destroy =		ipv4_dst_destroy,
1581da177e4SLinus Torvalds 	.ifdown =		ipv4_dst_ifdown,
1591da177e4SLinus Torvalds 	.negative_advice =	ipv4_negative_advice,
1601da177e4SLinus Torvalds 	.link_failure =		ipv4_link_failure,
1611da177e4SLinus Torvalds 	.update_pmtu =		ip_rt_update_pmtu,
1621ac06e03SHerbert Xu 	.local_out =		__ip_local_out,
163e2422970SEric Dumazet 	.entries =		ATOMIC_INIT(0),
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1671da177e4SLinus Torvalds 
1684839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1691da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1701da177e4SLinus Torvalds 	ECN_OR_COST(FILLER),
1711da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1721da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1731da177e4SLinus Torvalds 	TC_PRIO_BULK,
1741da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1751da177e4SLinus Torvalds 	TC_PRIO_BULK,
1761da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1771da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1781da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1791da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1801da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1811da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1821da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1831da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1841da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
1891da177e4SLinus Torvalds  * Route cache.
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /* The locking scheme is rather straight forward:
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * 1) Read-Copy Update protects the buckets of the central route hash.
1951da177e4SLinus Torvalds  * 2) Only writers remove entries, and they hold the lock
1961da177e4SLinus Torvalds  *    as they look at rtable reference counts.
1971da177e4SLinus Torvalds  * 3) Only readers acquire references to rtable entries,
1981da177e4SLinus Torvalds  *    they do so with atomic increments and with the
1991da177e4SLinus Torvalds  *    lock held.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds struct rt_hash_bucket {
2031da177e4SLinus Torvalds 	struct rtable	*chain;
20422c047ccSEric Dumazet };
2051080d709SNeil Horman 
2068a25d5deSIngo Molnar #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
2078a25d5deSIngo Molnar 	defined(CONFIG_PROVE_LOCKING)
20822c047ccSEric Dumazet /*
20922c047ccSEric Dumazet  * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
21022c047ccSEric Dumazet  * The size of this table is a power of two and depends on the number of CPUS.
21162051200SIngo Molnar  * (on lockdep we have a quite big spinlock_t, so keep the size down there)
21222c047ccSEric Dumazet  */
21362051200SIngo Molnar #ifdef CONFIG_LOCKDEP
21462051200SIngo Molnar # define RT_HASH_LOCK_SZ	256
21562051200SIngo Molnar #else
21622c047ccSEric Dumazet # if NR_CPUS >= 32
21722c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	4096
21822c047ccSEric Dumazet # elif NR_CPUS >= 16
21922c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	2048
22022c047ccSEric Dumazet # elif NR_CPUS >= 8
22122c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	1024
22222c047ccSEric Dumazet # elif NR_CPUS >= 4
22322c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	512
22422c047ccSEric Dumazet # else
22522c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	256
22622c047ccSEric Dumazet # endif
22762051200SIngo Molnar #endif
22822c047ccSEric Dumazet 
22922c047ccSEric Dumazet static spinlock_t	*rt_hash_locks;
23022c047ccSEric Dumazet # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
2311ff1cc20SPavel Emelyanov 
2321ff1cc20SPavel Emelyanov static __init void rt_hash_lock_init(void)
2331ff1cc20SPavel Emelyanov {
2341ff1cc20SPavel Emelyanov 	int i;
2351ff1cc20SPavel Emelyanov 
2361ff1cc20SPavel Emelyanov 	rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ,
2371ff1cc20SPavel Emelyanov 			GFP_KERNEL);
2381ff1cc20SPavel Emelyanov 	if (!rt_hash_locks)
2391ff1cc20SPavel Emelyanov 		panic("IP: failed to allocate rt_hash_locks\n");
2401ff1cc20SPavel Emelyanov 
2411ff1cc20SPavel Emelyanov 	for (i = 0; i < RT_HASH_LOCK_SZ; i++)
2421ff1cc20SPavel Emelyanov 		spin_lock_init(&rt_hash_locks[i]);
24322c047ccSEric Dumazet }
24422c047ccSEric Dumazet #else
24522c047ccSEric Dumazet # define rt_hash_lock_addr(slot) NULL
2461ff1cc20SPavel Emelyanov 
2471ff1cc20SPavel Emelyanov static inline void rt_hash_lock_init(void)
2481ff1cc20SPavel Emelyanov {
2491ff1cc20SPavel Emelyanov }
25022c047ccSEric Dumazet #endif
2511da177e4SLinus Torvalds 
252817bc4dbSStephen Hemminger static struct rt_hash_bucket 	*rt_hash_table __read_mostly;
253817bc4dbSStephen Hemminger static unsigned			rt_hash_mask __read_mostly;
254817bc4dbSStephen Hemminger static unsigned int		rt_hash_log  __read_mostly;
2551da177e4SLinus Torvalds 
2562f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
257dbd2915cSAndrew Morton #define RT_CACHE_STAT_INC(field) \
258bfe5d834SPaul Mackerras 	(__raw_get_cpu_var(rt_cache_stat).field++)
2591da177e4SLinus Torvalds 
260b00180deSDenis V. Lunev static inline unsigned int rt_hash(__be32 daddr, __be32 saddr, int idx,
261b00180deSDenis V. Lunev 		int genid)
2621da177e4SLinus Torvalds {
2631294fc4aSStephen Hemminger 	return jhash_3words((__force u32)(__be32)(daddr),
2641294fc4aSStephen Hemminger 			    (__force u32)(__be32)(saddr),
265b00180deSDenis V. Lunev 			    idx, genid)
26629e75252SEric Dumazet 		& rt_hash_mask;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
269e84f84f2SDenis V. Lunev static inline int rt_genid(struct net *net)
270e84f84f2SDenis V. Lunev {
271e84f84f2SDenis V. Lunev 	return atomic_read(&net->ipv4.rt_genid);
272e84f84f2SDenis V. Lunev }
273e84f84f2SDenis V. Lunev 
2741da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
2751da177e4SLinus Torvalds struct rt_cache_iter_state {
276a75e936fSDenis V. Lunev 	struct seq_net_private p;
2771da177e4SLinus Torvalds 	int bucket;
27829e75252SEric Dumazet 	int genid;
2791da177e4SLinus Torvalds };
2801da177e4SLinus Torvalds 
2811218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_first(struct seq_file *seq)
2821da177e4SLinus Torvalds {
2831218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
2841da177e4SLinus Torvalds 	struct rtable *r = NULL;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
287a6272665SEric Dumazet 		if (!rt_hash_table[st->bucket].chain)
288a6272665SEric Dumazet 			continue;
2891da177e4SLinus Torvalds 		rcu_read_lock_bh();
29029e75252SEric Dumazet 		r = rcu_dereference(rt_hash_table[st->bucket].chain);
29129e75252SEric Dumazet 		while (r) {
2921218854aSYOSHIFUJI Hideaki 			if (dev_net(r->u.dst.dev) == seq_file_net(seq) &&
293a75e936fSDenis V. Lunev 			    r->rt_genid == st->genid)
29429e75252SEric Dumazet 				return r;
29529e75252SEric Dumazet 			r = rcu_dereference(r->u.dst.rt_next);
29629e75252SEric Dumazet 		}
2971da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2981da177e4SLinus Torvalds 	}
29929e75252SEric Dumazet 	return r;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021218854aSYOSHIFUJI Hideaki static struct rtable *__rt_cache_get_next(struct seq_file *seq,
303642d6318SDenis V. Lunev 					  struct rtable *r)
3041da177e4SLinus Torvalds {
3051218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
306a6272665SEric Dumazet 
307093c2ca4SEric Dumazet 	r = r->u.dst.rt_next;
3081da177e4SLinus Torvalds 	while (!r) {
3091da177e4SLinus Torvalds 		rcu_read_unlock_bh();
310a6272665SEric Dumazet 		do {
3111da177e4SLinus Torvalds 			if (--st->bucket < 0)
312a6272665SEric Dumazet 				return NULL;
313a6272665SEric Dumazet 		} while (!rt_hash_table[st->bucket].chain);
3141da177e4SLinus Torvalds 		rcu_read_lock_bh();
3151da177e4SLinus Torvalds 		r = rt_hash_table[st->bucket].chain;
3161da177e4SLinus Torvalds 	}
3170bcceadcSEric Dumazet 	return rcu_dereference(r);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_next(struct seq_file *seq,
321642d6318SDenis V. Lunev 					struct rtable *r)
322642d6318SDenis V. Lunev {
3231218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
3241218854aSYOSHIFUJI Hideaki 	while ((r = __rt_cache_get_next(seq, r)) != NULL) {
3251218854aSYOSHIFUJI Hideaki 		if (dev_net(r->u.dst.dev) != seq_file_net(seq))
326a75e936fSDenis V. Lunev 			continue;
327642d6318SDenis V. Lunev 		if (r->rt_genid == st->genid)
328642d6318SDenis V. Lunev 			break;
329642d6318SDenis V. Lunev 	}
330642d6318SDenis V. Lunev 	return r;
331642d6318SDenis V. Lunev }
332642d6318SDenis V. Lunev 
3331218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
3341da177e4SLinus Torvalds {
3351218854aSYOSHIFUJI Hideaki 	struct rtable *r = rt_cache_get_first(seq);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	if (r)
3381218854aSYOSHIFUJI Hideaki 		while (pos && (r = rt_cache_get_next(seq, r)))
3391da177e4SLinus Torvalds 			--pos;
3401da177e4SLinus Torvalds 	return pos ? NULL : r;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
3441da177e4SLinus Torvalds {
34529e75252SEric Dumazet 	struct rt_cache_iter_state *st = seq->private;
34629e75252SEric Dumazet 	if (*pos)
3471218854aSYOSHIFUJI Hideaki 		return rt_cache_get_idx(seq, *pos - 1);
348e84f84f2SDenis V. Lunev 	st->genid = rt_genid(seq_file_net(seq));
34929e75252SEric Dumazet 	return SEQ_START_TOKEN;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3531da177e4SLinus Torvalds {
35429e75252SEric Dumazet 	struct rtable *r;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
3571218854aSYOSHIFUJI Hideaki 		r = rt_cache_get_first(seq);
3581da177e4SLinus Torvalds 	else
3591218854aSYOSHIFUJI Hideaki 		r = rt_cache_get_next(seq, v);
3601da177e4SLinus Torvalds 	++*pos;
3611da177e4SLinus Torvalds 	return r;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	if (v && v != SEQ_START_TOKEN)
3671da177e4SLinus Torvalds 		rcu_read_unlock_bh();
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
3731da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
3741da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
3751da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
3761da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
3771da177e4SLinus Torvalds 	else {
3781da177e4SLinus Torvalds 		struct rtable *r = v;
3795e659e4cSPavel Emelyanov 		int len;
3801da177e4SLinus Torvalds 
3815e659e4cSPavel Emelyanov 		seq_printf(seq, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
3825e659e4cSPavel Emelyanov 			      "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X%n",
3831da177e4SLinus Torvalds 			r->u.dst.dev ? r->u.dst.dev->name : "*",
3841da177e4SLinus Torvalds 			(unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
3851da177e4SLinus Torvalds 			r->rt_flags, atomic_read(&r->u.dst.__refcnt),
3861da177e4SLinus Torvalds 			r->u.dst.__use, 0, (unsigned long)r->rt_src,
3871da177e4SLinus Torvalds 			(dst_metric(&r->u.dst, RTAX_ADVMSS) ?
3881da177e4SLinus Torvalds 			     (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
3891da177e4SLinus Torvalds 			dst_metric(&r->u.dst, RTAX_WINDOW),
3901da177e4SLinus Torvalds 			(int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
3911da177e4SLinus Torvalds 			      dst_metric(&r->u.dst, RTAX_RTTVAR)),
3921da177e4SLinus Torvalds 			r->fl.fl4_tos,
3931da177e4SLinus Torvalds 			r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
3941da177e4SLinus Torvalds 			r->u.dst.hh ? (r->u.dst.hh->hh_output ==
3951da177e4SLinus Torvalds 				       dev_queue_xmit) : 0,
3965e659e4cSPavel Emelyanov 			r->rt_spec_dst, &len);
3975e659e4cSPavel Emelyanov 
3985e659e4cSPavel Emelyanov 		seq_printf(seq, "%*s\n", 127 - len, "");
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 	return 0;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
403f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
4041da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
4051da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
4061da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
4071da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
4081da177e4SLinus Torvalds };
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file)
4111da177e4SLinus Torvalds {
412a75e936fSDenis V. Lunev 	return seq_open_net(inode, file, &rt_cache_seq_ops,
413cf7732e4SPavel Emelyanov 			sizeof(struct rt_cache_iter_state));
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4169a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = {
4171da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
4181da177e4SLinus Torvalds 	.open	 = rt_cache_seq_open,
4191da177e4SLinus Torvalds 	.read	 = seq_read,
4201da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
421a75e936fSDenis V. Lunev 	.release = seq_release_net,
4221da177e4SLinus Torvalds };
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	int cpu;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (*pos == 0)
4301da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
4311da177e4SLinus Torvalds 
4320f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
4331da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
4341da177e4SLinus Torvalds 			continue;
4351da177e4SLinus Torvalds 		*pos = cpu+1;
4362f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	return NULL;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	int cpu;
4441da177e4SLinus Torvalds 
4450f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
4461da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
4471da177e4SLinus Torvalds 			continue;
4481da177e4SLinus Torvalds 		*pos = cpu+1;
4492f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 	return NULL;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
4655bec0039SOlaf Rempel 		seq_printf(seq, "entries  in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src  out_hit out_slow_tot out_slow_mc  gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n");
4661da177e4SLinus Torvalds 		return 0;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	seq_printf(seq,"%08x  %08x %08x %08x %08x %08x %08x %08x "
4701da177e4SLinus Torvalds 		   " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
4711da177e4SLinus Torvalds 		   atomic_read(&ipv4_dst_ops.entries),
4721da177e4SLinus Torvalds 		   st->in_hit,
4731da177e4SLinus Torvalds 		   st->in_slow_tot,
4741da177e4SLinus Torvalds 		   st->in_slow_mc,
4751da177e4SLinus Torvalds 		   st->in_no_route,
4761da177e4SLinus Torvalds 		   st->in_brd,
4771da177e4SLinus Torvalds 		   st->in_martian_dst,
4781da177e4SLinus Torvalds 		   st->in_martian_src,
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 		   st->out_hit,
4811da177e4SLinus Torvalds 		   st->out_slow_tot,
4821da177e4SLinus Torvalds 		   st->out_slow_mc,
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 		   st->gc_total,
4851da177e4SLinus Torvalds 		   st->gc_ignored,
4861da177e4SLinus Torvalds 		   st->gc_goal_miss,
4871da177e4SLinus Torvalds 		   st->gc_dst_overflow,
4881da177e4SLinus Torvalds 		   st->in_hlist_search,
4891da177e4SLinus Torvalds 		   st->out_hlist_search
4901da177e4SLinus Torvalds 		);
4911da177e4SLinus Torvalds 	return 0;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
4951da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
4961da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
4971da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
4981da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
4991da177e4SLinus Torvalds };
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	return seq_open(file, &rt_cpu_seq_ops);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5079a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = {
5081da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
5091da177e4SLinus Torvalds 	.open	 = rt_cpu_seq_open,
5101da177e4SLinus Torvalds 	.read	 = seq_read,
5111da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
5121da177e4SLinus Torvalds 	.release = seq_release,
5131da177e4SLinus Torvalds };
5141da177e4SLinus Torvalds 
51578c686e9SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
51678c686e9SPavel Emelyanov static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
51778c686e9SPavel Emelyanov 			   int length, int *eof, void *data)
51878c686e9SPavel Emelyanov {
51978c686e9SPavel Emelyanov 	unsigned int i;
52078c686e9SPavel Emelyanov 
52178c686e9SPavel Emelyanov 	if ((offset & 3) || (length & 3))
52278c686e9SPavel Emelyanov 		return -EIO;
52378c686e9SPavel Emelyanov 
52478c686e9SPavel Emelyanov 	if (offset >= sizeof(struct ip_rt_acct) * 256) {
52578c686e9SPavel Emelyanov 		*eof = 1;
52678c686e9SPavel Emelyanov 		return 0;
52778c686e9SPavel Emelyanov 	}
52878c686e9SPavel Emelyanov 
52978c686e9SPavel Emelyanov 	if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
53078c686e9SPavel Emelyanov 		length = sizeof(struct ip_rt_acct) * 256 - offset;
53178c686e9SPavel Emelyanov 		*eof = 1;
53278c686e9SPavel Emelyanov 	}
53378c686e9SPavel Emelyanov 
53478c686e9SPavel Emelyanov 	offset /= sizeof(u32);
53578c686e9SPavel Emelyanov 
53678c686e9SPavel Emelyanov 	if (length > 0) {
53778c686e9SPavel Emelyanov 		u32 *dst = (u32 *) buffer;
53878c686e9SPavel Emelyanov 
53978c686e9SPavel Emelyanov 		*start = buffer;
54078c686e9SPavel Emelyanov 		memset(dst, 0, length);
54178c686e9SPavel Emelyanov 
54278c686e9SPavel Emelyanov 		for_each_possible_cpu(i) {
54378c686e9SPavel Emelyanov 			unsigned int j;
54478c686e9SPavel Emelyanov 			u32 *src;
54578c686e9SPavel Emelyanov 
54678c686e9SPavel Emelyanov 			src = ((u32 *) per_cpu_ptr(ip_rt_acct, i)) + offset;
54778c686e9SPavel Emelyanov 			for (j = 0; j < length/4; j++)
54878c686e9SPavel Emelyanov 				dst[j] += src[j];
54978c686e9SPavel Emelyanov 		}
55078c686e9SPavel Emelyanov 	}
55178c686e9SPavel Emelyanov 	return length;
55278c686e9SPavel Emelyanov }
55378c686e9SPavel Emelyanov #endif
554107f1634SPavel Emelyanov 
55573b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
556107f1634SPavel Emelyanov {
557107f1634SPavel Emelyanov 	struct proc_dir_entry *pde;
558107f1634SPavel Emelyanov 
559107f1634SPavel Emelyanov 	pde = proc_net_fops_create(net, "rt_cache", S_IRUGO,
560107f1634SPavel Emelyanov 			&rt_cache_seq_fops);
561107f1634SPavel Emelyanov 	if (!pde)
562107f1634SPavel Emelyanov 		goto err1;
563107f1634SPavel Emelyanov 
56477020720SWang Chen 	pde = proc_create("rt_cache", S_IRUGO,
56577020720SWang Chen 			  net->proc_net_stat, &rt_cpu_seq_fops);
566107f1634SPavel Emelyanov 	if (!pde)
567107f1634SPavel Emelyanov 		goto err2;
568107f1634SPavel Emelyanov 
569107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
570107f1634SPavel Emelyanov 	pde = create_proc_read_entry("rt_acct", 0, net->proc_net,
571107f1634SPavel Emelyanov 			ip_rt_acct_read, NULL);
572107f1634SPavel Emelyanov 	if (!pde)
573107f1634SPavel Emelyanov 		goto err3;
574107f1634SPavel Emelyanov #endif
575107f1634SPavel Emelyanov 	return 0;
576107f1634SPavel Emelyanov 
577107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
578107f1634SPavel Emelyanov err3:
579107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net_stat);
580107f1634SPavel Emelyanov #endif
581107f1634SPavel Emelyanov err2:
582107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net);
583107f1634SPavel Emelyanov err1:
584107f1634SPavel Emelyanov 	return -ENOMEM;
585107f1634SPavel Emelyanov }
58673b38711SDenis V. Lunev 
58773b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
58873b38711SDenis V. Lunev {
58973b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net_stat);
59073b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net);
59173b38711SDenis V. Lunev 	remove_proc_entry("rt_acct", net->proc_net);
59273b38711SDenis V. Lunev }
59373b38711SDenis V. Lunev 
59473b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
59573b38711SDenis V. Lunev 	.init = ip_rt_do_proc_init,
59673b38711SDenis V. Lunev 	.exit = ip_rt_do_proc_exit,
59773b38711SDenis V. Lunev };
59873b38711SDenis V. Lunev 
59973b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
60073b38711SDenis V. Lunev {
60173b38711SDenis V. Lunev 	return register_pernet_subsys(&ip_rt_proc_ops);
60273b38711SDenis V. Lunev }
60373b38711SDenis V. Lunev 
604107f1634SPavel Emelyanov #else
60573b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
606107f1634SPavel Emelyanov {
607107f1634SPavel Emelyanov 	return 0;
608107f1634SPavel Emelyanov }
6091da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
6101da177e4SLinus Torvalds 
6115969f71dSStephen Hemminger static inline void rt_free(struct rtable *rt)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6165969f71dSStephen Hemminger static inline void rt_drop(struct rtable *rt)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	ip_rt_put(rt);
6191da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6225969f71dSStephen Hemminger static inline int rt_fast_clean(struct rtable *rth)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	/* Kill broadcast/multicast entries very aggresively, if they
6251da177e4SLinus Torvalds 	   collide in hash table with more useful entries */
6261da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
627093c2ca4SEric Dumazet 		rth->fl.iif && rth->u.dst.rt_next;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6305969f71dSStephen Hemminger static inline int rt_valuable(struct rtable *rth)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
6331da177e4SLinus Torvalds 		rth->u.dst.expires;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	unsigned long age;
6391da177e4SLinus Torvalds 	int ret = 0;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (atomic_read(&rth->u.dst.__refcnt))
6421da177e4SLinus Torvalds 		goto out;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	ret = 1;
6451da177e4SLinus Torvalds 	if (rth->u.dst.expires &&
6461da177e4SLinus Torvalds 	    time_after_eq(jiffies, rth->u.dst.expires))
6471da177e4SLinus Torvalds 		goto out;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	age = jiffies - rth->u.dst.lastuse;
6501da177e4SLinus Torvalds 	ret = 0;
6511da177e4SLinus Torvalds 	if ((age <= tmo1 && !rt_fast_clean(rth)) ||
6521da177e4SLinus Torvalds 	    (age <= tmo2 && rt_valuable(rth)))
6531da177e4SLinus Torvalds 		goto out;
6541da177e4SLinus Torvalds 	ret = 1;
6551da177e4SLinus Torvalds out:	return ret;
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds /* Bits of score are:
6591da177e4SLinus Torvalds  * 31: very valuable
6601da177e4SLinus Torvalds  * 30: not quite useless
6611da177e4SLinus Torvalds  * 29..0: usage counter
6621da177e4SLinus Torvalds  */
6631da177e4SLinus Torvalds static inline u32 rt_score(struct rtable *rt)
6641da177e4SLinus Torvalds {
6651da177e4SLinus Torvalds 	u32 score = jiffies - rt->u.dst.lastuse;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	score = ~score & ~(3<<30);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (rt_valuable(rt))
6701da177e4SLinus Torvalds 		score |= (1<<31);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if (!rt->fl.iif ||
6731da177e4SLinus Torvalds 	    !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
6741da177e4SLinus Torvalds 		score |= (1<<30);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	return score;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791080d709SNeil Horman static inline bool rt_caching(const struct net *net)
6801080d709SNeil Horman {
6811080d709SNeil Horman 	return net->ipv4.current_rt_cache_rebuild_count <=
6821080d709SNeil Horman 		net->ipv4.sysctl_rt_cache_rebuild_count;
6831080d709SNeil Horman }
6841080d709SNeil Horman 
6851080d709SNeil Horman static inline bool compare_hash_inputs(const struct flowi *fl1,
6861080d709SNeil Horman 					const struct flowi *fl2)
6871080d709SNeil Horman {
6881080d709SNeil Horman 	return (__force u32)(((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
6891080d709SNeil Horman 		(fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr) |
6901080d709SNeil Horman 		(fl1->iif ^ fl2->iif)) == 0);
6911080d709SNeil Horman }
6921080d709SNeil Horman 
6931da177e4SLinus Torvalds static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
6941da177e4SLinus Torvalds {
695714e85beSAl Viro 	return ((__force u32)((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
696714e85beSAl Viro 		(fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr)) |
69747dcf0cbSThomas Graf 		(fl1->mark ^ fl2->mark) |
6988238b218SDavid S. Miller 		(*(u16 *)&fl1->nl_u.ip4_u.tos ^
6998238b218SDavid S. Miller 		 *(u16 *)&fl2->nl_u.ip4_u.tos) |
7008238b218SDavid S. Miller 		(fl1->oif ^ fl2->oif) |
7018238b218SDavid S. Miller 		(fl1->iif ^ fl2->iif)) == 0;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
704b5921910SDenis V. Lunev static inline int compare_netns(struct rtable *rt1, struct rtable *rt2)
705b5921910SDenis V. Lunev {
706c346dca1SYOSHIFUJI Hideaki 	return dev_net(rt1->u.dst.dev) == dev_net(rt2->u.dst.dev);
707b5921910SDenis V. Lunev }
708b5921910SDenis V. Lunev 
709e84f84f2SDenis V. Lunev static inline int rt_is_expired(struct rtable *rth)
710e84f84f2SDenis V. Lunev {
711e84f84f2SDenis V. Lunev 	return rth->rt_genid != rt_genid(dev_net(rth->u.dst.dev));
712e84f84f2SDenis V. Lunev }
713e84f84f2SDenis V. Lunev 
714beb659bdSEric Dumazet /*
715beb659bdSEric Dumazet  * Perform a full scan of hash table and free all entries.
716beb659bdSEric Dumazet  * Can be called by a softirq or a process.
717beb659bdSEric Dumazet  * In the later case, we want to be reschedule if necessary
718beb659bdSEric Dumazet  */
719beb659bdSEric Dumazet static void rt_do_flush(int process_context)
720beb659bdSEric Dumazet {
721beb659bdSEric Dumazet 	unsigned int i;
722beb659bdSEric Dumazet 	struct rtable *rth, *next;
72332cb5b4eSDenis V. Lunev 	struct rtable * tail;
724beb659bdSEric Dumazet 
725beb659bdSEric Dumazet 	for (i = 0; i <= rt_hash_mask; i++) {
726beb659bdSEric Dumazet 		if (process_context && need_resched())
727beb659bdSEric Dumazet 			cond_resched();
728beb659bdSEric Dumazet 		rth = rt_hash_table[i].chain;
729beb659bdSEric Dumazet 		if (!rth)
730beb659bdSEric Dumazet 			continue;
731beb659bdSEric Dumazet 
732beb659bdSEric Dumazet 		spin_lock_bh(rt_hash_lock_addr(i));
73332cb5b4eSDenis V. Lunev #ifdef CONFIG_NET_NS
73432cb5b4eSDenis V. Lunev 		{
73532cb5b4eSDenis V. Lunev 		struct rtable ** prev, * p;
73632cb5b4eSDenis V. Lunev 
73732cb5b4eSDenis V. Lunev 		rth = rt_hash_table[i].chain;
73832cb5b4eSDenis V. Lunev 
73932cb5b4eSDenis V. Lunev 		/* defer releasing the head of the list after spin_unlock */
74032cb5b4eSDenis V. Lunev 		for (tail = rth; tail; tail = tail->u.dst.rt_next)
74132cb5b4eSDenis V. Lunev 			if (!rt_is_expired(tail))
74232cb5b4eSDenis V. Lunev 				break;
74332cb5b4eSDenis V. Lunev 		if (rth != tail)
74432cb5b4eSDenis V. Lunev 			rt_hash_table[i].chain = tail;
74532cb5b4eSDenis V. Lunev 
74632cb5b4eSDenis V. Lunev 		/* call rt_free on entries after the tail requiring flush */
74732cb5b4eSDenis V. Lunev 		prev = &rt_hash_table[i].chain;
74832cb5b4eSDenis V. Lunev 		for (p = *prev; p; p = next) {
74932cb5b4eSDenis V. Lunev 			next = p->u.dst.rt_next;
75032cb5b4eSDenis V. Lunev 			if (!rt_is_expired(p)) {
75132cb5b4eSDenis V. Lunev 				prev = &p->u.dst.rt_next;
75232cb5b4eSDenis V. Lunev 			} else {
75332cb5b4eSDenis V. Lunev 				*prev = next;
75432cb5b4eSDenis V. Lunev 				rt_free(p);
75532cb5b4eSDenis V. Lunev 			}
75632cb5b4eSDenis V. Lunev 		}
75732cb5b4eSDenis V. Lunev 		}
75832cb5b4eSDenis V. Lunev #else
759beb659bdSEric Dumazet 		rth = rt_hash_table[i].chain;
760beb659bdSEric Dumazet 		rt_hash_table[i].chain = NULL;
76132cb5b4eSDenis V. Lunev 		tail = NULL;
76232cb5b4eSDenis V. Lunev #endif
763beb659bdSEric Dumazet 		spin_unlock_bh(rt_hash_lock_addr(i));
764beb659bdSEric Dumazet 
76532cb5b4eSDenis V. Lunev 		for (; rth != tail; rth = next) {
766beb659bdSEric Dumazet 			next = rth->u.dst.rt_next;
767beb659bdSEric Dumazet 			rt_free(rth);
768beb659bdSEric Dumazet 		}
769beb659bdSEric Dumazet 	}
770beb659bdSEric Dumazet }
771beb659bdSEric Dumazet 
7721080d709SNeil Horman /*
7731080d709SNeil Horman  * While freeing expired entries, we compute average chain length
7741080d709SNeil Horman  * and standard deviation, using fixed-point arithmetic.
7751080d709SNeil Horman  * This to have an estimation of rt_chain_length_max
7761080d709SNeil Horman  *  rt_chain_length_max = max(elasticity, AVG + 4*SD)
7771080d709SNeil Horman  * We use 3 bits for frational part, and 29 (or 61) for magnitude.
7781080d709SNeil Horman  */
7791080d709SNeil Horman 
7801080d709SNeil Horman #define FRACT_BITS 3
7811080d709SNeil Horman #define ONE (1UL << FRACT_BITS)
7821080d709SNeil Horman 
783beb659bdSEric Dumazet static void rt_check_expire(void)
7841da177e4SLinus Torvalds {
785bb1d23b0SEric Dumazet 	static unsigned int rover;
786bb1d23b0SEric Dumazet 	unsigned int i = rover, goal;
7871da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
788*cf8da764SEric Dumazet 	unsigned long samples = 0;
7891080d709SNeil Horman 	unsigned long sum = 0, sum2 = 0;
790bb1d23b0SEric Dumazet 	u64 mult;
7911da177e4SLinus Torvalds 
792bb1d23b0SEric Dumazet 	mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
793bb1d23b0SEric Dumazet 	if (ip_rt_gc_timeout > 1)
794bb1d23b0SEric Dumazet 		do_div(mult, ip_rt_gc_timeout);
795bb1d23b0SEric Dumazet 	goal = (unsigned int)mult;
79639c90eceSEric Dumazet 	if (goal > rt_hash_mask)
79739c90eceSEric Dumazet 		goal = rt_hash_mask + 1;
798bb1d23b0SEric Dumazet 	for (; goal > 0; goal--) {
7991da177e4SLinus Torvalds 		unsigned long tmo = ip_rt_gc_timeout;
800*cf8da764SEric Dumazet 		unsigned long length;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		i = (i + 1) & rt_hash_mask;
8031da177e4SLinus Torvalds 		rthp = &rt_hash_table[i].chain;
8041da177e4SLinus Torvalds 
805d90bf5a9SEric Dumazet 		if (need_resched())
806d90bf5a9SEric Dumazet 			cond_resched();
807d90bf5a9SEric Dumazet 
8081080d709SNeil Horman 		samples++;
8091080d709SNeil Horman 
810cfcabdccSStephen Hemminger 		if (*rthp == NULL)
811bb1d23b0SEric Dumazet 			continue;
812*cf8da764SEric Dumazet 		length = 0;
81339c90eceSEric Dumazet 		spin_lock_bh(rt_hash_lock_addr(i));
8141da177e4SLinus Torvalds 		while ((rth = *rthp) != NULL) {
815e84f84f2SDenis V. Lunev 			if (rt_is_expired(rth)) {
81629e75252SEric Dumazet 				*rthp = rth->u.dst.rt_next;
81729e75252SEric Dumazet 				rt_free(rth);
81829e75252SEric Dumazet 				continue;
81929e75252SEric Dumazet 			}
8201da177e4SLinus Torvalds 			if (rth->u.dst.expires) {
8211da177e4SLinus Torvalds 				/* Entry is expired even if it is in use */
82239c90eceSEric Dumazet 				if (time_before_eq(jiffies, rth->u.dst.expires)) {
8231da177e4SLinus Torvalds 					tmo >>= 1;
824093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
8251080d709SNeil Horman 					/*
8261080d709SNeil Horman 					 * Only bump our length if the hash
8271080d709SNeil Horman 					 * inputs on entries n and n+1 are not
8281080d709SNeil Horman 					 * the same, we only count entries on
8291080d709SNeil Horman 					 * a chain with equal hash inputs once
8301080d709SNeil Horman 					 * so that entries for different QOS
8311080d709SNeil Horman 					 * levels, and other non-hash input
8321080d709SNeil Horman 					 * attributes don't unfairly skew
8331080d709SNeil Horman 					 * the length computation
8341080d709SNeil Horman 					 */
8351080d709SNeil Horman 					if ((*rthp == NULL) ||
8361080d709SNeil Horman 					    !compare_hash_inputs(&(*rthp)->fl,
8371080d709SNeil Horman 								 &rth->fl))
8381080d709SNeil Horman 						length += ONE;
8391da177e4SLinus Torvalds 					continue;
8401da177e4SLinus Torvalds 				}
8411da177e4SLinus Torvalds 			} else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
8421da177e4SLinus Torvalds 				tmo >>= 1;
843093c2ca4SEric Dumazet 				rthp = &rth->u.dst.rt_next;
8441080d709SNeil Horman 				if ((*rthp == NULL) ||
8451080d709SNeil Horman 				    !compare_hash_inputs(&(*rthp)->fl,
8461080d709SNeil Horman 							 &rth->fl))
8471080d709SNeil Horman 					length += ONE;
8481da177e4SLinus Torvalds 				continue;
8491da177e4SLinus Torvalds 			}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 			/* Cleanup aged off entries. */
852093c2ca4SEric Dumazet 			*rthp = rth->u.dst.rt_next;
8531da177e4SLinus Torvalds 			rt_free(rth);
8541da177e4SLinus Torvalds 		}
85539c90eceSEric Dumazet 		spin_unlock_bh(rt_hash_lock_addr(i));
8561080d709SNeil Horman 		sum += length;
8571080d709SNeil Horman 		sum2 += length*length;
8581080d709SNeil Horman 	}
8591080d709SNeil Horman 	if (samples) {
8601080d709SNeil Horman 		unsigned long avg = sum / samples;
8611080d709SNeil Horman 		unsigned long sd = int_sqrt(sum2 / samples - avg*avg);
8621080d709SNeil Horman 		rt_chain_length_max = max_t(unsigned long,
8631080d709SNeil Horman 					ip_rt_gc_elasticity,
8641080d709SNeil Horman 					(avg + 4*sd) >> FRACT_BITS);
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	rover = i;
867beb659bdSEric Dumazet }
868beb659bdSEric Dumazet 
869beb659bdSEric Dumazet /*
870beb659bdSEric Dumazet  * rt_worker_func() is run in process context.
87129e75252SEric Dumazet  * we call rt_check_expire() to scan part of the hash table
872beb659bdSEric Dumazet  */
873beb659bdSEric Dumazet static void rt_worker_func(struct work_struct *work)
874beb659bdSEric Dumazet {
875beb659bdSEric Dumazet 	rt_check_expire();
87639c90eceSEric Dumazet 	schedule_delayed_work(&expires_work, ip_rt_gc_interval);
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
87929e75252SEric Dumazet /*
88029e75252SEric Dumazet  * Pertubation of rt_genid by a small quantity [1..256]
88129e75252SEric Dumazet  * Using 8 bits of shuffling ensure we can call rt_cache_invalidate()
88229e75252SEric Dumazet  * many times (2^24) without giving recent rt_genid.
88329e75252SEric Dumazet  * Jenkins hash is strong enough that litle changes of rt_genid are OK.
8841da177e4SLinus Torvalds  */
88586c657f6SDenis V. Lunev static void rt_cache_invalidate(struct net *net)
8861da177e4SLinus Torvalds {
88729e75252SEric Dumazet 	unsigned char shuffle;
8881da177e4SLinus Torvalds 
88929e75252SEric Dumazet 	get_random_bytes(&shuffle, sizeof(shuffle));
890e84f84f2SDenis V. Lunev 	atomic_add(shuffle + 1U, &net->ipv4.rt_genid);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
893beb659bdSEric Dumazet /*
89429e75252SEric Dumazet  * delay < 0  : invalidate cache (fast : entries will be deleted later)
89529e75252SEric Dumazet  * delay >= 0 : invalidate & flush cache (can be long)
89629e75252SEric Dumazet  */
89776e6ebfbSDenis V. Lunev void rt_cache_flush(struct net *net, int delay)
89829e75252SEric Dumazet {
89986c657f6SDenis V. Lunev 	rt_cache_invalidate(net);
90029e75252SEric Dumazet 	if (delay >= 0)
90129e75252SEric Dumazet 		rt_do_flush(!in_softirq());
90229e75252SEric Dumazet }
90329e75252SEric Dumazet 
90429e75252SEric Dumazet /*
90529e75252SEric Dumazet  * We change rt_genid and let gc do the cleanup
906beb659bdSEric Dumazet  */
9079f5e97e5SDenis V. Lunev static void rt_secret_rebuild(unsigned long __net)
9081da177e4SLinus Torvalds {
9099f5e97e5SDenis V. Lunev 	struct net *net = (struct net *)__net;
91086c657f6SDenis V. Lunev 	rt_cache_invalidate(net);
9119f5e97e5SDenis V. Lunev 	mod_timer(&net->ipv4.rt_secret_timer, jiffies + ip_rt_secret_interval);
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141080d709SNeil Horman static void rt_secret_rebuild_oneshot(struct net *net)
9151080d709SNeil Horman {
9161080d709SNeil Horman 	del_timer_sync(&net->ipv4.rt_secret_timer);
9171080d709SNeil Horman 	rt_cache_invalidate(net);
9181080d709SNeil Horman 	if (ip_rt_secret_interval) {
9191080d709SNeil Horman 		net->ipv4.rt_secret_timer.expires += ip_rt_secret_interval;
9201080d709SNeil Horman 		add_timer(&net->ipv4.rt_secret_timer);
9211080d709SNeil Horman 	}
9221080d709SNeil Horman }
9231080d709SNeil Horman 
9241080d709SNeil Horman static void rt_emergency_hash_rebuild(struct net *net)
9251080d709SNeil Horman {
9261080d709SNeil Horman 	if (net_ratelimit()) {
9271080d709SNeil Horman 		printk(KERN_WARNING "Route hash chain too long!\n");
9281080d709SNeil Horman 		printk(KERN_WARNING "Adjust your secret_interval!\n");
9291080d709SNeil Horman 	}
9301080d709SNeil Horman 
9311080d709SNeil Horman 	rt_secret_rebuild_oneshot(net);
9321080d709SNeil Horman }
9331080d709SNeil Horman 
9341da177e4SLinus Torvalds /*
9351da177e4SLinus Torvalds    Short description of GC goals.
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds    We want to build algorithm, which will keep routing cache
9381da177e4SLinus Torvalds    at some equilibrium point, when number of aged off entries
9391da177e4SLinus Torvalds    is kept approximately equal to newly generated ones.
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds    Current expiration strength is variable "expire".
9421da177e4SLinus Torvalds    We try to adjust it dynamically, so that if networking
9431da177e4SLinus Torvalds    is idle expires is large enough to keep enough of warm entries,
9441da177e4SLinus Torvalds    and when load increases it reduces to limit cache size.
9451da177e4SLinus Torvalds  */
9461da177e4SLinus Torvalds 
947569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	static unsigned long expire = RT_GC_TIMEOUT;
9501da177e4SLinus Torvalds 	static unsigned long last_gc;
9511da177e4SLinus Torvalds 	static int rover;
9521da177e4SLinus Torvalds 	static int equilibrium;
9531da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
9541da177e4SLinus Torvalds 	unsigned long now = jiffies;
9551da177e4SLinus Torvalds 	int goal;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	/*
9581da177e4SLinus Torvalds 	 * Garbage collection is pretty expensive,
9591da177e4SLinus Torvalds 	 * do not make it too frequently.
9601da177e4SLinus Torvalds 	 */
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_total);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	if (now - last_gc < ip_rt_gc_min_interval &&
9651da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
9661da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_ignored);
9671da177e4SLinus Torvalds 		goto out;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	/* Calculate number of entries, which we want to expire now. */
9711da177e4SLinus Torvalds 	goal = atomic_read(&ipv4_dst_ops.entries) -
9721da177e4SLinus Torvalds 		(ip_rt_gc_elasticity << rt_hash_log);
9731da177e4SLinus Torvalds 	if (goal <= 0) {
9741da177e4SLinus Torvalds 		if (equilibrium < ipv4_dst_ops.gc_thresh)
9751da177e4SLinus Torvalds 			equilibrium = ipv4_dst_ops.gc_thresh;
9761da177e4SLinus Torvalds 		goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
9771da177e4SLinus Torvalds 		if (goal > 0) {
978b790ceddSEric Dumazet 			equilibrium += min_t(unsigned int, goal >> 1, rt_hash_mask + 1);
9791da177e4SLinus Torvalds 			goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
9801da177e4SLinus Torvalds 		}
9811da177e4SLinus Torvalds 	} else {
9821da177e4SLinus Torvalds 		/* We are in dangerous area. Try to reduce cache really
9831da177e4SLinus Torvalds 		 * aggressively.
9841da177e4SLinus Torvalds 		 */
985b790ceddSEric Dumazet 		goal = max_t(unsigned int, goal >> 1, rt_hash_mask + 1);
9861da177e4SLinus Torvalds 		equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	if (now - last_gc >= ip_rt_gc_min_interval)
9901da177e4SLinus Torvalds 		last_gc = now;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (goal <= 0) {
9931da177e4SLinus Torvalds 		equilibrium += goal;
9941da177e4SLinus Torvalds 		goto work_done;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	do {
9981da177e4SLinus Torvalds 		int i, k;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 		for (i = rt_hash_mask, k = rover; i >= 0; i--) {
10011da177e4SLinus Torvalds 			unsigned long tmo = expire;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 			k = (k + 1) & rt_hash_mask;
10041da177e4SLinus Torvalds 			rthp = &rt_hash_table[k].chain;
100522c047ccSEric Dumazet 			spin_lock_bh(rt_hash_lock_addr(k));
10061da177e4SLinus Torvalds 			while ((rth = *rthp) != NULL) {
1007e84f84f2SDenis V. Lunev 				if (!rt_is_expired(rth) &&
100829e75252SEric Dumazet 					!rt_may_expire(rth, tmo, expire)) {
10091da177e4SLinus Torvalds 					tmo >>= 1;
1010093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
10111da177e4SLinus Torvalds 					continue;
10121da177e4SLinus Torvalds 				}
1013093c2ca4SEric Dumazet 				*rthp = rth->u.dst.rt_next;
10141da177e4SLinus Torvalds 				rt_free(rth);
10151da177e4SLinus Torvalds 				goal--;
10161da177e4SLinus Torvalds 			}
101722c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(k));
10181da177e4SLinus Torvalds 			if (goal <= 0)
10191da177e4SLinus Torvalds 				break;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 		rover = k;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 		if (goal <= 0)
10241da177e4SLinus Torvalds 			goto work_done;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 		/* Goal is not achieved. We stop process if:
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 		   - if expire reduced to zero. Otherwise, expire is halfed.
10291da177e4SLinus Torvalds 		   - if table is not full.
10301da177e4SLinus Torvalds 		   - if we are called from interrupt.
10311da177e4SLinus Torvalds 		   - jiffies check is just fallback/debug loop breaker.
10321da177e4SLinus Torvalds 		     We will not spin here for long time in any case.
10331da177e4SLinus Torvalds 		 */
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_goal_miss);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 		if (expire == 0)
10381da177e4SLinus Torvalds 			break;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		expire >>= 1;
10411da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
10421da177e4SLinus Torvalds 		printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
10431da177e4SLinus Torvalds 				atomic_read(&ipv4_dst_ops.entries), goal, i);
10441da177e4SLinus Torvalds #endif
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 		if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
10471da177e4SLinus Torvalds 			goto out;
10481da177e4SLinus Torvalds 	} while (!in_softirq() && time_before_eq(jiffies, now));
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
10511da177e4SLinus Torvalds 		goto out;
10521da177e4SLinus Torvalds 	if (net_ratelimit())
10531da177e4SLinus Torvalds 		printk(KERN_WARNING "dst cache overflow\n");
10541da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_dst_overflow);
10551da177e4SLinus Torvalds 	return 1;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds work_done:
10581da177e4SLinus Torvalds 	expire += ip_rt_gc_min_interval;
10591da177e4SLinus Torvalds 	if (expire > ip_rt_gc_timeout ||
10601da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
10611da177e4SLinus Torvalds 		expire = ip_rt_gc_timeout;
10621da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
10631da177e4SLinus Torvalds 	printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
10641da177e4SLinus Torvalds 			atomic_read(&ipv4_dst_ops.entries), goal, rover);
10651da177e4SLinus Torvalds #endif
10661da177e4SLinus Torvalds out:	return 0;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	struct rtable	*rth, **rthp;
10721080d709SNeil Horman 	struct rtable	*rthi;
10731da177e4SLinus Torvalds 	unsigned long	now;
10741da177e4SLinus Torvalds 	struct rtable *cand, **candp;
10751da177e4SLinus Torvalds 	u32 		min_score;
10761da177e4SLinus Torvalds 	int		chain_length;
10771da177e4SLinus Torvalds 	int attempts = !in_softirq();
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds restart:
10801da177e4SLinus Torvalds 	chain_length = 0;
10811da177e4SLinus Torvalds 	min_score = ~(u32)0;
10821da177e4SLinus Torvalds 	cand = NULL;
10831da177e4SLinus Torvalds 	candp = NULL;
10841da177e4SLinus Torvalds 	now = jiffies;
10851da177e4SLinus Torvalds 
10861080d709SNeil Horman 	if (!rt_caching(dev_net(rt->u.dst.dev))) {
10871080d709SNeil Horman 		rt_drop(rt);
10881080d709SNeil Horman 		return 0;
10891080d709SNeil Horman 	}
10901080d709SNeil Horman 
10911da177e4SLinus Torvalds 	rthp = &rt_hash_table[hash].chain;
10921080d709SNeil Horman 	rthi = NULL;
10931da177e4SLinus Torvalds 
109422c047ccSEric Dumazet 	spin_lock_bh(rt_hash_lock_addr(hash));
10951da177e4SLinus Torvalds 	while ((rth = *rthp) != NULL) {
1096e84f84f2SDenis V. Lunev 		if (rt_is_expired(rth)) {
109729e75252SEric Dumazet 			*rthp = rth->u.dst.rt_next;
109829e75252SEric Dumazet 			rt_free(rth);
109929e75252SEric Dumazet 			continue;
110029e75252SEric Dumazet 		}
1101b5921910SDenis V. Lunev 		if (compare_keys(&rth->fl, &rt->fl) && compare_netns(rth, rt)) {
11021da177e4SLinus Torvalds 			/* Put it first */
1103093c2ca4SEric Dumazet 			*rthp = rth->u.dst.rt_next;
11041da177e4SLinus Torvalds 			/*
11051da177e4SLinus Torvalds 			 * Since lookup is lockfree, the deletion
11061da177e4SLinus Torvalds 			 * must be visible to another weakly ordered CPU before
11071da177e4SLinus Torvalds 			 * the insertion at the start of the hash chain.
11081da177e4SLinus Torvalds 			 */
1109093c2ca4SEric Dumazet 			rcu_assign_pointer(rth->u.dst.rt_next,
11101da177e4SLinus Torvalds 					   rt_hash_table[hash].chain);
11111da177e4SLinus Torvalds 			/*
11121da177e4SLinus Torvalds 			 * Since lookup is lockfree, the update writes
11131da177e4SLinus Torvalds 			 * must be ordered for consistency on SMP.
11141da177e4SLinus Torvalds 			 */
11151da177e4SLinus Torvalds 			rcu_assign_pointer(rt_hash_table[hash].chain, rth);
11161da177e4SLinus Torvalds 
111703f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, now);
111822c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(hash));
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 			rt_drop(rt);
11211da177e4SLinus Torvalds 			*rp = rth;
11221da177e4SLinus Torvalds 			return 0;
11231da177e4SLinus Torvalds 		}
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 		if (!atomic_read(&rth->u.dst.__refcnt)) {
11261da177e4SLinus Torvalds 			u32 score = rt_score(rth);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 			if (score <= min_score) {
11291da177e4SLinus Torvalds 				cand = rth;
11301da177e4SLinus Torvalds 				candp = rthp;
11311da177e4SLinus Torvalds 				min_score = score;
11321da177e4SLinus Torvalds 			}
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		chain_length++;
11361da177e4SLinus Torvalds 
1137093c2ca4SEric Dumazet 		rthp = &rth->u.dst.rt_next;
11381080d709SNeil Horman 
11391080d709SNeil Horman 		/*
11401080d709SNeil Horman 		 * check to see if the next entry in the chain
11411080d709SNeil Horman 		 * contains the same hash input values as rt.  If it does
11421080d709SNeil Horman 		 * This is where we will insert into the list, instead of
11431080d709SNeil Horman 		 * at the head.  This groups entries that differ by aspects not
11441080d709SNeil Horman 		 * relvant to the hash function together, which we use to adjust
11451080d709SNeil Horman 		 * our chain length
11461080d709SNeil Horman 		 */
11471080d709SNeil Horman 		if (*rthp && compare_hash_inputs(&(*rthp)->fl, &rt->fl))
11481080d709SNeil Horman 			rthi = rth;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (cand) {
11521da177e4SLinus Torvalds 		/* ip_rt_gc_elasticity used to be average length of chain
11531da177e4SLinus Torvalds 		 * length, when exceeded gc becomes really aggressive.
11541da177e4SLinus Torvalds 		 *
11551da177e4SLinus Torvalds 		 * The second limit is less certain. At the moment it allows
11561da177e4SLinus Torvalds 		 * only 2 entries per bucket. We will see.
11571da177e4SLinus Torvalds 		 */
11581da177e4SLinus Torvalds 		if (chain_length > ip_rt_gc_elasticity) {
1159093c2ca4SEric Dumazet 			*candp = cand->u.dst.rt_next;
11601da177e4SLinus Torvalds 			rt_free(cand);
11611da177e4SLinus Torvalds 		}
11621080d709SNeil Horman 	} else {
11631080d709SNeil Horman 		if (chain_length > rt_chain_length_max) {
11641080d709SNeil Horman 			struct net *net = dev_net(rt->u.dst.dev);
11651080d709SNeil Horman 			int num = ++net->ipv4.current_rt_cache_rebuild_count;
11661080d709SNeil Horman 			if (!rt_caching(dev_net(rt->u.dst.dev))) {
11671080d709SNeil Horman 				printk(KERN_WARNING "%s: %d rebuilds is over limit, route caching disabled\n",
11681080d709SNeil Horman 					rt->u.dst.dev->name, num);
11691080d709SNeil Horman 			}
11701080d709SNeil Horman 			rt_emergency_hash_rebuild(dev_net(rt->u.dst.dev));
11711080d709SNeil Horman 		}
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/* Try to bind route to arp only if it is output
11751da177e4SLinus Torvalds 	   route or unicast forwarding path.
11761da177e4SLinus Torvalds 	 */
11771da177e4SLinus Torvalds 	if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
11781da177e4SLinus Torvalds 		int err = arp_bind_neighbour(&rt->u.dst);
11791da177e4SLinus Torvalds 		if (err) {
118022c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(hash));
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 			if (err != -ENOBUFS) {
11831da177e4SLinus Torvalds 				rt_drop(rt);
11841da177e4SLinus Torvalds 				return err;
11851da177e4SLinus Torvalds 			}
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 			/* Neighbour tables are full and nothing
11881da177e4SLinus Torvalds 			   can be released. Try to shrink route cache,
11891da177e4SLinus Torvalds 			   it is most likely it holds some neighbour records.
11901da177e4SLinus Torvalds 			 */
11911da177e4SLinus Torvalds 			if (attempts-- > 0) {
11921da177e4SLinus Torvalds 				int saved_elasticity = ip_rt_gc_elasticity;
11931da177e4SLinus Torvalds 				int saved_int = ip_rt_gc_min_interval;
11941da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= 1;
11951da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= 0;
1196569d3645SDaniel Lezcano 				rt_garbage_collect(&ipv4_dst_ops);
11971da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= saved_int;
11981da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= saved_elasticity;
11991da177e4SLinus Torvalds 				goto restart;
12001da177e4SLinus Torvalds 			}
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 			if (net_ratelimit())
12031da177e4SLinus Torvalds 				printk(KERN_WARNING "Neighbour table overflow.\n");
12041da177e4SLinus Torvalds 			rt_drop(rt);
12051da177e4SLinus Torvalds 			return -ENOBUFS;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 	}
12081da177e4SLinus Torvalds 
12091080d709SNeil Horman 	if (rthi)
12101080d709SNeil Horman 		rt->u.dst.rt_next = rthi->u.dst.rt_next;
12111080d709SNeil Horman 	else
1212093c2ca4SEric Dumazet 		rt->u.dst.rt_next = rt_hash_table[hash].chain;
12131080d709SNeil Horman 
12141da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
1215093c2ca4SEric Dumazet 	if (rt->u.dst.rt_next) {
12161da177e4SLinus Torvalds 		struct rtable *trt;
1217673d57e7SHarvey Harrison 		printk(KERN_DEBUG "rt_cache @%02x: %pI4", hash, &rt->rt_dst);
1218093c2ca4SEric Dumazet 		for (trt = rt->u.dst.rt_next; trt; trt = trt->u.dst.rt_next)
1219673d57e7SHarvey Harrison 			printk(" . %pI4", &trt->rt_dst);
12201da177e4SLinus Torvalds 		printk("\n");
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds #endif
122300269b54SEric Dumazet 	/*
122400269b54SEric Dumazet 	 * Since lookup is lockfree, we must make sure
122500269b54SEric Dumazet 	 * previous writes to rt are comitted to memory
122600269b54SEric Dumazet 	 * before making rt visible to other CPUS.
122700269b54SEric Dumazet 	 */
12281080d709SNeil Horman 	if (rthi)
12291080d709SNeil Horman 		rcu_assign_pointer(rthi->u.dst.rt_next, rt);
12301080d709SNeil Horman 	else
123100269b54SEric Dumazet 		rcu_assign_pointer(rt_hash_table[hash].chain, rt);
12321080d709SNeil Horman 
123322c047ccSEric Dumazet 	spin_unlock_bh(rt_hash_lock_addr(hash));
12341da177e4SLinus Torvalds 	*rp = rt;
12351da177e4SLinus Torvalds 	return 0;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds void rt_bind_peer(struct rtable *rt, int create)
12391da177e4SLinus Torvalds {
12401da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(rt_peer_lock);
12411da177e4SLinus Torvalds 	struct inet_peer *peer;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	peer = inet_getpeer(rt->rt_dst, create);
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	spin_lock_bh(&rt_peer_lock);
12461da177e4SLinus Torvalds 	if (rt->peer == NULL) {
12471da177e4SLinus Torvalds 		rt->peer = peer;
12481da177e4SLinus Torvalds 		peer = NULL;
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds 	spin_unlock_bh(&rt_peer_lock);
12511da177e4SLinus Torvalds 	if (peer)
12521da177e4SLinus Torvalds 		inet_putpeer(peer);
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds /*
12561da177e4SLinus Torvalds  * Peer allocation may fail only in serious out-of-memory conditions.  However
12571da177e4SLinus Torvalds  * we still can generate some output.
12581da177e4SLinus Torvalds  * Random ID selection looks a bit dangerous because we have no chances to
12591da177e4SLinus Torvalds  * select ID being unique in a reasonable period of time.
12601da177e4SLinus Torvalds  * But broken packet identifier may be better than no packet at all.
12611da177e4SLinus Torvalds  */
12621da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(ip_fb_id_lock);
12651da177e4SLinus Torvalds 	static u32 ip_fallback_id;
12661da177e4SLinus Torvalds 	u32 salt;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	spin_lock_bh(&ip_fb_id_lock);
1269e448515cSAl Viro 	salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
12701da177e4SLinus Torvalds 	iph->id = htons(salt & 0xFFFF);
12711da177e4SLinus Torvalds 	ip_fallback_id = salt;
12721da177e4SLinus Torvalds 	spin_unlock_bh(&ip_fb_id_lock);
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	if (rt) {
12801da177e4SLinus Torvalds 		if (rt->peer == NULL)
12811da177e4SLinus Torvalds 			rt_bind_peer(rt, 1);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 		/* If peer is attached to destination, it is never detached,
12841da177e4SLinus Torvalds 		   so that we need not to grab a lock to dereference it.
12851da177e4SLinus Torvalds 		 */
12861da177e4SLinus Torvalds 		if (rt->peer) {
12871da177e4SLinus Torvalds 			iph->id = htons(inet_getid(rt->peer, more));
12881da177e4SLinus Torvalds 			return;
12891da177e4SLinus Torvalds 		}
12901da177e4SLinus Torvalds 	} else
12919c2b3328SStephen Hemminger 		printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
12929c2b3328SStephen Hemminger 		       __builtin_return_address(0));
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	ip_select_fb_ident(iph);
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds static void rt_del(unsigned hash, struct rtable *rt)
12981da177e4SLinus Torvalds {
129929e75252SEric Dumazet 	struct rtable **rthp, *aux;
13001da177e4SLinus Torvalds 
130129e75252SEric Dumazet 	rthp = &rt_hash_table[hash].chain;
130222c047ccSEric Dumazet 	spin_lock_bh(rt_hash_lock_addr(hash));
13031da177e4SLinus Torvalds 	ip_rt_put(rt);
130429e75252SEric Dumazet 	while ((aux = *rthp) != NULL) {
1305e84f84f2SDenis V. Lunev 		if (aux == rt || rt_is_expired(aux)) {
130629e75252SEric Dumazet 			*rthp = aux->u.dst.rt_next;
130729e75252SEric Dumazet 			rt_free(aux);
130829e75252SEric Dumazet 			continue;
130929e75252SEric Dumazet 		}
131029e75252SEric Dumazet 		rthp = &aux->u.dst.rt_next;
13111da177e4SLinus Torvalds 	}
131222c047ccSEric Dumazet 	spin_unlock_bh(rt_hash_lock_addr(hash));
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
1315f7655229SAl Viro void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
1316f7655229SAl Viro 		    __be32 saddr, struct net_device *dev)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	int i, k;
13191da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
13201da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
1321f7655229SAl Viro 	__be32  skeys[2] = { saddr, 0 };
13221da177e4SLinus Torvalds 	int  ikeys[2] = { dev->ifindex, 0 };
13238d71740cSTom Tucker 	struct netevent_redirect netevent;
1324317805b8SDenis V. Lunev 	struct net *net;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	if (!in_dev)
13271da177e4SLinus Torvalds 		return;
13281da177e4SLinus Torvalds 
1329c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
13301da177e4SLinus Torvalds 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
13311e637c74SJan Engelhardt 	    || ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw)
1332f97c1e0cSJoe Perches 	    || ipv4_is_zeronet(new_gw))
13331da177e4SLinus Torvalds 		goto reject_redirect;
13341da177e4SLinus Torvalds 
13351080d709SNeil Horman 	if (!rt_caching(net))
13361080d709SNeil Horman 		goto reject_redirect;
13371080d709SNeil Horman 
13381da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
13391da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
13401da177e4SLinus Torvalds 			goto reject_redirect;
13411da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
13421da177e4SLinus Torvalds 			goto reject_redirect;
13431da177e4SLinus Torvalds 	} else {
1344317805b8SDenis V. Lunev 		if (inet_addr_type(net, new_gw) != RTN_UNICAST)
13451da177e4SLinus Torvalds 			goto reject_redirect;
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
13491da177e4SLinus Torvalds 		for (k = 0; k < 2; k++) {
1350b00180deSDenis V. Lunev 			unsigned hash = rt_hash(daddr, skeys[i], ikeys[k],
1351e84f84f2SDenis V. Lunev 						rt_genid(net));
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 			rthp=&rt_hash_table[hash].chain;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 			rcu_read_lock();
13561da177e4SLinus Torvalds 			while ((rth = rcu_dereference(*rthp)) != NULL) {
13571da177e4SLinus Torvalds 				struct rtable *rt;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 				if (rth->fl.fl4_dst != daddr ||
13601da177e4SLinus Torvalds 				    rth->fl.fl4_src != skeys[i] ||
13611da177e4SLinus Torvalds 				    rth->fl.oif != ikeys[k] ||
136229e75252SEric Dumazet 				    rth->fl.iif != 0 ||
1363e84f84f2SDenis V. Lunev 				    rt_is_expired(rth) ||
1364878628fbSYOSHIFUJI Hideaki 				    !net_eq(dev_net(rth->u.dst.dev), net)) {
1365093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
13661da177e4SLinus Torvalds 					continue;
13671da177e4SLinus Torvalds 				}
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 				if (rth->rt_dst != daddr ||
13701da177e4SLinus Torvalds 				    rth->rt_src != saddr ||
13711da177e4SLinus Torvalds 				    rth->u.dst.error ||
13721da177e4SLinus Torvalds 				    rth->rt_gateway != old_gw ||
13731da177e4SLinus Torvalds 				    rth->u.dst.dev != dev)
13741da177e4SLinus Torvalds 					break;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 				dst_hold(&rth->u.dst);
13771da177e4SLinus Torvalds 				rcu_read_unlock();
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 				rt = dst_alloc(&ipv4_dst_ops);
13801da177e4SLinus Torvalds 				if (rt == NULL) {
13811da177e4SLinus Torvalds 					ip_rt_put(rth);
13821da177e4SLinus Torvalds 					in_dev_put(in_dev);
13831da177e4SLinus Torvalds 					return;
13841da177e4SLinus Torvalds 				}
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 				/* Copy all the information. */
13871da177e4SLinus Torvalds 				*rt = *rth;
13881da177e4SLinus Torvalds 				rt->u.dst.__use		= 1;
13891da177e4SLinus Torvalds 				atomic_set(&rt->u.dst.__refcnt, 1);
13901da177e4SLinus Torvalds 				rt->u.dst.child		= NULL;
13911da177e4SLinus Torvalds 				if (rt->u.dst.dev)
13921da177e4SLinus Torvalds 					dev_hold(rt->u.dst.dev);
13931da177e4SLinus Torvalds 				if (rt->idev)
13941da177e4SLinus Torvalds 					in_dev_hold(rt->idev);
13951da177e4SLinus Torvalds 				rt->u.dst.obsolete	= 0;
13961da177e4SLinus Torvalds 				rt->u.dst.lastuse	= jiffies;
13971da177e4SLinus Torvalds 				rt->u.dst.path		= &rt->u.dst;
13981da177e4SLinus Torvalds 				rt->u.dst.neighbour	= NULL;
13991da177e4SLinus Torvalds 				rt->u.dst.hh		= NULL;
1400def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
14011da177e4SLinus Torvalds 				rt->u.dst.xfrm		= NULL;
1402def8b4faSAlexey Dobriyan #endif
1403e84f84f2SDenis V. Lunev 				rt->rt_genid		= rt_genid(net);
14041da177e4SLinus Torvalds 				rt->rt_flags		|= RTCF_REDIRECTED;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 				/* Gateway is different ... */
14071da177e4SLinus Torvalds 				rt->rt_gateway		= new_gw;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 				/* Redirect received -> path was valid */
14101da177e4SLinus Torvalds 				dst_confirm(&rth->u.dst);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 				if (rt->peer)
14131da177e4SLinus Torvalds 					atomic_inc(&rt->peer->refcnt);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 				if (arp_bind_neighbour(&rt->u.dst) ||
14161da177e4SLinus Torvalds 				    !(rt->u.dst.neighbour->nud_state &
14171da177e4SLinus Torvalds 					    NUD_VALID)) {
14181da177e4SLinus Torvalds 					if (rt->u.dst.neighbour)
14191da177e4SLinus Torvalds 						neigh_event_send(rt->u.dst.neighbour, NULL);
14201da177e4SLinus Torvalds 					ip_rt_put(rth);
14211da177e4SLinus Torvalds 					rt_drop(rt);
14221da177e4SLinus Torvalds 					goto do_next;
14231da177e4SLinus Torvalds 				}
14241da177e4SLinus Torvalds 
14258d71740cSTom Tucker 				netevent.old = &rth->u.dst;
14268d71740cSTom Tucker 				netevent.new = &rt->u.dst;
14278d71740cSTom Tucker 				call_netevent_notifiers(NETEVENT_REDIRECT,
14288d71740cSTom Tucker 							&netevent);
14298d71740cSTom Tucker 
14301da177e4SLinus Torvalds 				rt_del(hash, rth);
14311da177e4SLinus Torvalds 				if (!rt_intern_hash(hash, rt, &rt))
14321da177e4SLinus Torvalds 					ip_rt_put(rt);
14331da177e4SLinus Torvalds 				goto do_next;
14341da177e4SLinus Torvalds 			}
14351da177e4SLinus Torvalds 			rcu_read_unlock();
14361da177e4SLinus Torvalds 		do_next:
14371da177e4SLinus Torvalds 			;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 	in_dev_put(in_dev);
14411da177e4SLinus Torvalds 	return;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds reject_redirect:
14441da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
14451da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
1446673d57e7SHarvey Harrison 		printk(KERN_INFO "Redirect from %pI4 on %s about %pI4 ignored.\n"
1447673d57e7SHarvey Harrison 			"  Advised path = %pI4 -> %pI4\n",
1448673d57e7SHarvey Harrison 		       &old_gw, dev->name, &new_gw,
1449673d57e7SHarvey Harrison 		       &saddr, &daddr);
14501da177e4SLinus Torvalds #endif
14511da177e4SLinus Torvalds 	in_dev_put(in_dev);
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)dst;
14571da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (rt) {
14601da177e4SLinus Torvalds 		if (dst->obsolete) {
14611da177e4SLinus Torvalds 			ip_rt_put(rt);
14621da177e4SLinus Torvalds 			ret = NULL;
14631da177e4SLinus Torvalds 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
14641da177e4SLinus Torvalds 			   rt->u.dst.expires) {
14658c7bc840SAl Viro 			unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
1466b00180deSDenis V. Lunev 						rt->fl.oif,
1467e84f84f2SDenis V. Lunev 						rt_genid(dev_net(dst->dev)));
14681da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 1
1469673d57e7SHarvey Harrison 			printk(KERN_DEBUG "ipv4_negative_advice: redirect to %pI4/%02x dropped\n",
1470673d57e7SHarvey Harrison 				&rt->rt_dst, rt->fl.fl4_tos);
14711da177e4SLinus Torvalds #endif
14721da177e4SLinus Torvalds 			rt_del(hash, rt);
14731da177e4SLinus Torvalds 			ret = NULL;
14741da177e4SLinus Torvalds 		}
14751da177e4SLinus Torvalds 	}
14761da177e4SLinus Torvalds 	return ret;
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds /*
14801da177e4SLinus Torvalds  * Algorithm:
14811da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
14821da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
14831da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
14841da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
14851da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
14861da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
14871da177e4SLinus Torvalds  *
14881da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
14891da177e4SLinus Torvalds  * in icmp.c.
14901da177e4SLinus Torvalds  *
14911da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
14921da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
14931da177e4SLinus Torvalds  */
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
14961da177e4SLinus Torvalds {
1497ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
14981da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	if (!in_dev)
15011da177e4SLinus Torvalds 		return;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	if (!IN_DEV_TX_REDIRECTS(in_dev))
15041da177e4SLinus Torvalds 		goto out;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
15071da177e4SLinus Torvalds 	 * reset the algorithm.
15081da177e4SLinus Torvalds 	 */
15091da177e4SLinus Torvalds 	if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
15101da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = 0;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
15131da177e4SLinus Torvalds 	 * set u.dst.rate_last to the last seen redirected packet.
15141da177e4SLinus Torvalds 	 */
15151da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
15161da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
15171da177e4SLinus Torvalds 		goto out;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
15211da177e4SLinus Torvalds 	 * redirect.
15221da177e4SLinus Torvalds 	 */
152314fb8a76SLi Yewang 	if (rt->u.dst.rate_tokens == 0 ||
152414fb8a76SLi Yewang 	    time_after(jiffies,
15251da177e4SLinus Torvalds 		       (rt->u.dst.rate_last +
15261da177e4SLinus Torvalds 			(ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
15271da177e4SLinus Torvalds 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
15281da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
15291da177e4SLinus Torvalds 		++rt->u.dst.rate_tokens;
15301da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
15311da177e4SLinus Torvalds 		if (IN_DEV_LOG_MARTIANS(in_dev) &&
15321da177e4SLinus Torvalds 		    rt->u.dst.rate_tokens == ip_rt_redirect_number &&
15331da177e4SLinus Torvalds 		    net_ratelimit())
1534673d57e7SHarvey Harrison 			printk(KERN_WARNING "host %pI4/if%d ignores redirects for %pI4 to %pI4.\n",
1535673d57e7SHarvey Harrison 				&rt->rt_src, rt->rt_iif,
1536673d57e7SHarvey Harrison 				&rt->rt_dst, &rt->rt_gateway);
15371da177e4SLinus Torvalds #endif
15381da177e4SLinus Torvalds 	}
15391da177e4SLinus Torvalds out:
15401da177e4SLinus Torvalds 	in_dev_put(in_dev);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
15441da177e4SLinus Torvalds {
1545ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
15461da177e4SLinus Torvalds 	unsigned long now;
15471da177e4SLinus Torvalds 	int code;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	switch (rt->u.dst.error) {
15501da177e4SLinus Torvalds 		case EINVAL:
15511da177e4SLinus Torvalds 		default:
15521da177e4SLinus Torvalds 			goto out;
15531da177e4SLinus Torvalds 		case EHOSTUNREACH:
15541da177e4SLinus Torvalds 			code = ICMP_HOST_UNREACH;
15551da177e4SLinus Torvalds 			break;
15561da177e4SLinus Torvalds 		case ENETUNREACH:
15571da177e4SLinus Torvalds 			code = ICMP_NET_UNREACH;
15587c73a6faSPavel Emelyanov 			IP_INC_STATS_BH(dev_net(rt->u.dst.dev),
15597c73a6faSPavel Emelyanov 					IPSTATS_MIB_INNOROUTES);
15601da177e4SLinus Torvalds 			break;
15611da177e4SLinus Torvalds 		case EACCES:
15621da177e4SLinus Torvalds 			code = ICMP_PKT_FILTERED;
15631da177e4SLinus Torvalds 			break;
15641da177e4SLinus Torvalds 	}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	now = jiffies;
15671da177e4SLinus Torvalds 	rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
15681da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens > ip_rt_error_burst)
15691da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = ip_rt_error_burst;
15701da177e4SLinus Torvalds 	rt->u.dst.rate_last = now;
15711da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
15721da177e4SLinus Torvalds 		rt->u.dst.rate_tokens -= ip_rt_error_cost;
15731da177e4SLinus Torvalds 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
15741da177e4SLinus Torvalds 	}
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds out:	kfree_skb(skb);
15771da177e4SLinus Torvalds 	return 0;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds /*
15811da177e4SLinus Torvalds  *	The last two values are not from the RFC but
15821da177e4SLinus Torvalds  *	are needed for AMPRnet AX.25 paths.
15831da177e4SLinus Torvalds  */
15841da177e4SLinus Torvalds 
15859b5b5cffSArjan van de Ven static const unsigned short mtu_plateau[] =
15861da177e4SLinus Torvalds {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
15871da177e4SLinus Torvalds 
15885969f71dSStephen Hemminger static inline unsigned short guess_mtu(unsigned short old_mtu)
15891da177e4SLinus Torvalds {
15901da177e4SLinus Torvalds 	int i;
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
15931da177e4SLinus Torvalds 		if (old_mtu > mtu_plateau[i])
15941da177e4SLinus Torvalds 			return mtu_plateau[i];
15951da177e4SLinus Torvalds 	return 68;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
1598b5921910SDenis V. Lunev unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph,
15990010e465STimo Teras 				 unsigned short new_mtu,
16000010e465STimo Teras 				 struct net_device *dev)
16011da177e4SLinus Torvalds {
16020010e465STimo Teras 	int i, k;
16031da177e4SLinus Torvalds 	unsigned short old_mtu = ntohs(iph->tot_len);
16041da177e4SLinus Torvalds 	struct rtable *rth;
16050010e465STimo Teras 	int  ikeys[2] = { dev->ifindex, 0 };
1606e448515cSAl Viro 	__be32  skeys[2] = { iph->saddr, 0, };
1607e448515cSAl Viro 	__be32  daddr = iph->daddr;
16081da177e4SLinus Torvalds 	unsigned short est_mtu = 0;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
16111da177e4SLinus Torvalds 		return 0;
16121da177e4SLinus Torvalds 
16130010e465STimo Teras 	for (k = 0; k < 2; k++) {
16141da177e4SLinus Torvalds 		for (i = 0; i < 2; i++) {
1615b00180deSDenis V. Lunev 			unsigned hash = rt_hash(daddr, skeys[i], ikeys[k],
1616e84f84f2SDenis V. Lunev 						rt_genid(net));
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 			rcu_read_lock();
16191da177e4SLinus Torvalds 			for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
1620093c2ca4SEric Dumazet 			     rth = rcu_dereference(rth->u.dst.rt_next)) {
16211da177e4SLinus Torvalds 				unsigned short mtu = new_mtu;
16221da177e4SLinus Torvalds 
16230010e465STimo Teras 				if (rth->fl.fl4_dst != daddr ||
16240010e465STimo Teras 				    rth->fl.fl4_src != skeys[i] ||
16250010e465STimo Teras 				    rth->rt_dst != daddr ||
16260010e465STimo Teras 				    rth->rt_src != iph->saddr ||
16270010e465STimo Teras 				    rth->fl.oif != ikeys[k] ||
16280010e465STimo Teras 				    rth->fl.iif != 0 ||
16290010e465STimo Teras 				    dst_metric_locked(&rth->u.dst, RTAX_MTU) ||
16300010e465STimo Teras 				    !net_eq(dev_net(rth->u.dst.dev), net) ||
16316c3b8fc6SHugh Dickins 				    rt_is_expired(rth))
16320010e465STimo Teras 					continue;
16330010e465STimo Teras 
16341da177e4SLinus Torvalds 				if (new_mtu < 68 || new_mtu >= old_mtu) {
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 					/* BSD 4.2 compatibility hack :-( */
16371da177e4SLinus Torvalds 					if (mtu == 0 &&
16386d273f8dSRami Rosen 					    old_mtu >= dst_mtu(&rth->u.dst) &&
16391da177e4SLinus Torvalds 					    old_mtu >= 68 + (iph->ihl << 2))
16401da177e4SLinus Torvalds 						old_mtu -= iph->ihl << 2;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 					mtu = guess_mtu(old_mtu);
16431da177e4SLinus Torvalds 				}
16446d273f8dSRami Rosen 				if (mtu <= dst_mtu(&rth->u.dst)) {
16456d273f8dSRami Rosen 					if (mtu < dst_mtu(&rth->u.dst)) {
16461da177e4SLinus Torvalds 						dst_confirm(&rth->u.dst);
16471da177e4SLinus Torvalds 						if (mtu < ip_rt_min_pmtu) {
16481da177e4SLinus Torvalds 							mtu = ip_rt_min_pmtu;
16491da177e4SLinus Torvalds 							rth->u.dst.metrics[RTAX_LOCK-1] |=
16501da177e4SLinus Torvalds 								(1 << RTAX_MTU);
16511da177e4SLinus Torvalds 						}
16521da177e4SLinus Torvalds 						rth->u.dst.metrics[RTAX_MTU-1] = mtu;
16531da177e4SLinus Torvalds 						dst_set_expires(&rth->u.dst,
16541da177e4SLinus Torvalds 							ip_rt_mtu_expires);
16551da177e4SLinus Torvalds 					}
16561da177e4SLinus Torvalds 					est_mtu = mtu;
16571da177e4SLinus Torvalds 				}
16581da177e4SLinus Torvalds 			}
16591da177e4SLinus Torvalds 			rcu_read_unlock();
16601da177e4SLinus Torvalds 		}
16610010e465STimo Teras 	}
16621da177e4SLinus Torvalds 	return est_mtu ? : new_mtu;
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
16661da177e4SLinus Torvalds {
16676d273f8dSRami Rosen 	if (dst_mtu(dst) > mtu && mtu >= 68 &&
16681da177e4SLinus Torvalds 	    !(dst_metric_locked(dst, RTAX_MTU))) {
16691da177e4SLinus Torvalds 		if (mtu < ip_rt_min_pmtu) {
16701da177e4SLinus Torvalds 			mtu = ip_rt_min_pmtu;
16711da177e4SLinus Torvalds 			dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
16721da177e4SLinus Torvalds 		}
16731da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
16741da177e4SLinus Torvalds 		dst_set_expires(dst, ip_rt_mtu_expires);
16758d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	return NULL;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
16871da177e4SLinus Torvalds 	struct inet_peer *peer = rt->peer;
16881da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	if (peer) {
16911da177e4SLinus Torvalds 		rt->peer = NULL;
16921da177e4SLinus Torvalds 		inet_putpeer(peer);
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	if (idev) {
16961da177e4SLinus Torvalds 		rt->idev = NULL;
16971da177e4SLinus Torvalds 		in_dev_put(idev);
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
17021da177e4SLinus Torvalds 			    int how)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
17051da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
1706c346dca1SYOSHIFUJI Hideaki 	if (dev != dev_net(dev)->loopback_dev && idev && idev->dev == dev) {
17075a3e55d6SDenis V. Lunev 		struct in_device *loopback_idev =
1708c346dca1SYOSHIFUJI Hideaki 			in_dev_get(dev_net(dev)->loopback_dev);
17091da177e4SLinus Torvalds 		if (loopback_idev) {
17101da177e4SLinus Torvalds 			rt->idev = loopback_idev;
17111da177e4SLinus Torvalds 			in_dev_put(idev);
17121da177e4SLinus Torvalds 		}
17131da177e4SLinus Torvalds 	}
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb)
17171da177e4SLinus Torvalds {
17181da177e4SLinus Torvalds 	struct rtable *rt;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
17211da177e4SLinus Torvalds 
1722ee6b9673SEric Dumazet 	rt = skb->rtable;
17231da177e4SLinus Torvalds 	if (rt)
17241da177e4SLinus Torvalds 		dst_set_expires(&rt->u.dst, 0);
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb)
17281da177e4SLinus Torvalds {
1729673d57e7SHarvey Harrison 	printk(KERN_DEBUG "ip_rt_bug: %pI4 -> %pI4, %s\n",
1730673d57e7SHarvey Harrison 		&ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
17311da177e4SLinus Torvalds 		skb->dev ? skb->dev->name : "?");
17321da177e4SLinus Torvalds 	kfree_skb(skb);
17331da177e4SLinus Torvalds 	return 0;
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds /*
17371da177e4SLinus Torvalds    We do not cache source address of outgoing interface,
17381da177e4SLinus Torvalds    because it is used only by IP RR, TS and SRR options,
17391da177e4SLinus Torvalds    so that it out of fast path.
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds    BTW remember: "addr" is allowed to be not aligned
17421da177e4SLinus Torvalds    in IP options!
17431da177e4SLinus Torvalds  */
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds void ip_rt_get_source(u8 *addr, struct rtable *rt)
17461da177e4SLinus Torvalds {
1747a61ced5dSAl Viro 	__be32 src;
17481da177e4SLinus Torvalds 	struct fib_result res;
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	if (rt->fl.iif == 0)
17511da177e4SLinus Torvalds 		src = rt->rt_src;
1752c346dca1SYOSHIFUJI Hideaki 	else if (fib_lookup(dev_net(rt->u.dst.dev), &rt->fl, &res) == 0) {
17531da177e4SLinus Torvalds 		src = FIB_RES_PREFSRC(res);
17541da177e4SLinus Torvalds 		fib_res_put(&res);
17551da177e4SLinus Torvalds 	} else
17561da177e4SLinus Torvalds 		src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
17571da177e4SLinus Torvalds 					RT_SCOPE_UNIVERSE);
17581da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
17621da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
17631da177e4SLinus Torvalds {
17641da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF))
17651da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF;
17661da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF0000))
17671da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF0000;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds #endif
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
17721da177e4SLinus Torvalds {
17731da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	if (fi) {
17761da177e4SLinus Torvalds 		if (FIB_RES_GW(*res) &&
17771da177e4SLinus Torvalds 		    FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
17781da177e4SLinus Torvalds 			rt->rt_gateway = FIB_RES_GW(*res);
17791da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, fi->fib_metrics,
17801da177e4SLinus Torvalds 		       sizeof(rt->u.dst.metrics));
17811da177e4SLinus Torvalds 		if (fi->fib_mtu == 0) {
17821da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
17830bbeafd0SSatoru SATOH 			if (dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
17841da177e4SLinus Torvalds 			    rt->rt_gateway != rt->rt_dst &&
17851da177e4SLinus Torvalds 			    rt->u.dst.dev->mtu > 576)
17861da177e4SLinus Torvalds 				rt->u.dst.metrics[RTAX_MTU-1] = 576;
17871da177e4SLinus Torvalds 		}
17881da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
17891da177e4SLinus Torvalds 		rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
17901da177e4SLinus Torvalds #endif
17911da177e4SLinus Torvalds 	} else
17921da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
17931da177e4SLinus Torvalds 
17945ffc02a1SSatoru SATOH 	if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
17951da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
17966d273f8dSRami Rosen 	if (dst_mtu(&rt->u.dst) > IP_MAX_MTU)
17971da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
17985ffc02a1SSatoru SATOH 	if (dst_metric(&rt->u.dst, RTAX_ADVMSS) == 0)
17991da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
18001da177e4SLinus Torvalds 				       ip_rt_min_advmss);
18015ffc02a1SSatoru SATOH 	if (dst_metric(&rt->u.dst, RTAX_ADVMSS) > 65535 - 40)
18021da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
18051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
18061da177e4SLinus Torvalds 	set_class_tag(rt, fib_rules_tclass(res));
18071da177e4SLinus Torvalds #endif
18081da177e4SLinus Torvalds 	set_class_tag(rt, itag);
18091da177e4SLinus Torvalds #endif
18101da177e4SLinus Torvalds 	rt->rt_type = res->type;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
18139e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
18141da177e4SLinus Torvalds 				u8 tos, struct net_device *dev, int our)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	unsigned hash;
18171da177e4SLinus Torvalds 	struct rtable *rth;
1818a61ced5dSAl Viro 	__be32 spec_dst;
18191da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
18201da177e4SLinus Torvalds 	u32 itag = 0;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	/* Primary sanity checks. */
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	if (in_dev == NULL)
18251da177e4SLinus Torvalds 		return -EINVAL;
18261da177e4SLinus Torvalds 
18271e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1828f97c1e0cSJoe Perches 	    ipv4_is_loopback(saddr) || skb->protocol != htons(ETH_P_IP))
18291da177e4SLinus Torvalds 		goto e_inval;
18301da177e4SLinus Torvalds 
1831f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr)) {
1832f97c1e0cSJoe Perches 		if (!ipv4_is_local_multicast(daddr))
18331da177e4SLinus Torvalds 			goto e_inval;
18341da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
18351da177e4SLinus Torvalds 	} else if (fib_validate_source(saddr, 0, tos, 0,
18361da177e4SLinus Torvalds 					dev, &spec_dst, &itag) < 0)
18371da177e4SLinus Torvalds 		goto e_inval;
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
18401da177e4SLinus Torvalds 	if (!rth)
18411da177e4SLinus Torvalds 		goto e_nobufs;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
18461da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
184742f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
18481da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
18491da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
18501da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
18511da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
185247dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
18531da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
18541da177e4SLinus Torvalds 	rth->rt_src	= saddr;
18551da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
18561da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
18571da177e4SLinus Torvalds #endif
18581da177e4SLinus Torvalds 	rth->rt_iif	=
18591da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
18602774c7abSEric W. Biederman 	rth->u.dst.dev	= init_net.loopback_dev;
18611da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
18621da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
18631da177e4SLinus Torvalds 	rth->fl.oif	= 0;
18641da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
18651da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
1866e84f84f2SDenis V. Lunev 	rth->rt_genid	= rt_genid(dev_net(dev));
18671da177e4SLinus Torvalds 	rth->rt_flags	= RTCF_MULTICAST;
186829e75252SEric Dumazet 	rth->rt_type	= RTN_MULTICAST;
18691da177e4SLinus Torvalds 	if (our) {
18701da177e4SLinus Torvalds 		rth->u.dst.input= ip_local_deliver;
18711da177e4SLinus Torvalds 		rth->rt_flags |= RTCF_LOCAL;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1875f97c1e0cSJoe Perches 	if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
18761da177e4SLinus Torvalds 		rth->u.dst.input = ip_mr_input;
18771da177e4SLinus Torvalds #endif
18781da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	in_dev_put(in_dev);
1881e84f84f2SDenis V. Lunev 	hash = rt_hash(daddr, saddr, dev->ifindex, rt_genid(dev_net(dev)));
1882ee6b9673SEric Dumazet 	return rt_intern_hash(hash, rth, &skb->rtable);
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds e_nobufs:
18851da177e4SLinus Torvalds 	in_dev_put(in_dev);
18861da177e4SLinus Torvalds 	return -ENOBUFS;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds e_inval:
18891da177e4SLinus Torvalds 	in_dev_put(in_dev);
18901da177e4SLinus Torvalds 	return -EINVAL;
18911da177e4SLinus Torvalds }
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
18951da177e4SLinus Torvalds 				     struct in_device *in_dev,
18961da177e4SLinus Torvalds 				     struct sk_buff *skb,
18979e12bb22SAl Viro 				     __be32 daddr,
18989e12bb22SAl Viro 				     __be32 saddr)
18991da177e4SLinus Torvalds {
19001da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
19011da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
19021da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
19031da177e4SLinus Torvalds 		/*
19041da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
19051da177e4SLinus Torvalds 		 *	the only hint is MAC header.
19061da177e4SLinus Torvalds 		 */
1907673d57e7SHarvey Harrison 		printk(KERN_WARNING "martian source %pI4 from %pI4, on dev %s\n",
1908673d57e7SHarvey Harrison 			&daddr, &saddr, dev->name);
190998e399f8SArnaldo Carvalho de Melo 		if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
19101da177e4SLinus Torvalds 			int i;
191198e399f8SArnaldo Carvalho de Melo 			const unsigned char *p = skb_mac_header(skb);
19121da177e4SLinus Torvalds 			printk(KERN_WARNING "ll header: ");
19131da177e4SLinus Torvalds 			for (i = 0; i < dev->hard_header_len; i++, p++) {
19141da177e4SLinus Torvalds 				printk("%02x", *p);
19151da177e4SLinus Torvalds 				if (i < (dev->hard_header_len - 1))
19161da177e4SLinus Torvalds 					printk(":");
19171da177e4SLinus Torvalds 			}
19181da177e4SLinus Torvalds 			printk("\n");
19191da177e4SLinus Torvalds 		}
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds #endif
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19245969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb,
19251da177e4SLinus Torvalds 			   struct fib_result *res,
19261da177e4SLinus Torvalds 			   struct in_device *in_dev,
19279e12bb22SAl Viro 			   __be32 daddr, __be32 saddr, u32 tos,
19281da177e4SLinus Torvalds 			   struct rtable **result)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	struct rtable *rth;
19321da177e4SLinus Torvalds 	int err;
19331da177e4SLinus Torvalds 	struct in_device *out_dev;
19341da177e4SLinus Torvalds 	unsigned flags = 0;
1935d9c9df8cSAl Viro 	__be32 spec_dst;
1936d9c9df8cSAl Viro 	u32 itag;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	/* get a working reference to the output device */
19391da177e4SLinus Torvalds 	out_dev = in_dev_get(FIB_RES_DEV(*res));
19401da177e4SLinus Torvalds 	if (out_dev == NULL) {
19411da177e4SLinus Torvalds 		if (net_ratelimit())
19421da177e4SLinus Torvalds 			printk(KERN_CRIT "Bug in ip_route_input" \
19431da177e4SLinus Torvalds 			       "_slow(). Please, report\n");
19441da177e4SLinus Torvalds 		return -EINVAL;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
19491da177e4SLinus Torvalds 				  in_dev->dev, &spec_dst, &itag);
19501da177e4SLinus Torvalds 	if (err < 0) {
19511da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
19521da177e4SLinus Torvalds 					 saddr);
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 		err = -EINVAL;
19551da177e4SLinus Torvalds 		goto cleanup;
19561da177e4SLinus Torvalds 	}
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	if (err)
19591da177e4SLinus Torvalds 		flags |= RTCF_DIRECTSRC;
19601da177e4SLinus Torvalds 
196151b77caeSThomas Graf 	if (out_dev == in_dev && err &&
19621da177e4SLinus Torvalds 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
19631da177e4SLinus Torvalds 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
19641da177e4SLinus Torvalds 		flags |= RTCF_DOREDIRECT;
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
19671da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
19681da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
19691da177e4SLinus Torvalds 		 */
1970cb7928a5SRami Rosen 		if (out_dev == in_dev) {
19711da177e4SLinus Torvalds 			err = -EINVAL;
19721da177e4SLinus Torvalds 			goto cleanup;
19731da177e4SLinus Torvalds 		}
19741da177e4SLinus Torvalds 	}
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
19781da177e4SLinus Torvalds 	if (!rth) {
19791da177e4SLinus Torvalds 		err = -ENOBUFS;
19801da177e4SLinus Torvalds 		goto cleanup;
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
1983ce723d8eSJulian Anastasov 	atomic_set(&rth->u.dst.__refcnt, 1);
19841da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
198542f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
19861da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
198742f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(out_dev, NOXFRM))
19881da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
19891da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
19901da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
19911da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
199247dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
19931da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
19941da177e4SLinus Torvalds 	rth->rt_src	= saddr;
19951da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
19961da177e4SLinus Torvalds 	rth->rt_iif 	=
19971da177e4SLinus Torvalds 		rth->fl.iif	= in_dev->dev->ifindex;
19981da177e4SLinus Torvalds 	rth->u.dst.dev	= (out_dev)->dev;
19991da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
20001da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
20011da177e4SLinus Torvalds 	rth->fl.oif 	= 0;
20021da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	rth->u.dst.input = ip_forward;
20051da177e4SLinus Torvalds 	rth->u.dst.output = ip_output;
2006e84f84f2SDenis V. Lunev 	rth->rt_genid = rt_genid(dev_net(rth->u.dst.dev));
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, itag);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	rth->rt_flags = flags;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	*result = rth;
20131da177e4SLinus Torvalds 	err = 0;
20141da177e4SLinus Torvalds  cleanup:
20151da177e4SLinus Torvalds 	/* release the working reference to the output device */
20161da177e4SLinus Torvalds 	in_dev_put(out_dev);
20171da177e4SLinus Torvalds 	return err;
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
20205969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb,
20211da177e4SLinus Torvalds 			    struct fib_result *res,
20221da177e4SLinus Torvalds 			    const struct flowi *fl,
20231da177e4SLinus Torvalds 			    struct in_device *in_dev,
20249e12bb22SAl Viro 			    __be32 daddr, __be32 saddr, u32 tos)
20251da177e4SLinus Torvalds {
20267abaa27cSChuck Short 	struct rtable* rth = NULL;
20271da177e4SLinus Torvalds 	int err;
20281da177e4SLinus Torvalds 	unsigned hash;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
20311da177e4SLinus Torvalds 	if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
20321da177e4SLinus Torvalds 		fib_select_multipath(fl, res);
20331da177e4SLinus Torvalds #endif
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	/* create a routing cache entry */
20361da177e4SLinus Torvalds 	err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
20371da177e4SLinus Torvalds 	if (err)
20381da177e4SLinus Torvalds 		return err;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	/* put it into the cache */
2041e84f84f2SDenis V. Lunev 	hash = rt_hash(daddr, saddr, fl->iif,
2042e84f84f2SDenis V. Lunev 		       rt_genid(dev_net(rth->u.dst.dev)));
2043ee6b9673SEric Dumazet 	return rt_intern_hash(hash, rth, &skb->rtable);
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds /*
20471da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
20481da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
20491da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
20501da177e4SLinus Torvalds  *
20511da177e4SLinus Torvalds  *	Such approach solves two big problems:
20521da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
20531da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
20541da177e4SLinus Torvalds  */
20551da177e4SLinus Torvalds 
20569e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
20571da177e4SLinus Torvalds 			       u8 tos, struct net_device *dev)
20581da177e4SLinus Torvalds {
20591da177e4SLinus Torvalds 	struct fib_result res;
20601da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
20611da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
20621da177e4SLinus Torvalds 				      { .daddr = daddr,
20631da177e4SLinus Torvalds 					.saddr = saddr,
20641da177e4SLinus Torvalds 					.tos = tos,
20651da177e4SLinus Torvalds 					.scope = RT_SCOPE_UNIVERSE,
20661da177e4SLinus Torvalds 				      } },
206747dcf0cbSThomas Graf 			    .mark = skb->mark,
20681da177e4SLinus Torvalds 			    .iif = dev->ifindex };
20691da177e4SLinus Torvalds 	unsigned	flags = 0;
20701da177e4SLinus Torvalds 	u32		itag = 0;
20711da177e4SLinus Torvalds 	struct rtable * rth;
20721da177e4SLinus Torvalds 	unsigned	hash;
20739e12bb22SAl Viro 	__be32		spec_dst;
20741da177e4SLinus Torvalds 	int		err = -EINVAL;
20751da177e4SLinus Torvalds 	int		free_res = 0;
2076c346dca1SYOSHIFUJI Hideaki 	struct net    * net = dev_net(dev);
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	/* IP on this device is disabled. */
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	if (!in_dev)
20811da177e4SLinus Torvalds 		goto out;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
20841da177e4SLinus Torvalds 	   by fib_lookup.
20851da177e4SLinus Torvalds 	 */
20861da177e4SLinus Torvalds 
20871e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
2088f97c1e0cSJoe Perches 	    ipv4_is_loopback(saddr))
20891da177e4SLinus Torvalds 		goto martian_source;
20901da177e4SLinus Torvalds 
2091e448515cSAl Viro 	if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
20921da177e4SLinus Torvalds 		goto brd_input;
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
20951da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
20961da177e4SLinus Torvalds 	 */
2097f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
20981da177e4SLinus Torvalds 		goto martian_source;
20991da177e4SLinus Torvalds 
21001e637c74SJan Engelhardt 	if (ipv4_is_lbcast(daddr) || ipv4_is_zeronet(daddr) ||
2101f97c1e0cSJoe Perches 	    ipv4_is_loopback(daddr))
21021da177e4SLinus Torvalds 		goto martian_destination;
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	/*
21051da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
21061da177e4SLinus Torvalds 	 */
210784a885f4SDenis V. Lunev 	if ((err = fib_lookup(net, &fl, &res)) != 0) {
21081da177e4SLinus Torvalds 		if (!IN_DEV_FORWARD(in_dev))
21092c2910a4SDietmar Eggemann 			goto e_hostunreach;
21101da177e4SLinus Torvalds 		goto no_route;
21111da177e4SLinus Torvalds 	}
21121da177e4SLinus Torvalds 	free_res = 1;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_tot);
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	if (res.type == RTN_BROADCAST)
21171da177e4SLinus Torvalds 		goto brd_input;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
21201da177e4SLinus Torvalds 		int result;
21211da177e4SLinus Torvalds 		result = fib_validate_source(saddr, daddr, tos,
212284a885f4SDenis V. Lunev 					     net->loopback_dev->ifindex,
21231da177e4SLinus Torvalds 					     dev, &spec_dst, &itag);
21241da177e4SLinus Torvalds 		if (result < 0)
21251da177e4SLinus Torvalds 			goto martian_source;
21261da177e4SLinus Torvalds 		if (result)
21271da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
21281da177e4SLinus Torvalds 		spec_dst = daddr;
21291da177e4SLinus Torvalds 		goto local_input;
21301da177e4SLinus Torvalds 	}
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	if (!IN_DEV_FORWARD(in_dev))
21332c2910a4SDietmar Eggemann 		goto e_hostunreach;
21341da177e4SLinus Torvalds 	if (res.type != RTN_UNICAST)
21351da177e4SLinus Torvalds 		goto martian_destination;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
21381da177e4SLinus Torvalds done:
21391da177e4SLinus Torvalds 	in_dev_put(in_dev);
21401da177e4SLinus Torvalds 	if (free_res)
21411da177e4SLinus Torvalds 		fib_res_put(&res);
21421da177e4SLinus Torvalds out:	return err;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds brd_input:
21451da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
21461da177e4SLinus Torvalds 		goto e_inval;
21471da177e4SLinus Torvalds 
2148f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
21491da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
21501da177e4SLinus Torvalds 	else {
21511da177e4SLinus Torvalds 		err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
21521da177e4SLinus Torvalds 					  &itag);
21531da177e4SLinus Torvalds 		if (err < 0)
21541da177e4SLinus Torvalds 			goto martian_source;
21551da177e4SLinus Torvalds 		if (err)
21561da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
21571da177e4SLinus Torvalds 	}
21581da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
21591da177e4SLinus Torvalds 	res.type = RTN_BROADCAST;
21601da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds local_input:
21631da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
21641da177e4SLinus Torvalds 	if (!rth)
21651da177e4SLinus Torvalds 		goto e_nobufs;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
2168e84f84f2SDenis V. Lunev 	rth->rt_genid = rt_genid(net);
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
21711da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
217242f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
21731da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
21741da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
21751da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
21761da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
217747dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
21781da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
21791da177e4SLinus Torvalds 	rth->rt_src	= saddr;
21801da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
21811da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
21821da177e4SLinus Torvalds #endif
21831da177e4SLinus Torvalds 	rth->rt_iif	=
21841da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
218584a885f4SDenis V. Lunev 	rth->u.dst.dev	= net->loopback_dev;
21861da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
21871da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
21881da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
21891da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
21901da177e4SLinus Torvalds 	rth->u.dst.input= ip_local_deliver;
21911da177e4SLinus Torvalds 	rth->rt_flags 	= flags|RTCF_LOCAL;
21921da177e4SLinus Torvalds 	if (res.type == RTN_UNREACHABLE) {
21931da177e4SLinus Torvalds 		rth->u.dst.input= ip_error;
21941da177e4SLinus Torvalds 		rth->u.dst.error= -err;
21951da177e4SLinus Torvalds 		rth->rt_flags 	&= ~RTCF_LOCAL;
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 	rth->rt_type	= res.type;
2198e84f84f2SDenis V. Lunev 	hash = rt_hash(daddr, saddr, fl.iif, rt_genid(net));
2199ee6b9673SEric Dumazet 	err = rt_intern_hash(hash, rth, &skb->rtable);
22001da177e4SLinus Torvalds 	goto done;
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds no_route:
22031da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
22041da177e4SLinus Torvalds 	spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
22051da177e4SLinus Torvalds 	res.type = RTN_UNREACHABLE;
22067f53878dSMitsuru Chinen 	if (err == -ESRCH)
22077f53878dSMitsuru Chinen 		err = -ENETUNREACH;
22081da177e4SLinus Torvalds 	goto local_input;
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	/*
22111da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
22121da177e4SLinus Torvalds 	 */
22131da177e4SLinus Torvalds martian_destination:
22141da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
22151da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
22161da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
2217673d57e7SHarvey Harrison 		printk(KERN_WARNING "martian destination %pI4 from %pI4, dev %s\n",
2218673d57e7SHarvey Harrison 			&daddr, &saddr, dev->name);
22191da177e4SLinus Torvalds #endif
22202c2910a4SDietmar Eggemann 
22212c2910a4SDietmar Eggemann e_hostunreach:
22222c2910a4SDietmar Eggemann 	err = -EHOSTUNREACH;
22232c2910a4SDietmar Eggemann 	goto done;
22242c2910a4SDietmar Eggemann 
22251da177e4SLinus Torvalds e_inval:
22261da177e4SLinus Torvalds 	err = -EINVAL;
22271da177e4SLinus Torvalds 	goto done;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds e_nobufs:
22301da177e4SLinus Torvalds 	err = -ENOBUFS;
22311da177e4SLinus Torvalds 	goto done;
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds martian_source:
22341da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
22351da177e4SLinus Torvalds 	goto e_inval;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22389e12bb22SAl Viro int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
22391da177e4SLinus Torvalds 		   u8 tos, struct net_device *dev)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct rtable * rth;
22421da177e4SLinus Torvalds 	unsigned	hash;
22431da177e4SLinus Torvalds 	int iif = dev->ifindex;
2244b5921910SDenis V. Lunev 	struct net *net;
22451da177e4SLinus Torvalds 
2246c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
22471080d709SNeil Horman 
22481080d709SNeil Horman 	if (!rt_caching(net))
22491080d709SNeil Horman 		goto skip_cache;
22501080d709SNeil Horman 
22511da177e4SLinus Torvalds 	tos &= IPTOS_RT_MASK;
2252e84f84f2SDenis V. Lunev 	hash = rt_hash(daddr, saddr, iif, rt_genid(net));
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	rcu_read_lock();
22551da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2256093c2ca4SEric Dumazet 	     rth = rcu_dereference(rth->u.dst.rt_next)) {
2257c0b8c32bSStephen Hemminger 		if (((rth->fl.fl4_dst ^ daddr) |
2258c0b8c32bSStephen Hemminger 		     (rth->fl.fl4_src ^ saddr) |
2259c0b8c32bSStephen Hemminger 		     (rth->fl.iif ^ iif) |
2260c0b8c32bSStephen Hemminger 		     rth->fl.oif |
2261c0b8c32bSStephen Hemminger 		     (rth->fl.fl4_tos ^ tos)) == 0 &&
226247dcf0cbSThomas Graf 		    rth->fl.mark == skb->mark &&
2263878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(rth->u.dst.dev), net) &&
2264e84f84f2SDenis V. Lunev 		    !rt_is_expired(rth)) {
226503f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, jiffies);
22661da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(in_hit);
22671da177e4SLinus Torvalds 			rcu_read_unlock();
2268ee6b9673SEric Dumazet 			skb->rtable = rth;
22691da177e4SLinus Torvalds 			return 0;
22701da177e4SLinus Torvalds 		}
22711da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(in_hlist_search);
22721da177e4SLinus Torvalds 	}
22731da177e4SLinus Torvalds 	rcu_read_unlock();
22741da177e4SLinus Torvalds 
22751080d709SNeil Horman skip_cache:
22761da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
22771da177e4SLinus Torvalds 	   The problem was that too many Ethernet cards have broken/missing
22781da177e4SLinus Torvalds 	   hardware multicast filters :-( As result the host on multicasting
22791da177e4SLinus Torvalds 	   network acquires a lot of useless route cache entries, sort of
22801da177e4SLinus Torvalds 	   SDR messages from all the world. Now we try to get rid of them.
22811da177e4SLinus Torvalds 	   Really, provided software IP multicast filter is organized
22821da177e4SLinus Torvalds 	   reasonably (at least, hashed), it does not result in a slowdown
22831da177e4SLinus Torvalds 	   comparing with route cache reject entries.
22841da177e4SLinus Torvalds 	   Note, that multicast routers are not affected, because
22851da177e4SLinus Torvalds 	   route cache entry is created eventually.
22861da177e4SLinus Torvalds 	 */
2287f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
22881da177e4SLinus Torvalds 		struct in_device *in_dev;
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 		rcu_read_lock();
2291e5ed6399SHerbert Xu 		if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
22921da177e4SLinus Torvalds 			int our = ip_check_mc(in_dev, daddr, saddr,
2293eddc9ec5SArnaldo Carvalho de Melo 				ip_hdr(skb)->protocol);
22941da177e4SLinus Torvalds 			if (our
22951da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2296f97c1e0cSJoe Perches 			    || (!ipv4_is_local_multicast(daddr) &&
2297f97c1e0cSJoe Perches 				IN_DEV_MFORWARD(in_dev))
22981da177e4SLinus Torvalds #endif
22991da177e4SLinus Torvalds 			    ) {
23001da177e4SLinus Torvalds 				rcu_read_unlock();
23011da177e4SLinus Torvalds 				return ip_route_input_mc(skb, daddr, saddr,
23021da177e4SLinus Torvalds 							 tos, dev, our);
23031da177e4SLinus Torvalds 			}
23041da177e4SLinus Torvalds 		}
23051da177e4SLinus Torvalds 		rcu_read_unlock();
23061da177e4SLinus Torvalds 		return -EINVAL;
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 	return ip_route_input_slow(skb, daddr, saddr, tos, dev);
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds 
23115969f71dSStephen Hemminger static int __mkroute_output(struct rtable **result,
23121da177e4SLinus Torvalds 			    struct fib_result *res,
23131da177e4SLinus Torvalds 			    const struct flowi *fl,
23141da177e4SLinus Torvalds 			    const struct flowi *oldflp,
23151da177e4SLinus Torvalds 			    struct net_device *dev_out,
23161da177e4SLinus Torvalds 			    unsigned flags)
23171da177e4SLinus Torvalds {
23181da177e4SLinus Torvalds 	struct rtable *rth;
23191da177e4SLinus Torvalds 	struct in_device *in_dev;
23201da177e4SLinus Torvalds 	u32 tos = RT_FL_TOS(oldflp);
23211da177e4SLinus Torvalds 	int err = 0;
23221da177e4SLinus Torvalds 
2323f97c1e0cSJoe Perches 	if (ipv4_is_loopback(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
23241da177e4SLinus Torvalds 		return -EINVAL;
23251da177e4SLinus Torvalds 
2326e448515cSAl Viro 	if (fl->fl4_dst == htonl(0xFFFFFFFF))
23271da177e4SLinus Torvalds 		res->type = RTN_BROADCAST;
2328f97c1e0cSJoe Perches 	else if (ipv4_is_multicast(fl->fl4_dst))
23291da177e4SLinus Torvalds 		res->type = RTN_MULTICAST;
23301e637c74SJan Engelhardt 	else if (ipv4_is_lbcast(fl->fl4_dst) || ipv4_is_zeronet(fl->fl4_dst))
23311da177e4SLinus Torvalds 		return -EINVAL;
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
23341da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	/* get work reference to inet device */
23371da177e4SLinus Torvalds 	in_dev = in_dev_get(dev_out);
23381da177e4SLinus Torvalds 	if (!in_dev)
23391da177e4SLinus Torvalds 		return -EINVAL;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	if (res->type == RTN_BROADCAST) {
23421da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
23431da177e4SLinus Torvalds 		if (res->fi) {
23441da177e4SLinus Torvalds 			fib_info_put(res->fi);
23451da177e4SLinus Torvalds 			res->fi = NULL;
23461da177e4SLinus Torvalds 		}
23471da177e4SLinus Torvalds 	} else if (res->type == RTN_MULTICAST) {
23481da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST|RTCF_LOCAL;
23491da177e4SLinus Torvalds 		if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
23501da177e4SLinus Torvalds 				 oldflp->proto))
23511da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
23521da177e4SLinus Torvalds 		/* If multicast route do not exist use
23531da177e4SLinus Torvalds 		   default one, but do not gateway in this case.
23541da177e4SLinus Torvalds 		   Yes, it is hack.
23551da177e4SLinus Torvalds 		 */
23561da177e4SLinus Torvalds 		if (res->fi && res->prefixlen < 4) {
23571da177e4SLinus Torvalds 			fib_info_put(res->fi);
23581da177e4SLinus Torvalds 			res->fi = NULL;
23591da177e4SLinus Torvalds 		}
23601da177e4SLinus Torvalds 	}
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
23641da177e4SLinus Torvalds 	if (!rth) {
23651da177e4SLinus Torvalds 		err = -ENOBUFS;
23661da177e4SLinus Torvalds 		goto cleanup;
23671da177e4SLinus Torvalds 	}
23681da177e4SLinus Torvalds 
2369ce723d8eSJulian Anastasov 	atomic_set(&rth->u.dst.__refcnt, 1);
23701da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
237142f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOXFRM))
23721da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
237342f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
23741da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds 	rth->fl.fl4_dst	= oldflp->fl4_dst;
23771da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
23781da177e4SLinus Torvalds 	rth->fl.fl4_src	= oldflp->fl4_src;
23791da177e4SLinus Torvalds 	rth->fl.oif	= oldflp->oif;
238047dcf0cbSThomas Graf 	rth->fl.mark    = oldflp->mark;
23811da177e4SLinus Torvalds 	rth->rt_dst	= fl->fl4_dst;
23821da177e4SLinus Torvalds 	rth->rt_src	= fl->fl4_src;
23831da177e4SLinus Torvalds 	rth->rt_iif	= oldflp->oif ? : dev_out->ifindex;
23841da177e4SLinus Torvalds 	/* get references to the devices that are to be hold by the routing
23851da177e4SLinus Torvalds 	   cache entry */
23861da177e4SLinus Torvalds 	rth->u.dst.dev	= dev_out;
23871da177e4SLinus Torvalds 	dev_hold(dev_out);
23881da177e4SLinus Torvalds 	rth->idev	= in_dev_get(dev_out);
23891da177e4SLinus Torvalds 	rth->rt_gateway = fl->fl4_dst;
23901da177e4SLinus Torvalds 	rth->rt_spec_dst= fl->fl4_src;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	rth->u.dst.output=ip_output;
2393e84f84f2SDenis V. Lunev 	rth->rt_genid = rt_genid(dev_net(dev_out));
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (flags & RTCF_LOCAL) {
23981da177e4SLinus Torvalds 		rth->u.dst.input = ip_local_deliver;
23991da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_dst;
24001da177e4SLinus Torvalds 	}
24011da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
24021da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_src;
24031da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
24041da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
24051da177e4SLinus Torvalds 			rth->u.dst.output = ip_mc_output;
24061da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
24071da177e4SLinus Torvalds 		}
24081da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
24091da177e4SLinus Torvalds 		if (res->type == RTN_MULTICAST) {
24101da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
2411f97c1e0cSJoe Perches 			    !ipv4_is_local_multicast(oldflp->fl4_dst)) {
24121da177e4SLinus Torvalds 				rth->u.dst.input = ip_mr_input;
24131da177e4SLinus Torvalds 				rth->u.dst.output = ip_mc_output;
24141da177e4SLinus Torvalds 			}
24151da177e4SLinus Torvalds 		}
24161da177e4SLinus Torvalds #endif
24171da177e4SLinus Torvalds 	}
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, 0);
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	rth->rt_flags = flags;
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	*result = rth;
24241da177e4SLinus Torvalds  cleanup:
24251da177e4SLinus Torvalds 	/* release work reference to inet device */
24261da177e4SLinus Torvalds 	in_dev_put(in_dev);
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	return err;
24291da177e4SLinus Torvalds }
24301da177e4SLinus Torvalds 
24315969f71dSStephen Hemminger static int ip_mkroute_output(struct rtable **rp,
24321da177e4SLinus Torvalds 			     struct fib_result *res,
24331da177e4SLinus Torvalds 			     const struct flowi *fl,
24341da177e4SLinus Torvalds 			     const struct flowi *oldflp,
24351da177e4SLinus Torvalds 			     struct net_device *dev_out,
24361da177e4SLinus Torvalds 			     unsigned flags)
24371da177e4SLinus Torvalds {
24387abaa27cSChuck Short 	struct rtable *rth = NULL;
24391da177e4SLinus Torvalds 	int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
24401da177e4SLinus Torvalds 	unsigned hash;
24411da177e4SLinus Torvalds 	if (err == 0) {
2442b00180deSDenis V. Lunev 		hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif,
2443e84f84f2SDenis V. Lunev 			       rt_genid(dev_net(dev_out)));
24441da177e4SLinus Torvalds 		err = rt_intern_hash(hash, rth, rp);
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds 	return err;
24481da177e4SLinus Torvalds }
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds /*
24511da177e4SLinus Torvalds  * Major route resolver routine.
24521da177e4SLinus Torvalds  */
24531da177e4SLinus Torvalds 
2454b40afd0eSDenis V. Lunev static int ip_route_output_slow(struct net *net, struct rtable **rp,
2455b40afd0eSDenis V. Lunev 				const struct flowi *oldflp)
24561da177e4SLinus Torvalds {
24571da177e4SLinus Torvalds 	u32 tos	= RT_FL_TOS(oldflp);
24581da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
24591da177e4SLinus Torvalds 				      { .daddr = oldflp->fl4_dst,
24601da177e4SLinus Torvalds 					.saddr = oldflp->fl4_src,
24611da177e4SLinus Torvalds 					.tos = tos & IPTOS_RT_MASK,
24621da177e4SLinus Torvalds 					.scope = ((tos & RTO_ONLINK) ?
24631da177e4SLinus Torvalds 						  RT_SCOPE_LINK :
24641da177e4SLinus Torvalds 						  RT_SCOPE_UNIVERSE),
24651da177e4SLinus Torvalds 				      } },
246647dcf0cbSThomas Graf 			    .mark = oldflp->mark,
2467b40afd0eSDenis V. Lunev 			    .iif = net->loopback_dev->ifindex,
24681da177e4SLinus Torvalds 			    .oif = oldflp->oif };
24691da177e4SLinus Torvalds 	struct fib_result res;
24701da177e4SLinus Torvalds 	unsigned flags = 0;
24711da177e4SLinus Torvalds 	struct net_device *dev_out = NULL;
24721da177e4SLinus Torvalds 	int free_res = 0;
24731da177e4SLinus Torvalds 	int err;
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 	res.fi		= NULL;
24771da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
24781da177e4SLinus Torvalds 	res.r		= NULL;
24791da177e4SLinus Torvalds #endif
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if (oldflp->fl4_src) {
24821da177e4SLinus Torvalds 		err = -EINVAL;
2483f97c1e0cSJoe Perches 		if (ipv4_is_multicast(oldflp->fl4_src) ||
24841e637c74SJan Engelhardt 		    ipv4_is_lbcast(oldflp->fl4_src) ||
2485f97c1e0cSJoe Perches 		    ipv4_is_zeronet(oldflp->fl4_src))
24861da177e4SLinus Torvalds 			goto out;
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
24891da177e4SLinus Torvalds 		   It was wrong for two reasons:
24901ab35276SDenis V. Lunev 		   1. ip_dev_find(net, saddr) can return wrong iface, if saddr
24911ab35276SDenis V. Lunev 		      is assigned to multiple interfaces.
24921da177e4SLinus Torvalds 		   2. Moreover, we are allowed to send packets with saddr
24931da177e4SLinus Torvalds 		      of another iface. --ANK
24941da177e4SLinus Torvalds 		 */
24951da177e4SLinus Torvalds 
2496f6c5d736SDavid S. Miller 		if (oldflp->oif == 0
2497f97c1e0cSJoe Perches 		    && (ipv4_is_multicast(oldflp->fl4_dst) ||
2498f97c1e0cSJoe Perches 			oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
2499a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2500a210d01aSJulian Anastasov 			dev_out = ip_dev_find(net, oldflp->fl4_src);
2501a210d01aSJulian Anastasov 			if (dev_out == NULL)
2502a210d01aSJulian Anastasov 				goto out;
2503a210d01aSJulian Anastasov 
25041da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
25051da177e4SLinus Torvalds 			   and limited broadcast via necessary interface
25061da177e4SLinus Torvalds 			   without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
25071da177e4SLinus Torvalds 			   This hack is not just for fun, it allows
25081da177e4SLinus Torvalds 			   vic,vat and friends to work.
25091da177e4SLinus Torvalds 			   They bind socket to loopback, set ttl to zero
25101da177e4SLinus Torvalds 			   and expect that it will work.
25111da177e4SLinus Torvalds 			   From the viewpoint of routing cache they are broken,
25121da177e4SLinus Torvalds 			   because we are not allowed to build multicast path
25131da177e4SLinus Torvalds 			   with loopback source addr (look, routing cache
25141da177e4SLinus Torvalds 			   cannot know, that ttl is zero, so that packet
25151da177e4SLinus Torvalds 			   will not leave this host and route is valid).
25161da177e4SLinus Torvalds 			   Luckily, this hack is good workaround.
25171da177e4SLinus Torvalds 			 */
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 			fl.oif = dev_out->ifindex;
25201da177e4SLinus Torvalds 			goto make_route;
25211da177e4SLinus Torvalds 		}
2522a210d01aSJulian Anastasov 
2523a210d01aSJulian Anastasov 		if (!(oldflp->flags & FLOWI_FLAG_ANYSRC)) {
2524a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2525a210d01aSJulian Anastasov 			dev_out = ip_dev_find(net, oldflp->fl4_src);
2526a210d01aSJulian Anastasov 			if (dev_out == NULL)
2527a210d01aSJulian Anastasov 				goto out;
25281da177e4SLinus Torvalds 			dev_put(dev_out);
25291da177e4SLinus Torvalds 			dev_out = NULL;
25301da177e4SLinus Torvalds 		}
2531a210d01aSJulian Anastasov 	}
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	if (oldflp->oif) {
2535b40afd0eSDenis V. Lunev 		dev_out = dev_get_by_index(net, oldflp->oif);
25361da177e4SLinus Torvalds 		err = -ENODEV;
25371da177e4SLinus Torvalds 		if (dev_out == NULL)
25381da177e4SLinus Torvalds 			goto out;
2539e5ed6399SHerbert Xu 
2540e5ed6399SHerbert Xu 		/* RACE: Check return value of inet_select_addr instead. */
2541e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev_out) == NULL) {
25421da177e4SLinus Torvalds 			dev_put(dev_out);
25431da177e4SLinus Torvalds 			goto out;	/* Wrong error code */
25441da177e4SLinus Torvalds 		}
25451da177e4SLinus Torvalds 
2546f97c1e0cSJoe Perches 		if (ipv4_is_local_multicast(oldflp->fl4_dst) ||
2547f97c1e0cSJoe Perches 		    oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
25481da177e4SLinus Torvalds 			if (!fl.fl4_src)
25491da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
25501da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
25511da177e4SLinus Torvalds 			goto make_route;
25521da177e4SLinus Torvalds 		}
25531da177e4SLinus Torvalds 		if (!fl.fl4_src) {
2554f97c1e0cSJoe Perches 			if (ipv4_is_multicast(oldflp->fl4_dst))
25551da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
25561da177e4SLinus Torvalds 							      fl.fl4_scope);
25571da177e4SLinus Torvalds 			else if (!oldflp->fl4_dst)
25581da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
25591da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
25601da177e4SLinus Torvalds 		}
25611da177e4SLinus Torvalds 	}
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	if (!fl.fl4_dst) {
25641da177e4SLinus Torvalds 		fl.fl4_dst = fl.fl4_src;
25651da177e4SLinus Torvalds 		if (!fl.fl4_dst)
25661da177e4SLinus Torvalds 			fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
25671da177e4SLinus Torvalds 		if (dev_out)
25681da177e4SLinus Torvalds 			dev_put(dev_out);
2569b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
25701da177e4SLinus Torvalds 		dev_hold(dev_out);
2571b40afd0eSDenis V. Lunev 		fl.oif = net->loopback_dev->ifindex;
25721da177e4SLinus Torvalds 		res.type = RTN_LOCAL;
25731da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
25741da177e4SLinus Torvalds 		goto make_route;
25751da177e4SLinus Torvalds 	}
25761da177e4SLinus Torvalds 
2577b40afd0eSDenis V. Lunev 	if (fib_lookup(net, &fl, &res)) {
25781da177e4SLinus Torvalds 		res.fi = NULL;
25791da177e4SLinus Torvalds 		if (oldflp->oif) {
25801da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
25811da177e4SLinus Torvalds 			   that the destination is on link.
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 			   WHY? DW.
25841da177e4SLinus Torvalds 			   Because we are allowed to send to iface
25851da177e4SLinus Torvalds 			   even if it has NO routes and NO assigned
25861da177e4SLinus Torvalds 			   addresses. When oif is specified, routing
25871da177e4SLinus Torvalds 			   tables are looked up with only one purpose:
25881da177e4SLinus Torvalds 			   to catch if destination is gatewayed, rather than
25891da177e4SLinus Torvalds 			   direct. Moreover, if MSG_DONTROUTE is set,
25901da177e4SLinus Torvalds 			   we send packet, ignoring both routing tables
25911da177e4SLinus Torvalds 			   and ifaddr state. --ANK
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 			   We could make it even if oif is unknown,
25951da177e4SLinus Torvalds 			   likely IPv6, but we do not.
25961da177e4SLinus Torvalds 			 */
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 			if (fl.fl4_src == 0)
25991da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
26001da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
26011da177e4SLinus Torvalds 			res.type = RTN_UNICAST;
26021da177e4SLinus Torvalds 			goto make_route;
26031da177e4SLinus Torvalds 		}
26041da177e4SLinus Torvalds 		if (dev_out)
26051da177e4SLinus Torvalds 			dev_put(dev_out);
26061da177e4SLinus Torvalds 		err = -ENETUNREACH;
26071da177e4SLinus Torvalds 		goto out;
26081da177e4SLinus Torvalds 	}
26091da177e4SLinus Torvalds 	free_res = 1;
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
26121da177e4SLinus Torvalds 		if (!fl.fl4_src)
26131da177e4SLinus Torvalds 			fl.fl4_src = fl.fl4_dst;
26141da177e4SLinus Torvalds 		if (dev_out)
26151da177e4SLinus Torvalds 			dev_put(dev_out);
2616b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
26171da177e4SLinus Torvalds 		dev_hold(dev_out);
26181da177e4SLinus Torvalds 		fl.oif = dev_out->ifindex;
26191da177e4SLinus Torvalds 		if (res.fi)
26201da177e4SLinus Torvalds 			fib_info_put(res.fi);
26211da177e4SLinus Torvalds 		res.fi = NULL;
26221da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
26231da177e4SLinus Torvalds 		goto make_route;
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
26271da177e4SLinus Torvalds 	if (res.fi->fib_nhs > 1 && fl.oif == 0)
26281da177e4SLinus Torvalds 		fib_select_multipath(&fl, &res);
26291da177e4SLinus Torvalds 	else
26301da177e4SLinus Torvalds #endif
26311da177e4SLinus Torvalds 	if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
2632b40afd0eSDenis V. Lunev 		fib_select_default(net, &fl, &res);
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	if (!fl.fl4_src)
26351da177e4SLinus Torvalds 		fl.fl4_src = FIB_RES_PREFSRC(res);
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	if (dev_out)
26381da177e4SLinus Torvalds 		dev_put(dev_out);
26391da177e4SLinus Torvalds 	dev_out = FIB_RES_DEV(res);
26401da177e4SLinus Torvalds 	dev_hold(dev_out);
26411da177e4SLinus Torvalds 	fl.oif = dev_out->ifindex;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds make_route:
26451da177e4SLinus Torvalds 	err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	if (free_res)
26491da177e4SLinus Torvalds 		fib_res_put(&res);
26501da177e4SLinus Torvalds 	if (dev_out)
26511da177e4SLinus Torvalds 		dev_put(dev_out);
26521da177e4SLinus Torvalds out:	return err;
26531da177e4SLinus Torvalds }
26541da177e4SLinus Torvalds 
2655611c183eSDenis V. Lunev int __ip_route_output_key(struct net *net, struct rtable **rp,
2656611c183eSDenis V. Lunev 			  const struct flowi *flp)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	unsigned hash;
26591da177e4SLinus Torvalds 	struct rtable *rth;
26601da177e4SLinus Torvalds 
26611080d709SNeil Horman 	if (!rt_caching(net))
26621080d709SNeil Horman 		goto slow_output;
26631080d709SNeil Horman 
2664e84f84f2SDenis V. Lunev 	hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif, rt_genid(net));
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	rcu_read_lock_bh();
26671da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2668093c2ca4SEric Dumazet 		rth = rcu_dereference(rth->u.dst.rt_next)) {
26691da177e4SLinus Torvalds 		if (rth->fl.fl4_dst == flp->fl4_dst &&
26701da177e4SLinus Torvalds 		    rth->fl.fl4_src == flp->fl4_src &&
26711da177e4SLinus Torvalds 		    rth->fl.iif == 0 &&
26721da177e4SLinus Torvalds 		    rth->fl.oif == flp->oif &&
267347dcf0cbSThomas Graf 		    rth->fl.mark == flp->mark &&
26741da177e4SLinus Torvalds 		    !((rth->fl.fl4_tos ^ flp->fl4_tos) &
2675b5921910SDenis V. Lunev 			    (IPTOS_RT_MASK | RTO_ONLINK)) &&
2676878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(rth->u.dst.dev), net) &&
2677e84f84f2SDenis V. Lunev 		    !rt_is_expired(rth)) {
267803f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, jiffies);
26791da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_hit);
26801da177e4SLinus Torvalds 			rcu_read_unlock_bh();
26811da177e4SLinus Torvalds 			*rp = rth;
26821da177e4SLinus Torvalds 			return 0;
26831da177e4SLinus Torvalds 		}
26841da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(out_hlist_search);
26851da177e4SLinus Torvalds 	}
26861da177e4SLinus Torvalds 	rcu_read_unlock_bh();
26871da177e4SLinus Torvalds 
26881080d709SNeil Horman slow_output:
2689611c183eSDenis V. Lunev 	return ip_route_output_slow(net, rp, flp);
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds 
2692d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key);
2693d8c97a94SArnaldo Carvalho de Melo 
269414e50e57SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
269514e50e57SDavid S. Miller {
269614e50e57SDavid S. Miller }
269714e50e57SDavid S. Miller 
269814e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
269914e50e57SDavid S. Miller 	.family			=	AF_INET,
270009640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IP),
270114e50e57SDavid S. Miller 	.destroy		=	ipv4_dst_destroy,
270214e50e57SDavid S. Miller 	.check			=	ipv4_dst_check,
270314e50e57SDavid S. Miller 	.update_pmtu		=	ipv4_rt_blackhole_update_pmtu,
2704e2422970SEric Dumazet 	.entries		=	ATOMIC_INIT(0),
270514e50e57SDavid S. Miller };
270614e50e57SDavid S. Miller 
270714e50e57SDavid S. Miller 
2708e84f84f2SDenis V. Lunev static int ipv4_dst_blackhole(struct net *net, struct rtable **rp, struct flowi *flp)
270914e50e57SDavid S. Miller {
271014e50e57SDavid S. Miller 	struct rtable *ort = *rp;
271114e50e57SDavid S. Miller 	struct rtable *rt = (struct rtable *)
271214e50e57SDavid S. Miller 		dst_alloc(&ipv4_dst_blackhole_ops);
271314e50e57SDavid S. Miller 
271414e50e57SDavid S. Miller 	if (rt) {
271514e50e57SDavid S. Miller 		struct dst_entry *new = &rt->u.dst;
271614e50e57SDavid S. Miller 
271714e50e57SDavid S. Miller 		atomic_set(&new->__refcnt, 1);
271814e50e57SDavid S. Miller 		new->__use = 1;
2719352e512cSHerbert Xu 		new->input = dst_discard;
2720352e512cSHerbert Xu 		new->output = dst_discard;
272114e50e57SDavid S. Miller 		memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
272214e50e57SDavid S. Miller 
272314e50e57SDavid S. Miller 		new->dev = ort->u.dst.dev;
272414e50e57SDavid S. Miller 		if (new->dev)
272514e50e57SDavid S. Miller 			dev_hold(new->dev);
272614e50e57SDavid S. Miller 
272714e50e57SDavid S. Miller 		rt->fl = ort->fl;
272814e50e57SDavid S. Miller 
272914e50e57SDavid S. Miller 		rt->idev = ort->idev;
273014e50e57SDavid S. Miller 		if (rt->idev)
273114e50e57SDavid S. Miller 			in_dev_hold(rt->idev);
2732e84f84f2SDenis V. Lunev 		rt->rt_genid = rt_genid(net);
273314e50e57SDavid S. Miller 		rt->rt_flags = ort->rt_flags;
273414e50e57SDavid S. Miller 		rt->rt_type = ort->rt_type;
273514e50e57SDavid S. Miller 		rt->rt_dst = ort->rt_dst;
273614e50e57SDavid S. Miller 		rt->rt_src = ort->rt_src;
273714e50e57SDavid S. Miller 		rt->rt_iif = ort->rt_iif;
273814e50e57SDavid S. Miller 		rt->rt_gateway = ort->rt_gateway;
273914e50e57SDavid S. Miller 		rt->rt_spec_dst = ort->rt_spec_dst;
274014e50e57SDavid S. Miller 		rt->peer = ort->peer;
274114e50e57SDavid S. Miller 		if (rt->peer)
274214e50e57SDavid S. Miller 			atomic_inc(&rt->peer->refcnt);
274314e50e57SDavid S. Miller 
274414e50e57SDavid S. Miller 		dst_free(new);
274514e50e57SDavid S. Miller 	}
274614e50e57SDavid S. Miller 
274714e50e57SDavid S. Miller 	dst_release(&(*rp)->u.dst);
274814e50e57SDavid S. Miller 	*rp = rt;
274914e50e57SDavid S. Miller 	return (rt ? 0 : -ENOMEM);
275014e50e57SDavid S. Miller }
275114e50e57SDavid S. Miller 
2752f1b050bfSDenis V. Lunev int ip_route_output_flow(struct net *net, struct rtable **rp, struct flowi *flp,
2753f1b050bfSDenis V. Lunev 			 struct sock *sk, int flags)
27541da177e4SLinus Torvalds {
27551da177e4SLinus Torvalds 	int err;
27561da177e4SLinus Torvalds 
2757f1b050bfSDenis V. Lunev 	if ((err = __ip_route_output_key(net, rp, flp)) != 0)
27581da177e4SLinus Torvalds 		return err;
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds 	if (flp->proto) {
27611da177e4SLinus Torvalds 		if (!flp->fl4_src)
27621da177e4SLinus Torvalds 			flp->fl4_src = (*rp)->rt_src;
27631da177e4SLinus Torvalds 		if (!flp->fl4_dst)
27641da177e4SLinus Torvalds 			flp->fl4_dst = (*rp)->rt_dst;
276552479b62SAlexey Dobriyan 		err = __xfrm_lookup(net, (struct dst_entry **)rp, flp, sk,
2766bb72845eSHerbert Xu 				    flags ? XFRM_LOOKUP_WAIT : 0);
276714e50e57SDavid S. Miller 		if (err == -EREMOTE)
2768e84f84f2SDenis V. Lunev 			err = ipv4_dst_blackhole(net, rp, flp);
276914e50e57SDavid S. Miller 
277014e50e57SDavid S. Miller 		return err;
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 	return 0;
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds 
2776d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2777d8c97a94SArnaldo Carvalho de Melo 
2778f206351aSDenis V. Lunev int ip_route_output_key(struct net *net, struct rtable **rp, struct flowi *flp)
27791da177e4SLinus Torvalds {
2780f206351aSDenis V. Lunev 	return ip_route_output_flow(net, rp, flp, NULL, 0);
27811da177e4SLinus Torvalds }
27821da177e4SLinus Torvalds 
27834feb88e5SBenjamin Thery static int rt_fill_info(struct net *net,
27844feb88e5SBenjamin Thery 			struct sk_buff *skb, u32 pid, u32 seq, int event,
2785b6544c0bSJamal Hadi Salim 			int nowait, unsigned int flags)
27861da177e4SLinus Torvalds {
2787ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
27881da177e4SLinus Torvalds 	struct rtmsg *r;
27891da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2790e3703b3dSThomas Graf 	long expires;
2791e3703b3dSThomas Graf 	u32 id = 0, ts = 0, tsage = 0, error;
2792be403ea1SThomas Graf 
2793be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2794be403ea1SThomas Graf 	if (nlh == NULL)
279526932566SPatrick McHardy 		return -EMSGSIZE;
2796be403ea1SThomas Graf 
2797be403ea1SThomas Graf 	r = nlmsg_data(nlh);
27981da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
27991da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
28001da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
28011da177e4SLinus Torvalds 	r->rtm_tos	= rt->fl.fl4_tos;
28021da177e4SLinus Torvalds 	r->rtm_table	= RT_TABLE_MAIN;
2803be403ea1SThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
28041da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
28051da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
28061da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
28071da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
28081da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
28091da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
2810be403ea1SThomas Graf 
281117fb2c64SAl Viro 	NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
2812be403ea1SThomas Graf 
28131da177e4SLinus Torvalds 	if (rt->fl.fl4_src) {
28141da177e4SLinus Torvalds 		r->rtm_src_len = 32;
281517fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
28161da177e4SLinus Torvalds 	}
28171da177e4SLinus Torvalds 	if (rt->u.dst.dev)
2818be403ea1SThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
28191da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
28201da177e4SLinus Torvalds 	if (rt->u.dst.tclassid)
2821be403ea1SThomas Graf 		NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
28221da177e4SLinus Torvalds #endif
28231da177e4SLinus Torvalds 	if (rt->fl.iif)
282417fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
28251da177e4SLinus Torvalds 	else if (rt->rt_src != rt->fl.fl4_src)
282617fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
2827be403ea1SThomas Graf 
28281da177e4SLinus Torvalds 	if (rt->rt_dst != rt->rt_gateway)
282917fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
2830be403ea1SThomas Graf 
28311da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2832be403ea1SThomas Graf 		goto nla_put_failure;
2833be403ea1SThomas Graf 
2834e3703b3dSThomas Graf 	error = rt->u.dst.error;
2835e3703b3dSThomas Graf 	expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
28361da177e4SLinus Torvalds 	if (rt->peer) {
2837e3703b3dSThomas Graf 		id = rt->peer->ip_id_count;
28381da177e4SLinus Torvalds 		if (rt->peer->tcp_ts_stamp) {
2839e3703b3dSThomas Graf 			ts = rt->peer->tcp_ts;
28409d729f72SJames Morris 			tsage = get_seconds() - rt->peer->tcp_ts_stamp;
28411da177e4SLinus Torvalds 		}
28421da177e4SLinus Torvalds 	}
2843be403ea1SThomas Graf 
28441da177e4SLinus Torvalds 	if (rt->fl.iif) {
28451da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2846e448515cSAl Viro 		__be32 dst = rt->rt_dst;
28471da177e4SLinus Torvalds 
2848f97c1e0cSJoe Perches 		if (ipv4_is_multicast(dst) && !ipv4_is_local_multicast(dst) &&
28494feb88e5SBenjamin Thery 		    IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
28504feb88e5SBenjamin Thery 			int err = ipmr_get_route(net, skb, r, nowait);
28511da177e4SLinus Torvalds 			if (err <= 0) {
28521da177e4SLinus Torvalds 				if (!nowait) {
28531da177e4SLinus Torvalds 					if (err == 0)
28541da177e4SLinus Torvalds 						return 0;
2855be403ea1SThomas Graf 					goto nla_put_failure;
28561da177e4SLinus Torvalds 				} else {
28571da177e4SLinus Torvalds 					if (err == -EMSGSIZE)
2858be403ea1SThomas Graf 						goto nla_put_failure;
2859e3703b3dSThomas Graf 					error = err;
28601da177e4SLinus Torvalds 				}
28611da177e4SLinus Torvalds 			}
28621da177e4SLinus Torvalds 		} else
28631da177e4SLinus Torvalds #endif
2864be403ea1SThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
28651da177e4SLinus Torvalds 	}
28661da177e4SLinus Torvalds 
2867e3703b3dSThomas Graf 	if (rtnl_put_cacheinfo(skb, &rt->u.dst, id, ts, tsage,
2868e3703b3dSThomas Graf 			       expires, error) < 0)
2869e3703b3dSThomas Graf 		goto nla_put_failure;
28701da177e4SLinus Torvalds 
2871be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
2872be403ea1SThomas Graf 
2873be403ea1SThomas Graf nla_put_failure:
287426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
287526932566SPatrick McHardy 	return -EMSGSIZE;
28761da177e4SLinus Torvalds }
28771da177e4SLinus Torvalds 
287863f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
28791da177e4SLinus Torvalds {
28803b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2881d889ce3bSThomas Graf 	struct rtmsg *rtm;
2882d889ce3bSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
28831da177e4SLinus Torvalds 	struct rtable *rt = NULL;
28849e12bb22SAl Viro 	__be32 dst = 0;
28859e12bb22SAl Viro 	__be32 src = 0;
28869e12bb22SAl Viro 	u32 iif;
2887d889ce3bSThomas Graf 	int err;
28881da177e4SLinus Torvalds 	struct sk_buff *skb;
28891da177e4SLinus Torvalds 
2890d889ce3bSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2891d889ce3bSThomas Graf 	if (err < 0)
2892d889ce3bSThomas Graf 		goto errout;
2893d889ce3bSThomas Graf 
2894d889ce3bSThomas Graf 	rtm = nlmsg_data(nlh);
2895d889ce3bSThomas Graf 
28961da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2897d889ce3bSThomas Graf 	if (skb == NULL) {
2898d889ce3bSThomas Graf 		err = -ENOBUFS;
2899d889ce3bSThomas Graf 		goto errout;
2900d889ce3bSThomas Graf 	}
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
29031da177e4SLinus Torvalds 	   through good chunk of routing engine.
29041da177e4SLinus Torvalds 	 */
2905459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
2906c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2907d2c962b8SStephen Hemminger 
2908d2c962b8SStephen Hemminger 	/* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2909eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = IPPROTO_ICMP;
29101da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
29111da177e4SLinus Torvalds 
291217fb2c64SAl Viro 	src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
291317fb2c64SAl Viro 	dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2914d889ce3bSThomas Graf 	iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 	if (iif) {
2917d889ce3bSThomas Graf 		struct net_device *dev;
2918d889ce3bSThomas Graf 
29191937504dSDenis V. Lunev 		dev = __dev_get_by_index(net, iif);
2920d889ce3bSThomas Graf 		if (dev == NULL) {
29211da177e4SLinus Torvalds 			err = -ENODEV;
2922d889ce3bSThomas Graf 			goto errout_free;
2923d889ce3bSThomas Graf 		}
2924d889ce3bSThomas Graf 
29251da177e4SLinus Torvalds 		skb->protocol	= htons(ETH_P_IP);
29261da177e4SLinus Torvalds 		skb->dev	= dev;
29271da177e4SLinus Torvalds 		local_bh_disable();
29281da177e4SLinus Torvalds 		err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
29291da177e4SLinus Torvalds 		local_bh_enable();
2930d889ce3bSThomas Graf 
2931ee6b9673SEric Dumazet 		rt = skb->rtable;
2932d889ce3bSThomas Graf 		if (err == 0 && rt->u.dst.error)
29331da177e4SLinus Torvalds 			err = -rt->u.dst.error;
29341da177e4SLinus Torvalds 	} else {
2935d889ce3bSThomas Graf 		struct flowi fl = {
2936d889ce3bSThomas Graf 			.nl_u = {
2937d889ce3bSThomas Graf 				.ip4_u = {
2938d889ce3bSThomas Graf 					.daddr = dst,
29391da177e4SLinus Torvalds 					.saddr = src,
2940d889ce3bSThomas Graf 					.tos = rtm->rtm_tos,
2941d889ce3bSThomas Graf 				},
2942d889ce3bSThomas Graf 			},
2943d889ce3bSThomas Graf 			.oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
2944d889ce3bSThomas Graf 		};
29451937504dSDenis V. Lunev 		err = ip_route_output_key(net, &rt, &fl);
29461da177e4SLinus Torvalds 	}
2947d889ce3bSThomas Graf 
29481da177e4SLinus Torvalds 	if (err)
2949d889ce3bSThomas Graf 		goto errout_free;
29501da177e4SLinus Torvalds 
2951ee6b9673SEric Dumazet 	skb->rtable = rt;
29521da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
29531da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
29541da177e4SLinus Torvalds 
29554feb88e5SBenjamin Thery 	err = rt_fill_info(net, skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2956b6544c0bSJamal Hadi Salim 			   RTM_NEWROUTE, 0, 0);
2957d889ce3bSThomas Graf 	if (err <= 0)
2958d889ce3bSThomas Graf 		goto errout_free;
29591da177e4SLinus Torvalds 
29601937504dSDenis V. Lunev 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2961d889ce3bSThomas Graf errout:
29622942e900SThomas Graf 	return err;
29631da177e4SLinus Torvalds 
2964d889ce3bSThomas Graf errout_free:
29651da177e4SLinus Torvalds 	kfree_skb(skb);
2966d889ce3bSThomas Graf 	goto errout;
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
29701da177e4SLinus Torvalds {
29711da177e4SLinus Torvalds 	struct rtable *rt;
29721da177e4SLinus Torvalds 	int h, s_h;
29731da177e4SLinus Torvalds 	int idx, s_idx;
29741937504dSDenis V. Lunev 	struct net *net;
29751937504dSDenis V. Lunev 
29763b1e0a65SYOSHIFUJI Hideaki 	net = sock_net(skb->sk);
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	s_h = cb->args[0];
2979d8c92830SEric Dumazet 	if (s_h < 0)
2980d8c92830SEric Dumazet 		s_h = 0;
29811da177e4SLinus Torvalds 	s_idx = idx = cb->args[1];
2982a6272665SEric Dumazet 	for (h = s_h; h <= rt_hash_mask; h++, s_idx = 0) {
2983a6272665SEric Dumazet 		if (!rt_hash_table[h].chain)
2984a6272665SEric Dumazet 			continue;
29851da177e4SLinus Torvalds 		rcu_read_lock_bh();
29861da177e4SLinus Torvalds 		for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
2987093c2ca4SEric Dumazet 		     rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
2988878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(rt->u.dst.dev), net) || idx < s_idx)
29891da177e4SLinus Torvalds 				continue;
2990e84f84f2SDenis V. Lunev 			if (rt_is_expired(rt))
299129e75252SEric Dumazet 				continue;
29921da177e4SLinus Torvalds 			skb->dst = dst_clone(&rt->u.dst);
29934feb88e5SBenjamin Thery 			if (rt_fill_info(net, skb, NETLINK_CB(cb->skb).pid,
2994b6544c0bSJamal Hadi Salim 					 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2995b6544c0bSJamal Hadi Salim 					 1, NLM_F_MULTI) <= 0) {
29961da177e4SLinus Torvalds 				dst_release(xchg(&skb->dst, NULL));
29971da177e4SLinus Torvalds 				rcu_read_unlock_bh();
29981da177e4SLinus Torvalds 				goto done;
29991da177e4SLinus Torvalds 			}
30001da177e4SLinus Torvalds 			dst_release(xchg(&skb->dst, NULL));
30011da177e4SLinus Torvalds 		}
30021da177e4SLinus Torvalds 		rcu_read_unlock_bh();
30031da177e4SLinus Torvalds 	}
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds done:
30061da177e4SLinus Torvalds 	cb->args[0] = h;
30071da177e4SLinus Torvalds 	cb->args[1] = idx;
30081da177e4SLinus Torvalds 	return skb->len;
30091da177e4SLinus Torvalds }
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
30121da177e4SLinus Torvalds {
301376e6ebfbSDenis V. Lunev 	rt_cache_flush(dev_net(in_dev->dev), 0);
30141da177e4SLinus Torvalds }
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
301781c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write,
30181da177e4SLinus Torvalds 					struct file *filp, void __user *buffer,
30191da177e4SLinus Torvalds 					size_t *lenp, loff_t *ppos)
30201da177e4SLinus Torvalds {
30211da177e4SLinus Torvalds 	if (write) {
3022639e104fSDenis V. Lunev 		int flush_delay;
302381c684d1SDenis V. Lunev 		ctl_table ctl;
302439a23e75SDenis V. Lunev 		struct net *net;
3025639e104fSDenis V. Lunev 
302681c684d1SDenis V. Lunev 		memcpy(&ctl, __ctl, sizeof(ctl));
302781c684d1SDenis V. Lunev 		ctl.data = &flush_delay;
302881c684d1SDenis V. Lunev 		proc_dointvec(&ctl, write, filp, buffer, lenp, ppos);
3029639e104fSDenis V. Lunev 
303081c684d1SDenis V. Lunev 		net = (struct net *)__ctl->extra1;
303139a23e75SDenis V. Lunev 		rt_cache_flush(net, flush_delay);
30321da177e4SLinus Torvalds 		return 0;
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds 	return -EINVAL;
30361da177e4SLinus Torvalds }
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
30391da177e4SLinus Torvalds 						void __user *oldval,
30401da177e4SLinus Torvalds 						size_t __user *oldlenp,
30411da177e4SLinus Torvalds 						void __user *newval,
30421f29bcd7SAlexey Dobriyan 						size_t newlen)
30431da177e4SLinus Torvalds {
30441da177e4SLinus Torvalds 	int delay;
304539a23e75SDenis V. Lunev 	struct net *net;
30461da177e4SLinus Torvalds 	if (newlen != sizeof(int))
30471da177e4SLinus Torvalds 		return -EINVAL;
30481da177e4SLinus Torvalds 	if (get_user(delay, (int __user *)newval))
30491da177e4SLinus Torvalds 		return -EFAULT;
305039a23e75SDenis V. Lunev 	net = (struct net *)table->extra1;
305139a23e75SDenis V. Lunev 	rt_cache_flush(net, delay);
30521da177e4SLinus Torvalds 	return 0;
30531da177e4SLinus Torvalds }
30541da177e4SLinus Torvalds 
3055c6153b5bSHerbert Xu static void rt_secret_reschedule(int old)
3056c6153b5bSHerbert Xu {
3057c6153b5bSHerbert Xu 	struct net *net;
3058c6153b5bSHerbert Xu 	int new = ip_rt_secret_interval;
3059c6153b5bSHerbert Xu 	int diff = new - old;
3060c6153b5bSHerbert Xu 
3061c6153b5bSHerbert Xu 	if (!diff)
3062c6153b5bSHerbert Xu 		return;
3063c6153b5bSHerbert Xu 
3064c6153b5bSHerbert Xu 	rtnl_lock();
3065c6153b5bSHerbert Xu 	for_each_net(net) {
3066c6153b5bSHerbert Xu 		int deleted = del_timer_sync(&net->ipv4.rt_secret_timer);
3067c6153b5bSHerbert Xu 
3068c6153b5bSHerbert Xu 		if (!new)
3069c6153b5bSHerbert Xu 			continue;
3070c6153b5bSHerbert Xu 
3071c6153b5bSHerbert Xu 		if (deleted) {
3072c6153b5bSHerbert Xu 			long time = net->ipv4.rt_secret_timer.expires - jiffies;
3073c6153b5bSHerbert Xu 
3074c6153b5bSHerbert Xu 			if (time <= 0 || (time += diff) <= 0)
3075c6153b5bSHerbert Xu 				time = 0;
3076c6153b5bSHerbert Xu 
3077c6153b5bSHerbert Xu 			net->ipv4.rt_secret_timer.expires = time;
3078c6153b5bSHerbert Xu 		} else
3079c6153b5bSHerbert Xu 			net->ipv4.rt_secret_timer.expires = new;
3080c6153b5bSHerbert Xu 
3081c6153b5bSHerbert Xu 		net->ipv4.rt_secret_timer.expires += jiffies;
3082c6153b5bSHerbert Xu 		add_timer(&net->ipv4.rt_secret_timer);
3083c6153b5bSHerbert Xu 	}
3084c6153b5bSHerbert Xu 	rtnl_unlock();
3085c6153b5bSHerbert Xu }
3086c6153b5bSHerbert Xu 
3087c6153b5bSHerbert Xu static int ipv4_sysctl_rt_secret_interval(ctl_table *ctl, int write,
3088c6153b5bSHerbert Xu 					  struct file *filp,
3089c6153b5bSHerbert Xu 					  void __user *buffer, size_t *lenp,
3090c6153b5bSHerbert Xu 					  loff_t *ppos)
3091c6153b5bSHerbert Xu {
3092c6153b5bSHerbert Xu 	int old = ip_rt_secret_interval;
3093c6153b5bSHerbert Xu 	int ret = proc_dointvec_jiffies(ctl, write, filp, buffer, lenp, ppos);
3094c6153b5bSHerbert Xu 
3095c6153b5bSHerbert Xu 	rt_secret_reschedule(old);
3096c6153b5bSHerbert Xu 
3097c6153b5bSHerbert Xu 	return ret;
3098c6153b5bSHerbert Xu }
3099c6153b5bSHerbert Xu 
3100c6153b5bSHerbert Xu static int ipv4_sysctl_rt_secret_interval_strategy(ctl_table *table,
3101c6153b5bSHerbert Xu 						   void __user *oldval,
3102c6153b5bSHerbert Xu 						   size_t __user *oldlenp,
3103c6153b5bSHerbert Xu 						   void __user *newval,
3104c6153b5bSHerbert Xu 						   size_t newlen)
3105c6153b5bSHerbert Xu {
3106c6153b5bSHerbert Xu 	int old = ip_rt_secret_interval;
3107f221e726SAlexey Dobriyan 	int ret = sysctl_jiffies(table, oldval, oldlenp, newval, newlen);
3108c6153b5bSHerbert Xu 
3109c6153b5bSHerbert Xu 	rt_secret_reschedule(old);
3110c6153b5bSHerbert Xu 
3111c6153b5bSHerbert Xu 	return ret;
3112c6153b5bSHerbert Xu }
3113c6153b5bSHerbert Xu 
3114eeb61f71SAl Viro static ctl_table ipv4_route_table[] = {
31151da177e4SLinus Torvalds 	{
31161da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_THRESH,
31171da177e4SLinus Torvalds 		.procname	= "gc_thresh",
31181da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
31191da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31201da177e4SLinus Torvalds 		.mode		= 0644,
31216d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
31221da177e4SLinus Torvalds 	},
31231da177e4SLinus Torvalds 	{
31241da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MAX_SIZE,
31251da177e4SLinus Torvalds 		.procname	= "max_size",
31261da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
31271da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31281da177e4SLinus Torvalds 		.mode		= 0644,
31296d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
31301da177e4SLinus Torvalds 	},
31311da177e4SLinus Torvalds 	{
31321da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL,
31351da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
31361da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
31371da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31381da177e4SLinus Torvalds 		.mode		= 0644,
31396d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
31406d9f239aSAlexey Dobriyan 		.strategy	= sysctl_jiffies,
31411da177e4SLinus Torvalds 	},
31421da177e4SLinus Torvalds 	{
31431da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
31441da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
31451da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
31461da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31471da177e4SLinus Torvalds 		.mode		= 0644,
31486d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
31496d9f239aSAlexey Dobriyan 		.strategy	= sysctl_ms_jiffies,
31501da177e4SLinus Torvalds 	},
31511da177e4SLinus Torvalds 	{
31521da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_TIMEOUT,
31531da177e4SLinus Torvalds 		.procname	= "gc_timeout",
31541da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
31551da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31561da177e4SLinus Torvalds 		.mode		= 0644,
31576d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
31586d9f239aSAlexey Dobriyan 		.strategy	= sysctl_jiffies,
31591da177e4SLinus Torvalds 	},
31601da177e4SLinus Torvalds 	{
31611da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_INTERVAL,
31621da177e4SLinus Torvalds 		.procname	= "gc_interval",
31631da177e4SLinus Torvalds 		.data		= &ip_rt_gc_interval,
31641da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31651da177e4SLinus Torvalds 		.mode		= 0644,
31666d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
31676d9f239aSAlexey Dobriyan 		.strategy	= sysctl_jiffies,
31681da177e4SLinus Torvalds 	},
31691da177e4SLinus Torvalds 	{
31701da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_LOAD,
31711da177e4SLinus Torvalds 		.procname	= "redirect_load",
31721da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
31731da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31741da177e4SLinus Torvalds 		.mode		= 0644,
31756d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
31761da177e4SLinus Torvalds 	},
31771da177e4SLinus Torvalds 	{
31781da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_NUMBER,
31791da177e4SLinus Torvalds 		.procname	= "redirect_number",
31801da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
31811da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31821da177e4SLinus Torvalds 		.mode		= 0644,
31836d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
31841da177e4SLinus Torvalds 	},
31851da177e4SLinus Torvalds 	{
31861da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_SILENCE,
31871da177e4SLinus Torvalds 		.procname	= "redirect_silence",
31881da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
31891da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31901da177e4SLinus Torvalds 		.mode		= 0644,
31916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
31921da177e4SLinus Torvalds 	},
31931da177e4SLinus Torvalds 	{
31941da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_COST,
31951da177e4SLinus Torvalds 		.procname	= "error_cost",
31961da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
31971da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
31981da177e4SLinus Torvalds 		.mode		= 0644,
31996d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
32001da177e4SLinus Torvalds 	},
32011da177e4SLinus Torvalds 	{
32021da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_BURST,
32031da177e4SLinus Torvalds 		.procname	= "error_burst",
32041da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
32051da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32061da177e4SLinus Torvalds 		.mode		= 0644,
32076d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
32081da177e4SLinus Torvalds 	},
32091da177e4SLinus Torvalds 	{
32101da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_ELASTICITY,
32111da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
32121da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
32131da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32141da177e4SLinus Torvalds 		.mode		= 0644,
32156d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
32161da177e4SLinus Torvalds 	},
32171da177e4SLinus Torvalds 	{
32181da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MTU_EXPIRES,
32191da177e4SLinus Torvalds 		.procname	= "mtu_expires",
32201da177e4SLinus Torvalds 		.data		= &ip_rt_mtu_expires,
32211da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32221da177e4SLinus Torvalds 		.mode		= 0644,
32236d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
32246d9f239aSAlexey Dobriyan 		.strategy	= sysctl_jiffies,
32251da177e4SLinus Torvalds 	},
32261da177e4SLinus Torvalds 	{
32271da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_PMTU,
32281da177e4SLinus Torvalds 		.procname	= "min_pmtu",
32291da177e4SLinus Torvalds 		.data		= &ip_rt_min_pmtu,
32301da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32311da177e4SLinus Torvalds 		.mode		= 0644,
32326d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
32331da177e4SLinus Torvalds 	},
32341da177e4SLinus Torvalds 	{
32351da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_ADVMSS,
32361da177e4SLinus Torvalds 		.procname	= "min_adv_mss",
32371da177e4SLinus Torvalds 		.data		= &ip_rt_min_advmss,
32381da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32391da177e4SLinus Torvalds 		.mode		= 0644,
32406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
32411da177e4SLinus Torvalds 	},
32421da177e4SLinus Torvalds 	{
32431da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_SECRET_INTERVAL,
32441da177e4SLinus Torvalds 		.procname	= "secret_interval",
32451da177e4SLinus Torvalds 		.data		= &ip_rt_secret_interval,
32461da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
32471da177e4SLinus Torvalds 		.mode		= 0644,
32486d9f239aSAlexey Dobriyan 		.proc_handler	= ipv4_sysctl_rt_secret_interval,
32496d9f239aSAlexey Dobriyan 		.strategy	= ipv4_sysctl_rt_secret_interval_strategy,
32501da177e4SLinus Torvalds 	},
32511da177e4SLinus Torvalds 	{ .ctl_name = 0 }
32521da177e4SLinus Torvalds };
325339a23e75SDenis V. Lunev 
32542f4520d3SAl Viro static struct ctl_table empty[1];
32552f4520d3SAl Viro 
32562f4520d3SAl Viro static struct ctl_table ipv4_skeleton[] =
32572f4520d3SAl Viro {
32582f4520d3SAl Viro 	{ .procname = "route", .ctl_name = NET_IPV4_ROUTE,
3259d994af0dSHugh Dickins 	  .mode = 0555, .child = ipv4_route_table},
32602f4520d3SAl Viro 	{ .procname = "neigh", .ctl_name = NET_IPV4_NEIGH,
3261d994af0dSHugh Dickins 	  .mode = 0555, .child = empty},
32622f4520d3SAl Viro 	{ }
326339a23e75SDenis V. Lunev };
326439a23e75SDenis V. Lunev 
32652f4520d3SAl Viro static __net_initdata struct ctl_path ipv4_path[] = {
32662f4520d3SAl Viro 	{ .procname = "net", .ctl_name = CTL_NET, },
32672f4520d3SAl Viro 	{ .procname = "ipv4", .ctl_name = NET_IPV4, },
32682f4520d3SAl Viro 	{ },
32692f4520d3SAl Viro };
327039a23e75SDenis V. Lunev 
327139a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = {
327239a23e75SDenis V. Lunev 	{
327339a23e75SDenis V. Lunev 		.ctl_name 	= NET_IPV4_ROUTE_FLUSH,
327439a23e75SDenis V. Lunev 		.procname	= "flush",
327539a23e75SDenis V. Lunev 		.maxlen		= sizeof(int),
327639a23e75SDenis V. Lunev 		.mode		= 0200,
32776d9f239aSAlexey Dobriyan 		.proc_handler	= ipv4_sysctl_rtcache_flush,
32786d9f239aSAlexey Dobriyan 		.strategy	= ipv4_sysctl_rtcache_flush_strategy,
327939a23e75SDenis V. Lunev 	},
328039a23e75SDenis V. Lunev 	{ .ctl_name = 0 },
328139a23e75SDenis V. Lunev };
328239a23e75SDenis V. Lunev 
32832f4520d3SAl Viro static __net_initdata struct ctl_path ipv4_route_path[] = {
32842f4520d3SAl Viro 	{ .procname = "net", .ctl_name = CTL_NET, },
32852f4520d3SAl Viro 	{ .procname = "ipv4", .ctl_name = NET_IPV4, },
32862f4520d3SAl Viro 	{ .procname = "route", .ctl_name = NET_IPV4_ROUTE, },
32872f4520d3SAl Viro 	{ },
32882f4520d3SAl Viro };
32892f4520d3SAl Viro 
329039a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net)
329139a23e75SDenis V. Lunev {
329239a23e75SDenis V. Lunev 	struct ctl_table *tbl;
329339a23e75SDenis V. Lunev 
329439a23e75SDenis V. Lunev 	tbl = ipv4_route_flush_table;
329539a23e75SDenis V. Lunev 	if (net != &init_net) {
329639a23e75SDenis V. Lunev 		tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
329739a23e75SDenis V. Lunev 		if (tbl == NULL)
329839a23e75SDenis V. Lunev 			goto err_dup;
329939a23e75SDenis V. Lunev 	}
330039a23e75SDenis V. Lunev 	tbl[0].extra1 = net;
330139a23e75SDenis V. Lunev 
330239a23e75SDenis V. Lunev 	net->ipv4.route_hdr =
330339a23e75SDenis V. Lunev 		register_net_sysctl_table(net, ipv4_route_path, tbl);
330439a23e75SDenis V. Lunev 	if (net->ipv4.route_hdr == NULL)
330539a23e75SDenis V. Lunev 		goto err_reg;
330639a23e75SDenis V. Lunev 	return 0;
330739a23e75SDenis V. Lunev 
330839a23e75SDenis V. Lunev err_reg:
330939a23e75SDenis V. Lunev 	if (tbl != ipv4_route_flush_table)
331039a23e75SDenis V. Lunev 		kfree(tbl);
331139a23e75SDenis V. Lunev err_dup:
331239a23e75SDenis V. Lunev 	return -ENOMEM;
331339a23e75SDenis V. Lunev }
331439a23e75SDenis V. Lunev 
331539a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net)
331639a23e75SDenis V. Lunev {
331739a23e75SDenis V. Lunev 	struct ctl_table *tbl;
331839a23e75SDenis V. Lunev 
331939a23e75SDenis V. Lunev 	tbl = net->ipv4.route_hdr->ctl_table_arg;
332039a23e75SDenis V. Lunev 	unregister_net_sysctl_table(net->ipv4.route_hdr);
332139a23e75SDenis V. Lunev 	BUG_ON(tbl == ipv4_route_flush_table);
332239a23e75SDenis V. Lunev 	kfree(tbl);
332339a23e75SDenis V. Lunev }
332439a23e75SDenis V. Lunev 
332539a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = {
332639a23e75SDenis V. Lunev 	.init = sysctl_route_net_init,
332739a23e75SDenis V. Lunev 	.exit = sysctl_route_net_exit,
332839a23e75SDenis V. Lunev };
33291da177e4SLinus Torvalds #endif
33301da177e4SLinus Torvalds 
33319f5e97e5SDenis V. Lunev 
33329f5e97e5SDenis V. Lunev static __net_init int rt_secret_timer_init(struct net *net)
33339f5e97e5SDenis V. Lunev {
3334e84f84f2SDenis V. Lunev 	atomic_set(&net->ipv4.rt_genid,
3335e84f84f2SDenis V. Lunev 			(int) ((num_physpages ^ (num_physpages>>8)) ^
3336e84f84f2SDenis V. Lunev 			(jiffies ^ (jiffies >> 7))));
3337e84f84f2SDenis V. Lunev 
33389f5e97e5SDenis V. Lunev 	net->ipv4.rt_secret_timer.function = rt_secret_rebuild;
33399f5e97e5SDenis V. Lunev 	net->ipv4.rt_secret_timer.data = (unsigned long)net;
33409f5e97e5SDenis V. Lunev 	init_timer_deferrable(&net->ipv4.rt_secret_timer);
33419f5e97e5SDenis V. Lunev 
3342c6153b5bSHerbert Xu 	if (ip_rt_secret_interval) {
33439f5e97e5SDenis V. Lunev 		net->ipv4.rt_secret_timer.expires =
33449f5e97e5SDenis V. Lunev 			jiffies + net_random() % ip_rt_secret_interval +
33459f5e97e5SDenis V. Lunev 			ip_rt_secret_interval;
33469f5e97e5SDenis V. Lunev 		add_timer(&net->ipv4.rt_secret_timer);
3347c6153b5bSHerbert Xu 	}
33489f5e97e5SDenis V. Lunev 	return 0;
33499f5e97e5SDenis V. Lunev }
33509f5e97e5SDenis V. Lunev 
33519f5e97e5SDenis V. Lunev static __net_exit void rt_secret_timer_exit(struct net *net)
33529f5e97e5SDenis V. Lunev {
33539f5e97e5SDenis V. Lunev 	del_timer_sync(&net->ipv4.rt_secret_timer);
33549f5e97e5SDenis V. Lunev }
33559f5e97e5SDenis V. Lunev 
33569f5e97e5SDenis V. Lunev static __net_initdata struct pernet_operations rt_secret_timer_ops = {
33579f5e97e5SDenis V. Lunev 	.init = rt_secret_timer_init,
33589f5e97e5SDenis V. Lunev 	.exit = rt_secret_timer_exit,
33599f5e97e5SDenis V. Lunev };
33609f5e97e5SDenis V. Lunev 
33619f5e97e5SDenis V. Lunev 
33621da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
33638dbde28dSEric Dumazet struct ip_rt_acct *ip_rt_acct __read_mostly;
33641da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_ROUTE */
33651da177e4SLinus Torvalds 
33661da177e4SLinus Torvalds static __initdata unsigned long rhash_entries;
33671da177e4SLinus Torvalds static int __init set_rhash_entries(char *str)
33681da177e4SLinus Torvalds {
33691da177e4SLinus Torvalds 	if (!str)
33701da177e4SLinus Torvalds 		return 0;
33711da177e4SLinus Torvalds 	rhash_entries = simple_strtoul(str, &str, 0);
33721da177e4SLinus Torvalds 	return 1;
33731da177e4SLinus Torvalds }
33741da177e4SLinus Torvalds __setup("rhash_entries=", set_rhash_entries);
33751da177e4SLinus Torvalds 
33761da177e4SLinus Torvalds int __init ip_rt_init(void)
33771da177e4SLinus Torvalds {
3378424c4b70SEric Dumazet 	int rc = 0;
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
33810dcec8c2SIngo Molnar 	ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
33821da177e4SLinus Torvalds 	if (!ip_rt_acct)
33831da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
33841da177e4SLinus Torvalds #endif
33851da177e4SLinus Torvalds 
3386e5d679f3SAlexey Dobriyan 	ipv4_dst_ops.kmem_cachep =
3387e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
338820c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
33891da177e4SLinus Torvalds 
339014e50e57SDavid S. Miller 	ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
339114e50e57SDavid S. Miller 
33921da177e4SLinus Torvalds 	rt_hash_table = (struct rt_hash_bucket *)
3393424c4b70SEric Dumazet 		alloc_large_system_hash("IP route cache",
3394424c4b70SEric Dumazet 					sizeof(struct rt_hash_bucket),
3395424c4b70SEric Dumazet 					rhash_entries,
3396424c4b70SEric Dumazet 					(num_physpages >= 128 * 1024) ?
339718955cfcSMike Stroyan 					15 : 17,
33988d1502deSKirill Korotaev 					0,
3399424c4b70SEric Dumazet 					&rt_hash_log,
3400424c4b70SEric Dumazet 					&rt_hash_mask,
3401c9503e0fSAnton Blanchard 					rhash_entries ? 0 : 512 * 1024);
340222c047ccSEric Dumazet 	memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
340322c047ccSEric Dumazet 	rt_hash_lock_init();
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
34061da177e4SLinus Torvalds 	ip_rt_max_size = (rt_hash_mask + 1) * 16;
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds 	devinet_init();
34091da177e4SLinus Torvalds 	ip_fib_init();
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds 	/* All the timers, started at system startup tend
34121da177e4SLinus Torvalds 	   to synchronize. Perturb it a bit.
34131da177e4SLinus Torvalds 	 */
341439c90eceSEric Dumazet 	schedule_delayed_work(&expires_work,
341539c90eceSEric Dumazet 		net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
34161da177e4SLinus Torvalds 
34179f5e97e5SDenis V. Lunev 	if (register_pernet_subsys(&rt_secret_timer_ops))
34189f5e97e5SDenis V. Lunev 		printk(KERN_ERR "Unable to setup rt_secret_timer\n");
34191da177e4SLinus Torvalds 
342073b38711SDenis V. Lunev 	if (ip_rt_proc_init())
3421107f1634SPavel Emelyanov 		printk(KERN_ERR "Unable to create route proc files\n");
34221da177e4SLinus Torvalds #ifdef CONFIG_XFRM
34231da177e4SLinus Torvalds 	xfrm_init();
34241da177e4SLinus Torvalds 	xfrm4_init();
34251da177e4SLinus Torvalds #endif
342663f3444fSThomas Graf 	rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
342763f3444fSThomas Graf 
342839a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL
342939a23e75SDenis V. Lunev 	register_pernet_subsys(&sysctl_route_ops);
343039a23e75SDenis V. Lunev #endif
34311da177e4SLinus Torvalds 	return rc;
34321da177e4SLinus Torvalds }
34331da177e4SLinus Torvalds 
3434a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL
3435eeb61f71SAl Viro /*
3436eeb61f71SAl Viro  * We really need to sanitize the damn ipv4 init order, then all
3437eeb61f71SAl Viro  * this nonsense will go away.
3438eeb61f71SAl Viro  */
3439eeb61f71SAl Viro void __init ip_static_sysctl_init(void)
3440eeb61f71SAl Viro {
34412f4520d3SAl Viro 	register_sysctl_paths(ipv4_path, ipv4_skeleton);
3442eeb61f71SAl Viro }
3443a1bc6eb4SAl Viro #endif
3444eeb61f71SAl Viro 
34451da177e4SLinus Torvalds EXPORT_SYMBOL(__ip_select_ident);
34461da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_input);
34471da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_output_key);
3448