xref: /linux/net/ipv4/route.c (revision 7e3e0360b711568fb8ba5973accf51d51e746abc)
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  *
101da177e4SLinus Torvalds  * Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
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
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
591da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
601da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
611da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #include <linux/config.h>
651da177e4SLinus Torvalds #include <linux/module.h>
661da177e4SLinus Torvalds #include <asm/uaccess.h>
671da177e4SLinus Torvalds #include <asm/system.h>
681da177e4SLinus Torvalds #include <linux/bitops.h>
691da177e4SLinus Torvalds #include <linux/types.h>
701da177e4SLinus Torvalds #include <linux/kernel.h>
711da177e4SLinus Torvalds #include <linux/sched.h>
721da177e4SLinus Torvalds #include <linux/mm.h>
731da177e4SLinus Torvalds #include <linux/string.h>
741da177e4SLinus Torvalds #include <linux/socket.h>
751da177e4SLinus Torvalds #include <linux/sockios.h>
761da177e4SLinus Torvalds #include <linux/errno.h>
771da177e4SLinus Torvalds #include <linux/in.h>
781da177e4SLinus Torvalds #include <linux/inet.h>
791da177e4SLinus Torvalds #include <linux/netdevice.h>
801da177e4SLinus Torvalds #include <linux/proc_fs.h>
811da177e4SLinus Torvalds #include <linux/init.h>
821da177e4SLinus Torvalds #include <linux/skbuff.h>
831da177e4SLinus Torvalds #include <linux/rtnetlink.h>
841da177e4SLinus Torvalds #include <linux/inetdevice.h>
851da177e4SLinus Torvalds #include <linux/igmp.h>
861da177e4SLinus Torvalds #include <linux/pkt_sched.h>
871da177e4SLinus Torvalds #include <linux/mroute.h>
881da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
891da177e4SLinus Torvalds #include <linux/random.h>
901da177e4SLinus Torvalds #include <linux/jhash.h>
911da177e4SLinus Torvalds #include <linux/rcupdate.h>
921da177e4SLinus Torvalds #include <linux/times.h>
931da177e4SLinus Torvalds #include <net/protocol.h>
941da177e4SLinus Torvalds #include <net/ip.h>
951da177e4SLinus Torvalds #include <net/route.h>
961da177e4SLinus Torvalds #include <net/inetpeer.h>
971da177e4SLinus Torvalds #include <net/sock.h>
981da177e4SLinus Torvalds #include <net/ip_fib.h>
991da177e4SLinus Torvalds #include <net/arp.h>
1001da177e4SLinus Torvalds #include <net/tcp.h>
1011da177e4SLinus Torvalds #include <net/icmp.h>
1021da177e4SLinus Torvalds #include <net/xfrm.h>
1031da177e4SLinus Torvalds #include <net/ip_mp_alg.h>
1041da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1051da177e4SLinus Torvalds #include <linux/sysctl.h>
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define RT_FL_TOS(oldflp) \
1091da177e4SLinus Torvalds     ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define IP_MAX_MTU	0xFFF0
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static int ip_rt_min_delay		= 2 * HZ;
1161da177e4SLinus Torvalds static int ip_rt_max_delay		= 10 * HZ;
1171da177e4SLinus Torvalds static int ip_rt_max_size;
1181da177e4SLinus Torvalds static int ip_rt_gc_timeout		= RT_GC_TIMEOUT;
1191da177e4SLinus Torvalds static int ip_rt_gc_interval		= 60 * HZ;
1201da177e4SLinus Torvalds static int ip_rt_gc_min_interval	= HZ / 2;
1211da177e4SLinus Torvalds static int ip_rt_redirect_number	= 9;
1221da177e4SLinus Torvalds static int ip_rt_redirect_load		= HZ / 50;
1231da177e4SLinus Torvalds static int ip_rt_redirect_silence	= ((HZ / 50) << (9 + 1));
1241da177e4SLinus Torvalds static int ip_rt_error_cost		= HZ;
1251da177e4SLinus Torvalds static int ip_rt_error_burst		= 5 * HZ;
1261da177e4SLinus Torvalds static int ip_rt_gc_elasticity		= 8;
1271da177e4SLinus Torvalds static int ip_rt_mtu_expires		= 10 * 60 * HZ;
1281da177e4SLinus Torvalds static int ip_rt_min_pmtu		= 512 + 20 + 20;
1291da177e4SLinus Torvalds static int ip_rt_min_advmss		= 256;
1301da177e4SLinus Torvalds static int ip_rt_secret_interval	= 10 * 60 * HZ;
1311da177e4SLinus Torvalds static unsigned long rt_deadline;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define RTprint(a...)	printk(KERN_DEBUG a)
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static struct timer_list rt_flush_timer;
1361da177e4SLinus Torvalds static struct timer_list rt_periodic_timer;
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);
1501da177e4SLinus Torvalds static int rt_garbage_collect(void);
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,
1631da177e4SLinus Torvalds 	.entry_size =		sizeof(struct rtable),
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds __u8 ip_tos2prio[16] = {
1691da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1701da177e4SLinus Torvalds 	ECN_OR_COST(FILLER),
1711da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1721da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1731da177e4SLinus Torvalds 	TC_PRIO_BULK,
1741da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1751da177e4SLinus Torvalds 	TC_PRIO_BULK,
1761da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1771da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1781da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1791da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1801da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1811da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1821da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1831da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1841da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
1891da177e4SLinus Torvalds  * Route cache.
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /* The locking scheme is rather straight forward:
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * 1) Read-Copy Update protects the buckets of the central route hash.
1951da177e4SLinus Torvalds  * 2) Only writers remove entries, and they hold the lock
1961da177e4SLinus Torvalds  *    as they look at rtable reference counts.
1971da177e4SLinus Torvalds  * 3) Only readers acquire references to rtable entries,
1981da177e4SLinus Torvalds  *    they do so with atomic increments and with the
1991da177e4SLinus Torvalds  *    lock held.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds struct rt_hash_bucket {
2031da177e4SLinus Torvalds 	struct rtable	*chain;
2041da177e4SLinus Torvalds 	spinlock_t	lock;
2051da177e4SLinus Torvalds } __attribute__((__aligned__(8)));
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static struct rt_hash_bucket 	*rt_hash_table;
2081da177e4SLinus Torvalds static unsigned			rt_hash_mask;
2091da177e4SLinus Torvalds static int			rt_hash_log;
2101da177e4SLinus Torvalds static unsigned int		rt_hash_rnd;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds struct rt_cache_stat *rt_cache_stat;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int rt_intern_hash(unsigned hash, struct rtable *rth,
2151da177e4SLinus Torvalds 				struct rtable **res);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static unsigned int rt_hash_code(u32 daddr, u32 saddr, u8 tos)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	return (jhash_3words(daddr, saddr, (u32) tos, rt_hash_rnd)
2201da177e4SLinus Torvalds 		& rt_hash_mask);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
2241da177e4SLinus Torvalds struct rt_cache_iter_state {
2251da177e4SLinus Torvalds 	int bucket;
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static struct rtable *rt_cache_get_first(struct seq_file *seq)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	struct rtable *r = NULL;
2311da177e4SLinus Torvalds 	struct rt_cache_iter_state *st = seq->private;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
2341da177e4SLinus Torvalds 		rcu_read_lock_bh();
2351da177e4SLinus Torvalds 		r = rt_hash_table[st->bucket].chain;
2361da177e4SLinus Torvalds 		if (r)
2371da177e4SLinus Torvalds 			break;
2381da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	return r;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct rt_cache_iter_state *st = rcu_dereference(seq->private);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	r = r->u.rt_next;
2481da177e4SLinus Torvalds 	while (!r) {
2491da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2501da177e4SLinus Torvalds 		if (--st->bucket < 0)
2511da177e4SLinus Torvalds 			break;
2521da177e4SLinus Torvalds 		rcu_read_lock_bh();
2531da177e4SLinus Torvalds 		r = rt_hash_table[st->bucket].chain;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	return r;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	struct rtable *r = rt_cache_get_first(seq);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (r)
2631da177e4SLinus Torvalds 		while (pos && (r = rt_cache_get_next(seq, r)))
2641da177e4SLinus Torvalds 			--pos;
2651da177e4SLinus Torvalds 	return pos ? NULL : r;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	struct rtable *r = NULL;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2781da177e4SLinus Torvalds 		r = rt_cache_get_first(seq);
2791da177e4SLinus Torvalds 	else
2801da177e4SLinus Torvalds 		r = rt_cache_get_next(seq, v);
2811da177e4SLinus Torvalds 	++*pos;
2821da177e4SLinus Torvalds 	return r;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	if (v && v != SEQ_START_TOKEN)
2881da177e4SLinus Torvalds 		rcu_read_unlock_bh();
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2941da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
2951da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
2961da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
2971da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
2981da177e4SLinus Torvalds 	else {
2991da177e4SLinus Torvalds 		struct rtable *r = v;
3001da177e4SLinus Torvalds 		char temp[256];
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
3031da177e4SLinus Torvalds 			      "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
3041da177e4SLinus Torvalds 			r->u.dst.dev ? r->u.dst.dev->name : "*",
3051da177e4SLinus Torvalds 			(unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
3061da177e4SLinus Torvalds 			r->rt_flags, atomic_read(&r->u.dst.__refcnt),
3071da177e4SLinus Torvalds 			r->u.dst.__use, 0, (unsigned long)r->rt_src,
3081da177e4SLinus Torvalds 			(dst_metric(&r->u.dst, RTAX_ADVMSS) ?
3091da177e4SLinus Torvalds 			     (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
3101da177e4SLinus Torvalds 			dst_metric(&r->u.dst, RTAX_WINDOW),
3111da177e4SLinus Torvalds 			(int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
3121da177e4SLinus Torvalds 			      dst_metric(&r->u.dst, RTAX_RTTVAR)),
3131da177e4SLinus Torvalds 			r->fl.fl4_tos,
3141da177e4SLinus Torvalds 			r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
3151da177e4SLinus Torvalds 			r->u.dst.hh ? (r->u.dst.hh->hh_output ==
3161da177e4SLinus Torvalds 				       dev_queue_xmit) : 0,
3171da177e4SLinus Torvalds 			r->rt_spec_dst);
3181da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n", temp);
3191da177e4SLinus Torvalds         }
3201da177e4SLinus Torvalds   	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static struct seq_operations rt_cache_seq_ops = {
3241da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
3251da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
3261da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
3271da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
3281da177e4SLinus Torvalds };
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct seq_file *seq;
3331da177e4SLinus Torvalds 	int rc = -ENOMEM;
3341da177e4SLinus Torvalds 	struct rt_cache_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (!s)
3371da177e4SLinus Torvalds 		goto out;
3381da177e4SLinus Torvalds 	rc = seq_open(file, &rt_cache_seq_ops);
3391da177e4SLinus Torvalds 	if (rc)
3401da177e4SLinus Torvalds 		goto out_kfree;
3411da177e4SLinus Torvalds 	seq          = file->private_data;
3421da177e4SLinus Torvalds 	seq->private = s;
3431da177e4SLinus Torvalds 	memset(s, 0, sizeof(*s));
3441da177e4SLinus Torvalds out:
3451da177e4SLinus Torvalds 	return rc;
3461da177e4SLinus Torvalds out_kfree:
3471da177e4SLinus Torvalds 	kfree(s);
3481da177e4SLinus Torvalds 	goto out;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static struct file_operations rt_cache_seq_fops = {
3521da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
3531da177e4SLinus Torvalds 	.open	 = rt_cache_seq_open,
3541da177e4SLinus Torvalds 	.read	 = seq_read,
3551da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
3561da177e4SLinus Torvalds 	.release = seq_release_private,
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	int cpu;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (*pos == 0)
3651da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
3681da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
3691da177e4SLinus Torvalds 			continue;
3701da177e4SLinus Torvalds 		*pos = cpu+1;
3711da177e4SLinus Torvalds 		return per_cpu_ptr(rt_cache_stat, cpu);
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 	return NULL;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	int cpu;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
3811da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
3821da177e4SLinus Torvalds 			continue;
3831da177e4SLinus Torvalds 		*pos = cpu+1;
3841da177e4SLinus Torvalds 		return per_cpu_ptr(rt_cache_stat, cpu);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	return NULL;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
4001da177e4SLinus Torvalds 		seq_printf(seq, "entries  in_hit in_slow_tot 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");
4011da177e4SLinus Torvalds 		return 0;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	seq_printf(seq,"%08x  %08x %08x %08x %08x %08x %08x %08x "
4051da177e4SLinus Torvalds 		   " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
4061da177e4SLinus Torvalds 		   atomic_read(&ipv4_dst_ops.entries),
4071da177e4SLinus Torvalds 		   st->in_hit,
4081da177e4SLinus Torvalds 		   st->in_slow_tot,
4091da177e4SLinus Torvalds 		   st->in_slow_mc,
4101da177e4SLinus Torvalds 		   st->in_no_route,
4111da177e4SLinus Torvalds 		   st->in_brd,
4121da177e4SLinus Torvalds 		   st->in_martian_dst,
4131da177e4SLinus Torvalds 		   st->in_martian_src,
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		   st->out_hit,
4161da177e4SLinus Torvalds 		   st->out_slow_tot,
4171da177e4SLinus Torvalds 		   st->out_slow_mc,
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		   st->gc_total,
4201da177e4SLinus Torvalds 		   st->gc_ignored,
4211da177e4SLinus Torvalds 		   st->gc_goal_miss,
4221da177e4SLinus Torvalds 		   st->gc_dst_overflow,
4231da177e4SLinus Torvalds 		   st->in_hlist_search,
4241da177e4SLinus Torvalds 		   st->out_hlist_search
4251da177e4SLinus Torvalds 		);
4261da177e4SLinus Torvalds 	return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static struct seq_operations rt_cpu_seq_ops = {
4301da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
4311da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
4321da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
4331da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
4341da177e4SLinus Torvalds };
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	return seq_open(file, &rt_cpu_seq_ops);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds static struct file_operations rt_cpu_seq_fops = {
4431da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
4441da177e4SLinus Torvalds 	.open	 = rt_cpu_seq_open,
4451da177e4SLinus Torvalds 	.read	 = seq_read,
4461da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
4471da177e4SLinus Torvalds 	.release = seq_release,
4481da177e4SLinus Torvalds };
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static __inline__ void rt_free(struct rtable *rt)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	multipath_remove(rt);
4551da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds static __inline__ void rt_drop(struct rtable *rt)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	multipath_remove(rt);
4611da177e4SLinus Torvalds 	ip_rt_put(rt);
4621da177e4SLinus Torvalds 	call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static __inline__ int rt_fast_clean(struct rtable *rth)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	/* Kill broadcast/multicast entries very aggresively, if they
4681da177e4SLinus Torvalds 	   collide in hash table with more useful entries */
4691da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
4701da177e4SLinus Torvalds 		rth->fl.iif && rth->u.rt_next;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds static __inline__ int rt_valuable(struct rtable *rth)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
4761da177e4SLinus Torvalds 		rth->u.dst.expires;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	unsigned long age;
4821da177e4SLinus Torvalds 	int ret = 0;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (atomic_read(&rth->u.dst.__refcnt))
4851da177e4SLinus Torvalds 		goto out;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	ret = 1;
4881da177e4SLinus Torvalds 	if (rth->u.dst.expires &&
4891da177e4SLinus Torvalds 	    time_after_eq(jiffies, rth->u.dst.expires))
4901da177e4SLinus Torvalds 		goto out;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	age = jiffies - rth->u.dst.lastuse;
4931da177e4SLinus Torvalds 	ret = 0;
4941da177e4SLinus Torvalds 	if ((age <= tmo1 && !rt_fast_clean(rth)) ||
4951da177e4SLinus Torvalds 	    (age <= tmo2 && rt_valuable(rth)))
4961da177e4SLinus Torvalds 		goto out;
4971da177e4SLinus Torvalds 	ret = 1;
4981da177e4SLinus Torvalds out:	return ret;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /* Bits of score are:
5021da177e4SLinus Torvalds  * 31: very valuable
5031da177e4SLinus Torvalds  * 30: not quite useless
5041da177e4SLinus Torvalds  * 29..0: usage counter
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds static inline u32 rt_score(struct rtable *rt)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	u32 score = jiffies - rt->u.dst.lastuse;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	score = ~score & ~(3<<30);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	if (rt_valuable(rt))
5131da177e4SLinus Torvalds 		score |= (1<<31);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (!rt->fl.iif ||
5161da177e4SLinus Torvalds 	    !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
5171da177e4SLinus Torvalds 		score |= (1<<30);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	return score;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	return memcmp(&fl1->nl_u.ip4_u, &fl2->nl_u.ip4_u, sizeof(fl1->nl_u.ip4_u)) == 0 &&
5251da177e4SLinus Torvalds 	       fl1->oif     == fl2->oif &&
5261da177e4SLinus Torvalds 	       fl1->iif     == fl2->iif;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
5301da177e4SLinus Torvalds static struct rtable **rt_remove_balanced_route(struct rtable **chain_head,
5311da177e4SLinus Torvalds 						struct rtable *expentry,
5321da177e4SLinus Torvalds 						int *removed_count)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	int passedexpired = 0;
5351da177e4SLinus Torvalds 	struct rtable **nextstep = NULL;
5361da177e4SLinus Torvalds 	struct rtable **rthp = chain_head;
5371da177e4SLinus Torvalds 	struct rtable *rth;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (removed_count)
5401da177e4SLinus Torvalds 		*removed_count = 0;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	while ((rth = *rthp) != NULL) {
5431da177e4SLinus Torvalds 		if (rth == expentry)
5441da177e4SLinus Torvalds 			passedexpired = 1;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 		if (((*rthp)->u.dst.flags & DST_BALANCED) != 0  &&
5471da177e4SLinus Torvalds 		    compare_keys(&(*rthp)->fl, &expentry->fl)) {
5481da177e4SLinus Torvalds 			if (*rthp == expentry) {
5491da177e4SLinus Torvalds 				*rthp = rth->u.rt_next;
5501da177e4SLinus Torvalds 				continue;
5511da177e4SLinus Torvalds 			} else {
5521da177e4SLinus Torvalds 				*rthp = rth->u.rt_next;
5531da177e4SLinus Torvalds 				rt_free(rth);
5541da177e4SLinus Torvalds 				if (removed_count)
5551da177e4SLinus Torvalds 					++(*removed_count);
5561da177e4SLinus Torvalds 			}
5571da177e4SLinus Torvalds 		} else {
5581da177e4SLinus Torvalds 			if (!((*rthp)->u.dst.flags & DST_BALANCED) &&
5591da177e4SLinus Torvalds 			    passedexpired && !nextstep)
5601da177e4SLinus Torvalds 				nextstep = &rth->u.rt_next;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 			rthp = &rth->u.rt_next;
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	rt_free(expentry);
5671da177e4SLinus Torvalds 	if (removed_count)
5681da177e4SLinus Torvalds 		++(*removed_count);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	return nextstep;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds /* This runs via a timer and thus is always in BH context. */
5761da177e4SLinus Torvalds static void rt_check_expire(unsigned long dummy)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	static int rover;
5791da177e4SLinus Torvalds 	int i = rover, t;
5801da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
5811da177e4SLinus Torvalds 	unsigned long now = jiffies;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	for (t = ip_rt_gc_interval << rt_hash_log; t >= 0;
5841da177e4SLinus Torvalds 	     t -= ip_rt_gc_timeout) {
5851da177e4SLinus Torvalds 		unsigned long tmo = ip_rt_gc_timeout;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 		i = (i + 1) & rt_hash_mask;
5881da177e4SLinus Torvalds 		rthp = &rt_hash_table[i].chain;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 		spin_lock(&rt_hash_table[i].lock);
5911da177e4SLinus Torvalds 		while ((rth = *rthp) != NULL) {
5921da177e4SLinus Torvalds 			if (rth->u.dst.expires) {
5931da177e4SLinus Torvalds 				/* Entry is expired even if it is in use */
5941da177e4SLinus Torvalds 				if (time_before_eq(now, rth->u.dst.expires)) {
5951da177e4SLinus Torvalds 					tmo >>= 1;
5961da177e4SLinus Torvalds 					rthp = &rth->u.rt_next;
5971da177e4SLinus Torvalds 					continue;
5981da177e4SLinus Torvalds 				}
5991da177e4SLinus Torvalds 			} else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
6001da177e4SLinus Torvalds 				tmo >>= 1;
6011da177e4SLinus Torvalds 				rthp = &rth->u.rt_next;
6021da177e4SLinus Torvalds 				continue;
6031da177e4SLinus Torvalds 			}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 			/* Cleanup aged off entries. */
6061da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
6071da177e4SLinus Torvalds 			/* remove all related balanced entries if necessary */
6081da177e4SLinus Torvalds 			if (rth->u.dst.flags & DST_BALANCED) {
6091da177e4SLinus Torvalds 				rthp = rt_remove_balanced_route(
6101da177e4SLinus Torvalds 					&rt_hash_table[i].chain,
6111da177e4SLinus Torvalds 					rth, NULL);
6121da177e4SLinus Torvalds 				if (!rthp)
6131da177e4SLinus Torvalds 					break;
6141da177e4SLinus Torvalds 			} else {
6151da177e4SLinus Torvalds 				*rthp = rth->u.rt_next;
6161da177e4SLinus Torvalds 				rt_free(rth);
6171da177e4SLinus Torvalds 			}
6181da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
6191da177e4SLinus Torvalds  			*rthp = rth->u.rt_next;
6201da177e4SLinus Torvalds  			rt_free(rth);
6211da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
6221da177e4SLinus Torvalds 		}
6231da177e4SLinus Torvalds 		spin_unlock(&rt_hash_table[i].lock);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		/* Fallback loop breaker. */
6261da177e4SLinus Torvalds 		if (time_after(jiffies, now))
6271da177e4SLinus Torvalds 			break;
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 	rover = i;
6301da177e4SLinus Torvalds 	mod_timer(&rt_periodic_timer, now + ip_rt_gc_interval);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /* This can run from both BH and non-BH contexts, the latter
6341da177e4SLinus Torvalds  * in the case of a forced flush event.
6351da177e4SLinus Torvalds  */
6361da177e4SLinus Torvalds static void rt_run_flush(unsigned long dummy)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	int i;
6391da177e4SLinus Torvalds 	struct rtable *rth, *next;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	rt_deadline = 0;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	get_random_bytes(&rt_hash_rnd, 4);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	for (i = rt_hash_mask; i >= 0; i--) {
6461da177e4SLinus Torvalds 		spin_lock_bh(&rt_hash_table[i].lock);
6471da177e4SLinus Torvalds 		rth = rt_hash_table[i].chain;
6481da177e4SLinus Torvalds 		if (rth)
6491da177e4SLinus Torvalds 			rt_hash_table[i].chain = NULL;
6501da177e4SLinus Torvalds 		spin_unlock_bh(&rt_hash_table[i].lock);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 		for (; rth; rth = next) {
6531da177e4SLinus Torvalds 			next = rth->u.rt_next;
6541da177e4SLinus Torvalds 			rt_free(rth);
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds static DEFINE_SPINLOCK(rt_flush_lock);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds void rt_cache_flush(int delay)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	unsigned long now = jiffies;
6641da177e4SLinus Torvalds 	int user_mode = !in_softirq();
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (delay < 0)
6671da177e4SLinus Torvalds 		delay = ip_rt_min_delay;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* flush existing multipath state*/
6701da177e4SLinus Torvalds 	multipath_flush();
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	spin_lock_bh(&rt_flush_lock);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
6751da177e4SLinus Torvalds 		long tmo = (long)(rt_deadline - now);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		/* If flush timer is already running
6781da177e4SLinus Torvalds 		   and flush request is not immediate (delay > 0):
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 		   if deadline is not achieved, prolongate timer to "delay",
6811da177e4SLinus Torvalds 		   otherwise fire it at deadline time.
6821da177e4SLinus Torvalds 		 */
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
6851da177e4SLinus Torvalds 			tmo = 0;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		if (delay > tmo)
6881da177e4SLinus Torvalds 			delay = tmo;
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if (delay <= 0) {
6921da177e4SLinus Torvalds 		spin_unlock_bh(&rt_flush_lock);
6931da177e4SLinus Torvalds 		rt_run_flush(0);
6941da177e4SLinus Torvalds 		return;
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	if (rt_deadline == 0)
6981da177e4SLinus Torvalds 		rt_deadline = now + ip_rt_max_delay;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	mod_timer(&rt_flush_timer, now+delay);
7011da177e4SLinus Torvalds 	spin_unlock_bh(&rt_flush_lock);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static void rt_secret_rebuild(unsigned long dummy)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	unsigned long now = jiffies;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	rt_cache_flush(0);
7091da177e4SLinus Torvalds 	mod_timer(&rt_secret_timer, now + ip_rt_secret_interval);
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds    Short description of GC goals.
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds    We want to build algorithm, which will keep routing cache
7161da177e4SLinus Torvalds    at some equilibrium point, when number of aged off entries
7171da177e4SLinus Torvalds    is kept approximately equal to newly generated ones.
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds    Current expiration strength is variable "expire".
7201da177e4SLinus Torvalds    We try to adjust it dynamically, so that if networking
7211da177e4SLinus Torvalds    is idle expires is large enough to keep enough of warm entries,
7221da177e4SLinus Torvalds    and when load increases it reduces to limit cache size.
7231da177e4SLinus Torvalds  */
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds static int rt_garbage_collect(void)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	static unsigned long expire = RT_GC_TIMEOUT;
7281da177e4SLinus Torvalds 	static unsigned long last_gc;
7291da177e4SLinus Torvalds 	static int rover;
7301da177e4SLinus Torvalds 	static int equilibrium;
7311da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
7321da177e4SLinus Torvalds 	unsigned long now = jiffies;
7331da177e4SLinus Torvalds 	int goal;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/*
7361da177e4SLinus Torvalds 	 * Garbage collection is pretty expensive,
7371da177e4SLinus Torvalds 	 * do not make it too frequently.
7381da177e4SLinus Torvalds 	 */
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_total);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	if (now - last_gc < ip_rt_gc_min_interval &&
7431da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
7441da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_ignored);
7451da177e4SLinus Torvalds 		goto out;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/* Calculate number of entries, which we want to expire now. */
7491da177e4SLinus Torvalds 	goal = atomic_read(&ipv4_dst_ops.entries) -
7501da177e4SLinus Torvalds 		(ip_rt_gc_elasticity << rt_hash_log);
7511da177e4SLinus Torvalds 	if (goal <= 0) {
7521da177e4SLinus Torvalds 		if (equilibrium < ipv4_dst_ops.gc_thresh)
7531da177e4SLinus Torvalds 			equilibrium = ipv4_dst_ops.gc_thresh;
7541da177e4SLinus Torvalds 		goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
7551da177e4SLinus Torvalds 		if (goal > 0) {
7561da177e4SLinus Torvalds 			equilibrium += min_t(unsigned int, goal / 2, rt_hash_mask + 1);
7571da177e4SLinus Torvalds 			goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 	} else {
7601da177e4SLinus Torvalds 		/* We are in dangerous area. Try to reduce cache really
7611da177e4SLinus Torvalds 		 * aggressively.
7621da177e4SLinus Torvalds 		 */
7631da177e4SLinus Torvalds 		goal = max_t(unsigned int, goal / 2, rt_hash_mask + 1);
7641da177e4SLinus Torvalds 		equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (now - last_gc >= ip_rt_gc_min_interval)
7681da177e4SLinus Torvalds 		last_gc = now;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (goal <= 0) {
7711da177e4SLinus Torvalds 		equilibrium += goal;
7721da177e4SLinus Torvalds 		goto work_done;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	do {
7761da177e4SLinus Torvalds 		int i, k;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		for (i = rt_hash_mask, k = rover; i >= 0; i--) {
7791da177e4SLinus Torvalds 			unsigned long tmo = expire;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 			k = (k + 1) & rt_hash_mask;
7821da177e4SLinus Torvalds 			rthp = &rt_hash_table[k].chain;
7831da177e4SLinus Torvalds 			spin_lock_bh(&rt_hash_table[k].lock);
7841da177e4SLinus Torvalds 			while ((rth = *rthp) != NULL) {
7851da177e4SLinus Torvalds 				if (!rt_may_expire(rth, tmo, expire)) {
7861da177e4SLinus Torvalds 					tmo >>= 1;
7871da177e4SLinus Torvalds 					rthp = &rth->u.rt_next;
7881da177e4SLinus Torvalds 					continue;
7891da177e4SLinus Torvalds 				}
7901da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
7911da177e4SLinus Torvalds 				/* remove all related balanced entries
7921da177e4SLinus Torvalds 				 * if necessary
7931da177e4SLinus Torvalds 				 */
7941da177e4SLinus Torvalds 				if (rth->u.dst.flags & DST_BALANCED) {
7951da177e4SLinus Torvalds 					int r;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 					rthp = rt_remove_balanced_route(
7981da177e4SLinus Torvalds 						&rt_hash_table[i].chain,
7991da177e4SLinus Torvalds 						rth,
8001da177e4SLinus Torvalds 						&r);
8011da177e4SLinus Torvalds 					goal -= r;
8021da177e4SLinus Torvalds 					if (!rthp)
8031da177e4SLinus Torvalds 						break;
8041da177e4SLinus Torvalds 				} else {
8051da177e4SLinus Torvalds 					*rthp = rth->u.rt_next;
8061da177e4SLinus Torvalds 					rt_free(rth);
8071da177e4SLinus Torvalds 					goal--;
8081da177e4SLinus Torvalds 				}
8091da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
8101da177e4SLinus Torvalds 				*rthp = rth->u.rt_next;
8111da177e4SLinus Torvalds 				rt_free(rth);
8121da177e4SLinus Torvalds 				goal--;
8131da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
8141da177e4SLinus Torvalds 			}
8151da177e4SLinus Torvalds 			spin_unlock_bh(&rt_hash_table[k].lock);
8161da177e4SLinus Torvalds 			if (goal <= 0)
8171da177e4SLinus Torvalds 				break;
8181da177e4SLinus Torvalds 		}
8191da177e4SLinus Torvalds 		rover = k;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 		if (goal <= 0)
8221da177e4SLinus Torvalds 			goto work_done;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		/* Goal is not achieved. We stop process if:
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		   - if expire reduced to zero. Otherwise, expire is halfed.
8271da177e4SLinus Torvalds 		   - if table is not full.
8281da177e4SLinus Torvalds 		   - if we are called from interrupt.
8291da177e4SLinus Torvalds 		   - jiffies check is just fallback/debug loop breaker.
8301da177e4SLinus Torvalds 		     We will not spin here for long time in any case.
8311da177e4SLinus Torvalds 		 */
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(gc_goal_miss);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 		if (expire == 0)
8361da177e4SLinus Torvalds 			break;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 		expire >>= 1;
8391da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
8401da177e4SLinus Torvalds 		printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
8411da177e4SLinus Torvalds 				atomic_read(&ipv4_dst_ops.entries), goal, i);
8421da177e4SLinus Torvalds #endif
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
8451da177e4SLinus Torvalds 			goto out;
8461da177e4SLinus Torvalds 	} while (!in_softirq() && time_before_eq(jiffies, now));
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
8491da177e4SLinus Torvalds 		goto out;
8501da177e4SLinus Torvalds 	if (net_ratelimit())
8511da177e4SLinus Torvalds 		printk(KERN_WARNING "dst cache overflow\n");
8521da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(gc_dst_overflow);
8531da177e4SLinus Torvalds 	return 1;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds work_done:
8561da177e4SLinus Torvalds 	expire += ip_rt_gc_min_interval;
8571da177e4SLinus Torvalds 	if (expire > ip_rt_gc_timeout ||
8581da177e4SLinus Torvalds 	    atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
8591da177e4SLinus Torvalds 		expire = ip_rt_gc_timeout;
8601da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
8611da177e4SLinus Torvalds 	printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
8621da177e4SLinus Torvalds 			atomic_read(&ipv4_dst_ops.entries), goal, rover);
8631da177e4SLinus Torvalds #endif
8641da177e4SLinus Torvalds out:	return 0;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	struct rtable	*rth, **rthp;
8701da177e4SLinus Torvalds 	unsigned long	now;
8711da177e4SLinus Torvalds 	struct rtable *cand, **candp;
8721da177e4SLinus Torvalds 	u32 		min_score;
8731da177e4SLinus Torvalds 	int		chain_length;
8741da177e4SLinus Torvalds 	int attempts = !in_softirq();
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds restart:
8771da177e4SLinus Torvalds 	chain_length = 0;
8781da177e4SLinus Torvalds 	min_score = ~(u32)0;
8791da177e4SLinus Torvalds 	cand = NULL;
8801da177e4SLinus Torvalds 	candp = NULL;
8811da177e4SLinus Torvalds 	now = jiffies;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	rthp = &rt_hash_table[hash].chain;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	spin_lock_bh(&rt_hash_table[hash].lock);
8861da177e4SLinus Torvalds 	while ((rth = *rthp) != NULL) {
8871da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
8881da177e4SLinus Torvalds 		if (!(rth->u.dst.flags & DST_BALANCED) &&
8891da177e4SLinus Torvalds 		    compare_keys(&rth->fl, &rt->fl)) {
8901da177e4SLinus Torvalds #else
8911da177e4SLinus Torvalds 		if (compare_keys(&rth->fl, &rt->fl)) {
8921da177e4SLinus Torvalds #endif
8931da177e4SLinus Torvalds 			/* Put it first */
8941da177e4SLinus Torvalds 			*rthp = rth->u.rt_next;
8951da177e4SLinus Torvalds 			/*
8961da177e4SLinus Torvalds 			 * Since lookup is lockfree, the deletion
8971da177e4SLinus Torvalds 			 * must be visible to another weakly ordered CPU before
8981da177e4SLinus Torvalds 			 * the insertion at the start of the hash chain.
8991da177e4SLinus Torvalds 			 */
9001da177e4SLinus Torvalds 			rcu_assign_pointer(rth->u.rt_next,
9011da177e4SLinus Torvalds 					   rt_hash_table[hash].chain);
9021da177e4SLinus Torvalds 			/*
9031da177e4SLinus Torvalds 			 * Since lookup is lockfree, the update writes
9041da177e4SLinus Torvalds 			 * must be ordered for consistency on SMP.
9051da177e4SLinus Torvalds 			 */
9061da177e4SLinus Torvalds 			rcu_assign_pointer(rt_hash_table[hash].chain, rth);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 			rth->u.dst.__use++;
9091da177e4SLinus Torvalds 			dst_hold(&rth->u.dst);
9101da177e4SLinus Torvalds 			rth->u.dst.lastuse = now;
9111da177e4SLinus Torvalds 			spin_unlock_bh(&rt_hash_table[hash].lock);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 			rt_drop(rt);
9141da177e4SLinus Torvalds 			*rp = rth;
9151da177e4SLinus Torvalds 			return 0;
9161da177e4SLinus Torvalds 		}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		if (!atomic_read(&rth->u.dst.__refcnt)) {
9191da177e4SLinus Torvalds 			u32 score = rt_score(rth);
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 			if (score <= min_score) {
9221da177e4SLinus Torvalds 				cand = rth;
9231da177e4SLinus Torvalds 				candp = rthp;
9241da177e4SLinus Torvalds 				min_score = score;
9251da177e4SLinus Torvalds 			}
9261da177e4SLinus Torvalds 		}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 		chain_length++;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 		rthp = &rth->u.rt_next;
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (cand) {
9341da177e4SLinus Torvalds 		/* ip_rt_gc_elasticity used to be average length of chain
9351da177e4SLinus Torvalds 		 * length, when exceeded gc becomes really aggressive.
9361da177e4SLinus Torvalds 		 *
9371da177e4SLinus Torvalds 		 * The second limit is less certain. At the moment it allows
9381da177e4SLinus Torvalds 		 * only 2 entries per bucket. We will see.
9391da177e4SLinus Torvalds 		 */
9401da177e4SLinus Torvalds 		if (chain_length > ip_rt_gc_elasticity) {
9411da177e4SLinus Torvalds 			*candp = cand->u.rt_next;
9421da177e4SLinus Torvalds 			rt_free(cand);
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/* Try to bind route to arp only if it is output
9471da177e4SLinus Torvalds 	   route or unicast forwarding path.
9481da177e4SLinus Torvalds 	 */
9491da177e4SLinus Torvalds 	if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
9501da177e4SLinus Torvalds 		int err = arp_bind_neighbour(&rt->u.dst);
9511da177e4SLinus Torvalds 		if (err) {
9521da177e4SLinus Torvalds 			spin_unlock_bh(&rt_hash_table[hash].lock);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 			if (err != -ENOBUFS) {
9551da177e4SLinus Torvalds 				rt_drop(rt);
9561da177e4SLinus Torvalds 				return err;
9571da177e4SLinus Torvalds 			}
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 			/* Neighbour tables are full and nothing
9601da177e4SLinus Torvalds 			   can be released. Try to shrink route cache,
9611da177e4SLinus Torvalds 			   it is most likely it holds some neighbour records.
9621da177e4SLinus Torvalds 			 */
9631da177e4SLinus Torvalds 			if (attempts-- > 0) {
9641da177e4SLinus Torvalds 				int saved_elasticity = ip_rt_gc_elasticity;
9651da177e4SLinus Torvalds 				int saved_int = ip_rt_gc_min_interval;
9661da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= 1;
9671da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= 0;
9681da177e4SLinus Torvalds 				rt_garbage_collect();
9691da177e4SLinus Torvalds 				ip_rt_gc_min_interval	= saved_int;
9701da177e4SLinus Torvalds 				ip_rt_gc_elasticity	= saved_elasticity;
9711da177e4SLinus Torvalds 				goto restart;
9721da177e4SLinus Torvalds 			}
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 			if (net_ratelimit())
9751da177e4SLinus Torvalds 				printk(KERN_WARNING "Neighbour table overflow.\n");
9761da177e4SLinus Torvalds 			rt_drop(rt);
9771da177e4SLinus Torvalds 			return -ENOBUFS;
9781da177e4SLinus Torvalds 		}
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	rt->u.rt_next = rt_hash_table[hash].chain;
9821da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2
9831da177e4SLinus Torvalds 	if (rt->u.rt_next) {
9841da177e4SLinus Torvalds 		struct rtable *trt;
9851da177e4SLinus Torvalds 		printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
9861da177e4SLinus Torvalds 		       NIPQUAD(rt->rt_dst));
9871da177e4SLinus Torvalds 		for (trt = rt->u.rt_next; trt; trt = trt->u.rt_next)
9881da177e4SLinus Torvalds 			printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
9891da177e4SLinus Torvalds 		printk("\n");
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds #endif
9921da177e4SLinus Torvalds 	rt_hash_table[hash].chain = rt;
9931da177e4SLinus Torvalds 	spin_unlock_bh(&rt_hash_table[hash].lock);
9941da177e4SLinus Torvalds 	*rp = rt;
9951da177e4SLinus Torvalds 	return 0;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds void rt_bind_peer(struct rtable *rt, int create)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(rt_peer_lock);
10011da177e4SLinus Torvalds 	struct inet_peer *peer;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	peer = inet_getpeer(rt->rt_dst, create);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	spin_lock_bh(&rt_peer_lock);
10061da177e4SLinus Torvalds 	if (rt->peer == NULL) {
10071da177e4SLinus Torvalds 		rt->peer = peer;
10081da177e4SLinus Torvalds 		peer = NULL;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 	spin_unlock_bh(&rt_peer_lock);
10111da177e4SLinus Torvalds 	if (peer)
10121da177e4SLinus Torvalds 		inet_putpeer(peer);
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /*
10161da177e4SLinus Torvalds  * Peer allocation may fail only in serious out-of-memory conditions.  However
10171da177e4SLinus Torvalds  * we still can generate some output.
10181da177e4SLinus Torvalds  * Random ID selection looks a bit dangerous because we have no chances to
10191da177e4SLinus Torvalds  * select ID being unique in a reasonable period of time.
10201da177e4SLinus Torvalds  * But broken packet identifier may be better than no packet at all.
10211da177e4SLinus Torvalds  */
10221da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(ip_fb_id_lock);
10251da177e4SLinus Torvalds 	static u32 ip_fallback_id;
10261da177e4SLinus Torvalds 	u32 salt;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	spin_lock_bh(&ip_fb_id_lock);
10291da177e4SLinus Torvalds 	salt = secure_ip_id(ip_fallback_id ^ iph->daddr);
10301da177e4SLinus Torvalds 	iph->id = htons(salt & 0xFFFF);
10311da177e4SLinus Torvalds 	ip_fallback_id = salt;
10321da177e4SLinus Torvalds 	spin_unlock_bh(&ip_fb_id_lock);
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	if (rt) {
10401da177e4SLinus Torvalds 		if (rt->peer == NULL)
10411da177e4SLinus Torvalds 			rt_bind_peer(rt, 1);
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 		/* If peer is attached to destination, it is never detached,
10441da177e4SLinus Torvalds 		   so that we need not to grab a lock to dereference it.
10451da177e4SLinus Torvalds 		 */
10461da177e4SLinus Torvalds 		if (rt->peer) {
10471da177e4SLinus Torvalds 			iph->id = htons(inet_getid(rt->peer, more));
10481da177e4SLinus Torvalds 			return;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 	} else
10519c2b3328SStephen Hemminger 		printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
10529c2b3328SStephen Hemminger 		       __builtin_return_address(0));
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	ip_select_fb_ident(iph);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds static void rt_del(unsigned hash, struct rtable *rt)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	struct rtable **rthp;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	spin_lock_bh(&rt_hash_table[hash].lock);
10621da177e4SLinus Torvalds 	ip_rt_put(rt);
10631da177e4SLinus Torvalds 	for (rthp = &rt_hash_table[hash].chain; *rthp;
10641da177e4SLinus Torvalds 	     rthp = &(*rthp)->u.rt_next)
10651da177e4SLinus Torvalds 		if (*rthp == rt) {
10661da177e4SLinus Torvalds 			*rthp = rt->u.rt_next;
10671da177e4SLinus Torvalds 			rt_free(rt);
10681da177e4SLinus Torvalds 			break;
10691da177e4SLinus Torvalds 		}
10701da177e4SLinus Torvalds 	spin_unlock_bh(&rt_hash_table[hash].lock);
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds void ip_rt_redirect(u32 old_gw, u32 daddr, u32 new_gw,
10741da177e4SLinus Torvalds 		    u32 saddr, u8 tos, struct net_device *dev)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	int i, k;
10771da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
10781da177e4SLinus Torvalds 	struct rtable *rth, **rthp;
10791da177e4SLinus Torvalds 	u32  skeys[2] = { saddr, 0 };
10801da177e4SLinus Torvalds 	int  ikeys[2] = { dev->ifindex, 0 };
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	tos &= IPTOS_RT_MASK;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	if (!in_dev)
10851da177e4SLinus Torvalds 		return;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
10881da177e4SLinus Torvalds 	    || MULTICAST(new_gw) || BADCLASS(new_gw) || ZERONET(new_gw))
10891da177e4SLinus Torvalds 		goto reject_redirect;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
10921da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
10931da177e4SLinus Torvalds 			goto reject_redirect;
10941da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
10951da177e4SLinus Torvalds 			goto reject_redirect;
10961da177e4SLinus Torvalds 	} else {
10971da177e4SLinus Torvalds 		if (inet_addr_type(new_gw) != RTN_UNICAST)
10981da177e4SLinus Torvalds 			goto reject_redirect;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
11021da177e4SLinus Torvalds 		for (k = 0; k < 2; k++) {
11031da177e4SLinus Torvalds 			unsigned hash = rt_hash_code(daddr,
11041da177e4SLinus Torvalds 						     skeys[i] ^ (ikeys[k] << 5),
11051da177e4SLinus Torvalds 						     tos);
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 			rthp=&rt_hash_table[hash].chain;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 			rcu_read_lock();
11101da177e4SLinus Torvalds 			while ((rth = rcu_dereference(*rthp)) != NULL) {
11111da177e4SLinus Torvalds 				struct rtable *rt;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 				if (rth->fl.fl4_dst != daddr ||
11141da177e4SLinus Torvalds 				    rth->fl.fl4_src != skeys[i] ||
11151da177e4SLinus Torvalds 				    rth->fl.fl4_tos != tos ||
11161da177e4SLinus Torvalds 				    rth->fl.oif != ikeys[k] ||
11171da177e4SLinus Torvalds 				    rth->fl.iif != 0) {
11181da177e4SLinus Torvalds 					rthp = &rth->u.rt_next;
11191da177e4SLinus Torvalds 					continue;
11201da177e4SLinus Torvalds 				}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 				if (rth->rt_dst != daddr ||
11231da177e4SLinus Torvalds 				    rth->rt_src != saddr ||
11241da177e4SLinus Torvalds 				    rth->u.dst.error ||
11251da177e4SLinus Torvalds 				    rth->rt_gateway != old_gw ||
11261da177e4SLinus Torvalds 				    rth->u.dst.dev != dev)
11271da177e4SLinus Torvalds 					break;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 				dst_hold(&rth->u.dst);
11301da177e4SLinus Torvalds 				rcu_read_unlock();
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 				rt = dst_alloc(&ipv4_dst_ops);
11331da177e4SLinus Torvalds 				if (rt == NULL) {
11341da177e4SLinus Torvalds 					ip_rt_put(rth);
11351da177e4SLinus Torvalds 					in_dev_put(in_dev);
11361da177e4SLinus Torvalds 					return;
11371da177e4SLinus Torvalds 				}
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 				/* Copy all the information. */
11401da177e4SLinus Torvalds 				*rt = *rth;
11411da177e4SLinus Torvalds  				INIT_RCU_HEAD(&rt->u.dst.rcu_head);
11421da177e4SLinus Torvalds 				rt->u.dst.__use		= 1;
11431da177e4SLinus Torvalds 				atomic_set(&rt->u.dst.__refcnt, 1);
11441da177e4SLinus Torvalds 				rt->u.dst.child		= NULL;
11451da177e4SLinus Torvalds 				if (rt->u.dst.dev)
11461da177e4SLinus Torvalds 					dev_hold(rt->u.dst.dev);
11471da177e4SLinus Torvalds 				if (rt->idev)
11481da177e4SLinus Torvalds 					in_dev_hold(rt->idev);
11491da177e4SLinus Torvalds 				rt->u.dst.obsolete	= 0;
11501da177e4SLinus Torvalds 				rt->u.dst.lastuse	= jiffies;
11511da177e4SLinus Torvalds 				rt->u.dst.path		= &rt->u.dst;
11521da177e4SLinus Torvalds 				rt->u.dst.neighbour	= NULL;
11531da177e4SLinus Torvalds 				rt->u.dst.hh		= NULL;
11541da177e4SLinus Torvalds 				rt->u.dst.xfrm		= NULL;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 				rt->rt_flags		|= RTCF_REDIRECTED;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 				/* Gateway is different ... */
11591da177e4SLinus Torvalds 				rt->rt_gateway		= new_gw;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 				/* Redirect received -> path was valid */
11621da177e4SLinus Torvalds 				dst_confirm(&rth->u.dst);
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 				if (rt->peer)
11651da177e4SLinus Torvalds 					atomic_inc(&rt->peer->refcnt);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 				if (arp_bind_neighbour(&rt->u.dst) ||
11681da177e4SLinus Torvalds 				    !(rt->u.dst.neighbour->nud_state &
11691da177e4SLinus Torvalds 					    NUD_VALID)) {
11701da177e4SLinus Torvalds 					if (rt->u.dst.neighbour)
11711da177e4SLinus Torvalds 						neigh_event_send(rt->u.dst.neighbour, NULL);
11721da177e4SLinus Torvalds 					ip_rt_put(rth);
11731da177e4SLinus Torvalds 					rt_drop(rt);
11741da177e4SLinus Torvalds 					goto do_next;
11751da177e4SLinus Torvalds 				}
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 				rt_del(hash, rth);
11781da177e4SLinus Torvalds 				if (!rt_intern_hash(hash, rt, &rt))
11791da177e4SLinus Torvalds 					ip_rt_put(rt);
11801da177e4SLinus Torvalds 				goto do_next;
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 			rcu_read_unlock();
11831da177e4SLinus Torvalds 		do_next:
11841da177e4SLinus Torvalds 			;
11851da177e4SLinus Torvalds 		}
11861da177e4SLinus Torvalds 	}
11871da177e4SLinus Torvalds 	in_dev_put(in_dev);
11881da177e4SLinus Torvalds 	return;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds reject_redirect:
11911da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
11921da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
11931da177e4SLinus Torvalds 		printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
11941da177e4SLinus Torvalds 			"%u.%u.%u.%u ignored.\n"
11951da177e4SLinus Torvalds 			"  Advised path = %u.%u.%u.%u -> %u.%u.%u.%u, "
11961da177e4SLinus Torvalds 			"tos %02x\n",
11971da177e4SLinus Torvalds 		       NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
11981da177e4SLinus Torvalds 		       NIPQUAD(saddr), NIPQUAD(daddr), tos);
11991da177e4SLinus Torvalds #endif
12001da177e4SLinus Torvalds 	in_dev_put(in_dev);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
12041da177e4SLinus Torvalds {
12051da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)dst;
12061da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	if (rt) {
12091da177e4SLinus Torvalds 		if (dst->obsolete) {
12101da177e4SLinus Torvalds 			ip_rt_put(rt);
12111da177e4SLinus Torvalds 			ret = NULL;
12121da177e4SLinus Torvalds 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
12131da177e4SLinus Torvalds 			   rt->u.dst.expires) {
12141da177e4SLinus Torvalds 			unsigned hash = rt_hash_code(rt->fl.fl4_dst,
12151da177e4SLinus Torvalds 						     rt->fl.fl4_src ^
12161da177e4SLinus Torvalds 							(rt->fl.oif << 5),
12171da177e4SLinus Torvalds 						     rt->fl.fl4_tos);
12181da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 1
12191da177e4SLinus Torvalds 			printk(KERN_DEBUG "ip_rt_advice: redirect to "
12201da177e4SLinus Torvalds 					  "%u.%u.%u.%u/%02x dropped\n",
12211da177e4SLinus Torvalds 				NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
12221da177e4SLinus Torvalds #endif
12231da177e4SLinus Torvalds 			rt_del(hash, rt);
12241da177e4SLinus Torvalds 			ret = NULL;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 	return ret;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /*
12311da177e4SLinus Torvalds  * Algorithm:
12321da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
12331da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
12341da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
12351da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
12361da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
12371da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
12401da177e4SLinus Torvalds  * in icmp.c.
12411da177e4SLinus Torvalds  *
12421da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
12431da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
12441da177e4SLinus Torvalds  */
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
12471da177e4SLinus Torvalds {
12481da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
12491da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	if (!in_dev)
12521da177e4SLinus Torvalds 		return;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	if (!IN_DEV_TX_REDIRECTS(in_dev))
12551da177e4SLinus Torvalds 		goto out;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
12581da177e4SLinus Torvalds 	 * reset the algorithm.
12591da177e4SLinus Torvalds 	 */
12601da177e4SLinus Torvalds 	if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
12611da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = 0;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
12641da177e4SLinus Torvalds 	 * set u.dst.rate_last to the last seen redirected packet.
12651da177e4SLinus Torvalds 	 */
12661da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
12671da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
12681da177e4SLinus Torvalds 		goto out;
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
12721da177e4SLinus Torvalds 	 * redirect.
12731da177e4SLinus Torvalds 	 */
12741da177e4SLinus Torvalds 	if (time_after(jiffies,
12751da177e4SLinus Torvalds 		       (rt->u.dst.rate_last +
12761da177e4SLinus Torvalds 			(ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
12771da177e4SLinus Torvalds 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
12781da177e4SLinus Torvalds 		rt->u.dst.rate_last = jiffies;
12791da177e4SLinus Torvalds 		++rt->u.dst.rate_tokens;
12801da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
12811da177e4SLinus Torvalds 		if (IN_DEV_LOG_MARTIANS(in_dev) &&
12821da177e4SLinus Torvalds 		    rt->u.dst.rate_tokens == ip_rt_redirect_number &&
12831da177e4SLinus Torvalds 		    net_ratelimit())
12841da177e4SLinus Torvalds 			printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
12851da177e4SLinus Torvalds 				"redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
12861da177e4SLinus Torvalds 				NIPQUAD(rt->rt_src), rt->rt_iif,
12871da177e4SLinus Torvalds 				NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
12881da177e4SLinus Torvalds #endif
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds out:
12911da177e4SLinus Torvalds         in_dev_put(in_dev);
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
12951da177e4SLinus Torvalds {
12961da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
12971da177e4SLinus Torvalds 	unsigned long now;
12981da177e4SLinus Torvalds 	int code;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	switch (rt->u.dst.error) {
13011da177e4SLinus Torvalds 		case EINVAL:
13021da177e4SLinus Torvalds 		default:
13031da177e4SLinus Torvalds 			goto out;
13041da177e4SLinus Torvalds 		case EHOSTUNREACH:
13051da177e4SLinus Torvalds 			code = ICMP_HOST_UNREACH;
13061da177e4SLinus Torvalds 			break;
13071da177e4SLinus Torvalds 		case ENETUNREACH:
13081da177e4SLinus Torvalds 			code = ICMP_NET_UNREACH;
13091da177e4SLinus Torvalds 			break;
13101da177e4SLinus Torvalds 		case EACCES:
13111da177e4SLinus Torvalds 			code = ICMP_PKT_FILTERED;
13121da177e4SLinus Torvalds 			break;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	now = jiffies;
13161da177e4SLinus Torvalds 	rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
13171da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens > ip_rt_error_burst)
13181da177e4SLinus Torvalds 		rt->u.dst.rate_tokens = ip_rt_error_burst;
13191da177e4SLinus Torvalds 	rt->u.dst.rate_last = now;
13201da177e4SLinus Torvalds 	if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
13211da177e4SLinus Torvalds 		rt->u.dst.rate_tokens -= ip_rt_error_cost;
13221da177e4SLinus Torvalds 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds out:	kfree_skb(skb);
13261da177e4SLinus Torvalds 	return 0;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds /*
13301da177e4SLinus Torvalds  *	The last two values are not from the RFC but
13311da177e4SLinus Torvalds  *	are needed for AMPRnet AX.25 paths.
13321da177e4SLinus Torvalds  */
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds static unsigned short mtu_plateau[] =
13351da177e4SLinus Torvalds {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	int i;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
13421da177e4SLinus Torvalds 		if (old_mtu > mtu_plateau[i])
13431da177e4SLinus Torvalds 			return mtu_plateau[i];
13441da177e4SLinus Torvalds 	return 68;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	int i;
13501da177e4SLinus Torvalds 	unsigned short old_mtu = ntohs(iph->tot_len);
13511da177e4SLinus Torvalds 	struct rtable *rth;
13521da177e4SLinus Torvalds 	u32  skeys[2] = { iph->saddr, 0, };
13531da177e4SLinus Torvalds 	u32  daddr = iph->daddr;
13541da177e4SLinus Torvalds 	u8   tos = iph->tos & IPTOS_RT_MASK;
13551da177e4SLinus Torvalds 	unsigned short est_mtu = 0;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
13581da177e4SLinus Torvalds 		return 0;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
13611da177e4SLinus Torvalds 		unsigned hash = rt_hash_code(daddr, skeys[i], tos);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		rcu_read_lock();
13641da177e4SLinus Torvalds 		for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
13651da177e4SLinus Torvalds 		     rth = rcu_dereference(rth->u.rt_next)) {
13661da177e4SLinus Torvalds 			if (rth->fl.fl4_dst == daddr &&
13671da177e4SLinus Torvalds 			    rth->fl.fl4_src == skeys[i] &&
13681da177e4SLinus Torvalds 			    rth->rt_dst  == daddr &&
13691da177e4SLinus Torvalds 			    rth->rt_src  == iph->saddr &&
13701da177e4SLinus Torvalds 			    rth->fl.fl4_tos == tos &&
13711da177e4SLinus Torvalds 			    rth->fl.iif == 0 &&
13721da177e4SLinus Torvalds 			    !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
13731da177e4SLinus Torvalds 				unsigned short mtu = new_mtu;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 				if (new_mtu < 68 || new_mtu >= old_mtu) {
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 					/* BSD 4.2 compatibility hack :-( */
13781da177e4SLinus Torvalds 					if (mtu == 0 &&
13791da177e4SLinus Torvalds 					    old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
13801da177e4SLinus Torvalds 					    old_mtu >= 68 + (iph->ihl << 2))
13811da177e4SLinus Torvalds 						old_mtu -= iph->ihl << 2;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 					mtu = guess_mtu(old_mtu);
13841da177e4SLinus Torvalds 				}
13851da177e4SLinus Torvalds 				if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
13861da177e4SLinus Torvalds 					if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
13871da177e4SLinus Torvalds 						dst_confirm(&rth->u.dst);
13881da177e4SLinus Torvalds 						if (mtu < ip_rt_min_pmtu) {
13891da177e4SLinus Torvalds 							mtu = ip_rt_min_pmtu;
13901da177e4SLinus Torvalds 							rth->u.dst.metrics[RTAX_LOCK-1] |=
13911da177e4SLinus Torvalds 								(1 << RTAX_MTU);
13921da177e4SLinus Torvalds 						}
13931da177e4SLinus Torvalds 						rth->u.dst.metrics[RTAX_MTU-1] = mtu;
13941da177e4SLinus Torvalds 						dst_set_expires(&rth->u.dst,
13951da177e4SLinus Torvalds 							ip_rt_mtu_expires);
13961da177e4SLinus Torvalds 					}
13971da177e4SLinus Torvalds 					est_mtu = mtu;
13981da177e4SLinus Torvalds 				}
13991da177e4SLinus Torvalds 			}
14001da177e4SLinus Torvalds 		}
14011da177e4SLinus Torvalds 		rcu_read_unlock();
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 	return est_mtu ? : new_mtu;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
14091da177e4SLinus Torvalds 	    !(dst_metric_locked(dst, RTAX_MTU))) {
14101da177e4SLinus Torvalds 		if (mtu < ip_rt_min_pmtu) {
14111da177e4SLinus Torvalds 			mtu = ip_rt_min_pmtu;
14121da177e4SLinus Torvalds 			dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 		dst->metrics[RTAX_MTU-1] = mtu;
14151da177e4SLinus Torvalds 		dst_set_expires(dst, ip_rt_mtu_expires);
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	return NULL;
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst)
14251da177e4SLinus Torvalds {
14261da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
14271da177e4SLinus Torvalds 	struct inet_peer *peer = rt->peer;
14281da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	if (peer) {
14311da177e4SLinus Torvalds 		rt->peer = NULL;
14321da177e4SLinus Torvalds 		inet_putpeer(peer);
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	if (idev) {
14361da177e4SLinus Torvalds 		rt->idev = NULL;
14371da177e4SLinus Torvalds 		in_dev_put(idev);
14381da177e4SLinus Torvalds 	}
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
14421da177e4SLinus Torvalds 			    int how)
14431da177e4SLinus Torvalds {
14441da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *) dst;
14451da177e4SLinus Torvalds 	struct in_device *idev = rt->idev;
14461da177e4SLinus Torvalds 	if (dev != &loopback_dev && idev && idev->dev == dev) {
14471da177e4SLinus Torvalds 		struct in_device *loopback_idev = in_dev_get(&loopback_dev);
14481da177e4SLinus Torvalds 		if (loopback_idev) {
14491da177e4SLinus Torvalds 			rt->idev = loopback_idev;
14501da177e4SLinus Torvalds 			in_dev_put(idev);
14511da177e4SLinus Torvalds 		}
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds }
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb)
14561da177e4SLinus Torvalds {
14571da177e4SLinus Torvalds 	struct rtable *rt;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	rt = (struct rtable *) skb->dst;
14621da177e4SLinus Torvalds 	if (rt)
14631da177e4SLinus Torvalds 		dst_set_expires(&rt->u.dst, 0);
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb)
14671da177e4SLinus Torvalds {
14681da177e4SLinus Torvalds 	printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
14691da177e4SLinus Torvalds 		NIPQUAD(skb->nh.iph->saddr), NIPQUAD(skb->nh.iph->daddr),
14701da177e4SLinus Torvalds 		skb->dev ? skb->dev->name : "?");
14711da177e4SLinus Torvalds 	kfree_skb(skb);
14721da177e4SLinus Torvalds 	return 0;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds /*
14761da177e4SLinus Torvalds    We do not cache source address of outgoing interface,
14771da177e4SLinus Torvalds    because it is used only by IP RR, TS and SRR options,
14781da177e4SLinus Torvalds    so that it out of fast path.
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds    BTW remember: "addr" is allowed to be not aligned
14811da177e4SLinus Torvalds    in IP options!
14821da177e4SLinus Torvalds  */
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds void ip_rt_get_source(u8 *addr, struct rtable *rt)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	u32 src;
14871da177e4SLinus Torvalds 	struct fib_result res;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	if (rt->fl.iif == 0)
14901da177e4SLinus Torvalds 		src = rt->rt_src;
14911da177e4SLinus Torvalds 	else if (fib_lookup(&rt->fl, &res) == 0) {
14921da177e4SLinus Torvalds 		src = FIB_RES_PREFSRC(res);
14931da177e4SLinus Torvalds 		fib_res_put(&res);
14941da177e4SLinus Torvalds 	} else
14951da177e4SLinus Torvalds 		src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
14961da177e4SLinus Torvalds 					RT_SCOPE_UNIVERSE);
14971da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
14981da177e4SLinus Torvalds }
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
15011da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF))
15041da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF;
15051da177e4SLinus Torvalds 	if (!(rt->u.dst.tclassid & 0xFFFF0000))
15061da177e4SLinus Torvalds 		rt->u.dst.tclassid |= tag & 0xFFFF0000;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds #endif
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	struct fib_info *fi = res->fi;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	if (fi) {
15151da177e4SLinus Torvalds 		if (FIB_RES_GW(*res) &&
15161da177e4SLinus Torvalds 		    FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
15171da177e4SLinus Torvalds 			rt->rt_gateway = FIB_RES_GW(*res);
15181da177e4SLinus Torvalds 		memcpy(rt->u.dst.metrics, fi->fib_metrics,
15191da177e4SLinus Torvalds 		       sizeof(rt->u.dst.metrics));
15201da177e4SLinus Torvalds 		if (fi->fib_mtu == 0) {
15211da177e4SLinus Torvalds 			rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
15221da177e4SLinus Torvalds 			if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
15231da177e4SLinus Torvalds 			    rt->rt_gateway != rt->rt_dst &&
15241da177e4SLinus Torvalds 			    rt->u.dst.dev->mtu > 576)
15251da177e4SLinus Torvalds 				rt->u.dst.metrics[RTAX_MTU-1] = 576;
15261da177e4SLinus Torvalds 		}
15271da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
15281da177e4SLinus Torvalds 		rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
15291da177e4SLinus Torvalds #endif
15301da177e4SLinus Torvalds 	} else
15311da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
15341da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
15351da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
15361da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
15371da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
15381da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
15391da177e4SLinus Torvalds 				       ip_rt_min_advmss);
15401da177e4SLinus Torvalds 	if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
15411da177e4SLinus Torvalds 		rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
15441da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
15451da177e4SLinus Torvalds 	set_class_tag(rt, fib_rules_tclass(res));
15461da177e4SLinus Torvalds #endif
15471da177e4SLinus Torvalds 	set_class_tag(rt, itag);
15481da177e4SLinus Torvalds #endif
15491da177e4SLinus Torvalds         rt->rt_type = res->type;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds static int ip_route_input_mc(struct sk_buff *skb, u32 daddr, u32 saddr,
15531da177e4SLinus Torvalds 				u8 tos, struct net_device *dev, int our)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	unsigned hash;
15561da177e4SLinus Torvalds 	struct rtable *rth;
15571da177e4SLinus Torvalds 	u32 spec_dst;
15581da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
15591da177e4SLinus Torvalds 	u32 itag = 0;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* Primary sanity checks. */
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	if (in_dev == NULL)
15641da177e4SLinus Torvalds 		return -EINVAL;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr) ||
15671da177e4SLinus Torvalds 	    skb->protocol != htons(ETH_P_IP))
15681da177e4SLinus Torvalds 		goto e_inval;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	if (ZERONET(saddr)) {
15711da177e4SLinus Torvalds 		if (!LOCAL_MCAST(daddr))
15721da177e4SLinus Torvalds 			goto e_inval;
15731da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
15741da177e4SLinus Torvalds 	} else if (fib_validate_source(saddr, 0, tos, 0,
15751da177e4SLinus Torvalds 					dev, &spec_dst, &itag) < 0)
15761da177e4SLinus Torvalds 		goto e_inval;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
15791da177e4SLinus Torvalds 	if (!rth)
15801da177e4SLinus Torvalds 		goto e_nobufs;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
15851da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
15861da177e4SLinus Torvalds 	if (in_dev->cnf.no_policy)
15871da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
15881da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
15891da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
15901da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
15911da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
15921da177e4SLinus Torvalds 	rth->fl.fl4_fwmark= skb->nfmark;
15931da177e4SLinus Torvalds #endif
15941da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
15951da177e4SLinus Torvalds 	rth->rt_src	= saddr;
15961da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
15971da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
15981da177e4SLinus Torvalds #endif
15991da177e4SLinus Torvalds 	rth->rt_iif	=
16001da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
16011da177e4SLinus Torvalds 	rth->u.dst.dev	= &loopback_dev;
16021da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
16031da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
16041da177e4SLinus Torvalds 	rth->fl.oif	= 0;
16051da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
16061da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
16071da177e4SLinus Torvalds 	rth->rt_type	= RTN_MULTICAST;
16081da177e4SLinus Torvalds 	rth->rt_flags	= RTCF_MULTICAST;
16091da177e4SLinus Torvalds 	if (our) {
16101da177e4SLinus Torvalds 		rth->u.dst.input= ip_local_deliver;
16111da177e4SLinus Torvalds 		rth->rt_flags |= RTCF_LOCAL;
16121da177e4SLinus Torvalds 	}
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
16151da177e4SLinus Torvalds 	if (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
16161da177e4SLinus Torvalds 		rth->u.dst.input = ip_mr_input;
16171da177e4SLinus Torvalds #endif
16181da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	in_dev_put(in_dev);
16211da177e4SLinus Torvalds 	hash = rt_hash_code(daddr, saddr ^ (dev->ifindex << 5), tos);
16221da177e4SLinus Torvalds 	return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds e_nobufs:
16251da177e4SLinus Torvalds 	in_dev_put(in_dev);
16261da177e4SLinus Torvalds 	return -ENOBUFS;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds e_inval:
16291da177e4SLinus Torvalds 	in_dev_put(in_dev);
16301da177e4SLinus Torvalds 	return -EINVAL;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
16351da177e4SLinus Torvalds 				     struct in_device *in_dev,
16361da177e4SLinus Torvalds 				     struct sk_buff *skb,
16371da177e4SLinus Torvalds 				     u32 daddr,
16381da177e4SLinus Torvalds 				     u32 saddr)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
16411da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
16421da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
16431da177e4SLinus Torvalds 		/*
16441da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
16451da177e4SLinus Torvalds 		 *	the only hint is MAC header.
16461da177e4SLinus Torvalds 		 */
16471da177e4SLinus Torvalds 		printk(KERN_WARNING "martian source %u.%u.%u.%u from "
16481da177e4SLinus Torvalds 			"%u.%u.%u.%u, on dev %s\n",
16491da177e4SLinus Torvalds 			NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
16501da177e4SLinus Torvalds 		if (dev->hard_header_len) {
16511da177e4SLinus Torvalds 			int i;
16521da177e4SLinus Torvalds 			unsigned char *p = skb->mac.raw;
16531da177e4SLinus Torvalds 			printk(KERN_WARNING "ll header: ");
16541da177e4SLinus Torvalds 			for (i = 0; i < dev->hard_header_len; i++, p++) {
16551da177e4SLinus Torvalds 				printk("%02x", *p);
16561da177e4SLinus Torvalds 				if (i < (dev->hard_header_len - 1))
16571da177e4SLinus Torvalds 					printk(":");
16581da177e4SLinus Torvalds 			}
16591da177e4SLinus Torvalds 			printk("\n");
16601da177e4SLinus Torvalds 		}
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds #endif
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds static inline int __mkroute_input(struct sk_buff *skb,
16661da177e4SLinus Torvalds 				  struct fib_result* res,
16671da177e4SLinus Torvalds 				  struct in_device *in_dev,
16681da177e4SLinus Torvalds 				  u32 daddr, u32 saddr, u32 tos,
16691da177e4SLinus Torvalds 				  struct rtable **result)
16701da177e4SLinus Torvalds {
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	struct rtable *rth;
16731da177e4SLinus Torvalds 	int err;
16741da177e4SLinus Torvalds 	struct in_device *out_dev;
16751da177e4SLinus Torvalds 	unsigned flags = 0;
16761da177e4SLinus Torvalds 	u32 spec_dst, itag;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	/* get a working reference to the output device */
16791da177e4SLinus Torvalds 	out_dev = in_dev_get(FIB_RES_DEV(*res));
16801da177e4SLinus Torvalds 	if (out_dev == NULL) {
16811da177e4SLinus Torvalds 		if (net_ratelimit())
16821da177e4SLinus Torvalds 			printk(KERN_CRIT "Bug in ip_route_input" \
16831da177e4SLinus Torvalds 			       "_slow(). Please, report\n");
16841da177e4SLinus Torvalds 		return -EINVAL;
16851da177e4SLinus Torvalds 	}
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
16891da177e4SLinus Torvalds 				  in_dev->dev, &spec_dst, &itag);
16901da177e4SLinus Torvalds 	if (err < 0) {
16911da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
16921da177e4SLinus Torvalds 					 saddr);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 		err = -EINVAL;
16951da177e4SLinus Torvalds 		goto cleanup;
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	if (err)
16991da177e4SLinus Torvalds 		flags |= RTCF_DIRECTSRC;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
17021da177e4SLinus Torvalds 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
17031da177e4SLinus Torvalds 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
17041da177e4SLinus Torvalds 		flags |= RTCF_DOREDIRECT;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
17071da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
17081da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
17091da177e4SLinus Torvalds 		 */
17101da177e4SLinus Torvalds 		if (out_dev == in_dev && !(flags & RTCF_DNAT)) {
17111da177e4SLinus Torvalds 			err = -EINVAL;
17121da177e4SLinus Torvalds 			goto cleanup;
17131da177e4SLinus Torvalds 		}
17141da177e4SLinus Torvalds 	}
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
17181da177e4SLinus Torvalds 	if (!rth) {
17191da177e4SLinus Torvalds 		err = -ENOBUFS;
17201da177e4SLinus Torvalds 		goto cleanup;
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
17241da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
17251da177e4SLinus Torvalds 	if (res->fi->fib_nhs > 1)
17261da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_BALANCED;
17271da177e4SLinus Torvalds #endif
17281da177e4SLinus Torvalds 	if (in_dev->cnf.no_policy)
17291da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
17301da177e4SLinus Torvalds 	if (in_dev->cnf.no_xfrm)
17311da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
17321da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
17331da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
17341da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
17351da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
17361da177e4SLinus Torvalds 	rth->fl.fl4_fwmark= skb->nfmark;
17371da177e4SLinus Torvalds #endif
17381da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
17391da177e4SLinus Torvalds 	rth->rt_src	= saddr;
17401da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
17411da177e4SLinus Torvalds 	rth->rt_iif 	=
17421da177e4SLinus Torvalds 		rth->fl.iif	= in_dev->dev->ifindex;
17431da177e4SLinus Torvalds 	rth->u.dst.dev	= (out_dev)->dev;
17441da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
17451da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
17461da177e4SLinus Torvalds 	rth->fl.oif 	= 0;
17471da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	rth->u.dst.input = ip_forward;
17501da177e4SLinus Torvalds 	rth->u.dst.output = ip_output;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, itag);
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	rth->rt_flags = flags;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	*result = rth;
17571da177e4SLinus Torvalds 	err = 0;
17581da177e4SLinus Torvalds  cleanup:
17591da177e4SLinus Torvalds 	/* release the working reference to the output device */
17601da177e4SLinus Torvalds 	in_dev_put(out_dev);
17611da177e4SLinus Torvalds 	return err;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds static inline int ip_mkroute_input_def(struct sk_buff *skb,
17651da177e4SLinus Torvalds 				       struct fib_result* res,
17661da177e4SLinus Torvalds 				       const struct flowi *fl,
17671da177e4SLinus Torvalds 				       struct in_device *in_dev,
17681da177e4SLinus Torvalds 				       u32 daddr, u32 saddr, u32 tos)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	struct rtable* rth;
17711da177e4SLinus Torvalds 	int err;
17721da177e4SLinus Torvalds 	unsigned hash;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
17751da177e4SLinus Torvalds 	if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
17761da177e4SLinus Torvalds 		fib_select_multipath(fl, res);
17771da177e4SLinus Torvalds #endif
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/* create a routing cache entry */
17801da177e4SLinus Torvalds 	err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
17811da177e4SLinus Torvalds 	if (err)
17821da177e4SLinus Torvalds 		return err;
17831da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	/* put it into the cache */
17861da177e4SLinus Torvalds 	hash = rt_hash_code(daddr, saddr ^ (fl->iif << 5), tos);
17871da177e4SLinus Torvalds 	return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds static inline int ip_mkroute_input(struct sk_buff *skb,
17911da177e4SLinus Torvalds 				   struct fib_result* res,
17921da177e4SLinus Torvalds 				   const struct flowi *fl,
17931da177e4SLinus Torvalds 				   struct in_device *in_dev,
17941da177e4SLinus Torvalds 				   u32 daddr, u32 saddr, u32 tos)
17951da177e4SLinus Torvalds {
17961da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
17971da177e4SLinus Torvalds 	struct rtable* rth;
17981da177e4SLinus Torvalds 	unsigned char hop, hopcount, lasthop;
17991da177e4SLinus Torvalds 	int err = -EINVAL;
18001da177e4SLinus Torvalds 	unsigned int hash;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	if (res->fi)
18031da177e4SLinus Torvalds 		hopcount = res->fi->fib_nhs;
18041da177e4SLinus Torvalds 	else
18051da177e4SLinus Torvalds 		hopcount = 1;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	lasthop = hopcount - 1;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	/* distinguish between multipath and singlepath */
18101da177e4SLinus Torvalds 	if (hopcount < 2)
18111da177e4SLinus Torvalds 		return ip_mkroute_input_def(skb, res, fl, in_dev, daddr,
18121da177e4SLinus Torvalds 					    saddr, tos);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	/* add all alternatives to the routing cache */
18151da177e4SLinus Torvalds 	for (hop = 0; hop < hopcount; hop++) {
18161da177e4SLinus Torvalds 		res->nh_sel = hop;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 		/* create a routing cache entry */
18191da177e4SLinus Torvalds 		err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos,
18201da177e4SLinus Torvalds 				      &rth);
18211da177e4SLinus Torvalds 		if (err)
18221da177e4SLinus Torvalds 			return err;
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 		/* put it into the cache */
18251da177e4SLinus Torvalds 		hash = rt_hash_code(daddr, saddr ^ (fl->iif << 5), tos);
18261da177e4SLinus Torvalds 		err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
18271da177e4SLinus Torvalds 		if (err)
18281da177e4SLinus Torvalds 			return err;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 		/* forward hop information to multipath impl. */
18311da177e4SLinus Torvalds 		multipath_set_nhinfo(rth,
18321da177e4SLinus Torvalds 				     FIB_RES_NETWORK(*res),
18331da177e4SLinus Torvalds 				     FIB_RES_NETMASK(*res),
18341da177e4SLinus Torvalds 				     res->prefixlen,
18351da177e4SLinus Torvalds 				     &FIB_RES_NH(*res));
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 		/* only for the last hop the reference count is handled
18381da177e4SLinus Torvalds 		 * outside
18391da177e4SLinus Torvalds 		 */
18401da177e4SLinus Torvalds 		if (hop == lasthop)
18411da177e4SLinus Torvalds 			atomic_set(&(skb->dst->__refcnt), 1);
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 	return err;
18441da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED  */
18451da177e4SLinus Torvalds 	return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos);
18461da177e4SLinus Torvalds #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED  */
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds /*
18511da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
18521da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
18531da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
18541da177e4SLinus Torvalds  *
18551da177e4SLinus Torvalds  *	Such approach solves two big problems:
18561da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
18571da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
18581da177e4SLinus Torvalds  */
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds static int ip_route_input_slow(struct sk_buff *skb, u32 daddr, u32 saddr,
18611da177e4SLinus Torvalds 			       u8 tos, struct net_device *dev)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	struct fib_result res;
18641da177e4SLinus Torvalds 	struct in_device *in_dev = in_dev_get(dev);
18651da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
18661da177e4SLinus Torvalds 				      { .daddr = daddr,
18671da177e4SLinus Torvalds 					.saddr = saddr,
18681da177e4SLinus Torvalds 					.tos = tos,
18691da177e4SLinus Torvalds 					.scope = RT_SCOPE_UNIVERSE,
18701da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
18711da177e4SLinus Torvalds 					.fwmark = skb->nfmark
18721da177e4SLinus Torvalds #endif
18731da177e4SLinus Torvalds 				      } },
18741da177e4SLinus Torvalds 			    .iif = dev->ifindex };
18751da177e4SLinus Torvalds 	unsigned	flags = 0;
18761da177e4SLinus Torvalds 	u32		itag = 0;
18771da177e4SLinus Torvalds 	struct rtable * rth;
18781da177e4SLinus Torvalds 	unsigned	hash;
18791da177e4SLinus Torvalds 	u32		spec_dst;
18801da177e4SLinus Torvalds 	int		err = -EINVAL;
18811da177e4SLinus Torvalds 	int		free_res = 0;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	/* IP on this device is disabled. */
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	if (!in_dev)
18861da177e4SLinus Torvalds 		goto out;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
18891da177e4SLinus Torvalds 	   by fib_lookup.
18901da177e4SLinus Torvalds 	 */
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr))
18931da177e4SLinus Torvalds 		goto martian_source;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	if (daddr == 0xFFFFFFFF || (saddr == 0 && daddr == 0))
18961da177e4SLinus Torvalds 		goto brd_input;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
18991da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
19001da177e4SLinus Torvalds 	 */
19011da177e4SLinus Torvalds 	if (ZERONET(saddr))
19021da177e4SLinus Torvalds 		goto martian_source;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
19051da177e4SLinus Torvalds 		goto martian_destination;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	/*
19081da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
19091da177e4SLinus Torvalds 	 */
19101da177e4SLinus Torvalds 	if ((err = fib_lookup(&fl, &res)) != 0) {
19111da177e4SLinus Torvalds 		if (!IN_DEV_FORWARD(in_dev))
19121da177e4SLinus Torvalds 			goto e_inval;
19131da177e4SLinus Torvalds 		goto no_route;
19141da177e4SLinus Torvalds 	}
19151da177e4SLinus Torvalds 	free_res = 1;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_tot);
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	if (res.type == RTN_BROADCAST)
19201da177e4SLinus Torvalds 		goto brd_input;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
19231da177e4SLinus Torvalds 		int result;
19241da177e4SLinus Torvalds 		result = fib_validate_source(saddr, daddr, tos,
19251da177e4SLinus Torvalds 					     loopback_dev.ifindex,
19261da177e4SLinus Torvalds 					     dev, &spec_dst, &itag);
19271da177e4SLinus Torvalds 		if (result < 0)
19281da177e4SLinus Torvalds 			goto martian_source;
19291da177e4SLinus Torvalds 		if (result)
19301da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
19311da177e4SLinus Torvalds 		spec_dst = daddr;
19321da177e4SLinus Torvalds 		goto local_input;
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (!IN_DEV_FORWARD(in_dev))
19361da177e4SLinus Torvalds 		goto e_inval;
19371da177e4SLinus Torvalds 	if (res.type != RTN_UNICAST)
19381da177e4SLinus Torvalds 		goto martian_destination;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
19411da177e4SLinus Torvalds 	if (err == -ENOBUFS)
19421da177e4SLinus Torvalds 		goto e_nobufs;
19431da177e4SLinus Torvalds 	if (err == -EINVAL)
19441da177e4SLinus Torvalds 		goto e_inval;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds done:
19471da177e4SLinus Torvalds 	in_dev_put(in_dev);
19481da177e4SLinus Torvalds 	if (free_res)
19491da177e4SLinus Torvalds 		fib_res_put(&res);
19501da177e4SLinus Torvalds out:	return err;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds brd_input:
19531da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
19541da177e4SLinus Torvalds 		goto e_inval;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	if (ZERONET(saddr))
19571da177e4SLinus Torvalds 		spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
19581da177e4SLinus Torvalds 	else {
19591da177e4SLinus Torvalds 		err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
19601da177e4SLinus Torvalds 					  &itag);
19611da177e4SLinus Torvalds 		if (err < 0)
19621da177e4SLinus Torvalds 			goto martian_source;
19631da177e4SLinus Torvalds 		if (err)
19641da177e4SLinus Torvalds 			flags |= RTCF_DIRECTSRC;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
19671da177e4SLinus Torvalds 	res.type = RTN_BROADCAST;
19681da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds local_input:
19711da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
19721da177e4SLinus Torvalds 	if (!rth)
19731da177e4SLinus Torvalds 		goto e_nobufs;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	rth->u.dst.output= ip_rt_bug;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	atomic_set(&rth->u.dst.__refcnt, 1);
19781da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
19791da177e4SLinus Torvalds 	if (in_dev->cnf.no_policy)
19801da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
19811da177e4SLinus Torvalds 	rth->fl.fl4_dst	= daddr;
19821da177e4SLinus Torvalds 	rth->rt_dst	= daddr;
19831da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
19841da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
19851da177e4SLinus Torvalds 	rth->fl.fl4_fwmark= skb->nfmark;
19861da177e4SLinus Torvalds #endif
19871da177e4SLinus Torvalds 	rth->fl.fl4_src	= saddr;
19881da177e4SLinus Torvalds 	rth->rt_src	= saddr;
19891da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
19901da177e4SLinus Torvalds 	rth->u.dst.tclassid = itag;
19911da177e4SLinus Torvalds #endif
19921da177e4SLinus Torvalds 	rth->rt_iif	=
19931da177e4SLinus Torvalds 	rth->fl.iif	= dev->ifindex;
19941da177e4SLinus Torvalds 	rth->u.dst.dev	= &loopback_dev;
19951da177e4SLinus Torvalds 	dev_hold(rth->u.dst.dev);
19961da177e4SLinus Torvalds 	rth->idev	= in_dev_get(rth->u.dst.dev);
19971da177e4SLinus Torvalds 	rth->rt_gateway	= daddr;
19981da177e4SLinus Torvalds 	rth->rt_spec_dst= spec_dst;
19991da177e4SLinus Torvalds 	rth->u.dst.input= ip_local_deliver;
20001da177e4SLinus Torvalds 	rth->rt_flags 	= flags|RTCF_LOCAL;
20011da177e4SLinus Torvalds 	if (res.type == RTN_UNREACHABLE) {
20021da177e4SLinus Torvalds 		rth->u.dst.input= ip_error;
20031da177e4SLinus Torvalds 		rth->u.dst.error= -err;
20041da177e4SLinus Torvalds 		rth->rt_flags 	&= ~RTCF_LOCAL;
20051da177e4SLinus Torvalds 	}
20061da177e4SLinus Torvalds 	rth->rt_type	= res.type;
20071da177e4SLinus Torvalds 	hash = rt_hash_code(daddr, saddr ^ (fl.iif << 5), tos);
20081da177e4SLinus Torvalds 	err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
20091da177e4SLinus Torvalds 	goto done;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds no_route:
20121da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
20131da177e4SLinus Torvalds 	spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
20141da177e4SLinus Torvalds 	res.type = RTN_UNREACHABLE;
20151da177e4SLinus Torvalds 	goto local_input;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	/*
20181da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
20191da177e4SLinus Torvalds 	 */
20201da177e4SLinus Torvalds martian_destination:
20211da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
20221da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
20231da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
20241da177e4SLinus Torvalds 		printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
20251da177e4SLinus Torvalds 			"%u.%u.%u.%u, dev %s\n",
20261da177e4SLinus Torvalds 			NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
20271da177e4SLinus Torvalds #endif
20281da177e4SLinus Torvalds e_inval:
20291da177e4SLinus Torvalds 	err = -EINVAL;
20301da177e4SLinus Torvalds 	goto done;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds e_nobufs:
20331da177e4SLinus Torvalds 	err = -ENOBUFS;
20341da177e4SLinus Torvalds 	goto done;
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds martian_source:
20371da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
20381da177e4SLinus Torvalds 	goto e_inval;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
20421da177e4SLinus Torvalds 		   u8 tos, struct net_device *dev)
20431da177e4SLinus Torvalds {
20441da177e4SLinus Torvalds 	struct rtable * rth;
20451da177e4SLinus Torvalds 	unsigned	hash;
20461da177e4SLinus Torvalds 	int iif = dev->ifindex;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	tos &= IPTOS_RT_MASK;
20491da177e4SLinus Torvalds 	hash = rt_hash_code(daddr, saddr ^ (iif << 5), tos);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	rcu_read_lock();
20521da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
20531da177e4SLinus Torvalds 	     rth = rcu_dereference(rth->u.rt_next)) {
20541da177e4SLinus Torvalds 		if (rth->fl.fl4_dst == daddr &&
20551da177e4SLinus Torvalds 		    rth->fl.fl4_src == saddr &&
20561da177e4SLinus Torvalds 		    rth->fl.iif == iif &&
20571da177e4SLinus Torvalds 		    rth->fl.oif == 0 &&
20581da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
20591da177e4SLinus Torvalds 		    rth->fl.fl4_fwmark == skb->nfmark &&
20601da177e4SLinus Torvalds #endif
20611da177e4SLinus Torvalds 		    rth->fl.fl4_tos == tos) {
20621da177e4SLinus Torvalds 			rth->u.dst.lastuse = jiffies;
20631da177e4SLinus Torvalds 			dst_hold(&rth->u.dst);
20641da177e4SLinus Torvalds 			rth->u.dst.__use++;
20651da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(in_hit);
20661da177e4SLinus Torvalds 			rcu_read_unlock();
20671da177e4SLinus Torvalds 			skb->dst = (struct dst_entry*)rth;
20681da177e4SLinus Torvalds 			return 0;
20691da177e4SLinus Torvalds 		}
20701da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(in_hlist_search);
20711da177e4SLinus Torvalds 	}
20721da177e4SLinus Torvalds 	rcu_read_unlock();
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
20751da177e4SLinus Torvalds 	   The problem was that too many Ethernet cards have broken/missing
20761da177e4SLinus Torvalds 	   hardware multicast filters :-( As result the host on multicasting
20771da177e4SLinus Torvalds 	   network acquires a lot of useless route cache entries, sort of
20781da177e4SLinus Torvalds 	   SDR messages from all the world. Now we try to get rid of them.
20791da177e4SLinus Torvalds 	   Really, provided software IP multicast filter is organized
20801da177e4SLinus Torvalds 	   reasonably (at least, hashed), it does not result in a slowdown
20811da177e4SLinus Torvalds 	   comparing with route cache reject entries.
20821da177e4SLinus Torvalds 	   Note, that multicast routers are not affected, because
20831da177e4SLinus Torvalds 	   route cache entry is created eventually.
20841da177e4SLinus Torvalds 	 */
20851da177e4SLinus Torvalds 	if (MULTICAST(daddr)) {
20861da177e4SLinus Torvalds 		struct in_device *in_dev;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 		rcu_read_lock();
20891da177e4SLinus Torvalds 		if ((in_dev = __in_dev_get(dev)) != NULL) {
20901da177e4SLinus Torvalds 			int our = ip_check_mc(in_dev, daddr, saddr,
20911da177e4SLinus Torvalds 				skb->nh.iph->protocol);
20921da177e4SLinus Torvalds 			if (our
20931da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
20941da177e4SLinus Torvalds 			    || (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
20951da177e4SLinus Torvalds #endif
20961da177e4SLinus Torvalds 			    ) {
20971da177e4SLinus Torvalds 				rcu_read_unlock();
20981da177e4SLinus Torvalds 				return ip_route_input_mc(skb, daddr, saddr,
20991da177e4SLinus Torvalds 							 tos, dev, our);
21001da177e4SLinus Torvalds 			}
21011da177e4SLinus Torvalds 		}
21021da177e4SLinus Torvalds 		rcu_read_unlock();
21031da177e4SLinus Torvalds 		return -EINVAL;
21041da177e4SLinus Torvalds 	}
21051da177e4SLinus Torvalds 	return ip_route_input_slow(skb, daddr, saddr, tos, dev);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds static inline int __mkroute_output(struct rtable **result,
21091da177e4SLinus Torvalds 				   struct fib_result* res,
21101da177e4SLinus Torvalds 				   const struct flowi *fl,
21111da177e4SLinus Torvalds 				   const struct flowi *oldflp,
21121da177e4SLinus Torvalds 				   struct net_device *dev_out,
21131da177e4SLinus Torvalds 				   unsigned flags)
21141da177e4SLinus Torvalds {
21151da177e4SLinus Torvalds 	struct rtable *rth;
21161da177e4SLinus Torvalds 	struct in_device *in_dev;
21171da177e4SLinus Torvalds 	u32 tos = RT_FL_TOS(oldflp);
21181da177e4SLinus Torvalds 	int err = 0;
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	if (LOOPBACK(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
21211da177e4SLinus Torvalds 		return -EINVAL;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	if (fl->fl4_dst == 0xFFFFFFFF)
21241da177e4SLinus Torvalds 		res->type = RTN_BROADCAST;
21251da177e4SLinus Torvalds 	else if (MULTICAST(fl->fl4_dst))
21261da177e4SLinus Torvalds 		res->type = RTN_MULTICAST;
21271da177e4SLinus Torvalds 	else if (BADCLASS(fl->fl4_dst) || ZERONET(fl->fl4_dst))
21281da177e4SLinus Torvalds 		return -EINVAL;
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
21311da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	/* get work reference to inet device */
21341da177e4SLinus Torvalds 	in_dev = in_dev_get(dev_out);
21351da177e4SLinus Torvalds 	if (!in_dev)
21361da177e4SLinus Torvalds 		return -EINVAL;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	if (res->type == RTN_BROADCAST) {
21391da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
21401da177e4SLinus Torvalds 		if (res->fi) {
21411da177e4SLinus Torvalds 			fib_info_put(res->fi);
21421da177e4SLinus Torvalds 			res->fi = NULL;
21431da177e4SLinus Torvalds 		}
21441da177e4SLinus Torvalds 	} else if (res->type == RTN_MULTICAST) {
21451da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST|RTCF_LOCAL;
21461da177e4SLinus Torvalds 		if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
21471da177e4SLinus Torvalds 				 oldflp->proto))
21481da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
21491da177e4SLinus Torvalds 		/* If multicast route do not exist use
21501da177e4SLinus Torvalds 		   default one, but do not gateway in this case.
21511da177e4SLinus Torvalds 		   Yes, it is hack.
21521da177e4SLinus Torvalds 		 */
21531da177e4SLinus Torvalds 		if (res->fi && res->prefixlen < 4) {
21541da177e4SLinus Torvalds 			fib_info_put(res->fi);
21551da177e4SLinus Torvalds 			res->fi = NULL;
21561da177e4SLinus Torvalds 		}
21571da177e4SLinus Torvalds 	}
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	rth = dst_alloc(&ipv4_dst_ops);
21611da177e4SLinus Torvalds 	if (!rth) {
21621da177e4SLinus Torvalds 		err = -ENOBUFS;
21631da177e4SLinus Torvalds 		goto cleanup;
21641da177e4SLinus Torvalds 	}
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	rth->u.dst.flags= DST_HOST;
21671da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
21681da177e4SLinus Torvalds 	if (res->fi) {
21691da177e4SLinus Torvalds 		rth->rt_multipath_alg = res->fi->fib_mp_alg;
21701da177e4SLinus Torvalds 		if (res->fi->fib_nhs > 1)
21711da177e4SLinus Torvalds 			rth->u.dst.flags |= DST_BALANCED;
21721da177e4SLinus Torvalds 	}
21731da177e4SLinus Torvalds #endif
21741da177e4SLinus Torvalds 	if (in_dev->cnf.no_xfrm)
21751da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOXFRM;
21761da177e4SLinus Torvalds 	if (in_dev->cnf.no_policy)
21771da177e4SLinus Torvalds 		rth->u.dst.flags |= DST_NOPOLICY;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	rth->fl.fl4_dst	= oldflp->fl4_dst;
21801da177e4SLinus Torvalds 	rth->fl.fl4_tos	= tos;
21811da177e4SLinus Torvalds 	rth->fl.fl4_src	= oldflp->fl4_src;
21821da177e4SLinus Torvalds 	rth->fl.oif	= oldflp->oif;
21831da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
21841da177e4SLinus Torvalds 	rth->fl.fl4_fwmark= oldflp->fl4_fwmark;
21851da177e4SLinus Torvalds #endif
21861da177e4SLinus Torvalds 	rth->rt_dst	= fl->fl4_dst;
21871da177e4SLinus Torvalds 	rth->rt_src	= fl->fl4_src;
21881da177e4SLinus Torvalds 	rth->rt_iif	= oldflp->oif ? : dev_out->ifindex;
21891da177e4SLinus Torvalds 	/* get references to the devices that are to be hold by the routing
21901da177e4SLinus Torvalds 	   cache entry */
21911da177e4SLinus Torvalds 	rth->u.dst.dev	= dev_out;
21921da177e4SLinus Torvalds 	dev_hold(dev_out);
21931da177e4SLinus Torvalds 	rth->idev	= in_dev_get(dev_out);
21941da177e4SLinus Torvalds 	rth->rt_gateway = fl->fl4_dst;
21951da177e4SLinus Torvalds 	rth->rt_spec_dst= fl->fl4_src;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	rth->u.dst.output=ip_output;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	if (flags & RTCF_LOCAL) {
22021da177e4SLinus Torvalds 		rth->u.dst.input = ip_local_deliver;
22031da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_dst;
22041da177e4SLinus Torvalds 	}
22051da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
22061da177e4SLinus Torvalds 		rth->rt_spec_dst = fl->fl4_src;
22071da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
22081da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
22091da177e4SLinus Torvalds 			rth->u.dst.output = ip_mc_output;
22101da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
22111da177e4SLinus Torvalds 		}
22121da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
22131da177e4SLinus Torvalds 		if (res->type == RTN_MULTICAST) {
22141da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
22151da177e4SLinus Torvalds 			    !LOCAL_MCAST(oldflp->fl4_dst)) {
22161da177e4SLinus Torvalds 				rth->u.dst.input = ip_mr_input;
22171da177e4SLinus Torvalds 				rth->u.dst.output = ip_mc_output;
22181da177e4SLinus Torvalds 			}
22191da177e4SLinus Torvalds 		}
22201da177e4SLinus Torvalds #endif
22211da177e4SLinus Torvalds 	}
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	rt_set_nexthop(rth, res, 0);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	rth->rt_flags = flags;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	*result = rth;
22281da177e4SLinus Torvalds  cleanup:
22291da177e4SLinus Torvalds 	/* release work reference to inet device */
22301da177e4SLinus Torvalds 	in_dev_put(in_dev);
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	return err;
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds static inline int ip_mkroute_output_def(struct rtable **rp,
22361da177e4SLinus Torvalds 					struct fib_result* res,
22371da177e4SLinus Torvalds 					const struct flowi *fl,
22381da177e4SLinus Torvalds 					const struct flowi *oldflp,
22391da177e4SLinus Torvalds 					struct net_device *dev_out,
22401da177e4SLinus Torvalds 					unsigned flags)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	struct rtable *rth;
22431da177e4SLinus Torvalds 	int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
22441da177e4SLinus Torvalds 	unsigned hash;
22451da177e4SLinus Torvalds 	if (err == 0) {
22461da177e4SLinus Torvalds 		u32 tos = RT_FL_TOS(oldflp);
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 		atomic_set(&rth->u.dst.__refcnt, 1);
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 		hash = rt_hash_code(oldflp->fl4_dst,
22511da177e4SLinus Torvalds 				    oldflp->fl4_src ^ (oldflp->oif << 5), tos);
22521da177e4SLinus Torvalds 		err = rt_intern_hash(hash, rth, rp);
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	return err;
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static inline int ip_mkroute_output(struct rtable** rp,
22591da177e4SLinus Torvalds 				    struct fib_result* res,
22601da177e4SLinus Torvalds 				    const struct flowi *fl,
22611da177e4SLinus Torvalds 				    const struct flowi *oldflp,
22621da177e4SLinus Torvalds 				    struct net_device *dev_out,
22631da177e4SLinus Torvalds 				    unsigned flags)
22641da177e4SLinus Torvalds {
22651da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
22661da177e4SLinus Torvalds 	u32 tos = RT_FL_TOS(oldflp);
22671da177e4SLinus Torvalds 	unsigned char hop;
22681da177e4SLinus Torvalds 	unsigned hash;
22691da177e4SLinus Torvalds 	int err = -EINVAL;
22701da177e4SLinus Torvalds 	struct rtable *rth;
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	if (res->fi && res->fi->fib_nhs > 1) {
22731da177e4SLinus Torvalds 		unsigned char hopcount = res->fi->fib_nhs;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 		for (hop = 0; hop < hopcount; hop++) {
22761da177e4SLinus Torvalds 			struct net_device *dev2nexthop;
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 			res->nh_sel = hop;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 			/* hold a work reference to the output device */
22811da177e4SLinus Torvalds 			dev2nexthop = FIB_RES_DEV(*res);
22821da177e4SLinus Torvalds 			dev_hold(dev2nexthop);
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 			err = __mkroute_output(&rth, res, fl, oldflp,
22851da177e4SLinus Torvalds 					       dev2nexthop, flags);
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 			if (err != 0)
22881da177e4SLinus Torvalds 				goto cleanup;
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 			hash = rt_hash_code(oldflp->fl4_dst,
22911da177e4SLinus Torvalds 					    oldflp->fl4_src ^
22921da177e4SLinus Torvalds 					    (oldflp->oif << 5), tos);
22931da177e4SLinus Torvalds 			err = rt_intern_hash(hash, rth, rp);
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 			/* forward hop information to multipath impl. */
22961da177e4SLinus Torvalds 			multipath_set_nhinfo(rth,
22971da177e4SLinus Torvalds 					     FIB_RES_NETWORK(*res),
22981da177e4SLinus Torvalds 					     FIB_RES_NETMASK(*res),
22991da177e4SLinus Torvalds 					     res->prefixlen,
23001da177e4SLinus Torvalds 					     &FIB_RES_NH(*res));
23011da177e4SLinus Torvalds 		cleanup:
23021da177e4SLinus Torvalds 			/* release work reference to output device */
23031da177e4SLinus Torvalds 			dev_put(dev2nexthop);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 			if (err != 0)
23061da177e4SLinus Torvalds 				return err;
23071da177e4SLinus Torvalds 		}
23081da177e4SLinus Torvalds 		atomic_set(&(*rp)->u.dst.__refcnt, 1);
23091da177e4SLinus Torvalds 		return err;
23101da177e4SLinus Torvalds 	} else {
23111da177e4SLinus Torvalds 		return ip_mkroute_output_def(rp, res, fl, oldflp, dev_out,
23121da177e4SLinus Torvalds 					     flags);
23131da177e4SLinus Torvalds 	}
23141da177e4SLinus Torvalds #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
23151da177e4SLinus Torvalds 	return ip_mkroute_output_def(rp, res, fl, oldflp, dev_out, flags);
23161da177e4SLinus Torvalds #endif
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds /*
23201da177e4SLinus Torvalds  * Major route resolver routine.
23211da177e4SLinus Torvalds  */
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
23241da177e4SLinus Torvalds {
23251da177e4SLinus Torvalds 	u32 tos	= RT_FL_TOS(oldflp);
23261da177e4SLinus Torvalds 	struct flowi fl = { .nl_u = { .ip4_u =
23271da177e4SLinus Torvalds 				      { .daddr = oldflp->fl4_dst,
23281da177e4SLinus Torvalds 					.saddr = oldflp->fl4_src,
23291da177e4SLinus Torvalds 					.tos = tos & IPTOS_RT_MASK,
23301da177e4SLinus Torvalds 					.scope = ((tos & RTO_ONLINK) ?
23311da177e4SLinus Torvalds 						  RT_SCOPE_LINK :
23321da177e4SLinus Torvalds 						  RT_SCOPE_UNIVERSE),
23331da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
23341da177e4SLinus Torvalds 					.fwmark = oldflp->fl4_fwmark
23351da177e4SLinus Torvalds #endif
23361da177e4SLinus Torvalds 				      } },
23371da177e4SLinus Torvalds 			    .iif = loopback_dev.ifindex,
23381da177e4SLinus Torvalds 			    .oif = oldflp->oif };
23391da177e4SLinus Torvalds 	struct fib_result res;
23401da177e4SLinus Torvalds 	unsigned flags = 0;
23411da177e4SLinus Torvalds 	struct net_device *dev_out = NULL;
23421da177e4SLinus Torvalds 	int free_res = 0;
23431da177e4SLinus Torvalds 	int err;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	res.fi		= NULL;
23471da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
23481da177e4SLinus Torvalds 	res.r		= NULL;
23491da177e4SLinus Torvalds #endif
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	if (oldflp->fl4_src) {
23521da177e4SLinus Torvalds 		err = -EINVAL;
23531da177e4SLinus Torvalds 		if (MULTICAST(oldflp->fl4_src) ||
23541da177e4SLinus Torvalds 		    BADCLASS(oldflp->fl4_src) ||
23551da177e4SLinus Torvalds 		    ZERONET(oldflp->fl4_src))
23561da177e4SLinus Torvalds 			goto out;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 		/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
23591da177e4SLinus Torvalds 		dev_out = ip_dev_find(oldflp->fl4_src);
23601da177e4SLinus Torvalds 		if (dev_out == NULL)
23611da177e4SLinus Torvalds 			goto out;
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
23641da177e4SLinus Torvalds 		   It was wrong for two reasons:
23651da177e4SLinus Torvalds 		   1. ip_dev_find(saddr) can return wrong iface, if saddr is
23661da177e4SLinus Torvalds 		      assigned to multiple interfaces.
23671da177e4SLinus Torvalds 		   2. Moreover, we are allowed to send packets with saddr
23681da177e4SLinus Torvalds 		      of another iface. --ANK
23691da177e4SLinus Torvalds 		 */
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 		if (oldflp->oif == 0
23721da177e4SLinus Torvalds 		    && (MULTICAST(oldflp->fl4_dst) || oldflp->fl4_dst == 0xFFFFFFFF)) {
23731da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
23741da177e4SLinus Torvalds 			   and limited broadcast via necessary interface
23751da177e4SLinus Torvalds 			   without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
23761da177e4SLinus Torvalds 			   This hack is not just for fun, it allows
23771da177e4SLinus Torvalds 			   vic,vat and friends to work.
23781da177e4SLinus Torvalds 			   They bind socket to loopback, set ttl to zero
23791da177e4SLinus Torvalds 			   and expect that it will work.
23801da177e4SLinus Torvalds 			   From the viewpoint of routing cache they are broken,
23811da177e4SLinus Torvalds 			   because we are not allowed to build multicast path
23821da177e4SLinus Torvalds 			   with loopback source addr (look, routing cache
23831da177e4SLinus Torvalds 			   cannot know, that ttl is zero, so that packet
23841da177e4SLinus Torvalds 			   will not leave this host and route is valid).
23851da177e4SLinus Torvalds 			   Luckily, this hack is good workaround.
23861da177e4SLinus Torvalds 			 */
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 			fl.oif = dev_out->ifindex;
23891da177e4SLinus Torvalds 			goto make_route;
23901da177e4SLinus Torvalds 		}
23911da177e4SLinus Torvalds 		if (dev_out)
23921da177e4SLinus Torvalds 			dev_put(dev_out);
23931da177e4SLinus Torvalds 		dev_out = NULL;
23941da177e4SLinus Torvalds 	}
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (oldflp->oif) {
23981da177e4SLinus Torvalds 		dev_out = dev_get_by_index(oldflp->oif);
23991da177e4SLinus Torvalds 		err = -ENODEV;
24001da177e4SLinus Torvalds 		if (dev_out == NULL)
24011da177e4SLinus Torvalds 			goto out;
24021da177e4SLinus Torvalds 		if (__in_dev_get(dev_out) == NULL) {
24031da177e4SLinus Torvalds 			dev_put(dev_out);
24041da177e4SLinus Torvalds 			goto out;	/* Wrong error code */
24051da177e4SLinus Torvalds 		}
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 		if (LOCAL_MCAST(oldflp->fl4_dst) || oldflp->fl4_dst == 0xFFFFFFFF) {
24081da177e4SLinus Torvalds 			if (!fl.fl4_src)
24091da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
24101da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
24111da177e4SLinus Torvalds 			goto make_route;
24121da177e4SLinus Torvalds 		}
24131da177e4SLinus Torvalds 		if (!fl.fl4_src) {
24141da177e4SLinus Torvalds 			if (MULTICAST(oldflp->fl4_dst))
24151da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
24161da177e4SLinus Torvalds 							      fl.fl4_scope);
24171da177e4SLinus Torvalds 			else if (!oldflp->fl4_dst)
24181da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
24191da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
24201da177e4SLinus Torvalds 		}
24211da177e4SLinus Torvalds 	}
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	if (!fl.fl4_dst) {
24241da177e4SLinus Torvalds 		fl.fl4_dst = fl.fl4_src;
24251da177e4SLinus Torvalds 		if (!fl.fl4_dst)
24261da177e4SLinus Torvalds 			fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
24271da177e4SLinus Torvalds 		if (dev_out)
24281da177e4SLinus Torvalds 			dev_put(dev_out);
24291da177e4SLinus Torvalds 		dev_out = &loopback_dev;
24301da177e4SLinus Torvalds 		dev_hold(dev_out);
24311da177e4SLinus Torvalds 		fl.oif = loopback_dev.ifindex;
24321da177e4SLinus Torvalds 		res.type = RTN_LOCAL;
24331da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
24341da177e4SLinus Torvalds 		goto make_route;
24351da177e4SLinus Torvalds 	}
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	if (fib_lookup(&fl, &res)) {
24381da177e4SLinus Torvalds 		res.fi = NULL;
24391da177e4SLinus Torvalds 		if (oldflp->oif) {
24401da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
24411da177e4SLinus Torvalds 			   that the destination is on link.
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 			   WHY? DW.
24441da177e4SLinus Torvalds 			   Because we are allowed to send to iface
24451da177e4SLinus Torvalds 			   even if it has NO routes and NO assigned
24461da177e4SLinus Torvalds 			   addresses. When oif is specified, routing
24471da177e4SLinus Torvalds 			   tables are looked up with only one purpose:
24481da177e4SLinus Torvalds 			   to catch if destination is gatewayed, rather than
24491da177e4SLinus Torvalds 			   direct. Moreover, if MSG_DONTROUTE is set,
24501da177e4SLinus Torvalds 			   we send packet, ignoring both routing tables
24511da177e4SLinus Torvalds 			   and ifaddr state. --ANK
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 			   We could make it even if oif is unknown,
24551da177e4SLinus Torvalds 			   likely IPv6, but we do not.
24561da177e4SLinus Torvalds 			 */
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 			if (fl.fl4_src == 0)
24591da177e4SLinus Torvalds 				fl.fl4_src = inet_select_addr(dev_out, 0,
24601da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
24611da177e4SLinus Torvalds 			res.type = RTN_UNICAST;
24621da177e4SLinus Torvalds 			goto make_route;
24631da177e4SLinus Torvalds 		}
24641da177e4SLinus Torvalds 		if (dev_out)
24651da177e4SLinus Torvalds 			dev_put(dev_out);
24661da177e4SLinus Torvalds 		err = -ENETUNREACH;
24671da177e4SLinus Torvalds 		goto out;
24681da177e4SLinus Torvalds 	}
24691da177e4SLinus Torvalds 	free_res = 1;
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
24721da177e4SLinus Torvalds 		if (!fl.fl4_src)
24731da177e4SLinus Torvalds 			fl.fl4_src = fl.fl4_dst;
24741da177e4SLinus Torvalds 		if (dev_out)
24751da177e4SLinus Torvalds 			dev_put(dev_out);
24761da177e4SLinus Torvalds 		dev_out = &loopback_dev;
24771da177e4SLinus Torvalds 		dev_hold(dev_out);
24781da177e4SLinus Torvalds 		fl.oif = dev_out->ifindex;
24791da177e4SLinus Torvalds 		if (res.fi)
24801da177e4SLinus Torvalds 			fib_info_put(res.fi);
24811da177e4SLinus Torvalds 		res.fi = NULL;
24821da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
24831da177e4SLinus Torvalds 		goto make_route;
24841da177e4SLinus Torvalds 	}
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
24871da177e4SLinus Torvalds 	if (res.fi->fib_nhs > 1 && fl.oif == 0)
24881da177e4SLinus Torvalds 		fib_select_multipath(&fl, &res);
24891da177e4SLinus Torvalds 	else
24901da177e4SLinus Torvalds #endif
24911da177e4SLinus Torvalds 	if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
24921da177e4SLinus Torvalds 		fib_select_default(&fl, &res);
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	if (!fl.fl4_src)
24951da177e4SLinus Torvalds 		fl.fl4_src = FIB_RES_PREFSRC(res);
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	if (dev_out)
24981da177e4SLinus Torvalds 		dev_put(dev_out);
24991da177e4SLinus Torvalds 	dev_out = FIB_RES_DEV(res);
25001da177e4SLinus Torvalds 	dev_hold(dev_out);
25011da177e4SLinus Torvalds 	fl.oif = dev_out->ifindex;
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds make_route:
25051da177e4SLinus Torvalds 	err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 	if (free_res)
25091da177e4SLinus Torvalds 		fib_res_put(&res);
25101da177e4SLinus Torvalds 	if (dev_out)
25111da177e4SLinus Torvalds 		dev_put(dev_out);
25121da177e4SLinus Torvalds out:	return err;
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	unsigned hash;
25181da177e4SLinus Torvalds 	struct rtable *rth;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	hash = rt_hash_code(flp->fl4_dst, flp->fl4_src ^ (flp->oif << 5), flp->fl4_tos);
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	rcu_read_lock_bh();
25231da177e4SLinus Torvalds 	for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
25241da177e4SLinus Torvalds 		rth = rcu_dereference(rth->u.rt_next)) {
25251da177e4SLinus Torvalds 		if (rth->fl.fl4_dst == flp->fl4_dst &&
25261da177e4SLinus Torvalds 		    rth->fl.fl4_src == flp->fl4_src &&
25271da177e4SLinus Torvalds 		    rth->fl.iif == 0 &&
25281da177e4SLinus Torvalds 		    rth->fl.oif == flp->oif &&
25291da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK
25301da177e4SLinus Torvalds 		    rth->fl.fl4_fwmark == flp->fl4_fwmark &&
25311da177e4SLinus Torvalds #endif
25321da177e4SLinus Torvalds 		    !((rth->fl.fl4_tos ^ flp->fl4_tos) &
25331da177e4SLinus Torvalds 			    (IPTOS_RT_MASK | RTO_ONLINK))) {
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 			/* check for multipath routes and choose one if
25361da177e4SLinus Torvalds 			 * necessary
25371da177e4SLinus Torvalds 			 */
25381da177e4SLinus Torvalds 			if (multipath_select_route(flp, rth, rp)) {
25391da177e4SLinus Torvalds 				dst_hold(&(*rp)->u.dst);
25401da177e4SLinus Torvalds 				RT_CACHE_STAT_INC(out_hit);
25411da177e4SLinus Torvalds 				rcu_read_unlock_bh();
25421da177e4SLinus Torvalds 				return 0;
25431da177e4SLinus Torvalds 			}
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 			rth->u.dst.lastuse = jiffies;
25461da177e4SLinus Torvalds 			dst_hold(&rth->u.dst);
25471da177e4SLinus Torvalds 			rth->u.dst.__use++;
25481da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_hit);
25491da177e4SLinus Torvalds 			rcu_read_unlock_bh();
25501da177e4SLinus Torvalds 			*rp = rth;
25511da177e4SLinus Torvalds 			return 0;
25521da177e4SLinus Torvalds 		}
25531da177e4SLinus Torvalds 		RT_CACHE_STAT_INC(out_hlist_search);
25541da177e4SLinus Torvalds 	}
25551da177e4SLinus Torvalds 	rcu_read_unlock_bh();
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 	return ip_route_output_slow(rp, flp);
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	int err;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	if ((err = __ip_route_output_key(rp, flp)) != 0)
25651da177e4SLinus Torvalds 		return err;
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	if (flp->proto) {
25681da177e4SLinus Torvalds 		if (!flp->fl4_src)
25691da177e4SLinus Torvalds 			flp->fl4_src = (*rp)->rt_src;
25701da177e4SLinus Torvalds 		if (!flp->fl4_dst)
25711da177e4SLinus Torvalds 			flp->fl4_dst = (*rp)->rt_dst;
25721da177e4SLinus Torvalds 		return xfrm_lookup((struct dst_entry **)rp, flp, sk, flags);
25731da177e4SLinus Torvalds 	}
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	return 0;
25761da177e4SLinus Torvalds }
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds int ip_route_output_key(struct rtable **rp, struct flowi *flp)
25791da177e4SLinus Torvalds {
25801da177e4SLinus Torvalds 	return ip_route_output_flow(rp, flp, NULL, 0);
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
25841da177e4SLinus Torvalds 			int nowait)
25851da177e4SLinus Torvalds {
25861da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
25871da177e4SLinus Torvalds 	struct rtmsg *r;
25881da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
25891da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
25901da177e4SLinus Torvalds 	struct rta_cacheinfo ci;
25911da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
25921da177e4SLinus Torvalds 	struct rtattr *eptr;
25931da177e4SLinus Torvalds #endif
25941da177e4SLinus Torvalds 	nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
25951da177e4SLinus Torvalds 	r = NLMSG_DATA(nlh);
25961da177e4SLinus Torvalds 	nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
25971da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
25981da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
25991da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
26001da177e4SLinus Torvalds 	r->rtm_tos	= rt->fl.fl4_tos;
26011da177e4SLinus Torvalds 	r->rtm_table	= RT_TABLE_MAIN;
26021da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
26031da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
26041da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
26051da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
26061da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
26071da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
26081da177e4SLinus Torvalds 	RTA_PUT(skb, RTA_DST, 4, &rt->rt_dst);
26091da177e4SLinus Torvalds 	if (rt->fl.fl4_src) {
26101da177e4SLinus Torvalds 		r->rtm_src_len = 32;
26111da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_SRC, 4, &rt->fl.fl4_src);
26121da177e4SLinus Torvalds 	}
26131da177e4SLinus Torvalds 	if (rt->u.dst.dev)
26141da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->u.dst.dev->ifindex);
26151da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
26161da177e4SLinus Torvalds 	if (rt->u.dst.tclassid)
26171da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_FLOW, 4, &rt->u.dst.tclassid);
26181da177e4SLinus Torvalds #endif
26191da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
26201da177e4SLinus Torvalds 	if (rt->rt_multipath_alg != IP_MP_ALG_NONE) {
26211da177e4SLinus Torvalds 		__u32 alg = rt->rt_multipath_alg;
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_MP_ALGO, 4, &alg);
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds #endif
26261da177e4SLinus Torvalds 	if (rt->fl.iif)
26271da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_PREFSRC, 4, &rt->rt_spec_dst);
26281da177e4SLinus Torvalds 	else if (rt->rt_src != rt->fl.fl4_src)
26291da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_PREFSRC, 4, &rt->rt_src);
26301da177e4SLinus Torvalds 	if (rt->rt_dst != rt->rt_gateway)
26311da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_GATEWAY, 4, &rt->rt_gateway);
26321da177e4SLinus Torvalds 	if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
26331da177e4SLinus Torvalds 		goto rtattr_failure;
26341da177e4SLinus Torvalds 	ci.rta_lastuse	= jiffies_to_clock_t(jiffies - rt->u.dst.lastuse);
26351da177e4SLinus Torvalds 	ci.rta_used	= rt->u.dst.__use;
26361da177e4SLinus Torvalds 	ci.rta_clntref	= atomic_read(&rt->u.dst.__refcnt);
26371da177e4SLinus Torvalds 	if (rt->u.dst.expires)
26381da177e4SLinus Torvalds 		ci.rta_expires = jiffies_to_clock_t(rt->u.dst.expires - jiffies);
26391da177e4SLinus Torvalds 	else
26401da177e4SLinus Torvalds 		ci.rta_expires = 0;
26411da177e4SLinus Torvalds 	ci.rta_error	= rt->u.dst.error;
26421da177e4SLinus Torvalds 	ci.rta_id	= ci.rta_ts = ci.rta_tsage = 0;
26431da177e4SLinus Torvalds 	if (rt->peer) {
26441da177e4SLinus Torvalds 		ci.rta_id = rt->peer->ip_id_count;
26451da177e4SLinus Torvalds 		if (rt->peer->tcp_ts_stamp) {
26461da177e4SLinus Torvalds 			ci.rta_ts = rt->peer->tcp_ts;
26471da177e4SLinus Torvalds 			ci.rta_tsage = xtime.tv_sec - rt->peer->tcp_ts_stamp;
26481da177e4SLinus Torvalds 		}
26491da177e4SLinus Torvalds 	}
26501da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
26511da177e4SLinus Torvalds 	eptr = (struct rtattr*)skb->tail;
26521da177e4SLinus Torvalds #endif
26531da177e4SLinus Torvalds 	RTA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
26541da177e4SLinus Torvalds 	if (rt->fl.iif) {
26551da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
26561da177e4SLinus Torvalds 		u32 dst = rt->rt_dst;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 		if (MULTICAST(dst) && !LOCAL_MCAST(dst) &&
26591da177e4SLinus Torvalds 		    ipv4_devconf.mc_forwarding) {
26601da177e4SLinus Torvalds 			int err = ipmr_get_route(skb, r, nowait);
26611da177e4SLinus Torvalds 			if (err <= 0) {
26621da177e4SLinus Torvalds 				if (!nowait) {
26631da177e4SLinus Torvalds 					if (err == 0)
26641da177e4SLinus Torvalds 						return 0;
26651da177e4SLinus Torvalds 					goto nlmsg_failure;
26661da177e4SLinus Torvalds 				} else {
26671da177e4SLinus Torvalds 					if (err == -EMSGSIZE)
26681da177e4SLinus Torvalds 						goto nlmsg_failure;
26691da177e4SLinus Torvalds 					((struct rta_cacheinfo*)RTA_DATA(eptr))->rta_error = err;
26701da177e4SLinus Torvalds 				}
26711da177e4SLinus Torvalds 			}
26721da177e4SLinus Torvalds 		} else
26731da177e4SLinus Torvalds #endif
26741da177e4SLinus Torvalds 			RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
26751da177e4SLinus Torvalds 	}
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
26781da177e4SLinus Torvalds 	return skb->len;
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds nlmsg_failure:
26811da177e4SLinus Torvalds rtattr_failure:
26821da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
26831da177e4SLinus Torvalds 	return -1;
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
26871da177e4SLinus Torvalds {
26881da177e4SLinus Torvalds 	struct rtattr **rta = arg;
26891da177e4SLinus Torvalds 	struct rtmsg *rtm = NLMSG_DATA(nlh);
26901da177e4SLinus Torvalds 	struct rtable *rt = NULL;
26911da177e4SLinus Torvalds 	u32 dst = 0;
26921da177e4SLinus Torvalds 	u32 src = 0;
26931da177e4SLinus Torvalds 	int iif = 0;
26941da177e4SLinus Torvalds 	int err = -ENOBUFS;
26951da177e4SLinus Torvalds 	struct sk_buff *skb;
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
26981da177e4SLinus Torvalds 	if (!skb)
26991da177e4SLinus Torvalds 		goto out;
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27021da177e4SLinus Torvalds 	   through good chunk of routing engine.
27031da177e4SLinus Torvalds 	 */
27041da177e4SLinus Torvalds 	skb->mac.raw = skb->data;
27051da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	if (rta[RTA_SRC - 1])
27081da177e4SLinus Torvalds 		memcpy(&src, RTA_DATA(rta[RTA_SRC - 1]), 4);
27091da177e4SLinus Torvalds 	if (rta[RTA_DST - 1])
27101da177e4SLinus Torvalds 		memcpy(&dst, RTA_DATA(rta[RTA_DST - 1]), 4);
27111da177e4SLinus Torvalds 	if (rta[RTA_IIF - 1])
27121da177e4SLinus Torvalds 		memcpy(&iif, RTA_DATA(rta[RTA_IIF - 1]), sizeof(int));
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	if (iif) {
27151da177e4SLinus Torvalds 		struct net_device *dev = __dev_get_by_index(iif);
27161da177e4SLinus Torvalds 		err = -ENODEV;
27171da177e4SLinus Torvalds 		if (!dev)
27181da177e4SLinus Torvalds 			goto out_free;
27191da177e4SLinus Torvalds 		skb->protocol	= htons(ETH_P_IP);
27201da177e4SLinus Torvalds 		skb->dev	= dev;
27211da177e4SLinus Torvalds 		local_bh_disable();
27221da177e4SLinus Torvalds 		err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
27231da177e4SLinus Torvalds 		local_bh_enable();
27241da177e4SLinus Torvalds 		rt = (struct rtable*)skb->dst;
27251da177e4SLinus Torvalds 		if (!err && rt->u.dst.error)
27261da177e4SLinus Torvalds 			err = -rt->u.dst.error;
27271da177e4SLinus Torvalds 	} else {
27281da177e4SLinus Torvalds 		struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dst,
27291da177e4SLinus Torvalds 							 .saddr = src,
27301da177e4SLinus Torvalds 							 .tos = rtm->rtm_tos } } };
27311da177e4SLinus Torvalds 		int oif = 0;
27321da177e4SLinus Torvalds 		if (rta[RTA_OIF - 1])
27331da177e4SLinus Torvalds 			memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
27341da177e4SLinus Torvalds 		fl.oif = oif;
27351da177e4SLinus Torvalds 		err = ip_route_output_key(&rt, &fl);
27361da177e4SLinus Torvalds 	}
27371da177e4SLinus Torvalds 	if (err)
27381da177e4SLinus Torvalds 		goto out_free;
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
27411da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
27421da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
27471da177e4SLinus Torvalds 				RTM_NEWROUTE, 0);
27481da177e4SLinus Torvalds 	if (!err)
27491da177e4SLinus Torvalds 		goto out_free;
27501da177e4SLinus Torvalds 	if (err < 0) {
27511da177e4SLinus Torvalds 		err = -EMSGSIZE;
27521da177e4SLinus Torvalds 		goto out_free;
27531da177e4SLinus Torvalds 	}
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 	err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
27561da177e4SLinus Torvalds 	if (err > 0)
27571da177e4SLinus Torvalds 		err = 0;
27581da177e4SLinus Torvalds out:	return err;
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds out_free:
27611da177e4SLinus Torvalds 	kfree_skb(skb);
27621da177e4SLinus Torvalds 	goto out;
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
27661da177e4SLinus Torvalds {
27671da177e4SLinus Torvalds 	struct rtable *rt;
27681da177e4SLinus Torvalds 	int h, s_h;
27691da177e4SLinus Torvalds 	int idx, s_idx;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	s_h = cb->args[0];
27721da177e4SLinus Torvalds 	s_idx = idx = cb->args[1];
27731da177e4SLinus Torvalds 	for (h = 0; h <= rt_hash_mask; h++) {
27741da177e4SLinus Torvalds 		if (h < s_h) continue;
27751da177e4SLinus Torvalds 		if (h > s_h)
27761da177e4SLinus Torvalds 			s_idx = 0;
27771da177e4SLinus Torvalds 		rcu_read_lock_bh();
27781da177e4SLinus Torvalds 		for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
27791da177e4SLinus Torvalds 		     rt = rcu_dereference(rt->u.rt_next), idx++) {
27801da177e4SLinus Torvalds 			if (idx < s_idx)
27811da177e4SLinus Torvalds 				continue;
27821da177e4SLinus Torvalds 			skb->dst = dst_clone(&rt->u.dst);
27831da177e4SLinus Torvalds 			if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
27841da177e4SLinus Torvalds 					 cb->nlh->nlmsg_seq,
27851da177e4SLinus Torvalds 					 RTM_NEWROUTE, 1) <= 0) {
27861da177e4SLinus Torvalds 				dst_release(xchg(&skb->dst, NULL));
27871da177e4SLinus Torvalds 				rcu_read_unlock_bh();
27881da177e4SLinus Torvalds 				goto done;
27891da177e4SLinus Torvalds 			}
27901da177e4SLinus Torvalds 			dst_release(xchg(&skb->dst, NULL));
27911da177e4SLinus Torvalds 		}
27921da177e4SLinus Torvalds 		rcu_read_unlock_bh();
27931da177e4SLinus Torvalds 	}
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds done:
27961da177e4SLinus Torvalds 	cb->args[0] = h;
27971da177e4SLinus Torvalds 	cb->args[1] = idx;
27981da177e4SLinus Torvalds 	return skb->len;
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
28021da177e4SLinus Torvalds {
28031da177e4SLinus Torvalds 	rt_cache_flush(0);
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28071da177e4SLinus Torvalds static int flush_delay;
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
28101da177e4SLinus Torvalds 					struct file *filp, void __user *buffer,
28111da177e4SLinus Torvalds 					size_t *lenp, loff_t *ppos)
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	if (write) {
28141da177e4SLinus Torvalds 		proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
28151da177e4SLinus Torvalds 		rt_cache_flush(flush_delay);
28161da177e4SLinus Torvalds 		return 0;
28171da177e4SLinus Torvalds 	}
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	return -EINVAL;
28201da177e4SLinus Torvalds }
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
28231da177e4SLinus Torvalds 						int __user *name,
28241da177e4SLinus Torvalds 						int nlen,
28251da177e4SLinus Torvalds 						void __user *oldval,
28261da177e4SLinus Torvalds 						size_t __user *oldlenp,
28271da177e4SLinus Torvalds 						void __user *newval,
28281da177e4SLinus Torvalds 						size_t newlen,
28291da177e4SLinus Torvalds 						void **context)
28301da177e4SLinus Torvalds {
28311da177e4SLinus Torvalds 	int delay;
28321da177e4SLinus Torvalds 	if (newlen != sizeof(int))
28331da177e4SLinus Torvalds 		return -EINVAL;
28341da177e4SLinus Torvalds 	if (get_user(delay, (int __user *)newval))
28351da177e4SLinus Torvalds 		return -EFAULT;
28361da177e4SLinus Torvalds 	rt_cache_flush(delay);
28371da177e4SLinus Torvalds 	return 0;
28381da177e4SLinus Torvalds }
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds ctl_table ipv4_route_table[] = {
28411da177e4SLinus Torvalds         {
28421da177e4SLinus Torvalds 		.ctl_name 	= NET_IPV4_ROUTE_FLUSH,
28431da177e4SLinus Torvalds 		.procname	= "flush",
28441da177e4SLinus Torvalds 		.data		= &flush_delay,
28451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
2846*7e3e0360SDave Jones 		.mode		= 0200,
28471da177e4SLinus Torvalds 		.proc_handler	= &ipv4_sysctl_rtcache_flush,
28481da177e4SLinus Torvalds 		.strategy	= &ipv4_sysctl_rtcache_flush_strategy,
28491da177e4SLinus Torvalds 	},
28501da177e4SLinus Torvalds 	{
28511da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_DELAY,
28521da177e4SLinus Torvalds 		.procname	= "min_delay",
28531da177e4SLinus Torvalds 		.data		= &ip_rt_min_delay,
28541da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28551da177e4SLinus Torvalds 		.mode		= 0644,
28561da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
28571da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
28581da177e4SLinus Torvalds 	},
28591da177e4SLinus Torvalds 	{
28601da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MAX_DELAY,
28611da177e4SLinus Torvalds 		.procname	= "max_delay",
28621da177e4SLinus Torvalds 		.data		= &ip_rt_max_delay,
28631da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28641da177e4SLinus Torvalds 		.mode		= 0644,
28651da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
28661da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
28671da177e4SLinus Torvalds 	},
28681da177e4SLinus Torvalds 	{
28691da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_THRESH,
28701da177e4SLinus Torvalds 		.procname	= "gc_thresh",
28711da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
28721da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28731da177e4SLinus Torvalds 		.mode		= 0644,
28741da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
28751da177e4SLinus Torvalds 	},
28761da177e4SLinus Torvalds 	{
28771da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MAX_SIZE,
28781da177e4SLinus Torvalds 		.procname	= "max_size",
28791da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
28801da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28811da177e4SLinus Torvalds 		.mode		= 0644,
28821da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
28831da177e4SLinus Torvalds 	},
28841da177e4SLinus Torvalds 	{
28851da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL,
28881da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
28891da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
28901da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
28911da177e4SLinus Torvalds 		.mode		= 0644,
28921da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
28931da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
28941da177e4SLinus Torvalds 	},
28951da177e4SLinus Torvalds 	{
28961da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
28971da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
28981da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
28991da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29001da177e4SLinus Torvalds 		.mode		= 0644,
29011da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_ms_jiffies,
29021da177e4SLinus Torvalds 		.strategy	= &sysctl_ms_jiffies,
29031da177e4SLinus Torvalds 	},
29041da177e4SLinus Torvalds 	{
29051da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_TIMEOUT,
29061da177e4SLinus Torvalds 		.procname	= "gc_timeout",
29071da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
29081da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29091da177e4SLinus Torvalds 		.mode		= 0644,
29101da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29111da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29121da177e4SLinus Torvalds 	},
29131da177e4SLinus Torvalds 	{
29141da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_INTERVAL,
29151da177e4SLinus Torvalds 		.procname	= "gc_interval",
29161da177e4SLinus Torvalds 		.data		= &ip_rt_gc_interval,
29171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29181da177e4SLinus Torvalds 		.mode		= 0644,
29191da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29201da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29211da177e4SLinus Torvalds 	},
29221da177e4SLinus Torvalds 	{
29231da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_LOAD,
29241da177e4SLinus Torvalds 		.procname	= "redirect_load",
29251da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
29261da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29271da177e4SLinus Torvalds 		.mode		= 0644,
29281da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29291da177e4SLinus Torvalds 	},
29301da177e4SLinus Torvalds 	{
29311da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_NUMBER,
29321da177e4SLinus Torvalds 		.procname	= "redirect_number",
29331da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
29341da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29351da177e4SLinus Torvalds 		.mode		= 0644,
29361da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29371da177e4SLinus Torvalds 	},
29381da177e4SLinus Torvalds 	{
29391da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_REDIRECT_SILENCE,
29401da177e4SLinus Torvalds 		.procname	= "redirect_silence",
29411da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
29421da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29431da177e4SLinus Torvalds 		.mode		= 0644,
29441da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29451da177e4SLinus Torvalds 	},
29461da177e4SLinus Torvalds 	{
29471da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_COST,
29481da177e4SLinus Torvalds 		.procname	= "error_cost",
29491da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
29501da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29511da177e4SLinus Torvalds 		.mode		= 0644,
29521da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29531da177e4SLinus Torvalds 	},
29541da177e4SLinus Torvalds 	{
29551da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_ERROR_BURST,
29561da177e4SLinus Torvalds 		.procname	= "error_burst",
29571da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
29581da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29591da177e4SLinus Torvalds 		.mode		= 0644,
29601da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29611da177e4SLinus Torvalds 	},
29621da177e4SLinus Torvalds 	{
29631da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_GC_ELASTICITY,
29641da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
29651da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
29661da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29671da177e4SLinus Torvalds 		.mode		= 0644,
29681da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29691da177e4SLinus Torvalds 	},
29701da177e4SLinus Torvalds 	{
29711da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MTU_EXPIRES,
29721da177e4SLinus Torvalds 		.procname	= "mtu_expires",
29731da177e4SLinus Torvalds 		.data		= &ip_rt_mtu_expires,
29741da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29751da177e4SLinus Torvalds 		.mode		= 0644,
29761da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
29771da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
29781da177e4SLinus Torvalds 	},
29791da177e4SLinus Torvalds 	{
29801da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_PMTU,
29811da177e4SLinus Torvalds 		.procname	= "min_pmtu",
29821da177e4SLinus Torvalds 		.data		= &ip_rt_min_pmtu,
29831da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29841da177e4SLinus Torvalds 		.mode		= 0644,
29851da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29861da177e4SLinus Torvalds 	},
29871da177e4SLinus Torvalds 	{
29881da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_MIN_ADVMSS,
29891da177e4SLinus Torvalds 		.procname	= "min_adv_mss",
29901da177e4SLinus Torvalds 		.data		= &ip_rt_min_advmss,
29911da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
29921da177e4SLinus Torvalds 		.mode		= 0644,
29931da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec,
29941da177e4SLinus Torvalds 	},
29951da177e4SLinus Torvalds 	{
29961da177e4SLinus Torvalds 		.ctl_name	= NET_IPV4_ROUTE_SECRET_INTERVAL,
29971da177e4SLinus Torvalds 		.procname	= "secret_interval",
29981da177e4SLinus Torvalds 		.data		= &ip_rt_secret_interval,
29991da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
30001da177e4SLinus Torvalds 		.mode		= 0644,
30011da177e4SLinus Torvalds 		.proc_handler	= &proc_dointvec_jiffies,
30021da177e4SLinus Torvalds 		.strategy	= &sysctl_jiffies,
30031da177e4SLinus Torvalds 	},
30041da177e4SLinus Torvalds 	{ .ctl_name = 0 }
30051da177e4SLinus Torvalds };
30061da177e4SLinus Torvalds #endif
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
30091da177e4SLinus Torvalds struct ip_rt_acct *ip_rt_acct;
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds /* This code sucks.  But you should have seen it before! --RR */
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds /* IP route accounting ptr for this logical cpu number. */
30141da177e4SLinus Torvalds #define IP_RT_ACCT_CPU(i) (ip_rt_acct + i * 256)
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
30171da177e4SLinus Torvalds static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
30181da177e4SLinus Torvalds 			   int length, int *eof, void *data)
30191da177e4SLinus Torvalds {
30201da177e4SLinus Torvalds 	unsigned int i;
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 	if ((offset & 3) || (length & 3))
30231da177e4SLinus Torvalds 		return -EIO;
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds 	if (offset >= sizeof(struct ip_rt_acct) * 256) {
30261da177e4SLinus Torvalds 		*eof = 1;
30271da177e4SLinus Torvalds 		return 0;
30281da177e4SLinus Torvalds 	}
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
30311da177e4SLinus Torvalds 		length = sizeof(struct ip_rt_acct) * 256 - offset;
30321da177e4SLinus Torvalds 		*eof = 1;
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds 	offset /= sizeof(u32);
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds 	if (length > 0) {
30381da177e4SLinus Torvalds 		u32 *src = ((u32 *) IP_RT_ACCT_CPU(0)) + offset;
30391da177e4SLinus Torvalds 		u32 *dst = (u32 *) buffer;
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 		/* Copy first cpu. */
30421da177e4SLinus Torvalds 		*start = buffer;
30431da177e4SLinus Torvalds 		memcpy(dst, src, length);
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 		/* Add the other cpus in, one int at a time */
30461da177e4SLinus Torvalds 		for_each_cpu(i) {
30471da177e4SLinus Torvalds 			unsigned int j;
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 			src = ((u32 *) IP_RT_ACCT_CPU(i)) + offset;
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 			for (j = 0; j < length/4; j++)
30521da177e4SLinus Torvalds 				dst[j] += src[j];
30531da177e4SLinus Torvalds 		}
30541da177e4SLinus Torvalds 	}
30551da177e4SLinus Torvalds 	return length;
30561da177e4SLinus Torvalds }
30571da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
30581da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_ROUTE */
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds static __initdata unsigned long rhash_entries;
30611da177e4SLinus Torvalds static int __init set_rhash_entries(char *str)
30621da177e4SLinus Torvalds {
30631da177e4SLinus Torvalds 	if (!str)
30641da177e4SLinus Torvalds 		return 0;
30651da177e4SLinus Torvalds 	rhash_entries = simple_strtoul(str, &str, 0);
30661da177e4SLinus Torvalds 	return 1;
30671da177e4SLinus Torvalds }
30681da177e4SLinus Torvalds __setup("rhash_entries=", set_rhash_entries);
30691da177e4SLinus Torvalds 
30701da177e4SLinus Torvalds int __init ip_rt_init(void)
30711da177e4SLinus Torvalds {
30721da177e4SLinus Torvalds 	int i, order, goal, rc = 0;
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 	rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
30751da177e4SLinus Torvalds 			     (jiffies ^ (jiffies >> 7)));
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
30781da177e4SLinus Torvalds 	for (order = 0;
30791da177e4SLinus Torvalds 	     (PAGE_SIZE << order) < 256 * sizeof(struct ip_rt_acct) * NR_CPUS; order++)
30801da177e4SLinus Torvalds 		/* NOTHING */;
30811da177e4SLinus Torvalds 	ip_rt_acct = (struct ip_rt_acct *)__get_free_pages(GFP_KERNEL, order);
30821da177e4SLinus Torvalds 	if (!ip_rt_acct)
30831da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
30841da177e4SLinus Torvalds 	memset(ip_rt_acct, 0, PAGE_SIZE << order);
30851da177e4SLinus Torvalds #endif
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 	ipv4_dst_ops.kmem_cachep = kmem_cache_create("ip_dst_cache",
30881da177e4SLinus Torvalds 						     sizeof(struct rtable),
30891da177e4SLinus Torvalds 						     0, SLAB_HWCACHE_ALIGN,
30901da177e4SLinus Torvalds 						     NULL, NULL);
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	if (!ipv4_dst_ops.kmem_cachep)
30931da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_dst_cache\n");
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	goal = num_physpages >> (26 - PAGE_SHIFT);
30961da177e4SLinus Torvalds 	if (rhash_entries)
30971da177e4SLinus Torvalds 		goal = (rhash_entries * sizeof(struct rt_hash_bucket)) >> PAGE_SHIFT;
30981da177e4SLinus Torvalds 	for (order = 0; (1UL << order) < goal; order++)
30991da177e4SLinus Torvalds 		/* NOTHING */;
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 	do {
31021da177e4SLinus Torvalds 		rt_hash_mask = (1UL << order) * PAGE_SIZE /
31031da177e4SLinus Torvalds 			sizeof(struct rt_hash_bucket);
31041da177e4SLinus Torvalds 		while (rt_hash_mask & (rt_hash_mask - 1))
31051da177e4SLinus Torvalds 			rt_hash_mask--;
31061da177e4SLinus Torvalds 		rt_hash_table = (struct rt_hash_bucket *)
31071da177e4SLinus Torvalds 			__get_free_pages(GFP_ATOMIC, order);
31081da177e4SLinus Torvalds 	} while (rt_hash_table == NULL && --order > 0);
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds 	if (!rt_hash_table)
31111da177e4SLinus Torvalds 		panic("Failed to allocate IP route cache hash table\n");
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	printk(KERN_INFO "IP: routing cache hash table of %u buckets, %ldKbytes\n",
31141da177e4SLinus Torvalds 	       rt_hash_mask,
31151da177e4SLinus Torvalds 	       (long) (rt_hash_mask * sizeof(struct rt_hash_bucket)) / 1024);
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds 	for (rt_hash_log = 0; (1 << rt_hash_log) != rt_hash_mask; rt_hash_log++)
31181da177e4SLinus Torvalds 		/* NOTHING */;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 	rt_hash_mask--;
31211da177e4SLinus Torvalds 	for (i = 0; i <= rt_hash_mask; i++) {
31221da177e4SLinus Torvalds 		spin_lock_init(&rt_hash_table[i].lock);
31231da177e4SLinus Torvalds 		rt_hash_table[i].chain = NULL;
31241da177e4SLinus Torvalds 	}
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 	ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
31271da177e4SLinus Torvalds 	ip_rt_max_size = (rt_hash_mask + 1) * 16;
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds 	rt_cache_stat = alloc_percpu(struct rt_cache_stat);
31301da177e4SLinus Torvalds 	if (!rt_cache_stat)
31311da177e4SLinus Torvalds 		return -ENOMEM;
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 	devinet_init();
31341da177e4SLinus Torvalds 	ip_fib_init();
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	init_timer(&rt_flush_timer);
31371da177e4SLinus Torvalds 	rt_flush_timer.function = rt_run_flush;
31381da177e4SLinus Torvalds 	init_timer(&rt_periodic_timer);
31391da177e4SLinus Torvalds 	rt_periodic_timer.function = rt_check_expire;
31401da177e4SLinus Torvalds 	init_timer(&rt_secret_timer);
31411da177e4SLinus Torvalds 	rt_secret_timer.function = rt_secret_rebuild;
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds 	/* All the timers, started at system startup tend
31441da177e4SLinus Torvalds 	   to synchronize. Perturb it a bit.
31451da177e4SLinus Torvalds 	 */
31461da177e4SLinus Torvalds 	rt_periodic_timer.expires = jiffies + net_random() % ip_rt_gc_interval +
31471da177e4SLinus Torvalds 					ip_rt_gc_interval;
31481da177e4SLinus Torvalds 	add_timer(&rt_periodic_timer);
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 	rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
31511da177e4SLinus Torvalds 		ip_rt_secret_interval;
31521da177e4SLinus Torvalds 	add_timer(&rt_secret_timer);
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
31551da177e4SLinus Torvalds 	{
31561da177e4SLinus Torvalds 	struct proc_dir_entry *rtstat_pde = NULL; /* keep gcc happy */
31571da177e4SLinus Torvalds 	if (!proc_net_fops_create("rt_cache", S_IRUGO, &rt_cache_seq_fops) ||
31581da177e4SLinus Torvalds 	    !(rtstat_pde = create_proc_entry("rt_cache", S_IRUGO,
31591da177e4SLinus Torvalds 			    		     proc_net_stat))) {
31601da177e4SLinus Torvalds 		free_percpu(rt_cache_stat);
31611da177e4SLinus Torvalds 		return -ENOMEM;
31621da177e4SLinus Torvalds 	}
31631da177e4SLinus Torvalds 	rtstat_pde->proc_fops = &rt_cpu_seq_fops;
31641da177e4SLinus Torvalds 	}
31651da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
31661da177e4SLinus Torvalds 	create_proc_read_entry("rt_acct", 0, proc_net, ip_rt_acct_read, NULL);
31671da177e4SLinus Torvalds #endif
31681da177e4SLinus Torvalds #endif
31691da177e4SLinus Torvalds #ifdef CONFIG_XFRM
31701da177e4SLinus Torvalds 	xfrm_init();
31711da177e4SLinus Torvalds 	xfrm4_init();
31721da177e4SLinus Torvalds #endif
31731da177e4SLinus Torvalds 	return rc;
31741da177e4SLinus Torvalds }
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds EXPORT_SYMBOL(__ip_select_ident);
31771da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_input);
31781da177e4SLinus Torvalds EXPORT_SYMBOL(ip_route_output_key);
3179