xref: /linux/net/ipv4/route.c (revision 6ac66cb03ae306c2e288a9be18226310529f5b25)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		ROUTE - implementation of the IP router.
81da177e4SLinus Torvalds  *
902c30a84SJesper Juhl  * Authors:	Ross Biro
101da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
111da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
121da177e4SLinus Torvalds  *		Linus Torvalds, <Linus.Torvalds@helsinki.fi>
131da177e4SLinus Torvalds  *		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Fixes:
161da177e4SLinus Torvalds  *		Alan Cox	:	Verify area fixes.
171da177e4SLinus Torvalds  *		Alan Cox	:	cli() protects routing changes
181da177e4SLinus Torvalds  *		Rui Oliveira	:	ICMP routing table updates
191da177e4SLinus Torvalds  *		(rco@di.uminho.pt)	Routing table insertion and update
201da177e4SLinus Torvalds  *		Linus Torvalds	:	Rewrote bits to be sensible
211da177e4SLinus Torvalds  *		Alan Cox	:	Added BSD route gw semantics
221da177e4SLinus Torvalds  *		Alan Cox	:	Super /proc >4K
231da177e4SLinus Torvalds  *		Alan Cox	:	MTU in route table
241da177e4SLinus Torvalds  *		Alan Cox	:	MSS actually. Also added the window
251da177e4SLinus Torvalds  *					clamper.
261da177e4SLinus Torvalds  *		Sam Lantinga	:	Fixed route matching in rt_del()
271da177e4SLinus Torvalds  *		Alan Cox	:	Routing cache support.
281da177e4SLinus Torvalds  *		Alan Cox	:	Removed compatibility cruft.
291da177e4SLinus Torvalds  *		Alan Cox	:	RTF_REJECT support.
301da177e4SLinus Torvalds  *		Alan Cox	:	TCP irtt support.
311da177e4SLinus Torvalds  *		Jonathan Naylor	:	Added Metric support.
321da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	BSD API fixes.
331da177e4SLinus Torvalds  *	Miquel van Smoorenburg	:	Metrics.
341da177e4SLinus Torvalds  *		Alan Cox	:	Use __u32 properly
351da177e4SLinus Torvalds  *		Alan Cox	:	Aligned routing errors more closely with BSD
361da177e4SLinus Torvalds  *					our system is still very different.
371da177e4SLinus Torvalds  *		Alan Cox	:	Faster /proc handling
381da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Massive rework to support tree based routing,
391da177e4SLinus Torvalds  *					routing caches and better behaviour.
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  *		Olaf Erb	:	irtt wasn't being copied right.
421da177e4SLinus Torvalds  *		Bjorn Ekwall	:	Kerneld route support.
431da177e4SLinus Torvalds  *		Alan Cox	:	Multicast fixed (I hope)
441da177e4SLinus Torvalds  *		Pavel Krauz	:	Limited broadcast fixed
451da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
461da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	End of old history. Split to fib.c and
471da177e4SLinus Torvalds  *					route.c and rewritten from scratch.
481da177e4SLinus Torvalds  *		Andi Kleen	:	Load-limit warning messages.
491da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
501da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Race condition in ip_route_input_slow.
511da177e4SLinus Torvalds  *	Tobias Ringstrom	:	Uninitialized res.type in ip_route_output_slow.
521da177e4SLinus Torvalds  *	Vladimir V. Ivanov	:	IP rule info (flowid) is really useful.
531da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
541da177e4SLinus Torvalds  *	Robert Olsson		:	Added rt_cache statistics
551da177e4SLinus Torvalds  *	Arnaldo C. Melo		:	Convert proc stuff to seq_file
56bb1d23b0SEric Dumazet  *	Eric Dumazet		:	hashed spinlocks and rt_check_expire() fixes.
57cef2685eSIlia Sotnikov  *	Ilia Sotnikov		:	Ignore TOS on PMTUD and Redirect
58cef2685eSIlia Sotnikov  *	Ilia Sotnikov		:	Removed TOS from hash calculations
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
61afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt
62afd46503SJoe Perches 
631da177e4SLinus Torvalds #include <linux/module.h>
641da177e4SLinus Torvalds #include <linux/bitops.h>
651da177e4SLinus Torvalds #include <linux/kernel.h>
661da177e4SLinus Torvalds #include <linux/mm.h>
67aa6dd211SEric Dumazet #include <linux/memblock.h>
681da177e4SLinus Torvalds #include <linux/socket.h>
691da177e4SLinus Torvalds #include <linux/errno.h>
701da177e4SLinus Torvalds #include <linux/in.h>
711da177e4SLinus Torvalds #include <linux/inet.h>
721da177e4SLinus Torvalds #include <linux/netdevice.h>
731da177e4SLinus Torvalds #include <linux/proc_fs.h>
741da177e4SLinus Torvalds #include <linux/init.h>
751da177e4SLinus Torvalds #include <linux/skbuff.h>
761da177e4SLinus Torvalds #include <linux/inetdevice.h>
771da177e4SLinus Torvalds #include <linux/igmp.h>
781da177e4SLinus Torvalds #include <linux/pkt_sched.h>
791da177e4SLinus Torvalds #include <linux/mroute.h>
801da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
811da177e4SLinus Torvalds #include <linux/random.h>
821da177e4SLinus Torvalds #include <linux/rcupdate.h>
835a0e3ad6STejun Heo #include <linux/slab.h>
8473f156a6SEric Dumazet #include <linux/jhash.h>
85352e512cSHerbert Xu #include <net/dst.h>
861b7179d3SThomas Graf #include <net/dst_metadata.h>
8732ccf110SGuillaume Nault #include <net/inet_dscp.h>
88457c4cbcSEric W. Biederman #include <net/net_namespace.h>
891da177e4SLinus Torvalds #include <net/ip.h>
901da177e4SLinus Torvalds #include <net/route.h>
911da177e4SLinus Torvalds #include <net/inetpeer.h>
921da177e4SLinus Torvalds #include <net/sock.h>
931da177e4SLinus Torvalds #include <net/ip_fib.h>
945481d73fSDavid Ahern #include <net/nexthop.h>
951da177e4SLinus Torvalds #include <net/tcp.h>
961da177e4SLinus Torvalds #include <net/icmp.h>
971da177e4SLinus Torvalds #include <net/xfrm.h>
98571e7226SRoopa Prabhu #include <net/lwtunnel.h>
998d71740cSTom Tucker #include <net/netevent.h>
10063f3444fSThomas Graf #include <net/rtnetlink.h>
1011da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1021da177e4SLinus Torvalds #include <linux/sysctl.h>
1031da177e4SLinus Torvalds #endif
1046e5714eaSDavid S. Miller #include <net/secure_seq.h>
1051b7179d3SThomas Graf #include <net/ip_tunnels.h>
1061da177e4SLinus Torvalds 
107b6179813SRoopa Prabhu #include "fib_lookup.h"
108b6179813SRoopa Prabhu 
10968a5e3ddSDavid S. Miller #define RT_FL_TOS(oldflp4) \
110f61759e6SJulian Anastasov 	((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ)
1131da177e4SLinus Torvalds 
1141de6b15aSxu xin #define DEFAULT_MIN_PMTU (512 + 20 + 20)
1151135fad2Sxu xin #define DEFAULT_MTU_EXPIRES (10 * 60 * HZ)
1162e9589ffSxu xin #define DEFAULT_MIN_ADVMSS 256
1171da177e4SLinus Torvalds static int ip_rt_max_size;
118817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly	= 9;
119817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly	= HZ / 50;
120817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly	= ((HZ / 50) << (9 + 1));
121817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly	= HZ;
122817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly	= 5 * HZ;
1239f28a2fcSEric Dumazet 
124deed49dfSXin Long static int ip_rt_gc_timeout __read_mostly	= RT_GC_TIMEOUT;
125c7272c2fSSabrina Dubroca 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  *	Interface to generic destination cache.
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds 
130bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE
131bbd807dfSBrian Vazquez struct dst_entry	*ipv4_dst_check(struct dst_entry *dst, u32 cookie);
1320dbaee3bSDavid S. Miller static unsigned int	 ipv4_default_advmss(const struct dst_entry *dst);
133f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE
134f67fbeaeSBrian Vazquez unsigned int		ipv4_mtu(const struct dst_entry *dst);
1351da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
1361da177e4SLinus Torvalds static void		 ipv4_link_failure(struct sk_buff *skb);
1376700c270SDavid S. Miller static void		 ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
138bd085ef6SHangbin Liu 					   struct sk_buff *skb, u32 mtu,
139bd085ef6SHangbin Liu 					   bool confirm_neigh);
1406700c270SDavid S. Miller static void		 ip_do_redirect(struct dst_entry *dst, struct sock *sk,
1416700c270SDavid S. Miller 					struct sk_buff *skb);
142caacf05eSDavid S. Miller static void		ipv4_dst_destroy(struct dst_entry *dst);
1431da177e4SLinus Torvalds 
14462fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
14562fa8a84SDavid S. Miller {
14631248731SDavid S. Miller 	WARN_ON(1);
14731248731SDavid S. Miller 	return NULL;
14862fa8a84SDavid S. Miller }
14962fa8a84SDavid S. Miller 
150f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
151f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
152f894cbf8SDavid S. Miller 					   const void *daddr);
15363fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr);
154d3aaeb38SDavid S. Miller 
1551da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1561da177e4SLinus Torvalds 	.family =		AF_INET,
1571da177e4SLinus Torvalds 	.check =		ipv4_dst_check,
1580dbaee3bSDavid S. Miller 	.default_advmss =	ipv4_default_advmss,
159ebb762f2SSteffen Klassert 	.mtu =			ipv4_mtu,
16062fa8a84SDavid S. Miller 	.cow_metrics =		ipv4_cow_metrics,
161caacf05eSDavid S. Miller 	.destroy =		ipv4_dst_destroy,
1621da177e4SLinus Torvalds 	.negative_advice =	ipv4_negative_advice,
1631da177e4SLinus Torvalds 	.link_failure =		ipv4_link_failure,
1641da177e4SLinus Torvalds 	.update_pmtu =		ip_rt_update_pmtu,
165e47a185bSDavid S. Miller 	.redirect =		ip_do_redirect,
166b92dacd4SEric W. Biederman 	.local_out =		__ip_local_out,
167d3aaeb38SDavid S. Miller 	.neigh_lookup =		ipv4_neigh_lookup,
16863fca65dSJulian Anastasov 	.confirm_neigh =	ipv4_confirm_neigh,
1691da177e4SLinus Torvalds };
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define ECN_OR_COST(class)	TC_PRIO_##class
1721da177e4SLinus Torvalds 
1734839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1741da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1754a2b9c37SDan Siemon 	ECN_OR_COST(BESTEFFORT),
1761da177e4SLinus Torvalds 	TC_PRIO_BESTEFFORT,
1771da177e4SLinus Torvalds 	ECN_OR_COST(BESTEFFORT),
1781da177e4SLinus Torvalds 	TC_PRIO_BULK,
1791da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1801da177e4SLinus Torvalds 	TC_PRIO_BULK,
1811da177e4SLinus Torvalds 	ECN_OR_COST(BULK),
1821da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1831da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1841da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE,
1851da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE),
1861da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1871da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK),
1881da177e4SLinus Torvalds 	TC_PRIO_INTERACTIVE_BULK,
1891da177e4SLinus Torvalds 	ECN_OR_COST(INTERACTIVE_BULK)
1901da177e4SLinus Torvalds };
191d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio);
1921da177e4SLinus Torvalds 
1932f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
1943ed66e91SChristoph Lameter #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field)
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
1971da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1981da177e4SLinus Torvalds {
19929e75252SEric Dumazet 	if (*pos)
20089aef892SDavid S. Miller 		return NULL;
20129e75252SEric Dumazet 	return SEQ_START_TOKEN;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	++*pos;
20789aef892SDavid S. Miller 	return NULL;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2171da177e4SLinus Torvalds 		seq_printf(seq, "%-127s\n",
2181da177e4SLinus Torvalds 			   "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
2191da177e4SLinus Torvalds 			   "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
2201da177e4SLinus Torvalds 			   "HHUptod\tSpecDst");
2211da177e4SLinus Torvalds 	return 0;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
2251da177e4SLinus Torvalds 	.start  = rt_cache_seq_start,
2261da177e4SLinus Torvalds 	.next   = rt_cache_seq_next,
2271da177e4SLinus Torvalds 	.stop   = rt_cache_seq_stop,
2281da177e4SLinus Torvalds 	.show   = rt_cache_seq_show,
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	int cpu;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (*pos == 0)
2361da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
2371da177e4SLinus Torvalds 
2380f23174aSRusty Russell 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2391da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2401da177e4SLinus Torvalds 			continue;
2411da177e4SLinus Torvalds 		*pos = cpu+1;
2422f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 	return NULL;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	int cpu;
2501da177e4SLinus Torvalds 
2510f23174aSRusty Russell 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2521da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
2531da177e4SLinus Torvalds 			continue;
2541da177e4SLinus Torvalds 		*pos = cpu+1;
2552f970d83SEric Dumazet 		return &per_cpu(rt_cache_stat, cpu);
2561da177e4SLinus Torvalds 	}
257a3ea8673SVasily Averin 	(*pos)++;
2581da177e4SLinus Torvalds 	return NULL;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	struct rt_cache_stat *st = v;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
2720547ffe6SYajun Deng 		seq_puts(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");
2731da177e4SLinus Torvalds 		return 0;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2760547ffe6SYajun Deng 	seq_printf(seq, "%08x %08x %08x    %08x   %08x    %08x %08x       "
2770547ffe6SYajun Deng 			"%08x       %08x %08x     %08x    %08x %08x   "
2780547ffe6SYajun Deng 			"%08x     %08x        %08x        %08x\n",
279fc66f95cSEric Dumazet 		   dst_entries_get_slow(&ipv4_dst_ops),
2800baf2b35SEric Dumazet 		   0, /* st->in_hit */
2811da177e4SLinus Torvalds 		   st->in_slow_tot,
2821da177e4SLinus Torvalds 		   st->in_slow_mc,
2831da177e4SLinus Torvalds 		   st->in_no_route,
2841da177e4SLinus Torvalds 		   st->in_brd,
2851da177e4SLinus Torvalds 		   st->in_martian_dst,
2861da177e4SLinus Torvalds 		   st->in_martian_src,
2871da177e4SLinus Torvalds 
2880baf2b35SEric Dumazet 		   0, /* st->out_hit */
2891da177e4SLinus Torvalds 		   st->out_slow_tot,
2901da177e4SLinus Torvalds 		   st->out_slow_mc,
2911da177e4SLinus Torvalds 
2920baf2b35SEric Dumazet 		   0, /* st->gc_total */
2930baf2b35SEric Dumazet 		   0, /* st->gc_ignored */
2940baf2b35SEric Dumazet 		   0, /* st->gc_goal_miss */
2950baf2b35SEric Dumazet 		   0, /* st->gc_dst_overflow */
2960baf2b35SEric Dumazet 		   0, /* st->in_hlist_search */
2970baf2b35SEric Dumazet 		   0  /* st->out_hlist_search */
2981da177e4SLinus Torvalds 		);
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
302f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
3031da177e4SLinus Torvalds 	.start  = rt_cpu_seq_start,
3041da177e4SLinus Torvalds 	.next   = rt_cpu_seq_next,
3051da177e4SLinus Torvalds 	.stop   = rt_cpu_seq_stop,
3061da177e4SLinus Torvalds 	.show   = rt_cpu_seq_show,
3071da177e4SLinus Torvalds };
3081da177e4SLinus Torvalds 
309c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
310a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v)
31178c686e9SPavel Emelyanov {
312a661c419SAlexey Dobriyan 	struct ip_rt_acct *dst, *src;
313a661c419SAlexey Dobriyan 	unsigned int i, j;
31478c686e9SPavel Emelyanov 
315a661c419SAlexey Dobriyan 	dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
316a661c419SAlexey Dobriyan 	if (!dst)
317a661c419SAlexey Dobriyan 		return -ENOMEM;
31878c686e9SPavel Emelyanov 
319a661c419SAlexey Dobriyan 	for_each_possible_cpu(i) {
320a661c419SAlexey Dobriyan 		src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
321a661c419SAlexey Dobriyan 		for (j = 0; j < 256; j++) {
322a661c419SAlexey Dobriyan 			dst[j].o_bytes   += src[j].o_bytes;
323a661c419SAlexey Dobriyan 			dst[j].o_packets += src[j].o_packets;
324a661c419SAlexey Dobriyan 			dst[j].i_bytes   += src[j].i_bytes;
325a661c419SAlexey Dobriyan 			dst[j].i_packets += src[j].i_packets;
326a661c419SAlexey Dobriyan 		}
327a661c419SAlexey Dobriyan 	}
328a661c419SAlexey Dobriyan 
329a661c419SAlexey Dobriyan 	seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
330a661c419SAlexey Dobriyan 	kfree(dst);
33178c686e9SPavel Emelyanov 	return 0;
33278c686e9SPavel Emelyanov }
33378c686e9SPavel Emelyanov #endif
334107f1634SPavel Emelyanov 
33573b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
336107f1634SPavel Emelyanov {
337107f1634SPavel Emelyanov 	struct proc_dir_entry *pde;
338107f1634SPavel Emelyanov 
339f105f26eSYejune Deng 	pde = proc_create_seq("rt_cache", 0444, net->proc_net,
340f105f26eSYejune Deng 			      &rt_cache_seq_ops);
341107f1634SPavel Emelyanov 	if (!pde)
342107f1634SPavel Emelyanov 		goto err1;
343107f1634SPavel Emelyanov 
344f105f26eSYejune Deng 	pde = proc_create_seq("rt_cache", 0444, net->proc_net_stat,
345f105f26eSYejune Deng 			      &rt_cpu_seq_ops);
346107f1634SPavel Emelyanov 	if (!pde)
347107f1634SPavel Emelyanov 		goto err2;
348107f1634SPavel Emelyanov 
349c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3503f3942acSChristoph Hellwig 	pde = proc_create_single("rt_acct", 0, net->proc_net,
3513f3942acSChristoph Hellwig 			rt_acct_proc_show);
352107f1634SPavel Emelyanov 	if (!pde)
353107f1634SPavel Emelyanov 		goto err3;
354107f1634SPavel Emelyanov #endif
355107f1634SPavel Emelyanov 	return 0;
356107f1634SPavel Emelyanov 
357c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
358107f1634SPavel Emelyanov err3:
359107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net_stat);
360107f1634SPavel Emelyanov #endif
361107f1634SPavel Emelyanov err2:
362107f1634SPavel Emelyanov 	remove_proc_entry("rt_cache", net->proc_net);
363107f1634SPavel Emelyanov err1:
364107f1634SPavel Emelyanov 	return -ENOMEM;
365107f1634SPavel Emelyanov }
36673b38711SDenis V. Lunev 
36773b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
36873b38711SDenis V. Lunev {
36973b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net_stat);
37073b38711SDenis V. Lunev 	remove_proc_entry("rt_cache", net->proc_net);
371c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
37273b38711SDenis V. Lunev 	remove_proc_entry("rt_acct", net->proc_net);
3730a931acfSAlexey Dobriyan #endif
37473b38711SDenis V. Lunev }
37573b38711SDenis V. Lunev 
37673b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
37773b38711SDenis V. Lunev 	.init = ip_rt_do_proc_init,
37873b38711SDenis V. Lunev 	.exit = ip_rt_do_proc_exit,
37973b38711SDenis V. Lunev };
38073b38711SDenis V. Lunev 
38173b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
38273b38711SDenis V. Lunev {
38373b38711SDenis V. Lunev 	return register_pernet_subsys(&ip_rt_proc_ops);
38473b38711SDenis V. Lunev }
38573b38711SDenis V. Lunev 
386107f1634SPavel Emelyanov #else
38773b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
388107f1634SPavel Emelyanov {
389107f1634SPavel Emelyanov 	return 0;
390107f1634SPavel Emelyanov }
3911da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
3921da177e4SLinus Torvalds 
3934331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth)
394e84f84f2SDenis V. Lunev {
395ca4c3fc2Sfan.du 	return rth->rt_genid != rt_genid_ipv4(dev_net(rth->dst.dev));
396e84f84f2SDenis V. Lunev }
397e84f84f2SDenis V. Lunev 
3984ccfe6d4SNicolas Dichtel void rt_cache_flush(struct net *net)
39929e75252SEric Dumazet {
400ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
40198376387SEric Dumazet }
40298376387SEric Dumazet 
403f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
404f894cbf8SDavid S. Miller 					   struct sk_buff *skb,
405f894cbf8SDavid S. Miller 					   const void *daddr)
4063769cffbSDavid Miller {
4071550c171SDavid Ahern 	const struct rtable *rt = container_of(dst, struct rtable, dst);
408d3aaeb38SDavid S. Miller 	struct net_device *dev = dst->dev;
4093769cffbSDavid Miller 	struct neighbour *n;
4103769cffbSDavid Miller 
41109eed119SEric Dumazet 	rcu_read_lock();
412d3aaeb38SDavid S. Miller 
4135c9f7c1dSDavid Ahern 	if (likely(rt->rt_gw_family == AF_INET)) {
4145c9f7c1dSDavid Ahern 		n = ip_neigh_gw4(dev, rt->rt_gw4);
4155c9f7c1dSDavid Ahern 	} else if (rt->rt_gw_family == AF_INET6) {
4165c9f7c1dSDavid Ahern 		n = ip_neigh_gw6(dev, &rt->rt_gw6);
4175c9f7c1dSDavid Ahern         } else {
4185c9f7c1dSDavid Ahern 		__be32 pkey;
4195c9f7c1dSDavid Ahern 
4205c9f7c1dSDavid Ahern 		pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
4215c9f7c1dSDavid Ahern 		n = ip_neigh_gw4(dev, pkey);
4225c9f7c1dSDavid Ahern 	}
4235c9f7c1dSDavid Ahern 
424537de0c8SIdo Schimmel 	if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
4255c9f7c1dSDavid Ahern 		n = NULL;
4265c9f7c1dSDavid Ahern 
42709eed119SEric Dumazet 	rcu_read_unlock();
4285c9f7c1dSDavid Ahern 
429d3aaeb38SDavid S. Miller 	return n;
430d3aaeb38SDavid S. Miller }
431d3aaeb38SDavid S. Miller 
43263fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
43363fca65dSJulian Anastasov {
4341550c171SDavid Ahern 	const struct rtable *rt = container_of(dst, struct rtable, dst);
43563fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
43663fca65dSJulian Anastasov 	const __be32 *pkey = daddr;
43763fca65dSJulian Anastasov 
4386de9c055SDavid Ahern 	if (rt->rt_gw_family == AF_INET) {
4391550c171SDavid Ahern 		pkey = (const __be32 *)&rt->rt_gw4;
4406de9c055SDavid Ahern 	} else if (rt->rt_gw_family == AF_INET6) {
4416de9c055SDavid Ahern 		return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6);
4426de9c055SDavid Ahern 	} else if (!daddr ||
44363fca65dSJulian Anastasov 		 (rt->rt_flags &
4446de9c055SDavid Ahern 		  (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) {
44563fca65dSJulian Anastasov 		return;
4466de9c055SDavid Ahern 	}
44763fca65dSJulian Anastasov 	__ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
44863fca65dSJulian Anastasov }
44963fca65dSJulian Anastasov 
450aa6dd211SEric Dumazet /* Hash tables of size 2048..262144 depending on RAM size.
451aa6dd211SEric Dumazet  * Each bucket uses 8 bytes.
452aa6dd211SEric Dumazet  */
453aa6dd211SEric Dumazet static u32 ip_idents_mask __read_mostly;
454355b590cSEric Dumazet static atomic_t *ip_idents __read_mostly;
455355b590cSEric Dumazet static u32 *ip_tstamps __read_mostly;
45604ca6973SEric Dumazet 
45704ca6973SEric Dumazet /* In order to protect privacy, we add a perturbation to identifiers
45804ca6973SEric Dumazet  * if one generator is seldom used. This makes hard for an attacker
45904ca6973SEric Dumazet  * to infer how many packets were sent between two points in time.
46004ca6973SEric Dumazet  */
46147ed9442SDavid Ahern static u32 ip_idents_reserve(u32 hash, int segs)
46204ca6973SEric Dumazet {
463aa6dd211SEric Dumazet 	u32 bucket, old, now = (u32)jiffies;
464aa6dd211SEric Dumazet 	atomic_t *p_id;
465aa6dd211SEric Dumazet 	u32 *p_tstamp;
466a6211caaSYuqi Jin 	u32 delta = 0;
46704ca6973SEric Dumazet 
468aa6dd211SEric Dumazet 	bucket = hash & ip_idents_mask;
469aa6dd211SEric Dumazet 	p_tstamp = ip_tstamps + bucket;
470aa6dd211SEric Dumazet 	p_id = ip_idents + bucket;
471aa6dd211SEric Dumazet 	old = READ_ONCE(*p_tstamp);
472aa6dd211SEric Dumazet 
473355b590cSEric Dumazet 	if (old != now && cmpxchg(p_tstamp, old, now) == old)
4748032bf12SJason A. Donenfeld 		delta = get_random_u32_below(now - old);
47504ca6973SEric Dumazet 
476a6211caaSYuqi Jin 	/* If UBSAN reports an error there, please make sure your compiler
477a6211caaSYuqi Jin 	 * supports -fno-strict-overflow before reporting it that was a bug
478a6211caaSYuqi Jin 	 * in UBSAN, and it has been fixed in GCC-8.
479a6211caaSYuqi Jin 	 */
480a6211caaSYuqi Jin 	return atomic_add_return(segs + delta, p_id) - segs;
48104ca6973SEric Dumazet }
48273f156a6SEric Dumazet 
483b6a7719aSHannes Frederic Sowa void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
4841da177e4SLinus Torvalds {
48573f156a6SEric Dumazet 	u32 hash, id;
4861da177e4SLinus Torvalds 
487df453700SEric Dumazet 	/* Note the following code is not safe, but this is okay. */
488df453700SEric Dumazet 	if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key)))
489df453700SEric Dumazet 		get_random_bytes(&net->ipv4.ip_id_key,
490df453700SEric Dumazet 				 sizeof(net->ipv4.ip_id_key));
4911da177e4SLinus Torvalds 
492df453700SEric Dumazet 	hash = siphash_3u32((__force u32)iph->daddr,
49304ca6973SEric Dumazet 			    (__force u32)iph->saddr,
494df453700SEric Dumazet 			    iph->protocol,
495df453700SEric Dumazet 			    &net->ipv4.ip_id_key);
49673f156a6SEric Dumazet 	id = ip_idents_reserve(hash, segs);
49773f156a6SEric Dumazet 	iph->id = htons(id);
4981da177e4SLinus Torvalds }
4994bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident);
5001da177e4SLinus Torvalds 
501544b4dd5SGuillaume Nault static void ip_rt_fix_tos(struct flowi4 *fl4)
502544b4dd5SGuillaume Nault {
503544b4dd5SGuillaume Nault 	__u8 tos = RT_FL_TOS(fl4);
504544b4dd5SGuillaume Nault 
505544b4dd5SGuillaume Nault 	fl4->flowi4_tos = tos & IPTOS_RT_MASK;
50616a28267SGuillaume Nault 	if (tos & RTO_ONLINK)
50716a28267SGuillaume Nault 		fl4->flowi4_scope = RT_SCOPE_LINK;
508544b4dd5SGuillaume Nault }
509544b4dd5SGuillaume Nault 
510e2d118a1SLorenzo Colitti static void __build_flow_key(const struct net *net, struct flowi4 *fl4,
511b1ad4138SGuillaume Nault 			     const struct sock *sk, const struct iphdr *iph,
512b1ad4138SGuillaume Nault 			     int oif, __u8 tos, u8 prot, u32 mark,
513b1ad4138SGuillaume Nault 			     int flow_flags)
5144895c771SDavid S. Miller {
515b1ad4138SGuillaume Nault 	__u8 scope = RT_SCOPE_UNIVERSE;
516b1ad4138SGuillaume Nault 
5174895c771SDavid S. Miller 	if (sk) {
5184895c771SDavid S. Miller 		oif = sk->sk_bound_dev_if;
5193c5b4d69SEric Dumazet 		mark = READ_ONCE(sk->sk_mark);
520b1ad4138SGuillaume Nault 		tos = ip_sock_rt_tos(sk);
521b1ad4138SGuillaume Nault 		scope = ip_sock_rt_scope(sk);
522cafbe182SEric Dumazet 		prot = inet_test_bit(HDRINCL, sk) ? IPPROTO_RAW :
523cafbe182SEric Dumazet 						    sk->sk_protocol;
5244895c771SDavid S. Miller 	}
525b1ad4138SGuillaume Nault 
526b1ad4138SGuillaume Nault 	flowi4_init_output(fl4, oif, mark, tos & IPTOS_RT_MASK, scope,
527b1ad4138SGuillaume Nault 			   prot, flow_flags, iph->daddr, iph->saddr, 0, 0,
528e2d118a1SLorenzo Colitti 			   sock_net_uid(net, sk));
5294895c771SDavid S. Miller }
5304895c771SDavid S. Miller 
5315abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
5325abf7f7eSEric Dumazet 			       const struct sock *sk)
5334895c771SDavid S. Miller {
534d109e61bSLorenzo Colitti 	const struct net *net = dev_net(skb->dev);
5354895c771SDavid S. Miller 	const struct iphdr *iph = ip_hdr(skb);
5364895c771SDavid S. Miller 	int oif = skb->dev->ifindex;
5374895c771SDavid S. Miller 	u8 prot = iph->protocol;
5384895c771SDavid S. Miller 	u32 mark = skb->mark;
539b1ad4138SGuillaume Nault 	__u8 tos = iph->tos;
5404895c771SDavid S. Miller 
541d109e61bSLorenzo Colitti 	__build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0);
5424895c771SDavid S. Miller }
5434895c771SDavid S. Miller 
5445abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
5454895c771SDavid S. Miller {
5464895c771SDavid S. Miller 	const struct inet_sock *inet = inet_sk(sk);
5475abf7f7eSEric Dumazet 	const struct ip_options_rcu *inet_opt;
5484895c771SDavid S. Miller 	__be32 daddr = inet->inet_daddr;
5494895c771SDavid S. Miller 
5504895c771SDavid S. Miller 	rcu_read_lock();
5514895c771SDavid S. Miller 	inet_opt = rcu_dereference(inet->inet_opt);
5524895c771SDavid S. Miller 	if (inet_opt && inet_opt->opt.srr)
5534895c771SDavid S. Miller 		daddr = inet_opt->opt.faddr;
5543c5b4d69SEric Dumazet 	flowi4_init_output(fl4, sk->sk_bound_dev_if, READ_ONCE(sk->sk_mark),
555b1ad4138SGuillaume Nault 			   ip_sock_rt_tos(sk) & IPTOS_RT_MASK,
556b1ad4138SGuillaume Nault 			   ip_sock_rt_scope(sk),
557cafbe182SEric Dumazet 			   inet_test_bit(HDRINCL, sk) ?
558cafbe182SEric Dumazet 				IPPROTO_RAW : sk->sk_protocol,
5594895c771SDavid S. Miller 			   inet_sk_flowi_flags(sk),
560e2d118a1SLorenzo Colitti 			   daddr, inet->inet_saddr, 0, 0, sk->sk_uid);
5614895c771SDavid S. Miller 	rcu_read_unlock();
5624895c771SDavid S. Miller }
5634895c771SDavid S. Miller 
5645abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5655abf7f7eSEric Dumazet 				 const struct sk_buff *skb)
5664895c771SDavid S. Miller {
5674895c771SDavid S. Miller 	if (skb)
5684895c771SDavid S. Miller 		build_skb_flow_key(fl4, skb, sk);
5694895c771SDavid S. Miller 	else
5704895c771SDavid S. Miller 		build_sk_flow_key(fl4, sk);
5714895c771SDavid S. Miller }
5724895c771SDavid S. Miller 
573c5038a83SDavid S. Miller static DEFINE_SPINLOCK(fnhe_lock);
5744895c771SDavid S. Miller 
5752ffae99dSTimo Teräs static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
5762ffae99dSTimo Teräs {
5772ffae99dSTimo Teräs 	struct rtable *rt;
5782ffae99dSTimo Teräs 
5792ffae99dSTimo Teräs 	rt = rcu_dereference(fnhe->fnhe_rth_input);
5802ffae99dSTimo Teräs 	if (rt) {
5812ffae99dSTimo Teräs 		RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
58295c47f9cSWei Wang 		dst_dev_put(&rt->dst);
5830830106cSWei Wang 		dst_release(&rt->dst);
5842ffae99dSTimo Teräs 	}
5852ffae99dSTimo Teräs 	rt = rcu_dereference(fnhe->fnhe_rth_output);
5862ffae99dSTimo Teräs 	if (rt) {
5872ffae99dSTimo Teräs 		RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
58895c47f9cSWei Wang 		dst_dev_put(&rt->dst);
5890830106cSWei Wang 		dst_release(&rt->dst);
5902ffae99dSTimo Teräs 	}
5912ffae99dSTimo Teräs }
5922ffae99dSTimo Teräs 
59367d6d681SEric Dumazet static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
5944895c771SDavid S. Miller {
59567d6d681SEric Dumazet 	struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
59667d6d681SEric Dumazet 	struct fib_nh_exception *fnhe, *oldest = NULL;
5974895c771SDavid S. Miller 
59867d6d681SEric Dumazet 	for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
59967d6d681SEric Dumazet 		fnhe = rcu_dereference_protected(*fnhe_p,
60067d6d681SEric Dumazet 						 lockdep_is_held(&fnhe_lock));
60167d6d681SEric Dumazet 		if (!fnhe)
60267d6d681SEric Dumazet 			break;
60367d6d681SEric Dumazet 		if (!oldest ||
60467d6d681SEric Dumazet 		    time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
6054895c771SDavid S. Miller 			oldest = fnhe;
60667d6d681SEric Dumazet 			oldest_p = fnhe_p;
60767d6d681SEric Dumazet 		}
6084895c771SDavid S. Miller 	}
6092ffae99dSTimo Teräs 	fnhe_flush_routes(oldest);
61067d6d681SEric Dumazet 	*oldest_p = oldest->fnhe_next;
61167d6d681SEric Dumazet 	kfree_rcu(oldest, rcu);
6124895c771SDavid S. Miller }
6134895c771SDavid S. Miller 
6146457378fSEric Dumazet static u32 fnhe_hashfun(__be32 daddr)
615d3a25c98SDavid S. Miller {
61649ecc2e9SEric Dumazet 	static siphash_aligned_key_t fnhe_hash_key;
6176457378fSEric Dumazet 	u64 hval;
618d3a25c98SDavid S. Miller 
6196457378fSEric Dumazet 	net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key));
6206457378fSEric Dumazet 	hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key);
6216457378fSEric Dumazet 	return hash_64(hval, FNHE_HASH_SHIFT);
622d3a25c98SDavid S. Miller }
623d3a25c98SDavid S. Miller 
624387aa65aSTimo Teräs static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
625387aa65aSTimo Teräs {
626387aa65aSTimo Teräs 	rt->rt_pmtu = fnhe->fnhe_pmtu;
627d52e5a7eSSabrina Dubroca 	rt->rt_mtu_locked = fnhe->fnhe_mtu_locked;
628387aa65aSTimo Teräs 	rt->dst.expires = fnhe->fnhe_expires;
629387aa65aSTimo Teräs 
630387aa65aSTimo Teräs 	if (fnhe->fnhe_gw) {
631387aa65aSTimo Teräs 		rt->rt_flags |= RTCF_REDIRECTED;
63277d5bc7eSDavid Ahern 		rt->rt_uses_gateway = 1;
6331550c171SDavid Ahern 		rt->rt_gw_family = AF_INET;
6341550c171SDavid Ahern 		rt->rt_gw4 = fnhe->fnhe_gw;
635387aa65aSTimo Teräs 	}
636387aa65aSTimo Teräs }
637387aa65aSTimo Teräs 
638a5995e71SDavid Ahern static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr,
639a5995e71SDavid Ahern 				  __be32 gw, u32 pmtu, bool lock,
640a5995e71SDavid Ahern 				  unsigned long expires)
6414895c771SDavid S. Miller {
642aee06da6SJulian Anastasov 	struct fnhe_hash_bucket *hash;
6434895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
644387aa65aSTimo Teräs 	struct rtable *rt;
645cebe84c6SXin Long 	u32 genid, hval;
646387aa65aSTimo Teräs 	unsigned int i;
6474895c771SDavid S. Miller 	int depth;
648cebe84c6SXin Long 
649a5995e71SDavid Ahern 	genid = fnhe_genid(dev_net(nhc->nhc_dev));
650cebe84c6SXin Long 	hval = fnhe_hashfun(daddr);
6514895c771SDavid S. Miller 
652c5038a83SDavid S. Miller 	spin_lock_bh(&fnhe_lock);
653aee06da6SJulian Anastasov 
654a5995e71SDavid Ahern 	hash = rcu_dereference(nhc->nhc_exceptions);
6554895c771SDavid S. Miller 	if (!hash) {
6566396bb22SKees Cook 		hash = kcalloc(FNHE_HASH_SIZE, sizeof(*hash), GFP_ATOMIC);
6574895c771SDavid S. Miller 		if (!hash)
658aee06da6SJulian Anastasov 			goto out_unlock;
659a5995e71SDavid Ahern 		rcu_assign_pointer(nhc->nhc_exceptions, hash);
6604895c771SDavid S. Miller 	}
6614895c771SDavid S. Miller 
6624895c771SDavid S. Miller 	hash += hval;
6634895c771SDavid S. Miller 
6644895c771SDavid S. Miller 	depth = 0;
6654895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash->chain); fnhe;
6664895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
6674895c771SDavid S. Miller 		if (fnhe->fnhe_daddr == daddr)
668aee06da6SJulian Anastasov 			break;
6694895c771SDavid S. Miller 		depth++;
6704895c771SDavid S. Miller 	}
6714895c771SDavid S. Miller 
672aee06da6SJulian Anastasov 	if (fnhe) {
673cebe84c6SXin Long 		if (fnhe->fnhe_genid != genid)
674cebe84c6SXin Long 			fnhe->fnhe_genid = genid;
675aee06da6SJulian Anastasov 		if (gw)
676aee06da6SJulian Anastasov 			fnhe->fnhe_gw = gw;
677d52e5a7eSSabrina Dubroca 		if (pmtu) {
678aee06da6SJulian Anastasov 			fnhe->fnhe_pmtu = pmtu;
679d52e5a7eSSabrina Dubroca 			fnhe->fnhe_mtu_locked = lock;
680d52e5a7eSSabrina Dubroca 		}
681387aa65aSTimo Teräs 		fnhe->fnhe_expires = max(1UL, expires);
682387aa65aSTimo Teräs 		/* Update all cached dsts too */
6832ffae99dSTimo Teräs 		rt = rcu_dereference(fnhe->fnhe_rth_input);
6842ffae99dSTimo Teräs 		if (rt)
6852ffae99dSTimo Teräs 			fill_route_from_fnhe(rt, fnhe);
6862ffae99dSTimo Teräs 		rt = rcu_dereference(fnhe->fnhe_rth_output);
687387aa65aSTimo Teräs 		if (rt)
688387aa65aSTimo Teräs 			fill_route_from_fnhe(rt, fnhe);
689aee06da6SJulian Anastasov 	} else {
69067d6d681SEric Dumazet 		/* Randomize max depth to avoid some side channels attacks. */
69167d6d681SEric Dumazet 		int max_depth = FNHE_RECLAIM_DEPTH +
6928032bf12SJason A. Donenfeld 				get_random_u32_below(FNHE_RECLAIM_DEPTH);
69367d6d681SEric Dumazet 
69467d6d681SEric Dumazet 		while (depth > max_depth) {
69567d6d681SEric Dumazet 			fnhe_remove_oldest(hash);
69667d6d681SEric Dumazet 			depth--;
69767d6d681SEric Dumazet 		}
69867d6d681SEric Dumazet 
6994895c771SDavid S. Miller 		fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
7004895c771SDavid S. Miller 		if (!fnhe)
701aee06da6SJulian Anastasov 			goto out_unlock;
7024895c771SDavid S. Miller 
7034895c771SDavid S. Miller 		fnhe->fnhe_next = hash->chain;
70467d6d681SEric Dumazet 
705cebe84c6SXin Long 		fnhe->fnhe_genid = genid;
7064895c771SDavid S. Miller 		fnhe->fnhe_daddr = daddr;
707aee06da6SJulian Anastasov 		fnhe->fnhe_gw = gw;
708aee06da6SJulian Anastasov 		fnhe->fnhe_pmtu = pmtu;
709d52e5a7eSSabrina Dubroca 		fnhe->fnhe_mtu_locked = lock;
71094720e3aSJulian Anastasov 		fnhe->fnhe_expires = max(1UL, expires);
711387aa65aSTimo Teräs 
71267d6d681SEric Dumazet 		rcu_assign_pointer(hash->chain, fnhe);
71367d6d681SEric Dumazet 
714387aa65aSTimo Teräs 		/* Exception created; mark the cached routes for the nexthop
715387aa65aSTimo Teräs 		 * stale, so anyone caching it rechecks if this exception
716387aa65aSTimo Teräs 		 * applies to them.
717387aa65aSTimo Teräs 		 */
7180f457a36SDavid Ahern 		rt = rcu_dereference(nhc->nhc_rth_input);
7192ffae99dSTimo Teräs 		if (rt)
7202ffae99dSTimo Teräs 			rt->dst.obsolete = DST_OBSOLETE_KILL;
7212ffae99dSTimo Teräs 
722387aa65aSTimo Teräs 		for_each_possible_cpu(i) {
723387aa65aSTimo Teräs 			struct rtable __rcu **prt;
7246ad08600SShubhankar Kuranagatti 
7250f457a36SDavid Ahern 			prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i);
726387aa65aSTimo Teräs 			rt = rcu_dereference(*prt);
727387aa65aSTimo Teräs 			if (rt)
728387aa65aSTimo Teräs 				rt->dst.obsolete = DST_OBSOLETE_KILL;
729387aa65aSTimo Teräs 		}
730aee06da6SJulian Anastasov 	}
731aee06da6SJulian Anastasov 
7324895c771SDavid S. Miller 	fnhe->fnhe_stamp = jiffies;
733aee06da6SJulian Anastasov 
734aee06da6SJulian Anastasov out_unlock:
735c5038a83SDavid S. Miller 	spin_unlock_bh(&fnhe_lock);
7364895c771SDavid S. Miller }
7374895c771SDavid S. Miller 
738ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
739ceb33206SDavid S. Miller 			     bool kill_route)
7401da177e4SLinus Torvalds {
741e47a185bSDavid S. Miller 	__be32 new_gw = icmp_hdr(skb)->un.gateway;
74294206125SDavid S. Miller 	__be32 old_gw = ip_hdr(skb)->saddr;
743e47a185bSDavid S. Miller 	struct net_device *dev = skb->dev;
744e47a185bSDavid S. Miller 	struct in_device *in_dev;
7454895c771SDavid S. Miller 	struct fib_result res;
746e47a185bSDavid S. Miller 	struct neighbour *n;
747317805b8SDenis V. Lunev 	struct net *net;
7481da177e4SLinus Torvalds 
74994206125SDavid S. Miller 	switch (icmp_hdr(skb)->code & 7) {
75094206125SDavid S. Miller 	case ICMP_REDIR_NET:
75194206125SDavid S. Miller 	case ICMP_REDIR_NETTOS:
75294206125SDavid S. Miller 	case ICMP_REDIR_HOST:
75394206125SDavid S. Miller 	case ICMP_REDIR_HOSTTOS:
75494206125SDavid S. Miller 		break;
75594206125SDavid S. Miller 
75694206125SDavid S. Miller 	default:
75794206125SDavid S. Miller 		return;
75894206125SDavid S. Miller 	}
75994206125SDavid S. Miller 
7601550c171SDavid Ahern 	if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw)
761e47a185bSDavid S. Miller 		return;
762e47a185bSDavid S. Miller 
763e47a185bSDavid S. Miller 	in_dev = __in_dev_get_rcu(dev);
764e47a185bSDavid S. Miller 	if (!in_dev)
765e47a185bSDavid S. Miller 		return;
766e47a185bSDavid S. Miller 
767c346dca1SYOSHIFUJI Hideaki 	net = dev_net(dev);
7689d4fb27dSJoe Perches 	if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
7699d4fb27dSJoe Perches 	    ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
7709d4fb27dSJoe Perches 	    ipv4_is_zeronet(new_gw))
7711da177e4SLinus Torvalds 		goto reject_redirect;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	if (!IN_DEV_SHARED_MEDIA(in_dev)) {
7741da177e4SLinus Torvalds 		if (!inet_addr_onlink(in_dev, new_gw, old_gw))
7751da177e4SLinus Torvalds 			goto reject_redirect;
7761da177e4SLinus Torvalds 		if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
7771da177e4SLinus Torvalds 			goto reject_redirect;
7781da177e4SLinus Torvalds 	} else {
779317805b8SDenis V. Lunev 		if (inet_addr_type(net, new_gw) != RTN_UNICAST)
7801da177e4SLinus Torvalds 			goto reject_redirect;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
783969447f2SStephen Suryaputra Lin 	n = __ipv4_neigh_lookup(rt->dst.dev, new_gw);
784969447f2SStephen Suryaputra Lin 	if (!n)
785969447f2SStephen Suryaputra Lin 		n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev);
7862c1a4311SWANG Cong 	if (!IS_ERR(n)) {
787b071af52SEric Dumazet 		if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
788e47a185bSDavid S. Miller 			neigh_event_send(n, NULL);
789e47a185bSDavid S. Miller 		} else {
7900eeb075fSAndy Gospodarek 			if (fib_lookup(net, fl4, &res, 0) == 0) {
7912fbc6e89SDavid Ahern 				struct fib_nh_common *nhc;
7924895c771SDavid S. Miller 
7932fbc6e89SDavid Ahern 				fib_select_path(net, &res, fl4, skb);
7942fbc6e89SDavid Ahern 				nhc = FIB_RES_NHC(res);
795a5995e71SDavid Ahern 				update_or_create_fnhe(nhc, fl4->daddr, new_gw,
796d52e5a7eSSabrina Dubroca 						0, false,
797d52e5a7eSSabrina Dubroca 						jiffies + ip_rt_gc_timeout);
7984895c771SDavid S. Miller 			}
799ceb33206SDavid S. Miller 			if (kill_route)
800ceb33206SDavid S. Miller 				rt->dst.obsolete = DST_OBSOLETE_KILL;
801e47a185bSDavid S. Miller 			call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
802e47a185bSDavid S. Miller 		}
803e47a185bSDavid S. Miller 		neigh_release(n);
804e47a185bSDavid S. Miller 	}
805e47a185bSDavid S. Miller 	return;
806e47a185bSDavid S. Miller 
807e47a185bSDavid S. Miller reject_redirect:
808e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE
80999ee038dSDavid S. Miller 	if (IN_DEV_LOG_MARTIANS(in_dev)) {
81099ee038dSDavid S. Miller 		const struct iphdr *iph = (const struct iphdr *) skb->data;
81199ee038dSDavid S. Miller 		__be32 daddr = iph->daddr;
81299ee038dSDavid S. Miller 		__be32 saddr = iph->saddr;
81399ee038dSDavid S. Miller 
814e47a185bSDavid S. Miller 		net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
815e47a185bSDavid S. Miller 				     "  Advised path = %pI4 -> %pI4\n",
816e47a185bSDavid S. Miller 				     &old_gw, dev->name, &new_gw,
817e47a185bSDavid S. Miller 				     &saddr, &daddr);
81899ee038dSDavid S. Miller 	}
819e47a185bSDavid S. Miller #endif
820e47a185bSDavid S. Miller 	;
821e47a185bSDavid S. Miller }
822e47a185bSDavid S. Miller 
8234895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
8244895c771SDavid S. Miller {
8254895c771SDavid S. Miller 	struct rtable *rt;
8264895c771SDavid S. Miller 	struct flowi4 fl4;
827f96ef988SMichal Kubecek 	const struct iphdr *iph = (const struct iphdr *) skb->data;
8287d995694SLorenzo Colitti 	struct net *net = dev_net(skb->dev);
829f96ef988SMichal Kubecek 	int oif = skb->dev->ifindex;
830f96ef988SMichal Kubecek 	u8 prot = iph->protocol;
831f96ef988SMichal Kubecek 	u32 mark = skb->mark;
832b1ad4138SGuillaume Nault 	__u8 tos = iph->tos;
8334895c771SDavid S. Miller 
8344895c771SDavid S. Miller 	rt = (struct rtable *) dst;
8354895c771SDavid S. Miller 
8367d995694SLorenzo Colitti 	__build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0);
837ceb33206SDavid S. Miller 	__ip_do_redirect(rt, skb, &fl4, true);
8384895c771SDavid S. Miller }
8394895c771SDavid S. Miller 
8401da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)dst;
8431da177e4SLinus Torvalds 	struct dst_entry *ret = dst;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (rt) {
846d11a4dc1STimo Teräs 		if (dst->obsolete > 0) {
8471da177e4SLinus Torvalds 			ip_rt_put(rt);
8481da177e4SLinus Torvalds 			ret = NULL;
8495943634fSDavid S. Miller 		} else if ((rt->rt_flags & RTCF_REDIRECTED) ||
8505943634fSDavid S. Miller 			   rt->dst.expires) {
85189aef892SDavid S. Miller 			ip_rt_put(rt);
8521da177e4SLinus Torvalds 			ret = NULL;
8531da177e4SLinus Torvalds 		}
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 	return ret;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /*
8591da177e4SLinus Torvalds  * Algorithm:
8601da177e4SLinus Torvalds  *	1. The first ip_rt_redirect_number redirects are sent
8611da177e4SLinus Torvalds  *	   with exponential backoff, then we stop sending them at all,
8621da177e4SLinus Torvalds  *	   assuming that the host ignores our redirects.
8631da177e4SLinus Torvalds  *	2. If we did not see packets requiring redirects
8641da177e4SLinus Torvalds  *	   during ip_rt_redirect_silence, we assume that the host
8651da177e4SLinus Torvalds  *	   forgot redirected route and start to send redirects again.
8661da177e4SLinus Torvalds  *
8671da177e4SLinus Torvalds  * This algorithm is much cheaper and more intelligent than dumb load limiting
8681da177e4SLinus Torvalds  * in icmp.c.
8691da177e4SLinus Torvalds  *
8701da177e4SLinus Torvalds  * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
8711da177e4SLinus Torvalds  * and "frag. need" (breaks PMTU discovery) in icmp.c.
8721da177e4SLinus Torvalds  */
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb)
8751da177e4SLinus Torvalds {
876511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
87730038fc6SEric Dumazet 	struct in_device *in_dev;
87892d86829SDavid S. Miller 	struct inet_peer *peer;
8791d861aa4SDavid S. Miller 	struct net *net;
88030038fc6SEric Dumazet 	int log_martians;
881192132b9SDavid Ahern 	int vif;
8821da177e4SLinus Torvalds 
88330038fc6SEric Dumazet 	rcu_read_lock();
884d8d1f30bSChangli Gao 	in_dev = __in_dev_get_rcu(rt->dst.dev);
88530038fc6SEric Dumazet 	if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
88630038fc6SEric Dumazet 		rcu_read_unlock();
8871da177e4SLinus Torvalds 		return;
88830038fc6SEric Dumazet 	}
88930038fc6SEric Dumazet 	log_martians = IN_DEV_LOG_MARTIANS(in_dev);
890385add90SDavid Ahern 	vif = l3mdev_master_ifindex_rcu(rt->dst.dev);
89130038fc6SEric Dumazet 	rcu_read_unlock();
8921da177e4SLinus Torvalds 
8931d861aa4SDavid S. Miller 	net = dev_net(rt->dst.dev);
894192132b9SDavid Ahern 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif, 1);
89592d86829SDavid S. Miller 	if (!peer) {
896e81da0e1SJulian Anastasov 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST,
897e81da0e1SJulian Anastasov 			  rt_nexthop(rt, ip_hdr(skb)->daddr));
89892d86829SDavid S. Miller 		return;
89992d86829SDavid S. Miller 	}
90092d86829SDavid S. Miller 
9011da177e4SLinus Torvalds 	/* No redirected packets during ip_rt_redirect_silence;
9021da177e4SLinus Torvalds 	 * reset the algorithm.
9031da177e4SLinus Torvalds 	 */
904c09551c6SLorenzo Bianconi 	if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) {
90592d86829SDavid S. Miller 		peer->rate_tokens = 0;
906c09551c6SLorenzo Bianconi 		peer->n_redirects = 0;
907c09551c6SLorenzo Bianconi 	}
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/* Too many ignored redirects; do not send anything
910d8d1f30bSChangli Gao 	 * set dst.rate_last to the last seen redirected packet.
9111da177e4SLinus Torvalds 	 */
912c09551c6SLorenzo Bianconi 	if (peer->n_redirects >= ip_rt_redirect_number) {
91392d86829SDavid S. Miller 		peer->rate_last = jiffies;
9141d861aa4SDavid S. Miller 		goto out_put_peer;
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	/* Check for load limit; set rate_last to the latest sent
9181da177e4SLinus Torvalds 	 * redirect.
9191da177e4SLinus Torvalds 	 */
92057644431SPaolo Abeni 	if (peer->n_redirects == 0 ||
92114fb8a76SLi Yewang 	    time_after(jiffies,
92292d86829SDavid S. Miller 		       (peer->rate_last +
923b406472bSPaolo Abeni 			(ip_rt_redirect_load << peer->n_redirects)))) {
924e81da0e1SJulian Anastasov 		__be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr);
925e81da0e1SJulian Anastasov 
926e81da0e1SJulian Anastasov 		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw);
92792d86829SDavid S. Miller 		peer->rate_last = jiffies;
928c09551c6SLorenzo Bianconi 		++peer->n_redirects;
9291da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
93030038fc6SEric Dumazet 		if (log_martians &&
931b406472bSPaolo Abeni 		    peer->n_redirects == ip_rt_redirect_number)
932e87cc472SJoe Perches 			net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
93392101b3bSDavid S. Miller 					     &ip_hdr(skb)->saddr, inet_iif(skb),
934e81da0e1SJulian Anastasov 					     &ip_hdr(skb)->daddr, &gw);
9351da177e4SLinus Torvalds #endif
9361da177e4SLinus Torvalds 	}
9371d861aa4SDavid S. Miller out_put_peer:
9381d861aa4SDavid S. Miller 	inet_putpeer(peer);
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
9421da177e4SLinus Torvalds {
943511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
944e2c0dc1fSStephen Suryaputra 	struct net_device *dev = skb->dev;
945e2c0dc1fSStephen Suryaputra 	struct in_device *in_dev;
94692d86829SDavid S. Miller 	struct inet_peer *peer;
9471da177e4SLinus Torvalds 	unsigned long now;
948251da413SDavid S. Miller 	struct net *net;
949c4eb6641SMenglong Dong 	SKB_DR(reason);
95092d86829SDavid S. Miller 	bool send;
9511da177e4SLinus Torvalds 	int code;
9521da177e4SLinus Torvalds 
953e2c0dc1fSStephen Suryaputra 	if (netif_is_l3_master(skb->dev)) {
954e2c0dc1fSStephen Suryaputra 		dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
955e2c0dc1fSStephen Suryaputra 		if (!dev)
956e2c0dc1fSStephen Suryaputra 			goto out;
957e2c0dc1fSStephen Suryaputra 	}
958e2c0dc1fSStephen Suryaputra 
959e2c0dc1fSStephen Suryaputra 	in_dev = __in_dev_get_rcu(dev);
960e2c0dc1fSStephen Suryaputra 
961381c759dSEric W. Biederman 	/* IP on this device is disabled. */
962381c759dSEric W. Biederman 	if (!in_dev)
963381c759dSEric W. Biederman 		goto out;
964381c759dSEric W. Biederman 
965251da413SDavid S. Miller 	net = dev_net(rt->dst.dev);
966251da413SDavid S. Miller 	if (!IN_DEV_FORWARD(in_dev)) {
967251da413SDavid S. Miller 		switch (rt->dst.error) {
968251da413SDavid S. Miller 		case EHOSTUNREACH:
969c4eb6641SMenglong Dong 			SKB_DR_SET(reason, IP_INADDRERRORS);
970b45386efSEric Dumazet 			__IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS);
971251da413SDavid S. Miller 			break;
972251da413SDavid S. Miller 
973251da413SDavid S. Miller 		case ENETUNREACH:
974c4eb6641SMenglong Dong 			SKB_DR_SET(reason, IP_INNOROUTES);
975b45386efSEric Dumazet 			__IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
976251da413SDavid S. Miller 			break;
977251da413SDavid S. Miller 		}
978251da413SDavid S. Miller 		goto out;
979251da413SDavid S. Miller 	}
980251da413SDavid S. Miller 
981d8d1f30bSChangli Gao 	switch (rt->dst.error) {
9821da177e4SLinus Torvalds 	case EINVAL:
9831da177e4SLinus Torvalds 	default:
9841da177e4SLinus Torvalds 		goto out;
9851da177e4SLinus Torvalds 	case EHOSTUNREACH:
9861da177e4SLinus Torvalds 		code = ICMP_HOST_UNREACH;
9871da177e4SLinus Torvalds 		break;
9881da177e4SLinus Torvalds 	case ENETUNREACH:
9891da177e4SLinus Torvalds 		code = ICMP_NET_UNREACH;
990c4eb6641SMenglong Dong 		SKB_DR_SET(reason, IP_INNOROUTES);
991b45386efSEric Dumazet 		__IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
9921da177e4SLinus Torvalds 		break;
9931da177e4SLinus Torvalds 	case EACCES:
9941da177e4SLinus Torvalds 		code = ICMP_PKT_FILTERED;
9951da177e4SLinus Torvalds 		break;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
998192132b9SDavid Ahern 	peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr,
999385add90SDavid Ahern 			       l3mdev_master_ifindex(skb->dev), 1);
100092d86829SDavid S. Miller 
100192d86829SDavid S. Miller 	send = true;
100292d86829SDavid S. Miller 	if (peer) {
10031da177e4SLinus Torvalds 		now = jiffies;
100492d86829SDavid S. Miller 		peer->rate_tokens += now - peer->rate_last;
100592d86829SDavid S. Miller 		if (peer->rate_tokens > ip_rt_error_burst)
100692d86829SDavid S. Miller 			peer->rate_tokens = ip_rt_error_burst;
100792d86829SDavid S. Miller 		peer->rate_last = now;
100892d86829SDavid S. Miller 		if (peer->rate_tokens >= ip_rt_error_cost)
100992d86829SDavid S. Miller 			peer->rate_tokens -= ip_rt_error_cost;
101092d86829SDavid S. Miller 		else
101192d86829SDavid S. Miller 			send = false;
10121d861aa4SDavid S. Miller 		inet_putpeer(peer);
10131da177e4SLinus Torvalds 	}
101492d86829SDavid S. Miller 	if (send)
101592d86829SDavid S. Miller 		icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
10161da177e4SLinus Torvalds 
1017c4eb6641SMenglong Dong out:	kfree_skb_reason(skb, reason);
10181da177e4SLinus Torvalds 	return 0;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
1021d851c12bSSteffen Klassert static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
10221da177e4SLinus Torvalds {
1023d851c12bSSteffen Klassert 	struct dst_entry *dst = &rt->dst;
10242fbc6e89SDavid Ahern 	struct net *net = dev_net(dst->dev);
10254895c771SDavid S. Miller 	struct fib_result res;
1026d52e5a7eSSabrina Dubroca 	bool lock = false;
10278b4510d7SMiaohe Lin 	u32 old_mtu;
10282c8cec5cSDavid S. Miller 
1029d52e5a7eSSabrina Dubroca 	if (ip_mtu_locked(dst))
1030fa1e492aSSteffen Klassert 		return;
1031fa1e492aSSteffen Klassert 
10328b4510d7SMiaohe Lin 	old_mtu = ipv4_mtu(dst);
103328d35bcdSSabrina Dubroca 	if (old_mtu < mtu)
10343cdaa5beSLi Wei 		return;
10353cdaa5beSLi Wei 
10361de6b15aSxu xin 	if (mtu < net->ipv4.ip_rt_min_pmtu) {
1037d52e5a7eSSabrina Dubroca 		lock = true;
10381de6b15aSxu xin 		mtu = min(old_mtu, net->ipv4.ip_rt_min_pmtu);
1039d52e5a7eSSabrina Dubroca 	}
104046af3180SHiroaki SHIMODA 
104128d35bcdSSabrina Dubroca 	if (rt->rt_pmtu == mtu && !lock &&
10421135fad2Sxu xin 	    time_before(jiffies, dst->expires - net->ipv4.ip_rt_mtu_expires / 2))
1043f016229eSTimo Teräs 		return;
1044f016229eSTimo Teräs 
1045c5ae7d41SEric Dumazet 	rcu_read_lock();
10462fbc6e89SDavid Ahern 	if (fib_lookup(net, fl4, &res, 0) == 0) {
10472fbc6e89SDavid Ahern 		struct fib_nh_common *nhc;
10484895c771SDavid S. Miller 
10492fbc6e89SDavid Ahern 		fib_select_path(net, &res, fl4, NULL);
10502fbc6e89SDavid Ahern 		nhc = FIB_RES_NHC(res);
1051a5995e71SDavid Ahern 		update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
10521135fad2Sxu xin 				      jiffies + net->ipv4.ip_rt_mtu_expires);
10534895c771SDavid S. Miller 	}
1054c5ae7d41SEric Dumazet 	rcu_read_unlock();
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10574895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1058bd085ef6SHangbin Liu 			      struct sk_buff *skb, u32 mtu,
1059bd085ef6SHangbin Liu 			      bool confirm_neigh)
10604895c771SDavid S. Miller {
10614895c771SDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
10624895c771SDavid S. Miller 	struct flowi4 fl4;
10634895c771SDavid S. Miller 
10644895c771SDavid S. Miller 	ip_rt_build_flow_key(&fl4, sk, skb);
1065df23bb18SStefano Brivio 
1066df23bb18SStefano Brivio 	/* Don't make lookup fail for bridged encapsulations */
1067df23bb18SStefano Brivio 	if (skb && netif_is_any_bridge_port(skb->dev))
1068df23bb18SStefano Brivio 		fl4.flowi4_oif = 0;
1069df23bb18SStefano Brivio 
1070d851c12bSSteffen Klassert 	__ip_rt_update_pmtu(rt, &fl4, mtu);
10714895c771SDavid S. Miller }
10724895c771SDavid S. Miller 
107336393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
1074d888f396SMaciej Żenczykowski 		      int oif, u8 protocol)
107536393395SDavid S. Miller {
107636393395SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *)skb->data;
107736393395SDavid S. Miller 	struct flowi4 fl4;
107836393395SDavid S. Miller 	struct rtable *rt;
1079d888f396SMaciej Żenczykowski 	u32 mark = IP4_REPLY_MARK(net, skb->mark);
10801b3c61dcSLorenzo Colitti 
1081b1ad4138SGuillaume Nault 	__build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, mark,
1082b1ad4138SGuillaume Nault 			 0);
108336393395SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
108436393395SDavid S. Miller 	if (!IS_ERR(rt)) {
10854895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
108636393395SDavid S. Miller 		ip_rt_put(rt);
108736393395SDavid S. Miller 	}
108836393395SDavid S. Miller }
108936393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
109036393395SDavid S. Miller 
10919cb3a50cSSteffen Klassert static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
109236393395SDavid S. Miller {
10934895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *)skb->data;
10944895c771SDavid S. Miller 	struct flowi4 fl4;
10954895c771SDavid S. Miller 	struct rtable *rt;
109636393395SDavid S. Miller 
1097e2d118a1SLorenzo Colitti 	__build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0);
10981b3c61dcSLorenzo Colitti 
10991b3c61dcSLorenzo Colitti 	if (!fl4.flowi4_mark)
11001b3c61dcSLorenzo Colitti 		fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark);
11011b3c61dcSLorenzo Colitti 
11024895c771SDavid S. Miller 	rt = __ip_route_output_key(sock_net(sk), &fl4);
11034895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
11044895c771SDavid S. Miller 		__ip_rt_update_pmtu(rt, &fl4, mtu);
11054895c771SDavid S. Miller 		ip_rt_put(rt);
11064895c771SDavid S. Miller 	}
110736393395SDavid S. Miller }
11089cb3a50cSSteffen Klassert 
11099cb3a50cSSteffen Klassert void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
11109cb3a50cSSteffen Klassert {
11119cb3a50cSSteffen Klassert 	const struct iphdr *iph = (const struct iphdr *)skb->data;
11129cb3a50cSSteffen Klassert 	struct flowi4 fl4;
11139cb3a50cSSteffen Klassert 	struct rtable *rt;
11147f502361SEric Dumazet 	struct dst_entry *odst = NULL;
1115b44108dbSSteffen Klassert 	bool new = false;
1116e2d118a1SLorenzo Colitti 	struct net *net = sock_net(sk);
11179cb3a50cSSteffen Klassert 
11189cb3a50cSSteffen Klassert 	bh_lock_sock(sk);
1119482fc609SHannes Frederic Sowa 
1120482fc609SHannes Frederic Sowa 	if (!ip_sk_accept_pmtu(sk))
1121482fc609SHannes Frederic Sowa 		goto out;
1122482fc609SHannes Frederic Sowa 
11237f502361SEric Dumazet 	odst = sk_dst_get(sk);
11249cb3a50cSSteffen Klassert 
11257f502361SEric Dumazet 	if (sock_owned_by_user(sk) || !odst) {
11269cb3a50cSSteffen Klassert 		__ipv4_sk_update_pmtu(skb, sk, mtu);
11279cb3a50cSSteffen Klassert 		goto out;
11289cb3a50cSSteffen Klassert 	}
11299cb3a50cSSteffen Klassert 
1130e2d118a1SLorenzo Colitti 	__build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
11319cb3a50cSSteffen Klassert 
11327f502361SEric Dumazet 	rt = (struct rtable *)odst;
113351456b29SIan Morris 	if (odst->obsolete && !odst->ops->check(odst, 0)) {
11349cb3a50cSSteffen Klassert 		rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
11359cb3a50cSSteffen Klassert 		if (IS_ERR(rt))
11369cb3a50cSSteffen Klassert 			goto out;
1137b44108dbSSteffen Klassert 
1138b44108dbSSteffen Klassert 		new = true;
11399cb3a50cSSteffen Klassert 	}
11409cb3a50cSSteffen Klassert 
11410f6c480fSDavid Miller 	__ip_rt_update_pmtu((struct rtable *)xfrm_dst_path(&rt->dst), &fl4, mtu);
11429cb3a50cSSteffen Klassert 
11437f502361SEric Dumazet 	if (!dst_check(&rt->dst, 0)) {
1144b44108dbSSteffen Klassert 		if (new)
1145b44108dbSSteffen Klassert 			dst_release(&rt->dst);
1146b44108dbSSteffen Klassert 
11479cb3a50cSSteffen Klassert 		rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
11489cb3a50cSSteffen Klassert 		if (IS_ERR(rt))
11499cb3a50cSSteffen Klassert 			goto out;
11509cb3a50cSSteffen Klassert 
1151b44108dbSSteffen Klassert 		new = true;
11529cb3a50cSSteffen Klassert 	}
11539cb3a50cSSteffen Klassert 
1154b44108dbSSteffen Klassert 	if (new)
11557f502361SEric Dumazet 		sk_dst_set(sk, &rt->dst);
11569cb3a50cSSteffen Klassert 
11579cb3a50cSSteffen Klassert out:
11589cb3a50cSSteffen Klassert 	bh_unlock_sock(sk);
11597f502361SEric Dumazet 	dst_release(odst);
11609cb3a50cSSteffen Klassert }
116136393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
1162f39925dbSDavid S. Miller 
1163b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net,
11641042caa7SMaciej Żenczykowski 		   int oif, u8 protocol)
1165b42597e2SDavid S. Miller {
1166b42597e2SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *)skb->data;
1167b42597e2SDavid S. Miller 	struct flowi4 fl4;
1168b42597e2SDavid S. Miller 	struct rtable *rt;
1169b42597e2SDavid S. Miller 
1170b1ad4138SGuillaume Nault 	__build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, 0, 0);
1171b42597e2SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
1172b42597e2SDavid S. Miller 	if (!IS_ERR(rt)) {
1173ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
1174b42597e2SDavid S. Miller 		ip_rt_put(rt);
1175b42597e2SDavid S. Miller 	}
1176b42597e2SDavid S. Miller }
1177b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect);
1178b42597e2SDavid S. Miller 
1179b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
1180b42597e2SDavid S. Miller {
11814895c771SDavid S. Miller 	const struct iphdr *iph = (const struct iphdr *)skb->data;
11824895c771SDavid S. Miller 	struct flowi4 fl4;
11834895c771SDavid S. Miller 	struct rtable *rt;
1184e2d118a1SLorenzo Colitti 	struct net *net = sock_net(sk);
1185b42597e2SDavid S. Miller 
1186e2d118a1SLorenzo Colitti 	__build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
1187e2d118a1SLorenzo Colitti 	rt = __ip_route_output_key(net, &fl4);
11884895c771SDavid S. Miller 	if (!IS_ERR(rt)) {
1189ceb33206SDavid S. Miller 		__ip_do_redirect(rt, skb, &fl4, false);
11904895c771SDavid S. Miller 		ip_rt_put(rt);
11914895c771SDavid S. Miller 	}
1192b42597e2SDavid S. Miller }
1193b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
1194b42597e2SDavid S. Miller 
1195bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ipv4_dst_check(struct dst_entry *dst,
1196bbd807dfSBrian Vazquez 							 u32 cookie)
1197efbc368dSDavid S. Miller {
1198efbc368dSDavid S. Miller 	struct rtable *rt = (struct rtable *) dst;
1199efbc368dSDavid S. Miller 
1200ceb33206SDavid S. Miller 	/* All IPV4 dsts are created with ->obsolete set to the value
1201ceb33206SDavid S. Miller 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1202ceb33206SDavid S. Miller 	 * into this function always.
1203ceb33206SDavid S. Miller 	 *
1204387aa65aSTimo Teräs 	 * When a PMTU/redirect information update invalidates a route,
1205387aa65aSTimo Teräs 	 * this is indicated by setting obsolete to DST_OBSOLETE_KILL or
120602afc7adSJulian Wiedmann 	 * DST_OBSOLETE_DEAD.
1207ceb33206SDavid S. Miller 	 */
1208387aa65aSTimo Teräs 	if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt))
1209efbc368dSDavid S. Miller 		return NULL;
1210d11a4dc1STimo Teräs 	return dst;
12111da177e4SLinus Torvalds }
12129c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_dst_check);
12131da177e4SLinus Torvalds 
121420ff83f1SEric Dumazet static void ipv4_send_dest_unreach(struct sk_buff *skb)
12151da177e4SLinus Torvalds {
1216ed0de45aSStephen Suryaputra 	struct ip_options opt;
1217c543cb4aSEric Dumazet 	int res;
12181da177e4SLinus Torvalds 
1219ed0de45aSStephen Suryaputra 	/* Recompile ip options since IPCB may not be valid anymore.
122020ff83f1SEric Dumazet 	 * Also check we have a reasonable ipv4 header.
1221ed0de45aSStephen Suryaputra 	 */
122220ff83f1SEric Dumazet 	if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) ||
122320ff83f1SEric Dumazet 	    ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5)
122420ff83f1SEric Dumazet 		return;
122520ff83f1SEric Dumazet 
1226ed0de45aSStephen Suryaputra 	memset(&opt, 0, sizeof(opt));
122720ff83f1SEric Dumazet 	if (ip_hdr(skb)->ihl > 5) {
122820ff83f1SEric Dumazet 		if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4))
122920ff83f1SEric Dumazet 			return;
1230ed0de45aSStephen Suryaputra 		opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr);
1231c543cb4aSEric Dumazet 
1232c543cb4aSEric Dumazet 		rcu_read_lock();
1233c543cb4aSEric Dumazet 		res = __ip_options_compile(dev_net(skb->dev), &opt, skb, NULL);
1234c543cb4aSEric Dumazet 		rcu_read_unlock();
1235c543cb4aSEric Dumazet 
1236c543cb4aSEric Dumazet 		if (res)
1237ed0de45aSStephen Suryaputra 			return;
123820ff83f1SEric Dumazet 	}
1239ed0de45aSStephen Suryaputra 	__icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &opt);
124020ff83f1SEric Dumazet }
124120ff83f1SEric Dumazet 
124220ff83f1SEric Dumazet static void ipv4_link_failure(struct sk_buff *skb)
124320ff83f1SEric Dumazet {
124420ff83f1SEric Dumazet 	struct rtable *rt;
124520ff83f1SEric Dumazet 
124620ff83f1SEric Dumazet 	ipv4_send_dest_unreach(skb);
12471da177e4SLinus Torvalds 
1248511c3f92SEric Dumazet 	rt = skb_rtable(skb);
12495943634fSDavid S. Miller 	if (rt)
12505943634fSDavid S. Miller 		dst_set_expires(&rt->dst, 0);
12512c8cec5cSDavid S. Miller }
12521da177e4SLinus Torvalds 
1253ede2059dSEric W. Biederman static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb)
12541da177e4SLinus Torvalds {
125591df42beSJoe Perches 	pr_debug("%s: %pI4 -> %pI4, %s\n",
125691df42beSJoe Perches 		 __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
12571da177e4SLinus Torvalds 		 skb->dev ? skb->dev->name : "?");
12581da177e4SLinus Torvalds 	kfree_skb(skb);
1259c378a9c0SDave Jones 	WARN_ON(1);
12601da177e4SLinus Torvalds 	return 0;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds /*
12646ad08600SShubhankar Kuranagatti  * We do not cache source address of outgoing interface,
12656ad08600SShubhankar Kuranagatti  * because it is used only by IP RR, TS and SRR options,
12666ad08600SShubhankar Kuranagatti  * so that it out of fast path.
12676ad08600SShubhankar Kuranagatti  *
12686ad08600SShubhankar Kuranagatti  * BTW remember: "addr" is allowed to be not aligned
12696ad08600SShubhankar Kuranagatti  * in IP options!
12701da177e4SLinus Torvalds  */
12711da177e4SLinus Torvalds 
12728e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
12731da177e4SLinus Torvalds {
1274a61ced5dSAl Viro 	__be32 src;
12751da177e4SLinus Torvalds 
1276c7537967SDavid S. Miller 	if (rt_is_output_route(rt))
1277c5be24ffSDavid S. Miller 		src = ip_hdr(skb)->saddr;
1278ebc0ffaeSEric Dumazet 	else {
12798e36360aSDavid S. Miller 		struct fib_result res;
1280e351bb62SMaciej Żenczykowski 		struct iphdr *iph = ip_hdr(skb);
1281e351bb62SMaciej Żenczykowski 		struct flowi4 fl4 = {
1282e351bb62SMaciej Żenczykowski 			.daddr = iph->daddr,
1283e351bb62SMaciej Żenczykowski 			.saddr = iph->saddr,
1284e351bb62SMaciej Żenczykowski 			.flowi4_tos = RT_TOS(iph->tos),
1285e351bb62SMaciej Żenczykowski 			.flowi4_oif = rt->dst.dev->ifindex,
1286e351bb62SMaciej Żenczykowski 			.flowi4_iif = skb->dev->ifindex,
1287e351bb62SMaciej Żenczykowski 			.flowi4_mark = skb->mark,
1288e351bb62SMaciej Żenczykowski 		};
12895e2b61f7SDavid S. Miller 
1290ebc0ffaeSEric Dumazet 		rcu_read_lock();
12910eeb075fSAndy Gospodarek 		if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0)
1292eba618abSDavid Ahern 			src = fib_result_prefsrc(dev_net(rt->dst.dev), &res);
1293ebc0ffaeSEric Dumazet 		else
1294f8126f1dSDavid S. Miller 			src = inet_select_addr(rt->dst.dev,
1295f8126f1dSDavid S. Miller 					       rt_nexthop(rt, iph->daddr),
12961da177e4SLinus Torvalds 					       RT_SCOPE_UNIVERSE);
1297ebc0ffaeSEric Dumazet 		rcu_read_unlock();
1298ebc0ffaeSEric Dumazet 	}
12991da177e4SLinus Torvalds 	memcpy(addr, &src, 4);
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
1302c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
13031da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
13041da177e4SLinus Torvalds {
1305d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF))
1306d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF;
1307d8d1f30bSChangli Gao 	if (!(rt->dst.tclassid & 0xFFFF0000))
1308d8d1f30bSChangli Gao 		rt->dst.tclassid |= tag & 0xFFFF0000;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds #endif
13111da177e4SLinus Torvalds 
13120dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
13130dbaee3bSDavid S. Miller {
13142e9589ffSxu xin 	struct net *net = dev_net(dst->dev);
13157ed14d97SGao Feng 	unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
1316164a5e7aSEric Dumazet 	unsigned int advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size,
13172e9589ffSxu xin 				    net->ipv4.ip_rt_min_advmss);
13187ed14d97SGao Feng 
13197ed14d97SGao Feng 	return min(advmss, IPV4_MAX_PMTU - header_size);
13200dbaee3bSDavid S. Miller }
13210dbaee3bSDavid S. Miller 
1322f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ipv4_mtu(const struct dst_entry *dst)
1323d33e4553SDavid S. Miller {
1324ac6627a2SVadim Fedorenko 	return ip_dst_mtu_maybe_forward(dst, false);
1325d33e4553SDavid S. Miller }
13269c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_mtu);
1327d33e4553SDavid S. Miller 
1328a5995e71SDavid Ahern static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr)
132994720e3aSJulian Anastasov {
133094720e3aSJulian Anastasov 	struct fnhe_hash_bucket *hash;
133194720e3aSJulian Anastasov 	struct fib_nh_exception *fnhe, __rcu **fnhe_p;
133294720e3aSJulian Anastasov 	u32 hval = fnhe_hashfun(daddr);
133394720e3aSJulian Anastasov 
133494720e3aSJulian Anastasov 	spin_lock_bh(&fnhe_lock);
133594720e3aSJulian Anastasov 
1336a5995e71SDavid Ahern 	hash = rcu_dereference_protected(nhc->nhc_exceptions,
133794720e3aSJulian Anastasov 					 lockdep_is_held(&fnhe_lock));
133894720e3aSJulian Anastasov 	hash += hval;
133994720e3aSJulian Anastasov 
134094720e3aSJulian Anastasov 	fnhe_p = &hash->chain;
134194720e3aSJulian Anastasov 	fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock));
134294720e3aSJulian Anastasov 	while (fnhe) {
134394720e3aSJulian Anastasov 		if (fnhe->fnhe_daddr == daddr) {
134494720e3aSJulian Anastasov 			rcu_assign_pointer(*fnhe_p, rcu_dereference_protected(
134594720e3aSJulian Anastasov 				fnhe->fnhe_next, lockdep_is_held(&fnhe_lock)));
1346ee60ad21SXin Long 			/* set fnhe_daddr to 0 to ensure it won't bind with
1347ee60ad21SXin Long 			 * new dsts in rt_bind_exception().
1348ee60ad21SXin Long 			 */
1349ee60ad21SXin Long 			fnhe->fnhe_daddr = 0;
135094720e3aSJulian Anastasov 			fnhe_flush_routes(fnhe);
135194720e3aSJulian Anastasov 			kfree_rcu(fnhe, rcu);
135294720e3aSJulian Anastasov 			break;
135394720e3aSJulian Anastasov 		}
135494720e3aSJulian Anastasov 		fnhe_p = &fnhe->fnhe_next;
135594720e3aSJulian Anastasov 		fnhe = rcu_dereference_protected(fnhe->fnhe_next,
135694720e3aSJulian Anastasov 						 lockdep_is_held(&fnhe_lock));
135794720e3aSJulian Anastasov 	}
135894720e3aSJulian Anastasov 
135994720e3aSJulian Anastasov 	spin_unlock_bh(&fnhe_lock);
136094720e3aSJulian Anastasov }
136194720e3aSJulian Anastasov 
1362a5995e71SDavid Ahern static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc,
1363a5995e71SDavid Ahern 					       __be32 daddr)
13644895c771SDavid S. Miller {
1365a5995e71SDavid Ahern 	struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions);
13664895c771SDavid S. Miller 	struct fib_nh_exception *fnhe;
13674895c771SDavid S. Miller 	u32 hval;
13684895c771SDavid S. Miller 
1369f2bb4bedSDavid S. Miller 	if (!hash)
1370f2bb4bedSDavid S. Miller 		return NULL;
1371f2bb4bedSDavid S. Miller 
1372d3a25c98SDavid S. Miller 	hval = fnhe_hashfun(daddr);
13734895c771SDavid S. Miller 
13744895c771SDavid S. Miller 	for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
13754895c771SDavid S. Miller 	     fnhe = rcu_dereference(fnhe->fnhe_next)) {
137694720e3aSJulian Anastasov 		if (fnhe->fnhe_daddr == daddr) {
137794720e3aSJulian Anastasov 			if (fnhe->fnhe_expires &&
137894720e3aSJulian Anastasov 			    time_after(jiffies, fnhe->fnhe_expires)) {
1379a5995e71SDavid Ahern 				ip_del_fnhe(nhc, daddr);
138094720e3aSJulian Anastasov 				break;
138194720e3aSJulian Anastasov 			}
1382f2bb4bedSDavid S. Miller 			return fnhe;
1383f2bb4bedSDavid S. Miller 		}
138494720e3aSJulian Anastasov 	}
1385f2bb4bedSDavid S. Miller 	return NULL;
1386f2bb4bedSDavid S. Miller }
1387f2bb4bedSDavid S. Miller 
138850d889b1SDavid Ahern /* MTU selection:
138950d889b1SDavid Ahern  * 1. mtu on route is locked - use it
139050d889b1SDavid Ahern  * 2. mtu from nexthop exception
139150d889b1SDavid Ahern  * 3. mtu from egress device
139250d889b1SDavid Ahern  */
139350d889b1SDavid Ahern 
139450d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr)
139550d889b1SDavid Ahern {
1396eba618abSDavid Ahern 	struct fib_nh_common *nhc = res->nhc;
1397eba618abSDavid Ahern 	struct net_device *dev = nhc->nhc_dev;
139850d889b1SDavid Ahern 	struct fib_info *fi = res->fi;
139950d889b1SDavid Ahern 	u32 mtu = 0;
140050d889b1SDavid Ahern 
140160c158dcSKuniyuki Iwashima 	if (READ_ONCE(dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu) ||
140250d889b1SDavid Ahern 	    fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU))
140350d889b1SDavid Ahern 		mtu = fi->fib_mtu;
140450d889b1SDavid Ahern 
140550d889b1SDavid Ahern 	if (likely(!mtu)) {
140650d889b1SDavid Ahern 		struct fib_nh_exception *fnhe;
140750d889b1SDavid Ahern 
1408a5995e71SDavid Ahern 		fnhe = find_exception(nhc, daddr);
140950d889b1SDavid Ahern 		if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires))
141050d889b1SDavid Ahern 			mtu = fnhe->fnhe_pmtu;
141150d889b1SDavid Ahern 	}
141250d889b1SDavid Ahern 
141350d889b1SDavid Ahern 	if (likely(!mtu))
141450d889b1SDavid Ahern 		mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU);
141550d889b1SDavid Ahern 
1416eba618abSDavid Ahern 	return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu);
141750d889b1SDavid Ahern }
141850d889b1SDavid Ahern 
1419caacf05eSDavid S. Miller static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
1420a4c2fd7fSWei Wang 			      __be32 daddr, const bool do_cache)
1421f2bb4bedSDavid S. Miller {
1422caacf05eSDavid S. Miller 	bool ret = false;
1423caacf05eSDavid S. Miller 
1424c5038a83SDavid S. Miller 	spin_lock_bh(&fnhe_lock);
1425aee06da6SJulian Anastasov 
1426c5038a83SDavid S. Miller 	if (daddr == fnhe->fnhe_daddr) {
14272ffae99dSTimo Teräs 		struct rtable __rcu **porig;
14282ffae99dSTimo Teräs 		struct rtable *orig;
14295aad1de5STimo Teräs 		int genid = fnhe_genid(dev_net(rt->dst.dev));
14302ffae99dSTimo Teräs 
14312ffae99dSTimo Teräs 		if (rt_is_input_route(rt))
14322ffae99dSTimo Teräs 			porig = &fnhe->fnhe_rth_input;
14332ffae99dSTimo Teräs 		else
14342ffae99dSTimo Teräs 			porig = &fnhe->fnhe_rth_output;
14352ffae99dSTimo Teräs 		orig = rcu_dereference(*porig);
14365aad1de5STimo Teräs 
14375aad1de5STimo Teräs 		if (fnhe->fnhe_genid != genid) {
14385aad1de5STimo Teräs 			fnhe->fnhe_genid = genid;
143913d82bf5SSteffen Klassert 			fnhe->fnhe_gw = 0;
144013d82bf5SSteffen Klassert 			fnhe->fnhe_pmtu = 0;
144113d82bf5SSteffen Klassert 			fnhe->fnhe_expires = 0;
14420e8411e4SHangbin Liu 			fnhe->fnhe_mtu_locked = false;
14432ffae99dSTimo Teräs 			fnhe_flush_routes(fnhe);
14442ffae99dSTimo Teräs 			orig = NULL;
144513d82bf5SSteffen Klassert 		}
1446387aa65aSTimo Teräs 		fill_route_from_fnhe(rt, fnhe);
14471550c171SDavid Ahern 		if (!rt->rt_gw4) {
14481550c171SDavid Ahern 			rt->rt_gw4 = daddr;
14491550c171SDavid Ahern 			rt->rt_gw_family = AF_INET;
14501550c171SDavid Ahern 		}
1451f2bb4bedSDavid S. Miller 
1452a4c2fd7fSWei Wang 		if (do_cache) {
14530830106cSWei Wang 			dst_hold(&rt->dst);
14542ffae99dSTimo Teräs 			rcu_assign_pointer(*porig, rt);
14550830106cSWei Wang 			if (orig) {
145695c47f9cSWei Wang 				dst_dev_put(&orig->dst);
14570830106cSWei Wang 				dst_release(&orig->dst);
14580830106cSWei Wang 			}
14592ffae99dSTimo Teräs 			ret = true;
14602ffae99dSTimo Teräs 		}
1461c5038a83SDavid S. Miller 
1462c5038a83SDavid S. Miller 		fnhe->fnhe_stamp = jiffies;
1463c5038a83SDavid S. Miller 	}
1464c5038a83SDavid S. Miller 	spin_unlock_bh(&fnhe_lock);
1465caacf05eSDavid S. Miller 
1466caacf05eSDavid S. Miller 	return ret;
146754764bb6SEric Dumazet }
146854764bb6SEric Dumazet 
146987063a1fSDavid Ahern static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt)
1470f2bb4bedSDavid S. Miller {
1471d26b3a7cSEric Dumazet 	struct rtable *orig, *prev, **p;
1472caacf05eSDavid S. Miller 	bool ret = true;
1473f2bb4bedSDavid S. Miller 
1474d26b3a7cSEric Dumazet 	if (rt_is_input_route(rt)) {
14750f457a36SDavid Ahern 		p = (struct rtable **)&nhc->nhc_rth_input;
1476d26b3a7cSEric Dumazet 	} else {
14770f457a36SDavid Ahern 		p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
1478d26b3a7cSEric Dumazet 	}
1479f2bb4bedSDavid S. Miller 	orig = *p;
1480f2bb4bedSDavid S. Miller 
14810830106cSWei Wang 	/* hold dst before doing cmpxchg() to avoid race condition
14820830106cSWei Wang 	 * on this dst
14830830106cSWei Wang 	 */
14840830106cSWei Wang 	dst_hold(&rt->dst);
1485f2bb4bedSDavid S. Miller 	prev = cmpxchg(p, orig, rt);
1486f2bb4bedSDavid S. Miller 	if (prev == orig) {
14870830106cSWei Wang 		if (orig) {
14885018c596SWei Wang 			rt_add_uncached_list(orig);
14890830106cSWei Wang 			dst_release(&orig->dst);
14900830106cSWei Wang 		}
14910830106cSWei Wang 	} else {
14920830106cSWei Wang 		dst_release(&rt->dst);
1493caacf05eSDavid S. Miller 		ret = false;
14940830106cSWei Wang 	}
1495caacf05eSDavid S. Miller 
1496caacf05eSDavid S. Miller 	return ret;
1497caacf05eSDavid S. Miller }
1498caacf05eSDavid S. Miller 
14995055c371SEric Dumazet struct uncached_list {
15005055c371SEric Dumazet 	spinlock_t		lock;
15015055c371SEric Dumazet 	struct list_head	head;
150229e5375dSEric Dumazet 	struct list_head	quarantine;
15035055c371SEric Dumazet };
15045055c371SEric Dumazet 
15055055c371SEric Dumazet static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
1506caacf05eSDavid S. Miller 
1507510c321bSXin Long void rt_add_uncached_list(struct rtable *rt)
1508caacf05eSDavid S. Miller {
15095055c371SEric Dumazet 	struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
15105055c371SEric Dumazet 
1511d288a162SWangyang Guo 	rt->dst.rt_uncached_list = ul;
15125055c371SEric Dumazet 
15135055c371SEric Dumazet 	spin_lock_bh(&ul->lock);
1514d288a162SWangyang Guo 	list_add_tail(&rt->dst.rt_uncached, &ul->head);
15155055c371SEric Dumazet 	spin_unlock_bh(&ul->lock);
1516caacf05eSDavid S. Miller }
1517caacf05eSDavid S. Miller 
1518510c321bSXin Long void rt_del_uncached_list(struct rtable *rt)
1519510c321bSXin Long {
1520d288a162SWangyang Guo 	if (!list_empty(&rt->dst.rt_uncached)) {
1521d288a162SWangyang Guo 		struct uncached_list *ul = rt->dst.rt_uncached_list;
1522510c321bSXin Long 
1523510c321bSXin Long 		spin_lock_bh(&ul->lock);
1524d288a162SWangyang Guo 		list_del_init(&rt->dst.rt_uncached);
1525510c321bSXin Long 		spin_unlock_bh(&ul->lock);
1526510c321bSXin Long 	}
1527510c321bSXin Long }
1528510c321bSXin Long 
1529caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst)
1530caacf05eSDavid S. Miller {
1531caacf05eSDavid S. Miller 	struct rtable *rt = (struct rtable *)dst;
1532caacf05eSDavid S. Miller 
15331620a336SDavid Ahern 	ip_dst_metrics_put(dst);
1534510c321bSXin Long 	rt_del_uncached_list(rt);
1535caacf05eSDavid S. Miller }
1536caacf05eSDavid S. Miller 
1537caacf05eSDavid S. Miller void rt_flush_dev(struct net_device *dev)
1538caacf05eSDavid S. Miller {
153929e5375dSEric Dumazet 	struct rtable *rt, *safe;
15405055c371SEric Dumazet 	int cpu;
1541caacf05eSDavid S. Miller 
15425055c371SEric Dumazet 	for_each_possible_cpu(cpu) {
15435055c371SEric Dumazet 		struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
15445055c371SEric Dumazet 
154529e5375dSEric Dumazet 		if (list_empty(&ul->head))
154629e5375dSEric Dumazet 			continue;
154729e5375dSEric Dumazet 
15485055c371SEric Dumazet 		spin_lock_bh(&ul->lock);
1549d288a162SWangyang Guo 		list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
1550caacf05eSDavid S. Miller 			if (rt->dst.dev != dev)
1551caacf05eSDavid S. Miller 				continue;
15528d7017fdSMahesh Bandewar 			rt->dst.dev = blackhole_netdev;
1553d62607c3SJakub Kicinski 			netdev_ref_replace(dev, blackhole_netdev,
1554d62607c3SJakub Kicinski 					   &rt->dst.dev_tracker, GFP_ATOMIC);
1555d288a162SWangyang Guo 			list_move(&rt->dst.rt_uncached, &ul->quarantine);
1556caacf05eSDavid S. Miller 		}
15575055c371SEric Dumazet 		spin_unlock_bh(&ul->lock);
15584895c771SDavid S. Miller 	}
15594895c771SDavid S. Miller }
15604895c771SDavid S. Miller 
15614331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt)
1562d2d68ba9SDavid S. Miller {
15634331debcSEric Dumazet 	return	rt &&
15644331debcSEric Dumazet 		rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
15654331debcSEric Dumazet 		!rt_is_expired(rt);
1566d2d68ba9SDavid S. Miller }
1567d2d68ba9SDavid S. Miller 
1568f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
15695e2b61f7SDavid S. Miller 			   const struct fib_result *res,
1570f2bb4bedSDavid S. Miller 			   struct fib_nh_exception *fnhe,
1571a4c2fd7fSWei Wang 			   struct fib_info *fi, u16 type, u32 itag,
1572a4c2fd7fSWei Wang 			   const bool do_cache)
15731da177e4SLinus Torvalds {
1574caacf05eSDavid S. Miller 	bool cached = false;
1575caacf05eSDavid S. Miller 
15761da177e4SLinus Torvalds 	if (fi) {
1577eba618abSDavid Ahern 		struct fib_nh_common *nhc = FIB_RES_NHC(*res);
15784895c771SDavid S. Miller 
15790f5f7d7bSDavid Ahern 		if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) {
158077d5bc7eSDavid Ahern 			rt->rt_uses_gateway = 1;
15810f5f7d7bSDavid Ahern 			rt->rt_gw_family = nhc->nhc_gw_family;
15820f5f7d7bSDavid Ahern 			/* only INET and INET6 are supported */
15830f5f7d7bSDavid Ahern 			if (likely(nhc->nhc_gw_family == AF_INET))
15840f5f7d7bSDavid Ahern 				rt->rt_gw4 = nhc->nhc_gw.ipv4;
15850f5f7d7bSDavid Ahern 			else
15860f5f7d7bSDavid Ahern 				rt->rt_gw6 = nhc->nhc_gw.ipv6;
1587155e8336SJulian Anastasov 		}
15880f5f7d7bSDavid Ahern 
1589e1255ed4SDavid Ahern 		ip_dst_init_metrics(&rt->dst, fi->fib_metrics);
1590e1255ed4SDavid Ahern 
1591c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1592dcb1ecb5SDavid Ahern 		if (nhc->nhc_family == AF_INET) {
159387063a1fSDavid Ahern 			struct fib_nh *nh;
159487063a1fSDavid Ahern 
159587063a1fSDavid Ahern 			nh = container_of(nhc, struct fib_nh, nh_common);
1596f2bb4bedSDavid S. Miller 			rt->dst.tclassid = nh->nh_tclassid;
159787063a1fSDavid Ahern 		}
15981da177e4SLinus Torvalds #endif
159987063a1fSDavid Ahern 		rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
1600c5038a83SDavid S. Miller 		if (unlikely(fnhe))
1601a4c2fd7fSWei Wang 			cached = rt_bind_exception(rt, fnhe, daddr, do_cache);
1602a4c2fd7fSWei Wang 		else if (do_cache)
160387063a1fSDavid Ahern 			cached = rt_cache_route(nhc, rt);
1604155e8336SJulian Anastasov 		if (unlikely(!cached)) {
1605155e8336SJulian Anastasov 			/* Routes we intend to cache in nexthop exception or
1606155e8336SJulian Anastasov 			 * FIB nexthop have the DST_NOCACHE bit clear.
1607155e8336SJulian Anastasov 			 * However, if we are unsuccessful at storing this
1608155e8336SJulian Anastasov 			 * route into the cache we really need to set it.
1609155e8336SJulian Anastasov 			 */
16101550c171SDavid Ahern 			if (!rt->rt_gw4) {
16111550c171SDavid Ahern 				rt->rt_gw_family = AF_INET;
16121550c171SDavid Ahern 				rt->rt_gw4 = daddr;
16131550c171SDavid Ahern 			}
1614155e8336SJulian Anastasov 			rt_add_uncached_list(rt);
1615d33e4553SDavid S. Miller 		}
1616155e8336SJulian Anastasov 	} else
1617caacf05eSDavid S. Miller 		rt_add_uncached_list(rt);
16181da177e4SLinus Torvalds 
1619c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
16201da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
162185b91b03SDavid S. Miller 	set_class_tag(rt, res->tclassid);
16221da177e4SLinus Torvalds #endif
16231da177e4SLinus Torvalds 	set_class_tag(rt, itag);
16241da177e4SLinus Torvalds #endif
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16279ab179d8SDavid Ahern struct rtable *rt_dst_alloc(struct net_device *dev,
1628d08c4f35SDavid Ahern 			    unsigned int flags, u16 type,
1629b5c8b3feSEyal Birger 			    bool noxfrm)
16300c4dcd58SDavid S. Miller {
1631d08c4f35SDavid Ahern 	struct rtable *rt;
1632d08c4f35SDavid Ahern 
1633d08c4f35SDavid Ahern 	rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1634b2a9c0edSWei Wang 		       (noxfrm ? DST_NOXFRM : 0));
1635d08c4f35SDavid Ahern 
1636d08c4f35SDavid Ahern 	if (rt) {
1637d08c4f35SDavid Ahern 		rt->rt_genid = rt_genid_ipv4(dev_net(dev));
1638d08c4f35SDavid Ahern 		rt->rt_flags = flags;
1639d08c4f35SDavid Ahern 		rt->rt_type = type;
1640d08c4f35SDavid Ahern 		rt->rt_is_input = 0;
1641d08c4f35SDavid Ahern 		rt->rt_iif = 0;
1642d08c4f35SDavid Ahern 		rt->rt_pmtu = 0;
1643d52e5a7eSSabrina Dubroca 		rt->rt_mtu_locked = 0;
164477d5bc7eSDavid Ahern 		rt->rt_uses_gateway = 0;
16451550c171SDavid Ahern 		rt->rt_gw_family = 0;
16461550c171SDavid Ahern 		rt->rt_gw4 = 0;
1647d08c4f35SDavid Ahern 
1648d08c4f35SDavid Ahern 		rt->dst.output = ip_output;
1649d08c4f35SDavid Ahern 		if (flags & RTCF_LOCAL)
1650d08c4f35SDavid Ahern 			rt->dst.input = ip_local_deliver;
1651d08c4f35SDavid Ahern 	}
1652d08c4f35SDavid Ahern 
1653d08c4f35SDavid Ahern 	return rt;
16540c4dcd58SDavid S. Miller }
16559ab179d8SDavid Ahern EXPORT_SYMBOL(rt_dst_alloc);
16560c4dcd58SDavid S. Miller 
16575b18f128SStephen Suryaputra struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt)
16585b18f128SStephen Suryaputra {
16595b18f128SStephen Suryaputra 	struct rtable *new_rt;
16605b18f128SStephen Suryaputra 
16615b18f128SStephen Suryaputra 	new_rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
16625b18f128SStephen Suryaputra 			   rt->dst.flags);
16635b18f128SStephen Suryaputra 
16645b18f128SStephen Suryaputra 	if (new_rt) {
16655b18f128SStephen Suryaputra 		new_rt->rt_genid = rt_genid_ipv4(dev_net(dev));
16665b18f128SStephen Suryaputra 		new_rt->rt_flags = rt->rt_flags;
16675b18f128SStephen Suryaputra 		new_rt->rt_type = rt->rt_type;
16685b18f128SStephen Suryaputra 		new_rt->rt_is_input = rt->rt_is_input;
16695b18f128SStephen Suryaputra 		new_rt->rt_iif = rt->rt_iif;
16705b18f128SStephen Suryaputra 		new_rt->rt_pmtu = rt->rt_pmtu;
16715b18f128SStephen Suryaputra 		new_rt->rt_mtu_locked = rt->rt_mtu_locked;
16725b18f128SStephen Suryaputra 		new_rt->rt_gw_family = rt->rt_gw_family;
16735b18f128SStephen Suryaputra 		if (rt->rt_gw_family == AF_INET)
16745b18f128SStephen Suryaputra 			new_rt->rt_gw4 = rt->rt_gw4;
16755b18f128SStephen Suryaputra 		else if (rt->rt_gw_family == AF_INET6)
16765b18f128SStephen Suryaputra 			new_rt->rt_gw6 = rt->rt_gw6;
16775b18f128SStephen Suryaputra 
16785b18f128SStephen Suryaputra 		new_rt->dst.input = rt->dst.input;
16795b18f128SStephen Suryaputra 		new_rt->dst.output = rt->dst.output;
16805b18f128SStephen Suryaputra 		new_rt->dst.error = rt->dst.error;
16815b18f128SStephen Suryaputra 		new_rt->dst.lastuse = jiffies;
16825b18f128SStephen Suryaputra 		new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate);
16835b18f128SStephen Suryaputra 	}
16845b18f128SStephen Suryaputra 	return new_rt;
16855b18f128SStephen Suryaputra }
16865b18f128SStephen Suryaputra EXPORT_SYMBOL(rt_dst_clone);
16875b18f128SStephen Suryaputra 
168896d36220SEric Dumazet /* called in rcu_read_lock() section */
1689bc044e8dSPaolo Abeni int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1690bc044e8dSPaolo Abeni 			  u8 tos, struct net_device *dev,
1691bc044e8dSPaolo Abeni 			  struct in_device *in_dev, u32 *itag)
16921da177e4SLinus Torvalds {
1693b5f7e755SEric Dumazet 	int err;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	/* Primary sanity checks. */
169651456b29SIan Morris 	if (!in_dev)
16971da177e4SLinus Torvalds 		return -EINVAL;
16981da177e4SLinus Torvalds 
16991e637c74SJan Engelhardt 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1700d0daebc3SThomas Graf 	    skb->protocol != htons(ETH_P_IP))
1701bc044e8dSPaolo Abeni 		return -EINVAL;
1702d0daebc3SThomas Graf 
170375fea73dSAlexander Duyck 	if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev))
1704bc044e8dSPaolo Abeni 		return -EINVAL;
17051da177e4SLinus Torvalds 
1706f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr)) {
17071d2f4ebbSEdward Chron 		if (!ipv4_is_local_multicast(daddr) &&
17081d2f4ebbSEdward Chron 		    ip_hdr(skb)->protocol != IPPROTO_IGMP)
1709bc044e8dSPaolo Abeni 			return -EINVAL;
1710b5f7e755SEric Dumazet 	} else {
17119e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
1712bc044e8dSPaolo Abeni 					  in_dev, itag);
1713b5f7e755SEric Dumazet 		if (err < 0)
1714bc044e8dSPaolo Abeni 			return err;
1715b5f7e755SEric Dumazet 	}
1716bc044e8dSPaolo Abeni 	return 0;
1717bc044e8dSPaolo Abeni }
1718bc044e8dSPaolo Abeni 
1719bc044e8dSPaolo Abeni /* called in rcu_read_lock() section */
1720bc044e8dSPaolo Abeni static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1721bc044e8dSPaolo Abeni 			     u8 tos, struct net_device *dev, int our)
1722bc044e8dSPaolo Abeni {
1723bc044e8dSPaolo Abeni 	struct in_device *in_dev = __in_dev_get_rcu(dev);
1724bc044e8dSPaolo Abeni 	unsigned int flags = RTCF_MULTICAST;
1725bc044e8dSPaolo Abeni 	struct rtable *rth;
1726bc044e8dSPaolo Abeni 	u32 itag = 0;
1727bc044e8dSPaolo Abeni 	int err;
1728bc044e8dSPaolo Abeni 
1729bc044e8dSPaolo Abeni 	err = ip_mc_validate_source(skb, daddr, saddr, tos, dev, in_dev, &itag);
1730bc044e8dSPaolo Abeni 	if (err)
1731bc044e8dSPaolo Abeni 		return err;
1732bc044e8dSPaolo Abeni 
1733d08c4f35SDavid Ahern 	if (our)
1734d08c4f35SDavid Ahern 		flags |= RTCF_LOCAL;
1735d08c4f35SDavid Ahern 
1736b5c8b3feSEyal Birger 	if (IN_DEV_ORCONF(in_dev, NOPOLICY))
1737e6175a2eSEyal Birger 		IPCB(skb)->flags |= IPSKB_NOPOLICY;
1738e6175a2eSEyal Birger 
1739d08c4f35SDavid Ahern 	rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST,
1740b5c8b3feSEyal Birger 			   false);
17411da177e4SLinus Torvalds 	if (!rth)
1742bc044e8dSPaolo Abeni 		return -ENOBUFS;
17431da177e4SLinus Torvalds 
1744c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1745d8d1f30bSChangli Gao 	rth->dst.tclassid = itag;
17461da177e4SLinus Torvalds #endif
1747cf911662SDavid S. Miller 	rth->dst.output = ip_rt_bug;
17489917e1e8SDavid S. Miller 	rth->rt_is_input= 1;
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1751f97c1e0cSJoe Perches 	if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
1752d8d1f30bSChangli Gao 		rth->dst.input = ip_mr_input;
17531da177e4SLinus Torvalds #endif
17541da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_slow_mc);
17551da177e4SLinus Torvalds 
17569e6c6d17SLokesh Dhoundiyal 	skb_dst_drop(skb);
175789aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
175889aef892SDavid S. Miller 	return 0;
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
17631da177e4SLinus Torvalds 				     struct in_device *in_dev,
17641da177e4SLinus Torvalds 				     struct sk_buff *skb,
17659e12bb22SAl Viro 				     __be32 daddr,
17669e12bb22SAl Viro 				     __be32 saddr)
17671da177e4SLinus Torvalds {
17681da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_src);
17691da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
17701da177e4SLinus Torvalds 	if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
17711da177e4SLinus Torvalds 		/*
17721da177e4SLinus Torvalds 		 *	RFC1812 recommendation, if source is martian,
17731da177e4SLinus Torvalds 		 *	the only hint is MAC header.
17741da177e4SLinus Torvalds 		 */
1775058bd4d2SJoe Perches 		pr_warn("martian source %pI4 from %pI4, on dev %s\n",
1776673d57e7SHarvey Harrison 			&daddr, &saddr, dev->name);
177798e399f8SArnaldo Carvalho de Melo 		if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1778058bd4d2SJoe Perches 			print_hex_dump(KERN_WARNING, "ll header: ",
1779058bd4d2SJoe Perches 				       DUMP_PREFIX_OFFSET, 16, 1,
1780058bd4d2SJoe Perches 				       skb_mac_header(skb),
1781b2c85100SDavid S. Miller 				       dev->hard_header_len, false);
17821da177e4SLinus Torvalds 		}
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds #endif
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
178747360228SEric Dumazet /* called in rcu_read_lock() section */
17885969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb,
1789982721f3SDavid S. Miller 			   const struct fib_result *res,
17901da177e4SLinus Torvalds 			   struct in_device *in_dev,
1791c6cffba4SDavid S. Miller 			   __be32 daddr, __be32 saddr, u32 tos)
17921da177e4SLinus Torvalds {
1793eba618abSDavid Ahern 	struct fib_nh_common *nhc = FIB_RES_NHC(*res);
1794eba618abSDavid Ahern 	struct net_device *dev = nhc->nhc_dev;
17952ffae99dSTimo Teräs 	struct fib_nh_exception *fnhe;
17961da177e4SLinus Torvalds 	struct rtable *rth;
17971da177e4SLinus Torvalds 	int err;
17981da177e4SLinus Torvalds 	struct in_device *out_dev;
1799b5c8b3feSEyal Birger 	bool do_cache;
1800fbdc0ad0SLi RongQing 	u32 itag = 0;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	/* get a working reference to the output device */
1803eba618abSDavid Ahern 	out_dev = __in_dev_get_rcu(dev);
180451456b29SIan Morris 	if (!out_dev) {
1805e87cc472SJoe Perches 		net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
18061da177e4SLinus Torvalds 		return -EINVAL;
18071da177e4SLinus Torvalds 	}
18081da177e4SLinus Torvalds 
18095c04c819SMichael Smith 	err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
18109e56e380SDavid S. Miller 				  in_dev->dev, in_dev, &itag);
18111da177e4SLinus Torvalds 	if (err < 0) {
18121da177e4SLinus Torvalds 		ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
18131da177e4SLinus Torvalds 					 saddr);
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 		goto cleanup;
18161da177e4SLinus Torvalds 	}
18171da177e4SLinus Torvalds 
1818e81da0e1SJulian Anastasov 	do_cache = res->fi && !itag;
1819e81da0e1SJulian Anastasov 	if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
1820eba618abSDavid Ahern 	    skb->protocol == htons(ETH_P_IP)) {
1821bdf00467SDavid Ahern 		__be32 gw;
1822eba618abSDavid Ahern 
1823bdf00467SDavid Ahern 		gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0;
1824eba618abSDavid Ahern 		if (IN_DEV_SHARED_MEDIA(out_dev) ||
1825eba618abSDavid Ahern 		    inet_addr_onlink(out_dev, saddr, gw))
1826df4d9254SHannes Frederic Sowa 			IPCB(skb)->flags |= IPSKB_DOREDIRECT;
1827eba618abSDavid Ahern 	}
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP)) {
18301da177e4SLinus Torvalds 		/* Not IP (i.e. ARP). Do not create route, if it is
18311da177e4SLinus Torvalds 		 * invalid for proxy arp. DNAT routes are always valid.
183265324144SJesper Dangaard Brouer 		 *
183365324144SJesper Dangaard Brouer 		 * Proxy arp feature have been extended to allow, ARP
183465324144SJesper Dangaard Brouer 		 * replies back to the same interface, to support
183565324144SJesper Dangaard Brouer 		 * Private VLAN switch technologies. See arp.c.
18361da177e4SLinus Torvalds 		 */
183765324144SJesper Dangaard Brouer 		if (out_dev == in_dev &&
183865324144SJesper Dangaard Brouer 		    IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
18391da177e4SLinus Torvalds 			err = -EINVAL;
18401da177e4SLinus Torvalds 			goto cleanup;
18411da177e4SLinus Torvalds 		}
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
1844b5c8b3feSEyal Birger 	if (IN_DEV_ORCONF(in_dev, NOPOLICY))
1845e6175a2eSEyal Birger 		IPCB(skb)->flags |= IPSKB_NOPOLICY;
1846e6175a2eSEyal Birger 
1847a5995e71SDavid Ahern 	fnhe = find_exception(nhc, daddr);
1848e81da0e1SJulian Anastasov 	if (do_cache) {
184994720e3aSJulian Anastasov 		if (fnhe)
18502ffae99dSTimo Teräs 			rth = rcu_dereference(fnhe->fnhe_rth_input);
185194720e3aSJulian Anastasov 		else
18520f457a36SDavid Ahern 			rth = rcu_dereference(nhc->nhc_rth_input);
1853d2d68ba9SDavid S. Miller 		if (rt_cache_valid(rth)) {
1854c6cffba4SDavid S. Miller 			skb_dst_set_noref(skb, &rth->dst);
1855d2d68ba9SDavid S. Miller 			goto out;
1856d2d68ba9SDavid S. Miller 		}
1857d2d68ba9SDavid S. Miller 	}
1858f2bb4bedSDavid S. Miller 
1859b5c8b3feSEyal Birger 	rth = rt_dst_alloc(out_dev->dev, 0, res->type,
186062679a8dSVincent Bernat 			   IN_DEV_ORCONF(out_dev, NOXFRM));
18611da177e4SLinus Torvalds 	if (!rth) {
18621da177e4SLinus Torvalds 		err = -ENOBUFS;
18631da177e4SLinus Torvalds 		goto cleanup;
18641da177e4SLinus Torvalds 	}
18651da177e4SLinus Torvalds 
18669917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
1867a6254864SDuan Jiong 	RT_CACHE_STAT_INC(in_slow_tot);
18681da177e4SLinus Torvalds 
1869d8d1f30bSChangli Gao 	rth->dst.input = ip_forward;
18701da177e4SLinus Torvalds 
1871a4c2fd7fSWei Wang 	rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag,
1872a4c2fd7fSWei Wang 		       do_cache);
18739942895bSDavid Ahern 	lwtunnel_set_redirect(&rth->dst);
1874c6cffba4SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
1875d2d68ba9SDavid S. Miller out:
18761da177e4SLinus Torvalds 	err = 0;
18771da177e4SLinus Torvalds  cleanup:
18781da177e4SLinus Torvalds 	return err;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
188179a13159SPeter Nørlund #ifdef CONFIG_IP_ROUTE_MULTIPATH
188279a13159SPeter Nørlund /* To make ICMP packets follow the right flow, the multipath hash is
1883bf4e0a3dSNikolay Aleksandrov  * calculated from the inner IP addresses.
188479a13159SPeter Nørlund  */
1885bf4e0a3dSNikolay Aleksandrov static void ip_multipath_l3_keys(const struct sk_buff *skb,
1886bf4e0a3dSNikolay Aleksandrov 				 struct flow_keys *hash_keys)
188779a13159SPeter Nørlund {
188879a13159SPeter Nørlund 	const struct iphdr *outer_iph = ip_hdr(skb);
18896f74b6c2SDavid Ahern 	const struct iphdr *key_iph = outer_iph;
1890bf4e0a3dSNikolay Aleksandrov 	const struct iphdr *inner_iph;
189179a13159SPeter Nørlund 	const struct icmphdr *icmph;
189279a13159SPeter Nørlund 	struct iphdr _inner_iph;
1893bf4e0a3dSNikolay Aleksandrov 	struct icmphdr _icmph;
1894bf4e0a3dSNikolay Aleksandrov 
1895bf4e0a3dSNikolay Aleksandrov 	if (likely(outer_iph->protocol != IPPROTO_ICMP))
18966f74b6c2SDavid Ahern 		goto out;
189779a13159SPeter Nørlund 
189879a13159SPeter Nørlund 	if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0))
18996f74b6c2SDavid Ahern 		goto out;
190079a13159SPeter Nørlund 
190179a13159SPeter Nørlund 	icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph),
190279a13159SPeter Nørlund 				   &_icmph);
190379a13159SPeter Nørlund 	if (!icmph)
19046f74b6c2SDavid Ahern 		goto out;
190579a13159SPeter Nørlund 
190654074f1dSMatteo Croce 	if (!icmp_is_err(icmph->type))
19076f74b6c2SDavid Ahern 		goto out;
190879a13159SPeter Nørlund 
190979a13159SPeter Nørlund 	inner_iph = skb_header_pointer(skb,
191079a13159SPeter Nørlund 				       outer_iph->ihl * 4 + sizeof(_icmph),
191179a13159SPeter Nørlund 				       sizeof(_inner_iph), &_inner_iph);
191279a13159SPeter Nørlund 	if (!inner_iph)
19136f74b6c2SDavid Ahern 		goto out;
19146f74b6c2SDavid Ahern 
19156f74b6c2SDavid Ahern 	key_iph = inner_iph;
19166f74b6c2SDavid Ahern out:
19176f74b6c2SDavid Ahern 	hash_keys->addrs.v4addrs.src = key_iph->saddr;
19186f74b6c2SDavid Ahern 	hash_keys->addrs.v4addrs.dst = key_iph->daddr;
191979a13159SPeter Nørlund }
192079a13159SPeter Nørlund 
19214253b498SIdo Schimmel static u32 fib_multipath_custom_hash_outer(const struct net *net,
19224253b498SIdo Schimmel 					   const struct sk_buff *skb,
19234253b498SIdo Schimmel 					   bool *p_has_inner)
19244253b498SIdo Schimmel {
19258895a9c2SKuniyuki Iwashima 	u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
19264253b498SIdo Schimmel 	struct flow_keys keys, hash_keys;
19274253b498SIdo Schimmel 
19284253b498SIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
19294253b498SIdo Schimmel 		return 0;
19304253b498SIdo Schimmel 
19314253b498SIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
19324253b498SIdo Schimmel 	skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
19334253b498SIdo Schimmel 
19344253b498SIdo Schimmel 	hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
19354253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
19364253b498SIdo Schimmel 		hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
19374253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
19384253b498SIdo Schimmel 		hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
19394253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
19404253b498SIdo Schimmel 		hash_keys.basic.ip_proto = keys.basic.ip_proto;
19414253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
19424253b498SIdo Schimmel 		hash_keys.ports.src = keys.ports.src;
19434253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
19444253b498SIdo Schimmel 		hash_keys.ports.dst = keys.ports.dst;
19454253b498SIdo Schimmel 
19464253b498SIdo Schimmel 	*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
19474253b498SIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
19484253b498SIdo Schimmel }
19494253b498SIdo Schimmel 
19504253b498SIdo Schimmel static u32 fib_multipath_custom_hash_inner(const struct net *net,
19514253b498SIdo Schimmel 					   const struct sk_buff *skb,
19524253b498SIdo Schimmel 					   bool has_inner)
19534253b498SIdo Schimmel {
19548895a9c2SKuniyuki Iwashima 	u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
19554253b498SIdo Schimmel 	struct flow_keys keys, hash_keys;
19564253b498SIdo Schimmel 
19574253b498SIdo Schimmel 	/* We assume the packet carries an encapsulation, but if none was
19584253b498SIdo Schimmel 	 * encountered during dissection of the outer flow, then there is no
19594253b498SIdo Schimmel 	 * point in calling the flow dissector again.
19604253b498SIdo Schimmel 	 */
19614253b498SIdo Schimmel 	if (!has_inner)
19624253b498SIdo Schimmel 		return 0;
19634253b498SIdo Schimmel 
19644253b498SIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
19654253b498SIdo Schimmel 		return 0;
19664253b498SIdo Schimmel 
19674253b498SIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
19684253b498SIdo Schimmel 	skb_flow_dissect_flow_keys(skb, &keys, 0);
19694253b498SIdo Schimmel 
19704253b498SIdo Schimmel 	if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
19714253b498SIdo Schimmel 		return 0;
19724253b498SIdo Schimmel 
19734253b498SIdo Schimmel 	if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
19744253b498SIdo Schimmel 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
19754253b498SIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
19764253b498SIdo Schimmel 			hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
19774253b498SIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
19784253b498SIdo Schimmel 			hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
19794253b498SIdo Schimmel 	} else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
19804253b498SIdo Schimmel 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19814253b498SIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
19824253b498SIdo Schimmel 			hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
19834253b498SIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
19844253b498SIdo Schimmel 			hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
19854253b498SIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
19864253b498SIdo Schimmel 			hash_keys.tags.flow_label = keys.tags.flow_label;
19874253b498SIdo Schimmel 	}
19884253b498SIdo Schimmel 
19894253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
19904253b498SIdo Schimmel 		hash_keys.basic.ip_proto = keys.basic.ip_proto;
19914253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
19924253b498SIdo Schimmel 		hash_keys.ports.src = keys.ports.src;
19934253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
19944253b498SIdo Schimmel 		hash_keys.ports.dst = keys.ports.dst;
19954253b498SIdo Schimmel 
19964253b498SIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
19974253b498SIdo Schimmel }
19984253b498SIdo Schimmel 
19994253b498SIdo Schimmel static u32 fib_multipath_custom_hash_skb(const struct net *net,
20004253b498SIdo Schimmel 					 const struct sk_buff *skb)
20014253b498SIdo Schimmel {
20024253b498SIdo Schimmel 	u32 mhash, mhash_inner;
20034253b498SIdo Schimmel 	bool has_inner = true;
20044253b498SIdo Schimmel 
20054253b498SIdo Schimmel 	mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner);
20064253b498SIdo Schimmel 	mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner);
20074253b498SIdo Schimmel 
20084253b498SIdo Schimmel 	return jhash_2words(mhash, mhash_inner, 0);
20094253b498SIdo Schimmel }
20104253b498SIdo Schimmel 
20114253b498SIdo Schimmel static u32 fib_multipath_custom_hash_fl4(const struct net *net,
20124253b498SIdo Schimmel 					 const struct flowi4 *fl4)
20134253b498SIdo Schimmel {
20148895a9c2SKuniyuki Iwashima 	u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
20154253b498SIdo Schimmel 	struct flow_keys hash_keys;
20164253b498SIdo Schimmel 
20174253b498SIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
20184253b498SIdo Schimmel 		return 0;
20194253b498SIdo Schimmel 
20204253b498SIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
20214253b498SIdo Schimmel 	hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
20224253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
20234253b498SIdo Schimmel 		hash_keys.addrs.v4addrs.src = fl4->saddr;
20244253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
20254253b498SIdo Schimmel 		hash_keys.addrs.v4addrs.dst = fl4->daddr;
20264253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
20274253b498SIdo Schimmel 		hash_keys.basic.ip_proto = fl4->flowi4_proto;
20284253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
20294253b498SIdo Schimmel 		hash_keys.ports.src = fl4->fl4_sport;
20304253b498SIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
20314253b498SIdo Schimmel 		hash_keys.ports.dst = fl4->fl4_dport;
20324253b498SIdo Schimmel 
20334253b498SIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
20344253b498SIdo Schimmel }
20354253b498SIdo Schimmel 
2036bf4e0a3dSNikolay Aleksandrov /* if skb is set it will be used and fl4 can be NULL */
20377efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
2038e37b1e97SRoopa Prabhu 		       const struct sk_buff *skb, struct flow_keys *flkeys)
2039bf4e0a3dSNikolay Aleksandrov {
20402a8e4997SIdo Schimmel 	u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
2041bf4e0a3dSNikolay Aleksandrov 	struct flow_keys hash_keys;
20422e68ea92SIdo Schimmel 	u32 mhash = 0;
2043bf4e0a3dSNikolay Aleksandrov 
20447998c12aSKuniyuki Iwashima 	switch (READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_policy)) {
2045bf4e0a3dSNikolay Aleksandrov 	case 0:
2046bf4e0a3dSNikolay Aleksandrov 		memset(&hash_keys, 0, sizeof(hash_keys));
2047bf4e0a3dSNikolay Aleksandrov 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2048bf4e0a3dSNikolay Aleksandrov 		if (skb) {
2049bf4e0a3dSNikolay Aleksandrov 			ip_multipath_l3_keys(skb, &hash_keys);
2050bf4e0a3dSNikolay Aleksandrov 		} else {
2051bf4e0a3dSNikolay Aleksandrov 			hash_keys.addrs.v4addrs.src = fl4->saddr;
2052bf4e0a3dSNikolay Aleksandrov 			hash_keys.addrs.v4addrs.dst = fl4->daddr;
2053bf4e0a3dSNikolay Aleksandrov 		}
20542e68ea92SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2055bf4e0a3dSNikolay Aleksandrov 		break;
2056bf4e0a3dSNikolay Aleksandrov 	case 1:
2057bf4e0a3dSNikolay Aleksandrov 		/* skb is currently provided only when forwarding */
2058bf4e0a3dSNikolay Aleksandrov 		if (skb) {
2059bf4e0a3dSNikolay Aleksandrov 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2060bf4e0a3dSNikolay Aleksandrov 			struct flow_keys keys;
2061bf4e0a3dSNikolay Aleksandrov 
2062bf4e0a3dSNikolay Aleksandrov 			/* short-circuit if we already have L4 hash present */
2063bf4e0a3dSNikolay Aleksandrov 			if (skb->l4_hash)
2064bf4e0a3dSNikolay Aleksandrov 				return skb_get_hash_raw(skb) >> 1;
2065ec7127a5SDavid Ahern 
2066bf4e0a3dSNikolay Aleksandrov 			memset(&hash_keys, 0, sizeof(hash_keys));
20671fe4b118SDavid Ahern 
2068ec7127a5SDavid Ahern 			if (!flkeys) {
2069ec7127a5SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2070ec7127a5SDavid Ahern 				flkeys = &keys;
2071ec7127a5SDavid Ahern 			}
2072ec7127a5SDavid Ahern 
2073e37b1e97SRoopa Prabhu 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2074e37b1e97SRoopa Prabhu 			hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2075e37b1e97SRoopa Prabhu 			hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2076e37b1e97SRoopa Prabhu 			hash_keys.ports.src = flkeys->ports.src;
2077e37b1e97SRoopa Prabhu 			hash_keys.ports.dst = flkeys->ports.dst;
2078e37b1e97SRoopa Prabhu 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2079e37b1e97SRoopa Prabhu 		} else {
2080bf4e0a3dSNikolay Aleksandrov 			memset(&hash_keys, 0, sizeof(hash_keys));
2081bf4e0a3dSNikolay Aleksandrov 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2082bf4e0a3dSNikolay Aleksandrov 			hash_keys.addrs.v4addrs.src = fl4->saddr;
2083bf4e0a3dSNikolay Aleksandrov 			hash_keys.addrs.v4addrs.dst = fl4->daddr;
2084bf4e0a3dSNikolay Aleksandrov 			hash_keys.ports.src = fl4->fl4_sport;
2085bf4e0a3dSNikolay Aleksandrov 			hash_keys.ports.dst = fl4->fl4_dport;
2086bf4e0a3dSNikolay Aleksandrov 			hash_keys.basic.ip_proto = fl4->flowi4_proto;
2087bf4e0a3dSNikolay Aleksandrov 		}
20882e68ea92SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2089bf4e0a3dSNikolay Aleksandrov 		break;
2090363887a2SStephen Suryaputra 	case 2:
2091363887a2SStephen Suryaputra 		memset(&hash_keys, 0, sizeof(hash_keys));
2092363887a2SStephen Suryaputra 		/* skb is currently provided only when forwarding */
2093363887a2SStephen Suryaputra 		if (skb) {
2094363887a2SStephen Suryaputra 			struct flow_keys keys;
2095363887a2SStephen Suryaputra 
2096363887a2SStephen Suryaputra 			skb_flow_dissect_flow_keys(skb, &keys, 0);
2097828b2b44SStephen Suryaputra 			/* Inner can be v4 or v6 */
2098828b2b44SStephen Suryaputra 			if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2099828b2b44SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2100363887a2SStephen Suryaputra 				hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2101363887a2SStephen Suryaputra 				hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2102828b2b44SStephen Suryaputra 			} else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2103828b2b44SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2104828b2b44SStephen Suryaputra 				hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2105828b2b44SStephen Suryaputra 				hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2106828b2b44SStephen Suryaputra 				hash_keys.tags.flow_label = keys.tags.flow_label;
2107828b2b44SStephen Suryaputra 				hash_keys.basic.ip_proto = keys.basic.ip_proto;
2108363887a2SStephen Suryaputra 			} else {
2109363887a2SStephen Suryaputra 				/* Same as case 0 */
2110828b2b44SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2111828b2b44SStephen Suryaputra 				ip_multipath_l3_keys(skb, &hash_keys);
2112828b2b44SStephen Suryaputra 			}
2113828b2b44SStephen Suryaputra 		} else {
2114828b2b44SStephen Suryaputra 			/* Same as case 0 */
2115828b2b44SStephen Suryaputra 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2116363887a2SStephen Suryaputra 			hash_keys.addrs.v4addrs.src = fl4->saddr;
2117363887a2SStephen Suryaputra 			hash_keys.addrs.v4addrs.dst = fl4->daddr;
2118363887a2SStephen Suryaputra 		}
21192e68ea92SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2120363887a2SStephen Suryaputra 		break;
21214253b498SIdo Schimmel 	case 3:
21224253b498SIdo Schimmel 		if (skb)
21234253b498SIdo Schimmel 			mhash = fib_multipath_custom_hash_skb(net, skb);
21244253b498SIdo Schimmel 		else
21254253b498SIdo Schimmel 			mhash = fib_multipath_custom_hash_fl4(net, fl4);
21264253b498SIdo Schimmel 		break;
2127bf4e0a3dSNikolay Aleksandrov 	}
2128bf4e0a3dSNikolay Aleksandrov 
212924ba1440Swenxu 	if (multipath_hash)
213024ba1440Swenxu 		mhash = jhash_2words(mhash, multipath_hash, 0);
213124ba1440Swenxu 
2132bf4e0a3dSNikolay Aleksandrov 	return mhash >> 1;
2133bf4e0a3dSNikolay Aleksandrov }
213479a13159SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */
213579a13159SPeter Nørlund 
21365969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb,
21371da177e4SLinus Torvalds 			    struct fib_result *res,
21381da177e4SLinus Torvalds 			    struct in_device *in_dev,
2139e37b1e97SRoopa Prabhu 			    __be32 daddr, __be32 saddr, u32 tos,
2140e37b1e97SRoopa Prabhu 			    struct flow_keys *hkeys)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
21435481d73fSDavid Ahern 	if (res->fi && fib_info_num_path(res->fi) > 1) {
21447efc0b6bSDavid Ahern 		int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys);
21450e884c78SPeter Nørlund 
21460e884c78SPeter Nørlund 		fib_select_multipath(res, h);
2147*6ac66cb0SSriram Yagnaraman 		IPCB(skb)->flags |= IPSKB_MULTIPATH;
21480e884c78SPeter Nørlund 	}
21491da177e4SLinus Torvalds #endif
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	/* create a routing cache entry */
2152c6cffba4SDavid S. Miller 	return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
215502b24941SPaolo Abeni /* Implements all the saddr-related checks as ip_route_input_slow(),
215602b24941SPaolo Abeni  * assuming daddr is valid and the destination is not a local broadcast one.
215702b24941SPaolo Abeni  * Uses the provided hint instead of performing a route lookup.
215802b24941SPaolo Abeni  */
215902b24941SPaolo Abeni int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
216002b24941SPaolo Abeni 		      u8 tos, struct net_device *dev,
216102b24941SPaolo Abeni 		      const struct sk_buff *hint)
216202b24941SPaolo Abeni {
216302b24941SPaolo Abeni 	struct in_device *in_dev = __in_dev_get_rcu(dev);
21642ce578caSMiaohe Lin 	struct rtable *rt = skb_rtable(hint);
216502b24941SPaolo Abeni 	struct net *net = dev_net(dev);
216602b24941SPaolo Abeni 	int err = -EINVAL;
216702b24941SPaolo Abeni 	u32 tag = 0;
216802b24941SPaolo Abeni 
216902b24941SPaolo Abeni 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
217002b24941SPaolo Abeni 		goto martian_source;
217102b24941SPaolo Abeni 
217202b24941SPaolo Abeni 	if (ipv4_is_zeronet(saddr))
217302b24941SPaolo Abeni 		goto martian_source;
217402b24941SPaolo Abeni 
217502b24941SPaolo Abeni 	if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
217602b24941SPaolo Abeni 		goto martian_source;
217702b24941SPaolo Abeni 
217802b24941SPaolo Abeni 	if (rt->rt_type != RTN_LOCAL)
217902b24941SPaolo Abeni 		goto skip_validate_source;
218002b24941SPaolo Abeni 
218102b24941SPaolo Abeni 	tos &= IPTOS_RT_MASK;
218202b24941SPaolo Abeni 	err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag);
218302b24941SPaolo Abeni 	if (err < 0)
218402b24941SPaolo Abeni 		goto martian_source;
218502b24941SPaolo Abeni 
218602b24941SPaolo Abeni skip_validate_source:
218702b24941SPaolo Abeni 	skb_dst_copy(skb, hint);
218802b24941SPaolo Abeni 	return 0;
218902b24941SPaolo Abeni 
219002b24941SPaolo Abeni martian_source:
219102b24941SPaolo Abeni 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
219202b24941SPaolo Abeni 	return err;
219302b24941SPaolo Abeni }
219402b24941SPaolo Abeni 
2195b87b04f5SDavid Ahern /* get device for dst_alloc with local routes */
2196b87b04f5SDavid Ahern static struct net_device *ip_rt_get_dev(struct net *net,
2197b87b04f5SDavid Ahern 					const struct fib_result *res)
2198b87b04f5SDavid Ahern {
2199b87b04f5SDavid Ahern 	struct fib_nh_common *nhc = res->fi ? res->nhc : NULL;
2200b87b04f5SDavid Ahern 	struct net_device *dev = NULL;
2201b87b04f5SDavid Ahern 
2202b87b04f5SDavid Ahern 	if (nhc)
2203b87b04f5SDavid Ahern 		dev = l3mdev_master_dev_rcu(nhc->nhc_dev);
2204b87b04f5SDavid Ahern 
2205b87b04f5SDavid Ahern 	return dev ? : net->loopback_dev;
2206b87b04f5SDavid Ahern }
2207b87b04f5SDavid Ahern 
22081da177e4SLinus Torvalds /*
22091da177e4SLinus Torvalds  *	NOTE. We drop all the packets that has local source
22101da177e4SLinus Torvalds  *	addresses, because every properly looped back packet
22111da177e4SLinus Torvalds  *	must have correct destination already attached by output routine.
221202b24941SPaolo Abeni  *	Changes in the enforced policies must be applied also to
221302b24941SPaolo Abeni  *	ip_route_use_hint().
22141da177e4SLinus Torvalds  *
22151da177e4SLinus Torvalds  *	Such approach solves two big problems:
22161da177e4SLinus Torvalds  *	1. Not simplex devices are handled properly.
22171da177e4SLinus Torvalds  *	2. IP spoofing attempts are filtered with 100% of guarantee.
2218ebc0ffaeSEric Dumazet  *	called with rcu_read_lock()
22191da177e4SLinus Torvalds  */
22201da177e4SLinus Torvalds 
22219e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
22225510cdf7SDavid Ahern 			       u8 tos, struct net_device *dev,
22235510cdf7SDavid Ahern 			       struct fib_result *res)
22241da177e4SLinus Torvalds {
222596d36220SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
2226e37b1e97SRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2227e37b1e97SRoopa Prabhu 	struct net    *net = dev_net(dev);
22281b7179d3SThomas Graf 	struct ip_tunnel_info *tun_info;
2229e37b1e97SRoopa Prabhu 	int		err = -EINVAL;
223095c96174SEric Dumazet 	unsigned int	flags = 0;
22311da177e4SLinus Torvalds 	u32		itag = 0;
22321da177e4SLinus Torvalds 	struct rtable	*rth;
2233e37b1e97SRoopa Prabhu 	struct flowi4	fl4;
22340a90478bSXin Long 	bool do_cache = true;
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	/* IP on this device is disabled. */
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	if (!in_dev)
22391da177e4SLinus Torvalds 		goto out;
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds 	/* Check for the most weird martians, which can be not detected
22426ad08600SShubhankar Kuranagatti 	 * by fib_lookup.
22431da177e4SLinus Torvalds 	 */
22441da177e4SLinus Torvalds 
224561adedf3SJiri Benc 	tun_info = skb_tunnel_info(skb);
224646fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
22471b7179d3SThomas Graf 		fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id;
22481b7179d3SThomas Graf 	else
22491b7179d3SThomas Graf 		fl4.flowi4_tun_key.tun_id = 0;
2250f38a9eb1SThomas Graf 	skb_dst_drop(skb);
2251f38a9eb1SThomas Graf 
2252d0daebc3SThomas Graf 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
22531da177e4SLinus Torvalds 		goto martian_source;
22541da177e4SLinus Torvalds 
22555510cdf7SDavid Ahern 	res->fi = NULL;
22565510cdf7SDavid Ahern 	res->table = NULL;
225727a954bdSAndy Walls 	if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
22581da177e4SLinus Torvalds 		goto brd_input;
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 	/* Accept zero addresses only to limited broadcast;
22611da177e4SLinus Torvalds 	 * I even do not know to fix it or not. Waiting for complains :-)
22621da177e4SLinus Torvalds 	 */
2263f97c1e0cSJoe Perches 	if (ipv4_is_zeronet(saddr))
22641da177e4SLinus Torvalds 		goto martian_source;
22651da177e4SLinus Torvalds 
2266d0daebc3SThomas Graf 	if (ipv4_is_zeronet(daddr))
22671da177e4SLinus Torvalds 		goto martian_destination;
22681da177e4SLinus Torvalds 
22699eb43e76SEric Dumazet 	/* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
22709eb43e76SEric Dumazet 	 * and call it once if daddr or/and saddr are loopback addresses
22719eb43e76SEric Dumazet 	 */
22729eb43e76SEric Dumazet 	if (ipv4_is_loopback(daddr)) {
22739eb43e76SEric Dumazet 		if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2274d0daebc3SThomas Graf 			goto martian_destination;
22759eb43e76SEric Dumazet 	} else if (ipv4_is_loopback(saddr)) {
22769eb43e76SEric Dumazet 		if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2277d0daebc3SThomas Graf 			goto martian_source;
2278d0daebc3SThomas Graf 	}
2279d0daebc3SThomas Graf 
22801da177e4SLinus Torvalds 	/*
22811da177e4SLinus Torvalds 	 *	Now we are ready to route packet.
22821da177e4SLinus Torvalds 	 */
228340867d74SDavid Ahern 	fl4.flowi4_l3mdev = 0;
228468a5e3ddSDavid S. Miller 	fl4.flowi4_oif = 0;
2285e0d56fddSDavid Ahern 	fl4.flowi4_iif = dev->ifindex;
228668a5e3ddSDavid S. Miller 	fl4.flowi4_mark = skb->mark;
228768a5e3ddSDavid S. Miller 	fl4.flowi4_tos = tos;
228868a5e3ddSDavid S. Miller 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
2289b84f7878SDavid Ahern 	fl4.flowi4_flags = 0;
229068a5e3ddSDavid S. Miller 	fl4.daddr = daddr;
229168a5e3ddSDavid S. Miller 	fl4.saddr = saddr;
22928bcfd092SJulian Anastasov 	fl4.flowi4_uid = sock_net_uid(net, NULL);
22931869e226SDavid Ahern 	fl4.flowi4_multipath_hash = 0;
2294e37b1e97SRoopa Prabhu 
22955a847a6eSDavid Ahern 	if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) {
2296e37b1e97SRoopa Prabhu 		flkeys = &_flkeys;
22975a847a6eSDavid Ahern 	} else {
22985a847a6eSDavid Ahern 		fl4.flowi4_proto = 0;
22995a847a6eSDavid Ahern 		fl4.fl4_sport = 0;
23005a847a6eSDavid Ahern 		fl4.fl4_dport = 0;
23015a847a6eSDavid Ahern 	}
2302e37b1e97SRoopa Prabhu 
23035510cdf7SDavid Ahern 	err = fib_lookup(net, &fl4, res, 0);
2304cd0f0b95SDuan Jiong 	if (err != 0) {
2305cd0f0b95SDuan Jiong 		if (!IN_DEV_FORWARD(in_dev))
2306cd0f0b95SDuan Jiong 			err = -EHOSTUNREACH;
23071da177e4SLinus Torvalds 		goto no_route;
2308cd0f0b95SDuan Jiong 	}
23091da177e4SLinus Torvalds 
23105cbf777cSXin Long 	if (res->type == RTN_BROADCAST) {
23115cbf777cSXin Long 		if (IN_DEV_BFORWARD(in_dev))
23125cbf777cSXin Long 			goto make_route;
23130a90478bSXin Long 		/* not do cache if bc_forwarding is enabled */
23140a90478bSXin Long 		if (IPV4_DEVCONF_ALL(net, BC_FORWARDING))
23150a90478bSXin Long 			do_cache = false;
23161da177e4SLinus Torvalds 		goto brd_input;
23175cbf777cSXin Long 	}
23181da177e4SLinus Torvalds 
23195510cdf7SDavid Ahern 	if (res->type == RTN_LOCAL) {
23205c04c819SMichael Smith 		err = fib_validate_source(skb, saddr, daddr, tos,
23210d5edc68SCong Wang 					  0, dev, in_dev, &itag);
2322b5f7e755SEric Dumazet 		if (err < 0)
23230d753960SDavid Ahern 			goto martian_source;
23241da177e4SLinus Torvalds 		goto local_input;
23251da177e4SLinus Torvalds 	}
23261da177e4SLinus Torvalds 
2327cd0f0b95SDuan Jiong 	if (!IN_DEV_FORWARD(in_dev)) {
2328cd0f0b95SDuan Jiong 		err = -EHOSTUNREACH;
2329251da413SDavid S. Miller 		goto no_route;
2330cd0f0b95SDuan Jiong 	}
23315510cdf7SDavid Ahern 	if (res->type != RTN_UNICAST)
23321da177e4SLinus Torvalds 		goto martian_destination;
23331da177e4SLinus Torvalds 
23345cbf777cSXin Long make_route:
2335e37b1e97SRoopa Prabhu 	err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, tos, flkeys);
23361da177e4SLinus Torvalds out:	return err;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds brd_input:
23391da177e4SLinus Torvalds 	if (skb->protocol != htons(ETH_P_IP))
23401da177e4SLinus Torvalds 		goto e_inval;
23411da177e4SLinus Torvalds 
234241347dcdSDavid S. Miller 	if (!ipv4_is_zeronet(saddr)) {
23439e56e380SDavid S. Miller 		err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
23449e56e380SDavid S. Miller 					  in_dev, &itag);
23451da177e4SLinus Torvalds 		if (err < 0)
23460d753960SDavid Ahern 			goto martian_source;
23471da177e4SLinus Torvalds 	}
23481da177e4SLinus Torvalds 	flags |= RTCF_BROADCAST;
23495510cdf7SDavid Ahern 	res->type = RTN_BROADCAST;
23501da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_brd);
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds local_input:
2353b5c8b3feSEyal Birger 	if (IN_DEV_ORCONF(in_dev, NOPOLICY))
2354e6175a2eSEyal Birger 		IPCB(skb)->flags |= IPSKB_NOPOLICY;
2355e6175a2eSEyal Birger 
23560a90478bSXin Long 	do_cache &= res->fi && !itag;
23570a90478bSXin Long 	if (do_cache) {
2358eba618abSDavid Ahern 		struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2359eba618abSDavid Ahern 
23600f457a36SDavid Ahern 		rth = rcu_dereference(nhc->nhc_rth_input);
2361d2d68ba9SDavid S. Miller 		if (rt_cache_valid(rth)) {
2362c6cffba4SDavid S. Miller 			skb_dst_set_noref(skb, &rth->dst);
2363c6cffba4SDavid S. Miller 			err = 0;
2364c6cffba4SDavid S. Miller 			goto out;
2365d2d68ba9SDavid S. Miller 		}
2366d2d68ba9SDavid S. Miller 	}
2367d2d68ba9SDavid S. Miller 
2368b87b04f5SDavid Ahern 	rth = rt_dst_alloc(ip_rt_get_dev(net, res),
2369b5c8b3feSEyal Birger 			   flags | RTCF_LOCAL, res->type, false);
23701da177e4SLinus Torvalds 	if (!rth)
23711da177e4SLinus Torvalds 		goto e_nobufs;
23721da177e4SLinus Torvalds 
2373d8d1f30bSChangli Gao 	rth->dst.output= ip_rt_bug;
2374cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
2375cf911662SDavid S. Miller 	rth->dst.tclassid = itag;
2376cf911662SDavid S. Miller #endif
23779917e1e8SDavid S. Miller 	rth->rt_is_input = 1;
2378571e7226SRoopa Prabhu 
2379a6254864SDuan Jiong 	RT_CACHE_STAT_INC(in_slow_tot);
23805510cdf7SDavid Ahern 	if (res->type == RTN_UNREACHABLE) {
2381d8d1f30bSChangli Gao 		rth->dst.input= ip_error;
2382d8d1f30bSChangli Gao 		rth->dst.error= -err;
23831da177e4SLinus Torvalds 		rth->rt_flags	&= ~RTCF_LOCAL;
23841da177e4SLinus Torvalds 	}
2385efd85700SThomas Graf 
2386dcdfdf56SAlexei Starovoitov 	if (do_cache) {
2387eba618abSDavid Ahern 		struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2388efd85700SThomas Graf 
2389eba618abSDavid Ahern 		rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
2390efd85700SThomas Graf 		if (lwtunnel_input_redirect(rth->dst.lwtstate)) {
2391efd85700SThomas Graf 			WARN_ON(rth->dst.input == lwtunnel_input);
2392efd85700SThomas Graf 			rth->dst.lwtstate->orig_input = rth->dst.input;
2393efd85700SThomas Graf 			rth->dst.input = lwtunnel_input;
2394efd85700SThomas Graf 		}
2395efd85700SThomas Graf 
239687063a1fSDavid Ahern 		if (unlikely(!rt_cache_route(nhc, rth)))
2397dcdfdf56SAlexei Starovoitov 			rt_add_uncached_list(rth);
2398dcdfdf56SAlexei Starovoitov 	}
239989aef892SDavid S. Miller 	skb_dst_set(skb, &rth->dst);
2400b23dd4feSDavid S. Miller 	err = 0;
2401ebc0ffaeSEric Dumazet 	goto out;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds no_route:
24041da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_no_route);
24055510cdf7SDavid Ahern 	res->type = RTN_UNREACHABLE;
24065510cdf7SDavid Ahern 	res->fi = NULL;
24075510cdf7SDavid Ahern 	res->table = NULL;
24081da177e4SLinus Torvalds 	goto local_input;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	/*
24111da177e4SLinus Torvalds 	 *	Do not cache martian addresses: they should be logged (RFC1812)
24121da177e4SLinus Torvalds 	 */
24131da177e4SLinus Torvalds martian_destination:
24141da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(in_martian_dst);
24151da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
2416e87cc472SJoe Perches 	if (IN_DEV_LOG_MARTIANS(in_dev))
2417e87cc472SJoe Perches 		net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
2418673d57e7SHarvey Harrison 				     &daddr, &saddr, dev->name);
24191da177e4SLinus Torvalds #endif
24202c2910a4SDietmar Eggemann 
24211da177e4SLinus Torvalds e_inval:
24221da177e4SLinus Torvalds 	err = -EINVAL;
2423ebc0ffaeSEric Dumazet 	goto out;
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds e_nobufs:
24261da177e4SLinus Torvalds 	err = -ENOBUFS;
2427ebc0ffaeSEric Dumazet 	goto out;
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds martian_source:
24301da177e4SLinus Torvalds 	ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
2431ebc0ffaeSEric Dumazet 	goto out;
24321da177e4SLinus Torvalds }
24331da177e4SLinus Torvalds 
24345510cdf7SDavid Ahern /* called with rcu_read_lock held */
24355022e221SZhengchao Shao static int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
24365510cdf7SDavid Ahern 			      u8 tos, struct net_device *dev, struct fib_result *res)
24375510cdf7SDavid Ahern {
24381da177e4SLinus Torvalds 	/* Multicast recognition logic is moved from route cache to here.
24396ad08600SShubhankar Kuranagatti 	 * The problem was that too many Ethernet cards have broken/missing
24406ad08600SShubhankar Kuranagatti 	 * hardware multicast filters :-( As result the host on multicasting
24416ad08600SShubhankar Kuranagatti 	 * network acquires a lot of useless route cache entries, sort of
24426ad08600SShubhankar Kuranagatti 	 * SDR messages from all the world. Now we try to get rid of them.
24436ad08600SShubhankar Kuranagatti 	 * Really, provided software IP multicast filter is organized
24446ad08600SShubhankar Kuranagatti 	 * reasonably (at least, hashed), it does not result in a slowdown
24456ad08600SShubhankar Kuranagatti 	 * comparing with route cache reject entries.
24466ad08600SShubhankar Kuranagatti 	 * Note, that multicast routers are not affected, because
24476ad08600SShubhankar Kuranagatti 	 * route cache entry is created eventually.
24481da177e4SLinus Torvalds 	 */
2449f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
245096d36220SEric Dumazet 		struct in_device *in_dev = __in_dev_get_rcu(dev);
2451e58e4159SDavid Ahern 		int our = 0;
24525510cdf7SDavid Ahern 		int err = -EINVAL;
24531da177e4SLinus Torvalds 
245422c74764SPaolo Abeni 		if (!in_dev)
245522c74764SPaolo Abeni 			return err;
2456e58e4159SDavid Ahern 		our = ip_check_mc_rcu(in_dev, daddr, saddr,
2457eddc9ec5SArnaldo Carvalho de Melo 				      ip_hdr(skb)->protocol);
2458e58e4159SDavid Ahern 
2459e58e4159SDavid Ahern 		/* check l3 master if no match yet */
246022c74764SPaolo Abeni 		if (!our && netif_is_l3_slave(dev)) {
2461e58e4159SDavid Ahern 			struct in_device *l3_in_dev;
2462e58e4159SDavid Ahern 
2463e58e4159SDavid Ahern 			l3_in_dev = __in_dev_get_rcu(skb->dev);
2464e58e4159SDavid Ahern 			if (l3_in_dev)
2465e58e4159SDavid Ahern 				our = ip_check_mc_rcu(l3_in_dev, daddr, saddr,
2466e58e4159SDavid Ahern 						      ip_hdr(skb)->protocol);
2467e58e4159SDavid Ahern 		}
2468e58e4159SDavid Ahern 
24691da177e4SLinus Torvalds 		if (our
24701da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
24719d4fb27dSJoe Perches 			||
24729d4fb27dSJoe Perches 		    (!ipv4_is_local_multicast(daddr) &&
2473f97c1e0cSJoe Perches 		     IN_DEV_MFORWARD(in_dev))
24741da177e4SLinus Torvalds #endif
24751da177e4SLinus Torvalds 		   ) {
24765510cdf7SDavid Ahern 			err = ip_route_input_mc(skb, daddr, saddr,
24771da177e4SLinus Torvalds 						tos, dev, our);
2478e58e4159SDavid Ahern 		}
24795510cdf7SDavid Ahern 		return err;
24801da177e4SLinus Torvalds 	}
24815510cdf7SDavid Ahern 
24825510cdf7SDavid Ahern 	return ip_route_input_slow(skb, daddr, saddr, tos, dev, res);
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds 
24855022e221SZhengchao Shao int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
24865022e221SZhengchao Shao 			 u8 tos, struct net_device *dev)
24875022e221SZhengchao Shao {
24885022e221SZhengchao Shao 	struct fib_result res;
24895022e221SZhengchao Shao 	int err;
24905022e221SZhengchao Shao 
24915022e221SZhengchao Shao 	tos &= IPTOS_RT_MASK;
24925022e221SZhengchao Shao 	rcu_read_lock();
24935022e221SZhengchao Shao 	err = ip_route_input_rcu(skb, daddr, saddr, tos, dev, &res);
24945022e221SZhengchao Shao 	rcu_read_unlock();
24955022e221SZhengchao Shao 
24965022e221SZhengchao Shao 	return err;
24975022e221SZhengchao Shao }
24985022e221SZhengchao Shao EXPORT_SYMBOL(ip_route_input_noref);
24995022e221SZhengchao Shao 
2500ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */
2501982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res,
25021a00fee4SDavid Miller 				       const struct flowi4 *fl4, int orig_oif,
2503f61759e6SJulian Anastasov 				       struct net_device *dev_out,
25045ada5527SDavid S. Miller 				       unsigned int flags)
25051da177e4SLinus Torvalds {
2506982721f3SDavid S. Miller 	struct fib_info *fi = res->fi;
2507f2bb4bedSDavid S. Miller 	struct fib_nh_exception *fnhe;
25085ada5527SDavid S. Miller 	struct in_device *in_dev;
2509982721f3SDavid S. Miller 	u16 type = res->type;
25105ada5527SDavid S. Miller 	struct rtable *rth;
2511c92b9655SJulian Anastasov 	bool do_cache;
25121da177e4SLinus Torvalds 
2513d0daebc3SThomas Graf 	in_dev = __in_dev_get_rcu(dev_out);
2514d0daebc3SThomas Graf 	if (!in_dev)
2515d0daebc3SThomas Graf 		return ERR_PTR(-EINVAL);
2516d0daebc3SThomas Graf 
2517d0daebc3SThomas Graf 	if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
25185f02ce24SDavid Ahern 		if (ipv4_is_loopback(fl4->saddr) &&
25195f02ce24SDavid Ahern 		    !(dev_out->flags & IFF_LOOPBACK) &&
25205f02ce24SDavid Ahern 		    !netif_is_l3_master(dev_out))
25215ada5527SDavid S. Miller 			return ERR_PTR(-EINVAL);
25221da177e4SLinus Torvalds 
252368a5e3ddSDavid S. Miller 	if (ipv4_is_lbcast(fl4->daddr))
2524982721f3SDavid S. Miller 		type = RTN_BROADCAST;
252568a5e3ddSDavid S. Miller 	else if (ipv4_is_multicast(fl4->daddr))
2526982721f3SDavid S. Miller 		type = RTN_MULTICAST;
252768a5e3ddSDavid S. Miller 	else if (ipv4_is_zeronet(fl4->daddr))
25285ada5527SDavid S. Miller 		return ERR_PTR(-EINVAL);
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	if (dev_out->flags & IFF_LOOPBACK)
25311da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
25321da177e4SLinus Torvalds 
253363617421SJulian Anastasov 	do_cache = true;
2534982721f3SDavid S. Miller 	if (type == RTN_BROADCAST) {
25351da177e4SLinus Torvalds 		flags |= RTCF_BROADCAST | RTCF_LOCAL;
2536982721f3SDavid S. Miller 		fi = NULL;
2537982721f3SDavid S. Miller 	} else if (type == RTN_MULTICAST) {
25381da177e4SLinus Torvalds 		flags |= RTCF_MULTICAST | RTCF_LOCAL;
2539813b3b5dSDavid S. Miller 		if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
2540813b3b5dSDavid S. Miller 				     fl4->flowi4_proto))
25411da177e4SLinus Torvalds 			flags &= ~RTCF_LOCAL;
254263617421SJulian Anastasov 		else
254363617421SJulian Anastasov 			do_cache = false;
25441da177e4SLinus Torvalds 		/* If multicast route do not exist use
2545dd28d1a0SEric Dumazet 		 * default one, but do not gateway in this case.
2546dd28d1a0SEric Dumazet 		 * Yes, it is hack.
25471da177e4SLinus Torvalds 		 */
2548982721f3SDavid S. Miller 		if (fi && res->prefixlen < 4)
2549982721f3SDavid S. Miller 			fi = NULL;
2550d6d5e999SChris Friesen 	} else if ((type == RTN_LOCAL) && (orig_oif != 0) &&
2551d6d5e999SChris Friesen 		   (orig_oif != dev_out->ifindex)) {
2552d6d5e999SChris Friesen 		/* For local routes that require a particular output interface
2553d6d5e999SChris Friesen 		 * we do not want to cache the result.  Caching the result
2554d6d5e999SChris Friesen 		 * causes incorrect behaviour when there are multiple source
2555d6d5e999SChris Friesen 		 * addresses on the interface, the end result being that if the
2556d6d5e999SChris Friesen 		 * intended recipient is waiting on that interface for the
2557d6d5e999SChris Friesen 		 * packet he won't receive it because it will be delivered on
2558d6d5e999SChris Friesen 		 * the loopback interface and the IP_PKTINFO ipi_ifindex will
2559d6d5e999SChris Friesen 		 * be set to the loopback interface as well.
2560d6d5e999SChris Friesen 		 */
256194720e3aSJulian Anastasov 		do_cache = false;
25621da177e4SLinus Torvalds 	}
25631da177e4SLinus Torvalds 
2564f2bb4bedSDavid S. Miller 	fnhe = NULL;
256563617421SJulian Anastasov 	do_cache &= fi != NULL;
256694720e3aSJulian Anastasov 	if (fi) {
2567eba618abSDavid Ahern 		struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2568d26b3a7cSEric Dumazet 		struct rtable __rcu **prth;
2569d26b3a7cSEric Dumazet 
2570a5995e71SDavid Ahern 		fnhe = find_exception(nhc, fl4->daddr);
257194720e3aSJulian Anastasov 		if (!do_cache)
257294720e3aSJulian Anastasov 			goto add;
2573deed49dfSXin Long 		if (fnhe) {
25742ffae99dSTimo Teräs 			prth = &fnhe->fnhe_rth_output;
2575deed49dfSXin Long 		} else {
2576c92b9655SJulian Anastasov 			if (unlikely(fl4->flowi4_flags &
2577c92b9655SJulian Anastasov 				     FLOWI_FLAG_KNOWN_NH &&
2578bdf00467SDavid Ahern 				     !(nhc->nhc_gw_family &&
2579eba618abSDavid Ahern 				       nhc->nhc_scope == RT_SCOPE_LINK))) {
2580c92b9655SJulian Anastasov 				do_cache = false;
2581c92b9655SJulian Anastasov 				goto add;
2582c92b9655SJulian Anastasov 			}
25830f457a36SDavid Ahern 			prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
258494720e3aSJulian Anastasov 		}
2585d26b3a7cSEric Dumazet 		rth = rcu_dereference(*prth);
25869df16efaSWei Wang 		if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst))
2587f2bb4bedSDavid S. Miller 			return rth;
2588f2bb4bedSDavid S. Miller 	}
2589c92b9655SJulian Anastasov 
2590c92b9655SJulian Anastasov add:
2591d08c4f35SDavid Ahern 	rth = rt_dst_alloc(dev_out, flags, type,
259262679a8dSVincent Bernat 			   IN_DEV_ORCONF(in_dev, NOXFRM));
25938391d07bSDimitris Michailidis 	if (!rth)
25945ada5527SDavid S. Miller 		return ERR_PTR(-ENOBUFS);
25958391d07bSDimitris Michailidis 
25969438c871SDavid Ahern 	rth->rt_iif = orig_oif;
2597b7503e0cSDavid Ahern 
25981da177e4SLinus Torvalds 	RT_CACHE_STAT_INC(out_slow_tot);
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 	if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
26011da177e4SLinus Torvalds 		if (flags & RTCF_LOCAL &&
26021da177e4SLinus Torvalds 		    !(dev_out->flags & IFF_LOOPBACK)) {
2603d8d1f30bSChangli Gao 			rth->dst.output = ip_mc_output;
26041da177e4SLinus Torvalds 			RT_CACHE_STAT_INC(out_slow_mc);
26051da177e4SLinus Torvalds 		}
26061da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2607982721f3SDavid S. Miller 		if (type == RTN_MULTICAST) {
26081da177e4SLinus Torvalds 			if (IN_DEV_MFORWARD(in_dev) &&
2609813b3b5dSDavid S. Miller 			    !ipv4_is_local_multicast(fl4->daddr)) {
2610d8d1f30bSChangli Gao 				rth->dst.input = ip_mr_input;
2611d8d1f30bSChangli Gao 				rth->dst.output = ip_mc_output;
26121da177e4SLinus Torvalds 			}
26131da177e4SLinus Torvalds 		}
26141da177e4SLinus Torvalds #endif
26151da177e4SLinus Torvalds 	}
26161da177e4SLinus Torvalds 
2617a4c2fd7fSWei Wang 	rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache);
26189942895bSDavid Ahern 	lwtunnel_set_redirect(&rth->dst);
26191da177e4SLinus Torvalds 
26205ada5527SDavid S. Miller 	return rth;
26211da177e4SLinus Torvalds }
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds /*
26241da177e4SLinus Torvalds  * Major route resolver routine.
26251da177e4SLinus Torvalds  */
26261da177e4SLinus Torvalds 
26273abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
2628bf4e0a3dSNikolay Aleksandrov 					const struct sk_buff *skb)
26291da177e4SLinus Torvalds {
2630d0ea2b12SEric Dumazet 	struct fib_result res = {
2631d0ea2b12SEric Dumazet 		.type		= RTN_UNSPEC,
2632d0ea2b12SEric Dumazet 		.fi		= NULL,
2633d0ea2b12SEric Dumazet 		.table		= NULL,
2634d0ea2b12SEric Dumazet 		.tclassid	= 0,
2635d0ea2b12SEric Dumazet 	};
26365ada5527SDavid S. Miller 	struct rtable *rth;
26371da177e4SLinus Torvalds 
26381fb9489bSPavel Emelyanov 	fl4->flowi4_iif = LOOPBACK_IFINDEX;
2639544b4dd5SGuillaume Nault 	ip_rt_fix_tos(fl4);
264044713b67SDavid S. Miller 
2641010c2708SDavid S. Miller 	rcu_read_lock();
26423abd1adeSDavid Ahern 	rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb);
26433abd1adeSDavid Ahern 	rcu_read_unlock();
26443abd1adeSDavid Ahern 
26453abd1adeSDavid Ahern 	return rth;
26463abd1adeSDavid Ahern }
26473abd1adeSDavid Ahern EXPORT_SYMBOL_GPL(ip_route_output_key_hash);
26483abd1adeSDavid Ahern 
26493abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4,
26503abd1adeSDavid Ahern 					    struct fib_result *res,
26513abd1adeSDavid Ahern 					    const struct sk_buff *skb)
26523abd1adeSDavid Ahern {
26533abd1adeSDavid Ahern 	struct net_device *dev_out = NULL;
26543abd1adeSDavid Ahern 	int orig_oif = fl4->flowi4_oif;
26553abd1adeSDavid Ahern 	unsigned int flags = 0;
26563abd1adeSDavid Ahern 	struct rtable *rth;
2657595e0651SStefano Brivio 	int err;
26583abd1adeSDavid Ahern 
2659813b3b5dSDavid S. Miller 	if (fl4->saddr) {
2660813b3b5dSDavid S. Miller 		if (ipv4_is_multicast(fl4->saddr) ||
2661813b3b5dSDavid S. Miller 		    ipv4_is_lbcast(fl4->saddr) ||
2662595e0651SStefano Brivio 		    ipv4_is_zeronet(fl4->saddr)) {
2663595e0651SStefano Brivio 			rth = ERR_PTR(-EINVAL);
26641da177e4SLinus Torvalds 			goto out;
2665595e0651SStefano Brivio 		}
2666595e0651SStefano Brivio 
2667595e0651SStefano Brivio 		rth = ERR_PTR(-ENETUNREACH);
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds 		/* I removed check for oif == dev_out->oif here.
26706ad08600SShubhankar Kuranagatti 		 * It was wrong for two reasons:
26716ad08600SShubhankar Kuranagatti 		 * 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
26726ad08600SShubhankar Kuranagatti 		 *    is assigned to multiple interfaces.
26736ad08600SShubhankar Kuranagatti 		 * 2. Moreover, we are allowed to send packets with saddr
26746ad08600SShubhankar Kuranagatti 		 *    of another iface. --ANK
26751da177e4SLinus Torvalds 		 */
26761da177e4SLinus Torvalds 
2677813b3b5dSDavid S. Miller 		if (fl4->flowi4_oif == 0 &&
2678813b3b5dSDavid S. Miller 		    (ipv4_is_multicast(fl4->daddr) ||
2679813b3b5dSDavid S. Miller 		     ipv4_is_lbcast(fl4->daddr))) {
2680a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2681813b3b5dSDavid S. Miller 			dev_out = __ip_dev_find(net, fl4->saddr, false);
268251456b29SIan Morris 			if (!dev_out)
2683a210d01aSJulian Anastasov 				goto out;
2684a210d01aSJulian Anastasov 
26851da177e4SLinus Torvalds 			/* Special hack: user can direct multicasts
26866ad08600SShubhankar Kuranagatti 			 * and limited broadcast via necessary interface
26876ad08600SShubhankar Kuranagatti 			 * without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
26886ad08600SShubhankar Kuranagatti 			 * This hack is not just for fun, it allows
26896ad08600SShubhankar Kuranagatti 			 * vic,vat and friends to work.
26906ad08600SShubhankar Kuranagatti 			 * They bind socket to loopback, set ttl to zero
26916ad08600SShubhankar Kuranagatti 			 * and expect that it will work.
26926ad08600SShubhankar Kuranagatti 			 * From the viewpoint of routing cache they are broken,
26936ad08600SShubhankar Kuranagatti 			 * because we are not allowed to build multicast path
26946ad08600SShubhankar Kuranagatti 			 * with loopback source addr (look, routing cache
26956ad08600SShubhankar Kuranagatti 			 * cannot know, that ttl is zero, so that packet
26966ad08600SShubhankar Kuranagatti 			 * will not leave this host and route is valid).
26976ad08600SShubhankar Kuranagatti 			 * Luckily, this hack is good workaround.
26981da177e4SLinus Torvalds 			 */
26991da177e4SLinus Torvalds 
2700813b3b5dSDavid S. Miller 			fl4->flowi4_oif = dev_out->ifindex;
27011da177e4SLinus Torvalds 			goto make_route;
27021da177e4SLinus Torvalds 		}
2703a210d01aSJulian Anastasov 
2704813b3b5dSDavid S. Miller 		if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
2705a210d01aSJulian Anastasov 			/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2706813b3b5dSDavid S. Miller 			if (!__ip_dev_find(net, fl4->saddr, false))
2707a210d01aSJulian Anastasov 				goto out;
27081da177e4SLinus Torvalds 		}
2709a210d01aSJulian Anastasov 	}
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 
2712813b3b5dSDavid S. Miller 	if (fl4->flowi4_oif) {
2713813b3b5dSDavid S. Miller 		dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
2714b23dd4feSDavid S. Miller 		rth = ERR_PTR(-ENODEV);
271551456b29SIan Morris 		if (!dev_out)
27161da177e4SLinus Torvalds 			goto out;
2717e5ed6399SHerbert Xu 
2718e5ed6399SHerbert Xu 		/* RACE: Check return value of inet_select_addr instead. */
2719fc75fc83SEric Dumazet 		if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
2720b23dd4feSDavid S. Miller 			rth = ERR_PTR(-ENETUNREACH);
2721fc75fc83SEric Dumazet 			goto out;
2722fc75fc83SEric Dumazet 		}
2723813b3b5dSDavid S. Miller 		if (ipv4_is_local_multicast(fl4->daddr) ||
27246a211654SAndrew Lunn 		    ipv4_is_lbcast(fl4->daddr) ||
27256a211654SAndrew Lunn 		    fl4->flowi4_proto == IPPROTO_IGMP) {
2726813b3b5dSDavid S. Miller 			if (!fl4->saddr)
2727813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
27281da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
27291da177e4SLinus Torvalds 			goto make_route;
27301da177e4SLinus Torvalds 		}
27310a7e2260SJiri Benc 		if (!fl4->saddr) {
2732813b3b5dSDavid S. Miller 			if (ipv4_is_multicast(fl4->daddr))
2733813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
2734813b3b5dSDavid S. Miller 							      fl4->flowi4_scope);
2735813b3b5dSDavid S. Miller 			else if (!fl4->daddr)
2736813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
27371da177e4SLinus Torvalds 							      RT_SCOPE_HOST);
27381da177e4SLinus Torvalds 		}
2739613d09b3SDavid Ahern 	}
27401da177e4SLinus Torvalds 
2741813b3b5dSDavid S. Miller 	if (!fl4->daddr) {
2742813b3b5dSDavid S. Miller 		fl4->daddr = fl4->saddr;
2743813b3b5dSDavid S. Miller 		if (!fl4->daddr)
2744813b3b5dSDavid S. Miller 			fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
2745b40afd0eSDenis V. Lunev 		dev_out = net->loopback_dev;
27461fb9489bSPavel Emelyanov 		fl4->flowi4_oif = LOOPBACK_IFINDEX;
27473abd1adeSDavid Ahern 		res->type = RTN_LOCAL;
27481da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
27491da177e4SLinus Torvalds 		goto make_route;
27501da177e4SLinus Torvalds 	}
27511da177e4SLinus Torvalds 
27523abd1adeSDavid Ahern 	err = fib_lookup(net, fl4, res, 0);
27530315e382SNikola Forró 	if (err) {
27543abd1adeSDavid Ahern 		res->fi = NULL;
27553abd1adeSDavid Ahern 		res->table = NULL;
27566104e112SDavid Ahern 		if (fl4->flowi4_oif &&
275740867d74SDavid Ahern 		    (ipv4_is_multicast(fl4->daddr) || !fl4->flowi4_l3mdev)) {
27581da177e4SLinus Torvalds 			/* Apparently, routing tables are wrong. Assume,
27596ad08600SShubhankar Kuranagatti 			 * that the destination is on link.
27606ad08600SShubhankar Kuranagatti 			 *
27616ad08600SShubhankar Kuranagatti 			 * WHY? DW.
27626ad08600SShubhankar Kuranagatti 			 * Because we are allowed to send to iface
27636ad08600SShubhankar Kuranagatti 			 * even if it has NO routes and NO assigned
27646ad08600SShubhankar Kuranagatti 			 * addresses. When oif is specified, routing
27656ad08600SShubhankar Kuranagatti 			 * tables are looked up with only one purpose:
27666ad08600SShubhankar Kuranagatti 			 * to catch if destination is gatewayed, rather than
27676ad08600SShubhankar Kuranagatti 			 * direct. Moreover, if MSG_DONTROUTE is set,
27686ad08600SShubhankar Kuranagatti 			 * we send packet, ignoring both routing tables
27696ad08600SShubhankar Kuranagatti 			 * and ifaddr state. --ANK
27706ad08600SShubhankar Kuranagatti 			 *
27716ad08600SShubhankar Kuranagatti 			 *
27726ad08600SShubhankar Kuranagatti 			 * We could make it even if oif is unknown,
27736ad08600SShubhankar Kuranagatti 			 * likely IPv6, but we do not.
27741da177e4SLinus Torvalds 			 */
27751da177e4SLinus Torvalds 
2776813b3b5dSDavid S. Miller 			if (fl4->saddr == 0)
2777813b3b5dSDavid S. Miller 				fl4->saddr = inet_select_addr(dev_out, 0,
27781da177e4SLinus Torvalds 							      RT_SCOPE_LINK);
27793abd1adeSDavid Ahern 			res->type = RTN_UNICAST;
27801da177e4SLinus Torvalds 			goto make_route;
27811da177e4SLinus Torvalds 		}
27820315e382SNikola Forró 		rth = ERR_PTR(err);
27831da177e4SLinus Torvalds 		goto out;
27841da177e4SLinus Torvalds 	}
27851da177e4SLinus Torvalds 
27863abd1adeSDavid Ahern 	if (res->type == RTN_LOCAL) {
2787813b3b5dSDavid S. Miller 		if (!fl4->saddr) {
27883abd1adeSDavid Ahern 			if (res->fi->fib_prefsrc)
27893abd1adeSDavid Ahern 				fl4->saddr = res->fi->fib_prefsrc;
27909fc3bbb4SJoel Sing 			else
2791813b3b5dSDavid S. Miller 				fl4->saddr = fl4->daddr;
27929fc3bbb4SJoel Sing 		}
27935f02ce24SDavid Ahern 
27945f02ce24SDavid Ahern 		/* L3 master device is the loopback for that domain */
27953abd1adeSDavid Ahern 		dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? :
2796b7c8487cSRobert Shearman 			net->loopback_dev;
2797839da4d9SDavid Ahern 
2798839da4d9SDavid Ahern 		/* make sure orig_oif points to fib result device even
2799839da4d9SDavid Ahern 		 * though packet rx/tx happens over loopback or l3mdev
2800839da4d9SDavid Ahern 		 */
2801839da4d9SDavid Ahern 		orig_oif = FIB_RES_OIF(*res);
2802839da4d9SDavid Ahern 
2803813b3b5dSDavid S. Miller 		fl4->flowi4_oif = dev_out->ifindex;
28041da177e4SLinus Torvalds 		flags |= RTCF_LOCAL;
28051da177e4SLinus Torvalds 		goto make_route;
28061da177e4SLinus Torvalds 	}
28071da177e4SLinus Torvalds 
28083abd1adeSDavid Ahern 	fib_select_path(net, res, fl4, skb);
28091da177e4SLinus Torvalds 
28103abd1adeSDavid Ahern 	dev_out = FIB_RES_DEV(*res);
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds make_route:
28133abd1adeSDavid Ahern 	rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags);
28141da177e4SLinus Torvalds 
2815010c2708SDavid S. Miller out:
2816b23dd4feSDavid S. Miller 	return rth;
28171da177e4SLinus Torvalds }
2818d8c97a94SArnaldo Carvalho de Melo 
281914e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
282014e50e57SDavid S. Miller 	.family			= AF_INET,
2821214f45c9SEric Dumazet 	.default_advmss		= ipv4_default_advmss,
2822d3aaeb38SDavid S. Miller 	.neigh_lookup		= ipv4_neigh_lookup,
2823c4c877b2SDaniel Borkmann 	.check			= dst_blackhole_check,
2824c4c877b2SDaniel Borkmann 	.cow_metrics		= dst_blackhole_cow_metrics,
2825c4c877b2SDaniel Borkmann 	.update_pmtu		= dst_blackhole_update_pmtu,
2826c4c877b2SDaniel Borkmann 	.redirect		= dst_blackhole_redirect,
2827c4c877b2SDaniel Borkmann 	.mtu			= dst_blackhole_mtu,
282814e50e57SDavid S. Miller };
282914e50e57SDavid S. Miller 
28302774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
283114e50e57SDavid S. Miller {
28322774c131SDavid S. Miller 	struct rtable *ort = (struct rtable *) dst_orig;
2833f5b0a874SDavid S. Miller 	struct rtable *rt;
283414e50e57SDavid S. Miller 
28356c0e7284SSteffen Klassert 	rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_DEAD, 0);
283614e50e57SDavid S. Miller 	if (rt) {
2837d8d1f30bSChangli Gao 		struct dst_entry *new = &rt->dst;
283814e50e57SDavid S. Miller 
283914e50e57SDavid S. Miller 		new->__use = 1;
2840352e512cSHerbert Xu 		new->input = dst_discard;
2841ede2059dSEric W. Biederman 		new->output = dst_discard_out;
284214e50e57SDavid S. Miller 
28431dbe3252SWei Wang 		new->dev = net->loopback_dev;
2844d62607c3SJakub Kicinski 		netdev_hold(new->dev, &new->dev_tracker, GFP_ATOMIC);
284514e50e57SDavid S. Miller 
28469917e1e8SDavid S. Miller 		rt->rt_is_input = ort->rt_is_input;
28475e2b61f7SDavid S. Miller 		rt->rt_iif = ort->rt_iif;
28485943634fSDavid S. Miller 		rt->rt_pmtu = ort->rt_pmtu;
2849d52e5a7eSSabrina Dubroca 		rt->rt_mtu_locked = ort->rt_mtu_locked;
285014e50e57SDavid S. Miller 
2851ca4c3fc2Sfan.du 		rt->rt_genid = rt_genid_ipv4(net);
285214e50e57SDavid S. Miller 		rt->rt_flags = ort->rt_flags;
285314e50e57SDavid S. Miller 		rt->rt_type = ort->rt_type;
285477d5bc7eSDavid Ahern 		rt->rt_uses_gateway = ort->rt_uses_gateway;
28551550c171SDavid Ahern 		rt->rt_gw_family = ort->rt_gw_family;
28561550c171SDavid Ahern 		if (rt->rt_gw_family == AF_INET)
28571550c171SDavid Ahern 			rt->rt_gw4 = ort->rt_gw4;
28580f5f7d7bSDavid Ahern 		else if (rt->rt_gw_family == AF_INET6)
28590f5f7d7bSDavid Ahern 			rt->rt_gw6 = ort->rt_gw6;
286014e50e57SDavid S. Miller 	}
286114e50e57SDavid S. Miller 
28622774c131SDavid S. Miller 	dst_release(dst_orig);
28632774c131SDavid S. Miller 
28642774c131SDavid S. Miller 	return rt ? &rt->dst : ERR_PTR(-ENOMEM);
286514e50e57SDavid S. Miller }
286614e50e57SDavid S. Miller 
28679d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
28686f9c9615SEric Dumazet 				    const struct sock *sk)
28691da177e4SLinus Torvalds {
28709d6ec938SDavid S. Miller 	struct rtable *rt = __ip_route_output_key(net, flp4);
28711da177e4SLinus Torvalds 
2872b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
2873b23dd4feSDavid S. Miller 		return rt;
28741da177e4SLinus Torvalds 
2875874fb9e2SDavid Ahern 	if (flp4->flowi4_proto) {
2876874fb9e2SDavid Ahern 		flp4->flowi4_oif = rt->dst.dev->ifindex;
2877f92ee619SSteffen Klassert 		rt = (struct rtable *)xfrm_lookup_route(net, &rt->dst,
28789d6ec938SDavid S. Miller 							flowi4_to_flowi(flp4),
28799d6ec938SDavid S. Miller 							sk, 0);
2880874fb9e2SDavid Ahern 	}
28811da177e4SLinus Torvalds 
2882b23dd4feSDavid S. Miller 	return rt;
28831da177e4SLinus Torvalds }
2884d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2885d8c97a94SArnaldo Carvalho de Melo 
2886571912c6SMartin Varghese struct rtable *ip_route_output_tunnel(struct sk_buff *skb,
2887571912c6SMartin Varghese 				      struct net_device *dev,
2888571912c6SMartin Varghese 				      struct net *net, __be32 *saddr,
2889571912c6SMartin Varghese 				      const struct ip_tunnel_info *info,
2890571912c6SMartin Varghese 				      u8 protocol, bool use_cache)
2891571912c6SMartin Varghese {
2892571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2893571912c6SMartin Varghese 	struct dst_cache *dst_cache;
2894571912c6SMartin Varghese #endif
2895571912c6SMartin Varghese 	struct rtable *rt = NULL;
2896571912c6SMartin Varghese 	struct flowi4 fl4;
2897571912c6SMartin Varghese 	__u8 tos;
2898571912c6SMartin Varghese 
2899571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2900571912c6SMartin Varghese 	dst_cache = (struct dst_cache *)&info->dst_cache;
2901571912c6SMartin Varghese 	if (use_cache) {
2902571912c6SMartin Varghese 		rt = dst_cache_get_ip4(dst_cache, saddr);
2903571912c6SMartin Varghese 		if (rt)
2904571912c6SMartin Varghese 			return rt;
2905571912c6SMartin Varghese 	}
2906571912c6SMartin Varghese #endif
2907571912c6SMartin Varghese 	memset(&fl4, 0, sizeof(fl4));
2908571912c6SMartin Varghese 	fl4.flowi4_mark = skb->mark;
2909571912c6SMartin Varghese 	fl4.flowi4_proto = protocol;
2910571912c6SMartin Varghese 	fl4.daddr = info->key.u.ipv4.dst;
2911571912c6SMartin Varghese 	fl4.saddr = info->key.u.ipv4.src;
2912571912c6SMartin Varghese 	tos = info->key.tos;
2913571912c6SMartin Varghese 	fl4.flowi4_tos = RT_TOS(tos);
2914571912c6SMartin Varghese 
2915571912c6SMartin Varghese 	rt = ip_route_output_key(net, &fl4);
2916571912c6SMartin Varghese 	if (IS_ERR(rt)) {
2917571912c6SMartin Varghese 		netdev_dbg(dev, "no route to %pI4\n", &fl4.daddr);
2918571912c6SMartin Varghese 		return ERR_PTR(-ENETUNREACH);
2919571912c6SMartin Varghese 	}
2920571912c6SMartin Varghese 	if (rt->dst.dev == dev) { /* is this necessary? */
2921571912c6SMartin Varghese 		netdev_dbg(dev, "circular route to %pI4\n", &fl4.daddr);
2922571912c6SMartin Varghese 		ip_rt_put(rt);
2923571912c6SMartin Varghese 		return ERR_PTR(-ELOOP);
2924571912c6SMartin Varghese 	}
2925571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2926571912c6SMartin Varghese 	if (use_cache)
2927571912c6SMartin Varghese 		dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2928571912c6SMartin Varghese #endif
2929571912c6SMartin Varghese 	*saddr = fl4.saddr;
2930571912c6SMartin Varghese 	return rt;
2931571912c6SMartin Varghese }
2932571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip_route_output_tunnel);
2933571912c6SMartin Varghese 
29343765d35eSDavid Ahern /* called with rcu_read_lock held */
2935404eb77eSRoopa Prabhu static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
2936404eb77eSRoopa Prabhu 			struct rtable *rt, u32 table_id, struct flowi4 *fl4,
2937e93fb3e9SJohn Fastabend 			struct sk_buff *skb, u32 portid, u32 seq,
2938e93fb3e9SJohn Fastabend 			unsigned int flags)
29391da177e4SLinus Torvalds {
29401da177e4SLinus Torvalds 	struct rtmsg *r;
29411da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
29422bc8ca40SSteffen Klassert 	unsigned long expires = 0;
2943f185071dSDavid S. Miller 	u32 error;
2944521f5490SJulian Anastasov 	u32 metrics[RTAX_MAX];
2945be403ea1SThomas Graf 
2946e93fb3e9SJohn Fastabend 	nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags);
294751456b29SIan Morris 	if (!nlh)
294826932566SPatrick McHardy 		return -EMSGSIZE;
2949be403ea1SThomas Graf 
2950be403ea1SThomas Graf 	r = nlmsg_data(nlh);
29511da177e4SLinus Torvalds 	r->rtm_family	 = AF_INET;
29521da177e4SLinus Torvalds 	r->rtm_dst_len	= 32;
29531da177e4SLinus Torvalds 	r->rtm_src_len	= 0;
2954d948974cSStefano Brivio 	r->rtm_tos	= fl4 ? fl4->flowi4_tos : 0;
29558a430ed5SDavid Ahern 	r->rtm_table	= table_id < 256 ? table_id : RT_TABLE_COMPAT;
2956c36ba660SDavid Ahern 	if (nla_put_u32(skb, RTA_TABLE, table_id))
2957f3756b79SDavid S. Miller 		goto nla_put_failure;
29581da177e4SLinus Torvalds 	r->rtm_type	= rt->rt_type;
29591da177e4SLinus Torvalds 	r->rtm_scope	= RT_SCOPE_UNIVERSE;
29601da177e4SLinus Torvalds 	r->rtm_protocol = RTPROT_UNSPEC;
29611da177e4SLinus Torvalds 	r->rtm_flags	= (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
29621da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_NOTIFY)
29631da177e4SLinus Torvalds 		r->rtm_flags |= RTM_F_NOTIFY;
2964df4d9254SHannes Frederic Sowa 	if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
2965df4d9254SHannes Frederic Sowa 		r->rtm_flags |= RTCF_DOREDIRECT;
2966be403ea1SThomas Graf 
2967930345eaSJiri Benc 	if (nla_put_in_addr(skb, RTA_DST, dst))
2968f3756b79SDavid S. Miller 		goto nla_put_failure;
29691a00fee4SDavid Miller 	if (src) {
29701da177e4SLinus Torvalds 		r->rtm_src_len = 32;
2971930345eaSJiri Benc 		if (nla_put_in_addr(skb, RTA_SRC, src))
2972f3756b79SDavid S. Miller 			goto nla_put_failure;
29731da177e4SLinus Torvalds 	}
2974f3756b79SDavid S. Miller 	if (rt->dst.dev &&
2975f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2976f3756b79SDavid S. Miller 		goto nla_put_failure;
2977ae8cb932SOliver Herms 	if (rt->dst.lwtstate &&
2978ae8cb932SOliver Herms 	    lwtunnel_fill_encap(skb, rt->dst.lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
2979ae8cb932SOliver Herms 		goto nla_put_failure;
2980c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2981f3756b79SDavid S. Miller 	if (rt->dst.tclassid &&
2982f3756b79SDavid S. Miller 	    nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
2983f3756b79SDavid S. Miller 		goto nla_put_failure;
29841da177e4SLinus Torvalds #endif
2985d948974cSStefano Brivio 	if (fl4 && !rt_is_input_route(rt) &&
2986d6c0a4f6SDavid Miller 	    fl4->saddr != src) {
2987930345eaSJiri Benc 		if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
2988f3756b79SDavid S. Miller 			goto nla_put_failure;
2989f3756b79SDavid S. Miller 	}
299077d5bc7eSDavid Ahern 	if (rt->rt_uses_gateway) {
29911550c171SDavid Ahern 		if (rt->rt_gw_family == AF_INET &&
29920f5f7d7bSDavid Ahern 		    nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) {
2993f3756b79SDavid S. Miller 			goto nla_put_failure;
29940f5f7d7bSDavid Ahern 		} else if (rt->rt_gw_family == AF_INET6) {
29950f5f7d7bSDavid Ahern 			int alen = sizeof(struct in6_addr);
29960f5f7d7bSDavid Ahern 			struct nlattr *nla;
29970f5f7d7bSDavid Ahern 			struct rtvia *via;
29980f5f7d7bSDavid Ahern 
29990f5f7d7bSDavid Ahern 			nla = nla_reserve(skb, RTA_VIA, alen + 2);
30000f5f7d7bSDavid Ahern 			if (!nla)
30010f5f7d7bSDavid Ahern 				goto nla_put_failure;
30020f5f7d7bSDavid Ahern 
30030f5f7d7bSDavid Ahern 			via = nla_data(nla);
30040f5f7d7bSDavid Ahern 			via->rtvia_family = AF_INET6;
30050f5f7d7bSDavid Ahern 			memcpy(via->rtvia_addr, &rt->rt_gw6, alen);
30060f5f7d7bSDavid Ahern 		}
300777d5bc7eSDavid Ahern 	}
3008be403ea1SThomas Graf 
3009ee9a8f7aSSteffen Klassert 	expires = rt->dst.expires;
3010ee9a8f7aSSteffen Klassert 	if (expires) {
3011ee9a8f7aSSteffen Klassert 		unsigned long now = jiffies;
3012ee9a8f7aSSteffen Klassert 
3013ee9a8f7aSSteffen Klassert 		if (time_before(now, expires))
3014ee9a8f7aSSteffen Klassert 			expires -= now;
3015ee9a8f7aSSteffen Klassert 		else
3016ee9a8f7aSSteffen Klassert 			expires = 0;
3017ee9a8f7aSSteffen Klassert 	}
3018ee9a8f7aSSteffen Klassert 
3019521f5490SJulian Anastasov 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
3020ee9a8f7aSSteffen Klassert 	if (rt->rt_pmtu && expires)
3021521f5490SJulian Anastasov 		metrics[RTAX_MTU - 1] = rt->rt_pmtu;
3022d52e5a7eSSabrina Dubroca 	if (rt->rt_mtu_locked && expires)
3023d52e5a7eSSabrina Dubroca 		metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU);
3024521f5490SJulian Anastasov 	if (rtnetlink_put_metrics(skb, metrics) < 0)
3025be403ea1SThomas Graf 		goto nla_put_failure;
3026be403ea1SThomas Graf 
3027d948974cSStefano Brivio 	if (fl4) {
3028b4869889SDavid Miller 		if (fl4->flowi4_mark &&
302968aaed54Sstephen hemminger 		    nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark))
3030f3756b79SDavid S. Miller 			goto nla_put_failure;
3031963bfeeeSEric Dumazet 
3032622ec2c9SLorenzo Colitti 		if (!uid_eq(fl4->flowi4_uid, INVALID_UID) &&
3033622ec2c9SLorenzo Colitti 		    nla_put_u32(skb, RTA_UID,
3034d948974cSStefano Brivio 				from_kuid_munged(current_user_ns(),
3035d948974cSStefano Brivio 						 fl4->flowi4_uid)))
3036622ec2c9SLorenzo Colitti 			goto nla_put_failure;
3037622ec2c9SLorenzo Colitti 
3038c7537967SDavid S. Miller 		if (rt_is_input_route(rt)) {
30398caaf7b6SNicolas Dichtel #ifdef CONFIG_IP_MROUTE
3040d948974cSStefano Brivio 			if (ipv4_is_multicast(dst) &&
3041d948974cSStefano Brivio 			    !ipv4_is_local_multicast(dst) &&
30428caaf7b6SNicolas Dichtel 			    IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
30438caaf7b6SNicolas Dichtel 				int err = ipmr_get_route(net, skb,
30448caaf7b6SNicolas Dichtel 							 fl4->saddr, fl4->daddr,
30459f09eaeaSDavid Ahern 							 r, portid);
30462cf75070SNikolay Aleksandrov 
30478caaf7b6SNicolas Dichtel 				if (err <= 0) {
30488caaf7b6SNicolas Dichtel 					if (err == 0)
30498caaf7b6SNicolas Dichtel 						return 0;
30508caaf7b6SNicolas Dichtel 					goto nla_put_failure;
30518caaf7b6SNicolas Dichtel 				}
30528caaf7b6SNicolas Dichtel 			} else
30538caaf7b6SNicolas Dichtel #endif
3054404eb77eSRoopa Prabhu 				if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif))
3055f3756b79SDavid S. Miller 					goto nla_put_failure;
30561da177e4SLinus Torvalds 		}
3057d948974cSStefano Brivio 	}
3058d948974cSStefano Brivio 
3059d948974cSStefano Brivio 	error = rt->dst.error;
30601da177e4SLinus Torvalds 
3061f185071dSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
3062e3703b3dSThomas Graf 		goto nla_put_failure;
30631da177e4SLinus Torvalds 
3064053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3065053c095aSJohannes Berg 	return 0;
3066be403ea1SThomas Graf 
3067be403ea1SThomas Graf nla_put_failure:
306826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
306926932566SPatrick McHardy 	return -EMSGSIZE;
30701da177e4SLinus Torvalds }
30711da177e4SLinus Torvalds 
3072ee28906fSStefano Brivio static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb,
3073ee28906fSStefano Brivio 			    struct netlink_callback *cb, u32 table_id,
3074ee28906fSStefano Brivio 			    struct fnhe_hash_bucket *bucket, int genid,
3075e93fb3e9SJohn Fastabend 			    int *fa_index, int fa_start, unsigned int flags)
3076ee28906fSStefano Brivio {
3077ee28906fSStefano Brivio 	int i;
3078ee28906fSStefano Brivio 
3079ee28906fSStefano Brivio 	for (i = 0; i < FNHE_HASH_SIZE; i++) {
3080ee28906fSStefano Brivio 		struct fib_nh_exception *fnhe;
3081ee28906fSStefano Brivio 
3082ee28906fSStefano Brivio 		for (fnhe = rcu_dereference(bucket[i].chain); fnhe;
3083ee28906fSStefano Brivio 		     fnhe = rcu_dereference(fnhe->fnhe_next)) {
3084ee28906fSStefano Brivio 			struct rtable *rt;
3085ee28906fSStefano Brivio 			int err;
3086ee28906fSStefano Brivio 
3087ee28906fSStefano Brivio 			if (*fa_index < fa_start)
3088ee28906fSStefano Brivio 				goto next;
3089ee28906fSStefano Brivio 
3090ee28906fSStefano Brivio 			if (fnhe->fnhe_genid != genid)
3091ee28906fSStefano Brivio 				goto next;
3092ee28906fSStefano Brivio 
3093ee28906fSStefano Brivio 			if (fnhe->fnhe_expires &&
3094ee28906fSStefano Brivio 			    time_after(jiffies, fnhe->fnhe_expires))
3095ee28906fSStefano Brivio 				goto next;
3096ee28906fSStefano Brivio 
3097ee28906fSStefano Brivio 			rt = rcu_dereference(fnhe->fnhe_rth_input);
3098ee28906fSStefano Brivio 			if (!rt)
3099ee28906fSStefano Brivio 				rt = rcu_dereference(fnhe->fnhe_rth_output);
3100ee28906fSStefano Brivio 			if (!rt)
3101ee28906fSStefano Brivio 				goto next;
3102ee28906fSStefano Brivio 
3103ee28906fSStefano Brivio 			err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt,
3104ee28906fSStefano Brivio 					   table_id, NULL, skb,
3105ee28906fSStefano Brivio 					   NETLINK_CB(cb->skb).portid,
3106e93fb3e9SJohn Fastabend 					   cb->nlh->nlmsg_seq, flags);
3107ee28906fSStefano Brivio 			if (err)
3108ee28906fSStefano Brivio 				return err;
3109ee28906fSStefano Brivio next:
3110ee28906fSStefano Brivio 			(*fa_index)++;
3111ee28906fSStefano Brivio 		}
3112ee28906fSStefano Brivio 	}
3113ee28906fSStefano Brivio 
3114ee28906fSStefano Brivio 	return 0;
3115ee28906fSStefano Brivio }
3116ee28906fSStefano Brivio 
3117ee28906fSStefano Brivio int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
3118ee28906fSStefano Brivio 		       u32 table_id, struct fib_info *fi,
3119e93fb3e9SJohn Fastabend 		       int *fa_index, int fa_start, unsigned int flags)
3120ee28906fSStefano Brivio {
3121ee28906fSStefano Brivio 	struct net *net = sock_net(cb->skb->sk);
3122ee28906fSStefano Brivio 	int nhsel, genid = fnhe_genid(net);
3123ee28906fSStefano Brivio 
3124ee28906fSStefano Brivio 	for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
3125ee28906fSStefano Brivio 		struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel);
3126ee28906fSStefano Brivio 		struct fnhe_hash_bucket *bucket;
3127ee28906fSStefano Brivio 		int err;
3128ee28906fSStefano Brivio 
3129ee28906fSStefano Brivio 		if (nhc->nhc_flags & RTNH_F_DEAD)
3130ee28906fSStefano Brivio 			continue;
3131ee28906fSStefano Brivio 
313293ed54b1SEric Dumazet 		rcu_read_lock();
3133ee28906fSStefano Brivio 		bucket = rcu_dereference(nhc->nhc_exceptions);
313493ed54b1SEric Dumazet 		err = 0;
313593ed54b1SEric Dumazet 		if (bucket)
313693ed54b1SEric Dumazet 			err = fnhe_dump_bucket(net, skb, cb, table_id, bucket,
3137e93fb3e9SJohn Fastabend 					       genid, fa_index, fa_start,
3138e93fb3e9SJohn Fastabend 					       flags);
313993ed54b1SEric Dumazet 		rcu_read_unlock();
3140ee28906fSStefano Brivio 		if (err)
3141ee28906fSStefano Brivio 			return err;
3142ee28906fSStefano Brivio 	}
3143ee28906fSStefano Brivio 
3144ee28906fSStefano Brivio 	return 0;
3145ee28906fSStefano Brivio }
3146ee28906fSStefano Brivio 
3147404eb77eSRoopa Prabhu static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst,
3148404eb77eSRoopa Prabhu 						   u8 ip_proto, __be16 sport,
3149404eb77eSRoopa Prabhu 						   __be16 dport)
3150404eb77eSRoopa Prabhu {
3151404eb77eSRoopa Prabhu 	struct sk_buff *skb;
3152404eb77eSRoopa Prabhu 	struct iphdr *iph;
3153404eb77eSRoopa Prabhu 
3154404eb77eSRoopa Prabhu 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3155404eb77eSRoopa Prabhu 	if (!skb)
3156404eb77eSRoopa Prabhu 		return NULL;
3157404eb77eSRoopa Prabhu 
3158404eb77eSRoopa Prabhu 	/* Reserve room for dummy headers, this skb can pass
3159404eb77eSRoopa Prabhu 	 * through good chunk of routing engine.
3160404eb77eSRoopa Prabhu 	 */
3161404eb77eSRoopa Prabhu 	skb_reset_mac_header(skb);
3162404eb77eSRoopa Prabhu 	skb_reset_network_header(skb);
3163404eb77eSRoopa Prabhu 	skb->protocol = htons(ETH_P_IP);
3164404eb77eSRoopa Prabhu 	iph = skb_put(skb, sizeof(struct iphdr));
3165404eb77eSRoopa Prabhu 	iph->protocol = ip_proto;
3166404eb77eSRoopa Prabhu 	iph->saddr = src;
3167404eb77eSRoopa Prabhu 	iph->daddr = dst;
3168404eb77eSRoopa Prabhu 	iph->version = 0x4;
3169404eb77eSRoopa Prabhu 	iph->frag_off = 0;
3170404eb77eSRoopa Prabhu 	iph->ihl = 0x5;
3171404eb77eSRoopa Prabhu 	skb_set_transport_header(skb, skb->len);
3172404eb77eSRoopa Prabhu 
3173404eb77eSRoopa Prabhu 	switch (iph->protocol) {
3174404eb77eSRoopa Prabhu 	case IPPROTO_UDP: {
3175404eb77eSRoopa Prabhu 		struct udphdr *udph;
3176404eb77eSRoopa Prabhu 
3177404eb77eSRoopa Prabhu 		udph = skb_put_zero(skb, sizeof(struct udphdr));
3178404eb77eSRoopa Prabhu 		udph->source = sport;
3179404eb77eSRoopa Prabhu 		udph->dest = dport;
318092548b0eSEric Dumazet 		udph->len = htons(sizeof(struct udphdr));
3181404eb77eSRoopa Prabhu 		udph->check = 0;
3182404eb77eSRoopa Prabhu 		break;
3183404eb77eSRoopa Prabhu 	}
3184404eb77eSRoopa Prabhu 	case IPPROTO_TCP: {
3185404eb77eSRoopa Prabhu 		struct tcphdr *tcph;
3186404eb77eSRoopa Prabhu 
3187404eb77eSRoopa Prabhu 		tcph = skb_put_zero(skb, sizeof(struct tcphdr));
3188404eb77eSRoopa Prabhu 		tcph->source	= sport;
3189404eb77eSRoopa Prabhu 		tcph->dest	= dport;
3190404eb77eSRoopa Prabhu 		tcph->doff	= sizeof(struct tcphdr) / 4;
3191404eb77eSRoopa Prabhu 		tcph->rst = 1;
3192404eb77eSRoopa Prabhu 		tcph->check = ~tcp_v4_check(sizeof(struct tcphdr),
3193404eb77eSRoopa Prabhu 					    src, dst, 0);
3194404eb77eSRoopa Prabhu 		break;
3195404eb77eSRoopa Prabhu 	}
3196404eb77eSRoopa Prabhu 	case IPPROTO_ICMP: {
3197404eb77eSRoopa Prabhu 		struct icmphdr *icmph;
3198404eb77eSRoopa Prabhu 
3199404eb77eSRoopa Prabhu 		icmph = skb_put_zero(skb, sizeof(struct icmphdr));
3200404eb77eSRoopa Prabhu 		icmph->type = ICMP_ECHO;
3201404eb77eSRoopa Prabhu 		icmph->code = 0;
3202404eb77eSRoopa Prabhu 	}
3203404eb77eSRoopa Prabhu 	}
3204404eb77eSRoopa Prabhu 
3205404eb77eSRoopa Prabhu 	return skb;
3206404eb77eSRoopa Prabhu }
3207404eb77eSRoopa Prabhu 
3208a00302b6SJakub Kicinski static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
3209a00302b6SJakub Kicinski 				       const struct nlmsghdr *nlh,
3210a00302b6SJakub Kicinski 				       struct nlattr **tb,
3211a00302b6SJakub Kicinski 				       struct netlink_ext_ack *extack)
3212a00302b6SJakub Kicinski {
3213a00302b6SJakub Kicinski 	struct rtmsg *rtm;
3214a00302b6SJakub Kicinski 	int i, err;
3215a00302b6SJakub Kicinski 
3216a00302b6SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
3217a00302b6SJakub Kicinski 		NL_SET_ERR_MSG(extack,
3218a00302b6SJakub Kicinski 			       "ipv4: Invalid header for route get request");
3219a00302b6SJakub Kicinski 		return -EINVAL;
3220a00302b6SJakub Kicinski 	}
3221a00302b6SJakub Kicinski 
3222a00302b6SJakub Kicinski 	if (!netlink_strict_get_check(skb))
32238cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
3224a00302b6SJakub Kicinski 					      rtm_ipv4_policy, extack);
3225a00302b6SJakub Kicinski 
3226a00302b6SJakub Kicinski 	rtm = nlmsg_data(nlh);
3227a00302b6SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
3228a00302b6SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
3229a00302b6SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol ||
3230a00302b6SJakub Kicinski 	    rtm->rtm_scope || rtm->rtm_type) {
3231a00302b6SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request");
3232a00302b6SJakub Kicinski 		return -EINVAL;
3233a00302b6SJakub Kicinski 	}
3234a00302b6SJakub Kicinski 
3235a00302b6SJakub Kicinski 	if (rtm->rtm_flags & ~(RTM_F_NOTIFY |
3236a00302b6SJakub Kicinski 			       RTM_F_LOOKUP_TABLE |
3237a00302b6SJakub Kicinski 			       RTM_F_FIB_MATCH)) {
3238a00302b6SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request");
3239a00302b6SJakub Kicinski 		return -EINVAL;
3240a00302b6SJakub Kicinski 	}
3241a00302b6SJakub Kicinski 
32428cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
3243a00302b6SJakub Kicinski 					    rtm_ipv4_policy, extack);
3244a00302b6SJakub Kicinski 	if (err)
3245a00302b6SJakub Kicinski 		return err;
3246a00302b6SJakub Kicinski 
3247a00302b6SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
3248a00302b6SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
3249a00302b6SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
3250a00302b6SJakub Kicinski 		return -EINVAL;
3251a00302b6SJakub Kicinski 	}
3252a00302b6SJakub Kicinski 
3253a00302b6SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
3254a00302b6SJakub Kicinski 		if (!tb[i])
3255a00302b6SJakub Kicinski 			continue;
3256a00302b6SJakub Kicinski 
3257a00302b6SJakub Kicinski 		switch (i) {
3258a00302b6SJakub Kicinski 		case RTA_IIF:
3259a00302b6SJakub Kicinski 		case RTA_OIF:
3260a00302b6SJakub Kicinski 		case RTA_SRC:
3261a00302b6SJakub Kicinski 		case RTA_DST:
3262a00302b6SJakub Kicinski 		case RTA_IP_PROTO:
3263a00302b6SJakub Kicinski 		case RTA_SPORT:
3264a00302b6SJakub Kicinski 		case RTA_DPORT:
3265a00302b6SJakub Kicinski 		case RTA_MARK:
3266a00302b6SJakub Kicinski 		case RTA_UID:
3267a00302b6SJakub Kicinski 			break;
3268a00302b6SJakub Kicinski 		default:
3269a00302b6SJakub Kicinski 			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request");
3270a00302b6SJakub Kicinski 			return -EINVAL;
3271a00302b6SJakub Kicinski 		}
3272a00302b6SJakub Kicinski 	}
3273a00302b6SJakub Kicinski 
3274a00302b6SJakub Kicinski 	return 0;
3275a00302b6SJakub Kicinski }
3276a00302b6SJakub Kicinski 
3277c21ef3e3SDavid Ahern static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3278c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
32791da177e4SLinus Torvalds {
32803b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
3281d889ce3bSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
3282404eb77eSRoopa Prabhu 	u32 table_id = RT_TABLE_MAIN;
3283404eb77eSRoopa Prabhu 	__be16 sport = 0, dport = 0;
32843765d35eSDavid Ahern 	struct fib_result res = {};
3285404eb77eSRoopa Prabhu 	u8 ip_proto = IPPROTO_UDP;
32861da177e4SLinus Torvalds 	struct rtable *rt = NULL;
3287404eb77eSRoopa Prabhu 	struct sk_buff *skb;
3288404eb77eSRoopa Prabhu 	struct rtmsg *rtm;
3289e8e3fbe9SMaciej Żenczykowski 	struct flowi4 fl4 = {};
32909e12bb22SAl Viro 	__be32 dst = 0;
32919e12bb22SAl Viro 	__be32 src = 0;
3292404eb77eSRoopa Prabhu 	kuid_t uid;
32939e12bb22SAl Viro 	u32 iif;
3294d889ce3bSThomas Graf 	int err;
3295963bfeeeSEric Dumazet 	int mark;
32961da177e4SLinus Torvalds 
3297a00302b6SJakub Kicinski 	err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
3298d889ce3bSThomas Graf 	if (err < 0)
3299404eb77eSRoopa Prabhu 		return err;
3300d889ce3bSThomas Graf 
3301d889ce3bSThomas Graf 	rtm = nlmsg_data(nlh);
330267b61f6cSJiri Benc 	src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
330367b61f6cSJiri Benc 	dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
3304d889ce3bSThomas Graf 	iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
3305963bfeeeSEric Dumazet 	mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
3306622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
3307622ec2c9SLorenzo Colitti 		uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID]));
3308622ec2c9SLorenzo Colitti 	else
3309622ec2c9SLorenzo Colitti 		uid = (iif ? INVALID_UID : current_uid());
33101da177e4SLinus Torvalds 
3311404eb77eSRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
3312404eb77eSRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
33135e1a99eaSHangbin Liu 						  &ip_proto, AF_INET, extack);
3314404eb77eSRoopa Prabhu 		if (err)
3315404eb77eSRoopa Prabhu 			return err;
3316404eb77eSRoopa Prabhu 	}
3317bbadb9a2SFlorian Larysch 
3318404eb77eSRoopa Prabhu 	if (tb[RTA_SPORT])
3319404eb77eSRoopa Prabhu 		sport = nla_get_be16(tb[RTA_SPORT]);
3320404eb77eSRoopa Prabhu 
3321404eb77eSRoopa Prabhu 	if (tb[RTA_DPORT])
3322404eb77eSRoopa Prabhu 		dport = nla_get_be16(tb[RTA_DPORT]);
3323404eb77eSRoopa Prabhu 
3324404eb77eSRoopa Prabhu 	skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport);
3325404eb77eSRoopa Prabhu 	if (!skb)
3326404eb77eSRoopa Prabhu 		return -ENOBUFS;
3327bbadb9a2SFlorian Larysch 
3328d6c0a4f6SDavid Miller 	fl4.daddr = dst;
3329d6c0a4f6SDavid Miller 	fl4.saddr = src;
33301ebf1790SGuillaume Nault 	fl4.flowi4_tos = rtm->rtm_tos & IPTOS_RT_MASK;
3331d6c0a4f6SDavid Miller 	fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
3332d6c0a4f6SDavid Miller 	fl4.flowi4_mark = mark;
3333622ec2c9SLorenzo Colitti 	fl4.flowi4_uid = uid;
3334404eb77eSRoopa Prabhu 	if (sport)
3335404eb77eSRoopa Prabhu 		fl4.fl4_sport = sport;
3336404eb77eSRoopa Prabhu 	if (dport)
3337404eb77eSRoopa Prabhu 		fl4.fl4_dport = dport;
3338404eb77eSRoopa Prabhu 	fl4.flowi4_proto = ip_proto;
3339d6c0a4f6SDavid Miller 
33403765d35eSDavid Ahern 	rcu_read_lock();
33413765d35eSDavid Ahern 
33421da177e4SLinus Torvalds 	if (iif) {
3343d889ce3bSThomas Graf 		struct net_device *dev;
3344d889ce3bSThomas Graf 
33453765d35eSDavid Ahern 		dev = dev_get_by_index_rcu(net, iif);
334651456b29SIan Morris 		if (!dev) {
33471da177e4SLinus Torvalds 			err = -ENODEV;
3348404eb77eSRoopa Prabhu 			goto errout_rcu;
3349d889ce3bSThomas Graf 		}
3350d889ce3bSThomas Graf 
3351404eb77eSRoopa Prabhu 		fl4.flowi4_iif = iif; /* for rt_fill_info */
33521da177e4SLinus Torvalds 		skb->dev	= dev;
3353963bfeeeSEric Dumazet 		skb->mark	= mark;
33541ebf1790SGuillaume Nault 		err = ip_route_input_rcu(skb, dst, src,
33551ebf1790SGuillaume Nault 					 rtm->rtm_tos & IPTOS_RT_MASK, dev,
33561ebf1790SGuillaume Nault 					 &res);
3357d889ce3bSThomas Graf 
3358511c3f92SEric Dumazet 		rt = skb_rtable(skb);
3359d8d1f30bSChangli Gao 		if (err == 0 && rt->dst.error)
3360d8d1f30bSChangli Gao 			err = -rt->dst.error;
33611da177e4SLinus Torvalds 	} else {
33626503a304SLorenzo Colitti 		fl4.flowi4_iif = LOOPBACK_IFINDEX;
336321f94775SIdo Schimmel 		skb->dev = net->loopback_dev;
33643765d35eSDavid Ahern 		rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb);
3365b23dd4feSDavid S. Miller 		err = 0;
3366b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
3367b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
33682c87d63aSFlorian Westphal 		else
33692c87d63aSFlorian Westphal 			skb_dst_set(skb, &rt->dst);
33701da177e4SLinus Torvalds 	}
3371d889ce3bSThomas Graf 
33721da177e4SLinus Torvalds 	if (err)
3373404eb77eSRoopa Prabhu 		goto errout_rcu;
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds 	if (rtm->rtm_flags & RTM_F_NOTIFY)
33761da177e4SLinus Torvalds 		rt->rt_flags |= RTCF_NOTIFY;
33771da177e4SLinus Torvalds 
3378c36ba660SDavid Ahern 	if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE)
337968e813aaSDavid Ahern 		table_id = res.table ? res.table->tb_id : 0;
3380c36ba660SDavid Ahern 
3381404eb77eSRoopa Prabhu 	/* reset skb for netlink reply msg */
3382404eb77eSRoopa Prabhu 	skb_trim(skb, 0);
3383404eb77eSRoopa Prabhu 	skb_reset_network_header(skb);
3384404eb77eSRoopa Prabhu 	skb_reset_transport_header(skb);
3385404eb77eSRoopa Prabhu 	skb_reset_mac_header(skb);
3386404eb77eSRoopa Prabhu 
3387bc3aae2bSRoopa Prabhu 	if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
33881e301fd0SIdo Schimmel 		struct fib_rt_info fri;
33891e301fd0SIdo Schimmel 
3390bc3aae2bSRoopa Prabhu 		if (!res.fi) {
3391bc3aae2bSRoopa Prabhu 			err = fib_props[res.type].error;
3392bc3aae2bSRoopa Prabhu 			if (!err)
3393bc3aae2bSRoopa Prabhu 				err = -EHOSTUNREACH;
3394404eb77eSRoopa Prabhu 			goto errout_rcu;
3395bc3aae2bSRoopa Prabhu 		}
33961e301fd0SIdo Schimmel 		fri.fi = res.fi;
33971e301fd0SIdo Schimmel 		fri.tb_id = table_id;
33981e301fd0SIdo Schimmel 		fri.dst = res.prefix;
33991e301fd0SIdo Schimmel 		fri.dst_len = res.prefixlen;
3400888ade8fSGuillaume Nault 		fri.dscp = inet_dsfield_to_dscp(fl4.flowi4_tos);
34011e301fd0SIdo Schimmel 		fri.type = rt->rt_type;
340290b93f1bSIdo Schimmel 		fri.offload = 0;
340390b93f1bSIdo Schimmel 		fri.trap = 0;
340436c5100eSAmit Cohen 		fri.offload_failed = 0;
340590b93f1bSIdo Schimmel 		if (res.fa_head) {
340690b93f1bSIdo Schimmel 			struct fib_alias *fa;
340790b93f1bSIdo Schimmel 
340890b93f1bSIdo Schimmel 			hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) {
340990b93f1bSIdo Schimmel 				u8 slen = 32 - fri.dst_len;
341090b93f1bSIdo Schimmel 
341190b93f1bSIdo Schimmel 				if (fa->fa_slen == slen &&
341290b93f1bSIdo Schimmel 				    fa->tb_id == fri.tb_id &&
3413888ade8fSGuillaume Nault 				    fa->fa_dscp == fri.dscp &&
341490b93f1bSIdo Schimmel 				    fa->fa_info == res.fi &&
341590b93f1bSIdo Schimmel 				    fa->fa_type == fri.type) {
34169fcf986cSEric Dumazet 					fri.offload = READ_ONCE(fa->offload);
34179fcf986cSEric Dumazet 					fri.trap = READ_ONCE(fa->trap);
341890b93f1bSIdo Schimmel 					break;
341990b93f1bSIdo Schimmel 				}
342090b93f1bSIdo Schimmel 			}
342190b93f1bSIdo Schimmel 		}
3422b6179813SRoopa Prabhu 		err = fib_dump_info(skb, NETLINK_CB(in_skb).portid,
34231e301fd0SIdo Schimmel 				    nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0);
3424bc3aae2bSRoopa Prabhu 	} else {
3425404eb77eSRoopa Prabhu 		err = rt_fill_info(net, dst, src, rt, table_id, &fl4, skb,
3426e93fb3e9SJohn Fastabend 				   NETLINK_CB(in_skb).portid,
3427e93fb3e9SJohn Fastabend 				   nlh->nlmsg_seq, 0);
3428bc3aae2bSRoopa Prabhu 	}
34297b46a644SDavid S. Miller 	if (err < 0)
3430404eb77eSRoopa Prabhu 		goto errout_rcu;
34311da177e4SLinus Torvalds 
34323765d35eSDavid Ahern 	rcu_read_unlock();
34333765d35eSDavid Ahern 
343415e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
34351da177e4SLinus Torvalds 
3436d889ce3bSThomas Graf errout_free:
3437404eb77eSRoopa Prabhu 	return err;
3438404eb77eSRoopa Prabhu errout_rcu:
34393765d35eSDavid Ahern 	rcu_read_unlock();
34401da177e4SLinus Torvalds 	kfree_skb(skb);
3441404eb77eSRoopa Prabhu 	goto errout_free;
34421da177e4SLinus Torvalds }
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
34451da177e4SLinus Torvalds {
34464ccfe6d4SNicolas Dichtel 	rt_cache_flush(dev_net(in_dev->dev));
34471da177e4SLinus Torvalds }
34481da177e4SLinus Torvalds 
34491da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
3450082c7ca4SGao feng static int ip_rt_gc_interval __read_mostly  = 60 * HZ;
3451082c7ca4SGao feng static int ip_rt_gc_min_interval __read_mostly	= HZ / 2;
3452082c7ca4SGao feng static int ip_rt_gc_elasticity __read_mostly	= 8;
3453773daa3cSArnd Bergmann static int ip_min_valid_pmtu __read_mostly	= IPV4_MIN_MTU;
3454082c7ca4SGao feng 
3455fe2c6338SJoe Perches static int ipv4_sysctl_rtcache_flush(struct ctl_table *__ctl, int write,
345632927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
34571da177e4SLinus Torvalds {
34585aad1de5STimo Teräs 	struct net *net = (struct net *)__ctl->extra1;
34595aad1de5STimo Teräs 
34601da177e4SLinus Torvalds 	if (write) {
34615aad1de5STimo Teräs 		rt_cache_flush(net);
34625aad1de5STimo Teräs 		fnhe_genid_bump(net);
34631da177e4SLinus Torvalds 		return 0;
34641da177e4SLinus Torvalds 	}
34651da177e4SLinus Torvalds 
34661da177e4SLinus Torvalds 	return -EINVAL;
34671da177e4SLinus Torvalds }
34681da177e4SLinus Torvalds 
3469fe2c6338SJoe Perches static struct ctl_table ipv4_route_table[] = {
34701da177e4SLinus Torvalds 	{
34711da177e4SLinus Torvalds 		.procname	= "gc_thresh",
34721da177e4SLinus Torvalds 		.data		= &ipv4_dst_ops.gc_thresh,
34731da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
34741da177e4SLinus Torvalds 		.mode		= 0644,
34756d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
34761da177e4SLinus Torvalds 	},
34771da177e4SLinus Torvalds 	{
34781da177e4SLinus Torvalds 		.procname	= "max_size",
34791da177e4SLinus Torvalds 		.data		= &ip_rt_max_size,
34801da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
34811da177e4SLinus Torvalds 		.mode		= 0644,
34826d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
34831da177e4SLinus Torvalds 	},
34841da177e4SLinus Torvalds 	{
34851da177e4SLinus Torvalds 		/*  Deprecated. Use gc_min_interval_ms */
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds 		.procname	= "gc_min_interval",
34881da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
34891da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
34901da177e4SLinus Torvalds 		.mode		= 0644,
34916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
34921da177e4SLinus Torvalds 	},
34931da177e4SLinus Torvalds 	{
34941da177e4SLinus Torvalds 		.procname	= "gc_min_interval_ms",
34951da177e4SLinus Torvalds 		.data		= &ip_rt_gc_min_interval,
34961da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
34971da177e4SLinus Torvalds 		.mode		= 0644,
34986d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_ms_jiffies,
34991da177e4SLinus Torvalds 	},
35001da177e4SLinus Torvalds 	{
35011da177e4SLinus Torvalds 		.procname	= "gc_timeout",
35021da177e4SLinus Torvalds 		.data		= &ip_rt_gc_timeout,
35031da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35041da177e4SLinus Torvalds 		.mode		= 0644,
35056d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
35061da177e4SLinus Torvalds 	},
35071da177e4SLinus Torvalds 	{
35089f28a2fcSEric Dumazet 		.procname	= "gc_interval",
35099f28a2fcSEric Dumazet 		.data		= &ip_rt_gc_interval,
35109f28a2fcSEric Dumazet 		.maxlen		= sizeof(int),
35119f28a2fcSEric Dumazet 		.mode		= 0644,
35129f28a2fcSEric Dumazet 		.proc_handler	= proc_dointvec_jiffies,
35139f28a2fcSEric Dumazet 	},
35149f28a2fcSEric Dumazet 	{
35151da177e4SLinus Torvalds 		.procname	= "redirect_load",
35161da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_load,
35171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35181da177e4SLinus Torvalds 		.mode		= 0644,
35196d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35201da177e4SLinus Torvalds 	},
35211da177e4SLinus Torvalds 	{
35221da177e4SLinus Torvalds 		.procname	= "redirect_number",
35231da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_number,
35241da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35251da177e4SLinus Torvalds 		.mode		= 0644,
35266d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35271da177e4SLinus Torvalds 	},
35281da177e4SLinus Torvalds 	{
35291da177e4SLinus Torvalds 		.procname	= "redirect_silence",
35301da177e4SLinus Torvalds 		.data		= &ip_rt_redirect_silence,
35311da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35321da177e4SLinus Torvalds 		.mode		= 0644,
35336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35341da177e4SLinus Torvalds 	},
35351da177e4SLinus Torvalds 	{
35361da177e4SLinus Torvalds 		.procname	= "error_cost",
35371da177e4SLinus Torvalds 		.data		= &ip_rt_error_cost,
35381da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35391da177e4SLinus Torvalds 		.mode		= 0644,
35406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35411da177e4SLinus Torvalds 	},
35421da177e4SLinus Torvalds 	{
35431da177e4SLinus Torvalds 		.procname	= "error_burst",
35441da177e4SLinus Torvalds 		.data		= &ip_rt_error_burst,
35451da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35461da177e4SLinus Torvalds 		.mode		= 0644,
35476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35481da177e4SLinus Torvalds 	},
35491da177e4SLinus Torvalds 	{
35501da177e4SLinus Torvalds 		.procname	= "gc_elasticity",
35511da177e4SLinus Torvalds 		.data		= &ip_rt_gc_elasticity,
35521da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
35531da177e4SLinus Torvalds 		.mode		= 0644,
35546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
35551da177e4SLinus Torvalds 	},
3556f8572d8fSEric W. Biederman 	{ }
35571da177e4SLinus Torvalds };
355839a23e75SDenis V. Lunev 
35595cdda5f1SChristian Brauner static const char ipv4_route_flush_procname[] = "flush";
35605cdda5f1SChristian Brauner 
35611de6b15aSxu xin static struct ctl_table ipv4_route_netns_table[] = {
356239a23e75SDenis V. Lunev 	{
35635cdda5f1SChristian Brauner 		.procname	= ipv4_route_flush_procname,
356439a23e75SDenis V. Lunev 		.maxlen		= sizeof(int),
356539a23e75SDenis V. Lunev 		.mode		= 0200,
35666d9f239aSAlexey Dobriyan 		.proc_handler	= ipv4_sysctl_rtcache_flush,
356739a23e75SDenis V. Lunev 	},
35681de6b15aSxu xin 	{
35691de6b15aSxu xin 		.procname       = "min_pmtu",
35701de6b15aSxu xin 		.data           = &init_net.ipv4.ip_rt_min_pmtu,
35711de6b15aSxu xin 		.maxlen         = sizeof(int),
35721de6b15aSxu xin 		.mode           = 0644,
35731de6b15aSxu xin 		.proc_handler   = proc_dointvec_minmax,
35741de6b15aSxu xin 		.extra1         = &ip_min_valid_pmtu,
35751de6b15aSxu xin 	},
35761135fad2Sxu xin 	{
35771135fad2Sxu xin 		.procname       = "mtu_expires",
35781135fad2Sxu xin 		.data           = &init_net.ipv4.ip_rt_mtu_expires,
35791135fad2Sxu xin 		.maxlen         = sizeof(int),
35801135fad2Sxu xin 		.mode           = 0644,
35811135fad2Sxu xin 		.proc_handler   = proc_dointvec_jiffies,
35821135fad2Sxu xin 	},
35832e9589ffSxu xin 	{
35842e9589ffSxu xin 		.procname   = "min_adv_mss",
35852e9589ffSxu xin 		.data       = &init_net.ipv4.ip_rt_min_advmss,
35862e9589ffSxu xin 		.maxlen     = sizeof(int),
35872e9589ffSxu xin 		.mode       = 0644,
35882e9589ffSxu xin 		.proc_handler   = proc_dointvec,
35892e9589ffSxu xin 	},
3590f8572d8fSEric W. Biederman 	{ },
359139a23e75SDenis V. Lunev };
359239a23e75SDenis V. Lunev 
359339a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net)
359439a23e75SDenis V. Lunev {
359539a23e75SDenis V. Lunev 	struct ctl_table *tbl;
359639a23e75SDenis V. Lunev 
35971de6b15aSxu xin 	tbl = ipv4_route_netns_table;
359809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
35991de6b15aSxu xin 		int i;
36001de6b15aSxu xin 
36011de6b15aSxu xin 		tbl = kmemdup(tbl, sizeof(ipv4_route_netns_table), GFP_KERNEL);
360251456b29SIan Morris 		if (!tbl)
360339a23e75SDenis V. Lunev 			goto err_dup;
3604464dc801SEric W. Biederman 
36055cdda5f1SChristian Brauner 		/* Don't export non-whitelisted sysctls to unprivileged users */
36065cdda5f1SChristian Brauner 		if (net->user_ns != &init_user_ns) {
36075cdda5f1SChristian Brauner 			if (tbl[0].procname != ipv4_route_flush_procname)
3608464dc801SEric W. Biederman 				tbl[0].procname = NULL;
360939a23e75SDenis V. Lunev 		}
36101de6b15aSxu xin 
36111de6b15aSxu xin 		/* Update the variables to point into the current struct net
36121de6b15aSxu xin 		 * except for the first element flush
36131de6b15aSxu xin 		 */
36141de6b15aSxu xin 		for (i = 1; i < ARRAY_SIZE(ipv4_route_netns_table) - 1; i++)
36151de6b15aSxu xin 			tbl[i].data += (void *)net - (void *)&init_net;
36165cdda5f1SChristian Brauner 	}
361739a23e75SDenis V. Lunev 	tbl[0].extra1 = net;
361839a23e75SDenis V. Lunev 
3619ec8f23ceSEric W. Biederman 	net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
362051456b29SIan Morris 	if (!net->ipv4.route_hdr)
362139a23e75SDenis V. Lunev 		goto err_reg;
362239a23e75SDenis V. Lunev 	return 0;
362339a23e75SDenis V. Lunev 
362439a23e75SDenis V. Lunev err_reg:
36251de6b15aSxu xin 	if (tbl != ipv4_route_netns_table)
362639a23e75SDenis V. Lunev 		kfree(tbl);
362739a23e75SDenis V. Lunev err_dup:
362839a23e75SDenis V. Lunev 	return -ENOMEM;
362939a23e75SDenis V. Lunev }
363039a23e75SDenis V. Lunev 
363139a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net)
363239a23e75SDenis V. Lunev {
363339a23e75SDenis V. Lunev 	struct ctl_table *tbl;
363439a23e75SDenis V. Lunev 
363539a23e75SDenis V. Lunev 	tbl = net->ipv4.route_hdr->ctl_table_arg;
363639a23e75SDenis V. Lunev 	unregister_net_sysctl_table(net->ipv4.route_hdr);
36371de6b15aSxu xin 	BUG_ON(tbl == ipv4_route_netns_table);
363839a23e75SDenis V. Lunev 	kfree(tbl);
363939a23e75SDenis V. Lunev }
364039a23e75SDenis V. Lunev 
364139a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = {
364239a23e75SDenis V. Lunev 	.init = sysctl_route_net_init,
364339a23e75SDenis V. Lunev 	.exit = sysctl_route_net_exit,
364439a23e75SDenis V. Lunev };
36451da177e4SLinus Torvalds #endif
36461da177e4SLinus Torvalds 
36471de6b15aSxu xin static __net_init int netns_ip_rt_init(struct net *net)
36481de6b15aSxu xin {
36491de6b15aSxu xin 	/* Set default value for namespaceified sysctls */
36501de6b15aSxu xin 	net->ipv4.ip_rt_min_pmtu = DEFAULT_MIN_PMTU;
36511135fad2Sxu xin 	net->ipv4.ip_rt_mtu_expires = DEFAULT_MTU_EXPIRES;
36522e9589ffSxu xin 	net->ipv4.ip_rt_min_advmss = DEFAULT_MIN_ADVMSS;
36531de6b15aSxu xin 	return 0;
36541de6b15aSxu xin }
36551de6b15aSxu xin 
36561de6b15aSxu xin static struct pernet_operations __net_initdata ip_rt_ops = {
36571de6b15aSxu xin 	.init = netns_ip_rt_init,
36581de6b15aSxu xin };
36591de6b15aSxu xin 
36603ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net)
36619f5e97e5SDenis V. Lunev {
3662ca4c3fc2Sfan.du 	atomic_set(&net->ipv4.rt_genid, 0);
36635aad1de5STimo Teräs 	atomic_set(&net->fnhe_genid, 0);
3664a251c17aSJason A. Donenfeld 	atomic_set(&net->ipv4.dev_addr_genid, get_random_u32());
36659f5e97e5SDenis V. Lunev 	return 0;
36669f5e97e5SDenis V. Lunev }
36679f5e97e5SDenis V. Lunev 
36683ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = {
36693ee94372SNeil Horman 	.init = rt_genid_init,
36709f5e97e5SDenis V. Lunev };
36719f5e97e5SDenis V. Lunev 
3672c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net)
3673c3426b47SDavid S. Miller {
3674c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3675c3426b47SDavid S. Miller 
3676c3426b47SDavid S. Miller 	if (!bp)
3677c3426b47SDavid S. Miller 		return -ENOMEM;
3678c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3679c3426b47SDavid S. Miller 	net->ipv4.peers = bp;
3680c3426b47SDavid S. Miller 	return 0;
3681c3426b47SDavid S. Miller }
3682c3426b47SDavid S. Miller 
3683c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net)
3684c3426b47SDavid S. Miller {
3685c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv4.peers;
3686c3426b47SDavid S. Miller 
3687c3426b47SDavid S. Miller 	net->ipv4.peers = NULL;
368856a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3689c3426b47SDavid S. Miller 	kfree(bp);
3690c3426b47SDavid S. Miller }
3691c3426b47SDavid S. Miller 
3692c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
3693c3426b47SDavid S. Miller 	.init	=	ipv4_inetpeer_init,
3694c3426b47SDavid S. Miller 	.exit	=	ipv4_inetpeer_exit,
3695c3426b47SDavid S. Miller };
36969f5e97e5SDenis V. Lunev 
3697c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
36987d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
3699c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */
37001da177e4SLinus Torvalds 
37011da177e4SLinus Torvalds int __init ip_rt_init(void)
37021da177e4SLinus Torvalds {
3703aa6dd211SEric Dumazet 	void *idents_hash;
37045055c371SEric Dumazet 	int cpu;
37051da177e4SLinus Torvalds 
3706aa6dd211SEric Dumazet 	/* For modern hosts, this will use 2 MB of memory */
3707aa6dd211SEric Dumazet 	idents_hash = alloc_large_system_hash("IP idents",
3708aa6dd211SEric Dumazet 					      sizeof(*ip_idents) + sizeof(*ip_tstamps),
3709aa6dd211SEric Dumazet 					      0,
3710aa6dd211SEric Dumazet 					      16, /* one bucket per 64 KB */
3711aa6dd211SEric Dumazet 					      HASH_ZERO,
3712aa6dd211SEric Dumazet 					      NULL,
3713aa6dd211SEric Dumazet 					      &ip_idents_mask,
3714aa6dd211SEric Dumazet 					      2048,
3715aa6dd211SEric Dumazet 					      256*1024);
371673f156a6SEric Dumazet 
3717aa6dd211SEric Dumazet 	ip_idents = idents_hash;
371873f156a6SEric Dumazet 
3719197173dbSJason A. Donenfeld 	get_random_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
3720aa6dd211SEric Dumazet 
3721aa6dd211SEric Dumazet 	ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
3722355b590cSEric Dumazet 
37235055c371SEric Dumazet 	for_each_possible_cpu(cpu) {
37245055c371SEric Dumazet 		struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
37255055c371SEric Dumazet 
37265055c371SEric Dumazet 		INIT_LIST_HEAD(&ul->head);
372729e5375dSEric Dumazet 		INIT_LIST_HEAD(&ul->quarantine);
37285055c371SEric Dumazet 		spin_lock_init(&ul->lock);
37295055c371SEric Dumazet 	}
3730c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
37310dcec8c2SIngo Molnar 	ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
37321da177e4SLinus Torvalds 	if (!ip_rt_acct)
37331da177e4SLinus Torvalds 		panic("IP: failed to allocate ip_rt_acct\n");
37341da177e4SLinus Torvalds #endif
37351da177e4SLinus Torvalds 
3736e5d679f3SAlexey Dobriyan 	ipv4_dst_ops.kmem_cachep =
3737e5d679f3SAlexey Dobriyan 		kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
373820c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
37391da177e4SLinus Torvalds 
374014e50e57SDavid S. Miller 	ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
374114e50e57SDavid S. Miller 
3742fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_ops) < 0)
3743fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_ops counter\n");
3744fc66f95cSEric Dumazet 
3745fc66f95cSEric Dumazet 	if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
3746fc66f95cSEric Dumazet 		panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
3747fc66f95cSEric Dumazet 
374889aef892SDavid S. Miller 	ipv4_dst_ops.gc_thresh = ~0;
374989aef892SDavid S. Miller 	ip_rt_max_size = INT_MAX;
37501da177e4SLinus Torvalds 
37511da177e4SLinus Torvalds 	devinet_init();
37521da177e4SLinus Torvalds 	ip_fib_init();
37531da177e4SLinus Torvalds 
375473b38711SDenis V. Lunev 	if (ip_rt_proc_init())
3755058bd4d2SJoe Perches 		pr_err("Unable to create route proc files\n");
37561da177e4SLinus Torvalds #ifdef CONFIG_XFRM
37571da177e4SLinus Torvalds 	xfrm_init();
3758703fb94eSSteffen Klassert 	xfrm4_init();
37591da177e4SLinus Torvalds #endif
3760394f51abSFlorian Westphal 	rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL,
3761394f51abSFlorian Westphal 		      RTNL_FLAG_DOIT_UNLOCKED);
376263f3444fSThomas Graf 
376339a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL
376439a23e75SDenis V. Lunev 	register_pernet_subsys(&sysctl_route_ops);
376539a23e75SDenis V. Lunev #endif
37661de6b15aSxu xin 	register_pernet_subsys(&ip_rt_ops);
37673ee94372SNeil Horman 	register_pernet_subsys(&rt_genid_ops);
3768c3426b47SDavid S. Miller 	register_pernet_subsys(&ipv4_inetpeer_ops);
37691bcdca3fSTim Hansen 	return 0;
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
3772a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL
3773eeb61f71SAl Viro /*
3774eeb61f71SAl Viro  * We really need to sanitize the damn ipv4 init order, then all
3775eeb61f71SAl Viro  * this nonsense will go away.
3776eeb61f71SAl Viro  */
3777eeb61f71SAl Viro void __init ip_static_sysctl_init(void)
3778eeb61f71SAl Viro {
37794e5ca785SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
3780eeb61f71SAl Viro }
3781a1bc6eb4SAl Viro #endif
3782