xref: /linux/net/ipv4/route.c (revision 878628fbf2589eb24357e42027d5f54b1dafd3c8)
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  *
81da177e4SLinus Torvalds  * Version:	$Id: route.c,v 1.103 2002/01/12 07:44:09 davem Exp $
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
131da177e4SLinus Torvalds  *		Linus Torvalds, <Linus.Torvalds@helsinki.fi>
141da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Fixes:
171da177e4SLinus Torvalds  *		Alan Cox	:	Verify area fixes.
181da177e4SLinus Torvalds  *		Alan Cox	:	cli() protects routing changes
191da177e4SLinus Torvalds  *		Rui Oliveira	:	ICMP routing table updates
201da177e4SLinus Torvalds  *		(rco@di.uminho.pt)	Routing table insertion and update
211da177e4SLinus Torvalds  *		Linus Torvalds	:	Rewrote bits to be sensible
221da177e4SLinus Torvalds  *		Alan Cox	:	Added BSD route gw semantics
231da177e4SLinus Torvalds  *		Alan Cox	:	Super /proc >4K
241da177e4SLinus Torvalds  *		Alan Cox	:	MTU in route table
251da177e4SLinus Torvalds  *		Alan Cox	: 	MSS actually. Also added the window
261da177e4SLinus Torvalds  *					clamper.
271da177e4SLinus Torvalds  *		Sam Lantinga	:	Fixed route matching in rt_del()
281da177e4SLinus Torvalds  *		Alan Cox	:	Routing cache support.
291da177e4SLinus Torvalds  *		Alan Cox	:	Removed compatibility cruft.
301da177e4SLinus Torvalds  *		Alan Cox	:	RTF_REJECT support.
311da177e4SLinus Torvalds  *		Alan Cox	:	TCP irtt support.
321da177e4SLinus Torvalds  *		Jonathan Naylor	:	Added Metric support.
331da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	BSD API fixes.
341da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	Metrics.
351da177e4SLinus Torvalds  *		Alan Cox	:	Use __u32 properly
361da177e4SLinus Torvalds  *		Alan Cox	:	Aligned routing errors more closely with BSD
371da177e4SLinus Torvalds  *					our system is still very different.
381da177e4SLinus Torvalds  *		Alan Cox	:	Faster /proc handling
391da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Massive rework to support tree based routing,
401da177e4SLinus Torvalds  *					routing caches and better behaviour.
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  *		Olaf Erb	:	irtt wasn't being copied right.
431da177e4SLinus Torvalds  *		Bjorn Ekwall	:	Kerneld route support.
441da177e4SLinus Torvalds  *		Alan Cox	:	Multicast fixed (I hope)
451da177e4SLinus Torvalds  * 		Pavel Krauz	:	Limited broadcast fixed
461da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
471da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	End of old history. Split to fib.c and
481da177e4SLinus Torvalds  *					route.c and rewritten from scratch.
491da177e4SLinus Torvalds  *		Andi Kleen	:	Load-limit warning messages.
501da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
511da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Race condition in ip_route_input_slow.
521da177e4SLinus Torvalds  *	Tobias Ringstrom	:	Uninitialized res.type in ip_route_output_slow.
531da177e4SLinus Torvalds  *	Vladimir V. Ivanov	:	IP rule info (flowid) is really useful.
541da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
551da177e4SLinus Torvalds  *	Robert Olsson		:	Added rt_cache statistics
561da177e4SLinus Torvalds  *	Arnaldo C. Melo		:	Convert proc stuff to seq_file
57bb1d23b0SEric Dumazet  *	Eric Dumazet		:	hashed spinlocks and rt_check_expire() fixes.
58cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Ignore TOS on PMTUD and Redirect
59cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Removed TOS from hash calculations
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
621da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
631da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
641da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <linux/module.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <asm/system.h>
701da177e4SLinus Torvalds #include <linux/bitops.h>
711da177e4SLinus Torvalds #include <linux/types.h>
721da177e4SLinus Torvalds #include <linux/kernel.h>
731da177e4SLinus Torvalds #include <linux/mm.h>
74424c4b70SEric Dumazet #include <linux/bootmem.h>
751da177e4SLinus Torvalds #include <linux/string.h>
761da177e4SLinus Torvalds #include <linux/socket.h>
771da177e4SLinus Torvalds #include <linux/sockios.h>
781da177e4SLinus Torvalds #include <linux/errno.h>
791da177e4SLinus Torvalds #include <linux/in.h>
801da177e4SLinus Torvalds #include <linux/inet.h>
811da177e4SLinus Torvalds #include <linux/netdevice.h>
821da177e4SLinus Torvalds #include <linux/proc_fs.h>
831da177e4SLinus Torvalds #include <linux/init.h>
8439c90eceSEric Dumazet #include <linux/workqueue.h>
851da177e4SLinus Torvalds #include <linux/skbuff.h>
861da177e4SLinus Torvalds #include <linux/inetdevice.h>
871da177e4SLinus Torvalds #include <linux/igmp.h>
881da177e4SLinus Torvalds #include <linux/pkt_sched.h>
891da177e4SLinus Torvalds #include <linux/mroute.h>
901da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
911da177e4SLinus Torvalds #include <linux/random.h>
921da177e4SLinus Torvalds #include <linux/jhash.h>
931da177e4SLinus Torvalds #include <linux/rcupdate.h>
941da177e4SLinus Torvalds #include <linux/times.h>
95352e512cSHerbert Xu #include <net/dst.h>
96457c4cbcSEric W. Biederman #include <net/net_namespace.h>
971da177e4SLinus Torvalds #include <net/protocol.h>
981da177e4SLinus Torvalds #include <net/ip.h>
991da177e4SLinus Torvalds #include <net/route.h>
1001da177e4SLinus Torvalds #include <net/inetpeer.h>
1011da177e4SLinus Torvalds #include <net/sock.h>
1021da177e4SLinus Torvalds #include <net/ip_fib.h>
1031da177e4SLinus Torvalds #include <net/arp.h>
1041da177e4SLinus Torvalds #include <net/tcp.h>
1051da177e4SLinus Torvalds #include <net/icmp.h>
1061da177e4SLinus Torvalds #include <net/xfrm.h>
1078d71740cSTom Tucker #include <net/netevent.h>
10863f3444fSThomas Graf #include <net/rtnetlink.h>
1091da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1101da177e4SLinus Torvalds #include <linux/sysctl.h>
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define RT_FL_TOS(oldflp) \
1141da177e4SLinus Torvalds     ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define IP_MAX_MTU	0xFFF0
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static int ip_rt_max_size;
121817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly	= RT_GC_TIMEOUT;
122817bc4dbSStephen Hemminger static int ip_rt_gc_interval __read_mostly	= 60 * HZ;
123817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly	= HZ / 2;
124817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly	= 9;
125817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly	= HZ / 50;
126817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly	= ((HZ / 50) << (9 + 1));
127817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly	= HZ;
128817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly	= 5 * HZ;
129817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly	= 8;
130817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly	= 10 * 60 * HZ;
131817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly		= 512 + 20 + 20;
132817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly	= 256;
133817bc4dbSStephen Hemminger static int ip_rt_secret_interval __read_mostly	= 10 * 60 * HZ;
1341da177e4SLinus Torvalds 
135beb659bdSEric Dumazet static void rt_worker_func(struct work_struct *work);
136beb659bdSEric Dumazet static DECLARE_DELAYED_WORK(expires_work, rt_worker_func);
1371da177e4SLinus Torvalds static struct timer_list rt_secret_timer;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  *	Interface to generic destination cache.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
1441da177e4SLinus Torvalds static void		 ipv4_dst_destroy(struct dst_entry *dst);
1451da177e4SLinus Torvalds static void		 ipv4_dst_ifdown(struct dst_entry *dst,
1461da177e4SLinus Torvalds 					 struct net_device *dev, int how);
1471da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
1481da177e4SLinus Torvalds static void		 ipv4_link_failure(struct sk_buff *skb);
1491da177e4SLinus Torvalds static void		 ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
150569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1541da177e4SLinus Torvalds 	.family =		AF_INET,
1551da177e4SLinus Torvalds 	.protocol =		__constant_htons(ETH_P_IP),
1561da177e4SLinus Torvalds 	.gc =			rt_garbage_collect,
1571da177e4SLinus Torvalds 	.check =		ipv4_dst_check,
1581da177e4SLinus Torvalds 	.destroy =		ipv4_dst_destroy,
1591da177e4SLinus Torvalds 	.ifdown =		ipv4_dst_ifdown,
1601da177e4SLinus Torvalds 	.negative_advice =	ipv4_negative_advice,
1611da177e4SLinus Torvalds 	.link_failure =		ipv4_link_failure,
1621da177e4SLinus Torvalds 	.update_pmtu =		ip_rt_update_pmtu,
163862b82c6SHerbert Xu 	.local_out =		ip_local_out,
1641da177e4SLinus Torvalds 	.entry_size =		sizeof(struct rtable),
165e2422970SEric Dumazet 	.entries =		ATOMIC_INIT(0),
1661da177e4SLinus Torvalds };
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1691da177e4SLinus Torvalds 
1704839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1711da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1721da177e4SLinus Torvalds 	ECN_OR_COST(FILLER),
1731da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1741da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1751da177e4SLinus Torvalds 	TC_PRIO_BULK,
1761da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1771da177e4SLinus Torvalds 	TC_PRIO_BULK,
1781da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1791da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1801da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1811da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1821da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1831da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1841da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1851da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1861da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Route cache.
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* The locking scheme is rather straight forward:
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  * 1) Read-Copy Update protects the buckets of the central route hash.
1971da177e4SLinus Torvalds  * 2) Only writers remove entries, and they hold the lock
1981da177e4SLinus Torvalds  *    as they look at rtable reference counts.
1991da177e4SLinus Torvalds  * 3) Only readers acquire references to rtable entries,
2001da177e4SLinus Torvalds  *    they do so with atomic increments and with the
2011da177e4SLinus Torvalds  *    lock held.
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds struct rt_hash_bucket {
2051da177e4SLinus Torvalds 	struct rtable	*chain;
20622c047ccSEric Dumazet };
2078a25d5deSIngo Molnar #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
2088a25d5deSIngo Molnar 	defined(CONFIG_PROVE_LOCKING)
20922c047ccSEric Dumazet /*
21022c047ccSEric Dumazet  * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
21122c047ccSEric Dumazet  * The size of this table is a power of two and depends on the number of CPUS.
21262051200SIngo Molnar  * (on lockdep we have a quite big spinlock_t, so keep the size down there)
21322c047ccSEric Dumazet  */
21462051200SIngo Molnar #ifdef CONFIG_LOCKDEP
21562051200SIngo Molnar # define RT_HASH_LOCK_SZ	256
21662051200SIngo Molnar #else
21722c047ccSEric Dumazet # if NR_CPUS >= 32
21822c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	4096
21922c047ccSEric Dumazet # elif NR_CPUS >= 16
22022c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	2048
22122c047ccSEric Dumazet # elif NR_CPUS >= 8
22222c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	1024
22322c047ccSEric Dumazet # elif NR_CPUS >= 4
22422c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	512
22522c047ccSEric Dumazet # else
22622c047ccSEric Dumazet #  define RT_HASH_LOCK_SZ	256
22722c047ccSEric Dumazet # endif
22862051200SIngo Molnar #endif
22922c047ccSEric Dumazet 
23022c047ccSEric Dumazet static spinlock_t	*rt_hash_locks;
23122c047ccSEric Dumazet # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
2321ff1cc20SPavel Emelyanov 
2331ff1cc20SPavel Emelyanov static __init void rt_hash_lock_init(void)
2341ff1cc20SPavel Emelyanov {
2351ff1cc20SPavel Emelyanov 	int i;
2361ff1cc20SPavel Emelyanov 
2371ff1cc20SPavel Emelyanov 	rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ,
2381ff1cc20SPavel Emelyanov 			GFP_KERNEL);
2391ff1cc20SPavel Emelyanov 	if (!rt_hash_locks)
2401ff1cc20SPavel Emelyanov 		panic("IP: failed to allocate rt_hash_locks\n");
2411ff1cc20SPavel Emelyanov 
2421ff1cc20SPavel Emelyanov 	for (i = 0; i < RT_HASH_LOCK_SZ; i++)
2431ff1cc20SPavel Emelyanov 		spin_lock_init(&rt_hash_locks[i]);
24422c047ccSEric Dumazet }
24522c047ccSEric Dumazet #else
24622c047ccSEric Dumazet # define rt_hash_lock_addr(slot) NULL
2471ff1cc20SPavel Emelyanov 
2481ff1cc20SPavel Emelyanov static inline void rt_hash_lock_init(void)
2491ff1cc20SPavel Emelyanov {
2501ff1cc20SPavel Emelyanov }
25122c047ccSEric Dumazet #endif
2521da177e4SLinus Torvalds 
253817bc4dbSStephen Hemminger static struct rt_hash_bucket 	*rt_hash_table __read_mostly;
254817bc4dbSStephen Hemminger static unsigned			rt_hash_mask __read_mostly;
255817bc4dbSStephen Hemminger static unsigned int		rt_hash_log  __read_mostly;
256817bc4dbSStephen Hemminger static atomic_t			rt_genid __read_mostly;
2571da177e4SLinus Torvalds 
2582f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
259dbd2915cSAndrew Morton #define RT_CACHE_STAT_INC(field) \
260bfe5d834SPaul Mackerras 	(__raw_get_cpu_var(rt_cache_stat).field++)
2611da177e4SLinus Torvalds 
262cef2685eSIlia Sotnikov static unsigned int rt_hash_code(u32 daddr, u32 saddr)
2631da177e4SLinus Torvalds {
26429e75252SEric Dumazet 	return jhash_2words(daddr, saddr, atomic_read(&rt_genid))
26529e75252SEric Dumazet 		& rt_hash_mask;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2688c7bc840SAl Viro #define rt_hash(daddr, saddr, idx) \
2698c7bc840SAl Viro 	rt_hash_code((__force u32)(__be32)(daddr),\
2708c7bc840SAl Viro 		     (__force u32)(__be32)(saddr) ^ ((idx) << 5))
2718c7bc840SAl Viro 
2721da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
2731da177e4SLinus Torvalds struct rt_cache_iter_state {
274a75e936fSDenis V. Lunev 	struct seq_net_private p;
2751da177e4SLinus Torvalds 	int bucket;
27629e75252SEric Dumazet 	int genid;
2771da177e4SLinus Torvalds };
2781da177e4SLinus Torvalds 
2791218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_first(struct seq_file *seq)
2801da177e4SLinus Torvalds {
2811218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
2821da177e4SLinus Torvalds 	struct rtable *r = NULL;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
2851da177e4SLinus Torvalds 		rcu_read_lock_bh();
28629e75252SEric Dumazet 		r = rcu_dereference(rt_hash_table[st->bucket].chain);
28729e75252SEric Dumazet 		while (r) {
2881218854aSYOSHIFUJI Hideaki 			if (dev_net(r->u.dst.dev) == seq_file_net(seq) &&
289a75e936fSDenis V. Lunev 			    r->rt_genid == st->genid)
29029e75252SEric Dumazet 				return r;
29129e75252SEric Dumazet 			r = rcu_dereference(r->u.dst.rt_next);
29229e75252SEric Dumazet 		}
2931da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2941da177e4SLinus Torvalds 	}
29529e75252SEric Dumazet 	return r;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981218854aSYOSHIFUJI Hideaki static struct rtable *__rt_cache_get_next(struct seq_file *seq,
299642d6318SDenis V. Lunev 					  struct rtable *r)
3001da177e4SLinus Torvalds {
3011218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
302093c2ca4SEric Dumazet 	r = r->u.dst.rt_next;
3031da177e4SLinus Torvalds 	while (!r) {
3041da177e4SLinus Torvalds 		rcu_read_unlock_bh();
3051da177e4SLinus Torvalds 		if (--st->bucket < 0)
3061da177e4SLinus Torvalds 			break;
3071da177e4SLinus Torvalds 		rcu_read_lock_bh();
3081da177e4SLinus Torvalds 		r = rt_hash_table[st->bucket].chain;
3091da177e4SLinus Torvalds 	}
3100bcceadcSEric Dumazet 	return rcu_dereference(r);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_next(struct seq_file *seq,
314642d6318SDenis V. Lunev 					struct rtable *r)
315642d6318SDenis V. Lunev {
3161218854aSYOSHIFUJI Hideaki 	struct rt_cache_iter_state *st = seq->private;
3171218854aSYOSHIFUJI Hideaki 	while ((r = __rt_cache_get_next(seq, r)) != NULL) {
3181218854aSYOSHIFUJI Hideaki 		if (dev_net(r->u.dst.dev) != seq_file_net(seq))
319a75e936fSDenis V. Lunev 			continue;
320642d6318SDenis V. Lunev 		if (r->rt_genid == st->genid)
321642d6318SDenis V. Lunev 			break;
322642d6318SDenis V. Lunev 	}
323642d6318SDenis V. Lunev 	return r;
324642d6318SDenis V. Lunev }
325642d6318SDenis V. Lunev 
3261218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
3271da177e4SLinus Torvalds {
3281218854aSYOSHIFUJI Hideaki 	struct rtable *r = rt_cache_get_first(seq);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (r)
3311218854aSYOSHIFUJI Hideaki 		while (pos && (r = rt_cache_get_next(seq, r)))
3321da177e4SLinus Torvalds 			--pos;
3331da177e4SLinus Torvalds 	return pos ? NULL : r;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
3371da177e4SLinus Torvalds {
33829e75252SEric Dumazet 	struct rt_cache_iter_state *st = seq->private;
33929e75252SEric Dumazet 	if (*pos)
3401218854aSYOSHIFUJI Hideaki 		return rt_cache_get_idx(seq, *pos - 1);
34129e75252SEric Dumazet 	st->genid = atomic_read(&rt_genid);
34229e75252SEric Dumazet 	return SEQ_START_TOKEN;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3461da177e4SLinus Torvalds {
34729e75252SEric Dumazet 	struct rtable *r;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
3501218854aSYOSHIFUJI Hideaki 		r = rt_cache_get_first(seq);
3511da177e4SLinus Torvalds 	else
3521218854aSYOSHIFUJI Hideaki 		r = rt_cache_get_next(seq, v);
3531da177e4SLinus Torvalds 	++*pos;
3541da177e4SLinus Torvalds 	return r;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	if (v && v != SEQ_START_TOKEN)
3601da177e4SLinus Torvalds 		rcu_read_unlock_bh();
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
3661da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
3671da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
3681da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
3691da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
3701da177e4SLinus Torvalds 	else {
3711da177e4SLinus Torvalds 		struct rtable *r = v;
3721da177e4SLinus Torvalds 		char temp[256];
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
3751da177e4SLinus Torvalds 			      "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
3761da177e4SLinus Torvalds 			r->u.dst.dev ? r->u.dst.dev->name : "*",
3771da177e4SLinus Torvalds 			(unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
3781da177e4SLinus Torvalds 			r->rt_flags, atomic_read(&r->u.dst.__refcnt),
3791da177e4SLinus Torvalds 			r->u.dst.__use, 0, (unsigned long)r->rt_src,
3801da177e4SLinus Torvalds 			(dst_metric(&r->u.dst, RTAX_ADVMSS) ?
3811da177e4SLinus Torvalds 			     (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
3821da177e4SLinus Torvalds 			dst_metric(&r->u.dst, RTAX_WINDOW),
3831da177e4SLinus Torvalds 			(int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
3841da177e4SLinus Torvalds 			      dst_metric(&r->u.dst, RTAX_RTTVAR)),
3851da177e4SLinus Torvalds 			r->fl.fl4_tos,
3861da177e4SLinus Torvalds 			r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
3871da177e4SLinus Torvalds 			r->u.dst.hh ? (r->u.dst.hh->hh_output ==
3881da177e4SLinus Torvalds 				       dev_queue_xmit) : 0,
3891da177e4SLinus Torvalds 			r->rt_spec_dst);
3901da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n", temp);
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 	return 0;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
395f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
3961da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
3971da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
3981da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
3991da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
4001da177e4SLinus Torvalds };
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file)
4031da177e4SLinus Torvalds {
404a75e936fSDenis V. Lunev 	return seq_open_net(inode, file, &rt_cache_seq_ops,
405cf7732e4SPavel Emelyanov 			sizeof(struct rt_cache_iter_state));
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4089a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = {
4091da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
4101da177e4SLinus Torvalds 	.open	 = rt_cache_seq_open,
4111da177e4SLinus Torvalds 	.read	 = seq_read,
4121da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
413a75e936fSDenis V. Lunev 	.release = seq_release_net,
4141da177e4SLinus Torvalds };
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	int cpu;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (*pos == 0)
4221da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
4251da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
4261da177e4SLinus Torvalds 			continue;
4271da177e4SLinus Torvalds 		*pos = cpu+1;
4282f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 	return NULL;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	int cpu;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
4381da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
4391da177e4SLinus Torvalds 			continue;
4401da177e4SLinus Torvalds 		*pos = cpu+1;
4412f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 	return NULL;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
4575bec0039SOlaf 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");
4581da177e4SLinus Torvalds 		return 0;
4591da177e4SLinus Torvalds 	}
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	seq_printf(seq,"%08x  %08x %08x %08x %08x %08x %08x %08x "
4621da177e4SLinus Torvalds 		   " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
4631da177e4SLinus Torvalds 		   atomic_read(&ipv4_dst_ops.entries),
4641da177e4SLinus Torvalds 		   st->in_hit,
4651da177e4SLinus Torvalds 		   st->in_slow_tot,
4661da177e4SLinus Torvalds 		   st->in_slow_mc,
4671da177e4SLinus Torvalds 		   st->in_no_route,
4681da177e4SLinus Torvalds 		   st->in_brd,
4691da177e4SLinus Torvalds 		   st->in_martian_dst,
4701da177e4SLinus Torvalds 		   st->in_martian_src,
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		   st->out_hit,
4731da177e4SLinus Torvalds 		   st->out_slow_tot,
4741da177e4SLinus Torvalds 		   st->out_slow_mc,
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		   st->gc_total,
4771da177e4SLinus Torvalds 		   st->gc_ignored,
4781da177e4SLinus Torvalds 		   st->gc_goal_miss,
4791da177e4SLinus Torvalds 		   st->gc_dst_overflow,
4801da177e4SLinus Torvalds 		   st->in_hlist_search,
4811da177e4SLinus Torvalds 		   st->out_hlist_search
4821da177e4SLinus Torvalds 		);
4831da177e4SLinus Torvalds 	return 0;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
486f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
4871da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
4881da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
4891da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
4901da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
4911da177e4SLinus Torvalds };
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	return seq_open(file, &rt_cpu_seq_ops);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4999a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = {
5001da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
5011da177e4SLinus Torvalds 	.open	 = rt_cpu_seq_open,
5021da177e4SLinus Torvalds 	.read	 = seq_read,
5031da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
5041da177e4SLinus Torvalds 	.release = seq_release,
5051da177e4SLinus Torvalds };
5061da177e4SLinus Torvalds 
50778c686e9SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
50878c686e9SPavel Emelyanov static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
50978c686e9SPavel Emelyanov 			   int length, int *eof, void *data)
51078c686e9SPavel Emelyanov {
51178c686e9SPavel Emelyanov 	unsigned int i;
51278c686e9SPavel Emelyanov 
51378c686e9SPavel Emelyanov 	if ((offset & 3) || (length & 3))
51478c686e9SPavel Emelyanov 		return -EIO;
51578c686e9SPavel Emelyanov 
51678c686e9SPavel Emelyanov 	if (offset >= sizeof(struct ip_rt_acct) * 256) {
51778c686e9SPavel Emelyanov 		*eof = 1;
51878c686e9SPavel Emelyanov 		return 0;
51978c686e9SPavel Emelyanov 	}
52078c686e9SPavel Emelyanov 
52178c686e9SPavel Emelyanov 	if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
52278c686e9SPavel Emelyanov 		length = sizeof(struct ip_rt_acct) * 256 - offset;
52378c686e9SPavel Emelyanov 		*eof = 1;
52478c686e9SPavel Emelyanov 	}
52578c686e9SPavel Emelyanov 
52678c686e9SPavel Emelyanov 	offset /= sizeof(u32);
52778c686e9SPavel Emelyanov 
52878c686e9SPavel Emelyanov 	if (length > 0) {
52978c686e9SPavel Emelyanov 		u32 *dst = (u32 *) buffer;
53078c686e9SPavel Emelyanov 
53178c686e9SPavel Emelyanov 		*start = buffer;
53278c686e9SPavel Emelyanov 		memset(dst, 0, length);
53378c686e9SPavel Emelyanov 
53478c686e9SPavel Emelyanov 		for_each_possible_cpu(i) {
53578c686e9SPavel Emelyanov 			unsigned int j;
53678c686e9SPavel Emelyanov 			u32 *src;
53778c686e9SPavel Emelyanov 
53878c686e9SPavel Emelyanov 			src = ((u32 *) per_cpu_ptr(ip_rt_acct, i)) + offset;
53978c686e9SPavel Emelyanov 			for (j = 0; j < length/4; j++)
54078c686e9SPavel Emelyanov 				dst[j] += src[j];
54178c686e9SPavel Emelyanov 		}
54278c686e9SPavel Emelyanov 	}
54378c686e9SPavel Emelyanov 	return length;
54478c686e9SPavel Emelyanov }
54578c686e9SPavel Emelyanov #endif
546107f1634SPavel Emelyanov 
54773b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
548107f1634SPavel Emelyanov {
549107f1634SPavel Emelyanov 	struct proc_dir_entry *pde;
550107f1634SPavel Emelyanov 
551107f1634SPavel Emelyanov 	pde = proc_net_fops_create(net, "rt_cache", S_IRUGO,
552107f1634SPavel Emelyanov 			&rt_cache_seq_fops);
553107f1634SPavel Emelyanov 	if (!pde)
554107f1634SPavel Emelyanov 		goto err1;
555107f1634SPavel Emelyanov 
55677020720SWang Chen 	pde = proc_create("rt_cache", S_IRUGO,
55777020720SWang Chen 			  net->proc_net_stat, &rt_cpu_seq_fops);
558107f1634SPavel Emelyanov 	if (!pde)
559107f1634SPavel Emelyanov 		goto err2;
560107f1634SPavel Emelyanov 
561107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
562107f1634SPavel Emelyanov 	pde = create_proc_read_entry("rt_acct", 0, net->proc_net,
563107f1634SPavel Emelyanov 			ip_rt_acct_read, NULL);
564107f1634SPavel Emelyanov 	if (!pde)
565107f1634SPavel Emelyanov 		goto err3;
566107f1634SPavel Emelyanov #endif
567107f1634SPavel Emelyanov 	return 0;
568107f1634SPavel Emelyanov 
569107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE
570107f1634SPavel Emelyanov err3:
571107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net_stat);
572107f1634SPavel Emelyanov #endif
573107f1634SPavel Emelyanov err2:
574107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net);
575107f1634SPavel Emelyanov err1:
576107f1634SPavel Emelyanov 	return -ENOMEM;
577107f1634SPavel Emelyanov }
57873b38711SDenis V. Lunev 
57973b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
58073b38711SDenis V. Lunev {
58173b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net_stat);
58273b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net);
58373b38711SDenis V. Lunev 	remove_proc_entry("rt_acct", net->proc_net);
58473b38711SDenis V. Lunev }
58573b38711SDenis V. Lunev 
58673b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
58773b38711SDenis V. Lunev 	.init = ip_rt_do_proc_init,
58873b38711SDenis V. Lunev 	.exit = ip_rt_do_proc_exit,
58973b38711SDenis V. Lunev };
59073b38711SDenis V. Lunev 
59173b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
59273b38711SDenis V. Lunev {
59373b38711SDenis V. Lunev 	return register_pernet_subsys(&ip_rt_proc_ops);
59473b38711SDenis V. Lunev }
59573b38711SDenis V. Lunev 
596107f1634SPavel Emelyanov #else
59773b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
598107f1634SPavel Emelyanov {
599107f1634SPavel Emelyanov 	return 0;
600107f1634SPavel Emelyanov }
6011da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds static __inline__ void rt_free(struct rtable *rt)
6041da177e4SLinus Torvalds {
6051da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds static __inline__ void rt_drop(struct rtable *rt)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	ip_rt_put(rt);
6111da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds static __inline__ int rt_fast_clean(struct rtable *rth)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	/* Kill broadcast/multicast entries very aggresively, if they
6171da177e4SLinus Torvalds 	   collide in hash table with more useful entries */
6181da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
619093c2ca4SEric Dumazet 		rth->fl.iif && rth->u.dst.rt_next;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds static __inline__ int rt_valuable(struct rtable *rth)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
6251da177e4SLinus Torvalds 		rth->u.dst.expires;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	unsigned long age;
6311da177e4SLinus Torvalds 	int ret = 0;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (atomic_read(&rth->u.dst.__refcnt))
6341da177e4SLinus Torvalds 		goto out;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	ret = 1;
6371da177e4SLinus Torvalds 	if (rth->u.dst.expires &&
6381da177e4SLinus Torvalds 	    time_after_eq(jiffies, rth->u.dst.expires))
6391da177e4SLinus Torvalds 		goto out;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	age = jiffies - rth->u.dst.lastuse;
6421da177e4SLinus Torvalds 	ret = 0;
6431da177e4SLinus Torvalds 	if ((age <= tmo1 && !rt_fast_clean(rth)) ||
6441da177e4SLinus Torvalds 	    (age <= tmo2 && rt_valuable(rth)))
6451da177e4SLinus Torvalds 		goto out;
6461da177e4SLinus Torvalds 	ret = 1;
6471da177e4SLinus Torvalds out:	return ret;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds /* Bits of score are:
6511da177e4SLinus Torvalds  * 31: very valuable
6521da177e4SLinus Torvalds  * 30: not quite useless
6531da177e4SLinus Torvalds  * 29..0: usage counter
6541da177e4SLinus Torvalds  */
6551da177e4SLinus Torvalds static inline u32 rt_score(struct rtable *rt)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	u32 score = jiffies - rt->u.dst.lastuse;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	score = ~score & ~(3<<30);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (rt_valuable(rt))
6621da177e4SLinus Torvalds 		score |= (1<<31);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	if (!rt->fl.iif ||
6651da177e4SLinus Torvalds 	    !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
6661da177e4SLinus Torvalds 		score |= (1<<30);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	return score;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
6721da177e4SLinus Torvalds {
673714e85beSAl Viro 	return ((__force u32)((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
674714e85beSAl Viro 		(fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr)) |
67547dcf0cbSThomas Graf 		(fl1->mark ^ fl2->mark) |
6768238b218SDavid S. Miller 		(*(u16 *)&fl1->nl_u.ip4_u.tos ^
6778238b218SDavid S. Miller 		 *(u16 *)&fl2->nl_u.ip4_u.tos) |
6788238b218SDavid S. Miller 		(fl1->oif ^ fl2->oif) |
6798238b218SDavid S. Miller 		(fl1->iif ^ fl2->iif)) == 0;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
682b5921910SDenis V. Lunev static inline int compare_netns(struct rtable *rt1, struct rtable *rt2)
683b5921910SDenis V. Lunev {
684c346dca1SYOSHIFUJI Hideaki 	return dev_net(rt1->u.dst.dev) == dev_net(rt2->u.dst.dev);
685b5921910SDenis V. Lunev }
686b5921910SDenis V. Lunev 
687beb659bdSEric Dumazet /*
688beb659bdSEric Dumazet  * Perform a full scan of hash table and free all entries.
689beb659bdSEric Dumazet  * Can be called by a softirq or a process.
690beb659bdSEric Dumazet  * In the later case, we want to be reschedule if necessary
691beb659bdSEric Dumazet  */
692beb659bdSEric Dumazet static void rt_do_flush(int process_context)
693beb659bdSEric Dumazet {
694beb659bdSEric Dumazet 	unsigned int i;
695beb659bdSEric Dumazet 	struct rtable *rth, *next;
696beb659bdSEric Dumazet 
697beb659bdSEric Dumazet 	for (i = 0; i <= rt_hash_mask; i++) {
698beb659bdSEric Dumazet 		if (process_context && need_resched())
699beb659bdSEric Dumazet 			cond_resched();
700beb659bdSEric Dumazet 		rth = rt_hash_table[i].chain;
701beb659bdSEric Dumazet 		if (!rth)
702beb659bdSEric Dumazet 			continue;
703beb659bdSEric Dumazet 
704beb659bdSEric Dumazet 		spin_lock_bh(rt_hash_lock_addr(i));
705beb659bdSEric Dumazet 		rth = rt_hash_table[i].chain;
706beb659bdSEric Dumazet 		rt_hash_table[i].chain = NULL;
707beb659bdSEric Dumazet 		spin_unlock_bh(rt_hash_lock_addr(i));
708beb659bdSEric Dumazet 
709beb659bdSEric Dumazet 		for (; rth; rth = next) {
710beb659bdSEric Dumazet 			next = rth->u.dst.rt_next;
711beb659bdSEric Dumazet 			rt_free(rth);
712beb659bdSEric Dumazet 		}
713beb659bdSEric Dumazet 	}
714beb659bdSEric Dumazet }
715beb659bdSEric Dumazet 
716beb659bdSEric Dumazet static void rt_check_expire(void)
7171da177e4SLinus Torvalds {
718bb1d23b0SEric Dumazet 	static unsigned int rover;
719bb1d23b0SEric Dumazet 	unsigned int i = rover, goal;
7201da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
721bb1d23b0SEric Dumazet 	u64 mult;
7221da177e4SLinus Torvalds 
723bb1d23b0SEric Dumazet 	mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
724bb1d23b0SEric Dumazet 	if (ip_rt_gc_timeout > 1)
725bb1d23b0SEric Dumazet 		do_div(mult, ip_rt_gc_timeout);
726bb1d23b0SEric Dumazet 	goal = (unsigned int)mult;
72739c90eceSEric Dumazet 	if (goal > rt_hash_mask)
72839c90eceSEric Dumazet 		goal = rt_hash_mask + 1;
729bb1d23b0SEric Dumazet 	for (; goal > 0; goal--) {
7301da177e4SLinus Torvalds 		unsigned long tmo = ip_rt_gc_timeout;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 		i = (i + 1) & rt_hash_mask;
7331da177e4SLinus Torvalds 		rthp = &rt_hash_table[i].chain;
7341da177e4SLinus Torvalds 
735d90bf5a9SEric Dumazet 		if (need_resched())
736d90bf5a9SEric Dumazet 			cond_resched();
737d90bf5a9SEric Dumazet 
738cfcabdccSStephen Hemminger 		if (*rthp == NULL)
739bb1d23b0SEric Dumazet 			continue;
74039c90eceSEric Dumazet 		spin_lock_bh(rt_hash_lock_addr(i));
7411da177e4SLinus Torvalds 		while ((rth = *rthp) != NULL) {
74229e75252SEric Dumazet 			if (rth->rt_genid != atomic_read(&rt_genid)) {
74329e75252SEric Dumazet 				*rthp = rth->u.dst.rt_next;
74429e75252SEric Dumazet 				rt_free(rth);
74529e75252SEric Dumazet 				continue;
74629e75252SEric Dumazet 			}
7471da177e4SLinus Torvalds 			if (rth->u.dst.expires) {
7481da177e4SLinus Torvalds 				/* Entry is expired even if it is in use */
74939c90eceSEric Dumazet 				if (time_before_eq(jiffies, rth->u.dst.expires)) {
7501da177e4SLinus Torvalds 					tmo >>= 1;
751093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
7521da177e4SLinus Torvalds 					continue;
7531da177e4SLinus Torvalds 				}
7541da177e4SLinus Torvalds 			} else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
7551da177e4SLinus Torvalds 				tmo >>= 1;
756093c2ca4SEric Dumazet 				rthp = &rth->u.dst.rt_next;
7571da177e4SLinus Torvalds 				continue;
7581da177e4SLinus Torvalds 			}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 			/* Cleanup aged off entries. */
761093c2ca4SEric Dumazet 			*rthp = rth->u.dst.rt_next;
7621da177e4SLinus Torvalds 			rt_free(rth);
7631da177e4SLinus Torvalds 		}
76439c90eceSEric Dumazet 		spin_unlock_bh(rt_hash_lock_addr(i));
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 	rover = i;
767beb659bdSEric Dumazet }
768beb659bdSEric Dumazet 
769beb659bdSEric Dumazet /*
770beb659bdSEric Dumazet  * rt_worker_func() is run in process context.
77129e75252SEric Dumazet  * we call rt_check_expire() to scan part of the hash table
772beb659bdSEric Dumazet  */
773beb659bdSEric Dumazet static void rt_worker_func(struct work_struct *work)
774beb659bdSEric Dumazet {
775beb659bdSEric Dumazet 	rt_check_expire();
77639c90eceSEric Dumazet 	schedule_delayed_work(&expires_work, ip_rt_gc_interval);
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
77929e75252SEric Dumazet /*
78029e75252SEric Dumazet  * Pertubation of rt_genid by a small quantity [1..256]
78129e75252SEric Dumazet  * Using 8 bits of shuffling ensure we can call rt_cache_invalidate()
78229e75252SEric Dumazet  * many times (2^24) without giving recent rt_genid.
78329e75252SEric Dumazet  * Jenkins hash is strong enough that litle changes of rt_genid are OK.
7841da177e4SLinus Torvalds  */
78529e75252SEric Dumazet static void rt_cache_invalidate(void)
7861da177e4SLinus Torvalds {
78729e75252SEric Dumazet 	unsigned char shuffle;
7881da177e4SLinus Torvalds 
78929e75252SEric Dumazet 	get_random_bytes(&shuffle, sizeof(shuffle));
79029e75252SEric Dumazet 	atomic_add(shuffle + 1U, &rt_genid);
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793beb659bdSEric Dumazet /*
79429e75252SEric Dumazet  * delay < 0  : invalidate cache (fast : entries will be deleted later)
79529e75252SEric Dumazet  * delay >= 0 : invalidate & flush cache (can be long)
79629e75252SEric Dumazet  */
79729e75252SEric Dumazet void rt_cache_flush(int delay)
79829e75252SEric Dumazet {
79929e75252SEric Dumazet 	rt_cache_invalidate();
80029e75252SEric Dumazet 	if (delay >= 0)
80129e75252SEric Dumazet 		rt_do_flush(!in_softirq());
80229e75252SEric Dumazet }
80329e75252SEric Dumazet 
80429e75252SEric Dumazet /*
80529e75252SEric Dumazet  * We change rt_genid and let gc do the cleanup
806beb659bdSEric Dumazet  */
8071da177e4SLinus Torvalds static void rt_secret_rebuild(unsigned long dummy)
8081da177e4SLinus Torvalds {
80929e75252SEric Dumazet 	rt_cache_invalidate();
810beb659bdSEric Dumazet 	mod_timer(&rt_secret_timer, jiffies + ip_rt_secret_interval);
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /*
8141da177e4SLinus Torvalds    Short description of GC goals.
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds    We want to build algorithm, which will keep routing cache
8171da177e4SLinus Torvalds    at some equilibrium point, when number of aged off entries
8181da177e4SLinus Torvalds    is kept approximately equal to newly generated ones.
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds    Current expiration strength is variable "expire".
8211da177e4SLinus Torvalds    We try to adjust it dynamically, so that if networking
8221da177e4SLinus Torvalds    is idle expires is large enough to keep enough of warm entries,
8231da177e4SLinus Torvalds    and when load increases it reduces to limit cache size.
8241da177e4SLinus Torvalds  */
8251da177e4SLinus Torvalds 
826569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	static unsigned long expire = RT_GC_TIMEOUT;
8291da177e4SLinus Torvalds 	static unsigned long last_gc;
8301da177e4SLinus Torvalds 	static int rover;
8311da177e4SLinus Torvalds 	static int equilibrium;
8321da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
8331da177e4SLinus Torvalds 	unsigned long now = jiffies;
8341da177e4SLinus Torvalds 	int goal;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/*
8371da177e4SLinus Torvalds 	 * Garbage collection is pretty expensive,
8381da177e4SLinus Torvalds 	 * do not make it too frequently.
8391da177e4SLinus Torvalds 	 */
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_total);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	if (now - last_gc < ip_rt_gc_min_interval &&
8441da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
8451da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_ignored);
8461da177e4SLinus Torvalds 		goto out;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	/* Calculate number of entries, which we want to expire now. */
8501da177e4SLinus Torvalds 	goal = atomic_read(&ipv4_dst_ops.entries) -
8511da177e4SLinus Torvalds 		(ip_rt_gc_elasticity << rt_hash_log);
8521da177e4SLinus Torvalds 	if (goal <= 0) {
8531da177e4SLinus Torvalds 		if (equilibrium < ipv4_dst_ops.gc_thresh)
8541da177e4SLinus Torvalds 			equilibrium = ipv4_dst_ops.gc_thresh;
8551da177e4SLinus Torvalds 		goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
8561da177e4SLinus Torvalds 		if (goal > 0) {
857b790ceddSEric Dumazet 			equilibrium += min_t(unsigned int, goal >> 1, rt_hash_mask + 1);
8581da177e4SLinus Torvalds 			goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 	} else {
8611da177e4SLinus Torvalds 		/* We are in dangerous area. Try to reduce cache really
8621da177e4SLinus Torvalds 		 * aggressively.
8631da177e4SLinus Torvalds 		 */
864b790ceddSEric Dumazet 		goal = max_t(unsigned int, goal >> 1, rt_hash_mask + 1);
8651da177e4SLinus Torvalds 		equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	if (now - last_gc >= ip_rt_gc_min_interval)
8691da177e4SLinus Torvalds 		last_gc = now;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (goal <= 0) {
8721da177e4SLinus Torvalds 		equilibrium += goal;
8731da177e4SLinus Torvalds 		goto work_done;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	do {
8771da177e4SLinus Torvalds 		int i, k;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 		for (i = rt_hash_mask, k = rover; i >= 0; i--) {
8801da177e4SLinus Torvalds 			unsigned long tmo = expire;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 			k = (k + 1) & rt_hash_mask;
8831da177e4SLinus Torvalds 			rthp = &rt_hash_table[k].chain;
88422c047ccSEric Dumazet 			spin_lock_bh(rt_hash_lock_addr(k));
8851da177e4SLinus Torvalds 			while ((rth = *rthp) != NULL) {
88629e75252SEric Dumazet 				if (rth->rt_genid == atomic_read(&rt_genid) &&
88729e75252SEric Dumazet 					!rt_may_expire(rth, tmo, expire)) {
8881da177e4SLinus Torvalds 					tmo >>= 1;
889093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
8901da177e4SLinus Torvalds 					continue;
8911da177e4SLinus Torvalds 				}
892093c2ca4SEric Dumazet 				*rthp = rth->u.dst.rt_next;
8931da177e4SLinus Torvalds 				rt_free(rth);
8941da177e4SLinus Torvalds 				goal--;
8951da177e4SLinus Torvalds 			}
89622c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(k));
8971da177e4SLinus Torvalds 			if (goal <= 0)
8981da177e4SLinus Torvalds 				break;
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 		rover = k;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 		if (goal <= 0)
9031da177e4SLinus Torvalds 			goto work_done;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 		/* Goal is not achieved. We stop process if:
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 		   - if expire reduced to zero. Otherwise, expire is halfed.
9081da177e4SLinus Torvalds 		   - if table is not full.
9091da177e4SLinus Torvalds 		   - if we are called from interrupt.
9101da177e4SLinus Torvalds 		   - jiffies check is just fallback/debug loop breaker.
9111da177e4SLinus Torvalds 		     We will not spin here for long time in any case.
9121da177e4SLinus Torvalds 		 */
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_goal_miss);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 		if (expire == 0)
9171da177e4SLinus Torvalds 			break;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 		expire >>= 1;
9201da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
9211da177e4SLinus Torvalds 		printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
9221da177e4SLinus Torvalds 				atomic_read(&ipv4_dst_ops.entries), goal, i);
9231da177e4SLinus Torvalds #endif
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 		if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
9261da177e4SLinus Torvalds 			goto out;
9271da177e4SLinus Torvalds 	} while (!in_softirq() && time_before_eq(jiffies, now));
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
9301da177e4SLinus Torvalds 		goto out;
9311da177e4SLinus Torvalds 	if (net_ratelimit())
9321da177e4SLinus Torvalds 		printk(KERN_WARNING "dst cache overflow\n");
9331da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_dst_overflow);
9341da177e4SLinus Torvalds 	return 1;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds work_done:
9371da177e4SLinus Torvalds 	expire += ip_rt_gc_min_interval;
9381da177e4SLinus Torvalds 	if (expire > ip_rt_gc_timeout ||
9391da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
9401da177e4SLinus Torvalds 		expire = ip_rt_gc_timeout;
9411da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
9421da177e4SLinus Torvalds 	printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
9431da177e4SLinus Torvalds 			atomic_read(&ipv4_dst_ops.entries), goal, rover);
9441da177e4SLinus Torvalds #endif
9451da177e4SLinus Torvalds out:	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	struct rtable	*rth, **rthp;
9511da177e4SLinus Torvalds 	unsigned long	now;
9521da177e4SLinus Torvalds 	struct rtable *cand, **candp;
9531da177e4SLinus Torvalds 	u32 		min_score;
9541da177e4SLinus Torvalds 	int		chain_length;
9551da177e4SLinus Torvalds 	int attempts = !in_softirq();
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds restart:
9581da177e4SLinus Torvalds 	chain_length = 0;
9591da177e4SLinus Torvalds 	min_score = ~(u32)0;
9601da177e4SLinus Torvalds 	cand = NULL;
9611da177e4SLinus Torvalds 	candp = NULL;
9621da177e4SLinus Torvalds 	now = jiffies;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	rthp = &rt_hash_table[hash].chain;
9651da177e4SLinus Torvalds 
96622c047ccSEric Dumazet 	spin_lock_bh(rt_hash_lock_addr(hash));
9671da177e4SLinus Torvalds 	while ((rth = *rthp) != NULL) {
96829e75252SEric Dumazet 		if (rth->rt_genid != atomic_read(&rt_genid)) {
96929e75252SEric Dumazet 			*rthp = rth->u.dst.rt_next;
97029e75252SEric Dumazet 			rt_free(rth);
97129e75252SEric Dumazet 			continue;
97229e75252SEric Dumazet 		}
973b5921910SDenis V. Lunev 		if (compare_keys(&rth->fl, &rt->fl) && compare_netns(rth, rt)) {
9741da177e4SLinus Torvalds 			/* Put it first */
975093c2ca4SEric Dumazet 			*rthp = rth->u.dst.rt_next;
9761da177e4SLinus Torvalds 			/*
9771da177e4SLinus Torvalds 			 * Since lookup is lockfree, the deletion
9781da177e4SLinus Torvalds 			 * must be visible to another weakly ordered CPU before
9791da177e4SLinus Torvalds 			 * the insertion at the start of the hash chain.
9801da177e4SLinus Torvalds 			 */
981093c2ca4SEric Dumazet 			rcu_assign_pointer(rth->u.dst.rt_next,
9821da177e4SLinus Torvalds 					   rt_hash_table[hash].chain);
9831da177e4SLinus Torvalds 			/*
9841da177e4SLinus Torvalds 			 * Since lookup is lockfree, the update writes
9851da177e4SLinus Torvalds 			 * must be ordered for consistency on SMP.
9861da177e4SLinus Torvalds 			 */
9871da177e4SLinus Torvalds 			rcu_assign_pointer(rt_hash_table[hash].chain, rth);
9881da177e4SLinus Torvalds 
98903f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, now);
99022c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(hash));
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 			rt_drop(rt);
9931da177e4SLinus Torvalds 			*rp = rth;
9941da177e4SLinus Torvalds 			return 0;
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 		if (!atomic_read(&rth->u.dst.__refcnt)) {
9981da177e4SLinus Torvalds 			u32 score = rt_score(rth);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 			if (score <= min_score) {
10011da177e4SLinus Torvalds 				cand = rth;
10021da177e4SLinus Torvalds 				candp = rthp;
10031da177e4SLinus Torvalds 				min_score = score;
10041da177e4SLinus Torvalds 			}
10051da177e4SLinus Torvalds 		}
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 		chain_length++;
10081da177e4SLinus Torvalds 
1009093c2ca4SEric Dumazet 		rthp = &rth->u.dst.rt_next;
10101da177e4SLinus Torvalds 	}
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (cand) {
10131da177e4SLinus Torvalds 		/* ip_rt_gc_elasticity used to be average length of chain
10141da177e4SLinus Torvalds 		 * length, when exceeded gc becomes really aggressive.
10151da177e4SLinus Torvalds 		 *
10161da177e4SLinus Torvalds 		 * The second limit is less certain. At the moment it allows
10171da177e4SLinus Torvalds 		 * only 2 entries per bucket. We will see.
10181da177e4SLinus Torvalds 		 */
10191da177e4SLinus Torvalds 		if (chain_length > ip_rt_gc_elasticity) {
1020093c2ca4SEric Dumazet 			*candp = cand->u.dst.rt_next;
10211da177e4SLinus Torvalds 			rt_free(cand);
10221da177e4SLinus Torvalds 		}
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	/* Try to bind route to arp only if it is output
10261da177e4SLinus Torvalds 	   route or unicast forwarding path.
10271da177e4SLinus Torvalds 	 */
10281da177e4SLinus Torvalds 	if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
10291da177e4SLinus Torvalds 		int err = arp_bind_neighbour(&rt->u.dst);
10301da177e4SLinus Torvalds 		if (err) {
103122c047ccSEric Dumazet 			spin_unlock_bh(rt_hash_lock_addr(hash));
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 			if (err != -ENOBUFS) {
10341da177e4SLinus Torvalds 				rt_drop(rt);
10351da177e4SLinus Torvalds 				return err;
10361da177e4SLinus Torvalds 			}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 			/* Neighbour tables are full and nothing
10391da177e4SLinus Torvalds 			   can be released. Try to shrink route cache,
10401da177e4SLinus Torvalds 			   it is most likely it holds some neighbour records.
10411da177e4SLinus Torvalds 			 */
10421da177e4SLinus Torvalds 			if (attempts-- > 0) {
10431da177e4SLinus Torvalds 				int saved_elasticity = ip_rt_gc_elasticity;
10441da177e4SLinus Torvalds 				int saved_int = ip_rt_gc_min_interval;
10451da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= 1;
10461da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= 0;
1047569d3645SDaniel Lezcano 				rt_garbage_collect(&ipv4_dst_ops);
10481da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= saved_int;
10491da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= saved_elasticity;
10501da177e4SLinus Torvalds 				goto restart;
10511da177e4SLinus Torvalds 			}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 			if (net_ratelimit())
10541da177e4SLinus Torvalds 				printk(KERN_WARNING "Neighbour table overflow.\n");
10551da177e4SLinus Torvalds 			rt_drop(rt);
10561da177e4SLinus Torvalds 			return -ENOBUFS;
10571da177e4SLinus Torvalds 		}
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
1060093c2ca4SEric Dumazet 	rt->u.dst.rt_next = rt_hash_table[hash].chain;
10611da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
1062093c2ca4SEric Dumazet 	if (rt->u.dst.rt_next) {
10631da177e4SLinus Torvalds 		struct rtable *trt;
10641da177e4SLinus Torvalds 		printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
10651da177e4SLinus Torvalds 		       NIPQUAD(rt->rt_dst));
1066093c2ca4SEric Dumazet 		for (trt = rt->u.dst.rt_next; trt; trt = trt->u.dst.rt_next)
10671da177e4SLinus Torvalds 			printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
10681da177e4SLinus Torvalds 		printk("\n");
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds #endif
10711da177e4SLinus Torvalds 	rt_hash_table[hash].chain = rt;
107222c047ccSEric Dumazet 	spin_unlock_bh(rt_hash_lock_addr(hash));
10731da177e4SLinus Torvalds 	*rp = rt;
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds void rt_bind_peer(struct rtable *rt, int create)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(rt_peer_lock);
10801da177e4SLinus Torvalds 	struct inet_peer *peer;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	peer = inet_getpeer(rt->rt_dst, create);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	spin_lock_bh(&rt_peer_lock);
10851da177e4SLinus Torvalds 	if (rt->peer == NULL) {
10861da177e4SLinus Torvalds 		rt->peer = peer;
10871da177e4SLinus Torvalds 		peer = NULL;
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 	spin_unlock_bh(&rt_peer_lock);
10901da177e4SLinus Torvalds 	if (peer)
10911da177e4SLinus Torvalds 		inet_putpeer(peer);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds /*
10951da177e4SLinus Torvalds  * Peer allocation may fail only in serious out-of-memory conditions.  However
10961da177e4SLinus Torvalds  * we still can generate some output.
10971da177e4SLinus Torvalds  * Random ID selection looks a bit dangerous because we have no chances to
10981da177e4SLinus Torvalds  * select ID being unique in a reasonable period of time.
10991da177e4SLinus Torvalds  * But broken packet identifier may be better than no packet at all.
11001da177e4SLinus Torvalds  */
11011da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(ip_fb_id_lock);
11041da177e4SLinus Torvalds 	static u32 ip_fallback_id;
11051da177e4SLinus Torvalds 	u32 salt;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	spin_lock_bh(&ip_fb_id_lock);
1108e448515cSAl Viro 	salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
11091da177e4SLinus Torvalds 	iph->id = htons(salt & 0xFFFF);
11101da177e4SLinus Torvalds 	ip_fallback_id = salt;
11111da177e4SLinus Torvalds 	spin_unlock_bh(&ip_fb_id_lock);
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
11151da177e4SLinus Torvalds {
11161da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	if (rt) {
11191da177e4SLinus Torvalds 		if (rt->peer == NULL)
11201da177e4SLinus Torvalds 			rt_bind_peer(rt, 1);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 		/* If peer is attached to destination, it is never detached,
11231da177e4SLinus Torvalds 		   so that we need not to grab a lock to dereference it.
11241da177e4SLinus Torvalds 		 */
11251da177e4SLinus Torvalds 		if (rt->peer) {
11261da177e4SLinus Torvalds 			iph->id = htons(inet_getid(rt->peer, more));
11271da177e4SLinus Torvalds 			return;
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 	} else
11309c2b3328SStephen Hemminger 		printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
11319c2b3328SStephen Hemminger 		       __builtin_return_address(0));
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	ip_select_fb_ident(iph);
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds static void rt_del(unsigned hash, struct rtable *rt)
11371da177e4SLinus Torvalds {
113829e75252SEric Dumazet 	struct rtable **rthp, *aux;
11391da177e4SLinus Torvalds 
114029e75252SEric Dumazet 	rthp = &rt_hash_table[hash].chain;
114122c047ccSEric Dumazet 	spin_lock_bh(rt_hash_lock_addr(hash));
11421da177e4SLinus Torvalds 	ip_rt_put(rt);
114329e75252SEric Dumazet 	while ((aux = *rthp) != NULL) {
114429e75252SEric Dumazet 		if (aux == rt || (aux->rt_genid != atomic_read(&rt_genid))) {
114529e75252SEric Dumazet 			*rthp = aux->u.dst.rt_next;
114629e75252SEric Dumazet 			rt_free(aux);
114729e75252SEric Dumazet 			continue;
114829e75252SEric Dumazet 		}
114929e75252SEric Dumazet 		rthp = &aux->u.dst.rt_next;
11501da177e4SLinus Torvalds 	}
115122c047ccSEric Dumazet 	spin_unlock_bh(rt_hash_lock_addr(hash));
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
1154f7655229SAl Viro void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
1155f7655229SAl Viro 		    __be32 saddr, struct net_device *dev)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	int i, k;
11581da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
11591da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
1160f7655229SAl Viro 	__be32  skeys[2] = { saddr, 0 };
11611da177e4SLinus Torvalds 	int  ikeys[2] = { dev->ifindex, 0 };
11628d71740cSTom Tucker 	struct netevent_redirect netevent;
1163317805b8SDenis V. Lunev 	struct net *net;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (!in_dev)
11661da177e4SLinus Torvalds 		return;
11671da177e4SLinus Torvalds 
1168c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
11691da177e4SLinus Torvalds 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
11701e637c74SJan Engelhardt 	    || ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw)
1171f97c1e0cSJoe Perches 	    || ipv4_is_zeronet(new_gw))
11721da177e4SLinus Torvalds 		goto reject_redirect;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
11751da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
11761da177e4SLinus Torvalds 			goto reject_redirect;
11771da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
11781da177e4SLinus Torvalds 			goto reject_redirect;
11791da177e4SLinus Torvalds 	} else {
1180317805b8SDenis V. Lunev 		if (inet_addr_type(net, new_gw) != RTN_UNICAST)
11811da177e4SLinus Torvalds 			goto reject_redirect;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
11851da177e4SLinus Torvalds 		for (k = 0; k < 2; k++) {
11868c7bc840SAl Viro 			unsigned hash = rt_hash(daddr, skeys[i], ikeys[k]);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			rthp=&rt_hash_table[hash].chain;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 			rcu_read_lock();
11911da177e4SLinus Torvalds 			while ((rth = rcu_dereference(*rthp)) != NULL) {
11921da177e4SLinus Torvalds 				struct rtable *rt;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 				if (rth->fl.fl4_dst != daddr ||
11951da177e4SLinus Torvalds 				    rth->fl.fl4_src != skeys[i] ||
11961da177e4SLinus Torvalds 				    rth->fl.oif != ikeys[k] ||
119729e75252SEric Dumazet 				    rth->fl.iif != 0 ||
1198317805b8SDenis V. Lunev 				    rth->rt_genid != atomic_read(&rt_genid) ||
1199*878628fbSYOSHIFUJI Hideaki 				    !net_eq(dev_net(rth->u.dst.dev), net)) {
1200093c2ca4SEric Dumazet 					rthp = &rth->u.dst.rt_next;
12011da177e4SLinus Torvalds 					continue;
12021da177e4SLinus Torvalds 				}
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 				if (rth->rt_dst != daddr ||
12051da177e4SLinus Torvalds 				    rth->rt_src != saddr ||
12061da177e4SLinus Torvalds 				    rth->u.dst.error ||
12071da177e4SLinus Torvalds 				    rth->rt_gateway != old_gw ||
12081da177e4SLinus Torvalds 				    rth->u.dst.dev != dev)
12091da177e4SLinus Torvalds 					break;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 				dst_hold(&rth->u.dst);
12121da177e4SLinus Torvalds 				rcu_read_unlock();
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 				rt = dst_alloc(&ipv4_dst_ops);
12151da177e4SLinus Torvalds 				if (rt == NULL) {
12161da177e4SLinus Torvalds 					ip_rt_put(rth);
12171da177e4SLinus Torvalds 					in_dev_put(in_dev);
12181da177e4SLinus Torvalds 					return;
12191da177e4SLinus Torvalds 				}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 				/* Copy all the information. */
12221da177e4SLinus Torvalds 				*rt = *rth;
12231da177e4SLinus Torvalds 				INIT_RCU_HEAD(&rt->u.dst.rcu_head);
12241da177e4SLinus Torvalds 				rt->u.dst.__use		= 1;
12251da177e4SLinus Torvalds 				atomic_set(&rt->u.dst.__refcnt, 1);
12261da177e4SLinus Torvalds 				rt->u.dst.child		= NULL;
12271da177e4SLinus Torvalds 				if (rt->u.dst.dev)
12281da177e4SLinus Torvalds 					dev_hold(rt->u.dst.dev);
12291da177e4SLinus Torvalds 				if (rt->idev)
12301da177e4SLinus Torvalds 					in_dev_hold(rt->idev);
12311da177e4SLinus Torvalds 				rt->u.dst.obsolete	= 0;
12321da177e4SLinus Torvalds 				rt->u.dst.lastuse	= jiffies;
12331da177e4SLinus Torvalds 				rt->u.dst.path		= &rt->u.dst;
12341da177e4SLinus Torvalds 				rt->u.dst.neighbour	= NULL;
12351da177e4SLinus Torvalds 				rt->u.dst.hh		= NULL;
12361da177e4SLinus Torvalds 				rt->u.dst.xfrm		= NULL;
123729e75252SEric Dumazet 				rt->rt_genid		= atomic_read(&rt_genid);
12381da177e4SLinus Torvalds 				rt->rt_flags		|= RTCF_REDIRECTED;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 				/* Gateway is different ... */
12411da177e4SLinus Torvalds 				rt->rt_gateway		= new_gw;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 				/* Redirect received -> path was valid */
12441da177e4SLinus Torvalds 				dst_confirm(&rth->u.dst);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 				if (rt->peer)
12471da177e4SLinus Torvalds 					atomic_inc(&rt->peer->refcnt);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 				if (arp_bind_neighbour(&rt->u.dst) ||
12501da177e4SLinus Torvalds 				    !(rt->u.dst.neighbour->nud_state &
12511da177e4SLinus Torvalds 					    NUD_VALID)) {
12521da177e4SLinus Torvalds 					if (rt->u.dst.neighbour)
12531da177e4SLinus Torvalds 						neigh_event_send(rt->u.dst.neighbour, NULL);
12541da177e4SLinus Torvalds 					ip_rt_put(rth);
12551da177e4SLinus Torvalds 					rt_drop(rt);
12561da177e4SLinus Torvalds 					goto do_next;
12571da177e4SLinus Torvalds 				}
12581da177e4SLinus Torvalds 
12598d71740cSTom Tucker 				netevent.old = &rth->u.dst;
12608d71740cSTom Tucker 				netevent.new = &rt->u.dst;
12618d71740cSTom Tucker 				call_netevent_notifiers(NETEVENT_REDIRECT,
12628d71740cSTom Tucker 							&netevent);
12638d71740cSTom Tucker 
12641da177e4SLinus Torvalds 				rt_del(hash, rth);
12651da177e4SLinus Torvalds 				if (!rt_intern_hash(hash, rt, &rt))
12661da177e4SLinus Torvalds 					ip_rt_put(rt);
12671da177e4SLinus Torvalds 				goto do_next;
12681da177e4SLinus Torvalds 			}
12691da177e4SLinus Torvalds 			rcu_read_unlock();
12701da177e4SLinus Torvalds 		do_next:
12711da177e4SLinus Torvalds 			;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 	in_dev_put(in_dev);
12751da177e4SLinus Torvalds 	return;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds reject_redirect:
12781da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
12791da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
12801da177e4SLinus Torvalds 		printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
12811da177e4SLinus Torvalds 			"%u.%u.%u.%u ignored.\n"
1282cef2685eSIlia Sotnikov 			"  Advised path = %u.%u.%u.%u -> %u.%u.%u.%u\n",
12831da177e4SLinus Torvalds 		       NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
1284cef2685eSIlia Sotnikov 		       NIPQUAD(saddr), NIPQUAD(daddr));
12851da177e4SLinus Torvalds #endif
12861da177e4SLinus Torvalds 	in_dev_put(in_dev);
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)dst;
12921da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	if (rt) {
12951da177e4SLinus Torvalds 		if (dst->obsolete) {
12961da177e4SLinus Torvalds 			ip_rt_put(rt);
12971da177e4SLinus Torvalds 			ret = NULL;
12981da177e4SLinus Torvalds 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
12991da177e4SLinus Torvalds 			   rt->u.dst.expires) {
13008c7bc840SAl Viro 			unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
13018c7bc840SAl Viro 						rt->fl.oif);
13021da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 1
130356c99d04SDenis V. Lunev 			printk(KERN_DEBUG "ipv4_negative_advice: redirect to "
13041da177e4SLinus Torvalds 					  "%u.%u.%u.%u/%02x dropped\n",
13051da177e4SLinus Torvalds 				NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
13061da177e4SLinus Torvalds #endif
13071da177e4SLinus Torvalds 			rt_del(hash, rt);
13081da177e4SLinus Torvalds 			ret = NULL;
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 	return ret;
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds /*
13151da177e4SLinus Torvalds  * Algorithm:
13161da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
13171da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
13181da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
13191da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
13201da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
13211da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
13221da177e4SLinus Torvalds  *
13231da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
13241da177e4SLinus Torvalds  * in icmp.c.
13251da177e4SLinus Torvalds  *
13261da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
13271da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
13281da177e4SLinus Torvalds  */
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
13311da177e4SLinus Torvalds {
1332ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
13331da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	if (!in_dev)
13361da177e4SLinus Torvalds 		return;
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	if (!IN_DEV_TX_REDIRECTS(in_dev))
13391da177e4SLinus Torvalds 		goto out;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
13421da177e4SLinus Torvalds 	 * reset the algorithm.
13431da177e4SLinus Torvalds 	 */
13441da177e4SLinus Torvalds 	if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
13451da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = 0;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
13481da177e4SLinus Torvalds 	 * set u.dst.rate_last to the last seen redirected packet.
13491da177e4SLinus Torvalds 	 */
13501da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
13511da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
13521da177e4SLinus Torvalds 		goto out;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
13561da177e4SLinus Torvalds 	 * redirect.
13571da177e4SLinus Torvalds 	 */
135814fb8a76SLi Yewang 	if (rt->u.dst.rate_tokens == 0 ||
135914fb8a76SLi Yewang 	    time_after(jiffies,
13601da177e4SLinus Torvalds 		       (rt->u.dst.rate_last +
13611da177e4SLinus Torvalds 			(ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
13621da177e4SLinus Torvalds 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
13631da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
13641da177e4SLinus Torvalds 		++rt->u.dst.rate_tokens;
13651da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
13661da177e4SLinus Torvalds 		if (IN_DEV_LOG_MARTIANS(in_dev) &&
13671da177e4SLinus Torvalds 		    rt->u.dst.rate_tokens == ip_rt_redirect_number &&
13681da177e4SLinus Torvalds 		    net_ratelimit())
13691da177e4SLinus Torvalds 			printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
13701da177e4SLinus Torvalds 				"redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
13711da177e4SLinus Torvalds 				NIPQUAD(rt->rt_src), rt->rt_iif,
13721da177e4SLinus Torvalds 				NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
13731da177e4SLinus Torvalds #endif
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds out:
13761da177e4SLinus Torvalds 	in_dev_put(in_dev);
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
13801da177e4SLinus Torvalds {
1381ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
13821da177e4SLinus Torvalds 	unsigned long now;
13831da177e4SLinus Torvalds 	int code;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	switch (rt->u.dst.error) {
13861da177e4SLinus Torvalds 		case EINVAL:
13871da177e4SLinus Torvalds 		default:
13881da177e4SLinus Torvalds 			goto out;
13891da177e4SLinus Torvalds 		case EHOSTUNREACH:
13901da177e4SLinus Torvalds 			code = ICMP_HOST_UNREACH;
13911da177e4SLinus Torvalds 			break;
13921da177e4SLinus Torvalds 		case ENETUNREACH:
13931da177e4SLinus Torvalds 			code = ICMP_NET_UNREACH;
13947f53878dSMitsuru Chinen 			IP_INC_STATS_BH(IPSTATS_MIB_INNOROUTES);
13951da177e4SLinus Torvalds 			break;
13961da177e4SLinus Torvalds 		case EACCES:
13971da177e4SLinus Torvalds 			code = ICMP_PKT_FILTERED;
13981da177e4SLinus Torvalds 			break;
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	now = jiffies;
14021da177e4SLinus Torvalds 	rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
14031da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens > ip_rt_error_burst)
14041da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = ip_rt_error_burst;
14051da177e4SLinus Torvalds 	rt->u.dst.rate_last = now;
14061da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
14071da177e4SLinus Torvalds 		rt->u.dst.rate_tokens -= ip_rt_error_cost;
14081da177e4SLinus Torvalds 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
14091da177e4SLinus Torvalds 	}
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds out:	kfree_skb(skb);
14121da177e4SLinus Torvalds 	return 0;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds /*
14161da177e4SLinus Torvalds  *	The last two values are not from the RFC but
14171da177e4SLinus Torvalds  *	are needed for AMPRnet AX.25 paths.
14181da177e4SLinus Torvalds  */
14191da177e4SLinus Torvalds 
14209b5b5cffSArjan van de Ven static const unsigned short mtu_plateau[] =
14211da177e4SLinus Torvalds {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	int i;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
14281da177e4SLinus Torvalds 		if (old_mtu > mtu_plateau[i])
14291da177e4SLinus Torvalds 			return mtu_plateau[i];
14301da177e4SLinus Torvalds 	return 68;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
1433b5921910SDenis V. Lunev unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph,
1434b5921910SDenis V. Lunev 				 unsigned short new_mtu)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	int i;
14371da177e4SLinus Torvalds 	unsigned short old_mtu = ntohs(iph->tot_len);
14381da177e4SLinus Torvalds 	struct rtable *rth;
1439e448515cSAl Viro 	__be32  skeys[2] = { iph->saddr, 0, };
1440e448515cSAl Viro 	__be32  daddr = iph->daddr;
14411da177e4SLinus Torvalds 	unsigned short est_mtu = 0;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
14441da177e4SLinus Torvalds 		return 0;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
14478c7bc840SAl Viro 		unsigned hash = rt_hash(daddr, skeys[i], 0);
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 		rcu_read_lock();
14501da177e4SLinus Torvalds 		for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
1451093c2ca4SEric Dumazet 		     rth = rcu_dereference(rth->u.dst.rt_next)) {
14521da177e4SLinus Torvalds 			if (rth->fl.fl4_dst == daddr &&
14531da177e4SLinus Torvalds 			    rth->fl.fl4_src == skeys[i] &&
14541da177e4SLinus Torvalds 			    rth->rt_dst  == daddr &&
14551da177e4SLinus Torvalds 			    rth->rt_src  == iph->saddr &&
14561da177e4SLinus Torvalds 			    rth->fl.iif == 0 &&
1457b5921910SDenis V. Lunev 			    !(dst_metric_locked(&rth->u.dst, RTAX_MTU)) &&
1458*878628fbSYOSHIFUJI Hideaki 			    net_eq(dev_net(rth->u.dst.dev), net) &&
145929e75252SEric Dumazet 			    rth->rt_genid == atomic_read(&rt_genid)) {
14601da177e4SLinus Torvalds 				unsigned short mtu = new_mtu;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 				if (new_mtu < 68 || new_mtu >= old_mtu) {
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 					/* BSD 4.2 compatibility hack :-( */
14651da177e4SLinus Torvalds 					if (mtu == 0 &&
14661da177e4SLinus Torvalds 					    old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
14671da177e4SLinus Torvalds 					    old_mtu >= 68 + (iph->ihl << 2))
14681da177e4SLinus Torvalds 						old_mtu -= iph->ihl << 2;
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 					mtu = guess_mtu(old_mtu);
14711da177e4SLinus Torvalds 				}
14721da177e4SLinus Torvalds 				if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
14731da177e4SLinus Torvalds 					if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
14741da177e4SLinus Torvalds 						dst_confirm(&rth->u.dst);
14751da177e4SLinus Torvalds 						if (mtu < ip_rt_min_pmtu) {
14761da177e4SLinus Torvalds 							mtu = ip_rt_min_pmtu;
14771da177e4SLinus Torvalds 							rth->u.dst.metrics[RTAX_LOCK-1] |=
14781da177e4SLinus Torvalds 								(1 << RTAX_MTU);
14791da177e4SLinus Torvalds 						}
14801da177e4SLinus Torvalds 						rth->u.dst.metrics[RTAX_MTU-1] = mtu;
14811da177e4SLinus Torvalds 						dst_set_expires(&rth->u.dst,
14821da177e4SLinus Torvalds 							ip_rt_mtu_expires);
14831da177e4SLinus Torvalds 					}
14841da177e4SLinus Torvalds 					est_mtu = mtu;
14851da177e4SLinus Torvalds 				}
14861da177e4SLinus Torvalds 			}
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 		rcu_read_unlock();
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 	return est_mtu ? : new_mtu;
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
14961da177e4SLinus Torvalds 	    !(dst_metric_locked(dst, RTAX_MTU))) {
14971da177e4SLinus Torvalds 		if (mtu < ip_rt_min_pmtu) {
14981da177e4SLinus Torvalds 			mtu = ip_rt_min_pmtu;
14991da177e4SLinus Torvalds 			dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
15001da177e4SLinus Torvalds 		}
15011da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
15021da177e4SLinus Torvalds 		dst_set_expires(dst, ip_rt_mtu_expires);
15038d71740cSTom Tucker 		call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
15041da177e4SLinus Torvalds 	}
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	return NULL;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
15151da177e4SLinus Torvalds 	struct inet_peer *peer = rt->peer;
15161da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	if (peer) {
15191da177e4SLinus Torvalds 		rt->peer = NULL;
15201da177e4SLinus Torvalds 		inet_putpeer(peer);
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	if (idev) {
15241da177e4SLinus Torvalds 		rt->idev = NULL;
15251da177e4SLinus Torvalds 		in_dev_put(idev);
15261da177e4SLinus Torvalds 	}
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
15301da177e4SLinus Torvalds 			    int how)
15311da177e4SLinus Torvalds {
15321da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
15331da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
1534c346dca1SYOSHIFUJI Hideaki 	if (dev != dev_net(dev)->loopback_dev && idev && idev->dev == dev) {
15355a3e55d6SDenis V. Lunev 		struct in_device *loopback_idev =
1536c346dca1SYOSHIFUJI Hideaki 			in_dev_get(dev_net(dev)->loopback_dev);
15371da177e4SLinus Torvalds 		if (loopback_idev) {
15381da177e4SLinus Torvalds 			rt->idev = loopback_idev;
15391da177e4SLinus Torvalds 			in_dev_put(idev);
15401da177e4SLinus Torvalds 		}
15411da177e4SLinus Torvalds 	}
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	struct rtable *rt;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
15491da177e4SLinus Torvalds 
1550ee6b9673SEric Dumazet 	rt = skb->rtable;
15511da177e4SLinus Torvalds 	if (rt)
15521da177e4SLinus Torvalds 		dst_set_expires(&rt->u.dst, 0);
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
1558eddc9ec5SArnaldo Carvalho de Melo 		NIPQUAD(ip_hdr(skb)->saddr), NIPQUAD(ip_hdr(skb)->daddr),
15591da177e4SLinus Torvalds 		skb->dev ? skb->dev->name : "?");
15601da177e4SLinus Torvalds 	kfree_skb(skb);
15611da177e4SLinus Torvalds 	return 0;
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds /*
15651da177e4SLinus Torvalds    We do not cache source address of outgoing interface,
15661da177e4SLinus Torvalds    because it is used only by IP RR, TS and SRR options,
15671da177e4SLinus Torvalds    so that it out of fast path.
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds    BTW remember: "addr" is allowed to be not aligned
15701da177e4SLinus Torvalds    in IP options!
15711da177e4SLinus Torvalds  */
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds void ip_rt_get_source(u8 *addr, struct rtable *rt)
15741da177e4SLinus Torvalds {
1575a61ced5dSAl Viro 	__be32 src;
15761da177e4SLinus Torvalds 	struct fib_result res;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if (rt->fl.iif == 0)
15791da177e4SLinus Torvalds 		src = rt->rt_src;
1580c346dca1SYOSHIFUJI Hideaki 	else if (fib_lookup(dev_net(rt->u.dst.dev), &rt->fl, &res) == 0) {
15811da177e4SLinus Torvalds 		src = FIB_RES_PREFSRC(res);
15821da177e4SLinus Torvalds 		fib_res_put(&res);
15831da177e4SLinus Torvalds 	} else
15841da177e4SLinus Torvalds 		src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
15851da177e4SLinus Torvalds 					RT_SCOPE_UNIVERSE);
15861da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
15901da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF))
15931da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF;
15941da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF0000))
15951da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF0000;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds #endif
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
16001da177e4SLinus Torvalds {
16011da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	if (fi) {
16041da177e4SLinus Torvalds 		if (FIB_RES_GW(*res) &&
16051da177e4SLinus Torvalds 		    FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
16061da177e4SLinus Torvalds 			rt->rt_gateway = FIB_RES_GW(*res);
16071da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, fi->fib_metrics,
16081da177e4SLinus Torvalds 		       sizeof(rt->u.dst.metrics));
16091da177e4SLinus Torvalds 		if (fi->fib_mtu == 0) {
16101da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
16111da177e4SLinus Torvalds 			if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
16121da177e4SLinus Torvalds 			    rt->rt_gateway != rt->rt_dst &&
16131da177e4SLinus Torvalds 			    rt->u.dst.dev->mtu > 576)
16141da177e4SLinus Torvalds 				rt->u.dst.metrics[RTAX_MTU-1] = 576;
16151da177e4SLinus Torvalds 		}
16161da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
16171da177e4SLinus Torvalds 		rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
16181da177e4SLinus Torvalds #endif
16191da177e4SLinus Torvalds 	} else
16201da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
16231da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
16241da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
16251da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
16261da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
16271da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
16281da177e4SLinus Torvalds 				       ip_rt_min_advmss);
16291da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
16301da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
16331da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
16341da177e4SLinus Torvalds 	set_class_tag(rt, fib_rules_tclass(res));
16351da177e4SLinus Torvalds #endif
16361da177e4SLinus Torvalds 	set_class_tag(rt, itag);
16371da177e4SLinus Torvalds #endif
16381da177e4SLinus Torvalds 	rt->rt_type = res->type;
16391da177e4SLinus Torvalds }
16401da177e4SLinus Torvalds 
16419e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
16421da177e4SLinus Torvalds 				u8 tos, struct net_device *dev, int our)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	unsigned hash;
16451da177e4SLinus Torvalds 	struct rtable *rth;
1646a61ced5dSAl Viro 	__be32 spec_dst;
16471da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
16481da177e4SLinus Torvalds 	u32 itag = 0;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	/* Primary sanity checks. */
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	if (in_dev == NULL)
16531da177e4SLinus Torvalds 		return -EINVAL;
16541da177e4SLinus Torvalds 
16551e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1656f97c1e0cSJoe Perches 	    ipv4_is_loopback(saddr) || skb->protocol != htons(ETH_P_IP))
16571da177e4SLinus Torvalds 		goto e_inval;
16581da177e4SLinus Torvalds 
1659f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr)) {
1660f97c1e0cSJoe Perches 		if (!ipv4_is_local_multicast(daddr))
16611da177e4SLinus Torvalds 			goto e_inval;
16621da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
16631da177e4SLinus Torvalds 	} else if (fib_validate_source(saddr, 0, tos, 0,
16641da177e4SLinus Torvalds 					dev, &spec_dst, &itag) < 0)
16651da177e4SLinus Torvalds 		goto e_inval;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
16681da177e4SLinus Torvalds 	if (!rth)
16691da177e4SLinus Torvalds 		goto e_nobufs;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
16741da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
167542f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
16761da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
16771da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
16781da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
16791da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
168047dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
16811da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
16821da177e4SLinus Torvalds 	rth->rt_src	= saddr;
16831da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
16841da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
16851da177e4SLinus Torvalds #endif
16861da177e4SLinus Torvalds 	rth->rt_iif	=
16871da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
16882774c7abSEric W. Biederman 	rth->u.dst.dev	= init_net.loopback_dev;
16891da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
16901da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
16911da177e4SLinus Torvalds 	rth->fl.oif	= 0;
16921da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
16931da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
169429e75252SEric Dumazet 	rth->rt_genid	= atomic_read(&rt_genid);
16951da177e4SLinus Torvalds 	rth->rt_flags	= RTCF_MULTICAST;
169629e75252SEric Dumazet 	rth->rt_type	= RTN_MULTICAST;
16971da177e4SLinus Torvalds 	if (our) {
16981da177e4SLinus Torvalds 		rth->u.dst.input= ip_local_deliver;
16991da177e4SLinus Torvalds 		rth->rt_flags |= RTCF_LOCAL;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1703f97c1e0cSJoe Perches 	if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
17041da177e4SLinus Torvalds 		rth->u.dst.input = ip_mr_input;
17051da177e4SLinus Torvalds #endif
17061da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	in_dev_put(in_dev);
17098c7bc840SAl Viro 	hash = rt_hash(daddr, saddr, dev->ifindex);
1710ee6b9673SEric Dumazet 	return rt_intern_hash(hash, rth, &skb->rtable);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds e_nobufs:
17131da177e4SLinus Torvalds 	in_dev_put(in_dev);
17141da177e4SLinus Torvalds 	return -ENOBUFS;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds e_inval:
17171da177e4SLinus Torvalds 	in_dev_put(in_dev);
17181da177e4SLinus Torvalds 	return -EINVAL;
17191da177e4SLinus Torvalds }
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
17231da177e4SLinus Torvalds 				     struct in_device *in_dev,
17241da177e4SLinus Torvalds 				     struct sk_buff *skb,
17259e12bb22SAl Viro 				     __be32 daddr,
17269e12bb22SAl Viro 				     __be32 saddr)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
17291da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
17301da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
17311da177e4SLinus Torvalds 		/*
17321da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
17331da177e4SLinus Torvalds 		 *	the only hint is MAC header.
17341da177e4SLinus Torvalds 		 */
17351da177e4SLinus Torvalds 		printk(KERN_WARNING "martian source %u.%u.%u.%u from "
17361da177e4SLinus Torvalds 			"%u.%u.%u.%u, on dev %s\n",
17371da177e4SLinus Torvalds 			NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
173898e399f8SArnaldo Carvalho de Melo 		if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
17391da177e4SLinus Torvalds 			int i;
174098e399f8SArnaldo Carvalho de Melo 			const unsigned char *p = skb_mac_header(skb);
17411da177e4SLinus Torvalds 			printk(KERN_WARNING "ll header: ");
17421da177e4SLinus Torvalds 			for (i = 0; i < dev->hard_header_len; i++, p++) {
17431da177e4SLinus Torvalds 				printk("%02x", *p);
17441da177e4SLinus Torvalds 				if (i < (dev->hard_header_len - 1))
17451da177e4SLinus Torvalds 					printk(":");
17461da177e4SLinus Torvalds 			}
17471da177e4SLinus Torvalds 			printk("\n");
17481da177e4SLinus Torvalds 		}
17491da177e4SLinus Torvalds 	}
17501da177e4SLinus Torvalds #endif
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds static inline int __mkroute_input(struct sk_buff *skb,
17541da177e4SLinus Torvalds 				  struct fib_result* res,
17551da177e4SLinus Torvalds 				  struct in_device *in_dev,
17569e12bb22SAl Viro 				  __be32 daddr, __be32 saddr, u32 tos,
17571da177e4SLinus Torvalds 				  struct rtable **result)
17581da177e4SLinus Torvalds {
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	struct rtable *rth;
17611da177e4SLinus Torvalds 	int err;
17621da177e4SLinus Torvalds 	struct in_device *out_dev;
17631da177e4SLinus Torvalds 	unsigned flags = 0;
1764d9c9df8cSAl Viro 	__be32 spec_dst;
1765d9c9df8cSAl Viro 	u32 itag;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	/* get a working reference to the output device */
17681da177e4SLinus Torvalds 	out_dev = in_dev_get(FIB_RES_DEV(*res));
17691da177e4SLinus Torvalds 	if (out_dev == NULL) {
17701da177e4SLinus Torvalds 		if (net_ratelimit())
17711da177e4SLinus Torvalds 			printk(KERN_CRIT "Bug in ip_route_input" \
17721da177e4SLinus Torvalds 			       "_slow(). Please, report\n");
17731da177e4SLinus Torvalds 		return -EINVAL;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
17781da177e4SLinus Torvalds 				  in_dev->dev, &spec_dst, &itag);
17791da177e4SLinus Torvalds 	if (err < 0) {
17801da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
17811da177e4SLinus Torvalds 					 saddr);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 		err = -EINVAL;
17841da177e4SLinus Torvalds 		goto cleanup;
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	if (err)
17881da177e4SLinus Torvalds 		flags |= RTCF_DIRECTSRC;
17891da177e4SLinus Torvalds 
1790cb7928a5SRami Rosen 	if (out_dev == in_dev && err && !(flags & RTCF_MASQ) &&
17911da177e4SLinus Torvalds 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
17921da177e4SLinus Torvalds 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
17931da177e4SLinus Torvalds 		flags |= RTCF_DOREDIRECT;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
17961da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
17971da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
17981da177e4SLinus Torvalds 		 */
1799cb7928a5SRami Rosen 		if (out_dev == in_dev) {
18001da177e4SLinus Torvalds 			err = -EINVAL;
18011da177e4SLinus Torvalds 			goto cleanup;
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 	}
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
18071da177e4SLinus Torvalds 	if (!rth) {
18081da177e4SLinus Torvalds 		err = -ENOBUFS;
18091da177e4SLinus Torvalds 		goto cleanup;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
1812ce723d8eSJulian Anastasov 	atomic_set(&rth->u.dst.__refcnt, 1);
18131da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
181442f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
18151da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
181642f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(out_dev, NOXFRM))
18171da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
18181da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
18191da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
18201da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
182147dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
18221da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
18231da177e4SLinus Torvalds 	rth->rt_src	= saddr;
18241da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
18251da177e4SLinus Torvalds 	rth->rt_iif 	=
18261da177e4SLinus Torvalds 		rth->fl.iif	= in_dev->dev->ifindex;
18271da177e4SLinus Torvalds 	rth->u.dst.dev	= (out_dev)->dev;
18281da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
18291da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
18301da177e4SLinus Torvalds 	rth->fl.oif 	= 0;
18311da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	rth->u.dst.input = ip_forward;
18341da177e4SLinus Torvalds 	rth->u.dst.output = ip_output;
183529e75252SEric Dumazet 	rth->rt_genid = atomic_read(&rt_genid);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, itag);
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	rth->rt_flags = flags;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	*result = rth;
18421da177e4SLinus Torvalds 	err = 0;
18431da177e4SLinus Torvalds  cleanup:
18441da177e4SLinus Torvalds 	/* release the working reference to the output device */
18451da177e4SLinus Torvalds 	in_dev_put(out_dev);
18461da177e4SLinus Torvalds 	return err;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
1849e06e7c61SDavid S. Miller static inline int ip_mkroute_input(struct sk_buff *skb,
18501da177e4SLinus Torvalds 				   struct fib_result* res,
18511da177e4SLinus Torvalds 				   const struct flowi *fl,
18521da177e4SLinus Torvalds 				   struct in_device *in_dev,
18539e12bb22SAl Viro 				   __be32 daddr, __be32 saddr, u32 tos)
18541da177e4SLinus Torvalds {
18557abaa27cSChuck Short 	struct rtable* rth = NULL;
18561da177e4SLinus Torvalds 	int err;
18571da177e4SLinus Torvalds 	unsigned hash;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
18601da177e4SLinus Torvalds 	if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
18611da177e4SLinus Torvalds 		fib_select_multipath(fl, res);
18621da177e4SLinus Torvalds #endif
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	/* create a routing cache entry */
18651da177e4SLinus Torvalds 	err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
18661da177e4SLinus Torvalds 	if (err)
18671da177e4SLinus Torvalds 		return err;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	/* put it into the cache */
18708c7bc840SAl Viro 	hash = rt_hash(daddr, saddr, fl->iif);
1871ee6b9673SEric Dumazet 	return rt_intern_hash(hash, rth, &skb->rtable);
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /*
18751da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
18761da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
18771da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  *	Such approach solves two big problems:
18801da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
18811da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
18821da177e4SLinus Torvalds  */
18831da177e4SLinus Torvalds 
18849e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
18851da177e4SLinus Torvalds 			       u8 tos, struct net_device *dev)
18861da177e4SLinus Torvalds {
18871da177e4SLinus Torvalds 	struct fib_result res;
18881da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
18891da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
18901da177e4SLinus Torvalds 				      { .daddr = daddr,
18911da177e4SLinus Torvalds 					.saddr = saddr,
18921da177e4SLinus Torvalds 					.tos = tos,
18931da177e4SLinus Torvalds 					.scope = RT_SCOPE_UNIVERSE,
18941da177e4SLinus Torvalds 				      } },
189547dcf0cbSThomas Graf 			    .mark = skb->mark,
18961da177e4SLinus Torvalds 			    .iif = dev->ifindex };
18971da177e4SLinus Torvalds 	unsigned	flags = 0;
18981da177e4SLinus Torvalds 	u32		itag = 0;
18991da177e4SLinus Torvalds 	struct rtable * rth;
19001da177e4SLinus Torvalds 	unsigned	hash;
19019e12bb22SAl Viro 	__be32		spec_dst;
19021da177e4SLinus Torvalds 	int		err = -EINVAL;
19031da177e4SLinus Torvalds 	int		free_res = 0;
1904c346dca1SYOSHIFUJI Hideaki 	struct net    * net = dev_net(dev);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	/* IP on this device is disabled. */
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	if (!in_dev)
19091da177e4SLinus Torvalds 		goto out;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
19121da177e4SLinus Torvalds 	   by fib_lookup.
19131da177e4SLinus Torvalds 	 */
19141da177e4SLinus Torvalds 
19151e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1916f97c1e0cSJoe Perches 	    ipv4_is_loopback(saddr))
19171da177e4SLinus Torvalds 		goto martian_source;
19181da177e4SLinus Torvalds 
1919e448515cSAl Viro 	if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
19201da177e4SLinus Torvalds 		goto brd_input;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
19231da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
19241da177e4SLinus Torvalds 	 */
1925f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
19261da177e4SLinus Torvalds 		goto martian_source;
19271da177e4SLinus Torvalds 
19281e637c74SJan Engelhardt 	if (ipv4_is_lbcast(daddr) || ipv4_is_zeronet(daddr) ||
1929f97c1e0cSJoe Perches 	    ipv4_is_loopback(daddr))
19301da177e4SLinus Torvalds 		goto martian_destination;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	/*
19331da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
19341da177e4SLinus Torvalds 	 */
193584a885f4SDenis V. Lunev 	if ((err = fib_lookup(net, &fl, &res)) != 0) {
19361da177e4SLinus Torvalds 		if (!IN_DEV_FORWARD(in_dev))
19372c2910a4SDietmar Eggemann 			goto e_hostunreach;
19381da177e4SLinus Torvalds 		goto no_route;
19391da177e4SLinus Torvalds 	}
19401da177e4SLinus Torvalds 	free_res = 1;
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_tot);
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	if (res.type == RTN_BROADCAST)
19451da177e4SLinus Torvalds 		goto brd_input;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
19481da177e4SLinus Torvalds 		int result;
19491da177e4SLinus Torvalds 		result = fib_validate_source(saddr, daddr, tos,
195084a885f4SDenis V. Lunev 					     net->loopback_dev->ifindex,
19511da177e4SLinus Torvalds 					     dev, &spec_dst, &itag);
19521da177e4SLinus Torvalds 		if (result < 0)
19531da177e4SLinus Torvalds 			goto martian_source;
19541da177e4SLinus Torvalds 		if (result)
19551da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
19561da177e4SLinus Torvalds 		spec_dst = daddr;
19571da177e4SLinus Torvalds 		goto local_input;
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	if (!IN_DEV_FORWARD(in_dev))
19612c2910a4SDietmar Eggemann 		goto e_hostunreach;
19621da177e4SLinus Torvalds 	if (res.type != RTN_UNICAST)
19631da177e4SLinus Torvalds 		goto martian_destination;
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
19661da177e4SLinus Torvalds done:
19671da177e4SLinus Torvalds 	in_dev_put(in_dev);
19681da177e4SLinus Torvalds 	if (free_res)
19691da177e4SLinus Torvalds 		fib_res_put(&res);
19701da177e4SLinus Torvalds out:	return err;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds brd_input:
19731da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
19741da177e4SLinus Torvalds 		goto e_inval;
19751da177e4SLinus Torvalds 
1976f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
19771da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
19781da177e4SLinus Torvalds 	else {
19791da177e4SLinus Torvalds 		err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
19801da177e4SLinus Torvalds 					  &itag);
19811da177e4SLinus Torvalds 		if (err < 0)
19821da177e4SLinus Torvalds 			goto martian_source;
19831da177e4SLinus Torvalds 		if (err)
19841da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
19851da177e4SLinus Torvalds 	}
19861da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
19871da177e4SLinus Torvalds 	res.type = RTN_BROADCAST;
19881da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds local_input:
19911da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
19921da177e4SLinus Torvalds 	if (!rth)
19931da177e4SLinus Torvalds 		goto e_nobufs;
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
199629e75252SEric Dumazet 	rth->rt_genid = atomic_read(&rt_genid);
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
19991da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
200042f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
20011da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
20021da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
20031da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
20041da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
200547dcf0cbSThomas Graf 	rth->fl.mark    = skb->mark;
20061da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
20071da177e4SLinus Torvalds 	rth->rt_src	= saddr;
20081da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
20091da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
20101da177e4SLinus Torvalds #endif
20111da177e4SLinus Torvalds 	rth->rt_iif	=
20121da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
201384a885f4SDenis V. Lunev 	rth->u.dst.dev	= net->loopback_dev;
20141da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
20151da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
20161da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
20171da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
20181da177e4SLinus Torvalds 	rth->u.dst.input= ip_local_deliver;
20191da177e4SLinus Torvalds 	rth->rt_flags 	= flags|RTCF_LOCAL;
20201da177e4SLinus Torvalds 	if (res.type == RTN_UNREACHABLE) {
20211da177e4SLinus Torvalds 		rth->u.dst.input= ip_error;
20221da177e4SLinus Torvalds 		rth->u.dst.error= -err;
20231da177e4SLinus Torvalds 		rth->rt_flags 	&= ~RTCF_LOCAL;
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 	rth->rt_type	= res.type;
20268c7bc840SAl Viro 	hash = rt_hash(daddr, saddr, fl.iif);
2027ee6b9673SEric Dumazet 	err = rt_intern_hash(hash, rth, &skb->rtable);
20281da177e4SLinus Torvalds 	goto done;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds no_route:
20311da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
20321da177e4SLinus Torvalds 	spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
20331da177e4SLinus Torvalds 	res.type = RTN_UNREACHABLE;
20347f53878dSMitsuru Chinen 	if (err == -ESRCH)
20357f53878dSMitsuru Chinen 		err = -ENETUNREACH;
20361da177e4SLinus Torvalds 	goto local_input;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	/*
20391da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
20401da177e4SLinus Torvalds 	 */
20411da177e4SLinus Torvalds martian_destination:
20421da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
20431da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
20441da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
20451da177e4SLinus Torvalds 		printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
20461da177e4SLinus Torvalds 			"%u.%u.%u.%u, dev %s\n",
20471da177e4SLinus Torvalds 			NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
20481da177e4SLinus Torvalds #endif
20492c2910a4SDietmar Eggemann 
20502c2910a4SDietmar Eggemann e_hostunreach:
20512c2910a4SDietmar Eggemann 	err = -EHOSTUNREACH;
20522c2910a4SDietmar Eggemann 	goto done;
20532c2910a4SDietmar Eggemann 
20541da177e4SLinus Torvalds e_inval:
20551da177e4SLinus Torvalds 	err = -EINVAL;
20561da177e4SLinus Torvalds 	goto done;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds e_nobufs:
20591da177e4SLinus Torvalds 	err = -ENOBUFS;
20601da177e4SLinus Torvalds 	goto done;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds martian_source:
20631da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
20641da177e4SLinus Torvalds 	goto e_inval;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20679e12bb22SAl Viro int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
20681da177e4SLinus Torvalds 		   u8 tos, struct net_device *dev)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	struct rtable * rth;
20711da177e4SLinus Torvalds 	unsigned	hash;
20721da177e4SLinus Torvalds 	int iif = dev->ifindex;
2073b5921910SDenis V. Lunev 	struct net *net;
20741da177e4SLinus Torvalds 
2075c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
20761da177e4SLinus Torvalds 	tos &= IPTOS_RT_MASK;
20778c7bc840SAl Viro 	hash = rt_hash(daddr, saddr, iif);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	rcu_read_lock();
20801da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2081093c2ca4SEric Dumazet 	     rth = rcu_dereference(rth->u.dst.rt_next)) {
20821da177e4SLinus Torvalds 		if (rth->fl.fl4_dst == daddr &&
20831da177e4SLinus Torvalds 		    rth->fl.fl4_src == saddr &&
20841da177e4SLinus Torvalds 		    rth->fl.iif == iif &&
20851da177e4SLinus Torvalds 		    rth->fl.oif == 0 &&
208647dcf0cbSThomas Graf 		    rth->fl.mark == skb->mark &&
2087b5921910SDenis V. Lunev 		    rth->fl.fl4_tos == tos &&
2088*878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(rth->u.dst.dev), net) &&
208929e75252SEric Dumazet 		    rth->rt_genid == atomic_read(&rt_genid)) {
209003f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, jiffies);
20911da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(in_hit);
20921da177e4SLinus Torvalds 			rcu_read_unlock();
2093ee6b9673SEric Dumazet 			skb->rtable = rth;
20941da177e4SLinus Torvalds 			return 0;
20951da177e4SLinus Torvalds 		}
20961da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(in_hlist_search);
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 	rcu_read_unlock();
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
21011da177e4SLinus Torvalds 	   The problem was that too many Ethernet cards have broken/missing
21021da177e4SLinus Torvalds 	   hardware multicast filters :-( As result the host on multicasting
21031da177e4SLinus Torvalds 	   network acquires a lot of useless route cache entries, sort of
21041da177e4SLinus Torvalds 	   SDR messages from all the world. Now we try to get rid of them.
21051da177e4SLinus Torvalds 	   Really, provided software IP multicast filter is organized
21061da177e4SLinus Torvalds 	   reasonably (at least, hashed), it does not result in a slowdown
21071da177e4SLinus Torvalds 	   comparing with route cache reject entries.
21081da177e4SLinus Torvalds 	   Note, that multicast routers are not affected, because
21091da177e4SLinus Torvalds 	   route cache entry is created eventually.
21101da177e4SLinus Torvalds 	 */
2111f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
21121da177e4SLinus Torvalds 		struct in_device *in_dev;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 		rcu_read_lock();
2115e5ed6399SHerbert Xu 		if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
21161da177e4SLinus Torvalds 			int our = ip_check_mc(in_dev, daddr, saddr,
2117eddc9ec5SArnaldo Carvalho de Melo 				ip_hdr(skb)->protocol);
21181da177e4SLinus Torvalds 			if (our
21191da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2120f97c1e0cSJoe Perches 			    || (!ipv4_is_local_multicast(daddr) &&
2121f97c1e0cSJoe Perches 				IN_DEV_MFORWARD(in_dev))
21221da177e4SLinus Torvalds #endif
21231da177e4SLinus Torvalds 			    ) {
21241da177e4SLinus Torvalds 				rcu_read_unlock();
21251da177e4SLinus Torvalds 				return ip_route_input_mc(skb, daddr, saddr,
21261da177e4SLinus Torvalds 							 tos, dev, our);
21271da177e4SLinus Torvalds 			}
21281da177e4SLinus Torvalds 		}
21291da177e4SLinus Torvalds 		rcu_read_unlock();
21301da177e4SLinus Torvalds 		return -EINVAL;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 	return ip_route_input_slow(skb, daddr, saddr, tos, dev);
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds static inline int __mkroute_output(struct rtable **result,
21361da177e4SLinus Torvalds 				   struct fib_result* res,
21371da177e4SLinus Torvalds 				   const struct flowi *fl,
21381da177e4SLinus Torvalds 				   const struct flowi *oldflp,
21391da177e4SLinus Torvalds 				   struct net_device *dev_out,
21401da177e4SLinus Torvalds 				   unsigned flags)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds 	struct rtable *rth;
21431da177e4SLinus Torvalds 	struct in_device *in_dev;
21441da177e4SLinus Torvalds 	u32 tos = RT_FL_TOS(oldflp);
21451da177e4SLinus Torvalds 	int err = 0;
21461da177e4SLinus Torvalds 
2147f97c1e0cSJoe Perches 	if (ipv4_is_loopback(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
21481da177e4SLinus Torvalds 		return -EINVAL;
21491da177e4SLinus Torvalds 
2150e448515cSAl Viro 	if (fl->fl4_dst == htonl(0xFFFFFFFF))
21511da177e4SLinus Torvalds 		res->type = RTN_BROADCAST;
2152f97c1e0cSJoe Perches 	else if (ipv4_is_multicast(fl->fl4_dst))
21531da177e4SLinus Torvalds 		res->type = RTN_MULTICAST;
21541e637c74SJan Engelhardt 	else if (ipv4_is_lbcast(fl->fl4_dst) || ipv4_is_zeronet(fl->fl4_dst))
21551da177e4SLinus Torvalds 		return -EINVAL;
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
21581da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	/* get work reference to inet device */
21611da177e4SLinus Torvalds 	in_dev = in_dev_get(dev_out);
21621da177e4SLinus Torvalds 	if (!in_dev)
21631da177e4SLinus Torvalds 		return -EINVAL;
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	if (res->type == RTN_BROADCAST) {
21661da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
21671da177e4SLinus Torvalds 		if (res->fi) {
21681da177e4SLinus Torvalds 			fib_info_put(res->fi);
21691da177e4SLinus Torvalds 			res->fi = NULL;
21701da177e4SLinus Torvalds 		}
21711da177e4SLinus Torvalds 	} else if (res->type == RTN_MULTICAST) {
21721da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST|RTCF_LOCAL;
21731da177e4SLinus Torvalds 		if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
21741da177e4SLinus Torvalds 				 oldflp->proto))
21751da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
21761da177e4SLinus Torvalds 		/* If multicast route do not exist use
21771da177e4SLinus Torvalds 		   default one, but do not gateway in this case.
21781da177e4SLinus Torvalds 		   Yes, it is hack.
21791da177e4SLinus Torvalds 		 */
21801da177e4SLinus Torvalds 		if (res->fi && res->prefixlen < 4) {
21811da177e4SLinus Torvalds 			fib_info_put(res->fi);
21821da177e4SLinus Torvalds 			res->fi = NULL;
21831da177e4SLinus Torvalds 		}
21841da177e4SLinus Torvalds 	}
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
21881da177e4SLinus Torvalds 	if (!rth) {
21891da177e4SLinus Torvalds 		err = -ENOBUFS;
21901da177e4SLinus Torvalds 		goto cleanup;
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 
2193ce723d8eSJulian Anastasov 	atomic_set(&rth->u.dst.__refcnt, 1);
21941da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
219542f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOXFRM))
21961da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
219742f811b8SHerbert Xu 	if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
21981da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	rth->fl.fl4_dst	= oldflp->fl4_dst;
22011da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
22021da177e4SLinus Torvalds 	rth->fl.fl4_src	= oldflp->fl4_src;
22031da177e4SLinus Torvalds 	rth->fl.oif	= oldflp->oif;
220447dcf0cbSThomas Graf 	rth->fl.mark    = oldflp->mark;
22051da177e4SLinus Torvalds 	rth->rt_dst	= fl->fl4_dst;
22061da177e4SLinus Torvalds 	rth->rt_src	= fl->fl4_src;
22071da177e4SLinus Torvalds 	rth->rt_iif	= oldflp->oif ? : dev_out->ifindex;
22081da177e4SLinus Torvalds 	/* get references to the devices that are to be hold by the routing
22091da177e4SLinus Torvalds 	   cache entry */
22101da177e4SLinus Torvalds 	rth->u.dst.dev	= dev_out;
22111da177e4SLinus Torvalds 	dev_hold(dev_out);
22121da177e4SLinus Torvalds 	rth->idev	= in_dev_get(dev_out);
22131da177e4SLinus Torvalds 	rth->rt_gateway = fl->fl4_dst;
22141da177e4SLinus Torvalds 	rth->rt_spec_dst= fl->fl4_src;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	rth->u.dst.output=ip_output;
221729e75252SEric Dumazet 	rth->rt_genid = atomic_read(&rt_genid);
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	if (flags & RTCF_LOCAL) {
22221da177e4SLinus Torvalds 		rth->u.dst.input = ip_local_deliver;
22231da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_dst;
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
22261da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_src;
22271da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
22281da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
22291da177e4SLinus Torvalds 			rth->u.dst.output = ip_mc_output;
22301da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
22311da177e4SLinus Torvalds 		}
22321da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
22331da177e4SLinus Torvalds 		if (res->type == RTN_MULTICAST) {
22341da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
2235f97c1e0cSJoe Perches 			    !ipv4_is_local_multicast(oldflp->fl4_dst)) {
22361da177e4SLinus Torvalds 				rth->u.dst.input = ip_mr_input;
22371da177e4SLinus Torvalds 				rth->u.dst.output = ip_mc_output;
22381da177e4SLinus Torvalds 			}
22391da177e4SLinus Torvalds 		}
22401da177e4SLinus Torvalds #endif
22411da177e4SLinus Torvalds 	}
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, 0);
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	rth->rt_flags = flags;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	*result = rth;
22481da177e4SLinus Torvalds  cleanup:
22491da177e4SLinus Torvalds 	/* release work reference to inet device */
22501da177e4SLinus Torvalds 	in_dev_put(in_dev);
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	return err;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
2255e06e7c61SDavid S. Miller static inline int ip_mkroute_output(struct rtable **rp,
22561da177e4SLinus Torvalds 				    struct fib_result* res,
22571da177e4SLinus Torvalds 				    const struct flowi *fl,
22581da177e4SLinus Torvalds 				    const struct flowi *oldflp,
22591da177e4SLinus Torvalds 				    struct net_device *dev_out,
22601da177e4SLinus Torvalds 				    unsigned flags)
22611da177e4SLinus Torvalds {
22627abaa27cSChuck Short 	struct rtable *rth = NULL;
22631da177e4SLinus Torvalds 	int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
22641da177e4SLinus Torvalds 	unsigned hash;
22651da177e4SLinus Torvalds 	if (err == 0) {
22668c7bc840SAl Viro 		hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif);
22671da177e4SLinus Torvalds 		err = rt_intern_hash(hash, rth, rp);
22681da177e4SLinus Torvalds 	}
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 	return err;
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds /*
22741da177e4SLinus Torvalds  * Major route resolver routine.
22751da177e4SLinus Torvalds  */
22761da177e4SLinus Torvalds 
2277b40afd0eSDenis V. Lunev static int ip_route_output_slow(struct net *net, struct rtable **rp,
2278b40afd0eSDenis V. Lunev 				const struct flowi *oldflp)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	u32 tos	= RT_FL_TOS(oldflp);
22811da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
22821da177e4SLinus Torvalds 				      { .daddr = oldflp->fl4_dst,
22831da177e4SLinus Torvalds 					.saddr = oldflp->fl4_src,
22841da177e4SLinus Torvalds 					.tos = tos & IPTOS_RT_MASK,
22851da177e4SLinus Torvalds 					.scope = ((tos & RTO_ONLINK) ?
22861da177e4SLinus Torvalds 						  RT_SCOPE_LINK :
22871da177e4SLinus Torvalds 						  RT_SCOPE_UNIVERSE),
22881da177e4SLinus Torvalds 				      } },
228947dcf0cbSThomas Graf 			    .mark = oldflp->mark,
2290b40afd0eSDenis V. Lunev 			    .iif = net->loopback_dev->ifindex,
22911da177e4SLinus Torvalds 			    .oif = oldflp->oif };
22921da177e4SLinus Torvalds 	struct fib_result res;
22931da177e4SLinus Torvalds 	unsigned flags = 0;
22941da177e4SLinus Torvalds 	struct net_device *dev_out = NULL;
22951da177e4SLinus Torvalds 	int free_res = 0;
22961da177e4SLinus Torvalds 	int err;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	res.fi		= NULL;
23001da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
23011da177e4SLinus Torvalds 	res.r		= NULL;
23021da177e4SLinus Torvalds #endif
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	if (oldflp->fl4_src) {
23051da177e4SLinus Torvalds 		err = -EINVAL;
2306f97c1e0cSJoe Perches 		if (ipv4_is_multicast(oldflp->fl4_src) ||
23071e637c74SJan Engelhardt 		    ipv4_is_lbcast(oldflp->fl4_src) ||
2308f97c1e0cSJoe Perches 		    ipv4_is_zeronet(oldflp->fl4_src))
23091da177e4SLinus Torvalds 			goto out;
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 		/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2312b40afd0eSDenis V. Lunev 		dev_out = ip_dev_find(net, oldflp->fl4_src);
2313f6c5d736SDavid S. Miller 		if (dev_out == NULL)
23141da177e4SLinus Torvalds 			goto out;
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
23171da177e4SLinus Torvalds 		   It was wrong for two reasons:
23181ab35276SDenis V. Lunev 		   1. ip_dev_find(net, saddr) can return wrong iface, if saddr
23191ab35276SDenis V. Lunev 		      is assigned to multiple interfaces.
23201da177e4SLinus Torvalds 		   2. Moreover, we are allowed to send packets with saddr
23211da177e4SLinus Torvalds 		      of another iface. --ANK
23221da177e4SLinus Torvalds 		 */
23231da177e4SLinus Torvalds 
2324f6c5d736SDavid S. Miller 		if (oldflp->oif == 0
2325f97c1e0cSJoe Perches 		    && (ipv4_is_multicast(oldflp->fl4_dst) ||
2326f97c1e0cSJoe Perches 			oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
23271da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
23281da177e4SLinus Torvalds 			   and limited broadcast via necessary interface
23291da177e4SLinus Torvalds 			   without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
23301da177e4SLinus Torvalds 			   This hack is not just for fun, it allows
23311da177e4SLinus Torvalds 			   vic,vat and friends to work.
23321da177e4SLinus Torvalds 			   They bind socket to loopback, set ttl to zero
23331da177e4SLinus Torvalds 			   and expect that it will work.
23341da177e4SLinus Torvalds 			   From the viewpoint of routing cache they are broken,
23351da177e4SLinus Torvalds 			   because we are not allowed to build multicast path
23361da177e4SLinus Torvalds 			   with loopback source addr (look, routing cache
23371da177e4SLinus Torvalds 			   cannot know, that ttl is zero, so that packet
23381da177e4SLinus Torvalds 			   will not leave this host and route is valid).
23391da177e4SLinus Torvalds 			   Luckily, this hack is good workaround.
23401da177e4SLinus Torvalds 			 */
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 			fl.oif = dev_out->ifindex;
23431da177e4SLinus Torvalds 			goto make_route;
23441da177e4SLinus Torvalds 		}
23451da177e4SLinus Torvalds 		if (dev_out)
23461da177e4SLinus Torvalds 			dev_put(dev_out);
23471da177e4SLinus Torvalds 		dev_out = NULL;
23481da177e4SLinus Torvalds 	}
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	if (oldflp->oif) {
2352b40afd0eSDenis V. Lunev 		dev_out = dev_get_by_index(net, oldflp->oif);
23531da177e4SLinus Torvalds 		err = -ENODEV;
23541da177e4SLinus Torvalds 		if (dev_out == NULL)
23551da177e4SLinus Torvalds 			goto out;
2356e5ed6399SHerbert Xu 
2357e5ed6399SHerbert Xu 		/* RACE: Check return value of inet_select_addr instead. */
2358e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev_out) == NULL) {
23591da177e4SLinus Torvalds 			dev_put(dev_out);
23601da177e4SLinus Torvalds 			goto out;	/* Wrong error code */
23611da177e4SLinus Torvalds 		}
23621da177e4SLinus Torvalds 
2363f97c1e0cSJoe Perches 		if (ipv4_is_local_multicast(oldflp->fl4_dst) ||
2364f97c1e0cSJoe Perches 		    oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
23651da177e4SLinus Torvalds 			if (!fl.fl4_src)
23661da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
23671da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
23681da177e4SLinus Torvalds 			goto make_route;
23691da177e4SLinus Torvalds 		}
23701da177e4SLinus Torvalds 		if (!fl.fl4_src) {
2371f97c1e0cSJoe Perches 			if (ipv4_is_multicast(oldflp->fl4_dst))
23721da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
23731da177e4SLinus Torvalds 							      fl.fl4_scope);
23741da177e4SLinus Torvalds 			else if (!oldflp->fl4_dst)
23751da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
23761da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
23771da177e4SLinus Torvalds 		}
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	if (!fl.fl4_dst) {
23811da177e4SLinus Torvalds 		fl.fl4_dst = fl.fl4_src;
23821da177e4SLinus Torvalds 		if (!fl.fl4_dst)
23831da177e4SLinus Torvalds 			fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
23841da177e4SLinus Torvalds 		if (dev_out)
23851da177e4SLinus Torvalds 			dev_put(dev_out);
2386b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
23871da177e4SLinus Torvalds 		dev_hold(dev_out);
2388b40afd0eSDenis V. Lunev 		fl.oif = net->loopback_dev->ifindex;
23891da177e4SLinus Torvalds 		res.type = RTN_LOCAL;
23901da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
23911da177e4SLinus Torvalds 		goto make_route;
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 
2394b40afd0eSDenis V. Lunev 	if (fib_lookup(net, &fl, &res)) {
23951da177e4SLinus Torvalds 		res.fi = NULL;
23961da177e4SLinus Torvalds 		if (oldflp->oif) {
23971da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
23981da177e4SLinus Torvalds 			   that the destination is on link.
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 			   WHY? DW.
24011da177e4SLinus Torvalds 			   Because we are allowed to send to iface
24021da177e4SLinus Torvalds 			   even if it has NO routes and NO assigned
24031da177e4SLinus Torvalds 			   addresses. When oif is specified, routing
24041da177e4SLinus Torvalds 			   tables are looked up with only one purpose:
24051da177e4SLinus Torvalds 			   to catch if destination is gatewayed, rather than
24061da177e4SLinus Torvalds 			   direct. Moreover, if MSG_DONTROUTE is set,
24071da177e4SLinus Torvalds 			   we send packet, ignoring both routing tables
24081da177e4SLinus Torvalds 			   and ifaddr state. --ANK
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 			   We could make it even if oif is unknown,
24121da177e4SLinus Torvalds 			   likely IPv6, but we do not.
24131da177e4SLinus Torvalds 			 */
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 			if (fl.fl4_src == 0)
24161da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
24171da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
24181da177e4SLinus Torvalds 			res.type = RTN_UNICAST;
24191da177e4SLinus Torvalds 			goto make_route;
24201da177e4SLinus Torvalds 		}
24211da177e4SLinus Torvalds 		if (dev_out)
24221da177e4SLinus Torvalds 			dev_put(dev_out);
24231da177e4SLinus Torvalds 		err = -ENETUNREACH;
24241da177e4SLinus Torvalds 		goto out;
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds 	free_res = 1;
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
24291da177e4SLinus Torvalds 		if (!fl.fl4_src)
24301da177e4SLinus Torvalds 			fl.fl4_src = fl.fl4_dst;
24311da177e4SLinus Torvalds 		if (dev_out)
24321da177e4SLinus Torvalds 			dev_put(dev_out);
2433b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
24341da177e4SLinus Torvalds 		dev_hold(dev_out);
24351da177e4SLinus Torvalds 		fl.oif = dev_out->ifindex;
24361da177e4SLinus Torvalds 		if (res.fi)
24371da177e4SLinus Torvalds 			fib_info_put(res.fi);
24381da177e4SLinus Torvalds 		res.fi = NULL;
24391da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
24401da177e4SLinus Torvalds 		goto make_route;
24411da177e4SLinus Torvalds 	}
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
24441da177e4SLinus Torvalds 	if (res.fi->fib_nhs > 1 && fl.oif == 0)
24451da177e4SLinus Torvalds 		fib_select_multipath(&fl, &res);
24461da177e4SLinus Torvalds 	else
24471da177e4SLinus Torvalds #endif
24481da177e4SLinus Torvalds 	if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
2449b40afd0eSDenis V. Lunev 		fib_select_default(net, &fl, &res);
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	if (!fl.fl4_src)
24521da177e4SLinus Torvalds 		fl.fl4_src = FIB_RES_PREFSRC(res);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	if (dev_out)
24551da177e4SLinus Torvalds 		dev_put(dev_out);
24561da177e4SLinus Torvalds 	dev_out = FIB_RES_DEV(res);
24571da177e4SLinus Torvalds 	dev_hold(dev_out);
24581da177e4SLinus Torvalds 	fl.oif = dev_out->ifindex;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds make_route:
24621da177e4SLinus Torvalds 	err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 	if (free_res)
24661da177e4SLinus Torvalds 		fib_res_put(&res);
24671da177e4SLinus Torvalds 	if (dev_out)
24681da177e4SLinus Torvalds 		dev_put(dev_out);
24691da177e4SLinus Torvalds out:	return err;
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds 
2472611c183eSDenis V. Lunev int __ip_route_output_key(struct net *net, struct rtable **rp,
2473611c183eSDenis V. Lunev 			  const struct flowi *flp)
24741da177e4SLinus Torvalds {
24751da177e4SLinus Torvalds 	unsigned hash;
24761da177e4SLinus Torvalds 	struct rtable *rth;
24771da177e4SLinus Torvalds 
24788c7bc840SAl Viro 	hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif);
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	rcu_read_lock_bh();
24811da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2482093c2ca4SEric Dumazet 		rth = rcu_dereference(rth->u.dst.rt_next)) {
24831da177e4SLinus Torvalds 		if (rth->fl.fl4_dst == flp->fl4_dst &&
24841da177e4SLinus Torvalds 		    rth->fl.fl4_src == flp->fl4_src &&
24851da177e4SLinus Torvalds 		    rth->fl.iif == 0 &&
24861da177e4SLinus Torvalds 		    rth->fl.oif == flp->oif &&
248747dcf0cbSThomas Graf 		    rth->fl.mark == flp->mark &&
24881da177e4SLinus Torvalds 		    !((rth->fl.fl4_tos ^ flp->fl4_tos) &
2489b5921910SDenis V. Lunev 			    (IPTOS_RT_MASK | RTO_ONLINK)) &&
2490*878628fbSYOSHIFUJI Hideaki 		    net_eq(dev_net(rth->u.dst.dev), net) &&
249129e75252SEric Dumazet 		    rth->rt_genid == atomic_read(&rt_genid)) {
249203f49f34SPavel Emelyanov 			dst_use(&rth->u.dst, jiffies);
24931da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_hit);
24941da177e4SLinus Torvalds 			rcu_read_unlock_bh();
24951da177e4SLinus Torvalds 			*rp = rth;
24961da177e4SLinus Torvalds 			return 0;
24971da177e4SLinus Torvalds 		}
24981da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(out_hlist_search);
24991da177e4SLinus Torvalds 	}
25001da177e4SLinus Torvalds 	rcu_read_unlock_bh();
25011da177e4SLinus Torvalds 
2502611c183eSDenis V. Lunev 	return ip_route_output_slow(net, rp, flp);
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
2505d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key);
2506d8c97a94SArnaldo Carvalho de Melo 
250714e50e57SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
250814e50e57SDavid S. Miller {
250914e50e57SDavid S. Miller }
251014e50e57SDavid S. Miller 
251114e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
251214e50e57SDavid S. Miller 	.family			=	AF_INET,
251314e50e57SDavid S. Miller 	.protocol		=	__constant_htons(ETH_P_IP),
251414e50e57SDavid S. Miller 	.destroy		=	ipv4_dst_destroy,
251514e50e57SDavid S. Miller 	.check			=	ipv4_dst_check,
251614e50e57SDavid S. Miller 	.update_pmtu		=	ipv4_rt_blackhole_update_pmtu,
251714e50e57SDavid S. Miller 	.entry_size		=	sizeof(struct rtable),
2518e2422970SEric Dumazet 	.entries		=	ATOMIC_INIT(0),
251914e50e57SDavid S. Miller };
252014e50e57SDavid S. Miller 
252114e50e57SDavid S. Miller 
2522ce259990SDenis V. Lunev static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp)
252314e50e57SDavid S. Miller {
252414e50e57SDavid S. Miller 	struct rtable *ort = *rp;
252514e50e57SDavid S. Miller 	struct rtable *rt = (struct rtable *)
252614e50e57SDavid S. Miller 		dst_alloc(&ipv4_dst_blackhole_ops);
252714e50e57SDavid S. Miller 
252814e50e57SDavid S. Miller 	if (rt) {
252914e50e57SDavid S. Miller 		struct dst_entry *new = &rt->u.dst;
253014e50e57SDavid S. Miller 
253114e50e57SDavid S. Miller 		atomic_set(&new->__refcnt, 1);
253214e50e57SDavid S. Miller 		new->__use = 1;
2533352e512cSHerbert Xu 		new->input = dst_discard;
2534352e512cSHerbert Xu 		new->output = dst_discard;
253514e50e57SDavid S. Miller 		memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
253614e50e57SDavid S. Miller 
253714e50e57SDavid S. Miller 		new->dev = ort->u.dst.dev;
253814e50e57SDavid S. Miller 		if (new->dev)
253914e50e57SDavid S. Miller 			dev_hold(new->dev);
254014e50e57SDavid S. Miller 
254114e50e57SDavid S. Miller 		rt->fl = ort->fl;
254214e50e57SDavid S. Miller 
254314e50e57SDavid S. Miller 		rt->idev = ort->idev;
254414e50e57SDavid S. Miller 		if (rt->idev)
254514e50e57SDavid S. Miller 			in_dev_hold(rt->idev);
254629e75252SEric Dumazet 		rt->rt_genid = atomic_read(&rt_genid);
254714e50e57SDavid S. Miller 		rt->rt_flags = ort->rt_flags;
254814e50e57SDavid S. Miller 		rt->rt_type = ort->rt_type;
254914e50e57SDavid S. Miller 		rt->rt_dst = ort->rt_dst;
255014e50e57SDavid S. Miller 		rt->rt_src = ort->rt_src;
255114e50e57SDavid S. Miller 		rt->rt_iif = ort->rt_iif;
255214e50e57SDavid S. Miller 		rt->rt_gateway = ort->rt_gateway;
255314e50e57SDavid S. Miller 		rt->rt_spec_dst = ort->rt_spec_dst;
255414e50e57SDavid S. Miller 		rt->peer = ort->peer;
255514e50e57SDavid S. Miller 		if (rt->peer)
255614e50e57SDavid S. Miller 			atomic_inc(&rt->peer->refcnt);
255714e50e57SDavid S. Miller 
255814e50e57SDavid S. Miller 		dst_free(new);
255914e50e57SDavid S. Miller 	}
256014e50e57SDavid S. Miller 
256114e50e57SDavid S. Miller 	dst_release(&(*rp)->u.dst);
256214e50e57SDavid S. Miller 	*rp = rt;
256314e50e57SDavid S. Miller 	return (rt ? 0 : -ENOMEM);
256414e50e57SDavid S. Miller }
256514e50e57SDavid S. Miller 
2566f1b050bfSDenis V. Lunev int ip_route_output_flow(struct net *net, struct rtable **rp, struct flowi *flp,
2567f1b050bfSDenis V. Lunev 			 struct sock *sk, int flags)
25681da177e4SLinus Torvalds {
25691da177e4SLinus Torvalds 	int err;
25701da177e4SLinus Torvalds 
2571f1b050bfSDenis V. Lunev 	if ((err = __ip_route_output_key(net, rp, flp)) != 0)
25721da177e4SLinus Torvalds 		return err;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	if (flp->proto) {
25751da177e4SLinus Torvalds 		if (!flp->fl4_src)
25761da177e4SLinus Torvalds 			flp->fl4_src = (*rp)->rt_src;
25771da177e4SLinus Torvalds 		if (!flp->fl4_dst)
25781da177e4SLinus Torvalds 			flp->fl4_dst = (*rp)->rt_dst;
2579bb72845eSHerbert Xu 		err = __xfrm_lookup((struct dst_entry **)rp, flp, sk,
2580bb72845eSHerbert Xu 				    flags ? XFRM_LOOKUP_WAIT : 0);
258114e50e57SDavid S. Miller 		if (err == -EREMOTE)
2582ce259990SDenis V. Lunev 			err = ipv4_dst_blackhole(rp, flp);
258314e50e57SDavid S. Miller 
258414e50e57SDavid S. Miller 		return err;
25851da177e4SLinus Torvalds 	}
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	return 0;
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
2590d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2591d8c97a94SArnaldo Carvalho de Melo 
2592f206351aSDenis V. Lunev int ip_route_output_key(struct net *net, struct rtable **rp, struct flowi *flp)
25931da177e4SLinus Torvalds {
2594f206351aSDenis V. Lunev 	return ip_route_output_flow(net, rp, flp, NULL, 0);
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
2598b6544c0bSJamal Hadi Salim 			int nowait, unsigned int flags)
25991da177e4SLinus Torvalds {
2600ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
26011da177e4SLinus Torvalds 	struct rtmsg *r;
26021da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2603e3703b3dSThomas Graf 	long expires;
2604e3703b3dSThomas Graf 	u32 id = 0, ts = 0, tsage = 0, error;
2605be403ea1SThomas Graf 
2606be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2607be403ea1SThomas Graf 	if (nlh == NULL)
260826932566SPatrick McHardy 		return -EMSGSIZE;
2609be403ea1SThomas Graf 
2610be403ea1SThomas Graf 	r = nlmsg_data(nlh);
26111da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
26121da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
26131da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
26141da177e4SLinus Torvalds 	r->rtm_tos	= rt->fl.fl4_tos;
26151da177e4SLinus Torvalds 	r->rtm_table	= RT_TABLE_MAIN;
2616be403ea1SThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
26171da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
26181da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
26191da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
26201da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
26211da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
26221da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
2623be403ea1SThomas Graf 
262417fb2c64SAl Viro 	NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
2625be403ea1SThomas Graf 
26261da177e4SLinus Torvalds 	if (rt->fl.fl4_src) {
26271da177e4SLinus Torvalds 		r->rtm_src_len = 32;
262817fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
26291da177e4SLinus Torvalds 	}
26301da177e4SLinus Torvalds 	if (rt->u.dst.dev)
2631be403ea1SThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
26321da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
26331da177e4SLinus Torvalds 	if (rt->u.dst.tclassid)
2634be403ea1SThomas Graf 		NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
26351da177e4SLinus Torvalds #endif
26361da177e4SLinus Torvalds 	if (rt->fl.iif)
263717fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
26381da177e4SLinus Torvalds 	else if (rt->rt_src != rt->fl.fl4_src)
263917fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
2640be403ea1SThomas Graf 
26411da177e4SLinus Torvalds 	if (rt->rt_dst != rt->rt_gateway)
264217fb2c64SAl Viro 		NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
2643be403ea1SThomas Graf 
26441da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2645be403ea1SThomas Graf 		goto nla_put_failure;
2646be403ea1SThomas Graf 
2647e3703b3dSThomas Graf 	error = rt->u.dst.error;
2648e3703b3dSThomas Graf 	expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
26491da177e4SLinus Torvalds 	if (rt->peer) {
2650e3703b3dSThomas Graf 		id = rt->peer->ip_id_count;
26511da177e4SLinus Torvalds 		if (rt->peer->tcp_ts_stamp) {
2652e3703b3dSThomas Graf 			ts = rt->peer->tcp_ts;
26539d729f72SJames Morris 			tsage = get_seconds() - rt->peer->tcp_ts_stamp;
26541da177e4SLinus Torvalds 		}
26551da177e4SLinus Torvalds 	}
2656be403ea1SThomas Graf 
26571da177e4SLinus Torvalds 	if (rt->fl.iif) {
26581da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2659e448515cSAl Viro 		__be32 dst = rt->rt_dst;
26601da177e4SLinus Torvalds 
2661f97c1e0cSJoe Perches 		if (ipv4_is_multicast(dst) && !ipv4_is_local_multicast(dst) &&
2662586f1211SPavel Emelyanov 		    IPV4_DEVCONF_ALL(&init_net, MC_FORWARDING)) {
26631da177e4SLinus Torvalds 			int err = ipmr_get_route(skb, r, nowait);
26641da177e4SLinus Torvalds 			if (err <= 0) {
26651da177e4SLinus Torvalds 				if (!nowait) {
26661da177e4SLinus Torvalds 					if (err == 0)
26671da177e4SLinus Torvalds 						return 0;
2668be403ea1SThomas Graf 					goto nla_put_failure;
26691da177e4SLinus Torvalds 				} else {
26701da177e4SLinus Torvalds 					if (err == -EMSGSIZE)
2671be403ea1SThomas Graf 						goto nla_put_failure;
2672e3703b3dSThomas Graf 					error = err;
26731da177e4SLinus Torvalds 				}
26741da177e4SLinus Torvalds 			}
26751da177e4SLinus Torvalds 		} else
26761da177e4SLinus Torvalds #endif
2677be403ea1SThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
26781da177e4SLinus Torvalds 	}
26791da177e4SLinus Torvalds 
2680e3703b3dSThomas Graf 	if (rtnl_put_cacheinfo(skb, &rt->u.dst, id, ts, tsage,
2681e3703b3dSThomas Graf 			       expires, error) < 0)
2682e3703b3dSThomas Graf 		goto nla_put_failure;
26831da177e4SLinus Torvalds 
2684be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
2685be403ea1SThomas Graf 
2686be403ea1SThomas Graf nla_put_failure:
268726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
268826932566SPatrick McHardy 	return -EMSGSIZE;
26891da177e4SLinus Torvalds }
26901da177e4SLinus Torvalds 
269163f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
26921da177e4SLinus Torvalds {
26933b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2694d889ce3bSThomas Graf 	struct rtmsg *rtm;
2695d889ce3bSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26961da177e4SLinus Torvalds 	struct rtable *rt = NULL;
26979e12bb22SAl Viro 	__be32 dst = 0;
26989e12bb22SAl Viro 	__be32 src = 0;
26999e12bb22SAl Viro 	u32 iif;
2700d889ce3bSThomas Graf 	int err;
27011da177e4SLinus Torvalds 	struct sk_buff *skb;
27021da177e4SLinus Torvalds 
2703d889ce3bSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2704d889ce3bSThomas Graf 	if (err < 0)
2705d889ce3bSThomas Graf 		goto errout;
2706d889ce3bSThomas Graf 
2707d889ce3bSThomas Graf 	rtm = nlmsg_data(nlh);
2708d889ce3bSThomas Graf 
27091da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2710d889ce3bSThomas Graf 	if (skb == NULL) {
2711d889ce3bSThomas Graf 		err = -ENOBUFS;
2712d889ce3bSThomas Graf 		goto errout;
2713d889ce3bSThomas Graf 	}
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27161da177e4SLinus Torvalds 	   through good chunk of routing engine.
27171da177e4SLinus Torvalds 	 */
2718459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
2719c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2720d2c962b8SStephen Hemminger 
2721d2c962b8SStephen Hemminger 	/* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2722eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = IPPROTO_ICMP;
27231da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
27241da177e4SLinus Torvalds 
272517fb2c64SAl Viro 	src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
272617fb2c64SAl Viro 	dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2727d889ce3bSThomas Graf 	iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds 	if (iif) {
2730d889ce3bSThomas Graf 		struct net_device *dev;
2731d889ce3bSThomas Graf 
27321937504dSDenis V. Lunev 		dev = __dev_get_by_index(net, iif);
2733d889ce3bSThomas Graf 		if (dev == NULL) {
27341da177e4SLinus Torvalds 			err = -ENODEV;
2735d889ce3bSThomas Graf 			goto errout_free;
2736d889ce3bSThomas Graf 		}
2737d889ce3bSThomas Graf 
27381da177e4SLinus Torvalds 		skb->protocol	= htons(ETH_P_IP);
27391da177e4SLinus Torvalds 		skb->dev	= dev;
27401da177e4SLinus Torvalds 		local_bh_disable();
27411da177e4SLinus Torvalds 		err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
27421da177e4SLinus Torvalds 		local_bh_enable();
2743d889ce3bSThomas Graf 
2744ee6b9673SEric Dumazet 		rt = skb->rtable;
2745d889ce3bSThomas Graf 		if (err == 0 && rt->u.dst.error)
27461da177e4SLinus Torvalds 			err = -rt->u.dst.error;
27471da177e4SLinus Torvalds 	} else {
2748d889ce3bSThomas Graf 		struct flowi fl = {
2749d889ce3bSThomas Graf 			.nl_u = {
2750d889ce3bSThomas Graf 				.ip4_u = {
2751d889ce3bSThomas Graf 					.daddr = dst,
27521da177e4SLinus Torvalds 					.saddr = src,
2753d889ce3bSThomas Graf 					.tos = rtm->rtm_tos,
2754d889ce3bSThomas Graf 				},
2755d889ce3bSThomas Graf 			},
2756d889ce3bSThomas Graf 			.oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
2757d889ce3bSThomas Graf 		};
27581937504dSDenis V. Lunev 		err = ip_route_output_key(net, &rt, &fl);
27591da177e4SLinus Torvalds 	}
2760d889ce3bSThomas Graf 
27611da177e4SLinus Torvalds 	if (err)
2762d889ce3bSThomas Graf 		goto errout_free;
27631da177e4SLinus Torvalds 
2764ee6b9673SEric Dumazet 	skb->rtable = rt;
27651da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
27661da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2769b6544c0bSJamal Hadi Salim 			   RTM_NEWROUTE, 0, 0);
2770d889ce3bSThomas Graf 	if (err <= 0)
2771d889ce3bSThomas Graf 		goto errout_free;
27721da177e4SLinus Torvalds 
27731937504dSDenis V. Lunev 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2774d889ce3bSThomas Graf errout:
27752942e900SThomas Graf 	return err;
27761da177e4SLinus Torvalds 
2777d889ce3bSThomas Graf errout_free:
27781da177e4SLinus Torvalds 	kfree_skb(skb);
2779d889ce3bSThomas Graf 	goto errout;
27801da177e4SLinus Torvalds }
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
27831da177e4SLinus Torvalds {
27841da177e4SLinus Torvalds 	struct rtable *rt;
27851da177e4SLinus Torvalds 	int h, s_h;
27861da177e4SLinus Torvalds 	int idx, s_idx;
27871937504dSDenis V. Lunev 	struct net *net;
27881937504dSDenis V. Lunev 
27893b1e0a65SYOSHIFUJI Hideaki 	net = sock_net(skb->sk);
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	s_h = cb->args[0];
2792d8c92830SEric Dumazet 	if (s_h < 0)
2793d8c92830SEric Dumazet 		s_h = 0;
27941da177e4SLinus Torvalds 	s_idx = idx = cb->args[1];
2795d8c92830SEric Dumazet 	for (h = s_h; h <= rt_hash_mask; h++) {
27961da177e4SLinus Torvalds 		rcu_read_lock_bh();
27971da177e4SLinus Torvalds 		for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
2798093c2ca4SEric Dumazet 		     rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
2799*878628fbSYOSHIFUJI Hideaki 			if (!net_eq(dev_net(rt->u.dst.dev), net) || idx < s_idx)
28001da177e4SLinus Torvalds 				continue;
280129e75252SEric Dumazet 			if (rt->rt_genid != atomic_read(&rt_genid))
280229e75252SEric Dumazet 				continue;
28031da177e4SLinus Torvalds 			skb->dst = dst_clone(&rt->u.dst);
28041da177e4SLinus Torvalds 			if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
2805b6544c0bSJamal Hadi Salim 					 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2806b6544c0bSJamal Hadi Salim 					 1, NLM_F_MULTI) <= 0) {
28071da177e4SLinus Torvalds 				dst_release(xchg(&skb->dst, NULL));
28081da177e4SLinus Torvalds 				rcu_read_unlock_bh();
28091da177e4SLinus Torvalds 				goto done;
28101da177e4SLinus Torvalds 			}
28111da177e4SLinus Torvalds 			dst_release(xchg(&skb->dst, NULL));
28121da177e4SLinus Torvalds 		}
28131da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2814d8c92830SEric Dumazet 		s_idx = 0;
28151da177e4SLinus Torvalds 	}
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds done:
28181da177e4SLinus Torvalds 	cb->args[0] = h;
28191da177e4SLinus Torvalds 	cb->args[1] = idx;
28201da177e4SLinus Torvalds 	return skb->len;
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
28241da177e4SLinus Torvalds {
28251da177e4SLinus Torvalds 	rt_cache_flush(0);
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28291da177e4SLinus Torvalds static int flush_delay;
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
28321da177e4SLinus Torvalds 					struct file *filp, void __user *buffer,
28331da177e4SLinus Torvalds 					size_t *lenp, loff_t *ppos)
28341da177e4SLinus Torvalds {
28351da177e4SLinus Torvalds 	if (write) {
28361da177e4SLinus Torvalds 		proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
28371da177e4SLinus Torvalds 		rt_cache_flush(flush_delay);
28381da177e4SLinus Torvalds 		return 0;
28391da177e4SLinus Torvalds 	}
28401da177e4SLinus Torvalds 
28411da177e4SLinus Torvalds 	return -EINVAL;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
28451da177e4SLinus Torvalds 						int __user *name,
28461da177e4SLinus Torvalds 						int nlen,
28471da177e4SLinus Torvalds 						void __user *oldval,
28481da177e4SLinus Torvalds 						size_t __user *oldlenp,
28491da177e4SLinus Torvalds 						void __user *newval,
28501f29bcd7SAlexey Dobriyan 						size_t newlen)
28511da177e4SLinus Torvalds {
28521da177e4SLinus Torvalds 	int delay;
28531da177e4SLinus Torvalds 	if (newlen != sizeof(int))
28541da177e4SLinus Torvalds 		return -EINVAL;
28551da177e4SLinus Torvalds 	if (get_user(delay, (int __user *)newval))
28561da177e4SLinus Torvalds 		return -EFAULT;
28571da177e4SLinus Torvalds 	rt_cache_flush(delay);
28581da177e4SLinus Torvalds 	return 0;
28591da177e4SLinus Torvalds }
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds ctl_table ipv4_route_table[] = {
28621da177e4SLinus Torvalds 	{
28631da177e4SLinus Torvalds 		.ctl_name 	= NET_IPV4_ROUTE_FLUSH,
28641da177e4SLinus Torvalds 		.procname	= "flush",
28651da177e4SLinus Torvalds 		.data		= &flush_delay,
28661da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28677e3e0360SDave Jones 		.mode		= 0200,
28681da177e4SLinus Torvalds 		.proc_handler	= &ipv4_sysctl_rtcache_flush,
28691da177e4SLinus Torvalds 		.strategy	= &ipv4_sysctl_rtcache_flush_strategy,
28701da177e4SLinus Torvalds 	},
28711da177e4SLinus Torvalds 	{
28721da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_THRESH,
28731da177e4SLinus Torvalds 		.procname	= "gc_thresh",
28741da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
28751da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28761da177e4SLinus Torvalds 		.mode		= 0644,
28771da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
28781da177e4SLinus Torvalds 	},
28791da177e4SLinus Torvalds 	{
28801da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MAX_SIZE,
28811da177e4SLinus Torvalds 		.procname	= "max_size",
28821da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
28831da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28841da177e4SLinus Torvalds 		.mode		= 0644,
28851da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
28861da177e4SLinus Torvalds 	},
28871da177e4SLinus Torvalds 	{
28881da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL,
28911da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
28921da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
28931da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28941da177e4SLinus Torvalds 		.mode		= 0644,
28951da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
28961da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
28971da177e4SLinus Torvalds 	},
28981da177e4SLinus Torvalds 	{
28991da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
29001da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
29011da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
29021da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29031da177e4SLinus Torvalds 		.mode		= 0644,
29041da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_ms_jiffies,
29051da177e4SLinus Torvalds 		.strategy	= &sysctl_ms_jiffies,
29061da177e4SLinus Torvalds 	},
29071da177e4SLinus Torvalds 	{
29081da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_TIMEOUT,
29091da177e4SLinus Torvalds 		.procname	= "gc_timeout",
29101da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
29111da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29121da177e4SLinus Torvalds 		.mode		= 0644,
29131da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29141da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29151da177e4SLinus Torvalds 	},
29161da177e4SLinus Torvalds 	{
29171da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_INTERVAL,
29181da177e4SLinus Torvalds 		.procname	= "gc_interval",
29191da177e4SLinus Torvalds 		.data		= &ip_rt_gc_interval,
29201da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29211da177e4SLinus Torvalds 		.mode		= 0644,
29221da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29231da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29241da177e4SLinus Torvalds 	},
29251da177e4SLinus Torvalds 	{
29261da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_LOAD,
29271da177e4SLinus Torvalds 		.procname	= "redirect_load",
29281da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
29291da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29301da177e4SLinus Torvalds 		.mode		= 0644,
29311da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29321da177e4SLinus Torvalds 	},
29331da177e4SLinus Torvalds 	{
29341da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_NUMBER,
29351da177e4SLinus Torvalds 		.procname	= "redirect_number",
29361da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
29371da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29381da177e4SLinus Torvalds 		.mode		= 0644,
29391da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29401da177e4SLinus Torvalds 	},
29411da177e4SLinus Torvalds 	{
29421da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_SILENCE,
29431da177e4SLinus Torvalds 		.procname	= "redirect_silence",
29441da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
29451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29461da177e4SLinus Torvalds 		.mode		= 0644,
29471da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29481da177e4SLinus Torvalds 	},
29491da177e4SLinus Torvalds 	{
29501da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_COST,
29511da177e4SLinus Torvalds 		.procname	= "error_cost",
29521da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
29531da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29541da177e4SLinus Torvalds 		.mode		= 0644,
29551da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29561da177e4SLinus Torvalds 	},
29571da177e4SLinus Torvalds 	{
29581da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_BURST,
29591da177e4SLinus Torvalds 		.procname	= "error_burst",
29601da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
29611da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29621da177e4SLinus Torvalds 		.mode		= 0644,
29631da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29641da177e4SLinus Torvalds 	},
29651da177e4SLinus Torvalds 	{
29661da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_ELASTICITY,
29671da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
29681da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
29691da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29701da177e4SLinus Torvalds 		.mode		= 0644,
29711da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29721da177e4SLinus Torvalds 	},
29731da177e4SLinus Torvalds 	{
29741da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MTU_EXPIRES,
29751da177e4SLinus Torvalds 		.procname	= "mtu_expires",
29761da177e4SLinus Torvalds 		.data		= &ip_rt_mtu_expires,
29771da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29781da177e4SLinus Torvalds 		.mode		= 0644,
29791da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29801da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29811da177e4SLinus Torvalds 	},
29821da177e4SLinus Torvalds 	{
29831da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_PMTU,
29841da177e4SLinus Torvalds 		.procname	= "min_pmtu",
29851da177e4SLinus Torvalds 		.data		= &ip_rt_min_pmtu,
29861da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29871da177e4SLinus Torvalds 		.mode		= 0644,
29881da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29891da177e4SLinus Torvalds 	},
29901da177e4SLinus Torvalds 	{
29911da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_ADVMSS,
29921da177e4SLinus Torvalds 		.procname	= "min_adv_mss",
29931da177e4SLinus Torvalds 		.data		= &ip_rt_min_advmss,
29941da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29951da177e4SLinus Torvalds 		.mode		= 0644,
29961da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29971da177e4SLinus Torvalds 	},
29981da177e4SLinus Torvalds 	{
29991da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_SECRET_INTERVAL,
30001da177e4SLinus Torvalds 		.procname	= "secret_interval",
30011da177e4SLinus Torvalds 		.data		= &ip_rt_secret_interval,
30021da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
30031da177e4SLinus Torvalds 		.mode		= 0644,
30041da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
30051da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
30061da177e4SLinus Torvalds 	},
30071da177e4SLinus Torvalds 	{ .ctl_name = 0 }
30081da177e4SLinus Torvalds };
30091da177e4SLinus Torvalds #endif
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
30128dbde28dSEric Dumazet struct ip_rt_acct *ip_rt_acct __read_mostly;
30131da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_ROUTE */
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds static __initdata unsigned long rhash_entries;
30161da177e4SLinus Torvalds static int __init set_rhash_entries(char *str)
30171da177e4SLinus Torvalds {
30181da177e4SLinus Torvalds 	if (!str)
30191da177e4SLinus Torvalds 		return 0;
30201da177e4SLinus Torvalds 	rhash_entries = simple_strtoul(str, &str, 0);
30211da177e4SLinus Torvalds 	return 1;
30221da177e4SLinus Torvalds }
30231da177e4SLinus Torvalds __setup("rhash_entries=", set_rhash_entries);
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds int __init ip_rt_init(void)
30261da177e4SLinus Torvalds {
3027424c4b70SEric Dumazet 	int rc = 0;
30281da177e4SLinus Torvalds 
302929e75252SEric Dumazet 	atomic_set(&rt_genid, (int) ((num_physpages ^ (num_physpages>>8)) ^
303029e75252SEric Dumazet 			     (jiffies ^ (jiffies >> 7))));
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
30338dbde28dSEric Dumazet 	ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct));
30341da177e4SLinus Torvalds 	if (!ip_rt_acct)
30351da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
30361da177e4SLinus Torvalds #endif
30371da177e4SLinus Torvalds 
3038e5d679f3SAlexey Dobriyan 	ipv4_dst_ops.kmem_cachep =
3039e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
304020c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
30411da177e4SLinus Torvalds 
304214e50e57SDavid S. Miller 	ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
304314e50e57SDavid S. Miller 
30441da177e4SLinus Torvalds 	rt_hash_table = (struct rt_hash_bucket *)
3045424c4b70SEric Dumazet 		alloc_large_system_hash("IP route cache",
3046424c4b70SEric Dumazet 					sizeof(struct rt_hash_bucket),
3047424c4b70SEric Dumazet 					rhash_entries,
3048424c4b70SEric Dumazet 					(num_physpages >= 128 * 1024) ?
304918955cfcSMike Stroyan 					15 : 17,
30508d1502deSKirill Korotaev 					0,
3051424c4b70SEric Dumazet 					&rt_hash_log,
3052424c4b70SEric Dumazet 					&rt_hash_mask,
3053424c4b70SEric Dumazet 					0);
305422c047ccSEric Dumazet 	memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
305522c047ccSEric Dumazet 	rt_hash_lock_init();
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 	ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
30581da177e4SLinus Torvalds 	ip_rt_max_size = (rt_hash_mask + 1) * 16;
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	devinet_init();
30611da177e4SLinus Torvalds 	ip_fib_init();
30621da177e4SLinus Torvalds 
3063b24b8a24SPavel Emelyanov 	setup_timer(&rt_secret_timer, rt_secret_rebuild, 0);
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	/* All the timers, started at system startup tend
30661da177e4SLinus Torvalds 	   to synchronize. Perturb it a bit.
30671da177e4SLinus Torvalds 	 */
306839c90eceSEric Dumazet 	schedule_delayed_work(&expires_work,
306939c90eceSEric Dumazet 		net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
30721da177e4SLinus Torvalds 		ip_rt_secret_interval;
30731da177e4SLinus Torvalds 	add_timer(&rt_secret_timer);
30741da177e4SLinus Torvalds 
307573b38711SDenis V. Lunev 	if (ip_rt_proc_init())
3076107f1634SPavel Emelyanov 		printk(KERN_ERR "Unable to create route proc files\n");
30771da177e4SLinus Torvalds #ifdef CONFIG_XFRM
30781da177e4SLinus Torvalds 	xfrm_init();
30791da177e4SLinus Torvalds 	xfrm4_init();
30801da177e4SLinus Torvalds #endif
308163f3444fSThomas Graf 	rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
308263f3444fSThomas Graf 
30831da177e4SLinus Torvalds 	return rc;
30841da177e4SLinus Torvalds }
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds EXPORT_SYMBOL(__ip_select_ident);
30871da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_input);
30881da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_output_key);
3089