xref: /linux/net/ipv4/route.c (revision 54764bb647b2e847c512acf8d443df965da35000)
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>
73424c4b70SEric Dumazet #include <linux/bootmem.h>
741da177e4SLinus Torvalds #include <linux/string.h>
751da177e4SLinus Torvalds #include <linux/socket.h>
761da177e4SLinus Torvalds #include <linux/sockios.h>
771da177e4SLinus Torvalds #include <linux/errno.h>
781da177e4SLinus Torvalds #include <linux/in.h>
791da177e4SLinus Torvalds #include <linux/inet.h>
801da177e4SLinus Torvalds #include <linux/netdevice.h>
811da177e4SLinus Torvalds #include <linux/proc_fs.h>
821da177e4SLinus Torvalds #include <linux/init.h>
8339c90eceSEric Dumazet #include <linux/workqueue.h>
841da177e4SLinus Torvalds #include <linux/skbuff.h>
851da177e4SLinus Torvalds #include <linux/inetdevice.h>
861da177e4SLinus Torvalds #include <linux/igmp.h>
871da177e4SLinus Torvalds #include <linux/pkt_sched.h>
881da177e4SLinus Torvalds #include <linux/mroute.h>
891da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
901da177e4SLinus Torvalds #include <linux/random.h>
911da177e4SLinus Torvalds #include <linux/jhash.h>
921da177e4SLinus Torvalds #include <linux/rcupdate.h>
931da177e4SLinus Torvalds #include <linux/times.h>
945a0e3ad6STejun Heo #include <linux/slab.h>
95b9eda06fSStephen Rothwell #include <linux/prefetch.h>
96352e512cSHerbert Xu #include <net/dst.h>
97457c4cbcSEric W. Biederman #include <net/net_namespace.h>
981da177e4SLinus Torvalds #include <net/protocol.h>
991da177e4SLinus Torvalds #include <net/ip.h>
1001da177e4SLinus Torvalds #include <net/route.h>
1011da177e4SLinus Torvalds #include <net/inetpeer.h>
1021da177e4SLinus Torvalds #include <net/sock.h>
1031da177e4SLinus Torvalds #include <net/ip_fib.h>
1041da177e4SLinus Torvalds #include <net/arp.h>
1051da177e4SLinus Torvalds #include <net/tcp.h>
1061da177e4SLinus Torvalds #include <net/icmp.h>
1071da177e4SLinus Torvalds #include <net/xfrm.h>
1088d71740cSTom Tucker #include <net/netevent.h>
10963f3444fSThomas Graf #include <net/rtnetlink.h>
1101da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1111da177e4SLinus Torvalds #include <linux/sysctl.h>
1127426a564SShan Wei #include <linux/kmemleak.h>
1131da177e4SLinus Torvalds #endif
1146e5714eaSDavid S. Miller #include <net/secure_seq.h>
1151da177e4SLinus Torvalds 
11668a5e3ddSDavid S. Miller #define RT_FL_TOS(oldflp4) \
117f61759e6SJulian Anastasov 	((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define IP_MAX_MTU	0xFFF0
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static int ip_rt_max_size;
124817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly	= RT_GC_TIMEOUT;
1259f28a2fcSEric Dumazet static int ip_rt_gc_interval __read_mostly  = 60 * HZ;
126817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly	= HZ / 2;
127817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly	= 9;
128817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly	= HZ / 50;
129817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly	= ((HZ / 50) << (9 + 1));
130817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly	= HZ;
131817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly	= 5 * HZ;
132817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly	= 8;
133817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly	= 10 * 60 * HZ;
134817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly		= 512 + 20 + 20;
135817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly	= 256;
1369f28a2fcSEric Dumazet 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  *	Interface to generic destination cache.
1391da177e4SLinus Torvalds  */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
1420dbaee3bSDavid S. Miller static unsigned int	 ipv4_default_advmss(const struct dst_entry *dst);
143ebb762f2SSteffen Klassert static unsigned int	 ipv4_mtu(const struct dst_entry *dst);
1441da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
1451da177e4SLinus Torvalds static void		 ipv4_link_failure(struct sk_buff *skb);
1466700c270SDavid S. Miller static void		 ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1476700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1486700c270SDavid S. Miller static void		 ip_do_redirect(struct dst_entry *dst, struct sock *sk,
1496700c270SDavid S. Miller 					struct sk_buff *skb);
1501da177e4SLinus Torvalds 
15172cdd1d9SEric Dumazet static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
15272cdd1d9SEric Dumazet 			    int how)
15372cdd1d9SEric Dumazet {
15472cdd1d9SEric Dumazet }
1551da177e4SLinus Torvalds 
15662fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
15762fa8a84SDavid S. Miller {
15831248731SDavid S. Miller 	WARN_ON(1);
15931248731SDavid S. Miller 	return NULL;
16062fa8a84SDavid S. Miller }
16162fa8a84SDavid S. Miller 
162f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
163f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
164f894cbf8SDavid S. Miller 					   const void *daddr);
165d3aaeb38SDavid S. Miller 
1661da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1671da177e4SLinus Torvalds 	.family =		AF_INET,
16809640e63SHarvey Harrison 	.protocol =		cpu_to_be16(ETH_P_IP),
1691da177e4SLinus Torvalds 	.check =		ipv4_dst_check,
1700dbaee3bSDavid S. Miller 	.default_advmss =	ipv4_default_advmss,
171ebb762f2SSteffen Klassert 	.mtu =			ipv4_mtu,
17262fa8a84SDavid S. Miller 	.cow_metrics =		ipv4_cow_metrics,
1731da177e4SLinus Torvalds 	.ifdown =		ipv4_dst_ifdown,
1741da177e4SLinus Torvalds 	.negative_advice =	ipv4_negative_advice,
1751da177e4SLinus Torvalds 	.link_failure =		ipv4_link_failure,
1761da177e4SLinus Torvalds 	.update_pmtu =		ip_rt_update_pmtu,
177e47a185bSDavid S. Miller 	.redirect =		ip_do_redirect,
1781ac06e03SHerbert Xu 	.local_out =		__ip_local_out,
179d3aaeb38SDavid S. Miller 	.neigh_lookup =		ipv4_neigh_lookup,
1801da177e4SLinus Torvalds };
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1831da177e4SLinus Torvalds 
1844839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1851da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1864a2b9c37SDan Siemon 	ECN_OR_COST(BESTEFFORT),
1871da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1881da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1891da177e4SLinus Torvalds 	TC_PRIO_BULK,
1901da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1911da177e4SLinus Torvalds 	TC_PRIO_BULK,
1921da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1931da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1941da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1951da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1961da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1971da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1981da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1991da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
2001da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
2011da177e4SLinus Torvalds };
202d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio);
2031da177e4SLinus Torvalds 
2042f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
20527f39c73SEric Dumazet #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field)
2061da177e4SLinus Torvalds 
207e84f84f2SDenis V. Lunev static inline int rt_genid(struct net *net)
208e84f84f2SDenis V. Lunev {
209e84f84f2SDenis V. Lunev 	return atomic_read(&net->ipv4.rt_genid);
210e84f84f2SDenis V. Lunev }
211e84f84f2SDenis V. Lunev 
2121da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
2131da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
2141da177e4SLinus Torvalds {
21529e75252SEric Dumazet 	if (*pos)
21689aef892SDavid S. Miller 		return NULL;
21729e75252SEric Dumazet 	return SEQ_START_TOKEN;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	++*pos;
22389aef892SDavid S. Miller 	return NULL;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2331da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
2341da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
2351da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
2361da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
2371da177e4SLinus Torvalds 	return 0;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
2411da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
2421da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
2431da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
2441da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
2451da177e4SLinus Torvalds };
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file)
2481da177e4SLinus Torvalds {
24989aef892SDavid S. Miller 	return seq_open(file, &rt_cache_seq_ops);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2529a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = {
2531da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
2541da177e4SLinus Torvalds 	.open	 = rt_cache_seq_open,
2551da177e4SLinus Torvalds 	.read	 = seq_read,
2561da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
25789aef892SDavid S. Miller 	.release = seq_release,
2581da177e4SLinus Torvalds };
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	int cpu;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (*pos == 0)
2661da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
2671da177e4SLinus Torvalds 
2680f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2691da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2701da177e4SLinus Torvalds 			continue;
2711da177e4SLinus Torvalds 		*pos = cpu+1;
2722f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 	return NULL;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	int cpu;
2801da177e4SLinus Torvalds 
2810f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2821da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2831da177e4SLinus Torvalds 			continue;
2841da177e4SLinus Torvalds 		*pos = cpu+1;
2852f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	return NULL;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
3015bec0039SOlaf 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");
3021da177e4SLinus Torvalds 		return 0;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	seq_printf(seq,"%08x  %08x %08x %08x %08x %08x %08x %08x "
3061da177e4SLinus Torvalds 		   " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
307fc66f95cSEric Dumazet 		   dst_entries_get_slow(&ipv4_dst_ops),
3081da177e4SLinus Torvalds 		   st->in_hit,
3091da177e4SLinus Torvalds 		   st->in_slow_tot,
3101da177e4SLinus Torvalds 		   st->in_slow_mc,
3111da177e4SLinus Torvalds 		   st->in_no_route,
3121da177e4SLinus Torvalds 		   st->in_brd,
3131da177e4SLinus Torvalds 		   st->in_martian_dst,
3141da177e4SLinus Torvalds 		   st->in_martian_src,
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		   st->out_hit,
3171da177e4SLinus Torvalds 		   st->out_slow_tot,
3181da177e4SLinus Torvalds 		   st->out_slow_mc,
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 		   st->gc_total,
3211da177e4SLinus Torvalds 		   st->gc_ignored,
3221da177e4SLinus Torvalds 		   st->gc_goal_miss,
3231da177e4SLinus Torvalds 		   st->gc_dst_overflow,
3241da177e4SLinus Torvalds 		   st->in_hlist_search,
3251da177e4SLinus Torvalds 		   st->out_hlist_search
3261da177e4SLinus Torvalds 		);
3271da177e4SLinus Torvalds 	return 0;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
330f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
3311da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
3321da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
3331da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
3341da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	return seq_open(file, &rt_cpu_seq_ops);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3439a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = {
3441da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
3451da177e4SLinus Torvalds 	.open	 = rt_cpu_seq_open,
3461da177e4SLinus Torvalds 	.read	 = seq_read,
3471da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
3481da177e4SLinus Torvalds 	.release = seq_release,
3491da177e4SLinus Torvalds };
3501da177e4SLinus Torvalds 
351c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
352a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v)
35378c686e9SPavel Emelyanov {
354a661c419SAlexey Dobriyan 	struct ip_rt_acct *dst, *src;
355a661c419SAlexey Dobriyan 	unsigned int i, j;
35678c686e9SPavel Emelyanov 
357a661c419SAlexey Dobriyan 	dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
358a661c419SAlexey Dobriyan 	if (!dst)
359a661c419SAlexey Dobriyan 		return -ENOMEM;
36078c686e9SPavel Emelyanov 
361a661c419SAlexey Dobriyan 	for_each_possible_cpu(i) {
362a661c419SAlexey Dobriyan 		src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
363a661c419SAlexey Dobriyan 		for (j = 0; j < 256; j++) {
364a661c419SAlexey Dobriyan 			dst[j].o_bytes   += src[j].o_bytes;
365a661c419SAlexey Dobriyan 			dst[j].o_packets += src[j].o_packets;
366a661c419SAlexey Dobriyan 			dst[j].i_bytes   += src[j].i_bytes;
367a661c419SAlexey Dobriyan 			dst[j].i_packets += src[j].i_packets;
368a661c419SAlexey Dobriyan 		}
369a661c419SAlexey Dobriyan 	}
370a661c419SAlexey Dobriyan 
371a661c419SAlexey Dobriyan 	seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
372a661c419SAlexey Dobriyan 	kfree(dst);
37378c686e9SPavel Emelyanov 	return 0;
37478c686e9SPavel Emelyanov }
37578c686e9SPavel Emelyanov 
376a661c419SAlexey Dobriyan static int rt_acct_proc_open(struct inode *inode, struct file *file)
377a661c419SAlexey Dobriyan {
378a661c419SAlexey Dobriyan 	return single_open(file, rt_acct_proc_show, NULL);
37978c686e9SPavel Emelyanov }
38078c686e9SPavel Emelyanov 
381a661c419SAlexey Dobriyan static const struct file_operations rt_acct_proc_fops = {
382a661c419SAlexey Dobriyan 	.owner		= THIS_MODULE,
383a661c419SAlexey Dobriyan 	.open		= rt_acct_proc_open,
384a661c419SAlexey Dobriyan 	.read		= seq_read,
385a661c419SAlexey Dobriyan 	.llseek		= seq_lseek,
386a661c419SAlexey Dobriyan 	.release	= single_release,
387a661c419SAlexey Dobriyan };
38878c686e9SPavel Emelyanov #endif
389107f1634SPavel Emelyanov 
39073b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
391107f1634SPavel Emelyanov {
392107f1634SPavel Emelyanov 	struct proc_dir_entry *pde;
393107f1634SPavel Emelyanov 
394107f1634SPavel Emelyanov 	pde = proc_net_fops_create(net, "rt_cache", S_IRUGO,
395107f1634SPavel Emelyanov 			&rt_cache_seq_fops);
396107f1634SPavel Emelyanov 	if (!pde)
397107f1634SPavel Emelyanov 		goto err1;
398107f1634SPavel Emelyanov 
39977020720SWang Chen 	pde = proc_create("rt_cache", S_IRUGO,
40077020720SWang Chen 			  net->proc_net_stat, &rt_cpu_seq_fops);
401107f1634SPavel Emelyanov 	if (!pde)
402107f1634SPavel Emelyanov 		goto err2;
403107f1634SPavel Emelyanov 
404c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
405a661c419SAlexey Dobriyan 	pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops);
406107f1634SPavel Emelyanov 	if (!pde)
407107f1634SPavel Emelyanov 		goto err3;
408107f1634SPavel Emelyanov #endif
409107f1634SPavel Emelyanov 	return 0;
410107f1634SPavel Emelyanov 
411c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
412107f1634SPavel Emelyanov err3:
413107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net_stat);
414107f1634SPavel Emelyanov #endif
415107f1634SPavel Emelyanov err2:
416107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net);
417107f1634SPavel Emelyanov err1:
418107f1634SPavel Emelyanov 	return -ENOMEM;
419107f1634SPavel Emelyanov }
42073b38711SDenis V. Lunev 
42173b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
42273b38711SDenis V. Lunev {
42373b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net_stat);
42473b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net);
425c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
42673b38711SDenis V. Lunev 	remove_proc_entry("rt_acct", net->proc_net);
4270a931acfSAlexey Dobriyan #endif
42873b38711SDenis V. Lunev }
42973b38711SDenis V. Lunev 
43073b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
43173b38711SDenis V. Lunev 	.init = ip_rt_do_proc_init,
43273b38711SDenis V. Lunev 	.exit = ip_rt_do_proc_exit,
43373b38711SDenis V. Lunev };
43473b38711SDenis V. Lunev 
43573b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
43673b38711SDenis V. Lunev {
43773b38711SDenis V. Lunev 	return register_pernet_subsys(&ip_rt_proc_ops);
43873b38711SDenis V. Lunev }
43973b38711SDenis V. Lunev 
440107f1634SPavel Emelyanov #else
44173b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
442107f1634SPavel Emelyanov {
443107f1634SPavel Emelyanov 	return 0;
444107f1634SPavel Emelyanov }
4451da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
4461da177e4SLinus Torvalds 
4474331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth)
448e84f84f2SDenis V. Lunev {
449d8d1f30bSChangli Gao 	return rth->rt_genid != rt_genid(dev_net(rth->dst.dev));
450e84f84f2SDenis V. Lunev }
451e84f84f2SDenis V. Lunev 
452beb659bdSEric Dumazet /*
45325985edcSLucas De Marchi  * Perturbation of rt_genid by a small quantity [1..256]
45429e75252SEric Dumazet  * Using 8 bits of shuffling ensure we can call rt_cache_invalidate()
45529e75252SEric Dumazet  * many times (2^24) without giving recent rt_genid.
45629e75252SEric Dumazet  * Jenkins hash is strong enough that litle changes of rt_genid are OK.
4571da177e4SLinus Torvalds  */
45886c657f6SDenis V. Lunev static void rt_cache_invalidate(struct net *net)
4591da177e4SLinus Torvalds {
46029e75252SEric Dumazet 	unsigned char shuffle;
4611da177e4SLinus Torvalds 
46229e75252SEric Dumazet 	get_random_bytes(&shuffle, sizeof(shuffle));
463e84f84f2SDenis V. Lunev 	atomic_add(shuffle + 1U, &net->ipv4.rt_genid);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
466beb659bdSEric Dumazet /*
46729e75252SEric Dumazet  * delay < 0  : invalidate cache (fast : entries will be deleted later)
46829e75252SEric Dumazet  * delay >= 0 : invalidate & flush cache (can be long)
46929e75252SEric Dumazet  */
47076e6ebfbSDenis V. Lunev void rt_cache_flush(struct net *net, int delay)
47129e75252SEric Dumazet {
47286c657f6SDenis V. Lunev 	rt_cache_invalidate(net);
47398376387SEric Dumazet }
47498376387SEric Dumazet 
475f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
476f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
477f894cbf8SDavid S. Miller 					   const void *daddr)
4783769cffbSDavid Miller {
479d3aaeb38SDavid S. Miller 	struct net_device *dev = dst->dev;
480d3aaeb38SDavid S. Miller 	const __be32 *pkey = daddr;
48139232973SDavid S. Miller 	const struct rtable *rt;
4823769cffbSDavid Miller 	struct neighbour *n;
4833769cffbSDavid Miller 
48439232973SDavid S. Miller 	rt = (const struct rtable *) dst;
485a263b309SDavid S. Miller 	if (rt->rt_gateway)
48639232973SDavid S. Miller 		pkey = (const __be32 *) &rt->rt_gateway;
487f894cbf8SDavid S. Miller 	else if (skb)
488f894cbf8SDavid S. Miller 		pkey = &ip_hdr(skb)->daddr;
489d3aaeb38SDavid S. Miller 
49080703d26SDavid S. Miller 	n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey);
491d3aaeb38SDavid S. Miller 	if (n)
492d3aaeb38SDavid S. Miller 		return n;
49332092ecfSDavid Miller 	return neigh_create(&arp_tbl, pkey, dev);
494d3aaeb38SDavid S. Miller }
495d3aaeb38SDavid S. Miller 
4961da177e4SLinus Torvalds /*
4971da177e4SLinus Torvalds  * Peer allocation may fail only in serious out-of-memory conditions.  However
4981da177e4SLinus Torvalds  * we still can generate some output.
4991da177e4SLinus Torvalds  * Random ID selection looks a bit dangerous because we have no chances to
5001da177e4SLinus Torvalds  * select ID being unique in a reasonable period of time.
5011da177e4SLinus Torvalds  * But broken packet identifier may be better than no packet at all.
5021da177e4SLinus Torvalds  */
5031da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(ip_fb_id_lock);
5061da177e4SLinus Torvalds 	static u32 ip_fallback_id;
5071da177e4SLinus Torvalds 	u32 salt;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	spin_lock_bh(&ip_fb_id_lock);
510e448515cSAl Viro 	salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
5111da177e4SLinus Torvalds 	iph->id = htons(salt & 0xFFFF);
5121da177e4SLinus Torvalds 	ip_fallback_id = salt;
5131da177e4SLinus Torvalds 	spin_unlock_bh(&ip_fb_id_lock);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
5171da177e4SLinus Torvalds {
5181d861aa4SDavid S. Miller 	struct net *net = dev_net(dst->dev);
5191d861aa4SDavid S. Miller 	struct inet_peer *peer;
5201da177e4SLinus Torvalds 
5211d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, iph->daddr, 1);
522fbfe95a4SDavid S. Miller 	if (peer) {
523fbfe95a4SDavid S. Miller 		iph->id = htons(inet_getid(peer, more));
5241d861aa4SDavid S. Miller 		inet_putpeer(peer);
5251da177e4SLinus Torvalds 		return;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	ip_select_fb_ident(iph);
5291da177e4SLinus Torvalds }
5304bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident);
5311da177e4SLinus Torvalds 
5325abf7f7eSEric Dumazet static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5334895c771SDavid S. Miller 			     const struct iphdr *iph,
5344895c771SDavid S. Miller 			     int oif, u8 tos,
5354895c771SDavid S. Miller 			     u8 prot, u32 mark, int flow_flags)
5364895c771SDavid S. Miller {
5374895c771SDavid S. Miller 	if (sk) {
5384895c771SDavid S. Miller 		const struct inet_sock *inet = inet_sk(sk);
5394895c771SDavid S. Miller 
5404895c771SDavid S. Miller 		oif = sk->sk_bound_dev_if;
5414895c771SDavid S. Miller 		mark = sk->sk_mark;
5424895c771SDavid S. Miller 		tos = RT_CONN_FLAGS(sk);
5434895c771SDavid S. Miller 		prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol;
5444895c771SDavid S. Miller 	}
5454895c771SDavid S. Miller 	flowi4_init_output(fl4, oif, mark, tos,
5464895c771SDavid S. Miller 			   RT_SCOPE_UNIVERSE, prot,
5474895c771SDavid S. Miller 			   flow_flags,
5484895c771SDavid S. Miller 			   iph->daddr, iph->saddr, 0, 0);
5494895c771SDavid S. Miller }
5504895c771SDavid S. Miller 
5515abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
5525abf7f7eSEric Dumazet 			       const struct sock *sk)
5534895c771SDavid S. Miller {
5544895c771SDavid S. Miller 	const struct iphdr *iph = ip_hdr(skb);
5554895c771SDavid S. Miller 	int oif = skb->dev->ifindex;
5564895c771SDavid S. Miller 	u8 tos = RT_TOS(iph->tos);
5574895c771SDavid S. Miller 	u8 prot = iph->protocol;
5584895c771SDavid S. Miller 	u32 mark = skb->mark;
5594895c771SDavid S. Miller 
5604895c771SDavid S. Miller 	__build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0);
5614895c771SDavid S. Miller }
5624895c771SDavid S. Miller 
5635abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
5644895c771SDavid S. Miller {
5654895c771SDavid S. Miller 	const struct inet_sock *inet = inet_sk(sk);
5665abf7f7eSEric Dumazet 	const struct ip_options_rcu *inet_opt;
5674895c771SDavid S. Miller 	__be32 daddr = inet->inet_daddr;
5684895c771SDavid S. Miller 
5694895c771SDavid S. Miller 	rcu_read_lock();
5704895c771SDavid S. Miller 	inet_opt = rcu_dereference(inet->inet_opt);
5714895c771SDavid S. Miller 	if (inet_opt && inet_opt->opt.srr)
5724895c771SDavid S. Miller 		daddr = inet_opt->opt.faddr;
5734895c771SDavid S. Miller 	flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
5744895c771SDavid S. Miller 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
5754895c771SDavid S. Miller 			   inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
5764895c771SDavid S. Miller 			   inet_sk_flowi_flags(sk),
5774895c771SDavid S. Miller 			   daddr, inet->inet_saddr, 0, 0);
5784895c771SDavid S. Miller 	rcu_read_unlock();
5794895c771SDavid S. Miller }
5804895c771SDavid S. Miller 
5815abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5825abf7f7eSEric Dumazet 				 const struct sk_buff *skb)
5834895c771SDavid S. Miller {
5844895c771SDavid S. Miller 	if (skb)
5854895c771SDavid S. Miller 		build_skb_flow_key(fl4, skb, sk);
5864895c771SDavid S. Miller 	else
5874895c771SDavid S. Miller 		build_sk_flow_key(fl4, sk);
5884895c771SDavid S. Miller }
5894895c771SDavid S. Miller 
590aee06da6SJulian Anastasov static DEFINE_SEQLOCK(fnhe_seqlock);
5914895c771SDavid S. Miller 
592aee06da6SJulian Anastasov static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
5934895c771SDavid S. Miller {
5944895c771SDavid S. Miller 	struct fib_nh_exception *fnhe, *oldest;
5954895c771SDavid S. Miller 
5964895c771SDavid S. Miller 	oldest = rcu_dereference(hash->chain);
5974895c771SDavid S. Miller 	for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe;
5984895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
5994895c771SDavid S. Miller 		if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp))
6004895c771SDavid S. Miller 			oldest = fnhe;
6014895c771SDavid S. Miller 	}
6024895c771SDavid S. Miller 	return oldest;
6034895c771SDavid S. Miller }
6044895c771SDavid S. Miller 
605d3a25c98SDavid S. Miller static inline u32 fnhe_hashfun(__be32 daddr)
606d3a25c98SDavid S. Miller {
607d3a25c98SDavid S. Miller 	u32 hval;
608d3a25c98SDavid S. Miller 
609d3a25c98SDavid S. Miller 	hval = (__force u32) daddr;
610d3a25c98SDavid S. Miller 	hval ^= (hval >> 11) ^ (hval >> 22);
611d3a25c98SDavid S. Miller 
612d3a25c98SDavid S. Miller 	return hval & (FNHE_HASH_SIZE - 1);
613d3a25c98SDavid S. Miller }
614d3a25c98SDavid S. Miller 
615aee06da6SJulian Anastasov static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw,
616aee06da6SJulian Anastasov 				  u32 pmtu, unsigned long expires)
6174895c771SDavid S. Miller {
618aee06da6SJulian Anastasov 	struct fnhe_hash_bucket *hash;
6194895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
6204895c771SDavid S. Miller 	int depth;
621aee06da6SJulian Anastasov 	u32 hval = fnhe_hashfun(daddr);
6224895c771SDavid S. Miller 
623aee06da6SJulian Anastasov 	write_seqlock_bh(&fnhe_seqlock);
624aee06da6SJulian Anastasov 
625aee06da6SJulian Anastasov 	hash = nh->nh_exceptions;
6264895c771SDavid S. Miller 	if (!hash) {
627aee06da6SJulian Anastasov 		hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC);
6284895c771SDavid S. Miller 		if (!hash)
629aee06da6SJulian Anastasov 			goto out_unlock;
630aee06da6SJulian Anastasov 		nh->nh_exceptions = hash;
6314895c771SDavid S. Miller 	}
6324895c771SDavid S. Miller 
6334895c771SDavid S. Miller 	hash += hval;
6344895c771SDavid S. Miller 
6354895c771SDavid S. Miller 	depth = 0;
6364895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash->chain); fnhe;
6374895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
6384895c771SDavid S. Miller 		if (fnhe->fnhe_daddr == daddr)
639aee06da6SJulian Anastasov 			break;
6404895c771SDavid S. Miller 		depth++;
6414895c771SDavid S. Miller 	}
6424895c771SDavid S. Miller 
643aee06da6SJulian Anastasov 	if (fnhe) {
644aee06da6SJulian Anastasov 		if (gw)
645aee06da6SJulian Anastasov 			fnhe->fnhe_gw = gw;
646aee06da6SJulian Anastasov 		if (pmtu) {
647aee06da6SJulian Anastasov 			fnhe->fnhe_pmtu = pmtu;
648aee06da6SJulian Anastasov 			fnhe->fnhe_expires = expires;
6494895c771SDavid S. Miller 		}
650aee06da6SJulian Anastasov 	} else {
651aee06da6SJulian Anastasov 		if (depth > FNHE_RECLAIM_DEPTH)
652aee06da6SJulian Anastasov 			fnhe = fnhe_oldest(hash);
653aee06da6SJulian Anastasov 		else {
6544895c771SDavid S. Miller 			fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
6554895c771SDavid S. Miller 			if (!fnhe)
656aee06da6SJulian Anastasov 				goto out_unlock;
6574895c771SDavid S. Miller 
6584895c771SDavid S. Miller 			fnhe->fnhe_next = hash->chain;
6594895c771SDavid S. Miller 			rcu_assign_pointer(hash->chain, fnhe);
660aee06da6SJulian Anastasov 		}
6614895c771SDavid S. Miller 		fnhe->fnhe_daddr = daddr;
662aee06da6SJulian Anastasov 		fnhe->fnhe_gw = gw;
663aee06da6SJulian Anastasov 		fnhe->fnhe_pmtu = pmtu;
664aee06da6SJulian Anastasov 		fnhe->fnhe_expires = expires;
665aee06da6SJulian Anastasov 	}
666aee06da6SJulian Anastasov 
6674895c771SDavid S. Miller 	fnhe->fnhe_stamp = jiffies;
668aee06da6SJulian Anastasov 
669aee06da6SJulian Anastasov out_unlock:
670aee06da6SJulian Anastasov 	write_sequnlock_bh(&fnhe_seqlock);
671aee06da6SJulian Anastasov 	return;
6724895c771SDavid S. Miller }
6734895c771SDavid S. Miller 
674ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
675ceb33206SDavid S. Miller 			     bool kill_route)
6761da177e4SLinus Torvalds {
677e47a185bSDavid S. Miller 	__be32 new_gw = icmp_hdr(skb)->un.gateway;
67894206125SDavid S. Miller 	__be32 old_gw = ip_hdr(skb)->saddr;
679e47a185bSDavid S. Miller 	struct net_device *dev = skb->dev;
680e47a185bSDavid S. Miller 	struct in_device *in_dev;
6814895c771SDavid S. Miller 	struct fib_result res;
682e47a185bSDavid S. Miller 	struct neighbour *n;
683317805b8SDenis V. Lunev 	struct net *net;
6841da177e4SLinus Torvalds 
68594206125SDavid S. Miller 	switch (icmp_hdr(skb)->code & 7) {
68694206125SDavid S. Miller 	case ICMP_REDIR_NET:
68794206125SDavid S. Miller 	case ICMP_REDIR_NETTOS:
68894206125SDavid S. Miller 	case ICMP_REDIR_HOST:
68994206125SDavid S. Miller 	case ICMP_REDIR_HOSTTOS:
69094206125SDavid S. Miller 		break;
69194206125SDavid S. Miller 
69294206125SDavid S. Miller 	default:
69394206125SDavid S. Miller 		return;
69494206125SDavid S. Miller 	}
69594206125SDavid S. Miller 
696e47a185bSDavid S. Miller 	if (rt->rt_gateway != old_gw)
697e47a185bSDavid S. Miller 		return;
698e47a185bSDavid S. Miller 
699e47a185bSDavid S. Miller 	in_dev = __in_dev_get_rcu(dev);
700e47a185bSDavid S. Miller 	if (!in_dev)
701e47a185bSDavid S. Miller 		return;
702e47a185bSDavid S. Miller 
703c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
7049d4fb27dSJoe Perches 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
7059d4fb27dSJoe Perches 	    ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
7069d4fb27dSJoe Perches 	    ipv4_is_zeronet(new_gw))
7071da177e4SLinus Torvalds 		goto reject_redirect;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
7101da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
7111da177e4SLinus Torvalds 			goto reject_redirect;
7121da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
7131da177e4SLinus Torvalds 			goto reject_redirect;
7141da177e4SLinus Torvalds 	} else {
715317805b8SDenis V. Lunev 		if (inet_addr_type(net, new_gw) != RTN_UNICAST)
7161da177e4SLinus Torvalds 			goto reject_redirect;
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
7194895c771SDavid S. Miller 	n = ipv4_neigh_lookup(&rt->dst, NULL, &new_gw);
720e47a185bSDavid S. Miller 	if (n) {
721e47a185bSDavid S. Miller 		if (!(n->nud_state & NUD_VALID)) {
722e47a185bSDavid S. Miller 			neigh_event_send(n, NULL);
723e47a185bSDavid S. Miller 		} else {
7244895c771SDavid S. Miller 			if (fib_lookup(net, fl4, &res) == 0) {
7254895c771SDavid S. Miller 				struct fib_nh *nh = &FIB_RES_NH(res);
7264895c771SDavid S. Miller 
727aee06da6SJulian Anastasov 				update_or_create_fnhe(nh, fl4->daddr, new_gw,
728aee06da6SJulian Anastasov 						      0, 0);
7294895c771SDavid S. Miller 			}
730ceb33206SDavid S. Miller 			if (kill_route)
731ceb33206SDavid S. Miller 				rt->dst.obsolete = DST_OBSOLETE_KILL;
732e47a185bSDavid S. Miller 			call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
733e47a185bSDavid S. Miller 		}
734e47a185bSDavid S. Miller 		neigh_release(n);
735e47a185bSDavid S. Miller 	}
736e47a185bSDavid S. Miller 	return;
737e47a185bSDavid S. Miller 
738e47a185bSDavid S. Miller reject_redirect:
739e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE
74099ee038dSDavid S. Miller 	if (IN_DEV_LOG_MARTIANS(in_dev)) {
74199ee038dSDavid S. Miller 		const struct iphdr *iph = (const struct iphdr *) skb->data;
74299ee038dSDavid S. Miller 		__be32 daddr = iph->daddr;
74399ee038dSDavid S. Miller 		__be32 saddr = iph->saddr;
74499ee038dSDavid S. Miller 
745e47a185bSDavid S. Miller 		net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
746e47a185bSDavid S. Miller 				     "  Advised path = %pI4 -> %pI4\n",
747e47a185bSDavid S. Miller 				     &old_gw, dev->name, &new_gw,
748e47a185bSDavid S. Miller 				     &saddr, &daddr);
74999ee038dSDavid S. Miller 	}
750e47a185bSDavid S. Miller #endif
751e47a185bSDavid S. Miller 	;
752e47a185bSDavid S. Miller }
753e47a185bSDavid S. Miller 
7544895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
7554895c771SDavid S. Miller {
7564895c771SDavid S. Miller 	struct rtable *rt;
7574895c771SDavid S. Miller 	struct flowi4 fl4;
7584895c771SDavid S. Miller 
7594895c771SDavid S. Miller 	rt = (struct rtable *) dst;
7604895c771SDavid S. Miller 
7614895c771SDavid S. Miller 	ip_rt_build_flow_key(&fl4, sk, skb);
762ceb33206SDavid S. Miller 	__ip_do_redirect(rt, skb, &fl4, true);
7634895c771SDavid S. Miller }
7644895c771SDavid S. Miller 
7651da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)dst;
7681da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (rt) {
771d11a4dc1STimo Teräs 		if (dst->obsolete > 0) {
7721da177e4SLinus Torvalds 			ip_rt_put(rt);
7731da177e4SLinus Torvalds 			ret = NULL;
7745943634fSDavid S. Miller 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
7755943634fSDavid S. Miller 			   rt->dst.expires) {
77689aef892SDavid S. Miller 			ip_rt_put(rt);
7771da177e4SLinus Torvalds 			ret = NULL;
7781da177e4SLinus Torvalds 		}
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	return ret;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /*
7841da177e4SLinus Torvalds  * Algorithm:
7851da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
7861da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
7871da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
7881da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
7891da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
7901da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
7911da177e4SLinus Torvalds  *
7921da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
7931da177e4SLinus Torvalds  * in icmp.c.
7941da177e4SLinus Torvalds  *
7951da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
7961da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
7971da177e4SLinus Torvalds  */
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
8001da177e4SLinus Torvalds {
801511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
80230038fc6SEric Dumazet 	struct in_device *in_dev;
80392d86829SDavid S. Miller 	struct inet_peer *peer;
8041d861aa4SDavid S. Miller 	struct net *net;
80530038fc6SEric Dumazet 	int log_martians;
8061da177e4SLinus Torvalds 
80730038fc6SEric Dumazet 	rcu_read_lock();
808d8d1f30bSChangli Gao 	in_dev = __in_dev_get_rcu(rt->dst.dev);
80930038fc6SEric Dumazet 	if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
81030038fc6SEric Dumazet 		rcu_read_unlock();
8111da177e4SLinus Torvalds 		return;
81230038fc6SEric Dumazet 	}
81330038fc6SEric Dumazet 	log_martians = IN_DEV_LOG_MARTIANS(in_dev);
81430038fc6SEric Dumazet 	rcu_read_unlock();
8151da177e4SLinus Torvalds 
8161d861aa4SDavid S. Miller 	net = dev_net(rt->dst.dev);
8171d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
81892d86829SDavid S. Miller 	if (!peer) {
81992d86829SDavid S. Miller 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
82092d86829SDavid S. Miller 		return;
82192d86829SDavid S. Miller 	}
82292d86829SDavid S. Miller 
8231da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
8241da177e4SLinus Torvalds 	 * reset the algorithm.
8251da177e4SLinus Torvalds 	 */
82692d86829SDavid S. Miller 	if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence))
82792d86829SDavid S. Miller 		peer->rate_tokens = 0;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
830d8d1f30bSChangli Gao 	 * set dst.rate_last to the last seen redirected packet.
8311da177e4SLinus Torvalds 	 */
83292d86829SDavid S. Miller 	if (peer->rate_tokens >= ip_rt_redirect_number) {
83392d86829SDavid S. Miller 		peer->rate_last = jiffies;
8341d861aa4SDavid S. Miller 		goto out_put_peer;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
8381da177e4SLinus Torvalds 	 * redirect.
8391da177e4SLinus Torvalds 	 */
84092d86829SDavid S. Miller 	if (peer->rate_tokens == 0 ||
84114fb8a76SLi Yewang 	    time_after(jiffies,
84292d86829SDavid S. Miller 		       (peer->rate_last +
84392d86829SDavid S. Miller 			(ip_rt_redirect_load << peer->rate_tokens)))) {
8441da177e4SLinus Torvalds 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
84592d86829SDavid S. Miller 		peer->rate_last = jiffies;
84692d86829SDavid S. Miller 		++peer->rate_tokens;
8471da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
84830038fc6SEric Dumazet 		if (log_martians &&
849e87cc472SJoe Perches 		    peer->rate_tokens == ip_rt_redirect_number)
850e87cc472SJoe Perches 			net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
85192101b3bSDavid S. Miller 					     &ip_hdr(skb)->saddr, inet_iif(skb),
852f1ce3062SDavid S. Miller 					     &ip_hdr(skb)->daddr, &rt->rt_gateway);
8531da177e4SLinus Torvalds #endif
8541da177e4SLinus Torvalds 	}
8551d861aa4SDavid S. Miller out_put_peer:
8561d861aa4SDavid S. Miller 	inet_putpeer(peer);
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
8601da177e4SLinus Torvalds {
861251da413SDavid S. Miller 	struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
862511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
86392d86829SDavid S. Miller 	struct inet_peer *peer;
8641da177e4SLinus Torvalds 	unsigned long now;
865251da413SDavid S. Miller 	struct net *net;
86692d86829SDavid S. Miller 	bool send;
8671da177e4SLinus Torvalds 	int code;
8681da177e4SLinus Torvalds 
869251da413SDavid S. Miller 	net = dev_net(rt->dst.dev);
870251da413SDavid S. Miller 	if (!IN_DEV_FORWARD(in_dev)) {
871251da413SDavid S. Miller 		switch (rt->dst.error) {
872251da413SDavid S. Miller 		case EHOSTUNREACH:
873251da413SDavid S. Miller 			IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS);
874251da413SDavid S. Miller 			break;
875251da413SDavid S. Miller 
876251da413SDavid S. Miller 		case ENETUNREACH:
877251da413SDavid S. Miller 			IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
878251da413SDavid S. Miller 			break;
879251da413SDavid S. Miller 		}
880251da413SDavid S. Miller 		goto out;
881251da413SDavid S. Miller 	}
882251da413SDavid S. Miller 
883d8d1f30bSChangli Gao 	switch (rt->dst.error) {
8841da177e4SLinus Torvalds 	case EINVAL:
8851da177e4SLinus Torvalds 	default:
8861da177e4SLinus Torvalds 		goto out;
8871da177e4SLinus Torvalds 	case EHOSTUNREACH:
8881da177e4SLinus Torvalds 		code = ICMP_HOST_UNREACH;
8891da177e4SLinus Torvalds 		break;
8901da177e4SLinus Torvalds 	case ENETUNREACH:
8911da177e4SLinus Torvalds 		code = ICMP_NET_UNREACH;
892251da413SDavid S. Miller 		IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
8931da177e4SLinus Torvalds 		break;
8941da177e4SLinus Torvalds 	case EACCES:
8951da177e4SLinus Torvalds 		code = ICMP_PKT_FILTERED;
8961da177e4SLinus Torvalds 		break;
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 
8991d861aa4SDavid S. Miller 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
90092d86829SDavid S. Miller 
90192d86829SDavid S. Miller 	send = true;
90292d86829SDavid S. Miller 	if (peer) {
9031da177e4SLinus Torvalds 		now = jiffies;
90492d86829SDavid S. Miller 		peer->rate_tokens += now - peer->rate_last;
90592d86829SDavid S. Miller 		if (peer->rate_tokens > ip_rt_error_burst)
90692d86829SDavid S. Miller 			peer->rate_tokens = ip_rt_error_burst;
90792d86829SDavid S. Miller 		peer->rate_last = now;
90892d86829SDavid S. Miller 		if (peer->rate_tokens >= ip_rt_error_cost)
90992d86829SDavid S. Miller 			peer->rate_tokens -= ip_rt_error_cost;
91092d86829SDavid S. Miller 		else
91192d86829SDavid S. Miller 			send = false;
9121d861aa4SDavid S. Miller 		inet_putpeer(peer);
9131da177e4SLinus Torvalds 	}
91492d86829SDavid S. Miller 	if (send)
91592d86829SDavid S. Miller 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds out:	kfree_skb(skb);
9181da177e4SLinus Torvalds 	return 0;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
921ceb33206SDavid S. Miller static u32 __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
9221da177e4SLinus Torvalds {
9234895c771SDavid S. Miller 	struct fib_result res;
9242c8cec5cSDavid S. Miller 
9252c8cec5cSDavid S. Miller 	if (mtu < ip_rt_min_pmtu)
9261da177e4SLinus Torvalds 		mtu = ip_rt_min_pmtu;
92746af3180SHiroaki SHIMODA 
9284895c771SDavid S. Miller 	if (fib_lookup(dev_net(rt->dst.dev), fl4, &res) == 0) {
9294895c771SDavid S. Miller 		struct fib_nh *nh = &FIB_RES_NH(res);
9304895c771SDavid S. Miller 
931aee06da6SJulian Anastasov 		update_or_create_fnhe(nh, fl4->daddr, 0, mtu,
932aee06da6SJulian Anastasov 				      jiffies + ip_rt_mtu_expires);
9334895c771SDavid S. Miller 	}
934ceb33206SDavid S. Miller 	return mtu;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9374895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
9384895c771SDavid S. Miller 			      struct sk_buff *skb, u32 mtu)
9394895c771SDavid S. Miller {
9404895c771SDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
9414895c771SDavid S. Miller 	struct flowi4 fl4;
9424895c771SDavid S. Miller 
9434895c771SDavid S. Miller 	ip_rt_build_flow_key(&fl4, sk, skb);
944ceb33206SDavid S. Miller 	mtu = __ip_rt_update_pmtu(rt, &fl4, mtu);
945ceb33206SDavid S. Miller 
946ceb33206SDavid S. Miller 	if (!rt->rt_pmtu) {
947ceb33206SDavid S. Miller 		dst->obsolete = DST_OBSOLETE_KILL;
948ceb33206SDavid S. Miller 	} else {
949ceb33206SDavid S. Miller 		rt->rt_pmtu = mtu;
950ceb33206SDavid S. Miller 		dst_set_expires(&rt->dst, ip_rt_mtu_expires);
951ceb33206SDavid S. Miller 	}
9524895c771SDavid S. Miller }
9534895c771SDavid S. Miller 
95436393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
95536393395SDavid S. Miller 		      int oif, u32 mark, u8 protocol, int flow_flags)
95636393395SDavid S. Miller {
95736393395SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
95836393395SDavid S. Miller 	struct flowi4 fl4;
95936393395SDavid S. Miller 	struct rtable *rt;
96036393395SDavid S. Miller 
9614895c771SDavid S. Miller 	__build_flow_key(&fl4, NULL, iph, oif,
9624895c771SDavid S. Miller 			 RT_TOS(iph->tos), protocol, mark, flow_flags);
96336393395SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
96436393395SDavid S. Miller 	if (!IS_ERR(rt)) {
9654895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
96636393395SDavid S. Miller 		ip_rt_put(rt);
96736393395SDavid S. Miller 	}
96836393395SDavid S. Miller }
96936393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
97036393395SDavid S. Miller 
97136393395SDavid S. Miller void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
97236393395SDavid S. Miller {
9734895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
9744895c771SDavid S. Miller 	struct flowi4 fl4;
9754895c771SDavid S. Miller 	struct rtable *rt;
97636393395SDavid S. Miller 
9774895c771SDavid S. Miller 	__build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
9784895c771SDavid S. Miller 	rt = __ip_route_output_key(sock_net(sk), &fl4);
9794895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
9804895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
9814895c771SDavid S. Miller 		ip_rt_put(rt);
9824895c771SDavid S. Miller 	}
98336393395SDavid S. Miller }
98436393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
985f39925dbSDavid S. Miller 
986b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net,
987b42597e2SDavid S. Miller 		   int oif, u32 mark, u8 protocol, int flow_flags)
988b42597e2SDavid S. Miller {
989b42597e2SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
990b42597e2SDavid S. Miller 	struct flowi4 fl4;
991b42597e2SDavid S. Miller 	struct rtable *rt;
992b42597e2SDavid S. Miller 
9934895c771SDavid S. Miller 	__build_flow_key(&fl4, NULL, iph, oif,
9944895c771SDavid S. Miller 			 RT_TOS(iph->tos), protocol, mark, flow_flags);
995b42597e2SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
996b42597e2SDavid S. Miller 	if (!IS_ERR(rt)) {
997ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
998b42597e2SDavid S. Miller 		ip_rt_put(rt);
999b42597e2SDavid S. Miller 	}
1000b42597e2SDavid S. Miller }
1001b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect);
1002b42597e2SDavid S. Miller 
1003b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
1004b42597e2SDavid S. Miller {
10054895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *) skb->data;
10064895c771SDavid S. Miller 	struct flowi4 fl4;
10074895c771SDavid S. Miller 	struct rtable *rt;
1008b42597e2SDavid S. Miller 
10094895c771SDavid S. Miller 	__build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
10104895c771SDavid S. Miller 	rt = __ip_route_output_key(sock_net(sk), &fl4);
10114895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
1012ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
10134895c771SDavid S. Miller 		ip_rt_put(rt);
10144895c771SDavid S. Miller 	}
1015b42597e2SDavid S. Miller }
1016b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
1017b42597e2SDavid S. Miller 
1018efbc368dSDavid S. Miller static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
1019efbc368dSDavid S. Miller {
1020efbc368dSDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
1021efbc368dSDavid S. Miller 
1022ceb33206SDavid S. Miller 	/* All IPV4 dsts are created with ->obsolete set to the value
1023ceb33206SDavid S. Miller 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1024ceb33206SDavid S. Miller 	 * into this function always.
1025ceb33206SDavid S. Miller 	 *
1026ceb33206SDavid S. Miller 	 * When a PMTU/redirect information update invalidates a
1027ceb33206SDavid S. Miller 	 * route, this is indicated by setting obsolete to
1028ceb33206SDavid S. Miller 	 * DST_OBSOLETE_KILL.
1029ceb33206SDavid S. Miller 	 */
1030ceb33206SDavid S. Miller 	if (dst->obsolete == DST_OBSOLETE_KILL || rt_is_expired(rt))
1031efbc368dSDavid S. Miller 		return NULL;
1032d11a4dc1STimo Teräs 	return dst;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct rtable *rt;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
10401da177e4SLinus Torvalds 
1041511c3f92SEric Dumazet 	rt = skb_rtable(skb);
10425943634fSDavid S. Miller 	if (rt)
10435943634fSDavid S. Miller 		dst_set_expires(&rt->dst, 0);
10442c8cec5cSDavid S. Miller }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb)
10471da177e4SLinus Torvalds {
104891df42beSJoe Perches 	pr_debug("%s: %pI4 -> %pI4, %s\n",
104991df42beSJoe Perches 		 __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
10501da177e4SLinus Torvalds 		 skb->dev ? skb->dev->name : "?");
10511da177e4SLinus Torvalds 	kfree_skb(skb);
1052c378a9c0SDave Jones 	WARN_ON(1);
10531da177e4SLinus Torvalds 	return 0;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds /*
10571da177e4SLinus Torvalds    We do not cache source address of outgoing interface,
10581da177e4SLinus Torvalds    because it is used only by IP RR, TS and SRR options,
10591da177e4SLinus Torvalds    so that it out of fast path.
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds    BTW remember: "addr" is allowed to be not aligned
10621da177e4SLinus Torvalds    in IP options!
10631da177e4SLinus Torvalds  */
10641da177e4SLinus Torvalds 
10658e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
10661da177e4SLinus Torvalds {
1067a61ced5dSAl Viro 	__be32 src;
10681da177e4SLinus Torvalds 
1069c7537967SDavid S. Miller 	if (rt_is_output_route(rt))
1070c5be24ffSDavid S. Miller 		src = ip_hdr(skb)->saddr;
1071ebc0ffaeSEric Dumazet 	else {
10728e36360aSDavid S. Miller 		struct fib_result res;
10738e36360aSDavid S. Miller 		struct flowi4 fl4;
10748e36360aSDavid S. Miller 		struct iphdr *iph;
10758e36360aSDavid S. Miller 
10768e36360aSDavid S. Miller 		iph = ip_hdr(skb);
10778e36360aSDavid S. Miller 
10788e36360aSDavid S. Miller 		memset(&fl4, 0, sizeof(fl4));
10798e36360aSDavid S. Miller 		fl4.daddr = iph->daddr;
10808e36360aSDavid S. Miller 		fl4.saddr = iph->saddr;
1081b0fe4a31SJulian Anastasov 		fl4.flowi4_tos = RT_TOS(iph->tos);
10828e36360aSDavid S. Miller 		fl4.flowi4_oif = rt->dst.dev->ifindex;
10838e36360aSDavid S. Miller 		fl4.flowi4_iif = skb->dev->ifindex;
10848e36360aSDavid S. Miller 		fl4.flowi4_mark = skb->mark;
10855e2b61f7SDavid S. Miller 
1086ebc0ffaeSEric Dumazet 		rcu_read_lock();
108768a5e3ddSDavid S. Miller 		if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res) == 0)
1088436c3b66SDavid S. Miller 			src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res);
1089ebc0ffaeSEric Dumazet 		else
1090f8126f1dSDavid S. Miller 			src = inet_select_addr(rt->dst.dev,
1091f8126f1dSDavid S. Miller 					       rt_nexthop(rt, iph->daddr),
10921da177e4SLinus Torvalds 					       RT_SCOPE_UNIVERSE);
1093ebc0ffaeSEric Dumazet 		rcu_read_unlock();
1094ebc0ffaeSEric Dumazet 	}
10951da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
1098c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
10991da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
11001da177e4SLinus Torvalds {
1101d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF))
1102d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF;
1103d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF0000))
1104d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF0000;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds #endif
11071da177e4SLinus Torvalds 
11080dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
11090dbaee3bSDavid S. Miller {
11100dbaee3bSDavid S. Miller 	unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS);
11110dbaee3bSDavid S. Miller 
11120dbaee3bSDavid S. Miller 	if (advmss == 0) {
11130dbaee3bSDavid S. Miller 		advmss = max_t(unsigned int, dst->dev->mtu - 40,
11140dbaee3bSDavid S. Miller 			       ip_rt_min_advmss);
11150dbaee3bSDavid S. Miller 		if (advmss > 65535 - 40)
11160dbaee3bSDavid S. Miller 			advmss = 65535 - 40;
11170dbaee3bSDavid S. Miller 	}
11180dbaee3bSDavid S. Miller 	return advmss;
11190dbaee3bSDavid S. Miller }
11200dbaee3bSDavid S. Miller 
1121ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst)
1122d33e4553SDavid S. Miller {
1123261663b0SSteffen Klassert 	const struct rtable *rt = (const struct rtable *) dst;
11245943634fSDavid S. Miller 	unsigned int mtu = rt->rt_pmtu;
11255943634fSDavid S. Miller 
11265943634fSDavid S. Miller 	if (mtu && time_after_eq(jiffies, rt->dst.expires))
11275943634fSDavid S. Miller 		mtu = 0;
11285943634fSDavid S. Miller 
11295943634fSDavid S. Miller 	if (!mtu)
11305943634fSDavid S. Miller 		mtu = dst_metric_raw(dst, RTAX_MTU);
1131618f9bc7SSteffen Klassert 
1132261663b0SSteffen Klassert 	if (mtu && rt_is_output_route(rt))
1133618f9bc7SSteffen Klassert 		return mtu;
1134618f9bc7SSteffen Klassert 
1135618f9bc7SSteffen Klassert 	mtu = dst->dev->mtu;
1136d33e4553SDavid S. Miller 
1137d33e4553SDavid S. Miller 	if (unlikely(dst_metric_locked(dst, RTAX_MTU))) {
1138f8126f1dSDavid S. Miller 		if (rt->rt_gateway && mtu > 576)
1139d33e4553SDavid S. Miller 			mtu = 576;
1140d33e4553SDavid S. Miller 	}
1141d33e4553SDavid S. Miller 
1142d33e4553SDavid S. Miller 	if (mtu > IP_MAX_MTU)
1143d33e4553SDavid S. Miller 		mtu = IP_MAX_MTU;
1144d33e4553SDavid S. Miller 
1145d33e4553SDavid S. Miller 	return mtu;
1146d33e4553SDavid S. Miller }
1147d33e4553SDavid S. Miller 
1148f2bb4bedSDavid S. Miller static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr)
11494895c771SDavid S. Miller {
11504895c771SDavid S. Miller 	struct fnhe_hash_bucket *hash = nh->nh_exceptions;
11514895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
11524895c771SDavid S. Miller 	u32 hval;
11534895c771SDavid S. Miller 
1154f2bb4bedSDavid S. Miller 	if (!hash)
1155f2bb4bedSDavid S. Miller 		return NULL;
1156f2bb4bedSDavid S. Miller 
1157d3a25c98SDavid S. Miller 	hval = fnhe_hashfun(daddr);
11584895c771SDavid S. Miller 
11594895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
11604895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
1161f2bb4bedSDavid S. Miller 		if (fnhe->fnhe_daddr == daddr)
1162f2bb4bedSDavid S. Miller 			return fnhe;
1163f2bb4bedSDavid S. Miller 	}
1164f2bb4bedSDavid S. Miller 	return NULL;
1165f2bb4bedSDavid S. Miller }
1166f2bb4bedSDavid S. Miller 
1167f2bb4bedSDavid S. Miller static void rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
1168f2bb4bedSDavid S. Miller 			      __be32 daddr)
1169f2bb4bedSDavid S. Miller {
1170aee06da6SJulian Anastasov 	__be32 fnhe_daddr, gw;
1171aee06da6SJulian Anastasov 	unsigned long expires;
1172aee06da6SJulian Anastasov 	unsigned int seq;
1173aee06da6SJulian Anastasov 	u32 pmtu;
1174aee06da6SJulian Anastasov 
1175f2bb4bedSDavid S. Miller restart:
1176aee06da6SJulian Anastasov 	seq = read_seqbegin(&fnhe_seqlock);
1177aee06da6SJulian Anastasov 	fnhe_daddr = fnhe->fnhe_daddr;
1178aee06da6SJulian Anastasov 	gw = fnhe->fnhe_gw;
1179aee06da6SJulian Anastasov 	pmtu = fnhe->fnhe_pmtu;
1180aee06da6SJulian Anastasov 	expires = fnhe->fnhe_expires;
1181aee06da6SJulian Anastasov 	if (read_seqretry(&fnhe_seqlock, seq))
1182aee06da6SJulian Anastasov 		goto restart;
1183f2bb4bedSDavid S. Miller 
1184aee06da6SJulian Anastasov 	if (daddr != fnhe_daddr)
1185f2bb4bedSDavid S. Miller 		return;
1186f2bb4bedSDavid S. Miller 
1187aee06da6SJulian Anastasov 	if (pmtu) {
1188f31fd383SJulian Anastasov 		unsigned long diff = expires - jiffies;
11894895c771SDavid S. Miller 
11904895c771SDavid S. Miller 		if (time_before(jiffies, expires)) {
1191aee06da6SJulian Anastasov 			rt->rt_pmtu = pmtu;
11924895c771SDavid S. Miller 			dst_set_expires(&rt->dst, diff);
11934895c771SDavid S. Miller 		}
11944895c771SDavid S. Miller 	}
1195ceb33206SDavid S. Miller 	if (gw) {
1196ceb33206SDavid S. Miller 		rt->rt_flags |= RTCF_REDIRECTED;
1197aee06da6SJulian Anastasov 		rt->rt_gateway = gw;
1198ceb33206SDavid S. Miller 	}
11994895c771SDavid S. Miller 	fnhe->fnhe_stamp = jiffies;
1200f2bb4bedSDavid S. Miller }
1201f2bb4bedSDavid S. Miller 
1202*54764bb6SEric Dumazet static inline void rt_free(struct rtable *rt)
1203*54764bb6SEric Dumazet {
1204*54764bb6SEric Dumazet 	call_rcu(&rt->dst.rcu_head, dst_rcu_free);
1205*54764bb6SEric Dumazet }
1206*54764bb6SEric Dumazet 
1207f2bb4bedSDavid S. Miller static void rt_cache_route(struct fib_nh *nh, struct rtable *rt)
1208f2bb4bedSDavid S. Miller {
1209*54764bb6SEric Dumazet 	struct rtable *orig, *prev, **p = (struct rtable **)&nh->nh_rth_output;
1210f2bb4bedSDavid S. Miller 
1211d2d68ba9SDavid S. Miller 	if (rt_is_input_route(rt))
1212*54764bb6SEric Dumazet 		p = (struct rtable **)&nh->nh_rth_input;
1213d2d68ba9SDavid S. Miller 
1214f2bb4bedSDavid S. Miller 	orig = *p;
1215f2bb4bedSDavid S. Miller 
1216f2bb4bedSDavid S. Miller 	prev = cmpxchg(p, orig, rt);
1217f2bb4bedSDavid S. Miller 	if (prev == orig) {
1218f2bb4bedSDavid S. Miller 		if (orig)
1219*54764bb6SEric Dumazet 			rt_free(orig);
1220c6cffba4SDavid S. Miller 	} else {
1221*54764bb6SEric Dumazet 		/* Routes we intend to cache in the FIB nexthop have
1222*54764bb6SEric Dumazet 		 * the DST_NOCACHE bit clear.  However, if we are
1223*54764bb6SEric Dumazet 		 * unsuccessful at storing this route into the cache
1224*54764bb6SEric Dumazet 		 * we really need to set it.
1225*54764bb6SEric Dumazet 		 */
1226*54764bb6SEric Dumazet 		rt->dst.flags |= DST_NOCACHE;
12274895c771SDavid S. Miller 	}
12284895c771SDavid S. Miller }
12294895c771SDavid S. Miller 
12304331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt)
1231d2d68ba9SDavid S. Miller {
12324331debcSEric Dumazet 	return	rt &&
12334331debcSEric Dumazet 		rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
12344331debcSEric Dumazet 		!rt_is_expired(rt);
1235d2d68ba9SDavid S. Miller }
1236d2d68ba9SDavid S. Miller 
1237f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12385e2b61f7SDavid S. Miller 			   const struct fib_result *res,
1239f2bb4bedSDavid S. Miller 			   struct fib_nh_exception *fnhe,
1240982721f3SDavid S. Miller 			   struct fib_info *fi, u16 type, u32 itag)
12411da177e4SLinus Torvalds {
12421da177e4SLinus Torvalds 	if (fi) {
12434895c771SDavid S. Miller 		struct fib_nh *nh = &FIB_RES_NH(*res);
12444895c771SDavid S. Miller 
12454895c771SDavid S. Miller 		if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
12464895c771SDavid S. Miller 			rt->rt_gateway = nh->nh_gw;
1247f2bb4bedSDavid S. Miller 		if (unlikely(fnhe))
1248f2bb4bedSDavid S. Miller 			rt_bind_exception(rt, fnhe, daddr);
12492860583fSDavid S. Miller 		dst_init_metrics(&rt->dst, fi->fib_metrics, true);
1250c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1251f2bb4bedSDavid S. Miller 		rt->dst.tclassid = nh->nh_tclassid;
12521da177e4SLinus Torvalds #endif
1253c6cffba4SDavid S. Miller 		if (!(rt->dst.flags & DST_NOCACHE))
1254f2bb4bedSDavid S. Miller 			rt_cache_route(nh, rt);
1255d33e4553SDavid S. Miller 	}
12561da177e4SLinus Torvalds 
1257c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
12581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
125985b91b03SDavid S. Miller 	set_class_tag(rt, res->tclassid);
12601da177e4SLinus Torvalds #endif
12611da177e4SLinus Torvalds 	set_class_tag(rt, itag);
12621da177e4SLinus Torvalds #endif
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12655c1e6aa3SDavid S. Miller static struct rtable *rt_dst_alloc(struct net_device *dev,
1266f2bb4bedSDavid S. Miller 				   bool nopolicy, bool noxfrm, bool will_cache)
12670c4dcd58SDavid S. Miller {
1268f5b0a874SDavid S. Miller 	return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1269c6cffba4SDavid S. Miller 			 (will_cache ? 0 : (DST_HOST | DST_NOCACHE)) |
12700c4dcd58SDavid S. Miller 			 (nopolicy ? DST_NOPOLICY : 0) |
12715c1e6aa3SDavid S. Miller 			 (noxfrm ? DST_NOXFRM : 0));
12720c4dcd58SDavid S. Miller }
12730c4dcd58SDavid S. Miller 
127496d36220SEric Dumazet /* called in rcu_read_lock() section */
12759e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
12761da177e4SLinus Torvalds 				u8 tos, struct net_device *dev, int our)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	struct rtable *rth;
127996d36220SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
12801da177e4SLinus Torvalds 	u32 itag = 0;
1281b5f7e755SEric Dumazet 	int err;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	/* Primary sanity checks. */
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	if (in_dev == NULL)
12861da177e4SLinus Torvalds 		return -EINVAL;
12871da177e4SLinus Torvalds 
12881e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1289d0daebc3SThomas Graf 	    skb->protocol != htons(ETH_P_IP))
1290d0daebc3SThomas Graf 		goto e_inval;
1291d0daebc3SThomas Graf 
1292d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
1293d0daebc3SThomas Graf 		if (ipv4_is_loopback(saddr))
12941da177e4SLinus Torvalds 			goto e_inval;
12951da177e4SLinus Torvalds 
1296f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr)) {
1297f97c1e0cSJoe Perches 		if (!ipv4_is_local_multicast(daddr))
12981da177e4SLinus Torvalds 			goto e_inval;
1299b5f7e755SEric Dumazet 	} else {
13009e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
13019e56e380SDavid S. Miller 					  in_dev, &itag);
1302b5f7e755SEric Dumazet 		if (err < 0)
1303b5f7e755SEric Dumazet 			goto e_err;
1304b5f7e755SEric Dumazet 	}
13054e7b2f14SBenjamin LaHaise 	rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
1306f2bb4bedSDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
13071da177e4SLinus Torvalds 	if (!rth)
13081da177e4SLinus Torvalds 		goto e_nobufs;
13091da177e4SLinus Torvalds 
1310c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1311d8d1f30bSChangli Gao 	rth->dst.tclassid = itag;
13121da177e4SLinus Torvalds #endif
1313cf911662SDavid S. Miller 	rth->dst.output = ip_rt_bug;
1314cf911662SDavid S. Miller 
1315e84f84f2SDenis V. Lunev 	rth->rt_genid	= rt_genid(dev_net(dev));
13161da177e4SLinus Torvalds 	rth->rt_flags	= RTCF_MULTICAST;
131729e75252SEric Dumazet 	rth->rt_type	= RTN_MULTICAST;
13189917e1e8SDavid S. Miller 	rth->rt_is_input= 1;
131913378cadSDavid S. Miller 	rth->rt_iif	= 0;
13205943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1321f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
13221da177e4SLinus Torvalds 	if (our) {
1323d8d1f30bSChangli Gao 		rth->dst.input= ip_local_deliver;
13241da177e4SLinus Torvalds 		rth->rt_flags |= RTCF_LOCAL;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1328f97c1e0cSJoe Perches 	if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
1329d8d1f30bSChangli Gao 		rth->dst.input = ip_mr_input;
13301da177e4SLinus Torvalds #endif
13311da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
13321da177e4SLinus Torvalds 
133389aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
133489aef892SDavid S. Miller 	return 0;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds e_nobufs:
13371da177e4SLinus Torvalds 	return -ENOBUFS;
13381da177e4SLinus Torvalds e_inval:
133996d36220SEric Dumazet 	return -EINVAL;
1340b5f7e755SEric Dumazet e_err:
1341b5f7e755SEric Dumazet 	return err;
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
13461da177e4SLinus Torvalds 				     struct in_device *in_dev,
13471da177e4SLinus Torvalds 				     struct sk_buff *skb,
13489e12bb22SAl Viro 				     __be32 daddr,
13499e12bb22SAl Viro 				     __be32 saddr)
13501da177e4SLinus Torvalds {
13511da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
13521da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
13531da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
13541da177e4SLinus Torvalds 		/*
13551da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
13561da177e4SLinus Torvalds 		 *	the only hint is MAC header.
13571da177e4SLinus Torvalds 		 */
1358058bd4d2SJoe Perches 		pr_warn("martian source %pI4 from %pI4, on dev %s\n",
1359673d57e7SHarvey Harrison 			&daddr, &saddr, dev->name);
136098e399f8SArnaldo Carvalho de Melo 		if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1361058bd4d2SJoe Perches 			print_hex_dump(KERN_WARNING, "ll header: ",
1362058bd4d2SJoe Perches 				       DUMP_PREFIX_OFFSET, 16, 1,
1363058bd4d2SJoe Perches 				       skb_mac_header(skb),
1364058bd4d2SJoe Perches 				       dev->hard_header_len, true);
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds #endif
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
137047360228SEric Dumazet /* called in rcu_read_lock() section */
13715969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb,
1372982721f3SDavid S. Miller 			   const struct fib_result *res,
13731da177e4SLinus Torvalds 			   struct in_device *in_dev,
1374c6cffba4SDavid S. Miller 			   __be32 daddr, __be32 saddr, u32 tos)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	struct rtable *rth;
13771da177e4SLinus Torvalds 	int err;
13781da177e4SLinus Torvalds 	struct in_device *out_dev;
137947360228SEric Dumazet 	unsigned int flags = 0;
1380d2d68ba9SDavid S. Miller 	bool do_cache;
1381d9c9df8cSAl Viro 	u32 itag;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/* get a working reference to the output device */
138447360228SEric Dumazet 	out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res));
13851da177e4SLinus Torvalds 	if (out_dev == NULL) {
1386e87cc472SJoe Perches 		net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
13871da177e4SLinus Torvalds 		return -EINVAL;
13881da177e4SLinus Torvalds 	}
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 
13915c04c819SMichael Smith 	err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
13929e56e380SDavid S. Miller 				  in_dev->dev, in_dev, &itag);
13931da177e4SLinus Torvalds 	if (err < 0) {
13941da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
13951da177e4SLinus Torvalds 					 saddr);
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 		goto cleanup;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 
140051b77caeSThomas Graf 	if (out_dev == in_dev && err &&
14011da177e4SLinus Torvalds 	    (IN_DEV_SHARED_MEDIA(out_dev) ||
14021da177e4SLinus Torvalds 	     inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
14031da177e4SLinus Torvalds 		flags |= RTCF_DOREDIRECT;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
14061da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
14071da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
140865324144SJesper Dangaard Brouer 		 *
140965324144SJesper Dangaard Brouer 		 * Proxy arp feature have been extended to allow, ARP
141065324144SJesper Dangaard Brouer 		 * replies back to the same interface, to support
141165324144SJesper Dangaard Brouer 		 * Private VLAN switch technologies. See arp.c.
14121da177e4SLinus Torvalds 		 */
141365324144SJesper Dangaard Brouer 		if (out_dev == in_dev &&
141465324144SJesper Dangaard Brouer 		    IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
14151da177e4SLinus Torvalds 			err = -EINVAL;
14161da177e4SLinus Torvalds 			goto cleanup;
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 	}
14191da177e4SLinus Torvalds 
1420d2d68ba9SDavid S. Miller 	do_cache = false;
1421d2d68ba9SDavid S. Miller 	if (res->fi) {
1422fe3edf45SDavid S. Miller 		if (!itag) {
1423*54764bb6SEric Dumazet 			rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input);
1424d2d68ba9SDavid S. Miller 			if (rt_cache_valid(rth)) {
1425c6cffba4SDavid S. Miller 				skb_dst_set_noref(skb, &rth->dst);
1426d2d68ba9SDavid S. Miller 				goto out;
1427d2d68ba9SDavid S. Miller 			}
1428d2d68ba9SDavid S. Miller 			do_cache = true;
1429d2d68ba9SDavid S. Miller 		}
1430d2d68ba9SDavid S. Miller 	}
1431f2bb4bedSDavid S. Miller 
14325c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(out_dev->dev,
14335c1e6aa3SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY),
1434d2d68ba9SDavid S. Miller 			   IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
14351da177e4SLinus Torvalds 	if (!rth) {
14361da177e4SLinus Torvalds 		err = -ENOBUFS;
14371da177e4SLinus Torvalds 		goto cleanup;
14381da177e4SLinus Torvalds 	}
14391da177e4SLinus Torvalds 
1440cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(dev_net(rth->dst.dev));
1441cf911662SDavid S. Miller 	rth->rt_flags = flags;
1442cf911662SDavid S. Miller 	rth->rt_type = res->type;
14439917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
144413378cadSDavid S. Miller 	rth->rt_iif 	= 0;
14455943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1446f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
14471da177e4SLinus Torvalds 
1448d8d1f30bSChangli Gao 	rth->dst.input = ip_forward;
1449d8d1f30bSChangli Gao 	rth->dst.output = ip_output;
14501da177e4SLinus Torvalds 
1451d2d68ba9SDavid S. Miller 	rt_set_nexthop(rth, daddr, res, NULL, res->fi, res->type, itag);
1452c6cffba4SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
1453d2d68ba9SDavid S. Miller out:
14541da177e4SLinus Torvalds 	err = 0;
14551da177e4SLinus Torvalds  cleanup:
14561da177e4SLinus Torvalds 	return err;
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds 
14595969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb,
14601da177e4SLinus Torvalds 			    struct fib_result *res,
146168a5e3ddSDavid S. Miller 			    const struct flowi4 *fl4,
14621da177e4SLinus Torvalds 			    struct in_device *in_dev,
14639e12bb22SAl Viro 			    __be32 daddr, __be32 saddr, u32 tos)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1466ff3fccb3SDavid S. Miller 	if (res->fi && res->fi->fib_nhs > 1)
14671b7fe593SDavid S. Miller 		fib_select_multipath(res);
14681da177e4SLinus Torvalds #endif
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	/* create a routing cache entry */
1471c6cffba4SDavid S. Miller 	return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds /*
14751da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
14761da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
14771da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
14781da177e4SLinus Torvalds  *
14791da177e4SLinus Torvalds  *	Such approach solves two big problems:
14801da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
14811da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
1482ebc0ffaeSEric Dumazet  *	called with rcu_read_lock()
14831da177e4SLinus Torvalds  */
14841da177e4SLinus Torvalds 
14859e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1486c10237e0SDavid S. Miller 			       u8 tos, struct net_device *dev)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	struct fib_result res;
148996d36220SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
149068a5e3ddSDavid S. Miller 	struct flowi4	fl4;
149195c96174SEric Dumazet 	unsigned int	flags = 0;
14921da177e4SLinus Torvalds 	u32		itag = 0;
14931da177e4SLinus Torvalds 	struct rtable	*rth;
14941da177e4SLinus Torvalds 	int		err = -EINVAL;
1495c346dca1SYOSHIFUJI Hideaki 	struct net    *net = dev_net(dev);
1496d2d68ba9SDavid S. Miller 	bool do_cache;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/* IP on this device is disabled. */
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	if (!in_dev)
15011da177e4SLinus Torvalds 		goto out;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
15041da177e4SLinus Torvalds 	   by fib_lookup.
15051da177e4SLinus Torvalds 	 */
15061da177e4SLinus Torvalds 
1507d0daebc3SThomas Graf 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
15081da177e4SLinus Torvalds 		goto martian_source;
15091da177e4SLinus Torvalds 
1510d2d68ba9SDavid S. Miller 	res.fi = NULL;
151127a954bdSAndy Walls 	if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
15121da177e4SLinus Torvalds 		goto brd_input;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
15151da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
15161da177e4SLinus Torvalds 	 */
1517f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
15181da177e4SLinus Torvalds 		goto martian_source;
15191da177e4SLinus Torvalds 
1520d0daebc3SThomas Graf 	if (ipv4_is_zeronet(daddr))
15211da177e4SLinus Torvalds 		goto martian_destination;
15221da177e4SLinus Torvalds 
1523d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) {
1524d0daebc3SThomas Graf 		if (ipv4_is_loopback(daddr))
1525d0daebc3SThomas Graf 			goto martian_destination;
1526d0daebc3SThomas Graf 
1527d0daebc3SThomas Graf 		if (ipv4_is_loopback(saddr))
1528d0daebc3SThomas Graf 			goto martian_source;
1529d0daebc3SThomas Graf 	}
1530d0daebc3SThomas Graf 
15311da177e4SLinus Torvalds 	/*
15321da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
15331da177e4SLinus Torvalds 	 */
153468a5e3ddSDavid S. Miller 	fl4.flowi4_oif = 0;
153568a5e3ddSDavid S. Miller 	fl4.flowi4_iif = dev->ifindex;
153668a5e3ddSDavid S. Miller 	fl4.flowi4_mark = skb->mark;
153768a5e3ddSDavid S. Miller 	fl4.flowi4_tos = tos;
153868a5e3ddSDavid S. Miller 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
153968a5e3ddSDavid S. Miller 	fl4.daddr = daddr;
154068a5e3ddSDavid S. Miller 	fl4.saddr = saddr;
154168a5e3ddSDavid S. Miller 	err = fib_lookup(net, &fl4, &res);
1542251da413SDavid S. Miller 	if (err != 0)
15431da177e4SLinus Torvalds 		goto no_route;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_tot);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	if (res.type == RTN_BROADCAST)
15481da177e4SLinus Torvalds 		goto brd_input;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
15515c04c819SMichael Smith 		err = fib_validate_source(skb, saddr, daddr, tos,
155284a885f4SDenis V. Lunev 					  net->loopback_dev->ifindex,
15539e56e380SDavid S. Miller 					  dev, in_dev, &itag);
1554b5f7e755SEric Dumazet 		if (err < 0)
1555b5f7e755SEric Dumazet 			goto martian_source_keep_err;
15561da177e4SLinus Torvalds 		goto local_input;
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	if (!IN_DEV_FORWARD(in_dev))
1560251da413SDavid S. Miller 		goto no_route;
15611da177e4SLinus Torvalds 	if (res.type != RTN_UNICAST)
15621da177e4SLinus Torvalds 		goto martian_destination;
15631da177e4SLinus Torvalds 
156468a5e3ddSDavid S. Miller 	err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos);
15651da177e4SLinus Torvalds out:	return err;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds brd_input:
15681da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
15691da177e4SLinus Torvalds 		goto e_inval;
15701da177e4SLinus Torvalds 
157141347dcdSDavid S. Miller 	if (!ipv4_is_zeronet(saddr)) {
15729e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
15739e56e380SDavid S. Miller 					  in_dev, &itag);
15741da177e4SLinus Torvalds 		if (err < 0)
1575b5f7e755SEric Dumazet 			goto martian_source_keep_err;
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
15781da177e4SLinus Torvalds 	res.type = RTN_BROADCAST;
15791da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds local_input:
1582d2d68ba9SDavid S. Miller 	do_cache = false;
1583d2d68ba9SDavid S. Miller 	if (res.fi) {
1584fe3edf45SDavid S. Miller 		if (!itag) {
1585*54764bb6SEric Dumazet 			rth = rcu_dereference(FIB_RES_NH(res).nh_rth_input);
1586d2d68ba9SDavid S. Miller 			if (rt_cache_valid(rth)) {
1587c6cffba4SDavid S. Miller 				skb_dst_set_noref(skb, &rth->dst);
1588c6cffba4SDavid S. Miller 				err = 0;
1589c6cffba4SDavid S. Miller 				goto out;
1590d2d68ba9SDavid S. Miller 			}
1591d2d68ba9SDavid S. Miller 			do_cache = true;
1592d2d68ba9SDavid S. Miller 		}
1593d2d68ba9SDavid S. Miller 	}
1594d2d68ba9SDavid S. Miller 
15955c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(net->loopback_dev,
1596d2d68ba9SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache);
15971da177e4SLinus Torvalds 	if (!rth)
15981da177e4SLinus Torvalds 		goto e_nobufs;
15991da177e4SLinus Torvalds 
1600cf911662SDavid S. Miller 	rth->dst.input= ip_local_deliver;
1601d8d1f30bSChangli Gao 	rth->dst.output= ip_rt_bug;
1602cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
1603cf911662SDavid S. Miller 	rth->dst.tclassid = itag;
1604cf911662SDavid S. Miller #endif
16051da177e4SLinus Torvalds 
1606cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(net);
1607cf911662SDavid S. Miller 	rth->rt_flags 	= flags|RTCF_LOCAL;
1608cf911662SDavid S. Miller 	rth->rt_type	= res.type;
16099917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
161013378cadSDavid S. Miller 	rth->rt_iif	= 0;
16115943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1612f8126f1dSDavid S. Miller 	rth->rt_gateway	= 0;
16131da177e4SLinus Torvalds 	if (res.type == RTN_UNREACHABLE) {
1614d8d1f30bSChangli Gao 		rth->dst.input= ip_error;
1615d8d1f30bSChangli Gao 		rth->dst.error= -err;
16161da177e4SLinus Torvalds 		rth->rt_flags 	&= ~RTCF_LOCAL;
16171da177e4SLinus Torvalds 	}
1618d2d68ba9SDavid S. Miller 	if (do_cache)
1619d2d68ba9SDavid S. Miller 		rt_cache_route(&FIB_RES_NH(res), rth);
162089aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
1621b23dd4feSDavid S. Miller 	err = 0;
1622ebc0ffaeSEric Dumazet 	goto out;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds no_route:
16251da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
16261da177e4SLinus Torvalds 	res.type = RTN_UNREACHABLE;
16277f53878dSMitsuru Chinen 	if (err == -ESRCH)
16287f53878dSMitsuru Chinen 		err = -ENETUNREACH;
16291da177e4SLinus Torvalds 	goto local_input;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	/*
16321da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
16331da177e4SLinus Torvalds 	 */
16341da177e4SLinus Torvalds martian_destination:
16351da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
16361da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
1637e87cc472SJoe Perches 	if (IN_DEV_LOG_MARTIANS(in_dev))
1638e87cc472SJoe Perches 		net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
1639673d57e7SHarvey Harrison 				     &daddr, &saddr, dev->name);
16401da177e4SLinus Torvalds #endif
16412c2910a4SDietmar Eggemann 
16421da177e4SLinus Torvalds e_inval:
16431da177e4SLinus Torvalds 	err = -EINVAL;
1644ebc0ffaeSEric Dumazet 	goto out;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds e_nobufs:
16471da177e4SLinus Torvalds 	err = -ENOBUFS;
1648ebc0ffaeSEric Dumazet 	goto out;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds martian_source:
1651b5f7e755SEric Dumazet 	err = -EINVAL;
1652b5f7e755SEric Dumazet martian_source_keep_err:
16531da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
1654ebc0ffaeSEric Dumazet 	goto out;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds 
1657c6cffba4SDavid S. Miller int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
165838a424e4SDavid Miller 			 u8 tos, struct net_device *dev)
16591da177e4SLinus Torvalds {
166096d36220SEric Dumazet 	int res;
16611da177e4SLinus Torvalds 
166296d36220SEric Dumazet 	rcu_read_lock();
166396d36220SEric Dumazet 
16641da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
16651da177e4SLinus Torvalds 	   The problem was that too many Ethernet cards have broken/missing
16661da177e4SLinus Torvalds 	   hardware multicast filters :-( As result the host on multicasting
16671da177e4SLinus Torvalds 	   network acquires a lot of useless route cache entries, sort of
16681da177e4SLinus Torvalds 	   SDR messages from all the world. Now we try to get rid of them.
16691da177e4SLinus Torvalds 	   Really, provided software IP multicast filter is organized
16701da177e4SLinus Torvalds 	   reasonably (at least, hashed), it does not result in a slowdown
16711da177e4SLinus Torvalds 	   comparing with route cache reject entries.
16721da177e4SLinus Torvalds 	   Note, that multicast routers are not affected, because
16731da177e4SLinus Torvalds 	   route cache entry is created eventually.
16741da177e4SLinus Torvalds 	 */
1675f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
167696d36220SEric Dumazet 		struct in_device *in_dev = __in_dev_get_rcu(dev);
16771da177e4SLinus Torvalds 
167896d36220SEric Dumazet 		if (in_dev) {
1679dbdd9a52SDavid S. Miller 			int our = ip_check_mc_rcu(in_dev, daddr, saddr,
1680eddc9ec5SArnaldo Carvalho de Melo 						  ip_hdr(skb)->protocol);
16811da177e4SLinus Torvalds 			if (our
16821da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
16839d4fb27dSJoe Perches 				||
16849d4fb27dSJoe Perches 			    (!ipv4_is_local_multicast(daddr) &&
1685f97c1e0cSJoe Perches 			     IN_DEV_MFORWARD(in_dev))
16861da177e4SLinus Torvalds #endif
16871da177e4SLinus Torvalds 			   ) {
168896d36220SEric Dumazet 				int res = ip_route_input_mc(skb, daddr, saddr,
16891da177e4SLinus Torvalds 							    tos, dev, our);
169096d36220SEric Dumazet 				rcu_read_unlock();
169196d36220SEric Dumazet 				return res;
16921da177e4SLinus Torvalds 			}
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 		rcu_read_unlock();
16951da177e4SLinus Torvalds 		return -EINVAL;
16961da177e4SLinus Torvalds 	}
1697c10237e0SDavid S. Miller 	res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
169896d36220SEric Dumazet 	rcu_read_unlock();
169996d36220SEric Dumazet 	return res;
17001da177e4SLinus Torvalds }
1701c6cffba4SDavid S. Miller EXPORT_SYMBOL(ip_route_input_noref);
17021da177e4SLinus Torvalds 
1703ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */
1704982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res,
17051a00fee4SDavid Miller 				       const struct flowi4 *fl4, int orig_oif,
1706f61759e6SJulian Anastasov 				       struct net_device *dev_out,
17075ada5527SDavid S. Miller 				       unsigned int flags)
17081da177e4SLinus Torvalds {
1709982721f3SDavid S. Miller 	struct fib_info *fi = res->fi;
1710f2bb4bedSDavid S. Miller 	struct fib_nh_exception *fnhe;
17115ada5527SDavid S. Miller 	struct in_device *in_dev;
1712982721f3SDavid S. Miller 	u16 type = res->type;
17135ada5527SDavid S. Miller 	struct rtable *rth;
17141da177e4SLinus Torvalds 
1715d0daebc3SThomas Graf 	in_dev = __in_dev_get_rcu(dev_out);
1716d0daebc3SThomas Graf 	if (!in_dev)
1717d0daebc3SThomas Graf 		return ERR_PTR(-EINVAL);
1718d0daebc3SThomas Graf 
1719d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
172068a5e3ddSDavid S. Miller 		if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK))
17215ada5527SDavid S. Miller 			return ERR_PTR(-EINVAL);
17221da177e4SLinus Torvalds 
172368a5e3ddSDavid S. Miller 	if (ipv4_is_lbcast(fl4->daddr))
1724982721f3SDavid S. Miller 		type = RTN_BROADCAST;
172568a5e3ddSDavid S. Miller 	else if (ipv4_is_multicast(fl4->daddr))
1726982721f3SDavid S. Miller 		type = RTN_MULTICAST;
172768a5e3ddSDavid S. Miller 	else if (ipv4_is_zeronet(fl4->daddr))
17285ada5527SDavid S. Miller 		return ERR_PTR(-EINVAL);
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
17311da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
17321da177e4SLinus Torvalds 
1733982721f3SDavid S. Miller 	if (type == RTN_BROADCAST) {
17341da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
1735982721f3SDavid S. Miller 		fi = NULL;
1736982721f3SDavid S. Miller 	} else if (type == RTN_MULTICAST) {
17371da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST | RTCF_LOCAL;
1738813b3b5dSDavid S. Miller 		if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
1739813b3b5dSDavid S. Miller 				     fl4->flowi4_proto))
17401da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
17411da177e4SLinus Torvalds 		/* If multicast route do not exist use
1742dd28d1a0SEric Dumazet 		 * default one, but do not gateway in this case.
1743dd28d1a0SEric Dumazet 		 * Yes, it is hack.
17441da177e4SLinus Torvalds 		 */
1745982721f3SDavid S. Miller 		if (fi && res->prefixlen < 4)
1746982721f3SDavid S. Miller 			fi = NULL;
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
1749f2bb4bedSDavid S. Miller 	fnhe = NULL;
1750f2bb4bedSDavid S. Miller 	if (fi) {
1751f2bb4bedSDavid S. Miller 		fnhe = find_exception(&FIB_RES_NH(*res), fl4->daddr);
1752f2bb4bedSDavid S. Miller 		if (!fnhe) {
1753*54764bb6SEric Dumazet 			rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_output);
1754d2d68ba9SDavid S. Miller 			if (rt_cache_valid(rth)) {
175593ac5341SDavid S. Miller 				dst_hold(&rth->dst);
1756f2bb4bedSDavid S. Miller 				return rth;
1757f2bb4bedSDavid S. Miller 			}
1758f2bb4bedSDavid S. Miller 		}
1759f2bb4bedSDavid S. Miller 	}
17605c1e6aa3SDavid S. Miller 	rth = rt_dst_alloc(dev_out,
17615c1e6aa3SDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOPOLICY),
1762f2bb4bedSDavid S. Miller 			   IN_DEV_CONF_GET(in_dev, NOXFRM),
1763f2bb4bedSDavid S. Miller 			   fi && !fnhe);
17648391d07bSDimitris Michailidis 	if (!rth)
17655ada5527SDavid S. Miller 		return ERR_PTR(-ENOBUFS);
17668391d07bSDimitris Michailidis 
1767cf911662SDavid S. Miller 	rth->dst.output = ip_output;
1768cf911662SDavid S. Miller 
1769cf911662SDavid S. Miller 	rth->rt_genid = rt_genid(dev_net(dev_out));
1770cf911662SDavid S. Miller 	rth->rt_flags	= flags;
1771cf911662SDavid S. Miller 	rth->rt_type	= type;
17729917e1e8SDavid S. Miller 	rth->rt_is_input = 0;
177313378cadSDavid S. Miller 	rth->rt_iif	= orig_oif ? : 0;
17745943634fSDavid S. Miller 	rth->rt_pmtu	= 0;
1775f8126f1dSDavid S. Miller 	rth->rt_gateway = 0;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
17781da177e4SLinus Torvalds 
177941347dcdSDavid S. Miller 	if (flags & RTCF_LOCAL)
1780d8d1f30bSChangli Gao 		rth->dst.input = ip_local_deliver;
17811da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
17821da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
17831da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
1784d8d1f30bSChangli Gao 			rth->dst.output = ip_mc_output;
17851da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
17861da177e4SLinus Torvalds 		}
17871da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1788982721f3SDavid S. Miller 		if (type == RTN_MULTICAST) {
17891da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
1790813b3b5dSDavid S. Miller 			    !ipv4_is_local_multicast(fl4->daddr)) {
1791d8d1f30bSChangli Gao 				rth->dst.input = ip_mr_input;
1792d8d1f30bSChangli Gao 				rth->dst.output = ip_mc_output;
17931da177e4SLinus Torvalds 			}
17941da177e4SLinus Torvalds 		}
17951da177e4SLinus Torvalds #endif
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
1798f2bb4bedSDavid S. Miller 	rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0);
17991da177e4SLinus Torvalds 
18005ada5527SDavid S. Miller 	return rth;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /*
18041da177e4SLinus Torvalds  * Major route resolver routine.
18051da177e4SLinus Torvalds  */
18061da177e4SLinus Torvalds 
180789aef892SDavid S. Miller struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
18081da177e4SLinus Torvalds {
18091da177e4SLinus Torvalds 	struct net_device *dev_out = NULL;
1810f61759e6SJulian Anastasov 	__u8 tos = RT_FL_TOS(fl4);
1811813b3b5dSDavid S. Miller 	unsigned int flags = 0;
1812813b3b5dSDavid S. Miller 	struct fib_result res;
18135ada5527SDavid S. Miller 	struct rtable *rth;
1814813b3b5dSDavid S. Miller 	int orig_oif;
18151da177e4SLinus Torvalds 
181685b91b03SDavid S. Miller 	res.tclassid	= 0;
18171da177e4SLinus Torvalds 	res.fi		= NULL;
18188b96d22dSDavid S. Miller 	res.table	= NULL;
18191da177e4SLinus Torvalds 
1820813b3b5dSDavid S. Miller 	orig_oif = fl4->flowi4_oif;
1821813b3b5dSDavid S. Miller 
1822813b3b5dSDavid S. Miller 	fl4->flowi4_iif = net->loopback_dev->ifindex;
1823813b3b5dSDavid S. Miller 	fl4->flowi4_tos = tos & IPTOS_RT_MASK;
1824813b3b5dSDavid S. Miller 	fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
182544713b67SDavid S. Miller 			 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
182644713b67SDavid S. Miller 
1827010c2708SDavid S. Miller 	rcu_read_lock();
1828813b3b5dSDavid S. Miller 	if (fl4->saddr) {
1829b23dd4feSDavid S. Miller 		rth = ERR_PTR(-EINVAL);
1830813b3b5dSDavid S. Miller 		if (ipv4_is_multicast(fl4->saddr) ||
1831813b3b5dSDavid S. Miller 		    ipv4_is_lbcast(fl4->saddr) ||
1832813b3b5dSDavid S. Miller 		    ipv4_is_zeronet(fl4->saddr))
18331da177e4SLinus Torvalds 			goto out;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
18361da177e4SLinus Torvalds 		   It was wrong for two reasons:
18371ab35276SDenis V. Lunev 		   1. ip_dev_find(net, saddr) can return wrong iface, if saddr
18381ab35276SDenis V. Lunev 		      is assigned to multiple interfaces.
18391da177e4SLinus Torvalds 		   2. Moreover, we are allowed to send packets with saddr
18401da177e4SLinus Torvalds 		      of another iface. --ANK
18411da177e4SLinus Torvalds 		 */
18421da177e4SLinus Torvalds 
1843813b3b5dSDavid S. Miller 		if (fl4->flowi4_oif == 0 &&
1844813b3b5dSDavid S. Miller 		    (ipv4_is_multicast(fl4->daddr) ||
1845813b3b5dSDavid S. Miller 		     ipv4_is_lbcast(fl4->daddr))) {
1846a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
1847813b3b5dSDavid S. Miller 			dev_out = __ip_dev_find(net, fl4->saddr, false);
1848a210d01aSJulian Anastasov 			if (dev_out == NULL)
1849a210d01aSJulian Anastasov 				goto out;
1850a210d01aSJulian Anastasov 
18511da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
18521da177e4SLinus Torvalds 			   and limited broadcast via necessary interface
18531da177e4SLinus Torvalds 			   without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
18541da177e4SLinus Torvalds 			   This hack is not just for fun, it allows
18551da177e4SLinus Torvalds 			   vic,vat and friends to work.
18561da177e4SLinus Torvalds 			   They bind socket to loopback, set ttl to zero
18571da177e4SLinus Torvalds 			   and expect that it will work.
18581da177e4SLinus Torvalds 			   From the viewpoint of routing cache they are broken,
18591da177e4SLinus Torvalds 			   because we are not allowed to build multicast path
18601da177e4SLinus Torvalds 			   with loopback source addr (look, routing cache
18611da177e4SLinus Torvalds 			   cannot know, that ttl is zero, so that packet
18621da177e4SLinus Torvalds 			   will not leave this host and route is valid).
18631da177e4SLinus Torvalds 			   Luckily, this hack is good workaround.
18641da177e4SLinus Torvalds 			 */
18651da177e4SLinus Torvalds 
1866813b3b5dSDavid S. Miller 			fl4->flowi4_oif = dev_out->ifindex;
18671da177e4SLinus Torvalds 			goto make_route;
18681da177e4SLinus Torvalds 		}
1869a210d01aSJulian Anastasov 
1870813b3b5dSDavid S. Miller 		if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
1871a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
1872813b3b5dSDavid S. Miller 			if (!__ip_dev_find(net, fl4->saddr, false))
1873a210d01aSJulian Anastasov 				goto out;
18741da177e4SLinus Torvalds 		}
1875a210d01aSJulian Anastasov 	}
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 
1878813b3b5dSDavid S. Miller 	if (fl4->flowi4_oif) {
1879813b3b5dSDavid S. Miller 		dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
1880b23dd4feSDavid S. Miller 		rth = ERR_PTR(-ENODEV);
18811da177e4SLinus Torvalds 		if (dev_out == NULL)
18821da177e4SLinus Torvalds 			goto out;
1883e5ed6399SHerbert Xu 
1884e5ed6399SHerbert Xu 		/* RACE: Check return value of inet_select_addr instead. */
1885fc75fc83SEric Dumazet 		if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
1886b23dd4feSDavid S. Miller 			rth = ERR_PTR(-ENETUNREACH);
1887fc75fc83SEric Dumazet 			goto out;
1888fc75fc83SEric Dumazet 		}
1889813b3b5dSDavid S. Miller 		if (ipv4_is_local_multicast(fl4->daddr) ||
1890813b3b5dSDavid S. Miller 		    ipv4_is_lbcast(fl4->daddr)) {
1891813b3b5dSDavid S. Miller 			if (!fl4->saddr)
1892813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
18931da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
18941da177e4SLinus Torvalds 			goto make_route;
18951da177e4SLinus Torvalds 		}
1896813b3b5dSDavid S. Miller 		if (fl4->saddr) {
1897813b3b5dSDavid S. Miller 			if (ipv4_is_multicast(fl4->daddr))
1898813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
1899813b3b5dSDavid S. Miller 							      fl4->flowi4_scope);
1900813b3b5dSDavid S. Miller 			else if (!fl4->daddr)
1901813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
19021da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
19031da177e4SLinus Torvalds 		}
19041da177e4SLinus Torvalds 	}
19051da177e4SLinus Torvalds 
1906813b3b5dSDavid S. Miller 	if (!fl4->daddr) {
1907813b3b5dSDavid S. Miller 		fl4->daddr = fl4->saddr;
1908813b3b5dSDavid S. Miller 		if (!fl4->daddr)
1909813b3b5dSDavid S. Miller 			fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
1910b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
1911813b3b5dSDavid S. Miller 		fl4->flowi4_oif = net->loopback_dev->ifindex;
19121da177e4SLinus Torvalds 		res.type = RTN_LOCAL;
19131da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
19141da177e4SLinus Torvalds 		goto make_route;
19151da177e4SLinus Torvalds 	}
19161da177e4SLinus Torvalds 
1917813b3b5dSDavid S. Miller 	if (fib_lookup(net, fl4, &res)) {
19181da177e4SLinus Torvalds 		res.fi = NULL;
19198b96d22dSDavid S. Miller 		res.table = NULL;
1920813b3b5dSDavid S. Miller 		if (fl4->flowi4_oif) {
19211da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
19221da177e4SLinus Torvalds 			   that the destination is on link.
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 			   WHY? DW.
19251da177e4SLinus Torvalds 			   Because we are allowed to send to iface
19261da177e4SLinus Torvalds 			   even if it has NO routes and NO assigned
19271da177e4SLinus Torvalds 			   addresses. When oif is specified, routing
19281da177e4SLinus Torvalds 			   tables are looked up with only one purpose:
19291da177e4SLinus Torvalds 			   to catch if destination is gatewayed, rather than
19301da177e4SLinus Torvalds 			   direct. Moreover, if MSG_DONTROUTE is set,
19311da177e4SLinus Torvalds 			   we send packet, ignoring both routing tables
19321da177e4SLinus Torvalds 			   and ifaddr state. --ANK
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 			   We could make it even if oif is unknown,
19361da177e4SLinus Torvalds 			   likely IPv6, but we do not.
19371da177e4SLinus Torvalds 			 */
19381da177e4SLinus Torvalds 
1939813b3b5dSDavid S. Miller 			if (fl4->saddr == 0)
1940813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
19411da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
19421da177e4SLinus Torvalds 			res.type = RTN_UNICAST;
19431da177e4SLinus Torvalds 			goto make_route;
19441da177e4SLinus Torvalds 		}
1945b23dd4feSDavid S. Miller 		rth = ERR_PTR(-ENETUNREACH);
19461da177e4SLinus Torvalds 		goto out;
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	if (res.type == RTN_LOCAL) {
1950813b3b5dSDavid S. Miller 		if (!fl4->saddr) {
19519fc3bbb4SJoel Sing 			if (res.fi->fib_prefsrc)
1952813b3b5dSDavid S. Miller 				fl4->saddr = res.fi->fib_prefsrc;
19539fc3bbb4SJoel Sing 			else
1954813b3b5dSDavid S. Miller 				fl4->saddr = fl4->daddr;
19559fc3bbb4SJoel Sing 		}
1956b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
1957813b3b5dSDavid S. Miller 		fl4->flowi4_oif = dev_out->ifindex;
19581da177e4SLinus Torvalds 		res.fi = NULL;
19591da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
19601da177e4SLinus Torvalds 		goto make_route;
19611da177e4SLinus Torvalds 	}
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
1964813b3b5dSDavid S. Miller 	if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0)
19651b7fe593SDavid S. Miller 		fib_select_multipath(&res);
19661da177e4SLinus Torvalds 	else
19671da177e4SLinus Torvalds #endif
196821d8c49eSDavid S. Miller 	if (!res.prefixlen &&
196921d8c49eSDavid S. Miller 	    res.table->tb_num_default > 1 &&
1970813b3b5dSDavid S. Miller 	    res.type == RTN_UNICAST && !fl4->flowi4_oif)
19710c838ff1SDavid S. Miller 		fib_select_default(&res);
19721da177e4SLinus Torvalds 
1973813b3b5dSDavid S. Miller 	if (!fl4->saddr)
1974813b3b5dSDavid S. Miller 		fl4->saddr = FIB_RES_PREFSRC(net, res);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	dev_out = FIB_RES_DEV(res);
1977813b3b5dSDavid S. Miller 	fl4->flowi4_oif = dev_out->ifindex;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds make_route:
19811a00fee4SDavid Miller 	rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags);
19821da177e4SLinus Torvalds 
1983010c2708SDavid S. Miller out:
1984010c2708SDavid S. Miller 	rcu_read_unlock();
1985b23dd4feSDavid S. Miller 	return rth;
19861da177e4SLinus Torvalds }
1987d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key);
1988d8c97a94SArnaldo Carvalho de Melo 
1989ae2688d5SJianzhao Wang static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie)
1990ae2688d5SJianzhao Wang {
1991ae2688d5SJianzhao Wang 	return NULL;
1992ae2688d5SJianzhao Wang }
1993ae2688d5SJianzhao Wang 
1994ebb762f2SSteffen Klassert static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst)
1995ec831ea7SRoland Dreier {
1996618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1997618f9bc7SSteffen Klassert 
1998618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
1999ec831ea7SRoland Dreier }
2000ec831ea7SRoland Dreier 
20016700c270SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
20026700c270SDavid S. Miller 					  struct sk_buff *skb, u32 mtu)
200314e50e57SDavid S. Miller {
200414e50e57SDavid S. Miller }
200514e50e57SDavid S. Miller 
20066700c270SDavid S. Miller static void ipv4_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
20076700c270SDavid S. Miller 				       struct sk_buff *skb)
2008b587ee3bSDavid S. Miller {
2009b587ee3bSDavid S. Miller }
2010b587ee3bSDavid S. Miller 
20110972ddb2SHeld Bernhard static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst,
20120972ddb2SHeld Bernhard 					  unsigned long old)
20130972ddb2SHeld Bernhard {
20140972ddb2SHeld Bernhard 	return NULL;
20150972ddb2SHeld Bernhard }
20160972ddb2SHeld Bernhard 
201714e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
201814e50e57SDavid S. Miller 	.family			=	AF_INET,
201909640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IP),
2020ae2688d5SJianzhao Wang 	.check			=	ipv4_blackhole_dst_check,
2021ebb762f2SSteffen Klassert 	.mtu			=	ipv4_blackhole_mtu,
2022214f45c9SEric Dumazet 	.default_advmss		=	ipv4_default_advmss,
202314e50e57SDavid S. Miller 	.update_pmtu		=	ipv4_rt_blackhole_update_pmtu,
2024b587ee3bSDavid S. Miller 	.redirect		=	ipv4_rt_blackhole_redirect,
20250972ddb2SHeld Bernhard 	.cow_metrics		=	ipv4_rt_blackhole_cow_metrics,
2026d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ipv4_neigh_lookup,
202714e50e57SDavid S. Miller };
202814e50e57SDavid S. Miller 
20292774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
203014e50e57SDavid S. Miller {
20312774c131SDavid S. Miller 	struct rtable *ort = (struct rtable *) dst_orig;
2032f5b0a874SDavid S. Miller 	struct rtable *rt;
203314e50e57SDavid S. Miller 
2034f5b0a874SDavid S. Miller 	rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
203514e50e57SDavid S. Miller 	if (rt) {
2036d8d1f30bSChangli Gao 		struct dst_entry *new = &rt->dst;
203714e50e57SDavid S. Miller 
203814e50e57SDavid S. Miller 		new->__use = 1;
2039352e512cSHerbert Xu 		new->input = dst_discard;
2040352e512cSHerbert Xu 		new->output = dst_discard;
204114e50e57SDavid S. Miller 
2042d8d1f30bSChangli Gao 		new->dev = ort->dst.dev;
204314e50e57SDavid S. Miller 		if (new->dev)
204414e50e57SDavid S. Miller 			dev_hold(new->dev);
204514e50e57SDavid S. Miller 
20469917e1e8SDavid S. Miller 		rt->rt_is_input = ort->rt_is_input;
20475e2b61f7SDavid S. Miller 		rt->rt_iif = ort->rt_iif;
20485943634fSDavid S. Miller 		rt->rt_pmtu = ort->rt_pmtu;
204914e50e57SDavid S. Miller 
2050e84f84f2SDenis V. Lunev 		rt->rt_genid = rt_genid(net);
205114e50e57SDavid S. Miller 		rt->rt_flags = ort->rt_flags;
205214e50e57SDavid S. Miller 		rt->rt_type = ort->rt_type;
205314e50e57SDavid S. Miller 		rt->rt_gateway = ort->rt_gateway;
205414e50e57SDavid S. Miller 
205514e50e57SDavid S. Miller 		dst_free(new);
205614e50e57SDavid S. Miller 	}
205714e50e57SDavid S. Miller 
20582774c131SDavid S. Miller 	dst_release(dst_orig);
20592774c131SDavid S. Miller 
20602774c131SDavid S. Miller 	return rt ? &rt->dst : ERR_PTR(-ENOMEM);
206114e50e57SDavid S. Miller }
206214e50e57SDavid S. Miller 
20639d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
2064273447b3SDavid S. Miller 				    struct sock *sk)
20651da177e4SLinus Torvalds {
20669d6ec938SDavid S. Miller 	struct rtable *rt = __ip_route_output_key(net, flp4);
20671da177e4SLinus Torvalds 
2068b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
2069b23dd4feSDavid S. Miller 		return rt;
20701da177e4SLinus Torvalds 
207156157872SDavid S. Miller 	if (flp4->flowi4_proto)
20729d6ec938SDavid S. Miller 		rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
20739d6ec938SDavid S. Miller 						   flowi4_to_flowi(flp4),
20749d6ec938SDavid S. Miller 						   sk, 0);
20751da177e4SLinus Torvalds 
2076b23dd4feSDavid S. Miller 	return rt;
20771da177e4SLinus Torvalds }
2078d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2079d8c97a94SArnaldo Carvalho de Melo 
2080f1ce3062SDavid S. Miller static int rt_fill_info(struct net *net,  __be32 dst, __be32 src,
2081f1ce3062SDavid S. Miller 			struct flowi4 *fl4, struct sk_buff *skb, u32 pid,
2082f1ce3062SDavid S. Miller 			u32 seq, int event, int nowait, unsigned int flags)
20831da177e4SLinus Torvalds {
2084511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
20851da177e4SLinus Torvalds 	struct rtmsg *r;
20861da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
20872bc8ca40SSteffen Klassert 	unsigned long expires = 0;
2088f185071dSDavid S. Miller 	u32 error;
2089521f5490SJulian Anastasov 	u32 metrics[RTAX_MAX];
2090be403ea1SThomas Graf 
2091be403ea1SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2092be403ea1SThomas Graf 	if (nlh == NULL)
209326932566SPatrick McHardy 		return -EMSGSIZE;
2094be403ea1SThomas Graf 
2095be403ea1SThomas Graf 	r = nlmsg_data(nlh);
20961da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
20971da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
20981da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
2099d6c0a4f6SDavid Miller 	r->rtm_tos	= fl4->flowi4_tos;
21001da177e4SLinus Torvalds 	r->rtm_table	= RT_TABLE_MAIN;
2101f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN))
2102f3756b79SDavid S. Miller 		goto nla_put_failure;
21031da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
21041da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
21051da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
21061da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
21071da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
21081da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
2109be403ea1SThomas Graf 
2110f1ce3062SDavid S. Miller 	if (nla_put_be32(skb, RTA_DST, dst))
2111f3756b79SDavid S. Miller 		goto nla_put_failure;
21121a00fee4SDavid Miller 	if (src) {
21131da177e4SLinus Torvalds 		r->rtm_src_len = 32;
21141a00fee4SDavid Miller 		if (nla_put_be32(skb, RTA_SRC, src))
2115f3756b79SDavid S. Miller 			goto nla_put_failure;
21161da177e4SLinus Torvalds 	}
2117f3756b79SDavid S. Miller 	if (rt->dst.dev &&
2118f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2119f3756b79SDavid S. Miller 		goto nla_put_failure;
2120c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2121f3756b79SDavid S. Miller 	if (rt->dst.tclassid &&
2122f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
2123f3756b79SDavid S. Miller 		goto nla_put_failure;
21241da177e4SLinus Torvalds #endif
212541347dcdSDavid S. Miller 	if (!rt_is_input_route(rt) &&
2126d6c0a4f6SDavid Miller 	    fl4->saddr != src) {
2127d6c0a4f6SDavid Miller 		if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr))
2128f3756b79SDavid S. Miller 			goto nla_put_failure;
2129f3756b79SDavid S. Miller 	}
2130f8126f1dSDavid S. Miller 	if (rt->rt_gateway &&
2131f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway))
2132f3756b79SDavid S. Miller 		goto nla_put_failure;
2133be403ea1SThomas Graf 
2134521f5490SJulian Anastasov 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
2135521f5490SJulian Anastasov 	if (rt->rt_pmtu)
2136521f5490SJulian Anastasov 		metrics[RTAX_MTU - 1] = rt->rt_pmtu;
2137521f5490SJulian Anastasov 	if (rtnetlink_put_metrics(skb, metrics) < 0)
2138be403ea1SThomas Graf 		goto nla_put_failure;
2139be403ea1SThomas Graf 
2140b4869889SDavid Miller 	if (fl4->flowi4_mark &&
2141b4869889SDavid Miller 	    nla_put_be32(skb, RTA_MARK, fl4->flowi4_mark))
2142f3756b79SDavid S. Miller 		goto nla_put_failure;
2143963bfeeeSEric Dumazet 
2144d8d1f30bSChangli Gao 	error = rt->dst.error;
21455943634fSDavid S. Miller 	expires = rt->dst.expires;
21462bc8ca40SSteffen Klassert 	if (expires) {
21472bc8ca40SSteffen Klassert 		if (time_before(jiffies, expires))
2148fe6fe792SEric Dumazet 			expires -= jiffies;
21492bc8ca40SSteffen Klassert 		else
21502bc8ca40SSteffen Klassert 			expires = 0;
21512bc8ca40SSteffen Klassert 	}
2152be403ea1SThomas Graf 
2153c7537967SDavid S. Miller 	if (rt_is_input_route(rt)) {
2154f3756b79SDavid S. Miller 		if (nla_put_u32(skb, RTA_IIF, rt->rt_iif))
2155f3756b79SDavid S. Miller 			goto nla_put_failure;
21561da177e4SLinus Torvalds 	}
21571da177e4SLinus Torvalds 
2158f185071dSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
2159e3703b3dSThomas Graf 		goto nla_put_failure;
21601da177e4SLinus Torvalds 
2161be403ea1SThomas Graf 	return nlmsg_end(skb, nlh);
2162be403ea1SThomas Graf 
2163be403ea1SThomas Graf nla_put_failure:
216426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
216526932566SPatrick McHardy 	return -EMSGSIZE;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds 
216863f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
21691da177e4SLinus Torvalds {
21703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2171d889ce3bSThomas Graf 	struct rtmsg *rtm;
2172d889ce3bSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
21731da177e4SLinus Torvalds 	struct rtable *rt = NULL;
2174d6c0a4f6SDavid Miller 	struct flowi4 fl4;
21759e12bb22SAl Viro 	__be32 dst = 0;
21769e12bb22SAl Viro 	__be32 src = 0;
21779e12bb22SAl Viro 	u32 iif;
2178d889ce3bSThomas Graf 	int err;
2179963bfeeeSEric Dumazet 	int mark;
21801da177e4SLinus Torvalds 	struct sk_buff *skb;
21811da177e4SLinus Torvalds 
2182d889ce3bSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2183d889ce3bSThomas Graf 	if (err < 0)
2184d889ce3bSThomas Graf 		goto errout;
2185d889ce3bSThomas Graf 
2186d889ce3bSThomas Graf 	rtm = nlmsg_data(nlh);
2187d889ce3bSThomas Graf 
21881da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2189d889ce3bSThomas Graf 	if (skb == NULL) {
2190d889ce3bSThomas Graf 		err = -ENOBUFS;
2191d889ce3bSThomas Graf 		goto errout;
2192d889ce3bSThomas Graf 	}
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
21951da177e4SLinus Torvalds 	   through good chunk of routing engine.
21961da177e4SLinus Torvalds 	 */
2197459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
2198c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2199d2c962b8SStephen Hemminger 
2200d2c962b8SStephen Hemminger 	/* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2201eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = IPPROTO_ICMP;
22021da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
22031da177e4SLinus Torvalds 
220417fb2c64SAl Viro 	src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
220517fb2c64SAl Viro 	dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2206d889ce3bSThomas Graf 	iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
2207963bfeeeSEric Dumazet 	mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
22081da177e4SLinus Torvalds 
2209d6c0a4f6SDavid Miller 	memset(&fl4, 0, sizeof(fl4));
2210d6c0a4f6SDavid Miller 	fl4.daddr = dst;
2211d6c0a4f6SDavid Miller 	fl4.saddr = src;
2212d6c0a4f6SDavid Miller 	fl4.flowi4_tos = rtm->rtm_tos;
2213d6c0a4f6SDavid Miller 	fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
2214d6c0a4f6SDavid Miller 	fl4.flowi4_mark = mark;
2215d6c0a4f6SDavid Miller 
22161da177e4SLinus Torvalds 	if (iif) {
2217d889ce3bSThomas Graf 		struct net_device *dev;
2218d889ce3bSThomas Graf 
22191937504dSDenis V. Lunev 		dev = __dev_get_by_index(net, iif);
2220d889ce3bSThomas Graf 		if (dev == NULL) {
22211da177e4SLinus Torvalds 			err = -ENODEV;
2222d889ce3bSThomas Graf 			goto errout_free;
2223d889ce3bSThomas Graf 		}
2224d889ce3bSThomas Graf 
22251da177e4SLinus Torvalds 		skb->protocol	= htons(ETH_P_IP);
22261da177e4SLinus Torvalds 		skb->dev	= dev;
2227963bfeeeSEric Dumazet 		skb->mark	= mark;
22281da177e4SLinus Torvalds 		local_bh_disable();
22291da177e4SLinus Torvalds 		err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
22301da177e4SLinus Torvalds 		local_bh_enable();
2231d889ce3bSThomas Graf 
2232511c3f92SEric Dumazet 		rt = skb_rtable(skb);
2233d8d1f30bSChangli Gao 		if (err == 0 && rt->dst.error)
2234d8d1f30bSChangli Gao 			err = -rt->dst.error;
22351da177e4SLinus Torvalds 	} else {
22369d6ec938SDavid S. Miller 		rt = ip_route_output_key(net, &fl4);
2237b23dd4feSDavid S. Miller 
2238b23dd4feSDavid S. Miller 		err = 0;
2239b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
2240b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
22411da177e4SLinus Torvalds 	}
2242d889ce3bSThomas Graf 
22431da177e4SLinus Torvalds 	if (err)
2244d889ce3bSThomas Graf 		goto errout_free;
22451da177e4SLinus Torvalds 
2246d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
22471da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
22481da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
22491da177e4SLinus Torvalds 
2250f1ce3062SDavid S. Miller 	err = rt_fill_info(net, dst, src, &fl4, skb,
22511a00fee4SDavid Miller 			   NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2252b6544c0bSJamal Hadi Salim 			   RTM_NEWROUTE, 0, 0);
2253d889ce3bSThomas Graf 	if (err <= 0)
2254d889ce3bSThomas Graf 		goto errout_free;
22551da177e4SLinus Torvalds 
22561937504dSDenis V. Lunev 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2257d889ce3bSThomas Graf errout:
22582942e900SThomas Graf 	return err;
22591da177e4SLinus Torvalds 
2260d889ce3bSThomas Graf errout_free:
22611da177e4SLinus Torvalds 	kfree_skb(skb);
2262d889ce3bSThomas Graf 	goto errout;
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
22661da177e4SLinus Torvalds {
22671da177e4SLinus Torvalds 	return skb->len;
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
22711da177e4SLinus Torvalds {
227276e6ebfbSDenis V. Lunev 	rt_cache_flush(dev_net(in_dev->dev), 0);
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
227681c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write,
22778d65af78SAlexey Dobriyan 					void __user *buffer,
22781da177e4SLinus Torvalds 					size_t *lenp, loff_t *ppos)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	if (write) {
2281639e104fSDenis V. Lunev 		int flush_delay;
228281c684d1SDenis V. Lunev 		ctl_table ctl;
228339a23e75SDenis V. Lunev 		struct net *net;
2284639e104fSDenis V. Lunev 
228581c684d1SDenis V. Lunev 		memcpy(&ctl, __ctl, sizeof(ctl));
228681c684d1SDenis V. Lunev 		ctl.data = &flush_delay;
22878d65af78SAlexey Dobriyan 		proc_dointvec(&ctl, write, buffer, lenp, ppos);
2288639e104fSDenis V. Lunev 
228981c684d1SDenis V. Lunev 		net = (struct net *)__ctl->extra1;
229039a23e75SDenis V. Lunev 		rt_cache_flush(net, flush_delay);
22911da177e4SLinus Torvalds 		return 0;
22921da177e4SLinus Torvalds 	}
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	return -EINVAL;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
2297eeb61f71SAl Viro static ctl_table ipv4_route_table[] = {
22981da177e4SLinus Torvalds 	{
22991da177e4SLinus Torvalds 		.procname	= "gc_thresh",
23001da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
23011da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23021da177e4SLinus Torvalds 		.mode		= 0644,
23036d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23041da177e4SLinus Torvalds 	},
23051da177e4SLinus Torvalds 	{
23061da177e4SLinus Torvalds 		.procname	= "max_size",
23071da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
23081da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23091da177e4SLinus Torvalds 		.mode		= 0644,
23106d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23111da177e4SLinus Torvalds 	},
23121da177e4SLinus Torvalds 	{
23131da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
23161da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
23171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23181da177e4SLinus Torvalds 		.mode		= 0644,
23196d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
23201da177e4SLinus Torvalds 	},
23211da177e4SLinus Torvalds 	{
23221da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
23231da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
23241da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23251da177e4SLinus Torvalds 		.mode		= 0644,
23266d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
23271da177e4SLinus Torvalds 	},
23281da177e4SLinus Torvalds 	{
23291da177e4SLinus Torvalds 		.procname	= "gc_timeout",
23301da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
23311da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23321da177e4SLinus Torvalds 		.mode		= 0644,
23336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
23341da177e4SLinus Torvalds 	},
23351da177e4SLinus Torvalds 	{
23369f28a2fcSEric Dumazet 		.procname	= "gc_interval",
23379f28a2fcSEric Dumazet 		.data		= &ip_rt_gc_interval,
23389f28a2fcSEric Dumazet 		.maxlen		= sizeof(int),
23399f28a2fcSEric Dumazet 		.mode		= 0644,
23409f28a2fcSEric Dumazet 		.proc_handler	= proc_dointvec_jiffies,
23419f28a2fcSEric Dumazet 	},
23429f28a2fcSEric Dumazet 	{
23431da177e4SLinus Torvalds 		.procname	= "redirect_load",
23441da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
23451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23461da177e4SLinus Torvalds 		.mode		= 0644,
23476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23481da177e4SLinus Torvalds 	},
23491da177e4SLinus Torvalds 	{
23501da177e4SLinus Torvalds 		.procname	= "redirect_number",
23511da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
23521da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23531da177e4SLinus Torvalds 		.mode		= 0644,
23546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23551da177e4SLinus Torvalds 	},
23561da177e4SLinus Torvalds 	{
23571da177e4SLinus Torvalds 		.procname	= "redirect_silence",
23581da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
23591da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23601da177e4SLinus Torvalds 		.mode		= 0644,
23616d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23621da177e4SLinus Torvalds 	},
23631da177e4SLinus Torvalds 	{
23641da177e4SLinus Torvalds 		.procname	= "error_cost",
23651da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
23661da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23671da177e4SLinus Torvalds 		.mode		= 0644,
23686d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23691da177e4SLinus Torvalds 	},
23701da177e4SLinus Torvalds 	{
23711da177e4SLinus Torvalds 		.procname	= "error_burst",
23721da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
23731da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23741da177e4SLinus Torvalds 		.mode		= 0644,
23756d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23761da177e4SLinus Torvalds 	},
23771da177e4SLinus Torvalds 	{
23781da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
23791da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
23801da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23811da177e4SLinus Torvalds 		.mode		= 0644,
23826d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23831da177e4SLinus Torvalds 	},
23841da177e4SLinus Torvalds 	{
23851da177e4SLinus Torvalds 		.procname	= "mtu_expires",
23861da177e4SLinus Torvalds 		.data		= &ip_rt_mtu_expires,
23871da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23881da177e4SLinus Torvalds 		.mode		= 0644,
23896d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
23901da177e4SLinus Torvalds 	},
23911da177e4SLinus Torvalds 	{
23921da177e4SLinus Torvalds 		.procname	= "min_pmtu",
23931da177e4SLinus Torvalds 		.data		= &ip_rt_min_pmtu,
23941da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
23951da177e4SLinus Torvalds 		.mode		= 0644,
23966d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
23971da177e4SLinus Torvalds 	},
23981da177e4SLinus Torvalds 	{
23991da177e4SLinus Torvalds 		.procname	= "min_adv_mss",
24001da177e4SLinus Torvalds 		.data		= &ip_rt_min_advmss,
24011da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
24021da177e4SLinus Torvalds 		.mode		= 0644,
24036d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
24041da177e4SLinus Torvalds 	},
2405f8572d8fSEric W. Biederman 	{ }
24061da177e4SLinus Torvalds };
240739a23e75SDenis V. Lunev 
240839a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = {
240939a23e75SDenis V. Lunev 	{
241039a23e75SDenis V. Lunev 		.procname	= "flush",
241139a23e75SDenis V. Lunev 		.maxlen		= sizeof(int),
241239a23e75SDenis V. Lunev 		.mode		= 0200,
24136d9f239aSAlexey Dobriyan 		.proc_handler	= ipv4_sysctl_rtcache_flush,
241439a23e75SDenis V. Lunev 	},
2415f8572d8fSEric W. Biederman 	{ },
241639a23e75SDenis V. Lunev };
241739a23e75SDenis V. Lunev 
241839a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net)
241939a23e75SDenis V. Lunev {
242039a23e75SDenis V. Lunev 	struct ctl_table *tbl;
242139a23e75SDenis V. Lunev 
242239a23e75SDenis V. Lunev 	tbl = ipv4_route_flush_table;
242309ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
242439a23e75SDenis V. Lunev 		tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
242539a23e75SDenis V. Lunev 		if (tbl == NULL)
242639a23e75SDenis V. Lunev 			goto err_dup;
242739a23e75SDenis V. Lunev 	}
242839a23e75SDenis V. Lunev 	tbl[0].extra1 = net;
242939a23e75SDenis V. Lunev 
2430ec8f23ceSEric W. Biederman 	net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
243139a23e75SDenis V. Lunev 	if (net->ipv4.route_hdr == NULL)
243239a23e75SDenis V. Lunev 		goto err_reg;
243339a23e75SDenis V. Lunev 	return 0;
243439a23e75SDenis V. Lunev 
243539a23e75SDenis V. Lunev err_reg:
243639a23e75SDenis V. Lunev 	if (tbl != ipv4_route_flush_table)
243739a23e75SDenis V. Lunev 		kfree(tbl);
243839a23e75SDenis V. Lunev err_dup:
243939a23e75SDenis V. Lunev 	return -ENOMEM;
244039a23e75SDenis V. Lunev }
244139a23e75SDenis V. Lunev 
244239a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net)
244339a23e75SDenis V. Lunev {
244439a23e75SDenis V. Lunev 	struct ctl_table *tbl;
244539a23e75SDenis V. Lunev 
244639a23e75SDenis V. Lunev 	tbl = net->ipv4.route_hdr->ctl_table_arg;
244739a23e75SDenis V. Lunev 	unregister_net_sysctl_table(net->ipv4.route_hdr);
244839a23e75SDenis V. Lunev 	BUG_ON(tbl == ipv4_route_flush_table);
244939a23e75SDenis V. Lunev 	kfree(tbl);
245039a23e75SDenis V. Lunev }
245139a23e75SDenis V. Lunev 
245239a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = {
245339a23e75SDenis V. Lunev 	.init = sysctl_route_net_init,
245439a23e75SDenis V. Lunev 	.exit = sysctl_route_net_exit,
245539a23e75SDenis V. Lunev };
24561da177e4SLinus Torvalds #endif
24571da177e4SLinus Torvalds 
24583ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net)
24599f5e97e5SDenis V. Lunev {
24603ee94372SNeil Horman 	get_random_bytes(&net->ipv4.rt_genid,
24613ee94372SNeil Horman 			 sizeof(net->ipv4.rt_genid));
2462436c3b66SDavid S. Miller 	get_random_bytes(&net->ipv4.dev_addr_genid,
2463436c3b66SDavid S. Miller 			 sizeof(net->ipv4.dev_addr_genid));
24649f5e97e5SDenis V. Lunev 	return 0;
24659f5e97e5SDenis V. Lunev }
24669f5e97e5SDenis V. Lunev 
24673ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = {
24683ee94372SNeil Horman 	.init = rt_genid_init,
24699f5e97e5SDenis V. Lunev };
24709f5e97e5SDenis V. Lunev 
2471c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net)
2472c3426b47SDavid S. Miller {
2473c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
2474c3426b47SDavid S. Miller 
2475c3426b47SDavid S. Miller 	if (!bp)
2476c3426b47SDavid S. Miller 		return -ENOMEM;
2477c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
2478c3426b47SDavid S. Miller 	net->ipv4.peers = bp;
2479c3426b47SDavid S. Miller 	return 0;
2480c3426b47SDavid S. Miller }
2481c3426b47SDavid S. Miller 
2482c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net)
2483c3426b47SDavid S. Miller {
2484c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv4.peers;
2485c3426b47SDavid S. Miller 
2486c3426b47SDavid S. Miller 	net->ipv4.peers = NULL;
248756a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
2488c3426b47SDavid S. Miller 	kfree(bp);
2489c3426b47SDavid S. Miller }
2490c3426b47SDavid S. Miller 
2491c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
2492c3426b47SDavid S. Miller 	.init	=	ipv4_inetpeer_init,
2493c3426b47SDavid S. Miller 	.exit	=	ipv4_inetpeer_exit,
2494c3426b47SDavid S. Miller };
24959f5e97e5SDenis V. Lunev 
2496c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
24977d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
2498c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds int __init ip_rt_init(void)
25011da177e4SLinus Torvalds {
2502424c4b70SEric Dumazet 	int rc = 0;
25031da177e4SLinus Torvalds 
2504c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
25050dcec8c2SIngo Molnar 	ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
25061da177e4SLinus Torvalds 	if (!ip_rt_acct)
25071da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
25081da177e4SLinus Torvalds #endif
25091da177e4SLinus Torvalds 
2510e5d679f3SAlexey Dobriyan 	ipv4_dst_ops.kmem_cachep =
2511e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
251220c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
25131da177e4SLinus Torvalds 
251414e50e57SDavid S. Miller 	ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
251514e50e57SDavid S. Miller 
2516fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_ops) < 0)
2517fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_ops counter\n");
2518fc66f95cSEric Dumazet 
2519fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
2520fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
2521fc66f95cSEric Dumazet 
252289aef892SDavid S. Miller 	ipv4_dst_ops.gc_thresh = ~0;
252389aef892SDavid S. Miller 	ip_rt_max_size = INT_MAX;
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds 	devinet_init();
25261da177e4SLinus Torvalds 	ip_fib_init();
25271da177e4SLinus Torvalds 
252873b38711SDenis V. Lunev 	if (ip_rt_proc_init())
2529058bd4d2SJoe Perches 		pr_err("Unable to create route proc files\n");
25301da177e4SLinus Torvalds #ifdef CONFIG_XFRM
25311da177e4SLinus Torvalds 	xfrm_init();
2532a33bc5c1SNeil Horman 	xfrm4_init(ip_rt_max_size);
25331da177e4SLinus Torvalds #endif
2534c7ac8679SGreg Rose 	rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL);
253563f3444fSThomas Graf 
253639a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL
253739a23e75SDenis V. Lunev 	register_pernet_subsys(&sysctl_route_ops);
253839a23e75SDenis V. Lunev #endif
25393ee94372SNeil Horman 	register_pernet_subsys(&rt_genid_ops);
2540c3426b47SDavid S. Miller 	register_pernet_subsys(&ipv4_inetpeer_ops);
25411da177e4SLinus Torvalds 	return rc;
25421da177e4SLinus Torvalds }
25431da177e4SLinus Torvalds 
2544a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL
2545eeb61f71SAl Viro /*
2546eeb61f71SAl Viro  * We really need to sanitize the damn ipv4 init order, then all
2547eeb61f71SAl Viro  * this nonsense will go away.
2548eeb61f71SAl Viro  */
2549eeb61f71SAl Viro void __init ip_static_sysctl_init(void)
2550eeb61f71SAl Viro {
25514e5ca785SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
2552eeb61f71SAl Viro }
2553a1bc6eb4SAl Viro #endif
2554