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);
1355af198c3SEric Dumazet static void ipv4_negative_advice(struct sock *sk,
1365af198c3SEric Dumazet struct dst_entry *dst);
1371da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb);
1386700c270SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
139bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu,
140bd085ef6SHangbin Liu bool confirm_neigh);
1416700c270SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk,
1426700c270SDavid S. Miller struct sk_buff *skb);
143caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst);
1441da177e4SLinus Torvalds
ipv4_cow_metrics(struct dst_entry * dst,unsigned long old)14562fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
14662fa8a84SDavid S. Miller {
14731248731SDavid S. Miller WARN_ON(1);
14831248731SDavid S. Miller return NULL;
14962fa8a84SDavid S. Miller }
15062fa8a84SDavid S. Miller
151f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
152f894cbf8SDavid S. Miller struct sk_buff *skb,
153f894cbf8SDavid S. Miller const void *daddr);
15463fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr);
155d3aaeb38SDavid S. Miller
1561da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = {
1571da177e4SLinus Torvalds .family = AF_INET,
1581da177e4SLinus Torvalds .check = ipv4_dst_check,
1590dbaee3bSDavid S. Miller .default_advmss = ipv4_default_advmss,
160ebb762f2SSteffen Klassert .mtu = ipv4_mtu,
16162fa8a84SDavid S. Miller .cow_metrics = ipv4_cow_metrics,
162caacf05eSDavid S. Miller .destroy = ipv4_dst_destroy,
1631da177e4SLinus Torvalds .negative_advice = ipv4_negative_advice,
1641da177e4SLinus Torvalds .link_failure = ipv4_link_failure,
1651da177e4SLinus Torvalds .update_pmtu = ip_rt_update_pmtu,
166e47a185bSDavid S. Miller .redirect = ip_do_redirect,
167b92dacd4SEric W. Biederman .local_out = __ip_local_out,
168d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup,
16963fca65dSJulian Anastasov .confirm_neigh = ipv4_confirm_neigh,
1701da177e4SLinus Torvalds };
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds #define ECN_OR_COST(class) TC_PRIO_##class
1731da177e4SLinus Torvalds
1744839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = {
1751da177e4SLinus Torvalds TC_PRIO_BESTEFFORT,
1764a2b9c37SDan Siemon ECN_OR_COST(BESTEFFORT),
1771da177e4SLinus Torvalds TC_PRIO_BESTEFFORT,
1781da177e4SLinus Torvalds ECN_OR_COST(BESTEFFORT),
1791da177e4SLinus Torvalds TC_PRIO_BULK,
1801da177e4SLinus Torvalds ECN_OR_COST(BULK),
1811da177e4SLinus Torvalds TC_PRIO_BULK,
1821da177e4SLinus Torvalds ECN_OR_COST(BULK),
1831da177e4SLinus Torvalds TC_PRIO_INTERACTIVE,
1841da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE),
1851da177e4SLinus Torvalds TC_PRIO_INTERACTIVE,
1861da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE),
1871da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK,
1881da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK),
1891da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK,
1901da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK)
1911da177e4SLinus Torvalds };
192d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio);
1931da177e4SLinus Torvalds
1942f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
1953ed66e91SChristoph Lameter #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field)
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
rt_cache_seq_start(struct seq_file * seq,loff_t * pos)1981da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1991da177e4SLinus Torvalds {
20029e75252SEric Dumazet if (*pos)
20189aef892SDavid S. Miller return NULL;
20229e75252SEric Dumazet return SEQ_START_TOKEN;
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds
rt_cache_seq_next(struct seq_file * seq,void * v,loff_t * pos)2051da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds ++*pos;
20889aef892SDavid S. Miller return NULL;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds
rt_cache_seq_stop(struct seq_file * seq,void * v)2111da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds
rt_cache_seq_show(struct seq_file * seq,void * v)2151da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds if (v == SEQ_START_TOKEN)
2181da177e4SLinus Torvalds seq_printf(seq, "%-127s\n",
2191da177e4SLinus Torvalds "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
2201da177e4SLinus Torvalds "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
2211da177e4SLinus Torvalds "HHUptod\tSpecDst");
2221da177e4SLinus Torvalds return 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds
225f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = {
2261da177e4SLinus Torvalds .start = rt_cache_seq_start,
2271da177e4SLinus Torvalds .next = rt_cache_seq_next,
2281da177e4SLinus Torvalds .stop = rt_cache_seq_stop,
2291da177e4SLinus Torvalds .show = rt_cache_seq_show,
2301da177e4SLinus Torvalds };
2311da177e4SLinus Torvalds
rt_cpu_seq_start(struct seq_file * seq,loff_t * pos)2321da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds int cpu;
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds if (*pos == 0)
2371da177e4SLinus Torvalds return SEQ_START_TOKEN;
2381da177e4SLinus Torvalds
2390f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
2401da177e4SLinus Torvalds if (!cpu_possible(cpu))
2411da177e4SLinus Torvalds continue;
2421da177e4SLinus Torvalds *pos = cpu+1;
2432f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds return NULL;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds
rt_cpu_seq_next(struct seq_file * seq,void * v,loff_t * pos)2481da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds int cpu;
2511da177e4SLinus Torvalds
2520f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
2531da177e4SLinus Torvalds if (!cpu_possible(cpu))
2541da177e4SLinus Torvalds continue;
2551da177e4SLinus Torvalds *pos = cpu+1;
2562f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu);
2571da177e4SLinus Torvalds }
258a3ea8673SVasily Averin (*pos)++;
2591da177e4SLinus Torvalds return NULL;
2601da177e4SLinus Torvalds
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds
rt_cpu_seq_stop(struct seq_file * seq,void * v)2631da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds
rt_cpu_seq_show(struct seq_file * seq,void * v)2681da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds struct rt_cache_stat *st = v;
2711da177e4SLinus Torvalds
2721da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) {
2730547ffe6SYajun 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");
2741da177e4SLinus Torvalds return 0;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds
2770547ffe6SYajun Deng seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x "
2780547ffe6SYajun Deng "%08x %08x %08x %08x %08x %08x "
2790547ffe6SYajun Deng "%08x %08x %08x %08x\n",
280fc66f95cSEric Dumazet dst_entries_get_slow(&ipv4_dst_ops),
2810baf2b35SEric Dumazet 0, /* st->in_hit */
2821da177e4SLinus Torvalds st->in_slow_tot,
2831da177e4SLinus Torvalds st->in_slow_mc,
2841da177e4SLinus Torvalds st->in_no_route,
2851da177e4SLinus Torvalds st->in_brd,
2861da177e4SLinus Torvalds st->in_martian_dst,
2871da177e4SLinus Torvalds st->in_martian_src,
2881da177e4SLinus Torvalds
2890baf2b35SEric Dumazet 0, /* st->out_hit */
2901da177e4SLinus Torvalds st->out_slow_tot,
2911da177e4SLinus Torvalds st->out_slow_mc,
2921da177e4SLinus Torvalds
2930baf2b35SEric Dumazet 0, /* st->gc_total */
2940baf2b35SEric Dumazet 0, /* st->gc_ignored */
2950baf2b35SEric Dumazet 0, /* st->gc_goal_miss */
2960baf2b35SEric Dumazet 0, /* st->gc_dst_overflow */
2970baf2b35SEric Dumazet 0, /* st->in_hlist_search */
2980baf2b35SEric Dumazet 0 /* st->out_hlist_search */
2991da177e4SLinus Torvalds );
3001da177e4SLinus Torvalds return 0;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds
303f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = {
3041da177e4SLinus Torvalds .start = rt_cpu_seq_start,
3051da177e4SLinus Torvalds .next = rt_cpu_seq_next,
3061da177e4SLinus Torvalds .stop = rt_cpu_seq_stop,
3071da177e4SLinus Torvalds .show = rt_cpu_seq_show,
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds
310c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
rt_acct_proc_show(struct seq_file * m,void * v)311a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v)
31278c686e9SPavel Emelyanov {
313a661c419SAlexey Dobriyan struct ip_rt_acct *dst, *src;
314a661c419SAlexey Dobriyan unsigned int i, j;
31578c686e9SPavel Emelyanov
316a661c419SAlexey Dobriyan dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
317a661c419SAlexey Dobriyan if (!dst)
318a661c419SAlexey Dobriyan return -ENOMEM;
31978c686e9SPavel Emelyanov
320a661c419SAlexey Dobriyan for_each_possible_cpu(i) {
321a661c419SAlexey Dobriyan src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
322a661c419SAlexey Dobriyan for (j = 0; j < 256; j++) {
323a661c419SAlexey Dobriyan dst[j].o_bytes += src[j].o_bytes;
324a661c419SAlexey Dobriyan dst[j].o_packets += src[j].o_packets;
325a661c419SAlexey Dobriyan dst[j].i_bytes += src[j].i_bytes;
326a661c419SAlexey Dobriyan dst[j].i_packets += src[j].i_packets;
327a661c419SAlexey Dobriyan }
328a661c419SAlexey Dobriyan }
329a661c419SAlexey Dobriyan
330a661c419SAlexey Dobriyan seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
331a661c419SAlexey Dobriyan kfree(dst);
33278c686e9SPavel Emelyanov return 0;
33378c686e9SPavel Emelyanov }
33478c686e9SPavel Emelyanov #endif
335107f1634SPavel Emelyanov
ip_rt_do_proc_init(struct net * net)33673b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net)
337107f1634SPavel Emelyanov {
338107f1634SPavel Emelyanov struct proc_dir_entry *pde;
339107f1634SPavel Emelyanov
340f105f26eSYejune Deng pde = proc_create_seq("rt_cache", 0444, net->proc_net,
341f105f26eSYejune Deng &rt_cache_seq_ops);
342107f1634SPavel Emelyanov if (!pde)
343107f1634SPavel Emelyanov goto err1;
344107f1634SPavel Emelyanov
345f105f26eSYejune Deng pde = proc_create_seq("rt_cache", 0444, net->proc_net_stat,
346f105f26eSYejune Deng &rt_cpu_seq_ops);
347107f1634SPavel Emelyanov if (!pde)
348107f1634SPavel Emelyanov goto err2;
349107f1634SPavel Emelyanov
350c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3513f3942acSChristoph Hellwig pde = proc_create_single("rt_acct", 0, net->proc_net,
3523f3942acSChristoph Hellwig rt_acct_proc_show);
353107f1634SPavel Emelyanov if (!pde)
354107f1634SPavel Emelyanov goto err3;
355107f1634SPavel Emelyanov #endif
356107f1634SPavel Emelyanov return 0;
357107f1634SPavel Emelyanov
358c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
359107f1634SPavel Emelyanov err3:
360107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net_stat);
361107f1634SPavel Emelyanov #endif
362107f1634SPavel Emelyanov err2:
363107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net);
364107f1634SPavel Emelyanov err1:
365107f1634SPavel Emelyanov return -ENOMEM;
366107f1634SPavel Emelyanov }
36773b38711SDenis V. Lunev
ip_rt_do_proc_exit(struct net * net)36873b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net)
36973b38711SDenis V. Lunev {
37073b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net_stat);
37173b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net);
372c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
37373b38711SDenis V. Lunev remove_proc_entry("rt_acct", net->proc_net);
3740a931acfSAlexey Dobriyan #endif
37573b38711SDenis V. Lunev }
37673b38711SDenis V. Lunev
37773b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata = {
37873b38711SDenis V. Lunev .init = ip_rt_do_proc_init,
37973b38711SDenis V. Lunev .exit = ip_rt_do_proc_exit,
38073b38711SDenis V. Lunev };
38173b38711SDenis V. Lunev
ip_rt_proc_init(void)38273b38711SDenis V. Lunev static int __init ip_rt_proc_init(void)
38373b38711SDenis V. Lunev {
38473b38711SDenis V. Lunev return register_pernet_subsys(&ip_rt_proc_ops);
38573b38711SDenis V. Lunev }
38673b38711SDenis V. Lunev
387107f1634SPavel Emelyanov #else
ip_rt_proc_init(void)38873b38711SDenis V. Lunev static inline int ip_rt_proc_init(void)
389107f1634SPavel Emelyanov {
390107f1634SPavel Emelyanov return 0;
391107f1634SPavel Emelyanov }
3921da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
3931da177e4SLinus Torvalds
rt_is_expired(const struct rtable * rth)3944331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth)
395e84f84f2SDenis V. Lunev {
3961f778d7aSEric Dumazet bool res;
3971f778d7aSEric Dumazet
3981f778d7aSEric Dumazet rcu_read_lock();
3991f778d7aSEric Dumazet res = rth->rt_genid != rt_genid_ipv4(dev_net_rcu(rth->dst.dev));
4001f778d7aSEric Dumazet rcu_read_unlock();
4011f778d7aSEric Dumazet
4021f778d7aSEric Dumazet return res;
403e84f84f2SDenis V. Lunev }
404e84f84f2SDenis V. Lunev
rt_cache_flush(struct net * net)4054ccfe6d4SNicolas Dichtel void rt_cache_flush(struct net *net)
40629e75252SEric Dumazet {
407ca4c3fc2Sfan.du rt_genid_bump_ipv4(net);
40898376387SEric Dumazet }
40998376387SEric Dumazet
ipv4_neigh_lookup(const struct dst_entry * dst,struct sk_buff * skb,const void * daddr)410f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
411f894cbf8SDavid S. Miller struct sk_buff *skb,
412f894cbf8SDavid S. Miller const void *daddr)
4133769cffbSDavid Miller {
4141550c171SDavid Ahern const struct rtable *rt = container_of(dst, struct rtable, dst);
415d3aaeb38SDavid S. Miller struct net_device *dev = dst->dev;
4163769cffbSDavid Miller struct neighbour *n;
4173769cffbSDavid Miller
41809eed119SEric Dumazet rcu_read_lock();
419d3aaeb38SDavid S. Miller
4205c9f7c1dSDavid Ahern if (likely(rt->rt_gw_family == AF_INET)) {
4215c9f7c1dSDavid Ahern n = ip_neigh_gw4(dev, rt->rt_gw4);
4225c9f7c1dSDavid Ahern } else if (rt->rt_gw_family == AF_INET6) {
4235c9f7c1dSDavid Ahern n = ip_neigh_gw6(dev, &rt->rt_gw6);
4245c9f7c1dSDavid Ahern } else {
4255c9f7c1dSDavid Ahern __be32 pkey;
4265c9f7c1dSDavid Ahern
4275c9f7c1dSDavid Ahern pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
4285c9f7c1dSDavid Ahern n = ip_neigh_gw4(dev, pkey);
4295c9f7c1dSDavid Ahern }
4305c9f7c1dSDavid Ahern
431537de0c8SIdo Schimmel if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
4325c9f7c1dSDavid Ahern n = NULL;
4335c9f7c1dSDavid Ahern
43409eed119SEric Dumazet rcu_read_unlock();
4355c9f7c1dSDavid Ahern
436d3aaeb38SDavid S. Miller return n;
437d3aaeb38SDavid S. Miller }
438d3aaeb38SDavid S. Miller
ipv4_confirm_neigh(const struct dst_entry * dst,const void * daddr)43963fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
44063fca65dSJulian Anastasov {
4411550c171SDavid Ahern const struct rtable *rt = container_of(dst, struct rtable, dst);
44263fca65dSJulian Anastasov struct net_device *dev = dst->dev;
44363fca65dSJulian Anastasov const __be32 *pkey = daddr;
44463fca65dSJulian Anastasov
4456de9c055SDavid Ahern if (rt->rt_gw_family == AF_INET) {
4461550c171SDavid Ahern pkey = (const __be32 *)&rt->rt_gw4;
4476de9c055SDavid Ahern } else if (rt->rt_gw_family == AF_INET6) {
4486de9c055SDavid Ahern return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6);
4496de9c055SDavid Ahern } else if (!daddr ||
45063fca65dSJulian Anastasov (rt->rt_flags &
4516de9c055SDavid Ahern (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) {
45263fca65dSJulian Anastasov return;
4536de9c055SDavid Ahern }
45463fca65dSJulian Anastasov __ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
45563fca65dSJulian Anastasov }
45663fca65dSJulian Anastasov
457aa6dd211SEric Dumazet /* Hash tables of size 2048..262144 depending on RAM size.
458aa6dd211SEric Dumazet * Each bucket uses 8 bytes.
459aa6dd211SEric Dumazet */
460aa6dd211SEric Dumazet static u32 ip_idents_mask __read_mostly;
461355b590cSEric Dumazet static atomic_t *ip_idents __read_mostly;
462355b590cSEric Dumazet static u32 *ip_tstamps __read_mostly;
46304ca6973SEric Dumazet
46404ca6973SEric Dumazet /* In order to protect privacy, we add a perturbation to identifiers
46504ca6973SEric Dumazet * if one generator is seldom used. This makes hard for an attacker
46604ca6973SEric Dumazet * to infer how many packets were sent between two points in time.
46704ca6973SEric Dumazet */
ip_idents_reserve(u32 hash,int segs)46847ed9442SDavid Ahern static u32 ip_idents_reserve(u32 hash, int segs)
46904ca6973SEric Dumazet {
470aa6dd211SEric Dumazet u32 bucket, old, now = (u32)jiffies;
471aa6dd211SEric Dumazet atomic_t *p_id;
472aa6dd211SEric Dumazet u32 *p_tstamp;
473a6211caaSYuqi Jin u32 delta = 0;
47404ca6973SEric Dumazet
475aa6dd211SEric Dumazet bucket = hash & ip_idents_mask;
476aa6dd211SEric Dumazet p_tstamp = ip_tstamps + bucket;
477aa6dd211SEric Dumazet p_id = ip_idents + bucket;
478aa6dd211SEric Dumazet old = READ_ONCE(*p_tstamp);
479aa6dd211SEric Dumazet
480355b590cSEric Dumazet if (old != now && cmpxchg(p_tstamp, old, now) == old)
4818032bf12SJason A. Donenfeld delta = get_random_u32_below(now - old);
48204ca6973SEric Dumazet
483a6211caaSYuqi Jin /* If UBSAN reports an error there, please make sure your compiler
484a6211caaSYuqi Jin * supports -fno-strict-overflow before reporting it that was a bug
485a6211caaSYuqi Jin * in UBSAN, and it has been fixed in GCC-8.
486a6211caaSYuqi Jin */
487a6211caaSYuqi Jin return atomic_add_return(segs + delta, p_id) - segs;
48804ca6973SEric Dumazet }
48973f156a6SEric Dumazet
__ip_select_ident(struct net * net,struct iphdr * iph,int segs)490b6a7719aSHannes Frederic Sowa void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
4911da177e4SLinus Torvalds {
49273f156a6SEric Dumazet u32 hash, id;
4931da177e4SLinus Torvalds
494df453700SEric Dumazet /* Note the following code is not safe, but this is okay. */
495df453700SEric Dumazet if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key)))
496df453700SEric Dumazet get_random_bytes(&net->ipv4.ip_id_key,
497df453700SEric Dumazet sizeof(net->ipv4.ip_id_key));
4981da177e4SLinus Torvalds
499df453700SEric Dumazet hash = siphash_3u32((__force u32)iph->daddr,
50004ca6973SEric Dumazet (__force u32)iph->saddr,
501df453700SEric Dumazet iph->protocol,
502df453700SEric Dumazet &net->ipv4.ip_id_key);
50373f156a6SEric Dumazet id = ip_idents_reserve(hash, segs);
50473f156a6SEric Dumazet iph->id = htons(id);
5051da177e4SLinus Torvalds }
5064bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident);
5071da177e4SLinus Torvalds
ip_rt_fix_tos(struct flowi4 * fl4)508544b4dd5SGuillaume Nault static void ip_rt_fix_tos(struct flowi4 *fl4)
509544b4dd5SGuillaume Nault {
510544b4dd5SGuillaume Nault __u8 tos = RT_FL_TOS(fl4);
511544b4dd5SGuillaume Nault
512544b4dd5SGuillaume Nault fl4->flowi4_tos = tos & IPTOS_RT_MASK;
51316a28267SGuillaume Nault if (tos & RTO_ONLINK)
51416a28267SGuillaume Nault fl4->flowi4_scope = RT_SCOPE_LINK;
515544b4dd5SGuillaume Nault }
516544b4dd5SGuillaume Nault
__build_flow_key(const struct net * net,struct flowi4 * fl4,const struct sock * sk,const struct iphdr * iph,int oif,__u8 tos,u8 prot,u32 mark,int flow_flags)517e2d118a1SLorenzo Colitti static void __build_flow_key(const struct net *net, struct flowi4 *fl4,
518b1ad4138SGuillaume Nault const struct sock *sk, const struct iphdr *iph,
519b1ad4138SGuillaume Nault int oif, __u8 tos, u8 prot, u32 mark,
520b1ad4138SGuillaume Nault int flow_flags)
5214895c771SDavid S. Miller {
522b1ad4138SGuillaume Nault __u8 scope = RT_SCOPE_UNIVERSE;
523b1ad4138SGuillaume Nault
5244895c771SDavid S. Miller if (sk) {
5254895c771SDavid S. Miller oif = sk->sk_bound_dev_if;
5263c5b4d69SEric Dumazet mark = READ_ONCE(sk->sk_mark);
527b1ad4138SGuillaume Nault tos = ip_sock_rt_tos(sk);
528b1ad4138SGuillaume Nault scope = ip_sock_rt_scope(sk);
529cafbe182SEric Dumazet prot = inet_test_bit(HDRINCL, sk) ? IPPROTO_RAW :
530cafbe182SEric Dumazet sk->sk_protocol;
5314895c771SDavid S. Miller }
532b1ad4138SGuillaume Nault
533b1ad4138SGuillaume Nault flowi4_init_output(fl4, oif, mark, tos & IPTOS_RT_MASK, scope,
534b1ad4138SGuillaume Nault prot, flow_flags, iph->daddr, iph->saddr, 0, 0,
535e2d118a1SLorenzo Colitti sock_net_uid(net, sk));
5364895c771SDavid S. Miller }
5374895c771SDavid S. Miller
build_skb_flow_key(struct flowi4 * fl4,const struct sk_buff * skb,const struct sock * sk)5385abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
5395abf7f7eSEric Dumazet const struct sock *sk)
5404895c771SDavid S. Miller {
541d109e61bSLorenzo Colitti const struct net *net = dev_net(skb->dev);
5424895c771SDavid S. Miller const struct iphdr *iph = ip_hdr(skb);
5434895c771SDavid S. Miller int oif = skb->dev->ifindex;
5444895c771SDavid S. Miller u8 prot = iph->protocol;
5454895c771SDavid S. Miller u32 mark = skb->mark;
546b1ad4138SGuillaume Nault __u8 tos = iph->tos;
5474895c771SDavid S. Miller
548d109e61bSLorenzo Colitti __build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0);
5494895c771SDavid S. Miller }
5504895c771SDavid S. Miller
build_sk_flow_key(struct flowi4 * fl4,const struct sock * sk)5515abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
5524895c771SDavid S. Miller {
5534895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk);
5545abf7f7eSEric Dumazet const struct ip_options_rcu *inet_opt;
5554895c771SDavid S. Miller __be32 daddr = inet->inet_daddr;
5564895c771SDavid S. Miller
5574895c771SDavid S. Miller rcu_read_lock();
5584895c771SDavid S. Miller inet_opt = rcu_dereference(inet->inet_opt);
5594895c771SDavid S. Miller if (inet_opt && inet_opt->opt.srr)
5604895c771SDavid S. Miller daddr = inet_opt->opt.faddr;
5613c5b4d69SEric Dumazet flowi4_init_output(fl4, sk->sk_bound_dev_if, READ_ONCE(sk->sk_mark),
562b1ad4138SGuillaume Nault ip_sock_rt_tos(sk) & IPTOS_RT_MASK,
563b1ad4138SGuillaume Nault ip_sock_rt_scope(sk),
564cafbe182SEric Dumazet inet_test_bit(HDRINCL, sk) ?
565cafbe182SEric Dumazet IPPROTO_RAW : sk->sk_protocol,
5664895c771SDavid S. Miller inet_sk_flowi_flags(sk),
567e2d118a1SLorenzo Colitti daddr, inet->inet_saddr, 0, 0, sk->sk_uid);
5684895c771SDavid S. Miller rcu_read_unlock();
5694895c771SDavid S. Miller }
5704895c771SDavid S. Miller
ip_rt_build_flow_key(struct flowi4 * fl4,const struct sock * sk,const struct sk_buff * skb)5715abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
5725abf7f7eSEric Dumazet const struct sk_buff *skb)
5734895c771SDavid S. Miller {
5744895c771SDavid S. Miller if (skb)
5754895c771SDavid S. Miller build_skb_flow_key(fl4, skb, sk);
5764895c771SDavid S. Miller else
5774895c771SDavid S. Miller build_sk_flow_key(fl4, sk);
5784895c771SDavid S. Miller }
5794895c771SDavid S. Miller
580c5038a83SDavid S. Miller static DEFINE_SPINLOCK(fnhe_lock);
5814895c771SDavid S. Miller
fnhe_flush_routes(struct fib_nh_exception * fnhe)5822ffae99dSTimo Teräs static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
5832ffae99dSTimo Teräs {
5842ffae99dSTimo Teräs struct rtable *rt;
5852ffae99dSTimo Teräs
5862ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_input);
5872ffae99dSTimo Teräs if (rt) {
5882ffae99dSTimo Teräs RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
58995c47f9cSWei Wang dst_dev_put(&rt->dst);
5900830106cSWei Wang dst_release(&rt->dst);
5912ffae99dSTimo Teräs }
5922ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_output);
5932ffae99dSTimo Teräs if (rt) {
5942ffae99dSTimo Teräs RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
59595c47f9cSWei Wang dst_dev_put(&rt->dst);
5960830106cSWei Wang dst_release(&rt->dst);
5972ffae99dSTimo Teräs }
5982ffae99dSTimo Teräs }
5992ffae99dSTimo Teräs
fnhe_remove_oldest(struct fnhe_hash_bucket * hash)60067d6d681SEric Dumazet static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
6014895c771SDavid S. Miller {
60267d6d681SEric Dumazet struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
60367d6d681SEric Dumazet struct fib_nh_exception *fnhe, *oldest = NULL;
6044895c771SDavid S. Miller
60567d6d681SEric Dumazet for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
60667d6d681SEric Dumazet fnhe = rcu_dereference_protected(*fnhe_p,
60767d6d681SEric Dumazet lockdep_is_held(&fnhe_lock));
60867d6d681SEric Dumazet if (!fnhe)
60967d6d681SEric Dumazet break;
61067d6d681SEric Dumazet if (!oldest ||
61167d6d681SEric Dumazet time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
6124895c771SDavid S. Miller oldest = fnhe;
61367d6d681SEric Dumazet oldest_p = fnhe_p;
61467d6d681SEric Dumazet }
6154895c771SDavid S. Miller }
6162ffae99dSTimo Teräs fnhe_flush_routes(oldest);
61767d6d681SEric Dumazet *oldest_p = oldest->fnhe_next;
61867d6d681SEric Dumazet kfree_rcu(oldest, rcu);
6194895c771SDavid S. Miller }
6204895c771SDavid S. Miller
fnhe_hashfun(__be32 daddr)6216457378fSEric Dumazet static u32 fnhe_hashfun(__be32 daddr)
622d3a25c98SDavid S. Miller {
62349ecc2e9SEric Dumazet static siphash_aligned_key_t fnhe_hash_key;
6246457378fSEric Dumazet u64 hval;
625d3a25c98SDavid S. Miller
6266457378fSEric Dumazet net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key));
6276457378fSEric Dumazet hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key);
6286457378fSEric Dumazet return hash_64(hval, FNHE_HASH_SHIFT);
629d3a25c98SDavid S. Miller }
630d3a25c98SDavid S. Miller
fill_route_from_fnhe(struct rtable * rt,struct fib_nh_exception * fnhe)631387aa65aSTimo Teräs static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
632387aa65aSTimo Teräs {
633387aa65aSTimo Teräs rt->rt_pmtu = fnhe->fnhe_pmtu;
634d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = fnhe->fnhe_mtu_locked;
635387aa65aSTimo Teräs rt->dst.expires = fnhe->fnhe_expires;
636387aa65aSTimo Teräs
637387aa65aSTimo Teräs if (fnhe->fnhe_gw) {
638387aa65aSTimo Teräs rt->rt_flags |= RTCF_REDIRECTED;
63977d5bc7eSDavid Ahern rt->rt_uses_gateway = 1;
6401550c171SDavid Ahern rt->rt_gw_family = AF_INET;
6411550c171SDavid Ahern rt->rt_gw4 = fnhe->fnhe_gw;
642387aa65aSTimo Teräs }
643387aa65aSTimo Teräs }
644387aa65aSTimo Teräs
update_or_create_fnhe(struct fib_nh_common * nhc,__be32 daddr,__be32 gw,u32 pmtu,bool lock,unsigned long expires)645a5995e71SDavid Ahern static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr,
646a5995e71SDavid Ahern __be32 gw, u32 pmtu, bool lock,
647a5995e71SDavid Ahern unsigned long expires)
6484895c771SDavid S. Miller {
649aee06da6SJulian Anastasov struct fnhe_hash_bucket *hash;
6504895c771SDavid S. Miller struct fib_nh_exception *fnhe;
651387aa65aSTimo Teräs struct rtable *rt;
652cebe84c6SXin Long u32 genid, hval;
653387aa65aSTimo Teräs unsigned int i;
6544895c771SDavid S. Miller int depth;
655cebe84c6SXin Long
656a5995e71SDavid Ahern genid = fnhe_genid(dev_net(nhc->nhc_dev));
657cebe84c6SXin Long hval = fnhe_hashfun(daddr);
6584895c771SDavid S. Miller
659c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock);
660aee06da6SJulian Anastasov
661a5995e71SDavid Ahern hash = rcu_dereference(nhc->nhc_exceptions);
6624895c771SDavid S. Miller if (!hash) {
6636396bb22SKees Cook hash = kcalloc(FNHE_HASH_SIZE, sizeof(*hash), GFP_ATOMIC);
6644895c771SDavid S. Miller if (!hash)
665aee06da6SJulian Anastasov goto out_unlock;
666a5995e71SDavid Ahern rcu_assign_pointer(nhc->nhc_exceptions, hash);
6674895c771SDavid S. Miller }
6684895c771SDavid S. Miller
6694895c771SDavid S. Miller hash += hval;
6704895c771SDavid S. Miller
6714895c771SDavid S. Miller depth = 0;
6724895c771SDavid S. Miller for (fnhe = rcu_dereference(hash->chain); fnhe;
6734895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) {
6744895c771SDavid S. Miller if (fnhe->fnhe_daddr == daddr)
675aee06da6SJulian Anastasov break;
6764895c771SDavid S. Miller depth++;
6774895c771SDavid S. Miller }
6784895c771SDavid S. Miller
679aee06da6SJulian Anastasov if (fnhe) {
680cebe84c6SXin Long if (fnhe->fnhe_genid != genid)
681cebe84c6SXin Long fnhe->fnhe_genid = genid;
682aee06da6SJulian Anastasov if (gw)
683aee06da6SJulian Anastasov fnhe->fnhe_gw = gw;
684d52e5a7eSSabrina Dubroca if (pmtu) {
685aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu;
686d52e5a7eSSabrina Dubroca fnhe->fnhe_mtu_locked = lock;
687d52e5a7eSSabrina Dubroca }
688387aa65aSTimo Teräs fnhe->fnhe_expires = max(1UL, expires);
689387aa65aSTimo Teräs /* Update all cached dsts too */
6902ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_input);
6912ffae99dSTimo Teräs if (rt)
6922ffae99dSTimo Teräs fill_route_from_fnhe(rt, fnhe);
6932ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_output);
694387aa65aSTimo Teräs if (rt)
695387aa65aSTimo Teräs fill_route_from_fnhe(rt, fnhe);
696aee06da6SJulian Anastasov } else {
69767d6d681SEric Dumazet /* Randomize max depth to avoid some side channels attacks. */
69867d6d681SEric Dumazet int max_depth = FNHE_RECLAIM_DEPTH +
6998032bf12SJason A. Donenfeld get_random_u32_below(FNHE_RECLAIM_DEPTH);
70067d6d681SEric Dumazet
70167d6d681SEric Dumazet while (depth > max_depth) {
70267d6d681SEric Dumazet fnhe_remove_oldest(hash);
70367d6d681SEric Dumazet depth--;
70467d6d681SEric Dumazet }
70567d6d681SEric Dumazet
7064895c771SDavid S. Miller fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
7074895c771SDavid S. Miller if (!fnhe)
708aee06da6SJulian Anastasov goto out_unlock;
7094895c771SDavid S. Miller
7104895c771SDavid S. Miller fnhe->fnhe_next = hash->chain;
71167d6d681SEric Dumazet
712cebe84c6SXin Long fnhe->fnhe_genid = genid;
7134895c771SDavid S. Miller fnhe->fnhe_daddr = daddr;
714aee06da6SJulian Anastasov fnhe->fnhe_gw = gw;
715aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu;
716d52e5a7eSSabrina Dubroca fnhe->fnhe_mtu_locked = lock;
71794720e3aSJulian Anastasov fnhe->fnhe_expires = max(1UL, expires);
718387aa65aSTimo Teräs
71967d6d681SEric Dumazet rcu_assign_pointer(hash->chain, fnhe);
72067d6d681SEric Dumazet
721387aa65aSTimo Teräs /* Exception created; mark the cached routes for the nexthop
722387aa65aSTimo Teräs * stale, so anyone caching it rechecks if this exception
723387aa65aSTimo Teräs * applies to them.
724387aa65aSTimo Teräs */
7250f457a36SDavid Ahern rt = rcu_dereference(nhc->nhc_rth_input);
7262ffae99dSTimo Teräs if (rt)
7272ffae99dSTimo Teräs rt->dst.obsolete = DST_OBSOLETE_KILL;
7282ffae99dSTimo Teräs
729387aa65aSTimo Teräs for_each_possible_cpu(i) {
730387aa65aSTimo Teräs struct rtable __rcu **prt;
7316ad08600SShubhankar Kuranagatti
7320f457a36SDavid Ahern prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i);
733387aa65aSTimo Teräs rt = rcu_dereference(*prt);
734387aa65aSTimo Teräs if (rt)
735387aa65aSTimo Teräs rt->dst.obsolete = DST_OBSOLETE_KILL;
736387aa65aSTimo Teräs }
737aee06da6SJulian Anastasov }
738aee06da6SJulian Anastasov
7394895c771SDavid S. Miller fnhe->fnhe_stamp = jiffies;
740aee06da6SJulian Anastasov
741aee06da6SJulian Anastasov out_unlock:
742c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock);
7434895c771SDavid S. Miller }
7444895c771SDavid S. Miller
__ip_do_redirect(struct rtable * rt,struct sk_buff * skb,struct flowi4 * fl4,bool kill_route)745ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
746ceb33206SDavid S. Miller bool kill_route)
7471da177e4SLinus Torvalds {
748e47a185bSDavid S. Miller __be32 new_gw = icmp_hdr(skb)->un.gateway;
74994206125SDavid S. Miller __be32 old_gw = ip_hdr(skb)->saddr;
750e47a185bSDavid S. Miller struct net_device *dev = skb->dev;
751e47a185bSDavid S. Miller struct in_device *in_dev;
7524895c771SDavid S. Miller struct fib_result res;
753e47a185bSDavid S. Miller struct neighbour *n;
754317805b8SDenis V. Lunev struct net *net;
7551da177e4SLinus Torvalds
75694206125SDavid S. Miller switch (icmp_hdr(skb)->code & 7) {
75794206125SDavid S. Miller case ICMP_REDIR_NET:
75894206125SDavid S. Miller case ICMP_REDIR_NETTOS:
75994206125SDavid S. Miller case ICMP_REDIR_HOST:
76094206125SDavid S. Miller case ICMP_REDIR_HOSTTOS:
76194206125SDavid S. Miller break;
76294206125SDavid S. Miller
76394206125SDavid S. Miller default:
76494206125SDavid S. Miller return;
76594206125SDavid S. Miller }
76694206125SDavid S. Miller
7671550c171SDavid Ahern if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw)
768e47a185bSDavid S. Miller return;
769e47a185bSDavid S. Miller
770e47a185bSDavid S. Miller in_dev = __in_dev_get_rcu(dev);
771e47a185bSDavid S. Miller if (!in_dev)
772e47a185bSDavid S. Miller return;
773e47a185bSDavid S. Miller
774c346dca1SYOSHIFUJI Hideaki net = dev_net(dev);
7759d4fb27dSJoe Perches if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
7769d4fb27dSJoe Perches ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
7779d4fb27dSJoe Perches ipv4_is_zeronet(new_gw))
7781da177e4SLinus Torvalds goto reject_redirect;
7791da177e4SLinus Torvalds
7801da177e4SLinus Torvalds if (!IN_DEV_SHARED_MEDIA(in_dev)) {
7811da177e4SLinus Torvalds if (!inet_addr_onlink(in_dev, new_gw, old_gw))
7821da177e4SLinus Torvalds goto reject_redirect;
7831da177e4SLinus Torvalds if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
7841da177e4SLinus Torvalds goto reject_redirect;
7851da177e4SLinus Torvalds } else {
786317805b8SDenis V. Lunev if (inet_addr_type(net, new_gw) != RTN_UNICAST)
7871da177e4SLinus Torvalds goto reject_redirect;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds
79079b5228eSKunwu Chan n = __ipv4_neigh_lookup(rt->dst.dev, (__force u32)new_gw);
791969447f2SStephen Suryaputra Lin if (!n)
792969447f2SStephen Suryaputra Lin n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev);
7932c1a4311SWANG Cong if (!IS_ERR(n)) {
794b071af52SEric Dumazet if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
795e47a185bSDavid S. Miller neigh_event_send(n, NULL);
796e47a185bSDavid S. Miller } else {
7970eeb075fSAndy Gospodarek if (fib_lookup(net, fl4, &res, 0) == 0) {
7982fbc6e89SDavid Ahern struct fib_nh_common *nhc;
7994895c771SDavid S. Miller
8002fbc6e89SDavid Ahern fib_select_path(net, &res, fl4, skb);
8012fbc6e89SDavid Ahern nhc = FIB_RES_NHC(res);
802a5995e71SDavid Ahern update_or_create_fnhe(nhc, fl4->daddr, new_gw,
803d52e5a7eSSabrina Dubroca 0, false,
804d52e5a7eSSabrina Dubroca jiffies + ip_rt_gc_timeout);
8054895c771SDavid S. Miller }
806ceb33206SDavid S. Miller if (kill_route)
807ceb33206SDavid S. Miller rt->dst.obsolete = DST_OBSOLETE_KILL;
808e47a185bSDavid S. Miller call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
809e47a185bSDavid S. Miller }
810e47a185bSDavid S. Miller neigh_release(n);
811e47a185bSDavid S. Miller }
812e47a185bSDavid S. Miller return;
813e47a185bSDavid S. Miller
814e47a185bSDavid S. Miller reject_redirect:
815e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE
81699ee038dSDavid S. Miller if (IN_DEV_LOG_MARTIANS(in_dev)) {
81799ee038dSDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data;
81899ee038dSDavid S. Miller __be32 daddr = iph->daddr;
81999ee038dSDavid S. Miller __be32 saddr = iph->saddr;
82099ee038dSDavid S. Miller
821e47a185bSDavid S. Miller net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
822e47a185bSDavid S. Miller " Advised path = %pI4 -> %pI4\n",
823e47a185bSDavid S. Miller &old_gw, dev->name, &new_gw,
824e47a185bSDavid S. Miller &saddr, &daddr);
82599ee038dSDavid S. Miller }
826e47a185bSDavid S. Miller #endif
827e47a185bSDavid S. Miller ;
828e47a185bSDavid S. Miller }
829e47a185bSDavid S. Miller
ip_do_redirect(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb)8304895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
8314895c771SDavid S. Miller {
8324895c771SDavid S. Miller struct rtable *rt;
8334895c771SDavid S. Miller struct flowi4 fl4;
834f96ef988SMichal Kubecek const struct iphdr *iph = (const struct iphdr *) skb->data;
8357d995694SLorenzo Colitti struct net *net = dev_net(skb->dev);
836f96ef988SMichal Kubecek int oif = skb->dev->ifindex;
837f96ef988SMichal Kubecek u8 prot = iph->protocol;
838f96ef988SMichal Kubecek u32 mark = skb->mark;
839b1ad4138SGuillaume Nault __u8 tos = iph->tos;
8404895c771SDavid S. Miller
8414895c771SDavid S. Miller rt = (struct rtable *) dst;
8424895c771SDavid S. Miller
8437d995694SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0);
844ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, true);
8454895c771SDavid S. Miller }
8464895c771SDavid S. Miller
ipv4_negative_advice(struct sock * sk,struct dst_entry * dst)8475af198c3SEric Dumazet static void ipv4_negative_advice(struct sock *sk,
8485af198c3SEric Dumazet struct dst_entry *dst)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst;
8511da177e4SLinus Torvalds
8525af198c3SEric Dumazet if ((dst->obsolete > 0) ||
8535af198c3SEric Dumazet (rt->rt_flags & RTCF_REDIRECTED) ||
8545af198c3SEric Dumazet rt->dst.expires)
8555af198c3SEric Dumazet sk_dst_reset(sk);
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
ip_rt_send_redirect(struct sk_buff * skb)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);
8911da177e4SLinus Torvalds
8921d861aa4SDavid S. Miller net = dev_net(rt->dst.dev);
893fdaa6b3cSEric Dumazet peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif);
89492d86829SDavid S. Miller if (!peer) {
895280fb099SEric Dumazet rcu_read_unlock();
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;
914280fb099SEric Dumazet goto out_unlock;
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;
9295fd0b8b4SArnd Bergmann if (IS_ENABLED(CONFIG_IP_ROUTE_VERBOSE) && log_martians &&
930b406472bSPaolo Abeni peer->n_redirects == ip_rt_redirect_number)
931e87cc472SJoe Perches net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
93292101b3bSDavid S. Miller &ip_hdr(skb)->saddr, inet_iif(skb),
933e81da0e1SJulian Anastasov &ip_hdr(skb)->daddr, &gw);
9341da177e4SLinus Torvalds }
935280fb099SEric Dumazet out_unlock:
936280fb099SEric Dumazet rcu_read_unlock();
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds
ip_error(struct sk_buff * skb)9391da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb)
9401da177e4SLinus Torvalds {
941511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb);
942e2c0dc1fSStephen Suryaputra struct net_device *dev = skb->dev;
943e2c0dc1fSStephen Suryaputra struct in_device *in_dev;
94492d86829SDavid S. Miller struct inet_peer *peer;
9451da177e4SLinus Torvalds unsigned long now;
946251da413SDavid S. Miller struct net *net;
947c4eb6641SMenglong Dong SKB_DR(reason);
94892d86829SDavid S. Miller bool send;
9491da177e4SLinus Torvalds int code;
9501da177e4SLinus Torvalds
951e2c0dc1fSStephen Suryaputra if (netif_is_l3_master(skb->dev)) {
952e2c0dc1fSStephen Suryaputra dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
953e2c0dc1fSStephen Suryaputra if (!dev)
954e2c0dc1fSStephen Suryaputra goto out;
955e2c0dc1fSStephen Suryaputra }
956e2c0dc1fSStephen Suryaputra
957e2c0dc1fSStephen Suryaputra in_dev = __in_dev_get_rcu(dev);
958e2c0dc1fSStephen Suryaputra
959381c759dSEric W. Biederman /* IP on this device is disabled. */
960381c759dSEric W. Biederman if (!in_dev)
961381c759dSEric W. Biederman goto out;
962381c759dSEric W. Biederman
963251da413SDavid S. Miller net = dev_net(rt->dst.dev);
964251da413SDavid S. Miller if (!IN_DEV_FORWARD(in_dev)) {
965251da413SDavid S. Miller switch (rt->dst.error) {
966251da413SDavid S. Miller case EHOSTUNREACH:
967c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INADDRERRORS);
968b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS);
969251da413SDavid S. Miller break;
970251da413SDavid S. Miller
971251da413SDavid S. Miller case ENETUNREACH:
972c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INNOROUTES);
973b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
974251da413SDavid S. Miller break;
975251da413SDavid S. Miller }
976251da413SDavid S. Miller goto out;
977251da413SDavid S. Miller }
978251da413SDavid S. Miller
979d8d1f30bSChangli Gao switch (rt->dst.error) {
9801da177e4SLinus Torvalds case EINVAL:
9811da177e4SLinus Torvalds default:
9821da177e4SLinus Torvalds goto out;
9831da177e4SLinus Torvalds case EHOSTUNREACH:
9841da177e4SLinus Torvalds code = ICMP_HOST_UNREACH;
9851da177e4SLinus Torvalds break;
9861da177e4SLinus Torvalds case ENETUNREACH:
9871da177e4SLinus Torvalds code = ICMP_NET_UNREACH;
988c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INNOROUTES);
989b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
9901da177e4SLinus Torvalds break;
9911da177e4SLinus Torvalds case EACCES:
9921da177e4SLinus Torvalds code = ICMP_PKT_FILTERED;
9931da177e4SLinus Torvalds break;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds
996280fb099SEric Dumazet rcu_read_lock();
997192132b9SDavid Ahern peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr,
998280fb099SEric Dumazet l3mdev_master_ifindex_rcu(skb->dev));
99992d86829SDavid S. Miller send = true;
100092d86829SDavid S. Miller if (peer) {
10011da177e4SLinus Torvalds now = jiffies;
100292d86829SDavid S. Miller peer->rate_tokens += now - peer->rate_last;
100392d86829SDavid S. Miller if (peer->rate_tokens > ip_rt_error_burst)
100492d86829SDavid S. Miller peer->rate_tokens = ip_rt_error_burst;
100592d86829SDavid S. Miller peer->rate_last = now;
100692d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_error_cost)
100792d86829SDavid S. Miller peer->rate_tokens -= ip_rt_error_cost;
100892d86829SDavid S. Miller else
100992d86829SDavid S. Miller send = false;
10101da177e4SLinus Torvalds }
1011280fb099SEric Dumazet rcu_read_unlock();
1012280fb099SEric Dumazet
101392d86829SDavid S. Miller if (send)
101492d86829SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
10151da177e4SLinus Torvalds
1016c4eb6641SMenglong Dong out: kfree_skb_reason(skb, reason);
10171da177e4SLinus Torvalds return 0;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds
__ip_rt_update_pmtu(struct rtable * rt,struct flowi4 * fl4,u32 mtu)1020d851c12bSSteffen Klassert static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
10211da177e4SLinus Torvalds {
1022d851c12bSSteffen Klassert struct dst_entry *dst = &rt->dst;
10234895c771SDavid S. Miller struct fib_result res;
1024d52e5a7eSSabrina Dubroca bool lock = false;
1025*9b1766d1SEric Dumazet struct net *net;
10268b4510d7SMiaohe Lin u32 old_mtu;
10272c8cec5cSDavid S. Miller
1028d52e5a7eSSabrina Dubroca if (ip_mtu_locked(dst))
1029fa1e492aSSteffen Klassert return;
1030fa1e492aSSteffen Klassert
10318b4510d7SMiaohe Lin old_mtu = ipv4_mtu(dst);
103228d35bcdSSabrina Dubroca if (old_mtu < mtu)
10333cdaa5beSLi Wei return;
10343cdaa5beSLi Wei
1035*9b1766d1SEric Dumazet rcu_read_lock();
1036*9b1766d1SEric Dumazet net = dev_net_rcu(dst->dev);
10371de6b15aSxu xin if (mtu < net->ipv4.ip_rt_min_pmtu) {
1038d52e5a7eSSabrina Dubroca lock = true;
10391de6b15aSxu xin mtu = min(old_mtu, net->ipv4.ip_rt_min_pmtu);
1040d52e5a7eSSabrina Dubroca }
104146af3180SHiroaki SHIMODA
104228d35bcdSSabrina Dubroca if (rt->rt_pmtu == mtu && !lock &&
10431135fad2Sxu xin time_before(jiffies, dst->expires - net->ipv4.ip_rt_mtu_expires / 2))
1044*9b1766d1SEric Dumazet goto out;
1045f016229eSTimo Teräs
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);
10503bed2abaSVladimir Vdovin #ifdef CONFIG_IP_ROUTE_MULTIPATH
10513bed2abaSVladimir Vdovin if (fib_info_num_path(res.fi) > 1) {
10523bed2abaSVladimir Vdovin int nhsel;
10533bed2abaSVladimir Vdovin
10543bed2abaSVladimir Vdovin for (nhsel = 0; nhsel < fib_info_num_path(res.fi); nhsel++) {
10553bed2abaSVladimir Vdovin nhc = fib_info_nhc(res.fi, nhsel);
10563bed2abaSVladimir Vdovin update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
10573bed2abaSVladimir Vdovin jiffies + net->ipv4.ip_rt_mtu_expires);
10583bed2abaSVladimir Vdovin }
1059*9b1766d1SEric Dumazet goto out;
10603bed2abaSVladimir Vdovin }
10613bed2abaSVladimir Vdovin #endif /* CONFIG_IP_ROUTE_MULTIPATH */
10622fbc6e89SDavid Ahern nhc = FIB_RES_NHC(res);
1063a5995e71SDavid Ahern update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
10641135fad2Sxu xin jiffies + net->ipv4.ip_rt_mtu_expires);
10654895c771SDavid S. Miller }
1066*9b1766d1SEric Dumazet out:
1067c5ae7d41SEric Dumazet rcu_read_unlock();
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds
ip_rt_update_pmtu(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb,u32 mtu,bool confirm_neigh)10704895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1071bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu,
1072bd085ef6SHangbin Liu bool confirm_neigh)
10734895c771SDavid S. Miller {
10744895c771SDavid S. Miller struct rtable *rt = (struct rtable *) dst;
10754895c771SDavid S. Miller struct flowi4 fl4;
10764895c771SDavid S. Miller
10774895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb);
1078df23bb18SStefano Brivio
1079df23bb18SStefano Brivio /* Don't make lookup fail for bridged encapsulations */
1080df23bb18SStefano Brivio if (skb && netif_is_any_bridge_port(skb->dev))
1081df23bb18SStefano Brivio fl4.flowi4_oif = 0;
1082df23bb18SStefano Brivio
1083d851c12bSSteffen Klassert __ip_rt_update_pmtu(rt, &fl4, mtu);
10844895c771SDavid S. Miller }
10854895c771SDavid S. Miller
ipv4_update_pmtu(struct sk_buff * skb,struct net * net,u32 mtu,int oif,u8 protocol)108636393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
1087d888f396SMaciej Żenczykowski int oif, u8 protocol)
108836393395SDavid S. Miller {
108936393395SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data;
109036393395SDavid S. Miller struct flowi4 fl4;
109136393395SDavid S. Miller struct rtable *rt;
1092d888f396SMaciej Żenczykowski u32 mark = IP4_REPLY_MARK(net, skb->mark);
10931b3c61dcSLorenzo Colitti
1094b1ad4138SGuillaume Nault __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, mark,
1095b1ad4138SGuillaume Nault 0);
109636393395SDavid S. Miller rt = __ip_route_output_key(net, &fl4);
109736393395SDavid S. Miller if (!IS_ERR(rt)) {
10984895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu);
109936393395SDavid S. Miller ip_rt_put(rt);
110036393395SDavid S. Miller }
110136393395SDavid S. Miller }
110236393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
110336393395SDavid S. Miller
__ipv4_sk_update_pmtu(struct sk_buff * skb,struct sock * sk,u32 mtu)11049cb3a50cSSteffen Klassert static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
110536393395SDavid S. Miller {
11064895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data;
11074895c771SDavid S. Miller struct flowi4 fl4;
11084895c771SDavid S. Miller struct rtable *rt;
110936393395SDavid S. Miller
1110e2d118a1SLorenzo Colitti __build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0);
11111b3c61dcSLorenzo Colitti
11121b3c61dcSLorenzo Colitti if (!fl4.flowi4_mark)
11131b3c61dcSLorenzo Colitti fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark);
11141b3c61dcSLorenzo Colitti
11154895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4);
11164895c771SDavid S. Miller if (!IS_ERR(rt)) {
11174895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu);
11184895c771SDavid S. Miller ip_rt_put(rt);
11194895c771SDavid S. Miller }
112036393395SDavid S. Miller }
11219cb3a50cSSteffen Klassert
ipv4_sk_update_pmtu(struct sk_buff * skb,struct sock * sk,u32 mtu)11229cb3a50cSSteffen Klassert void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
11239cb3a50cSSteffen Klassert {
11249cb3a50cSSteffen Klassert const struct iphdr *iph = (const struct iphdr *)skb->data;
11259cb3a50cSSteffen Klassert struct flowi4 fl4;
11269cb3a50cSSteffen Klassert struct rtable *rt;
11277f502361SEric Dumazet struct dst_entry *odst = NULL;
1128b44108dbSSteffen Klassert bool new = false;
1129e2d118a1SLorenzo Colitti struct net *net = sock_net(sk);
11309cb3a50cSSteffen Klassert
11319cb3a50cSSteffen Klassert bh_lock_sock(sk);
1132482fc609SHannes Frederic Sowa
1133482fc609SHannes Frederic Sowa if (!ip_sk_accept_pmtu(sk))
1134482fc609SHannes Frederic Sowa goto out;
1135482fc609SHannes Frederic Sowa
11367f502361SEric Dumazet odst = sk_dst_get(sk);
11379cb3a50cSSteffen Klassert
11387f502361SEric Dumazet if (sock_owned_by_user(sk) || !odst) {
11399cb3a50cSSteffen Klassert __ipv4_sk_update_pmtu(skb, sk, mtu);
11409cb3a50cSSteffen Klassert goto out;
11419cb3a50cSSteffen Klassert }
11429cb3a50cSSteffen Klassert
1143e2d118a1SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
11449cb3a50cSSteffen Klassert
11457f502361SEric Dumazet rt = (struct rtable *)odst;
114651456b29SIan Morris if (odst->obsolete && !odst->ops->check(odst, 0)) {
11479cb3a50cSSteffen Klassert rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
11489cb3a50cSSteffen Klassert if (IS_ERR(rt))
11499cb3a50cSSteffen Klassert goto out;
1150b44108dbSSteffen Klassert
1151b44108dbSSteffen Klassert new = true;
11529cb3a50cSSteffen Klassert }
11539cb3a50cSSteffen Klassert
11540f6c480fSDavid Miller __ip_rt_update_pmtu((struct rtable *)xfrm_dst_path(&rt->dst), &fl4, mtu);
11559cb3a50cSSteffen Klassert
11567f502361SEric Dumazet if (!dst_check(&rt->dst, 0)) {
1157b44108dbSSteffen Klassert if (new)
1158b44108dbSSteffen Klassert dst_release(&rt->dst);
1159b44108dbSSteffen Klassert
11609cb3a50cSSteffen Klassert rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
11619cb3a50cSSteffen Klassert if (IS_ERR(rt))
11629cb3a50cSSteffen Klassert goto out;
11639cb3a50cSSteffen Klassert
1164b44108dbSSteffen Klassert new = true;
11659cb3a50cSSteffen Klassert }
11669cb3a50cSSteffen Klassert
1167b44108dbSSteffen Klassert if (new)
11687f502361SEric Dumazet sk_dst_set(sk, &rt->dst);
11699cb3a50cSSteffen Klassert
11709cb3a50cSSteffen Klassert out:
11719cb3a50cSSteffen Klassert bh_unlock_sock(sk);
11727f502361SEric Dumazet dst_release(odst);
11739cb3a50cSSteffen Klassert }
117436393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
1175f39925dbSDavid S. Miller
ipv4_redirect(struct sk_buff * skb,struct net * net,int oif,u8 protocol)1176b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net,
11771042caa7SMaciej Żenczykowski int oif, u8 protocol)
1178b42597e2SDavid S. Miller {
1179b42597e2SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data;
1180b42597e2SDavid S. Miller struct flowi4 fl4;
1181b42597e2SDavid S. Miller struct rtable *rt;
1182b42597e2SDavid S. Miller
1183b1ad4138SGuillaume Nault __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, 0, 0);
1184b42597e2SDavid S. Miller rt = __ip_route_output_key(net, &fl4);
1185b42597e2SDavid S. Miller if (!IS_ERR(rt)) {
1186ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false);
1187b42597e2SDavid S. Miller ip_rt_put(rt);
1188b42597e2SDavid S. Miller }
1189b42597e2SDavid S. Miller }
1190b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect);
1191b42597e2SDavid S. Miller
ipv4_sk_redirect(struct sk_buff * skb,struct sock * sk)1192b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
1193b42597e2SDavid S. Miller {
11944895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data;
11954895c771SDavid S. Miller struct flowi4 fl4;
11964895c771SDavid S. Miller struct rtable *rt;
1197e2d118a1SLorenzo Colitti struct net *net = sock_net(sk);
1198b42597e2SDavid S. Miller
1199e2d118a1SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
1200e2d118a1SLorenzo Colitti rt = __ip_route_output_key(net, &fl4);
12014895c771SDavid S. Miller if (!IS_ERR(rt)) {
1202ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false);
12034895c771SDavid S. Miller ip_rt_put(rt);
12044895c771SDavid S. Miller }
1205b42597e2SDavid S. Miller }
1206b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
1207b42597e2SDavid S. Miller
ipv4_dst_check(struct dst_entry * dst,u32 cookie)1208bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ipv4_dst_check(struct dst_entry *dst,
1209bbd807dfSBrian Vazquez u32 cookie)
1210efbc368dSDavid S. Miller {
1211efbc368dSDavid S. Miller struct rtable *rt = (struct rtable *) dst;
1212efbc368dSDavid S. Miller
1213ceb33206SDavid S. Miller /* All IPV4 dsts are created with ->obsolete set to the value
1214ceb33206SDavid S. Miller * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1215ceb33206SDavid S. Miller * into this function always.
1216ceb33206SDavid S. Miller *
1217387aa65aSTimo Teräs * When a PMTU/redirect information update invalidates a route,
1218387aa65aSTimo Teräs * this is indicated by setting obsolete to DST_OBSOLETE_KILL or
121902afc7adSJulian Wiedmann * DST_OBSOLETE_DEAD.
1220ceb33206SDavid S. Miller */
1221387aa65aSTimo Teräs if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt))
1222efbc368dSDavid S. Miller return NULL;
1223d11a4dc1STimo Teräs return dst;
12241da177e4SLinus Torvalds }
12259c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_dst_check);
12261da177e4SLinus Torvalds
ipv4_send_dest_unreach(struct sk_buff * skb)122720ff83f1SEric Dumazet static void ipv4_send_dest_unreach(struct sk_buff *skb)
12281da177e4SLinus Torvalds {
12290113d9c9SKyle Zeng struct net_device *dev;
1230ed0de45aSStephen Suryaputra struct ip_options opt;
1231c543cb4aSEric Dumazet int res;
12321da177e4SLinus Torvalds
1233ed0de45aSStephen Suryaputra /* Recompile ip options since IPCB may not be valid anymore.
123420ff83f1SEric Dumazet * Also check we have a reasonable ipv4 header.
1235ed0de45aSStephen Suryaputra */
123620ff83f1SEric Dumazet if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) ||
123720ff83f1SEric Dumazet ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5)
123820ff83f1SEric Dumazet return;
123920ff83f1SEric Dumazet
1240ed0de45aSStephen Suryaputra memset(&opt, 0, sizeof(opt));
124120ff83f1SEric Dumazet if (ip_hdr(skb)->ihl > 5) {
124220ff83f1SEric Dumazet if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4))
124320ff83f1SEric Dumazet return;
1244ed0de45aSStephen Suryaputra opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr);
1245c543cb4aSEric Dumazet
1246c543cb4aSEric Dumazet rcu_read_lock();
12470113d9c9SKyle Zeng dev = skb->dev ? skb->dev : skb_rtable(skb)->dst.dev;
12480113d9c9SKyle Zeng res = __ip_options_compile(dev_net(dev), &opt, skb, NULL);
1249c543cb4aSEric Dumazet rcu_read_unlock();
1250c543cb4aSEric Dumazet
1251c543cb4aSEric Dumazet if (res)
1252ed0de45aSStephen Suryaputra return;
125320ff83f1SEric Dumazet }
1254ed0de45aSStephen Suryaputra __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &opt);
125520ff83f1SEric Dumazet }
125620ff83f1SEric Dumazet
ipv4_link_failure(struct sk_buff * skb)125720ff83f1SEric Dumazet static void ipv4_link_failure(struct sk_buff *skb)
125820ff83f1SEric Dumazet {
125920ff83f1SEric Dumazet struct rtable *rt;
126020ff83f1SEric Dumazet
126120ff83f1SEric Dumazet ipv4_send_dest_unreach(skb);
12621da177e4SLinus Torvalds
1263511c3f92SEric Dumazet rt = skb_rtable(skb);
12645943634fSDavid S. Miller if (rt)
12655943634fSDavid S. Miller dst_set_expires(&rt->dst, 0);
12662c8cec5cSDavid S. Miller }
12671da177e4SLinus Torvalds
ip_rt_bug(struct net * net,struct sock * sk,struct sk_buff * skb)1268ede2059dSEric W. Biederman static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb)
12691da177e4SLinus Torvalds {
127091df42beSJoe Perches pr_debug("%s: %pI4 -> %pI4, %s\n",
127191df42beSJoe Perches __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
12721da177e4SLinus Torvalds skb->dev ? skb->dev->name : "?");
12731da177e4SLinus Torvalds kfree_skb(skb);
1274c378a9c0SDave Jones WARN_ON(1);
12751da177e4SLinus Torvalds return 0;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds
12781da177e4SLinus Torvalds /*
12796ad08600SShubhankar Kuranagatti * We do not cache source address of outgoing interface,
12806ad08600SShubhankar Kuranagatti * because it is used only by IP RR, TS and SRR options,
12816ad08600SShubhankar Kuranagatti * so that it out of fast path.
12826ad08600SShubhankar Kuranagatti *
12836ad08600SShubhankar Kuranagatti * BTW remember: "addr" is allowed to be not aligned
12846ad08600SShubhankar Kuranagatti * in IP options!
12851da177e4SLinus Torvalds */
12861da177e4SLinus Torvalds
ip_rt_get_source(u8 * addr,struct sk_buff * skb,struct rtable * rt)12878e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
12881da177e4SLinus Torvalds {
1289a61ced5dSAl Viro __be32 src;
12901da177e4SLinus Torvalds
1291c7537967SDavid S. Miller if (rt_is_output_route(rt))
1292c5be24ffSDavid S. Miller src = ip_hdr(skb)->saddr;
1293ebc0ffaeSEric Dumazet else {
12948e36360aSDavid S. Miller struct fib_result res;
1295e351bb62SMaciej Żenczykowski struct iphdr *iph = ip_hdr(skb);
1296e351bb62SMaciej Żenczykowski struct flowi4 fl4 = {
1297e351bb62SMaciej Żenczykowski .daddr = iph->daddr,
1298e351bb62SMaciej Żenczykowski .saddr = iph->saddr,
129942f493bdSIdo Schimmel .flowi4_tos = iph->tos & IPTOS_RT_MASK,
1300e351bb62SMaciej Żenczykowski .flowi4_oif = rt->dst.dev->ifindex,
1301e351bb62SMaciej Żenczykowski .flowi4_iif = skb->dev->ifindex,
1302e351bb62SMaciej Żenczykowski .flowi4_mark = skb->mark,
1303e351bb62SMaciej Żenczykowski };
13045e2b61f7SDavid S. Miller
1305ebc0ffaeSEric Dumazet rcu_read_lock();
13060eeb075fSAndy Gospodarek if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0)
1307eba618abSDavid Ahern src = fib_result_prefsrc(dev_net(rt->dst.dev), &res);
1308ebc0ffaeSEric Dumazet else
1309f8126f1dSDavid S. Miller src = inet_select_addr(rt->dst.dev,
1310f8126f1dSDavid S. Miller rt_nexthop(rt, iph->daddr),
13111da177e4SLinus Torvalds RT_SCOPE_UNIVERSE);
1312ebc0ffaeSEric Dumazet rcu_read_unlock();
1313ebc0ffaeSEric Dumazet }
13141da177e4SLinus Torvalds memcpy(addr, &src, 4);
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds
1317c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
set_class_tag(struct rtable * rt,u32 tag)13181da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag)
13191da177e4SLinus Torvalds {
1320d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF))
1321d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF;
1322d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF0000))
1323d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF0000;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds #endif
13261da177e4SLinus Torvalds
ipv4_default_advmss(const struct dst_entry * dst)13270dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
13280dbaee3bSDavid S. Miller {
13297ed14d97SGao Feng unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
133030a26bffSEric Dumazet unsigned int advmss;
133130a26bffSEric Dumazet struct net *net;
133230a26bffSEric Dumazet
133330a26bffSEric Dumazet rcu_read_lock();
133430a26bffSEric Dumazet net = dev_net_rcu(dst->dev);
133530a26bffSEric Dumazet advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size,
13362e9589ffSxu xin net->ipv4.ip_rt_min_advmss);
133730a26bffSEric Dumazet rcu_read_unlock();
13387ed14d97SGao Feng
13397ed14d97SGao Feng return min(advmss, IPV4_MAX_PMTU - header_size);
13400dbaee3bSDavid S. Miller }
13410dbaee3bSDavid S. Miller
ipv4_mtu(const struct dst_entry * dst)1342f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ipv4_mtu(const struct dst_entry *dst)
1343d33e4553SDavid S. Miller {
1344ac6627a2SVadim Fedorenko return ip_dst_mtu_maybe_forward(dst, false);
1345d33e4553SDavid S. Miller }
13469c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_mtu);
1347d33e4553SDavid S. Miller
ip_del_fnhe(struct fib_nh_common * nhc,__be32 daddr)1348a5995e71SDavid Ahern static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr)
134994720e3aSJulian Anastasov {
135094720e3aSJulian Anastasov struct fnhe_hash_bucket *hash;
135194720e3aSJulian Anastasov struct fib_nh_exception *fnhe, __rcu **fnhe_p;
135294720e3aSJulian Anastasov u32 hval = fnhe_hashfun(daddr);
135394720e3aSJulian Anastasov
135494720e3aSJulian Anastasov spin_lock_bh(&fnhe_lock);
135594720e3aSJulian Anastasov
1356a5995e71SDavid Ahern hash = rcu_dereference_protected(nhc->nhc_exceptions,
135794720e3aSJulian Anastasov lockdep_is_held(&fnhe_lock));
135894720e3aSJulian Anastasov hash += hval;
135994720e3aSJulian Anastasov
136094720e3aSJulian Anastasov fnhe_p = &hash->chain;
136194720e3aSJulian Anastasov fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock));
136294720e3aSJulian Anastasov while (fnhe) {
136394720e3aSJulian Anastasov if (fnhe->fnhe_daddr == daddr) {
136494720e3aSJulian Anastasov rcu_assign_pointer(*fnhe_p, rcu_dereference_protected(
136594720e3aSJulian Anastasov fnhe->fnhe_next, lockdep_is_held(&fnhe_lock)));
1366ee60ad21SXin Long /* set fnhe_daddr to 0 to ensure it won't bind with
1367ee60ad21SXin Long * new dsts in rt_bind_exception().
1368ee60ad21SXin Long */
1369ee60ad21SXin Long fnhe->fnhe_daddr = 0;
137094720e3aSJulian Anastasov fnhe_flush_routes(fnhe);
137194720e3aSJulian Anastasov kfree_rcu(fnhe, rcu);
137294720e3aSJulian Anastasov break;
137394720e3aSJulian Anastasov }
137494720e3aSJulian Anastasov fnhe_p = &fnhe->fnhe_next;
137594720e3aSJulian Anastasov fnhe = rcu_dereference_protected(fnhe->fnhe_next,
137694720e3aSJulian Anastasov lockdep_is_held(&fnhe_lock));
137794720e3aSJulian Anastasov }
137894720e3aSJulian Anastasov
137994720e3aSJulian Anastasov spin_unlock_bh(&fnhe_lock);
138094720e3aSJulian Anastasov }
138194720e3aSJulian Anastasov
find_exception(struct fib_nh_common * nhc,__be32 daddr)1382a5995e71SDavid Ahern static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc,
1383a5995e71SDavid Ahern __be32 daddr)
13844895c771SDavid S. Miller {
1385a5995e71SDavid Ahern struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions);
13864895c771SDavid S. Miller struct fib_nh_exception *fnhe;
13874895c771SDavid S. Miller u32 hval;
13884895c771SDavid S. Miller
1389f2bb4bedSDavid S. Miller if (!hash)
1390f2bb4bedSDavid S. Miller return NULL;
1391f2bb4bedSDavid S. Miller
1392d3a25c98SDavid S. Miller hval = fnhe_hashfun(daddr);
13934895c771SDavid S. Miller
13944895c771SDavid S. Miller for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
13954895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) {
139694720e3aSJulian Anastasov if (fnhe->fnhe_daddr == daddr) {
139794720e3aSJulian Anastasov if (fnhe->fnhe_expires &&
139894720e3aSJulian Anastasov time_after(jiffies, fnhe->fnhe_expires)) {
1399a5995e71SDavid Ahern ip_del_fnhe(nhc, daddr);
140094720e3aSJulian Anastasov break;
140194720e3aSJulian Anastasov }
1402f2bb4bedSDavid S. Miller return fnhe;
1403f2bb4bedSDavid S. Miller }
140494720e3aSJulian Anastasov }
1405f2bb4bedSDavid S. Miller return NULL;
1406f2bb4bedSDavid S. Miller }
1407f2bb4bedSDavid S. Miller
140850d889b1SDavid Ahern /* MTU selection:
140950d889b1SDavid Ahern * 1. mtu on route is locked - use it
141050d889b1SDavid Ahern * 2. mtu from nexthop exception
141150d889b1SDavid Ahern * 3. mtu from egress device
141250d889b1SDavid Ahern */
141350d889b1SDavid Ahern
ip_mtu_from_fib_result(struct fib_result * res,__be32 daddr)141450d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr)
141550d889b1SDavid Ahern {
1416eba618abSDavid Ahern struct fib_nh_common *nhc = res->nhc;
1417eba618abSDavid Ahern struct net_device *dev = nhc->nhc_dev;
141850d889b1SDavid Ahern struct fib_info *fi = res->fi;
141950d889b1SDavid Ahern u32 mtu = 0;
142050d889b1SDavid Ahern
142160c158dcSKuniyuki Iwashima if (READ_ONCE(dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu) ||
142250d889b1SDavid Ahern fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU))
142350d889b1SDavid Ahern mtu = fi->fib_mtu;
142450d889b1SDavid Ahern
142550d889b1SDavid Ahern if (likely(!mtu)) {
142650d889b1SDavid Ahern struct fib_nh_exception *fnhe;
142750d889b1SDavid Ahern
1428a5995e71SDavid Ahern fnhe = find_exception(nhc, daddr);
142950d889b1SDavid Ahern if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires))
143050d889b1SDavid Ahern mtu = fnhe->fnhe_pmtu;
143150d889b1SDavid Ahern }
143250d889b1SDavid Ahern
143350d889b1SDavid Ahern if (likely(!mtu))
143450d889b1SDavid Ahern mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU);
143550d889b1SDavid Ahern
1436eba618abSDavid Ahern return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu);
143750d889b1SDavid Ahern }
143850d889b1SDavid Ahern
rt_bind_exception(struct rtable * rt,struct fib_nh_exception * fnhe,__be32 daddr,const bool do_cache)1439caacf05eSDavid S. Miller static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
1440a4c2fd7fSWei Wang __be32 daddr, const bool do_cache)
1441f2bb4bedSDavid S. Miller {
1442caacf05eSDavid S. Miller bool ret = false;
1443caacf05eSDavid S. Miller
1444c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock);
1445aee06da6SJulian Anastasov
1446c5038a83SDavid S. Miller if (daddr == fnhe->fnhe_daddr) {
14472ffae99dSTimo Teräs struct rtable __rcu **porig;
14482ffae99dSTimo Teräs struct rtable *orig;
14495aad1de5STimo Teräs int genid = fnhe_genid(dev_net(rt->dst.dev));
14502ffae99dSTimo Teräs
14512ffae99dSTimo Teräs if (rt_is_input_route(rt))
14522ffae99dSTimo Teräs porig = &fnhe->fnhe_rth_input;
14532ffae99dSTimo Teräs else
14542ffae99dSTimo Teräs porig = &fnhe->fnhe_rth_output;
14552ffae99dSTimo Teräs orig = rcu_dereference(*porig);
14565aad1de5STimo Teräs
14575aad1de5STimo Teräs if (fnhe->fnhe_genid != genid) {
14585aad1de5STimo Teräs fnhe->fnhe_genid = genid;
145913d82bf5SSteffen Klassert fnhe->fnhe_gw = 0;
146013d82bf5SSteffen Klassert fnhe->fnhe_pmtu = 0;
146113d82bf5SSteffen Klassert fnhe->fnhe_expires = 0;
14620e8411e4SHangbin Liu fnhe->fnhe_mtu_locked = false;
14632ffae99dSTimo Teräs fnhe_flush_routes(fnhe);
14642ffae99dSTimo Teräs orig = NULL;
146513d82bf5SSteffen Klassert }
1466387aa65aSTimo Teräs fill_route_from_fnhe(rt, fnhe);
14671550c171SDavid Ahern if (!rt->rt_gw4) {
14681550c171SDavid Ahern rt->rt_gw4 = daddr;
14691550c171SDavid Ahern rt->rt_gw_family = AF_INET;
14701550c171SDavid Ahern }
1471f2bb4bedSDavid S. Miller
1472a4c2fd7fSWei Wang if (do_cache) {
14730830106cSWei Wang dst_hold(&rt->dst);
14742ffae99dSTimo Teräs rcu_assign_pointer(*porig, rt);
14750830106cSWei Wang if (orig) {
147695c47f9cSWei Wang dst_dev_put(&orig->dst);
14770830106cSWei Wang dst_release(&orig->dst);
14780830106cSWei Wang }
14792ffae99dSTimo Teräs ret = true;
14802ffae99dSTimo Teräs }
1481c5038a83SDavid S. Miller
1482c5038a83SDavid S. Miller fnhe->fnhe_stamp = jiffies;
1483c5038a83SDavid S. Miller }
1484c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock);
1485caacf05eSDavid S. Miller
1486caacf05eSDavid S. Miller return ret;
148754764bb6SEric Dumazet }
148854764bb6SEric Dumazet
rt_cache_route(struct fib_nh_common * nhc,struct rtable * rt)148987063a1fSDavid Ahern static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt)
1490f2bb4bedSDavid S. Miller {
1491d26b3a7cSEric Dumazet struct rtable *orig, *prev, **p;
1492caacf05eSDavid S. Miller bool ret = true;
1493f2bb4bedSDavid S. Miller
1494d26b3a7cSEric Dumazet if (rt_is_input_route(rt)) {
14950f457a36SDavid Ahern p = (struct rtable **)&nhc->nhc_rth_input;
1496d26b3a7cSEric Dumazet } else {
14970f457a36SDavid Ahern p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
1498d26b3a7cSEric Dumazet }
1499f2bb4bedSDavid S. Miller orig = *p;
1500f2bb4bedSDavid S. Miller
15010830106cSWei Wang /* hold dst before doing cmpxchg() to avoid race condition
15020830106cSWei Wang * on this dst
15030830106cSWei Wang */
15040830106cSWei Wang dst_hold(&rt->dst);
1505f2bb4bedSDavid S. Miller prev = cmpxchg(p, orig, rt);
1506f2bb4bedSDavid S. Miller if (prev == orig) {
15070830106cSWei Wang if (orig) {
15085018c596SWei Wang rt_add_uncached_list(orig);
15090830106cSWei Wang dst_release(&orig->dst);
15100830106cSWei Wang }
15110830106cSWei Wang } else {
15120830106cSWei Wang dst_release(&rt->dst);
1513caacf05eSDavid S. Miller ret = false;
15140830106cSWei Wang }
1515caacf05eSDavid S. Miller
1516caacf05eSDavid S. Miller return ret;
1517caacf05eSDavid S. Miller }
1518caacf05eSDavid S. Miller
15195055c371SEric Dumazet struct uncached_list {
15205055c371SEric Dumazet spinlock_t lock;
15215055c371SEric Dumazet struct list_head head;
152229e5375dSEric Dumazet struct list_head quarantine;
15235055c371SEric Dumazet };
15245055c371SEric Dumazet
15255055c371SEric Dumazet static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
1526caacf05eSDavid S. Miller
rt_add_uncached_list(struct rtable * rt)1527510c321bSXin Long void rt_add_uncached_list(struct rtable *rt)
1528caacf05eSDavid S. Miller {
15295055c371SEric Dumazet struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
15305055c371SEric Dumazet
1531d288a162SWangyang Guo rt->dst.rt_uncached_list = ul;
15325055c371SEric Dumazet
15335055c371SEric Dumazet spin_lock_bh(&ul->lock);
1534d288a162SWangyang Guo list_add_tail(&rt->dst.rt_uncached, &ul->head);
15355055c371SEric Dumazet spin_unlock_bh(&ul->lock);
1536caacf05eSDavid S. Miller }
1537caacf05eSDavid S. Miller
rt_del_uncached_list(struct rtable * rt)1538510c321bSXin Long void rt_del_uncached_list(struct rtable *rt)
1539510c321bSXin Long {
1540d288a162SWangyang Guo if (!list_empty(&rt->dst.rt_uncached)) {
1541d288a162SWangyang Guo struct uncached_list *ul = rt->dst.rt_uncached_list;
1542510c321bSXin Long
1543510c321bSXin Long spin_lock_bh(&ul->lock);
1544d288a162SWangyang Guo list_del_init(&rt->dst.rt_uncached);
1545510c321bSXin Long spin_unlock_bh(&ul->lock);
1546510c321bSXin Long }
1547510c321bSXin Long }
1548510c321bSXin Long
ipv4_dst_destroy(struct dst_entry * dst)1549caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst)
1550caacf05eSDavid S. Miller {
1551caacf05eSDavid S. Miller struct rtable *rt = (struct rtable *)dst;
1552caacf05eSDavid S. Miller
15531620a336SDavid Ahern ip_dst_metrics_put(dst);
1554510c321bSXin Long rt_del_uncached_list(rt);
1555caacf05eSDavid S. Miller }
1556caacf05eSDavid S. Miller
rt_flush_dev(struct net_device * dev)1557caacf05eSDavid S. Miller void rt_flush_dev(struct net_device *dev)
1558caacf05eSDavid S. Miller {
155929e5375dSEric Dumazet struct rtable *rt, *safe;
15605055c371SEric Dumazet int cpu;
1561caacf05eSDavid S. Miller
15625055c371SEric Dumazet for_each_possible_cpu(cpu) {
15635055c371SEric Dumazet struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
15645055c371SEric Dumazet
156529e5375dSEric Dumazet if (list_empty(&ul->head))
156629e5375dSEric Dumazet continue;
156729e5375dSEric Dumazet
15685055c371SEric Dumazet spin_lock_bh(&ul->lock);
1569d288a162SWangyang Guo list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
1570caacf05eSDavid S. Miller if (rt->dst.dev != dev)
1571caacf05eSDavid S. Miller continue;
15728d7017fdSMahesh Bandewar rt->dst.dev = blackhole_netdev;
1573d62607c3SJakub Kicinski netdev_ref_replace(dev, blackhole_netdev,
1574d62607c3SJakub Kicinski &rt->dst.dev_tracker, GFP_ATOMIC);
1575d288a162SWangyang Guo list_move(&rt->dst.rt_uncached, &ul->quarantine);
1576caacf05eSDavid S. Miller }
15775055c371SEric Dumazet spin_unlock_bh(&ul->lock);
15784895c771SDavid S. Miller }
15794895c771SDavid S. Miller }
15804895c771SDavid S. Miller
rt_cache_valid(const struct rtable * rt)15814331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt)
1582d2d68ba9SDavid S. Miller {
15834331debcSEric Dumazet return rt &&
15844331debcSEric Dumazet rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
15854331debcSEric Dumazet !rt_is_expired(rt);
1586d2d68ba9SDavid S. Miller }
1587d2d68ba9SDavid S. Miller
rt_set_nexthop(struct rtable * rt,__be32 daddr,const struct fib_result * res,struct fib_nh_exception * fnhe,struct fib_info * fi,u16 type,u32 itag,const bool do_cache)1588f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
15895e2b61f7SDavid S. Miller const struct fib_result *res,
1590f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe,
1591a4c2fd7fSWei Wang struct fib_info *fi, u16 type, u32 itag,
1592a4c2fd7fSWei Wang const bool do_cache)
15931da177e4SLinus Torvalds {
1594caacf05eSDavid S. Miller bool cached = false;
1595caacf05eSDavid S. Miller
15961da177e4SLinus Torvalds if (fi) {
1597eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res);
15984895c771SDavid S. Miller
15990f5f7d7bSDavid Ahern if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) {
160077d5bc7eSDavid Ahern rt->rt_uses_gateway = 1;
16010f5f7d7bSDavid Ahern rt->rt_gw_family = nhc->nhc_gw_family;
16020f5f7d7bSDavid Ahern /* only INET and INET6 are supported */
16030f5f7d7bSDavid Ahern if (likely(nhc->nhc_gw_family == AF_INET))
16040f5f7d7bSDavid Ahern rt->rt_gw4 = nhc->nhc_gw.ipv4;
16050f5f7d7bSDavid Ahern else
16060f5f7d7bSDavid Ahern rt->rt_gw6 = nhc->nhc_gw.ipv6;
1607155e8336SJulian Anastasov }
16080f5f7d7bSDavid Ahern
1609e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, fi->fib_metrics);
1610e1255ed4SDavid Ahern
1611c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1612dcb1ecb5SDavid Ahern if (nhc->nhc_family == AF_INET) {
161387063a1fSDavid Ahern struct fib_nh *nh;
161487063a1fSDavid Ahern
161587063a1fSDavid Ahern nh = container_of(nhc, struct fib_nh, nh_common);
1616f2bb4bedSDavid S. Miller rt->dst.tclassid = nh->nh_tclassid;
161787063a1fSDavid Ahern }
16181da177e4SLinus Torvalds #endif
161987063a1fSDavid Ahern rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
1620c5038a83SDavid S. Miller if (unlikely(fnhe))
1621a4c2fd7fSWei Wang cached = rt_bind_exception(rt, fnhe, daddr, do_cache);
1622a4c2fd7fSWei Wang else if (do_cache)
162387063a1fSDavid Ahern cached = rt_cache_route(nhc, rt);
1624155e8336SJulian Anastasov if (unlikely(!cached)) {
1625155e8336SJulian Anastasov /* Routes we intend to cache in nexthop exception or
1626155e8336SJulian Anastasov * FIB nexthop have the DST_NOCACHE bit clear.
1627155e8336SJulian Anastasov * However, if we are unsuccessful at storing this
1628155e8336SJulian Anastasov * route into the cache we really need to set it.
1629155e8336SJulian Anastasov */
16301550c171SDavid Ahern if (!rt->rt_gw4) {
16311550c171SDavid Ahern rt->rt_gw_family = AF_INET;
16321550c171SDavid Ahern rt->rt_gw4 = daddr;
16331550c171SDavid Ahern }
1634155e8336SJulian Anastasov rt_add_uncached_list(rt);
1635d33e4553SDavid S. Miller }
1636155e8336SJulian Anastasov } else
1637caacf05eSDavid S. Miller rt_add_uncached_list(rt);
16381da177e4SLinus Torvalds
1639c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
16401da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES
164185b91b03SDavid S. Miller set_class_tag(rt, res->tclassid);
16421da177e4SLinus Torvalds #endif
16431da177e4SLinus Torvalds set_class_tag(rt, itag);
16441da177e4SLinus Torvalds #endif
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds
rt_dst_alloc(struct net_device * dev,unsigned int flags,u16 type,bool noxfrm)16479ab179d8SDavid Ahern struct rtable *rt_dst_alloc(struct net_device *dev,
1648d08c4f35SDavid Ahern unsigned int flags, u16 type,
1649b5c8b3feSEyal Birger bool noxfrm)
16500c4dcd58SDavid S. Miller {
1651d08c4f35SDavid Ahern struct rtable *rt;
1652d08c4f35SDavid Ahern
1653d08c4f35SDavid Ahern rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1654b2a9c0edSWei Wang (noxfrm ? DST_NOXFRM : 0));
1655d08c4f35SDavid Ahern
1656d08c4f35SDavid Ahern if (rt) {
1657d08c4f35SDavid Ahern rt->rt_genid = rt_genid_ipv4(dev_net(dev));
1658d08c4f35SDavid Ahern rt->rt_flags = flags;
1659d08c4f35SDavid Ahern rt->rt_type = type;
1660d08c4f35SDavid Ahern rt->rt_is_input = 0;
1661d08c4f35SDavid Ahern rt->rt_iif = 0;
1662d08c4f35SDavid Ahern rt->rt_pmtu = 0;
1663d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = 0;
166477d5bc7eSDavid Ahern rt->rt_uses_gateway = 0;
16651550c171SDavid Ahern rt->rt_gw_family = 0;
16661550c171SDavid Ahern rt->rt_gw4 = 0;
1667d08c4f35SDavid Ahern
1668d08c4f35SDavid Ahern rt->dst.output = ip_output;
1669d08c4f35SDavid Ahern if (flags & RTCF_LOCAL)
1670d08c4f35SDavid Ahern rt->dst.input = ip_local_deliver;
1671d08c4f35SDavid Ahern }
1672d08c4f35SDavid Ahern
1673d08c4f35SDavid Ahern return rt;
16740c4dcd58SDavid S. Miller }
16759ab179d8SDavid Ahern EXPORT_SYMBOL(rt_dst_alloc);
16760c4dcd58SDavid S. Miller
rt_dst_clone(struct net_device * dev,struct rtable * rt)16775b18f128SStephen Suryaputra struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt)
16785b18f128SStephen Suryaputra {
16795b18f128SStephen Suryaputra struct rtable *new_rt;
16805b18f128SStephen Suryaputra
16815b18f128SStephen Suryaputra new_rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
16825b18f128SStephen Suryaputra rt->dst.flags);
16835b18f128SStephen Suryaputra
16845b18f128SStephen Suryaputra if (new_rt) {
16855b18f128SStephen Suryaputra new_rt->rt_genid = rt_genid_ipv4(dev_net(dev));
16865b18f128SStephen Suryaputra new_rt->rt_flags = rt->rt_flags;
16875b18f128SStephen Suryaputra new_rt->rt_type = rt->rt_type;
16885b18f128SStephen Suryaputra new_rt->rt_is_input = rt->rt_is_input;
16895b18f128SStephen Suryaputra new_rt->rt_iif = rt->rt_iif;
16905b18f128SStephen Suryaputra new_rt->rt_pmtu = rt->rt_pmtu;
16915b18f128SStephen Suryaputra new_rt->rt_mtu_locked = rt->rt_mtu_locked;
16925b18f128SStephen Suryaputra new_rt->rt_gw_family = rt->rt_gw_family;
16935b18f128SStephen Suryaputra if (rt->rt_gw_family == AF_INET)
16945b18f128SStephen Suryaputra new_rt->rt_gw4 = rt->rt_gw4;
16955b18f128SStephen Suryaputra else if (rt->rt_gw_family == AF_INET6)
16965b18f128SStephen Suryaputra new_rt->rt_gw6 = rt->rt_gw6;
16975b18f128SStephen Suryaputra
16985b18f128SStephen Suryaputra new_rt->dst.input = rt->dst.input;
16995b18f128SStephen Suryaputra new_rt->dst.output = rt->dst.output;
17005b18f128SStephen Suryaputra new_rt->dst.error = rt->dst.error;
17015b18f128SStephen Suryaputra new_rt->dst.lastuse = jiffies;
17025b18f128SStephen Suryaputra new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate);
17035b18f128SStephen Suryaputra }
17045b18f128SStephen Suryaputra return new_rt;
17055b18f128SStephen Suryaputra }
17065b18f128SStephen Suryaputra EXPORT_SYMBOL(rt_dst_clone);
17075b18f128SStephen Suryaputra
170896d36220SEric Dumazet /* called in rcu_read_lock() section */
ip_mc_validate_source(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev,struct in_device * in_dev,u32 * itag)1709bc044e8dSPaolo Abeni int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1710bc044e8dSPaolo Abeni u8 tos, struct net_device *dev,
1711bc044e8dSPaolo Abeni struct in_device *in_dev, u32 *itag)
17121da177e4SLinus Torvalds {
1713b5f7e755SEric Dumazet int err;
17141da177e4SLinus Torvalds
17151da177e4SLinus Torvalds /* Primary sanity checks. */
171651456b29SIan Morris if (!in_dev)
17171da177e4SLinus Torvalds return -EINVAL;
17181da177e4SLinus Torvalds
17191e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1720d0daebc3SThomas Graf skb->protocol != htons(ETH_P_IP))
1721bc044e8dSPaolo Abeni return -EINVAL;
1722d0daebc3SThomas Graf
172375fea73dSAlexander Duyck if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev))
1724bc044e8dSPaolo Abeni return -EINVAL;
17251da177e4SLinus Torvalds
1726f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) {
17271d2f4ebbSEdward Chron if (!ipv4_is_local_multicast(daddr) &&
17281d2f4ebbSEdward Chron ip_hdr(skb)->protocol != IPPROTO_IGMP)
1729bc044e8dSPaolo Abeni return -EINVAL;
1730b5f7e755SEric Dumazet } else {
17319e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
1732bc044e8dSPaolo Abeni in_dev, itag);
1733b5f7e755SEric Dumazet if (err < 0)
1734bc044e8dSPaolo Abeni return err;
1735b5f7e755SEric Dumazet }
1736bc044e8dSPaolo Abeni return 0;
1737bc044e8dSPaolo Abeni }
1738bc044e8dSPaolo Abeni
1739bc044e8dSPaolo Abeni /* called in rcu_read_lock() section */
ip_route_input_mc(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev,int our)1740bc044e8dSPaolo Abeni static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1741bc044e8dSPaolo Abeni u8 tos, struct net_device *dev, int our)
1742bc044e8dSPaolo Abeni {
1743bc044e8dSPaolo Abeni struct in_device *in_dev = __in_dev_get_rcu(dev);
1744bc044e8dSPaolo Abeni unsigned int flags = RTCF_MULTICAST;
1745bc044e8dSPaolo Abeni struct rtable *rth;
1746bc044e8dSPaolo Abeni u32 itag = 0;
1747bc044e8dSPaolo Abeni int err;
1748bc044e8dSPaolo Abeni
1749bc044e8dSPaolo Abeni err = ip_mc_validate_source(skb, daddr, saddr, tos, dev, in_dev, &itag);
1750bc044e8dSPaolo Abeni if (err)
1751bc044e8dSPaolo Abeni return err;
1752bc044e8dSPaolo Abeni
1753d08c4f35SDavid Ahern if (our)
1754d08c4f35SDavid Ahern flags |= RTCF_LOCAL;
1755d08c4f35SDavid Ahern
1756b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY))
1757e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY;
1758e6175a2eSEyal Birger
1759d08c4f35SDavid Ahern rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST,
1760b5c8b3feSEyal Birger false);
17611da177e4SLinus Torvalds if (!rth)
1762bc044e8dSPaolo Abeni return -ENOBUFS;
17631da177e4SLinus Torvalds
1764c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
1765d8d1f30bSChangli Gao rth->dst.tclassid = itag;
17661da177e4SLinus Torvalds #endif
1767cf911662SDavid S. Miller rth->dst.output = ip_rt_bug;
17689917e1e8SDavid S. Miller rth->rt_is_input= 1;
17691da177e4SLinus Torvalds
17701da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1771f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
1772d8d1f30bSChangli Gao rth->dst.input = ip_mr_input;
17731da177e4SLinus Torvalds #endif
17741da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_mc);
17751da177e4SLinus Torvalds
17769e6c6d17SLokesh Dhoundiyal skb_dst_drop(skb);
177789aef892SDavid S. Miller skb_dst_set(skb, &rth->dst);
177889aef892SDavid S. Miller return 0;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds
17811da177e4SLinus Torvalds
ip_handle_martian_source(struct net_device * dev,struct in_device * in_dev,struct sk_buff * skb,__be32 daddr,__be32 saddr)17821da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev,
17831da177e4SLinus Torvalds struct in_device *in_dev,
17841da177e4SLinus Torvalds struct sk_buff *skb,
17859e12bb22SAl Viro __be32 daddr,
17869e12bb22SAl Viro __be32 saddr)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_src);
17891da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
17901da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
17911da177e4SLinus Torvalds /*
17921da177e4SLinus Torvalds * RFC1812 recommendation, if source is martian,
17931da177e4SLinus Torvalds * the only hint is MAC header.
17941da177e4SLinus Torvalds */
1795058bd4d2SJoe Perches pr_warn("martian source %pI4 from %pI4, on dev %s\n",
1796673d57e7SHarvey Harrison &daddr, &saddr, dev->name);
179798e399f8SArnaldo Carvalho de Melo if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1798058bd4d2SJoe Perches print_hex_dump(KERN_WARNING, "ll header: ",
1799058bd4d2SJoe Perches DUMP_PREFIX_OFFSET, 16, 1,
1800058bd4d2SJoe Perches skb_mac_header(skb),
1801b2c85100SDavid S. Miller dev->hard_header_len, false);
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds #endif
18051da177e4SLinus Torvalds }
18061da177e4SLinus Torvalds
180747360228SEric Dumazet /* called in rcu_read_lock() section */
__mkroute_input(struct sk_buff * skb,const struct fib_result * res,struct in_device * in_dev,__be32 daddr,__be32 saddr,u32 tos)18085969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb,
1809982721f3SDavid S. Miller const struct fib_result *res,
18101da177e4SLinus Torvalds struct in_device *in_dev,
1811c6cffba4SDavid S. Miller __be32 daddr, __be32 saddr, u32 tos)
18121da177e4SLinus Torvalds {
1813eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res);
1814eba618abSDavid Ahern struct net_device *dev = nhc->nhc_dev;
18152ffae99dSTimo Teräs struct fib_nh_exception *fnhe;
18161da177e4SLinus Torvalds struct rtable *rth;
18171da177e4SLinus Torvalds int err;
18181da177e4SLinus Torvalds struct in_device *out_dev;
1819b5c8b3feSEyal Birger bool do_cache;
1820fbdc0ad0SLi RongQing u32 itag = 0;
18211da177e4SLinus Torvalds
18221da177e4SLinus Torvalds /* get a working reference to the output device */
1823eba618abSDavid Ahern out_dev = __in_dev_get_rcu(dev);
182451456b29SIan Morris if (!out_dev) {
1825e87cc472SJoe Perches net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
18261da177e4SLinus Torvalds return -EINVAL;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds
18295c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
18309e56e380SDavid S. Miller in_dev->dev, in_dev, &itag);
18311da177e4SLinus Torvalds if (err < 0) {
18321da177e4SLinus Torvalds ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
18331da177e4SLinus Torvalds saddr);
18341da177e4SLinus Torvalds
18351da177e4SLinus Torvalds goto cleanup;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds
1838e81da0e1SJulian Anastasov do_cache = res->fi && !itag;
1839e81da0e1SJulian Anastasov if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
1840eba618abSDavid Ahern skb->protocol == htons(ETH_P_IP)) {
1841bdf00467SDavid Ahern __be32 gw;
1842eba618abSDavid Ahern
1843bdf00467SDavid Ahern gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0;
1844eba618abSDavid Ahern if (IN_DEV_SHARED_MEDIA(out_dev) ||
1845eba618abSDavid Ahern inet_addr_onlink(out_dev, saddr, gw))
1846df4d9254SHannes Frederic Sowa IPCB(skb)->flags |= IPSKB_DOREDIRECT;
1847eba618abSDavid Ahern }
18481da177e4SLinus Torvalds
18491da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) {
18501da177e4SLinus Torvalds /* Not IP (i.e. ARP). Do not create route, if it is
18511da177e4SLinus Torvalds * invalid for proxy arp. DNAT routes are always valid.
185265324144SJesper Dangaard Brouer *
185365324144SJesper Dangaard Brouer * Proxy arp feature have been extended to allow, ARP
185465324144SJesper Dangaard Brouer * replies back to the same interface, to support
185565324144SJesper Dangaard Brouer * Private VLAN switch technologies. See arp.c.
18561da177e4SLinus Torvalds */
185765324144SJesper Dangaard Brouer if (out_dev == in_dev &&
185865324144SJesper Dangaard Brouer IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
18591da177e4SLinus Torvalds err = -EINVAL;
18601da177e4SLinus Torvalds goto cleanup;
18611da177e4SLinus Torvalds }
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds
1864b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY))
1865e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY;
1866e6175a2eSEyal Birger
1867a5995e71SDavid Ahern fnhe = find_exception(nhc, daddr);
1868e81da0e1SJulian Anastasov if (do_cache) {
186994720e3aSJulian Anastasov if (fnhe)
18702ffae99dSTimo Teräs rth = rcu_dereference(fnhe->fnhe_rth_input);
187194720e3aSJulian Anastasov else
18720f457a36SDavid Ahern rth = rcu_dereference(nhc->nhc_rth_input);
1873d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) {
1874c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst);
1875d2d68ba9SDavid S. Miller goto out;
1876d2d68ba9SDavid S. Miller }
1877d2d68ba9SDavid S. Miller }
1878f2bb4bedSDavid S. Miller
1879b5c8b3feSEyal Birger rth = rt_dst_alloc(out_dev->dev, 0, res->type,
188062679a8dSVincent Bernat IN_DEV_ORCONF(out_dev, NOXFRM));
18811da177e4SLinus Torvalds if (!rth) {
18821da177e4SLinus Torvalds err = -ENOBUFS;
18831da177e4SLinus Torvalds goto cleanup;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds
18869917e1e8SDavid S. Miller rth->rt_is_input = 1;
1887a6254864SDuan Jiong RT_CACHE_STAT_INC(in_slow_tot);
18881da177e4SLinus Torvalds
1889d8d1f30bSChangli Gao rth->dst.input = ip_forward;
18901da177e4SLinus Torvalds
1891a4c2fd7fSWei Wang rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag,
1892a4c2fd7fSWei Wang do_cache);
18939942895bSDavid Ahern lwtunnel_set_redirect(&rth->dst);
1894c6cffba4SDavid S. Miller skb_dst_set(skb, &rth->dst);
1895d2d68ba9SDavid S. Miller out:
18961da177e4SLinus Torvalds err = 0;
18971da177e4SLinus Torvalds cleanup:
18981da177e4SLinus Torvalds return err;
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds
190179a13159SPeter Nørlund #ifdef CONFIG_IP_ROUTE_MULTIPATH
190279a13159SPeter Nørlund /* To make ICMP packets follow the right flow, the multipath hash is
1903bf4e0a3dSNikolay Aleksandrov * calculated from the inner IP addresses.
190479a13159SPeter Nørlund */
ip_multipath_l3_keys(const struct sk_buff * skb,struct flow_keys * hash_keys)1905bf4e0a3dSNikolay Aleksandrov static void ip_multipath_l3_keys(const struct sk_buff *skb,
1906bf4e0a3dSNikolay Aleksandrov struct flow_keys *hash_keys)
190779a13159SPeter Nørlund {
190879a13159SPeter Nørlund const struct iphdr *outer_iph = ip_hdr(skb);
19096f74b6c2SDavid Ahern const struct iphdr *key_iph = outer_iph;
1910bf4e0a3dSNikolay Aleksandrov const struct iphdr *inner_iph;
191179a13159SPeter Nørlund const struct icmphdr *icmph;
191279a13159SPeter Nørlund struct iphdr _inner_iph;
1913bf4e0a3dSNikolay Aleksandrov struct icmphdr _icmph;
1914bf4e0a3dSNikolay Aleksandrov
1915bf4e0a3dSNikolay Aleksandrov if (likely(outer_iph->protocol != IPPROTO_ICMP))
19166f74b6c2SDavid Ahern goto out;
191779a13159SPeter Nørlund
191879a13159SPeter Nørlund if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0))
19196f74b6c2SDavid Ahern goto out;
192079a13159SPeter Nørlund
192179a13159SPeter Nørlund icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph),
192279a13159SPeter Nørlund &_icmph);
192379a13159SPeter Nørlund if (!icmph)
19246f74b6c2SDavid Ahern goto out;
192579a13159SPeter Nørlund
192654074f1dSMatteo Croce if (!icmp_is_err(icmph->type))
19276f74b6c2SDavid Ahern goto out;
192879a13159SPeter Nørlund
192979a13159SPeter Nørlund inner_iph = skb_header_pointer(skb,
193079a13159SPeter Nørlund outer_iph->ihl * 4 + sizeof(_icmph),
193179a13159SPeter Nørlund sizeof(_inner_iph), &_inner_iph);
193279a13159SPeter Nørlund if (!inner_iph)
19336f74b6c2SDavid Ahern goto out;
19346f74b6c2SDavid Ahern
19356f74b6c2SDavid Ahern key_iph = inner_iph;
19366f74b6c2SDavid Ahern out:
19376f74b6c2SDavid Ahern hash_keys->addrs.v4addrs.src = key_iph->saddr;
19386f74b6c2SDavid Ahern hash_keys->addrs.v4addrs.dst = key_iph->daddr;
193979a13159SPeter Nørlund }
194079a13159SPeter Nørlund
fib_multipath_custom_hash_outer(const struct net * net,const struct sk_buff * skb,bool * p_has_inner)19414253b498SIdo Schimmel static u32 fib_multipath_custom_hash_outer(const struct net *net,
19424253b498SIdo Schimmel const struct sk_buff *skb,
19434253b498SIdo Schimmel bool *p_has_inner)
19444253b498SIdo Schimmel {
19458895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
19464253b498SIdo Schimmel struct flow_keys keys, hash_keys;
19474253b498SIdo Schimmel
19484253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
19494253b498SIdo Schimmel return 0;
19504253b498SIdo Schimmel
19514253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys));
19524253b498SIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
19534253b498SIdo Schimmel
19544253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
19554253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
19564253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
19574253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
19584253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
19594253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
19604253b498SIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto;
19614253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
19624253b498SIdo Schimmel hash_keys.ports.src = keys.ports.src;
19634253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
19644253b498SIdo Schimmel hash_keys.ports.dst = keys.ports.dst;
19654253b498SIdo Schimmel
19664253b498SIdo Schimmel *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
19674253b498SIdo Schimmel return flow_hash_from_keys(&hash_keys);
19684253b498SIdo Schimmel }
19694253b498SIdo Schimmel
fib_multipath_custom_hash_inner(const struct net * net,const struct sk_buff * skb,bool has_inner)19704253b498SIdo Schimmel static u32 fib_multipath_custom_hash_inner(const struct net *net,
19714253b498SIdo Schimmel const struct sk_buff *skb,
19724253b498SIdo Schimmel bool has_inner)
19734253b498SIdo Schimmel {
19748895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
19754253b498SIdo Schimmel struct flow_keys keys, hash_keys;
19764253b498SIdo Schimmel
19774253b498SIdo Schimmel /* We assume the packet carries an encapsulation, but if none was
19784253b498SIdo Schimmel * encountered during dissection of the outer flow, then there is no
19794253b498SIdo Schimmel * point in calling the flow dissector again.
19804253b498SIdo Schimmel */
19814253b498SIdo Schimmel if (!has_inner)
19824253b498SIdo Schimmel return 0;
19834253b498SIdo Schimmel
19844253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
19854253b498SIdo Schimmel return 0;
19864253b498SIdo Schimmel
19874253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys));
19884253b498SIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, 0);
19894253b498SIdo Schimmel
19904253b498SIdo Schimmel if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
19914253b498SIdo Schimmel return 0;
19924253b498SIdo Schimmel
19934253b498SIdo Schimmel if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
19944253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
19954253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
19964253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
19974253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
19984253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
19994253b498SIdo Schimmel } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
20004253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
20014253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
20024253b498SIdo Schimmel hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
20034253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
20044253b498SIdo Schimmel hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
20054253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
20064253b498SIdo Schimmel hash_keys.tags.flow_label = keys.tags.flow_label;
20074253b498SIdo Schimmel }
20084253b498SIdo Schimmel
20094253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
20104253b498SIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto;
20114253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
20124253b498SIdo Schimmel hash_keys.ports.src = keys.ports.src;
20134253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
20144253b498SIdo Schimmel hash_keys.ports.dst = keys.ports.dst;
20154253b498SIdo Schimmel
20164253b498SIdo Schimmel return flow_hash_from_keys(&hash_keys);
20174253b498SIdo Schimmel }
20184253b498SIdo Schimmel
fib_multipath_custom_hash_skb(const struct net * net,const struct sk_buff * skb)20194253b498SIdo Schimmel static u32 fib_multipath_custom_hash_skb(const struct net *net,
20204253b498SIdo Schimmel const struct sk_buff *skb)
20214253b498SIdo Schimmel {
20224253b498SIdo Schimmel u32 mhash, mhash_inner;
20234253b498SIdo Schimmel bool has_inner = true;
20244253b498SIdo Schimmel
20254253b498SIdo Schimmel mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner);
20264253b498SIdo Schimmel mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner);
20274253b498SIdo Schimmel
20284253b498SIdo Schimmel return jhash_2words(mhash, mhash_inner, 0);
20294253b498SIdo Schimmel }
20304253b498SIdo Schimmel
fib_multipath_custom_hash_fl4(const struct net * net,const struct flowi4 * fl4)20314253b498SIdo Schimmel static u32 fib_multipath_custom_hash_fl4(const struct net *net,
20324253b498SIdo Schimmel const struct flowi4 *fl4)
20334253b498SIdo Schimmel {
20348895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
20354253b498SIdo Schimmel struct flow_keys hash_keys;
20364253b498SIdo Schimmel
20374253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
20384253b498SIdo Schimmel return 0;
20394253b498SIdo Schimmel
20404253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys));
20414253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
20424253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
20434253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = fl4->saddr;
20444253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
20454253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = fl4->daddr;
20464253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
20474253b498SIdo Schimmel hash_keys.basic.ip_proto = fl4->flowi4_proto;
20484253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
20494253b498SIdo Schimmel hash_keys.ports.src = fl4->fl4_sport;
20504253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
20514253b498SIdo Schimmel hash_keys.ports.dst = fl4->fl4_dport;
20524253b498SIdo Schimmel
20534253b498SIdo Schimmel return flow_hash_from_keys(&hash_keys);
20544253b498SIdo Schimmel }
20554253b498SIdo Schimmel
2056bf4e0a3dSNikolay Aleksandrov /* if skb is set it will be used and fl4 can be NULL */
fib_multipath_hash(const struct net * net,const struct flowi4 * fl4,const struct sk_buff * skb,struct flow_keys * flkeys)20577efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
2058e37b1e97SRoopa Prabhu const struct sk_buff *skb, struct flow_keys *flkeys)
2059bf4e0a3dSNikolay Aleksandrov {
20602a8e4997SIdo Schimmel u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
2061bf4e0a3dSNikolay Aleksandrov struct flow_keys hash_keys;
20622e68ea92SIdo Schimmel u32 mhash = 0;
2063bf4e0a3dSNikolay Aleksandrov
20647998c12aSKuniyuki Iwashima switch (READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_policy)) {
2065bf4e0a3dSNikolay Aleksandrov case 0:
2066bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys));
2067bf4e0a3dSNikolay Aleksandrov hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2068bf4e0a3dSNikolay Aleksandrov if (skb) {
2069bf4e0a3dSNikolay Aleksandrov ip_multipath_l3_keys(skb, &hash_keys);
2070bf4e0a3dSNikolay Aleksandrov } else {
2071bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.src = fl4->saddr;
2072bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.dst = fl4->daddr;
2073bf4e0a3dSNikolay Aleksandrov }
20742e68ea92SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys);
2075bf4e0a3dSNikolay Aleksandrov break;
2076bf4e0a3dSNikolay Aleksandrov case 1:
2077bf4e0a3dSNikolay Aleksandrov /* skb is currently provided only when forwarding */
2078bf4e0a3dSNikolay Aleksandrov if (skb) {
2079bf4e0a3dSNikolay Aleksandrov unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2080bf4e0a3dSNikolay Aleksandrov struct flow_keys keys;
2081bf4e0a3dSNikolay Aleksandrov
2082bf4e0a3dSNikolay Aleksandrov /* short-circuit if we already have L4 hash present */
2083bf4e0a3dSNikolay Aleksandrov if (skb->l4_hash)
2084bf4e0a3dSNikolay Aleksandrov return skb_get_hash_raw(skb) >> 1;
2085ec7127a5SDavid Ahern
2086bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys));
20871fe4b118SDavid Ahern
2088ec7127a5SDavid Ahern if (!flkeys) {
2089ec7127a5SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag);
2090ec7127a5SDavid Ahern flkeys = &keys;
2091ec7127a5SDavid Ahern }
2092ec7127a5SDavid Ahern
2093e37b1e97SRoopa Prabhu hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2094e37b1e97SRoopa Prabhu hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2095e37b1e97SRoopa Prabhu hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2096e37b1e97SRoopa Prabhu hash_keys.ports.src = flkeys->ports.src;
2097e37b1e97SRoopa Prabhu hash_keys.ports.dst = flkeys->ports.dst;
2098e37b1e97SRoopa Prabhu hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2099e37b1e97SRoopa Prabhu } else {
2100bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys));
2101bf4e0a3dSNikolay Aleksandrov hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2102bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.src = fl4->saddr;
2103bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.dst = fl4->daddr;
2104bf4e0a3dSNikolay Aleksandrov hash_keys.ports.src = fl4->fl4_sport;
2105bf4e0a3dSNikolay Aleksandrov hash_keys.ports.dst = fl4->fl4_dport;
2106bf4e0a3dSNikolay Aleksandrov hash_keys.basic.ip_proto = fl4->flowi4_proto;
2107bf4e0a3dSNikolay Aleksandrov }
21082e68ea92SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys);
2109bf4e0a3dSNikolay Aleksandrov break;
2110363887a2SStephen Suryaputra case 2:
2111363887a2SStephen Suryaputra memset(&hash_keys, 0, sizeof(hash_keys));
2112363887a2SStephen Suryaputra /* skb is currently provided only when forwarding */
2113363887a2SStephen Suryaputra if (skb) {
2114363887a2SStephen Suryaputra struct flow_keys keys;
2115363887a2SStephen Suryaputra
2116363887a2SStephen Suryaputra skb_flow_dissect_flow_keys(skb, &keys, 0);
2117828b2b44SStephen Suryaputra /* Inner can be v4 or v6 */
2118828b2b44SStephen Suryaputra if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2119828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2120363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2121363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2122828b2b44SStephen Suryaputra } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2123828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2124828b2b44SStephen Suryaputra hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2125828b2b44SStephen Suryaputra hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2126828b2b44SStephen Suryaputra hash_keys.tags.flow_label = keys.tags.flow_label;
2127828b2b44SStephen Suryaputra hash_keys.basic.ip_proto = keys.basic.ip_proto;
2128363887a2SStephen Suryaputra } else {
2129363887a2SStephen Suryaputra /* Same as case 0 */
2130828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2131828b2b44SStephen Suryaputra ip_multipath_l3_keys(skb, &hash_keys);
2132828b2b44SStephen Suryaputra }
2133828b2b44SStephen Suryaputra } else {
2134828b2b44SStephen Suryaputra /* Same as case 0 */
2135828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2136363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.src = fl4->saddr;
2137363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.dst = fl4->daddr;
2138363887a2SStephen Suryaputra }
21392e68ea92SIdo Schimmel mhash = flow_hash_from_keys(&hash_keys);
2140363887a2SStephen Suryaputra break;
21414253b498SIdo Schimmel case 3:
21424253b498SIdo Schimmel if (skb)
21434253b498SIdo Schimmel mhash = fib_multipath_custom_hash_skb(net, skb);
21444253b498SIdo Schimmel else
21454253b498SIdo Schimmel mhash = fib_multipath_custom_hash_fl4(net, fl4);
21464253b498SIdo Schimmel break;
2147bf4e0a3dSNikolay Aleksandrov }
2148bf4e0a3dSNikolay Aleksandrov
214924ba1440Swenxu if (multipath_hash)
215024ba1440Swenxu mhash = jhash_2words(mhash, multipath_hash, 0);
215124ba1440Swenxu
2152bf4e0a3dSNikolay Aleksandrov return mhash >> 1;
2153bf4e0a3dSNikolay Aleksandrov }
215479a13159SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */
215579a13159SPeter Nørlund
ip_mkroute_input(struct sk_buff * skb,struct fib_result * res,struct in_device * in_dev,__be32 daddr,__be32 saddr,u32 tos,struct flow_keys * hkeys)21565969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb,
21571da177e4SLinus Torvalds struct fib_result *res,
21581da177e4SLinus Torvalds struct in_device *in_dev,
2159e37b1e97SRoopa Prabhu __be32 daddr, __be32 saddr, u32 tos,
2160e37b1e97SRoopa Prabhu struct flow_keys *hkeys)
21611da177e4SLinus Torvalds {
21621da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH
21635481d73fSDavid Ahern if (res->fi && fib_info_num_path(res->fi) > 1) {
21647efc0b6bSDavid Ahern int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys);
21650e884c78SPeter Nørlund
21660e884c78SPeter Nørlund fib_select_multipath(res, h);
21676ac66cb0SSriram Yagnaraman IPCB(skb)->flags |= IPSKB_MULTIPATH;
21680e884c78SPeter Nørlund }
21691da177e4SLinus Torvalds #endif
21701da177e4SLinus Torvalds
21711da177e4SLinus Torvalds /* create a routing cache entry */
2172c6cffba4SDavid S. Miller return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds
217502b24941SPaolo Abeni /* Implements all the saddr-related checks as ip_route_input_slow(),
217602b24941SPaolo Abeni * assuming daddr is valid and the destination is not a local broadcast one.
217702b24941SPaolo Abeni * Uses the provided hint instead of performing a route lookup.
217802b24941SPaolo Abeni */
ip_route_use_hint(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev,const struct sk_buff * hint)217902b24941SPaolo Abeni int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
218002b24941SPaolo Abeni u8 tos, struct net_device *dev,
218102b24941SPaolo Abeni const struct sk_buff *hint)
218202b24941SPaolo Abeni {
218302b24941SPaolo Abeni struct in_device *in_dev = __in_dev_get_rcu(dev);
21842ce578caSMiaohe Lin struct rtable *rt = skb_rtable(hint);
218502b24941SPaolo Abeni struct net *net = dev_net(dev);
218602b24941SPaolo Abeni int err = -EINVAL;
218702b24941SPaolo Abeni u32 tag = 0;
218802b24941SPaolo Abeni
21898240c730SEric Dumazet if (!in_dev)
21908240c730SEric Dumazet return -EINVAL;
21918240c730SEric Dumazet
219202b24941SPaolo Abeni if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
219302b24941SPaolo Abeni goto martian_source;
219402b24941SPaolo Abeni
219502b24941SPaolo Abeni if (ipv4_is_zeronet(saddr))
219602b24941SPaolo Abeni goto martian_source;
219702b24941SPaolo Abeni
219802b24941SPaolo Abeni if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
219902b24941SPaolo Abeni goto martian_source;
220002b24941SPaolo Abeni
220102b24941SPaolo Abeni if (rt->rt_type != RTN_LOCAL)
220202b24941SPaolo Abeni goto skip_validate_source;
220302b24941SPaolo Abeni
220402b24941SPaolo Abeni tos &= IPTOS_RT_MASK;
220502b24941SPaolo Abeni err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag);
220602b24941SPaolo Abeni if (err < 0)
220702b24941SPaolo Abeni goto martian_source;
220802b24941SPaolo Abeni
220902b24941SPaolo Abeni skip_validate_source:
221002b24941SPaolo Abeni skb_dst_copy(skb, hint);
221102b24941SPaolo Abeni return 0;
221202b24941SPaolo Abeni
221302b24941SPaolo Abeni martian_source:
221402b24941SPaolo Abeni ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
221502b24941SPaolo Abeni return err;
221602b24941SPaolo Abeni }
221702b24941SPaolo Abeni
2218b87b04f5SDavid Ahern /* get device for dst_alloc with local routes */
ip_rt_get_dev(struct net * net,const struct fib_result * res)2219b87b04f5SDavid Ahern static struct net_device *ip_rt_get_dev(struct net *net,
2220b87b04f5SDavid Ahern const struct fib_result *res)
2221b87b04f5SDavid Ahern {
2222b87b04f5SDavid Ahern struct fib_nh_common *nhc = res->fi ? res->nhc : NULL;
2223b87b04f5SDavid Ahern struct net_device *dev = NULL;
2224b87b04f5SDavid Ahern
2225b87b04f5SDavid Ahern if (nhc)
2226b87b04f5SDavid Ahern dev = l3mdev_master_dev_rcu(nhc->nhc_dev);
2227b87b04f5SDavid Ahern
2228b87b04f5SDavid Ahern return dev ? : net->loopback_dev;
2229b87b04f5SDavid Ahern }
2230b87b04f5SDavid Ahern
22311da177e4SLinus Torvalds /*
22321da177e4SLinus Torvalds * NOTE. We drop all the packets that has local source
22331da177e4SLinus Torvalds * addresses, because every properly looped back packet
22341da177e4SLinus Torvalds * must have correct destination already attached by output routine.
223502b24941SPaolo Abeni * Changes in the enforced policies must be applied also to
223602b24941SPaolo Abeni * ip_route_use_hint().
22371da177e4SLinus Torvalds *
22381da177e4SLinus Torvalds * Such approach solves two big problems:
22391da177e4SLinus Torvalds * 1. Not simplex devices are handled properly.
22401da177e4SLinus Torvalds * 2. IP spoofing attempts are filtered with 100% of guarantee.
2241ebc0ffaeSEric Dumazet * called with rcu_read_lock()
22421da177e4SLinus Torvalds */
22431da177e4SLinus Torvalds
ip_route_input_slow(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev,struct fib_result * res)22449e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
22455510cdf7SDavid Ahern u8 tos, struct net_device *dev,
22465510cdf7SDavid Ahern struct fib_result *res)
22471da177e4SLinus Torvalds {
224896d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev);
2249e37b1e97SRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys;
2250e37b1e97SRoopa Prabhu struct net *net = dev_net(dev);
22511b7179d3SThomas Graf struct ip_tunnel_info *tun_info;
2252e37b1e97SRoopa Prabhu int err = -EINVAL;
225395c96174SEric Dumazet unsigned int flags = 0;
22541da177e4SLinus Torvalds u32 itag = 0;
22551da177e4SLinus Torvalds struct rtable *rth;
2256e37b1e97SRoopa Prabhu struct flowi4 fl4;
22570a90478bSXin Long bool do_cache = true;
22581da177e4SLinus Torvalds
22591da177e4SLinus Torvalds /* IP on this device is disabled. */
22601da177e4SLinus Torvalds
22611da177e4SLinus Torvalds if (!in_dev)
22621da177e4SLinus Torvalds goto out;
22631da177e4SLinus Torvalds
22641da177e4SLinus Torvalds /* Check for the most weird martians, which can be not detected
22656ad08600SShubhankar Kuranagatti * by fib_lookup.
22661da177e4SLinus Torvalds */
22671da177e4SLinus Torvalds
226861adedf3SJiri Benc tun_info = skb_tunnel_info(skb);
226946fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
22701b7179d3SThomas Graf fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id;
22711b7179d3SThomas Graf else
22721b7179d3SThomas Graf fl4.flowi4_tun_key.tun_id = 0;
2273f38a9eb1SThomas Graf skb_dst_drop(skb);
2274f38a9eb1SThomas Graf
2275d0daebc3SThomas Graf if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
22761da177e4SLinus Torvalds goto martian_source;
22771da177e4SLinus Torvalds
22785510cdf7SDavid Ahern res->fi = NULL;
22795510cdf7SDavid Ahern res->table = NULL;
228027a954bdSAndy Walls if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
22811da177e4SLinus Torvalds goto brd_input;
22821da177e4SLinus Torvalds
22831da177e4SLinus Torvalds /* Accept zero addresses only to limited broadcast;
22841da177e4SLinus Torvalds * I even do not know to fix it or not. Waiting for complains :-)
22851da177e4SLinus Torvalds */
2286f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr))
22871da177e4SLinus Torvalds goto martian_source;
22881da177e4SLinus Torvalds
2289d0daebc3SThomas Graf if (ipv4_is_zeronet(daddr))
22901da177e4SLinus Torvalds goto martian_destination;
22911da177e4SLinus Torvalds
22929eb43e76SEric Dumazet /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
22939eb43e76SEric Dumazet * and call it once if daddr or/and saddr are loopback addresses
22949eb43e76SEric Dumazet */
22959eb43e76SEric Dumazet if (ipv4_is_loopback(daddr)) {
22969eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2297d0daebc3SThomas Graf goto martian_destination;
22989eb43e76SEric Dumazet } else if (ipv4_is_loopback(saddr)) {
22999eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2300d0daebc3SThomas Graf goto martian_source;
2301d0daebc3SThomas Graf }
2302d0daebc3SThomas Graf
23031da177e4SLinus Torvalds /*
23041da177e4SLinus Torvalds * Now we are ready to route packet.
23051da177e4SLinus Torvalds */
230640867d74SDavid Ahern fl4.flowi4_l3mdev = 0;
230768a5e3ddSDavid S. Miller fl4.flowi4_oif = 0;
2308e0d56fddSDavid Ahern fl4.flowi4_iif = dev->ifindex;
230968a5e3ddSDavid S. Miller fl4.flowi4_mark = skb->mark;
231068a5e3ddSDavid S. Miller fl4.flowi4_tos = tos;
231168a5e3ddSDavid S. Miller fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
2312b84f7878SDavid Ahern fl4.flowi4_flags = 0;
231368a5e3ddSDavid S. Miller fl4.daddr = daddr;
231468a5e3ddSDavid S. Miller fl4.saddr = saddr;
23158bcfd092SJulian Anastasov fl4.flowi4_uid = sock_net_uid(net, NULL);
23161869e226SDavid Ahern fl4.flowi4_multipath_hash = 0;
2317e37b1e97SRoopa Prabhu
23185a847a6eSDavid Ahern if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) {
2319e37b1e97SRoopa Prabhu flkeys = &_flkeys;
23205a847a6eSDavid Ahern } else {
23215a847a6eSDavid Ahern fl4.flowi4_proto = 0;
23225a847a6eSDavid Ahern fl4.fl4_sport = 0;
23235a847a6eSDavid Ahern fl4.fl4_dport = 0;
23245a847a6eSDavid Ahern }
2325e37b1e97SRoopa Prabhu
23265510cdf7SDavid Ahern err = fib_lookup(net, &fl4, res, 0);
2327cd0f0b95SDuan Jiong if (err != 0) {
2328cd0f0b95SDuan Jiong if (!IN_DEV_FORWARD(in_dev))
2329cd0f0b95SDuan Jiong err = -EHOSTUNREACH;
23301da177e4SLinus Torvalds goto no_route;
2331cd0f0b95SDuan Jiong }
23321da177e4SLinus Torvalds
23335cbf777cSXin Long if (res->type == RTN_BROADCAST) {
23345cbf777cSXin Long if (IN_DEV_BFORWARD(in_dev))
23355cbf777cSXin Long goto make_route;
23360a90478bSXin Long /* not do cache if bc_forwarding is enabled */
23370a90478bSXin Long if (IPV4_DEVCONF_ALL(net, BC_FORWARDING))
23380a90478bSXin Long do_cache = false;
23391da177e4SLinus Torvalds goto brd_input;
23405cbf777cSXin Long }
23411da177e4SLinus Torvalds
23425510cdf7SDavid Ahern if (res->type == RTN_LOCAL) {
23435c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos,
23440d5edc68SCong Wang 0, dev, in_dev, &itag);
2345b5f7e755SEric Dumazet if (err < 0)
23460d753960SDavid Ahern goto martian_source;
23471da177e4SLinus Torvalds goto local_input;
23481da177e4SLinus Torvalds }
23491da177e4SLinus Torvalds
2350cd0f0b95SDuan Jiong if (!IN_DEV_FORWARD(in_dev)) {
2351cd0f0b95SDuan Jiong err = -EHOSTUNREACH;
2352251da413SDavid S. Miller goto no_route;
2353cd0f0b95SDuan Jiong }
23545510cdf7SDavid Ahern if (res->type != RTN_UNICAST)
23551da177e4SLinus Torvalds goto martian_destination;
23561da177e4SLinus Torvalds
23575cbf777cSXin Long make_route:
2358e37b1e97SRoopa Prabhu err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, tos, flkeys);
23591da177e4SLinus Torvalds out: return err;
23601da177e4SLinus Torvalds
23611da177e4SLinus Torvalds brd_input:
23621da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP))
23631da177e4SLinus Torvalds goto e_inval;
23641da177e4SLinus Torvalds
236541347dcdSDavid S. Miller if (!ipv4_is_zeronet(saddr)) {
23669e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
23679e56e380SDavid S. Miller in_dev, &itag);
23681da177e4SLinus Torvalds if (err < 0)
23690d753960SDavid Ahern goto martian_source;
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds flags |= RTCF_BROADCAST;
23725510cdf7SDavid Ahern res->type = RTN_BROADCAST;
23731da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_brd);
23741da177e4SLinus Torvalds
23751da177e4SLinus Torvalds local_input:
2376b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY))
2377e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY;
2378e6175a2eSEyal Birger
23790a90478bSXin Long do_cache &= res->fi && !itag;
23800a90478bSXin Long if (do_cache) {
2381eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2382eba618abSDavid Ahern
23830f457a36SDavid Ahern rth = rcu_dereference(nhc->nhc_rth_input);
2384d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) {
2385c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst);
2386c6cffba4SDavid S. Miller err = 0;
2387c6cffba4SDavid S. Miller goto out;
2388d2d68ba9SDavid S. Miller }
2389d2d68ba9SDavid S. Miller }
2390d2d68ba9SDavid S. Miller
2391b87b04f5SDavid Ahern rth = rt_dst_alloc(ip_rt_get_dev(net, res),
2392b5c8b3feSEyal Birger flags | RTCF_LOCAL, res->type, false);
23931da177e4SLinus Torvalds if (!rth)
23941da177e4SLinus Torvalds goto e_nobufs;
23951da177e4SLinus Torvalds
2396d8d1f30bSChangli Gao rth->dst.output= ip_rt_bug;
2397cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
2398cf911662SDavid S. Miller rth->dst.tclassid = itag;
2399cf911662SDavid S. Miller #endif
24009917e1e8SDavid S. Miller rth->rt_is_input = 1;
2401571e7226SRoopa Prabhu
2402a6254864SDuan Jiong RT_CACHE_STAT_INC(in_slow_tot);
24035510cdf7SDavid Ahern if (res->type == RTN_UNREACHABLE) {
2404d8d1f30bSChangli Gao rth->dst.input= ip_error;
2405d8d1f30bSChangli Gao rth->dst.error= -err;
24061da177e4SLinus Torvalds rth->rt_flags &= ~RTCF_LOCAL;
24071da177e4SLinus Torvalds }
2408efd85700SThomas Graf
2409dcdfdf56SAlexei Starovoitov if (do_cache) {
2410eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2411efd85700SThomas Graf
2412eba618abSDavid Ahern rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
2413efd85700SThomas Graf if (lwtunnel_input_redirect(rth->dst.lwtstate)) {
2414efd85700SThomas Graf WARN_ON(rth->dst.input == lwtunnel_input);
2415efd85700SThomas Graf rth->dst.lwtstate->orig_input = rth->dst.input;
2416efd85700SThomas Graf rth->dst.input = lwtunnel_input;
2417efd85700SThomas Graf }
2418efd85700SThomas Graf
241987063a1fSDavid Ahern if (unlikely(!rt_cache_route(nhc, rth)))
2420dcdfdf56SAlexei Starovoitov rt_add_uncached_list(rth);
2421dcdfdf56SAlexei Starovoitov }
242289aef892SDavid S. Miller skb_dst_set(skb, &rth->dst);
2423b23dd4feSDavid S. Miller err = 0;
2424ebc0ffaeSEric Dumazet goto out;
24251da177e4SLinus Torvalds
24261da177e4SLinus Torvalds no_route:
24271da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_no_route);
24285510cdf7SDavid Ahern res->type = RTN_UNREACHABLE;
24295510cdf7SDavid Ahern res->fi = NULL;
24305510cdf7SDavid Ahern res->table = NULL;
24311da177e4SLinus Torvalds goto local_input;
24321da177e4SLinus Torvalds
24331da177e4SLinus Torvalds /*
24341da177e4SLinus Torvalds * Do not cache martian addresses: they should be logged (RFC1812)
24351da177e4SLinus Torvalds */
24361da177e4SLinus Torvalds martian_destination:
24371da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_dst);
24381da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE
2439e87cc472SJoe Perches if (IN_DEV_LOG_MARTIANS(in_dev))
2440e87cc472SJoe Perches net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
2441673d57e7SHarvey Harrison &daddr, &saddr, dev->name);
24421da177e4SLinus Torvalds #endif
24432c2910a4SDietmar Eggemann
24441da177e4SLinus Torvalds e_inval:
24451da177e4SLinus Torvalds err = -EINVAL;
2446ebc0ffaeSEric Dumazet goto out;
24471da177e4SLinus Torvalds
24481da177e4SLinus Torvalds e_nobufs:
24491da177e4SLinus Torvalds err = -ENOBUFS;
2450ebc0ffaeSEric Dumazet goto out;
24511da177e4SLinus Torvalds
24521da177e4SLinus Torvalds martian_source:
24531da177e4SLinus Torvalds ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
2454ebc0ffaeSEric Dumazet goto out;
24551da177e4SLinus Torvalds }
24561da177e4SLinus Torvalds
24575510cdf7SDavid Ahern /* called with rcu_read_lock held */
ip_route_input_rcu(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev,struct fib_result * res)24585022e221SZhengchao Shao static int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
24595510cdf7SDavid Ahern u8 tos, struct net_device *dev, struct fib_result *res)
24605510cdf7SDavid Ahern {
24611da177e4SLinus Torvalds /* Multicast recognition logic is moved from route cache to here.
24626ad08600SShubhankar Kuranagatti * The problem was that too many Ethernet cards have broken/missing
24636ad08600SShubhankar Kuranagatti * hardware multicast filters :-( As result the host on multicasting
24646ad08600SShubhankar Kuranagatti * network acquires a lot of useless route cache entries, sort of
24656ad08600SShubhankar Kuranagatti * SDR messages from all the world. Now we try to get rid of them.
24666ad08600SShubhankar Kuranagatti * Really, provided software IP multicast filter is organized
24676ad08600SShubhankar Kuranagatti * reasonably (at least, hashed), it does not result in a slowdown
24686ad08600SShubhankar Kuranagatti * comparing with route cache reject entries.
24696ad08600SShubhankar Kuranagatti * Note, that multicast routers are not affected, because
24706ad08600SShubhankar Kuranagatti * route cache entry is created eventually.
24711da177e4SLinus Torvalds */
2472f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) {
247396d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev);
2474e58e4159SDavid Ahern int our = 0;
24755510cdf7SDavid Ahern int err = -EINVAL;
24761da177e4SLinus Torvalds
247722c74764SPaolo Abeni if (!in_dev)
247822c74764SPaolo Abeni return err;
2479e58e4159SDavid Ahern our = ip_check_mc_rcu(in_dev, daddr, saddr,
2480eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol);
2481e58e4159SDavid Ahern
2482e58e4159SDavid Ahern /* check l3 master if no match yet */
248322c74764SPaolo Abeni if (!our && netif_is_l3_slave(dev)) {
2484e58e4159SDavid Ahern struct in_device *l3_in_dev;
2485e58e4159SDavid Ahern
2486e58e4159SDavid Ahern l3_in_dev = __in_dev_get_rcu(skb->dev);
2487e58e4159SDavid Ahern if (l3_in_dev)
2488e58e4159SDavid Ahern our = ip_check_mc_rcu(l3_in_dev, daddr, saddr,
2489e58e4159SDavid Ahern ip_hdr(skb)->protocol);
2490e58e4159SDavid Ahern }
2491e58e4159SDavid Ahern
24921da177e4SLinus Torvalds if (our
24931da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
24949d4fb27dSJoe Perches ||
24959d4fb27dSJoe Perches (!ipv4_is_local_multicast(daddr) &&
2496f97c1e0cSJoe Perches IN_DEV_MFORWARD(in_dev))
24971da177e4SLinus Torvalds #endif
24981da177e4SLinus Torvalds ) {
24995510cdf7SDavid Ahern err = ip_route_input_mc(skb, daddr, saddr,
25001da177e4SLinus Torvalds tos, dev, our);
2501e58e4159SDavid Ahern }
25025510cdf7SDavid Ahern return err;
25031da177e4SLinus Torvalds }
25045510cdf7SDavid Ahern
25055510cdf7SDavid Ahern return ip_route_input_slow(skb, daddr, saddr, tos, dev, res);
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds
ip_route_input_noref(struct sk_buff * skb,__be32 daddr,__be32 saddr,u8 tos,struct net_device * dev)25085022e221SZhengchao Shao int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
25095022e221SZhengchao Shao u8 tos, struct net_device *dev)
25105022e221SZhengchao Shao {
25115022e221SZhengchao Shao struct fib_result res;
25125022e221SZhengchao Shao int err;
25135022e221SZhengchao Shao
25145022e221SZhengchao Shao tos &= IPTOS_RT_MASK;
25155022e221SZhengchao Shao rcu_read_lock();
25165022e221SZhengchao Shao err = ip_route_input_rcu(skb, daddr, saddr, tos, dev, &res);
25175022e221SZhengchao Shao rcu_read_unlock();
25185022e221SZhengchao Shao
25195022e221SZhengchao Shao return err;
25205022e221SZhengchao Shao }
25215022e221SZhengchao Shao EXPORT_SYMBOL(ip_route_input_noref);
25225022e221SZhengchao Shao
2523ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */
__mkroute_output(const struct fib_result * res,const struct flowi4 * fl4,int orig_oif,struct net_device * dev_out,unsigned int flags)2524982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res,
25251a00fee4SDavid Miller const struct flowi4 *fl4, int orig_oif,
2526f61759e6SJulian Anastasov struct net_device *dev_out,
25275ada5527SDavid S. Miller unsigned int flags)
25281da177e4SLinus Torvalds {
2529982721f3SDavid S. Miller struct fib_info *fi = res->fi;
2530f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe;
25315ada5527SDavid S. Miller struct in_device *in_dev;
2532982721f3SDavid S. Miller u16 type = res->type;
25335ada5527SDavid S. Miller struct rtable *rth;
2534c92b9655SJulian Anastasov bool do_cache;
25351da177e4SLinus Torvalds
2536d0daebc3SThomas Graf in_dev = __in_dev_get_rcu(dev_out);
2537d0daebc3SThomas Graf if (!in_dev)
2538d0daebc3SThomas Graf return ERR_PTR(-EINVAL);
2539d0daebc3SThomas Graf
2540d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
25415f02ce24SDavid Ahern if (ipv4_is_loopback(fl4->saddr) &&
25425f02ce24SDavid Ahern !(dev_out->flags & IFF_LOOPBACK) &&
25435f02ce24SDavid Ahern !netif_is_l3_master(dev_out))
25445ada5527SDavid S. Miller return ERR_PTR(-EINVAL);
25451da177e4SLinus Torvalds
254668a5e3ddSDavid S. Miller if (ipv4_is_lbcast(fl4->daddr))
2547982721f3SDavid S. Miller type = RTN_BROADCAST;
254868a5e3ddSDavid S. Miller else if (ipv4_is_multicast(fl4->daddr))
2549982721f3SDavid S. Miller type = RTN_MULTICAST;
255068a5e3ddSDavid S. Miller else if (ipv4_is_zeronet(fl4->daddr))
25515ada5527SDavid S. Miller return ERR_PTR(-EINVAL);
25521da177e4SLinus Torvalds
25531da177e4SLinus Torvalds if (dev_out->flags & IFF_LOOPBACK)
25541da177e4SLinus Torvalds flags |= RTCF_LOCAL;
25551da177e4SLinus Torvalds
255663617421SJulian Anastasov do_cache = true;
2557982721f3SDavid S. Miller if (type == RTN_BROADCAST) {
25581da177e4SLinus Torvalds flags |= RTCF_BROADCAST | RTCF_LOCAL;
2559982721f3SDavid S. Miller fi = NULL;
2560982721f3SDavid S. Miller } else if (type == RTN_MULTICAST) {
25611da177e4SLinus Torvalds flags |= RTCF_MULTICAST | RTCF_LOCAL;
2562813b3b5dSDavid S. Miller if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
2563813b3b5dSDavid S. Miller fl4->flowi4_proto))
25641da177e4SLinus Torvalds flags &= ~RTCF_LOCAL;
256563617421SJulian Anastasov else
256663617421SJulian Anastasov do_cache = false;
25671da177e4SLinus Torvalds /* If multicast route do not exist use
2568dd28d1a0SEric Dumazet * default one, but do not gateway in this case.
2569dd28d1a0SEric Dumazet * Yes, it is hack.
25701da177e4SLinus Torvalds */
2571982721f3SDavid S. Miller if (fi && res->prefixlen < 4)
2572982721f3SDavid S. Miller fi = NULL;
2573d6d5e999SChris Friesen } else if ((type == RTN_LOCAL) && (orig_oif != 0) &&
2574d6d5e999SChris Friesen (orig_oif != dev_out->ifindex)) {
2575d6d5e999SChris Friesen /* For local routes that require a particular output interface
2576d6d5e999SChris Friesen * we do not want to cache the result. Caching the result
2577d6d5e999SChris Friesen * causes incorrect behaviour when there are multiple source
2578d6d5e999SChris Friesen * addresses on the interface, the end result being that if the
2579d6d5e999SChris Friesen * intended recipient is waiting on that interface for the
2580d6d5e999SChris Friesen * packet he won't receive it because it will be delivered on
2581d6d5e999SChris Friesen * the loopback interface and the IP_PKTINFO ipi_ifindex will
2582d6d5e999SChris Friesen * be set to the loopback interface as well.
2583d6d5e999SChris Friesen */
258494720e3aSJulian Anastasov do_cache = false;
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds
2587f2bb4bedSDavid S. Miller fnhe = NULL;
258863617421SJulian Anastasov do_cache &= fi != NULL;
258994720e3aSJulian Anastasov if (fi) {
2590eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res);
2591d26b3a7cSEric Dumazet struct rtable __rcu **prth;
2592d26b3a7cSEric Dumazet
2593a5995e71SDavid Ahern fnhe = find_exception(nhc, fl4->daddr);
259494720e3aSJulian Anastasov if (!do_cache)
259594720e3aSJulian Anastasov goto add;
2596deed49dfSXin Long if (fnhe) {
25972ffae99dSTimo Teräs prth = &fnhe->fnhe_rth_output;
2598deed49dfSXin Long } else {
2599c92b9655SJulian Anastasov if (unlikely(fl4->flowi4_flags &
2600c92b9655SJulian Anastasov FLOWI_FLAG_KNOWN_NH &&
2601bdf00467SDavid Ahern !(nhc->nhc_gw_family &&
2602eba618abSDavid Ahern nhc->nhc_scope == RT_SCOPE_LINK))) {
2603c92b9655SJulian Anastasov do_cache = false;
2604c92b9655SJulian Anastasov goto add;
2605c92b9655SJulian Anastasov }
26060f457a36SDavid Ahern prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
260794720e3aSJulian Anastasov }
2608d26b3a7cSEric Dumazet rth = rcu_dereference(*prth);
26099df16efaSWei Wang if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst))
2610f2bb4bedSDavid S. Miller return rth;
2611f2bb4bedSDavid S. Miller }
2612c92b9655SJulian Anastasov
2613c92b9655SJulian Anastasov add:
2614d08c4f35SDavid Ahern rth = rt_dst_alloc(dev_out, flags, type,
261562679a8dSVincent Bernat IN_DEV_ORCONF(in_dev, NOXFRM));
26168391d07bSDimitris Michailidis if (!rth)
26175ada5527SDavid S. Miller return ERR_PTR(-ENOBUFS);
26188391d07bSDimitris Michailidis
26199438c871SDavid Ahern rth->rt_iif = orig_oif;
2620b7503e0cSDavid Ahern
26211da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_tot);
26221da177e4SLinus Torvalds
26231da177e4SLinus Torvalds if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
26241da177e4SLinus Torvalds if (flags & RTCF_LOCAL &&
26251da177e4SLinus Torvalds !(dev_out->flags & IFF_LOOPBACK)) {
2626d8d1f30bSChangli Gao rth->dst.output = ip_mc_output;
26271da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_mc);
26281da177e4SLinus Torvalds }
26291da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2630982721f3SDavid S. Miller if (type == RTN_MULTICAST) {
26311da177e4SLinus Torvalds if (IN_DEV_MFORWARD(in_dev) &&
2632813b3b5dSDavid S. Miller !ipv4_is_local_multicast(fl4->daddr)) {
2633d8d1f30bSChangli Gao rth->dst.input = ip_mr_input;
2634d8d1f30bSChangli Gao rth->dst.output = ip_mc_output;
26351da177e4SLinus Torvalds }
26361da177e4SLinus Torvalds }
26371da177e4SLinus Torvalds #endif
26381da177e4SLinus Torvalds }
26391da177e4SLinus Torvalds
2640a4c2fd7fSWei Wang rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache);
26419942895bSDavid Ahern lwtunnel_set_redirect(&rth->dst);
26421da177e4SLinus Torvalds
26435ada5527SDavid S. Miller return rth;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds
26461da177e4SLinus Torvalds /*
26471da177e4SLinus Torvalds * Major route resolver routine.
26481da177e4SLinus Torvalds */
26491da177e4SLinus Torvalds
ip_route_output_key_hash(struct net * net,struct flowi4 * fl4,const struct sk_buff * skb)26503abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
2651bf4e0a3dSNikolay Aleksandrov const struct sk_buff *skb)
26521da177e4SLinus Torvalds {
2653d0ea2b12SEric Dumazet struct fib_result res = {
2654d0ea2b12SEric Dumazet .type = RTN_UNSPEC,
2655d0ea2b12SEric Dumazet .fi = NULL,
2656d0ea2b12SEric Dumazet .table = NULL,
2657d0ea2b12SEric Dumazet .tclassid = 0,
2658d0ea2b12SEric Dumazet };
26595ada5527SDavid S. Miller struct rtable *rth;
26601da177e4SLinus Torvalds
26611fb9489bSPavel Emelyanov fl4->flowi4_iif = LOOPBACK_IFINDEX;
2662544b4dd5SGuillaume Nault ip_rt_fix_tos(fl4);
266344713b67SDavid S. Miller
2664010c2708SDavid S. Miller rcu_read_lock();
26653abd1adeSDavid Ahern rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb);
26663abd1adeSDavid Ahern rcu_read_unlock();
26673abd1adeSDavid Ahern
26683abd1adeSDavid Ahern return rth;
26693abd1adeSDavid Ahern }
26703abd1adeSDavid Ahern EXPORT_SYMBOL_GPL(ip_route_output_key_hash);
26713abd1adeSDavid Ahern
ip_route_output_key_hash_rcu(struct net * net,struct flowi4 * fl4,struct fib_result * res,const struct sk_buff * skb)26723abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4,
26733abd1adeSDavid Ahern struct fib_result *res,
26743abd1adeSDavid Ahern const struct sk_buff *skb)
26753abd1adeSDavid Ahern {
26763abd1adeSDavid Ahern struct net_device *dev_out = NULL;
26773abd1adeSDavid Ahern int orig_oif = fl4->flowi4_oif;
26783abd1adeSDavid Ahern unsigned int flags = 0;
26793abd1adeSDavid Ahern struct rtable *rth;
2680595e0651SStefano Brivio int err;
26813abd1adeSDavid Ahern
2682813b3b5dSDavid S. Miller if (fl4->saddr) {
2683813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->saddr) ||
2684813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->saddr) ||
2685595e0651SStefano Brivio ipv4_is_zeronet(fl4->saddr)) {
2686595e0651SStefano Brivio rth = ERR_PTR(-EINVAL);
26871da177e4SLinus Torvalds goto out;
2688595e0651SStefano Brivio }
2689595e0651SStefano Brivio
2690595e0651SStefano Brivio rth = ERR_PTR(-ENETUNREACH);
26911da177e4SLinus Torvalds
26921da177e4SLinus Torvalds /* I removed check for oif == dev_out->oif here.
26936ad08600SShubhankar Kuranagatti * It was wrong for two reasons:
26946ad08600SShubhankar Kuranagatti * 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
26956ad08600SShubhankar Kuranagatti * is assigned to multiple interfaces.
26966ad08600SShubhankar Kuranagatti * 2. Moreover, we are allowed to send packets with saddr
26976ad08600SShubhankar Kuranagatti * of another iface. --ANK
26981da177e4SLinus Torvalds */
26991da177e4SLinus Torvalds
2700813b3b5dSDavid S. Miller if (fl4->flowi4_oif == 0 &&
2701813b3b5dSDavid S. Miller (ipv4_is_multicast(fl4->daddr) ||
2702813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr))) {
2703a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2704813b3b5dSDavid S. Miller dev_out = __ip_dev_find(net, fl4->saddr, false);
270551456b29SIan Morris if (!dev_out)
2706a210d01aSJulian Anastasov goto out;
2707a210d01aSJulian Anastasov
27081da177e4SLinus Torvalds /* Special hack: user can direct multicasts
27096ad08600SShubhankar Kuranagatti * and limited broadcast via necessary interface
27106ad08600SShubhankar Kuranagatti * without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
27116ad08600SShubhankar Kuranagatti * This hack is not just for fun, it allows
27126ad08600SShubhankar Kuranagatti * vic,vat and friends to work.
27136ad08600SShubhankar Kuranagatti * They bind socket to loopback, set ttl to zero
27146ad08600SShubhankar Kuranagatti * and expect that it will work.
27156ad08600SShubhankar Kuranagatti * From the viewpoint of routing cache they are broken,
27166ad08600SShubhankar Kuranagatti * because we are not allowed to build multicast path
27176ad08600SShubhankar Kuranagatti * with loopback source addr (look, routing cache
27186ad08600SShubhankar Kuranagatti * cannot know, that ttl is zero, so that packet
27196ad08600SShubhankar Kuranagatti * will not leave this host and route is valid).
27206ad08600SShubhankar Kuranagatti * Luckily, this hack is good workaround.
27211da177e4SLinus Torvalds */
27221da177e4SLinus Torvalds
2723813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex;
27241da177e4SLinus Torvalds goto make_route;
27251da177e4SLinus Torvalds }
2726a210d01aSJulian Anastasov
2727813b3b5dSDavid S. Miller if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
2728a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2729813b3b5dSDavid S. Miller if (!__ip_dev_find(net, fl4->saddr, false))
2730a210d01aSJulian Anastasov goto out;
27311da177e4SLinus Torvalds }
2732a210d01aSJulian Anastasov }
27331da177e4SLinus Torvalds
27341da177e4SLinus Torvalds
2735813b3b5dSDavid S. Miller if (fl4->flowi4_oif) {
2736813b3b5dSDavid S. Miller dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
2737b23dd4feSDavid S. Miller rth = ERR_PTR(-ENODEV);
273851456b29SIan Morris if (!dev_out)
27391da177e4SLinus Torvalds goto out;
2740e5ed6399SHerbert Xu
2741e5ed6399SHerbert Xu /* RACE: Check return value of inet_select_addr instead. */
2742fc75fc83SEric Dumazet if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
2743b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH);
2744fc75fc83SEric Dumazet goto out;
2745fc75fc83SEric Dumazet }
2746813b3b5dSDavid S. Miller if (ipv4_is_local_multicast(fl4->daddr) ||
27476a211654SAndrew Lunn ipv4_is_lbcast(fl4->daddr) ||
27486a211654SAndrew Lunn fl4->flowi4_proto == IPPROTO_IGMP) {
2749813b3b5dSDavid S. Miller if (!fl4->saddr)
2750813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0,
27511da177e4SLinus Torvalds RT_SCOPE_LINK);
27521da177e4SLinus Torvalds goto make_route;
27531da177e4SLinus Torvalds }
27540a7e2260SJiri Benc if (!fl4->saddr) {
2755813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->daddr))
2756813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0,
2757813b3b5dSDavid S. Miller fl4->flowi4_scope);
2758813b3b5dSDavid S. Miller else if (!fl4->daddr)
2759813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0,
27601da177e4SLinus Torvalds RT_SCOPE_HOST);
27611da177e4SLinus Torvalds }
2762613d09b3SDavid Ahern }
27631da177e4SLinus Torvalds
2764813b3b5dSDavid S. Miller if (!fl4->daddr) {
2765813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr;
2766813b3b5dSDavid S. Miller if (!fl4->daddr)
2767813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
2768b40afd0eSDenis V. Lunev dev_out = net->loopback_dev;
27691fb9489bSPavel Emelyanov fl4->flowi4_oif = LOOPBACK_IFINDEX;
27703abd1adeSDavid Ahern res->type = RTN_LOCAL;
27711da177e4SLinus Torvalds flags |= RTCF_LOCAL;
27721da177e4SLinus Torvalds goto make_route;
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds
27753abd1adeSDavid Ahern err = fib_lookup(net, fl4, res, 0);
27760315e382SNikola Forró if (err) {
27773abd1adeSDavid Ahern res->fi = NULL;
27783abd1adeSDavid Ahern res->table = NULL;
27796104e112SDavid Ahern if (fl4->flowi4_oif &&
278040867d74SDavid Ahern (ipv4_is_multicast(fl4->daddr) || !fl4->flowi4_l3mdev)) {
27811da177e4SLinus Torvalds /* Apparently, routing tables are wrong. Assume,
27826ad08600SShubhankar Kuranagatti * that the destination is on link.
27836ad08600SShubhankar Kuranagatti *
27846ad08600SShubhankar Kuranagatti * WHY? DW.
27856ad08600SShubhankar Kuranagatti * Because we are allowed to send to iface
27866ad08600SShubhankar Kuranagatti * even if it has NO routes and NO assigned
27876ad08600SShubhankar Kuranagatti * addresses. When oif is specified, routing
27886ad08600SShubhankar Kuranagatti * tables are looked up with only one purpose:
27896ad08600SShubhankar Kuranagatti * to catch if destination is gatewayed, rather than
27906ad08600SShubhankar Kuranagatti * direct. Moreover, if MSG_DONTROUTE is set,
27916ad08600SShubhankar Kuranagatti * we send packet, ignoring both routing tables
27926ad08600SShubhankar Kuranagatti * and ifaddr state. --ANK
27936ad08600SShubhankar Kuranagatti *
27946ad08600SShubhankar Kuranagatti *
27956ad08600SShubhankar Kuranagatti * We could make it even if oif is unknown,
27966ad08600SShubhankar Kuranagatti * likely IPv6, but we do not.
27971da177e4SLinus Torvalds */
27981da177e4SLinus Torvalds
2799813b3b5dSDavid S. Miller if (fl4->saddr == 0)
2800813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0,
28011da177e4SLinus Torvalds RT_SCOPE_LINK);
28023abd1adeSDavid Ahern res->type = RTN_UNICAST;
28031da177e4SLinus Torvalds goto make_route;
28041da177e4SLinus Torvalds }
28050315e382SNikola Forró rth = ERR_PTR(err);
28061da177e4SLinus Torvalds goto out;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds
28093abd1adeSDavid Ahern if (res->type == RTN_LOCAL) {
2810813b3b5dSDavid S. Miller if (!fl4->saddr) {
28113abd1adeSDavid Ahern if (res->fi->fib_prefsrc)
28123abd1adeSDavid Ahern fl4->saddr = res->fi->fib_prefsrc;
28139fc3bbb4SJoel Sing else
2814813b3b5dSDavid S. Miller fl4->saddr = fl4->daddr;
28159fc3bbb4SJoel Sing }
28165f02ce24SDavid Ahern
28175f02ce24SDavid Ahern /* L3 master device is the loopback for that domain */
28183abd1adeSDavid Ahern dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? :
2819b7c8487cSRobert Shearman net->loopback_dev;
2820839da4d9SDavid Ahern
2821839da4d9SDavid Ahern /* make sure orig_oif points to fib result device even
2822839da4d9SDavid Ahern * though packet rx/tx happens over loopback or l3mdev
2823839da4d9SDavid Ahern */
2824839da4d9SDavid Ahern orig_oif = FIB_RES_OIF(*res);
2825839da4d9SDavid Ahern
2826813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex;
28271da177e4SLinus Torvalds flags |= RTCF_LOCAL;
28281da177e4SLinus Torvalds goto make_route;
28291da177e4SLinus Torvalds }
28301da177e4SLinus Torvalds
28313abd1adeSDavid Ahern fib_select_path(net, res, fl4, skb);
28321da177e4SLinus Torvalds
28333abd1adeSDavid Ahern dev_out = FIB_RES_DEV(*res);
28341da177e4SLinus Torvalds
28351da177e4SLinus Torvalds make_route:
28363abd1adeSDavid Ahern rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags);
28371da177e4SLinus Torvalds
2838010c2708SDavid S. Miller out:
2839b23dd4feSDavid S. Miller return rth;
28401da177e4SLinus Torvalds }
2841d8c97a94SArnaldo Carvalho de Melo
284214e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = {
284314e50e57SDavid S. Miller .family = AF_INET,
2844214f45c9SEric Dumazet .default_advmss = ipv4_default_advmss,
2845d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup,
2846c4c877b2SDaniel Borkmann .check = dst_blackhole_check,
2847c4c877b2SDaniel Borkmann .cow_metrics = dst_blackhole_cow_metrics,
2848c4c877b2SDaniel Borkmann .update_pmtu = dst_blackhole_update_pmtu,
2849c4c877b2SDaniel Borkmann .redirect = dst_blackhole_redirect,
2850c4c877b2SDaniel Borkmann .mtu = dst_blackhole_mtu,
285114e50e57SDavid S. Miller };
285214e50e57SDavid S. Miller
ipv4_blackhole_route(struct net * net,struct dst_entry * dst_orig)28532774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
285414e50e57SDavid S. Miller {
28552774c131SDavid S. Miller struct rtable *ort = (struct rtable *) dst_orig;
2856f5b0a874SDavid S. Miller struct rtable *rt;
285714e50e57SDavid S. Miller
28586c0e7284SSteffen Klassert rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_DEAD, 0);
285914e50e57SDavid S. Miller if (rt) {
2860d8d1f30bSChangli Gao struct dst_entry *new = &rt->dst;
286114e50e57SDavid S. Miller
286214e50e57SDavid S. Miller new->__use = 1;
2863352e512cSHerbert Xu new->input = dst_discard;
2864ede2059dSEric W. Biederman new->output = dst_discard_out;
286514e50e57SDavid S. Miller
28661dbe3252SWei Wang new->dev = net->loopback_dev;
2867d62607c3SJakub Kicinski netdev_hold(new->dev, &new->dev_tracker, GFP_ATOMIC);
286814e50e57SDavid S. Miller
28699917e1e8SDavid S. Miller rt->rt_is_input = ort->rt_is_input;
28705e2b61f7SDavid S. Miller rt->rt_iif = ort->rt_iif;
28715943634fSDavid S. Miller rt->rt_pmtu = ort->rt_pmtu;
2872d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = ort->rt_mtu_locked;
287314e50e57SDavid S. Miller
2874ca4c3fc2Sfan.du rt->rt_genid = rt_genid_ipv4(net);
287514e50e57SDavid S. Miller rt->rt_flags = ort->rt_flags;
287614e50e57SDavid S. Miller rt->rt_type = ort->rt_type;
287777d5bc7eSDavid Ahern rt->rt_uses_gateway = ort->rt_uses_gateway;
28781550c171SDavid Ahern rt->rt_gw_family = ort->rt_gw_family;
28791550c171SDavid Ahern if (rt->rt_gw_family == AF_INET)
28801550c171SDavid Ahern rt->rt_gw4 = ort->rt_gw4;
28810f5f7d7bSDavid Ahern else if (rt->rt_gw_family == AF_INET6)
28820f5f7d7bSDavid Ahern rt->rt_gw6 = ort->rt_gw6;
288314e50e57SDavid S. Miller }
288414e50e57SDavid S. Miller
28852774c131SDavid S. Miller dst_release(dst_orig);
28862774c131SDavid S. Miller
28872774c131SDavid S. Miller return rt ? &rt->dst : ERR_PTR(-ENOMEM);
288814e50e57SDavid S. Miller }
288914e50e57SDavid S. Miller
ip_route_output_flow(struct net * net,struct flowi4 * flp4,const struct sock * sk)28909d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
28916f9c9615SEric Dumazet const struct sock *sk)
28921da177e4SLinus Torvalds {
28939d6ec938SDavid S. Miller struct rtable *rt = __ip_route_output_key(net, flp4);
28941da177e4SLinus Torvalds
2895b23dd4feSDavid S. Miller if (IS_ERR(rt))
2896b23dd4feSDavid S. Miller return rt;
28971da177e4SLinus Torvalds
2898874fb9e2SDavid Ahern if (flp4->flowi4_proto) {
2899874fb9e2SDavid Ahern flp4->flowi4_oif = rt->dst.dev->ifindex;
2900f92ee619SSteffen Klassert rt = (struct rtable *)xfrm_lookup_route(net, &rt->dst,
29019d6ec938SDavid S. Miller flowi4_to_flowi(flp4),
29029d6ec938SDavid S. Miller sk, 0);
2903874fb9e2SDavid Ahern }
29041da177e4SLinus Torvalds
2905b23dd4feSDavid S. Miller return rt;
29061da177e4SLinus Torvalds }
2907d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow);
2908d8c97a94SArnaldo Carvalho de Melo
ip_route_output_tunnel(struct sk_buff * skb,struct net_device * dev,struct net * net,__be32 * saddr,const struct ip_tunnel_info * info,u8 protocol,bool use_cache)2909571912c6SMartin Varghese struct rtable *ip_route_output_tunnel(struct sk_buff *skb,
2910571912c6SMartin Varghese struct net_device *dev,
2911571912c6SMartin Varghese struct net *net, __be32 *saddr,
2912571912c6SMartin Varghese const struct ip_tunnel_info *info,
2913571912c6SMartin Varghese u8 protocol, bool use_cache)
2914571912c6SMartin Varghese {
2915571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2916571912c6SMartin Varghese struct dst_cache *dst_cache;
2917571912c6SMartin Varghese #endif
2918571912c6SMartin Varghese struct rtable *rt = NULL;
2919571912c6SMartin Varghese struct flowi4 fl4;
2920571912c6SMartin Varghese __u8 tos;
2921571912c6SMartin Varghese
2922571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2923571912c6SMartin Varghese dst_cache = (struct dst_cache *)&info->dst_cache;
2924571912c6SMartin Varghese if (use_cache) {
2925571912c6SMartin Varghese rt = dst_cache_get_ip4(dst_cache, saddr);
2926571912c6SMartin Varghese if (rt)
2927571912c6SMartin Varghese return rt;
2928571912c6SMartin Varghese }
2929571912c6SMartin Varghese #endif
2930571912c6SMartin Varghese memset(&fl4, 0, sizeof(fl4));
2931571912c6SMartin Varghese fl4.flowi4_mark = skb->mark;
2932571912c6SMartin Varghese fl4.flowi4_proto = protocol;
2933571912c6SMartin Varghese fl4.daddr = info->key.u.ipv4.dst;
2934571912c6SMartin Varghese fl4.saddr = info->key.u.ipv4.src;
2935571912c6SMartin Varghese tos = info->key.tos;
2936571912c6SMartin Varghese fl4.flowi4_tos = RT_TOS(tos);
2937571912c6SMartin Varghese
2938571912c6SMartin Varghese rt = ip_route_output_key(net, &fl4);
2939571912c6SMartin Varghese if (IS_ERR(rt)) {
2940571912c6SMartin Varghese netdev_dbg(dev, "no route to %pI4\n", &fl4.daddr);
2941571912c6SMartin Varghese return ERR_PTR(-ENETUNREACH);
2942571912c6SMartin Varghese }
2943571912c6SMartin Varghese if (rt->dst.dev == dev) { /* is this necessary? */
2944571912c6SMartin Varghese netdev_dbg(dev, "circular route to %pI4\n", &fl4.daddr);
2945571912c6SMartin Varghese ip_rt_put(rt);
2946571912c6SMartin Varghese return ERR_PTR(-ELOOP);
2947571912c6SMartin Varghese }
2948571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
2949571912c6SMartin Varghese if (use_cache)
2950571912c6SMartin Varghese dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2951571912c6SMartin Varghese #endif
2952571912c6SMartin Varghese *saddr = fl4.saddr;
2953571912c6SMartin Varghese return rt;
2954571912c6SMartin Varghese }
2955571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip_route_output_tunnel);
2956571912c6SMartin Varghese
29573765d35eSDavid Ahern /* called with rcu_read_lock held */
rt_fill_info(struct net * net,__be32 dst,__be32 src,struct rtable * rt,u32 table_id,dscp_t dscp,struct flowi4 * fl4,struct sk_buff * skb,u32 portid,u32 seq,unsigned int flags)2958404eb77eSRoopa Prabhu static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
2959015d29dbSIdo Schimmel struct rtable *rt, u32 table_id, dscp_t dscp,
2960015d29dbSIdo Schimmel struct flowi4 *fl4, struct sk_buff *skb, u32 portid,
2961015d29dbSIdo Schimmel u32 seq, unsigned int flags)
29621da177e4SLinus Torvalds {
29631da177e4SLinus Torvalds struct rtmsg *r;
29641da177e4SLinus Torvalds struct nlmsghdr *nlh;
29652bc8ca40SSteffen Klassert unsigned long expires = 0;
2966f185071dSDavid S. Miller u32 error;
2967521f5490SJulian Anastasov u32 metrics[RTAX_MAX];
2968be403ea1SThomas Graf
2969e93fb3e9SJohn Fastabend nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags);
297051456b29SIan Morris if (!nlh)
297126932566SPatrick McHardy return -EMSGSIZE;
2972be403ea1SThomas Graf
2973be403ea1SThomas Graf r = nlmsg_data(nlh);
29741da177e4SLinus Torvalds r->rtm_family = AF_INET;
29751da177e4SLinus Torvalds r->rtm_dst_len = 32;
29761da177e4SLinus Torvalds r->rtm_src_len = 0;
2977015d29dbSIdo Schimmel r->rtm_tos = inet_dscp_to_dsfield(dscp);
29788a430ed5SDavid Ahern r->rtm_table = table_id < 256 ? table_id : RT_TABLE_COMPAT;
2979c36ba660SDavid Ahern if (nla_put_u32(skb, RTA_TABLE, table_id))
2980f3756b79SDavid S. Miller goto nla_put_failure;
29811da177e4SLinus Torvalds r->rtm_type = rt->rt_type;
29821da177e4SLinus Torvalds r->rtm_scope = RT_SCOPE_UNIVERSE;
29831da177e4SLinus Torvalds r->rtm_protocol = RTPROT_UNSPEC;
29841da177e4SLinus Torvalds r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
29851da177e4SLinus Torvalds if (rt->rt_flags & RTCF_NOTIFY)
29861da177e4SLinus Torvalds r->rtm_flags |= RTM_F_NOTIFY;
2987df4d9254SHannes Frederic Sowa if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
2988df4d9254SHannes Frederic Sowa r->rtm_flags |= RTCF_DOREDIRECT;
2989be403ea1SThomas Graf
2990930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_DST, dst))
2991f3756b79SDavid S. Miller goto nla_put_failure;
29921a00fee4SDavid Miller if (src) {
29931da177e4SLinus Torvalds r->rtm_src_len = 32;
2994930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_SRC, src))
2995f3756b79SDavid S. Miller goto nla_put_failure;
29961da177e4SLinus Torvalds }
2997f3756b79SDavid S. Miller if (rt->dst.dev &&
2998f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2999f3756b79SDavid S. Miller goto nla_put_failure;
3000ae8cb932SOliver Herms if (rt->dst.lwtstate &&
3001ae8cb932SOliver Herms lwtunnel_fill_encap(skb, rt->dst.lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
3002ae8cb932SOliver Herms goto nla_put_failure;
3003c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
3004f3756b79SDavid S. Miller if (rt->dst.tclassid &&
3005f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
3006f3756b79SDavid S. Miller goto nla_put_failure;
30071da177e4SLinus Torvalds #endif
3008d948974cSStefano Brivio if (fl4 && !rt_is_input_route(rt) &&
3009d6c0a4f6SDavid Miller fl4->saddr != src) {
3010930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
3011f3756b79SDavid S. Miller goto nla_put_failure;
3012f3756b79SDavid S. Miller }
301377d5bc7eSDavid Ahern if (rt->rt_uses_gateway) {
30141550c171SDavid Ahern if (rt->rt_gw_family == AF_INET &&
30150f5f7d7bSDavid Ahern nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) {
3016f3756b79SDavid S. Miller goto nla_put_failure;
30170f5f7d7bSDavid Ahern } else if (rt->rt_gw_family == AF_INET6) {
30180f5f7d7bSDavid Ahern int alen = sizeof(struct in6_addr);
30190f5f7d7bSDavid Ahern struct nlattr *nla;
30200f5f7d7bSDavid Ahern struct rtvia *via;
30210f5f7d7bSDavid Ahern
30220f5f7d7bSDavid Ahern nla = nla_reserve(skb, RTA_VIA, alen + 2);
30230f5f7d7bSDavid Ahern if (!nla)
30240f5f7d7bSDavid Ahern goto nla_put_failure;
30250f5f7d7bSDavid Ahern
30260f5f7d7bSDavid Ahern via = nla_data(nla);
30270f5f7d7bSDavid Ahern via->rtvia_family = AF_INET6;
30280f5f7d7bSDavid Ahern memcpy(via->rtvia_addr, &rt->rt_gw6, alen);
30290f5f7d7bSDavid Ahern }
303077d5bc7eSDavid Ahern }
3031be403ea1SThomas Graf
3032ee9a8f7aSSteffen Klassert expires = rt->dst.expires;
3033ee9a8f7aSSteffen Klassert if (expires) {
3034ee9a8f7aSSteffen Klassert unsigned long now = jiffies;
3035ee9a8f7aSSteffen Klassert
3036ee9a8f7aSSteffen Klassert if (time_before(now, expires))
3037ee9a8f7aSSteffen Klassert expires -= now;
3038ee9a8f7aSSteffen Klassert else
3039ee9a8f7aSSteffen Klassert expires = 0;
3040ee9a8f7aSSteffen Klassert }
3041ee9a8f7aSSteffen Klassert
3042521f5490SJulian Anastasov memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
3043ee9a8f7aSSteffen Klassert if (rt->rt_pmtu && expires)
3044521f5490SJulian Anastasov metrics[RTAX_MTU - 1] = rt->rt_pmtu;
3045d52e5a7eSSabrina Dubroca if (rt->rt_mtu_locked && expires)
3046d52e5a7eSSabrina Dubroca metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU);
3047521f5490SJulian Anastasov if (rtnetlink_put_metrics(skb, metrics) < 0)
3048be403ea1SThomas Graf goto nla_put_failure;
3049be403ea1SThomas Graf
3050d948974cSStefano Brivio if (fl4) {
3051b4869889SDavid Miller if (fl4->flowi4_mark &&
305268aaed54Sstephen hemminger nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark))
3053f3756b79SDavid S. Miller goto nla_put_failure;
3054963bfeeeSEric Dumazet
3055622ec2c9SLorenzo Colitti if (!uid_eq(fl4->flowi4_uid, INVALID_UID) &&
3056622ec2c9SLorenzo Colitti nla_put_u32(skb, RTA_UID,
3057d948974cSStefano Brivio from_kuid_munged(current_user_ns(),
3058d948974cSStefano Brivio fl4->flowi4_uid)))
3059622ec2c9SLorenzo Colitti goto nla_put_failure;
3060622ec2c9SLorenzo Colitti
3061c7537967SDavid S. Miller if (rt_is_input_route(rt)) {
30628caaf7b6SNicolas Dichtel #ifdef CONFIG_IP_MROUTE
3063d948974cSStefano Brivio if (ipv4_is_multicast(dst) &&
3064d948974cSStefano Brivio !ipv4_is_local_multicast(dst) &&
30658caaf7b6SNicolas Dichtel IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
30668caaf7b6SNicolas Dichtel int err = ipmr_get_route(net, skb,
30678caaf7b6SNicolas Dichtel fl4->saddr, fl4->daddr,
30689f09eaeaSDavid Ahern r, portid);
30692cf75070SNikolay Aleksandrov
30708caaf7b6SNicolas Dichtel if (err <= 0) {
30718caaf7b6SNicolas Dichtel if (err == 0)
30728caaf7b6SNicolas Dichtel return 0;
30738caaf7b6SNicolas Dichtel goto nla_put_failure;
30748caaf7b6SNicolas Dichtel }
30758caaf7b6SNicolas Dichtel } else
30768caaf7b6SNicolas Dichtel #endif
3077404eb77eSRoopa Prabhu if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif))
3078f3756b79SDavid S. Miller goto nla_put_failure;
30791da177e4SLinus Torvalds }
3080d948974cSStefano Brivio }
3081d948974cSStefano Brivio
3082d948974cSStefano Brivio error = rt->dst.error;
30831da177e4SLinus Torvalds
3084f185071dSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
3085e3703b3dSThomas Graf goto nla_put_failure;
30861da177e4SLinus Torvalds
3087053c095aSJohannes Berg nlmsg_end(skb, nlh);
3088053c095aSJohannes Berg return 0;
3089be403ea1SThomas Graf
3090be403ea1SThomas Graf nla_put_failure:
309126932566SPatrick McHardy nlmsg_cancel(skb, nlh);
309226932566SPatrick McHardy return -EMSGSIZE;
30931da177e4SLinus Torvalds }
30941da177e4SLinus Torvalds
fnhe_dump_bucket(struct net * net,struct sk_buff * skb,struct netlink_callback * cb,u32 table_id,struct fnhe_hash_bucket * bucket,int genid,int * fa_index,int fa_start,unsigned int flags)3095ee28906fSStefano Brivio static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb,
3096ee28906fSStefano Brivio struct netlink_callback *cb, u32 table_id,
3097ee28906fSStefano Brivio struct fnhe_hash_bucket *bucket, int genid,
3098e93fb3e9SJohn Fastabend int *fa_index, int fa_start, unsigned int flags)
3099ee28906fSStefano Brivio {
3100ee28906fSStefano Brivio int i;
3101ee28906fSStefano Brivio
3102ee28906fSStefano Brivio for (i = 0; i < FNHE_HASH_SIZE; i++) {
3103ee28906fSStefano Brivio struct fib_nh_exception *fnhe;
3104ee28906fSStefano Brivio
3105ee28906fSStefano Brivio for (fnhe = rcu_dereference(bucket[i].chain); fnhe;
3106ee28906fSStefano Brivio fnhe = rcu_dereference(fnhe->fnhe_next)) {
3107ee28906fSStefano Brivio struct rtable *rt;
3108ee28906fSStefano Brivio int err;
3109ee28906fSStefano Brivio
3110ee28906fSStefano Brivio if (*fa_index < fa_start)
3111ee28906fSStefano Brivio goto next;
3112ee28906fSStefano Brivio
3113ee28906fSStefano Brivio if (fnhe->fnhe_genid != genid)
3114ee28906fSStefano Brivio goto next;
3115ee28906fSStefano Brivio
3116ee28906fSStefano Brivio if (fnhe->fnhe_expires &&
3117ee28906fSStefano Brivio time_after(jiffies, fnhe->fnhe_expires))
3118ee28906fSStefano Brivio goto next;
3119ee28906fSStefano Brivio
3120ee28906fSStefano Brivio rt = rcu_dereference(fnhe->fnhe_rth_input);
3121ee28906fSStefano Brivio if (!rt)
3122ee28906fSStefano Brivio rt = rcu_dereference(fnhe->fnhe_rth_output);
3123ee28906fSStefano Brivio if (!rt)
3124ee28906fSStefano Brivio goto next;
3125ee28906fSStefano Brivio
3126ee28906fSStefano Brivio err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt,
3127015d29dbSIdo Schimmel table_id, 0, NULL, skb,
3128ee28906fSStefano Brivio NETLINK_CB(cb->skb).portid,
3129e93fb3e9SJohn Fastabend cb->nlh->nlmsg_seq, flags);
3130ee28906fSStefano Brivio if (err)
3131ee28906fSStefano Brivio return err;
3132ee28906fSStefano Brivio next:
3133ee28906fSStefano Brivio (*fa_index)++;
3134ee28906fSStefano Brivio }
3135ee28906fSStefano Brivio }
3136ee28906fSStefano Brivio
3137ee28906fSStefano Brivio return 0;
3138ee28906fSStefano Brivio }
3139ee28906fSStefano Brivio
fib_dump_info_fnhe(struct sk_buff * skb,struct netlink_callback * cb,u32 table_id,struct fib_info * fi,int * fa_index,int fa_start,unsigned int flags)3140ee28906fSStefano Brivio int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
3141ee28906fSStefano Brivio u32 table_id, struct fib_info *fi,
3142e93fb3e9SJohn Fastabend int *fa_index, int fa_start, unsigned int flags)
3143ee28906fSStefano Brivio {
3144ee28906fSStefano Brivio struct net *net = sock_net(cb->skb->sk);
3145ee28906fSStefano Brivio int nhsel, genid = fnhe_genid(net);
3146ee28906fSStefano Brivio
3147ee28906fSStefano Brivio for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
3148ee28906fSStefano Brivio struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel);
3149ee28906fSStefano Brivio struct fnhe_hash_bucket *bucket;
3150ee28906fSStefano Brivio int err;
3151ee28906fSStefano Brivio
3152ee28906fSStefano Brivio if (nhc->nhc_flags & RTNH_F_DEAD)
3153ee28906fSStefano Brivio continue;
3154ee28906fSStefano Brivio
315593ed54b1SEric Dumazet rcu_read_lock();
3156ee28906fSStefano Brivio bucket = rcu_dereference(nhc->nhc_exceptions);
315793ed54b1SEric Dumazet err = 0;
315893ed54b1SEric Dumazet if (bucket)
315993ed54b1SEric Dumazet err = fnhe_dump_bucket(net, skb, cb, table_id, bucket,
3160e93fb3e9SJohn Fastabend genid, fa_index, fa_start,
3161e93fb3e9SJohn Fastabend flags);
316293ed54b1SEric Dumazet rcu_read_unlock();
3163ee28906fSStefano Brivio if (err)
3164ee28906fSStefano Brivio return err;
3165ee28906fSStefano Brivio }
3166ee28906fSStefano Brivio
3167ee28906fSStefano Brivio return 0;
3168ee28906fSStefano Brivio }
3169ee28906fSStefano Brivio
inet_rtm_getroute_build_skb(__be32 src,__be32 dst,u8 ip_proto,__be16 sport,__be16 dport)3170404eb77eSRoopa Prabhu static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst,
3171404eb77eSRoopa Prabhu u8 ip_proto, __be16 sport,
3172404eb77eSRoopa Prabhu __be16 dport)
3173404eb77eSRoopa Prabhu {
3174404eb77eSRoopa Prabhu struct sk_buff *skb;
3175404eb77eSRoopa Prabhu struct iphdr *iph;
3176404eb77eSRoopa Prabhu
3177404eb77eSRoopa Prabhu skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3178404eb77eSRoopa Prabhu if (!skb)
3179404eb77eSRoopa Prabhu return NULL;
3180404eb77eSRoopa Prabhu
3181404eb77eSRoopa Prabhu /* Reserve room for dummy headers, this skb can pass
3182404eb77eSRoopa Prabhu * through good chunk of routing engine.
3183404eb77eSRoopa Prabhu */
3184404eb77eSRoopa Prabhu skb_reset_mac_header(skb);
3185404eb77eSRoopa Prabhu skb_reset_network_header(skb);
3186404eb77eSRoopa Prabhu skb->protocol = htons(ETH_P_IP);
3187404eb77eSRoopa Prabhu iph = skb_put(skb, sizeof(struct iphdr));
3188404eb77eSRoopa Prabhu iph->protocol = ip_proto;
3189404eb77eSRoopa Prabhu iph->saddr = src;
3190404eb77eSRoopa Prabhu iph->daddr = dst;
3191404eb77eSRoopa Prabhu iph->version = 0x4;
3192404eb77eSRoopa Prabhu iph->frag_off = 0;
3193404eb77eSRoopa Prabhu iph->ihl = 0x5;
3194404eb77eSRoopa Prabhu skb_set_transport_header(skb, skb->len);
3195404eb77eSRoopa Prabhu
3196404eb77eSRoopa Prabhu switch (iph->protocol) {
3197404eb77eSRoopa Prabhu case IPPROTO_UDP: {
3198404eb77eSRoopa Prabhu struct udphdr *udph;
3199404eb77eSRoopa Prabhu
3200404eb77eSRoopa Prabhu udph = skb_put_zero(skb, sizeof(struct udphdr));
3201404eb77eSRoopa Prabhu udph->source = sport;
3202404eb77eSRoopa Prabhu udph->dest = dport;
320392548b0eSEric Dumazet udph->len = htons(sizeof(struct udphdr));
3204404eb77eSRoopa Prabhu udph->check = 0;
3205404eb77eSRoopa Prabhu break;
3206404eb77eSRoopa Prabhu }
3207404eb77eSRoopa Prabhu case IPPROTO_TCP: {
3208404eb77eSRoopa Prabhu struct tcphdr *tcph;
3209404eb77eSRoopa Prabhu
3210404eb77eSRoopa Prabhu tcph = skb_put_zero(skb, sizeof(struct tcphdr));
3211404eb77eSRoopa Prabhu tcph->source = sport;
3212404eb77eSRoopa Prabhu tcph->dest = dport;
3213404eb77eSRoopa Prabhu tcph->doff = sizeof(struct tcphdr) / 4;
3214404eb77eSRoopa Prabhu tcph->rst = 1;
3215404eb77eSRoopa Prabhu tcph->check = ~tcp_v4_check(sizeof(struct tcphdr),
3216404eb77eSRoopa Prabhu src, dst, 0);
3217404eb77eSRoopa Prabhu break;
3218404eb77eSRoopa Prabhu }
3219404eb77eSRoopa Prabhu case IPPROTO_ICMP: {
3220404eb77eSRoopa Prabhu struct icmphdr *icmph;
3221404eb77eSRoopa Prabhu
3222404eb77eSRoopa Prabhu icmph = skb_put_zero(skb, sizeof(struct icmphdr));
3223404eb77eSRoopa Prabhu icmph->type = ICMP_ECHO;
3224404eb77eSRoopa Prabhu icmph->code = 0;
3225404eb77eSRoopa Prabhu }
3226404eb77eSRoopa Prabhu }
3227404eb77eSRoopa Prabhu
3228404eb77eSRoopa Prabhu return skb;
3229404eb77eSRoopa Prabhu }
3230404eb77eSRoopa Prabhu
inet_rtm_valid_getroute_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)3231a00302b6SJakub Kicinski static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
3232a00302b6SJakub Kicinski const struct nlmsghdr *nlh,
3233a00302b6SJakub Kicinski struct nlattr **tb,
3234a00302b6SJakub Kicinski struct netlink_ext_ack *extack)
3235a00302b6SJakub Kicinski {
3236a00302b6SJakub Kicinski struct rtmsg *rtm;
3237a00302b6SJakub Kicinski int i, err;
3238a00302b6SJakub Kicinski
3239a00302b6SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
3240a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack,
3241a00302b6SJakub Kicinski "ipv4: Invalid header for route get request");
3242a00302b6SJakub Kicinski return -EINVAL;
3243a00302b6SJakub Kicinski }
3244a00302b6SJakub Kicinski
3245a00302b6SJakub Kicinski if (!netlink_strict_get_check(skb))
32468cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
3247a00302b6SJakub Kicinski rtm_ipv4_policy, extack);
3248a00302b6SJakub Kicinski
3249a00302b6SJakub Kicinski rtm = nlmsg_data(nlh);
3250a00302b6SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
3251a00302b6SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
3252a00302b6SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol ||
3253a00302b6SJakub Kicinski rtm->rtm_scope || rtm->rtm_type) {
3254a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request");
3255a00302b6SJakub Kicinski return -EINVAL;
3256a00302b6SJakub Kicinski }
3257a00302b6SJakub Kicinski
3258a00302b6SJakub Kicinski if (rtm->rtm_flags & ~(RTM_F_NOTIFY |
3259a00302b6SJakub Kicinski RTM_F_LOOKUP_TABLE |
3260a00302b6SJakub Kicinski RTM_F_FIB_MATCH)) {
3261a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request");
3262a00302b6SJakub Kicinski return -EINVAL;
3263a00302b6SJakub Kicinski }
3264a00302b6SJakub Kicinski
32658cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
3266a00302b6SJakub Kicinski rtm_ipv4_policy, extack);
3267a00302b6SJakub Kicinski if (err)
3268a00302b6SJakub Kicinski return err;
3269a00302b6SJakub Kicinski
3270a00302b6SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
3271a00302b6SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) {
3272a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
3273a00302b6SJakub Kicinski return -EINVAL;
3274a00302b6SJakub Kicinski }
3275a00302b6SJakub Kicinski
3276a00302b6SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) {
3277a00302b6SJakub Kicinski if (!tb[i])
3278a00302b6SJakub Kicinski continue;
3279a00302b6SJakub Kicinski
3280a00302b6SJakub Kicinski switch (i) {
3281a00302b6SJakub Kicinski case RTA_IIF:
3282a00302b6SJakub Kicinski case RTA_OIF:
3283a00302b6SJakub Kicinski case RTA_SRC:
3284a00302b6SJakub Kicinski case RTA_DST:
3285a00302b6SJakub Kicinski case RTA_IP_PROTO:
3286a00302b6SJakub Kicinski case RTA_SPORT:
3287a00302b6SJakub Kicinski case RTA_DPORT:
3288a00302b6SJakub Kicinski case RTA_MARK:
3289a00302b6SJakub Kicinski case RTA_UID:
3290a00302b6SJakub Kicinski break;
3291a00302b6SJakub Kicinski default:
3292a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request");
3293a00302b6SJakub Kicinski return -EINVAL;
3294a00302b6SJakub Kicinski }
3295a00302b6SJakub Kicinski }
3296a00302b6SJakub Kicinski
3297a00302b6SJakub Kicinski return 0;
3298a00302b6SJakub Kicinski }
3299a00302b6SJakub Kicinski
inet_rtm_getroute(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3300c21ef3e3SDavid Ahern static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
3301c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
33021da177e4SLinus Torvalds {
33033b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk);
3304d889ce3bSThomas Graf struct nlattr *tb[RTA_MAX+1];
3305404eb77eSRoopa Prabhu u32 table_id = RT_TABLE_MAIN;
3306404eb77eSRoopa Prabhu __be16 sport = 0, dport = 0;
33073765d35eSDavid Ahern struct fib_result res = {};
3308404eb77eSRoopa Prabhu u8 ip_proto = IPPROTO_UDP;
33091da177e4SLinus Torvalds struct rtable *rt = NULL;
3310404eb77eSRoopa Prabhu struct sk_buff *skb;
3311404eb77eSRoopa Prabhu struct rtmsg *rtm;
3312e8e3fbe9SMaciej Żenczykowski struct flowi4 fl4 = {};
33139e12bb22SAl Viro __be32 dst = 0;
33149e12bb22SAl Viro __be32 src = 0;
3315404eb77eSRoopa Prabhu kuid_t uid;
33169e12bb22SAl Viro u32 iif;
3317d889ce3bSThomas Graf int err;
3318963bfeeeSEric Dumazet int mark;
33191da177e4SLinus Torvalds
3320a00302b6SJakub Kicinski err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
3321d889ce3bSThomas Graf if (err < 0)
3322404eb77eSRoopa Prabhu return err;
3323d889ce3bSThomas Graf
3324d889ce3bSThomas Graf rtm = nlmsg_data(nlh);
332567b61f6cSJiri Benc src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
332667b61f6cSJiri Benc dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
3327d889ce3bSThomas Graf iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
3328963bfeeeSEric Dumazet mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
3329622ec2c9SLorenzo Colitti if (tb[RTA_UID])
3330622ec2c9SLorenzo Colitti uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID]));
3331622ec2c9SLorenzo Colitti else
3332622ec2c9SLorenzo Colitti uid = (iif ? INVALID_UID : current_uid());
33331da177e4SLinus Torvalds
3334404eb77eSRoopa Prabhu if (tb[RTA_IP_PROTO]) {
3335404eb77eSRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
33365e1a99eaSHangbin Liu &ip_proto, AF_INET, extack);
3337404eb77eSRoopa Prabhu if (err)
3338404eb77eSRoopa Prabhu return err;
3339404eb77eSRoopa Prabhu }
3340bbadb9a2SFlorian Larysch
3341404eb77eSRoopa Prabhu if (tb[RTA_SPORT])
3342404eb77eSRoopa Prabhu sport = nla_get_be16(tb[RTA_SPORT]);
3343404eb77eSRoopa Prabhu
3344404eb77eSRoopa Prabhu if (tb[RTA_DPORT])
3345404eb77eSRoopa Prabhu dport = nla_get_be16(tb[RTA_DPORT]);
3346404eb77eSRoopa Prabhu
3347404eb77eSRoopa Prabhu skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport);
3348404eb77eSRoopa Prabhu if (!skb)
3349404eb77eSRoopa Prabhu return -ENOBUFS;
3350bbadb9a2SFlorian Larysch
3351d6c0a4f6SDavid Miller fl4.daddr = dst;
3352d6c0a4f6SDavid Miller fl4.saddr = src;
33531ebf1790SGuillaume Nault fl4.flowi4_tos = rtm->rtm_tos & IPTOS_RT_MASK;
3354d6c0a4f6SDavid Miller fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
3355d6c0a4f6SDavid Miller fl4.flowi4_mark = mark;
3356622ec2c9SLorenzo Colitti fl4.flowi4_uid = uid;
3357404eb77eSRoopa Prabhu if (sport)
3358404eb77eSRoopa Prabhu fl4.fl4_sport = sport;
3359404eb77eSRoopa Prabhu if (dport)
3360404eb77eSRoopa Prabhu fl4.fl4_dport = dport;
3361404eb77eSRoopa Prabhu fl4.flowi4_proto = ip_proto;
3362d6c0a4f6SDavid Miller
33633765d35eSDavid Ahern rcu_read_lock();
33643765d35eSDavid Ahern
33651da177e4SLinus Torvalds if (iif) {
3366d889ce3bSThomas Graf struct net_device *dev;
3367d889ce3bSThomas Graf
33683765d35eSDavid Ahern dev = dev_get_by_index_rcu(net, iif);
336951456b29SIan Morris if (!dev) {
33701da177e4SLinus Torvalds err = -ENODEV;
3371404eb77eSRoopa Prabhu goto errout_rcu;
3372d889ce3bSThomas Graf }
3373d889ce3bSThomas Graf
3374404eb77eSRoopa Prabhu fl4.flowi4_iif = iif; /* for rt_fill_info */
33751da177e4SLinus Torvalds skb->dev = dev;
3376963bfeeeSEric Dumazet skb->mark = mark;
33771ebf1790SGuillaume Nault err = ip_route_input_rcu(skb, dst, src,
33781ebf1790SGuillaume Nault rtm->rtm_tos & IPTOS_RT_MASK, dev,
33791ebf1790SGuillaume Nault &res);
3380d889ce3bSThomas Graf
3381511c3f92SEric Dumazet rt = skb_rtable(skb);
3382d8d1f30bSChangli Gao if (err == 0 && rt->dst.error)
3383d8d1f30bSChangli Gao err = -rt->dst.error;
33841da177e4SLinus Torvalds } else {
33856503a304SLorenzo Colitti fl4.flowi4_iif = LOOPBACK_IFINDEX;
338621f94775SIdo Schimmel skb->dev = net->loopback_dev;
33873765d35eSDavid Ahern rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb);
3388b23dd4feSDavid S. Miller err = 0;
3389b23dd4feSDavid S. Miller if (IS_ERR(rt))
3390b23dd4feSDavid S. Miller err = PTR_ERR(rt);
33912c87d63aSFlorian Westphal else
33922c87d63aSFlorian Westphal skb_dst_set(skb, &rt->dst);
33931da177e4SLinus Torvalds }
3394d889ce3bSThomas Graf
33951da177e4SLinus Torvalds if (err)
3396404eb77eSRoopa Prabhu goto errout_rcu;
33971da177e4SLinus Torvalds
33981da177e4SLinus Torvalds if (rtm->rtm_flags & RTM_F_NOTIFY)
33991da177e4SLinus Torvalds rt->rt_flags |= RTCF_NOTIFY;
34001da177e4SLinus Torvalds
3401c36ba660SDavid Ahern if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE)
340268e813aaSDavid Ahern table_id = res.table ? res.table->tb_id : 0;
3403c36ba660SDavid Ahern
3404404eb77eSRoopa Prabhu /* reset skb for netlink reply msg */
3405404eb77eSRoopa Prabhu skb_trim(skb, 0);
3406404eb77eSRoopa Prabhu skb_reset_network_header(skb);
3407404eb77eSRoopa Prabhu skb_reset_transport_header(skb);
3408404eb77eSRoopa Prabhu skb_reset_mac_header(skb);
3409404eb77eSRoopa Prabhu
3410bc3aae2bSRoopa Prabhu if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
34111e301fd0SIdo Schimmel struct fib_rt_info fri;
34121e301fd0SIdo Schimmel
3413bc3aae2bSRoopa Prabhu if (!res.fi) {
3414bc3aae2bSRoopa Prabhu err = fib_props[res.type].error;
3415bc3aae2bSRoopa Prabhu if (!err)
3416bc3aae2bSRoopa Prabhu err = -EHOSTUNREACH;
3417404eb77eSRoopa Prabhu goto errout_rcu;
3418bc3aae2bSRoopa Prabhu }
34191e301fd0SIdo Schimmel fri.fi = res.fi;
34201e301fd0SIdo Schimmel fri.tb_id = table_id;
34211e301fd0SIdo Schimmel fri.dst = res.prefix;
34221e301fd0SIdo Schimmel fri.dst_len = res.prefixlen;
3423935dec4eSIdo Schimmel fri.dscp = res.dscp;
34241e301fd0SIdo Schimmel fri.type = rt->rt_type;
342590b93f1bSIdo Schimmel fri.offload = 0;
342690b93f1bSIdo Schimmel fri.trap = 0;
342736c5100eSAmit Cohen fri.offload_failed = 0;
342890b93f1bSIdo Schimmel if (res.fa_head) {
342990b93f1bSIdo Schimmel struct fib_alias *fa;
343090b93f1bSIdo Schimmel
343190b93f1bSIdo Schimmel hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) {
343290b93f1bSIdo Schimmel u8 slen = 32 - fri.dst_len;
343390b93f1bSIdo Schimmel
343490b93f1bSIdo Schimmel if (fa->fa_slen == slen &&
343590b93f1bSIdo Schimmel fa->tb_id == fri.tb_id &&
3436888ade8fSGuillaume Nault fa->fa_dscp == fri.dscp &&
343790b93f1bSIdo Schimmel fa->fa_info == res.fi &&
343890b93f1bSIdo Schimmel fa->fa_type == fri.type) {
34399fcf986cSEric Dumazet fri.offload = READ_ONCE(fa->offload);
34409fcf986cSEric Dumazet fri.trap = READ_ONCE(fa->trap);
34410add5c59SBenjamin Poirier fri.offload_failed =
34420add5c59SBenjamin Poirier READ_ONCE(fa->offload_failed);
344390b93f1bSIdo Schimmel break;
344490b93f1bSIdo Schimmel }
344590b93f1bSIdo Schimmel }
344690b93f1bSIdo Schimmel }
3447b6179813SRoopa Prabhu err = fib_dump_info(skb, NETLINK_CB(in_skb).portid,
34481e301fd0SIdo Schimmel nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0);
3449bc3aae2bSRoopa Prabhu } else {
3450015d29dbSIdo Schimmel err = rt_fill_info(net, dst, src, rt, table_id, res.dscp, &fl4,
3451015d29dbSIdo Schimmel skb, NETLINK_CB(in_skb).portid,
3452e93fb3e9SJohn Fastabend nlh->nlmsg_seq, 0);
3453bc3aae2bSRoopa Prabhu }
34547b46a644SDavid S. Miller if (err < 0)
3455404eb77eSRoopa Prabhu goto errout_rcu;
34561da177e4SLinus Torvalds
34573765d35eSDavid Ahern rcu_read_unlock();
34583765d35eSDavid Ahern
345915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
34601da177e4SLinus Torvalds
3461d889ce3bSThomas Graf errout_free:
3462404eb77eSRoopa Prabhu return err;
3463404eb77eSRoopa Prabhu errout_rcu:
34643765d35eSDavid Ahern rcu_read_unlock();
34651da177e4SLinus Torvalds kfree_skb(skb);
3466404eb77eSRoopa Prabhu goto errout_free;
34671da177e4SLinus Torvalds }
34681da177e4SLinus Torvalds
ip_rt_multicast_event(struct in_device * in_dev)34691da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev)
34701da177e4SLinus Torvalds {
34714ccfe6d4SNicolas Dichtel rt_cache_flush(dev_net(in_dev->dev));
34721da177e4SLinus Torvalds }
34731da177e4SLinus Torvalds
34741da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
3475082c7ca4SGao feng static int ip_rt_gc_interval __read_mostly = 60 * HZ;
3476082c7ca4SGao feng static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
3477082c7ca4SGao feng static int ip_rt_gc_elasticity __read_mostly = 8;
3478773daa3cSArnd Bergmann static int ip_min_valid_pmtu __read_mostly = IPV4_MIN_MTU;
3479082c7ca4SGao feng
ipv4_sysctl_rtcache_flush(struct ctl_table * __ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)3480fe2c6338SJoe Perches static int ipv4_sysctl_rtcache_flush(struct ctl_table *__ctl, int write,
348132927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
34821da177e4SLinus Torvalds {
34835aad1de5STimo Teräs struct net *net = (struct net *)__ctl->extra1;
34845aad1de5STimo Teräs
34851da177e4SLinus Torvalds if (write) {
34865aad1de5STimo Teräs rt_cache_flush(net);
34875aad1de5STimo Teräs fnhe_genid_bump(net);
34881da177e4SLinus Torvalds return 0;
34891da177e4SLinus Torvalds }
34901da177e4SLinus Torvalds
34911da177e4SLinus Torvalds return -EINVAL;
34921da177e4SLinus Torvalds }
34931da177e4SLinus Torvalds
3494fe2c6338SJoe Perches static struct ctl_table ipv4_route_table[] = {
34951da177e4SLinus Torvalds {
34961da177e4SLinus Torvalds .procname = "gc_thresh",
34971da177e4SLinus Torvalds .data = &ipv4_dst_ops.gc_thresh,
34981da177e4SLinus Torvalds .maxlen = sizeof(int),
34991da177e4SLinus Torvalds .mode = 0644,
35006d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35011da177e4SLinus Torvalds },
35021da177e4SLinus Torvalds {
35031da177e4SLinus Torvalds .procname = "max_size",
35041da177e4SLinus Torvalds .data = &ip_rt_max_size,
35051da177e4SLinus Torvalds .maxlen = sizeof(int),
35061da177e4SLinus Torvalds .mode = 0644,
35076d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35081da177e4SLinus Torvalds },
35091da177e4SLinus Torvalds {
35101da177e4SLinus Torvalds /* Deprecated. Use gc_min_interval_ms */
35111da177e4SLinus Torvalds
35121da177e4SLinus Torvalds .procname = "gc_min_interval",
35131da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval,
35141da177e4SLinus Torvalds .maxlen = sizeof(int),
35151da177e4SLinus Torvalds .mode = 0644,
35166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies,
35171da177e4SLinus Torvalds },
35181da177e4SLinus Torvalds {
35191da177e4SLinus Torvalds .procname = "gc_min_interval_ms",
35201da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval,
35211da177e4SLinus Torvalds .maxlen = sizeof(int),
35221da177e4SLinus Torvalds .mode = 0644,
35236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies,
35241da177e4SLinus Torvalds },
35251da177e4SLinus Torvalds {
35261da177e4SLinus Torvalds .procname = "gc_timeout",
35271da177e4SLinus Torvalds .data = &ip_rt_gc_timeout,
35281da177e4SLinus Torvalds .maxlen = sizeof(int),
35291da177e4SLinus Torvalds .mode = 0644,
35306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies,
35311da177e4SLinus Torvalds },
35321da177e4SLinus Torvalds {
35339f28a2fcSEric Dumazet .procname = "gc_interval",
35349f28a2fcSEric Dumazet .data = &ip_rt_gc_interval,
35359f28a2fcSEric Dumazet .maxlen = sizeof(int),
35369f28a2fcSEric Dumazet .mode = 0644,
35379f28a2fcSEric Dumazet .proc_handler = proc_dointvec_jiffies,
35389f28a2fcSEric Dumazet },
35399f28a2fcSEric Dumazet {
35401da177e4SLinus Torvalds .procname = "redirect_load",
35411da177e4SLinus Torvalds .data = &ip_rt_redirect_load,
35421da177e4SLinus Torvalds .maxlen = sizeof(int),
35431da177e4SLinus Torvalds .mode = 0644,
35446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35451da177e4SLinus Torvalds },
35461da177e4SLinus Torvalds {
35471da177e4SLinus Torvalds .procname = "redirect_number",
35481da177e4SLinus Torvalds .data = &ip_rt_redirect_number,
35491da177e4SLinus Torvalds .maxlen = sizeof(int),
35501da177e4SLinus Torvalds .mode = 0644,
35516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35521da177e4SLinus Torvalds },
35531da177e4SLinus Torvalds {
35541da177e4SLinus Torvalds .procname = "redirect_silence",
35551da177e4SLinus Torvalds .data = &ip_rt_redirect_silence,
35561da177e4SLinus Torvalds .maxlen = sizeof(int),
35571da177e4SLinus Torvalds .mode = 0644,
35586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35591da177e4SLinus Torvalds },
35601da177e4SLinus Torvalds {
35611da177e4SLinus Torvalds .procname = "error_cost",
35621da177e4SLinus Torvalds .data = &ip_rt_error_cost,
35631da177e4SLinus Torvalds .maxlen = sizeof(int),
35641da177e4SLinus Torvalds .mode = 0644,
35656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35661da177e4SLinus Torvalds },
35671da177e4SLinus Torvalds {
35681da177e4SLinus Torvalds .procname = "error_burst",
35691da177e4SLinus Torvalds .data = &ip_rt_error_burst,
35701da177e4SLinus Torvalds .maxlen = sizeof(int),
35711da177e4SLinus Torvalds .mode = 0644,
35726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35731da177e4SLinus Torvalds },
35741da177e4SLinus Torvalds {
35751da177e4SLinus Torvalds .procname = "gc_elasticity",
35761da177e4SLinus Torvalds .data = &ip_rt_gc_elasticity,
35771da177e4SLinus Torvalds .maxlen = sizeof(int),
35781da177e4SLinus Torvalds .mode = 0644,
35796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
35801da177e4SLinus Torvalds },
3581f8572d8fSEric W. Biederman { }
35821da177e4SLinus Torvalds };
358339a23e75SDenis V. Lunev
35845cdda5f1SChristian Brauner static const char ipv4_route_flush_procname[] = "flush";
35855cdda5f1SChristian Brauner
35861de6b15aSxu xin static struct ctl_table ipv4_route_netns_table[] = {
358739a23e75SDenis V. Lunev {
35885cdda5f1SChristian Brauner .procname = ipv4_route_flush_procname,
358939a23e75SDenis V. Lunev .maxlen = sizeof(int),
359039a23e75SDenis V. Lunev .mode = 0200,
35916d9f239aSAlexey Dobriyan .proc_handler = ipv4_sysctl_rtcache_flush,
359239a23e75SDenis V. Lunev },
35931de6b15aSxu xin {
35941de6b15aSxu xin .procname = "min_pmtu",
35951de6b15aSxu xin .data = &init_net.ipv4.ip_rt_min_pmtu,
35961de6b15aSxu xin .maxlen = sizeof(int),
35971de6b15aSxu xin .mode = 0644,
35981de6b15aSxu xin .proc_handler = proc_dointvec_minmax,
35991de6b15aSxu xin .extra1 = &ip_min_valid_pmtu,
36001de6b15aSxu xin },
36011135fad2Sxu xin {
36021135fad2Sxu xin .procname = "mtu_expires",
36031135fad2Sxu xin .data = &init_net.ipv4.ip_rt_mtu_expires,
36041135fad2Sxu xin .maxlen = sizeof(int),
36051135fad2Sxu xin .mode = 0644,
36061135fad2Sxu xin .proc_handler = proc_dointvec_jiffies,
36071135fad2Sxu xin },
36082e9589ffSxu xin {
36092e9589ffSxu xin .procname = "min_adv_mss",
36102e9589ffSxu xin .data = &init_net.ipv4.ip_rt_min_advmss,
36112e9589ffSxu xin .maxlen = sizeof(int),
36122e9589ffSxu xin .mode = 0644,
36132e9589ffSxu xin .proc_handler = proc_dointvec,
36142e9589ffSxu xin },
3615f8572d8fSEric W. Biederman { },
361639a23e75SDenis V. Lunev };
361739a23e75SDenis V. Lunev
sysctl_route_net_init(struct net * net)361839a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net)
361939a23e75SDenis V. Lunev {
362039a23e75SDenis V. Lunev struct ctl_table *tbl;
3621c899710fSJoel Granados size_t table_size = ARRAY_SIZE(ipv4_route_netns_table);
362239a23e75SDenis V. Lunev
36231de6b15aSxu xin tbl = ipv4_route_netns_table;
362409ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) {
36251de6b15aSxu xin int i;
36261de6b15aSxu xin
36271de6b15aSxu xin tbl = kmemdup(tbl, sizeof(ipv4_route_netns_table), GFP_KERNEL);
362851456b29SIan Morris if (!tbl)
362939a23e75SDenis V. Lunev goto err_dup;
3630464dc801SEric W. Biederman
36315cdda5f1SChristian Brauner /* Don't export non-whitelisted sysctls to unprivileged users */
36325cdda5f1SChristian Brauner if (net->user_ns != &init_user_ns) {
3633c899710fSJoel Granados if (tbl[0].procname != ipv4_route_flush_procname) {
3634464dc801SEric W. Biederman tbl[0].procname = NULL;
3635c899710fSJoel Granados table_size = 0;
3636c899710fSJoel Granados }
363739a23e75SDenis V. Lunev }
36381de6b15aSxu xin
36391de6b15aSxu xin /* Update the variables to point into the current struct net
36401de6b15aSxu xin * except for the first element flush
36411de6b15aSxu xin */
36421de6b15aSxu xin for (i = 1; i < ARRAY_SIZE(ipv4_route_netns_table) - 1; i++)
36431de6b15aSxu xin tbl[i].data += (void *)net - (void *)&init_net;
36445cdda5f1SChristian Brauner }
364539a23e75SDenis V. Lunev tbl[0].extra1 = net;
364639a23e75SDenis V. Lunev
3647c899710fSJoel Granados net->ipv4.route_hdr = register_net_sysctl_sz(net, "net/ipv4/route",
3648c899710fSJoel Granados tbl, table_size);
364951456b29SIan Morris if (!net->ipv4.route_hdr)
365039a23e75SDenis V. Lunev goto err_reg;
365139a23e75SDenis V. Lunev return 0;
365239a23e75SDenis V. Lunev
365339a23e75SDenis V. Lunev err_reg:
36541de6b15aSxu xin if (tbl != ipv4_route_netns_table)
365539a23e75SDenis V. Lunev kfree(tbl);
365639a23e75SDenis V. Lunev err_dup:
365739a23e75SDenis V. Lunev return -ENOMEM;
365839a23e75SDenis V. Lunev }
365939a23e75SDenis V. Lunev
sysctl_route_net_exit(struct net * net)366039a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net)
366139a23e75SDenis V. Lunev {
366239a23e75SDenis V. Lunev struct ctl_table *tbl;
366339a23e75SDenis V. Lunev
366439a23e75SDenis V. Lunev tbl = net->ipv4.route_hdr->ctl_table_arg;
366539a23e75SDenis V. Lunev unregister_net_sysctl_table(net->ipv4.route_hdr);
36661de6b15aSxu xin BUG_ON(tbl == ipv4_route_netns_table);
366739a23e75SDenis V. Lunev kfree(tbl);
366839a23e75SDenis V. Lunev }
366939a23e75SDenis V. Lunev
367039a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = {
367139a23e75SDenis V. Lunev .init = sysctl_route_net_init,
367239a23e75SDenis V. Lunev .exit = sysctl_route_net_exit,
367339a23e75SDenis V. Lunev };
36741da177e4SLinus Torvalds #endif
36751da177e4SLinus Torvalds
netns_ip_rt_init(struct net * net)36761de6b15aSxu xin static __net_init int netns_ip_rt_init(struct net *net)
36771de6b15aSxu xin {
36781de6b15aSxu xin /* Set default value for namespaceified sysctls */
36791de6b15aSxu xin net->ipv4.ip_rt_min_pmtu = DEFAULT_MIN_PMTU;
36801135fad2Sxu xin net->ipv4.ip_rt_mtu_expires = DEFAULT_MTU_EXPIRES;
36812e9589ffSxu xin net->ipv4.ip_rt_min_advmss = DEFAULT_MIN_ADVMSS;
36821de6b15aSxu xin return 0;
36831de6b15aSxu xin }
36841de6b15aSxu xin
36851de6b15aSxu xin static struct pernet_operations __net_initdata ip_rt_ops = {
36861de6b15aSxu xin .init = netns_ip_rt_init,
36871de6b15aSxu xin };
36881de6b15aSxu xin
rt_genid_init(struct net * net)36893ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net)
36909f5e97e5SDenis V. Lunev {
3691ca4c3fc2Sfan.du atomic_set(&net->ipv4.rt_genid, 0);
36925aad1de5STimo Teräs atomic_set(&net->fnhe_genid, 0);
3693a251c17aSJason A. Donenfeld atomic_set(&net->ipv4.dev_addr_genid, get_random_u32());
36949f5e97e5SDenis V. Lunev return 0;
36959f5e97e5SDenis V. Lunev }
36969f5e97e5SDenis V. Lunev
36973ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = {
36983ee94372SNeil Horman .init = rt_genid_init,
36999f5e97e5SDenis V. Lunev };
37009f5e97e5SDenis V. Lunev
ipv4_inetpeer_init(struct net * net)3701c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net)
3702c3426b47SDavid S. Miller {
3703c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3704c3426b47SDavid S. Miller
3705c3426b47SDavid S. Miller if (!bp)
3706c3426b47SDavid S. Miller return -ENOMEM;
3707c3426b47SDavid S. Miller inet_peer_base_init(bp);
3708c3426b47SDavid S. Miller net->ipv4.peers = bp;
3709c3426b47SDavid S. Miller return 0;
3710c3426b47SDavid S. Miller }
3711c3426b47SDavid S. Miller
ipv4_inetpeer_exit(struct net * net)3712c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net)
3713c3426b47SDavid S. Miller {
3714c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv4.peers;
3715c3426b47SDavid S. Miller
3716c3426b47SDavid S. Miller net->ipv4.peers = NULL;
371756a6b248SDavid S. Miller inetpeer_invalidate_tree(bp);
3718c3426b47SDavid S. Miller kfree(bp);
3719c3426b47SDavid S. Miller }
3720c3426b47SDavid S. Miller
3721c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
3722c3426b47SDavid S. Miller .init = ipv4_inetpeer_init,
3723c3426b47SDavid S. Miller .exit = ipv4_inetpeer_exit,
3724c3426b47SDavid S. Miller };
37259f5e97e5SDenis V. Lunev
3726c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
37277d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
3728c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */
37291da177e4SLinus Torvalds
ip_rt_init(void)37301da177e4SLinus Torvalds int __init ip_rt_init(void)
37311da177e4SLinus Torvalds {
3732aa6dd211SEric Dumazet void *idents_hash;
37335055c371SEric Dumazet int cpu;
37341da177e4SLinus Torvalds
3735aa6dd211SEric Dumazet /* For modern hosts, this will use 2 MB of memory */
3736aa6dd211SEric Dumazet idents_hash = alloc_large_system_hash("IP idents",
3737aa6dd211SEric Dumazet sizeof(*ip_idents) + sizeof(*ip_tstamps),
3738aa6dd211SEric Dumazet 0,
3739aa6dd211SEric Dumazet 16, /* one bucket per 64 KB */
3740aa6dd211SEric Dumazet HASH_ZERO,
3741aa6dd211SEric Dumazet NULL,
3742aa6dd211SEric Dumazet &ip_idents_mask,
3743aa6dd211SEric Dumazet 2048,
3744aa6dd211SEric Dumazet 256*1024);
374573f156a6SEric Dumazet
3746aa6dd211SEric Dumazet ip_idents = idents_hash;
374773f156a6SEric Dumazet
3748197173dbSJason A. Donenfeld get_random_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
3749aa6dd211SEric Dumazet
3750aa6dd211SEric Dumazet ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
3751355b590cSEric Dumazet
37525055c371SEric Dumazet for_each_possible_cpu(cpu) {
37535055c371SEric Dumazet struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
37545055c371SEric Dumazet
37555055c371SEric Dumazet INIT_LIST_HEAD(&ul->head);
375629e5375dSEric Dumazet INIT_LIST_HEAD(&ul->quarantine);
37575055c371SEric Dumazet spin_lock_init(&ul->lock);
37585055c371SEric Dumazet }
3759c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
37600dcec8c2SIngo Molnar ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
37611da177e4SLinus Torvalds if (!ip_rt_acct)
37621da177e4SLinus Torvalds panic("IP: failed to allocate ip_rt_acct\n");
37631da177e4SLinus Torvalds #endif
37641da177e4SLinus Torvalds
3765e5d679f3SAlexey Dobriyan ipv4_dst_ops.kmem_cachep =
3766e5d679f3SAlexey Dobriyan kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
376720c2df83SPaul Mundt SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
37681da177e4SLinus Torvalds
376914e50e57SDavid S. Miller ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
377014e50e57SDavid S. Miller
3771fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_ops) < 0)
3772fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_ops counter\n");
3773fc66f95cSEric Dumazet
3774fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
3775fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
3776fc66f95cSEric Dumazet
377789aef892SDavid S. Miller ipv4_dst_ops.gc_thresh = ~0;
377889aef892SDavid S. Miller ip_rt_max_size = INT_MAX;
37791da177e4SLinus Torvalds
37801da177e4SLinus Torvalds devinet_init();
37811da177e4SLinus Torvalds ip_fib_init();
37821da177e4SLinus Torvalds
378373b38711SDenis V. Lunev if (ip_rt_proc_init())
3784058bd4d2SJoe Perches pr_err("Unable to create route proc files\n");
37851da177e4SLinus Torvalds #ifdef CONFIG_XFRM
37861da177e4SLinus Torvalds xfrm_init();
3787703fb94eSSteffen Klassert xfrm4_init();
37881da177e4SLinus Torvalds #endif
3789394f51abSFlorian Westphal rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL,
3790394f51abSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED);
379163f3444fSThomas Graf
379239a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL
379339a23e75SDenis V. Lunev register_pernet_subsys(&sysctl_route_ops);
379439a23e75SDenis V. Lunev #endif
37951de6b15aSxu xin register_pernet_subsys(&ip_rt_ops);
37963ee94372SNeil Horman register_pernet_subsys(&rt_genid_ops);
3797c3426b47SDavid S. Miller register_pernet_subsys(&ipv4_inetpeer_ops);
37981bcdca3fSTim Hansen return 0;
37991da177e4SLinus Torvalds }
38001da177e4SLinus Torvalds
3801a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL
3802eeb61f71SAl Viro /*
3803eeb61f71SAl Viro * We really need to sanitize the damn ipv4 init order, then all
3804eeb61f71SAl Viro * this nonsense will go away.
3805eeb61f71SAl Viro */
ip_static_sysctl_init(void)3806eeb61f71SAl Viro void __init ip_static_sysctl_init(void)
3807eeb61f71SAl Viro {
38084e5ca785SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
3809eeb61f71SAl Viro }
3810a1bc6eb4SAl Viro #endif
3811