xref: /linux/net/ipv4/route.c (revision b42664f898c976247f7f609b8bb9c94d7475ca10)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		ROUTE - implementation of the IP router.
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
111da177e4SLinus Torvalds  *		Linus Torvalds, <Linus.Torvalds@helsinki.fi>
121da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Fixes:
151da177e4SLinus Torvalds  *		Alan Cox	:	Verify area fixes.
161da177e4SLinus Torvalds  *		Alan Cox	:	cli() protects routing changes
171da177e4SLinus Torvalds  *		Rui Oliveira	:	ICMP routing table updates
181da177e4SLinus Torvalds  *		(rco@di.uminho.pt)	Routing table insertion and update
191da177e4SLinus Torvalds  *		Linus Torvalds	:	Rewrote bits to be sensible
201da177e4SLinus Torvalds  *		Alan Cox	:	Added BSD route gw semantics
211da177e4SLinus Torvalds  *		Alan Cox	:	Super /proc >4K
221da177e4SLinus Torvalds  *		Alan Cox	:	MTU in route table
231da177e4SLinus Torvalds  *		Alan Cox	: 	MSS actually. Also added the window
241da177e4SLinus Torvalds  *					clamper.
251da177e4SLinus Torvalds  *		Sam Lantinga	:	Fixed route matching in rt_del()
261da177e4SLinus Torvalds  *		Alan Cox	:	Routing cache support.
271da177e4SLinus Torvalds  *		Alan Cox	:	Removed compatibility cruft.
281da177e4SLinus Torvalds  *		Alan Cox	:	RTF_REJECT support.
291da177e4SLinus Torvalds  *		Alan Cox	:	TCP irtt support.
301da177e4SLinus Torvalds  *		Jonathan Naylor	:	Added Metric support.
311da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	BSD API fixes.
321da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	Metrics.
331da177e4SLinus Torvalds  *		Alan Cox	:	Use __u32 properly
341da177e4SLinus Torvalds  *		Alan Cox	:	Aligned routing errors more closely with BSD
351da177e4SLinus Torvalds  *					our system is still very different.
361da177e4SLinus Torvalds  *		Alan Cox	:	Faster /proc handling
371da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Massive rework to support tree based routing,
381da177e4SLinus Torvalds  *					routing caches and better behaviour.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  *		Olaf Erb	:	irtt wasn't being copied right.
411da177e4SLinus Torvalds  *		Bjorn Ekwall	:	Kerneld route support.
421da177e4SLinus Torvalds  *		Alan Cox	:	Multicast fixed (I hope)
431da177e4SLinus Torvalds  * 		Pavel Krauz	:	Limited broadcast fixed
441da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
451da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	End of old history. Split to fib.c and
461da177e4SLinus Torvalds  *					route.c and rewritten from scratch.
471da177e4SLinus Torvalds  *		Andi Kleen	:	Load-limit warning messages.
481da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
491da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Race condition in ip_route_input_slow.
501da177e4SLinus Torvalds  *	Tobias Ringstrom	:	Uninitialized res.type in ip_route_output_slow.
511da177e4SLinus Torvalds  *	Vladimir V. Ivanov	:	IP rule info (flowid) is really useful.
521da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
531da177e4SLinus Torvalds  *	Robert Olsson		:	Added rt_cache statistics
541da177e4SLinus Torvalds  *	Arnaldo C. Melo		:	Convert proc stuff to seq_file
55bb1d23b0SEric Dumazet  *	Eric Dumazet		:	hashed spinlocks and rt_check_expire() fixes.
56cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Ignore TOS on PMTUD and Redirect
57cef2685eSIlia Sotnikov  * 	Ilia Sotnikov		:	Removed TOS from hash calculations
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
601da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
611da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
621da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds 
65afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt
66afd46503SJoe Perches 
671da177e4SLinus Torvalds #include <linux/module.h>
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <linux/bitops.h>
701da177e4SLinus Torvalds #include <linux/types.h>
711da177e4SLinus Torvalds #include <linux/kernel.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/inetdevice.h>
841da177e4SLinus Torvalds #include <linux/igmp.h>
851da177e4SLinus Torvalds #include <linux/pkt_sched.h>
861da177e4SLinus Torvalds #include <linux/mroute.h>
871da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
881da177e4SLinus Torvalds #include <linux/random.h>
891da177e4SLinus Torvalds #include <linux/rcupdate.h>
901da177e4SLinus Torvalds #include <linux/times.h>
915a0e3ad6STejun Heo #include <linux/slab.h>
92352e512cSHerbert Xu #include <net/dst.h>
93457c4cbcSEric W. Biederman #include <net/net_namespace.h>
941da177e4SLinus Torvalds #include <net/protocol.h>
951da177e4SLinus Torvalds #include <net/ip.h>
961da177e4SLinus Torvalds #include <net/route.h>
971da177e4SLinus Torvalds #include <net/inetpeer.h>
981da177e4SLinus Torvalds #include <net/sock.h>
991da177e4SLinus Torvalds #include <net/ip_fib.h>
1001da177e4SLinus Torvalds #include <net/arp.h>
1011da177e4SLinus Torvalds #include <net/tcp.h>
1021da177e4SLinus Torvalds #include <net/icmp.h>
1031da177e4SLinus Torvalds #include <net/xfrm.h>
1048d71740cSTom Tucker #include <net/netevent.h>
10563f3444fSThomas Graf #include <net/rtnetlink.h>
1061da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1071da177e4SLinus Torvalds #include <linux/sysctl.h>
1087426a564SShan Wei #include <linux/kmemleak.h>
1091da177e4SLinus Torvalds #endif
1106e5714eaSDavid S. Miller #include <net/secure_seq.h>
1111da177e4SLinus Torvalds 
11268a5e3ddSDavid S. Miller #define RT_FL_TOS(oldflp4) \
113f61759e6SJulian Anastasov 	((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define IP_MAX_MTU	0xFFF0
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static int ip_rt_max_size;
120817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly	= RT_GC_TIMEOUT;
1219f28a2fcSEric Dumazet static int ip_rt_gc_interval __read_mostly  = 60 * HZ;
122817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly	= HZ / 2;
123817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly	= 9;
124817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly	= HZ / 50;
125817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly	= ((HZ / 50) << (9 + 1));
126817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly	= HZ;
127817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly	= 5 * HZ;
128817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly	= 8;
129817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly	= 10 * 60 * HZ;
130817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly		= 512 + 20 + 20;
131817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly	= 256;
1329f28a2fcSEric Dumazet 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  *	Interface to generic destination cache.
1351da177e4SLinus Torvalds  */
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
1380dbaee3bSDavid S. Miller static unsigned int	 ipv4_default_advmss(const struct dst_entry *dst);
139ebb762f2SSteffen Klassert static unsigned int	 ipv4_mtu(const struct dst_entry *dst);
1401da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
1411da177e4SLinus Torvalds static void		 ipv4_link_failure(struct sk_buff *skb);
1426700c270SDavid S. Miller static void		 ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1436700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1446700c270SDavid S. Miller static void		 ip_do_redirect(struct dst_entry *dst, struct sock *sk,
1456700c270SDavid S. Miller 					struct sk_buff *skb);
146caacf05eSDavid S. Miller static void		ipv4_dst_destroy(struct dst_entry *dst);
1471da177e4SLinus Torvalds 
14872cdd1d9SEric Dumazet static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
14972cdd1d9SEric Dumazet 			    int how)
15072cdd1d9SEric Dumazet {
15172cdd1d9SEric Dumazet }
1521da177e4SLinus Torvalds 
15362fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
15462fa8a84SDavid S. Miller {
15531248731SDavid S. Miller 	WARN_ON(1);
15631248731SDavid S. Miller 	return NULL;
15762fa8a84SDavid S. Miller }
15862fa8a84SDavid S. Miller 
159f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
160f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
161f894cbf8SDavid S. Miller 					   const void *daddr);
162d3aaeb38SDavid S. Miller 
1631da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1641da177e4SLinus Torvalds 	.family =		AF_INET,
16509640e63SHarvey Harrison 	.protocol =		cpu_to_be16(ETH_P_IP),
1661da177e4SLinus Torvalds 	.check =		ipv4_dst_check,
1670dbaee3bSDavid S. Miller 	.default_advmss =	ipv4_default_advmss,
168ebb762f2SSteffen Klassert 	.mtu =			ipv4_mtu,
16962fa8a84SDavid S. Miller 	.cow_metrics =		ipv4_cow_metrics,
170caacf05eSDavid S. Miller 	.destroy =		ipv4_dst_destroy,
1711da177e4SLinus Torvalds 	.ifdown =		ipv4_dst_ifdown,
1721da177e4SLinus Torvalds 	.negative_advice =	ipv4_negative_advice,
1731da177e4SLinus Torvalds 	.link_failure =		ipv4_link_failure,
1741da177e4SLinus Torvalds 	.update_pmtu =		ip_rt_update_pmtu,
175e47a185bSDavid S. Miller 	.redirect =		ip_do_redirect,
1761ac06e03SHerbert Xu 	.local_out =		__ip_local_out,
177d3aaeb38SDavid S. Miller 	.neigh_lookup =		ipv4_neigh_lookup,
1781da177e4SLinus Torvalds };
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1811da177e4SLinus Torvalds 
1824839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1831da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1844a2b9c37SDan Siemon 	ECN_OR_COST(BESTEFFORT),
1851da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1861da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1871da177e4SLinus Torvalds 	TC_PRIO_BULK,
1881da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1891da177e4SLinus Torvalds 	TC_PRIO_BULK,
1901da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1911da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1921da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1931da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1941da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1951da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1961da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1971da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1981da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
1991da177e4SLinus Torvalds };
200d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio);
2011da177e4SLinus Torvalds 
2022f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
20327f39c73SEric Dumazet #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field)
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
2061da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
2071da177e4SLinus Torvalds {
20829e75252SEric Dumazet 	if (*pos)
20989aef892SDavid S. Miller 		return NULL;
21029e75252SEric Dumazet 	return SEQ_START_TOKEN;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	++*pos;
21689aef892SDavid S. Miller 	return NULL;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2261da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
2271da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
2281da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
2291da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
2301da177e4SLinus Torvalds 	return 0;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
233f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
2341da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
2351da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
2361da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
2371da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
2381da177e4SLinus Torvalds };
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file)
2411da177e4SLinus Torvalds {
24289aef892SDavid S. Miller 	return seq_open(file, &rt_cache_seq_ops);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2459a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = {
2461da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
2471da177e4SLinus Torvalds 	.open	 = rt_cache_seq_open,
2481da177e4SLinus Torvalds 	.read	 = seq_read,
2491da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
25089aef892SDavid S. Miller 	.release = seq_release,
2511da177e4SLinus Torvalds };
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	int cpu;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (*pos == 0)
2591da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
2601da177e4SLinus Torvalds 
2610f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2621da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2631da177e4SLinus Torvalds 			continue;
2641da177e4SLinus Torvalds 		*pos = cpu+1;
2652f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 	return NULL;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	int cpu;
2731da177e4SLinus Torvalds 
2740f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2751da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2761da177e4SLinus Torvalds 			continue;
2771da177e4SLinus Torvalds 		*pos = cpu+1;
2782f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 	return NULL;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2945bec0039SOlaf Rempel 		seq_printf(seq, "entries  in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src  out_hit out_slow_tot out_slow_mc  gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n");
2951da177e4SLinus Torvalds 		return 0;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	seq_printf(seq,"%08x  %08x %08x %08x %08x %08x %08x %08x "
2991da177e4SLinus Torvalds 		   " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
300fc66f95cSEric Dumazet 		   dst_entries_get_slow(&ipv4_dst_ops),
3011da177e4SLinus Torvalds 		   st->in_hit,
3021da177e4SLinus Torvalds 		   st->in_slow_tot,
3031da177e4SLinus Torvalds 		   st->in_slow_mc,
3041da177e4SLinus Torvalds 		   st->in_no_route,
3051da177e4SLinus Torvalds 		   st->in_brd,
3061da177e4SLinus Torvalds 		   st->in_martian_dst,
3071da177e4SLinus Torvalds 		   st->in_martian_src,
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 		   st->out_hit,
3101da177e4SLinus Torvalds 		   st->out_slow_tot,
3111da177e4SLinus Torvalds 		   st->out_slow_mc,
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 		   st->gc_total,
3141da177e4SLinus Torvalds 		   st->gc_ignored,
3151da177e4SLinus Torvalds 		   st->gc_goal_miss,
3161da177e4SLinus Torvalds 		   st->gc_dst_overflow,
3171da177e4SLinus Torvalds 		   st->in_hlist_search,
3181da177e4SLinus Torvalds 		   st->out_hlist_search
3191da177e4SLinus Torvalds 		);
3201da177e4SLinus Torvalds 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
3241da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
3251da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
3261da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
3271da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
3281da177e4SLinus Torvalds };
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	return seq_open(file, &rt_cpu_seq_ops);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3369a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = {
3371da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
3381da177e4SLinus Torvalds 	.open	 = rt_cpu_seq_open,
3391da177e4SLinus Torvalds 	.read	 = seq_read,
3401da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
3411da177e4SLinus Torvalds 	.release = seq_release,
3421da177e4SLinus Torvalds };
3431da177e4SLinus Torvalds 
344c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
345a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v)
34678c686e9SPavel Emelyanov {
347a661c419SAlexey Dobriyan 	struct ip_rt_acct *dst, *src;
348a661c419SAlexey Dobriyan 	unsigned int i, j;
34978c686e9SPavel Emelyanov 
350a661c419SAlexey Dobriyan 	dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
351a661c419SAlexey Dobriyan 	if (!dst)
352a661c419SAlexey Dobriyan 		return -ENOMEM;
35378c686e9SPavel Emelyanov 
354a661c419SAlexey Dobriyan 	for_each_possible_cpu(i) {
355a661c419SAlexey Dobriyan 		src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
356a661c419SAlexey Dobriyan 		for (j = 0; j < 256; j++) {
357a661c419SAlexey Dobriyan 			dst[j].o_bytes   += src[j].o_bytes;
358a661c419SAlexey Dobriyan 			dst[j].o_packets += src[j].o_packets;
359a661c419SAlexey Dobriyan 			dst[j].i_bytes   += src[j].i_bytes;
360a661c419SAlexey Dobriyan 			dst[j].i_packets += src[j].i_packets;
361a661c419SAlexey Dobriyan 		}
362a661c419SAlexey Dobriyan 	}
363a661c419SAlexey Dobriyan 
364a661c419SAlexey Dobriyan 	seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
365a661c419SAlexey Dobriyan 	kfree(dst);
36678c686e9SPavel Emelyanov 	return 0;
36778c686e9SPavel Emelyanov }
36878c686e9SPavel Emelyanov 
369a661c419SAlexey Dobriyan static int rt_acct_proc_open(struct inode *inode, struct file *file)
370a661c419SAlexey Dobriyan {
371a661c419SAlexey Dobriyan 	return single_open(file, rt_acct_proc_show, NULL);
37278c686e9SPavel Emelyanov }
37378c686e9SPavel Emelyanov 
374a661c419SAlexey Dobriyan static const struct file_operations rt_acct_proc_fops = {
375a661c419SAlexey Dobriyan 	.owner		= THIS_MODULE,
376a661c419SAlexey Dobriyan 	.open		= rt_acct_proc_open,
377a661c419SAlexey Dobriyan 	.read		= seq_read,
378a661c419SAlexey Dobriyan 	.llseek		= seq_lseek,
379a661c419SAlexey Dobriyan 	.release	= single_release,
380a661c419SAlexey Dobriyan };
38178c686e9SPavel Emelyanov #endif
382107f1634SPavel Emelyanov 
38373b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
384107f1634SPavel Emelyanov {
385107f1634SPavel Emelyanov 	struct proc_dir_entry *pde;
386107f1634SPavel Emelyanov 
387107f1634SPavel Emelyanov 	pde = proc_net_fops_create(net, "rt_cache", S_IRUGO,
388107f1634SPavel Emelyanov 			&rt_cache_seq_fops);
389107f1634SPavel Emelyanov 	if (!pde)
390107f1634SPavel Emelyanov 		goto err1;
391107f1634SPavel Emelyanov 
39277020720SWang Chen 	pde = proc_create("rt_cache", S_IRUGO,
39377020720SWang Chen 			  net->proc_net_stat, &rt_cpu_seq_fops);
394107f1634SPavel Emelyanov 	if (!pde)
395107f1634SPavel Emelyanov 		goto err2;
396107f1634SPavel Emelyanov 
397c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
398a661c419SAlexey Dobriyan 	pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops);
399107f1634SPavel Emelyanov 	if (!pde)
400107f1634SPavel Emelyanov 		goto err3;
401107f1634SPavel Emelyanov #endif
402107f1634SPavel Emelyanov 	return 0;
403107f1634SPavel Emelyanov 
404c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
405107f1634SPavel Emelyanov err3:
406107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net_stat);
407107f1634SPavel Emelyanov #endif
408107f1634SPavel Emelyanov err2:
409107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net);
410107f1634SPavel Emelyanov err1:
411107f1634SPavel Emelyanov 	return -ENOMEM;
412107f1634SPavel Emelyanov }
41373b38711SDenis V. Lunev 
41473b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
41573b38711SDenis V. Lunev {
41673b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net_stat);
41773b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net);
418c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
41973b38711SDenis V. Lunev 	remove_proc_entry("rt_acct", net->proc_net);
4200a931acfSAlexey Dobriyan #endif
42173b38711SDenis V. Lunev }
42273b38711SDenis V. Lunev 
42373b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
42473b38711SDenis V. Lunev 	.init = ip_rt_do_proc_init,
42573b38711SDenis V. Lunev 	.exit = ip_rt_do_proc_exit,
42673b38711SDenis V. Lunev };
42773b38711SDenis V. Lunev 
42873b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
42973b38711SDenis V. Lunev {
43073b38711SDenis V. Lunev 	return register_pernet_subsys(&ip_rt_proc_ops);
43173b38711SDenis V. Lunev }
43273b38711SDenis V. Lunev 
433107f1634SPavel Emelyanov #else
43473b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
435107f1634SPavel Emelyanov {
436107f1634SPavel Emelyanov 	return 0;
437107f1634SPavel Emelyanov }
4381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
4391da177e4SLinus Torvalds 
4404331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth)
441e84f84f2SDenis V. Lunev {
442d8d1f30bSChangli Gao 	return rth->rt_genid != rt_genid(dev_net(rth->dst.dev));
443e84f84f2SDenis V. Lunev }
444e84f84f2SDenis V. Lunev 
445bafa6d9dSNicolas Dichtel void rt_cache_flush(struct net *net)
44629e75252SEric Dumazet {
447*b42664f8SNicolas Dichtel 	rt_genid_bump(net);
44898376387SEric Dumazet }
44998376387SEric Dumazet 
450f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
451f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
452f894cbf8SDavid S. Miller 					   const void *daddr)
4533769cffbSDavid Miller {
454d3aaeb38SDavid S. Miller 	struct net_device *dev = dst->dev;
455d3aaeb38SDavid S. Miller 	const __be32 *pkey = daddr;
45639232973SDavid S. Miller 	const struct rtable *rt;
4573769cffbSDavid Miller 	struct neighbour *n;
4583769cffbSDavid Miller 
45939232973SDavid S. Miller 	rt = (const struct rtable *) dst;
460a263b309SDavid S. Miller 	if (rt->rt_gateway)
46139232973SDavid S. Miller 		pkey = (const __be32 *) &rt->rt_gateway;
462f894cbf8SDavid S. Miller 	else if (skb)
463f894cbf8SDavid S. Miller 		pkey = &ip_hdr(skb)->daddr;
464d3aaeb38SDavid S. Miller 
46580703d26SDavid S. Miller 	n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey);
466d3aaeb38SDavid S. Miller 	if (n)
467d3aaeb38SDavid S. Miller 		return n;
46832092ecfSDavid Miller 	return neigh_create(&arp_tbl, pkey, dev);
469d3aaeb38SDavid S. Miller }
470d3aaeb38SDavid S. Miller 
4711da177e4SLinus Torvalds /*
4721da177e4SLinus Torvalds  * Peer allocation may fail only in serious out-of-memory conditions.  However
4731da177e4SLinus Torvalds  * we still can generate some output.
4741da177e4SLinus Torvalds  * Random ID selection looks a bit dangerous because we have no chances to
4751da177e4SLinus Torvalds  * select ID being unique in a reasonable period of time.
4761da177e4SLinus Torvalds  * But broken packet identifier may be better than no packet at all.
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(ip_fb_id_lock);
4811da177e4SLinus Torvalds 	static u32 ip_fallback_id;
4821da177e4SLinus Torvalds 	u32 salt;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	spin_lock_bh(&ip_fb_id_lock);
485e448515cSAl Viro 	salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
4861da177e4SLinus Torvalds 	iph->id = htons(salt & 0xFFFF);
4871da177e4SLinus Torvalds 	ip_fallback_id = salt;
4881da177e4SLinus Torvalds 	spin_unlock_bh(&ip_fb_id_lock);
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
4921da177e4SLinus Torvalds {
4931d861aa4SDavid S. Miller 	struct net *net = dev_net(dst->dev);
4941d861aa4SDavid S. Miller 	struct inet_peer *peer;
4951da177e4SLinus Torvalds 
4961d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, iph->daddr, 1);
497fbfe95a4SDavid S. Miller 	if (peer) {
498fbfe95a4SDavid S. Miller 		iph->id = htons(inet_getid(peer, more));
4991d861aa4SDavid S. Miller 		inet_putpeer(peer);
5001da177e4SLinus Torvalds 		return;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	ip_select_fb_ident(iph);
5041da177e4SLinus Torvalds }
5054bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident);
5061da177e4SLinus Torvalds 
5075abf7f7eSEric Dumazet static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5084895c771SDavid S. Miller 			     const struct iphdr *iph,
5094895c771SDavid S. Miller 			     int oif, u8 tos,
5104895c771SDavid S. Miller 			     u8 prot, u32 mark, int flow_flags)
5114895c771SDavid S. Miller {
5124895c771SDavid S. Miller 	if (sk) {
5134895c771SDavid S. Miller 		const struct inet_sock *inet = inet_sk(sk);
5144895c771SDavid S. Miller 
5154895c771SDavid S. Miller 		oif = sk->sk_bound_dev_if;
5164895c771SDavid S. Miller 		mark = sk->sk_mark;
5174895c771SDavid S. Miller 		tos = RT_CONN_FLAGS(sk);
5184895c771SDavid S. Miller 		prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol;
5194895c771SDavid S. Miller 	}
5204895c771SDavid S. Miller 	flowi4_init_output(fl4, oif, mark, tos,
5214895c771SDavid S. Miller 			   RT_SCOPE_UNIVERSE, prot,
5224895c771SDavid S. Miller 			   flow_flags,
5234895c771SDavid S. Miller 			   iph->daddr, iph->saddr, 0, 0);
5244895c771SDavid S. Miller }
5254895c771SDavid S. Miller 
5265abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
5275abf7f7eSEric Dumazet 			       const struct sock *sk)
5284895c771SDavid S. Miller {
5294895c771SDavid S. Miller 	const struct iphdr *iph = ip_hdr(skb);
5304895c771SDavid S. Miller 	int oif = skb->dev->ifindex;
5314895c771SDavid S. Miller 	u8 tos = RT_TOS(iph->tos);
5324895c771SDavid S. Miller 	u8 prot = iph->protocol;
5334895c771SDavid S. Miller 	u32 mark = skb->mark;
5344895c771SDavid S. Miller 
5354895c771SDavid S. Miller 	__build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0);
5364895c771SDavid S. Miller }
5374895c771SDavid S. Miller 
5385abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
5394895c771SDavid S. Miller {
5404895c771SDavid S. Miller 	const struct inet_sock *inet = inet_sk(sk);
5415abf7f7eSEric Dumazet 	const struct ip_options_rcu *inet_opt;
5424895c771SDavid S. Miller 	__be32 daddr = inet->inet_daddr;
5434895c771SDavid S. Miller 
5444895c771SDavid S. Miller 	rcu_read_lock();
5454895c771SDavid S. Miller 	inet_opt = rcu_dereference(inet->inet_opt);
5464895c771SDavid S. Miller 	if (inet_opt && inet_opt->opt.srr)
5474895c771SDavid S. Miller 		daddr = inet_opt->opt.faddr;
5484895c771SDavid S. Miller 	flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
5494895c771SDavid S. Miller 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
5504895c771SDavid S. Miller 			   inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
5514895c771SDavid S. Miller 			   inet_sk_flowi_flags(sk),
5524895c771SDavid S. Miller 			   daddr, inet->inet_saddr, 0, 0);
5534895c771SDavid S. Miller 	rcu_read_unlock();
5544895c771SDavid S. Miller }
5554895c771SDavid S. Miller 
5565abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5575abf7f7eSEric Dumazet 				 const struct sk_buff *skb)
5584895c771SDavid S. Miller {
5594895c771SDavid S. Miller 	if (skb)
5604895c771SDavid S. Miller 		build_skb_flow_key(fl4, skb, sk);
5614895c771SDavid S. Miller 	else
5624895c771SDavid S. Miller 		build_sk_flow_key(fl4, sk);
5634895c771SDavid S. Miller }
5644895c771SDavid S. Miller 
565c5038a83SDavid S. Miller static inline void rt_free(struct rtable *rt)
566c5038a83SDavid S. Miller {
567c5038a83SDavid S. Miller 	call_rcu(&rt->dst.rcu_head, dst_rcu_free);
568c5038a83SDavid S. Miller }
569c5038a83SDavid S. Miller 
570c5038a83SDavid S. Miller static DEFINE_SPINLOCK(fnhe_lock);
5714895c771SDavid S. Miller 
572aee06da6SJulian Anastasov static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
5734895c771SDavid S. Miller {
5744895c771SDavid S. Miller 	struct fib_nh_exception *fnhe, *oldest;
575c5038a83SDavid S. Miller 	struct rtable *orig;
5764895c771SDavid S. Miller 
5774895c771SDavid S. Miller 	oldest = rcu_dereference(hash->chain);
5784895c771SDavid S. Miller 	for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe;
5794895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
5804895c771SDavid S. Miller 		if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp))
5814895c771SDavid S. Miller 			oldest = fnhe;
5824895c771SDavid S. Miller 	}
583c5038a83SDavid S. Miller 	orig = rcu_dereference(oldest->fnhe_rth);
584c5038a83SDavid S. Miller 	if (orig) {
585c5038a83SDavid S. Miller 		RCU_INIT_POINTER(oldest->fnhe_rth, NULL);
586c5038a83SDavid S. Miller 		rt_free(orig);
587c5038a83SDavid S. Miller 	}
5884895c771SDavid S. Miller 	return oldest;
5894895c771SDavid S. Miller }
5904895c771SDavid S. Miller 
591d3a25c98SDavid S. Miller static inline u32 fnhe_hashfun(__be32 daddr)
592d3a25c98SDavid S. Miller {
593d3a25c98SDavid S. Miller 	u32 hval;
594d3a25c98SDavid S. Miller 
595d3a25c98SDavid S. Miller 	hval = (__force u32) daddr;
596d3a25c98SDavid S. Miller 	hval ^= (hval >> 11) ^ (hval >> 22);
597d3a25c98SDavid S. Miller 
598d3a25c98SDavid S. Miller 	return hval & (FNHE_HASH_SIZE - 1);
599d3a25c98SDavid S. Miller }
600d3a25c98SDavid S. Miller 
601aee06da6SJulian Anastasov static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw,
602aee06da6SJulian Anastasov 				  u32 pmtu, unsigned long expires)
6034895c771SDavid S. Miller {
604aee06da6SJulian Anastasov 	struct fnhe_hash_bucket *hash;
6054895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
6064895c771SDavid S. Miller 	int depth;
607aee06da6SJulian Anastasov 	u32 hval = fnhe_hashfun(daddr);
6084895c771SDavid S. Miller 
609c5038a83SDavid S. Miller 	spin_lock_bh(&fnhe_lock);
610aee06da6SJulian Anastasov 
611aee06da6SJulian Anastasov 	hash = nh->nh_exceptions;
6124895c771SDavid S. Miller 	if (!hash) {
613aee06da6SJulian Anastasov 		hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC);
6144895c771SDavid S. Miller 		if (!hash)
615aee06da6SJulian Anastasov 			goto out_unlock;
616aee06da6SJulian Anastasov 		nh->nh_exceptions = hash;
6174895c771SDavid S. Miller 	}
6184895c771SDavid S. Miller 
6194895c771SDavid S. Miller 	hash += hval;
6204895c771SDavid S. Miller 
6214895c771SDavid S. Miller 	depth = 0;
6224895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash->chain); fnhe;
6234895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
6244895c771SDavid S. Miller 		if (fnhe->fnhe_daddr == daddr)
625aee06da6SJulian Anastasov 			break;
6264895c771SDavid S. Miller 		depth++;
6274895c771SDavid S. Miller 	}
6284895c771SDavid S. Miller 
629aee06da6SJulian Anastasov 	if (fnhe) {
630aee06da6SJulian Anastasov 		if (gw)
631aee06da6SJulian Anastasov 			fnhe->fnhe_gw = gw;
632aee06da6SJulian Anastasov 		if (pmtu) {
633aee06da6SJulian Anastasov 			fnhe->fnhe_pmtu = pmtu;
634aee06da6SJulian Anastasov 			fnhe->fnhe_expires = expires;
6354895c771SDavid S. Miller 		}
636aee06da6SJulian Anastasov 	} else {
637aee06da6SJulian Anastasov 		if (depth > FNHE_RECLAIM_DEPTH)
638aee06da6SJulian Anastasov 			fnhe = fnhe_oldest(hash);
639aee06da6SJulian Anastasov 		else {
6404895c771SDavid S. Miller 			fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
6414895c771SDavid S. Miller 			if (!fnhe)
642aee06da6SJulian Anastasov 				goto out_unlock;
6434895c771SDavid S. Miller 
6444895c771SDavid S. Miller 			fnhe->fnhe_next = hash->chain;
6454895c771SDavid S. Miller 			rcu_assign_pointer(hash->chain, fnhe);
646aee06da6SJulian Anastasov 		}
6474895c771SDavid S. Miller 		fnhe->fnhe_daddr = daddr;
648aee06da6SJulian Anastasov 		fnhe->fnhe_gw = gw;
649aee06da6SJulian Anastasov 		fnhe->fnhe_pmtu = pmtu;
650aee06da6SJulian Anastasov 		fnhe->fnhe_expires = expires;
651aee06da6SJulian Anastasov 	}
652aee06da6SJulian Anastasov 
6534895c771SDavid S. Miller 	fnhe->fnhe_stamp = jiffies;
654aee06da6SJulian Anastasov 
655aee06da6SJulian Anastasov out_unlock:
656c5038a83SDavid S. Miller 	spin_unlock_bh(&fnhe_lock);
657aee06da6SJulian Anastasov 	return;
6584895c771SDavid S. Miller }
6594895c771SDavid S. Miller 
660ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
661ceb33206SDavid S. Miller 			     bool kill_route)
6621da177e4SLinus Torvalds {
663e47a185bSDavid S. Miller 	__be32 new_gw = icmp_hdr(skb)->un.gateway;
66494206125SDavid S. Miller 	__be32 old_gw = ip_hdr(skb)->saddr;
665e47a185bSDavid S. Miller 	struct net_device *dev = skb->dev;
666e47a185bSDavid S. Miller 	struct in_device *in_dev;
6674895c771SDavid S. Miller 	struct fib_result res;
668e47a185bSDavid S. Miller 	struct neighbour *n;
669317805b8SDenis V. Lunev 	struct net *net;
6701da177e4SLinus Torvalds 
67194206125SDavid S. Miller 	switch (icmp_hdr(skb)->code & 7) {
67294206125SDavid S. Miller 	case ICMP_REDIR_NET:
67394206125SDavid S. Miller 	case ICMP_REDIR_NETTOS:
67494206125SDavid S. Miller 	case ICMP_REDIR_HOST:
67594206125SDavid S. Miller 	case ICMP_REDIR_HOSTTOS:
67694206125SDavid S. Miller 		break;
67794206125SDavid S. Miller 
67894206125SDavid S. Miller 	default:
67994206125SDavid S. Miller 		return;
68094206125SDavid S. Miller 	}
68194206125SDavid S. Miller 
682e47a185bSDavid S. Miller 	if (rt->rt_gateway != old_gw)
683e47a185bSDavid S. Miller 		return;
684e47a185bSDavid S. Miller 
685e47a185bSDavid S. Miller 	in_dev = __in_dev_get_rcu(dev);
686e47a185bSDavid S. Miller 	if (!in_dev)
687e47a185bSDavid S. Miller 		return;
688e47a185bSDavid S. Miller 
689c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
6909d4fb27dSJoe Perches 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
6919d4fb27dSJoe Perches 	    ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
6929d4fb27dSJoe Perches 	    ipv4_is_zeronet(new_gw))
6931da177e4SLinus Torvalds 		goto reject_redirect;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
6961da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
6971da177e4SLinus Torvalds 			goto reject_redirect;
6981da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
6991da177e4SLinus Torvalds 			goto reject_redirect;
7001da177e4SLinus Torvalds 	} else {
701317805b8SDenis V. Lunev 		if (inet_addr_type(net, new_gw) != RTN_UNICAST)
7021da177e4SLinus Torvalds 			goto reject_redirect;
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7054895c771SDavid S. Miller 	n = ipv4_neigh_lookup(&rt->dst, NULL, &new_gw);
706e47a185bSDavid S. Miller 	if (n) {
707e47a185bSDavid S. Miller 		if (!(n->nud_state & NUD_VALID)) {
708e47a185bSDavid S. Miller 			neigh_event_send(n, NULL);
709e47a185bSDavid S. Miller 		} else {
7104895c771SDavid S. Miller 			if (fib_lookup(net, fl4, &res) == 0) {
7114895c771SDavid S. Miller 				struct fib_nh *nh = &FIB_RES_NH(res);
7124895c771SDavid S. Miller 
713aee06da6SJulian Anastasov 				update_or_create_fnhe(nh, fl4->daddr, new_gw,
714aee06da6SJulian Anastasov 						      0, 0);
7154895c771SDavid S. Miller 			}
716ceb33206SDavid S. Miller 			if (kill_route)
717ceb33206SDavid S. Miller 				rt->dst.obsolete = DST_OBSOLETE_KILL;
718e47a185bSDavid S. Miller 			call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
719e47a185bSDavid S. Miller 		}
720e47a185bSDavid S. Miller 		neigh_release(n);
721e47a185bSDavid S. Miller 	}
722e47a185bSDavid S. Miller 	return;
723e47a185bSDavid S. Miller 
724e47a185bSDavid S. Miller reject_redirect:
725e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE
72699ee038dSDavid S. Miller 	if (IN_DEV_LOG_MARTIANS(in_dev)) {
72799ee038dSDavid S. Miller 		const struct iphdr *iph = (const struct iphdr *) skb->data;
72899ee038dSDavid S. Miller 		__be32 daddr = iph->daddr;
72999ee038dSDavid S. Miller 		__be32 saddr = iph->saddr;
73099ee038dSDavid S. Miller 
731e47a185bSDavid S. Miller 		net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
732e47a185bSDavid S. Miller 				     "  Advised path = %pI4 -> %pI4\n",
733e47a185bSDavid S. Miller 				     &old_gw, dev->name, &new_gw,
734e47a185bSDavid S. Miller 				     &saddr, &daddr);
73599ee038dSDavid S. Miller 	}
736e47a185bSDavid S. Miller #endif
737e47a185bSDavid S. Miller 	;
738e47a185bSDavid S. Miller }
739e47a185bSDavid S. Miller 
7404895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
7414895c771SDavid S. Miller {
7424895c771SDavid S. Miller 	struct rtable *rt;
7434895c771SDavid S. Miller 	struct flowi4 fl4;
7444895c771SDavid S. Miller 
7454895c771SDavid S. Miller 	rt = (struct rtable *) dst;
7464895c771SDavid S. Miller 
7474895c771SDavid S. Miller 	ip_rt_build_flow_key(&fl4, sk, skb);
748ceb33206SDavid S. Miller 	__ip_do_redirect(rt, skb, &fl4, true);
7494895c771SDavid S. Miller }
7504895c771SDavid S. Miller 
7511da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)dst;
7541da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	if (rt) {
757d11a4dc1STimo Teräs 		if (dst->obsolete > 0) {
7581da177e4SLinus Torvalds 			ip_rt_put(rt);
7591da177e4SLinus Torvalds 			ret = NULL;
7605943634fSDavid S. Miller 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
7615943634fSDavid S. Miller 			   rt->dst.expires) {
76289aef892SDavid S. Miller 			ip_rt_put(rt);
7631da177e4SLinus Torvalds 			ret = NULL;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 	return ret;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds /*
7701da177e4SLinus Torvalds  * Algorithm:
7711da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
7721da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
7731da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
7741da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
7751da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
7761da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
7771da177e4SLinus Torvalds  *
7781da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
7791da177e4SLinus Torvalds  * in icmp.c.
7801da177e4SLinus Torvalds  *
7811da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
7821da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
7831da177e4SLinus Torvalds  */
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
7861da177e4SLinus Torvalds {
787511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
78830038fc6SEric Dumazet 	struct in_device *in_dev;
78992d86829SDavid S. Miller 	struct inet_peer *peer;
7901d861aa4SDavid S. Miller 	struct net *net;
79130038fc6SEric Dumazet 	int log_martians;
7921da177e4SLinus Torvalds 
79330038fc6SEric Dumazet 	rcu_read_lock();
794d8d1f30bSChangli Gao 	in_dev = __in_dev_get_rcu(rt->dst.dev);
79530038fc6SEric Dumazet 	if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
79630038fc6SEric Dumazet 		rcu_read_unlock();
7971da177e4SLinus Torvalds 		return;
79830038fc6SEric Dumazet 	}
79930038fc6SEric Dumazet 	log_martians = IN_DEV_LOG_MARTIANS(in_dev);
80030038fc6SEric Dumazet 	rcu_read_unlock();
8011da177e4SLinus Torvalds 
8021d861aa4SDavid S. Miller 	net = dev_net(rt->dst.dev);
8031d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
80492d86829SDavid S. Miller 	if (!peer) {
80592d86829SDavid S. Miller 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
80692d86829SDavid S. Miller 		return;
80792d86829SDavid S. Miller 	}
80892d86829SDavid S. Miller 
8091da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
8101da177e4SLinus Torvalds 	 * reset the algorithm.
8111da177e4SLinus Torvalds 	 */
81292d86829SDavid S. Miller 	if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence))
81392d86829SDavid S. Miller 		peer->rate_tokens = 0;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
816d8d1f30bSChangli Gao 	 * set dst.rate_last to the last seen redirected packet.
8171da177e4SLinus Torvalds 	 */
81892d86829SDavid S. Miller 	if (peer->rate_tokens >= ip_rt_redirect_number) {
81992d86829SDavid S. Miller 		peer->rate_last = jiffies;
8201d861aa4SDavid S. Miller 		goto out_put_peer;
8211da177e4SLinus Torvalds 	}
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
8241da177e4SLinus Torvalds 	 * redirect.
8251da177e4SLinus Torvalds 	 */
82692d86829SDavid S. Miller 	if (peer->rate_tokens == 0 ||
82714fb8a76SLi Yewang 	    time_after(jiffies,
82892d86829SDavid S. Miller 		       (peer->rate_last +
82992d86829SDavid S. Miller 			(ip_rt_redirect_load << peer->rate_tokens)))) {
8301da177e4SLinus Torvalds 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
83192d86829SDavid S. Miller 		peer->rate_last = jiffies;
83292d86829SDavid S. Miller 		++peer->rate_tokens;
8331da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
83430038fc6SEric Dumazet 		if (log_martians &&
835e87cc472SJoe Perches 		    peer->rate_tokens == ip_rt_redirect_number)
836e87cc472SJoe Perches 			net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
83792101b3bSDavid S. Miller 					     &ip_hdr(skb)->saddr, inet_iif(skb),
838f1ce3062SDavid S. Miller 					     &ip_hdr(skb)->daddr, &rt->rt_gateway);
8391da177e4SLinus Torvalds #endif
8401da177e4SLinus Torvalds 	}
8411d861aa4SDavid S. Miller out_put_peer:
8421d861aa4SDavid S. Miller 	inet_putpeer(peer);
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
8461da177e4SLinus Torvalds {
847251da413SDavid S. Miller 	struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
848511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
84992d86829SDavid S. Miller 	struct inet_peer *peer;
8501da177e4SLinus Torvalds 	unsigned long now;
851251da413SDavid S. Miller 	struct net *net;
85292d86829SDavid S. Miller 	bool send;
8531da177e4SLinus Torvalds 	int code;
8541da177e4SLinus Torvalds 
855251da413SDavid S. Miller 	net = dev_net(rt->dst.dev);
856251da413SDavid S. Miller 	if (!IN_DEV_FORWARD(in_dev)) {
857251da413SDavid S. Miller 		switch (rt->dst.error) {
858251da413SDavid S. Miller 		case EHOSTUNREACH:
859251da413SDavid S. Miller 			IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS);
860251da413SDavid S. Miller 			break;
861251da413SDavid S. Miller 
862251da413SDavid S. Miller 		case ENETUNREACH:
863251da413SDavid S. Miller 			IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
864251da413SDavid S. Miller 			break;
865251da413SDavid S. Miller 		}
866251da413SDavid S. Miller 		goto out;
867251da413SDavid S. Miller 	}
868251da413SDavid S. Miller 
869d8d1f30bSChangli Gao 	switch (rt->dst.error) {
8701da177e4SLinus Torvalds 	case EINVAL:
8711da177e4SLinus Torvalds 	default:
8721da177e4SLinus Torvalds 		goto out;
8731da177e4SLinus Torvalds 	case EHOSTUNREACH:
8741da177e4SLinus Torvalds 		code = ICMP_HOST_UNREACH;
8751da177e4SLinus Torvalds 		break;
8761da177e4SLinus Torvalds 	case ENETUNREACH:
8771da177e4SLinus Torvalds 		code = ICMP_NET_UNREACH;
878251da413SDavid S. Miller 		IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
8791da177e4SLinus Torvalds 		break;
8801da177e4SLinus Torvalds 	case EACCES:
8811da177e4SLinus Torvalds 		code = ICMP_PKT_FILTERED;
8821da177e4SLinus Torvalds 		break;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8851d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
88692d86829SDavid S. Miller 
88792d86829SDavid S. Miller 	send = true;
88892d86829SDavid S. Miller 	if (peer) {
8891da177e4SLinus Torvalds 		now = jiffies;
89092d86829SDavid S. Miller 		peer->rate_tokens += now - peer->rate_last;
89192d86829SDavid S. Miller 		if (peer->rate_tokens > ip_rt_error_burst)
89292d86829SDavid S. Miller 			peer->rate_tokens = ip_rt_error_burst;
89392d86829SDavid S. Miller 		peer->rate_last = now;
89492d86829SDavid S. Miller 		if (peer->rate_tokens >= ip_rt_error_cost)
89592d86829SDavid S. Miller 			peer->rate_tokens -= ip_rt_error_cost;
89692d86829SDavid S. Miller 		else
89792d86829SDavid S. Miller 			send = false;
8981d861aa4SDavid S. Miller 		inet_putpeer(peer);
8991da177e4SLinus Torvalds 	}
90092d86829SDavid S. Miller 	if (send)
90192d86829SDavid S. Miller 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds out:	kfree_skb(skb);
9041da177e4SLinus Torvalds 	return 0;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
907ceb33206SDavid S. Miller static u32 __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
9081da177e4SLinus Torvalds {
9094895c771SDavid S. Miller 	struct fib_result res;
9102c8cec5cSDavid S. Miller 
9112c8cec5cSDavid S. Miller 	if (mtu < ip_rt_min_pmtu)
9121da177e4SLinus Torvalds 		mtu = ip_rt_min_pmtu;
91346af3180SHiroaki SHIMODA 
914c5ae7d41SEric Dumazet 	rcu_read_lock();
9154895c771SDavid S. Miller 	if (fib_lookup(dev_net(rt->dst.dev), fl4, &res) == 0) {
9164895c771SDavid S. Miller 		struct fib_nh *nh = &FIB_RES_NH(res);
9174895c771SDavid S. Miller 
918aee06da6SJulian Anastasov 		update_or_create_fnhe(nh, fl4->daddr, 0, mtu,
919aee06da6SJulian Anastasov 				      jiffies + ip_rt_mtu_expires);
9204895c771SDavid S. Miller 	}
921c5ae7d41SEric Dumazet 	rcu_read_unlock();
922ceb33206SDavid S. Miller 	return mtu;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9254895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
9264895c771SDavid S. Miller 			      struct sk_buff *skb, u32 mtu)
9274895c771SDavid S. Miller {
9284895c771SDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
9294895c771SDavid S. Miller 	struct flowi4 fl4;
9304895c771SDavid S. Miller 
9314895c771SDavid S. Miller 	ip_rt_build_flow_key(&fl4, sk, skb);
932ceb33206SDavid S. Miller 	mtu = __ip_rt_update_pmtu(rt, &fl4, mtu);
933ceb33206SDavid S. Miller 
934ceb33206SDavid S. Miller 	if (!rt->rt_pmtu) {
935ceb33206SDavid S. Miller 		dst->obsolete = DST_OBSOLETE_KILL;
936ceb33206SDavid S. Miller 	} else {
937ceb33206SDavid S. Miller 		rt->rt_pmtu = mtu;
9389b04f350SEric Dumazet 		rt->dst.expires = max(1UL, jiffies + ip_rt_mtu_expires);
939ceb33206SDavid S. Miller 	}
9404895c771SDavid S. Miller }
9414895c771SDavid S. Miller 
94236393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
94336393395SDavid S. Miller 		      int oif, u32 mark, u8 protocol, int flow_flags)
94436393395SDavid S. Miller {
94536393395SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
94636393395SDavid S. Miller 	struct flowi4 fl4;
94736393395SDavid S. Miller 	struct rtable *rt;
94836393395SDavid S. Miller 
9494895c771SDavid S. Miller 	__build_flow_key(&fl4, NULL, iph, oif,
9504895c771SDavid S. Miller 			 RT_TOS(iph->tos), protocol, mark, flow_flags);
95136393395SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
95236393395SDavid S. Miller 	if (!IS_ERR(rt)) {
9534895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
95436393395SDavid S. Miller 		ip_rt_put(rt);
95536393395SDavid S. Miller 	}
95636393395SDavid S. Miller }
95736393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
95836393395SDavid S. Miller 
95936393395SDavid S. Miller void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
96036393395SDavid S. Miller {
9614895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
9624895c771SDavid S. Miller 	struct flowi4 fl4;
9634895c771SDavid S. Miller 	struct rtable *rt;
96436393395SDavid S. Miller 
9654895c771SDavid S. Miller 	__build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
9664895c771SDavid S. Miller 	rt = __ip_route_output_key(sock_net(sk), &fl4);
9674895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
9684895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
9694895c771SDavid S. Miller 		ip_rt_put(rt);
9704895c771SDavid S. Miller 	}
97136393395SDavid S. Miller }
97236393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
973f39925dbSDavid S. Miller 
974b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net,
975b42597e2SDavid S. Miller 		   int oif, u32 mark, u8 protocol, int flow_flags)
976b42597e2SDavid S. Miller {
977b42597e2SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
978b42597e2SDavid S. Miller 	struct flowi4 fl4;
979b42597e2SDavid S. Miller 	struct rtable *rt;
980b42597e2SDavid S. Miller 
9814895c771SDavid S. Miller 	__build_flow_key(&fl4, NULL, iph, oif,
9824895c771SDavid S. Miller 			 RT_TOS(iph->tos), protocol, mark, flow_flags);
983b42597e2SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
984b42597e2SDavid S. Miller 	if (!IS_ERR(rt)) {
985ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
986b42597e2SDavid S. Miller 		ip_rt_put(rt);
987b42597e2SDavid S. Miller 	}
988b42597e2SDavid S. Miller }
989b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect);
990b42597e2SDavid S. Miller 
991b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
992b42597e2SDavid S. Miller {
9934895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
9944895c771SDavid S. Miller 	struct flowi4 fl4;
9954895c771SDavid S. Miller 	struct rtable *rt;
996b42597e2SDavid S. Miller 
9974895c771SDavid S. Miller 	__build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
9984895c771SDavid S. Miller 	rt = __ip_route_output_key(sock_net(sk), &fl4);
9994895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
1000ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
10014895c771SDavid S. Miller 		ip_rt_put(rt);
10024895c771SDavid S. Miller 	}
1003b42597e2SDavid S. Miller }
1004b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
1005b42597e2SDavid S. Miller 
1006efbc368dSDavid S. Miller static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
1007efbc368dSDavid S. Miller {
1008efbc368dSDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
1009efbc368dSDavid S. Miller 
1010ceb33206SDavid S. Miller 	/* All IPV4 dsts are created with ->obsolete set to the value
1011ceb33206SDavid S. Miller 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1012ceb33206SDavid S. Miller 	 * into this function always.
1013ceb33206SDavid S. Miller 	 *
1014ceb33206SDavid S. Miller 	 * When a PMTU/redirect information update invalidates a
1015ceb33206SDavid S. Miller 	 * route, this is indicated by setting obsolete to
1016ceb33206SDavid S. Miller 	 * DST_OBSOLETE_KILL.
1017ceb33206SDavid S. Miller 	 */
1018ceb33206SDavid S. Miller 	if (dst->obsolete == DST_OBSOLETE_KILL || rt_is_expired(rt))
1019efbc368dSDavid S. Miller 		return NULL;
1020d11a4dc1STimo Teräs 	return dst;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	struct rtable *rt;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
10281da177e4SLinus Torvalds 
1029511c3f92SEric Dumazet 	rt = skb_rtable(skb);
10305943634fSDavid S. Miller 	if (rt)
10315943634fSDavid S. Miller 		dst_set_expires(&rt->dst, 0);
10322c8cec5cSDavid S. Miller }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb)
10351da177e4SLinus Torvalds {
103691df42beSJoe Perches 	pr_debug("%s: %pI4 -> %pI4, %s\n",
103791df42beSJoe Perches 		 __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
10381da177e4SLinus Torvalds 		 skb->dev ? skb->dev->name : "?");
10391da177e4SLinus Torvalds 	kfree_skb(skb);
1040c378a9c0SDave Jones 	WARN_ON(1);
10411da177e4SLinus Torvalds 	return 0;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds    We do not cache source address of outgoing interface,
10461da177e4SLinus Torvalds    because it is used only by IP RR, TS and SRR options,
10471da177e4SLinus Torvalds    so that it out of fast path.
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds    BTW remember: "addr" is allowed to be not aligned
10501da177e4SLinus Torvalds    in IP options!
10511da177e4SLinus Torvalds  */
10521da177e4SLinus Torvalds 
10538e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
10541da177e4SLinus Torvalds {
1055a61ced5dSAl Viro 	__be32 src;
10561da177e4SLinus Torvalds 
1057c7537967SDavid S. Miller 	if (rt_is_output_route(rt))
1058c5be24ffSDavid S. Miller 		src = ip_hdr(skb)->saddr;
1059ebc0ffaeSEric Dumazet 	else {
10608e36360aSDavid S. Miller 		struct fib_result res;
10618e36360aSDavid S. Miller 		struct flowi4 fl4;
10628e36360aSDavid S. Miller 		struct iphdr *iph;
10638e36360aSDavid S. Miller 
10648e36360aSDavid S. Miller 		iph = ip_hdr(skb);
10658e36360aSDavid S. Miller 
10668e36360aSDavid S. Miller 		memset(&fl4, 0, sizeof(fl4));
10678e36360aSDavid S. Miller 		fl4.daddr = iph->daddr;
10688e36360aSDavid S. Miller 		fl4.saddr = iph->saddr;
1069b0fe4a31SJulian Anastasov 		fl4.flowi4_tos = RT_TOS(iph->tos);
10708e36360aSDavid S. Miller 		fl4.flowi4_oif = rt->dst.dev->ifindex;
10718e36360aSDavid S. Miller 		fl4.flowi4_iif = skb->dev->ifindex;
10728e36360aSDavid S. Miller 		fl4.flowi4_mark = skb->mark;
10735e2b61f7SDavid S. Miller 
1074ebc0ffaeSEric Dumazet 		rcu_read_lock();
107568a5e3ddSDavid S. Miller 		if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res) == 0)
1076436c3b66SDavid S. Miller 			src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res);
1077ebc0ffaeSEric Dumazet 		else
1078f8126f1dSDavid S. Miller 			src = inet_select_addr(rt->dst.dev,
1079f8126f1dSDavid S. Miller 					       rt_nexthop(rt, iph->daddr),
10801da177e4SLinus Torvalds 					       RT_SCOPE_UNIVERSE);
1081ebc0ffaeSEric Dumazet 		rcu_read_unlock();
1082ebc0ffaeSEric Dumazet 	}
10831da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds 
1086c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
10871da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
10881da177e4SLinus Torvalds {
1089d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF))
1090d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF;
1091d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF0000))
1092d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF0000;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds #endif
10951da177e4SLinus Torvalds 
10960dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
10970dbaee3bSDavid S. Miller {
10980dbaee3bSDavid S. Miller 	unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS);
10990dbaee3bSDavid S. Miller 
11000dbaee3bSDavid S. Miller 	if (advmss == 0) {
11010dbaee3bSDavid S. Miller 		advmss = max_t(unsigned int, dst->dev->mtu - 40,
11020dbaee3bSDavid S. Miller 			       ip_rt_min_advmss);
11030dbaee3bSDavid S. Miller 		if (advmss > 65535 - 40)
11040dbaee3bSDavid S. Miller 			advmss = 65535 - 40;
11050dbaee3bSDavid S. Miller 	}
11060dbaee3bSDavid S. Miller 	return advmss;
11070dbaee3bSDavid S. Miller }
11080dbaee3bSDavid S. Miller 
1109ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst)
1110d33e4553SDavid S. Miller {
1111261663b0SSteffen Klassert 	const struct rtable *rt = (const struct rtable *) dst;
11125943634fSDavid S. Miller 	unsigned int mtu = rt->rt_pmtu;
11135943634fSDavid S. Miller 
11145943634fSDavid S. Miller 	if (mtu && time_after_eq(jiffies, rt->dst.expires))
11155943634fSDavid S. Miller 		mtu = 0;
11165943634fSDavid S. Miller 
11175943634fSDavid S. Miller 	if (!mtu)
11185943634fSDavid S. Miller 		mtu = dst_metric_raw(dst, RTAX_MTU);
1119618f9bc7SSteffen Klassert 
1120261663b0SSteffen Klassert 	if (mtu && rt_is_output_route(rt))
1121618f9bc7SSteffen Klassert 		return mtu;
1122618f9bc7SSteffen Klassert 
1123618f9bc7SSteffen Klassert 	mtu = dst->dev->mtu;
1124d33e4553SDavid S. Miller 
1125d33e4553SDavid S. Miller 	if (unlikely(dst_metric_locked(dst, RTAX_MTU))) {
1126f8126f1dSDavid S. Miller 		if (rt->rt_gateway && mtu > 576)
1127d33e4553SDavid S. Miller 			mtu = 576;
1128d33e4553SDavid S. Miller 	}
1129d33e4553SDavid S. Miller 
1130d33e4553SDavid S. Miller 	if (mtu > IP_MAX_MTU)
1131d33e4553SDavid S. Miller 		mtu = IP_MAX_MTU;
1132d33e4553SDavid S. Miller 
1133d33e4553SDavid S. Miller 	return mtu;
1134d33e4553SDavid S. Miller }
1135d33e4553SDavid S. Miller 
1136f2bb4bedSDavid S. Miller static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr)
11374895c771SDavid S. Miller {
11384895c771SDavid S. Miller 	struct fnhe_hash_bucket *hash = nh->nh_exceptions;
11394895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
11404895c771SDavid S. Miller 	u32 hval;
11414895c771SDavid S. Miller 
1142f2bb4bedSDavid S. Miller 	if (!hash)
1143f2bb4bedSDavid S. Miller 		return NULL;
1144f2bb4bedSDavid S. Miller 
1145d3a25c98SDavid S. Miller 	hval = fnhe_hashfun(daddr);
11464895c771SDavid S. Miller 
11474895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
11484895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
1149f2bb4bedSDavid S. Miller 		if (fnhe->fnhe_daddr == daddr)
1150f2bb4bedSDavid S. Miller 			return fnhe;
1151f2bb4bedSDavid S. Miller 	}
1152f2bb4bedSDavid S. Miller 	return NULL;
1153f2bb4bedSDavid S. Miller }
1154f2bb4bedSDavid S. Miller 
1155caacf05eSDavid S. Miller static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
1156f2bb4bedSDavid S. Miller 			      __be32 daddr)
1157f2bb4bedSDavid S. Miller {
1158caacf05eSDavid S. Miller 	bool ret = false;
1159caacf05eSDavid S. Miller 
1160c5038a83SDavid S. Miller 	spin_lock_bh(&fnhe_lock);
1161aee06da6SJulian Anastasov 
1162c5038a83SDavid S. Miller 	if (daddr == fnhe->fnhe_daddr) {
1163c5038a83SDavid S. Miller 		struct rtable *orig;
1164f2bb4bedSDavid S. Miller 
1165c5038a83SDavid S. Miller 		if (fnhe->fnhe_pmtu) {
1166c5038a83SDavid S. Miller 			unsigned long expires = fnhe->fnhe_expires;
1167f31fd383SJulian Anastasov 			unsigned long diff = expires - jiffies;
11684895c771SDavid S. Miller 
11694895c771SDavid S. Miller 			if (time_before(jiffies, expires)) {
1170c5038a83SDavid S. Miller 				rt->rt_pmtu = fnhe->fnhe_pmtu;
11714895c771SDavid S. Miller 				dst_set_expires(&rt->dst, diff);
11724895c771SDavid S. Miller 			}
11734895c771SDavid S. Miller 		}
1174c5038a83SDavid S. Miller 		if (fnhe->fnhe_gw) {
1175ceb33206SDavid S. Miller 			rt->rt_flags |= RTCF_REDIRECTED;
1176c5038a83SDavid S. Miller 			rt->rt_gateway = fnhe->fnhe_gw;
1177f2bb4bedSDavid S. Miller 		}
1178f2bb4bedSDavid S. Miller 
1179c5038a83SDavid S. Miller 		orig = rcu_dereference(fnhe->fnhe_rth);
1180c5038a83SDavid S. Miller 		rcu_assign_pointer(fnhe->fnhe_rth, rt);
1181c5038a83SDavid S. Miller 		if (orig)
1182c5038a83SDavid S. Miller 			rt_free(orig);
1183c5038a83SDavid S. Miller 
1184c5038a83SDavid S. Miller 		fnhe->fnhe_stamp = jiffies;
1185caacf05eSDavid S. Miller 		ret = true;
1186c5038a83SDavid S. Miller 	} else {
1187c5038a83SDavid S. Miller 		/* Routes we intend to cache in nexthop exception have
1188c5038a83SDavid S. Miller 		 * the DST_NOCACHE bit clear.  However, if we are
1189c5038a83SDavid S. Miller 		 * unsuccessful at storing this route into the cache
1190c5038a83SDavid S. Miller 		 * we really need to set it.
1191c5038a83SDavid S. Miller 		 */
1192c5038a83SDavid S. Miller 		rt->dst.flags |= DST_NOCACHE;
1193c5038a83SDavid S. Miller 	}
1194c5038a83SDavid S. Miller 	spin_unlock_bh(&fnhe_lock);
1195caacf05eSDavid S. Miller 
1196caacf05eSDavid S. Miller 	return ret;
119754764bb6SEric Dumazet }
119854764bb6SEric Dumazet 
1199caacf05eSDavid S. Miller static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
1200f2bb4bedSDavid S. Miller {
1201d26b3a7cSEric Dumazet 	struct rtable *orig, *prev, **p;
1202caacf05eSDavid S. Miller 	bool ret = true;
1203f2bb4bedSDavid S. Miller 
1204d26b3a7cSEric Dumazet 	if (rt_is_input_route(rt)) {
120554764bb6SEric Dumazet 		p = (struct rtable **)&nh->nh_rth_input;
1206d26b3a7cSEric Dumazet 	} else {
1207d26b3a7cSEric Dumazet 		if (!nh->nh_pcpu_rth_output)
1208d26b3a7cSEric Dumazet 			goto nocache;
1209d26b3a7cSEric Dumazet 		p = (struct rtable **)__this_cpu_ptr(nh->nh_pcpu_rth_output);
1210d26b3a7cSEric Dumazet 	}
1211f2bb4bedSDavid S. Miller 	orig = *p;
1212f2bb4bedSDavid S. Miller 
1213f2bb4bedSDavid S. Miller 	prev = cmpxchg(p, orig, rt);
1214f2bb4bedSDavid S. Miller 	if (prev == orig) {
1215f2bb4bedSDavid S. Miller 		if (orig)
121654764bb6SEric Dumazet 			rt_free(orig);
1217c6cffba4SDavid S. Miller 	} else {
121854764bb6SEric Dumazet 		/* Routes we intend to cache in the FIB nexthop have
121954764bb6SEric Dumazet 		 * the DST_NOCACHE bit clear.  However, if we are
122054764bb6SEric Dumazet 		 * unsuccessful at storing this route into the cache
122154764bb6SEric Dumazet 		 * we really need to set it.
122254764bb6SEric Dumazet 		 */
1223d26b3a7cSEric Dumazet nocache:
122454764bb6SEric Dumazet 		rt->dst.flags |= DST_NOCACHE;
1225caacf05eSDavid S. Miller 		ret = false;
1226caacf05eSDavid S. Miller 	}
1227caacf05eSDavid S. Miller 
1228caacf05eSDavid S. Miller 	return ret;
1229caacf05eSDavid S. Miller }
1230caacf05eSDavid S. Miller 
1231caacf05eSDavid S. Miller static DEFINE_SPINLOCK(rt_uncached_lock);
1232caacf05eSDavid S. Miller static LIST_HEAD(rt_uncached_list);
1233caacf05eSDavid S. Miller 
1234caacf05eSDavid S. Miller static void rt_add_uncached_list(struct rtable *rt)
1235caacf05eSDavid S. Miller {
1236caacf05eSDavid S. Miller 	spin_lock_bh(&rt_uncached_lock);
1237caacf05eSDavid S. Miller 	list_add_tail(&rt->rt_uncached, &rt_uncached_list);
1238caacf05eSDavid S. Miller 	spin_unlock_bh(&rt_uncached_lock);
1239caacf05eSDavid S. Miller }
1240caacf05eSDavid S. Miller 
1241caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst)
1242caacf05eSDavid S. Miller {
1243caacf05eSDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
1244caacf05eSDavid S. Miller 
124578df76a0SEric Dumazet 	if (!list_empty(&rt->rt_uncached)) {
1246caacf05eSDavid S. Miller 		spin_lock_bh(&rt_uncached_lock);
1247caacf05eSDavid S. Miller 		list_del(&rt->rt_uncached);
1248caacf05eSDavid S. Miller 		spin_unlock_bh(&rt_uncached_lock);
1249caacf05eSDavid S. Miller 	}
1250caacf05eSDavid S. Miller }
1251caacf05eSDavid S. Miller 
1252caacf05eSDavid S. Miller void rt_flush_dev(struct net_device *dev)
1253caacf05eSDavid S. Miller {
1254caacf05eSDavid S. Miller 	if (!list_empty(&rt_uncached_list)) {
1255caacf05eSDavid S. Miller 		struct net *net = dev_net(dev);
1256caacf05eSDavid S. Miller 		struct rtable *rt;
1257caacf05eSDavid S. Miller 
1258caacf05eSDavid S. Miller 		spin_lock_bh(&rt_uncached_lock);
1259caacf05eSDavid S. Miller 		list_for_each_entry(rt, &rt_uncached_list, rt_uncached) {
1260caacf05eSDavid S. Miller 			if (rt->dst.dev != dev)
1261caacf05eSDavid S. Miller 				continue;
1262caacf05eSDavid S. Miller 			rt->dst.dev = net->loopback_dev;
1263caacf05eSDavid S. Miller 			dev_hold(rt->dst.dev);
1264caacf05eSDavid S. Miller 			dev_put(dev);
1265caacf05eSDavid S. Miller 		}
1266caacf05eSDavid S. Miller 		spin_unlock_bh(&rt_uncached_lock);
12674895c771SDavid S. Miller 	}
12684895c771SDavid S. Miller }
12694895c771SDavid S. Miller 
12704331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt)
1271d2d68ba9SDavid S. Miller {
12724331debcSEric Dumazet 	return	rt &&
12734331debcSEric Dumazet 		rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
12744331debcSEric Dumazet 		!rt_is_expired(rt);
1275d2d68ba9SDavid S. Miller }
1276d2d68ba9SDavid S. Miller 
1277f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12785e2b61f7SDavid S. Miller 			   const struct fib_result *res,
1279f2bb4bedSDavid S. Miller 			   struct fib_nh_exception *fnhe,
1280982721f3SDavid S. Miller 			   struct fib_info *fi, u16 type, u32 itag)
12811da177e4SLinus Torvalds {
1282caacf05eSDavid S. Miller 	bool cached = false;
1283caacf05eSDavid S. Miller 
12841da177e4SLinus Torvalds 	if (fi) {
12854895c771SDavid S. Miller 		struct fib_nh *nh = &FIB_RES_NH(*res);
12864895c771SDavid S. Miller 
12874895c771SDavid S. Miller 		if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
12884895c771SDavid S. Miller 			rt->rt_gateway = nh->nh_gw;
12892860583fSDavid S. Miller 		dst_init_metrics(&rt->dst, fi->fib_metrics, true);
1290c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1291f2bb4bedSDavid S. Miller 		rt->dst.tclassid = nh->nh_tclassid;
12921da177e4SLinus Torvalds #endif
1293c5038a83SDavid S. Miller 		if (unlikely(fnhe))
1294caacf05eSDavid S. Miller 			cached = rt_bind_exception(rt, fnhe, daddr);
1295c5038a83SDavid S. Miller 		else if (!(rt->dst.flags & DST_NOCACHE))
1296caacf05eSDavid S. Miller 			cached = rt_cache_route(nh, rt);
1297d33e4553SDavid S. Miller 	}
1298caacf05eSDavid S. Miller 	if (unlikely(!cached))
1299caacf05eSDavid S. Miller 		rt_add_uncached_list(rt);
13001da177e4SLinus Torvalds 
1301c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
13021da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
130385b91b03SDavid S. Miller 	set_class_tag(rt, res->tclassid);
13041da177e4SLinus Torvalds #endif
13051da177e4SLinus Torvalds 	set_class_tag(rt, itag);
13061da177e4SLinus Torvalds #endif
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
13095c1e6aa3SDavid S. Miller static struct rtable *rt_dst_alloc(struct net_device *dev,
1310f2bb4bedSDavid S. Miller 				   bool nopolicy, bool noxfrm, bool will_cache)
13110c4dcd58SDavid S. Miller {
1312f5b0a874SDavid S. Miller 	return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1313c6cffba4SDavid S. Miller 			 (will_cache ? 0 : (DST_HOST | DST_NOCACHE)) |
13140c4dcd58SDavid S. Miller 			 (nopolicy ? DST_NOPOLICY : 0) |
13155c1e6aa3SDavid S. Miller 			 (noxfrm ? DST_NOXFRM : 0));
13160c4dcd58SDavid S. Miller }
13170c4dcd58SDavid S. Miller 
131896d36220SEric Dumazet /* called in rcu_read_lock() section */
13199e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
13201da177e4SLinus Torvalds 				u8 tos, struct net_device *dev, int our)
13211da177e4SLinus Torvalds {
13221da177e4SLinus Torvalds 	struct rtable *rth;
132396d36220SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
13241da177e4SLinus Torvalds 	u32 itag = 0;
1325b5f7e755SEric Dumazet 	int err;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	/* Primary sanity checks. */
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	if (in_dev == NULL)
13301da177e4SLinus Torvalds 		return -EINVAL;
13311da177e4SLinus Torvalds 
13321e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1333d0daebc3SThomas Graf 	    skb->protocol != htons(ETH_P_IP))
1334d0daebc3SThomas Graf 		goto e_inval;
1335d0daebc3SThomas Graf 
1336d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
1337d0daebc3SThomas Graf 		if (ipv4_is_loopback(saddr))
13381da177e4SLinus Torvalds 			goto e_inval;
13391da177e4SLinus Torvalds 
1340f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr)) {
1341f97c1e0cSJoe Perches 		if (!ipv4_is_local_multicast(daddr))
13421da177e4SLinus Torvalds 			goto e_inval;
1343b5f7e755SEric Dumazet 	} else {
13449e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
13459e56e380SDavid S. Miller 					  in_dev, &itag);
1346b5f7e755SEric Dumazet 		if (err < 0)
1347b5f7e755SEric Dumazet 			goto e_err;
1348b5f7e755SEric Dumazet 	}
13494e7b2f14SBenjamin LaHaise 	rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
1350f2bb4bedSDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
13511da177e4SLinus Torvalds 	if (!rth)
13521da177e4SLinus Torvalds 		goto e_nobufs;
13531da177e4SLinus Torvalds 
1354c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1355d8d1f30bSChangli Gao 	rth->dst.tclassid = itag;
13561da177e4SLinus Torvalds #endif
1357cf911662SDavid S. Miller 	rth->dst.output = ip_rt_bug;
1358cf911662SDavid S. Miller 
1359e84f84f2SDenis V. Lunev 	rth->rt_genid	= rt_genid(dev_net(dev));
13601da177e4SLinus Torvalds 	rth->rt_flags	= RTCF_MULTICAST;
136129e75252SEric Dumazet 	rth->rt_type	= RTN_MULTICAST;
13629917e1e8SDavid S. Miller 	rth->rt_is_input= 1;
136313378cadSDavid S. Miller 	rth->rt_iif	= 0;
13645943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1365f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
1366caacf05eSDavid S. Miller 	INIT_LIST_HEAD(&rth->rt_uncached);
13671da177e4SLinus Torvalds 	if (our) {
1368d8d1f30bSChangli Gao 		rth->dst.input= ip_local_deliver;
13691da177e4SLinus Torvalds 		rth->rt_flags |= RTCF_LOCAL;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1373f97c1e0cSJoe Perches 	if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
1374d8d1f30bSChangli Gao 		rth->dst.input = ip_mr_input;
13751da177e4SLinus Torvalds #endif
13761da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
13771da177e4SLinus Torvalds 
137889aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
137989aef892SDavid S. Miller 	return 0;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds e_nobufs:
13821da177e4SLinus Torvalds 	return -ENOBUFS;
13831da177e4SLinus Torvalds e_inval:
138496d36220SEric Dumazet 	return -EINVAL;
1385b5f7e755SEric Dumazet e_err:
1386b5f7e755SEric Dumazet 	return err;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
13911da177e4SLinus Torvalds 				     struct in_device *in_dev,
13921da177e4SLinus Torvalds 				     struct sk_buff *skb,
13939e12bb22SAl Viro 				     __be32 daddr,
13949e12bb22SAl Viro 				     __be32 saddr)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
13971da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
13981da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
13991da177e4SLinus Torvalds 		/*
14001da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
14011da177e4SLinus Torvalds 		 *	the only hint is MAC header.
14021da177e4SLinus Torvalds 		 */
1403058bd4d2SJoe Perches 		pr_warn("martian source %pI4 from %pI4, on dev %s\n",
1404673d57e7SHarvey Harrison 			&daddr, &saddr, dev->name);
140598e399f8SArnaldo Carvalho de Melo 		if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1406058bd4d2SJoe Perches 			print_hex_dump(KERN_WARNING, "ll header: ",
1407058bd4d2SJoe Perches 				       DUMP_PREFIX_OFFSET, 16, 1,
1408058bd4d2SJoe Perches 				       skb_mac_header(skb),
1409058bd4d2SJoe Perches 				       dev->hard_header_len, true);
14101da177e4SLinus Torvalds 		}
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds #endif
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
141547360228SEric Dumazet /* called in rcu_read_lock() section */
14165969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb,
1417982721f3SDavid S. Miller 			   const struct fib_result *res,
14181da177e4SLinus Torvalds 			   struct in_device *in_dev,
1419c6cffba4SDavid S. Miller 			   __be32 daddr, __be32 saddr, u32 tos)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	struct rtable *rth;
14221da177e4SLinus Torvalds 	int err;
14231da177e4SLinus Torvalds 	struct in_device *out_dev;
142447360228SEric Dumazet 	unsigned int flags = 0;
1425d2d68ba9SDavid S. Miller 	bool do_cache;
1426d9c9df8cSAl Viro 	u32 itag;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/* get a working reference to the output device */
142947360228SEric Dumazet 	out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res));
14301da177e4SLinus Torvalds 	if (out_dev == NULL) {
1431e87cc472SJoe Perches 		net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
14321da177e4SLinus Torvalds 		return -EINVAL;
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 
14365c04c819SMichael Smith 	err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
14379e56e380SDavid S. Miller 				  in_dev->dev, in_dev, &itag);
14381da177e4SLinus Torvalds 	if (err < 0) {
14391da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
14401da177e4SLinus Torvalds 					 saddr);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 		goto cleanup;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
144551b77caeSThomas Graf 	if (out_dev == in_dev && err &&
14461da177e4SLinus Torvalds 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
14471da177e4SLinus Torvalds 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
14481da177e4SLinus Torvalds 		flags |= RTCF_DOREDIRECT;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
14511da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
14521da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
145365324144SJesper Dangaard Brouer 		 *
145465324144SJesper Dangaard Brouer 		 * Proxy arp feature have been extended to allow, ARP
145565324144SJesper Dangaard Brouer 		 * replies back to the same interface, to support
145665324144SJesper Dangaard Brouer 		 * Private VLAN switch technologies. See arp.c.
14571da177e4SLinus Torvalds 		 */
145865324144SJesper Dangaard Brouer 		if (out_dev == in_dev &&
145965324144SJesper Dangaard Brouer 		    IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
14601da177e4SLinus Torvalds 			err = -EINVAL;
14611da177e4SLinus Torvalds 			goto cleanup;
14621da177e4SLinus Torvalds 		}
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
1465d2d68ba9SDavid S. Miller 	do_cache = false;
1466d2d68ba9SDavid S. Miller 	if (res->fi) {
1467fe3edf45SDavid S. Miller 		if (!itag) {
146854764bb6SEric Dumazet 			rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input);
1469d2d68ba9SDavid S. Miller 			if (rt_cache_valid(rth)) {
1470c6cffba4SDavid S. Miller 				skb_dst_set_noref(skb, &rth->dst);
1471d2d68ba9SDavid S. Miller 				goto out;
1472d2d68ba9SDavid S. Miller 			}
1473d2d68ba9SDavid S. Miller 			do_cache = true;
1474d2d68ba9SDavid S. Miller 		}
1475d2d68ba9SDavid S. Miller 	}
1476f2bb4bedSDavid S. Miller 
14775c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(out_dev->dev,
14785c1e6aa3SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY),
1479d2d68ba9SDavid S. Miller 			   IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
14801da177e4SLinus Torvalds 	if (!rth) {
14811da177e4SLinus Torvalds 		err = -ENOBUFS;
14821da177e4SLinus Torvalds 		goto cleanup;
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
1485cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(dev_net(rth->dst.dev));
1486cf911662SDavid S. Miller 	rth->rt_flags = flags;
1487cf911662SDavid S. Miller 	rth->rt_type = res->type;
14889917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
148913378cadSDavid S. Miller 	rth->rt_iif 	= 0;
14905943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1491f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
1492caacf05eSDavid S. Miller 	INIT_LIST_HEAD(&rth->rt_uncached);
14931da177e4SLinus Torvalds 
1494d8d1f30bSChangli Gao 	rth->dst.input = ip_forward;
1495d8d1f30bSChangli Gao 	rth->dst.output = ip_output;
14961da177e4SLinus Torvalds 
1497d2d68ba9SDavid S. Miller 	rt_set_nexthop(rth, daddr, res, NULL, res->fi, res->type, itag);
1498c6cffba4SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
1499d2d68ba9SDavid S. Miller out:
15001da177e4SLinus Torvalds 	err = 0;
15011da177e4SLinus Torvalds  cleanup:
15021da177e4SLinus Torvalds 	return err;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15055969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb,
15061da177e4SLinus Torvalds 			    struct fib_result *res,
150768a5e3ddSDavid S. Miller 			    const struct flowi4 *fl4,
15081da177e4SLinus Torvalds 			    struct in_device *in_dev,
15099e12bb22SAl Viro 			    __be32 daddr, __be32 saddr, u32 tos)
15101da177e4SLinus Torvalds {
15111da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1512ff3fccb3SDavid S. Miller 	if (res->fi && res->fi->fib_nhs > 1)
15131b7fe593SDavid S. Miller 		fib_select_multipath(res);
15141da177e4SLinus Torvalds #endif
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	/* create a routing cache entry */
1517c6cffba4SDavid S. Miller 	return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds /*
15211da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
15221da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
15231da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
15241da177e4SLinus Torvalds  *
15251da177e4SLinus Torvalds  *	Such approach solves two big problems:
15261da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
15271da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
1528ebc0ffaeSEric Dumazet  *	called with rcu_read_lock()
15291da177e4SLinus Torvalds  */
15301da177e4SLinus Torvalds 
15319e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1532c10237e0SDavid S. Miller 			       u8 tos, struct net_device *dev)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	struct fib_result res;
153596d36220SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
153668a5e3ddSDavid S. Miller 	struct flowi4	fl4;
153795c96174SEric Dumazet 	unsigned int	flags = 0;
15381da177e4SLinus Torvalds 	u32		itag = 0;
15391da177e4SLinus Torvalds 	struct rtable	*rth;
15401da177e4SLinus Torvalds 	int		err = -EINVAL;
1541c346dca1SYOSHIFUJI Hideaki 	struct net    *net = dev_net(dev);
1542d2d68ba9SDavid S. Miller 	bool do_cache;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/* IP on this device is disabled. */
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	if (!in_dev)
15471da177e4SLinus Torvalds 		goto out;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
15501da177e4SLinus Torvalds 	   by fib_lookup.
15511da177e4SLinus Torvalds 	 */
15521da177e4SLinus Torvalds 
1553d0daebc3SThomas Graf 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
15541da177e4SLinus Torvalds 		goto martian_source;
15551da177e4SLinus Torvalds 
1556d2d68ba9SDavid S. Miller 	res.fi = NULL;
155727a954bdSAndy Walls 	if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
15581da177e4SLinus Torvalds 		goto brd_input;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
15611da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
15621da177e4SLinus Torvalds 	 */
1563f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
15641da177e4SLinus Torvalds 		goto martian_source;
15651da177e4SLinus Torvalds 
1566d0daebc3SThomas Graf 	if (ipv4_is_zeronet(daddr))
15671da177e4SLinus Torvalds 		goto martian_destination;
15681da177e4SLinus Torvalds 
1569d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) {
1570d0daebc3SThomas Graf 		if (ipv4_is_loopback(daddr))
1571d0daebc3SThomas Graf 			goto martian_destination;
1572d0daebc3SThomas Graf 
1573d0daebc3SThomas Graf 		if (ipv4_is_loopback(saddr))
1574d0daebc3SThomas Graf 			goto martian_source;
1575d0daebc3SThomas Graf 	}
1576d0daebc3SThomas Graf 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
15791da177e4SLinus Torvalds 	 */
158068a5e3ddSDavid S. Miller 	fl4.flowi4_oif = 0;
158168a5e3ddSDavid S. Miller 	fl4.flowi4_iif = dev->ifindex;
158268a5e3ddSDavid S. Miller 	fl4.flowi4_mark = skb->mark;
158368a5e3ddSDavid S. Miller 	fl4.flowi4_tos = tos;
158468a5e3ddSDavid S. Miller 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
158568a5e3ddSDavid S. Miller 	fl4.daddr = daddr;
158668a5e3ddSDavid S. Miller 	fl4.saddr = saddr;
158768a5e3ddSDavid S. Miller 	err = fib_lookup(net, &fl4, &res);
1588251da413SDavid S. Miller 	if (err != 0)
15891da177e4SLinus Torvalds 		goto no_route;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_tot);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	if (res.type == RTN_BROADCAST)
15941da177e4SLinus Torvalds 		goto brd_input;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
15975c04c819SMichael Smith 		err = fib_validate_source(skb, saddr, daddr, tos,
159884a885f4SDenis V. Lunev 					  net->loopback_dev->ifindex,
15999e56e380SDavid S. Miller 					  dev, in_dev, &itag);
1600b5f7e755SEric Dumazet 		if (err < 0)
1601b5f7e755SEric Dumazet 			goto martian_source_keep_err;
16021da177e4SLinus Torvalds 		goto local_input;
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	if (!IN_DEV_FORWARD(in_dev))
1606251da413SDavid S. Miller 		goto no_route;
16071da177e4SLinus Torvalds 	if (res.type != RTN_UNICAST)
16081da177e4SLinus Torvalds 		goto martian_destination;
16091da177e4SLinus Torvalds 
161068a5e3ddSDavid S. Miller 	err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos);
16111da177e4SLinus Torvalds out:	return err;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds brd_input:
16141da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
16151da177e4SLinus Torvalds 		goto e_inval;
16161da177e4SLinus Torvalds 
161741347dcdSDavid S. Miller 	if (!ipv4_is_zeronet(saddr)) {
16189e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
16199e56e380SDavid S. Miller 					  in_dev, &itag);
16201da177e4SLinus Torvalds 		if (err < 0)
1621b5f7e755SEric Dumazet 			goto martian_source_keep_err;
16221da177e4SLinus Torvalds 	}
16231da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
16241da177e4SLinus Torvalds 	res.type = RTN_BROADCAST;
16251da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds local_input:
1628d2d68ba9SDavid S. Miller 	do_cache = false;
1629d2d68ba9SDavid S. Miller 	if (res.fi) {
1630fe3edf45SDavid S. Miller 		if (!itag) {
163154764bb6SEric Dumazet 			rth = rcu_dereference(FIB_RES_NH(res).nh_rth_input);
1632d2d68ba9SDavid S. Miller 			if (rt_cache_valid(rth)) {
1633c6cffba4SDavid S. Miller 				skb_dst_set_noref(skb, &rth->dst);
1634c6cffba4SDavid S. Miller 				err = 0;
1635c6cffba4SDavid S. Miller 				goto out;
1636d2d68ba9SDavid S. Miller 			}
1637d2d68ba9SDavid S. Miller 			do_cache = true;
1638d2d68ba9SDavid S. Miller 		}
1639d2d68ba9SDavid S. Miller 	}
1640d2d68ba9SDavid S. Miller 
16415c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(net->loopback_dev,
1642d2d68ba9SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache);
16431da177e4SLinus Torvalds 	if (!rth)
16441da177e4SLinus Torvalds 		goto e_nobufs;
16451da177e4SLinus Torvalds 
1646cf911662SDavid S. Miller 	rth->dst.input= ip_local_deliver;
1647d8d1f30bSChangli Gao 	rth->dst.output= ip_rt_bug;
1648cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
1649cf911662SDavid S. Miller 	rth->dst.tclassid = itag;
1650cf911662SDavid S. Miller #endif
16511da177e4SLinus Torvalds 
1652cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(net);
1653cf911662SDavid S. Miller 	rth->rt_flags 	= flags|RTCF_LOCAL;
1654cf911662SDavid S. Miller 	rth->rt_type	= res.type;
16559917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
165613378cadSDavid S. Miller 	rth->rt_iif	= 0;
16575943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1658f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
1659caacf05eSDavid S. Miller 	INIT_LIST_HEAD(&rth->rt_uncached);
16601da177e4SLinus Torvalds 	if (res.type == RTN_UNREACHABLE) {
1661d8d1f30bSChangli Gao 		rth->dst.input= ip_error;
1662d8d1f30bSChangli Gao 		rth->dst.error= -err;
16631da177e4SLinus Torvalds 		rth->rt_flags 	&= ~RTCF_LOCAL;
16641da177e4SLinus Torvalds 	}
1665d2d68ba9SDavid S. Miller 	if (do_cache)
1666d2d68ba9SDavid S. Miller 		rt_cache_route(&FIB_RES_NH(res), rth);
166789aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
1668b23dd4feSDavid S. Miller 	err = 0;
1669ebc0ffaeSEric Dumazet 	goto out;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds no_route:
16721da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
16731da177e4SLinus Torvalds 	res.type = RTN_UNREACHABLE;
16747f53878dSMitsuru Chinen 	if (err == -ESRCH)
16757f53878dSMitsuru Chinen 		err = -ENETUNREACH;
16761da177e4SLinus Torvalds 	goto local_input;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	/*
16791da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
16801da177e4SLinus Torvalds 	 */
16811da177e4SLinus Torvalds martian_destination:
16821da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
16831da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
1684e87cc472SJoe Perches 	if (IN_DEV_LOG_MARTIANS(in_dev))
1685e87cc472SJoe Perches 		net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
1686673d57e7SHarvey Harrison 				     &daddr, &saddr, dev->name);
16871da177e4SLinus Torvalds #endif
16882c2910a4SDietmar Eggemann 
16891da177e4SLinus Torvalds e_inval:
16901da177e4SLinus Torvalds 	err = -EINVAL;
1691ebc0ffaeSEric Dumazet 	goto out;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds e_nobufs:
16941da177e4SLinus Torvalds 	err = -ENOBUFS;
1695ebc0ffaeSEric Dumazet 	goto out;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds martian_source:
1698b5f7e755SEric Dumazet 	err = -EINVAL;
1699b5f7e755SEric Dumazet martian_source_keep_err:
17001da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
1701ebc0ffaeSEric Dumazet 	goto out;
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
1704c6cffba4SDavid S. Miller int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
170538a424e4SDavid Miller 			 u8 tos, struct net_device *dev)
17061da177e4SLinus Torvalds {
170796d36220SEric Dumazet 	int res;
17081da177e4SLinus Torvalds 
170996d36220SEric Dumazet 	rcu_read_lock();
171096d36220SEric Dumazet 
17111da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
17121da177e4SLinus Torvalds 	   The problem was that too many Ethernet cards have broken/missing
17131da177e4SLinus Torvalds 	   hardware multicast filters :-( As result the host on multicasting
17141da177e4SLinus Torvalds 	   network acquires a lot of useless route cache entries, sort of
17151da177e4SLinus Torvalds 	   SDR messages from all the world. Now we try to get rid of them.
17161da177e4SLinus Torvalds 	   Really, provided software IP multicast filter is organized
17171da177e4SLinus Torvalds 	   reasonably (at least, hashed), it does not result in a slowdown
17181da177e4SLinus Torvalds 	   comparing with route cache reject entries.
17191da177e4SLinus Torvalds 	   Note, that multicast routers are not affected, because
17201da177e4SLinus Torvalds 	   route cache entry is created eventually.
17211da177e4SLinus Torvalds 	 */
1722f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
172396d36220SEric Dumazet 		struct in_device *in_dev = __in_dev_get_rcu(dev);
17241da177e4SLinus Torvalds 
172596d36220SEric Dumazet 		if (in_dev) {
1726dbdd9a52SDavid S. Miller 			int our = ip_check_mc_rcu(in_dev, daddr, saddr,
1727eddc9ec5SArnaldo Carvalho de Melo 						  ip_hdr(skb)->protocol);
17281da177e4SLinus Torvalds 			if (our
17291da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
17309d4fb27dSJoe Perches 				||
17319d4fb27dSJoe Perches 			    (!ipv4_is_local_multicast(daddr) &&
1732f97c1e0cSJoe Perches 			     IN_DEV_MFORWARD(in_dev))
17331da177e4SLinus Torvalds #endif
17341da177e4SLinus Torvalds 			   ) {
173596d36220SEric Dumazet 				int res = ip_route_input_mc(skb, daddr, saddr,
17361da177e4SLinus Torvalds 							    tos, dev, our);
173796d36220SEric Dumazet 				rcu_read_unlock();
173896d36220SEric Dumazet 				return res;
17391da177e4SLinus Torvalds 			}
17401da177e4SLinus Torvalds 		}
17411da177e4SLinus Torvalds 		rcu_read_unlock();
17421da177e4SLinus Torvalds 		return -EINVAL;
17431da177e4SLinus Torvalds 	}
1744c10237e0SDavid S. Miller 	res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
174596d36220SEric Dumazet 	rcu_read_unlock();
174696d36220SEric Dumazet 	return res;
17471da177e4SLinus Torvalds }
1748c6cffba4SDavid S. Miller EXPORT_SYMBOL(ip_route_input_noref);
17491da177e4SLinus Torvalds 
1750ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */
1751982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res,
17521a00fee4SDavid Miller 				       const struct flowi4 *fl4, int orig_oif,
1753f61759e6SJulian Anastasov 				       struct net_device *dev_out,
17545ada5527SDavid S. Miller 				       unsigned int flags)
17551da177e4SLinus Torvalds {
1756982721f3SDavid S. Miller 	struct fib_info *fi = res->fi;
1757f2bb4bedSDavid S. Miller 	struct fib_nh_exception *fnhe;
17585ada5527SDavid S. Miller 	struct in_device *in_dev;
1759982721f3SDavid S. Miller 	u16 type = res->type;
17605ada5527SDavid S. Miller 	struct rtable *rth;
17611da177e4SLinus Torvalds 
1762d0daebc3SThomas Graf 	in_dev = __in_dev_get_rcu(dev_out);
1763d0daebc3SThomas Graf 	if (!in_dev)
1764d0daebc3SThomas Graf 		return ERR_PTR(-EINVAL);
1765d0daebc3SThomas Graf 
1766d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
176768a5e3ddSDavid S. Miller 		if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK))
17685ada5527SDavid S. Miller 			return ERR_PTR(-EINVAL);
17691da177e4SLinus Torvalds 
177068a5e3ddSDavid S. Miller 	if (ipv4_is_lbcast(fl4->daddr))
1771982721f3SDavid S. Miller 		type = RTN_BROADCAST;
177268a5e3ddSDavid S. Miller 	else if (ipv4_is_multicast(fl4->daddr))
1773982721f3SDavid S. Miller 		type = RTN_MULTICAST;
177468a5e3ddSDavid S. Miller 	else if (ipv4_is_zeronet(fl4->daddr))
17755ada5527SDavid S. Miller 		return ERR_PTR(-EINVAL);
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
17781da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
17791da177e4SLinus Torvalds 
1780982721f3SDavid S. Miller 	if (type == RTN_BROADCAST) {
17811da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
1782982721f3SDavid S. Miller 		fi = NULL;
1783982721f3SDavid S. Miller 	} else if (type == RTN_MULTICAST) {
17841da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST | RTCF_LOCAL;
1785813b3b5dSDavid S. Miller 		if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
1786813b3b5dSDavid S. Miller 				     fl4->flowi4_proto))
17871da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
17881da177e4SLinus Torvalds 		/* If multicast route do not exist use
1789dd28d1a0SEric Dumazet 		 * default one, but do not gateway in this case.
1790dd28d1a0SEric Dumazet 		 * Yes, it is hack.
17911da177e4SLinus Torvalds 		 */
1792982721f3SDavid S. Miller 		if (fi && res->prefixlen < 4)
1793982721f3SDavid S. Miller 			fi = NULL;
17941da177e4SLinus Torvalds 	}
17951da177e4SLinus Torvalds 
1796f2bb4bedSDavid S. Miller 	fnhe = NULL;
1797f2bb4bedSDavid S. Miller 	if (fi) {
1798d26b3a7cSEric Dumazet 		struct rtable __rcu **prth;
1799d26b3a7cSEric Dumazet 
1800c5038a83SDavid S. Miller 		fnhe = find_exception(&FIB_RES_NH(*res), fl4->daddr);
1801c5038a83SDavid S. Miller 		if (fnhe)
1802c5038a83SDavid S. Miller 			prth = &fnhe->fnhe_rth;
1803c5038a83SDavid S. Miller 		else
1804d26b3a7cSEric Dumazet 			prth = __this_cpu_ptr(FIB_RES_NH(*res).nh_pcpu_rth_output);
1805d26b3a7cSEric Dumazet 		rth = rcu_dereference(*prth);
1806d2d68ba9SDavid S. Miller 		if (rt_cache_valid(rth)) {
180793ac5341SDavid S. Miller 			dst_hold(&rth->dst);
1808f2bb4bedSDavid S. Miller 			return rth;
1809f2bb4bedSDavid S. Miller 		}
1810f2bb4bedSDavid S. Miller 	}
18115c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(dev_out,
18125c1e6aa3SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY),
1813f2bb4bedSDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOXFRM),
1814c5038a83SDavid S. Miller 			   fi);
18158391d07bSDimitris Michailidis 	if (!rth)
18165ada5527SDavid S. Miller 		return ERR_PTR(-ENOBUFS);
18178391d07bSDimitris Michailidis 
1818cf911662SDavid S. Miller 	rth->dst.output = ip_output;
1819cf911662SDavid S. Miller 
1820cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(dev_net(dev_out));
1821cf911662SDavid S. Miller 	rth->rt_flags	= flags;
1822cf911662SDavid S. Miller 	rth->rt_type	= type;
18239917e1e8SDavid S. Miller 	rth->rt_is_input = 0;
182413378cadSDavid S. Miller 	rth->rt_iif	= orig_oif ? : 0;
18255943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1826f8126f1dSDavid S. Miller 	rth->rt_gateway = 0;
1827caacf05eSDavid S. Miller 	INIT_LIST_HEAD(&rth->rt_uncached);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
18301da177e4SLinus Torvalds 
183141347dcdSDavid S. Miller 	if (flags & RTCF_LOCAL)
1832d8d1f30bSChangli Gao 		rth->dst.input = ip_local_deliver;
18331da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
18341da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
18351da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
1836d8d1f30bSChangli Gao 			rth->dst.output = ip_mc_output;
18371da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
18381da177e4SLinus Torvalds 		}
18391da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1840982721f3SDavid S. Miller 		if (type == RTN_MULTICAST) {
18411da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
1842813b3b5dSDavid S. Miller 			    !ipv4_is_local_multicast(fl4->daddr)) {
1843d8d1f30bSChangli Gao 				rth->dst.input = ip_mr_input;
1844d8d1f30bSChangli Gao 				rth->dst.output = ip_mc_output;
18451da177e4SLinus Torvalds 			}
18461da177e4SLinus Torvalds 		}
18471da177e4SLinus Torvalds #endif
18481da177e4SLinus Torvalds 	}
18491da177e4SLinus Torvalds 
1850f2bb4bedSDavid S. Miller 	rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0);
18511da177e4SLinus Torvalds 
18525ada5527SDavid S. Miller 	return rth;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds /*
18561da177e4SLinus Torvalds  * Major route resolver routine.
18571da177e4SLinus Torvalds  */
18581da177e4SLinus Torvalds 
185989aef892SDavid S. Miller struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	struct net_device *dev_out = NULL;
1862f61759e6SJulian Anastasov 	__u8 tos = RT_FL_TOS(fl4);
1863813b3b5dSDavid S. Miller 	unsigned int flags = 0;
1864813b3b5dSDavid S. Miller 	struct fib_result res;
18655ada5527SDavid S. Miller 	struct rtable *rth;
1866813b3b5dSDavid S. Miller 	int orig_oif;
18671da177e4SLinus Torvalds 
186885b91b03SDavid S. Miller 	res.tclassid	= 0;
18691da177e4SLinus Torvalds 	res.fi		= NULL;
18708b96d22dSDavid S. Miller 	res.table	= NULL;
18711da177e4SLinus Torvalds 
1872813b3b5dSDavid S. Miller 	orig_oif = fl4->flowi4_oif;
1873813b3b5dSDavid S. Miller 
1874813b3b5dSDavid S. Miller 	fl4->flowi4_iif = net->loopback_dev->ifindex;
1875813b3b5dSDavid S. Miller 	fl4->flowi4_tos = tos & IPTOS_RT_MASK;
1876813b3b5dSDavid S. Miller 	fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
187744713b67SDavid S. Miller 			 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
187844713b67SDavid S. Miller 
1879010c2708SDavid S. Miller 	rcu_read_lock();
1880813b3b5dSDavid S. Miller 	if (fl4->saddr) {
1881b23dd4feSDavid S. Miller 		rth = ERR_PTR(-EINVAL);
1882813b3b5dSDavid S. Miller 		if (ipv4_is_multicast(fl4->saddr) ||
1883813b3b5dSDavid S. Miller 		    ipv4_is_lbcast(fl4->saddr) ||
1884813b3b5dSDavid S. Miller 		    ipv4_is_zeronet(fl4->saddr))
18851da177e4SLinus Torvalds 			goto out;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
18881da177e4SLinus Torvalds 		   It was wrong for two reasons:
18891ab35276SDenis V. Lunev 		   1. ip_dev_find(net, saddr) can return wrong iface, if saddr
18901ab35276SDenis V. Lunev 		      is assigned to multiple interfaces.
18911da177e4SLinus Torvalds 		   2. Moreover, we are allowed to send packets with saddr
18921da177e4SLinus Torvalds 		      of another iface. --ANK
18931da177e4SLinus Torvalds 		 */
18941da177e4SLinus Torvalds 
1895813b3b5dSDavid S. Miller 		if (fl4->flowi4_oif == 0 &&
1896813b3b5dSDavid S. Miller 		    (ipv4_is_multicast(fl4->daddr) ||
1897813b3b5dSDavid S. Miller 		     ipv4_is_lbcast(fl4->daddr))) {
1898a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
1899813b3b5dSDavid S. Miller 			dev_out = __ip_dev_find(net, fl4->saddr, false);
1900a210d01aSJulian Anastasov 			if (dev_out == NULL)
1901a210d01aSJulian Anastasov 				goto out;
1902a210d01aSJulian Anastasov 
19031da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
19041da177e4SLinus Torvalds 			   and limited broadcast via necessary interface
19051da177e4SLinus Torvalds 			   without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
19061da177e4SLinus Torvalds 			   This hack is not just for fun, it allows
19071da177e4SLinus Torvalds 			   vic,vat and friends to work.
19081da177e4SLinus Torvalds 			   They bind socket to loopback, set ttl to zero
19091da177e4SLinus Torvalds 			   and expect that it will work.
19101da177e4SLinus Torvalds 			   From the viewpoint of routing cache they are broken,
19111da177e4SLinus Torvalds 			   because we are not allowed to build multicast path
19121da177e4SLinus Torvalds 			   with loopback source addr (look, routing cache
19131da177e4SLinus Torvalds 			   cannot know, that ttl is zero, so that packet
19141da177e4SLinus Torvalds 			   will not leave this host and route is valid).
19151da177e4SLinus Torvalds 			   Luckily, this hack is good workaround.
19161da177e4SLinus Torvalds 			 */
19171da177e4SLinus Torvalds 
1918813b3b5dSDavid S. Miller 			fl4->flowi4_oif = dev_out->ifindex;
19191da177e4SLinus Torvalds 			goto make_route;
19201da177e4SLinus Torvalds 		}
1921a210d01aSJulian Anastasov 
1922813b3b5dSDavid S. Miller 		if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
1923a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
1924813b3b5dSDavid S. Miller 			if (!__ip_dev_find(net, fl4->saddr, false))
1925a210d01aSJulian Anastasov 				goto out;
19261da177e4SLinus Torvalds 		}
1927a210d01aSJulian Anastasov 	}
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 
1930813b3b5dSDavid S. Miller 	if (fl4->flowi4_oif) {
1931813b3b5dSDavid S. Miller 		dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
1932b23dd4feSDavid S. Miller 		rth = ERR_PTR(-ENODEV);
19331da177e4SLinus Torvalds 		if (dev_out == NULL)
19341da177e4SLinus Torvalds 			goto out;
1935e5ed6399SHerbert Xu 
1936e5ed6399SHerbert Xu 		/* RACE: Check return value of inet_select_addr instead. */
1937fc75fc83SEric Dumazet 		if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
1938b23dd4feSDavid S. Miller 			rth = ERR_PTR(-ENETUNREACH);
1939fc75fc83SEric Dumazet 			goto out;
1940fc75fc83SEric Dumazet 		}
1941813b3b5dSDavid S. Miller 		if (ipv4_is_local_multicast(fl4->daddr) ||
1942813b3b5dSDavid S. Miller 		    ipv4_is_lbcast(fl4->daddr)) {
1943813b3b5dSDavid S. Miller 			if (!fl4->saddr)
1944813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
19451da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
19461da177e4SLinus Torvalds 			goto make_route;
19471da177e4SLinus Torvalds 		}
1948813b3b5dSDavid S. Miller 		if (fl4->saddr) {
1949813b3b5dSDavid S. Miller 			if (ipv4_is_multicast(fl4->daddr))
1950813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
1951813b3b5dSDavid S. Miller 							      fl4->flowi4_scope);
1952813b3b5dSDavid S. Miller 			else if (!fl4->daddr)
1953813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
19541da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
19551da177e4SLinus Torvalds 		}
19561da177e4SLinus Torvalds 	}
19571da177e4SLinus Torvalds 
1958813b3b5dSDavid S. Miller 	if (!fl4->daddr) {
1959813b3b5dSDavid S. Miller 		fl4->daddr = fl4->saddr;
1960813b3b5dSDavid S. Miller 		if (!fl4->daddr)
1961813b3b5dSDavid S. Miller 			fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
1962b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
1963813b3b5dSDavid S. Miller 		fl4->flowi4_oif = net->loopback_dev->ifindex;
19641da177e4SLinus Torvalds 		res.type = RTN_LOCAL;
19651da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
19661da177e4SLinus Torvalds 		goto make_route;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 
1969813b3b5dSDavid S. Miller 	if (fib_lookup(net, fl4, &res)) {
19701da177e4SLinus Torvalds 		res.fi = NULL;
19718b96d22dSDavid S. Miller 		res.table = NULL;
1972813b3b5dSDavid S. Miller 		if (fl4->flowi4_oif) {
19731da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
19741da177e4SLinus Torvalds 			   that the destination is on link.
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 			   WHY? DW.
19771da177e4SLinus Torvalds 			   Because we are allowed to send to iface
19781da177e4SLinus Torvalds 			   even if it has NO routes and NO assigned
19791da177e4SLinus Torvalds 			   addresses. When oif is specified, routing
19801da177e4SLinus Torvalds 			   tables are looked up with only one purpose:
19811da177e4SLinus Torvalds 			   to catch if destination is gatewayed, rather than
19821da177e4SLinus Torvalds 			   direct. Moreover, if MSG_DONTROUTE is set,
19831da177e4SLinus Torvalds 			   we send packet, ignoring both routing tables
19841da177e4SLinus Torvalds 			   and ifaddr state. --ANK
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 			   We could make it even if oif is unknown,
19881da177e4SLinus Torvalds 			   likely IPv6, but we do not.
19891da177e4SLinus Torvalds 			 */
19901da177e4SLinus Torvalds 
1991813b3b5dSDavid S. Miller 			if (fl4->saddr == 0)
1992813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
19931da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
19941da177e4SLinus Torvalds 			res.type = RTN_UNICAST;
19951da177e4SLinus Torvalds 			goto make_route;
19961da177e4SLinus Torvalds 		}
1997b23dd4feSDavid S. Miller 		rth = ERR_PTR(-ENETUNREACH);
19981da177e4SLinus Torvalds 		goto out;
19991da177e4SLinus Torvalds 	}
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
2002813b3b5dSDavid S. Miller 		if (!fl4->saddr) {
20039fc3bbb4SJoel Sing 			if (res.fi->fib_prefsrc)
2004813b3b5dSDavid S. Miller 				fl4->saddr = res.fi->fib_prefsrc;
20059fc3bbb4SJoel Sing 			else
2006813b3b5dSDavid S. Miller 				fl4->saddr = fl4->daddr;
20079fc3bbb4SJoel Sing 		}
2008b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
2009813b3b5dSDavid S. Miller 		fl4->flowi4_oif = dev_out->ifindex;
20101da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
20111da177e4SLinus Torvalds 		goto make_route;
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
2015813b3b5dSDavid S. Miller 	if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0)
20161b7fe593SDavid S. Miller 		fib_select_multipath(&res);
20171da177e4SLinus Torvalds 	else
20181da177e4SLinus Torvalds #endif
201921d8c49eSDavid S. Miller 	if (!res.prefixlen &&
202021d8c49eSDavid S. Miller 	    res.table->tb_num_default > 1 &&
2021813b3b5dSDavid S. Miller 	    res.type == RTN_UNICAST && !fl4->flowi4_oif)
20220c838ff1SDavid S. Miller 		fib_select_default(&res);
20231da177e4SLinus Torvalds 
2024813b3b5dSDavid S. Miller 	if (!fl4->saddr)
2025813b3b5dSDavid S. Miller 		fl4->saddr = FIB_RES_PREFSRC(net, res);
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	dev_out = FIB_RES_DEV(res);
2028813b3b5dSDavid S. Miller 	fl4->flowi4_oif = dev_out->ifindex;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds make_route:
20321a00fee4SDavid Miller 	rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags);
20331da177e4SLinus Torvalds 
2034010c2708SDavid S. Miller out:
2035010c2708SDavid S. Miller 	rcu_read_unlock();
2036b23dd4feSDavid S. Miller 	return rth;
20371da177e4SLinus Torvalds }
2038d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key);
2039d8c97a94SArnaldo Carvalho de Melo 
2040ae2688d5SJianzhao Wang static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie)
2041ae2688d5SJianzhao Wang {
2042ae2688d5SJianzhao Wang 	return NULL;
2043ae2688d5SJianzhao Wang }
2044ae2688d5SJianzhao Wang 
2045ebb762f2SSteffen Klassert static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst)
2046ec831ea7SRoland Dreier {
2047618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2048618f9bc7SSteffen Klassert 
2049618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
2050ec831ea7SRoland Dreier }
2051ec831ea7SRoland Dreier 
20526700c270SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
20536700c270SDavid S. Miller 					  struct sk_buff *skb, u32 mtu)
205414e50e57SDavid S. Miller {
205514e50e57SDavid S. Miller }
205614e50e57SDavid S. Miller 
20576700c270SDavid S. Miller static void ipv4_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
20586700c270SDavid S. Miller 				       struct sk_buff *skb)
2059b587ee3bSDavid S. Miller {
2060b587ee3bSDavid S. Miller }
2061b587ee3bSDavid S. Miller 
20620972ddb2SHeld Bernhard static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst,
20630972ddb2SHeld Bernhard 					  unsigned long old)
20640972ddb2SHeld Bernhard {
20650972ddb2SHeld Bernhard 	return NULL;
20660972ddb2SHeld Bernhard }
20670972ddb2SHeld Bernhard 
206814e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
206914e50e57SDavid S. Miller 	.family			=	AF_INET,
207009640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IP),
2071ae2688d5SJianzhao Wang 	.check			=	ipv4_blackhole_dst_check,
2072ebb762f2SSteffen Klassert 	.mtu			=	ipv4_blackhole_mtu,
2073214f45c9SEric Dumazet 	.default_advmss		=	ipv4_default_advmss,
207414e50e57SDavid S. Miller 	.update_pmtu		=	ipv4_rt_blackhole_update_pmtu,
2075b587ee3bSDavid S. Miller 	.redirect		=	ipv4_rt_blackhole_redirect,
20760972ddb2SHeld Bernhard 	.cow_metrics		=	ipv4_rt_blackhole_cow_metrics,
2077d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ipv4_neigh_lookup,
207814e50e57SDavid S. Miller };
207914e50e57SDavid S. Miller 
20802774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
208114e50e57SDavid S. Miller {
20822774c131SDavid S. Miller 	struct rtable *ort = (struct rtable *) dst_orig;
2083f5b0a874SDavid S. Miller 	struct rtable *rt;
208414e50e57SDavid S. Miller 
2085f5b0a874SDavid S. Miller 	rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
208614e50e57SDavid S. Miller 	if (rt) {
2087d8d1f30bSChangli Gao 		struct dst_entry *new = &rt->dst;
208814e50e57SDavid S. Miller 
208914e50e57SDavid S. Miller 		new->__use = 1;
2090352e512cSHerbert Xu 		new->input = dst_discard;
2091352e512cSHerbert Xu 		new->output = dst_discard;
209214e50e57SDavid S. Miller 
2093d8d1f30bSChangli Gao 		new->dev = ort->dst.dev;
209414e50e57SDavid S. Miller 		if (new->dev)
209514e50e57SDavid S. Miller 			dev_hold(new->dev);
209614e50e57SDavid S. Miller 
20979917e1e8SDavid S. Miller 		rt->rt_is_input = ort->rt_is_input;
20985e2b61f7SDavid S. Miller 		rt->rt_iif = ort->rt_iif;
20995943634fSDavid S. Miller 		rt->rt_pmtu = ort->rt_pmtu;
210014e50e57SDavid S. Miller 
2101e84f84f2SDenis V. Lunev 		rt->rt_genid = rt_genid(net);
210214e50e57SDavid S. Miller 		rt->rt_flags = ort->rt_flags;
210314e50e57SDavid S. Miller 		rt->rt_type = ort->rt_type;
210414e50e57SDavid S. Miller 		rt->rt_gateway = ort->rt_gateway;
210514e50e57SDavid S. Miller 
2106caacf05eSDavid S. Miller 		INIT_LIST_HEAD(&rt->rt_uncached);
2107caacf05eSDavid S. Miller 
210814e50e57SDavid S. Miller 		dst_free(new);
210914e50e57SDavid S. Miller 	}
211014e50e57SDavid S. Miller 
21112774c131SDavid S. Miller 	dst_release(dst_orig);
21122774c131SDavid S. Miller 
21132774c131SDavid S. Miller 	return rt ? &rt->dst : ERR_PTR(-ENOMEM);
211414e50e57SDavid S. Miller }
211514e50e57SDavid S. Miller 
21169d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
2117273447b3SDavid S. Miller 				    struct sock *sk)
21181da177e4SLinus Torvalds {
21199d6ec938SDavid S. Miller 	struct rtable *rt = __ip_route_output_key(net, flp4);
21201da177e4SLinus Torvalds 
2121b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
2122b23dd4feSDavid S. Miller 		return rt;
21231da177e4SLinus Torvalds 
212456157872SDavid S. Miller 	if (flp4->flowi4_proto)
21259d6ec938SDavid S. Miller 		rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
21269d6ec938SDavid S. Miller 						   flowi4_to_flowi(flp4),
21279d6ec938SDavid S. Miller 						   sk, 0);
21281da177e4SLinus Torvalds 
2129b23dd4feSDavid S. Miller 	return rt;
21301da177e4SLinus Torvalds }
2131d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2132d8c97a94SArnaldo Carvalho de Melo 
2133f1ce3062SDavid S. Miller static int rt_fill_info(struct net *net,  __be32 dst, __be32 src,
2134f1ce3062SDavid S. Miller 			struct flowi4 *fl4, struct sk_buff *skb, u32 pid,
2135f1ce3062SDavid S. Miller 			u32 seq, int event, int nowait, unsigned int flags)
21361da177e4SLinus Torvalds {
2137511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
21381da177e4SLinus Torvalds 	struct rtmsg *r;
21391da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
21402bc8ca40SSteffen Klassert 	unsigned long expires = 0;
2141f185071dSDavid S. Miller 	u32 error;
2142521f5490SJulian Anastasov 	u32 metrics[RTAX_MAX];
2143be403ea1SThomas Graf 
2144be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2145be403ea1SThomas Graf 	if (nlh == NULL)
214626932566SPatrick McHardy 		return -EMSGSIZE;
2147be403ea1SThomas Graf 
2148be403ea1SThomas Graf 	r = nlmsg_data(nlh);
21491da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
21501da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
21511da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
2152d6c0a4f6SDavid Miller 	r->rtm_tos	= fl4->flowi4_tos;
21531da177e4SLinus Torvalds 	r->rtm_table	= RT_TABLE_MAIN;
2154f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN))
2155f3756b79SDavid S. Miller 		goto nla_put_failure;
21561da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
21571da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
21581da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
21591da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
21601da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
21611da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
2162be403ea1SThomas Graf 
2163f1ce3062SDavid S. Miller 	if (nla_put_be32(skb, RTA_DST, dst))
2164f3756b79SDavid S. Miller 		goto nla_put_failure;
21651a00fee4SDavid Miller 	if (src) {
21661da177e4SLinus Torvalds 		r->rtm_src_len = 32;
21671a00fee4SDavid Miller 		if (nla_put_be32(skb, RTA_SRC, src))
2168f3756b79SDavid S. Miller 			goto nla_put_failure;
21691da177e4SLinus Torvalds 	}
2170f3756b79SDavid S. Miller 	if (rt->dst.dev &&
2171f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2172f3756b79SDavid S. Miller 		goto nla_put_failure;
2173c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2174f3756b79SDavid S. Miller 	if (rt->dst.tclassid &&
2175f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
2176f3756b79SDavid S. Miller 		goto nla_put_failure;
21771da177e4SLinus Torvalds #endif
217841347dcdSDavid S. Miller 	if (!rt_is_input_route(rt) &&
2179d6c0a4f6SDavid Miller 	    fl4->saddr != src) {
2180d6c0a4f6SDavid Miller 		if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr))
2181f3756b79SDavid S. Miller 			goto nla_put_failure;
2182f3756b79SDavid S. Miller 	}
2183f8126f1dSDavid S. Miller 	if (rt->rt_gateway &&
2184f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway))
2185f3756b79SDavid S. Miller 		goto nla_put_failure;
2186be403ea1SThomas Graf 
2187521f5490SJulian Anastasov 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
2188521f5490SJulian Anastasov 	if (rt->rt_pmtu)
2189521f5490SJulian Anastasov 		metrics[RTAX_MTU - 1] = rt->rt_pmtu;
2190521f5490SJulian Anastasov 	if (rtnetlink_put_metrics(skb, metrics) < 0)
2191be403ea1SThomas Graf 		goto nla_put_failure;
2192be403ea1SThomas Graf 
2193b4869889SDavid Miller 	if (fl4->flowi4_mark &&
2194b4869889SDavid Miller 	    nla_put_be32(skb, RTA_MARK, fl4->flowi4_mark))
2195f3756b79SDavid S. Miller 		goto nla_put_failure;
2196963bfeeeSEric Dumazet 
2197d8d1f30bSChangli Gao 	error = rt->dst.error;
21985943634fSDavid S. Miller 	expires = rt->dst.expires;
21992bc8ca40SSteffen Klassert 	if (expires) {
22002bc8ca40SSteffen Klassert 		if (time_before(jiffies, expires))
2201fe6fe792SEric Dumazet 			expires -= jiffies;
22022bc8ca40SSteffen Klassert 		else
22032bc8ca40SSteffen Klassert 			expires = 0;
22042bc8ca40SSteffen Klassert 	}
2205be403ea1SThomas Graf 
2206c7537967SDavid S. Miller 	if (rt_is_input_route(rt)) {
2207f3756b79SDavid S. Miller 		if (nla_put_u32(skb, RTA_IIF, rt->rt_iif))
2208f3756b79SDavid S. Miller 			goto nla_put_failure;
22091da177e4SLinus Torvalds 	}
22101da177e4SLinus Torvalds 
2211f185071dSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
2212e3703b3dSThomas Graf 		goto nla_put_failure;
22131da177e4SLinus Torvalds 
2214be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
2215be403ea1SThomas Graf 
2216be403ea1SThomas Graf nla_put_failure:
221726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
221826932566SPatrick McHardy 	return -EMSGSIZE;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
222163f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
22221da177e4SLinus Torvalds {
22233b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2224d889ce3bSThomas Graf 	struct rtmsg *rtm;
2225d889ce3bSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
22261da177e4SLinus Torvalds 	struct rtable *rt = NULL;
2227d6c0a4f6SDavid Miller 	struct flowi4 fl4;
22289e12bb22SAl Viro 	__be32 dst = 0;
22299e12bb22SAl Viro 	__be32 src = 0;
22309e12bb22SAl Viro 	u32 iif;
2231d889ce3bSThomas Graf 	int err;
2232963bfeeeSEric Dumazet 	int mark;
22331da177e4SLinus Torvalds 	struct sk_buff *skb;
22341da177e4SLinus Torvalds 
2235d889ce3bSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2236d889ce3bSThomas Graf 	if (err < 0)
2237d889ce3bSThomas Graf 		goto errout;
2238d889ce3bSThomas Graf 
2239d889ce3bSThomas Graf 	rtm = nlmsg_data(nlh);
2240d889ce3bSThomas Graf 
22411da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2242d889ce3bSThomas Graf 	if (skb == NULL) {
2243d889ce3bSThomas Graf 		err = -ENOBUFS;
2244d889ce3bSThomas Graf 		goto errout;
2245d889ce3bSThomas Graf 	}
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
22481da177e4SLinus Torvalds 	   through good chunk of routing engine.
22491da177e4SLinus Torvalds 	 */
2250459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
2251c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2252d2c962b8SStephen Hemminger 
2253d2c962b8SStephen Hemminger 	/* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2254eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = IPPROTO_ICMP;
22551da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
22561da177e4SLinus Torvalds 
225717fb2c64SAl Viro 	src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
225817fb2c64SAl Viro 	dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2259d889ce3bSThomas Graf 	iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
2260963bfeeeSEric Dumazet 	mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
22611da177e4SLinus Torvalds 
2262d6c0a4f6SDavid Miller 	memset(&fl4, 0, sizeof(fl4));
2263d6c0a4f6SDavid Miller 	fl4.daddr = dst;
2264d6c0a4f6SDavid Miller 	fl4.saddr = src;
2265d6c0a4f6SDavid Miller 	fl4.flowi4_tos = rtm->rtm_tos;
2266d6c0a4f6SDavid Miller 	fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
2267d6c0a4f6SDavid Miller 	fl4.flowi4_mark = mark;
2268d6c0a4f6SDavid Miller 
22691da177e4SLinus Torvalds 	if (iif) {
2270d889ce3bSThomas Graf 		struct net_device *dev;
2271d889ce3bSThomas Graf 
22721937504dSDenis V. Lunev 		dev = __dev_get_by_index(net, iif);
2273d889ce3bSThomas Graf 		if (dev == NULL) {
22741da177e4SLinus Torvalds 			err = -ENODEV;
2275d889ce3bSThomas Graf 			goto errout_free;
2276d889ce3bSThomas Graf 		}
2277d889ce3bSThomas Graf 
22781da177e4SLinus Torvalds 		skb->protocol	= htons(ETH_P_IP);
22791da177e4SLinus Torvalds 		skb->dev	= dev;
2280963bfeeeSEric Dumazet 		skb->mark	= mark;
22811da177e4SLinus Torvalds 		local_bh_disable();
22821da177e4SLinus Torvalds 		err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
22831da177e4SLinus Torvalds 		local_bh_enable();
2284d889ce3bSThomas Graf 
2285511c3f92SEric Dumazet 		rt = skb_rtable(skb);
2286d8d1f30bSChangli Gao 		if (err == 0 && rt->dst.error)
2287d8d1f30bSChangli Gao 			err = -rt->dst.error;
22881da177e4SLinus Torvalds 	} else {
22899d6ec938SDavid S. Miller 		rt = ip_route_output_key(net, &fl4);
2290b23dd4feSDavid S. Miller 
2291b23dd4feSDavid S. Miller 		err = 0;
2292b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
2293b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
22941da177e4SLinus Torvalds 	}
2295d889ce3bSThomas Graf 
22961da177e4SLinus Torvalds 	if (err)
2297d889ce3bSThomas Graf 		goto errout_free;
22981da177e4SLinus Torvalds 
2299d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
23001da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
23011da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
23021da177e4SLinus Torvalds 
2303f1ce3062SDavid S. Miller 	err = rt_fill_info(net, dst, src, &fl4, skb,
23041a00fee4SDavid Miller 			   NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2305b6544c0bSJamal Hadi Salim 			   RTM_NEWROUTE, 0, 0);
2306d889ce3bSThomas Graf 	if (err <= 0)
2307d889ce3bSThomas Graf 		goto errout_free;
23081da177e4SLinus Torvalds 
23091937504dSDenis V. Lunev 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2310d889ce3bSThomas Graf errout:
23112942e900SThomas Graf 	return err;
23121da177e4SLinus Torvalds 
2313d889ce3bSThomas Graf errout_free:
23141da177e4SLinus Torvalds 	kfree_skb(skb);
2315d889ce3bSThomas Graf 	goto errout;
23161da177e4SLinus Torvalds }
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
23191da177e4SLinus Torvalds {
23201da177e4SLinus Torvalds 	return skb->len;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
23241da177e4SLinus Torvalds {
2325bafa6d9dSNicolas Dichtel 	rt_cache_flush(dev_net(in_dev->dev));
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
232981c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write,
23308d65af78SAlexey Dobriyan 					void __user *buffer,
23311da177e4SLinus Torvalds 					size_t *lenp, loff_t *ppos)
23321da177e4SLinus Torvalds {
23331da177e4SLinus Torvalds 	if (write) {
2334bafa6d9dSNicolas Dichtel 		rt_cache_flush((struct net *)__ctl->extra1);
23351da177e4SLinus Torvalds 		return 0;
23361da177e4SLinus Torvalds 	}
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	return -EINVAL;
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
2341eeb61f71SAl Viro static ctl_table ipv4_route_table[] = {
23421da177e4SLinus Torvalds 	{
23431da177e4SLinus Torvalds 		.procname	= "gc_thresh",
23441da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
23451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23461da177e4SLinus Torvalds 		.mode		= 0644,
23476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23481da177e4SLinus Torvalds 	},
23491da177e4SLinus Torvalds 	{
23501da177e4SLinus Torvalds 		.procname	= "max_size",
23511da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
23521da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23531da177e4SLinus Torvalds 		.mode		= 0644,
23546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23551da177e4SLinus Torvalds 	},
23561da177e4SLinus Torvalds 	{
23571da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
23601da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
23611da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23621da177e4SLinus Torvalds 		.mode		= 0644,
23636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
23641da177e4SLinus Torvalds 	},
23651da177e4SLinus Torvalds 	{
23661da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
23671da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
23681da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23691da177e4SLinus Torvalds 		.mode		= 0644,
23706d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
23711da177e4SLinus Torvalds 	},
23721da177e4SLinus Torvalds 	{
23731da177e4SLinus Torvalds 		.procname	= "gc_timeout",
23741da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
23751da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23761da177e4SLinus Torvalds 		.mode		= 0644,
23776d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
23781da177e4SLinus Torvalds 	},
23791da177e4SLinus Torvalds 	{
23809f28a2fcSEric Dumazet 		.procname	= "gc_interval",
23819f28a2fcSEric Dumazet 		.data		= &ip_rt_gc_interval,
23829f28a2fcSEric Dumazet 		.maxlen		= sizeof(int),
23839f28a2fcSEric Dumazet 		.mode		= 0644,
23849f28a2fcSEric Dumazet 		.proc_handler	= proc_dointvec_jiffies,
23859f28a2fcSEric Dumazet 	},
23869f28a2fcSEric Dumazet 	{
23871da177e4SLinus Torvalds 		.procname	= "redirect_load",
23881da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
23891da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23901da177e4SLinus Torvalds 		.mode		= 0644,
23916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23921da177e4SLinus Torvalds 	},
23931da177e4SLinus Torvalds 	{
23941da177e4SLinus Torvalds 		.procname	= "redirect_number",
23951da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
23961da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23971da177e4SLinus Torvalds 		.mode		= 0644,
23986d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23991da177e4SLinus Torvalds 	},
24001da177e4SLinus Torvalds 	{
24011da177e4SLinus Torvalds 		.procname	= "redirect_silence",
24021da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
24031da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24041da177e4SLinus Torvalds 		.mode		= 0644,
24056d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24061da177e4SLinus Torvalds 	},
24071da177e4SLinus Torvalds 	{
24081da177e4SLinus Torvalds 		.procname	= "error_cost",
24091da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
24101da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24111da177e4SLinus Torvalds 		.mode		= 0644,
24126d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24131da177e4SLinus Torvalds 	},
24141da177e4SLinus Torvalds 	{
24151da177e4SLinus Torvalds 		.procname	= "error_burst",
24161da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
24171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24181da177e4SLinus Torvalds 		.mode		= 0644,
24196d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24201da177e4SLinus Torvalds 	},
24211da177e4SLinus Torvalds 	{
24221da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
24231da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
24241da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24251da177e4SLinus Torvalds 		.mode		= 0644,
24266d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24271da177e4SLinus Torvalds 	},
24281da177e4SLinus Torvalds 	{
24291da177e4SLinus Torvalds 		.procname	= "mtu_expires",
24301da177e4SLinus Torvalds 		.data		= &ip_rt_mtu_expires,
24311da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24321da177e4SLinus Torvalds 		.mode		= 0644,
24336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
24341da177e4SLinus Torvalds 	},
24351da177e4SLinus Torvalds 	{
24361da177e4SLinus Torvalds 		.procname	= "min_pmtu",
24371da177e4SLinus Torvalds 		.data		= &ip_rt_min_pmtu,
24381da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24391da177e4SLinus Torvalds 		.mode		= 0644,
24406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24411da177e4SLinus Torvalds 	},
24421da177e4SLinus Torvalds 	{
24431da177e4SLinus Torvalds 		.procname	= "min_adv_mss",
24441da177e4SLinus Torvalds 		.data		= &ip_rt_min_advmss,
24451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24461da177e4SLinus Torvalds 		.mode		= 0644,
24476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24481da177e4SLinus Torvalds 	},
2449f8572d8fSEric W. Biederman 	{ }
24501da177e4SLinus Torvalds };
245139a23e75SDenis V. Lunev 
245239a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = {
245339a23e75SDenis V. Lunev 	{
245439a23e75SDenis V. Lunev 		.procname	= "flush",
245539a23e75SDenis V. Lunev 		.maxlen		= sizeof(int),
245639a23e75SDenis V. Lunev 		.mode		= 0200,
24576d9f239aSAlexey Dobriyan 		.proc_handler	= ipv4_sysctl_rtcache_flush,
245839a23e75SDenis V. Lunev 	},
2459f8572d8fSEric W. Biederman 	{ },
246039a23e75SDenis V. Lunev };
246139a23e75SDenis V. Lunev 
246239a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net)
246339a23e75SDenis V. Lunev {
246439a23e75SDenis V. Lunev 	struct ctl_table *tbl;
246539a23e75SDenis V. Lunev 
246639a23e75SDenis V. Lunev 	tbl = ipv4_route_flush_table;
246709ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
246839a23e75SDenis V. Lunev 		tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
246939a23e75SDenis V. Lunev 		if (tbl == NULL)
247039a23e75SDenis V. Lunev 			goto err_dup;
247139a23e75SDenis V. Lunev 	}
247239a23e75SDenis V. Lunev 	tbl[0].extra1 = net;
247339a23e75SDenis V. Lunev 
2474ec8f23ceSEric W. Biederman 	net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
247539a23e75SDenis V. Lunev 	if (net->ipv4.route_hdr == NULL)
247639a23e75SDenis V. Lunev 		goto err_reg;
247739a23e75SDenis V. Lunev 	return 0;
247839a23e75SDenis V. Lunev 
247939a23e75SDenis V. Lunev err_reg:
248039a23e75SDenis V. Lunev 	if (tbl != ipv4_route_flush_table)
248139a23e75SDenis V. Lunev 		kfree(tbl);
248239a23e75SDenis V. Lunev err_dup:
248339a23e75SDenis V. Lunev 	return -ENOMEM;
248439a23e75SDenis V. Lunev }
248539a23e75SDenis V. Lunev 
248639a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net)
248739a23e75SDenis V. Lunev {
248839a23e75SDenis V. Lunev 	struct ctl_table *tbl;
248939a23e75SDenis V. Lunev 
249039a23e75SDenis V. Lunev 	tbl = net->ipv4.route_hdr->ctl_table_arg;
249139a23e75SDenis V. Lunev 	unregister_net_sysctl_table(net->ipv4.route_hdr);
249239a23e75SDenis V. Lunev 	BUG_ON(tbl == ipv4_route_flush_table);
249339a23e75SDenis V. Lunev 	kfree(tbl);
249439a23e75SDenis V. Lunev }
249539a23e75SDenis V. Lunev 
249639a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = {
249739a23e75SDenis V. Lunev 	.init = sysctl_route_net_init,
249839a23e75SDenis V. Lunev 	.exit = sysctl_route_net_exit,
249939a23e75SDenis V. Lunev };
25001da177e4SLinus Torvalds #endif
25011da177e4SLinus Torvalds 
25023ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net)
25039f5e97e5SDenis V. Lunev {
2504*b42664f8SNicolas Dichtel 	atomic_set(&net->rt_genid, 0);
2505436c3b66SDavid S. Miller 	get_random_bytes(&net->ipv4.dev_addr_genid,
2506436c3b66SDavid S. Miller 			 sizeof(net->ipv4.dev_addr_genid));
25079f5e97e5SDenis V. Lunev 	return 0;
25089f5e97e5SDenis V. Lunev }
25099f5e97e5SDenis V. Lunev 
25103ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = {
25113ee94372SNeil Horman 	.init = rt_genid_init,
25129f5e97e5SDenis V. Lunev };
25139f5e97e5SDenis V. Lunev 
2514c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net)
2515c3426b47SDavid S. Miller {
2516c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
2517c3426b47SDavid S. Miller 
2518c3426b47SDavid S. Miller 	if (!bp)
2519c3426b47SDavid S. Miller 		return -ENOMEM;
2520c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
2521c3426b47SDavid S. Miller 	net->ipv4.peers = bp;
2522c3426b47SDavid S. Miller 	return 0;
2523c3426b47SDavid S. Miller }
2524c3426b47SDavid S. Miller 
2525c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net)
2526c3426b47SDavid S. Miller {
2527c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv4.peers;
2528c3426b47SDavid S. Miller 
2529c3426b47SDavid S. Miller 	net->ipv4.peers = NULL;
253056a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
2531c3426b47SDavid S. Miller 	kfree(bp);
2532c3426b47SDavid S. Miller }
2533c3426b47SDavid S. Miller 
2534c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
2535c3426b47SDavid S. Miller 	.init	=	ipv4_inetpeer_init,
2536c3426b47SDavid S. Miller 	.exit	=	ipv4_inetpeer_exit,
2537c3426b47SDavid S. Miller };
25389f5e97e5SDenis V. Lunev 
2539c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
25407d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
2541c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds int __init ip_rt_init(void)
25441da177e4SLinus Torvalds {
2545424c4b70SEric Dumazet 	int rc = 0;
25461da177e4SLinus Torvalds 
2547c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
25480dcec8c2SIngo Molnar 	ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
25491da177e4SLinus Torvalds 	if (!ip_rt_acct)
25501da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
25511da177e4SLinus Torvalds #endif
25521da177e4SLinus Torvalds 
2553e5d679f3SAlexey Dobriyan 	ipv4_dst_ops.kmem_cachep =
2554e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
255520c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
25561da177e4SLinus Torvalds 
255714e50e57SDavid S. Miller 	ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
255814e50e57SDavid S. Miller 
2559fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_ops) < 0)
2560fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_ops counter\n");
2561fc66f95cSEric Dumazet 
2562fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
2563fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
2564fc66f95cSEric Dumazet 
256589aef892SDavid S. Miller 	ipv4_dst_ops.gc_thresh = ~0;
256689aef892SDavid S. Miller 	ip_rt_max_size = INT_MAX;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	devinet_init();
25691da177e4SLinus Torvalds 	ip_fib_init();
25701da177e4SLinus Torvalds 
257173b38711SDenis V. Lunev 	if (ip_rt_proc_init())
2572058bd4d2SJoe Perches 		pr_err("Unable to create route proc files\n");
25731da177e4SLinus Torvalds #ifdef CONFIG_XFRM
25741da177e4SLinus Torvalds 	xfrm_init();
2575a33bc5c1SNeil Horman 	xfrm4_init(ip_rt_max_size);
25761da177e4SLinus Torvalds #endif
2577c7ac8679SGreg Rose 	rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL);
257863f3444fSThomas Graf 
257939a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL
258039a23e75SDenis V. Lunev 	register_pernet_subsys(&sysctl_route_ops);
258139a23e75SDenis V. Lunev #endif
25823ee94372SNeil Horman 	register_pernet_subsys(&rt_genid_ops);
2583c3426b47SDavid S. Miller 	register_pernet_subsys(&ipv4_inetpeer_ops);
25841da177e4SLinus Torvalds 	return rc;
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds 
2587a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL
2588eeb61f71SAl Viro /*
2589eeb61f71SAl Viro  * We really need to sanitize the damn ipv4 init order, then all
2590eeb61f71SAl Viro  * this nonsense will go away.
2591eeb61f71SAl Viro  */
2592eeb61f71SAl Viro void __init ip_static_sysctl_init(void)
2593eeb61f71SAl Viro {
25944e5ca785SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
2595eeb61f71SAl Viro }
2596a1bc6eb4SAl Viro #endif
2597