11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * ROUTE - implementation of the IP router. 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 111da177e4SLinus Torvalds * Linus Torvalds, <Linus.Torvalds@helsinki.fi> 121da177e4SLinus Torvalds * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Fixes: 151da177e4SLinus Torvalds * Alan Cox : Verify area fixes. 161da177e4SLinus Torvalds * Alan Cox : cli() protects routing changes 171da177e4SLinus Torvalds * Rui Oliveira : ICMP routing table updates 181da177e4SLinus Torvalds * (rco@di.uminho.pt) Routing table insertion and update 191da177e4SLinus Torvalds * Linus Torvalds : Rewrote bits to be sensible 201da177e4SLinus Torvalds * Alan Cox : Added BSD route gw semantics 211da177e4SLinus Torvalds * Alan Cox : Super /proc >4K 221da177e4SLinus Torvalds * Alan Cox : MTU in route table 231da177e4SLinus Torvalds * Alan Cox : MSS actually. Also added the window 241da177e4SLinus Torvalds * clamper. 251da177e4SLinus Torvalds * Sam Lantinga : Fixed route matching in rt_del() 261da177e4SLinus Torvalds * Alan Cox : Routing cache support. 271da177e4SLinus Torvalds * Alan Cox : Removed compatibility cruft. 281da177e4SLinus Torvalds * Alan Cox : RTF_REJECT support. 291da177e4SLinus Torvalds * Alan Cox : TCP irtt support. 301da177e4SLinus Torvalds * Jonathan Naylor : Added Metric support. 311da177e4SLinus Torvalds * Miquel van Smoorenburg : BSD API fixes. 321da177e4SLinus Torvalds * Miquel van Smoorenburg : Metrics. 331da177e4SLinus Torvalds * Alan Cox : Use __u32 properly 341da177e4SLinus Torvalds * Alan Cox : Aligned routing errors more closely with BSD 351da177e4SLinus Torvalds * our system is still very different. 361da177e4SLinus Torvalds * Alan Cox : Faster /proc handling 371da177e4SLinus Torvalds * Alexey Kuznetsov : Massive rework to support tree based routing, 381da177e4SLinus Torvalds * routing caches and better behaviour. 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * Olaf Erb : irtt wasn't being copied right. 411da177e4SLinus Torvalds * Bjorn Ekwall : Kerneld route support. 421da177e4SLinus Torvalds * Alan Cox : Multicast fixed (I hope) 431da177e4SLinus Torvalds * Pavel Krauz : Limited broadcast fixed 441da177e4SLinus Torvalds * Mike McLagan : Routing by source 451da177e4SLinus Torvalds * Alexey Kuznetsov : End of old history. Split to fib.c and 461da177e4SLinus Torvalds * route.c and rewritten from scratch. 471da177e4SLinus Torvalds * Andi Kleen : Load-limit warning messages. 481da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 491da177e4SLinus Torvalds * Vitaly E. Lavrov : Race condition in ip_route_input_slow. 501da177e4SLinus Torvalds * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow. 511da177e4SLinus Torvalds * Vladimir V. Ivanov : IP rule info (flowid) is really useful. 521da177e4SLinus Torvalds * Marc Boucher : routing by fwmark 531da177e4SLinus Torvalds * Robert Olsson : Added rt_cache statistics 541da177e4SLinus Torvalds * Arnaldo C. Melo : Convert proc stuff to seq_file 55bb1d23b0SEric Dumazet * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes. 56cef2685eSIlia Sotnikov * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect 57cef2685eSIlia Sotnikov * Ilia Sotnikov : Removed TOS from hash calculations 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 601da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 611da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 621da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds 65afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 66afd46503SJoe Perches 671da177e4SLinus Torvalds #include <linux/module.h> 681da177e4SLinus Torvalds #include <asm/uaccess.h> 691da177e4SLinus Torvalds #include <linux/bitops.h> 701da177e4SLinus Torvalds #include <linux/types.h> 711da177e4SLinus Torvalds #include <linux/kernel.h> 721da177e4SLinus Torvalds #include <linux/mm.h> 73424c4b70SEric Dumazet #include <linux/bootmem.h> 741da177e4SLinus Torvalds #include <linux/string.h> 751da177e4SLinus Torvalds #include <linux/socket.h> 761da177e4SLinus Torvalds #include <linux/sockios.h> 771da177e4SLinus Torvalds #include <linux/errno.h> 781da177e4SLinus Torvalds #include <linux/in.h> 791da177e4SLinus Torvalds #include <linux/inet.h> 801da177e4SLinus Torvalds #include <linux/netdevice.h> 811da177e4SLinus Torvalds #include <linux/proc_fs.h> 821da177e4SLinus Torvalds #include <linux/init.h> 8339c90eceSEric Dumazet #include <linux/workqueue.h> 841da177e4SLinus Torvalds #include <linux/skbuff.h> 851da177e4SLinus Torvalds #include <linux/inetdevice.h> 861da177e4SLinus Torvalds #include <linux/igmp.h> 871da177e4SLinus Torvalds #include <linux/pkt_sched.h> 881da177e4SLinus Torvalds #include <linux/mroute.h> 891da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 901da177e4SLinus Torvalds #include <linux/random.h> 911da177e4SLinus Torvalds #include <linux/jhash.h> 921da177e4SLinus Torvalds #include <linux/rcupdate.h> 931da177e4SLinus Torvalds #include <linux/times.h> 945a0e3ad6STejun Heo #include <linux/slab.h> 95b9eda06fSStephen Rothwell #include <linux/prefetch.h> 96352e512cSHerbert Xu #include <net/dst.h> 97457c4cbcSEric W. Biederman #include <net/net_namespace.h> 981da177e4SLinus Torvalds #include <net/protocol.h> 991da177e4SLinus Torvalds #include <net/ip.h> 1001da177e4SLinus Torvalds #include <net/route.h> 1011da177e4SLinus Torvalds #include <net/inetpeer.h> 1021da177e4SLinus Torvalds #include <net/sock.h> 1031da177e4SLinus Torvalds #include <net/ip_fib.h> 1041da177e4SLinus Torvalds #include <net/arp.h> 1051da177e4SLinus Torvalds #include <net/tcp.h> 1061da177e4SLinus Torvalds #include <net/icmp.h> 1071da177e4SLinus Torvalds #include <net/xfrm.h> 1088d71740cSTom Tucker #include <net/netevent.h> 10963f3444fSThomas Graf #include <net/rtnetlink.h> 1101da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1111da177e4SLinus Torvalds #include <linux/sysctl.h> 1127426a564SShan Wei #include <linux/kmemleak.h> 1131da177e4SLinus Torvalds #endif 1146e5714eaSDavid S. Miller #include <net/secure_seq.h> 1151da177e4SLinus Torvalds 11668a5e3ddSDavid S. Miller #define RT_FL_TOS(oldflp4) \ 117f61759e6SJulian Anastasov ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK)) 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds #define IP_MAX_MTU 0xFFF0 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ) 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds static int ip_rt_max_size; 124817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT; 1259f28a2fcSEric Dumazet static int ip_rt_gc_interval __read_mostly = 60 * HZ; 126817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly = HZ / 2; 127817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly = 9; 128817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly = HZ / 50; 129817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1)); 130817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly = HZ; 131817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly = 5 * HZ; 132817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly = 8; 133817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ; 134817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20; 135817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly = 256; 1369f28a2fcSEric Dumazet 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * Interface to generic destination cache. 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie); 1420dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst); 143ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst); 1441da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst); 1451da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst); 1461da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb); 1476700c270SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 1486700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 1496700c270SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, 1506700c270SDavid S. Miller struct sk_buff *skb); 1511da177e4SLinus Torvalds 15272cdd1d9SEric Dumazet static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 15372cdd1d9SEric Dumazet int how) 15472cdd1d9SEric Dumazet { 15572cdd1d9SEric Dumazet } 1561da177e4SLinus Torvalds 15762fa8a84SDavid S. Miller static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old) 15862fa8a84SDavid S. Miller { 15931248731SDavid S. Miller WARN_ON(1); 16031248731SDavid S. Miller return NULL; 16162fa8a84SDavid S. Miller } 16262fa8a84SDavid S. Miller 163f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 164f894cbf8SDavid S. Miller struct sk_buff *skb, 165f894cbf8SDavid S. Miller const void *daddr); 166d3aaeb38SDavid S. Miller 1671da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = { 1681da177e4SLinus Torvalds .family = AF_INET, 16909640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 1701da177e4SLinus Torvalds .check = ipv4_dst_check, 1710dbaee3bSDavid S. Miller .default_advmss = ipv4_default_advmss, 172ebb762f2SSteffen Klassert .mtu = ipv4_mtu, 17362fa8a84SDavid S. Miller .cow_metrics = ipv4_cow_metrics, 1741da177e4SLinus Torvalds .destroy = ipv4_dst_destroy, 1751da177e4SLinus Torvalds .ifdown = ipv4_dst_ifdown, 1761da177e4SLinus Torvalds .negative_advice = ipv4_negative_advice, 1771da177e4SLinus Torvalds .link_failure = ipv4_link_failure, 1781da177e4SLinus Torvalds .update_pmtu = ip_rt_update_pmtu, 179e47a185bSDavid S. Miller .redirect = ip_do_redirect, 1801ac06e03SHerbert Xu .local_out = __ip_local_out, 181d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 1821da177e4SLinus Torvalds }; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds #define ECN_OR_COST(class) TC_PRIO_##class 1851da177e4SLinus Torvalds 1864839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = { 1871da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1884a2b9c37SDan Siemon ECN_OR_COST(BESTEFFORT), 1891da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1901da177e4SLinus Torvalds ECN_OR_COST(BESTEFFORT), 1911da177e4SLinus Torvalds TC_PRIO_BULK, 1921da177e4SLinus Torvalds ECN_OR_COST(BULK), 1931da177e4SLinus Torvalds TC_PRIO_BULK, 1941da177e4SLinus Torvalds ECN_OR_COST(BULK), 1951da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1961da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1971da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1981da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1991da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 2001da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK), 2011da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 2021da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK) 2031da177e4SLinus Torvalds }; 204d4a96865SAmir Vadai EXPORT_SYMBOL(ip_tos2prio); 2051da177e4SLinus Torvalds 2062f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 20727f39c73SEric Dumazet #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field) 2081da177e4SLinus Torvalds 209e84f84f2SDenis V. Lunev static inline int rt_genid(struct net *net) 210e84f84f2SDenis V. Lunev { 211e84f84f2SDenis V. Lunev return atomic_read(&net->ipv4.rt_genid); 212e84f84f2SDenis V. Lunev } 213e84f84f2SDenis V. Lunev 2141da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 2151da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 2161da177e4SLinus Torvalds { 21729e75252SEric Dumazet if (*pos) 21889aef892SDavid S. Miller return NULL; 21929e75252SEric Dumazet return SEQ_START_TOKEN; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds ++*pos; 22589aef892SDavid S. Miller return NULL; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2351da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 2361da177e4SLinus Torvalds "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t" 2371da177e4SLinus Torvalds "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t" 2381da177e4SLinus Torvalds "HHUptod\tSpecDst"); 2391da177e4SLinus Torvalds return 0; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 242f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = { 2431da177e4SLinus Torvalds .start = rt_cache_seq_start, 2441da177e4SLinus Torvalds .next = rt_cache_seq_next, 2451da177e4SLinus Torvalds .stop = rt_cache_seq_stop, 2461da177e4SLinus Torvalds .show = rt_cache_seq_show, 2471da177e4SLinus Torvalds }; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file) 2501da177e4SLinus Torvalds { 25189aef892SDavid S. Miller return seq_open(file, &rt_cache_seq_ops); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2549a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = { 2551da177e4SLinus Torvalds .owner = THIS_MODULE, 2561da177e4SLinus Torvalds .open = rt_cache_seq_open, 2571da177e4SLinus Torvalds .read = seq_read, 2581da177e4SLinus Torvalds .llseek = seq_lseek, 25989aef892SDavid S. Miller .release = seq_release, 2601da177e4SLinus Torvalds }; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds int cpu; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (*pos == 0) 2681da177e4SLinus Torvalds return SEQ_START_TOKEN; 2691da177e4SLinus Torvalds 2700f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 2711da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2721da177e4SLinus Torvalds continue; 2731da177e4SLinus Torvalds *pos = cpu+1; 2742f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds return NULL; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds int cpu; 2821da177e4SLinus Torvalds 2830f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 2841da177e4SLinus Torvalds if (!cpu_possible(cpu)) 2851da177e4SLinus Torvalds continue; 2861da177e4SLinus Torvalds *pos = cpu+1; 2872f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds return NULL; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds struct rt_cache_stat *st = v; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 3035bec0039SOlaf 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"); 3041da177e4SLinus Torvalds return 0; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x " 3081da177e4SLinus Torvalds " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n", 309fc66f95cSEric Dumazet dst_entries_get_slow(&ipv4_dst_ops), 3101da177e4SLinus Torvalds st->in_hit, 3111da177e4SLinus Torvalds st->in_slow_tot, 3121da177e4SLinus Torvalds st->in_slow_mc, 3131da177e4SLinus Torvalds st->in_no_route, 3141da177e4SLinus Torvalds st->in_brd, 3151da177e4SLinus Torvalds st->in_martian_dst, 3161da177e4SLinus Torvalds st->in_martian_src, 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds st->out_hit, 3191da177e4SLinus Torvalds st->out_slow_tot, 3201da177e4SLinus Torvalds st->out_slow_mc, 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds st->gc_total, 3231da177e4SLinus Torvalds st->gc_ignored, 3241da177e4SLinus Torvalds st->gc_goal_miss, 3251da177e4SLinus Torvalds st->gc_dst_overflow, 3261da177e4SLinus Torvalds st->in_hlist_search, 3271da177e4SLinus Torvalds st->out_hlist_search 3281da177e4SLinus Torvalds ); 3291da177e4SLinus Torvalds return 0; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = { 3331da177e4SLinus Torvalds .start = rt_cpu_seq_start, 3341da177e4SLinus Torvalds .next = rt_cpu_seq_next, 3351da177e4SLinus Torvalds .stop = rt_cpu_seq_stop, 3361da177e4SLinus Torvalds .show = rt_cpu_seq_show, 3371da177e4SLinus Torvalds }; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds return seq_open(file, &rt_cpu_seq_ops); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3459a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = { 3461da177e4SLinus Torvalds .owner = THIS_MODULE, 3471da177e4SLinus Torvalds .open = rt_cpu_seq_open, 3481da177e4SLinus Torvalds .read = seq_read, 3491da177e4SLinus Torvalds .llseek = seq_lseek, 3501da177e4SLinus Torvalds .release = seq_release, 3511da177e4SLinus Torvalds }; 3521da177e4SLinus Torvalds 353c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 354a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v) 35578c686e9SPavel Emelyanov { 356a661c419SAlexey Dobriyan struct ip_rt_acct *dst, *src; 357a661c419SAlexey Dobriyan unsigned int i, j; 35878c686e9SPavel Emelyanov 359a661c419SAlexey Dobriyan dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL); 360a661c419SAlexey Dobriyan if (!dst) 361a661c419SAlexey Dobriyan return -ENOMEM; 36278c686e9SPavel Emelyanov 363a661c419SAlexey Dobriyan for_each_possible_cpu(i) { 364a661c419SAlexey Dobriyan src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i); 365a661c419SAlexey Dobriyan for (j = 0; j < 256; j++) { 366a661c419SAlexey Dobriyan dst[j].o_bytes += src[j].o_bytes; 367a661c419SAlexey Dobriyan dst[j].o_packets += src[j].o_packets; 368a661c419SAlexey Dobriyan dst[j].i_bytes += src[j].i_bytes; 369a661c419SAlexey Dobriyan dst[j].i_packets += src[j].i_packets; 370a661c419SAlexey Dobriyan } 371a661c419SAlexey Dobriyan } 372a661c419SAlexey Dobriyan 373a661c419SAlexey Dobriyan seq_write(m, dst, 256 * sizeof(struct ip_rt_acct)); 374a661c419SAlexey Dobriyan kfree(dst); 37578c686e9SPavel Emelyanov return 0; 37678c686e9SPavel Emelyanov } 37778c686e9SPavel Emelyanov 378a661c419SAlexey Dobriyan static int rt_acct_proc_open(struct inode *inode, struct file *file) 379a661c419SAlexey Dobriyan { 380a661c419SAlexey Dobriyan return single_open(file, rt_acct_proc_show, NULL); 38178c686e9SPavel Emelyanov } 38278c686e9SPavel Emelyanov 383a661c419SAlexey Dobriyan static const struct file_operations rt_acct_proc_fops = { 384a661c419SAlexey Dobriyan .owner = THIS_MODULE, 385a661c419SAlexey Dobriyan .open = rt_acct_proc_open, 386a661c419SAlexey Dobriyan .read = seq_read, 387a661c419SAlexey Dobriyan .llseek = seq_lseek, 388a661c419SAlexey Dobriyan .release = single_release, 389a661c419SAlexey Dobriyan }; 39078c686e9SPavel Emelyanov #endif 391107f1634SPavel Emelyanov 39273b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net) 393107f1634SPavel Emelyanov { 394107f1634SPavel Emelyanov struct proc_dir_entry *pde; 395107f1634SPavel Emelyanov 396107f1634SPavel Emelyanov pde = proc_net_fops_create(net, "rt_cache", S_IRUGO, 397107f1634SPavel Emelyanov &rt_cache_seq_fops); 398107f1634SPavel Emelyanov if (!pde) 399107f1634SPavel Emelyanov goto err1; 400107f1634SPavel Emelyanov 40177020720SWang Chen pde = proc_create("rt_cache", S_IRUGO, 40277020720SWang Chen net->proc_net_stat, &rt_cpu_seq_fops); 403107f1634SPavel Emelyanov if (!pde) 404107f1634SPavel Emelyanov goto err2; 405107f1634SPavel Emelyanov 406c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 407a661c419SAlexey Dobriyan pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops); 408107f1634SPavel Emelyanov if (!pde) 409107f1634SPavel Emelyanov goto err3; 410107f1634SPavel Emelyanov #endif 411107f1634SPavel Emelyanov return 0; 412107f1634SPavel Emelyanov 413c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 414107f1634SPavel Emelyanov err3: 415107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net_stat); 416107f1634SPavel Emelyanov #endif 417107f1634SPavel Emelyanov err2: 418107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net); 419107f1634SPavel Emelyanov err1: 420107f1634SPavel Emelyanov return -ENOMEM; 421107f1634SPavel Emelyanov } 42273b38711SDenis V. Lunev 42373b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net) 42473b38711SDenis V. Lunev { 42573b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net_stat); 42673b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net); 427c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 42873b38711SDenis V. Lunev remove_proc_entry("rt_acct", net->proc_net); 4290a931acfSAlexey Dobriyan #endif 43073b38711SDenis V. Lunev } 43173b38711SDenis V. Lunev 43273b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata = { 43373b38711SDenis V. Lunev .init = ip_rt_do_proc_init, 43473b38711SDenis V. Lunev .exit = ip_rt_do_proc_exit, 43573b38711SDenis V. Lunev }; 43673b38711SDenis V. Lunev 43773b38711SDenis V. Lunev static int __init ip_rt_proc_init(void) 43873b38711SDenis V. Lunev { 43973b38711SDenis V. Lunev return register_pernet_subsys(&ip_rt_proc_ops); 44073b38711SDenis V. Lunev } 44173b38711SDenis V. Lunev 442107f1634SPavel Emelyanov #else 44373b38711SDenis V. Lunev static inline int ip_rt_proc_init(void) 444107f1634SPavel Emelyanov { 445107f1634SPavel Emelyanov return 0; 446107f1634SPavel Emelyanov } 4471da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 4481da177e4SLinus Torvalds 449e84f84f2SDenis V. Lunev static inline int rt_is_expired(struct rtable *rth) 450e84f84f2SDenis V. Lunev { 451d8d1f30bSChangli Gao return rth->rt_genid != rt_genid(dev_net(rth->dst.dev)); 452e84f84f2SDenis V. Lunev } 453e84f84f2SDenis V. Lunev 454beb659bdSEric Dumazet /* 45525985edcSLucas De Marchi * Perturbation of rt_genid by a small quantity [1..256] 45629e75252SEric Dumazet * Using 8 bits of shuffling ensure we can call rt_cache_invalidate() 45729e75252SEric Dumazet * many times (2^24) without giving recent rt_genid. 45829e75252SEric Dumazet * Jenkins hash is strong enough that litle changes of rt_genid are OK. 4591da177e4SLinus Torvalds */ 46086c657f6SDenis V. Lunev static void rt_cache_invalidate(struct net *net) 4611da177e4SLinus Torvalds { 46229e75252SEric Dumazet unsigned char shuffle; 4631da177e4SLinus Torvalds 46429e75252SEric Dumazet get_random_bytes(&shuffle, sizeof(shuffle)); 465e84f84f2SDenis V. Lunev atomic_add(shuffle + 1U, &net->ipv4.rt_genid); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468beb659bdSEric Dumazet /* 46929e75252SEric Dumazet * delay < 0 : invalidate cache (fast : entries will be deleted later) 47029e75252SEric Dumazet * delay >= 0 : invalidate & flush cache (can be long) 47129e75252SEric Dumazet */ 47276e6ebfbSDenis V. Lunev void rt_cache_flush(struct net *net, int delay) 47329e75252SEric Dumazet { 47486c657f6SDenis V. Lunev rt_cache_invalidate(net); 47598376387SEric Dumazet } 47698376387SEric Dumazet 477f894cbf8SDavid S. Miller static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 478f894cbf8SDavid S. Miller struct sk_buff *skb, 479f894cbf8SDavid S. Miller const void *daddr) 4803769cffbSDavid Miller { 481d3aaeb38SDavid S. Miller struct net_device *dev = dst->dev; 482d3aaeb38SDavid S. Miller const __be32 *pkey = daddr; 48339232973SDavid S. Miller const struct rtable *rt; 4843769cffbSDavid Miller struct neighbour *n; 4853769cffbSDavid Miller 48639232973SDavid S. Miller rt = (const struct rtable *) dst; 487a263b309SDavid S. Miller if (rt->rt_gateway) 48839232973SDavid S. Miller pkey = (const __be32 *) &rt->rt_gateway; 489f894cbf8SDavid S. Miller else if (skb) 490f894cbf8SDavid S. Miller pkey = &ip_hdr(skb)->daddr; 491d3aaeb38SDavid S. Miller 49280703d26SDavid S. Miller n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey); 493d3aaeb38SDavid S. Miller if (n) 494d3aaeb38SDavid S. Miller return n; 49532092ecfSDavid Miller return neigh_create(&arp_tbl, pkey, dev); 496d3aaeb38SDavid S. Miller } 497d3aaeb38SDavid S. Miller 4981da177e4SLinus Torvalds /* 4991da177e4SLinus Torvalds * Peer allocation may fail only in serious out-of-memory conditions. However 5001da177e4SLinus Torvalds * we still can generate some output. 5011da177e4SLinus Torvalds * Random ID selection looks a bit dangerous because we have no chances to 5021da177e4SLinus Torvalds * select ID being unique in a reasonable period of time. 5031da177e4SLinus Torvalds * But broken packet identifier may be better than no packet at all. 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds static DEFINE_SPINLOCK(ip_fb_id_lock); 5081da177e4SLinus Torvalds static u32 ip_fallback_id; 5091da177e4SLinus Torvalds u32 salt; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds spin_lock_bh(&ip_fb_id_lock); 512e448515cSAl Viro salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr); 5131da177e4SLinus Torvalds iph->id = htons(salt & 0xFFFF); 5141da177e4SLinus Torvalds ip_fallback_id = salt; 5151da177e4SLinus Torvalds spin_unlock_bh(&ip_fb_id_lock); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more) 5191da177e4SLinus Torvalds { 5201d861aa4SDavid S. Miller struct net *net = dev_net(dst->dev); 5211d861aa4SDavid S. Miller struct inet_peer *peer; 5221da177e4SLinus Torvalds 5231d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, iph->daddr, 1); 524fbfe95a4SDavid S. Miller if (peer) { 525fbfe95a4SDavid S. Miller iph->id = htons(inet_getid(peer, more)); 5261d861aa4SDavid S. Miller inet_putpeer(peer); 5271da177e4SLinus Torvalds return; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds ip_select_fb_ident(iph); 5311da177e4SLinus Torvalds } 5324bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident); 5331da177e4SLinus Torvalds 5345abf7f7eSEric Dumazet static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk, 5354895c771SDavid S. Miller const struct iphdr *iph, 5364895c771SDavid S. Miller int oif, u8 tos, 5374895c771SDavid S. Miller u8 prot, u32 mark, int flow_flags) 5384895c771SDavid S. Miller { 5394895c771SDavid S. Miller if (sk) { 5404895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk); 5414895c771SDavid S. Miller 5424895c771SDavid S. Miller oif = sk->sk_bound_dev_if; 5434895c771SDavid S. Miller mark = sk->sk_mark; 5444895c771SDavid S. Miller tos = RT_CONN_FLAGS(sk); 5454895c771SDavid S. Miller prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol; 5464895c771SDavid S. Miller } 5474895c771SDavid S. Miller flowi4_init_output(fl4, oif, mark, tos, 5484895c771SDavid S. Miller RT_SCOPE_UNIVERSE, prot, 5494895c771SDavid S. Miller flow_flags, 5504895c771SDavid S. Miller iph->daddr, iph->saddr, 0, 0); 5514895c771SDavid S. Miller } 5524895c771SDavid S. Miller 5535abf7f7eSEric Dumazet static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb, 5545abf7f7eSEric Dumazet const struct sock *sk) 5554895c771SDavid S. Miller { 5564895c771SDavid S. Miller const struct iphdr *iph = ip_hdr(skb); 5574895c771SDavid S. Miller int oif = skb->dev->ifindex; 5584895c771SDavid S. Miller u8 tos = RT_TOS(iph->tos); 5594895c771SDavid S. Miller u8 prot = iph->protocol; 5604895c771SDavid S. Miller u32 mark = skb->mark; 5614895c771SDavid S. Miller 5624895c771SDavid S. Miller __build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0); 5634895c771SDavid S. Miller } 5644895c771SDavid S. Miller 5655abf7f7eSEric Dumazet static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk) 5664895c771SDavid S. Miller { 5674895c771SDavid S. Miller const struct inet_sock *inet = inet_sk(sk); 5685abf7f7eSEric Dumazet const struct ip_options_rcu *inet_opt; 5694895c771SDavid S. Miller __be32 daddr = inet->inet_daddr; 5704895c771SDavid S. Miller 5714895c771SDavid S. Miller rcu_read_lock(); 5724895c771SDavid S. Miller inet_opt = rcu_dereference(inet->inet_opt); 5734895c771SDavid S. Miller if (inet_opt && inet_opt->opt.srr) 5744895c771SDavid S. Miller daddr = inet_opt->opt.faddr; 5754895c771SDavid S. Miller flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, 5764895c771SDavid S. Miller RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 5774895c771SDavid S. Miller inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol, 5784895c771SDavid S. Miller inet_sk_flowi_flags(sk), 5794895c771SDavid S. Miller daddr, inet->inet_saddr, 0, 0); 5804895c771SDavid S. Miller rcu_read_unlock(); 5814895c771SDavid S. Miller } 5824895c771SDavid S. Miller 5835abf7f7eSEric Dumazet static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk, 5845abf7f7eSEric Dumazet const struct sk_buff *skb) 5854895c771SDavid S. Miller { 5864895c771SDavid S. Miller if (skb) 5874895c771SDavid S. Miller build_skb_flow_key(fl4, skb, sk); 5884895c771SDavid S. Miller else 5894895c771SDavid S. Miller build_sk_flow_key(fl4, sk); 5904895c771SDavid S. Miller } 5914895c771SDavid S. Miller 592aee06da6SJulian Anastasov static DEFINE_SEQLOCK(fnhe_seqlock); 5934895c771SDavid S. Miller 594aee06da6SJulian Anastasov static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash) 5954895c771SDavid S. Miller { 5964895c771SDavid S. Miller struct fib_nh_exception *fnhe, *oldest; 5974895c771SDavid S. Miller 5984895c771SDavid S. Miller oldest = rcu_dereference(hash->chain); 5994895c771SDavid S. Miller for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe; 6004895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 6014895c771SDavid S. Miller if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) 6024895c771SDavid S. Miller oldest = fnhe; 6034895c771SDavid S. Miller } 6044895c771SDavid S. Miller return oldest; 6054895c771SDavid S. Miller } 6064895c771SDavid S. Miller 607d3a25c98SDavid S. Miller static inline u32 fnhe_hashfun(__be32 daddr) 608d3a25c98SDavid S. Miller { 609d3a25c98SDavid S. Miller u32 hval; 610d3a25c98SDavid S. Miller 611d3a25c98SDavid S. Miller hval = (__force u32) daddr; 612d3a25c98SDavid S. Miller hval ^= (hval >> 11) ^ (hval >> 22); 613d3a25c98SDavid S. Miller 614d3a25c98SDavid S. Miller return hval & (FNHE_HASH_SIZE - 1); 615d3a25c98SDavid S. Miller } 616d3a25c98SDavid S. Miller 617aee06da6SJulian Anastasov static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw, 618aee06da6SJulian Anastasov u32 pmtu, unsigned long expires) 6194895c771SDavid S. Miller { 620aee06da6SJulian Anastasov struct fnhe_hash_bucket *hash; 6214895c771SDavid S. Miller struct fib_nh_exception *fnhe; 6224895c771SDavid S. Miller int depth; 623aee06da6SJulian Anastasov u32 hval = fnhe_hashfun(daddr); 6244895c771SDavid S. Miller 625aee06da6SJulian Anastasov write_seqlock_bh(&fnhe_seqlock); 626aee06da6SJulian Anastasov 627aee06da6SJulian Anastasov hash = nh->nh_exceptions; 6284895c771SDavid S. Miller if (!hash) { 629aee06da6SJulian Anastasov hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC); 6304895c771SDavid S. Miller if (!hash) 631aee06da6SJulian Anastasov goto out_unlock; 632aee06da6SJulian Anastasov nh->nh_exceptions = hash; 6334895c771SDavid S. Miller } 6344895c771SDavid S. Miller 6354895c771SDavid S. Miller hash += hval; 6364895c771SDavid S. Miller 6374895c771SDavid S. Miller depth = 0; 6384895c771SDavid S. Miller for (fnhe = rcu_dereference(hash->chain); fnhe; 6394895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 6404895c771SDavid S. Miller if (fnhe->fnhe_daddr == daddr) 641aee06da6SJulian Anastasov break; 6424895c771SDavid S. Miller depth++; 6434895c771SDavid S. Miller } 6444895c771SDavid S. Miller 645aee06da6SJulian Anastasov if (fnhe) { 646aee06da6SJulian Anastasov if (gw) 647aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 648aee06da6SJulian Anastasov if (pmtu) { 649aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 650aee06da6SJulian Anastasov fnhe->fnhe_expires = expires; 6514895c771SDavid S. Miller } 652aee06da6SJulian Anastasov } else { 653aee06da6SJulian Anastasov if (depth > FNHE_RECLAIM_DEPTH) 654aee06da6SJulian Anastasov fnhe = fnhe_oldest(hash); 655aee06da6SJulian Anastasov else { 6564895c771SDavid S. Miller fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); 6574895c771SDavid S. Miller if (!fnhe) 658aee06da6SJulian Anastasov goto out_unlock; 6594895c771SDavid S. Miller 6604895c771SDavid S. Miller fnhe->fnhe_next = hash->chain; 6614895c771SDavid S. Miller rcu_assign_pointer(hash->chain, fnhe); 662aee06da6SJulian Anastasov } 6634895c771SDavid S. Miller fnhe->fnhe_daddr = daddr; 664aee06da6SJulian Anastasov fnhe->fnhe_gw = gw; 665aee06da6SJulian Anastasov fnhe->fnhe_pmtu = pmtu; 666aee06da6SJulian Anastasov fnhe->fnhe_expires = expires; 667aee06da6SJulian Anastasov } 668aee06da6SJulian Anastasov 6694895c771SDavid S. Miller fnhe->fnhe_stamp = jiffies; 670aee06da6SJulian Anastasov 671aee06da6SJulian Anastasov out_unlock: 672aee06da6SJulian Anastasov write_sequnlock_bh(&fnhe_seqlock); 673aee06da6SJulian Anastasov return; 6744895c771SDavid S. Miller } 6754895c771SDavid S. Miller 676ceb33206SDavid S. Miller static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4, 677ceb33206SDavid S. Miller bool kill_route) 6781da177e4SLinus Torvalds { 679e47a185bSDavid S. Miller __be32 new_gw = icmp_hdr(skb)->un.gateway; 68094206125SDavid S. Miller __be32 old_gw = ip_hdr(skb)->saddr; 681e47a185bSDavid S. Miller struct net_device *dev = skb->dev; 682e47a185bSDavid S. Miller struct in_device *in_dev; 6834895c771SDavid S. Miller struct fib_result res; 684e47a185bSDavid S. Miller struct neighbour *n; 685317805b8SDenis V. Lunev struct net *net; 6861da177e4SLinus Torvalds 68794206125SDavid S. Miller switch (icmp_hdr(skb)->code & 7) { 68894206125SDavid S. Miller case ICMP_REDIR_NET: 68994206125SDavid S. Miller case ICMP_REDIR_NETTOS: 69094206125SDavid S. Miller case ICMP_REDIR_HOST: 69194206125SDavid S. Miller case ICMP_REDIR_HOSTTOS: 69294206125SDavid S. Miller break; 69394206125SDavid S. Miller 69494206125SDavid S. Miller default: 69594206125SDavid S. Miller return; 69694206125SDavid S. Miller } 69794206125SDavid S. Miller 698e47a185bSDavid S. Miller if (rt->rt_gateway != old_gw) 699e47a185bSDavid S. Miller return; 700e47a185bSDavid S. Miller 701e47a185bSDavid S. Miller in_dev = __in_dev_get_rcu(dev); 702e47a185bSDavid S. Miller if (!in_dev) 703e47a185bSDavid S. Miller return; 704e47a185bSDavid S. Miller 705c346dca1SYOSHIFUJI Hideaki net = dev_net(dev); 7069d4fb27dSJoe Perches if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) || 7079d4fb27dSJoe Perches ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) || 7089d4fb27dSJoe Perches ipv4_is_zeronet(new_gw)) 7091da177e4SLinus Torvalds goto reject_redirect; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds if (!IN_DEV_SHARED_MEDIA(in_dev)) { 7121da177e4SLinus Torvalds if (!inet_addr_onlink(in_dev, new_gw, old_gw)) 7131da177e4SLinus Torvalds goto reject_redirect; 7141da177e4SLinus Torvalds if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev)) 7151da177e4SLinus Torvalds goto reject_redirect; 7161da177e4SLinus Torvalds } else { 717317805b8SDenis V. Lunev if (inet_addr_type(net, new_gw) != RTN_UNICAST) 7181da177e4SLinus Torvalds goto reject_redirect; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7214895c771SDavid S. Miller n = ipv4_neigh_lookup(&rt->dst, NULL, &new_gw); 722e47a185bSDavid S. Miller if (n) { 723e47a185bSDavid S. Miller if (!(n->nud_state & NUD_VALID)) { 724e47a185bSDavid S. Miller neigh_event_send(n, NULL); 725e47a185bSDavid S. Miller } else { 7264895c771SDavid S. Miller if (fib_lookup(net, fl4, &res) == 0) { 7274895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(res); 7284895c771SDavid S. Miller 729aee06da6SJulian Anastasov update_or_create_fnhe(nh, fl4->daddr, new_gw, 730aee06da6SJulian Anastasov 0, 0); 7314895c771SDavid S. Miller } 732ceb33206SDavid S. Miller if (kill_route) 733ceb33206SDavid S. Miller rt->dst.obsolete = DST_OBSOLETE_KILL; 734e47a185bSDavid S. Miller call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n); 735e47a185bSDavid S. Miller } 736e47a185bSDavid S. Miller neigh_release(n); 737e47a185bSDavid S. Miller } 738e47a185bSDavid S. Miller return; 739e47a185bSDavid S. Miller 740e47a185bSDavid S. Miller reject_redirect: 741e47a185bSDavid S. Miller #ifdef CONFIG_IP_ROUTE_VERBOSE 74299ee038dSDavid S. Miller if (IN_DEV_LOG_MARTIANS(in_dev)) { 74399ee038dSDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 74499ee038dSDavid S. Miller __be32 daddr = iph->daddr; 74599ee038dSDavid S. Miller __be32 saddr = iph->saddr; 74699ee038dSDavid S. Miller 747e47a185bSDavid S. Miller net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n" 748e47a185bSDavid S. Miller " Advised path = %pI4 -> %pI4\n", 749e47a185bSDavid S. Miller &old_gw, dev->name, &new_gw, 750e47a185bSDavid S. Miller &saddr, &daddr); 75199ee038dSDavid S. Miller } 752e47a185bSDavid S. Miller #endif 753e47a185bSDavid S. Miller ; 754e47a185bSDavid S. Miller } 755e47a185bSDavid S. Miller 7564895c771SDavid S. Miller static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 7574895c771SDavid S. Miller { 7584895c771SDavid S. Miller struct rtable *rt; 7594895c771SDavid S. Miller struct flowi4 fl4; 7604895c771SDavid S. Miller 7614895c771SDavid S. Miller rt = (struct rtable *) dst; 7624895c771SDavid S. Miller 7634895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb); 764ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, true); 7654895c771SDavid S. Miller } 7664895c771SDavid S. Miller 7671da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 7701da177e4SLinus Torvalds struct dst_entry *ret = dst; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds if (rt) { 773d11a4dc1STimo Teräs if (dst->obsolete > 0) { 7741da177e4SLinus Torvalds ip_rt_put(rt); 7751da177e4SLinus Torvalds ret = NULL; 7765943634fSDavid S. Miller } else if ((rt->rt_flags & RTCF_REDIRECTED) || 7775943634fSDavid S. Miller rt->dst.expires) { 77889aef892SDavid S. Miller ip_rt_put(rt); 7791da177e4SLinus Torvalds ret = NULL; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds return ret; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * Algorithm: 7871da177e4SLinus Torvalds * 1. The first ip_rt_redirect_number redirects are sent 7881da177e4SLinus Torvalds * with exponential backoff, then we stop sending them at all, 7891da177e4SLinus Torvalds * assuming that the host ignores our redirects. 7901da177e4SLinus Torvalds * 2. If we did not see packets requiring redirects 7911da177e4SLinus Torvalds * during ip_rt_redirect_silence, we assume that the host 7921da177e4SLinus Torvalds * forgot redirected route and start to send redirects again. 7931da177e4SLinus Torvalds * 7941da177e4SLinus Torvalds * This algorithm is much cheaper and more intelligent than dumb load limiting 7951da177e4SLinus Torvalds * in icmp.c. 7961da177e4SLinus Torvalds * 7971da177e4SLinus Torvalds * NOTE. Do not forget to inhibit load limiting for redirects (redundant) 7981da177e4SLinus Torvalds * and "frag. need" (breaks PMTU discovery) in icmp.c. 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb) 8021da177e4SLinus Torvalds { 803511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 80430038fc6SEric Dumazet struct in_device *in_dev; 80592d86829SDavid S. Miller struct inet_peer *peer; 8061d861aa4SDavid S. Miller struct net *net; 80730038fc6SEric Dumazet int log_martians; 8081da177e4SLinus Torvalds 80930038fc6SEric Dumazet rcu_read_lock(); 810d8d1f30bSChangli Gao in_dev = __in_dev_get_rcu(rt->dst.dev); 81130038fc6SEric Dumazet if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) { 81230038fc6SEric Dumazet rcu_read_unlock(); 8131da177e4SLinus Torvalds return; 81430038fc6SEric Dumazet } 81530038fc6SEric Dumazet log_martians = IN_DEV_LOG_MARTIANS(in_dev); 81630038fc6SEric Dumazet rcu_read_unlock(); 8171da177e4SLinus Torvalds 8181d861aa4SDavid S. Miller net = dev_net(rt->dst.dev); 8191d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1); 82092d86829SDavid S. Miller if (!peer) { 82192d86829SDavid S. Miller icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway); 82292d86829SDavid S. Miller return; 82392d86829SDavid S. Miller } 82492d86829SDavid S. Miller 8251da177e4SLinus Torvalds /* No redirected packets during ip_rt_redirect_silence; 8261da177e4SLinus Torvalds * reset the algorithm. 8271da177e4SLinus Torvalds */ 82892d86829SDavid S. Miller if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) 82992d86829SDavid S. Miller peer->rate_tokens = 0; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /* Too many ignored redirects; do not send anything 832d8d1f30bSChangli Gao * set dst.rate_last to the last seen redirected packet. 8331da177e4SLinus Torvalds */ 83492d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_redirect_number) { 83592d86829SDavid S. Miller peer->rate_last = jiffies; 8361d861aa4SDavid S. Miller goto out_put_peer; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* Check for load limit; set rate_last to the latest sent 8401da177e4SLinus Torvalds * redirect. 8411da177e4SLinus Torvalds */ 84292d86829SDavid S. Miller if (peer->rate_tokens == 0 || 84314fb8a76SLi Yewang time_after(jiffies, 84492d86829SDavid S. Miller (peer->rate_last + 84592d86829SDavid S. Miller (ip_rt_redirect_load << peer->rate_tokens)))) { 8461da177e4SLinus Torvalds icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway); 84792d86829SDavid S. Miller peer->rate_last = jiffies; 84892d86829SDavid S. Miller ++peer->rate_tokens; 8491da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 85030038fc6SEric Dumazet if (log_martians && 851e87cc472SJoe Perches peer->rate_tokens == ip_rt_redirect_number) 852e87cc472SJoe Perches net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n", 853c5be24ffSDavid S. Miller &ip_hdr(skb)->saddr, rt->rt_iif, 854f1ce3062SDavid S. Miller &ip_hdr(skb)->daddr, &rt->rt_gateway); 8551da177e4SLinus Torvalds #endif 8561da177e4SLinus Torvalds } 8571d861aa4SDavid S. Miller out_put_peer: 8581d861aa4SDavid S. Miller inet_putpeer(peer); 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb) 8621da177e4SLinus Torvalds { 863251da413SDavid S. Miller struct in_device *in_dev = __in_dev_get_rcu(skb->dev); 864511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 86592d86829SDavid S. Miller struct inet_peer *peer; 8661da177e4SLinus Torvalds unsigned long now; 867251da413SDavid S. Miller struct net *net; 86892d86829SDavid S. Miller bool send; 8691da177e4SLinus Torvalds int code; 8701da177e4SLinus Torvalds 871251da413SDavid S. Miller net = dev_net(rt->dst.dev); 872251da413SDavid S. Miller if (!IN_DEV_FORWARD(in_dev)) { 873251da413SDavid S. Miller switch (rt->dst.error) { 874251da413SDavid S. Miller case EHOSTUNREACH: 875251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS); 876251da413SDavid S. Miller break; 877251da413SDavid S. Miller 878251da413SDavid S. Miller case ENETUNREACH: 879251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES); 880251da413SDavid S. Miller break; 881251da413SDavid S. Miller } 882251da413SDavid S. Miller goto out; 883251da413SDavid S. Miller } 884251da413SDavid S. Miller 885d8d1f30bSChangli Gao switch (rt->dst.error) { 8861da177e4SLinus Torvalds case EINVAL: 8871da177e4SLinus Torvalds default: 8881da177e4SLinus Torvalds goto out; 8891da177e4SLinus Torvalds case EHOSTUNREACH: 8901da177e4SLinus Torvalds code = ICMP_HOST_UNREACH; 8911da177e4SLinus Torvalds break; 8921da177e4SLinus Torvalds case ENETUNREACH: 8931da177e4SLinus Torvalds code = ICMP_NET_UNREACH; 894251da413SDavid S. Miller IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES); 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds case EACCES: 8971da177e4SLinus Torvalds code = ICMP_PKT_FILTERED; 8981da177e4SLinus Torvalds break; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011d861aa4SDavid S. Miller peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1); 90292d86829SDavid S. Miller 90392d86829SDavid S. Miller send = true; 90492d86829SDavid S. Miller if (peer) { 9051da177e4SLinus Torvalds now = jiffies; 90692d86829SDavid S. Miller peer->rate_tokens += now - peer->rate_last; 90792d86829SDavid S. Miller if (peer->rate_tokens > ip_rt_error_burst) 90892d86829SDavid S. Miller peer->rate_tokens = ip_rt_error_burst; 90992d86829SDavid S. Miller peer->rate_last = now; 91092d86829SDavid S. Miller if (peer->rate_tokens >= ip_rt_error_cost) 91192d86829SDavid S. Miller peer->rate_tokens -= ip_rt_error_cost; 91292d86829SDavid S. Miller else 91392d86829SDavid S. Miller send = false; 9141d861aa4SDavid S. Miller inet_putpeer(peer); 9151da177e4SLinus Torvalds } 91692d86829SDavid S. Miller if (send) 91792d86829SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, code, 0); 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds out: kfree_skb(skb); 9201da177e4SLinus Torvalds return 0; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923ceb33206SDavid S. Miller static u32 __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu) 9241da177e4SLinus Torvalds { 9254895c771SDavid S. Miller struct fib_result res; 9262c8cec5cSDavid S. Miller 9272c8cec5cSDavid S. Miller if (mtu < ip_rt_min_pmtu) 9281da177e4SLinus Torvalds mtu = ip_rt_min_pmtu; 92946af3180SHiroaki SHIMODA 9304895c771SDavid S. Miller if (fib_lookup(dev_net(rt->dst.dev), fl4, &res) == 0) { 9314895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(res); 9324895c771SDavid S. Miller 933aee06da6SJulian Anastasov update_or_create_fnhe(nh, fl4->daddr, 0, mtu, 934aee06da6SJulian Anastasov jiffies + ip_rt_mtu_expires); 9354895c771SDavid S. Miller } 936ceb33206SDavid S. Miller return mtu; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9394895c771SDavid S. Miller static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 9404895c771SDavid S. Miller struct sk_buff *skb, u32 mtu) 9414895c771SDavid S. Miller { 9424895c771SDavid S. Miller struct rtable *rt = (struct rtable *) dst; 9434895c771SDavid S. Miller struct flowi4 fl4; 9444895c771SDavid S. Miller 9454895c771SDavid S. Miller ip_rt_build_flow_key(&fl4, sk, skb); 946ceb33206SDavid S. Miller mtu = __ip_rt_update_pmtu(rt, &fl4, mtu); 947ceb33206SDavid S. Miller 948ceb33206SDavid S. Miller if (!rt->rt_pmtu) { 949ceb33206SDavid S. Miller dst->obsolete = DST_OBSOLETE_KILL; 950ceb33206SDavid S. Miller } else { 951ceb33206SDavid S. Miller rt->rt_pmtu = mtu; 952ceb33206SDavid S. Miller dst_set_expires(&rt->dst, ip_rt_mtu_expires); 953ceb33206SDavid S. Miller } 9544895c771SDavid S. Miller } 9554895c771SDavid S. Miller 95636393395SDavid S. Miller void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, 95736393395SDavid S. Miller int oif, u32 mark, u8 protocol, int flow_flags) 95836393395SDavid S. Miller { 95936393395SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 96036393395SDavid S. Miller struct flowi4 fl4; 96136393395SDavid S. Miller struct rtable *rt; 96236393395SDavid S. Miller 9634895c771SDavid S. Miller __build_flow_key(&fl4, NULL, iph, oif, 9644895c771SDavid S. Miller RT_TOS(iph->tos), protocol, mark, flow_flags); 96536393395SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 96636393395SDavid S. Miller if (!IS_ERR(rt)) { 9674895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 96836393395SDavid S. Miller ip_rt_put(rt); 96936393395SDavid S. Miller } 97036393395SDavid S. Miller } 97136393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_update_pmtu); 97236393395SDavid S. Miller 97336393395SDavid S. Miller void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 97436393395SDavid S. Miller { 9754895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 9764895c771SDavid S. Miller struct flowi4 fl4; 9774895c771SDavid S. Miller struct rtable *rt; 97836393395SDavid S. Miller 9794895c771SDavid S. Miller __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0); 9804895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4); 9814895c771SDavid S. Miller if (!IS_ERR(rt)) { 9824895c771SDavid S. Miller __ip_rt_update_pmtu(rt, &fl4, mtu); 9834895c771SDavid S. Miller ip_rt_put(rt); 9844895c771SDavid S. Miller } 98536393395SDavid S. Miller } 98636393395SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu); 987f39925dbSDavid S. Miller 988b42597e2SDavid S. Miller void ipv4_redirect(struct sk_buff *skb, struct net *net, 989b42597e2SDavid S. Miller int oif, u32 mark, u8 protocol, int flow_flags) 990b42597e2SDavid S. Miller { 991b42597e2SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 992b42597e2SDavid S. Miller struct flowi4 fl4; 993b42597e2SDavid S. Miller struct rtable *rt; 994b42597e2SDavid S. Miller 9954895c771SDavid S. Miller __build_flow_key(&fl4, NULL, iph, oif, 9964895c771SDavid S. Miller RT_TOS(iph->tos), protocol, mark, flow_flags); 997b42597e2SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 998b42597e2SDavid S. Miller if (!IS_ERR(rt)) { 999ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 1000b42597e2SDavid S. Miller ip_rt_put(rt); 1001b42597e2SDavid S. Miller } 1002b42597e2SDavid S. Miller } 1003b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_redirect); 1004b42597e2SDavid S. Miller 1005b42597e2SDavid S. Miller void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk) 1006b42597e2SDavid S. Miller { 10074895c771SDavid S. Miller const struct iphdr *iph = (const struct iphdr *) skb->data; 10084895c771SDavid S. Miller struct flowi4 fl4; 10094895c771SDavid S. Miller struct rtable *rt; 1010b42597e2SDavid S. Miller 10114895c771SDavid S. Miller __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0); 10124895c771SDavid S. Miller rt = __ip_route_output_key(sock_net(sk), &fl4); 10134895c771SDavid S. Miller if (!IS_ERR(rt)) { 1014ceb33206SDavid S. Miller __ip_do_redirect(rt, skb, &fl4, false); 10154895c771SDavid S. Miller ip_rt_put(rt); 10164895c771SDavid S. Miller } 1017b42597e2SDavid S. Miller } 1018b42597e2SDavid S. Miller EXPORT_SYMBOL_GPL(ipv4_sk_redirect); 1019b42597e2SDavid S. Miller 1020efbc368dSDavid S. Miller static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie) 1021efbc368dSDavid S. Miller { 1022efbc368dSDavid S. Miller struct rtable *rt = (struct rtable *) dst; 1023efbc368dSDavid S. Miller 1024ceb33206SDavid S. Miller /* All IPV4 dsts are created with ->obsolete set to the value 1025ceb33206SDavid S. Miller * DST_OBSOLETE_FORCE_CHK which forces validation calls down 1026ceb33206SDavid S. Miller * into this function always. 1027ceb33206SDavid S. Miller * 1028ceb33206SDavid S. Miller * When a PMTU/redirect information update invalidates a 1029ceb33206SDavid S. Miller * route, this is indicated by setting obsolete to 1030ceb33206SDavid S. Miller * DST_OBSOLETE_KILL. 1031ceb33206SDavid S. Miller */ 1032ceb33206SDavid S. Miller if (dst->obsolete == DST_OBSOLETE_KILL || rt_is_expired(rt)) 1033efbc368dSDavid S. Miller return NULL; 1034d11a4dc1STimo Teräs return dst; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds struct rtable *rt = (struct rtable *) dst; 10401da177e4SLinus Torvalds 104162fa8a84SDavid S. Miller if (rt->fi) { 104262fa8a84SDavid S. Miller fib_info_put(rt->fi); 104362fa8a84SDavid S. Miller rt->fi = NULL; 104462fa8a84SDavid S. Miller } 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds struct rtable *rt; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 10531da177e4SLinus Torvalds 1054511c3f92SEric Dumazet rt = skb_rtable(skb); 10555943634fSDavid S. Miller if (rt) 10565943634fSDavid S. Miller dst_set_expires(&rt->dst, 0); 10572c8cec5cSDavid S. Miller } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb) 10601da177e4SLinus Torvalds { 106191df42beSJoe Perches pr_debug("%s: %pI4 -> %pI4, %s\n", 106291df42beSJoe Perches __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, 10631da177e4SLinus Torvalds skb->dev ? skb->dev->name : "?"); 10641da177e4SLinus Torvalds kfree_skb(skb); 1065c378a9c0SDave Jones WARN_ON(1); 10661da177e4SLinus Torvalds return 0; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds /* 10701da177e4SLinus Torvalds We do not cache source address of outgoing interface, 10711da177e4SLinus Torvalds because it is used only by IP RR, TS and SRR options, 10721da177e4SLinus Torvalds so that it out of fast path. 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds BTW remember: "addr" is allowed to be not aligned 10751da177e4SLinus Torvalds in IP options! 10761da177e4SLinus Torvalds */ 10771da177e4SLinus Torvalds 10788e36360aSDavid S. Miller void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt) 10791da177e4SLinus Torvalds { 1080a61ced5dSAl Viro __be32 src; 10811da177e4SLinus Torvalds 1082c7537967SDavid S. Miller if (rt_is_output_route(rt)) 1083c5be24ffSDavid S. Miller src = ip_hdr(skb)->saddr; 1084ebc0ffaeSEric Dumazet else { 10858e36360aSDavid S. Miller struct fib_result res; 10868e36360aSDavid S. Miller struct flowi4 fl4; 10878e36360aSDavid S. Miller struct iphdr *iph; 10888e36360aSDavid S. Miller 10898e36360aSDavid S. Miller iph = ip_hdr(skb); 10908e36360aSDavid S. Miller 10918e36360aSDavid S. Miller memset(&fl4, 0, sizeof(fl4)); 10928e36360aSDavid S. Miller fl4.daddr = iph->daddr; 10938e36360aSDavid S. Miller fl4.saddr = iph->saddr; 1094b0fe4a31SJulian Anastasov fl4.flowi4_tos = RT_TOS(iph->tos); 10958e36360aSDavid S. Miller fl4.flowi4_oif = rt->dst.dev->ifindex; 10968e36360aSDavid S. Miller fl4.flowi4_iif = skb->dev->ifindex; 10978e36360aSDavid S. Miller fl4.flowi4_mark = skb->mark; 10985e2b61f7SDavid S. Miller 1099ebc0ffaeSEric Dumazet rcu_read_lock(); 110068a5e3ddSDavid S. Miller if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res) == 0) 1101436c3b66SDavid S. Miller src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res); 1102ebc0ffaeSEric Dumazet else 1103f8126f1dSDavid S. Miller src = inet_select_addr(rt->dst.dev, 1104f8126f1dSDavid S. Miller rt_nexthop(rt, iph->daddr), 11051da177e4SLinus Torvalds RT_SCOPE_UNIVERSE); 1106ebc0ffaeSEric Dumazet rcu_read_unlock(); 1107ebc0ffaeSEric Dumazet } 11081da177e4SLinus Torvalds memcpy(addr, &src, 4); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 1111c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 11121da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag) 11131da177e4SLinus Torvalds { 1114d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF)) 1115d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF; 1116d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF0000)) 1117d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF0000; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds #endif 11201da177e4SLinus Torvalds 11210dbaee3bSDavid S. Miller static unsigned int ipv4_default_advmss(const struct dst_entry *dst) 11220dbaee3bSDavid S. Miller { 11230dbaee3bSDavid S. Miller unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS); 11240dbaee3bSDavid S. Miller 11250dbaee3bSDavid S. Miller if (advmss == 0) { 11260dbaee3bSDavid S. Miller advmss = max_t(unsigned int, dst->dev->mtu - 40, 11270dbaee3bSDavid S. Miller ip_rt_min_advmss); 11280dbaee3bSDavid S. Miller if (advmss > 65535 - 40) 11290dbaee3bSDavid S. Miller advmss = 65535 - 40; 11300dbaee3bSDavid S. Miller } 11310dbaee3bSDavid S. Miller return advmss; 11320dbaee3bSDavid S. Miller } 11330dbaee3bSDavid S. Miller 1134ebb762f2SSteffen Klassert static unsigned int ipv4_mtu(const struct dst_entry *dst) 1135d33e4553SDavid S. Miller { 1136261663b0SSteffen Klassert const struct rtable *rt = (const struct rtable *) dst; 11375943634fSDavid S. Miller unsigned int mtu = rt->rt_pmtu; 11385943634fSDavid S. Miller 11395943634fSDavid S. Miller if (mtu && time_after_eq(jiffies, rt->dst.expires)) 11405943634fSDavid S. Miller mtu = 0; 11415943634fSDavid S. Miller 11425943634fSDavid S. Miller if (!mtu) 11435943634fSDavid S. Miller mtu = dst_metric_raw(dst, RTAX_MTU); 1144618f9bc7SSteffen Klassert 1145261663b0SSteffen Klassert if (mtu && rt_is_output_route(rt)) 1146618f9bc7SSteffen Klassert return mtu; 1147618f9bc7SSteffen Klassert 1148618f9bc7SSteffen Klassert mtu = dst->dev->mtu; 1149d33e4553SDavid S. Miller 1150d33e4553SDavid S. Miller if (unlikely(dst_metric_locked(dst, RTAX_MTU))) { 1151f8126f1dSDavid S. Miller if (rt->rt_gateway && mtu > 576) 1152d33e4553SDavid S. Miller mtu = 576; 1153d33e4553SDavid S. Miller } 1154d33e4553SDavid S. Miller 1155d33e4553SDavid S. Miller if (mtu > IP_MAX_MTU) 1156d33e4553SDavid S. Miller mtu = IP_MAX_MTU; 1157d33e4553SDavid S. Miller 1158d33e4553SDavid S. Miller return mtu; 1159d33e4553SDavid S. Miller } 1160d33e4553SDavid S. Miller 1161f2bb4bedSDavid S. Miller static void rt_init_metrics(struct rtable *rt, struct fib_info *fi) 1162a4daad6bSDavid S. Miller { 1163b8dad61cSDavid S. Miller if (fi->fib_metrics != (u32 *) dst_default_metrics) { 1164a4daad6bSDavid S. Miller rt->fi = fi; 1165a4daad6bSDavid S. Miller atomic_inc(&fi->fib_clntref); 1166b8dad61cSDavid S. Miller } 1167a4daad6bSDavid S. Miller dst_init_metrics(&rt->dst, fi->fib_metrics, true); 1168a4daad6bSDavid S. Miller } 1169a4daad6bSDavid S. Miller 1170f2bb4bedSDavid S. Miller static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr) 11714895c771SDavid S. Miller { 11724895c771SDavid S. Miller struct fnhe_hash_bucket *hash = nh->nh_exceptions; 11734895c771SDavid S. Miller struct fib_nh_exception *fnhe; 11744895c771SDavid S. Miller u32 hval; 11754895c771SDavid S. Miller 1176f2bb4bedSDavid S. Miller if (!hash) 1177f2bb4bedSDavid S. Miller return NULL; 1178f2bb4bedSDavid S. Miller 1179d3a25c98SDavid S. Miller hval = fnhe_hashfun(daddr); 11804895c771SDavid S. Miller 11814895c771SDavid S. Miller for (fnhe = rcu_dereference(hash[hval].chain); fnhe; 11824895c771SDavid S. Miller fnhe = rcu_dereference(fnhe->fnhe_next)) { 1183f2bb4bedSDavid S. Miller if (fnhe->fnhe_daddr == daddr) 1184f2bb4bedSDavid S. Miller return fnhe; 1185f2bb4bedSDavid S. Miller } 1186f2bb4bedSDavid S. Miller return NULL; 1187f2bb4bedSDavid S. Miller } 1188f2bb4bedSDavid S. Miller 1189f2bb4bedSDavid S. Miller static void rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe, 1190f2bb4bedSDavid S. Miller __be32 daddr) 1191f2bb4bedSDavid S. Miller { 1192aee06da6SJulian Anastasov __be32 fnhe_daddr, gw; 1193aee06da6SJulian Anastasov unsigned long expires; 1194aee06da6SJulian Anastasov unsigned int seq; 1195aee06da6SJulian Anastasov u32 pmtu; 1196aee06da6SJulian Anastasov 1197f2bb4bedSDavid S. Miller restart: 1198aee06da6SJulian Anastasov seq = read_seqbegin(&fnhe_seqlock); 1199aee06da6SJulian Anastasov fnhe_daddr = fnhe->fnhe_daddr; 1200aee06da6SJulian Anastasov gw = fnhe->fnhe_gw; 1201aee06da6SJulian Anastasov pmtu = fnhe->fnhe_pmtu; 1202aee06da6SJulian Anastasov expires = fnhe->fnhe_expires; 1203aee06da6SJulian Anastasov if (read_seqretry(&fnhe_seqlock, seq)) 1204aee06da6SJulian Anastasov goto restart; 1205f2bb4bedSDavid S. Miller 1206aee06da6SJulian Anastasov if (daddr != fnhe_daddr) 1207f2bb4bedSDavid S. Miller return; 1208f2bb4bedSDavid S. Miller 1209aee06da6SJulian Anastasov if (pmtu) { 1210f31fd383SJulian Anastasov unsigned long diff = expires - jiffies; 12114895c771SDavid S. Miller 12124895c771SDavid S. Miller if (time_before(jiffies, expires)) { 1213aee06da6SJulian Anastasov rt->rt_pmtu = pmtu; 12144895c771SDavid S. Miller dst_set_expires(&rt->dst, diff); 12154895c771SDavid S. Miller } 12164895c771SDavid S. Miller } 1217ceb33206SDavid S. Miller if (gw) { 1218ceb33206SDavid S. Miller rt->rt_flags |= RTCF_REDIRECTED; 1219aee06da6SJulian Anastasov rt->rt_gateway = gw; 1220ceb33206SDavid S. Miller } 12214895c771SDavid S. Miller fnhe->fnhe_stamp = jiffies; 1222f2bb4bedSDavid S. Miller } 1223f2bb4bedSDavid S. Miller 1224f2bb4bedSDavid S. Miller static inline void rt_release_rcu(struct rcu_head *head) 1225f2bb4bedSDavid S. Miller { 1226f2bb4bedSDavid S. Miller struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head); 1227f2bb4bedSDavid S. Miller dst_release(dst); 1228f2bb4bedSDavid S. Miller } 1229f2bb4bedSDavid S. Miller 1230f2bb4bedSDavid S. Miller static void rt_cache_route(struct fib_nh *nh, struct rtable *rt) 1231f2bb4bedSDavid S. Miller { 1232f2bb4bedSDavid S. Miller struct rtable *orig, *prev, **p = &nh->nh_rth_output; 1233f2bb4bedSDavid S. Miller 1234*d2d68ba9SDavid S. Miller if (rt_is_input_route(rt)) 1235*d2d68ba9SDavid S. Miller p = &nh->nh_rth_input; 1236*d2d68ba9SDavid S. Miller 1237f2bb4bedSDavid S. Miller orig = *p; 1238f2bb4bedSDavid S. Miller 1239f2bb4bedSDavid S. Miller prev = cmpxchg(p, orig, rt); 1240f2bb4bedSDavid S. Miller if (prev == orig) { 1241f2bb4bedSDavid S. Miller dst_clone(&rt->dst); 1242f2bb4bedSDavid S. Miller if (orig) 1243f2bb4bedSDavid S. Miller call_rcu_bh(&orig->dst.rcu_head, rt_release_rcu); 12444895c771SDavid S. Miller } 12454895c771SDavid S. Miller } 12464895c771SDavid S. Miller 1247*d2d68ba9SDavid S. Miller static bool rt_cache_valid(struct rtable *rt) 1248*d2d68ba9SDavid S. Miller { 1249*d2d68ba9SDavid S. Miller return (rt && rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK); 1250*d2d68ba9SDavid S. Miller } 1251*d2d68ba9SDavid S. Miller 1252f2bb4bedSDavid S. Miller static void rt_set_nexthop(struct rtable *rt, __be32 daddr, 12535e2b61f7SDavid S. Miller const struct fib_result *res, 1254f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe, 1255982721f3SDavid S. Miller struct fib_info *fi, u16 type, u32 itag) 12561da177e4SLinus Torvalds { 12571da177e4SLinus Torvalds if (fi) { 12584895c771SDavid S. Miller struct fib_nh *nh = &FIB_RES_NH(*res); 12594895c771SDavid S. Miller 12604895c771SDavid S. Miller if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK) 12614895c771SDavid S. Miller rt->rt_gateway = nh->nh_gw; 1262f2bb4bedSDavid S. Miller if (unlikely(fnhe)) 1263f2bb4bedSDavid S. Miller rt_bind_exception(rt, fnhe, daddr); 1264f2bb4bedSDavid S. Miller rt_init_metrics(rt, fi); 1265c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1266f2bb4bedSDavid S. Miller rt->dst.tclassid = nh->nh_tclassid; 12671da177e4SLinus Torvalds #endif 1268*d2d68ba9SDavid S. Miller if (!(rt->dst.flags & DST_HOST)) 1269f2bb4bedSDavid S. Miller rt_cache_route(nh, rt); 1270d33e4553SDavid S. Miller } 12711da177e4SLinus Torvalds 1272c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 12731da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES 127485b91b03SDavid S. Miller set_class_tag(rt, res->tclassid); 12751da177e4SLinus Torvalds #endif 12761da177e4SLinus Torvalds set_class_tag(rt, itag); 12771da177e4SLinus Torvalds #endif 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12805c1e6aa3SDavid S. Miller static struct rtable *rt_dst_alloc(struct net_device *dev, 1281f2bb4bedSDavid S. Miller bool nopolicy, bool noxfrm, bool will_cache) 12820c4dcd58SDavid S. Miller { 1283f5b0a874SDavid S. Miller return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK, 1284f2bb4bedSDavid S. Miller (will_cache ? 0 : DST_HOST) | DST_NOCACHE | 12850c4dcd58SDavid S. Miller (nopolicy ? DST_NOPOLICY : 0) | 12865c1e6aa3SDavid S. Miller (noxfrm ? DST_NOXFRM : 0)); 12870c4dcd58SDavid S. Miller } 12880c4dcd58SDavid S. Miller 128996d36220SEric Dumazet /* called in rcu_read_lock() section */ 12909e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr, 12911da177e4SLinus Torvalds u8 tos, struct net_device *dev, int our) 12921da177e4SLinus Torvalds { 12931da177e4SLinus Torvalds struct rtable *rth; 129496d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 12951da177e4SLinus Torvalds u32 itag = 0; 1296b5f7e755SEric Dumazet int err; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds /* Primary sanity checks. */ 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds if (in_dev == NULL) 13011da177e4SLinus Torvalds return -EINVAL; 13021da177e4SLinus Torvalds 13031e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1304d0daebc3SThomas Graf skb->protocol != htons(ETH_P_IP)) 1305d0daebc3SThomas Graf goto e_inval; 1306d0daebc3SThomas Graf 1307d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 1308d0daebc3SThomas Graf if (ipv4_is_loopback(saddr)) 13091da177e4SLinus Torvalds goto e_inval; 13101da177e4SLinus Torvalds 1311f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) { 1312f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr)) 13131da177e4SLinus Torvalds goto e_inval; 1314b5f7e755SEric Dumazet } else { 13159e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 13169e56e380SDavid S. Miller in_dev, &itag); 1317b5f7e755SEric Dumazet if (err < 0) 1318b5f7e755SEric Dumazet goto e_err; 1319b5f7e755SEric Dumazet } 13204e7b2f14SBenjamin LaHaise rth = rt_dst_alloc(dev_net(dev)->loopback_dev, 1321f2bb4bedSDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false); 13221da177e4SLinus Torvalds if (!rth) 13231da177e4SLinus Torvalds goto e_nobufs; 13241da177e4SLinus Torvalds 1325c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1326d8d1f30bSChangli Gao rth->dst.tclassid = itag; 13271da177e4SLinus Torvalds #endif 1328cf911662SDavid S. Miller rth->dst.output = ip_rt_bug; 1329cf911662SDavid S. Miller 1330e84f84f2SDenis V. Lunev rth->rt_genid = rt_genid(dev_net(dev)); 13311da177e4SLinus Torvalds rth->rt_flags = RTCF_MULTICAST; 133229e75252SEric Dumazet rth->rt_type = RTN_MULTICAST; 1333cf911662SDavid S. Miller rth->rt_route_iif = dev->ifindex; 1334cf911662SDavid S. Miller rth->rt_iif = dev->ifindex; 1335cf911662SDavid S. Miller rth->rt_oif = 0; 13365943634fSDavid S. Miller rth->rt_pmtu = 0; 1337f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1338cf911662SDavid S. Miller rth->fi = NULL; 13391da177e4SLinus Torvalds if (our) { 1340d8d1f30bSChangli Gao rth->dst.input= ip_local_deliver; 13411da177e4SLinus Torvalds rth->rt_flags |= RTCF_LOCAL; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1345f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev)) 1346d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 13471da177e4SLinus Torvalds #endif 13481da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_mc); 13491da177e4SLinus Torvalds 135089aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 135189aef892SDavid S. Miller return 0; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds e_nobufs: 13541da177e4SLinus Torvalds return -ENOBUFS; 13551da177e4SLinus Torvalds e_inval: 135696d36220SEric Dumazet return -EINVAL; 1357b5f7e755SEric Dumazet e_err: 1358b5f7e755SEric Dumazet return err; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev, 13631da177e4SLinus Torvalds struct in_device *in_dev, 13641da177e4SLinus Torvalds struct sk_buff *skb, 13659e12bb22SAl Viro __be32 daddr, 13669e12bb22SAl Viro __be32 saddr) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_src); 13691da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 13701da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) { 13711da177e4SLinus Torvalds /* 13721da177e4SLinus Torvalds * RFC1812 recommendation, if source is martian, 13731da177e4SLinus Torvalds * the only hint is MAC header. 13741da177e4SLinus Torvalds */ 1375058bd4d2SJoe Perches pr_warn("martian source %pI4 from %pI4, on dev %s\n", 1376673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 137798e399f8SArnaldo Carvalho de Melo if (dev->hard_header_len && skb_mac_header_was_set(skb)) { 1378058bd4d2SJoe Perches print_hex_dump(KERN_WARNING, "ll header: ", 1379058bd4d2SJoe Perches DUMP_PREFIX_OFFSET, 16, 1, 1380058bd4d2SJoe Perches skb_mac_header(skb), 1381058bd4d2SJoe Perches dev->hard_header_len, true); 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds #endif 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds 138747360228SEric Dumazet /* called in rcu_read_lock() section */ 13885969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb, 1389982721f3SDavid S. Miller const struct fib_result *res, 13901da177e4SLinus Torvalds struct in_device *in_dev, 13919e12bb22SAl Viro __be32 daddr, __be32 saddr, u32 tos, 13921da177e4SLinus Torvalds struct rtable **result) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct rtable *rth; 13951da177e4SLinus Torvalds int err; 13961da177e4SLinus Torvalds struct in_device *out_dev; 139747360228SEric Dumazet unsigned int flags = 0; 1398*d2d68ba9SDavid S. Miller bool do_cache; 1399d9c9df8cSAl Viro u32 itag; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /* get a working reference to the output device */ 140247360228SEric Dumazet out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res)); 14031da177e4SLinus Torvalds if (out_dev == NULL) { 1404e87cc472SJoe Perches net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n"); 14051da177e4SLinus Torvalds return -EINVAL; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds 14095c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res), 14109e56e380SDavid S. Miller in_dev->dev, in_dev, &itag); 14111da177e4SLinus Torvalds if (err < 0) { 14121da177e4SLinus Torvalds ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr, 14131da177e4SLinus Torvalds saddr); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds goto cleanup; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds if (err) 14191da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 14201da177e4SLinus Torvalds 142151b77caeSThomas Graf if (out_dev == in_dev && err && 14221da177e4SLinus Torvalds (IN_DEV_SHARED_MEDIA(out_dev) || 14231da177e4SLinus Torvalds inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res)))) 14241da177e4SLinus Torvalds flags |= RTCF_DOREDIRECT; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) { 14271da177e4SLinus Torvalds /* Not IP (i.e. ARP). Do not create route, if it is 14281da177e4SLinus Torvalds * invalid for proxy arp. DNAT routes are always valid. 142965324144SJesper Dangaard Brouer * 143065324144SJesper Dangaard Brouer * Proxy arp feature have been extended to allow, ARP 143165324144SJesper Dangaard Brouer * replies back to the same interface, to support 143265324144SJesper Dangaard Brouer * Private VLAN switch technologies. See arp.c. 14331da177e4SLinus Torvalds */ 143465324144SJesper Dangaard Brouer if (out_dev == in_dev && 143565324144SJesper Dangaard Brouer IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) { 14361da177e4SLinus Torvalds err = -EINVAL; 14371da177e4SLinus Torvalds goto cleanup; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 1441*d2d68ba9SDavid S. Miller do_cache = false; 1442*d2d68ba9SDavid S. Miller if (res->fi) { 1443*d2d68ba9SDavid S. Miller if (!(flags & RTCF_DIRECTSRC) && !itag) { 1444*d2d68ba9SDavid S. Miller rth = FIB_RES_NH(*res).nh_rth_input; 1445*d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1446*d2d68ba9SDavid S. Miller dst_use(&rth->dst, jiffies); 1447*d2d68ba9SDavid S. Miller goto out; 1448*d2d68ba9SDavid S. Miller } 1449*d2d68ba9SDavid S. Miller do_cache = true; 1450*d2d68ba9SDavid S. Miller } 1451*d2d68ba9SDavid S. Miller } 1452f2bb4bedSDavid S. Miller 14535c1e6aa3SDavid S. Miller rth = rt_dst_alloc(out_dev->dev, 14545c1e6aa3SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), 1455*d2d68ba9SDavid S. Miller IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache); 14561da177e4SLinus Torvalds if (!rth) { 14571da177e4SLinus Torvalds err = -ENOBUFS; 14581da177e4SLinus Torvalds goto cleanup; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 1461cf911662SDavid S. Miller rth->rt_genid = rt_genid(dev_net(rth->dst.dev)); 1462cf911662SDavid S. Miller rth->rt_flags = flags; 1463cf911662SDavid S. Miller rth->rt_type = res->type; 14641b86a58fSOGAWA Hirofumi rth->rt_route_iif = in_dev->dev->ifindex; 14655e2b61f7SDavid S. Miller rth->rt_iif = in_dev->dev->ifindex; 14665e2b61f7SDavid S. Miller rth->rt_oif = 0; 14675943634fSDavid S. Miller rth->rt_pmtu = 0; 1468f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1469cf911662SDavid S. Miller rth->fi = NULL; 14701da177e4SLinus Torvalds 1471d8d1f30bSChangli Gao rth->dst.input = ip_forward; 1472d8d1f30bSChangli Gao rth->dst.output = ip_output; 14731da177e4SLinus Torvalds 1474*d2d68ba9SDavid S. Miller rt_set_nexthop(rth, daddr, res, NULL, res->fi, res->type, itag); 1475*d2d68ba9SDavid S. Miller out: 14761da177e4SLinus Torvalds *result = rth; 14771da177e4SLinus Torvalds err = 0; 14781da177e4SLinus Torvalds cleanup: 14791da177e4SLinus Torvalds return err; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 14825969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb, 14831da177e4SLinus Torvalds struct fib_result *res, 148468a5e3ddSDavid S. Miller const struct flowi4 *fl4, 14851da177e4SLinus Torvalds struct in_device *in_dev, 14869e12bb22SAl Viro __be32 daddr, __be32 saddr, u32 tos) 14871da177e4SLinus Torvalds { 14887abaa27cSChuck Short struct rtable *rth = NULL; 14891da177e4SLinus Torvalds int err; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 1492ff3fccb3SDavid S. Miller if (res->fi && res->fi->fib_nhs > 1) 14931b7fe593SDavid S. Miller fib_select_multipath(res); 14941da177e4SLinus Torvalds #endif 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds /* create a routing cache entry */ 14971da177e4SLinus Torvalds err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth); 14981da177e4SLinus Torvalds if (err) 14991da177e4SLinus Torvalds return err; 15001da177e4SLinus Torvalds 150189aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 1502b23dd4feSDavid S. Miller return 0; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 15061da177e4SLinus Torvalds * NOTE. We drop all the packets that has local source 15071da177e4SLinus Torvalds * addresses, because every properly looped back packet 15081da177e4SLinus Torvalds * must have correct destination already attached by output routine. 15091da177e4SLinus Torvalds * 15101da177e4SLinus Torvalds * Such approach solves two big problems: 15111da177e4SLinus Torvalds * 1. Not simplex devices are handled properly. 15121da177e4SLinus Torvalds * 2. IP spoofing attempts are filtered with 100% of guarantee. 1513ebc0ffaeSEric Dumazet * called with rcu_read_lock() 15141da177e4SLinus Torvalds */ 15151da177e4SLinus Torvalds 15169e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1517c10237e0SDavid S. Miller u8 tos, struct net_device *dev) 15181da177e4SLinus Torvalds { 15191da177e4SLinus Torvalds struct fib_result res; 152096d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 152168a5e3ddSDavid S. Miller struct flowi4 fl4; 152295c96174SEric Dumazet unsigned int flags = 0; 15231da177e4SLinus Torvalds u32 itag = 0; 15241da177e4SLinus Torvalds struct rtable *rth; 15251da177e4SLinus Torvalds int err = -EINVAL; 1526c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1527*d2d68ba9SDavid S. Miller bool do_cache; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* IP on this device is disabled. */ 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds if (!in_dev) 15321da177e4SLinus Torvalds goto out; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* Check for the most weird martians, which can be not detected 15351da177e4SLinus Torvalds by fib_lookup. 15361da177e4SLinus Torvalds */ 15371da177e4SLinus Torvalds 1538d0daebc3SThomas Graf if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 15391da177e4SLinus Torvalds goto martian_source; 15401da177e4SLinus Torvalds 1541*d2d68ba9SDavid S. Miller res.fi = NULL; 154227a954bdSAndy Walls if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0)) 15431da177e4SLinus Torvalds goto brd_input; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* Accept zero addresses only to limited broadcast; 15461da177e4SLinus Torvalds * I even do not know to fix it or not. Waiting for complains :-) 15471da177e4SLinus Torvalds */ 1548f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) 15491da177e4SLinus Torvalds goto martian_source; 15501da177e4SLinus Torvalds 1551d0daebc3SThomas Graf if (ipv4_is_zeronet(daddr)) 15521da177e4SLinus Torvalds goto martian_destination; 15531da177e4SLinus Torvalds 1554d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) { 1555d0daebc3SThomas Graf if (ipv4_is_loopback(daddr)) 1556d0daebc3SThomas Graf goto martian_destination; 1557d0daebc3SThomas Graf 1558d0daebc3SThomas Graf if (ipv4_is_loopback(saddr)) 1559d0daebc3SThomas Graf goto martian_source; 1560d0daebc3SThomas Graf } 1561d0daebc3SThomas Graf 15621da177e4SLinus Torvalds /* 15631da177e4SLinus Torvalds * Now we are ready to route packet. 15641da177e4SLinus Torvalds */ 156568a5e3ddSDavid S. Miller fl4.flowi4_oif = 0; 156668a5e3ddSDavid S. Miller fl4.flowi4_iif = dev->ifindex; 156768a5e3ddSDavid S. Miller fl4.flowi4_mark = skb->mark; 156868a5e3ddSDavid S. Miller fl4.flowi4_tos = tos; 156968a5e3ddSDavid S. Miller fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 157068a5e3ddSDavid S. Miller fl4.daddr = daddr; 157168a5e3ddSDavid S. Miller fl4.saddr = saddr; 157268a5e3ddSDavid S. Miller err = fib_lookup(net, &fl4, &res); 1573251da413SDavid S. Miller if (err != 0) 15741da177e4SLinus Torvalds goto no_route; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_tot); 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds if (res.type == RTN_BROADCAST) 15791da177e4SLinus Torvalds goto brd_input; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 15825c04c819SMichael Smith err = fib_validate_source(skb, saddr, daddr, tos, 158384a885f4SDenis V. Lunev net->loopback_dev->ifindex, 15849e56e380SDavid S. Miller dev, in_dev, &itag); 1585b5f7e755SEric Dumazet if (err < 0) 1586b5f7e755SEric Dumazet goto martian_source_keep_err; 1587b5f7e755SEric Dumazet if (err) 15881da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 15891da177e4SLinus Torvalds goto local_input; 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds if (!IN_DEV_FORWARD(in_dev)) 1593251da413SDavid S. Miller goto no_route; 15941da177e4SLinus Torvalds if (res.type != RTN_UNICAST) 15951da177e4SLinus Torvalds goto martian_destination; 15961da177e4SLinus Torvalds 159768a5e3ddSDavid S. Miller err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos); 15981da177e4SLinus Torvalds out: return err; 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds brd_input: 16011da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) 16021da177e4SLinus Torvalds goto e_inval; 16031da177e4SLinus Torvalds 160441347dcdSDavid S. Miller if (!ipv4_is_zeronet(saddr)) { 16059e56e380SDavid S. Miller err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 16069e56e380SDavid S. Miller in_dev, &itag); 16071da177e4SLinus Torvalds if (err < 0) 1608b5f7e755SEric Dumazet goto martian_source_keep_err; 16091da177e4SLinus Torvalds if (err) 16101da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds flags |= RTCF_BROADCAST; 16131da177e4SLinus Torvalds res.type = RTN_BROADCAST; 16141da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_brd); 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds local_input: 1617*d2d68ba9SDavid S. Miller do_cache = false; 1618*d2d68ba9SDavid S. Miller if (res.fi) { 1619*d2d68ba9SDavid S. Miller if (!(flags & RTCF_DIRECTSRC) && !itag) { 1620*d2d68ba9SDavid S. Miller rth = FIB_RES_NH(res).nh_rth_input; 1621*d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1622*d2d68ba9SDavid S. Miller dst_use(&rth->dst, jiffies); 1623*d2d68ba9SDavid S. Miller goto set_and_out; 1624*d2d68ba9SDavid S. Miller } 1625*d2d68ba9SDavid S. Miller do_cache = true; 1626*d2d68ba9SDavid S. Miller } 1627*d2d68ba9SDavid S. Miller } 1628*d2d68ba9SDavid S. Miller 16295c1e6aa3SDavid S. Miller rth = rt_dst_alloc(net->loopback_dev, 1630*d2d68ba9SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache); 16311da177e4SLinus Torvalds if (!rth) 16321da177e4SLinus Torvalds goto e_nobufs; 16331da177e4SLinus Torvalds 1634cf911662SDavid S. Miller rth->dst.input= ip_local_deliver; 1635d8d1f30bSChangli Gao rth->dst.output= ip_rt_bug; 1636cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 1637cf911662SDavid S. Miller rth->dst.tclassid = itag; 1638cf911662SDavid S. Miller #endif 16391da177e4SLinus Torvalds 1640cf911662SDavid S. Miller rth->rt_genid = rt_genid(net); 1641cf911662SDavid S. Miller rth->rt_flags = flags|RTCF_LOCAL; 1642cf911662SDavid S. Miller rth->rt_type = res.type; 16431b86a58fSOGAWA Hirofumi rth->rt_route_iif = dev->ifindex; 16445e2b61f7SDavid S. Miller rth->rt_iif = dev->ifindex; 1645cf911662SDavid S. Miller rth->rt_oif = 0; 16465943634fSDavid S. Miller rth->rt_pmtu = 0; 1647f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1648cf911662SDavid S. Miller rth->fi = NULL; 16491da177e4SLinus Torvalds if (res.type == RTN_UNREACHABLE) { 1650d8d1f30bSChangli Gao rth->dst.input= ip_error; 1651d8d1f30bSChangli Gao rth->dst.error= -err; 16521da177e4SLinus Torvalds rth->rt_flags &= ~RTCF_LOCAL; 16531da177e4SLinus Torvalds } 1654*d2d68ba9SDavid S. Miller if (do_cache) 1655*d2d68ba9SDavid S. Miller rt_cache_route(&FIB_RES_NH(res), rth); 1656*d2d68ba9SDavid S. Miller set_and_out: 165789aef892SDavid S. Miller skb_dst_set(skb, &rth->dst); 1658b23dd4feSDavid S. Miller err = 0; 1659ebc0ffaeSEric Dumazet goto out; 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds no_route: 16621da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_no_route); 16631da177e4SLinus Torvalds res.type = RTN_UNREACHABLE; 16647f53878dSMitsuru Chinen if (err == -ESRCH) 16657f53878dSMitsuru Chinen err = -ENETUNREACH; 16661da177e4SLinus Torvalds goto local_input; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds /* 16691da177e4SLinus Torvalds * Do not cache martian addresses: they should be logged (RFC1812) 16701da177e4SLinus Torvalds */ 16711da177e4SLinus Torvalds martian_destination: 16721da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_dst); 16731da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 1674e87cc472SJoe Perches if (IN_DEV_LOG_MARTIANS(in_dev)) 1675e87cc472SJoe Perches net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n", 1676673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 16771da177e4SLinus Torvalds #endif 16782c2910a4SDietmar Eggemann 16791da177e4SLinus Torvalds e_inval: 16801da177e4SLinus Torvalds err = -EINVAL; 1681ebc0ffaeSEric Dumazet goto out; 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds e_nobufs: 16841da177e4SLinus Torvalds err = -ENOBUFS; 1685ebc0ffaeSEric Dumazet goto out; 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds martian_source: 1688b5f7e755SEric Dumazet err = -EINVAL; 1689b5f7e755SEric Dumazet martian_source_keep_err: 16901da177e4SLinus Torvalds ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 1691ebc0ffaeSEric Dumazet goto out; 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds 169438a424e4SDavid Miller int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr, 169538a424e4SDavid Miller u8 tos, struct net_device *dev) 16961da177e4SLinus Torvalds { 169796d36220SEric Dumazet int res; 16981da177e4SLinus Torvalds 169996d36220SEric Dumazet rcu_read_lock(); 170096d36220SEric Dumazet 17011da177e4SLinus Torvalds /* Multicast recognition logic is moved from route cache to here. 17021da177e4SLinus Torvalds The problem was that too many Ethernet cards have broken/missing 17031da177e4SLinus Torvalds hardware multicast filters :-( As result the host on multicasting 17041da177e4SLinus Torvalds network acquires a lot of useless route cache entries, sort of 17051da177e4SLinus Torvalds SDR messages from all the world. Now we try to get rid of them. 17061da177e4SLinus Torvalds Really, provided software IP multicast filter is organized 17071da177e4SLinus Torvalds reasonably (at least, hashed), it does not result in a slowdown 17081da177e4SLinus Torvalds comparing with route cache reject entries. 17091da177e4SLinus Torvalds Note, that multicast routers are not affected, because 17101da177e4SLinus Torvalds route cache entry is created eventually. 17111da177e4SLinus Torvalds */ 1712f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 171396d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 17141da177e4SLinus Torvalds 171596d36220SEric Dumazet if (in_dev) { 1716dbdd9a52SDavid S. Miller int our = ip_check_mc_rcu(in_dev, daddr, saddr, 1717eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol); 17181da177e4SLinus Torvalds if (our 17191da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 17209d4fb27dSJoe Perches || 17219d4fb27dSJoe Perches (!ipv4_is_local_multicast(daddr) && 1722f97c1e0cSJoe Perches IN_DEV_MFORWARD(in_dev)) 17231da177e4SLinus Torvalds #endif 17241da177e4SLinus Torvalds ) { 172596d36220SEric Dumazet int res = ip_route_input_mc(skb, daddr, saddr, 17261da177e4SLinus Torvalds tos, dev, our); 172796d36220SEric Dumazet rcu_read_unlock(); 172896d36220SEric Dumazet return res; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds rcu_read_unlock(); 17321da177e4SLinus Torvalds return -EINVAL; 17331da177e4SLinus Torvalds } 1734c10237e0SDavid S. Miller res = ip_route_input_slow(skb, daddr, saddr, tos, dev); 173596d36220SEric Dumazet rcu_read_unlock(); 173696d36220SEric Dumazet return res; 17371da177e4SLinus Torvalds } 173838a424e4SDavid Miller EXPORT_SYMBOL(ip_route_input); 17391da177e4SLinus Torvalds 1740ebc0ffaeSEric Dumazet /* called with rcu_read_lock() */ 1741982721f3SDavid S. Miller static struct rtable *__mkroute_output(const struct fib_result *res, 17421a00fee4SDavid Miller const struct flowi4 *fl4, int orig_oif, 1743f61759e6SJulian Anastasov struct net_device *dev_out, 17445ada5527SDavid S. Miller unsigned int flags) 17451da177e4SLinus Torvalds { 1746982721f3SDavid S. Miller struct fib_info *fi = res->fi; 1747f2bb4bedSDavid S. Miller struct fib_nh_exception *fnhe; 17485ada5527SDavid S. Miller struct in_device *in_dev; 1749982721f3SDavid S. Miller u16 type = res->type; 17505ada5527SDavid S. Miller struct rtable *rth; 17511da177e4SLinus Torvalds 1752d0daebc3SThomas Graf in_dev = __in_dev_get_rcu(dev_out); 1753d0daebc3SThomas Graf if (!in_dev) 1754d0daebc3SThomas Graf return ERR_PTR(-EINVAL); 1755d0daebc3SThomas Graf 1756d0daebc3SThomas Graf if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 175768a5e3ddSDavid S. Miller if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK)) 17585ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 17591da177e4SLinus Torvalds 176068a5e3ddSDavid S. Miller if (ipv4_is_lbcast(fl4->daddr)) 1761982721f3SDavid S. Miller type = RTN_BROADCAST; 176268a5e3ddSDavid S. Miller else if (ipv4_is_multicast(fl4->daddr)) 1763982721f3SDavid S. Miller type = RTN_MULTICAST; 176468a5e3ddSDavid S. Miller else if (ipv4_is_zeronet(fl4->daddr)) 17655ada5527SDavid S. Miller return ERR_PTR(-EINVAL); 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds if (dev_out->flags & IFF_LOOPBACK) 17681da177e4SLinus Torvalds flags |= RTCF_LOCAL; 17691da177e4SLinus Torvalds 1770982721f3SDavid S. Miller if (type == RTN_BROADCAST) { 17711da177e4SLinus Torvalds flags |= RTCF_BROADCAST | RTCF_LOCAL; 1772982721f3SDavid S. Miller fi = NULL; 1773982721f3SDavid S. Miller } else if (type == RTN_MULTICAST) { 17741da177e4SLinus Torvalds flags |= RTCF_MULTICAST | RTCF_LOCAL; 1775813b3b5dSDavid S. Miller if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr, 1776813b3b5dSDavid S. Miller fl4->flowi4_proto)) 17771da177e4SLinus Torvalds flags &= ~RTCF_LOCAL; 17781da177e4SLinus Torvalds /* If multicast route do not exist use 1779dd28d1a0SEric Dumazet * default one, but do not gateway in this case. 1780dd28d1a0SEric Dumazet * Yes, it is hack. 17811da177e4SLinus Torvalds */ 1782982721f3SDavid S. Miller if (fi && res->prefixlen < 4) 1783982721f3SDavid S. Miller fi = NULL; 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 1786f2bb4bedSDavid S. Miller fnhe = NULL; 1787f2bb4bedSDavid S. Miller if (fi) { 1788f2bb4bedSDavid S. Miller fnhe = find_exception(&FIB_RES_NH(*res), fl4->daddr); 1789f2bb4bedSDavid S. Miller if (!fnhe) { 1790f2bb4bedSDavid S. Miller rth = FIB_RES_NH(*res).nh_rth_output; 1791*d2d68ba9SDavid S. Miller if (rt_cache_valid(rth)) { 1792f2bb4bedSDavid S. Miller dst_use(&rth->dst, jiffies); 1793f2bb4bedSDavid S. Miller return rth; 1794f2bb4bedSDavid S. Miller } 1795f2bb4bedSDavid S. Miller } 1796f2bb4bedSDavid S. Miller } 17975c1e6aa3SDavid S. Miller rth = rt_dst_alloc(dev_out, 17985c1e6aa3SDavid S. Miller IN_DEV_CONF_GET(in_dev, NOPOLICY), 1799f2bb4bedSDavid S. Miller IN_DEV_CONF_GET(in_dev, NOXFRM), 1800f2bb4bedSDavid S. Miller fi && !fnhe); 18018391d07bSDimitris Michailidis if (!rth) 18025ada5527SDavid S. Miller return ERR_PTR(-ENOBUFS); 18038391d07bSDimitris Michailidis 1804cf911662SDavid S. Miller rth->dst.output = ip_output; 1805cf911662SDavid S. Miller 1806cf911662SDavid S. Miller rth->rt_genid = rt_genid(dev_net(dev_out)); 1807cf911662SDavid S. Miller rth->rt_flags = flags; 1808cf911662SDavid S. Miller rth->rt_type = type; 18091b86a58fSOGAWA Hirofumi rth->rt_route_iif = 0; 1810813b3b5dSDavid S. Miller rth->rt_iif = orig_oif ? : dev_out->ifindex; 1811813b3b5dSDavid S. Miller rth->rt_oif = orig_oif; 18125943634fSDavid S. Miller rth->rt_pmtu = 0; 1813f8126f1dSDavid S. Miller rth->rt_gateway = 0; 1814cf911662SDavid S. Miller rth->fi = NULL; 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_tot); 18171da177e4SLinus Torvalds 181841347dcdSDavid S. Miller if (flags & RTCF_LOCAL) 1819d8d1f30bSChangli Gao rth->dst.input = ip_local_deliver; 18201da177e4SLinus Torvalds if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 18211da177e4SLinus Torvalds if (flags & RTCF_LOCAL && 18221da177e4SLinus Torvalds !(dev_out->flags & IFF_LOOPBACK)) { 1823d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 18241da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_mc); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1827982721f3SDavid S. Miller if (type == RTN_MULTICAST) { 18281da177e4SLinus Torvalds if (IN_DEV_MFORWARD(in_dev) && 1829813b3b5dSDavid S. Miller !ipv4_is_local_multicast(fl4->daddr)) { 1830d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 1831d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds #endif 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 1837f2bb4bedSDavid S. Miller rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0); 18381da177e4SLinus Torvalds 18397586ecebSEric Dumazet if (fl4->flowi4_flags & FLOWI_FLAG_RT_NOCACHE) 18407586ecebSEric Dumazet rth->dst.flags |= DST_NOCACHE; 18417586ecebSEric Dumazet 18425ada5527SDavid S. Miller return rth; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * Major route resolver routine. 18471da177e4SLinus Torvalds */ 18481da177e4SLinus Torvalds 184989aef892SDavid S. Miller struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds struct net_device *dev_out = NULL; 1852f61759e6SJulian Anastasov __u8 tos = RT_FL_TOS(fl4); 1853813b3b5dSDavid S. Miller unsigned int flags = 0; 1854813b3b5dSDavid S. Miller struct fib_result res; 18555ada5527SDavid S. Miller struct rtable *rth; 1856813b3b5dSDavid S. Miller int orig_oif; 18571da177e4SLinus Torvalds 185885b91b03SDavid S. Miller res.tclassid = 0; 18591da177e4SLinus Torvalds res.fi = NULL; 18608b96d22dSDavid S. Miller res.table = NULL; 18611da177e4SLinus Torvalds 1862813b3b5dSDavid S. Miller orig_oif = fl4->flowi4_oif; 1863813b3b5dSDavid S. Miller 1864813b3b5dSDavid S. Miller fl4->flowi4_iif = net->loopback_dev->ifindex; 1865813b3b5dSDavid S. Miller fl4->flowi4_tos = tos & IPTOS_RT_MASK; 1866813b3b5dSDavid S. Miller fl4->flowi4_scope = ((tos & RTO_ONLINK) ? 186744713b67SDavid S. Miller RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); 186844713b67SDavid S. Miller 1869010c2708SDavid S. Miller rcu_read_lock(); 1870813b3b5dSDavid S. Miller if (fl4->saddr) { 1871b23dd4feSDavid S. Miller rth = ERR_PTR(-EINVAL); 1872813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->saddr) || 1873813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->saddr) || 1874813b3b5dSDavid S. Miller ipv4_is_zeronet(fl4->saddr)) 18751da177e4SLinus Torvalds goto out; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds /* I removed check for oif == dev_out->oif here. 18781da177e4SLinus Torvalds It was wrong for two reasons: 18791ab35276SDenis V. Lunev 1. ip_dev_find(net, saddr) can return wrong iface, if saddr 18801ab35276SDenis V. Lunev is assigned to multiple interfaces. 18811da177e4SLinus Torvalds 2. Moreover, we are allowed to send packets with saddr 18821da177e4SLinus Torvalds of another iface. --ANK 18831da177e4SLinus Torvalds */ 18841da177e4SLinus Torvalds 1885813b3b5dSDavid S. Miller if (fl4->flowi4_oif == 0 && 1886813b3b5dSDavid S. Miller (ipv4_is_multicast(fl4->daddr) || 1887813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr))) { 1888a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 1889813b3b5dSDavid S. Miller dev_out = __ip_dev_find(net, fl4->saddr, false); 1890a210d01aSJulian Anastasov if (dev_out == NULL) 1891a210d01aSJulian Anastasov goto out; 1892a210d01aSJulian Anastasov 18931da177e4SLinus Torvalds /* Special hack: user can direct multicasts 18941da177e4SLinus Torvalds and limited broadcast via necessary interface 18951da177e4SLinus Torvalds without fiddling with IP_MULTICAST_IF or IP_PKTINFO. 18961da177e4SLinus Torvalds This hack is not just for fun, it allows 18971da177e4SLinus Torvalds vic,vat and friends to work. 18981da177e4SLinus Torvalds They bind socket to loopback, set ttl to zero 18991da177e4SLinus Torvalds and expect that it will work. 19001da177e4SLinus Torvalds From the viewpoint of routing cache they are broken, 19011da177e4SLinus Torvalds because we are not allowed to build multicast path 19021da177e4SLinus Torvalds with loopback source addr (look, routing cache 19031da177e4SLinus Torvalds cannot know, that ttl is zero, so that packet 19041da177e4SLinus Torvalds will not leave this host and route is valid). 19051da177e4SLinus Torvalds Luckily, this hack is good workaround. 19061da177e4SLinus Torvalds */ 19071da177e4SLinus Torvalds 1908813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 19091da177e4SLinus Torvalds goto make_route; 19101da177e4SLinus Torvalds } 1911a210d01aSJulian Anastasov 1912813b3b5dSDavid S. Miller if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) { 1913a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 1914813b3b5dSDavid S. Miller if (!__ip_dev_find(net, fl4->saddr, false)) 1915a210d01aSJulian Anastasov goto out; 19161da177e4SLinus Torvalds } 1917a210d01aSJulian Anastasov } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds 1920813b3b5dSDavid S. Miller if (fl4->flowi4_oif) { 1921813b3b5dSDavid S. Miller dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif); 1922b23dd4feSDavid S. Miller rth = ERR_PTR(-ENODEV); 19231da177e4SLinus Torvalds if (dev_out == NULL) 19241da177e4SLinus Torvalds goto out; 1925e5ed6399SHerbert Xu 1926e5ed6399SHerbert Xu /* RACE: Check return value of inet_select_addr instead. */ 1927fc75fc83SEric Dumazet if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) { 1928b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH); 1929fc75fc83SEric Dumazet goto out; 1930fc75fc83SEric Dumazet } 1931813b3b5dSDavid S. Miller if (ipv4_is_local_multicast(fl4->daddr) || 1932813b3b5dSDavid S. Miller ipv4_is_lbcast(fl4->daddr)) { 1933813b3b5dSDavid S. Miller if (!fl4->saddr) 1934813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 19351da177e4SLinus Torvalds RT_SCOPE_LINK); 19361da177e4SLinus Torvalds goto make_route; 19371da177e4SLinus Torvalds } 1938813b3b5dSDavid S. Miller if (fl4->saddr) { 1939813b3b5dSDavid S. Miller if (ipv4_is_multicast(fl4->daddr)) 1940813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 1941813b3b5dSDavid S. Miller fl4->flowi4_scope); 1942813b3b5dSDavid S. Miller else if (!fl4->daddr) 1943813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 19441da177e4SLinus Torvalds RT_SCOPE_HOST); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 1948813b3b5dSDavid S. Miller if (!fl4->daddr) { 1949813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr; 1950813b3b5dSDavid S. Miller if (!fl4->daddr) 1951813b3b5dSDavid S. Miller fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK); 1952b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 1953813b3b5dSDavid S. Miller fl4->flowi4_oif = net->loopback_dev->ifindex; 19541da177e4SLinus Torvalds res.type = RTN_LOCAL; 19551da177e4SLinus Torvalds flags |= RTCF_LOCAL; 19561da177e4SLinus Torvalds goto make_route; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 1959813b3b5dSDavid S. Miller if (fib_lookup(net, fl4, &res)) { 19601da177e4SLinus Torvalds res.fi = NULL; 19618b96d22dSDavid S. Miller res.table = NULL; 1962813b3b5dSDavid S. Miller if (fl4->flowi4_oif) { 19631da177e4SLinus Torvalds /* Apparently, routing tables are wrong. Assume, 19641da177e4SLinus Torvalds that the destination is on link. 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds WHY? DW. 19671da177e4SLinus Torvalds Because we are allowed to send to iface 19681da177e4SLinus Torvalds even if it has NO routes and NO assigned 19691da177e4SLinus Torvalds addresses. When oif is specified, routing 19701da177e4SLinus Torvalds tables are looked up with only one purpose: 19711da177e4SLinus Torvalds to catch if destination is gatewayed, rather than 19721da177e4SLinus Torvalds direct. Moreover, if MSG_DONTROUTE is set, 19731da177e4SLinus Torvalds we send packet, ignoring both routing tables 19741da177e4SLinus Torvalds and ifaddr state. --ANK 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds We could make it even if oif is unknown, 19781da177e4SLinus Torvalds likely IPv6, but we do not. 19791da177e4SLinus Torvalds */ 19801da177e4SLinus Torvalds 1981813b3b5dSDavid S. Miller if (fl4->saddr == 0) 1982813b3b5dSDavid S. Miller fl4->saddr = inet_select_addr(dev_out, 0, 19831da177e4SLinus Torvalds RT_SCOPE_LINK); 19841da177e4SLinus Torvalds res.type = RTN_UNICAST; 19851da177e4SLinus Torvalds goto make_route; 19861da177e4SLinus Torvalds } 1987b23dd4feSDavid S. Miller rth = ERR_PTR(-ENETUNREACH); 19881da177e4SLinus Torvalds goto out; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 1992813b3b5dSDavid S. Miller if (!fl4->saddr) { 19939fc3bbb4SJoel Sing if (res.fi->fib_prefsrc) 1994813b3b5dSDavid S. Miller fl4->saddr = res.fi->fib_prefsrc; 19959fc3bbb4SJoel Sing else 1996813b3b5dSDavid S. Miller fl4->saddr = fl4->daddr; 19979fc3bbb4SJoel Sing } 1998b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 1999813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 20001da177e4SLinus Torvalds res.fi = NULL; 20011da177e4SLinus Torvalds flags |= RTCF_LOCAL; 20021da177e4SLinus Torvalds goto make_route; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 2006813b3b5dSDavid S. Miller if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0) 20071b7fe593SDavid S. Miller fib_select_multipath(&res); 20081da177e4SLinus Torvalds else 20091da177e4SLinus Torvalds #endif 201021d8c49eSDavid S. Miller if (!res.prefixlen && 201121d8c49eSDavid S. Miller res.table->tb_num_default > 1 && 2012813b3b5dSDavid S. Miller res.type == RTN_UNICAST && !fl4->flowi4_oif) 20130c838ff1SDavid S. Miller fib_select_default(&res); 20141da177e4SLinus Torvalds 2015813b3b5dSDavid S. Miller if (!fl4->saddr) 2016813b3b5dSDavid S. Miller fl4->saddr = FIB_RES_PREFSRC(net, res); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds dev_out = FIB_RES_DEV(res); 2019813b3b5dSDavid S. Miller fl4->flowi4_oif = dev_out->ifindex; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds make_route: 20231a00fee4SDavid Miller rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags); 20241da177e4SLinus Torvalds 2025010c2708SDavid S. Miller out: 2026010c2708SDavid S. Miller rcu_read_unlock(); 2027b23dd4feSDavid S. Miller return rth; 20281da177e4SLinus Torvalds } 2029d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key); 2030d8c97a94SArnaldo Carvalho de Melo 2031ae2688d5SJianzhao Wang static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie) 2032ae2688d5SJianzhao Wang { 2033ae2688d5SJianzhao Wang return NULL; 2034ae2688d5SJianzhao Wang } 2035ae2688d5SJianzhao Wang 2036ebb762f2SSteffen Klassert static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst) 2037ec831ea7SRoland Dreier { 2038618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 2039618f9bc7SSteffen Klassert 2040618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 2041ec831ea7SRoland Dreier } 2042ec831ea7SRoland Dreier 20436700c270SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 20446700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 204514e50e57SDavid S. Miller { 204614e50e57SDavid S. Miller } 204714e50e57SDavid S. Miller 20486700c270SDavid S. Miller static void ipv4_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 20496700c270SDavid S. Miller struct sk_buff *skb) 2050b587ee3bSDavid S. Miller { 2051b587ee3bSDavid S. Miller } 2052b587ee3bSDavid S. Miller 20530972ddb2SHeld Bernhard static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst, 20540972ddb2SHeld Bernhard unsigned long old) 20550972ddb2SHeld Bernhard { 20560972ddb2SHeld Bernhard return NULL; 20570972ddb2SHeld Bernhard } 20580972ddb2SHeld Bernhard 205914e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = { 206014e50e57SDavid S. Miller .family = AF_INET, 206109640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 206214e50e57SDavid S. Miller .destroy = ipv4_dst_destroy, 2063ae2688d5SJianzhao Wang .check = ipv4_blackhole_dst_check, 2064ebb762f2SSteffen Klassert .mtu = ipv4_blackhole_mtu, 2065214f45c9SEric Dumazet .default_advmss = ipv4_default_advmss, 206614e50e57SDavid S. Miller .update_pmtu = ipv4_rt_blackhole_update_pmtu, 2067b587ee3bSDavid S. Miller .redirect = ipv4_rt_blackhole_redirect, 20680972ddb2SHeld Bernhard .cow_metrics = ipv4_rt_blackhole_cow_metrics, 2069d3aaeb38SDavid S. Miller .neigh_lookup = ipv4_neigh_lookup, 207014e50e57SDavid S. Miller }; 207114e50e57SDavid S. Miller 20722774c131SDavid S. Miller struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig) 207314e50e57SDavid S. Miller { 20742774c131SDavid S. Miller struct rtable *ort = (struct rtable *) dst_orig; 2075f5b0a874SDavid S. Miller struct rtable *rt; 207614e50e57SDavid S. Miller 2077f5b0a874SDavid S. Miller rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE, 0); 207814e50e57SDavid S. Miller if (rt) { 2079d8d1f30bSChangli Gao struct dst_entry *new = &rt->dst; 208014e50e57SDavid S. Miller 208114e50e57SDavid S. Miller new->__use = 1; 2082352e512cSHerbert Xu new->input = dst_discard; 2083352e512cSHerbert Xu new->output = dst_discard; 208414e50e57SDavid S. Miller 2085d8d1f30bSChangli Gao new->dev = ort->dst.dev; 208614e50e57SDavid S. Miller if (new->dev) 208714e50e57SDavid S. Miller dev_hold(new->dev); 208814e50e57SDavid S. Miller 20891b86a58fSOGAWA Hirofumi rt->rt_route_iif = ort->rt_route_iif; 20905e2b61f7SDavid S. Miller rt->rt_iif = ort->rt_iif; 20915e2b61f7SDavid S. Miller rt->rt_oif = ort->rt_oif; 20925943634fSDavid S. Miller rt->rt_pmtu = ort->rt_pmtu; 209314e50e57SDavid S. Miller 2094e84f84f2SDenis V. Lunev rt->rt_genid = rt_genid(net); 209514e50e57SDavid S. Miller rt->rt_flags = ort->rt_flags; 209614e50e57SDavid S. Miller rt->rt_type = ort->rt_type; 209714e50e57SDavid S. Miller rt->rt_gateway = ort->rt_gateway; 209862fa8a84SDavid S. Miller rt->fi = ort->fi; 209962fa8a84SDavid S. Miller if (rt->fi) 210062fa8a84SDavid S. Miller atomic_inc(&rt->fi->fib_clntref); 210114e50e57SDavid S. Miller 210214e50e57SDavid S. Miller dst_free(new); 210314e50e57SDavid S. Miller } 210414e50e57SDavid S. Miller 21052774c131SDavid S. Miller dst_release(dst_orig); 21062774c131SDavid S. Miller 21072774c131SDavid S. Miller return rt ? &rt->dst : ERR_PTR(-ENOMEM); 210814e50e57SDavid S. Miller } 210914e50e57SDavid S. Miller 21109d6ec938SDavid S. Miller struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4, 2111273447b3SDavid S. Miller struct sock *sk) 21121da177e4SLinus Torvalds { 21139d6ec938SDavid S. Miller struct rtable *rt = __ip_route_output_key(net, flp4); 21141da177e4SLinus Torvalds 2115b23dd4feSDavid S. Miller if (IS_ERR(rt)) 2116b23dd4feSDavid S. Miller return rt; 21171da177e4SLinus Torvalds 211856157872SDavid S. Miller if (flp4->flowi4_proto) 21199d6ec938SDavid S. Miller rt = (struct rtable *) xfrm_lookup(net, &rt->dst, 21209d6ec938SDavid S. Miller flowi4_to_flowi(flp4), 21219d6ec938SDavid S. Miller sk, 0); 21221da177e4SLinus Torvalds 2123b23dd4feSDavid S. Miller return rt; 21241da177e4SLinus Torvalds } 2125d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow); 2126d8c97a94SArnaldo Carvalho de Melo 2127f1ce3062SDavid S. Miller static int rt_fill_info(struct net *net, __be32 dst, __be32 src, 2128f1ce3062SDavid S. Miller struct flowi4 *fl4, struct sk_buff *skb, u32 pid, 2129f1ce3062SDavid S. Miller u32 seq, int event, int nowait, unsigned int flags) 21301da177e4SLinus Torvalds { 2131511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 21321da177e4SLinus Torvalds struct rtmsg *r; 21331da177e4SLinus Torvalds struct nlmsghdr *nlh; 21342bc8ca40SSteffen Klassert unsigned long expires = 0; 2135f185071dSDavid S. Miller u32 error; 2136521f5490SJulian Anastasov u32 metrics[RTAX_MAX]; 2137be403ea1SThomas Graf 2138be403ea1SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags); 2139be403ea1SThomas Graf if (nlh == NULL) 214026932566SPatrick McHardy return -EMSGSIZE; 2141be403ea1SThomas Graf 2142be403ea1SThomas Graf r = nlmsg_data(nlh); 21431da177e4SLinus Torvalds r->rtm_family = AF_INET; 21441da177e4SLinus Torvalds r->rtm_dst_len = 32; 21451da177e4SLinus Torvalds r->rtm_src_len = 0; 2146d6c0a4f6SDavid Miller r->rtm_tos = fl4->flowi4_tos; 21471da177e4SLinus Torvalds r->rtm_table = RT_TABLE_MAIN; 2148f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN)) 2149f3756b79SDavid S. Miller goto nla_put_failure; 21501da177e4SLinus Torvalds r->rtm_type = rt->rt_type; 21511da177e4SLinus Torvalds r->rtm_scope = RT_SCOPE_UNIVERSE; 21521da177e4SLinus Torvalds r->rtm_protocol = RTPROT_UNSPEC; 21531da177e4SLinus Torvalds r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; 21541da177e4SLinus Torvalds if (rt->rt_flags & RTCF_NOTIFY) 21551da177e4SLinus Torvalds r->rtm_flags |= RTM_F_NOTIFY; 2156be403ea1SThomas Graf 2157f1ce3062SDavid S. Miller if (nla_put_be32(skb, RTA_DST, dst)) 2158f3756b79SDavid S. Miller goto nla_put_failure; 21591a00fee4SDavid Miller if (src) { 21601da177e4SLinus Torvalds r->rtm_src_len = 32; 21611a00fee4SDavid Miller if (nla_put_be32(skb, RTA_SRC, src)) 2162f3756b79SDavid S. Miller goto nla_put_failure; 21631da177e4SLinus Torvalds } 2164f3756b79SDavid S. Miller if (rt->dst.dev && 2165f3756b79SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2166f3756b79SDavid S. Miller goto nla_put_failure; 2167c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2168f3756b79SDavid S. Miller if (rt->dst.tclassid && 2169f3756b79SDavid S. Miller nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid)) 2170f3756b79SDavid S. Miller goto nla_put_failure; 21711da177e4SLinus Torvalds #endif 217241347dcdSDavid S. Miller if (!rt_is_input_route(rt) && 2173d6c0a4f6SDavid Miller fl4->saddr != src) { 2174d6c0a4f6SDavid Miller if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr)) 2175f3756b79SDavid S. Miller goto nla_put_failure; 2176f3756b79SDavid S. Miller } 2177f8126f1dSDavid S. Miller if (rt->rt_gateway && 2178f3756b79SDavid S. Miller nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway)) 2179f3756b79SDavid S. Miller goto nla_put_failure; 2180be403ea1SThomas Graf 2181521f5490SJulian Anastasov memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 2182521f5490SJulian Anastasov if (rt->rt_pmtu) 2183521f5490SJulian Anastasov metrics[RTAX_MTU - 1] = rt->rt_pmtu; 2184521f5490SJulian Anastasov if (rtnetlink_put_metrics(skb, metrics) < 0) 2185be403ea1SThomas Graf goto nla_put_failure; 2186be403ea1SThomas Graf 2187b4869889SDavid Miller if (fl4->flowi4_mark && 2188b4869889SDavid Miller nla_put_be32(skb, RTA_MARK, fl4->flowi4_mark)) 2189f3756b79SDavid S. Miller goto nla_put_failure; 2190963bfeeeSEric Dumazet 2191d8d1f30bSChangli Gao error = rt->dst.error; 21925943634fSDavid S. Miller expires = rt->dst.expires; 21932bc8ca40SSteffen Klassert if (expires) { 21942bc8ca40SSteffen Klassert if (time_before(jiffies, expires)) 2195fe6fe792SEric Dumazet expires -= jiffies; 21962bc8ca40SSteffen Klassert else 21972bc8ca40SSteffen Klassert expires = 0; 21982bc8ca40SSteffen Klassert } 2199be403ea1SThomas Graf 2200c7537967SDavid S. Miller if (rt_is_input_route(rt)) { 2201f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, rt->rt_iif)) 2202f3756b79SDavid S. Miller goto nla_put_failure; 22031da177e4SLinus Torvalds } 22041da177e4SLinus Torvalds 2205f185071dSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0) 2206e3703b3dSThomas Graf goto nla_put_failure; 22071da177e4SLinus Torvalds 2208be403ea1SThomas Graf return nlmsg_end(skb, nlh); 2209be403ea1SThomas Graf 2210be403ea1SThomas Graf nla_put_failure: 221126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 221226932566SPatrick McHardy return -EMSGSIZE; 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 221563f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg) 22161da177e4SLinus Torvalds { 22173b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2218d889ce3bSThomas Graf struct rtmsg *rtm; 2219d889ce3bSThomas Graf struct nlattr *tb[RTA_MAX+1]; 22201da177e4SLinus Torvalds struct rtable *rt = NULL; 2221d6c0a4f6SDavid Miller struct flowi4 fl4; 22229e12bb22SAl Viro __be32 dst = 0; 22239e12bb22SAl Viro __be32 src = 0; 22249e12bb22SAl Viro u32 iif; 2225d889ce3bSThomas Graf int err; 2226963bfeeeSEric Dumazet int mark; 22271da177e4SLinus Torvalds struct sk_buff *skb; 22281da177e4SLinus Torvalds 2229d889ce3bSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy); 2230d889ce3bSThomas Graf if (err < 0) 2231d889ce3bSThomas Graf goto errout; 2232d889ce3bSThomas Graf 2233d889ce3bSThomas Graf rtm = nlmsg_data(nlh); 2234d889ce3bSThomas Graf 22351da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2236d889ce3bSThomas Graf if (skb == NULL) { 2237d889ce3bSThomas Graf err = -ENOBUFS; 2238d889ce3bSThomas Graf goto errout; 2239d889ce3bSThomas Graf } 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 22421da177e4SLinus Torvalds through good chunk of routing engine. 22431da177e4SLinus Torvalds */ 2244459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 2245c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2246d2c962b8SStephen Hemminger 2247d2c962b8SStephen Hemminger /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */ 2248eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = IPPROTO_ICMP; 22491da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr)); 22501da177e4SLinus Torvalds 225117fb2c64SAl Viro src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0; 225217fb2c64SAl Viro dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0; 2253d889ce3bSThomas Graf iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 2254963bfeeeSEric Dumazet mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 22551da177e4SLinus Torvalds 2256d6c0a4f6SDavid Miller memset(&fl4, 0, sizeof(fl4)); 2257d6c0a4f6SDavid Miller fl4.daddr = dst; 2258d6c0a4f6SDavid Miller fl4.saddr = src; 2259d6c0a4f6SDavid Miller fl4.flowi4_tos = rtm->rtm_tos; 2260d6c0a4f6SDavid Miller fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0; 2261d6c0a4f6SDavid Miller fl4.flowi4_mark = mark; 2262d6c0a4f6SDavid Miller 22631da177e4SLinus Torvalds if (iif) { 2264d889ce3bSThomas Graf struct net_device *dev; 2265d889ce3bSThomas Graf 22661937504dSDenis V. Lunev dev = __dev_get_by_index(net, iif); 2267d889ce3bSThomas Graf if (dev == NULL) { 22681da177e4SLinus Torvalds err = -ENODEV; 2269d889ce3bSThomas Graf goto errout_free; 2270d889ce3bSThomas Graf } 2271d889ce3bSThomas Graf 22721da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 22731da177e4SLinus Torvalds skb->dev = dev; 2274963bfeeeSEric Dumazet skb->mark = mark; 22751da177e4SLinus Torvalds local_bh_disable(); 22761da177e4SLinus Torvalds err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev); 22771da177e4SLinus Torvalds local_bh_enable(); 2278d889ce3bSThomas Graf 2279511c3f92SEric Dumazet rt = skb_rtable(skb); 2280d8d1f30bSChangli Gao if (err == 0 && rt->dst.error) 2281d8d1f30bSChangli Gao err = -rt->dst.error; 22821da177e4SLinus Torvalds } else { 22839d6ec938SDavid S. Miller rt = ip_route_output_key(net, &fl4); 2284b23dd4feSDavid S. Miller 2285b23dd4feSDavid S. Miller err = 0; 2286b23dd4feSDavid S. Miller if (IS_ERR(rt)) 2287b23dd4feSDavid S. Miller err = PTR_ERR(rt); 22881da177e4SLinus Torvalds } 2289d889ce3bSThomas Graf 22901da177e4SLinus Torvalds if (err) 2291d889ce3bSThomas Graf goto errout_free; 22921da177e4SLinus Torvalds 2293d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 22941da177e4SLinus Torvalds if (rtm->rtm_flags & RTM_F_NOTIFY) 22951da177e4SLinus Torvalds rt->rt_flags |= RTCF_NOTIFY; 22961da177e4SLinus Torvalds 2297f1ce3062SDavid S. Miller err = rt_fill_info(net, dst, src, &fl4, skb, 22981a00fee4SDavid Miller NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, 2299b6544c0bSJamal Hadi Salim RTM_NEWROUTE, 0, 0); 2300d889ce3bSThomas Graf if (err <= 0) 2301d889ce3bSThomas Graf goto errout_free; 23021da177e4SLinus Torvalds 23031937504dSDenis V. Lunev err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2304d889ce3bSThomas Graf errout: 23052942e900SThomas Graf return err; 23061da177e4SLinus Torvalds 2307d889ce3bSThomas Graf errout_free: 23081da177e4SLinus Torvalds kfree_skb(skb); 2309d889ce3bSThomas Graf goto errout; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb) 23131da177e4SLinus Torvalds { 23141da177e4SLinus Torvalds return skb->len; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev) 23181da177e4SLinus Torvalds { 231976e6ebfbSDenis V. Lunev rt_cache_flush(dev_net(in_dev->dev), 0); 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 232381c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write, 23248d65af78SAlexey Dobriyan void __user *buffer, 23251da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 23261da177e4SLinus Torvalds { 23271da177e4SLinus Torvalds if (write) { 2328639e104fSDenis V. Lunev int flush_delay; 232981c684d1SDenis V. Lunev ctl_table ctl; 233039a23e75SDenis V. Lunev struct net *net; 2331639e104fSDenis V. Lunev 233281c684d1SDenis V. Lunev memcpy(&ctl, __ctl, sizeof(ctl)); 233381c684d1SDenis V. Lunev ctl.data = &flush_delay; 23348d65af78SAlexey Dobriyan proc_dointvec(&ctl, write, buffer, lenp, ppos); 2335639e104fSDenis V. Lunev 233681c684d1SDenis V. Lunev net = (struct net *)__ctl->extra1; 233739a23e75SDenis V. Lunev rt_cache_flush(net, flush_delay); 23381da177e4SLinus Torvalds return 0; 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds return -EINVAL; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 2344eeb61f71SAl Viro static ctl_table ipv4_route_table[] = { 23451da177e4SLinus Torvalds { 23461da177e4SLinus Torvalds .procname = "gc_thresh", 23471da177e4SLinus Torvalds .data = &ipv4_dst_ops.gc_thresh, 23481da177e4SLinus Torvalds .maxlen = sizeof(int), 23491da177e4SLinus Torvalds .mode = 0644, 23506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 23511da177e4SLinus Torvalds }, 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds .procname = "max_size", 23541da177e4SLinus Torvalds .data = &ip_rt_max_size, 23551da177e4SLinus Torvalds .maxlen = sizeof(int), 23561da177e4SLinus Torvalds .mode = 0644, 23576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 23581da177e4SLinus Torvalds }, 23591da177e4SLinus Torvalds { 23601da177e4SLinus Torvalds /* Deprecated. Use gc_min_interval_ms */ 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds .procname = "gc_min_interval", 23631da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 23641da177e4SLinus Torvalds .maxlen = sizeof(int), 23651da177e4SLinus Torvalds .mode = 0644, 23666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 23671da177e4SLinus Torvalds }, 23681da177e4SLinus Torvalds { 23691da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 23701da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 23711da177e4SLinus Torvalds .maxlen = sizeof(int), 23721da177e4SLinus Torvalds .mode = 0644, 23736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 23741da177e4SLinus Torvalds }, 23751da177e4SLinus Torvalds { 23761da177e4SLinus Torvalds .procname = "gc_timeout", 23771da177e4SLinus Torvalds .data = &ip_rt_gc_timeout, 23781da177e4SLinus Torvalds .maxlen = sizeof(int), 23791da177e4SLinus Torvalds .mode = 0644, 23806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 23811da177e4SLinus Torvalds }, 23821da177e4SLinus Torvalds { 23839f28a2fcSEric Dumazet .procname = "gc_interval", 23849f28a2fcSEric Dumazet .data = &ip_rt_gc_interval, 23859f28a2fcSEric Dumazet .maxlen = sizeof(int), 23869f28a2fcSEric Dumazet .mode = 0644, 23879f28a2fcSEric Dumazet .proc_handler = proc_dointvec_jiffies, 23889f28a2fcSEric Dumazet }, 23899f28a2fcSEric Dumazet { 23901da177e4SLinus Torvalds .procname = "redirect_load", 23911da177e4SLinus Torvalds .data = &ip_rt_redirect_load, 23921da177e4SLinus Torvalds .maxlen = sizeof(int), 23931da177e4SLinus Torvalds .mode = 0644, 23946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 23951da177e4SLinus Torvalds }, 23961da177e4SLinus Torvalds { 23971da177e4SLinus Torvalds .procname = "redirect_number", 23981da177e4SLinus Torvalds .data = &ip_rt_redirect_number, 23991da177e4SLinus Torvalds .maxlen = sizeof(int), 24001da177e4SLinus Torvalds .mode = 0644, 24016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24021da177e4SLinus Torvalds }, 24031da177e4SLinus Torvalds { 24041da177e4SLinus Torvalds .procname = "redirect_silence", 24051da177e4SLinus Torvalds .data = &ip_rt_redirect_silence, 24061da177e4SLinus Torvalds .maxlen = sizeof(int), 24071da177e4SLinus Torvalds .mode = 0644, 24086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24091da177e4SLinus Torvalds }, 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds .procname = "error_cost", 24121da177e4SLinus Torvalds .data = &ip_rt_error_cost, 24131da177e4SLinus Torvalds .maxlen = sizeof(int), 24141da177e4SLinus Torvalds .mode = 0644, 24156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24161da177e4SLinus Torvalds }, 24171da177e4SLinus Torvalds { 24181da177e4SLinus Torvalds .procname = "error_burst", 24191da177e4SLinus Torvalds .data = &ip_rt_error_burst, 24201da177e4SLinus Torvalds .maxlen = sizeof(int), 24211da177e4SLinus Torvalds .mode = 0644, 24226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24231da177e4SLinus Torvalds }, 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds .procname = "gc_elasticity", 24261da177e4SLinus Torvalds .data = &ip_rt_gc_elasticity, 24271da177e4SLinus Torvalds .maxlen = sizeof(int), 24281da177e4SLinus Torvalds .mode = 0644, 24296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24301da177e4SLinus Torvalds }, 24311da177e4SLinus Torvalds { 24321da177e4SLinus Torvalds .procname = "mtu_expires", 24331da177e4SLinus Torvalds .data = &ip_rt_mtu_expires, 24341da177e4SLinus Torvalds .maxlen = sizeof(int), 24351da177e4SLinus Torvalds .mode = 0644, 24366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 24371da177e4SLinus Torvalds }, 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds .procname = "min_pmtu", 24401da177e4SLinus Torvalds .data = &ip_rt_min_pmtu, 24411da177e4SLinus Torvalds .maxlen = sizeof(int), 24421da177e4SLinus Torvalds .mode = 0644, 24436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24441da177e4SLinus Torvalds }, 24451da177e4SLinus Torvalds { 24461da177e4SLinus Torvalds .procname = "min_adv_mss", 24471da177e4SLinus Torvalds .data = &ip_rt_min_advmss, 24481da177e4SLinus Torvalds .maxlen = sizeof(int), 24491da177e4SLinus Torvalds .mode = 0644, 24506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 24511da177e4SLinus Torvalds }, 2452f8572d8fSEric W. Biederman { } 24531da177e4SLinus Torvalds }; 245439a23e75SDenis V. Lunev 245539a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = { 245639a23e75SDenis V. Lunev { 245739a23e75SDenis V. Lunev .procname = "flush", 245839a23e75SDenis V. Lunev .maxlen = sizeof(int), 245939a23e75SDenis V. Lunev .mode = 0200, 24606d9f239aSAlexey Dobriyan .proc_handler = ipv4_sysctl_rtcache_flush, 246139a23e75SDenis V. Lunev }, 2462f8572d8fSEric W. Biederman { }, 246339a23e75SDenis V. Lunev }; 246439a23e75SDenis V. Lunev 246539a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net) 246639a23e75SDenis V. Lunev { 246739a23e75SDenis V. Lunev struct ctl_table *tbl; 246839a23e75SDenis V. Lunev 246939a23e75SDenis V. Lunev tbl = ipv4_route_flush_table; 247009ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 247139a23e75SDenis V. Lunev tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL); 247239a23e75SDenis V. Lunev if (tbl == NULL) 247339a23e75SDenis V. Lunev goto err_dup; 247439a23e75SDenis V. Lunev } 247539a23e75SDenis V. Lunev tbl[0].extra1 = net; 247639a23e75SDenis V. Lunev 2477ec8f23ceSEric W. Biederman net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl); 247839a23e75SDenis V. Lunev if (net->ipv4.route_hdr == NULL) 247939a23e75SDenis V. Lunev goto err_reg; 248039a23e75SDenis V. Lunev return 0; 248139a23e75SDenis V. Lunev 248239a23e75SDenis V. Lunev err_reg: 248339a23e75SDenis V. Lunev if (tbl != ipv4_route_flush_table) 248439a23e75SDenis V. Lunev kfree(tbl); 248539a23e75SDenis V. Lunev err_dup: 248639a23e75SDenis V. Lunev return -ENOMEM; 248739a23e75SDenis V. Lunev } 248839a23e75SDenis V. Lunev 248939a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net) 249039a23e75SDenis V. Lunev { 249139a23e75SDenis V. Lunev struct ctl_table *tbl; 249239a23e75SDenis V. Lunev 249339a23e75SDenis V. Lunev tbl = net->ipv4.route_hdr->ctl_table_arg; 249439a23e75SDenis V. Lunev unregister_net_sysctl_table(net->ipv4.route_hdr); 249539a23e75SDenis V. Lunev BUG_ON(tbl == ipv4_route_flush_table); 249639a23e75SDenis V. Lunev kfree(tbl); 249739a23e75SDenis V. Lunev } 249839a23e75SDenis V. Lunev 249939a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = { 250039a23e75SDenis V. Lunev .init = sysctl_route_net_init, 250139a23e75SDenis V. Lunev .exit = sysctl_route_net_exit, 250239a23e75SDenis V. Lunev }; 25031da177e4SLinus Torvalds #endif 25041da177e4SLinus Torvalds 25053ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net) 25069f5e97e5SDenis V. Lunev { 25073ee94372SNeil Horman get_random_bytes(&net->ipv4.rt_genid, 25083ee94372SNeil Horman sizeof(net->ipv4.rt_genid)); 2509436c3b66SDavid S. Miller get_random_bytes(&net->ipv4.dev_addr_genid, 2510436c3b66SDavid S. Miller sizeof(net->ipv4.dev_addr_genid)); 25119f5e97e5SDenis V. Lunev return 0; 25129f5e97e5SDenis V. Lunev } 25139f5e97e5SDenis V. Lunev 25143ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = { 25153ee94372SNeil Horman .init = rt_genid_init, 25169f5e97e5SDenis V. Lunev }; 25179f5e97e5SDenis V. Lunev 2518c3426b47SDavid S. Miller static int __net_init ipv4_inetpeer_init(struct net *net) 2519c3426b47SDavid S. Miller { 2520c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 2521c3426b47SDavid S. Miller 2522c3426b47SDavid S. Miller if (!bp) 2523c3426b47SDavid S. Miller return -ENOMEM; 2524c3426b47SDavid S. Miller inet_peer_base_init(bp); 2525c3426b47SDavid S. Miller net->ipv4.peers = bp; 2526c3426b47SDavid S. Miller return 0; 2527c3426b47SDavid S. Miller } 2528c3426b47SDavid S. Miller 2529c3426b47SDavid S. Miller static void __net_exit ipv4_inetpeer_exit(struct net *net) 2530c3426b47SDavid S. Miller { 2531c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv4.peers; 2532c3426b47SDavid S. Miller 2533c3426b47SDavid S. Miller net->ipv4.peers = NULL; 253456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 2535c3426b47SDavid S. Miller kfree(bp); 2536c3426b47SDavid S. Miller } 2537c3426b47SDavid S. Miller 2538c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv4_inetpeer_ops = { 2539c3426b47SDavid S. Miller .init = ipv4_inetpeer_init, 2540c3426b47SDavid S. Miller .exit = ipv4_inetpeer_exit, 2541c3426b47SDavid S. Miller }; 25429f5e97e5SDenis V. Lunev 2543c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 25447d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; 2545c7066f70SPatrick McHardy #endif /* CONFIG_IP_ROUTE_CLASSID */ 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds int __init ip_rt_init(void) 25481da177e4SLinus Torvalds { 2549424c4b70SEric Dumazet int rc = 0; 25501da177e4SLinus Torvalds 2551c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 25520dcec8c2SIngo Molnar ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct)); 25531da177e4SLinus Torvalds if (!ip_rt_acct) 25541da177e4SLinus Torvalds panic("IP: failed to allocate ip_rt_acct\n"); 25551da177e4SLinus Torvalds #endif 25561da177e4SLinus Torvalds 2557e5d679f3SAlexey Dobriyan ipv4_dst_ops.kmem_cachep = 2558e5d679f3SAlexey Dobriyan kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0, 255920c2df83SPaul Mundt SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 25601da177e4SLinus Torvalds 256114e50e57SDavid S. Miller ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep; 256214e50e57SDavid S. Miller 2563fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_ops) < 0) 2564fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_ops counter\n"); 2565fc66f95cSEric Dumazet 2566fc66f95cSEric Dumazet if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0) 2567fc66f95cSEric Dumazet panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n"); 2568fc66f95cSEric Dumazet 256989aef892SDavid S. Miller ipv4_dst_ops.gc_thresh = ~0; 257089aef892SDavid S. Miller ip_rt_max_size = INT_MAX; 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds devinet_init(); 25731da177e4SLinus Torvalds ip_fib_init(); 25741da177e4SLinus Torvalds 257573b38711SDenis V. Lunev if (ip_rt_proc_init()) 2576058bd4d2SJoe Perches pr_err("Unable to create route proc files\n"); 25771da177e4SLinus Torvalds #ifdef CONFIG_XFRM 25781da177e4SLinus Torvalds xfrm_init(); 2579a33bc5c1SNeil Horman xfrm4_init(ip_rt_max_size); 25801da177e4SLinus Torvalds #endif 2581c7ac8679SGreg Rose rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL); 258263f3444fSThomas Graf 258339a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL 258439a23e75SDenis V. Lunev register_pernet_subsys(&sysctl_route_ops); 258539a23e75SDenis V. Lunev #endif 25863ee94372SNeil Horman register_pernet_subsys(&rt_genid_ops); 2587c3426b47SDavid S. Miller register_pernet_subsys(&ipv4_inetpeer_ops); 25881da177e4SLinus Torvalds return rc; 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds 2591a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL 2592eeb61f71SAl Viro /* 2593eeb61f71SAl Viro * We really need to sanitize the damn ipv4 init order, then all 2594eeb61f71SAl Viro * this nonsense will go away. 2595eeb61f71SAl Viro */ 2596eeb61f71SAl Viro void __init ip_static_sysctl_init(void) 2597eeb61f71SAl Viro { 25984e5ca785SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table); 2599eeb61f71SAl Viro } 2600a1bc6eb4SAl Viro #endif 2601