11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * ROUTE - implementation of the IP router. 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 111da177e4SLinus Torvalds * Linus Torvalds, <Linus.Torvalds@helsinki.fi> 121da177e4SLinus Torvalds * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Fixes: 151da177e4SLinus Torvalds * Alan Cox : Verify area fixes. 161da177e4SLinus Torvalds * Alan Cox : cli() protects routing changes 171da177e4SLinus Torvalds * Rui Oliveira : ICMP routing table updates 181da177e4SLinus Torvalds * (rco@di.uminho.pt) Routing table insertion and update 191da177e4SLinus Torvalds * Linus Torvalds : Rewrote bits to be sensible 201da177e4SLinus Torvalds * Alan Cox : Added BSD route gw semantics 211da177e4SLinus Torvalds * Alan Cox : Super /proc >4K 221da177e4SLinus Torvalds * Alan Cox : MTU in route table 231da177e4SLinus Torvalds * Alan Cox : MSS actually. Also added the window 241da177e4SLinus Torvalds * clamper. 251da177e4SLinus Torvalds * Sam Lantinga : Fixed route matching in rt_del() 261da177e4SLinus Torvalds * Alan Cox : Routing cache support. 271da177e4SLinus Torvalds * Alan Cox : Removed compatibility cruft. 281da177e4SLinus Torvalds * Alan Cox : RTF_REJECT support. 291da177e4SLinus Torvalds * Alan Cox : TCP irtt support. 301da177e4SLinus Torvalds * Jonathan Naylor : Added Metric support. 311da177e4SLinus Torvalds * Miquel van Smoorenburg : BSD API fixes. 321da177e4SLinus Torvalds * Miquel van Smoorenburg : Metrics. 331da177e4SLinus Torvalds * Alan Cox : Use __u32 properly 341da177e4SLinus Torvalds * Alan Cox : Aligned routing errors more closely with BSD 351da177e4SLinus Torvalds * our system is still very different. 361da177e4SLinus Torvalds * Alan Cox : Faster /proc handling 371da177e4SLinus Torvalds * Alexey Kuznetsov : Massive rework to support tree based routing, 381da177e4SLinus Torvalds * routing caches and better behaviour. 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * Olaf Erb : irtt wasn't being copied right. 411da177e4SLinus Torvalds * Bjorn Ekwall : Kerneld route support. 421da177e4SLinus Torvalds * Alan Cox : Multicast fixed (I hope) 431da177e4SLinus Torvalds * Pavel Krauz : Limited broadcast fixed 441da177e4SLinus Torvalds * Mike McLagan : Routing by source 451da177e4SLinus Torvalds * Alexey Kuznetsov : End of old history. Split to fib.c and 461da177e4SLinus Torvalds * route.c and rewritten from scratch. 471da177e4SLinus Torvalds * Andi Kleen : Load-limit warning messages. 481da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 491da177e4SLinus Torvalds * Vitaly E. Lavrov : Race condition in ip_route_input_slow. 501da177e4SLinus Torvalds * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow. 511da177e4SLinus Torvalds * Vladimir V. Ivanov : IP rule info (flowid) is really useful. 521da177e4SLinus Torvalds * Marc Boucher : routing by fwmark 531da177e4SLinus Torvalds * Robert Olsson : Added rt_cache statistics 541da177e4SLinus Torvalds * Arnaldo C. Melo : Convert proc stuff to seq_file 55bb1d23b0SEric Dumazet * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes. 56cef2685eSIlia Sotnikov * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect 57cef2685eSIlia Sotnikov * Ilia Sotnikov : Removed TOS from hash calculations 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 601da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 611da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 621da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds 65afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 66afd46503SJoe Perches 671da177e4SLinus Torvalds #include <linux/module.h> 681da177e4SLinus Torvalds #include <asm/uaccess.h> 691da177e4SLinus Torvalds #include <linux/bitops.h> 701da177e4SLinus Torvalds #include <linux/types.h> 711da177e4SLinus Torvalds #include <linux/kernel.h> 721da177e4SLinus Torvalds #include <linux/mm.h> 731da177e4SLinus Torvalds #include <linux/string.h> 741da177e4SLinus Torvalds #include <linux/socket.h> 751da177e4SLinus Torvalds #include <linux/sockios.h> 761da177e4SLinus Torvalds #include <linux/errno.h> 771da177e4SLinus Torvalds #include <linux/in.h> 781da177e4SLinus Torvalds #include <linux/inet.h> 791da177e4SLinus Torvalds #include <linux/netdevice.h> 801da177e4SLinus Torvalds #include <linux/proc_fs.h> 811da177e4SLinus Torvalds #include <linux/init.h> 821da177e4SLinus Torvalds #include <linux/skbuff.h> 831da177e4SLinus Torvalds #include <linux/inetdevice.h> 841da177e4SLinus Torvalds #include <linux/igmp.h> 851da177e4SLinus Torvalds #include <linux/pkt_sched.h> 861da177e4SLinus Torvalds #include <linux/mroute.h> 871da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 881da177e4SLinus Torvalds #include <linux/random.h> 891da177e4SLinus Torvalds #include <linux/rcupdate.h> 901da177e4SLinus Torvalds #include <linux/times.h> 915a0e3ad6STejun Heo #include <linux/slab.h> 92352e512cSHerbert Xu #include <net/dst.h> 93457c4cbcSEric W. Biederman #include <net/net_namespace.h> 941da177e4SLinus Torvalds #include <net/protocol.h> 951da177e4SLinus Torvalds #include <net/ip.h> 961da177e4SLinus Torvalds #include <net/route.h> 971da177e4SLinus Torvalds #include <net/inetpeer.h> 981da177e4SLinus Torvalds #include <net/sock.h> 991da177e4SLinus Torvalds #include <net/ip_fib.h> 1001da177e4SLinus Torvalds #include <net/arp.h> 1011da177e4SLinus Torvalds #include <net/tcp.h> 1021da177e4SLinus Torvalds #include <net/icmp.h> 1031da177e4SLinus Torvalds #include <net/xfrm.h> 1048d71740cSTom Tucker #include <net/netevent.h> 10563f3444fSThomas Graf #include <net/rtnetlink.h> 1061da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1071da177e4SLinus Torvalds #include <linux/sysctl.h> 1087426a564SShan Wei #include <linux/kmemleak.h> 1091da177e4SLinus Torvalds #endif 1106e5714eaSDavid S. Miller #include <net/secure_seq.h> 1111da177e4SLinus Torvalds 11268a5e3ddSDavid S. Miller #define RT_FL_TOS(oldflp4) \ 113f61759e6SJulian Anastasov ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK)) 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #define IP_MAX_MTU 0xFFF0 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ) 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static int ip_rt_max_size; 120817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT; 1219f28a2fcSEric Dumazet static int ip_rt_gc_interval __read_mostly = 60 * HZ; 122817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly = HZ / 2; 123817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly = 9; 124817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly = HZ / 50; 125817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1)); 126817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly = HZ; 127817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly = 5 * HZ; 128817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly = 8; 129817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ; 130817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20; 131817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly = 256; 1329f28a2fcSEric Dumazet 1331da177e4SLinus Torvalds /* 1341da177e4SLinus Torvalds * Interface to generic destination cache. 1351da177e4SLinus Torvalds */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie); 1380dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst); 139ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst); 1401da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst); 1411da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb); 1426700c270SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 1436700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 1446700c270SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, 1456700c270SDavid S. Miller struct sk_buff *skb); 146caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst); 1471da177e4SLinus Torvalds 14872cdd1d9SEric Dumazet static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 14972cdd1d9SEric Dumazet int how) 15072cdd1d9SEric Dumazet { 15172cdd1d9SEric Dumazet } 1521da177e4SLinus Torvalds 15362fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old) 15462fa8a84SDavid S. Miller { 15531248731SDavid S. Miller WARN_ON(1); 15631248731SDavid S. Miller return NULL; 15762fa8a84SDavid S. Miller } 15862fa8a84SDavid S. Miller 159f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 160f894cbf8SDavid S. Miller struct sk_buff *skb, 161f894cbf8SDavid S. Miller const void *daddr); 162d3aaeb38SDavid S. Miller 1631da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = { 1641da177e4SLinus Torvalds .family = AF_INET, 16509640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 1661da177e4SLinus Torvalds .check = ipv4_dst_check, 1670dbaee3bSDavid S. Miller .default_advmss = ipv4_default_advmss, 168ebb762f2SSteffen Klassert .mtu = ipv4_mtu, 16962fa8a84SDavid S. Miller .cow_metrics = ipv4_cow_metrics, 170caacf05eSDavid S. Miller .destroy = ipv4_dst_destroy, 1711da177e4SLinus Torvalds .ifdown = ipv4_dst_ifdown, 1721da177e4SLinus Torvalds .negative_advice = ipv4_negative_advice, 1731da177e4SLinus Torvalds .link_failure = ipv4_link_failure, 1741da177e4SLinus Torvalds .update_pmtu = ip_rt_update_pmtu, 175e47a185bSDavid S. Miller .redirect = ip_do_redirect, 1761ac06e03SHerbert Xu .local_out = __ip_local_out, 177d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 1781da177e4SLinus Torvalds }; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds #define ECN_OR_COST(class) TC_PRIO_##class 1811da177e4SLinus Torvalds 1824839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = { 1831da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1844a2b9c37SDan Siemon ECN_OR_COST(BESTEFFORT), 1851da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1861da177e4SLinus Torvalds ECN_OR_COST(BESTEFFORT), 1871da177e4SLinus Torvalds TC_PRIO_BULK, 1881da177e4SLinus Torvalds ECN_OR_COST(BULK), 1891da177e4SLinus Torvalds TC_PRIO_BULK, 1901da177e4SLinus Torvalds ECN_OR_COST(BULK), 1911da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1921da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1931da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1941da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1951da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1961da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK), 1971da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1981da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK) 1991da177e4SLinus Torvalds }; 200d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio); 2011da177e4SLinus Torvalds 2022f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 20327f39c73SEric Dumazet #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field) 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 2061da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 2071da177e4SLinus Torvalds { 20829e75252SEric Dumazet if (*pos) 20989aef892SDavid S. Miller return NULL; 21029e75252SEric Dumazet return SEQ_START_TOKEN; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds ++*pos; 21689aef892SDavid S. Miller return NULL; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2261da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 2271da177e4SLinus Torvalds "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t" 2281da177e4SLinus Torvalds "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t" 2291da177e4SLinus Torvalds "HHUptod\tSpecDst"); 2301da177e4SLinus Torvalds return 0; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 233f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = { 2341da177e4SLinus Torvalds .start = rt_cache_seq_start, 2351da177e4SLinus Torvalds .next = rt_cache_seq_next, 2361da177e4SLinus Torvalds .stop = rt_cache_seq_stop, 2371da177e4SLinus Torvalds .show = rt_cache_seq_show, 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file) 2411da177e4SLinus Torvalds { 24289aef892SDavid S. Miller return seq_open(file, &rt_cache_seq_ops); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2459a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = { 2461da177e4SLinus Torvalds .owner = THIS_MODULE, 2471da177e4SLinus Torvalds .open = rt_cache_seq_open, 2481da177e4SLinus Torvalds .read = seq_read, 2491da177e4SLinus Torvalds .llseek = seq_lseek, 25089aef892SDavid S. Miller .release = seq_release, 2511da177e4SLinus Torvalds }; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds int cpu; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (*pos == 0) 2591da177e4SLinus Torvalds return SEQ_START_TOKEN; 2601da177e4SLinus Torvalds 2610f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 2621da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2631da177e4SLinus Torvalds continue; 2641da177e4SLinus Torvalds *pos = cpu+1; 2652f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds return NULL; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds int cpu; 2731da177e4SLinus Torvalds 2740f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 2751da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2761da177e4SLinus Torvalds continue; 2771da177e4SLinus Torvalds *pos = cpu+1; 2782f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds return NULL; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct rt_cache_stat *st = v; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2945bec0039SOlaf Rempel seq_printf(seq, "entries in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"); 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x " 2991da177e4SLinus Torvalds " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n", 300fc66f95cSEric Dumazet dst_entries_get_slow(&ipv4_dst_ops), 3011da177e4SLinus Torvalds st->in_hit, 3021da177e4SLinus Torvalds st->in_slow_tot, 3031da177e4SLinus Torvalds st->in_slow_mc, 3041da177e4SLinus Torvalds st->in_no_route, 3051da177e4SLinus Torvalds st->in_brd, 3061da177e4SLinus Torvalds st->in_martian_dst, 3071da177e4SLinus Torvalds st->in_martian_src, 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds st->out_hit, 3101da177e4SLinus Torvalds st->out_slow_tot, 3111da177e4SLinus Torvalds st->out_slow_mc, 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds st->gc_total, 3141da177e4SLinus Torvalds st->gc_ignored, 3151da177e4SLinus Torvalds st->gc_goal_miss, 3161da177e4SLinus Torvalds st->gc_dst_overflow, 3171da177e4SLinus Torvalds st->in_hlist_search, 3181da177e4SLinus Torvalds st->out_hlist_search 3191da177e4SLinus Torvalds ); 3201da177e4SLinus Torvalds return 0; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 323f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = { 3241da177e4SLinus Torvalds .start = rt_cpu_seq_start, 3251da177e4SLinus Torvalds .next = rt_cpu_seq_next, 3261da177e4SLinus Torvalds .stop = rt_cpu_seq_stop, 3271da177e4SLinus Torvalds .show = rt_cpu_seq_show, 3281da177e4SLinus Torvalds }; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds return seq_open(file, &rt_cpu_seq_ops); 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3369a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = { 3371da177e4SLinus Torvalds .owner = THIS_MODULE, 3381da177e4SLinus Torvalds .open = rt_cpu_seq_open, 3391da177e4SLinus Torvalds .read = seq_read, 3401da177e4SLinus Torvalds .llseek = seq_lseek, 3411da177e4SLinus Torvalds .release = seq_release, 3421da177e4SLinus Torvalds }; 3431da177e4SLinus Torvalds 344c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 345a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v) 34678c686e9SPavel Emelyanov { 347a661c419SAlexey Dobriyan struct ip_rt_acct *dst, *src; 348a661c419SAlexey Dobriyan unsigned int i, j; 34978c686e9SPavel Emelyanov 350a661c419SAlexey Dobriyan dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL); 351a661c419SAlexey Dobriyan if (!dst) 352a661c419SAlexey Dobriyan return -ENOMEM; 35378c686e9SPavel Emelyanov 354a661c419SAlexey Dobriyan for_each_possible_cpu(i) { 355a661c419SAlexey Dobriyan src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i); 356a661c419SAlexey Dobriyan for (j = 0; j < 256; j++) { 357a661c419SAlexey Dobriyan dst[j].o_bytes += src[j].o_bytes; 358a661c419SAlexey Dobriyan dst[j].o_packets += src[j].o_packets; 359a661c419SAlexey Dobriyan dst[j].i_bytes += src[j].i_bytes; 360a661c419SAlexey Dobriyan dst[j].i_packets += src[j].i_packets; 361a661c419SAlexey Dobriyan } 362a661c419SAlexey Dobriyan } 363a661c419SAlexey Dobriyan 364a661c419SAlexey Dobriyan seq_write(m, dst, 256 * sizeof(struct ip_rt_acct)); 365a661c419SAlexey Dobriyan kfree(dst); 36678c686e9SPavel Emelyanov return 0; 36778c686e9SPavel Emelyanov } 36878c686e9SPavel Emelyanov 369a661c419SAlexey Dobriyan static int rt_acct_proc_open(struct inode *inode, struct file *file) 370a661c419SAlexey Dobriyan { 371a661c419SAlexey Dobriyan return single_open(file, rt_acct_proc_show, NULL); 37278c686e9SPavel Emelyanov } 37378c686e9SPavel Emelyanov 374a661c419SAlexey Dobriyan static const struct file_operations rt_acct_proc_fops = { 375a661c419SAlexey Dobriyan .owner = THIS_MODULE, 376a661c419SAlexey Dobriyan .open = rt_acct_proc_open, 377a661c419SAlexey Dobriyan .read = seq_read, 378a661c419SAlexey Dobriyan .llseek = seq_lseek, 379a661c419SAlexey Dobriyan .release = single_release, 380a661c419SAlexey Dobriyan }; 38178c686e9SPavel Emelyanov #endif 382107f1634SPavel Emelyanov 38373b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net) 384107f1634SPavel Emelyanov { 385107f1634SPavel Emelyanov struct proc_dir_entry *pde; 386107f1634SPavel Emelyanov 387107f1634SPavel Emelyanov pde = proc_net_fops_create(net, "rt_cache", S_IRUGO, 388107f1634SPavel Emelyanov &rt_cache_seq_fops); 389107f1634SPavel Emelyanov if (!pde) 390107f1634SPavel Emelyanov goto err1; 391107f1634SPavel Emelyanov 39277020720SWang Chen pde = proc_create("rt_cache", S_IRUGO, 39377020720SWang Chen net->proc_net_stat, &rt_cpu_seq_fops); 394107f1634SPavel Emelyanov if (!pde) 395107f1634SPavel Emelyanov goto err2; 396107f1634SPavel Emelyanov 397c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 398a661c419SAlexey Dobriyan pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops); 399107f1634SPavel Emelyanov if (!pde) 400107f1634SPavel Emelyanov goto err3; 401107f1634SPavel Emelyanov #endif 402107f1634SPavel Emelyanov return 0; 403107f1634SPavel Emelyanov 404c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 405107f1634SPavel Emelyanov err3: 406107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net_stat); 407107f1634SPavel Emelyanov #endif 408107f1634SPavel Emelyanov err2: 409107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net); 410107f1634SPavel Emelyanov err1: 411107f1634SPavel Emelyanov return -ENOMEM; 412107f1634SPavel Emelyanov } 41373b38711SDenis V. Lunev 41473b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net) 41573b38711SDenis V. Lunev { 41673b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net_stat); 41773b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net); 418c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 41973b38711SDenis V. Lunev remove_proc_entry("rt_acct", net->proc_net); 4200a931acfSAlexey Dobriyan #endif 42173b38711SDenis V. Lunev } 42273b38711SDenis V. Lunev 42373b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata = { 42473b38711SDenis V. Lunev .init = ip_rt_do_proc_init, 42573b38711SDenis V. Lunev .exit = ip_rt_do_proc_exit, 42673b38711SDenis V. Lunev }; 42773b38711SDenis V. Lunev 42873b38711SDenis V. Lunev static int __init ip_rt_proc_init(void) 42973b38711SDenis V. Lunev { 43073b38711SDenis V. Lunev return register_pernet_subsys(&ip_rt_proc_ops); 43173b38711SDenis V. Lunev } 43273b38711SDenis V. Lunev 433107f1634SPavel Emelyanov #else 43473b38711SDenis V. Lunev static inline int ip_rt_proc_init(void) 435107f1634SPavel Emelyanov { 436107f1634SPavel Emelyanov return 0; 437107f1634SPavel Emelyanov } 4381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 4391da177e4SLinus Torvalds 4404331debcSEric Dumazet static inline bool rt_is_expired(const struct rtable *rth) 441e84f84f2SDenis V. Lunev { 442d8d1f30bSChangli Gao return rth->rt_genid != rt_genid(dev_net(rth->dst.dev)); 443e84f84f2SDenis V. Lunev } 444e84f84f2SDenis V. Lunev 4454ccfe6d4SNicolas Dichtel void rt_cache_flush(struct net *net) 44629e75252SEric Dumazet { 447b42664f8SNicolas Dichtel rt_genid_bump(net); 44898376387SEric Dumazet } 44998376387SEric Dumazet 450f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 451f894cbf8SDavid S. Miller struct sk_buff *skb, 452f894cbf8SDavid S. Miller const void *daddr) 4533769cffbSDavid Miller { 454d3aaeb38SDavid S. Miller struct net_device *dev = dst->dev; 455d3aaeb38SDavid S. Miller const __be32 *pkey = daddr; 45639232973SDavid S. Miller const struct rtable *rt; 4573769cffbSDavid Miller struct neighbour *n; 4583769cffbSDavid Miller 45939232973SDavid S. Miller rt = (const struct rtable *) dst; 460a263b309SDavid S. Miller if (rt->rt_gateway) 46139232973SDavid S. Miller pkey = (const __be32 *) &rt->rt_gateway; 462f894cbf8SDavid S. Miller else if (skb) 463f894cbf8SDavid S. Miller pkey = &ip_hdr(skb)->daddr; 464d3aaeb38SDavid S. Miller 46580703d26SDavid S. Miller n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey); 466d3aaeb38SDavid S. Miller if (n) 467d3aaeb38SDavid S. Miller return n; 46832092ecfSDavid Miller return neigh_create(&arp_tbl, pkey, dev); 469d3aaeb38SDavid S. Miller } 470d3aaeb38SDavid S. Miller 4711da177e4SLinus Torvalds /* 4721da177e4SLinus Torvalds * Peer allocation may fail only in serious out-of-memory conditions. However 4731da177e4SLinus Torvalds * we still can generate some output. 4741da177e4SLinus Torvalds * Random ID selection looks a bit dangerous because we have no chances to 4751da177e4SLinus Torvalds * select ID being unique in a reasonable period of time. 4761da177e4SLinus Torvalds * But broken packet identifier may be better than no packet at all. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds static DEFINE_SPINLOCK(ip_fb_id_lock); 4811da177e4SLinus Torvalds static u32 ip_fallback_id; 4821da177e4SLinus Torvalds u32 salt; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds spin_lock_bh(&ip_fb_id_lock); 485e448515cSAl Viro salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr); 4861da177e4SLinus Torvalds iph->id = htons(salt & 0xFFFF); 4871da177e4SLinus Torvalds ip_fallback_id = salt; 4881da177e4SLinus Torvalds spin_unlock_bh(&ip_fb_id_lock); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more) 4921da177e4SLinus Torvalds { 4931d861aa4SDavid S. Miller struct net *net = dev_net(dst->dev); 4941d861aa4SDavid S. Miller struct inet_peer *peer; 4951da177e4SLinus Torvalds 4961d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, iph->daddr, 1); 497fbfe95a4SDavid S. Miller if (peer) { 498fbfe95a4SDavid S. Miller iph->id = htons(inet_getid(peer, more)); 4991d861aa4SDavid S. Miller inet_putpeer(peer); 5001da177e4SLinus Torvalds return; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds ip_select_fb_ident(iph); 5041da177e4SLinus Torvalds } 5054bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident); 5061da177e4SLinus Torvalds 5075abf7f7eSEric Dumazet static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk, 5084895c771SDavid S. Miller const struct iphdr *iph, 5094895c771SDavid S. Miller int oif, u8 tos, 5104895c771SDavid S. Miller u8 prot, u32 mark, int flow_flags) 5114895c771SDavid S. Miller { 5124895c771SDavid S. Miller if (sk) { 5134895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk); 5144895c771SDavid S. Miller 5154895c771SDavid S. Miller oif = sk->sk_bound_dev_if; 5164895c771SDavid S. Miller mark = sk->sk_mark; 5174895c771SDavid S. Miller tos = RT_CONN_FLAGS(sk); 5184895c771SDavid S. Miller prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol; 5194895c771SDavid S. Miller } 5204895c771SDavid S. Miller flowi4_init_output(fl4, oif, mark, tos, 5214895c771SDavid S. Miller RT_SCOPE_UNIVERSE, prot, 5224895c771SDavid S. Miller flow_flags, 5234895c771SDavid S. Miller iph->daddr, iph->saddr, 0, 0); 5244895c771SDavid S. Miller } 5254895c771SDavid S. Miller 5265abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb, 5275abf7f7eSEric Dumazet const struct sock *sk) 5284895c771SDavid S. Miller { 5294895c771SDavid S. Miller const struct iphdr *iph = ip_hdr(skb); 5304895c771SDavid S. Miller int oif = skb->dev->ifindex; 5314895c771SDavid S. Miller u8 tos = RT_TOS(iph->tos); 5324895c771SDavid S. Miller u8 prot = iph->protocol; 5334895c771SDavid S. Miller u32 mark = skb->mark; 5344895c771SDavid S. Miller 5354895c771SDavid S. Miller __build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0); 5364895c771SDavid S. Miller } 5374895c771SDavid S. Miller 5385abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk) 5394895c771SDavid S. Miller { 5404895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk); 5415abf7f7eSEric Dumazet const struct ip_options_rcu *inet_opt; 5424895c771SDavid S. Miller __be32 daddr = inet->inet_daddr; 5434895c771SDavid S. Miller 5444895c771SDavid S. Miller rcu_read_lock(); 5454895c771SDavid S. Miller inet_opt = rcu_dereference(inet->inet_opt); 5464895c771SDavid S. Miller if (inet_opt && inet_opt->opt.srr) 5474895c771SDavid S. Miller daddr = inet_opt->opt.faddr; 5484895c771SDavid S. Miller flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, 5494895c771SDavid S. Miller RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 5504895c771SDavid S. Miller inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol, 5514895c771SDavid S. Miller inet_sk_flowi_flags(sk), 5524895c771SDavid S. Miller daddr, inet->inet_saddr, 0, 0); 5534895c771SDavid S. Miller rcu_read_unlock(); 5544895c771SDavid S. Miller } 5554895c771SDavid S. Miller 5565abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk, 5575abf7f7eSEric Dumazet const struct sk_buff *skb) 5584895c771SDavid S. Miller { 5594895c771SDavid S. Miller if (skb) 5604895c771SDavid S. Miller build_skb_flow_key(fl4, skb, sk); 5614895c771SDavid S. Miller else 5624895c771SDavid S. Miller build_sk_flow_key(fl4, sk); 5634895c771SDavid S. Miller } 5644895c771SDavid S. Miller 565c5038a83SDavid S. Miller static inline void rt_free(struct rtable *rt) 566c5038a83SDavid S. Miller { 567c5038a83SDavid S. Miller call_rcu(&rt->dst.rcu_head, dst_rcu_free); 568c5038a83SDavid S. Miller } 569c5038a83SDavid S. Miller 570c5038a83SDavid S. Miller static DEFINE_SPINLOCK(fnhe_lock); 5714895c771SDavid S. Miller 572aee06da6SJulian Anastasov static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash) 5734895c771SDavid S. Miller { 5744895c771SDavid S. Miller struct fib_nh_exception *fnhe, *oldest; 575c5038a83SDavid S. Miller struct rtable *orig; 5764895c771SDavid S. Miller 5774895c771SDavid S. Miller oldest = rcu_dereference(hash->chain); 5784895c771SDavid S. Miller for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe; 5794895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 5804895c771SDavid S. Miller if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) 5814895c771SDavid S. Miller oldest = fnhe; 5824895c771SDavid S. Miller } 583c5038a83SDavid S. Miller orig = rcu_dereference(oldest->fnhe_rth); 584c5038a83SDavid S. Miller if (orig) { 585c5038a83SDavid S. Miller RCU_INIT_POINTER(oldest->fnhe_rth, NULL); 586c5038a83SDavid S. Miller rt_free(orig); 587c5038a83SDavid S. Miller } 5884895c771SDavid S. Miller return oldest; 5894895c771SDavid S. Miller } 5904895c771SDavid S. Miller 591d3a25c98SDavid S. Miller static inline u32 fnhe_hashfun(__be32 daddr) 592d3a25c98SDavid S. Miller { 593d3a25c98SDavid S. Miller u32 hval; 594d3a25c98SDavid S. Miller 595d3a25c98SDavid S. Miller hval = (__force u32) daddr; 596d3a25c98SDavid S. Miller hval ^= (hval >> 11) ^ (hval >> 22); 597d3a25c98SDavid S. Miller 598d3a25c98SDavid S. Miller return hval & (FNHE_HASH_SIZE - 1); 599d3a25c98SDavid S. Miller } 600d3a25c98SDavid S. Miller 601aee06da6SJulian Anastasov static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw, 602aee06da6SJulian Anastasov u32 pmtu, unsigned long expires) 6034895c771SDavid S. Miller { 604aee06da6SJulian Anastasov struct fnhe_hash_bucket *hash; 6054895c771SDavid S. Miller struct fib_nh_exception *fnhe; 6064895c771SDavid S. Miller int depth; 607aee06da6SJulian Anastasov u32 hval = fnhe_hashfun(daddr); 6084895c771SDavid S. Miller 609c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock); 610aee06da6SJulian Anastasov 611aee06da6SJulian Anastasov hash = nh->nh_exceptions; 6124895c771SDavid S. Miller if (!hash) { 613aee06da6SJulian Anastasov hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC); 6144895c771SDavid S. Miller if (!hash) 615aee06da6SJulian Anastasov goto out_unlock; 616aee06da6SJulian Anastasov nh->nh_exceptions = hash; 6174895c771SDavid S. Miller } 6184895c771SDavid S. Miller 6194895c771SDavid S. Miller hash += hval; 6204895c771SDavid S. Miller 6214895c771SDavid S. Miller depth = 0; 6224895c771SDavid S. Miller for (fnhe = rcu_dereference(hash->chain); fnhe; 6234895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 6244895c771SDavid S. Miller if (fnhe->fnhe_daddr == daddr) 625aee06da6SJulian Anastasov break; 6264895c771SDavid S. Miller depth++; 6274895c771SDavid S. Miller } 6284895c771SDavid S. Miller 629aee06da6SJulian Anastasov if (fnhe) { 630aee06da6SJulian Anastasov if (gw) 631aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 632aee06da6SJulian Anastasov if (pmtu) { 633aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 634aee06da6SJulian Anastasov fnhe->fnhe_expires = expires; 6354895c771SDavid S. Miller } 636aee06da6SJulian Anastasov } else { 637aee06da6SJulian Anastasov if (depth > FNHE_RECLAIM_DEPTH) 638aee06da6SJulian Anastasov fnhe = fnhe_oldest(hash); 639aee06da6SJulian Anastasov else { 6404895c771SDavid S. Miller fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); 6414895c771SDavid S. Miller if (!fnhe) 642aee06da6SJulian Anastasov goto out_unlock; 6434895c771SDavid S. Miller 6444895c771SDavid S. Miller fnhe->fnhe_next = hash->chain; 6454895c771SDavid S. Miller rcu_assign_pointer(hash->chain, fnhe); 646aee06da6SJulian Anastasov } 6474895c771SDavid S. Miller fnhe->fnhe_daddr = daddr; 648aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 649aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 650aee06da6SJulian Anastasov fnhe->fnhe_expires = expires; 651aee06da6SJulian Anastasov } 652aee06da6SJulian Anastasov 6534895c771SDavid S. Miller fnhe->fnhe_stamp = jiffies; 654aee06da6SJulian Anastasov 655aee06da6SJulian Anastasov out_unlock: 656c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock); 657aee06da6SJulian Anastasov return; 6584895c771SDavid S. Miller } 6594895c771SDavid S. Miller 660ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4, 661ceb33206SDavid S. Miller bool kill_route) 6621da177e4SLinus Torvalds { 663e47a185bSDavid S. Miller __be32 new_gw = icmp_hdr(skb)->un.gateway; 66494206125SDavid S. Miller __be32 old_gw = ip_hdr(skb)->saddr; 665e47a185bSDavid S. Miller struct net_device *dev = skb->dev; 666e47a185bSDavid S. Miller struct in_device *in_dev; 6674895c771SDavid S. Miller struct fib_result res; 668e47a185bSDavid S. Miller struct neighbour *n; 669317805b8SDenis V. Lunev struct net *net; 6701da177e4SLinus Torvalds 67194206125SDavid S. Miller switch (icmp_hdr(skb)->code & 7) { 67294206125SDavid S. Miller case ICMP_REDIR_NET: 67394206125SDavid S. Miller case ICMP_REDIR_NETTOS: 67494206125SDavid S. Miller case ICMP_REDIR_HOST: 67594206125SDavid S. Miller case ICMP_REDIR_HOSTTOS: 67694206125SDavid S. Miller break; 67794206125SDavid S. Miller 67894206125SDavid S. Miller default: 67994206125SDavid S. Miller return; 68094206125SDavid S. Miller } 68194206125SDavid S. Miller 682e47a185bSDavid S. Miller if (rt->rt_gateway != old_gw) 683e47a185bSDavid S. Miller return; 684e47a185bSDavid S. Miller 685e47a185bSDavid S. Miller in_dev = __in_dev_get_rcu(dev); 686e47a185bSDavid S. Miller if (!in_dev) 687e47a185bSDavid S. Miller return; 688e47a185bSDavid S. Miller 689c346dca1SYOSHIFUJI Hideaki net = dev_net(dev); 6909d4fb27dSJoe Perches if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) || 6919d4fb27dSJoe Perches ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) || 6929d4fb27dSJoe Perches ipv4_is_zeronet(new_gw)) 6931da177e4SLinus Torvalds goto reject_redirect; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (!IN_DEV_SHARED_MEDIA(in_dev)) { 6961da177e4SLinus Torvalds if (!inet_addr_onlink(in_dev, new_gw, old_gw)) 6971da177e4SLinus Torvalds goto reject_redirect; 6981da177e4SLinus Torvalds if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev)) 6991da177e4SLinus Torvalds goto reject_redirect; 7001da177e4SLinus Torvalds } else { 701317805b8SDenis V. Lunev if (inet_addr_type(net, new_gw) != RTN_UNICAST) 7021da177e4SLinus Torvalds goto reject_redirect; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7054895c771SDavid S. Miller n = ipv4_neigh_lookup(&rt->dst, NULL, &new_gw); 706e47a185bSDavid S. Miller if (n) { 707e47a185bSDavid S. Miller if (!(n->nud_state & NUD_VALID)) { 708e47a185bSDavid S. Miller neigh_event_send(n, NULL); 709e47a185bSDavid S. Miller } else { 7104895c771SDavid S. Miller if (fib_lookup(net, fl4, &res) == 0) { 7114895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(res); 7124895c771SDavid S. Miller 713aee06da6SJulian Anastasov update_or_create_fnhe(nh, fl4->daddr, new_gw, 714aee06da6SJulian Anastasov 0, 0); 7154895c771SDavid S. Miller } 716ceb33206SDavid S. Miller if (kill_route) 717ceb33206SDavid S. Miller rt->dst.obsolete = DST_OBSOLETE_KILL; 718e47a185bSDavid S. Miller call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n); 719e47a185bSDavid S. Miller } 720e47a185bSDavid S. Miller neigh_release(n); 721e47a185bSDavid S. Miller } 722e47a185bSDavid S. Miller return; 723e47a185bSDavid S. Miller 724e47a185bSDavid S. Miller reject_redirect: 725e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE 72699ee038dSDavid S. Miller if (IN_DEV_LOG_MARTIANS(in_dev)) { 72799ee038dSDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 72899ee038dSDavid S. Miller __be32 daddr = iph->daddr; 72999ee038dSDavid S. Miller __be32 saddr = iph->saddr; 73099ee038dSDavid S. Miller 731e47a185bSDavid S. Miller net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n" 732e47a185bSDavid S. Miller " Advised path = %pI4 -> %pI4\n", 733e47a185bSDavid S. Miller &old_gw, dev->name, &new_gw, 734e47a185bSDavid S. Miller &saddr, &daddr); 73599ee038dSDavid S. Miller } 736e47a185bSDavid S. Miller #endif 737e47a185bSDavid S. Miller ; 738e47a185bSDavid S. Miller } 739e47a185bSDavid S. Miller 7404895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 7414895c771SDavid S. Miller { 7424895c771SDavid S. Miller struct rtable *rt; 7434895c771SDavid S. Miller struct flowi4 fl4; 7444895c771SDavid S. Miller 7454895c771SDavid S. Miller rt = (struct rtable *) dst; 7464895c771SDavid S. Miller 7474895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb); 748ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, true); 7494895c771SDavid S. Miller } 7504895c771SDavid S. Miller 7511da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 7541da177e4SLinus Torvalds struct dst_entry *ret = dst; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds if (rt) { 757d11a4dc1STimo Teräs if (dst->obsolete > 0) { 7581da177e4SLinus Torvalds ip_rt_put(rt); 7591da177e4SLinus Torvalds ret = NULL; 7605943634fSDavid S. Miller } else if ((rt->rt_flags & RTCF_REDIRECTED) || 7615943634fSDavid S. Miller rt->dst.expires) { 76289aef892SDavid S. Miller ip_rt_put(rt); 7631da177e4SLinus Torvalds ret = NULL; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds return ret; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * Algorithm: 7711da177e4SLinus Torvalds * 1. The first ip_rt_redirect_number redirects are sent 7721da177e4SLinus Torvalds * with exponential backoff, then we stop sending them at all, 7731da177e4SLinus Torvalds * assuming that the host ignores our redirects. 7741da177e4SLinus Torvalds * 2. If we did not see packets requiring redirects 7751da177e4SLinus Torvalds * during ip_rt_redirect_silence, we assume that the host 7761da177e4SLinus Torvalds * forgot redirected route and start to send redirects again. 7771da177e4SLinus Torvalds * 7781da177e4SLinus Torvalds * This algorithm is much cheaper and more intelligent than dumb load limiting 7791da177e4SLinus Torvalds * in icmp.c. 7801da177e4SLinus Torvalds * 7811da177e4SLinus Torvalds * NOTE. Do not forget to inhibit load limiting for redirects (redundant) 7821da177e4SLinus Torvalds * and "frag. need" (breaks PMTU discovery) in icmp.c. 7831da177e4SLinus Torvalds */ 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb) 7861da177e4SLinus Torvalds { 787511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 78830038fc6SEric Dumazet struct in_device *in_dev; 78992d86829SDavid S. Miller struct inet_peer *peer; 7901d861aa4SDavid S. Miller struct net *net; 79130038fc6SEric Dumazet int log_martians; 7921da177e4SLinus Torvalds 79330038fc6SEric Dumazet rcu_read_lock(); 794d8d1f30bSChangli Gao in_dev = __in_dev_get_rcu(rt->dst.dev); 79530038fc6SEric Dumazet if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) { 79630038fc6SEric Dumazet rcu_read_unlock(); 7971da177e4SLinus Torvalds return; 79830038fc6SEric Dumazet } 79930038fc6SEric Dumazet log_martians = IN_DEV_LOG_MARTIANS(in_dev); 80030038fc6SEric Dumazet rcu_read_unlock(); 8011da177e4SLinus Torvalds 8021d861aa4SDavid S. Miller net = dev_net(rt->dst.dev); 8031d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1); 80492d86829SDavid S. Miller if (!peer) { 805e81da0e1SJulian Anastasov icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, 806e81da0e1SJulian Anastasov rt_nexthop(rt, ip_hdr(skb)->daddr)); 80792d86829SDavid S. Miller return; 80892d86829SDavid S. Miller } 80992d86829SDavid S. Miller 8101da177e4SLinus Torvalds /* No redirected packets during ip_rt_redirect_silence; 8111da177e4SLinus Torvalds * reset the algorithm. 8121da177e4SLinus Torvalds */ 81392d86829SDavid S. Miller if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) 81492d86829SDavid S. Miller peer->rate_tokens = 0; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds /* Too many ignored redirects; do not send anything 817d8d1f30bSChangli Gao * set dst.rate_last to the last seen redirected packet. 8181da177e4SLinus Torvalds */ 81992d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_redirect_number) { 82092d86829SDavid S. Miller peer->rate_last = jiffies; 8211d861aa4SDavid S. Miller goto out_put_peer; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* Check for load limit; set rate_last to the latest sent 8251da177e4SLinus Torvalds * redirect. 8261da177e4SLinus Torvalds */ 82792d86829SDavid S. Miller if (peer->rate_tokens == 0 || 82814fb8a76SLi Yewang time_after(jiffies, 82992d86829SDavid S. Miller (peer->rate_last + 83092d86829SDavid S. Miller (ip_rt_redirect_load << peer->rate_tokens)))) { 831e81da0e1SJulian Anastasov __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr); 832e81da0e1SJulian Anastasov 833e81da0e1SJulian Anastasov icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw); 83492d86829SDavid S. Miller peer->rate_last = jiffies; 83592d86829SDavid S. Miller ++peer->rate_tokens; 8361da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 83730038fc6SEric Dumazet if (log_martians && 838e87cc472SJoe Perches peer->rate_tokens == ip_rt_redirect_number) 839e87cc472SJoe Perches net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n", 84092101b3bSDavid S. Miller &ip_hdr(skb)->saddr, inet_iif(skb), 841e81da0e1SJulian Anastasov &ip_hdr(skb)->daddr, &gw); 8421da177e4SLinus Torvalds #endif 8431da177e4SLinus Torvalds } 8441d861aa4SDavid S. Miller out_put_peer: 8451d861aa4SDavid S. Miller inet_putpeer(peer); 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb) 8491da177e4SLinus Torvalds { 850251da413SDavid S. Miller struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 851511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 85292d86829SDavid S. Miller struct inet_peer *peer; 8531da177e4SLinus Torvalds unsigned long now; 854251da413SDavid S. Miller struct net *net; 85592d86829SDavid S. Miller bool send; 8561da177e4SLinus Torvalds int code; 8571da177e4SLinus Torvalds 858251da413SDavid S. Miller net = dev_net(rt->dst.dev); 859251da413SDavid S. Miller if (!IN_DEV_FORWARD(in_dev)) { 860251da413SDavid S. Miller switch (rt->dst.error) { 861251da413SDavid S. Miller case EHOSTUNREACH: 862251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS); 863251da413SDavid S. Miller break; 864251da413SDavid S. Miller 865251da413SDavid S. Miller case ENETUNREACH: 866251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES); 867251da413SDavid S. Miller break; 868251da413SDavid S. Miller } 869251da413SDavid S. Miller goto out; 870251da413SDavid S. Miller } 871251da413SDavid S. Miller 872d8d1f30bSChangli Gao switch (rt->dst.error) { 8731da177e4SLinus Torvalds case EINVAL: 8741da177e4SLinus Torvalds default: 8751da177e4SLinus Torvalds goto out; 8761da177e4SLinus Torvalds case EHOSTUNREACH: 8771da177e4SLinus Torvalds code = ICMP_HOST_UNREACH; 8781da177e4SLinus Torvalds break; 8791da177e4SLinus Torvalds case ENETUNREACH: 8801da177e4SLinus Torvalds code = ICMP_NET_UNREACH; 881251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES); 8821da177e4SLinus Torvalds break; 8831da177e4SLinus Torvalds case EACCES: 8841da177e4SLinus Torvalds code = ICMP_PKT_FILTERED; 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 8881d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1); 88992d86829SDavid S. Miller 89092d86829SDavid S. Miller send = true; 89192d86829SDavid S. Miller if (peer) { 8921da177e4SLinus Torvalds now = jiffies; 89392d86829SDavid S. Miller peer->rate_tokens += now - peer->rate_last; 89492d86829SDavid S. Miller if (peer->rate_tokens > ip_rt_error_burst) 89592d86829SDavid S. Miller peer->rate_tokens = ip_rt_error_burst; 89692d86829SDavid S. Miller peer->rate_last = now; 89792d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_error_cost) 89892d86829SDavid S. Miller peer->rate_tokens -= ip_rt_error_cost; 89992d86829SDavid S. Miller else 90092d86829SDavid S. Miller send = false; 9011d861aa4SDavid S. Miller inet_putpeer(peer); 9021da177e4SLinus Torvalds } 90392d86829SDavid S. Miller if (send) 90492d86829SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, code, 0); 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds out: kfree_skb(skb); 9071da177e4SLinus Torvalds return 0; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 910d851c12bSSteffen Klassert static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu) 9111da177e4SLinus Torvalds { 912d851c12bSSteffen Klassert struct dst_entry *dst = &rt->dst; 9134895c771SDavid S. Miller struct fib_result res; 9142c8cec5cSDavid S. Miller 9157f92d334SSteffen Klassert if (dst->dev->mtu < mtu) 9167f92d334SSteffen Klassert return; 9177f92d334SSteffen Klassert 9182c8cec5cSDavid S. Miller if (mtu < ip_rt_min_pmtu) 9191da177e4SLinus Torvalds mtu = ip_rt_min_pmtu; 92046af3180SHiroaki SHIMODA 921d851c12bSSteffen Klassert if (!rt->rt_pmtu) { 922d851c12bSSteffen Klassert dst->obsolete = DST_OBSOLETE_KILL; 923d851c12bSSteffen Klassert } else { 924d851c12bSSteffen Klassert rt->rt_pmtu = mtu; 925d851c12bSSteffen Klassert dst->expires = max(1UL, jiffies + ip_rt_mtu_expires); 926d851c12bSSteffen Klassert } 927d851c12bSSteffen Klassert 928c5ae7d41SEric Dumazet rcu_read_lock(); 929d851c12bSSteffen Klassert if (fib_lookup(dev_net(dst->dev), fl4, &res) == 0) { 9304895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(res); 9314895c771SDavid S. Miller 932aee06da6SJulian Anastasov update_or_create_fnhe(nh, fl4->daddr, 0, mtu, 933aee06da6SJulian Anastasov jiffies + ip_rt_mtu_expires); 9344895c771SDavid S. Miller } 935c5ae7d41SEric Dumazet rcu_read_unlock(); 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9384895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 9394895c771SDavid S. Miller struct sk_buff *skb, u32 mtu) 9404895c771SDavid S. Miller { 9414895c771SDavid S. Miller struct rtable *rt = (struct rtable *) dst; 9424895c771SDavid S. Miller struct flowi4 fl4; 9434895c771SDavid S. Miller 9444895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb); 945d851c12bSSteffen Klassert __ip_rt_update_pmtu(rt, &fl4, mtu); 9464895c771SDavid S. Miller } 9474895c771SDavid S. Miller 94836393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, 94936393395SDavid S. Miller int oif, u32 mark, u8 protocol, int flow_flags) 95036393395SDavid S. Miller { 95136393395SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 95236393395SDavid S. Miller struct flowi4 fl4; 95336393395SDavid S. Miller struct rtable *rt; 95436393395SDavid S. Miller 9554895c771SDavid S. Miller __build_flow_key(&fl4, NULL, iph, oif, 9564895c771SDavid S. Miller RT_TOS(iph->tos), protocol, mark, flow_flags); 95736393395SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 95836393395SDavid S. Miller if (!IS_ERR(rt)) { 9594895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 96036393395SDavid S. Miller ip_rt_put(rt); 96136393395SDavid S. Miller } 96236393395SDavid S. Miller } 96336393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu); 96436393395SDavid S. Miller 96536393395SDavid S. Miller void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 96636393395SDavid S. Miller { 9674895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 9684895c771SDavid S. Miller struct flowi4 fl4; 9694895c771SDavid S. Miller struct rtable *rt; 97036393395SDavid S. Miller 9714895c771SDavid S. Miller __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0); 9724895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4); 9734895c771SDavid S. Miller if (!IS_ERR(rt)) { 9744895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 9754895c771SDavid S. Miller ip_rt_put(rt); 9764895c771SDavid S. Miller } 97736393395SDavid S. Miller } 97836393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu); 979f39925dbSDavid S. Miller 980b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net, 981b42597e2SDavid S. Miller int oif, u32 mark, u8 protocol, int flow_flags) 982b42597e2SDavid S. Miller { 983b42597e2SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 984b42597e2SDavid S. Miller struct flowi4 fl4; 985b42597e2SDavid S. Miller struct rtable *rt; 986b42597e2SDavid S. Miller 9874895c771SDavid S. Miller __build_flow_key(&fl4, NULL, iph, oif, 9884895c771SDavid S. Miller RT_TOS(iph->tos), protocol, mark, flow_flags); 989b42597e2SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 990b42597e2SDavid S. Miller if (!IS_ERR(rt)) { 991ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 992b42597e2SDavid S. Miller ip_rt_put(rt); 993b42597e2SDavid S. Miller } 994b42597e2SDavid S. Miller } 995b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect); 996b42597e2SDavid S. Miller 997b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk) 998b42597e2SDavid S. Miller { 9994895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 10004895c771SDavid S. Miller struct flowi4 fl4; 10014895c771SDavid S. Miller struct rtable *rt; 1002b42597e2SDavid S. Miller 10034895c771SDavid S. Miller __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0); 10044895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4); 10054895c771SDavid S. Miller if (!IS_ERR(rt)) { 1006ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 10074895c771SDavid S. Miller ip_rt_put(rt); 10084895c771SDavid S. Miller } 1009b42597e2SDavid S. Miller } 1010b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect); 1011b42597e2SDavid S. Miller 1012efbc368dSDavid S. Miller static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie) 1013efbc368dSDavid S. Miller { 1014efbc368dSDavid S. Miller struct rtable *rt = (struct rtable *) dst; 1015efbc368dSDavid S. Miller 1016ceb33206SDavid S. Miller /* All IPV4 dsts are created with ->obsolete set to the value 1017ceb33206SDavid S. Miller * DST_OBSOLETE_FORCE_CHK which forces validation calls down 1018ceb33206SDavid S. Miller * into this function always. 1019ceb33206SDavid S. Miller * 1020ceb33206SDavid S. Miller * When a PMTU/redirect information update invalidates a 1021ceb33206SDavid S. Miller * route, this is indicated by setting obsolete to 1022ceb33206SDavid S. Miller * DST_OBSOLETE_KILL. 1023ceb33206SDavid S. Miller */ 1024ceb33206SDavid S. Miller if (dst->obsolete == DST_OBSOLETE_KILL || rt_is_expired(rt)) 1025efbc368dSDavid S. Miller return NULL; 1026d11a4dc1STimo Teräs return dst; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds struct rtable *rt; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 10341da177e4SLinus Torvalds 1035511c3f92SEric Dumazet rt = skb_rtable(skb); 10365943634fSDavid S. Miller if (rt) 10375943634fSDavid S. Miller dst_set_expires(&rt->dst, 0); 10382c8cec5cSDavid S. Miller } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb) 10411da177e4SLinus Torvalds { 104291df42beSJoe Perches pr_debug("%s: %pI4 -> %pI4, %s\n", 104391df42beSJoe Perches __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, 10441da177e4SLinus Torvalds skb->dev ? skb->dev->name : "?"); 10451da177e4SLinus Torvalds kfree_skb(skb); 1046c378a9c0SDave Jones WARN_ON(1); 10471da177e4SLinus Torvalds return 0; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds /* 10511da177e4SLinus Torvalds We do not cache source address of outgoing interface, 10521da177e4SLinus Torvalds because it is used only by IP RR, TS and SRR options, 10531da177e4SLinus Torvalds so that it out of fast path. 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds BTW remember: "addr" is allowed to be not aligned 10561da177e4SLinus Torvalds in IP options! 10571da177e4SLinus Torvalds */ 10581da177e4SLinus Torvalds 10598e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt) 10601da177e4SLinus Torvalds { 1061a61ced5dSAl Viro __be32 src; 10621da177e4SLinus Torvalds 1063c7537967SDavid S. Miller if (rt_is_output_route(rt)) 1064c5be24ffSDavid S. Miller src = ip_hdr(skb)->saddr; 1065ebc0ffaeSEric Dumazet else { 10668e36360aSDavid S. Miller struct fib_result res; 10678e36360aSDavid S. Miller struct flowi4 fl4; 10688e36360aSDavid S. Miller struct iphdr *iph; 10698e36360aSDavid S. Miller 10708e36360aSDavid S. Miller iph = ip_hdr(skb); 10718e36360aSDavid S. Miller 10728e36360aSDavid S. Miller memset(&fl4, 0, sizeof(fl4)); 10738e36360aSDavid S. Miller fl4.daddr = iph->daddr; 10748e36360aSDavid S. Miller fl4.saddr = iph->saddr; 1075b0fe4a31SJulian Anastasov fl4.flowi4_tos = RT_TOS(iph->tos); 10768e36360aSDavid S. Miller fl4.flowi4_oif = rt->dst.dev->ifindex; 10778e36360aSDavid S. Miller fl4.flowi4_iif = skb->dev->ifindex; 10788e36360aSDavid S. Miller fl4.flowi4_mark = skb->mark; 10795e2b61f7SDavid S. Miller 1080ebc0ffaeSEric Dumazet rcu_read_lock(); 108168a5e3ddSDavid S. Miller if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res) == 0) 1082436c3b66SDavid S. Miller src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res); 1083ebc0ffaeSEric Dumazet else 1084f8126f1dSDavid S. Miller src = inet_select_addr(rt->dst.dev, 1085f8126f1dSDavid S. Miller rt_nexthop(rt, iph->daddr), 10861da177e4SLinus Torvalds RT_SCOPE_UNIVERSE); 1087ebc0ffaeSEric Dumazet rcu_read_unlock(); 1088ebc0ffaeSEric Dumazet } 10891da177e4SLinus Torvalds memcpy(addr, &src, 4); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 1092c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 10931da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag) 10941da177e4SLinus Torvalds { 1095d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF)) 1096d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF; 1097d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF0000)) 1098d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF0000; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds #endif 11011da177e4SLinus Torvalds 11020dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst) 11030dbaee3bSDavid S. Miller { 11040dbaee3bSDavid S. Miller unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS); 11050dbaee3bSDavid S. Miller 11060dbaee3bSDavid S. Miller if (advmss == 0) { 11070dbaee3bSDavid S. Miller advmss = max_t(unsigned int, dst->dev->mtu - 40, 11080dbaee3bSDavid S. Miller ip_rt_min_advmss); 11090dbaee3bSDavid S. Miller if (advmss > 65535 - 40) 11100dbaee3bSDavid S. Miller advmss = 65535 - 40; 11110dbaee3bSDavid S. Miller } 11120dbaee3bSDavid S. Miller return advmss; 11130dbaee3bSDavid S. Miller } 11140dbaee3bSDavid S. Miller 1115ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst) 1116d33e4553SDavid S. Miller { 1117261663b0SSteffen Klassert const struct rtable *rt = (const struct rtable *) dst; 11185943634fSDavid S. Miller unsigned int mtu = rt->rt_pmtu; 11195943634fSDavid S. Miller 112098d75c37SAlexander Duyck if (!mtu || time_after_eq(jiffies, rt->dst.expires)) 11215943634fSDavid S. Miller mtu = dst_metric_raw(dst, RTAX_MTU); 1122618f9bc7SSteffen Klassert 1123261663b0SSteffen Klassert if (mtu && rt_is_output_route(rt)) 1124618f9bc7SSteffen Klassert return mtu; 1125618f9bc7SSteffen Klassert 1126618f9bc7SSteffen Klassert mtu = dst->dev->mtu; 1127d33e4553SDavid S. Miller 1128d33e4553SDavid S. Miller if (unlikely(dst_metric_locked(dst, RTAX_MTU))) { 1129155e8336SJulian Anastasov if (rt->rt_uses_gateway && mtu > 576) 1130d33e4553SDavid S. Miller mtu = 576; 1131d33e4553SDavid S. Miller } 1132d33e4553SDavid S. Miller 1133d33e4553SDavid S. Miller if (mtu > IP_MAX_MTU) 1134d33e4553SDavid S. Miller mtu = IP_MAX_MTU; 1135d33e4553SDavid S. Miller 1136d33e4553SDavid S. Miller return mtu; 1137d33e4553SDavid S. Miller } 1138d33e4553SDavid S. Miller 1139f2bb4bedSDavid S. Miller static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr) 11404895c771SDavid S. Miller { 11414895c771SDavid S. Miller struct fnhe_hash_bucket *hash = nh->nh_exceptions; 11424895c771SDavid S. Miller struct fib_nh_exception *fnhe; 11434895c771SDavid S. Miller u32 hval; 11444895c771SDavid S. Miller 1145f2bb4bedSDavid S. Miller if (!hash) 1146f2bb4bedSDavid S. Miller return NULL; 1147f2bb4bedSDavid S. Miller 1148d3a25c98SDavid S. Miller hval = fnhe_hashfun(daddr); 11494895c771SDavid S. Miller 11504895c771SDavid S. Miller for (fnhe = rcu_dereference(hash[hval].chain); fnhe; 11514895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 1152f2bb4bedSDavid S. Miller if (fnhe->fnhe_daddr == daddr) 1153f2bb4bedSDavid S. Miller return fnhe; 1154f2bb4bedSDavid S. Miller } 1155f2bb4bedSDavid S. Miller return NULL; 1156f2bb4bedSDavid S. Miller } 1157f2bb4bedSDavid S. Miller 1158caacf05eSDavid S. Miller static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe, 1159f2bb4bedSDavid S. Miller __be32 daddr) 1160f2bb4bedSDavid S. Miller { 1161caacf05eSDavid S. Miller bool ret = false; 1162caacf05eSDavid S. Miller 1163c5038a83SDavid S. Miller spin_lock_bh(&fnhe_lock); 1164aee06da6SJulian Anastasov 1165c5038a83SDavid S. Miller if (daddr == fnhe->fnhe_daddr) { 116613d82bf5SSteffen Klassert struct rtable *orig = rcu_dereference(fnhe->fnhe_rth); 116713d82bf5SSteffen Klassert if (orig && rt_is_expired(orig)) { 116813d82bf5SSteffen Klassert fnhe->fnhe_gw = 0; 116913d82bf5SSteffen Klassert fnhe->fnhe_pmtu = 0; 117013d82bf5SSteffen Klassert fnhe->fnhe_expires = 0; 117113d82bf5SSteffen Klassert } 1172c5038a83SDavid S. Miller if (fnhe->fnhe_pmtu) { 1173c5038a83SDavid S. Miller unsigned long expires = fnhe->fnhe_expires; 1174f31fd383SJulian Anastasov unsigned long diff = expires - jiffies; 11754895c771SDavid S. Miller 11764895c771SDavid S. Miller if (time_before(jiffies, expires)) { 1177c5038a83SDavid S. Miller rt->rt_pmtu = fnhe->fnhe_pmtu; 11784895c771SDavid S. Miller dst_set_expires(&rt->dst, diff); 11794895c771SDavid S. Miller } 11804895c771SDavid S. Miller } 1181c5038a83SDavid S. Miller if (fnhe->fnhe_gw) { 1182ceb33206SDavid S. Miller rt->rt_flags |= RTCF_REDIRECTED; 1183c5038a83SDavid S. Miller rt->rt_gateway = fnhe->fnhe_gw; 1184155e8336SJulian Anastasov rt->rt_uses_gateway = 1; 1185155e8336SJulian Anastasov } else if (!rt->rt_gateway) 1186155e8336SJulian Anastasov rt->rt_gateway = daddr; 1187f2bb4bedSDavid S. Miller 1188c5038a83SDavid S. Miller rcu_assign_pointer(fnhe->fnhe_rth, rt); 1189c5038a83SDavid S. Miller if (orig) 1190c5038a83SDavid S. Miller rt_free(orig); 1191c5038a83SDavid S. Miller 1192c5038a83SDavid S. Miller fnhe->fnhe_stamp = jiffies; 1193caacf05eSDavid S. Miller ret = true; 1194c5038a83SDavid S. Miller } 1195c5038a83SDavid S. Miller spin_unlock_bh(&fnhe_lock); 1196caacf05eSDavid S. Miller 1197caacf05eSDavid S. Miller return ret; 119854764bb6SEric Dumazet } 119954764bb6SEric Dumazet 1200caacf05eSDavid S. Miller static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt) 1201f2bb4bedSDavid S. Miller { 1202d26b3a7cSEric Dumazet struct rtable *orig, *prev, **p; 1203caacf05eSDavid S. Miller bool ret = true; 1204f2bb4bedSDavid S. Miller 1205d26b3a7cSEric Dumazet if (rt_is_input_route(rt)) { 120654764bb6SEric Dumazet p = (struct rtable **)&nh->nh_rth_input; 1207d26b3a7cSEric Dumazet } else { 1208d26b3a7cSEric Dumazet p = (struct rtable **)__this_cpu_ptr(nh->nh_pcpu_rth_output); 1209d26b3a7cSEric Dumazet } 1210f2bb4bedSDavid S. Miller orig = *p; 1211f2bb4bedSDavid S. Miller 1212f2bb4bedSDavid S. Miller prev = cmpxchg(p, orig, rt); 1213f2bb4bedSDavid S. Miller if (prev == orig) { 1214f2bb4bedSDavid S. Miller if (orig) 121554764bb6SEric Dumazet rt_free(orig); 1216155e8336SJulian Anastasov } else 1217caacf05eSDavid S. Miller ret = false; 1218caacf05eSDavid S. Miller 1219caacf05eSDavid S. Miller return ret; 1220caacf05eSDavid S. Miller } 1221caacf05eSDavid S. Miller 1222caacf05eSDavid S. Miller static DEFINE_SPINLOCK(rt_uncached_lock); 1223caacf05eSDavid S. Miller static LIST_HEAD(rt_uncached_list); 1224caacf05eSDavid S. Miller 1225caacf05eSDavid S. Miller static void rt_add_uncached_list(struct rtable *rt) 1226caacf05eSDavid S. Miller { 1227caacf05eSDavid S. Miller spin_lock_bh(&rt_uncached_lock); 1228caacf05eSDavid S. Miller list_add_tail(&rt->rt_uncached, &rt_uncached_list); 1229caacf05eSDavid S. Miller spin_unlock_bh(&rt_uncached_lock); 1230caacf05eSDavid S. Miller } 1231caacf05eSDavid S. Miller 1232caacf05eSDavid S. Miller static void ipv4_dst_destroy(struct dst_entry *dst) 1233caacf05eSDavid S. Miller { 1234caacf05eSDavid S. Miller struct rtable *rt = (struct rtable *) dst; 1235caacf05eSDavid S. Miller 123678df76a0SEric Dumazet if (!list_empty(&rt->rt_uncached)) { 1237caacf05eSDavid S. Miller spin_lock_bh(&rt_uncached_lock); 1238caacf05eSDavid S. Miller list_del(&rt->rt_uncached); 1239caacf05eSDavid S. Miller spin_unlock_bh(&rt_uncached_lock); 1240caacf05eSDavid S. Miller } 1241caacf05eSDavid S. Miller } 1242caacf05eSDavid S. Miller 1243caacf05eSDavid S. Miller void rt_flush_dev(struct net_device *dev) 1244caacf05eSDavid S. Miller { 1245caacf05eSDavid S. Miller if (!list_empty(&rt_uncached_list)) { 1246caacf05eSDavid S. Miller struct net *net = dev_net(dev); 1247caacf05eSDavid S. Miller struct rtable *rt; 1248caacf05eSDavid S. Miller 1249caacf05eSDavid S. Miller spin_lock_bh(&rt_uncached_lock); 1250caacf05eSDavid S. Miller list_for_each_entry(rt, &rt_uncached_list, rt_uncached) { 1251caacf05eSDavid S. Miller if (rt->dst.dev != dev) 1252caacf05eSDavid S. Miller continue; 1253caacf05eSDavid S. Miller rt->dst.dev = net->loopback_dev; 1254caacf05eSDavid S. Miller dev_hold(rt->dst.dev); 1255caacf05eSDavid S. Miller dev_put(dev); 1256caacf05eSDavid S. Miller } 1257caacf05eSDavid S. Miller spin_unlock_bh(&rt_uncached_lock); 12584895c771SDavid S. Miller } 12594895c771SDavid S. Miller } 12604895c771SDavid S. Miller 12614331debcSEric Dumazet static bool rt_cache_valid(const struct rtable *rt) 1262d2d68ba9SDavid S. Miller { 12634331debcSEric Dumazet return rt && 12644331debcSEric Dumazet rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 12654331debcSEric Dumazet !rt_is_expired(rt); 1266d2d68ba9SDavid S. Miller } 1267d2d68ba9SDavid S. Miller 1268f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr, 12695e2b61f7SDavid S. Miller const struct fib_result *res, 1270f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe, 1271982721f3SDavid S. Miller struct fib_info *fi, u16 type, u32 itag) 12721da177e4SLinus Torvalds { 1273caacf05eSDavid S. Miller bool cached = false; 1274caacf05eSDavid S. Miller 12751da177e4SLinus Torvalds if (fi) { 12764895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(*res); 12774895c771SDavid S. Miller 1278155e8336SJulian Anastasov if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK) { 12794895c771SDavid S. Miller rt->rt_gateway = nh->nh_gw; 1280155e8336SJulian Anastasov rt->rt_uses_gateway = 1; 1281155e8336SJulian Anastasov } 12822860583fSDavid S. Miller dst_init_metrics(&rt->dst, fi->fib_metrics, true); 1283c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1284f2bb4bedSDavid S. Miller rt->dst.tclassid = nh->nh_tclassid; 12851da177e4SLinus Torvalds #endif 1286c5038a83SDavid S. Miller if (unlikely(fnhe)) 1287caacf05eSDavid S. Miller cached = rt_bind_exception(rt, fnhe, daddr); 1288c5038a83SDavid S. Miller else if (!(rt->dst.flags & DST_NOCACHE)) 1289caacf05eSDavid S. Miller cached = rt_cache_route(nh, rt); 1290155e8336SJulian Anastasov if (unlikely(!cached)) { 1291155e8336SJulian Anastasov /* Routes we intend to cache in nexthop exception or 1292155e8336SJulian Anastasov * FIB nexthop have the DST_NOCACHE bit clear. 1293155e8336SJulian Anastasov * However, if we are unsuccessful at storing this 1294155e8336SJulian Anastasov * route into the cache we really need to set it. 1295155e8336SJulian Anastasov */ 1296155e8336SJulian Anastasov rt->dst.flags |= DST_NOCACHE; 1297155e8336SJulian Anastasov if (!rt->rt_gateway) 1298155e8336SJulian Anastasov rt->rt_gateway = daddr; 1299155e8336SJulian Anastasov rt_add_uncached_list(rt); 1300d33e4553SDavid S. Miller } 1301155e8336SJulian Anastasov } else 1302caacf05eSDavid S. Miller rt_add_uncached_list(rt); 13031da177e4SLinus Torvalds 1304c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 13051da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES 130685b91b03SDavid S. Miller set_class_tag(rt, res->tclassid); 13071da177e4SLinus Torvalds #endif 13081da177e4SLinus Torvalds set_class_tag(rt, itag); 13091da177e4SLinus Torvalds #endif 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13125c1e6aa3SDavid S. Miller static struct rtable *rt_dst_alloc(struct net_device *dev, 1313f2bb4bedSDavid S. Miller bool nopolicy, bool noxfrm, bool will_cache) 13140c4dcd58SDavid S. Miller { 1315f5b0a874SDavid S. Miller return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK, 1316c6cffba4SDavid S. Miller (will_cache ? 0 : (DST_HOST | DST_NOCACHE)) | 13170c4dcd58SDavid S. Miller (nopolicy ? DST_NOPOLICY : 0) | 13185c1e6aa3SDavid S. Miller (noxfrm ? DST_NOXFRM : 0)); 13190c4dcd58SDavid S. Miller } 13200c4dcd58SDavid S. Miller 132196d36220SEric Dumazet /* called in rcu_read_lock() section */ 13229e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr, 13231da177e4SLinus Torvalds u8 tos, struct net_device *dev, int our) 13241da177e4SLinus Torvalds { 13251da177e4SLinus Torvalds struct rtable *rth; 132696d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 13271da177e4SLinus Torvalds u32 itag = 0; 1328b5f7e755SEric Dumazet int err; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* Primary sanity checks. */ 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds if (in_dev == NULL) 13331da177e4SLinus Torvalds return -EINVAL; 13341da177e4SLinus Torvalds 13351e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1336d0daebc3SThomas Graf skb->protocol != htons(ETH_P_IP)) 1337d0daebc3SThomas Graf goto e_inval; 1338d0daebc3SThomas Graf 1339d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 1340d0daebc3SThomas Graf if (ipv4_is_loopback(saddr)) 13411da177e4SLinus Torvalds goto e_inval; 13421da177e4SLinus Torvalds 1343f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) { 1344f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr)) 13451da177e4SLinus Torvalds goto e_inval; 1346b5f7e755SEric Dumazet } else { 13479e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 13489e56e380SDavid S. Miller in_dev, &itag); 1349b5f7e755SEric Dumazet if (err < 0) 1350b5f7e755SEric Dumazet goto e_err; 1351b5f7e755SEric Dumazet } 13524e7b2f14SBenjamin LaHaise rth = rt_dst_alloc(dev_net(dev)->loopback_dev, 1353f2bb4bedSDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false); 13541da177e4SLinus Torvalds if (!rth) 13551da177e4SLinus Torvalds goto e_nobufs; 13561da177e4SLinus Torvalds 1357c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1358d8d1f30bSChangli Gao rth->dst.tclassid = itag; 13591da177e4SLinus Torvalds #endif 1360cf911662SDavid S. Miller rth->dst.output = ip_rt_bug; 1361cf911662SDavid S. Miller 1362e84f84f2SDenis V. Lunev rth->rt_genid = rt_genid(dev_net(dev)); 13631da177e4SLinus Torvalds rth->rt_flags = RTCF_MULTICAST; 136429e75252SEric Dumazet rth->rt_type = RTN_MULTICAST; 13659917e1e8SDavid S. Miller rth->rt_is_input= 1; 136613378cadSDavid S. Miller rth->rt_iif = 0; 13675943634fSDavid S. Miller rth->rt_pmtu = 0; 1368f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1369155e8336SJulian Anastasov rth->rt_uses_gateway = 0; 1370caacf05eSDavid S. Miller INIT_LIST_HEAD(&rth->rt_uncached); 13711da177e4SLinus Torvalds if (our) { 1372d8d1f30bSChangli Gao rth->dst.input= ip_local_deliver; 13731da177e4SLinus Torvalds rth->rt_flags |= RTCF_LOCAL; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1377f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev)) 1378d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 13791da177e4SLinus Torvalds #endif 13801da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_mc); 13811da177e4SLinus Torvalds 138289aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 138389aef892SDavid S. Miller return 0; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds e_nobufs: 13861da177e4SLinus Torvalds return -ENOBUFS; 13871da177e4SLinus Torvalds e_inval: 138896d36220SEric Dumazet return -EINVAL; 1389b5f7e755SEric Dumazet e_err: 1390b5f7e755SEric Dumazet return err; 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev, 13951da177e4SLinus Torvalds struct in_device *in_dev, 13961da177e4SLinus Torvalds struct sk_buff *skb, 13979e12bb22SAl Viro __be32 daddr, 13989e12bb22SAl Viro __be32 saddr) 13991da177e4SLinus Torvalds { 14001da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_src); 14011da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 14021da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) { 14031da177e4SLinus Torvalds /* 14041da177e4SLinus Torvalds * RFC1812 recommendation, if source is martian, 14051da177e4SLinus Torvalds * the only hint is MAC header. 14061da177e4SLinus Torvalds */ 1407058bd4d2SJoe Perches pr_warn("martian source %pI4 from %pI4, on dev %s\n", 1408673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 140998e399f8SArnaldo Carvalho de Melo if (dev->hard_header_len && skb_mac_header_was_set(skb)) { 1410058bd4d2SJoe Perches print_hex_dump(KERN_WARNING, "ll header: ", 1411058bd4d2SJoe Perches DUMP_PREFIX_OFFSET, 16, 1, 1412058bd4d2SJoe Perches skb_mac_header(skb), 1413058bd4d2SJoe Perches dev->hard_header_len, true); 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds #endif 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 141947360228SEric Dumazet /* called in rcu_read_lock() section */ 14205969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb, 1421982721f3SDavid S. Miller const struct fib_result *res, 14221da177e4SLinus Torvalds struct in_device *in_dev, 1423c6cffba4SDavid S. Miller __be32 daddr, __be32 saddr, u32 tos) 14241da177e4SLinus Torvalds { 14251da177e4SLinus Torvalds struct rtable *rth; 14261da177e4SLinus Torvalds int err; 14271da177e4SLinus Torvalds struct in_device *out_dev; 142847360228SEric Dumazet unsigned int flags = 0; 1429d2d68ba9SDavid S. Miller bool do_cache; 1430d9c9df8cSAl Viro u32 itag; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds /* get a working reference to the output device */ 143347360228SEric Dumazet out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res)); 14341da177e4SLinus Torvalds if (out_dev == NULL) { 1435e87cc472SJoe Perches net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n"); 14361da177e4SLinus Torvalds return -EINVAL; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14395c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res), 14409e56e380SDavid S. Miller in_dev->dev, in_dev, &itag); 14411da177e4SLinus Torvalds if (err < 0) { 14421da177e4SLinus Torvalds ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr, 14431da177e4SLinus Torvalds saddr); 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds goto cleanup; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 1448e81da0e1SJulian Anastasov do_cache = res->fi && !itag; 1449e81da0e1SJulian Anastasov if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) && 14501da177e4SLinus Torvalds (IN_DEV_SHARED_MEDIA(out_dev) || 1451e81da0e1SJulian Anastasov inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res)))) { 14521da177e4SLinus Torvalds flags |= RTCF_DOREDIRECT; 1453e81da0e1SJulian Anastasov do_cache = false; 1454e81da0e1SJulian Anastasov } 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) { 14571da177e4SLinus Torvalds /* Not IP (i.e. ARP). Do not create route, if it is 14581da177e4SLinus Torvalds * invalid for proxy arp. DNAT routes are always valid. 145965324144SJesper Dangaard Brouer * 146065324144SJesper Dangaard Brouer * Proxy arp feature have been extended to allow, ARP 146165324144SJesper Dangaard Brouer * replies back to the same interface, to support 146265324144SJesper Dangaard Brouer * Private VLAN switch technologies. See arp.c. 14631da177e4SLinus Torvalds */ 146465324144SJesper Dangaard Brouer if (out_dev == in_dev && 146565324144SJesper Dangaard Brouer IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) { 14661da177e4SLinus Torvalds err = -EINVAL; 14671da177e4SLinus Torvalds goto cleanup; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 1471e81da0e1SJulian Anastasov if (do_cache) { 147254764bb6SEric Dumazet rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input); 1473d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1474c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst); 1475d2d68ba9SDavid S. Miller goto out; 1476d2d68ba9SDavid S. Miller } 1477d2d68ba9SDavid S. Miller } 1478f2bb4bedSDavid S. Miller 14795c1e6aa3SDavid S. Miller rth = rt_dst_alloc(out_dev->dev, 14805c1e6aa3SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), 1481d2d68ba9SDavid S. Miller IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache); 14821da177e4SLinus Torvalds if (!rth) { 14831da177e4SLinus Torvalds err = -ENOBUFS; 14841da177e4SLinus Torvalds goto cleanup; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 1487cf911662SDavid S. Miller rth->rt_genid = rt_genid(dev_net(rth->dst.dev)); 1488cf911662SDavid S. Miller rth->rt_flags = flags; 1489cf911662SDavid S. Miller rth->rt_type = res->type; 14909917e1e8SDavid S. Miller rth->rt_is_input = 1; 149113378cadSDavid S. Miller rth->rt_iif = 0; 14925943634fSDavid S. Miller rth->rt_pmtu = 0; 1493f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1494155e8336SJulian Anastasov rth->rt_uses_gateway = 0; 1495caacf05eSDavid S. Miller INIT_LIST_HEAD(&rth->rt_uncached); 14961da177e4SLinus Torvalds 1497d8d1f30bSChangli Gao rth->dst.input = ip_forward; 1498d8d1f30bSChangli Gao rth->dst.output = ip_output; 14991da177e4SLinus Torvalds 1500d2d68ba9SDavid S. Miller rt_set_nexthop(rth, daddr, res, NULL, res->fi, res->type, itag); 1501c6cffba4SDavid S. Miller skb_dst_set(skb, &rth->dst); 1502d2d68ba9SDavid S. Miller out: 15031da177e4SLinus Torvalds err = 0; 15041da177e4SLinus Torvalds cleanup: 15051da177e4SLinus Torvalds return err; 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 15085969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb, 15091da177e4SLinus Torvalds struct fib_result *res, 151068a5e3ddSDavid S. Miller const struct flowi4 *fl4, 15111da177e4SLinus Torvalds struct in_device *in_dev, 15129e12bb22SAl Viro __be32 daddr, __be32 saddr, u32 tos) 15131da177e4SLinus Torvalds { 15141da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 1515ff3fccb3SDavid S. Miller if (res->fi && res->fi->fib_nhs > 1) 15161b7fe593SDavid S. Miller fib_select_multipath(res); 15171da177e4SLinus Torvalds #endif 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds /* create a routing cache entry */ 1520c6cffba4SDavid S. Miller return __mkroute_input(skb, res, in_dev, daddr, saddr, tos); 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /* 15241da177e4SLinus Torvalds * NOTE. We drop all the packets that has local source 15251da177e4SLinus Torvalds * addresses, because every properly looped back packet 15261da177e4SLinus Torvalds * must have correct destination already attached by output routine. 15271da177e4SLinus Torvalds * 15281da177e4SLinus Torvalds * Such approach solves two big problems: 15291da177e4SLinus Torvalds * 1. Not simplex devices are handled properly. 15301da177e4SLinus Torvalds * 2. IP spoofing attempts are filtered with 100% of guarantee. 1531ebc0ffaeSEric Dumazet * called with rcu_read_lock() 15321da177e4SLinus Torvalds */ 15331da177e4SLinus Torvalds 15349e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1535c10237e0SDavid S. Miller u8 tos, struct net_device *dev) 15361da177e4SLinus Torvalds { 15371da177e4SLinus Torvalds struct fib_result res; 153896d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 153968a5e3ddSDavid S. Miller struct flowi4 fl4; 154095c96174SEric Dumazet unsigned int flags = 0; 15411da177e4SLinus Torvalds u32 itag = 0; 15421da177e4SLinus Torvalds struct rtable *rth; 15431da177e4SLinus Torvalds int err = -EINVAL; 1544c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1545d2d68ba9SDavid S. Miller bool do_cache; 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* IP on this device is disabled. */ 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds if (!in_dev) 15501da177e4SLinus Torvalds goto out; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /* Check for the most weird martians, which can be not detected 15531da177e4SLinus Torvalds by fib_lookup. 15541da177e4SLinus Torvalds */ 15551da177e4SLinus Torvalds 1556d0daebc3SThomas Graf if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 15571da177e4SLinus Torvalds goto martian_source; 15581da177e4SLinus Torvalds 1559d2d68ba9SDavid S. Miller res.fi = NULL; 156027a954bdSAndy Walls if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0)) 15611da177e4SLinus Torvalds goto brd_input; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* Accept zero addresses only to limited broadcast; 15641da177e4SLinus Torvalds * I even do not know to fix it or not. Waiting for complains :-) 15651da177e4SLinus Torvalds */ 1566f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) 15671da177e4SLinus Torvalds goto martian_source; 15681da177e4SLinus Torvalds 1569d0daebc3SThomas Graf if (ipv4_is_zeronet(daddr)) 15701da177e4SLinus Torvalds goto martian_destination; 15711da177e4SLinus Torvalds 15729eb43e76SEric Dumazet /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(), 15739eb43e76SEric Dumazet * and call it once if daddr or/and saddr are loopback addresses 15749eb43e76SEric Dumazet */ 15759eb43e76SEric Dumazet if (ipv4_is_loopback(daddr)) { 15769eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 1577d0daebc3SThomas Graf goto martian_destination; 15789eb43e76SEric Dumazet } else if (ipv4_is_loopback(saddr)) { 15799eb43e76SEric Dumazet if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 1580d0daebc3SThomas Graf goto martian_source; 1581d0daebc3SThomas Graf } 1582d0daebc3SThomas Graf 15831da177e4SLinus Torvalds /* 15841da177e4SLinus Torvalds * Now we are ready to route packet. 15851da177e4SLinus Torvalds */ 158668a5e3ddSDavid S. Miller fl4.flowi4_oif = 0; 158768a5e3ddSDavid S. Miller fl4.flowi4_iif = dev->ifindex; 158868a5e3ddSDavid S. Miller fl4.flowi4_mark = skb->mark; 158968a5e3ddSDavid S. Miller fl4.flowi4_tos = tos; 159068a5e3ddSDavid S. Miller fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 159168a5e3ddSDavid S. Miller fl4.daddr = daddr; 159268a5e3ddSDavid S. Miller fl4.saddr = saddr; 159368a5e3ddSDavid S. Miller err = fib_lookup(net, &fl4, &res); 1594251da413SDavid S. Miller if (err != 0) 15951da177e4SLinus Torvalds goto no_route; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_tot); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds if (res.type == RTN_BROADCAST) 16001da177e4SLinus Torvalds goto brd_input; 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 16035c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, 16041fb9489bSPavel Emelyanov LOOPBACK_IFINDEX, 16059e56e380SDavid S. Miller dev, in_dev, &itag); 1606b5f7e755SEric Dumazet if (err < 0) 1607b5f7e755SEric Dumazet goto martian_source_keep_err; 16081da177e4SLinus Torvalds goto local_input; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds if (!IN_DEV_FORWARD(in_dev)) 1612251da413SDavid S. Miller goto no_route; 16131da177e4SLinus Torvalds if (res.type != RTN_UNICAST) 16141da177e4SLinus Torvalds goto martian_destination; 16151da177e4SLinus Torvalds 161668a5e3ddSDavid S. Miller err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos); 16171da177e4SLinus Torvalds out: return err; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds brd_input: 16201da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) 16211da177e4SLinus Torvalds goto e_inval; 16221da177e4SLinus Torvalds 162341347dcdSDavid S. Miller if (!ipv4_is_zeronet(saddr)) { 16249e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 16259e56e380SDavid S. Miller in_dev, &itag); 16261da177e4SLinus Torvalds if (err < 0) 1627b5f7e755SEric Dumazet goto martian_source_keep_err; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds flags |= RTCF_BROADCAST; 16301da177e4SLinus Torvalds res.type = RTN_BROADCAST; 16311da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_brd); 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds local_input: 1634d2d68ba9SDavid S. Miller do_cache = false; 1635d2d68ba9SDavid S. Miller if (res.fi) { 1636fe3edf45SDavid S. Miller if (!itag) { 163754764bb6SEric Dumazet rth = rcu_dereference(FIB_RES_NH(res).nh_rth_input); 1638d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1639c6cffba4SDavid S. Miller skb_dst_set_noref(skb, &rth->dst); 1640c6cffba4SDavid S. Miller err = 0; 1641c6cffba4SDavid S. Miller goto out; 1642d2d68ba9SDavid S. Miller } 1643d2d68ba9SDavid S. Miller do_cache = true; 1644d2d68ba9SDavid S. Miller } 1645d2d68ba9SDavid S. Miller } 1646d2d68ba9SDavid S. Miller 16475c1e6aa3SDavid S. Miller rth = rt_dst_alloc(net->loopback_dev, 1648d2d68ba9SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache); 16491da177e4SLinus Torvalds if (!rth) 16501da177e4SLinus Torvalds goto e_nobufs; 16511da177e4SLinus Torvalds 1652cf911662SDavid S. Miller rth->dst.input= ip_local_deliver; 1653d8d1f30bSChangli Gao rth->dst.output= ip_rt_bug; 1654cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 1655cf911662SDavid S. Miller rth->dst.tclassid = itag; 1656cf911662SDavid S. Miller #endif 16571da177e4SLinus Torvalds 1658cf911662SDavid S. Miller rth->rt_genid = rt_genid(net); 1659cf911662SDavid S. Miller rth->rt_flags = flags|RTCF_LOCAL; 1660cf911662SDavid S. Miller rth->rt_type = res.type; 16619917e1e8SDavid S. Miller rth->rt_is_input = 1; 166213378cadSDavid S. Miller rth->rt_iif = 0; 16635943634fSDavid S. Miller rth->rt_pmtu = 0; 1664f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1665155e8336SJulian Anastasov rth->rt_uses_gateway = 0; 1666caacf05eSDavid S. Miller INIT_LIST_HEAD(&rth->rt_uncached); 16671da177e4SLinus Torvalds if (res.type == RTN_UNREACHABLE) { 1668d8d1f30bSChangli Gao rth->dst.input= ip_error; 1669d8d1f30bSChangli Gao rth->dst.error= -err; 16701da177e4SLinus Torvalds rth->rt_flags &= ~RTCF_LOCAL; 16711da177e4SLinus Torvalds } 1672d2d68ba9SDavid S. Miller if (do_cache) 1673d2d68ba9SDavid S. Miller rt_cache_route(&FIB_RES_NH(res), rth); 167489aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 1675b23dd4feSDavid S. Miller err = 0; 1676ebc0ffaeSEric Dumazet goto out; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds no_route: 16791da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_no_route); 16801da177e4SLinus Torvalds res.type = RTN_UNREACHABLE; 16817f53878dSMitsuru Chinen if (err == -ESRCH) 16827f53878dSMitsuru Chinen err = -ENETUNREACH; 16831da177e4SLinus Torvalds goto local_input; 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds /* 16861da177e4SLinus Torvalds * Do not cache martian addresses: they should be logged (RFC1812) 16871da177e4SLinus Torvalds */ 16881da177e4SLinus Torvalds martian_destination: 16891da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_dst); 16901da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 1691e87cc472SJoe Perches if (IN_DEV_LOG_MARTIANS(in_dev)) 1692e87cc472SJoe Perches net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n", 1693673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 16941da177e4SLinus Torvalds #endif 16952c2910a4SDietmar Eggemann 16961da177e4SLinus Torvalds e_inval: 16971da177e4SLinus Torvalds err = -EINVAL; 1698ebc0ffaeSEric Dumazet goto out; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds e_nobufs: 17011da177e4SLinus Torvalds err = -ENOBUFS; 1702ebc0ffaeSEric Dumazet goto out; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds martian_source: 1705b5f7e755SEric Dumazet err = -EINVAL; 1706b5f7e755SEric Dumazet martian_source_keep_err: 17071da177e4SLinus Torvalds ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 1708ebc0ffaeSEric Dumazet goto out; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 1711c6cffba4SDavid S. Miller int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr, 171238a424e4SDavid Miller u8 tos, struct net_device *dev) 17131da177e4SLinus Torvalds { 171496d36220SEric Dumazet int res; 17151da177e4SLinus Torvalds 171696d36220SEric Dumazet rcu_read_lock(); 171796d36220SEric Dumazet 17181da177e4SLinus Torvalds /* Multicast recognition logic is moved from route cache to here. 17191da177e4SLinus Torvalds The problem was that too many Ethernet cards have broken/missing 17201da177e4SLinus Torvalds hardware multicast filters :-( As result the host on multicasting 17211da177e4SLinus Torvalds network acquires a lot of useless route cache entries, sort of 17221da177e4SLinus Torvalds SDR messages from all the world. Now we try to get rid of them. 17231da177e4SLinus Torvalds Really, provided software IP multicast filter is organized 17241da177e4SLinus Torvalds reasonably (at least, hashed), it does not result in a slowdown 17251da177e4SLinus Torvalds comparing with route cache reject entries. 17261da177e4SLinus Torvalds Note, that multicast routers are not affected, because 17271da177e4SLinus Torvalds route cache entry is created eventually. 17281da177e4SLinus Torvalds */ 1729f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 173096d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 17311da177e4SLinus Torvalds 173296d36220SEric Dumazet if (in_dev) { 1733dbdd9a52SDavid S. Miller int our = ip_check_mc_rcu(in_dev, daddr, saddr, 1734eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol); 17351da177e4SLinus Torvalds if (our 17361da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 17379d4fb27dSJoe Perches || 17389d4fb27dSJoe Perches (!ipv4_is_local_multicast(daddr) && 1739f97c1e0cSJoe Perches IN_DEV_MFORWARD(in_dev)) 17401da177e4SLinus Torvalds #endif 17411da177e4SLinus Torvalds ) { 174296d36220SEric Dumazet int res = ip_route_input_mc(skb, daddr, saddr, 17431da177e4SLinus Torvalds tos, dev, our); 174496d36220SEric Dumazet rcu_read_unlock(); 174596d36220SEric Dumazet return res; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds rcu_read_unlock(); 17491da177e4SLinus Torvalds return -EINVAL; 17501da177e4SLinus Torvalds } 1751c10237e0SDavid S. Miller res = ip_route_input_slow(skb, daddr, saddr, tos, dev); 175296d36220SEric Dumazet rcu_read_unlock(); 175396d36220SEric Dumazet return res; 17541da177e4SLinus Torvalds } 1755c6cffba4SDavid S. Miller EXPORT_SYMBOL(ip_route_input_noref); 17561da177e4SLinus Torvalds 1757ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */ 1758982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res, 17591a00fee4SDavid Miller const struct flowi4 *fl4, int orig_oif, 1760f61759e6SJulian Anastasov struct net_device *dev_out, 17615ada5527SDavid S. Miller unsigned int flags) 17621da177e4SLinus Torvalds { 1763982721f3SDavid S. Miller struct fib_info *fi = res->fi; 1764f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe; 17655ada5527SDavid S. Miller struct in_device *in_dev; 1766982721f3SDavid S. Miller u16 type = res->type; 17675ada5527SDavid S. Miller struct rtable *rth; 1768c92b9655SJulian Anastasov bool do_cache; 17691da177e4SLinus Torvalds 1770d0daebc3SThomas Graf in_dev = __in_dev_get_rcu(dev_out); 1771d0daebc3SThomas Graf if (!in_dev) 1772d0daebc3SThomas Graf return ERR_PTR(-EINVAL); 1773d0daebc3SThomas Graf 1774d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 177568a5e3ddSDavid S. Miller if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK)) 17765ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 17771da177e4SLinus Torvalds 177868a5e3ddSDavid S. Miller if (ipv4_is_lbcast(fl4->daddr)) 1779982721f3SDavid S. Miller type = RTN_BROADCAST; 178068a5e3ddSDavid S. Miller else if (ipv4_is_multicast(fl4->daddr)) 1781982721f3SDavid S. Miller type = RTN_MULTICAST; 178268a5e3ddSDavid S. Miller else if (ipv4_is_zeronet(fl4->daddr)) 17835ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (dev_out->flags & IFF_LOOPBACK) 17861da177e4SLinus Torvalds flags |= RTCF_LOCAL; 17871da177e4SLinus Torvalds 1788*63617421SJulian Anastasov do_cache = true; 1789982721f3SDavid S. Miller if (type == RTN_BROADCAST) { 17901da177e4SLinus Torvalds flags |= RTCF_BROADCAST | RTCF_LOCAL; 1791982721f3SDavid S. Miller fi = NULL; 1792982721f3SDavid S. Miller } else if (type == RTN_MULTICAST) { 17931da177e4SLinus Torvalds flags |= RTCF_MULTICAST | RTCF_LOCAL; 1794813b3b5dSDavid S. Miller if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr, 1795813b3b5dSDavid S. Miller fl4->flowi4_proto)) 17961da177e4SLinus Torvalds flags &= ~RTCF_LOCAL; 1797*63617421SJulian Anastasov else 1798*63617421SJulian Anastasov do_cache = false; 17991da177e4SLinus Torvalds /* If multicast route do not exist use 1800dd28d1a0SEric Dumazet * default one, but do not gateway in this case. 1801dd28d1a0SEric Dumazet * Yes, it is hack. 18021da177e4SLinus Torvalds */ 1803982721f3SDavid S. Miller if (fi && res->prefixlen < 4) 1804982721f3SDavid S. Miller fi = NULL; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 1807f2bb4bedSDavid S. Miller fnhe = NULL; 1808*63617421SJulian Anastasov do_cache &= fi != NULL; 1809*63617421SJulian Anastasov if (do_cache) { 1810d26b3a7cSEric Dumazet struct rtable __rcu **prth; 1811c92b9655SJulian Anastasov struct fib_nh *nh = &FIB_RES_NH(*res); 1812d26b3a7cSEric Dumazet 1813c92b9655SJulian Anastasov fnhe = find_exception(nh, fl4->daddr); 1814c5038a83SDavid S. Miller if (fnhe) 1815c5038a83SDavid S. Miller prth = &fnhe->fnhe_rth; 1816c92b9655SJulian Anastasov else { 1817c92b9655SJulian Anastasov if (unlikely(fl4->flowi4_flags & 1818c92b9655SJulian Anastasov FLOWI_FLAG_KNOWN_NH && 1819c92b9655SJulian Anastasov !(nh->nh_gw && 1820c92b9655SJulian Anastasov nh->nh_scope == RT_SCOPE_LINK))) { 1821c92b9655SJulian Anastasov do_cache = false; 1822c92b9655SJulian Anastasov goto add; 1823c92b9655SJulian Anastasov } 1824c92b9655SJulian Anastasov prth = __this_cpu_ptr(nh->nh_pcpu_rth_output); 1825c92b9655SJulian Anastasov } 1826d26b3a7cSEric Dumazet rth = rcu_dereference(*prth); 1827d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 182893ac5341SDavid S. Miller dst_hold(&rth->dst); 1829f2bb4bedSDavid S. Miller return rth; 1830f2bb4bedSDavid S. Miller } 1831f2bb4bedSDavid S. Miller } 1832c92b9655SJulian Anastasov 1833c92b9655SJulian Anastasov add: 18345c1e6aa3SDavid S. Miller rth = rt_dst_alloc(dev_out, 18355c1e6aa3SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), 1836f2bb4bedSDavid S. Miller IN_DEV_CONF_GET(in_dev, NOXFRM), 1837c92b9655SJulian Anastasov do_cache); 18388391d07bSDimitris Michailidis if (!rth) 18395ada5527SDavid S. Miller return ERR_PTR(-ENOBUFS); 18408391d07bSDimitris Michailidis 1841cf911662SDavid S. Miller rth->dst.output = ip_output; 1842cf911662SDavid S. Miller 1843cf911662SDavid S. Miller rth->rt_genid = rt_genid(dev_net(dev_out)); 1844cf911662SDavid S. Miller rth->rt_flags = flags; 1845cf911662SDavid S. Miller rth->rt_type = type; 18469917e1e8SDavid S. Miller rth->rt_is_input = 0; 184713378cadSDavid S. Miller rth->rt_iif = orig_oif ? : 0; 18485943634fSDavid S. Miller rth->rt_pmtu = 0; 1849f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1850155e8336SJulian Anastasov rth->rt_uses_gateway = 0; 1851caacf05eSDavid S. Miller INIT_LIST_HEAD(&rth->rt_uncached); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_tot); 18541da177e4SLinus Torvalds 185541347dcdSDavid S. Miller if (flags & RTCF_LOCAL) 1856d8d1f30bSChangli Gao rth->dst.input = ip_local_deliver; 18571da177e4SLinus Torvalds if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 18581da177e4SLinus Torvalds if (flags & RTCF_LOCAL && 18591da177e4SLinus Torvalds !(dev_out->flags & IFF_LOOPBACK)) { 1860d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 18611da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_mc); 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1864982721f3SDavid S. Miller if (type == RTN_MULTICAST) { 18651da177e4SLinus Torvalds if (IN_DEV_MFORWARD(in_dev) && 1866813b3b5dSDavid S. Miller !ipv4_is_local_multicast(fl4->daddr)) { 1867d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 1868d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds #endif 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874f2bb4bedSDavid S. Miller rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0); 18751da177e4SLinus Torvalds 18765ada5527SDavid S. Miller return rth; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds /* 18801da177e4SLinus Torvalds * Major route resolver routine. 18811da177e4SLinus Torvalds */ 18821da177e4SLinus Torvalds 188389aef892SDavid S. Miller struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4) 18841da177e4SLinus Torvalds { 18851da177e4SLinus Torvalds struct net_device *dev_out = NULL; 1886f61759e6SJulian Anastasov __u8 tos = RT_FL_TOS(fl4); 1887813b3b5dSDavid S. Miller unsigned int flags = 0; 1888813b3b5dSDavid S. Miller struct fib_result res; 18895ada5527SDavid S. Miller struct rtable *rth; 1890813b3b5dSDavid S. Miller int orig_oif; 18911da177e4SLinus Torvalds 189285b91b03SDavid S. Miller res.tclassid = 0; 18931da177e4SLinus Torvalds res.fi = NULL; 18948b96d22dSDavid S. Miller res.table = NULL; 18951da177e4SLinus Torvalds 1896813b3b5dSDavid S. Miller orig_oif = fl4->flowi4_oif; 1897813b3b5dSDavid S. Miller 18981fb9489bSPavel Emelyanov fl4->flowi4_iif = LOOPBACK_IFINDEX; 1899813b3b5dSDavid S. Miller fl4->flowi4_tos = tos & IPTOS_RT_MASK; 1900813b3b5dSDavid S. Miller fl4->flowi4_scope = ((tos & RTO_ONLINK) ? 190144713b67SDavid S. Miller RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); 190244713b67SDavid S. Miller 1903010c2708SDavid S. Miller rcu_read_lock(); 1904813b3b5dSDavid S. Miller if (fl4->saddr) { 1905b23dd4feSDavid S. Miller rth = ERR_PTR(-EINVAL); 1906813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->saddr) || 1907813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->saddr) || 1908813b3b5dSDavid S. Miller ipv4_is_zeronet(fl4->saddr)) 19091da177e4SLinus Torvalds goto out; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds /* I removed check for oif == dev_out->oif here. 19121da177e4SLinus Torvalds It was wrong for two reasons: 19131ab35276SDenis V. Lunev 1. ip_dev_find(net, saddr) can return wrong iface, if saddr 19141ab35276SDenis V. Lunev is assigned to multiple interfaces. 19151da177e4SLinus Torvalds 2. Moreover, we are allowed to send packets with saddr 19161da177e4SLinus Torvalds of another iface. --ANK 19171da177e4SLinus Torvalds */ 19181da177e4SLinus Torvalds 1919813b3b5dSDavid S. Miller if (fl4->flowi4_oif == 0 && 1920813b3b5dSDavid S. Miller (ipv4_is_multicast(fl4->daddr) || 1921813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr))) { 1922a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 1923813b3b5dSDavid S. Miller dev_out = __ip_dev_find(net, fl4->saddr, false); 1924a210d01aSJulian Anastasov if (dev_out == NULL) 1925a210d01aSJulian Anastasov goto out; 1926a210d01aSJulian Anastasov 19271da177e4SLinus Torvalds /* Special hack: user can direct multicasts 19281da177e4SLinus Torvalds and limited broadcast via necessary interface 19291da177e4SLinus Torvalds without fiddling with IP_MULTICAST_IF or IP_PKTINFO. 19301da177e4SLinus Torvalds This hack is not just for fun, it allows 19311da177e4SLinus Torvalds vic,vat and friends to work. 19321da177e4SLinus Torvalds They bind socket to loopback, set ttl to zero 19331da177e4SLinus Torvalds and expect that it will work. 19341da177e4SLinus Torvalds From the viewpoint of routing cache they are broken, 19351da177e4SLinus Torvalds because we are not allowed to build multicast path 19361da177e4SLinus Torvalds with loopback source addr (look, routing cache 19371da177e4SLinus Torvalds cannot know, that ttl is zero, so that packet 19381da177e4SLinus Torvalds will not leave this host and route is valid). 19391da177e4SLinus Torvalds Luckily, this hack is good workaround. 19401da177e4SLinus Torvalds */ 19411da177e4SLinus Torvalds 1942813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 19431da177e4SLinus Torvalds goto make_route; 19441da177e4SLinus Torvalds } 1945a210d01aSJulian Anastasov 1946813b3b5dSDavid S. Miller if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) { 1947a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 1948813b3b5dSDavid S. Miller if (!__ip_dev_find(net, fl4->saddr, false)) 1949a210d01aSJulian Anastasov goto out; 19501da177e4SLinus Torvalds } 1951a210d01aSJulian Anastasov } 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds 1954813b3b5dSDavid S. Miller if (fl4->flowi4_oif) { 1955813b3b5dSDavid S. Miller dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif); 1956b23dd4feSDavid S. Miller rth = ERR_PTR(-ENODEV); 19571da177e4SLinus Torvalds if (dev_out == NULL) 19581da177e4SLinus Torvalds goto out; 1959e5ed6399SHerbert Xu 1960e5ed6399SHerbert Xu /* RACE: Check return value of inet_select_addr instead. */ 1961fc75fc83SEric Dumazet if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) { 1962b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH); 1963fc75fc83SEric Dumazet goto out; 1964fc75fc83SEric Dumazet } 1965813b3b5dSDavid S. Miller if (ipv4_is_local_multicast(fl4->daddr) || 1966813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr)) { 1967813b3b5dSDavid S. Miller if (!fl4->saddr) 1968813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 19691da177e4SLinus Torvalds RT_SCOPE_LINK); 19701da177e4SLinus Torvalds goto make_route; 19711da177e4SLinus Torvalds } 1972813b3b5dSDavid S. Miller if (fl4->saddr) { 1973813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->daddr)) 1974813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 1975813b3b5dSDavid S. Miller fl4->flowi4_scope); 1976813b3b5dSDavid S. Miller else if (!fl4->daddr) 1977813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 19781da177e4SLinus Torvalds RT_SCOPE_HOST); 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 1982813b3b5dSDavid S. Miller if (!fl4->daddr) { 1983813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr; 1984813b3b5dSDavid S. Miller if (!fl4->daddr) 1985813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK); 1986b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 19871fb9489bSPavel Emelyanov fl4->flowi4_oif = LOOPBACK_IFINDEX; 19881da177e4SLinus Torvalds res.type = RTN_LOCAL; 19891da177e4SLinus Torvalds flags |= RTCF_LOCAL; 19901da177e4SLinus Torvalds goto make_route; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 1993813b3b5dSDavid S. Miller if (fib_lookup(net, fl4, &res)) { 19941da177e4SLinus Torvalds res.fi = NULL; 19958b96d22dSDavid S. Miller res.table = NULL; 1996813b3b5dSDavid S. Miller if (fl4->flowi4_oif) { 19971da177e4SLinus Torvalds /* Apparently, routing tables are wrong. Assume, 19981da177e4SLinus Torvalds that the destination is on link. 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds WHY? DW. 20011da177e4SLinus Torvalds Because we are allowed to send to iface 20021da177e4SLinus Torvalds even if it has NO routes and NO assigned 20031da177e4SLinus Torvalds addresses. When oif is specified, routing 20041da177e4SLinus Torvalds tables are looked up with only one purpose: 20051da177e4SLinus Torvalds to catch if destination is gatewayed, rather than 20061da177e4SLinus Torvalds direct. Moreover, if MSG_DONTROUTE is set, 20071da177e4SLinus Torvalds we send packet, ignoring both routing tables 20081da177e4SLinus Torvalds and ifaddr state. --ANK 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds We could make it even if oif is unknown, 20121da177e4SLinus Torvalds likely IPv6, but we do not. 20131da177e4SLinus Torvalds */ 20141da177e4SLinus Torvalds 2015813b3b5dSDavid S. Miller if (fl4->saddr == 0) 2016813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 20171da177e4SLinus Torvalds RT_SCOPE_LINK); 20181da177e4SLinus Torvalds res.type = RTN_UNICAST; 20191da177e4SLinus Torvalds goto make_route; 20201da177e4SLinus Torvalds } 2021b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH); 20221da177e4SLinus Torvalds goto out; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 2026813b3b5dSDavid S. Miller if (!fl4->saddr) { 20279fc3bbb4SJoel Sing if (res.fi->fib_prefsrc) 2028813b3b5dSDavid S. Miller fl4->saddr = res.fi->fib_prefsrc; 20299fc3bbb4SJoel Sing else 2030813b3b5dSDavid S. Miller fl4->saddr = fl4->daddr; 20319fc3bbb4SJoel Sing } 2032b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 2033813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 20341da177e4SLinus Torvalds flags |= RTCF_LOCAL; 20351da177e4SLinus Torvalds goto make_route; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 2039813b3b5dSDavid S. Miller if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0) 20401b7fe593SDavid S. Miller fib_select_multipath(&res); 20411da177e4SLinus Torvalds else 20421da177e4SLinus Torvalds #endif 204321d8c49eSDavid S. Miller if (!res.prefixlen && 204421d8c49eSDavid S. Miller res.table->tb_num_default > 1 && 2045813b3b5dSDavid S. Miller res.type == RTN_UNICAST && !fl4->flowi4_oif) 20460c838ff1SDavid S. Miller fib_select_default(&res); 20471da177e4SLinus Torvalds 2048813b3b5dSDavid S. Miller if (!fl4->saddr) 2049813b3b5dSDavid S. Miller fl4->saddr = FIB_RES_PREFSRC(net, res); 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds dev_out = FIB_RES_DEV(res); 2052813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds make_route: 20561a00fee4SDavid Miller rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags); 20571da177e4SLinus Torvalds 2058010c2708SDavid S. Miller out: 2059010c2708SDavid S. Miller rcu_read_unlock(); 2060b23dd4feSDavid S. Miller return rth; 20611da177e4SLinus Torvalds } 2062d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key); 2063d8c97a94SArnaldo Carvalho de Melo 2064ae2688d5SJianzhao Wang static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie) 2065ae2688d5SJianzhao Wang { 2066ae2688d5SJianzhao Wang return NULL; 2067ae2688d5SJianzhao Wang } 2068ae2688d5SJianzhao Wang 2069ebb762f2SSteffen Klassert static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst) 2070ec831ea7SRoland Dreier { 2071618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 2072618f9bc7SSteffen Klassert 2073618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 2074ec831ea7SRoland Dreier } 2075ec831ea7SRoland Dreier 20766700c270SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 20776700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 207814e50e57SDavid S. Miller { 207914e50e57SDavid S. Miller } 208014e50e57SDavid S. Miller 20816700c270SDavid S. Miller static void ipv4_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 20826700c270SDavid S. Miller struct sk_buff *skb) 2083b587ee3bSDavid S. Miller { 2084b587ee3bSDavid S. Miller } 2085b587ee3bSDavid S. Miller 20860972ddb2SHeld Bernhard static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst, 20870972ddb2SHeld Bernhard unsigned long old) 20880972ddb2SHeld Bernhard { 20890972ddb2SHeld Bernhard return NULL; 20900972ddb2SHeld Bernhard } 20910972ddb2SHeld Bernhard 209214e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = { 209314e50e57SDavid S. Miller .family = AF_INET, 209409640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 2095ae2688d5SJianzhao Wang .check = ipv4_blackhole_dst_check, 2096ebb762f2SSteffen Klassert .mtu = ipv4_blackhole_mtu, 2097214f45c9SEric Dumazet .default_advmss = ipv4_default_advmss, 209814e50e57SDavid S. Miller .update_pmtu = ipv4_rt_blackhole_update_pmtu, 2099b587ee3bSDavid S. Miller .redirect = ipv4_rt_blackhole_redirect, 21000972ddb2SHeld Bernhard .cow_metrics = ipv4_rt_blackhole_cow_metrics, 2101d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 210214e50e57SDavid S. Miller }; 210314e50e57SDavid S. Miller 21042774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig) 210514e50e57SDavid S. Miller { 21062774c131SDavid S. Miller struct rtable *ort = (struct rtable *) dst_orig; 2107f5b0a874SDavid S. Miller struct rtable *rt; 210814e50e57SDavid S. Miller 2109f5b0a874SDavid S. Miller rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE, 0); 211014e50e57SDavid S. Miller if (rt) { 2111d8d1f30bSChangli Gao struct dst_entry *new = &rt->dst; 211214e50e57SDavid S. Miller 211314e50e57SDavid S. Miller new->__use = 1; 2114352e512cSHerbert Xu new->input = dst_discard; 2115352e512cSHerbert Xu new->output = dst_discard; 211614e50e57SDavid S. Miller 2117d8d1f30bSChangli Gao new->dev = ort->dst.dev; 211814e50e57SDavid S. Miller if (new->dev) 211914e50e57SDavid S. Miller dev_hold(new->dev); 212014e50e57SDavid S. Miller 21219917e1e8SDavid S. Miller rt->rt_is_input = ort->rt_is_input; 21225e2b61f7SDavid S. Miller rt->rt_iif = ort->rt_iif; 21235943634fSDavid S. Miller rt->rt_pmtu = ort->rt_pmtu; 212414e50e57SDavid S. Miller 2125e84f84f2SDenis V. Lunev rt->rt_genid = rt_genid(net); 212614e50e57SDavid S. Miller rt->rt_flags = ort->rt_flags; 212714e50e57SDavid S. Miller rt->rt_type = ort->rt_type; 212814e50e57SDavid S. Miller rt->rt_gateway = ort->rt_gateway; 2129155e8336SJulian Anastasov rt->rt_uses_gateway = ort->rt_uses_gateway; 213014e50e57SDavid S. Miller 2131caacf05eSDavid S. Miller INIT_LIST_HEAD(&rt->rt_uncached); 2132caacf05eSDavid S. Miller 213314e50e57SDavid S. Miller dst_free(new); 213414e50e57SDavid S. Miller } 213514e50e57SDavid S. Miller 21362774c131SDavid S. Miller dst_release(dst_orig); 21372774c131SDavid S. Miller 21382774c131SDavid S. Miller return rt ? &rt->dst : ERR_PTR(-ENOMEM); 213914e50e57SDavid S. Miller } 214014e50e57SDavid S. Miller 21419d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4, 2142273447b3SDavid S. Miller struct sock *sk) 21431da177e4SLinus Torvalds { 21449d6ec938SDavid S. Miller struct rtable *rt = __ip_route_output_key(net, flp4); 21451da177e4SLinus Torvalds 2146b23dd4feSDavid S. Miller if (IS_ERR(rt)) 2147b23dd4feSDavid S. Miller return rt; 21481da177e4SLinus Torvalds 214956157872SDavid S. Miller if (flp4->flowi4_proto) 21509d6ec938SDavid S. Miller rt = (struct rtable *) xfrm_lookup(net, &rt->dst, 21519d6ec938SDavid S. Miller flowi4_to_flowi(flp4), 21529d6ec938SDavid S. Miller sk, 0); 21531da177e4SLinus Torvalds 2154b23dd4feSDavid S. Miller return rt; 21551da177e4SLinus Torvalds } 2156d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow); 2157d8c97a94SArnaldo Carvalho de Melo 2158f1ce3062SDavid S. Miller static int rt_fill_info(struct net *net, __be32 dst, __be32 src, 215915e47304SEric W. Biederman struct flowi4 *fl4, struct sk_buff *skb, u32 portid, 2160f1ce3062SDavid S. Miller u32 seq, int event, int nowait, unsigned int flags) 21611da177e4SLinus Torvalds { 2162511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 21631da177e4SLinus Torvalds struct rtmsg *r; 21641da177e4SLinus Torvalds struct nlmsghdr *nlh; 21652bc8ca40SSteffen Klassert unsigned long expires = 0; 2166f185071dSDavid S. Miller u32 error; 2167521f5490SJulian Anastasov u32 metrics[RTAX_MAX]; 2168be403ea1SThomas Graf 216915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags); 2170be403ea1SThomas Graf if (nlh == NULL) 217126932566SPatrick McHardy return -EMSGSIZE; 2172be403ea1SThomas Graf 2173be403ea1SThomas Graf r = nlmsg_data(nlh); 21741da177e4SLinus Torvalds r->rtm_family = AF_INET; 21751da177e4SLinus Torvalds r->rtm_dst_len = 32; 21761da177e4SLinus Torvalds r->rtm_src_len = 0; 2177d6c0a4f6SDavid Miller r->rtm_tos = fl4->flowi4_tos; 21781da177e4SLinus Torvalds r->rtm_table = RT_TABLE_MAIN; 2179f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN)) 2180f3756b79SDavid S. Miller goto nla_put_failure; 21811da177e4SLinus Torvalds r->rtm_type = rt->rt_type; 21821da177e4SLinus Torvalds r->rtm_scope = RT_SCOPE_UNIVERSE; 21831da177e4SLinus Torvalds r->rtm_protocol = RTPROT_UNSPEC; 21841da177e4SLinus Torvalds r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; 21851da177e4SLinus Torvalds if (rt->rt_flags & RTCF_NOTIFY) 21861da177e4SLinus Torvalds r->rtm_flags |= RTM_F_NOTIFY; 2187be403ea1SThomas Graf 2188f1ce3062SDavid S. Miller if (nla_put_be32(skb, RTA_DST, dst)) 2189f3756b79SDavid S. Miller goto nla_put_failure; 21901a00fee4SDavid Miller if (src) { 21911da177e4SLinus Torvalds r->rtm_src_len = 32; 21921a00fee4SDavid Miller if (nla_put_be32(skb, RTA_SRC, src)) 2193f3756b79SDavid S. Miller goto nla_put_failure; 21941da177e4SLinus Torvalds } 2195f3756b79SDavid S. Miller if (rt->dst.dev && 2196f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2197f3756b79SDavid S. Miller goto nla_put_failure; 2198c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2199f3756b79SDavid S. Miller if (rt->dst.tclassid && 2200f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid)) 2201f3756b79SDavid S. Miller goto nla_put_failure; 22021da177e4SLinus Torvalds #endif 220341347dcdSDavid S. Miller if (!rt_is_input_route(rt) && 2204d6c0a4f6SDavid Miller fl4->saddr != src) { 2205d6c0a4f6SDavid Miller if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr)) 2206f3756b79SDavid S. Miller goto nla_put_failure; 2207f3756b79SDavid S. Miller } 2208155e8336SJulian Anastasov if (rt->rt_uses_gateway && 2209f3756b79SDavid S. Miller nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway)) 2210f3756b79SDavid S. Miller goto nla_put_failure; 2211be403ea1SThomas Graf 2212ee9a8f7aSSteffen Klassert expires = rt->dst.expires; 2213ee9a8f7aSSteffen Klassert if (expires) { 2214ee9a8f7aSSteffen Klassert unsigned long now = jiffies; 2215ee9a8f7aSSteffen Klassert 2216ee9a8f7aSSteffen Klassert if (time_before(now, expires)) 2217ee9a8f7aSSteffen Klassert expires -= now; 2218ee9a8f7aSSteffen Klassert else 2219ee9a8f7aSSteffen Klassert expires = 0; 2220ee9a8f7aSSteffen Klassert } 2221ee9a8f7aSSteffen Klassert 2222521f5490SJulian Anastasov memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 2223ee9a8f7aSSteffen Klassert if (rt->rt_pmtu && expires) 2224521f5490SJulian Anastasov metrics[RTAX_MTU - 1] = rt->rt_pmtu; 2225521f5490SJulian Anastasov if (rtnetlink_put_metrics(skb, metrics) < 0) 2226be403ea1SThomas Graf goto nla_put_failure; 2227be403ea1SThomas Graf 2228b4869889SDavid Miller if (fl4->flowi4_mark && 222968aaed54Sstephen hemminger nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark)) 2230f3756b79SDavid S. Miller goto nla_put_failure; 2231963bfeeeSEric Dumazet 2232d8d1f30bSChangli Gao error = rt->dst.error; 2233be403ea1SThomas Graf 2234c7537967SDavid S. Miller if (rt_is_input_route(rt)) { 2235f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, rt->rt_iif)) 2236f3756b79SDavid S. Miller goto nla_put_failure; 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 2239f185071dSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0) 2240e3703b3dSThomas Graf goto nla_put_failure; 22411da177e4SLinus Torvalds 2242be403ea1SThomas Graf return nlmsg_end(skb, nlh); 2243be403ea1SThomas Graf 2244be403ea1SThomas Graf nla_put_failure: 224526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 224626932566SPatrick McHardy return -EMSGSIZE; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 224963f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg) 22501da177e4SLinus Torvalds { 22513b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2252d889ce3bSThomas Graf struct rtmsg *rtm; 2253d889ce3bSThomas Graf struct nlattr *tb[RTA_MAX+1]; 22541da177e4SLinus Torvalds struct rtable *rt = NULL; 2255d6c0a4f6SDavid Miller struct flowi4 fl4; 22569e12bb22SAl Viro __be32 dst = 0; 22579e12bb22SAl Viro __be32 src = 0; 22589e12bb22SAl Viro u32 iif; 2259d889ce3bSThomas Graf int err; 2260963bfeeeSEric Dumazet int mark; 22611da177e4SLinus Torvalds struct sk_buff *skb; 22621da177e4SLinus Torvalds 2263d889ce3bSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy); 2264d889ce3bSThomas Graf if (err < 0) 2265d889ce3bSThomas Graf goto errout; 2266d889ce3bSThomas Graf 2267d889ce3bSThomas Graf rtm = nlmsg_data(nlh); 2268d889ce3bSThomas Graf 22691da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2270d889ce3bSThomas Graf if (skb == NULL) { 2271d889ce3bSThomas Graf err = -ENOBUFS; 2272d889ce3bSThomas Graf goto errout; 2273d889ce3bSThomas Graf } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 22761da177e4SLinus Torvalds through good chunk of routing engine. 22771da177e4SLinus Torvalds */ 2278459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 2279c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2280d2c962b8SStephen Hemminger 2281d2c962b8SStephen Hemminger /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */ 2282eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = IPPROTO_ICMP; 22831da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr)); 22841da177e4SLinus Torvalds 228517fb2c64SAl Viro src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0; 228617fb2c64SAl Viro dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0; 2287d889ce3bSThomas Graf iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 2288963bfeeeSEric Dumazet mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 22891da177e4SLinus Torvalds 2290d6c0a4f6SDavid Miller memset(&fl4, 0, sizeof(fl4)); 2291d6c0a4f6SDavid Miller fl4.daddr = dst; 2292d6c0a4f6SDavid Miller fl4.saddr = src; 2293d6c0a4f6SDavid Miller fl4.flowi4_tos = rtm->rtm_tos; 2294d6c0a4f6SDavid Miller fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0; 2295d6c0a4f6SDavid Miller fl4.flowi4_mark = mark; 2296d6c0a4f6SDavid Miller 22971da177e4SLinus Torvalds if (iif) { 2298d889ce3bSThomas Graf struct net_device *dev; 2299d889ce3bSThomas Graf 23001937504dSDenis V. Lunev dev = __dev_get_by_index(net, iif); 2301d889ce3bSThomas Graf if (dev == NULL) { 23021da177e4SLinus Torvalds err = -ENODEV; 2303d889ce3bSThomas Graf goto errout_free; 2304d889ce3bSThomas Graf } 2305d889ce3bSThomas Graf 23061da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 23071da177e4SLinus Torvalds skb->dev = dev; 2308963bfeeeSEric Dumazet skb->mark = mark; 23091da177e4SLinus Torvalds local_bh_disable(); 23101da177e4SLinus Torvalds err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev); 23111da177e4SLinus Torvalds local_bh_enable(); 2312d889ce3bSThomas Graf 2313511c3f92SEric Dumazet rt = skb_rtable(skb); 2314d8d1f30bSChangli Gao if (err == 0 && rt->dst.error) 2315d8d1f30bSChangli Gao err = -rt->dst.error; 23161da177e4SLinus Torvalds } else { 23179d6ec938SDavid S. Miller rt = ip_route_output_key(net, &fl4); 2318b23dd4feSDavid S. Miller 2319b23dd4feSDavid S. Miller err = 0; 2320b23dd4feSDavid S. Miller if (IS_ERR(rt)) 2321b23dd4feSDavid S. Miller err = PTR_ERR(rt); 23221da177e4SLinus Torvalds } 2323d889ce3bSThomas Graf 23241da177e4SLinus Torvalds if (err) 2325d889ce3bSThomas Graf goto errout_free; 23261da177e4SLinus Torvalds 2327d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 23281da177e4SLinus Torvalds if (rtm->rtm_flags & RTM_F_NOTIFY) 23291da177e4SLinus Torvalds rt->rt_flags |= RTCF_NOTIFY; 23301da177e4SLinus Torvalds 2331f1ce3062SDavid S. Miller err = rt_fill_info(net, dst, src, &fl4, skb, 233215e47304SEric W. Biederman NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 2333b6544c0bSJamal Hadi Salim RTM_NEWROUTE, 0, 0); 2334d889ce3bSThomas Graf if (err <= 0) 2335d889ce3bSThomas Graf goto errout_free; 23361da177e4SLinus Torvalds 233715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 2338d889ce3bSThomas Graf errout: 23392942e900SThomas Graf return err; 23401da177e4SLinus Torvalds 2341d889ce3bSThomas Graf errout_free: 23421da177e4SLinus Torvalds kfree_skb(skb); 2343d889ce3bSThomas Graf goto errout; 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb) 23471da177e4SLinus Torvalds { 23481da177e4SLinus Torvalds return skb->len; 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev) 23521da177e4SLinus Torvalds { 23534ccfe6d4SNicolas Dichtel rt_cache_flush(dev_net(in_dev->dev)); 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 235781c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write, 23588d65af78SAlexey Dobriyan void __user *buffer, 23591da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 23601da177e4SLinus Torvalds { 23611da177e4SLinus Torvalds if (write) { 23624ccfe6d4SNicolas Dichtel rt_cache_flush((struct net *)__ctl->extra1); 23631da177e4SLinus Torvalds return 0; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds return -EINVAL; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 2369eeb61f71SAl Viro static ctl_table ipv4_route_table[] = { 23701da177e4SLinus Torvalds { 23711da177e4SLinus Torvalds .procname = "gc_thresh", 23721da177e4SLinus Torvalds .data = &ipv4_dst_ops.gc_thresh, 23731da177e4SLinus Torvalds .maxlen = sizeof(int), 23741da177e4SLinus Torvalds .mode = 0644, 23756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 23761da177e4SLinus Torvalds }, 23771da177e4SLinus Torvalds { 23781da177e4SLinus Torvalds .procname = "max_size", 23791da177e4SLinus Torvalds .data = &ip_rt_max_size, 23801da177e4SLinus Torvalds .maxlen = sizeof(int), 23811da177e4SLinus Torvalds .mode = 0644, 23826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 23831da177e4SLinus Torvalds }, 23841da177e4SLinus Torvalds { 23851da177e4SLinus Torvalds /* Deprecated. Use gc_min_interval_ms */ 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds .procname = "gc_min_interval", 23881da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 23891da177e4SLinus Torvalds .maxlen = sizeof(int), 23901da177e4SLinus Torvalds .mode = 0644, 23916d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 23921da177e4SLinus Torvalds }, 23931da177e4SLinus Torvalds { 23941da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 23951da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 23961da177e4SLinus Torvalds .maxlen = sizeof(int), 23971da177e4SLinus Torvalds .mode = 0644, 23986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 23991da177e4SLinus Torvalds }, 24001da177e4SLinus Torvalds { 24011da177e4SLinus Torvalds .procname = "gc_timeout", 24021da177e4SLinus Torvalds .data = &ip_rt_gc_timeout, 24031da177e4SLinus Torvalds .maxlen = sizeof(int), 24041da177e4SLinus Torvalds .mode = 0644, 24056d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 24061da177e4SLinus Torvalds }, 24071da177e4SLinus Torvalds { 24089f28a2fcSEric Dumazet .procname = "gc_interval", 24099f28a2fcSEric Dumazet .data = &ip_rt_gc_interval, 24109f28a2fcSEric Dumazet .maxlen = sizeof(int), 24119f28a2fcSEric Dumazet .mode = 0644, 24129f28a2fcSEric Dumazet .proc_handler = proc_dointvec_jiffies, 24139f28a2fcSEric Dumazet }, 24149f28a2fcSEric Dumazet { 24151da177e4SLinus Torvalds .procname = "redirect_load", 24161da177e4SLinus Torvalds .data = &ip_rt_redirect_load, 24171da177e4SLinus Torvalds .maxlen = sizeof(int), 24181da177e4SLinus Torvalds .mode = 0644, 24196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24201da177e4SLinus Torvalds }, 24211da177e4SLinus Torvalds { 24221da177e4SLinus Torvalds .procname = "redirect_number", 24231da177e4SLinus Torvalds .data = &ip_rt_redirect_number, 24241da177e4SLinus Torvalds .maxlen = sizeof(int), 24251da177e4SLinus Torvalds .mode = 0644, 24266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24271da177e4SLinus Torvalds }, 24281da177e4SLinus Torvalds { 24291da177e4SLinus Torvalds .procname = "redirect_silence", 24301da177e4SLinus Torvalds .data = &ip_rt_redirect_silence, 24311da177e4SLinus Torvalds .maxlen = sizeof(int), 24321da177e4SLinus Torvalds .mode = 0644, 24336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24341da177e4SLinus Torvalds }, 24351da177e4SLinus Torvalds { 24361da177e4SLinus Torvalds .procname = "error_cost", 24371da177e4SLinus Torvalds .data = &ip_rt_error_cost, 24381da177e4SLinus Torvalds .maxlen = sizeof(int), 24391da177e4SLinus Torvalds .mode = 0644, 24406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24411da177e4SLinus Torvalds }, 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds .procname = "error_burst", 24441da177e4SLinus Torvalds .data = &ip_rt_error_burst, 24451da177e4SLinus Torvalds .maxlen = sizeof(int), 24461da177e4SLinus Torvalds .mode = 0644, 24476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24481da177e4SLinus Torvalds }, 24491da177e4SLinus Torvalds { 24501da177e4SLinus Torvalds .procname = "gc_elasticity", 24511da177e4SLinus Torvalds .data = &ip_rt_gc_elasticity, 24521da177e4SLinus Torvalds .maxlen = sizeof(int), 24531da177e4SLinus Torvalds .mode = 0644, 24546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24551da177e4SLinus Torvalds }, 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds .procname = "mtu_expires", 24581da177e4SLinus Torvalds .data = &ip_rt_mtu_expires, 24591da177e4SLinus Torvalds .maxlen = sizeof(int), 24601da177e4SLinus Torvalds .mode = 0644, 24616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 24621da177e4SLinus Torvalds }, 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds .procname = "min_pmtu", 24651da177e4SLinus Torvalds .data = &ip_rt_min_pmtu, 24661da177e4SLinus Torvalds .maxlen = sizeof(int), 24671da177e4SLinus Torvalds .mode = 0644, 24686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24691da177e4SLinus Torvalds }, 24701da177e4SLinus Torvalds { 24711da177e4SLinus Torvalds .procname = "min_adv_mss", 24721da177e4SLinus Torvalds .data = &ip_rt_min_advmss, 24731da177e4SLinus Torvalds .maxlen = sizeof(int), 24741da177e4SLinus Torvalds .mode = 0644, 24756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24761da177e4SLinus Torvalds }, 2477f8572d8fSEric W. Biederman { } 24781da177e4SLinus Torvalds }; 247939a23e75SDenis V. Lunev 248039a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = { 248139a23e75SDenis V. Lunev { 248239a23e75SDenis V. Lunev .procname = "flush", 248339a23e75SDenis V. Lunev .maxlen = sizeof(int), 248439a23e75SDenis V. Lunev .mode = 0200, 24856d9f239aSAlexey Dobriyan .proc_handler = ipv4_sysctl_rtcache_flush, 248639a23e75SDenis V. Lunev }, 2487f8572d8fSEric W. Biederman { }, 248839a23e75SDenis V. Lunev }; 248939a23e75SDenis V. Lunev 249039a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net) 249139a23e75SDenis V. Lunev { 249239a23e75SDenis V. Lunev struct ctl_table *tbl; 249339a23e75SDenis V. Lunev 249439a23e75SDenis V. Lunev tbl = ipv4_route_flush_table; 249509ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 249639a23e75SDenis V. Lunev tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL); 249739a23e75SDenis V. Lunev if (tbl == NULL) 249839a23e75SDenis V. Lunev goto err_dup; 249939a23e75SDenis V. Lunev } 250039a23e75SDenis V. Lunev tbl[0].extra1 = net; 250139a23e75SDenis V. Lunev 2502ec8f23ceSEric W. Biederman net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl); 250339a23e75SDenis V. Lunev if (net->ipv4.route_hdr == NULL) 250439a23e75SDenis V. Lunev goto err_reg; 250539a23e75SDenis V. Lunev return 0; 250639a23e75SDenis V. Lunev 250739a23e75SDenis V. Lunev err_reg: 250839a23e75SDenis V. Lunev if (tbl != ipv4_route_flush_table) 250939a23e75SDenis V. Lunev kfree(tbl); 251039a23e75SDenis V. Lunev err_dup: 251139a23e75SDenis V. Lunev return -ENOMEM; 251239a23e75SDenis V. Lunev } 251339a23e75SDenis V. Lunev 251439a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net) 251539a23e75SDenis V. Lunev { 251639a23e75SDenis V. Lunev struct ctl_table *tbl; 251739a23e75SDenis V. Lunev 251839a23e75SDenis V. Lunev tbl = net->ipv4.route_hdr->ctl_table_arg; 251939a23e75SDenis V. Lunev unregister_net_sysctl_table(net->ipv4.route_hdr); 252039a23e75SDenis V. Lunev BUG_ON(tbl == ipv4_route_flush_table); 252139a23e75SDenis V. Lunev kfree(tbl); 252239a23e75SDenis V. Lunev } 252339a23e75SDenis V. Lunev 252439a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = { 252539a23e75SDenis V. Lunev .init = sysctl_route_net_init, 252639a23e75SDenis V. Lunev .exit = sysctl_route_net_exit, 252739a23e75SDenis V. Lunev }; 25281da177e4SLinus Torvalds #endif 25291da177e4SLinus Torvalds 25303ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net) 25319f5e97e5SDenis V. Lunev { 2532b42664f8SNicolas Dichtel atomic_set(&net->rt_genid, 0); 2533436c3b66SDavid S. Miller get_random_bytes(&net->ipv4.dev_addr_genid, 2534436c3b66SDavid S. Miller sizeof(net->ipv4.dev_addr_genid)); 25359f5e97e5SDenis V. Lunev return 0; 25369f5e97e5SDenis V. Lunev } 25379f5e97e5SDenis V. Lunev 25383ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = { 25393ee94372SNeil Horman .init = rt_genid_init, 25409f5e97e5SDenis V. Lunev }; 25419f5e97e5SDenis V. Lunev 2542c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net) 2543c3426b47SDavid S. Miller { 2544c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 2545c3426b47SDavid S. Miller 2546c3426b47SDavid S. Miller if (!bp) 2547c3426b47SDavid S. Miller return -ENOMEM; 2548c3426b47SDavid S. Miller inet_peer_base_init(bp); 2549c3426b47SDavid S. Miller net->ipv4.peers = bp; 2550c3426b47SDavid S. Miller return 0; 2551c3426b47SDavid S. Miller } 2552c3426b47SDavid S. Miller 2553c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net) 2554c3426b47SDavid S. Miller { 2555c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv4.peers; 2556c3426b47SDavid S. Miller 2557c3426b47SDavid S. Miller net->ipv4.peers = NULL; 255856a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 2559c3426b47SDavid S. Miller kfree(bp); 2560c3426b47SDavid S. Miller } 2561c3426b47SDavid S. Miller 2562c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = { 2563c3426b47SDavid S. Miller .init = ipv4_inetpeer_init, 2564c3426b47SDavid S. Miller .exit = ipv4_inetpeer_exit, 2565c3426b47SDavid S. Miller }; 25669f5e97e5SDenis V. Lunev 2567c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 25687d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; 2569c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */ 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds int __init ip_rt_init(void) 25721da177e4SLinus Torvalds { 2573424c4b70SEric Dumazet int rc = 0; 25741da177e4SLinus Torvalds 2575c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 25760dcec8c2SIngo Molnar ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct)); 25771da177e4SLinus Torvalds if (!ip_rt_acct) 25781da177e4SLinus Torvalds panic("IP: failed to allocate ip_rt_acct\n"); 25791da177e4SLinus Torvalds #endif 25801da177e4SLinus Torvalds 2581e5d679f3SAlexey Dobriyan ipv4_dst_ops.kmem_cachep = 2582e5d679f3SAlexey Dobriyan kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0, 258320c2df83SPaul Mundt SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 25841da177e4SLinus Torvalds 258514e50e57SDavid S. Miller ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep; 258614e50e57SDavid S. Miller 2587fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_ops) < 0) 2588fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_ops counter\n"); 2589fc66f95cSEric Dumazet 2590fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0) 2591fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n"); 2592fc66f95cSEric Dumazet 259389aef892SDavid S. Miller ipv4_dst_ops.gc_thresh = ~0; 259489aef892SDavid S. Miller ip_rt_max_size = INT_MAX; 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds devinet_init(); 25971da177e4SLinus Torvalds ip_fib_init(); 25981da177e4SLinus Torvalds 259973b38711SDenis V. Lunev if (ip_rt_proc_init()) 2600058bd4d2SJoe Perches pr_err("Unable to create route proc files\n"); 26011da177e4SLinus Torvalds #ifdef CONFIG_XFRM 26021da177e4SLinus Torvalds xfrm_init(); 2603703fb94eSSteffen Klassert xfrm4_init(); 26041da177e4SLinus Torvalds #endif 2605c7ac8679SGreg Rose rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL); 260663f3444fSThomas Graf 260739a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL 260839a23e75SDenis V. Lunev register_pernet_subsys(&sysctl_route_ops); 260939a23e75SDenis V. Lunev #endif 26103ee94372SNeil Horman register_pernet_subsys(&rt_genid_ops); 2611c3426b47SDavid S. Miller register_pernet_subsys(&ipv4_inetpeer_ops); 26121da177e4SLinus Torvalds return rc; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 2615a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL 2616eeb61f71SAl Viro /* 2617eeb61f71SAl Viro * We really need to sanitize the damn ipv4 init order, then all 2618eeb61f71SAl Viro * this nonsense will go away. 2619eeb61f71SAl Viro */ 2620eeb61f71SAl Viro void __init ip_static_sysctl_init(void) 2621eeb61f71SAl Viro { 26224e5ca785SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table); 2623eeb61f71SAl Viro } 2624a1bc6eb4SAl Viro #endif 2625