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 1091da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ) 1101da177e4SLinus Torvalds 1111de6b15aSxu xin #define DEFAULT_MIN_PMTU (512 + 20 + 20) 1121135fad2Sxu xin #define DEFAULT_MTU_EXPIRES (10 * 60 * HZ) 1132e9589ffSxu xin #define DEFAULT_MIN_ADVMSS 256 1141da177e4SLinus Torvalds static int ip_rt_max_size; 115817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly = 9; 116817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly = HZ / 50; 117817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1)); 118817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly = HZ; 119817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly = 5 * HZ; 1209f28a2fcSEric Dumazet 121deed49dfSXin Long static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT; 122c7272c2fSSabrina Dubroca 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * Interface to generic destination cache. 1251da177e4SLinus Torvalds */ 1261da177e4SLinus Torvalds 127bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE 128bbd807dfSBrian Vazquez struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie); 1290dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst); 130f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE 131f67fbeaeSBrian Vazquez unsigned int ipv4_mtu(const struct dst_entry *dst); 13292f1655aSEric Dumazet static void ipv4_negative_advice(struct sock *sk, 13392f1655aSEric Dumazet struct dst_entry *dst); 1341da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb); 1356700c270SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 136bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 137bd085ef6SHangbin Liu bool confirm_neigh); 1386700c270SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, 1396700c270SDavid S. Miller struct sk_buff *skb); 140caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst); 1411da177e4SLinus Torvalds 14262fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old) 14362fa8a84SDavid S. Miller { 14431248731SDavid S. Miller WARN_ON(1); 14531248731SDavid S. Miller return NULL; 14662fa8a84SDavid S. Miller } 14762fa8a84SDavid S. Miller 148f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 149f894cbf8SDavid S. Miller struct sk_buff *skb, 150f894cbf8SDavid S. Miller const void *daddr); 15163fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr); 152d3aaeb38SDavid S. Miller 1531da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = { 1541da177e4SLinus Torvalds .family = AF_INET, 1551da177e4SLinus Torvalds .check = ipv4_dst_check, 1560dbaee3bSDavid S. Miller .default_advmss = ipv4_default_advmss, 157ebb762f2SSteffen Klassert .mtu = ipv4_mtu, 15862fa8a84SDavid S. Miller .cow_metrics = ipv4_cow_metrics, 159caacf05eSDavid S. Miller .destroy = ipv4_dst_destroy, 1601da177e4SLinus Torvalds .negative_advice = ipv4_negative_advice, 1611da177e4SLinus Torvalds .link_failure = ipv4_link_failure, 1621da177e4SLinus Torvalds .update_pmtu = ip_rt_update_pmtu, 163e47a185bSDavid S. Miller .redirect = ip_do_redirect, 164b92dacd4SEric W. Biederman .local_out = __ip_local_out, 165d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 16663fca65dSJulian Anastasov .confirm_neigh = ipv4_confirm_neigh, 1671da177e4SLinus Torvalds }; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds #define ECN_OR_COST(class) TC_PRIO_##class 1701da177e4SLinus Torvalds 1714839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = { 1721da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1734a2b9c37SDan Siemon ECN_OR_COST(BESTEFFORT), 1741da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1751da177e4SLinus Torvalds ECN_OR_COST(BESTEFFORT), 1761da177e4SLinus Torvalds TC_PRIO_BULK, 1771da177e4SLinus Torvalds ECN_OR_COST(BULK), 1781da177e4SLinus Torvalds TC_PRIO_BULK, 1791da177e4SLinus Torvalds ECN_OR_COST(BULK), 1801da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1811da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1821da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1831da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1841da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1851da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK), 1861da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1871da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK) 1881da177e4SLinus Torvalds }; 189d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio); 1901da177e4SLinus Torvalds 1912f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 1923ed66e91SChristoph Lameter #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field) 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1951da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 1961da177e4SLinus Torvalds { 19729e75252SEric Dumazet if (*pos) 19889aef892SDavid S. Miller return NULL; 19929e75252SEric Dumazet return SEQ_START_TOKEN; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds ++*pos; 20589aef892SDavid S. Miller return NULL; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2151da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 2161da177e4SLinus Torvalds "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t" 2171da177e4SLinus Torvalds "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t" 2181da177e4SLinus Torvalds "HHUptod\tSpecDst"); 2191da177e4SLinus Torvalds return 0; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = { 2231da177e4SLinus Torvalds .start = rt_cache_seq_start, 2241da177e4SLinus Torvalds .next = rt_cache_seq_next, 2251da177e4SLinus Torvalds .stop = rt_cache_seq_stop, 2261da177e4SLinus Torvalds .show = rt_cache_seq_show, 2271da177e4SLinus Torvalds }; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds int cpu; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds if (*pos == 0) 2341da177e4SLinus Torvalds return SEQ_START_TOKEN; 2351da177e4SLinus Torvalds 2360f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 2371da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2381da177e4SLinus Torvalds continue; 2391da177e4SLinus Torvalds *pos = cpu+1; 2402f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds return NULL; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds int cpu; 2481da177e4SLinus Torvalds 2490f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 2501da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2511da177e4SLinus Torvalds continue; 2521da177e4SLinus Torvalds *pos = cpu+1; 2532f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2541da177e4SLinus Torvalds } 255a3ea8673SVasily Averin (*pos)++; 2561da177e4SLinus Torvalds return NULL; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct rt_cache_stat *st = v; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2700547ffe6SYajun 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"); 2711da177e4SLinus Torvalds return 0; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2740547ffe6SYajun Deng seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x " 2750547ffe6SYajun Deng "%08x %08x %08x %08x %08x %08x " 2760547ffe6SYajun Deng "%08x %08x %08x %08x\n", 277fc66f95cSEric Dumazet dst_entries_get_slow(&ipv4_dst_ops), 2780baf2b35SEric Dumazet 0, /* st->in_hit */ 2791da177e4SLinus Torvalds st->in_slow_tot, 2801da177e4SLinus Torvalds st->in_slow_mc, 2811da177e4SLinus Torvalds st->in_no_route, 2821da177e4SLinus Torvalds st->in_brd, 2831da177e4SLinus Torvalds st->in_martian_dst, 2841da177e4SLinus Torvalds st->in_martian_src, 2851da177e4SLinus Torvalds 2860baf2b35SEric Dumazet 0, /* st->out_hit */ 2871da177e4SLinus Torvalds st->out_slow_tot, 2881da177e4SLinus Torvalds st->out_slow_mc, 2891da177e4SLinus Torvalds 2900baf2b35SEric Dumazet 0, /* st->gc_total */ 2910baf2b35SEric Dumazet 0, /* st->gc_ignored */ 2920baf2b35SEric Dumazet 0, /* st->gc_goal_miss */ 2930baf2b35SEric Dumazet 0, /* st->gc_dst_overflow */ 2940baf2b35SEric Dumazet 0, /* st->in_hlist_search */ 2950baf2b35SEric Dumazet 0 /* st->out_hlist_search */ 2961da177e4SLinus Torvalds ); 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 300f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = { 3011da177e4SLinus Torvalds .start = rt_cpu_seq_start, 3021da177e4SLinus Torvalds .next = rt_cpu_seq_next, 3031da177e4SLinus Torvalds .stop = rt_cpu_seq_stop, 3041da177e4SLinus Torvalds .show = rt_cpu_seq_show, 3051da177e4SLinus Torvalds }; 3061da177e4SLinus Torvalds 307c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 308a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v) 30978c686e9SPavel Emelyanov { 310a661c419SAlexey Dobriyan struct ip_rt_acct *dst, *src; 311a661c419SAlexey Dobriyan unsigned int i, j; 31278c686e9SPavel Emelyanov 313a661c419SAlexey Dobriyan dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL); 314a661c419SAlexey Dobriyan if (!dst) 315a661c419SAlexey Dobriyan return -ENOMEM; 31678c686e9SPavel Emelyanov 317a661c419SAlexey Dobriyan for_each_possible_cpu(i) { 318a661c419SAlexey Dobriyan src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i); 319a661c419SAlexey Dobriyan for (j = 0; j < 256; j++) { 320a661c419SAlexey Dobriyan dst[j].o_bytes += src[j].o_bytes; 321a661c419SAlexey Dobriyan dst[j].o_packets += src[j].o_packets; 322a661c419SAlexey Dobriyan dst[j].i_bytes += src[j].i_bytes; 323a661c419SAlexey Dobriyan dst[j].i_packets += src[j].i_packets; 324a661c419SAlexey Dobriyan } 325a661c419SAlexey Dobriyan } 326a661c419SAlexey Dobriyan 327a661c419SAlexey Dobriyan seq_write(m, dst, 256 * sizeof(struct ip_rt_acct)); 328a661c419SAlexey Dobriyan kfree(dst); 32978c686e9SPavel Emelyanov return 0; 33078c686e9SPavel Emelyanov } 33178c686e9SPavel Emelyanov #endif 332107f1634SPavel Emelyanov 33373b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net) 334107f1634SPavel Emelyanov { 335107f1634SPavel Emelyanov struct proc_dir_entry *pde; 336107f1634SPavel Emelyanov 337f105f26eSYejune Deng pde = proc_create_seq("rt_cache", 0444, net->proc_net, 338f105f26eSYejune Deng &rt_cache_seq_ops); 339107f1634SPavel Emelyanov if (!pde) 340107f1634SPavel Emelyanov goto err1; 341107f1634SPavel Emelyanov 342f105f26eSYejune Deng pde = proc_create_seq("rt_cache", 0444, net->proc_net_stat, 343f105f26eSYejune Deng &rt_cpu_seq_ops); 344107f1634SPavel Emelyanov if (!pde) 345107f1634SPavel Emelyanov goto err2; 346107f1634SPavel Emelyanov 347c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 3483f3942acSChristoph Hellwig pde = proc_create_single("rt_acct", 0, net->proc_net, 3493f3942acSChristoph Hellwig rt_acct_proc_show); 350107f1634SPavel Emelyanov if (!pde) 351107f1634SPavel Emelyanov goto err3; 352107f1634SPavel Emelyanov #endif 353107f1634SPavel Emelyanov return 0; 354107f1634SPavel Emelyanov 355c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 356107f1634SPavel Emelyanov err3: 357107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net_stat); 358107f1634SPavel Emelyanov #endif 359107f1634SPavel Emelyanov err2: 360107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net); 361107f1634SPavel Emelyanov err1: 362107f1634SPavel Emelyanov return -ENOMEM; 363107f1634SPavel Emelyanov } 36473b38711SDenis V. Lunev 36573b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net) 36673b38711SDenis V. Lunev { 36773b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net_stat); 36873b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net); 369c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 37073b38711SDenis V. Lunev remove_proc_entry("rt_acct", net->proc_net); 3710a931acfSAlexey Dobriyan #endif 37273b38711SDenis V. Lunev } 37373b38711SDenis V. Lunev 37473b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata = { 37573b38711SDenis V. Lunev .init = ip_rt_do_proc_init, 37673b38711SDenis V. Lunev .exit = ip_rt_do_proc_exit, 37773b38711SDenis V. Lunev }; 37873b38711SDenis V. Lunev 37973b38711SDenis V. Lunev static int __init ip_rt_proc_init(void) 38073b38711SDenis V. Lunev { 38173b38711SDenis V. Lunev return register_pernet_subsys(&ip_rt_proc_ops); 38273b38711SDenis V. Lunev } 38373b38711SDenis V. Lunev 384107f1634SPavel Emelyanov #else 38573b38711SDenis V. Lunev static inline int ip_rt_proc_init(void) 386107f1634SPavel Emelyanov { 387107f1634SPavel Emelyanov return 0; 388107f1634SPavel Emelyanov } 3891da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 3901da177e4SLinus Torvalds 3914331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth) 392e84f84f2SDenis V. Lunev { 393ca4c3fc2Sfan.du return rth->rt_genid != rt_genid_ipv4(dev_net(rth->dst.dev)); 394e84f84f2SDenis V. Lunev } 395e84f84f2SDenis V. Lunev 3964ccfe6d4SNicolas Dichtel void rt_cache_flush(struct net *net) 39729e75252SEric Dumazet { 398ca4c3fc2Sfan.du rt_genid_bump_ipv4(net); 39998376387SEric Dumazet } 40098376387SEric Dumazet 401f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 402f894cbf8SDavid S. Miller struct sk_buff *skb, 403f894cbf8SDavid S. Miller const void *daddr) 4043769cffbSDavid Miller { 4051550c171SDavid Ahern const struct rtable *rt = container_of(dst, struct rtable, dst); 406d3aaeb38SDavid S. Miller struct net_device *dev = dst->dev; 4073769cffbSDavid Miller struct neighbour *n; 4083769cffbSDavid Miller 40909eed119SEric Dumazet rcu_read_lock(); 410d3aaeb38SDavid S. Miller 4115c9f7c1dSDavid Ahern if (likely(rt->rt_gw_family == AF_INET)) { 4125c9f7c1dSDavid Ahern n = ip_neigh_gw4(dev, rt->rt_gw4); 4135c9f7c1dSDavid Ahern } else if (rt->rt_gw_family == AF_INET6) { 4145c9f7c1dSDavid Ahern n = ip_neigh_gw6(dev, &rt->rt_gw6); 4155c9f7c1dSDavid Ahern } else { 4165c9f7c1dSDavid Ahern __be32 pkey; 4175c9f7c1dSDavid Ahern 4185c9f7c1dSDavid Ahern pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr); 4195c9f7c1dSDavid Ahern n = ip_neigh_gw4(dev, pkey); 4205c9f7c1dSDavid Ahern } 4215c9f7c1dSDavid Ahern 422537de0c8SIdo Schimmel if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt)) 4235c9f7c1dSDavid Ahern n = NULL; 4245c9f7c1dSDavid Ahern 42509eed119SEric Dumazet rcu_read_unlock(); 4265c9f7c1dSDavid Ahern 427d3aaeb38SDavid S. Miller return n; 428d3aaeb38SDavid S. Miller } 429d3aaeb38SDavid S. Miller 43063fca65dSJulian Anastasov static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr) 43163fca65dSJulian Anastasov { 4321550c171SDavid Ahern const struct rtable *rt = container_of(dst, struct rtable, dst); 43363fca65dSJulian Anastasov struct net_device *dev = dst->dev; 43463fca65dSJulian Anastasov const __be32 *pkey = daddr; 43563fca65dSJulian Anastasov 4366de9c055SDavid Ahern if (rt->rt_gw_family == AF_INET) { 4371550c171SDavid Ahern pkey = (const __be32 *)&rt->rt_gw4; 4386de9c055SDavid Ahern } else if (rt->rt_gw_family == AF_INET6) { 4396de9c055SDavid Ahern return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6); 4406de9c055SDavid Ahern } else if (!daddr || 44163fca65dSJulian Anastasov (rt->rt_flags & 4426de9c055SDavid Ahern (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) { 44363fca65dSJulian Anastasov return; 4446de9c055SDavid Ahern } 44563fca65dSJulian Anastasov __ipv4_confirm_neigh(dev, *(__force u32 *)pkey); 44663fca65dSJulian Anastasov } 44763fca65dSJulian Anastasov 448aa6dd211SEric Dumazet /* Hash tables of size 2048..262144 depending on RAM size. 449aa6dd211SEric Dumazet * Each bucket uses 8 bytes. 450aa6dd211SEric Dumazet */ 451aa6dd211SEric Dumazet static u32 ip_idents_mask __read_mostly; 452355b590cSEric Dumazet static atomic_t *ip_idents __read_mostly; 453355b590cSEric Dumazet static u32 *ip_tstamps __read_mostly; 45404ca6973SEric Dumazet 45504ca6973SEric Dumazet /* In order to protect privacy, we add a perturbation to identifiers 45604ca6973SEric Dumazet * if one generator is seldom used. This makes hard for an attacker 45704ca6973SEric Dumazet * to infer how many packets were sent between two points in time. 45804ca6973SEric Dumazet */ 45947ed9442SDavid Ahern static u32 ip_idents_reserve(u32 hash, int segs) 46004ca6973SEric Dumazet { 461aa6dd211SEric Dumazet u32 bucket, old, now = (u32)jiffies; 462aa6dd211SEric Dumazet atomic_t *p_id; 463aa6dd211SEric Dumazet u32 *p_tstamp; 464a6211caaSYuqi Jin u32 delta = 0; 46504ca6973SEric Dumazet 466aa6dd211SEric Dumazet bucket = hash & ip_idents_mask; 467aa6dd211SEric Dumazet p_tstamp = ip_tstamps + bucket; 468aa6dd211SEric Dumazet p_id = ip_idents + bucket; 469aa6dd211SEric Dumazet old = READ_ONCE(*p_tstamp); 470aa6dd211SEric Dumazet 471355b590cSEric Dumazet if (old != now && cmpxchg(p_tstamp, old, now) == old) 4728032bf12SJason A. Donenfeld delta = get_random_u32_below(now - old); 47304ca6973SEric Dumazet 474a6211caaSYuqi Jin /* If UBSAN reports an error there, please make sure your compiler 475a6211caaSYuqi Jin * supports -fno-strict-overflow before reporting it that was a bug 476a6211caaSYuqi Jin * in UBSAN, and it has been fixed in GCC-8. 477a6211caaSYuqi Jin */ 478a6211caaSYuqi Jin return atomic_add_return(segs + delta, p_id) - segs; 47904ca6973SEric Dumazet } 48073f156a6SEric Dumazet 481b6a7719aSHannes Frederic Sowa void __ip_select_ident(struct net *net, struct iphdr *iph, int segs) 4821da177e4SLinus Torvalds { 48373f156a6SEric Dumazet u32 hash, id; 4841da177e4SLinus Torvalds 485df453700SEric Dumazet /* Note the following code is not safe, but this is okay. */ 486df453700SEric Dumazet if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key))) 487df453700SEric Dumazet get_random_bytes(&net->ipv4.ip_id_key, 488df453700SEric Dumazet sizeof(net->ipv4.ip_id_key)); 4891da177e4SLinus Torvalds 490df453700SEric Dumazet hash = siphash_3u32((__force u32)iph->daddr, 49104ca6973SEric Dumazet (__force u32)iph->saddr, 492df453700SEric Dumazet iph->protocol, 493df453700SEric Dumazet &net->ipv4.ip_id_key); 49473f156a6SEric Dumazet id = ip_idents_reserve(hash, segs); 49573f156a6SEric Dumazet iph->id = htons(id); 4961da177e4SLinus Torvalds } 4974bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident); 4981da177e4SLinus Torvalds 499e2d118a1SLorenzo Colitti static void __build_flow_key(const struct net *net, struct flowi4 *fl4, 500b1ad4138SGuillaume Nault const struct sock *sk, const struct iphdr *iph, 501b1ad4138SGuillaume Nault int oif, __u8 tos, u8 prot, u32 mark, 502b1ad4138SGuillaume Nault int flow_flags) 5034895c771SDavid S. Miller { 504b1ad4138SGuillaume Nault __u8 scope = RT_SCOPE_UNIVERSE; 505b1ad4138SGuillaume Nault 5064895c771SDavid S. Miller if (sk) { 5074895c771SDavid S. Miller oif = sk->sk_bound_dev_if; 5083c5b4d69SEric Dumazet mark = READ_ONCE(sk->sk_mark); 509b1ad4138SGuillaume Nault tos = ip_sock_rt_tos(sk); 510b1ad4138SGuillaume Nault scope = ip_sock_rt_scope(sk); 511cafbe182SEric Dumazet prot = inet_test_bit(HDRINCL, sk) ? IPPROTO_RAW : 512cafbe182SEric Dumazet sk->sk_protocol; 5134895c771SDavid S. Miller } 514b1ad4138SGuillaume Nault 515b1ad4138SGuillaume Nault flowi4_init_output(fl4, oif, mark, tos & IPTOS_RT_MASK, scope, 516b1ad4138SGuillaume Nault prot, flow_flags, iph->daddr, iph->saddr, 0, 0, 517e2d118a1SLorenzo Colitti sock_net_uid(net, sk)); 5184895c771SDavid S. Miller } 5194895c771SDavid S. Miller 5205abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb, 5215abf7f7eSEric Dumazet const struct sock *sk) 5224895c771SDavid S. Miller { 523d109e61bSLorenzo Colitti const struct net *net = dev_net(skb->dev); 5244895c771SDavid S. Miller const struct iphdr *iph = ip_hdr(skb); 5254895c771SDavid S. Miller int oif = skb->dev->ifindex; 5264895c771SDavid S. Miller u8 prot = iph->protocol; 5274895c771SDavid S. Miller u32 mark = skb->mark; 528b1ad4138SGuillaume Nault __u8 tos = iph->tos; 5294895c771SDavid S. Miller 530d109e61bSLorenzo Colitti __build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0); 5314895c771SDavid S. Miller } 5324895c771SDavid S. Miller 5335abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk) 5344895c771SDavid S. Miller { 5354895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk); 5365abf7f7eSEric Dumazet const struct ip_options_rcu *inet_opt; 5374895c771SDavid S. Miller __be32 daddr = inet->inet_daddr; 5384895c771SDavid S. Miller 5394895c771SDavid S. Miller rcu_read_lock(); 5404895c771SDavid S. Miller inet_opt = rcu_dereference(inet->inet_opt); 5414895c771SDavid S. Miller if (inet_opt && inet_opt->opt.srr) 5424895c771SDavid S. Miller daddr = inet_opt->opt.faddr; 5433c5b4d69SEric Dumazet flowi4_init_output(fl4, sk->sk_bound_dev_if, READ_ONCE(sk->sk_mark), 544b1ad4138SGuillaume Nault ip_sock_rt_tos(sk) & IPTOS_RT_MASK, 545b1ad4138SGuillaume Nault ip_sock_rt_scope(sk), 546cafbe182SEric Dumazet inet_test_bit(HDRINCL, sk) ? 547cafbe182SEric Dumazet IPPROTO_RAW : sk->sk_protocol, 5484895c771SDavid S. Miller inet_sk_flowi_flags(sk), 549e2d118a1SLorenzo Colitti daddr, inet->inet_saddr, 0, 0, sk->sk_uid); 5504895c771SDavid S. Miller rcu_read_unlock(); 5514895c771SDavid S. Miller } 5524895c771SDavid S. Miller 5535abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk, 5545abf7f7eSEric Dumazet const struct sk_buff *skb) 5554895c771SDavid S. Miller { 5564895c771SDavid S. Miller if (skb) 5574895c771SDavid S. Miller build_skb_flow_key(fl4, skb, sk); 5584895c771SDavid S. Miller else 5594895c771SDavid S. Miller build_sk_flow_key(fl4, sk); 5604895c771SDavid S. Miller } 5614895c771SDavid S. Miller 562c5038a83SDavid S. Miller static DEFINE_SPINLOCK(fnhe_lock); 5634895c771SDavid S. Miller 5642ffae99dSTimo Teräs static void fnhe_flush_routes(struct fib_nh_exception *fnhe) 5652ffae99dSTimo Teräs { 5662ffae99dSTimo Teräs struct rtable *rt; 5672ffae99dSTimo Teräs 5682ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_input); 5692ffae99dSTimo Teräs if (rt) { 5702ffae99dSTimo Teräs RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL); 57195c47f9cSWei Wang dst_dev_put(&rt->dst); 5720830106cSWei Wang dst_release(&rt->dst); 5732ffae99dSTimo Teräs } 5742ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_output); 5752ffae99dSTimo Teräs if (rt) { 5762ffae99dSTimo Teräs RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL); 57795c47f9cSWei Wang dst_dev_put(&rt->dst); 5780830106cSWei Wang dst_release(&rt->dst); 5792ffae99dSTimo Teräs } 5802ffae99dSTimo Teräs } 5812ffae99dSTimo Teräs 58267d6d681SEric Dumazet static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash) 5834895c771SDavid S. Miller { 58467d6d681SEric Dumazet struct fib_nh_exception __rcu **fnhe_p, **oldest_p; 58567d6d681SEric Dumazet struct fib_nh_exception *fnhe, *oldest = NULL; 5864895c771SDavid S. Miller 58767d6d681SEric Dumazet for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) { 58867d6d681SEric Dumazet fnhe = rcu_dereference_protected(*fnhe_p, 58967d6d681SEric Dumazet lockdep_is_held(&fnhe_lock)); 59067d6d681SEric Dumazet if (!fnhe) 59167d6d681SEric Dumazet break; 59267d6d681SEric Dumazet if (!oldest || 59367d6d681SEric Dumazet time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) { 5944895c771SDavid S. Miller oldest = fnhe; 59567d6d681SEric Dumazet oldest_p = fnhe_p; 59667d6d681SEric Dumazet } 5974895c771SDavid S. Miller } 5982ffae99dSTimo Teräs fnhe_flush_routes(oldest); 59967d6d681SEric Dumazet *oldest_p = oldest->fnhe_next; 60067d6d681SEric Dumazet kfree_rcu(oldest, rcu); 6014895c771SDavid S. Miller } 6024895c771SDavid S. Miller 6036457378fSEric Dumazet static u32 fnhe_hashfun(__be32 daddr) 604d3a25c98SDavid S. Miller { 60549ecc2e9SEric Dumazet static siphash_aligned_key_t fnhe_hash_key; 6066457378fSEric Dumazet u64 hval; 607d3a25c98SDavid S. Miller 6086457378fSEric Dumazet net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key)); 6096457378fSEric Dumazet hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key); 6106457378fSEric Dumazet return hash_64(hval, FNHE_HASH_SHIFT); 611d3a25c98SDavid S. Miller } 612d3a25c98SDavid S. Miller 613387aa65aSTimo Teräs static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe) 614387aa65aSTimo Teräs { 615387aa65aSTimo Teräs rt->rt_pmtu = fnhe->fnhe_pmtu; 616d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = fnhe->fnhe_mtu_locked; 617387aa65aSTimo Teräs rt->dst.expires = fnhe->fnhe_expires; 618387aa65aSTimo Teräs 619387aa65aSTimo Teräs if (fnhe->fnhe_gw) { 620387aa65aSTimo Teräs rt->rt_flags |= RTCF_REDIRECTED; 62177d5bc7eSDavid Ahern rt->rt_uses_gateway = 1; 6221550c171SDavid Ahern rt->rt_gw_family = AF_INET; 6231550c171SDavid Ahern rt->rt_gw4 = fnhe->fnhe_gw; 624387aa65aSTimo Teräs } 625387aa65aSTimo Teräs } 626387aa65aSTimo Teräs 627a5995e71SDavid Ahern static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr, 628a5995e71SDavid Ahern __be32 gw, u32 pmtu, bool lock, 629a5995e71SDavid Ahern unsigned long expires) 6304895c771SDavid S. Miller { 631aee06da6SJulian Anastasov struct fnhe_hash_bucket *hash; 6324895c771SDavid S. Miller struct fib_nh_exception *fnhe; 633387aa65aSTimo Teräs struct rtable *rt; 634cebe84c6SXin Long u32 genid, hval; 635387aa65aSTimo Teräs unsigned int i; 6364895c771SDavid S. Miller int depth; 637cebe84c6SXin Long 638a5995e71SDavid Ahern genid = fnhe_genid(dev_net(nhc->nhc_dev)); 639cebe84c6SXin Long hval = fnhe_hashfun(daddr); 6404895c771SDavid S. Miller 641c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock); 642aee06da6SJulian Anastasov 643a5995e71SDavid Ahern hash = rcu_dereference(nhc->nhc_exceptions); 6444895c771SDavid S. Miller if (!hash) { 6456396bb22SKees Cook hash = kcalloc(FNHE_HASH_SIZE, sizeof(*hash), GFP_ATOMIC); 6464895c771SDavid S. Miller if (!hash) 647aee06da6SJulian Anastasov goto out_unlock; 648a5995e71SDavid Ahern rcu_assign_pointer(nhc->nhc_exceptions, hash); 6494895c771SDavid S. Miller } 6504895c771SDavid S. Miller 6514895c771SDavid S. Miller hash += hval; 6524895c771SDavid S. Miller 6534895c771SDavid S. Miller depth = 0; 6544895c771SDavid S. Miller for (fnhe = rcu_dereference(hash->chain); fnhe; 6554895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 6564895c771SDavid S. Miller if (fnhe->fnhe_daddr == daddr) 657aee06da6SJulian Anastasov break; 6584895c771SDavid S. Miller depth++; 6594895c771SDavid S. Miller } 6604895c771SDavid S. Miller 661aee06da6SJulian Anastasov if (fnhe) { 662cebe84c6SXin Long if (fnhe->fnhe_genid != genid) 663cebe84c6SXin Long fnhe->fnhe_genid = genid; 664aee06da6SJulian Anastasov if (gw) 665aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 666d52e5a7eSSabrina Dubroca if (pmtu) { 667aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 668d52e5a7eSSabrina Dubroca fnhe->fnhe_mtu_locked = lock; 669d52e5a7eSSabrina Dubroca } 670387aa65aSTimo Teräs fnhe->fnhe_expires = max(1UL, expires); 671387aa65aSTimo Teräs /* Update all cached dsts too */ 6722ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_input); 6732ffae99dSTimo Teräs if (rt) 6742ffae99dSTimo Teräs fill_route_from_fnhe(rt, fnhe); 6752ffae99dSTimo Teräs rt = rcu_dereference(fnhe->fnhe_rth_output); 676387aa65aSTimo Teräs if (rt) 677387aa65aSTimo Teräs fill_route_from_fnhe(rt, fnhe); 678aee06da6SJulian Anastasov } else { 67967d6d681SEric Dumazet /* Randomize max depth to avoid some side channels attacks. */ 68067d6d681SEric Dumazet int max_depth = FNHE_RECLAIM_DEPTH + 6818032bf12SJason A. Donenfeld get_random_u32_below(FNHE_RECLAIM_DEPTH); 68267d6d681SEric Dumazet 68367d6d681SEric Dumazet while (depth > max_depth) { 68467d6d681SEric Dumazet fnhe_remove_oldest(hash); 68567d6d681SEric Dumazet depth--; 68667d6d681SEric Dumazet } 68767d6d681SEric Dumazet 6884895c771SDavid S. Miller fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); 6894895c771SDavid S. Miller if (!fnhe) 690aee06da6SJulian Anastasov goto out_unlock; 6914895c771SDavid S. Miller 6924895c771SDavid S. Miller fnhe->fnhe_next = hash->chain; 69367d6d681SEric Dumazet 694cebe84c6SXin Long fnhe->fnhe_genid = genid; 6954895c771SDavid S. Miller fnhe->fnhe_daddr = daddr; 696aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 697aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 698d52e5a7eSSabrina Dubroca fnhe->fnhe_mtu_locked = lock; 69994720e3aSJulian Anastasov fnhe->fnhe_expires = max(1UL, expires); 700387aa65aSTimo Teräs 70167d6d681SEric Dumazet rcu_assign_pointer(hash->chain, fnhe); 70267d6d681SEric Dumazet 703387aa65aSTimo Teräs /* Exception created; mark the cached routes for the nexthop 704387aa65aSTimo Teräs * stale, so anyone caching it rechecks if this exception 705387aa65aSTimo Teräs * applies to them. 706387aa65aSTimo Teräs */ 7070f457a36SDavid Ahern rt = rcu_dereference(nhc->nhc_rth_input); 7082ffae99dSTimo Teräs if (rt) 7092ffae99dSTimo Teräs rt->dst.obsolete = DST_OBSOLETE_KILL; 7102ffae99dSTimo Teräs 711387aa65aSTimo Teräs for_each_possible_cpu(i) { 712387aa65aSTimo Teräs struct rtable __rcu **prt; 7136ad08600SShubhankar Kuranagatti 7140f457a36SDavid Ahern prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i); 715387aa65aSTimo Teräs rt = rcu_dereference(*prt); 716387aa65aSTimo Teräs if (rt) 717387aa65aSTimo Teräs rt->dst.obsolete = DST_OBSOLETE_KILL; 718387aa65aSTimo Teräs } 719aee06da6SJulian Anastasov } 720aee06da6SJulian Anastasov 7214895c771SDavid S. Miller fnhe->fnhe_stamp = jiffies; 722aee06da6SJulian Anastasov 723aee06da6SJulian Anastasov out_unlock: 724c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock); 7254895c771SDavid S. Miller } 7264895c771SDavid S. Miller 727ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4, 728ceb33206SDavid S. Miller bool kill_route) 7291da177e4SLinus Torvalds { 730e47a185bSDavid S. Miller __be32 new_gw = icmp_hdr(skb)->un.gateway; 73194206125SDavid S. Miller __be32 old_gw = ip_hdr(skb)->saddr; 732e47a185bSDavid S. Miller struct net_device *dev = skb->dev; 733e47a185bSDavid S. Miller struct in_device *in_dev; 7344895c771SDavid S. Miller struct fib_result res; 735e47a185bSDavid S. Miller struct neighbour *n; 736317805b8SDenis V. Lunev struct net *net; 7371da177e4SLinus Torvalds 73894206125SDavid S. Miller switch (icmp_hdr(skb)->code & 7) { 73994206125SDavid S. Miller case ICMP_REDIR_NET: 74094206125SDavid S. Miller case ICMP_REDIR_NETTOS: 74194206125SDavid S. Miller case ICMP_REDIR_HOST: 74294206125SDavid S. Miller case ICMP_REDIR_HOSTTOS: 74394206125SDavid S. Miller break; 74494206125SDavid S. Miller 74594206125SDavid S. Miller default: 74694206125SDavid S. Miller return; 74794206125SDavid S. Miller } 74894206125SDavid S. Miller 7491550c171SDavid Ahern if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw) 750e47a185bSDavid S. Miller return; 751e47a185bSDavid S. Miller 752e47a185bSDavid S. Miller in_dev = __in_dev_get_rcu(dev); 753e47a185bSDavid S. Miller if (!in_dev) 754e47a185bSDavid S. Miller return; 755e47a185bSDavid S. Miller 756c346dca1SYOSHIFUJI Hideaki net = dev_net(dev); 7579d4fb27dSJoe Perches if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) || 7589d4fb27dSJoe Perches ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) || 7599d4fb27dSJoe Perches ipv4_is_zeronet(new_gw)) 7601da177e4SLinus Torvalds goto reject_redirect; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds if (!IN_DEV_SHARED_MEDIA(in_dev)) { 7631da177e4SLinus Torvalds if (!inet_addr_onlink(in_dev, new_gw, old_gw)) 7641da177e4SLinus Torvalds goto reject_redirect; 7651da177e4SLinus Torvalds if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev)) 7661da177e4SLinus Torvalds goto reject_redirect; 7671da177e4SLinus Torvalds } else { 768317805b8SDenis V. Lunev if (inet_addr_type(net, new_gw) != RTN_UNICAST) 7691da177e4SLinus Torvalds goto reject_redirect; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 772c0e29262SKunwu Chan n = __ipv4_neigh_lookup(rt->dst.dev, (__force u32)new_gw); 773969447f2SStephen Suryaputra Lin if (!n) 774969447f2SStephen Suryaputra Lin n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev); 7752c1a4311SWANG Cong if (!IS_ERR(n)) { 776b071af52SEric Dumazet if (!(READ_ONCE(n->nud_state) & NUD_VALID)) { 777e47a185bSDavid S. Miller neigh_event_send(n, NULL); 778e47a185bSDavid S. Miller } else { 7790eeb075fSAndy Gospodarek if (fib_lookup(net, fl4, &res, 0) == 0) { 7802fbc6e89SDavid Ahern struct fib_nh_common *nhc; 7814895c771SDavid S. Miller 7822fbc6e89SDavid Ahern fib_select_path(net, &res, fl4, skb); 7832fbc6e89SDavid Ahern nhc = FIB_RES_NHC(res); 784a5995e71SDavid Ahern update_or_create_fnhe(nhc, fl4->daddr, new_gw, 785d52e5a7eSSabrina Dubroca 0, false, 786d52e5a7eSSabrina Dubroca jiffies + ip_rt_gc_timeout); 7874895c771SDavid S. Miller } 788ceb33206SDavid S. Miller if (kill_route) 789ceb33206SDavid S. Miller rt->dst.obsolete = DST_OBSOLETE_KILL; 790e47a185bSDavid S. Miller call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n); 791e47a185bSDavid S. Miller } 792e47a185bSDavid S. Miller neigh_release(n); 793e47a185bSDavid S. Miller } 794e47a185bSDavid S. Miller return; 795e47a185bSDavid S. Miller 796e47a185bSDavid S. Miller reject_redirect: 797e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE 79899ee038dSDavid S. Miller if (IN_DEV_LOG_MARTIANS(in_dev)) { 79999ee038dSDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 80099ee038dSDavid S. Miller __be32 daddr = iph->daddr; 80199ee038dSDavid S. Miller __be32 saddr = iph->saddr; 80299ee038dSDavid S. Miller 803e47a185bSDavid S. Miller net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n" 804e47a185bSDavid S. Miller " Advised path = %pI4 -> %pI4\n", 805e47a185bSDavid S. Miller &old_gw, dev->name, &new_gw, 806e47a185bSDavid S. Miller &saddr, &daddr); 80799ee038dSDavid S. Miller } 808e47a185bSDavid S. Miller #endif 809e47a185bSDavid S. Miller ; 810e47a185bSDavid S. Miller } 811e47a185bSDavid S. Miller 8124895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 8134895c771SDavid S. Miller { 8144895c771SDavid S. Miller struct rtable *rt; 8154895c771SDavid S. Miller struct flowi4 fl4; 816f96ef988SMichal Kubecek const struct iphdr *iph = (const struct iphdr *) skb->data; 8177d995694SLorenzo Colitti struct net *net = dev_net(skb->dev); 818f96ef988SMichal Kubecek int oif = skb->dev->ifindex; 819f96ef988SMichal Kubecek u8 prot = iph->protocol; 820f96ef988SMichal Kubecek u32 mark = skb->mark; 821b1ad4138SGuillaume Nault __u8 tos = iph->tos; 8224895c771SDavid S. Miller 82305d6d492SEric Dumazet rt = dst_rtable(dst); 8244895c771SDavid S. Miller 8257d995694SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0); 826ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, true); 8274895c771SDavid S. Miller } 8284895c771SDavid S. Miller 82992f1655aSEric Dumazet static void ipv4_negative_advice(struct sock *sk, 83092f1655aSEric Dumazet struct dst_entry *dst) 8311da177e4SLinus Torvalds { 83205d6d492SEric Dumazet struct rtable *rt = dst_rtable(dst); 8331da177e4SLinus Torvalds 83492f1655aSEric Dumazet if ((dst->obsolete > 0) || 83592f1655aSEric Dumazet (rt->rt_flags & RTCF_REDIRECTED) || 83692f1655aSEric Dumazet rt->dst.expires) 83792f1655aSEric Dumazet sk_dst_reset(sk); 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* 8411da177e4SLinus Torvalds * Algorithm: 8421da177e4SLinus Torvalds * 1. The first ip_rt_redirect_number redirects are sent 8431da177e4SLinus Torvalds * with exponential backoff, then we stop sending them at all, 8441da177e4SLinus Torvalds * assuming that the host ignores our redirects. 8451da177e4SLinus Torvalds * 2. If we did not see packets requiring redirects 8461da177e4SLinus Torvalds * during ip_rt_redirect_silence, we assume that the host 8471da177e4SLinus Torvalds * forgot redirected route and start to send redirects again. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * This algorithm is much cheaper and more intelligent than dumb load limiting 8501da177e4SLinus Torvalds * in icmp.c. 8511da177e4SLinus Torvalds * 8521da177e4SLinus Torvalds * NOTE. Do not forget to inhibit load limiting for redirects (redundant) 8531da177e4SLinus Torvalds * and "frag. need" (breaks PMTU discovery) in icmp.c. 8541da177e4SLinus Torvalds */ 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb) 8571da177e4SLinus Torvalds { 858511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 85930038fc6SEric Dumazet struct in_device *in_dev; 86092d86829SDavid S. Miller struct inet_peer *peer; 8611d861aa4SDavid S. Miller struct net *net; 86230038fc6SEric Dumazet int log_martians; 863192132b9SDavid Ahern int vif; 8641da177e4SLinus Torvalds 86530038fc6SEric Dumazet rcu_read_lock(); 866d8d1f30bSChangli Gao in_dev = __in_dev_get_rcu(rt->dst.dev); 86730038fc6SEric Dumazet if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) { 86830038fc6SEric Dumazet rcu_read_unlock(); 8691da177e4SLinus Torvalds return; 87030038fc6SEric Dumazet } 87130038fc6SEric Dumazet log_martians = IN_DEV_LOG_MARTIANS(in_dev); 872385add90SDavid Ahern vif = l3mdev_master_ifindex_rcu(rt->dst.dev); 87330038fc6SEric Dumazet rcu_read_unlock(); 8741da177e4SLinus Torvalds 8751d861aa4SDavid S. Miller net = dev_net(rt->dst.dev); 876192132b9SDavid Ahern peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif, 1); 87792d86829SDavid S. Miller if (!peer) { 878e81da0e1SJulian Anastasov icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, 879e81da0e1SJulian Anastasov rt_nexthop(rt, ip_hdr(skb)->daddr)); 88092d86829SDavid S. Miller return; 88192d86829SDavid S. Miller } 88292d86829SDavid S. Miller 8831da177e4SLinus Torvalds /* No redirected packets during ip_rt_redirect_silence; 8841da177e4SLinus Torvalds * reset the algorithm. 8851da177e4SLinus Torvalds */ 886c09551c6SLorenzo Bianconi if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) { 88792d86829SDavid S. Miller peer->rate_tokens = 0; 888c09551c6SLorenzo Bianconi peer->n_redirects = 0; 889c09551c6SLorenzo Bianconi } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds /* Too many ignored redirects; do not send anything 892d8d1f30bSChangli Gao * set dst.rate_last to the last seen redirected packet. 8931da177e4SLinus Torvalds */ 894c09551c6SLorenzo Bianconi if (peer->n_redirects >= ip_rt_redirect_number) { 89592d86829SDavid S. Miller peer->rate_last = jiffies; 8961d861aa4SDavid S. Miller goto out_put_peer; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* Check for load limit; set rate_last to the latest sent 9001da177e4SLinus Torvalds * redirect. 9011da177e4SLinus Torvalds */ 90257644431SPaolo Abeni if (peer->n_redirects == 0 || 90314fb8a76SLi Yewang time_after(jiffies, 90492d86829SDavid S. Miller (peer->rate_last + 905b406472bSPaolo Abeni (ip_rt_redirect_load << peer->n_redirects)))) { 906e81da0e1SJulian Anastasov __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr); 907e81da0e1SJulian Anastasov 908e81da0e1SJulian Anastasov icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw); 90992d86829SDavid S. Miller peer->rate_last = jiffies; 910c09551c6SLorenzo Bianconi ++peer->n_redirects; 911cf1b7201SArnd Bergmann if (IS_ENABLED(CONFIG_IP_ROUTE_VERBOSE) && log_martians && 912b406472bSPaolo Abeni peer->n_redirects == ip_rt_redirect_number) 913e87cc472SJoe Perches net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n", 91492101b3bSDavid S. Miller &ip_hdr(skb)->saddr, inet_iif(skb), 915e81da0e1SJulian Anastasov &ip_hdr(skb)->daddr, &gw); 9161da177e4SLinus Torvalds } 9171d861aa4SDavid S. Miller out_put_peer: 9181d861aa4SDavid S. Miller inet_putpeer(peer); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb) 9221da177e4SLinus Torvalds { 923511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 924e2c0dc1fSStephen Suryaputra struct net_device *dev = skb->dev; 925e2c0dc1fSStephen Suryaputra struct in_device *in_dev; 92692d86829SDavid S. Miller struct inet_peer *peer; 9271da177e4SLinus Torvalds unsigned long now; 928251da413SDavid S. Miller struct net *net; 929c4eb6641SMenglong Dong SKB_DR(reason); 93092d86829SDavid S. Miller bool send; 9311da177e4SLinus Torvalds int code; 9321da177e4SLinus Torvalds 933e2c0dc1fSStephen Suryaputra if (netif_is_l3_master(skb->dev)) { 934e2c0dc1fSStephen Suryaputra dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif); 935e2c0dc1fSStephen Suryaputra if (!dev) 936e2c0dc1fSStephen Suryaputra goto out; 937e2c0dc1fSStephen Suryaputra } 938e2c0dc1fSStephen Suryaputra 939e2c0dc1fSStephen Suryaputra in_dev = __in_dev_get_rcu(dev); 940e2c0dc1fSStephen Suryaputra 941381c759dSEric W. Biederman /* IP on this device is disabled. */ 942381c759dSEric W. Biederman if (!in_dev) 943381c759dSEric W. Biederman goto out; 944381c759dSEric W. Biederman 945251da413SDavid S. Miller net = dev_net(rt->dst.dev); 946251da413SDavid S. Miller if (!IN_DEV_FORWARD(in_dev)) { 947251da413SDavid S. Miller switch (rt->dst.error) { 948251da413SDavid S. Miller case EHOSTUNREACH: 949c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INADDRERRORS); 950b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS); 951251da413SDavid S. Miller break; 952251da413SDavid S. Miller 953251da413SDavid S. Miller case ENETUNREACH: 954c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INNOROUTES); 955b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES); 956251da413SDavid S. Miller break; 957251da413SDavid S. Miller } 958251da413SDavid S. Miller goto out; 959251da413SDavid S. Miller } 960251da413SDavid S. Miller 961d8d1f30bSChangli Gao switch (rt->dst.error) { 9621da177e4SLinus Torvalds case EINVAL: 9631da177e4SLinus Torvalds default: 9641da177e4SLinus Torvalds goto out; 9651da177e4SLinus Torvalds case EHOSTUNREACH: 9661da177e4SLinus Torvalds code = ICMP_HOST_UNREACH; 9671da177e4SLinus Torvalds break; 9681da177e4SLinus Torvalds case ENETUNREACH: 9691da177e4SLinus Torvalds code = ICMP_NET_UNREACH; 970c4eb6641SMenglong Dong SKB_DR_SET(reason, IP_INNOROUTES); 971b45386efSEric Dumazet __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES); 9721da177e4SLinus Torvalds break; 9731da177e4SLinus Torvalds case EACCES: 9741da177e4SLinus Torvalds code = ICMP_PKT_FILTERED; 9751da177e4SLinus Torvalds break; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 978192132b9SDavid Ahern peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 979385add90SDavid Ahern l3mdev_master_ifindex(skb->dev), 1); 98092d86829SDavid S. Miller 98192d86829SDavid S. Miller send = true; 98292d86829SDavid S. Miller if (peer) { 9831da177e4SLinus Torvalds now = jiffies; 98492d86829SDavid S. Miller peer->rate_tokens += now - peer->rate_last; 98592d86829SDavid S. Miller if (peer->rate_tokens > ip_rt_error_burst) 98692d86829SDavid S. Miller peer->rate_tokens = ip_rt_error_burst; 98792d86829SDavid S. Miller peer->rate_last = now; 98892d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_error_cost) 98992d86829SDavid S. Miller peer->rate_tokens -= ip_rt_error_cost; 99092d86829SDavid S. Miller else 99192d86829SDavid S. Miller send = false; 9921d861aa4SDavid S. Miller inet_putpeer(peer); 9931da177e4SLinus Torvalds } 99492d86829SDavid S. Miller if (send) 99592d86829SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, code, 0); 9961da177e4SLinus Torvalds 997c4eb6641SMenglong Dong out: kfree_skb_reason(skb, reason); 9981da177e4SLinus Torvalds return 0; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 1001d851c12bSSteffen Klassert static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu) 10021da177e4SLinus Torvalds { 1003d851c12bSSteffen Klassert struct dst_entry *dst = &rt->dst; 10042fbc6e89SDavid Ahern struct net *net = dev_net(dst->dev); 10054895c771SDavid S. Miller struct fib_result res; 1006d52e5a7eSSabrina Dubroca bool lock = false; 10078b4510d7SMiaohe Lin u32 old_mtu; 10082c8cec5cSDavid S. Miller 1009d52e5a7eSSabrina Dubroca if (ip_mtu_locked(dst)) 1010fa1e492aSSteffen Klassert return; 1011fa1e492aSSteffen Klassert 10128b4510d7SMiaohe Lin old_mtu = ipv4_mtu(dst); 101328d35bcdSSabrina Dubroca if (old_mtu < mtu) 10143cdaa5beSLi Wei return; 10153cdaa5beSLi Wei 10161de6b15aSxu xin if (mtu < net->ipv4.ip_rt_min_pmtu) { 1017d52e5a7eSSabrina Dubroca lock = true; 10181de6b15aSxu xin mtu = min(old_mtu, net->ipv4.ip_rt_min_pmtu); 1019d52e5a7eSSabrina Dubroca } 102046af3180SHiroaki SHIMODA 102128d35bcdSSabrina Dubroca if (rt->rt_pmtu == mtu && !lock && 10221135fad2Sxu xin time_before(jiffies, dst->expires - net->ipv4.ip_rt_mtu_expires / 2)) 1023f016229eSTimo Teräs return; 1024f016229eSTimo Teräs 1025c5ae7d41SEric Dumazet rcu_read_lock(); 10262fbc6e89SDavid Ahern if (fib_lookup(net, fl4, &res, 0) == 0) { 10272fbc6e89SDavid Ahern struct fib_nh_common *nhc; 10284895c771SDavid S. Miller 10292fbc6e89SDavid Ahern fib_select_path(net, &res, fl4, NULL); 10302fbc6e89SDavid Ahern nhc = FIB_RES_NHC(res); 1031a5995e71SDavid Ahern update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock, 10321135fad2Sxu xin jiffies + net->ipv4.ip_rt_mtu_expires); 10334895c771SDavid S. Miller } 1034c5ae7d41SEric Dumazet rcu_read_unlock(); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10374895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 1038bd085ef6SHangbin Liu struct sk_buff *skb, u32 mtu, 1039bd085ef6SHangbin Liu bool confirm_neigh) 10404895c771SDavid S. Miller { 104105d6d492SEric Dumazet struct rtable *rt = dst_rtable(dst); 10424895c771SDavid S. Miller struct flowi4 fl4; 10434895c771SDavid S. Miller 10444895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb); 1045df23bb18SStefano Brivio 1046df23bb18SStefano Brivio /* Don't make lookup fail for bridged encapsulations */ 1047df23bb18SStefano Brivio if (skb && netif_is_any_bridge_port(skb->dev)) 1048df23bb18SStefano Brivio fl4.flowi4_oif = 0; 1049df23bb18SStefano Brivio 1050d851c12bSSteffen Klassert __ip_rt_update_pmtu(rt, &fl4, mtu); 10514895c771SDavid S. Miller } 10524895c771SDavid S. Miller 105336393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, 1054d888f396SMaciej Żenczykowski int oif, u8 protocol) 105536393395SDavid S. Miller { 105636393395SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data; 105736393395SDavid S. Miller struct flowi4 fl4; 105836393395SDavid S. Miller struct rtable *rt; 1059d888f396SMaciej Żenczykowski u32 mark = IP4_REPLY_MARK(net, skb->mark); 10601b3c61dcSLorenzo Colitti 1061b1ad4138SGuillaume Nault __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, mark, 1062b1ad4138SGuillaume Nault 0); 106336393395SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 106436393395SDavid S. Miller if (!IS_ERR(rt)) { 10654895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 106636393395SDavid S. Miller ip_rt_put(rt); 106736393395SDavid S. Miller } 106836393395SDavid S. Miller } 106936393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu); 107036393395SDavid S. Miller 10719cb3a50cSSteffen Klassert static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 107236393395SDavid S. Miller { 10734895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data; 10744895c771SDavid S. Miller struct flowi4 fl4; 10754895c771SDavid S. Miller struct rtable *rt; 107636393395SDavid S. Miller 1077e2d118a1SLorenzo Colitti __build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0); 10781b3c61dcSLorenzo Colitti 10791b3c61dcSLorenzo Colitti if (!fl4.flowi4_mark) 10801b3c61dcSLorenzo Colitti fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark); 10811b3c61dcSLorenzo Colitti 10824895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4); 10834895c771SDavid S. Miller if (!IS_ERR(rt)) { 10844895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 10854895c771SDavid S. Miller ip_rt_put(rt); 10864895c771SDavid S. Miller } 108736393395SDavid S. Miller } 10889cb3a50cSSteffen Klassert 10899cb3a50cSSteffen Klassert void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 10909cb3a50cSSteffen Klassert { 10919cb3a50cSSteffen Klassert const struct iphdr *iph = (const struct iphdr *)skb->data; 10929cb3a50cSSteffen Klassert struct flowi4 fl4; 10939cb3a50cSSteffen Klassert struct rtable *rt; 10947f502361SEric Dumazet struct dst_entry *odst = NULL; 1095b44108dbSSteffen Klassert bool new = false; 1096e2d118a1SLorenzo Colitti struct net *net = sock_net(sk); 10979cb3a50cSSteffen Klassert 10989cb3a50cSSteffen Klassert bh_lock_sock(sk); 1099482fc609SHannes Frederic Sowa 1100482fc609SHannes Frederic Sowa if (!ip_sk_accept_pmtu(sk)) 1101482fc609SHannes Frederic Sowa goto out; 1102482fc609SHannes Frederic Sowa 11037f502361SEric Dumazet odst = sk_dst_get(sk); 11049cb3a50cSSteffen Klassert 11057f502361SEric Dumazet if (sock_owned_by_user(sk) || !odst) { 11069cb3a50cSSteffen Klassert __ipv4_sk_update_pmtu(skb, sk, mtu); 11079cb3a50cSSteffen Klassert goto out; 11089cb3a50cSSteffen Klassert } 11099cb3a50cSSteffen Klassert 1110e2d118a1SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0); 11119cb3a50cSSteffen Klassert 111205d6d492SEric Dumazet rt = dst_rtable(odst); 111351456b29SIan Morris if (odst->obsolete && !odst->ops->check(odst, 0)) { 11149cb3a50cSSteffen Klassert rt = ip_route_output_flow(sock_net(sk), &fl4, sk); 11159cb3a50cSSteffen Klassert if (IS_ERR(rt)) 11169cb3a50cSSteffen Klassert goto out; 1117b44108dbSSteffen Klassert 1118b44108dbSSteffen Klassert new = true; 11199cb3a50cSSteffen Klassert } 11209cb3a50cSSteffen Klassert 112105d6d492SEric Dumazet __ip_rt_update_pmtu(dst_rtable(xfrm_dst_path(&rt->dst)), &fl4, mtu); 11229cb3a50cSSteffen Klassert 11237f502361SEric Dumazet if (!dst_check(&rt->dst, 0)) { 1124b44108dbSSteffen Klassert if (new) 1125b44108dbSSteffen Klassert dst_release(&rt->dst); 1126b44108dbSSteffen Klassert 11279cb3a50cSSteffen Klassert rt = ip_route_output_flow(sock_net(sk), &fl4, sk); 11289cb3a50cSSteffen Klassert if (IS_ERR(rt)) 11299cb3a50cSSteffen Klassert goto out; 11309cb3a50cSSteffen Klassert 1131b44108dbSSteffen Klassert new = true; 11329cb3a50cSSteffen Klassert } 11339cb3a50cSSteffen Klassert 1134b44108dbSSteffen Klassert if (new) 11357f502361SEric Dumazet sk_dst_set(sk, &rt->dst); 11369cb3a50cSSteffen Klassert 11379cb3a50cSSteffen Klassert out: 11389cb3a50cSSteffen Klassert bh_unlock_sock(sk); 11397f502361SEric Dumazet dst_release(odst); 11409cb3a50cSSteffen Klassert } 114136393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu); 1142f39925dbSDavid S. Miller 1143b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net, 11441042caa7SMaciej Żenczykowski int oif, u8 protocol) 1145b42597e2SDavid S. Miller { 1146b42597e2SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data; 1147b42597e2SDavid S. Miller struct flowi4 fl4; 1148b42597e2SDavid S. Miller struct rtable *rt; 1149b42597e2SDavid S. Miller 1150b1ad4138SGuillaume Nault __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, 0, 0); 1151b42597e2SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 1152b42597e2SDavid S. Miller if (!IS_ERR(rt)) { 1153ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 1154b42597e2SDavid S. Miller ip_rt_put(rt); 1155b42597e2SDavid S. Miller } 1156b42597e2SDavid S. Miller } 1157b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect); 1158b42597e2SDavid S. Miller 1159b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk) 1160b42597e2SDavid S. Miller { 11614895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *)skb->data; 11624895c771SDavid S. Miller struct flowi4 fl4; 11634895c771SDavid S. Miller struct rtable *rt; 1164e2d118a1SLorenzo Colitti struct net *net = sock_net(sk); 1165b42597e2SDavid S. Miller 1166e2d118a1SLorenzo Colitti __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0); 1167e2d118a1SLorenzo Colitti rt = __ip_route_output_key(net, &fl4); 11684895c771SDavid S. Miller if (!IS_ERR(rt)) { 1169ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 11704895c771SDavid S. Miller ip_rt_put(rt); 11714895c771SDavid S. Miller } 1172b42597e2SDavid S. Miller } 1173b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect); 1174b42597e2SDavid S. Miller 1175bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ipv4_dst_check(struct dst_entry *dst, 1176bbd807dfSBrian Vazquez u32 cookie) 1177efbc368dSDavid S. Miller { 117805d6d492SEric Dumazet struct rtable *rt = dst_rtable(dst); 1179efbc368dSDavid S. Miller 1180ceb33206SDavid S. Miller /* All IPV4 dsts are created with ->obsolete set to the value 1181ceb33206SDavid S. Miller * DST_OBSOLETE_FORCE_CHK which forces validation calls down 1182ceb33206SDavid S. Miller * into this function always. 1183ceb33206SDavid S. Miller * 1184387aa65aSTimo Teräs * When a PMTU/redirect information update invalidates a route, 1185387aa65aSTimo Teräs * this is indicated by setting obsolete to DST_OBSOLETE_KILL or 118602afc7adSJulian Wiedmann * DST_OBSOLETE_DEAD. 1187ceb33206SDavid S. Miller */ 1188387aa65aSTimo Teräs if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt)) 1189efbc368dSDavid S. Miller return NULL; 1190d11a4dc1STimo Teräs return dst; 11911da177e4SLinus Torvalds } 11929c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_dst_check); 11931da177e4SLinus Torvalds 119420ff83f1SEric Dumazet static void ipv4_send_dest_unreach(struct sk_buff *skb) 11951da177e4SLinus Torvalds { 11960113d9c9SKyle Zeng struct net_device *dev; 1197ed0de45aSStephen Suryaputra struct ip_options opt; 1198c543cb4aSEric Dumazet int res; 11991da177e4SLinus Torvalds 1200ed0de45aSStephen Suryaputra /* Recompile ip options since IPCB may not be valid anymore. 120120ff83f1SEric Dumazet * Also check we have a reasonable ipv4 header. 1202ed0de45aSStephen Suryaputra */ 120320ff83f1SEric Dumazet if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) || 120420ff83f1SEric Dumazet ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5) 120520ff83f1SEric Dumazet return; 120620ff83f1SEric Dumazet 1207ed0de45aSStephen Suryaputra memset(&opt, 0, sizeof(opt)); 120820ff83f1SEric Dumazet if (ip_hdr(skb)->ihl > 5) { 120920ff83f1SEric Dumazet if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4)) 121020ff83f1SEric Dumazet return; 1211ed0de45aSStephen Suryaputra opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr); 1212c543cb4aSEric Dumazet 1213c543cb4aSEric Dumazet rcu_read_lock(); 12140113d9c9SKyle Zeng dev = skb->dev ? skb->dev : skb_rtable(skb)->dst.dev; 12150113d9c9SKyle Zeng res = __ip_options_compile(dev_net(dev), &opt, skb, NULL); 1216c543cb4aSEric Dumazet rcu_read_unlock(); 1217c543cb4aSEric Dumazet 1218c543cb4aSEric Dumazet if (res) 1219ed0de45aSStephen Suryaputra return; 122020ff83f1SEric Dumazet } 1221ed0de45aSStephen Suryaputra __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &opt); 122220ff83f1SEric Dumazet } 122320ff83f1SEric Dumazet 122420ff83f1SEric Dumazet static void ipv4_link_failure(struct sk_buff *skb) 122520ff83f1SEric Dumazet { 122620ff83f1SEric Dumazet struct rtable *rt; 122720ff83f1SEric Dumazet 122820ff83f1SEric Dumazet ipv4_send_dest_unreach(skb); 12291da177e4SLinus Torvalds 1230511c3f92SEric Dumazet rt = skb_rtable(skb); 12315943634fSDavid S. Miller if (rt) 12325943634fSDavid S. Miller dst_set_expires(&rt->dst, 0); 12332c8cec5cSDavid S. Miller } 12341da177e4SLinus Torvalds 1235ede2059dSEric W. Biederman static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb) 12361da177e4SLinus Torvalds { 123791df42beSJoe Perches pr_debug("%s: %pI4 -> %pI4, %s\n", 123891df42beSJoe Perches __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, 12391da177e4SLinus Torvalds skb->dev ? skb->dev->name : "?"); 12401da177e4SLinus Torvalds kfree_skb(skb); 1241c378a9c0SDave Jones WARN_ON(1); 12421da177e4SLinus Torvalds return 0; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* 12466ad08600SShubhankar Kuranagatti * We do not cache source address of outgoing interface, 12476ad08600SShubhankar Kuranagatti * because it is used only by IP RR, TS and SRR options, 12486ad08600SShubhankar Kuranagatti * so that it out of fast path. 12496ad08600SShubhankar Kuranagatti * 12506ad08600SShubhankar Kuranagatti * BTW remember: "addr" is allowed to be not aligned 12516ad08600SShubhankar Kuranagatti * in IP options! 12521da177e4SLinus Torvalds */ 12531da177e4SLinus Torvalds 12548e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt) 12551da177e4SLinus Torvalds { 1256a61ced5dSAl Viro __be32 src; 12571da177e4SLinus Torvalds 1258c7537967SDavid S. Miller if (rt_is_output_route(rt)) 1259c5be24ffSDavid S. Miller src = ip_hdr(skb)->saddr; 1260ebc0ffaeSEric Dumazet else { 12618e36360aSDavid S. Miller struct fib_result res; 1262e351bb62SMaciej Żenczykowski struct iphdr *iph = ip_hdr(skb); 1263e351bb62SMaciej Żenczykowski struct flowi4 fl4 = { 1264e351bb62SMaciej Żenczykowski .daddr = iph->daddr, 1265e351bb62SMaciej Żenczykowski .saddr = iph->saddr, 1266be2e9089SIdo Schimmel .flowi4_tos = iph->tos & INET_DSCP_MASK, 1267e351bb62SMaciej Żenczykowski .flowi4_oif = rt->dst.dev->ifindex, 1268e351bb62SMaciej Żenczykowski .flowi4_iif = skb->dev->ifindex, 1269e351bb62SMaciej Żenczykowski .flowi4_mark = skb->mark, 1270e351bb62SMaciej Żenczykowski }; 12715e2b61f7SDavid S. Miller 1272ebc0ffaeSEric Dumazet rcu_read_lock(); 12730eeb075fSAndy Gospodarek if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0) 1274eba618abSDavid Ahern src = fib_result_prefsrc(dev_net(rt->dst.dev), &res); 1275ebc0ffaeSEric Dumazet else 1276f8126f1dSDavid S. Miller src = inet_select_addr(rt->dst.dev, 1277f8126f1dSDavid S. Miller rt_nexthop(rt, iph->daddr), 12781da177e4SLinus Torvalds RT_SCOPE_UNIVERSE); 1279ebc0ffaeSEric Dumazet rcu_read_unlock(); 1280ebc0ffaeSEric Dumazet } 12811da177e4SLinus Torvalds memcpy(addr, &src, 4); 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 1284c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 12851da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag) 12861da177e4SLinus Torvalds { 1287d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF)) 1288d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF; 1289d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF0000)) 1290d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF0000; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds #endif 12931da177e4SLinus Torvalds 12940dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst) 12950dbaee3bSDavid S. Miller { 12962e9589ffSxu xin struct net *net = dev_net(dst->dev); 12977ed14d97SGao Feng unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr); 1298164a5e7aSEric Dumazet unsigned int advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size, 12992e9589ffSxu xin net->ipv4.ip_rt_min_advmss); 13007ed14d97SGao Feng 13017ed14d97SGao Feng return min(advmss, IPV4_MAX_PMTU - header_size); 13020dbaee3bSDavid S. Miller } 13030dbaee3bSDavid S. Miller 1304f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ipv4_mtu(const struct dst_entry *dst) 1305d33e4553SDavid S. Miller { 1306ac6627a2SVadim Fedorenko return ip_dst_mtu_maybe_forward(dst, false); 1307d33e4553SDavid S. Miller } 13089c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ipv4_mtu); 1309d33e4553SDavid S. Miller 1310a5995e71SDavid Ahern static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr) 131194720e3aSJulian Anastasov { 131294720e3aSJulian Anastasov struct fnhe_hash_bucket *hash; 131394720e3aSJulian Anastasov struct fib_nh_exception *fnhe, __rcu **fnhe_p; 131494720e3aSJulian Anastasov u32 hval = fnhe_hashfun(daddr); 131594720e3aSJulian Anastasov 131694720e3aSJulian Anastasov spin_lock_bh(&fnhe_lock); 131794720e3aSJulian Anastasov 1318a5995e71SDavid Ahern hash = rcu_dereference_protected(nhc->nhc_exceptions, 131994720e3aSJulian Anastasov lockdep_is_held(&fnhe_lock)); 132094720e3aSJulian Anastasov hash += hval; 132194720e3aSJulian Anastasov 132294720e3aSJulian Anastasov fnhe_p = &hash->chain; 132394720e3aSJulian Anastasov fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock)); 132494720e3aSJulian Anastasov while (fnhe) { 132594720e3aSJulian Anastasov if (fnhe->fnhe_daddr == daddr) { 132694720e3aSJulian Anastasov rcu_assign_pointer(*fnhe_p, rcu_dereference_protected( 132794720e3aSJulian Anastasov fnhe->fnhe_next, lockdep_is_held(&fnhe_lock))); 1328ee60ad21SXin Long /* set fnhe_daddr to 0 to ensure it won't bind with 1329ee60ad21SXin Long * new dsts in rt_bind_exception(). 1330ee60ad21SXin Long */ 1331ee60ad21SXin Long fnhe->fnhe_daddr = 0; 133294720e3aSJulian Anastasov fnhe_flush_routes(fnhe); 133394720e3aSJulian Anastasov kfree_rcu(fnhe, rcu); 133494720e3aSJulian Anastasov break; 133594720e3aSJulian Anastasov } 133694720e3aSJulian Anastasov fnhe_p = &fnhe->fnhe_next; 133794720e3aSJulian Anastasov fnhe = rcu_dereference_protected(fnhe->fnhe_next, 133894720e3aSJulian Anastasov lockdep_is_held(&fnhe_lock)); 133994720e3aSJulian Anastasov } 134094720e3aSJulian Anastasov 134194720e3aSJulian Anastasov spin_unlock_bh(&fnhe_lock); 134294720e3aSJulian Anastasov } 134394720e3aSJulian Anastasov 1344a5995e71SDavid Ahern static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc, 1345a5995e71SDavid Ahern __be32 daddr) 13464895c771SDavid S. Miller { 1347a5995e71SDavid Ahern struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions); 13484895c771SDavid S. Miller struct fib_nh_exception *fnhe; 13494895c771SDavid S. Miller u32 hval; 13504895c771SDavid S. Miller 1351f2bb4bedSDavid S. Miller if (!hash) 1352f2bb4bedSDavid S. Miller return NULL; 1353f2bb4bedSDavid S. Miller 1354d3a25c98SDavid S. Miller hval = fnhe_hashfun(daddr); 13554895c771SDavid S. Miller 13564895c771SDavid S. Miller for (fnhe = rcu_dereference(hash[hval].chain); fnhe; 13574895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 135894720e3aSJulian Anastasov if (fnhe->fnhe_daddr == daddr) { 135994720e3aSJulian Anastasov if (fnhe->fnhe_expires && 136094720e3aSJulian Anastasov time_after(jiffies, fnhe->fnhe_expires)) { 1361a5995e71SDavid Ahern ip_del_fnhe(nhc, daddr); 136294720e3aSJulian Anastasov break; 136394720e3aSJulian Anastasov } 1364f2bb4bedSDavid S. Miller return fnhe; 1365f2bb4bedSDavid S. Miller } 136694720e3aSJulian Anastasov } 1367f2bb4bedSDavid S. Miller return NULL; 1368f2bb4bedSDavid S. Miller } 1369f2bb4bedSDavid S. Miller 137050d889b1SDavid Ahern /* MTU selection: 137150d889b1SDavid Ahern * 1. mtu on route is locked - use it 137250d889b1SDavid Ahern * 2. mtu from nexthop exception 137350d889b1SDavid Ahern * 3. mtu from egress device 137450d889b1SDavid Ahern */ 137550d889b1SDavid Ahern 137650d889b1SDavid Ahern u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr) 137750d889b1SDavid Ahern { 1378eba618abSDavid Ahern struct fib_nh_common *nhc = res->nhc; 1379eba618abSDavid Ahern struct net_device *dev = nhc->nhc_dev; 138050d889b1SDavid Ahern struct fib_info *fi = res->fi; 138150d889b1SDavid Ahern u32 mtu = 0; 138250d889b1SDavid Ahern 138360c158dcSKuniyuki Iwashima if (READ_ONCE(dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu) || 138450d889b1SDavid Ahern fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU)) 138550d889b1SDavid Ahern mtu = fi->fib_mtu; 138650d889b1SDavid Ahern 138750d889b1SDavid Ahern if (likely(!mtu)) { 138850d889b1SDavid Ahern struct fib_nh_exception *fnhe; 138950d889b1SDavid Ahern 1390a5995e71SDavid Ahern fnhe = find_exception(nhc, daddr); 139150d889b1SDavid Ahern if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires)) 139250d889b1SDavid Ahern mtu = fnhe->fnhe_pmtu; 139350d889b1SDavid Ahern } 139450d889b1SDavid Ahern 139550d889b1SDavid Ahern if (likely(!mtu)) 139650d889b1SDavid Ahern mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU); 139750d889b1SDavid Ahern 1398eba618abSDavid Ahern return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu); 139950d889b1SDavid Ahern } 140050d889b1SDavid Ahern 1401caacf05eSDavid S. Miller static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe, 1402a4c2fd7fSWei Wang __be32 daddr, const bool do_cache) 1403f2bb4bedSDavid S. Miller { 1404caacf05eSDavid S. Miller bool ret = false; 1405caacf05eSDavid S. Miller 1406c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock); 1407aee06da6SJulian Anastasov 1408c5038a83SDavid S. Miller if (daddr == fnhe->fnhe_daddr) { 14092ffae99dSTimo Teräs struct rtable __rcu **porig; 14102ffae99dSTimo Teräs struct rtable *orig; 14115aad1de5STimo Teräs int genid = fnhe_genid(dev_net(rt->dst.dev)); 14122ffae99dSTimo Teräs 14132ffae99dSTimo Teräs if (rt_is_input_route(rt)) 14142ffae99dSTimo Teräs porig = &fnhe->fnhe_rth_input; 14152ffae99dSTimo Teräs else 14162ffae99dSTimo Teräs porig = &fnhe->fnhe_rth_output; 14172ffae99dSTimo Teräs orig = rcu_dereference(*porig); 14185aad1de5STimo Teräs 14195aad1de5STimo Teräs if (fnhe->fnhe_genid != genid) { 14205aad1de5STimo Teräs fnhe->fnhe_genid = genid; 142113d82bf5SSteffen Klassert fnhe->fnhe_gw = 0; 142213d82bf5SSteffen Klassert fnhe->fnhe_pmtu = 0; 142313d82bf5SSteffen Klassert fnhe->fnhe_expires = 0; 14240e8411e4SHangbin Liu fnhe->fnhe_mtu_locked = false; 14252ffae99dSTimo Teräs fnhe_flush_routes(fnhe); 14262ffae99dSTimo Teräs orig = NULL; 142713d82bf5SSteffen Klassert } 1428387aa65aSTimo Teräs fill_route_from_fnhe(rt, fnhe); 14291550c171SDavid Ahern if (!rt->rt_gw4) { 14301550c171SDavid Ahern rt->rt_gw4 = daddr; 14311550c171SDavid Ahern rt->rt_gw_family = AF_INET; 14321550c171SDavid Ahern } 1433f2bb4bedSDavid S. Miller 1434a4c2fd7fSWei Wang if (do_cache) { 14350830106cSWei Wang dst_hold(&rt->dst); 14362ffae99dSTimo Teräs rcu_assign_pointer(*porig, rt); 14370830106cSWei Wang if (orig) { 143895c47f9cSWei Wang dst_dev_put(&orig->dst); 14390830106cSWei Wang dst_release(&orig->dst); 14400830106cSWei Wang } 14412ffae99dSTimo Teräs ret = true; 14422ffae99dSTimo Teräs } 1443c5038a83SDavid S. Miller 1444c5038a83SDavid S. Miller fnhe->fnhe_stamp = jiffies; 1445c5038a83SDavid S. Miller } 1446c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock); 1447caacf05eSDavid S. Miller 1448caacf05eSDavid S. Miller return ret; 144954764bb6SEric Dumazet } 145054764bb6SEric Dumazet 145187063a1fSDavid Ahern static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt) 1452f2bb4bedSDavid S. Miller { 1453d26b3a7cSEric Dumazet struct rtable *orig, *prev, **p; 1454caacf05eSDavid S. Miller bool ret = true; 1455f2bb4bedSDavid S. Miller 1456d26b3a7cSEric Dumazet if (rt_is_input_route(rt)) { 14570f457a36SDavid Ahern p = (struct rtable **)&nhc->nhc_rth_input; 1458d26b3a7cSEric Dumazet } else { 14590f457a36SDavid Ahern p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output); 1460d26b3a7cSEric Dumazet } 1461f2bb4bedSDavid S. Miller orig = *p; 1462f2bb4bedSDavid S. Miller 14630830106cSWei Wang /* hold dst before doing cmpxchg() to avoid race condition 14640830106cSWei Wang * on this dst 14650830106cSWei Wang */ 14660830106cSWei Wang dst_hold(&rt->dst); 1467f2bb4bedSDavid S. Miller prev = cmpxchg(p, orig, rt); 1468f2bb4bedSDavid S. Miller if (prev == orig) { 14690830106cSWei Wang if (orig) { 14705018c596SWei Wang rt_add_uncached_list(orig); 14710830106cSWei Wang dst_release(&orig->dst); 14720830106cSWei Wang } 14730830106cSWei Wang } else { 14740830106cSWei Wang dst_release(&rt->dst); 1475caacf05eSDavid S. Miller ret = false; 14760830106cSWei Wang } 1477caacf05eSDavid S. Miller 1478caacf05eSDavid S. Miller return ret; 1479caacf05eSDavid S. Miller } 1480caacf05eSDavid S. Miller 14815055c371SEric Dumazet struct uncached_list { 14825055c371SEric Dumazet spinlock_t lock; 14835055c371SEric Dumazet struct list_head head; 14845055c371SEric Dumazet }; 14855055c371SEric Dumazet 14865055c371SEric Dumazet static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list); 1487caacf05eSDavid S. Miller 1488510c321bSXin Long void rt_add_uncached_list(struct rtable *rt) 1489caacf05eSDavid S. Miller { 14905055c371SEric Dumazet struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list); 14915055c371SEric Dumazet 1492d288a162SWangyang Guo rt->dst.rt_uncached_list = ul; 14935055c371SEric Dumazet 14945055c371SEric Dumazet spin_lock_bh(&ul->lock); 1495d288a162SWangyang Guo list_add_tail(&rt->dst.rt_uncached, &ul->head); 14965055c371SEric Dumazet spin_unlock_bh(&ul->lock); 1497caacf05eSDavid S. Miller } 1498caacf05eSDavid S. Miller 1499510c321bSXin Long void rt_del_uncached_list(struct rtable *rt) 1500510c321bSXin Long { 1501d288a162SWangyang Guo if (!list_empty(&rt->dst.rt_uncached)) { 1502d288a162SWangyang Guo struct uncached_list *ul = rt->dst.rt_uncached_list; 1503510c321bSXin Long 1504510c321bSXin Long spin_lock_bh(&ul->lock); 1505d288a162SWangyang Guo list_del_init(&rt->dst.rt_uncached); 1506510c321bSXin Long spin_unlock_bh(&ul->lock); 1507510c321bSXin Long } 1508510c321bSXin Long } 1509510c321bSXin Long 1510caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst) 1511caacf05eSDavid S. Miller { 15121620a336SDavid Ahern ip_dst_metrics_put(dst); 151305d6d492SEric Dumazet rt_del_uncached_list(dst_rtable(dst)); 1514caacf05eSDavid S. Miller } 1515caacf05eSDavid S. Miller 1516caacf05eSDavid S. Miller void rt_flush_dev(struct net_device *dev) 1517caacf05eSDavid S. Miller { 151829e5375dSEric Dumazet struct rtable *rt, *safe; 15195055c371SEric Dumazet int cpu; 1520caacf05eSDavid S. Miller 15215055c371SEric Dumazet for_each_possible_cpu(cpu) { 15225055c371SEric Dumazet struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu); 15235055c371SEric Dumazet 152429e5375dSEric Dumazet if (list_empty(&ul->head)) 152529e5375dSEric Dumazet continue; 152629e5375dSEric Dumazet 15275055c371SEric Dumazet spin_lock_bh(&ul->lock); 1528d288a162SWangyang Guo list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) { 1529caacf05eSDavid S. Miller if (rt->dst.dev != dev) 1530caacf05eSDavid S. Miller continue; 15318d7017fdSMahesh Bandewar rt->dst.dev = blackhole_netdev; 1532d62607c3SJakub Kicinski netdev_ref_replace(dev, blackhole_netdev, 1533d62607c3SJakub Kicinski &rt->dst.dev_tracker, GFP_ATOMIC); 153498aa546aSEric Dumazet list_del_init(&rt->dst.rt_uncached); 1535caacf05eSDavid S. Miller } 15365055c371SEric Dumazet spin_unlock_bh(&ul->lock); 15374895c771SDavid S. Miller } 15384895c771SDavid S. Miller } 15394895c771SDavid S. Miller 15404331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt) 1541d2d68ba9SDavid S. Miller { 15424331debcSEric Dumazet return rt && 15434331debcSEric Dumazet rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 15444331debcSEric Dumazet !rt_is_expired(rt); 1545d2d68ba9SDavid S. Miller } 1546d2d68ba9SDavid S. Miller 1547f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr, 15485e2b61f7SDavid S. Miller const struct fib_result *res, 1549f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe, 1550a4c2fd7fSWei Wang struct fib_info *fi, u16 type, u32 itag, 1551a4c2fd7fSWei Wang const bool do_cache) 15521da177e4SLinus Torvalds { 1553caacf05eSDavid S. Miller bool cached = false; 1554caacf05eSDavid S. Miller 15551da177e4SLinus Torvalds if (fi) { 1556eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res); 15574895c771SDavid S. Miller 15580f5f7d7bSDavid Ahern if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) { 155977d5bc7eSDavid Ahern rt->rt_uses_gateway = 1; 15600f5f7d7bSDavid Ahern rt->rt_gw_family = nhc->nhc_gw_family; 15610f5f7d7bSDavid Ahern /* only INET and INET6 are supported */ 15620f5f7d7bSDavid Ahern if (likely(nhc->nhc_gw_family == AF_INET)) 15630f5f7d7bSDavid Ahern rt->rt_gw4 = nhc->nhc_gw.ipv4; 15640f5f7d7bSDavid Ahern else 15650f5f7d7bSDavid Ahern rt->rt_gw6 = nhc->nhc_gw.ipv6; 1566155e8336SJulian Anastasov } 15670f5f7d7bSDavid Ahern 1568e1255ed4SDavid Ahern ip_dst_init_metrics(&rt->dst, fi->fib_metrics); 1569e1255ed4SDavid Ahern 1570c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1571dcb1ecb5SDavid Ahern if (nhc->nhc_family == AF_INET) { 157287063a1fSDavid Ahern struct fib_nh *nh; 157387063a1fSDavid Ahern 157487063a1fSDavid Ahern nh = container_of(nhc, struct fib_nh, nh_common); 1575f2bb4bedSDavid S. Miller rt->dst.tclassid = nh->nh_tclassid; 157687063a1fSDavid Ahern } 15771da177e4SLinus Torvalds #endif 157887063a1fSDavid Ahern rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate); 1579c5038a83SDavid S. Miller if (unlikely(fnhe)) 1580a4c2fd7fSWei Wang cached = rt_bind_exception(rt, fnhe, daddr, do_cache); 1581a4c2fd7fSWei Wang else if (do_cache) 158287063a1fSDavid Ahern cached = rt_cache_route(nhc, rt); 1583155e8336SJulian Anastasov if (unlikely(!cached)) { 1584155e8336SJulian Anastasov /* Routes we intend to cache in nexthop exception or 1585155e8336SJulian Anastasov * FIB nexthop have the DST_NOCACHE bit clear. 1586155e8336SJulian Anastasov * However, if we are unsuccessful at storing this 1587155e8336SJulian Anastasov * route into the cache we really need to set it. 1588155e8336SJulian Anastasov */ 15891550c171SDavid Ahern if (!rt->rt_gw4) { 15901550c171SDavid Ahern rt->rt_gw_family = AF_INET; 15911550c171SDavid Ahern rt->rt_gw4 = daddr; 15921550c171SDavid Ahern } 1593155e8336SJulian Anastasov rt_add_uncached_list(rt); 1594d33e4553SDavid S. Miller } 1595155e8336SJulian Anastasov } else 1596caacf05eSDavid S. Miller rt_add_uncached_list(rt); 15971da177e4SLinus Torvalds 1598c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 15991da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES 160085b91b03SDavid S. Miller set_class_tag(rt, res->tclassid); 16011da177e4SLinus Torvalds #endif 16021da177e4SLinus Torvalds set_class_tag(rt, itag); 16031da177e4SLinus Torvalds #endif 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16069ab179d8SDavid Ahern struct rtable *rt_dst_alloc(struct net_device *dev, 1607d08c4f35SDavid Ahern unsigned int flags, u16 type, 1608b5c8b3feSEyal Birger bool noxfrm) 16090c4dcd58SDavid S. Miller { 1610d08c4f35SDavid Ahern struct rtable *rt; 1611d08c4f35SDavid Ahern 1612762c8dc7SZhengchao Shao rt = dst_alloc(&ipv4_dst_ops, dev, DST_OBSOLETE_FORCE_CHK, 1613b2a9c0edSWei Wang (noxfrm ? DST_NOXFRM : 0)); 1614d08c4f35SDavid Ahern 1615d08c4f35SDavid Ahern if (rt) { 1616d08c4f35SDavid Ahern rt->rt_genid = rt_genid_ipv4(dev_net(dev)); 1617d08c4f35SDavid Ahern rt->rt_flags = flags; 1618d08c4f35SDavid Ahern rt->rt_type = type; 1619d08c4f35SDavid Ahern rt->rt_is_input = 0; 1620d08c4f35SDavid Ahern rt->rt_iif = 0; 1621d08c4f35SDavid Ahern rt->rt_pmtu = 0; 1622d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = 0; 162377d5bc7eSDavid Ahern rt->rt_uses_gateway = 0; 16241550c171SDavid Ahern rt->rt_gw_family = 0; 16251550c171SDavid Ahern rt->rt_gw4 = 0; 1626d08c4f35SDavid Ahern 1627d08c4f35SDavid Ahern rt->dst.output = ip_output; 1628d08c4f35SDavid Ahern if (flags & RTCF_LOCAL) 1629d08c4f35SDavid Ahern rt->dst.input = ip_local_deliver; 1630d08c4f35SDavid Ahern } 1631d08c4f35SDavid Ahern 1632d08c4f35SDavid Ahern return rt; 16330c4dcd58SDavid S. Miller } 16349ab179d8SDavid Ahern EXPORT_SYMBOL(rt_dst_alloc); 16350c4dcd58SDavid S. Miller 16365b18f128SStephen Suryaputra struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt) 16375b18f128SStephen Suryaputra { 16385b18f128SStephen Suryaputra struct rtable *new_rt; 16395b18f128SStephen Suryaputra 1640762c8dc7SZhengchao Shao new_rt = dst_alloc(&ipv4_dst_ops, dev, DST_OBSOLETE_FORCE_CHK, 16415b18f128SStephen Suryaputra rt->dst.flags); 16425b18f128SStephen Suryaputra 16435b18f128SStephen Suryaputra if (new_rt) { 16445b18f128SStephen Suryaputra new_rt->rt_genid = rt_genid_ipv4(dev_net(dev)); 16455b18f128SStephen Suryaputra new_rt->rt_flags = rt->rt_flags; 16465b18f128SStephen Suryaputra new_rt->rt_type = rt->rt_type; 16475b18f128SStephen Suryaputra new_rt->rt_is_input = rt->rt_is_input; 16485b18f128SStephen Suryaputra new_rt->rt_iif = rt->rt_iif; 16495b18f128SStephen Suryaputra new_rt->rt_pmtu = rt->rt_pmtu; 16505b18f128SStephen Suryaputra new_rt->rt_mtu_locked = rt->rt_mtu_locked; 16515b18f128SStephen Suryaputra new_rt->rt_gw_family = rt->rt_gw_family; 16525b18f128SStephen Suryaputra if (rt->rt_gw_family == AF_INET) 16535b18f128SStephen Suryaputra new_rt->rt_gw4 = rt->rt_gw4; 16545b18f128SStephen Suryaputra else if (rt->rt_gw_family == AF_INET6) 16555b18f128SStephen Suryaputra new_rt->rt_gw6 = rt->rt_gw6; 16565b18f128SStephen Suryaputra 16575b18f128SStephen Suryaputra new_rt->dst.input = rt->dst.input; 16585b18f128SStephen Suryaputra new_rt->dst.output = rt->dst.output; 16595b18f128SStephen Suryaputra new_rt->dst.error = rt->dst.error; 16605b18f128SStephen Suryaputra new_rt->dst.lastuse = jiffies; 16615b18f128SStephen Suryaputra new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate); 16625b18f128SStephen Suryaputra } 16635b18f128SStephen Suryaputra return new_rt; 16645b18f128SStephen Suryaputra } 16655b18f128SStephen Suryaputra EXPORT_SYMBOL(rt_dst_clone); 16665b18f128SStephen Suryaputra 166796d36220SEric Dumazet /* called in rcu_read_lock() section */ 1668bc044e8dSPaolo Abeni int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1669bc044e8dSPaolo Abeni u8 tos, struct net_device *dev, 1670bc044e8dSPaolo Abeni struct in_device *in_dev, u32 *itag) 16711da177e4SLinus Torvalds { 1672b5f7e755SEric Dumazet int err; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* Primary sanity checks. */ 167551456b29SIan Morris if (!in_dev) 16761da177e4SLinus Torvalds return -EINVAL; 16771da177e4SLinus Torvalds 16781e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1679d0daebc3SThomas Graf skb->protocol != htons(ETH_P_IP)) 1680bc044e8dSPaolo Abeni return -EINVAL; 1681d0daebc3SThomas Graf 168275fea73dSAlexander Duyck if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev)) 1683bc044e8dSPaolo Abeni return -EINVAL; 16841da177e4SLinus Torvalds 1685f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) { 16861d2f4ebbSEdward Chron if (!ipv4_is_local_multicast(daddr) && 16871d2f4ebbSEdward Chron ip_hdr(skb)->protocol != IPPROTO_IGMP) 1688bc044e8dSPaolo Abeni return -EINVAL; 1689b5f7e755SEric Dumazet } else { 16909e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 1691bc044e8dSPaolo Abeni in_dev, itag); 1692b5f7e755SEric Dumazet if (err < 0) 1693bc044e8dSPaolo Abeni return err; 1694b5f7e755SEric Dumazet } 1695bc044e8dSPaolo Abeni return 0; 1696bc044e8dSPaolo Abeni } 1697bc044e8dSPaolo Abeni 1698bc044e8dSPaolo Abeni /* called in rcu_read_lock() section */ 1699bc044e8dSPaolo Abeni static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1700bc044e8dSPaolo Abeni u8 tos, struct net_device *dev, int our) 1701bc044e8dSPaolo Abeni { 1702bc044e8dSPaolo Abeni struct in_device *in_dev = __in_dev_get_rcu(dev); 1703bc044e8dSPaolo Abeni unsigned int flags = RTCF_MULTICAST; 1704bc044e8dSPaolo Abeni struct rtable *rth; 1705bc044e8dSPaolo Abeni u32 itag = 0; 1706bc044e8dSPaolo Abeni int err; 1707bc044e8dSPaolo Abeni 1708bc044e8dSPaolo Abeni err = ip_mc_validate_source(skb, daddr, saddr, tos, dev, in_dev, &itag); 1709bc044e8dSPaolo Abeni if (err) 1710bc044e8dSPaolo Abeni return err; 1711bc044e8dSPaolo Abeni 1712d08c4f35SDavid Ahern if (our) 1713d08c4f35SDavid Ahern flags |= RTCF_LOCAL; 1714d08c4f35SDavid Ahern 1715b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY)) 1716e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY; 1717e6175a2eSEyal Birger 1718d08c4f35SDavid Ahern rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST, 1719b5c8b3feSEyal Birger false); 17201da177e4SLinus Torvalds if (!rth) 1721bc044e8dSPaolo Abeni return -ENOBUFS; 17221da177e4SLinus Torvalds 1723c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1724d8d1f30bSChangli Gao rth->dst.tclassid = itag; 17251da177e4SLinus Torvalds #endif 1726cf911662SDavid S. Miller rth->dst.output = ip_rt_bug; 17279917e1e8SDavid S. Miller rth->rt_is_input= 1; 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1730f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev)) 1731d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 17321da177e4SLinus Torvalds #endif 17331da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_mc); 17341da177e4SLinus Torvalds 17359e6c6d17SLokesh Dhoundiyal skb_dst_drop(skb); 173689aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 173789aef892SDavid S. Miller return 0; 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev, 17421da177e4SLinus Torvalds struct in_device *in_dev, 17431da177e4SLinus Torvalds struct sk_buff *skb, 17449e12bb22SAl Viro __be32 daddr, 17459e12bb22SAl Viro __be32 saddr) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_src); 17481da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 17491da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) { 17501da177e4SLinus Torvalds /* 17511da177e4SLinus Torvalds * RFC1812 recommendation, if source is martian, 17521da177e4SLinus Torvalds * the only hint is MAC header. 17531da177e4SLinus Torvalds */ 1754058bd4d2SJoe Perches pr_warn("martian source %pI4 from %pI4, on dev %s\n", 1755673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 175698e399f8SArnaldo Carvalho de Melo if (dev->hard_header_len && skb_mac_header_was_set(skb)) { 1757058bd4d2SJoe Perches print_hex_dump(KERN_WARNING, "ll header: ", 1758058bd4d2SJoe Perches DUMP_PREFIX_OFFSET, 16, 1, 1759058bd4d2SJoe Perches skb_mac_header(skb), 1760b2c85100SDavid S. Miller dev->hard_header_len, false); 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds #endif 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 176647360228SEric Dumazet /* called in rcu_read_lock() section */ 17675969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb, 1768982721f3SDavid S. Miller const struct fib_result *res, 17691da177e4SLinus Torvalds struct in_device *in_dev, 1770c6cffba4SDavid S. Miller __be32 daddr, __be32 saddr, u32 tos) 17711da177e4SLinus Torvalds { 1772eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res); 1773eba618abSDavid Ahern struct net_device *dev = nhc->nhc_dev; 17742ffae99dSTimo Teräs struct fib_nh_exception *fnhe; 17751da177e4SLinus Torvalds struct rtable *rth; 17761da177e4SLinus Torvalds int err; 17771da177e4SLinus Torvalds struct in_device *out_dev; 1778b5c8b3feSEyal Birger bool do_cache; 1779fbdc0ad0SLi RongQing u32 itag = 0; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* get a working reference to the output device */ 1782eba618abSDavid Ahern out_dev = __in_dev_get_rcu(dev); 178351456b29SIan Morris if (!out_dev) { 1784e87cc472SJoe Perches net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n"); 17851da177e4SLinus Torvalds return -EINVAL; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 17885c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res), 17899e56e380SDavid S. Miller in_dev->dev, in_dev, &itag); 17901da177e4SLinus Torvalds if (err < 0) { 17911da177e4SLinus Torvalds ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr, 17921da177e4SLinus Torvalds saddr); 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds goto cleanup; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 1797e81da0e1SJulian Anastasov do_cache = res->fi && !itag; 1798e81da0e1SJulian Anastasov if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) && 1799eba618abSDavid Ahern skb->protocol == htons(ETH_P_IP)) { 1800bdf00467SDavid Ahern __be32 gw; 1801eba618abSDavid Ahern 1802bdf00467SDavid Ahern gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0; 1803eba618abSDavid Ahern if (IN_DEV_SHARED_MEDIA(out_dev) || 1804eba618abSDavid Ahern inet_addr_onlink(out_dev, saddr, gw)) 1805df4d9254SHannes Frederic Sowa IPCB(skb)->flags |= IPSKB_DOREDIRECT; 1806eba618abSDavid Ahern } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) { 18091da177e4SLinus Torvalds /* Not IP (i.e. ARP). Do not create route, if it is 18101da177e4SLinus Torvalds * invalid for proxy arp. DNAT routes are always valid. 181165324144SJesper Dangaard Brouer * 181265324144SJesper Dangaard Brouer * Proxy arp feature have been extended to allow, ARP 181365324144SJesper Dangaard Brouer * replies back to the same interface, to support 181465324144SJesper Dangaard Brouer * Private VLAN switch technologies. See arp.c. 18151da177e4SLinus Torvalds */ 181665324144SJesper Dangaard Brouer if (out_dev == in_dev && 181765324144SJesper Dangaard Brouer IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) { 18181da177e4SLinus Torvalds err = -EINVAL; 18191da177e4SLinus Torvalds goto cleanup; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 1823b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY)) 1824e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY; 1825e6175a2eSEyal Birger 1826a5995e71SDavid Ahern fnhe = find_exception(nhc, daddr); 1827e81da0e1SJulian Anastasov if (do_cache) { 182894720e3aSJulian Anastasov if (fnhe) 18292ffae99dSTimo Teräs rth = rcu_dereference(fnhe->fnhe_rth_input); 183094720e3aSJulian Anastasov else 18310f457a36SDavid Ahern rth = rcu_dereference(nhc->nhc_rth_input); 1832d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1833c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst); 1834d2d68ba9SDavid S. Miller goto out; 1835d2d68ba9SDavid S. Miller } 1836d2d68ba9SDavid S. Miller } 1837f2bb4bedSDavid S. Miller 1838b5c8b3feSEyal Birger rth = rt_dst_alloc(out_dev->dev, 0, res->type, 183962679a8dSVincent Bernat IN_DEV_ORCONF(out_dev, NOXFRM)); 18401da177e4SLinus Torvalds if (!rth) { 18411da177e4SLinus Torvalds err = -ENOBUFS; 18421da177e4SLinus Torvalds goto cleanup; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 18459917e1e8SDavid S. Miller rth->rt_is_input = 1; 1846a6254864SDuan Jiong RT_CACHE_STAT_INC(in_slow_tot); 18471da177e4SLinus Torvalds 1848d8d1f30bSChangli Gao rth->dst.input = ip_forward; 18491da177e4SLinus Torvalds 1850a4c2fd7fSWei Wang rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag, 1851a4c2fd7fSWei Wang do_cache); 18529942895bSDavid Ahern lwtunnel_set_redirect(&rth->dst); 1853c6cffba4SDavid S. Miller skb_dst_set(skb, &rth->dst); 1854d2d68ba9SDavid S. Miller out: 18551da177e4SLinus Torvalds err = 0; 18561da177e4SLinus Torvalds cleanup: 18571da177e4SLinus Torvalds return err; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 186079a13159SPeter Nørlund #ifdef CONFIG_IP_ROUTE_MULTIPATH 186179a13159SPeter Nørlund /* To make ICMP packets follow the right flow, the multipath hash is 1862bf4e0a3dSNikolay Aleksandrov * calculated from the inner IP addresses. 186379a13159SPeter Nørlund */ 1864bf4e0a3dSNikolay Aleksandrov static void ip_multipath_l3_keys(const struct sk_buff *skb, 1865bf4e0a3dSNikolay Aleksandrov struct flow_keys *hash_keys) 186679a13159SPeter Nørlund { 186779a13159SPeter Nørlund const struct iphdr *outer_iph = ip_hdr(skb); 18686f74b6c2SDavid Ahern const struct iphdr *key_iph = outer_iph; 1869bf4e0a3dSNikolay Aleksandrov const struct iphdr *inner_iph; 187079a13159SPeter Nørlund const struct icmphdr *icmph; 187179a13159SPeter Nørlund struct iphdr _inner_iph; 1872bf4e0a3dSNikolay Aleksandrov struct icmphdr _icmph; 1873bf4e0a3dSNikolay Aleksandrov 1874bf4e0a3dSNikolay Aleksandrov if (likely(outer_iph->protocol != IPPROTO_ICMP)) 18756f74b6c2SDavid Ahern goto out; 187679a13159SPeter Nørlund 187779a13159SPeter Nørlund if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0)) 18786f74b6c2SDavid Ahern goto out; 187979a13159SPeter Nørlund 188079a13159SPeter Nørlund icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph), 188179a13159SPeter Nørlund &_icmph); 188279a13159SPeter Nørlund if (!icmph) 18836f74b6c2SDavid Ahern goto out; 188479a13159SPeter Nørlund 188554074f1dSMatteo Croce if (!icmp_is_err(icmph->type)) 18866f74b6c2SDavid Ahern goto out; 188779a13159SPeter Nørlund 188879a13159SPeter Nørlund inner_iph = skb_header_pointer(skb, 188979a13159SPeter Nørlund outer_iph->ihl * 4 + sizeof(_icmph), 189079a13159SPeter Nørlund sizeof(_inner_iph), &_inner_iph); 189179a13159SPeter Nørlund if (!inner_iph) 18926f74b6c2SDavid Ahern goto out; 18936f74b6c2SDavid Ahern 18946f74b6c2SDavid Ahern key_iph = inner_iph; 18956f74b6c2SDavid Ahern out: 18966f74b6c2SDavid Ahern hash_keys->addrs.v4addrs.src = key_iph->saddr; 18976f74b6c2SDavid Ahern hash_keys->addrs.v4addrs.dst = key_iph->daddr; 189879a13159SPeter Nørlund } 189979a13159SPeter Nørlund 19004253b498SIdo Schimmel static u32 fib_multipath_custom_hash_outer(const struct net *net, 19014253b498SIdo Schimmel const struct sk_buff *skb, 19024253b498SIdo Schimmel bool *p_has_inner) 19034253b498SIdo Schimmel { 19048895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields); 19054253b498SIdo Schimmel struct flow_keys keys, hash_keys; 19064253b498SIdo Schimmel 19074253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 19084253b498SIdo Schimmel return 0; 19094253b498SIdo Schimmel 19104253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 19114253b498SIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP); 19124253b498SIdo Schimmel 19134253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 19144253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 19154253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 19164253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 19174253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 19184253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 19194253b498SIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto; 19204253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 19214253b498SIdo Schimmel hash_keys.ports.src = keys.ports.src; 19224253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 19234253b498SIdo Schimmel hash_keys.ports.dst = keys.ports.dst; 19244253b498SIdo Schimmel 19254253b498SIdo Schimmel *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION); 19263e453ca1SPetr Machata return fib_multipath_hash_from_keys(net, &hash_keys); 19274253b498SIdo Schimmel } 19284253b498SIdo Schimmel 19294253b498SIdo Schimmel static u32 fib_multipath_custom_hash_inner(const struct net *net, 19304253b498SIdo Schimmel const struct sk_buff *skb, 19314253b498SIdo Schimmel bool has_inner) 19324253b498SIdo Schimmel { 19338895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields); 19344253b498SIdo Schimmel struct flow_keys keys, hash_keys; 19354253b498SIdo Schimmel 19364253b498SIdo Schimmel /* We assume the packet carries an encapsulation, but if none was 19374253b498SIdo Schimmel * encountered during dissection of the outer flow, then there is no 19384253b498SIdo Schimmel * point in calling the flow dissector again. 19394253b498SIdo Schimmel */ 19404253b498SIdo Schimmel if (!has_inner) 19414253b498SIdo Schimmel return 0; 19424253b498SIdo Schimmel 19434253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK)) 19444253b498SIdo Schimmel return 0; 19454253b498SIdo Schimmel 19464253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 19474253b498SIdo Schimmel skb_flow_dissect_flow_keys(skb, &keys, 0); 19484253b498SIdo Schimmel 19494253b498SIdo Schimmel if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION)) 19504253b498SIdo Schimmel return 0; 19514253b498SIdo Schimmel 19524253b498SIdo Schimmel if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 19534253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 19544253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 19554253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 19564253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 19574253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 19584253b498SIdo Schimmel } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 19594253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 19604253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 19614253b498SIdo Schimmel hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 19624253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 19634253b498SIdo Schimmel hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 19644253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL) 19654253b498SIdo Schimmel hash_keys.tags.flow_label = keys.tags.flow_label; 19664253b498SIdo Schimmel } 19674253b498SIdo Schimmel 19684253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO) 19694253b498SIdo Schimmel hash_keys.basic.ip_proto = keys.basic.ip_proto; 19704253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT) 19714253b498SIdo Schimmel hash_keys.ports.src = keys.ports.src; 19724253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT) 19734253b498SIdo Schimmel hash_keys.ports.dst = keys.ports.dst; 19744253b498SIdo Schimmel 19753e453ca1SPetr Machata return fib_multipath_hash_from_keys(net, &hash_keys); 19764253b498SIdo Schimmel } 19774253b498SIdo Schimmel 19784253b498SIdo Schimmel static u32 fib_multipath_custom_hash_skb(const struct net *net, 19794253b498SIdo Schimmel const struct sk_buff *skb) 19804253b498SIdo Schimmel { 19814253b498SIdo Schimmel u32 mhash, mhash_inner; 19824253b498SIdo Schimmel bool has_inner = true; 19834253b498SIdo Schimmel 19844253b498SIdo Schimmel mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner); 19854253b498SIdo Schimmel mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner); 19864253b498SIdo Schimmel 19874253b498SIdo Schimmel return jhash_2words(mhash, mhash_inner, 0); 19884253b498SIdo Schimmel } 19894253b498SIdo Schimmel 19904253b498SIdo Schimmel static u32 fib_multipath_custom_hash_fl4(const struct net *net, 19914253b498SIdo Schimmel const struct flowi4 *fl4) 19924253b498SIdo Schimmel { 19938895a9c2SKuniyuki Iwashima u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields); 19944253b498SIdo Schimmel struct flow_keys hash_keys; 19954253b498SIdo Schimmel 19964253b498SIdo Schimmel if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 19974253b498SIdo Schimmel return 0; 19984253b498SIdo Schimmel 19994253b498SIdo Schimmel memset(&hash_keys, 0, sizeof(hash_keys)); 20004253b498SIdo Schimmel hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 20014253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 20024253b498SIdo Schimmel hash_keys.addrs.v4addrs.src = fl4->saddr; 20034253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 20044253b498SIdo Schimmel hash_keys.addrs.v4addrs.dst = fl4->daddr; 20054253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 20064253b498SIdo Schimmel hash_keys.basic.ip_proto = fl4->flowi4_proto; 20074253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 20084253b498SIdo Schimmel hash_keys.ports.src = fl4->fl4_sport; 20094253b498SIdo Schimmel if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 20104253b498SIdo Schimmel hash_keys.ports.dst = fl4->fl4_dport; 20114253b498SIdo Schimmel 20123e453ca1SPetr Machata return fib_multipath_hash_from_keys(net, &hash_keys); 20134253b498SIdo Schimmel } 20144253b498SIdo Schimmel 2015bf4e0a3dSNikolay Aleksandrov /* if skb is set it will be used and fl4 can be NULL */ 20167efc0b6bSDavid Ahern int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4, 2017e37b1e97SRoopa Prabhu const struct sk_buff *skb, struct flow_keys *flkeys) 2018bf4e0a3dSNikolay Aleksandrov { 20192a8e4997SIdo Schimmel u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0; 2020bf4e0a3dSNikolay Aleksandrov struct flow_keys hash_keys; 20212e68ea92SIdo Schimmel u32 mhash = 0; 2022bf4e0a3dSNikolay Aleksandrov 20237998c12aSKuniyuki Iwashima switch (READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_policy)) { 2024bf4e0a3dSNikolay Aleksandrov case 0: 2025bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys)); 2026bf4e0a3dSNikolay Aleksandrov hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2027bf4e0a3dSNikolay Aleksandrov if (skb) { 2028bf4e0a3dSNikolay Aleksandrov ip_multipath_l3_keys(skb, &hash_keys); 2029bf4e0a3dSNikolay Aleksandrov } else { 2030bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.src = fl4->saddr; 2031bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.dst = fl4->daddr; 2032bf4e0a3dSNikolay Aleksandrov } 20333e453ca1SPetr Machata mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2034bf4e0a3dSNikolay Aleksandrov break; 2035bf4e0a3dSNikolay Aleksandrov case 1: 2036bf4e0a3dSNikolay Aleksandrov /* skb is currently provided only when forwarding */ 2037bf4e0a3dSNikolay Aleksandrov if (skb) { 2038bf4e0a3dSNikolay Aleksandrov unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2039bf4e0a3dSNikolay Aleksandrov struct flow_keys keys; 2040bf4e0a3dSNikolay Aleksandrov 2041bf4e0a3dSNikolay Aleksandrov /* short-circuit if we already have L4 hash present */ 2042bf4e0a3dSNikolay Aleksandrov if (skb->l4_hash) 2043bf4e0a3dSNikolay Aleksandrov return skb_get_hash_raw(skb) >> 1; 2044ec7127a5SDavid Ahern 2045bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys)); 20461fe4b118SDavid Ahern 2047ec7127a5SDavid Ahern if (!flkeys) { 2048ec7127a5SDavid Ahern skb_flow_dissect_flow_keys(skb, &keys, flag); 2049ec7127a5SDavid Ahern flkeys = &keys; 2050ec7127a5SDavid Ahern } 2051ec7127a5SDavid Ahern 2052e37b1e97SRoopa Prabhu hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2053e37b1e97SRoopa Prabhu hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2054e37b1e97SRoopa Prabhu hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2055e37b1e97SRoopa Prabhu hash_keys.ports.src = flkeys->ports.src; 2056e37b1e97SRoopa Prabhu hash_keys.ports.dst = flkeys->ports.dst; 2057e37b1e97SRoopa Prabhu hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2058e37b1e97SRoopa Prabhu } else { 2059bf4e0a3dSNikolay Aleksandrov memset(&hash_keys, 0, sizeof(hash_keys)); 2060bf4e0a3dSNikolay Aleksandrov hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2061bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.src = fl4->saddr; 2062bf4e0a3dSNikolay Aleksandrov hash_keys.addrs.v4addrs.dst = fl4->daddr; 2063bf4e0a3dSNikolay Aleksandrov hash_keys.ports.src = fl4->fl4_sport; 2064bf4e0a3dSNikolay Aleksandrov hash_keys.ports.dst = fl4->fl4_dport; 2065bf4e0a3dSNikolay Aleksandrov hash_keys.basic.ip_proto = fl4->flowi4_proto; 2066bf4e0a3dSNikolay Aleksandrov } 20673e453ca1SPetr Machata mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2068bf4e0a3dSNikolay Aleksandrov break; 2069363887a2SStephen Suryaputra case 2: 2070363887a2SStephen Suryaputra memset(&hash_keys, 0, sizeof(hash_keys)); 2071363887a2SStephen Suryaputra /* skb is currently provided only when forwarding */ 2072363887a2SStephen Suryaputra if (skb) { 2073363887a2SStephen Suryaputra struct flow_keys keys; 2074363887a2SStephen Suryaputra 2075363887a2SStephen Suryaputra skb_flow_dissect_flow_keys(skb, &keys, 0); 2076828b2b44SStephen Suryaputra /* Inner can be v4 or v6 */ 2077828b2b44SStephen Suryaputra if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2078828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2079363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 2080363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 2081828b2b44SStephen Suryaputra } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2082828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2083828b2b44SStephen Suryaputra hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 2084828b2b44SStephen Suryaputra hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 2085828b2b44SStephen Suryaputra hash_keys.tags.flow_label = keys.tags.flow_label; 2086828b2b44SStephen Suryaputra hash_keys.basic.ip_proto = keys.basic.ip_proto; 2087363887a2SStephen Suryaputra } else { 2088363887a2SStephen Suryaputra /* Same as case 0 */ 2089828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2090828b2b44SStephen Suryaputra ip_multipath_l3_keys(skb, &hash_keys); 2091828b2b44SStephen Suryaputra } 2092828b2b44SStephen Suryaputra } else { 2093828b2b44SStephen Suryaputra /* Same as case 0 */ 2094828b2b44SStephen Suryaputra hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2095363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.src = fl4->saddr; 2096363887a2SStephen Suryaputra hash_keys.addrs.v4addrs.dst = fl4->daddr; 2097363887a2SStephen Suryaputra } 20983e453ca1SPetr Machata mhash = fib_multipath_hash_from_keys(net, &hash_keys); 2099363887a2SStephen Suryaputra break; 21004253b498SIdo Schimmel case 3: 21014253b498SIdo Schimmel if (skb) 21024253b498SIdo Schimmel mhash = fib_multipath_custom_hash_skb(net, skb); 21034253b498SIdo Schimmel else 21044253b498SIdo Schimmel mhash = fib_multipath_custom_hash_fl4(net, fl4); 21054253b498SIdo Schimmel break; 2106bf4e0a3dSNikolay Aleksandrov } 2107bf4e0a3dSNikolay Aleksandrov 210824ba1440Swenxu if (multipath_hash) 210924ba1440Swenxu mhash = jhash_2words(mhash, multipath_hash, 0); 211024ba1440Swenxu 2111bf4e0a3dSNikolay Aleksandrov return mhash >> 1; 2112bf4e0a3dSNikolay Aleksandrov } 211379a13159SPeter Nørlund #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 211479a13159SPeter Nørlund 21155969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb, 21161da177e4SLinus Torvalds struct fib_result *res, 21171da177e4SLinus Torvalds struct in_device *in_dev, 2118e37b1e97SRoopa Prabhu __be32 daddr, __be32 saddr, u32 tos, 2119e37b1e97SRoopa Prabhu struct flow_keys *hkeys) 21201da177e4SLinus Torvalds { 21211da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 21225481d73fSDavid Ahern if (res->fi && fib_info_num_path(res->fi) > 1) { 21237efc0b6bSDavid Ahern int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys); 21240e884c78SPeter Nørlund 21250e884c78SPeter Nørlund fib_select_multipath(res, h); 21266ac66cb0SSriram Yagnaraman IPCB(skb)->flags |= IPSKB_MULTIPATH; 21270e884c78SPeter Nørlund } 21281da177e4SLinus Torvalds #endif 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* create a routing cache entry */ 2131c6cffba4SDavid S. Miller return __mkroute_input(skb, res, in_dev, daddr, saddr, tos); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 213402b24941SPaolo Abeni /* Implements all the saddr-related checks as ip_route_input_slow(), 213502b24941SPaolo Abeni * assuming daddr is valid and the destination is not a local broadcast one. 213602b24941SPaolo Abeni * Uses the provided hint instead of performing a route lookup. 213702b24941SPaolo Abeni */ 213802b24941SPaolo Abeni int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr, 213902b24941SPaolo Abeni u8 tos, struct net_device *dev, 214002b24941SPaolo Abeni const struct sk_buff *hint) 214102b24941SPaolo Abeni { 214202b24941SPaolo Abeni struct in_device *in_dev = __in_dev_get_rcu(dev); 21432ce578caSMiaohe Lin struct rtable *rt = skb_rtable(hint); 214402b24941SPaolo Abeni struct net *net = dev_net(dev); 214502b24941SPaolo Abeni int err = -EINVAL; 214602b24941SPaolo Abeni u32 tag = 0; 214702b24941SPaolo Abeni 214858a4c9b1SEric Dumazet if (!in_dev) 214958a4c9b1SEric Dumazet return -EINVAL; 215058a4c9b1SEric Dumazet 215102b24941SPaolo Abeni if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 215202b24941SPaolo Abeni goto martian_source; 215302b24941SPaolo Abeni 215402b24941SPaolo Abeni if (ipv4_is_zeronet(saddr)) 215502b24941SPaolo Abeni goto martian_source; 215602b24941SPaolo Abeni 215702b24941SPaolo Abeni if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 215802b24941SPaolo Abeni goto martian_source; 215902b24941SPaolo Abeni 216002b24941SPaolo Abeni if (rt->rt_type != RTN_LOCAL) 216102b24941SPaolo Abeni goto skip_validate_source; 216202b24941SPaolo Abeni 2163be8b8dedSIdo Schimmel tos &= INET_DSCP_MASK; 216402b24941SPaolo Abeni err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag); 216502b24941SPaolo Abeni if (err < 0) 216602b24941SPaolo Abeni goto martian_source; 216702b24941SPaolo Abeni 216802b24941SPaolo Abeni skip_validate_source: 216902b24941SPaolo Abeni skb_dst_copy(skb, hint); 217002b24941SPaolo Abeni return 0; 217102b24941SPaolo Abeni 217202b24941SPaolo Abeni martian_source: 217302b24941SPaolo Abeni ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 217402b24941SPaolo Abeni return err; 217502b24941SPaolo Abeni } 217602b24941SPaolo Abeni 2177b87b04f5SDavid Ahern /* get device for dst_alloc with local routes */ 2178b87b04f5SDavid Ahern static struct net_device *ip_rt_get_dev(struct net *net, 2179b87b04f5SDavid Ahern const struct fib_result *res) 2180b87b04f5SDavid Ahern { 2181b87b04f5SDavid Ahern struct fib_nh_common *nhc = res->fi ? res->nhc : NULL; 2182b87b04f5SDavid Ahern struct net_device *dev = NULL; 2183b87b04f5SDavid Ahern 2184b87b04f5SDavid Ahern if (nhc) 2185b87b04f5SDavid Ahern dev = l3mdev_master_dev_rcu(nhc->nhc_dev); 2186b87b04f5SDavid Ahern 2187b87b04f5SDavid Ahern return dev ? : net->loopback_dev; 2188b87b04f5SDavid Ahern } 2189b87b04f5SDavid Ahern 21901da177e4SLinus Torvalds /* 21911da177e4SLinus Torvalds * NOTE. We drop all the packets that has local source 21921da177e4SLinus Torvalds * addresses, because every properly looped back packet 21931da177e4SLinus Torvalds * must have correct destination already attached by output routine. 219402b24941SPaolo Abeni * Changes in the enforced policies must be applied also to 219502b24941SPaolo Abeni * ip_route_use_hint(). 21961da177e4SLinus Torvalds * 21971da177e4SLinus Torvalds * Such approach solves two big problems: 21981da177e4SLinus Torvalds * 1. Not simplex devices are handled properly. 21991da177e4SLinus Torvalds * 2. IP spoofing attempts are filtered with 100% of guarantee. 2200ebc0ffaeSEric Dumazet * called with rcu_read_lock() 22011da177e4SLinus Torvalds */ 22021da177e4SLinus Torvalds 22039e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, 22045510cdf7SDavid Ahern u8 tos, struct net_device *dev, 22055510cdf7SDavid Ahern struct fib_result *res) 22061da177e4SLinus Torvalds { 220796d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 2208e37b1e97SRoopa Prabhu struct flow_keys *flkeys = NULL, _flkeys; 2209e37b1e97SRoopa Prabhu struct net *net = dev_net(dev); 22101b7179d3SThomas Graf struct ip_tunnel_info *tun_info; 2211e37b1e97SRoopa Prabhu int err = -EINVAL; 221295c96174SEric Dumazet unsigned int flags = 0; 22131da177e4SLinus Torvalds u32 itag = 0; 22141da177e4SLinus Torvalds struct rtable *rth; 2215e37b1e97SRoopa Prabhu struct flowi4 fl4; 22160a90478bSXin Long bool do_cache = true; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds /* IP on this device is disabled. */ 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds if (!in_dev) 22211da177e4SLinus Torvalds goto out; 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds /* Check for the most weird martians, which can be not detected 22246ad08600SShubhankar Kuranagatti * by fib_lookup. 22251da177e4SLinus Torvalds */ 22261da177e4SLinus Torvalds 222761adedf3SJiri Benc tun_info = skb_tunnel_info(skb); 222846fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 22291b7179d3SThomas Graf fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id; 22301b7179d3SThomas Graf else 22311b7179d3SThomas Graf fl4.flowi4_tun_key.tun_id = 0; 2232f38a9eb1SThomas Graf skb_dst_drop(skb); 2233f38a9eb1SThomas Graf 2234d0daebc3SThomas Graf if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 22351da177e4SLinus Torvalds goto martian_source; 22361da177e4SLinus Torvalds 22375510cdf7SDavid Ahern res->fi = NULL; 22385510cdf7SDavid Ahern res->table = NULL; 223927a954bdSAndy Walls if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0)) 22401da177e4SLinus Torvalds goto brd_input; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds /* Accept zero addresses only to limited broadcast; 22431da177e4SLinus Torvalds * I even do not know to fix it or not. Waiting for complains :-) 22441da177e4SLinus Torvalds */ 2245f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) 22461da177e4SLinus Torvalds goto martian_source; 22471da177e4SLinus Torvalds 2248d0daebc3SThomas Graf if (ipv4_is_zeronet(daddr)) 22491da177e4SLinus Torvalds goto martian_destination; 22501da177e4SLinus Torvalds 22519eb43e76SEric Dumazet /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(), 22529eb43e76SEric Dumazet * and call it once if daddr or/and saddr are loopback addresses 22539eb43e76SEric Dumazet */ 22549eb43e76SEric Dumazet if (ipv4_is_loopback(daddr)) { 22559eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 2256d0daebc3SThomas Graf goto martian_destination; 22579eb43e76SEric Dumazet } else if (ipv4_is_loopback(saddr)) { 22589eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 2259d0daebc3SThomas Graf goto martian_source; 2260d0daebc3SThomas Graf } 2261d0daebc3SThomas Graf 22621da177e4SLinus Torvalds /* 22631da177e4SLinus Torvalds * Now we are ready to route packet. 22641da177e4SLinus Torvalds */ 226540867d74SDavid Ahern fl4.flowi4_l3mdev = 0; 226668a5e3ddSDavid S. Miller fl4.flowi4_oif = 0; 2267e0d56fddSDavid Ahern fl4.flowi4_iif = dev->ifindex; 226868a5e3ddSDavid S. Miller fl4.flowi4_mark = skb->mark; 226968a5e3ddSDavid S. Miller fl4.flowi4_tos = tos; 227068a5e3ddSDavid S. Miller fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 2271b84f7878SDavid Ahern fl4.flowi4_flags = 0; 227268a5e3ddSDavid S. Miller fl4.daddr = daddr; 227368a5e3ddSDavid S. Miller fl4.saddr = saddr; 22748bcfd092SJulian Anastasov fl4.flowi4_uid = sock_net_uid(net, NULL); 22751869e226SDavid Ahern fl4.flowi4_multipath_hash = 0; 2276e37b1e97SRoopa Prabhu 22775a847a6eSDavid Ahern if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) { 2278e37b1e97SRoopa Prabhu flkeys = &_flkeys; 22795a847a6eSDavid Ahern } else { 22805a847a6eSDavid Ahern fl4.flowi4_proto = 0; 22815a847a6eSDavid Ahern fl4.fl4_sport = 0; 22825a847a6eSDavid Ahern fl4.fl4_dport = 0; 22835a847a6eSDavid Ahern } 2284e37b1e97SRoopa Prabhu 22855510cdf7SDavid Ahern err = fib_lookup(net, &fl4, res, 0); 2286cd0f0b95SDuan Jiong if (err != 0) { 2287cd0f0b95SDuan Jiong if (!IN_DEV_FORWARD(in_dev)) 2288cd0f0b95SDuan Jiong err = -EHOSTUNREACH; 22891da177e4SLinus Torvalds goto no_route; 2290cd0f0b95SDuan Jiong } 22911da177e4SLinus Torvalds 22925cbf777cSXin Long if (res->type == RTN_BROADCAST) { 22935cbf777cSXin Long if (IN_DEV_BFORWARD(in_dev)) 22945cbf777cSXin Long goto make_route; 22950a90478bSXin Long /* not do cache if bc_forwarding is enabled */ 22960598f8f3SEric Dumazet if (IPV4_DEVCONF_ALL_RO(net, BC_FORWARDING)) 22970a90478bSXin Long do_cache = false; 22981da177e4SLinus Torvalds goto brd_input; 22995cbf777cSXin Long } 23001da177e4SLinus Torvalds 23015510cdf7SDavid Ahern if (res->type == RTN_LOCAL) { 23025c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, 23030d5edc68SCong Wang 0, dev, in_dev, &itag); 2304b5f7e755SEric Dumazet if (err < 0) 23050d753960SDavid Ahern goto martian_source; 23061da177e4SLinus Torvalds goto local_input; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 2309cd0f0b95SDuan Jiong if (!IN_DEV_FORWARD(in_dev)) { 2310cd0f0b95SDuan Jiong err = -EHOSTUNREACH; 2311251da413SDavid S. Miller goto no_route; 2312cd0f0b95SDuan Jiong } 23135510cdf7SDavid Ahern if (res->type != RTN_UNICAST) 23141da177e4SLinus Torvalds goto martian_destination; 23151da177e4SLinus Torvalds 23165cbf777cSXin Long make_route: 2317e37b1e97SRoopa Prabhu err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, tos, flkeys); 23181da177e4SLinus Torvalds out: return err; 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds brd_input: 23211da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) 23221da177e4SLinus Torvalds goto e_inval; 23231da177e4SLinus Torvalds 232441347dcdSDavid S. Miller if (!ipv4_is_zeronet(saddr)) { 23259e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 23269e56e380SDavid S. Miller in_dev, &itag); 23271da177e4SLinus Torvalds if (err < 0) 23280d753960SDavid Ahern goto martian_source; 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds flags |= RTCF_BROADCAST; 23315510cdf7SDavid Ahern res->type = RTN_BROADCAST; 23321da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_brd); 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds local_input: 2335b5c8b3feSEyal Birger if (IN_DEV_ORCONF(in_dev, NOPOLICY)) 2336e6175a2eSEyal Birger IPCB(skb)->flags |= IPSKB_NOPOLICY; 2337e6175a2eSEyal Birger 23380a90478bSXin Long do_cache &= res->fi && !itag; 23390a90478bSXin Long if (do_cache) { 2340eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2341eba618abSDavid Ahern 23420f457a36SDavid Ahern rth = rcu_dereference(nhc->nhc_rth_input); 2343d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 2344c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst); 2345c6cffba4SDavid S. Miller err = 0; 2346c6cffba4SDavid S. Miller goto out; 2347d2d68ba9SDavid S. Miller } 2348d2d68ba9SDavid S. Miller } 2349d2d68ba9SDavid S. Miller 2350b87b04f5SDavid Ahern rth = rt_dst_alloc(ip_rt_get_dev(net, res), 2351b5c8b3feSEyal Birger flags | RTCF_LOCAL, res->type, false); 23521da177e4SLinus Torvalds if (!rth) 23531da177e4SLinus Torvalds goto e_nobufs; 23541da177e4SLinus Torvalds 2355d8d1f30bSChangli Gao rth->dst.output= ip_rt_bug; 2356cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 2357cf911662SDavid S. Miller rth->dst.tclassid = itag; 2358cf911662SDavid S. Miller #endif 23599917e1e8SDavid S. Miller rth->rt_is_input = 1; 2360571e7226SRoopa Prabhu 2361a6254864SDuan Jiong RT_CACHE_STAT_INC(in_slow_tot); 23625510cdf7SDavid Ahern if (res->type == RTN_UNREACHABLE) { 2363d8d1f30bSChangli Gao rth->dst.input= ip_error; 2364d8d1f30bSChangli Gao rth->dst.error= -err; 23651da177e4SLinus Torvalds rth->rt_flags &= ~RTCF_LOCAL; 23661da177e4SLinus Torvalds } 2367efd85700SThomas Graf 2368dcdfdf56SAlexei Starovoitov if (do_cache) { 2369eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2370efd85700SThomas Graf 2371eba618abSDavid Ahern rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate); 2372efd85700SThomas Graf if (lwtunnel_input_redirect(rth->dst.lwtstate)) { 2373efd85700SThomas Graf WARN_ON(rth->dst.input == lwtunnel_input); 2374efd85700SThomas Graf rth->dst.lwtstate->orig_input = rth->dst.input; 2375efd85700SThomas Graf rth->dst.input = lwtunnel_input; 2376efd85700SThomas Graf } 2377efd85700SThomas Graf 237887063a1fSDavid Ahern if (unlikely(!rt_cache_route(nhc, rth))) 2379dcdfdf56SAlexei Starovoitov rt_add_uncached_list(rth); 2380dcdfdf56SAlexei Starovoitov } 238189aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 2382b23dd4feSDavid S. Miller err = 0; 2383ebc0ffaeSEric Dumazet goto out; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds no_route: 23861da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_no_route); 23875510cdf7SDavid Ahern res->type = RTN_UNREACHABLE; 23885510cdf7SDavid Ahern res->fi = NULL; 23895510cdf7SDavid Ahern res->table = NULL; 23901da177e4SLinus Torvalds goto local_input; 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds /* 23931da177e4SLinus Torvalds * Do not cache martian addresses: they should be logged (RFC1812) 23941da177e4SLinus Torvalds */ 23951da177e4SLinus Torvalds martian_destination: 23961da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_dst); 23971da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 2398e87cc472SJoe Perches if (IN_DEV_LOG_MARTIANS(in_dev)) 2399e87cc472SJoe Perches net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n", 2400673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 24011da177e4SLinus Torvalds #endif 24022c2910a4SDietmar Eggemann 24031da177e4SLinus Torvalds e_inval: 24041da177e4SLinus Torvalds err = -EINVAL; 2405ebc0ffaeSEric Dumazet goto out; 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds e_nobufs: 24081da177e4SLinus Torvalds err = -ENOBUFS; 2409ebc0ffaeSEric Dumazet goto out; 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds martian_source: 24121da177e4SLinus Torvalds ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 2413ebc0ffaeSEric Dumazet goto out; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 24165510cdf7SDavid Ahern /* called with rcu_read_lock held */ 24175022e221SZhengchao Shao static int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr, 24185510cdf7SDavid Ahern u8 tos, struct net_device *dev, struct fib_result *res) 24195510cdf7SDavid Ahern { 24201da177e4SLinus Torvalds /* Multicast recognition logic is moved from route cache to here. 24216ad08600SShubhankar Kuranagatti * The problem was that too many Ethernet cards have broken/missing 24226ad08600SShubhankar Kuranagatti * hardware multicast filters :-( As result the host on multicasting 24236ad08600SShubhankar Kuranagatti * network acquires a lot of useless route cache entries, sort of 24246ad08600SShubhankar Kuranagatti * SDR messages from all the world. Now we try to get rid of them. 24256ad08600SShubhankar Kuranagatti * Really, provided software IP multicast filter is organized 24266ad08600SShubhankar Kuranagatti * reasonably (at least, hashed), it does not result in a slowdown 24276ad08600SShubhankar Kuranagatti * comparing with route cache reject entries. 24286ad08600SShubhankar Kuranagatti * Note, that multicast routers are not affected, because 24296ad08600SShubhankar Kuranagatti * route cache entry is created eventually. 24301da177e4SLinus Torvalds */ 2431f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 243296d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 2433e58e4159SDavid Ahern int our = 0; 24345510cdf7SDavid Ahern int err = -EINVAL; 24351da177e4SLinus Torvalds 243622c74764SPaolo Abeni if (!in_dev) 243722c74764SPaolo Abeni return err; 2438e58e4159SDavid Ahern our = ip_check_mc_rcu(in_dev, daddr, saddr, 2439eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol); 2440e58e4159SDavid Ahern 2441e58e4159SDavid Ahern /* check l3 master if no match yet */ 244222c74764SPaolo Abeni if (!our && netif_is_l3_slave(dev)) { 2443e58e4159SDavid Ahern struct in_device *l3_in_dev; 2444e58e4159SDavid Ahern 2445e58e4159SDavid Ahern l3_in_dev = __in_dev_get_rcu(skb->dev); 2446e58e4159SDavid Ahern if (l3_in_dev) 2447e58e4159SDavid Ahern our = ip_check_mc_rcu(l3_in_dev, daddr, saddr, 2448e58e4159SDavid Ahern ip_hdr(skb)->protocol); 2449e58e4159SDavid Ahern } 2450e58e4159SDavid Ahern 24511da177e4SLinus Torvalds if (our 24521da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 24539d4fb27dSJoe Perches || 24549d4fb27dSJoe Perches (!ipv4_is_local_multicast(daddr) && 2455f97c1e0cSJoe Perches IN_DEV_MFORWARD(in_dev)) 24561da177e4SLinus Torvalds #endif 24571da177e4SLinus Torvalds ) { 24585510cdf7SDavid Ahern err = ip_route_input_mc(skb, daddr, saddr, 24591da177e4SLinus Torvalds tos, dev, our); 2460e58e4159SDavid Ahern } 24615510cdf7SDavid Ahern return err; 24621da177e4SLinus Torvalds } 24635510cdf7SDavid Ahern 24645510cdf7SDavid Ahern return ip_route_input_slow(skb, daddr, saddr, tos, dev, res); 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24675022e221SZhengchao Shao int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr, 24685022e221SZhengchao Shao u8 tos, struct net_device *dev) 24695022e221SZhengchao Shao { 24705022e221SZhengchao Shao struct fib_result res; 24715022e221SZhengchao Shao int err; 24725022e221SZhengchao Shao 2473df9131c7SIdo Schimmel tos &= INET_DSCP_MASK; 24745022e221SZhengchao Shao rcu_read_lock(); 24755022e221SZhengchao Shao err = ip_route_input_rcu(skb, daddr, saddr, tos, dev, &res); 24765022e221SZhengchao Shao rcu_read_unlock(); 24775022e221SZhengchao Shao 24785022e221SZhengchao Shao return err; 24795022e221SZhengchao Shao } 24805022e221SZhengchao Shao EXPORT_SYMBOL(ip_route_input_noref); 24815022e221SZhengchao Shao 2482ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */ 2483982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res, 24841a00fee4SDavid Miller const struct flowi4 *fl4, int orig_oif, 2485f61759e6SJulian Anastasov struct net_device *dev_out, 24865ada5527SDavid S. Miller unsigned int flags) 24871da177e4SLinus Torvalds { 2488982721f3SDavid S. Miller struct fib_info *fi = res->fi; 2489f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe; 24905ada5527SDavid S. Miller struct in_device *in_dev; 2491982721f3SDavid S. Miller u16 type = res->type; 24925ada5527SDavid S. Miller struct rtable *rth; 2493c92b9655SJulian Anastasov bool do_cache; 24941da177e4SLinus Torvalds 2495d0daebc3SThomas Graf in_dev = __in_dev_get_rcu(dev_out); 2496d0daebc3SThomas Graf if (!in_dev) 2497d0daebc3SThomas Graf return ERR_PTR(-EINVAL); 2498d0daebc3SThomas Graf 2499d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 25005f02ce24SDavid Ahern if (ipv4_is_loopback(fl4->saddr) && 25015f02ce24SDavid Ahern !(dev_out->flags & IFF_LOOPBACK) && 25025f02ce24SDavid Ahern !netif_is_l3_master(dev_out)) 25035ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 25041da177e4SLinus Torvalds 250568a5e3ddSDavid S. Miller if (ipv4_is_lbcast(fl4->daddr)) 2506982721f3SDavid S. Miller type = RTN_BROADCAST; 250768a5e3ddSDavid S. Miller else if (ipv4_is_multicast(fl4->daddr)) 2508982721f3SDavid S. Miller type = RTN_MULTICAST; 250968a5e3ddSDavid S. Miller else if (ipv4_is_zeronet(fl4->daddr)) 25105ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 25111da177e4SLinus Torvalds 25121da177e4SLinus Torvalds if (dev_out->flags & IFF_LOOPBACK) 25131da177e4SLinus Torvalds flags |= RTCF_LOCAL; 25141da177e4SLinus Torvalds 251563617421SJulian Anastasov do_cache = true; 2516982721f3SDavid S. Miller if (type == RTN_BROADCAST) { 25171da177e4SLinus Torvalds flags |= RTCF_BROADCAST | RTCF_LOCAL; 2518982721f3SDavid S. Miller fi = NULL; 2519982721f3SDavid S. Miller } else if (type == RTN_MULTICAST) { 25201da177e4SLinus Torvalds flags |= RTCF_MULTICAST | RTCF_LOCAL; 2521813b3b5dSDavid S. Miller if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr, 2522813b3b5dSDavid S. Miller fl4->flowi4_proto)) 25231da177e4SLinus Torvalds flags &= ~RTCF_LOCAL; 252463617421SJulian Anastasov else 252563617421SJulian Anastasov do_cache = false; 25261da177e4SLinus Torvalds /* If multicast route do not exist use 2527dd28d1a0SEric Dumazet * default one, but do not gateway in this case. 2528dd28d1a0SEric Dumazet * Yes, it is hack. 25291da177e4SLinus Torvalds */ 2530982721f3SDavid S. Miller if (fi && res->prefixlen < 4) 2531982721f3SDavid S. Miller fi = NULL; 2532d6d5e999SChris Friesen } else if ((type == RTN_LOCAL) && (orig_oif != 0) && 2533d6d5e999SChris Friesen (orig_oif != dev_out->ifindex)) { 2534d6d5e999SChris Friesen /* For local routes that require a particular output interface 2535d6d5e999SChris Friesen * we do not want to cache the result. Caching the result 2536d6d5e999SChris Friesen * causes incorrect behaviour when there are multiple source 2537d6d5e999SChris Friesen * addresses on the interface, the end result being that if the 2538d6d5e999SChris Friesen * intended recipient is waiting on that interface for the 2539d6d5e999SChris Friesen * packet he won't receive it because it will be delivered on 2540d6d5e999SChris Friesen * the loopback interface and the IP_PKTINFO ipi_ifindex will 2541d6d5e999SChris Friesen * be set to the loopback interface as well. 2542d6d5e999SChris Friesen */ 254394720e3aSJulian Anastasov do_cache = false; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds 2546f2bb4bedSDavid S. Miller fnhe = NULL; 254763617421SJulian Anastasov do_cache &= fi != NULL; 254894720e3aSJulian Anastasov if (fi) { 2549eba618abSDavid Ahern struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2550d26b3a7cSEric Dumazet struct rtable __rcu **prth; 2551d26b3a7cSEric Dumazet 2552a5995e71SDavid Ahern fnhe = find_exception(nhc, fl4->daddr); 255394720e3aSJulian Anastasov if (!do_cache) 255494720e3aSJulian Anastasov goto add; 2555deed49dfSXin Long if (fnhe) { 25562ffae99dSTimo Teräs prth = &fnhe->fnhe_rth_output; 2557deed49dfSXin Long } else { 2558c92b9655SJulian Anastasov if (unlikely(fl4->flowi4_flags & 2559c92b9655SJulian Anastasov FLOWI_FLAG_KNOWN_NH && 2560bdf00467SDavid Ahern !(nhc->nhc_gw_family && 2561eba618abSDavid Ahern nhc->nhc_scope == RT_SCOPE_LINK))) { 2562c92b9655SJulian Anastasov do_cache = false; 2563c92b9655SJulian Anastasov goto add; 2564c92b9655SJulian Anastasov } 25650f457a36SDavid Ahern prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output); 256694720e3aSJulian Anastasov } 2567d26b3a7cSEric Dumazet rth = rcu_dereference(*prth); 25689df16efaSWei Wang if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst)) 2569f2bb4bedSDavid S. Miller return rth; 2570f2bb4bedSDavid S. Miller } 2571c92b9655SJulian Anastasov 2572c92b9655SJulian Anastasov add: 2573d08c4f35SDavid Ahern rth = rt_dst_alloc(dev_out, flags, type, 257462679a8dSVincent Bernat IN_DEV_ORCONF(in_dev, NOXFRM)); 25758391d07bSDimitris Michailidis if (!rth) 25765ada5527SDavid S. Miller return ERR_PTR(-ENOBUFS); 25778391d07bSDimitris Michailidis 25789438c871SDavid Ahern rth->rt_iif = orig_oif; 2579b7503e0cSDavid Ahern 25801da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_tot); 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 25831da177e4SLinus Torvalds if (flags & RTCF_LOCAL && 25841da177e4SLinus Torvalds !(dev_out->flags & IFF_LOOPBACK)) { 2585d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 25861da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_mc); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 2589982721f3SDavid S. Miller if (type == RTN_MULTICAST) { 25901da177e4SLinus Torvalds if (IN_DEV_MFORWARD(in_dev) && 2591813b3b5dSDavid S. Miller !ipv4_is_local_multicast(fl4->daddr)) { 2592d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 2593d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds #endif 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 2599a4c2fd7fSWei Wang rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache); 26009942895bSDavid Ahern lwtunnel_set_redirect(&rth->dst); 26011da177e4SLinus Torvalds 26025ada5527SDavid S. Miller return rth; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds /* 26061da177e4SLinus Torvalds * Major route resolver routine. 26071da177e4SLinus Torvalds */ 26081da177e4SLinus Torvalds 26093abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4, 2610bf4e0a3dSNikolay Aleksandrov const struct sk_buff *skb) 26111da177e4SLinus Torvalds { 2612d0ea2b12SEric Dumazet struct fib_result res = { 2613d0ea2b12SEric Dumazet .type = RTN_UNSPEC, 2614d0ea2b12SEric Dumazet .fi = NULL, 2615d0ea2b12SEric Dumazet .table = NULL, 2616d0ea2b12SEric Dumazet .tclassid = 0, 2617d0ea2b12SEric Dumazet }; 26185ada5527SDavid S. Miller struct rtable *rth; 26191da177e4SLinus Torvalds 26201fb9489bSPavel Emelyanov fl4->flowi4_iif = LOOPBACK_IFINDEX; 2621*a63cef46SIdo Schimmel fl4->flowi4_tos &= INET_DSCP_MASK; 262244713b67SDavid S. Miller 2623010c2708SDavid S. Miller rcu_read_lock(); 26243abd1adeSDavid Ahern rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb); 26253abd1adeSDavid Ahern rcu_read_unlock(); 26263abd1adeSDavid Ahern 26273abd1adeSDavid Ahern return rth; 26283abd1adeSDavid Ahern } 26293abd1adeSDavid Ahern EXPORT_SYMBOL_GPL(ip_route_output_key_hash); 26303abd1adeSDavid Ahern 26313abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4, 26323abd1adeSDavid Ahern struct fib_result *res, 26333abd1adeSDavid Ahern const struct sk_buff *skb) 26343abd1adeSDavid Ahern { 26353abd1adeSDavid Ahern struct net_device *dev_out = NULL; 26363abd1adeSDavid Ahern int orig_oif = fl4->flowi4_oif; 26373abd1adeSDavid Ahern unsigned int flags = 0; 26383abd1adeSDavid Ahern struct rtable *rth; 2639595e0651SStefano Brivio int err; 26403abd1adeSDavid Ahern 2641813b3b5dSDavid S. Miller if (fl4->saddr) { 2642813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->saddr) || 2643813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->saddr) || 2644595e0651SStefano Brivio ipv4_is_zeronet(fl4->saddr)) { 2645595e0651SStefano Brivio rth = ERR_PTR(-EINVAL); 26461da177e4SLinus Torvalds goto out; 2647595e0651SStefano Brivio } 2648595e0651SStefano Brivio 2649595e0651SStefano Brivio rth = ERR_PTR(-ENETUNREACH); 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds /* I removed check for oif == dev_out->oif here. 26526ad08600SShubhankar Kuranagatti * It was wrong for two reasons: 26536ad08600SShubhankar Kuranagatti * 1. ip_dev_find(net, saddr) can return wrong iface, if saddr 26546ad08600SShubhankar Kuranagatti * is assigned to multiple interfaces. 26556ad08600SShubhankar Kuranagatti * 2. Moreover, we are allowed to send packets with saddr 26566ad08600SShubhankar Kuranagatti * of another iface. --ANK 26571da177e4SLinus Torvalds */ 26581da177e4SLinus Torvalds 2659813b3b5dSDavid S. Miller if (fl4->flowi4_oif == 0 && 2660813b3b5dSDavid S. Miller (ipv4_is_multicast(fl4->daddr) || 2661813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr))) { 2662a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2663813b3b5dSDavid S. Miller dev_out = __ip_dev_find(net, fl4->saddr, false); 266451456b29SIan Morris if (!dev_out) 2665a210d01aSJulian Anastasov goto out; 2666a210d01aSJulian Anastasov 26671da177e4SLinus Torvalds /* Special hack: user can direct multicasts 26686ad08600SShubhankar Kuranagatti * and limited broadcast via necessary interface 26696ad08600SShubhankar Kuranagatti * without fiddling with IP_MULTICAST_IF or IP_PKTINFO. 26706ad08600SShubhankar Kuranagatti * This hack is not just for fun, it allows 26716ad08600SShubhankar Kuranagatti * vic,vat and friends to work. 26726ad08600SShubhankar Kuranagatti * They bind socket to loopback, set ttl to zero 26736ad08600SShubhankar Kuranagatti * and expect that it will work. 26746ad08600SShubhankar Kuranagatti * From the viewpoint of routing cache they are broken, 26756ad08600SShubhankar Kuranagatti * because we are not allowed to build multicast path 26766ad08600SShubhankar Kuranagatti * with loopback source addr (look, routing cache 26776ad08600SShubhankar Kuranagatti * cannot know, that ttl is zero, so that packet 26786ad08600SShubhankar Kuranagatti * will not leave this host and route is valid). 26796ad08600SShubhankar Kuranagatti * Luckily, this hack is good workaround. 26801da177e4SLinus Torvalds */ 26811da177e4SLinus Torvalds 2682813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 26831da177e4SLinus Torvalds goto make_route; 26841da177e4SLinus Torvalds } 2685a210d01aSJulian Anastasov 2686813b3b5dSDavid S. Miller if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) { 2687a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2688813b3b5dSDavid S. Miller if (!__ip_dev_find(net, fl4->saddr, false)) 2689a210d01aSJulian Anastasov goto out; 26901da177e4SLinus Torvalds } 2691a210d01aSJulian Anastasov } 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds 2694813b3b5dSDavid S. Miller if (fl4->flowi4_oif) { 2695813b3b5dSDavid S. Miller dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif); 2696b23dd4feSDavid S. Miller rth = ERR_PTR(-ENODEV); 269751456b29SIan Morris if (!dev_out) 26981da177e4SLinus Torvalds goto out; 2699e5ed6399SHerbert Xu 2700e5ed6399SHerbert Xu /* RACE: Check return value of inet_select_addr instead. */ 2701fc75fc83SEric Dumazet if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) { 2702b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH); 2703fc75fc83SEric Dumazet goto out; 2704fc75fc83SEric Dumazet } 2705813b3b5dSDavid S. Miller if (ipv4_is_local_multicast(fl4->daddr) || 27066a211654SAndrew Lunn ipv4_is_lbcast(fl4->daddr) || 27076a211654SAndrew Lunn fl4->flowi4_proto == IPPROTO_IGMP) { 2708813b3b5dSDavid S. Miller if (!fl4->saddr) 2709813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 27101da177e4SLinus Torvalds RT_SCOPE_LINK); 27111da177e4SLinus Torvalds goto make_route; 27121da177e4SLinus Torvalds } 27130a7e2260SJiri Benc if (!fl4->saddr) { 2714813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->daddr)) 2715813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 2716813b3b5dSDavid S. Miller fl4->flowi4_scope); 2717813b3b5dSDavid S. Miller else if (!fl4->daddr) 2718813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 27191da177e4SLinus Torvalds RT_SCOPE_HOST); 27201da177e4SLinus Torvalds } 2721613d09b3SDavid Ahern } 27221da177e4SLinus Torvalds 2723813b3b5dSDavid S. Miller if (!fl4->daddr) { 2724813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr; 2725813b3b5dSDavid S. Miller if (!fl4->daddr) 2726813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK); 2727b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 27281fb9489bSPavel Emelyanov fl4->flowi4_oif = LOOPBACK_IFINDEX; 27293abd1adeSDavid Ahern res->type = RTN_LOCAL; 27301da177e4SLinus Torvalds flags |= RTCF_LOCAL; 27311da177e4SLinus Torvalds goto make_route; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 27343abd1adeSDavid Ahern err = fib_lookup(net, fl4, res, 0); 27350315e382SNikola Forró if (err) { 27363abd1adeSDavid Ahern res->fi = NULL; 27373abd1adeSDavid Ahern res->table = NULL; 27386104e112SDavid Ahern if (fl4->flowi4_oif && 273940867d74SDavid Ahern (ipv4_is_multicast(fl4->daddr) || !fl4->flowi4_l3mdev)) { 27401da177e4SLinus Torvalds /* Apparently, routing tables are wrong. Assume, 27416ad08600SShubhankar Kuranagatti * that the destination is on link. 27426ad08600SShubhankar Kuranagatti * 27436ad08600SShubhankar Kuranagatti * WHY? DW. 27446ad08600SShubhankar Kuranagatti * Because we are allowed to send to iface 27456ad08600SShubhankar Kuranagatti * even if it has NO routes and NO assigned 27466ad08600SShubhankar Kuranagatti * addresses. When oif is specified, routing 27476ad08600SShubhankar Kuranagatti * tables are looked up with only one purpose: 27486ad08600SShubhankar Kuranagatti * to catch if destination is gatewayed, rather than 27496ad08600SShubhankar Kuranagatti * direct. Moreover, if MSG_DONTROUTE is set, 27506ad08600SShubhankar Kuranagatti * we send packet, ignoring both routing tables 27516ad08600SShubhankar Kuranagatti * and ifaddr state. --ANK 27526ad08600SShubhankar Kuranagatti * 27536ad08600SShubhankar Kuranagatti * 27546ad08600SShubhankar Kuranagatti * We could make it even if oif is unknown, 27556ad08600SShubhankar Kuranagatti * likely IPv6, but we do not. 27561da177e4SLinus Torvalds */ 27571da177e4SLinus Torvalds 2758813b3b5dSDavid S. Miller if (fl4->saddr == 0) 2759813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 27601da177e4SLinus Torvalds RT_SCOPE_LINK); 27613abd1adeSDavid Ahern res->type = RTN_UNICAST; 27621da177e4SLinus Torvalds goto make_route; 27631da177e4SLinus Torvalds } 27640315e382SNikola Forró rth = ERR_PTR(err); 27651da177e4SLinus Torvalds goto out; 27661da177e4SLinus Torvalds } 27671da177e4SLinus Torvalds 27683abd1adeSDavid Ahern if (res->type == RTN_LOCAL) { 2769813b3b5dSDavid S. Miller if (!fl4->saddr) { 27703abd1adeSDavid Ahern if (res->fi->fib_prefsrc) 27713abd1adeSDavid Ahern fl4->saddr = res->fi->fib_prefsrc; 27729fc3bbb4SJoel Sing else 2773813b3b5dSDavid S. Miller fl4->saddr = fl4->daddr; 27749fc3bbb4SJoel Sing } 27755f02ce24SDavid Ahern 27765f02ce24SDavid Ahern /* L3 master device is the loopback for that domain */ 27773abd1adeSDavid Ahern dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? : 2778b7c8487cSRobert Shearman net->loopback_dev; 2779839da4d9SDavid Ahern 2780839da4d9SDavid Ahern /* make sure orig_oif points to fib result device even 2781839da4d9SDavid Ahern * though packet rx/tx happens over loopback or l3mdev 2782839da4d9SDavid Ahern */ 2783839da4d9SDavid Ahern orig_oif = FIB_RES_OIF(*res); 2784839da4d9SDavid Ahern 2785813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 27861da177e4SLinus Torvalds flags |= RTCF_LOCAL; 27871da177e4SLinus Torvalds goto make_route; 27881da177e4SLinus Torvalds } 27891da177e4SLinus Torvalds 27903abd1adeSDavid Ahern fib_select_path(net, res, fl4, skb); 27911da177e4SLinus Torvalds 27923abd1adeSDavid Ahern dev_out = FIB_RES_DEV(*res); 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds make_route: 27953abd1adeSDavid Ahern rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags); 27961da177e4SLinus Torvalds 2797010c2708SDavid S. Miller out: 2798b23dd4feSDavid S. Miller return rth; 27991da177e4SLinus Torvalds } 2800d8c97a94SArnaldo Carvalho de Melo 280114e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = { 280214e50e57SDavid S. Miller .family = AF_INET, 2803214f45c9SEric Dumazet .default_advmss = ipv4_default_advmss, 2804d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 2805c4c877b2SDaniel Borkmann .check = dst_blackhole_check, 2806c4c877b2SDaniel Borkmann .cow_metrics = dst_blackhole_cow_metrics, 2807c4c877b2SDaniel Borkmann .update_pmtu = dst_blackhole_update_pmtu, 2808c4c877b2SDaniel Borkmann .redirect = dst_blackhole_redirect, 2809c4c877b2SDaniel Borkmann .mtu = dst_blackhole_mtu, 281014e50e57SDavid S. Miller }; 281114e50e57SDavid S. Miller 28122774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig) 281314e50e57SDavid S. Miller { 281405d6d492SEric Dumazet struct rtable *ort = dst_rtable(dst_orig); 2815f5b0a874SDavid S. Miller struct rtable *rt; 281614e50e57SDavid S. Miller 2817762c8dc7SZhengchao Shao rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, DST_OBSOLETE_DEAD, 0); 281814e50e57SDavid S. Miller if (rt) { 2819d8d1f30bSChangli Gao struct dst_entry *new = &rt->dst; 282014e50e57SDavid S. Miller 282114e50e57SDavid S. Miller new->__use = 1; 2822352e512cSHerbert Xu new->input = dst_discard; 2823ede2059dSEric W. Biederman new->output = dst_discard_out; 282414e50e57SDavid S. Miller 28251dbe3252SWei Wang new->dev = net->loopback_dev; 2826d62607c3SJakub Kicinski netdev_hold(new->dev, &new->dev_tracker, GFP_ATOMIC); 282714e50e57SDavid S. Miller 28289917e1e8SDavid S. Miller rt->rt_is_input = ort->rt_is_input; 28295e2b61f7SDavid S. Miller rt->rt_iif = ort->rt_iif; 28305943634fSDavid S. Miller rt->rt_pmtu = ort->rt_pmtu; 2831d52e5a7eSSabrina Dubroca rt->rt_mtu_locked = ort->rt_mtu_locked; 283214e50e57SDavid S. Miller 2833ca4c3fc2Sfan.du rt->rt_genid = rt_genid_ipv4(net); 283414e50e57SDavid S. Miller rt->rt_flags = ort->rt_flags; 283514e50e57SDavid S. Miller rt->rt_type = ort->rt_type; 283677d5bc7eSDavid Ahern rt->rt_uses_gateway = ort->rt_uses_gateway; 28371550c171SDavid Ahern rt->rt_gw_family = ort->rt_gw_family; 28381550c171SDavid Ahern if (rt->rt_gw_family == AF_INET) 28391550c171SDavid Ahern rt->rt_gw4 = ort->rt_gw4; 28400f5f7d7bSDavid Ahern else if (rt->rt_gw_family == AF_INET6) 28410f5f7d7bSDavid Ahern rt->rt_gw6 = ort->rt_gw6; 284214e50e57SDavid S. Miller } 284314e50e57SDavid S. Miller 28442774c131SDavid S. Miller dst_release(dst_orig); 28452774c131SDavid S. Miller 28462774c131SDavid S. Miller return rt ? &rt->dst : ERR_PTR(-ENOMEM); 284714e50e57SDavid S. Miller } 284814e50e57SDavid S. Miller 28499d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4, 28506f9c9615SEric Dumazet const struct sock *sk) 28511da177e4SLinus Torvalds { 28529d6ec938SDavid S. Miller struct rtable *rt = __ip_route_output_key(net, flp4); 28531da177e4SLinus Torvalds 2854b23dd4feSDavid S. Miller if (IS_ERR(rt)) 2855b23dd4feSDavid S. Miller return rt; 28561da177e4SLinus Torvalds 2857874fb9e2SDavid Ahern if (flp4->flowi4_proto) { 2858874fb9e2SDavid Ahern flp4->flowi4_oif = rt->dst.dev->ifindex; 285905d6d492SEric Dumazet rt = dst_rtable(xfrm_lookup_route(net, &rt->dst, 28609d6ec938SDavid S. Miller flowi4_to_flowi(flp4), 286105d6d492SEric Dumazet sk, 0)); 2862874fb9e2SDavid Ahern } 28631da177e4SLinus Torvalds 2864b23dd4feSDavid S. Miller return rt; 28651da177e4SLinus Torvalds } 2866d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow); 2867d8c97a94SArnaldo Carvalho de Melo 28683765d35eSDavid Ahern /* called with rcu_read_lock held */ 2869404eb77eSRoopa Prabhu static int rt_fill_info(struct net *net, __be32 dst, __be32 src, 2870338bb57eSIdo Schimmel struct rtable *rt, u32 table_id, dscp_t dscp, 2871338bb57eSIdo Schimmel struct flowi4 *fl4, struct sk_buff *skb, u32 portid, 2872338bb57eSIdo Schimmel u32 seq, unsigned int flags) 28731da177e4SLinus Torvalds { 28741da177e4SLinus Torvalds struct rtmsg *r; 28751da177e4SLinus Torvalds struct nlmsghdr *nlh; 28762bc8ca40SSteffen Klassert unsigned long expires = 0; 2877f185071dSDavid S. Miller u32 error; 2878521f5490SJulian Anastasov u32 metrics[RTAX_MAX]; 2879be403ea1SThomas Graf 2880e93fb3e9SJohn Fastabend nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags); 288151456b29SIan Morris if (!nlh) 288226932566SPatrick McHardy return -EMSGSIZE; 2883be403ea1SThomas Graf 2884be403ea1SThomas Graf r = nlmsg_data(nlh); 28851da177e4SLinus Torvalds r->rtm_family = AF_INET; 28861da177e4SLinus Torvalds r->rtm_dst_len = 32; 28871da177e4SLinus Torvalds r->rtm_src_len = 0; 2888338bb57eSIdo Schimmel r->rtm_tos = inet_dscp_to_dsfield(dscp); 28898a430ed5SDavid Ahern r->rtm_table = table_id < 256 ? table_id : RT_TABLE_COMPAT; 2890c36ba660SDavid Ahern if (nla_put_u32(skb, RTA_TABLE, table_id)) 2891f3756b79SDavid S. Miller goto nla_put_failure; 28921da177e4SLinus Torvalds r->rtm_type = rt->rt_type; 28931da177e4SLinus Torvalds r->rtm_scope = RT_SCOPE_UNIVERSE; 28941da177e4SLinus Torvalds r->rtm_protocol = RTPROT_UNSPEC; 28951da177e4SLinus Torvalds r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; 28961da177e4SLinus Torvalds if (rt->rt_flags & RTCF_NOTIFY) 28971da177e4SLinus Torvalds r->rtm_flags |= RTM_F_NOTIFY; 2898df4d9254SHannes Frederic Sowa if (IPCB(skb)->flags & IPSKB_DOREDIRECT) 2899df4d9254SHannes Frederic Sowa r->rtm_flags |= RTCF_DOREDIRECT; 2900be403ea1SThomas Graf 2901930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_DST, dst)) 2902f3756b79SDavid S. Miller goto nla_put_failure; 29031a00fee4SDavid Miller if (src) { 29041da177e4SLinus Torvalds r->rtm_src_len = 32; 2905930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_SRC, src)) 2906f3756b79SDavid S. Miller goto nla_put_failure; 29071da177e4SLinus Torvalds } 2908f3756b79SDavid S. Miller if (rt->dst.dev && 2909f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2910f3756b79SDavid S. Miller goto nla_put_failure; 2911ae8cb932SOliver Herms if (rt->dst.lwtstate && 2912ae8cb932SOliver Herms lwtunnel_fill_encap(skb, rt->dst.lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0) 2913ae8cb932SOliver Herms goto nla_put_failure; 2914c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2915f3756b79SDavid S. Miller if (rt->dst.tclassid && 2916f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid)) 2917f3756b79SDavid S. Miller goto nla_put_failure; 29181da177e4SLinus Torvalds #endif 2919d948974cSStefano Brivio if (fl4 && !rt_is_input_route(rt) && 2920d6c0a4f6SDavid Miller fl4->saddr != src) { 2921930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr)) 2922f3756b79SDavid S. Miller goto nla_put_failure; 2923f3756b79SDavid S. Miller } 292477d5bc7eSDavid Ahern if (rt->rt_uses_gateway) { 29251550c171SDavid Ahern if (rt->rt_gw_family == AF_INET && 29260f5f7d7bSDavid Ahern nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) { 2927f3756b79SDavid S. Miller goto nla_put_failure; 29280f5f7d7bSDavid Ahern } else if (rt->rt_gw_family == AF_INET6) { 29290f5f7d7bSDavid Ahern int alen = sizeof(struct in6_addr); 29300f5f7d7bSDavid Ahern struct nlattr *nla; 29310f5f7d7bSDavid Ahern struct rtvia *via; 29320f5f7d7bSDavid Ahern 29330f5f7d7bSDavid Ahern nla = nla_reserve(skb, RTA_VIA, alen + 2); 29340f5f7d7bSDavid Ahern if (!nla) 29350f5f7d7bSDavid Ahern goto nla_put_failure; 29360f5f7d7bSDavid Ahern 29370f5f7d7bSDavid Ahern via = nla_data(nla); 29380f5f7d7bSDavid Ahern via->rtvia_family = AF_INET6; 29390f5f7d7bSDavid Ahern memcpy(via->rtvia_addr, &rt->rt_gw6, alen); 29400f5f7d7bSDavid Ahern } 294177d5bc7eSDavid Ahern } 2942be403ea1SThomas Graf 2943ee9a8f7aSSteffen Klassert expires = rt->dst.expires; 2944ee9a8f7aSSteffen Klassert if (expires) { 2945ee9a8f7aSSteffen Klassert unsigned long now = jiffies; 2946ee9a8f7aSSteffen Klassert 2947ee9a8f7aSSteffen Klassert if (time_before(now, expires)) 2948ee9a8f7aSSteffen Klassert expires -= now; 2949ee9a8f7aSSteffen Klassert else 2950ee9a8f7aSSteffen Klassert expires = 0; 2951ee9a8f7aSSteffen Klassert } 2952ee9a8f7aSSteffen Klassert 2953521f5490SJulian Anastasov memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 2954ee9a8f7aSSteffen Klassert if (rt->rt_pmtu && expires) 2955521f5490SJulian Anastasov metrics[RTAX_MTU - 1] = rt->rt_pmtu; 2956d52e5a7eSSabrina Dubroca if (rt->rt_mtu_locked && expires) 2957d52e5a7eSSabrina Dubroca metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU); 2958521f5490SJulian Anastasov if (rtnetlink_put_metrics(skb, metrics) < 0) 2959be403ea1SThomas Graf goto nla_put_failure; 2960be403ea1SThomas Graf 2961d948974cSStefano Brivio if (fl4) { 2962b4869889SDavid Miller if (fl4->flowi4_mark && 296368aaed54Sstephen hemminger nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark)) 2964f3756b79SDavid S. Miller goto nla_put_failure; 2965963bfeeeSEric Dumazet 2966622ec2c9SLorenzo Colitti if (!uid_eq(fl4->flowi4_uid, INVALID_UID) && 2967622ec2c9SLorenzo Colitti nla_put_u32(skb, RTA_UID, 2968d948974cSStefano Brivio from_kuid_munged(current_user_ns(), 2969d948974cSStefano Brivio fl4->flowi4_uid))) 2970622ec2c9SLorenzo Colitti goto nla_put_failure; 2971622ec2c9SLorenzo Colitti 2972c7537967SDavid S. Miller if (rt_is_input_route(rt)) { 29738caaf7b6SNicolas Dichtel #ifdef CONFIG_IP_MROUTE 2974d948974cSStefano Brivio if (ipv4_is_multicast(dst) && 2975d948974cSStefano Brivio !ipv4_is_local_multicast(dst) && 29760598f8f3SEric Dumazet IPV4_DEVCONF_ALL_RO(net, MC_FORWARDING)) { 29778caaf7b6SNicolas Dichtel int err = ipmr_get_route(net, skb, 29788caaf7b6SNicolas Dichtel fl4->saddr, fl4->daddr, 29799f09eaeaSDavid Ahern r, portid); 29802cf75070SNikolay Aleksandrov 29818caaf7b6SNicolas Dichtel if (err <= 0) { 29828caaf7b6SNicolas Dichtel if (err == 0) 29838caaf7b6SNicolas Dichtel return 0; 29848caaf7b6SNicolas Dichtel goto nla_put_failure; 29858caaf7b6SNicolas Dichtel } 29868caaf7b6SNicolas Dichtel } else 29878caaf7b6SNicolas Dichtel #endif 2988404eb77eSRoopa Prabhu if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif)) 2989f3756b79SDavid S. Miller goto nla_put_failure; 29901da177e4SLinus Torvalds } 2991d948974cSStefano Brivio } 2992d948974cSStefano Brivio 2993d948974cSStefano Brivio error = rt->dst.error; 29941da177e4SLinus Torvalds 2995f185071dSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0) 2996e3703b3dSThomas Graf goto nla_put_failure; 29971da177e4SLinus Torvalds 2998053c095aSJohannes Berg nlmsg_end(skb, nlh); 2999053c095aSJohannes Berg return 0; 3000be403ea1SThomas Graf 3001be403ea1SThomas Graf nla_put_failure: 300226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 300326932566SPatrick McHardy return -EMSGSIZE; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 3006ee28906fSStefano Brivio static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb, 3007ee28906fSStefano Brivio struct netlink_callback *cb, u32 table_id, 3008ee28906fSStefano Brivio struct fnhe_hash_bucket *bucket, int genid, 3009e93fb3e9SJohn Fastabend int *fa_index, int fa_start, unsigned int flags) 3010ee28906fSStefano Brivio { 3011ee28906fSStefano Brivio int i; 3012ee28906fSStefano Brivio 3013ee28906fSStefano Brivio for (i = 0; i < FNHE_HASH_SIZE; i++) { 3014ee28906fSStefano Brivio struct fib_nh_exception *fnhe; 3015ee28906fSStefano Brivio 3016ee28906fSStefano Brivio for (fnhe = rcu_dereference(bucket[i].chain); fnhe; 3017ee28906fSStefano Brivio fnhe = rcu_dereference(fnhe->fnhe_next)) { 3018ee28906fSStefano Brivio struct rtable *rt; 3019ee28906fSStefano Brivio int err; 3020ee28906fSStefano Brivio 3021ee28906fSStefano Brivio if (*fa_index < fa_start) 3022ee28906fSStefano Brivio goto next; 3023ee28906fSStefano Brivio 3024ee28906fSStefano Brivio if (fnhe->fnhe_genid != genid) 3025ee28906fSStefano Brivio goto next; 3026ee28906fSStefano Brivio 3027ee28906fSStefano Brivio if (fnhe->fnhe_expires && 3028ee28906fSStefano Brivio time_after(jiffies, fnhe->fnhe_expires)) 3029ee28906fSStefano Brivio goto next; 3030ee28906fSStefano Brivio 3031ee28906fSStefano Brivio rt = rcu_dereference(fnhe->fnhe_rth_input); 3032ee28906fSStefano Brivio if (!rt) 3033ee28906fSStefano Brivio rt = rcu_dereference(fnhe->fnhe_rth_output); 3034ee28906fSStefano Brivio if (!rt) 3035ee28906fSStefano Brivio goto next; 3036ee28906fSStefano Brivio 3037ee28906fSStefano Brivio err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt, 3038338bb57eSIdo Schimmel table_id, 0, NULL, skb, 3039ee28906fSStefano Brivio NETLINK_CB(cb->skb).portid, 3040e93fb3e9SJohn Fastabend cb->nlh->nlmsg_seq, flags); 3041ee28906fSStefano Brivio if (err) 3042ee28906fSStefano Brivio return err; 3043ee28906fSStefano Brivio next: 3044ee28906fSStefano Brivio (*fa_index)++; 3045ee28906fSStefano Brivio } 3046ee28906fSStefano Brivio } 3047ee28906fSStefano Brivio 3048ee28906fSStefano Brivio return 0; 3049ee28906fSStefano Brivio } 3050ee28906fSStefano Brivio 3051ee28906fSStefano Brivio int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb, 3052ee28906fSStefano Brivio u32 table_id, struct fib_info *fi, 3053e93fb3e9SJohn Fastabend int *fa_index, int fa_start, unsigned int flags) 3054ee28906fSStefano Brivio { 3055ee28906fSStefano Brivio struct net *net = sock_net(cb->skb->sk); 3056ee28906fSStefano Brivio int nhsel, genid = fnhe_genid(net); 3057ee28906fSStefano Brivio 3058ee28906fSStefano Brivio for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) { 3059ee28906fSStefano Brivio struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel); 3060ee28906fSStefano Brivio struct fnhe_hash_bucket *bucket; 3061ee28906fSStefano Brivio int err; 3062ee28906fSStefano Brivio 3063ee28906fSStefano Brivio if (nhc->nhc_flags & RTNH_F_DEAD) 3064ee28906fSStefano Brivio continue; 3065ee28906fSStefano Brivio 306693ed54b1SEric Dumazet rcu_read_lock(); 3067ee28906fSStefano Brivio bucket = rcu_dereference(nhc->nhc_exceptions); 306893ed54b1SEric Dumazet err = 0; 306993ed54b1SEric Dumazet if (bucket) 307093ed54b1SEric Dumazet err = fnhe_dump_bucket(net, skb, cb, table_id, bucket, 3071e93fb3e9SJohn Fastabend genid, fa_index, fa_start, 3072e93fb3e9SJohn Fastabend flags); 307393ed54b1SEric Dumazet rcu_read_unlock(); 3074ee28906fSStefano Brivio if (err) 3075ee28906fSStefano Brivio return err; 3076ee28906fSStefano Brivio } 3077ee28906fSStefano Brivio 3078ee28906fSStefano Brivio return 0; 3079ee28906fSStefano Brivio } 3080ee28906fSStefano Brivio 3081404eb77eSRoopa Prabhu static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst, 3082404eb77eSRoopa Prabhu u8 ip_proto, __be16 sport, 3083404eb77eSRoopa Prabhu __be16 dport) 3084404eb77eSRoopa Prabhu { 3085404eb77eSRoopa Prabhu struct sk_buff *skb; 3086404eb77eSRoopa Prabhu struct iphdr *iph; 3087404eb77eSRoopa Prabhu 3088404eb77eSRoopa Prabhu skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 3089404eb77eSRoopa Prabhu if (!skb) 3090404eb77eSRoopa Prabhu return NULL; 3091404eb77eSRoopa Prabhu 3092404eb77eSRoopa Prabhu /* Reserve room for dummy headers, this skb can pass 3093404eb77eSRoopa Prabhu * through good chunk of routing engine. 3094404eb77eSRoopa Prabhu */ 3095404eb77eSRoopa Prabhu skb_reset_mac_header(skb); 3096404eb77eSRoopa Prabhu skb_reset_network_header(skb); 3097404eb77eSRoopa Prabhu skb->protocol = htons(ETH_P_IP); 3098404eb77eSRoopa Prabhu iph = skb_put(skb, sizeof(struct iphdr)); 3099404eb77eSRoopa Prabhu iph->protocol = ip_proto; 3100404eb77eSRoopa Prabhu iph->saddr = src; 3101404eb77eSRoopa Prabhu iph->daddr = dst; 3102404eb77eSRoopa Prabhu iph->version = 0x4; 3103404eb77eSRoopa Prabhu iph->frag_off = 0; 3104404eb77eSRoopa Prabhu iph->ihl = 0x5; 3105404eb77eSRoopa Prabhu skb_set_transport_header(skb, skb->len); 3106404eb77eSRoopa Prabhu 3107404eb77eSRoopa Prabhu switch (iph->protocol) { 3108404eb77eSRoopa Prabhu case IPPROTO_UDP: { 3109404eb77eSRoopa Prabhu struct udphdr *udph; 3110404eb77eSRoopa Prabhu 3111404eb77eSRoopa Prabhu udph = skb_put_zero(skb, sizeof(struct udphdr)); 3112404eb77eSRoopa Prabhu udph->source = sport; 3113404eb77eSRoopa Prabhu udph->dest = dport; 311492548b0eSEric Dumazet udph->len = htons(sizeof(struct udphdr)); 3115404eb77eSRoopa Prabhu udph->check = 0; 3116404eb77eSRoopa Prabhu break; 3117404eb77eSRoopa Prabhu } 3118404eb77eSRoopa Prabhu case IPPROTO_TCP: { 3119404eb77eSRoopa Prabhu struct tcphdr *tcph; 3120404eb77eSRoopa Prabhu 3121404eb77eSRoopa Prabhu tcph = skb_put_zero(skb, sizeof(struct tcphdr)); 3122404eb77eSRoopa Prabhu tcph->source = sport; 3123404eb77eSRoopa Prabhu tcph->dest = dport; 3124404eb77eSRoopa Prabhu tcph->doff = sizeof(struct tcphdr) / 4; 3125404eb77eSRoopa Prabhu tcph->rst = 1; 3126404eb77eSRoopa Prabhu tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), 3127404eb77eSRoopa Prabhu src, dst, 0); 3128404eb77eSRoopa Prabhu break; 3129404eb77eSRoopa Prabhu } 3130404eb77eSRoopa Prabhu case IPPROTO_ICMP: { 3131404eb77eSRoopa Prabhu struct icmphdr *icmph; 3132404eb77eSRoopa Prabhu 3133404eb77eSRoopa Prabhu icmph = skb_put_zero(skb, sizeof(struct icmphdr)); 3134404eb77eSRoopa Prabhu icmph->type = ICMP_ECHO; 3135404eb77eSRoopa Prabhu icmph->code = 0; 3136404eb77eSRoopa Prabhu } 3137404eb77eSRoopa Prabhu } 3138404eb77eSRoopa Prabhu 3139404eb77eSRoopa Prabhu return skb; 3140404eb77eSRoopa Prabhu } 3141404eb77eSRoopa Prabhu 3142a00302b6SJakub Kicinski static int inet_rtm_valid_getroute_req(struct sk_buff *skb, 3143a00302b6SJakub Kicinski const struct nlmsghdr *nlh, 3144a00302b6SJakub Kicinski struct nlattr **tb, 3145a00302b6SJakub Kicinski struct netlink_ext_ack *extack) 3146a00302b6SJakub Kicinski { 3147a00302b6SJakub Kicinski struct rtmsg *rtm; 3148a00302b6SJakub Kicinski int i, err; 3149a00302b6SJakub Kicinski 3150a00302b6SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 3151a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, 3152a00302b6SJakub Kicinski "ipv4: Invalid header for route get request"); 3153a00302b6SJakub Kicinski return -EINVAL; 3154a00302b6SJakub Kicinski } 3155a00302b6SJakub Kicinski 3156a00302b6SJakub Kicinski if (!netlink_strict_get_check(skb)) 31578cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 3158a00302b6SJakub Kicinski rtm_ipv4_policy, extack); 3159a00302b6SJakub Kicinski 3160a00302b6SJakub Kicinski rtm = nlmsg_data(nlh); 3161a00302b6SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) || 3162a00302b6SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) || 3163a00302b6SJakub Kicinski rtm->rtm_table || rtm->rtm_protocol || 3164a00302b6SJakub Kicinski rtm->rtm_scope || rtm->rtm_type) { 3165a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request"); 3166a00302b6SJakub Kicinski return -EINVAL; 3167a00302b6SJakub Kicinski } 3168a00302b6SJakub Kicinski 3169a00302b6SJakub Kicinski if (rtm->rtm_flags & ~(RTM_F_NOTIFY | 3170a00302b6SJakub Kicinski RTM_F_LOOKUP_TABLE | 3171a00302b6SJakub Kicinski RTM_F_FIB_MATCH)) { 3172a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request"); 3173a00302b6SJakub Kicinski return -EINVAL; 3174a00302b6SJakub Kicinski } 3175a00302b6SJakub Kicinski 31768cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 3177a00302b6SJakub Kicinski rtm_ipv4_policy, extack); 3178a00302b6SJakub Kicinski if (err) 3179a00302b6SJakub Kicinski return err; 3180a00302b6SJakub Kicinski 3181a00302b6SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 3182a00302b6SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 3183a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4"); 3184a00302b6SJakub Kicinski return -EINVAL; 3185a00302b6SJakub Kicinski } 3186a00302b6SJakub Kicinski 3187a00302b6SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 3188a00302b6SJakub Kicinski if (!tb[i]) 3189a00302b6SJakub Kicinski continue; 3190a00302b6SJakub Kicinski 3191a00302b6SJakub Kicinski switch (i) { 3192a00302b6SJakub Kicinski case RTA_IIF: 3193a00302b6SJakub Kicinski case RTA_OIF: 3194a00302b6SJakub Kicinski case RTA_SRC: 3195a00302b6SJakub Kicinski case RTA_DST: 3196a00302b6SJakub Kicinski case RTA_IP_PROTO: 3197a00302b6SJakub Kicinski case RTA_SPORT: 3198a00302b6SJakub Kicinski case RTA_DPORT: 3199a00302b6SJakub Kicinski case RTA_MARK: 3200a00302b6SJakub Kicinski case RTA_UID: 3201a00302b6SJakub Kicinski break; 3202a00302b6SJakub Kicinski default: 3203a00302b6SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request"); 3204a00302b6SJakub Kicinski return -EINVAL; 3205a00302b6SJakub Kicinski } 3206a00302b6SJakub Kicinski } 3207a00302b6SJakub Kicinski 3208a00302b6SJakub Kicinski return 0; 3209a00302b6SJakub Kicinski } 3210a00302b6SJakub Kicinski 3211c21ef3e3SDavid Ahern static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3212c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 32131da177e4SLinus Torvalds { 32143b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3215d889ce3bSThomas Graf struct nlattr *tb[RTA_MAX+1]; 3216404eb77eSRoopa Prabhu u32 table_id = RT_TABLE_MAIN; 3217404eb77eSRoopa Prabhu __be16 sport = 0, dport = 0; 32183765d35eSDavid Ahern struct fib_result res = {}; 3219404eb77eSRoopa Prabhu u8 ip_proto = IPPROTO_UDP; 32201da177e4SLinus Torvalds struct rtable *rt = NULL; 3221404eb77eSRoopa Prabhu struct sk_buff *skb; 3222404eb77eSRoopa Prabhu struct rtmsg *rtm; 3223e8e3fbe9SMaciej Żenczykowski struct flowi4 fl4 = {}; 32249e12bb22SAl Viro __be32 dst = 0; 32259e12bb22SAl Viro __be32 src = 0; 3226404eb77eSRoopa Prabhu kuid_t uid; 32279e12bb22SAl Viro u32 iif; 3228d889ce3bSThomas Graf int err; 3229963bfeeeSEric Dumazet int mark; 32301da177e4SLinus Torvalds 3231a00302b6SJakub Kicinski err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 3232d889ce3bSThomas Graf if (err < 0) 3233404eb77eSRoopa Prabhu return err; 3234d889ce3bSThomas Graf 3235d889ce3bSThomas Graf rtm = nlmsg_data(nlh); 323667b61f6cSJiri Benc src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0; 323767b61f6cSJiri Benc dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0; 3238d889ce3bSThomas Graf iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 3239963bfeeeSEric Dumazet mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 3240622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 3241622ec2c9SLorenzo Colitti uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID])); 3242622ec2c9SLorenzo Colitti else 3243622ec2c9SLorenzo Colitti uid = (iif ? INVALID_UID : current_uid()); 32441da177e4SLinus Torvalds 3245404eb77eSRoopa Prabhu if (tb[RTA_IP_PROTO]) { 3246404eb77eSRoopa Prabhu err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 32475e1a99eaSHangbin Liu &ip_proto, AF_INET, extack); 3248404eb77eSRoopa Prabhu if (err) 3249404eb77eSRoopa Prabhu return err; 3250404eb77eSRoopa Prabhu } 3251bbadb9a2SFlorian Larysch 3252404eb77eSRoopa Prabhu if (tb[RTA_SPORT]) 3253404eb77eSRoopa Prabhu sport = nla_get_be16(tb[RTA_SPORT]); 3254404eb77eSRoopa Prabhu 3255404eb77eSRoopa Prabhu if (tb[RTA_DPORT]) 3256404eb77eSRoopa Prabhu dport = nla_get_be16(tb[RTA_DPORT]); 3257404eb77eSRoopa Prabhu 3258404eb77eSRoopa Prabhu skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport); 3259404eb77eSRoopa Prabhu if (!skb) 3260404eb77eSRoopa Prabhu return -ENOBUFS; 3261bbadb9a2SFlorian Larysch 3262d6c0a4f6SDavid Miller fl4.daddr = dst; 3263d6c0a4f6SDavid Miller fl4.saddr = src; 326447afa284SIdo Schimmel fl4.flowi4_tos = rtm->rtm_tos & INET_DSCP_MASK; 3265d6c0a4f6SDavid Miller fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0; 3266d6c0a4f6SDavid Miller fl4.flowi4_mark = mark; 3267622ec2c9SLorenzo Colitti fl4.flowi4_uid = uid; 3268404eb77eSRoopa Prabhu if (sport) 3269404eb77eSRoopa Prabhu fl4.fl4_sport = sport; 3270404eb77eSRoopa Prabhu if (dport) 3271404eb77eSRoopa Prabhu fl4.fl4_dport = dport; 3272404eb77eSRoopa Prabhu fl4.flowi4_proto = ip_proto; 3273d6c0a4f6SDavid Miller 32743765d35eSDavid Ahern rcu_read_lock(); 32753765d35eSDavid Ahern 32761da177e4SLinus Torvalds if (iif) { 3277d889ce3bSThomas Graf struct net_device *dev; 3278d889ce3bSThomas Graf 32793765d35eSDavid Ahern dev = dev_get_by_index_rcu(net, iif); 328051456b29SIan Morris if (!dev) { 32811da177e4SLinus Torvalds err = -ENODEV; 3282404eb77eSRoopa Prabhu goto errout_rcu; 3283d889ce3bSThomas Graf } 3284d889ce3bSThomas Graf 3285404eb77eSRoopa Prabhu fl4.flowi4_iif = iif; /* for rt_fill_info */ 32861da177e4SLinus Torvalds skb->dev = dev; 3287963bfeeeSEric Dumazet skb->mark = mark; 32881ebf1790SGuillaume Nault err = ip_route_input_rcu(skb, dst, src, 3289b1251a6fSIdo Schimmel rtm->rtm_tos & INET_DSCP_MASK, dev, 32901ebf1790SGuillaume Nault &res); 3291d889ce3bSThomas Graf 3292511c3f92SEric Dumazet rt = skb_rtable(skb); 3293d8d1f30bSChangli Gao if (err == 0 && rt->dst.error) 3294d8d1f30bSChangli Gao err = -rt->dst.error; 32951da177e4SLinus Torvalds } else { 32966503a304SLorenzo Colitti fl4.flowi4_iif = LOOPBACK_IFINDEX; 329721f94775SIdo Schimmel skb->dev = net->loopback_dev; 32983765d35eSDavid Ahern rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb); 3299b23dd4feSDavid S. Miller err = 0; 3300b23dd4feSDavid S. Miller if (IS_ERR(rt)) 3301b23dd4feSDavid S. Miller err = PTR_ERR(rt); 33022c87d63aSFlorian Westphal else 33032c87d63aSFlorian Westphal skb_dst_set(skb, &rt->dst); 33041da177e4SLinus Torvalds } 3305d889ce3bSThomas Graf 33061da177e4SLinus Torvalds if (err) 3307404eb77eSRoopa Prabhu goto errout_rcu; 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds if (rtm->rtm_flags & RTM_F_NOTIFY) 33101da177e4SLinus Torvalds rt->rt_flags |= RTCF_NOTIFY; 33111da177e4SLinus Torvalds 3312c36ba660SDavid Ahern if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE) 331368e813aaSDavid Ahern table_id = res.table ? res.table->tb_id : 0; 3314c36ba660SDavid Ahern 3315404eb77eSRoopa Prabhu /* reset skb for netlink reply msg */ 3316404eb77eSRoopa Prabhu skb_trim(skb, 0); 3317404eb77eSRoopa Prabhu skb_reset_network_header(skb); 3318404eb77eSRoopa Prabhu skb_reset_transport_header(skb); 3319404eb77eSRoopa Prabhu skb_reset_mac_header(skb); 3320404eb77eSRoopa Prabhu 3321bc3aae2bSRoopa Prabhu if (rtm->rtm_flags & RTM_F_FIB_MATCH) { 33221e301fd0SIdo Schimmel struct fib_rt_info fri; 33231e301fd0SIdo Schimmel 3324bc3aae2bSRoopa Prabhu if (!res.fi) { 3325bc3aae2bSRoopa Prabhu err = fib_props[res.type].error; 3326bc3aae2bSRoopa Prabhu if (!err) 3327bc3aae2bSRoopa Prabhu err = -EHOSTUNREACH; 3328404eb77eSRoopa Prabhu goto errout_rcu; 3329bc3aae2bSRoopa Prabhu } 33301e301fd0SIdo Schimmel fri.fi = res.fi; 33311e301fd0SIdo Schimmel fri.tb_id = table_id; 33321e301fd0SIdo Schimmel fri.dst = res.prefix; 33331e301fd0SIdo Schimmel fri.dst_len = res.prefixlen; 3334f036e682SIdo Schimmel fri.dscp = res.dscp; 33351e301fd0SIdo Schimmel fri.type = rt->rt_type; 333690b93f1bSIdo Schimmel fri.offload = 0; 333790b93f1bSIdo Schimmel fri.trap = 0; 333836c5100eSAmit Cohen fri.offload_failed = 0; 333990b93f1bSIdo Schimmel if (res.fa_head) { 334090b93f1bSIdo Schimmel struct fib_alias *fa; 334190b93f1bSIdo Schimmel 334290b93f1bSIdo Schimmel hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) { 334390b93f1bSIdo Schimmel u8 slen = 32 - fri.dst_len; 334490b93f1bSIdo Schimmel 334590b93f1bSIdo Schimmel if (fa->fa_slen == slen && 334690b93f1bSIdo Schimmel fa->tb_id == fri.tb_id && 3347888ade8fSGuillaume Nault fa->fa_dscp == fri.dscp && 334890b93f1bSIdo Schimmel fa->fa_info == res.fi && 334990b93f1bSIdo Schimmel fa->fa_type == fri.type) { 33509fcf986cSEric Dumazet fri.offload = READ_ONCE(fa->offload); 33519fcf986cSEric Dumazet fri.trap = READ_ONCE(fa->trap); 33520add5c59SBenjamin Poirier fri.offload_failed = 33530add5c59SBenjamin Poirier READ_ONCE(fa->offload_failed); 335490b93f1bSIdo Schimmel break; 335590b93f1bSIdo Schimmel } 335690b93f1bSIdo Schimmel } 335790b93f1bSIdo Schimmel } 3358b6179813SRoopa Prabhu err = fib_dump_info(skb, NETLINK_CB(in_skb).portid, 33591e301fd0SIdo Schimmel nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0); 3360bc3aae2bSRoopa Prabhu } else { 3361338bb57eSIdo Schimmel err = rt_fill_info(net, dst, src, rt, table_id, res.dscp, &fl4, 3362338bb57eSIdo Schimmel skb, NETLINK_CB(in_skb).portid, 3363e93fb3e9SJohn Fastabend nlh->nlmsg_seq, 0); 3364bc3aae2bSRoopa Prabhu } 33657b46a644SDavid S. Miller if (err < 0) 3366404eb77eSRoopa Prabhu goto errout_rcu; 33671da177e4SLinus Torvalds 33683765d35eSDavid Ahern rcu_read_unlock(); 33693765d35eSDavid Ahern 337015e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 33711da177e4SLinus Torvalds 3372d889ce3bSThomas Graf errout_free: 3373404eb77eSRoopa Prabhu return err; 3374404eb77eSRoopa Prabhu errout_rcu: 33753765d35eSDavid Ahern rcu_read_unlock(); 33761da177e4SLinus Torvalds kfree_skb(skb); 3377404eb77eSRoopa Prabhu goto errout_free; 33781da177e4SLinus Torvalds } 33791da177e4SLinus Torvalds 33801da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev) 33811da177e4SLinus Torvalds { 33824ccfe6d4SNicolas Dichtel rt_cache_flush(dev_net(in_dev->dev)); 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds 33851da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 3386082c7ca4SGao feng static int ip_rt_gc_interval __read_mostly = 60 * HZ; 3387082c7ca4SGao feng static int ip_rt_gc_min_interval __read_mostly = HZ / 2; 3388082c7ca4SGao feng static int ip_rt_gc_elasticity __read_mostly = 8; 3389773daa3cSArnd Bergmann static int ip_min_valid_pmtu __read_mostly = IPV4_MIN_MTU; 3390082c7ca4SGao feng 339178eb4ea2SJoel Granados static int ipv4_sysctl_rtcache_flush(const struct ctl_table *__ctl, int write, 339232927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 33931da177e4SLinus Torvalds { 33945aad1de5STimo Teräs struct net *net = (struct net *)__ctl->extra1; 33955aad1de5STimo Teräs 33961da177e4SLinus Torvalds if (write) { 33975aad1de5STimo Teräs rt_cache_flush(net); 33985aad1de5STimo Teräs fnhe_genid_bump(net); 33991da177e4SLinus Torvalds return 0; 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds 34021da177e4SLinus Torvalds return -EINVAL; 34031da177e4SLinus Torvalds } 34041da177e4SLinus Torvalds 3405fe2c6338SJoe Perches static struct ctl_table ipv4_route_table[] = { 34061da177e4SLinus Torvalds { 34071da177e4SLinus Torvalds .procname = "gc_thresh", 34081da177e4SLinus Torvalds .data = &ipv4_dst_ops.gc_thresh, 34091da177e4SLinus Torvalds .maxlen = sizeof(int), 34101da177e4SLinus Torvalds .mode = 0644, 34116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34121da177e4SLinus Torvalds }, 34131da177e4SLinus Torvalds { 34141da177e4SLinus Torvalds .procname = "max_size", 34151da177e4SLinus Torvalds .data = &ip_rt_max_size, 34161da177e4SLinus Torvalds .maxlen = sizeof(int), 34171da177e4SLinus Torvalds .mode = 0644, 34186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34191da177e4SLinus Torvalds }, 34201da177e4SLinus Torvalds { 34211da177e4SLinus Torvalds /* Deprecated. Use gc_min_interval_ms */ 34221da177e4SLinus Torvalds 34231da177e4SLinus Torvalds .procname = "gc_min_interval", 34241da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 34251da177e4SLinus Torvalds .maxlen = sizeof(int), 34261da177e4SLinus Torvalds .mode = 0644, 34276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 34281da177e4SLinus Torvalds }, 34291da177e4SLinus Torvalds { 34301da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 34311da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 34321da177e4SLinus Torvalds .maxlen = sizeof(int), 34331da177e4SLinus Torvalds .mode = 0644, 34346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 34351da177e4SLinus Torvalds }, 34361da177e4SLinus Torvalds { 34371da177e4SLinus Torvalds .procname = "gc_timeout", 34381da177e4SLinus Torvalds .data = &ip_rt_gc_timeout, 34391da177e4SLinus Torvalds .maxlen = sizeof(int), 34401da177e4SLinus Torvalds .mode = 0644, 34416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 34421da177e4SLinus Torvalds }, 34431da177e4SLinus Torvalds { 34449f28a2fcSEric Dumazet .procname = "gc_interval", 34459f28a2fcSEric Dumazet .data = &ip_rt_gc_interval, 34469f28a2fcSEric Dumazet .maxlen = sizeof(int), 34479f28a2fcSEric Dumazet .mode = 0644, 34489f28a2fcSEric Dumazet .proc_handler = proc_dointvec_jiffies, 34499f28a2fcSEric Dumazet }, 34509f28a2fcSEric Dumazet { 34511da177e4SLinus Torvalds .procname = "redirect_load", 34521da177e4SLinus Torvalds .data = &ip_rt_redirect_load, 34531da177e4SLinus Torvalds .maxlen = sizeof(int), 34541da177e4SLinus Torvalds .mode = 0644, 34556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34561da177e4SLinus Torvalds }, 34571da177e4SLinus Torvalds { 34581da177e4SLinus Torvalds .procname = "redirect_number", 34591da177e4SLinus Torvalds .data = &ip_rt_redirect_number, 34601da177e4SLinus Torvalds .maxlen = sizeof(int), 34611da177e4SLinus Torvalds .mode = 0644, 34626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34631da177e4SLinus Torvalds }, 34641da177e4SLinus Torvalds { 34651da177e4SLinus Torvalds .procname = "redirect_silence", 34661da177e4SLinus Torvalds .data = &ip_rt_redirect_silence, 34671da177e4SLinus Torvalds .maxlen = sizeof(int), 34681da177e4SLinus Torvalds .mode = 0644, 34696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34701da177e4SLinus Torvalds }, 34711da177e4SLinus Torvalds { 34721da177e4SLinus Torvalds .procname = "error_cost", 34731da177e4SLinus Torvalds .data = &ip_rt_error_cost, 34741da177e4SLinus Torvalds .maxlen = sizeof(int), 34751da177e4SLinus Torvalds .mode = 0644, 34766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34771da177e4SLinus Torvalds }, 34781da177e4SLinus Torvalds { 34791da177e4SLinus Torvalds .procname = "error_burst", 34801da177e4SLinus Torvalds .data = &ip_rt_error_burst, 34811da177e4SLinus Torvalds .maxlen = sizeof(int), 34821da177e4SLinus Torvalds .mode = 0644, 34836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34841da177e4SLinus Torvalds }, 34851da177e4SLinus Torvalds { 34861da177e4SLinus Torvalds .procname = "gc_elasticity", 34871da177e4SLinus Torvalds .data = &ip_rt_gc_elasticity, 34881da177e4SLinus Torvalds .maxlen = sizeof(int), 34891da177e4SLinus Torvalds .mode = 0644, 34906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 34911da177e4SLinus Torvalds }, 34921da177e4SLinus Torvalds }; 349339a23e75SDenis V. Lunev 34945cdda5f1SChristian Brauner static const char ipv4_route_flush_procname[] = "flush"; 34955cdda5f1SChristian Brauner 34961de6b15aSxu xin static struct ctl_table ipv4_route_netns_table[] = { 349739a23e75SDenis V. Lunev { 34985cdda5f1SChristian Brauner .procname = ipv4_route_flush_procname, 349939a23e75SDenis V. Lunev .maxlen = sizeof(int), 350039a23e75SDenis V. Lunev .mode = 0200, 35016d9f239aSAlexey Dobriyan .proc_handler = ipv4_sysctl_rtcache_flush, 350239a23e75SDenis V. Lunev }, 35031de6b15aSxu xin { 35041de6b15aSxu xin .procname = "min_pmtu", 35051de6b15aSxu xin .data = &init_net.ipv4.ip_rt_min_pmtu, 35061de6b15aSxu xin .maxlen = sizeof(int), 35071de6b15aSxu xin .mode = 0644, 35081de6b15aSxu xin .proc_handler = proc_dointvec_minmax, 35091de6b15aSxu xin .extra1 = &ip_min_valid_pmtu, 35101de6b15aSxu xin }, 35111135fad2Sxu xin { 35121135fad2Sxu xin .procname = "mtu_expires", 35131135fad2Sxu xin .data = &init_net.ipv4.ip_rt_mtu_expires, 35141135fad2Sxu xin .maxlen = sizeof(int), 35151135fad2Sxu xin .mode = 0644, 35161135fad2Sxu xin .proc_handler = proc_dointvec_jiffies, 35171135fad2Sxu xin }, 35182e9589ffSxu xin { 35192e9589ffSxu xin .procname = "min_adv_mss", 35202e9589ffSxu xin .data = &init_net.ipv4.ip_rt_min_advmss, 35212e9589ffSxu xin .maxlen = sizeof(int), 35222e9589ffSxu xin .mode = 0644, 35232e9589ffSxu xin .proc_handler = proc_dointvec, 35242e9589ffSxu xin }, 352539a23e75SDenis V. Lunev }; 352639a23e75SDenis V. Lunev 352739a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net) 352839a23e75SDenis V. Lunev { 352939a23e75SDenis V. Lunev struct ctl_table *tbl; 3530c899710fSJoel Granados size_t table_size = ARRAY_SIZE(ipv4_route_netns_table); 353139a23e75SDenis V. Lunev 35321de6b15aSxu xin tbl = ipv4_route_netns_table; 353309ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 35341de6b15aSxu xin int i; 35351de6b15aSxu xin 35361de6b15aSxu xin tbl = kmemdup(tbl, sizeof(ipv4_route_netns_table), GFP_KERNEL); 353751456b29SIan Morris if (!tbl) 353839a23e75SDenis V. Lunev goto err_dup; 3539464dc801SEric W. Biederman 35405cdda5f1SChristian Brauner /* Don't export non-whitelisted sysctls to unprivileged users */ 35415cdda5f1SChristian Brauner if (net->user_ns != &init_user_ns) { 35421c106eb0SJoel Granados if (tbl[0].procname != ipv4_route_flush_procname) 3543c899710fSJoel Granados table_size = 0; 3544c899710fSJoel Granados } 35451de6b15aSxu xin 35461de6b15aSxu xin /* Update the variables to point into the current struct net 35471de6b15aSxu xin * except for the first element flush 35481de6b15aSxu xin */ 35491c106eb0SJoel Granados for (i = 1; i < table_size; i++) 35501de6b15aSxu xin tbl[i].data += (void *)net - (void *)&init_net; 35515cdda5f1SChristian Brauner } 355239a23e75SDenis V. Lunev tbl[0].extra1 = net; 355339a23e75SDenis V. Lunev 3554c899710fSJoel Granados net->ipv4.route_hdr = register_net_sysctl_sz(net, "net/ipv4/route", 3555c899710fSJoel Granados tbl, table_size); 355651456b29SIan Morris if (!net->ipv4.route_hdr) 355739a23e75SDenis V. Lunev goto err_reg; 355839a23e75SDenis V. Lunev return 0; 355939a23e75SDenis V. Lunev 356039a23e75SDenis V. Lunev err_reg: 35611de6b15aSxu xin if (tbl != ipv4_route_netns_table) 356239a23e75SDenis V. Lunev kfree(tbl); 356339a23e75SDenis V. Lunev err_dup: 356439a23e75SDenis V. Lunev return -ENOMEM; 356539a23e75SDenis V. Lunev } 356639a23e75SDenis V. Lunev 356739a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net) 356839a23e75SDenis V. Lunev { 3569bfa858f2SThomas Weißschuh const struct ctl_table *tbl; 357039a23e75SDenis V. Lunev 357139a23e75SDenis V. Lunev tbl = net->ipv4.route_hdr->ctl_table_arg; 357239a23e75SDenis V. Lunev unregister_net_sysctl_table(net->ipv4.route_hdr); 35731de6b15aSxu xin BUG_ON(tbl == ipv4_route_netns_table); 357439a23e75SDenis V. Lunev kfree(tbl); 357539a23e75SDenis V. Lunev } 357639a23e75SDenis V. Lunev 357739a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = { 357839a23e75SDenis V. Lunev .init = sysctl_route_net_init, 357939a23e75SDenis V. Lunev .exit = sysctl_route_net_exit, 358039a23e75SDenis V. Lunev }; 35811da177e4SLinus Torvalds #endif 35821da177e4SLinus Torvalds 35831de6b15aSxu xin static __net_init int netns_ip_rt_init(struct net *net) 35841de6b15aSxu xin { 35851de6b15aSxu xin /* Set default value for namespaceified sysctls */ 35861de6b15aSxu xin net->ipv4.ip_rt_min_pmtu = DEFAULT_MIN_PMTU; 35871135fad2Sxu xin net->ipv4.ip_rt_mtu_expires = DEFAULT_MTU_EXPIRES; 35882e9589ffSxu xin net->ipv4.ip_rt_min_advmss = DEFAULT_MIN_ADVMSS; 35891de6b15aSxu xin return 0; 35901de6b15aSxu xin } 35911de6b15aSxu xin 35921de6b15aSxu xin static struct pernet_operations __net_initdata ip_rt_ops = { 35931de6b15aSxu xin .init = netns_ip_rt_init, 35941de6b15aSxu xin }; 35951de6b15aSxu xin 35963ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net) 35979f5e97e5SDenis V. Lunev { 3598ca4c3fc2Sfan.du atomic_set(&net->ipv4.rt_genid, 0); 35995aad1de5STimo Teräs atomic_set(&net->fnhe_genid, 0); 3600a251c17aSJason A. Donenfeld atomic_set(&net->ipv4.dev_addr_genid, get_random_u32()); 36019f5e97e5SDenis V. Lunev return 0; 36029f5e97e5SDenis V. Lunev } 36039f5e97e5SDenis V. Lunev 36043ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = { 36053ee94372SNeil Horman .init = rt_genid_init, 36069f5e97e5SDenis V. Lunev }; 36079f5e97e5SDenis V. Lunev 3608c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net) 3609c3426b47SDavid S. Miller { 3610c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3611c3426b47SDavid S. Miller 3612c3426b47SDavid S. Miller if (!bp) 3613c3426b47SDavid S. Miller return -ENOMEM; 3614c3426b47SDavid S. Miller inet_peer_base_init(bp); 3615c3426b47SDavid S. Miller net->ipv4.peers = bp; 3616c3426b47SDavid S. Miller return 0; 3617c3426b47SDavid S. Miller } 3618c3426b47SDavid S. Miller 3619c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net) 3620c3426b47SDavid S. Miller { 3621c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv4.peers; 3622c3426b47SDavid S. Miller 3623c3426b47SDavid S. Miller net->ipv4.peers = NULL; 362456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 3625c3426b47SDavid S. Miller kfree(bp); 3626c3426b47SDavid S. Miller } 3627c3426b47SDavid S. Miller 3628c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = { 3629c3426b47SDavid S. Miller .init = ipv4_inetpeer_init, 3630c3426b47SDavid S. Miller .exit = ipv4_inetpeer_exit, 3631c3426b47SDavid S. Miller }; 36329f5e97e5SDenis V. Lunev 3633c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 36347d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; 3635c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */ 36361da177e4SLinus Torvalds 36371da177e4SLinus Torvalds int __init ip_rt_init(void) 36381da177e4SLinus Torvalds { 3639aa6dd211SEric Dumazet void *idents_hash; 36405055c371SEric Dumazet int cpu; 36411da177e4SLinus Torvalds 3642aa6dd211SEric Dumazet /* For modern hosts, this will use 2 MB of memory */ 3643aa6dd211SEric Dumazet idents_hash = alloc_large_system_hash("IP idents", 3644aa6dd211SEric Dumazet sizeof(*ip_idents) + sizeof(*ip_tstamps), 3645aa6dd211SEric Dumazet 0, 3646aa6dd211SEric Dumazet 16, /* one bucket per 64 KB */ 3647aa6dd211SEric Dumazet HASH_ZERO, 3648aa6dd211SEric Dumazet NULL, 3649aa6dd211SEric Dumazet &ip_idents_mask, 3650aa6dd211SEric Dumazet 2048, 3651aa6dd211SEric Dumazet 256*1024); 365273f156a6SEric Dumazet 3653aa6dd211SEric Dumazet ip_idents = idents_hash; 365473f156a6SEric Dumazet 3655197173dbSJason A. Donenfeld get_random_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents)); 3656aa6dd211SEric Dumazet 3657aa6dd211SEric Dumazet ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents); 3658355b590cSEric Dumazet 36595055c371SEric Dumazet for_each_possible_cpu(cpu) { 36605055c371SEric Dumazet struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu); 36615055c371SEric Dumazet 36625055c371SEric Dumazet INIT_LIST_HEAD(&ul->head); 36635055c371SEric Dumazet spin_lock_init(&ul->lock); 36645055c371SEric Dumazet } 3665c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 36660dcec8c2SIngo Molnar ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct)); 36671da177e4SLinus Torvalds if (!ip_rt_acct) 36681da177e4SLinus Torvalds panic("IP: failed to allocate ip_rt_acct\n"); 36691da177e4SLinus Torvalds #endif 36701da177e4SLinus Torvalds 36717eb2bc24SKunwu Chan ipv4_dst_ops.kmem_cachep = KMEM_CACHE(rtable, 36727eb2bc24SKunwu Chan SLAB_HWCACHE_ALIGN | SLAB_PANIC); 36731da177e4SLinus Torvalds 367414e50e57SDavid S. Miller ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep; 367514e50e57SDavid S. Miller 3676fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_ops) < 0) 3677fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_ops counter\n"); 3678fc66f95cSEric Dumazet 3679fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0) 3680fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n"); 3681fc66f95cSEric Dumazet 368289aef892SDavid S. Miller ipv4_dst_ops.gc_thresh = ~0; 368389aef892SDavid S. Miller ip_rt_max_size = INT_MAX; 36841da177e4SLinus Torvalds 36851da177e4SLinus Torvalds devinet_init(); 36861da177e4SLinus Torvalds ip_fib_init(); 36871da177e4SLinus Torvalds 368873b38711SDenis V. Lunev if (ip_rt_proc_init()) 3689058bd4d2SJoe Perches pr_err("Unable to create route proc files\n"); 36901da177e4SLinus Torvalds #ifdef CONFIG_XFRM 36911da177e4SLinus Torvalds xfrm_init(); 3692703fb94eSSteffen Klassert xfrm4_init(); 36931da177e4SLinus Torvalds #endif 3694394f51abSFlorian Westphal rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, 3695394f51abSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 369663f3444fSThomas Graf 369739a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL 369839a23e75SDenis V. Lunev register_pernet_subsys(&sysctl_route_ops); 369939a23e75SDenis V. Lunev #endif 37001de6b15aSxu xin register_pernet_subsys(&ip_rt_ops); 37013ee94372SNeil Horman register_pernet_subsys(&rt_genid_ops); 3702c3426b47SDavid S. Miller register_pernet_subsys(&ipv4_inetpeer_ops); 37031bcdca3fSTim Hansen return 0; 37041da177e4SLinus Torvalds } 37051da177e4SLinus Torvalds 3706a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL 3707eeb61f71SAl Viro /* 3708eeb61f71SAl Viro * We really need to sanitize the damn ipv4 init order, then all 3709eeb61f71SAl Viro * this nonsense will go away. 3710eeb61f71SAl Viro */ 3711eeb61f71SAl Viro void __init ip_static_sysctl_init(void) 3712eeb61f71SAl Viro { 37134e5ca785SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table); 3714eeb61f71SAl Viro } 3715a1bc6eb4SAl Viro #endif 3716