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 651da177e4SLinus Torvalds #include <linux/module.h> 661da177e4SLinus Torvalds #include <asm/uaccess.h> 671da177e4SLinus Torvalds #include <asm/system.h> 681da177e4SLinus Torvalds #include <linux/bitops.h> 691da177e4SLinus Torvalds #include <linux/types.h> 701da177e4SLinus Torvalds #include <linux/kernel.h> 711da177e4SLinus Torvalds #include <linux/mm.h> 72424c4b70SEric Dumazet #include <linux/bootmem.h> 731da177e4SLinus Torvalds #include <linux/string.h> 741da177e4SLinus Torvalds #include <linux/socket.h> 751da177e4SLinus Torvalds #include <linux/sockios.h> 761da177e4SLinus Torvalds #include <linux/errno.h> 771da177e4SLinus Torvalds #include <linux/in.h> 781da177e4SLinus Torvalds #include <linux/inet.h> 791da177e4SLinus Torvalds #include <linux/netdevice.h> 801da177e4SLinus Torvalds #include <linux/proc_fs.h> 811da177e4SLinus Torvalds #include <linux/init.h> 8239c90eceSEric Dumazet #include <linux/workqueue.h> 831da177e4SLinus Torvalds #include <linux/skbuff.h> 841da177e4SLinus Torvalds #include <linux/inetdevice.h> 851da177e4SLinus Torvalds #include <linux/igmp.h> 861da177e4SLinus Torvalds #include <linux/pkt_sched.h> 871da177e4SLinus Torvalds #include <linux/mroute.h> 881da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 891da177e4SLinus Torvalds #include <linux/random.h> 901da177e4SLinus Torvalds #include <linux/jhash.h> 911da177e4SLinus Torvalds #include <linux/rcupdate.h> 921da177e4SLinus Torvalds #include <linux/times.h> 935a0e3ad6STejun Heo #include <linux/slab.h> 94352e512cSHerbert Xu #include <net/dst.h> 95457c4cbcSEric W. Biederman #include <net/net_namespace.h> 961da177e4SLinus Torvalds #include <net/protocol.h> 971da177e4SLinus Torvalds #include <net/ip.h> 981da177e4SLinus Torvalds #include <net/route.h> 991da177e4SLinus Torvalds #include <net/inetpeer.h> 1001da177e4SLinus Torvalds #include <net/sock.h> 1011da177e4SLinus Torvalds #include <net/ip_fib.h> 1021da177e4SLinus Torvalds #include <net/arp.h> 1031da177e4SLinus Torvalds #include <net/tcp.h> 1041da177e4SLinus Torvalds #include <net/icmp.h> 1051da177e4SLinus Torvalds #include <net/xfrm.h> 1068d71740cSTom Tucker #include <net/netevent.h> 10763f3444fSThomas Graf #include <net/rtnetlink.h> 1081da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1091da177e4SLinus Torvalds #include <linux/sysctl.h> 1101da177e4SLinus Torvalds #endif 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds #define RT_FL_TOS(oldflp) \ 1131da177e4SLinus Torvalds ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK))) 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #define IP_MAX_MTU 0xFFF0 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #define RT_GC_TIMEOUT (300*HZ) 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static int ip_rt_max_size; 120817bc4dbSStephen Hemminger static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT; 121817bc4dbSStephen Hemminger static int ip_rt_gc_interval __read_mostly = 60 * HZ; 122817bc4dbSStephen Hemminger static int ip_rt_gc_min_interval __read_mostly = HZ / 2; 123817bc4dbSStephen Hemminger static int ip_rt_redirect_number __read_mostly = 9; 124817bc4dbSStephen Hemminger static int ip_rt_redirect_load __read_mostly = HZ / 50; 125817bc4dbSStephen Hemminger static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1)); 126817bc4dbSStephen Hemminger static int ip_rt_error_cost __read_mostly = HZ; 127817bc4dbSStephen Hemminger static int ip_rt_error_burst __read_mostly = 5 * HZ; 128817bc4dbSStephen Hemminger static int ip_rt_gc_elasticity __read_mostly = 8; 129817bc4dbSStephen Hemminger static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ; 130817bc4dbSStephen Hemminger static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20; 131817bc4dbSStephen Hemminger static int ip_rt_min_advmss __read_mostly = 256; 1321080d709SNeil Horman static int rt_chain_length_max __read_mostly = 20; 1331da177e4SLinus Torvalds 134125bb8f5SEric Dumazet static struct delayed_work expires_work; 135125bb8f5SEric Dumazet static unsigned long expires_ljiffies; 1361da177e4SLinus Torvalds 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); 1421da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst); 1431da177e4SLinus Torvalds static void ipv4_dst_ifdown(struct dst_entry *dst, 1441da177e4SLinus Torvalds struct net_device *dev, int how); 1451da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst); 1461da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb); 1471da177e4SLinus Torvalds static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 148569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct dst_ops ipv4_dst_ops = { 1521da177e4SLinus Torvalds .family = AF_INET, 15309640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 1541da177e4SLinus Torvalds .gc = rt_garbage_collect, 1551da177e4SLinus Torvalds .check = ipv4_dst_check, 1561da177e4SLinus Torvalds .destroy = ipv4_dst_destroy, 1571da177e4SLinus Torvalds .ifdown = ipv4_dst_ifdown, 1581da177e4SLinus Torvalds .negative_advice = ipv4_negative_advice, 1591da177e4SLinus Torvalds .link_failure = ipv4_link_failure, 1601da177e4SLinus Torvalds .update_pmtu = ip_rt_update_pmtu, 1611ac06e03SHerbert Xu .local_out = __ip_local_out, 162e2422970SEric Dumazet .entries = ATOMIC_INIT(0), 1631da177e4SLinus Torvalds }; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds #define ECN_OR_COST(class) TC_PRIO_##class 1661da177e4SLinus Torvalds 1674839c52bSPhilippe De Muyter const __u8 ip_tos2prio[16] = { 1681da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1691da177e4SLinus Torvalds ECN_OR_COST(FILLER), 1701da177e4SLinus Torvalds TC_PRIO_BESTEFFORT, 1711da177e4SLinus Torvalds ECN_OR_COST(BESTEFFORT), 1721da177e4SLinus Torvalds TC_PRIO_BULK, 1731da177e4SLinus Torvalds ECN_OR_COST(BULK), 1741da177e4SLinus Torvalds TC_PRIO_BULK, 1751da177e4SLinus Torvalds ECN_OR_COST(BULK), 1761da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1771da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1781da177e4SLinus Torvalds TC_PRIO_INTERACTIVE, 1791da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE), 1801da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1811da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK), 1821da177e4SLinus Torvalds TC_PRIO_INTERACTIVE_BULK, 1831da177e4SLinus Torvalds ECN_OR_COST(INTERACTIVE_BULK) 1841da177e4SLinus Torvalds }; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * Route cache. 1891da177e4SLinus Torvalds */ 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* The locking scheme is rather straight forward: 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * 1) Read-Copy Update protects the buckets of the central route hash. 1941da177e4SLinus Torvalds * 2) Only writers remove entries, and they hold the lock 1951da177e4SLinus Torvalds * as they look at rtable reference counts. 1961da177e4SLinus Torvalds * 3) Only readers acquire references to rtable entries, 1971da177e4SLinus Torvalds * they do so with atomic increments and with the 1981da177e4SLinus Torvalds * lock held. 1991da177e4SLinus Torvalds */ 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds struct rt_hash_bucket { 2021da177e4SLinus Torvalds struct rtable *chain; 20322c047ccSEric Dumazet }; 2041080d709SNeil Horman 2058a25d5deSIngo Molnar #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \ 2068a25d5deSIngo Molnar defined(CONFIG_PROVE_LOCKING) 20722c047ccSEric Dumazet /* 20822c047ccSEric Dumazet * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks 20922c047ccSEric Dumazet * The size of this table is a power of two and depends on the number of CPUS. 21062051200SIngo Molnar * (on lockdep we have a quite big spinlock_t, so keep the size down there) 21122c047ccSEric Dumazet */ 21262051200SIngo Molnar #ifdef CONFIG_LOCKDEP 21362051200SIngo Molnar # define RT_HASH_LOCK_SZ 256 21462051200SIngo Molnar #else 21522c047ccSEric Dumazet # if NR_CPUS >= 32 21622c047ccSEric Dumazet # define RT_HASH_LOCK_SZ 4096 21722c047ccSEric Dumazet # elif NR_CPUS >= 16 21822c047ccSEric Dumazet # define RT_HASH_LOCK_SZ 2048 21922c047ccSEric Dumazet # elif NR_CPUS >= 8 22022c047ccSEric Dumazet # define RT_HASH_LOCK_SZ 1024 22122c047ccSEric Dumazet # elif NR_CPUS >= 4 22222c047ccSEric Dumazet # define RT_HASH_LOCK_SZ 512 22322c047ccSEric Dumazet # else 22422c047ccSEric Dumazet # define RT_HASH_LOCK_SZ 256 22522c047ccSEric Dumazet # endif 22662051200SIngo Molnar #endif 22722c047ccSEric Dumazet 22822c047ccSEric Dumazet static spinlock_t *rt_hash_locks; 22922c047ccSEric Dumazet # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)] 2301ff1cc20SPavel Emelyanov 2311ff1cc20SPavel Emelyanov static __init void rt_hash_lock_init(void) 2321ff1cc20SPavel Emelyanov { 2331ff1cc20SPavel Emelyanov int i; 2341ff1cc20SPavel Emelyanov 2351ff1cc20SPavel Emelyanov rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, 2361ff1cc20SPavel Emelyanov GFP_KERNEL); 2371ff1cc20SPavel Emelyanov if (!rt_hash_locks) 2381ff1cc20SPavel Emelyanov panic("IP: failed to allocate rt_hash_locks\n"); 2391ff1cc20SPavel Emelyanov 2401ff1cc20SPavel Emelyanov for (i = 0; i < RT_HASH_LOCK_SZ; i++) 2411ff1cc20SPavel Emelyanov spin_lock_init(&rt_hash_locks[i]); 24222c047ccSEric Dumazet } 24322c047ccSEric Dumazet #else 24422c047ccSEric Dumazet # define rt_hash_lock_addr(slot) NULL 2451ff1cc20SPavel Emelyanov 2461ff1cc20SPavel Emelyanov static inline void rt_hash_lock_init(void) 2471ff1cc20SPavel Emelyanov { 2481ff1cc20SPavel Emelyanov } 24922c047ccSEric Dumazet #endif 2501da177e4SLinus Torvalds 251817bc4dbSStephen Hemminger static struct rt_hash_bucket *rt_hash_table __read_mostly; 252817bc4dbSStephen Hemminger static unsigned rt_hash_mask __read_mostly; 253817bc4dbSStephen Hemminger static unsigned int rt_hash_log __read_mostly; 2541da177e4SLinus Torvalds 2552f970d83SEric Dumazet static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 25627f39c73SEric Dumazet #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field) 2571da177e4SLinus Torvalds 258b00180deSDenis V. Lunev static inline unsigned int rt_hash(__be32 daddr, __be32 saddr, int idx, 259b00180deSDenis V. Lunev int genid) 2601da177e4SLinus Torvalds { 2610eae88f3SEric Dumazet return jhash_3words((__force u32)daddr, (__force u32)saddr, 262b00180deSDenis V. Lunev idx, genid) 26329e75252SEric Dumazet & rt_hash_mask; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 266e84f84f2SDenis V. Lunev static inline int rt_genid(struct net *net) 267e84f84f2SDenis V. Lunev { 268e84f84f2SDenis V. Lunev return atomic_read(&net->ipv4.rt_genid); 269e84f84f2SDenis V. Lunev } 270e84f84f2SDenis V. Lunev 2711da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 2721da177e4SLinus Torvalds struct rt_cache_iter_state { 273a75e936fSDenis V. Lunev struct seq_net_private p; 2741da177e4SLinus Torvalds int bucket; 27529e75252SEric Dumazet int genid; 2761da177e4SLinus Torvalds }; 2771da177e4SLinus Torvalds 2781218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_first(struct seq_file *seq) 2791da177e4SLinus Torvalds { 2801218854aSYOSHIFUJI Hideaki struct rt_cache_iter_state *st = seq->private; 2811da177e4SLinus Torvalds struct rtable *r = NULL; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) { 284a6272665SEric Dumazet if (!rt_hash_table[st->bucket].chain) 285a6272665SEric Dumazet continue; 2861da177e4SLinus Torvalds rcu_read_lock_bh(); 287a898def2SPaul E. McKenney r = rcu_dereference_bh(rt_hash_table[st->bucket].chain); 28829e75252SEric Dumazet while (r) { 289d8d1f30bSChangli Gao if (dev_net(r->dst.dev) == seq_file_net(seq) && 290a75e936fSDenis V. Lunev r->rt_genid == st->genid) 29129e75252SEric Dumazet return r; 292d8d1f30bSChangli Gao r = rcu_dereference_bh(r->dst.rt_next); 29329e75252SEric Dumazet } 2941da177e4SLinus Torvalds rcu_read_unlock_bh(); 2951da177e4SLinus Torvalds } 29629e75252SEric Dumazet return r; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991218854aSYOSHIFUJI Hideaki static struct rtable *__rt_cache_get_next(struct seq_file *seq, 300642d6318SDenis V. Lunev struct rtable *r) 3011da177e4SLinus Torvalds { 3021218854aSYOSHIFUJI Hideaki struct rt_cache_iter_state *st = seq->private; 303a6272665SEric Dumazet 304d8d1f30bSChangli Gao r = r->dst.rt_next; 3051da177e4SLinus Torvalds while (!r) { 3061da177e4SLinus Torvalds rcu_read_unlock_bh(); 307a6272665SEric Dumazet do { 3081da177e4SLinus Torvalds if (--st->bucket < 0) 309a6272665SEric Dumazet return NULL; 310a6272665SEric Dumazet } while (!rt_hash_table[st->bucket].chain); 3111da177e4SLinus Torvalds rcu_read_lock_bh(); 3121da177e4SLinus Torvalds r = rt_hash_table[st->bucket].chain; 3131da177e4SLinus Torvalds } 314a898def2SPaul E. McKenney return rcu_dereference_bh(r); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_next(struct seq_file *seq, 318642d6318SDenis V. Lunev struct rtable *r) 319642d6318SDenis V. Lunev { 3201218854aSYOSHIFUJI Hideaki struct rt_cache_iter_state *st = seq->private; 3211218854aSYOSHIFUJI Hideaki while ((r = __rt_cache_get_next(seq, r)) != NULL) { 322d8d1f30bSChangli Gao if (dev_net(r->dst.dev) != seq_file_net(seq)) 323a75e936fSDenis V. Lunev continue; 324642d6318SDenis V. Lunev if (r->rt_genid == st->genid) 325642d6318SDenis V. Lunev break; 326642d6318SDenis V. Lunev } 327642d6318SDenis V. Lunev return r; 328642d6318SDenis V. Lunev } 329642d6318SDenis V. Lunev 3301218854aSYOSHIFUJI Hideaki static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos) 3311da177e4SLinus Torvalds { 3321218854aSYOSHIFUJI Hideaki struct rtable *r = rt_cache_get_first(seq); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds if (r) 3351218854aSYOSHIFUJI Hideaki while (pos && (r = rt_cache_get_next(seq, r))) 3361da177e4SLinus Torvalds --pos; 3371da177e4SLinus Torvalds return pos ? NULL : r; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 3411da177e4SLinus Torvalds { 34229e75252SEric Dumazet struct rt_cache_iter_state *st = seq->private; 34329e75252SEric Dumazet if (*pos) 3441218854aSYOSHIFUJI Hideaki return rt_cache_get_idx(seq, *pos - 1); 345e84f84f2SDenis V. Lunev st->genid = rt_genid(seq_file_net(seq)); 34629e75252SEric Dumazet return SEQ_START_TOKEN; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3501da177e4SLinus Torvalds { 35129e75252SEric Dumazet struct rtable *r; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 3541218854aSYOSHIFUJI Hideaki r = rt_cache_get_first(seq); 3551da177e4SLinus Torvalds else 3561218854aSYOSHIFUJI Hideaki r = rt_cache_get_next(seq, v); 3571da177e4SLinus Torvalds ++*pos; 3581da177e4SLinus Torvalds return r; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds static void rt_cache_seq_stop(struct seq_file *seq, void *v) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds if (v && v != SEQ_START_TOKEN) 3641da177e4SLinus Torvalds rcu_read_unlock_bh(); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds static int rt_cache_seq_show(struct seq_file *seq, void *v) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 3701da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 3711da177e4SLinus Torvalds "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t" 3721da177e4SLinus Torvalds "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t" 3731da177e4SLinus Torvalds "HHUptod\tSpecDst"); 3741da177e4SLinus Torvalds else { 3751da177e4SLinus Torvalds struct rtable *r = v; 3765e659e4cSPavel Emelyanov int len; 3771da177e4SLinus Torvalds 3780eae88f3SEric Dumazet seq_printf(seq, "%s\t%08X\t%08X\t%8X\t%d\t%u\t%d\t" 3790eae88f3SEric Dumazet "%08X\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X%n", 380d8d1f30bSChangli Gao r->dst.dev ? r->dst.dev->name : "*", 3810eae88f3SEric Dumazet (__force u32)r->rt_dst, 3820eae88f3SEric Dumazet (__force u32)r->rt_gateway, 383d8d1f30bSChangli Gao r->rt_flags, atomic_read(&r->dst.__refcnt), 384d8d1f30bSChangli Gao r->dst.__use, 0, (__force u32)r->rt_src, 385d8d1f30bSChangli Gao (dst_metric(&r->dst, RTAX_ADVMSS) ? 386d8d1f30bSChangli Gao (int)dst_metric(&r->dst, RTAX_ADVMSS) + 40 : 0), 387d8d1f30bSChangli Gao dst_metric(&r->dst, RTAX_WINDOW), 388d8d1f30bSChangli Gao (int)((dst_metric(&r->dst, RTAX_RTT) >> 3) + 389d8d1f30bSChangli Gao dst_metric(&r->dst, RTAX_RTTVAR)), 3901da177e4SLinus Torvalds r->fl.fl4_tos, 391d8d1f30bSChangli Gao r->dst.hh ? atomic_read(&r->dst.hh->hh_refcnt) : -1, 392d8d1f30bSChangli Gao r->dst.hh ? (r->dst.hh->hh_output == 3931da177e4SLinus Torvalds dev_queue_xmit) : 0, 3945e659e4cSPavel Emelyanov r->rt_spec_dst, &len); 3955e659e4cSPavel Emelyanov 3965e659e4cSPavel Emelyanov seq_printf(seq, "%*s\n", 127 - len, ""); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds return 0; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 401f690808eSStephen Hemminger static const struct seq_operations rt_cache_seq_ops = { 4021da177e4SLinus Torvalds .start = rt_cache_seq_start, 4031da177e4SLinus Torvalds .next = rt_cache_seq_next, 4041da177e4SLinus Torvalds .stop = rt_cache_seq_stop, 4051da177e4SLinus Torvalds .show = rt_cache_seq_show, 4061da177e4SLinus Torvalds }; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds static int rt_cache_seq_open(struct inode *inode, struct file *file) 4091da177e4SLinus Torvalds { 410a75e936fSDenis V. Lunev return seq_open_net(inode, file, &rt_cache_seq_ops, 411cf7732e4SPavel Emelyanov sizeof(struct rt_cache_iter_state)); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4149a32144eSArjan van de Ven static const struct file_operations rt_cache_seq_fops = { 4151da177e4SLinus Torvalds .owner = THIS_MODULE, 4161da177e4SLinus Torvalds .open = rt_cache_seq_open, 4171da177e4SLinus Torvalds .read = seq_read, 4181da177e4SLinus Torvalds .llseek = seq_lseek, 419a75e936fSDenis V. Lunev .release = seq_release_net, 4201da177e4SLinus Torvalds }; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds int cpu; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds if (*pos == 0) 4281da177e4SLinus Torvalds return SEQ_START_TOKEN; 4291da177e4SLinus Torvalds 4300f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 4311da177e4SLinus Torvalds if (!cpu_possible(cpu)) 4321da177e4SLinus Torvalds continue; 4331da177e4SLinus Torvalds *pos = cpu+1; 4342f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds return NULL; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds int cpu; 4421da177e4SLinus Torvalds 4430f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 4441da177e4SLinus Torvalds if (!cpu_possible(cpu)) 4451da177e4SLinus Torvalds continue; 4461da177e4SLinus Torvalds *pos = cpu+1; 4472f970d83SEric Dumazet return &per_cpu(rt_cache_stat, cpu); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds return NULL; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds static void rt_cpu_seq_stop(struct seq_file *seq, void *v) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static int rt_cpu_seq_show(struct seq_file *seq, void *v) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct rt_cache_stat *st = v; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 4635bec0039SOlaf 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"); 4641da177e4SLinus Torvalds return 0; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x " 4681da177e4SLinus Torvalds " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n", 4691da177e4SLinus Torvalds atomic_read(&ipv4_dst_ops.entries), 4701da177e4SLinus Torvalds st->in_hit, 4711da177e4SLinus Torvalds st->in_slow_tot, 4721da177e4SLinus Torvalds st->in_slow_mc, 4731da177e4SLinus Torvalds st->in_no_route, 4741da177e4SLinus Torvalds st->in_brd, 4751da177e4SLinus Torvalds st->in_martian_dst, 4761da177e4SLinus Torvalds st->in_martian_src, 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds st->out_hit, 4791da177e4SLinus Torvalds st->out_slow_tot, 4801da177e4SLinus Torvalds st->out_slow_mc, 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds st->gc_total, 4831da177e4SLinus Torvalds st->gc_ignored, 4841da177e4SLinus Torvalds st->gc_goal_miss, 4851da177e4SLinus Torvalds st->gc_dst_overflow, 4861da177e4SLinus Torvalds st->in_hlist_search, 4871da177e4SLinus Torvalds st->out_hlist_search 4881da177e4SLinus Torvalds ); 4891da177e4SLinus Torvalds return 0; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492f690808eSStephen Hemminger static const struct seq_operations rt_cpu_seq_ops = { 4931da177e4SLinus Torvalds .start = rt_cpu_seq_start, 4941da177e4SLinus Torvalds .next = rt_cpu_seq_next, 4951da177e4SLinus Torvalds .stop = rt_cpu_seq_stop, 4961da177e4SLinus Torvalds .show = rt_cpu_seq_show, 4971da177e4SLinus Torvalds }; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds static int rt_cpu_seq_open(struct inode *inode, struct file *file) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds return seq_open(file, &rt_cpu_seq_ops); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5059a32144eSArjan van de Ven static const struct file_operations rt_cpu_seq_fops = { 5061da177e4SLinus Torvalds .owner = THIS_MODULE, 5071da177e4SLinus Torvalds .open = rt_cpu_seq_open, 5081da177e4SLinus Torvalds .read = seq_read, 5091da177e4SLinus Torvalds .llseek = seq_lseek, 5101da177e4SLinus Torvalds .release = seq_release, 5111da177e4SLinus Torvalds }; 5121da177e4SLinus Torvalds 51378c686e9SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE 514a661c419SAlexey Dobriyan static int rt_acct_proc_show(struct seq_file *m, void *v) 51578c686e9SPavel Emelyanov { 516a661c419SAlexey Dobriyan struct ip_rt_acct *dst, *src; 517a661c419SAlexey Dobriyan unsigned int i, j; 51878c686e9SPavel Emelyanov 519a661c419SAlexey Dobriyan dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL); 520a661c419SAlexey Dobriyan if (!dst) 521a661c419SAlexey Dobriyan return -ENOMEM; 52278c686e9SPavel Emelyanov 523a661c419SAlexey Dobriyan for_each_possible_cpu(i) { 524a661c419SAlexey Dobriyan src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i); 525a661c419SAlexey Dobriyan for (j = 0; j < 256; j++) { 526a661c419SAlexey Dobriyan dst[j].o_bytes += src[j].o_bytes; 527a661c419SAlexey Dobriyan dst[j].o_packets += src[j].o_packets; 528a661c419SAlexey Dobriyan dst[j].i_bytes += src[j].i_bytes; 529a661c419SAlexey Dobriyan dst[j].i_packets += src[j].i_packets; 530a661c419SAlexey Dobriyan } 531a661c419SAlexey Dobriyan } 532a661c419SAlexey Dobriyan 533a661c419SAlexey Dobriyan seq_write(m, dst, 256 * sizeof(struct ip_rt_acct)); 534a661c419SAlexey Dobriyan kfree(dst); 53578c686e9SPavel Emelyanov return 0; 53678c686e9SPavel Emelyanov } 53778c686e9SPavel Emelyanov 538a661c419SAlexey Dobriyan static int rt_acct_proc_open(struct inode *inode, struct file *file) 539a661c419SAlexey Dobriyan { 540a661c419SAlexey Dobriyan return single_open(file, rt_acct_proc_show, NULL); 54178c686e9SPavel Emelyanov } 54278c686e9SPavel Emelyanov 543a661c419SAlexey Dobriyan static const struct file_operations rt_acct_proc_fops = { 544a661c419SAlexey Dobriyan .owner = THIS_MODULE, 545a661c419SAlexey Dobriyan .open = rt_acct_proc_open, 546a661c419SAlexey Dobriyan .read = seq_read, 547a661c419SAlexey Dobriyan .llseek = seq_lseek, 548a661c419SAlexey Dobriyan .release = single_release, 549a661c419SAlexey Dobriyan }; 55078c686e9SPavel Emelyanov #endif 551107f1634SPavel Emelyanov 55273b38711SDenis V. Lunev static int __net_init ip_rt_do_proc_init(struct net *net) 553107f1634SPavel Emelyanov { 554107f1634SPavel Emelyanov struct proc_dir_entry *pde; 555107f1634SPavel Emelyanov 556107f1634SPavel Emelyanov pde = proc_net_fops_create(net, "rt_cache", S_IRUGO, 557107f1634SPavel Emelyanov &rt_cache_seq_fops); 558107f1634SPavel Emelyanov if (!pde) 559107f1634SPavel Emelyanov goto err1; 560107f1634SPavel Emelyanov 56177020720SWang Chen pde = proc_create("rt_cache", S_IRUGO, 56277020720SWang Chen net->proc_net_stat, &rt_cpu_seq_fops); 563107f1634SPavel Emelyanov if (!pde) 564107f1634SPavel Emelyanov goto err2; 565107f1634SPavel Emelyanov 566107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE 567a661c419SAlexey Dobriyan pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops); 568107f1634SPavel Emelyanov if (!pde) 569107f1634SPavel Emelyanov goto err3; 570107f1634SPavel Emelyanov #endif 571107f1634SPavel Emelyanov return 0; 572107f1634SPavel Emelyanov 573107f1634SPavel Emelyanov #ifdef CONFIG_NET_CLS_ROUTE 574107f1634SPavel Emelyanov err3: 575107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net_stat); 576107f1634SPavel Emelyanov #endif 577107f1634SPavel Emelyanov err2: 578107f1634SPavel Emelyanov remove_proc_entry("rt_cache", net->proc_net); 579107f1634SPavel Emelyanov err1: 580107f1634SPavel Emelyanov return -ENOMEM; 581107f1634SPavel Emelyanov } 58273b38711SDenis V. Lunev 58373b38711SDenis V. Lunev static void __net_exit ip_rt_do_proc_exit(struct net *net) 58473b38711SDenis V. Lunev { 58573b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net_stat); 58673b38711SDenis V. Lunev remove_proc_entry("rt_cache", net->proc_net); 5870a931acfSAlexey Dobriyan #ifdef CONFIG_NET_CLS_ROUTE 58873b38711SDenis V. Lunev remove_proc_entry("rt_acct", net->proc_net); 5890a931acfSAlexey Dobriyan #endif 59073b38711SDenis V. Lunev } 59173b38711SDenis V. Lunev 59273b38711SDenis V. Lunev static struct pernet_operations ip_rt_proc_ops __net_initdata = { 59373b38711SDenis V. Lunev .init = ip_rt_do_proc_init, 59473b38711SDenis V. Lunev .exit = ip_rt_do_proc_exit, 59573b38711SDenis V. Lunev }; 59673b38711SDenis V. Lunev 59773b38711SDenis V. Lunev static int __init ip_rt_proc_init(void) 59873b38711SDenis V. Lunev { 59973b38711SDenis V. Lunev return register_pernet_subsys(&ip_rt_proc_ops); 60073b38711SDenis V. Lunev } 60173b38711SDenis V. Lunev 602107f1634SPavel Emelyanov #else 60373b38711SDenis V. Lunev static inline int ip_rt_proc_init(void) 604107f1634SPavel Emelyanov { 605107f1634SPavel Emelyanov return 0; 606107f1634SPavel Emelyanov } 6071da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 6081da177e4SLinus Torvalds 6095969f71dSStephen Hemminger static inline void rt_free(struct rtable *rt) 6101da177e4SLinus Torvalds { 611d8d1f30bSChangli Gao call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6145969f71dSStephen Hemminger static inline void rt_drop(struct rtable *rt) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds ip_rt_put(rt); 617d8d1f30bSChangli Gao call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free); 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6205969f71dSStephen Hemminger static inline int rt_fast_clean(struct rtable *rth) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds /* Kill broadcast/multicast entries very aggresively, if they 6231da177e4SLinus Torvalds collide in hash table with more useful entries */ 6241da177e4SLinus Torvalds return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) && 625d8d1f30bSChangli Gao rth->fl.iif && rth->dst.rt_next; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6285969f71dSStephen Hemminger static inline int rt_valuable(struct rtable *rth) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) || 631d8d1f30bSChangli Gao rth->dst.expires; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds unsigned long age; 6371da177e4SLinus Torvalds int ret = 0; 6381da177e4SLinus Torvalds 639d8d1f30bSChangli Gao if (atomic_read(&rth->dst.__refcnt)) 6401da177e4SLinus Torvalds goto out; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds ret = 1; 643d8d1f30bSChangli Gao if (rth->dst.expires && 644d8d1f30bSChangli Gao time_after_eq(jiffies, rth->dst.expires)) 6451da177e4SLinus Torvalds goto out; 6461da177e4SLinus Torvalds 647d8d1f30bSChangli Gao age = jiffies - rth->dst.lastuse; 6481da177e4SLinus Torvalds ret = 0; 6491da177e4SLinus Torvalds if ((age <= tmo1 && !rt_fast_clean(rth)) || 6501da177e4SLinus Torvalds (age <= tmo2 && rt_valuable(rth))) 6511da177e4SLinus Torvalds goto out; 6521da177e4SLinus Torvalds ret = 1; 6531da177e4SLinus Torvalds out: return ret; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* Bits of score are: 6571da177e4SLinus Torvalds * 31: very valuable 6581da177e4SLinus Torvalds * 30: not quite useless 6591da177e4SLinus Torvalds * 29..0: usage counter 6601da177e4SLinus Torvalds */ 6611da177e4SLinus Torvalds static inline u32 rt_score(struct rtable *rt) 6621da177e4SLinus Torvalds { 663d8d1f30bSChangli Gao u32 score = jiffies - rt->dst.lastuse; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds score = ~score & ~(3<<30); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds if (rt_valuable(rt)) 6681da177e4SLinus Torvalds score |= (1<<31); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds if (!rt->fl.iif || 6711da177e4SLinus Torvalds !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL))) 6721da177e4SLinus Torvalds score |= (1<<30); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds return score; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 6771080d709SNeil Horman static inline bool rt_caching(const struct net *net) 6781080d709SNeil Horman { 6791080d709SNeil Horman return net->ipv4.current_rt_cache_rebuild_count <= 6801080d709SNeil Horman net->ipv4.sysctl_rt_cache_rebuild_count; 6811080d709SNeil Horman } 6821080d709SNeil Horman 6831080d709SNeil Horman static inline bool compare_hash_inputs(const struct flowi *fl1, 6841080d709SNeil Horman const struct flowi *fl2) 6851080d709SNeil Horman { 6860eae88f3SEric Dumazet return ((((__force u32)fl1->nl_u.ip4_u.daddr ^ (__force u32)fl2->nl_u.ip4_u.daddr) | 6870eae88f3SEric Dumazet ((__force u32)fl1->nl_u.ip4_u.saddr ^ (__force u32)fl2->nl_u.ip4_u.saddr) | 6881080d709SNeil Horman (fl1->iif ^ fl2->iif)) == 0); 6891080d709SNeil Horman } 6901080d709SNeil Horman 6911da177e4SLinus Torvalds static inline int compare_keys(struct flowi *fl1, struct flowi *fl2) 6921da177e4SLinus Torvalds { 6930eae88f3SEric Dumazet return (((__force u32)fl1->nl_u.ip4_u.daddr ^ (__force u32)fl2->nl_u.ip4_u.daddr) | 6940eae88f3SEric Dumazet ((__force u32)fl1->nl_u.ip4_u.saddr ^ (__force u32)fl2->nl_u.ip4_u.saddr) | 69547dcf0cbSThomas Graf (fl1->mark ^ fl2->mark) | 6960eae88f3SEric Dumazet (*(u16 *)&fl1->nl_u.ip4_u.tos ^ *(u16 *)&fl2->nl_u.ip4_u.tos) | 6978238b218SDavid S. Miller (fl1->oif ^ fl2->oif) | 6988238b218SDavid S. Miller (fl1->iif ^ fl2->iif)) == 0; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 701b5921910SDenis V. Lunev static inline int compare_netns(struct rtable *rt1, struct rtable *rt2) 702b5921910SDenis V. Lunev { 703d8d1f30bSChangli Gao return net_eq(dev_net(rt1->dst.dev), dev_net(rt2->dst.dev)); 704b5921910SDenis V. Lunev } 705b5921910SDenis V. Lunev 706e84f84f2SDenis V. Lunev static inline int rt_is_expired(struct rtable *rth) 707e84f84f2SDenis V. Lunev { 708d8d1f30bSChangli Gao return rth->rt_genid != rt_genid(dev_net(rth->dst.dev)); 709e84f84f2SDenis V. Lunev } 710e84f84f2SDenis V. Lunev 711beb659bdSEric Dumazet /* 712beb659bdSEric Dumazet * Perform a full scan of hash table and free all entries. 713beb659bdSEric Dumazet * Can be called by a softirq or a process. 714beb659bdSEric Dumazet * In the later case, we want to be reschedule if necessary 715beb659bdSEric Dumazet */ 716beb659bdSEric Dumazet static void rt_do_flush(int process_context) 717beb659bdSEric Dumazet { 718beb659bdSEric Dumazet unsigned int i; 719beb659bdSEric Dumazet struct rtable *rth, *next; 72032cb5b4eSDenis V. Lunev struct rtable * tail; 721beb659bdSEric Dumazet 722beb659bdSEric Dumazet for (i = 0; i <= rt_hash_mask; i++) { 723beb659bdSEric Dumazet if (process_context && need_resched()) 724beb659bdSEric Dumazet cond_resched(); 725beb659bdSEric Dumazet rth = rt_hash_table[i].chain; 726beb659bdSEric Dumazet if (!rth) 727beb659bdSEric Dumazet continue; 728beb659bdSEric Dumazet 729beb659bdSEric Dumazet spin_lock_bh(rt_hash_lock_addr(i)); 73032cb5b4eSDenis V. Lunev #ifdef CONFIG_NET_NS 73132cb5b4eSDenis V. Lunev { 73232cb5b4eSDenis V. Lunev struct rtable ** prev, * p; 73332cb5b4eSDenis V. Lunev 73432cb5b4eSDenis V. Lunev rth = rt_hash_table[i].chain; 73532cb5b4eSDenis V. Lunev 73632cb5b4eSDenis V. Lunev /* defer releasing the head of the list after spin_unlock */ 737d8d1f30bSChangli Gao for (tail = rth; tail; tail = tail->dst.rt_next) 73832cb5b4eSDenis V. Lunev if (!rt_is_expired(tail)) 73932cb5b4eSDenis V. Lunev break; 74032cb5b4eSDenis V. Lunev if (rth != tail) 74132cb5b4eSDenis V. Lunev rt_hash_table[i].chain = tail; 74232cb5b4eSDenis V. Lunev 74332cb5b4eSDenis V. Lunev /* call rt_free on entries after the tail requiring flush */ 74432cb5b4eSDenis V. Lunev prev = &rt_hash_table[i].chain; 74532cb5b4eSDenis V. Lunev for (p = *prev; p; p = next) { 746d8d1f30bSChangli Gao next = p->dst.rt_next; 74732cb5b4eSDenis V. Lunev if (!rt_is_expired(p)) { 748d8d1f30bSChangli Gao prev = &p->dst.rt_next; 74932cb5b4eSDenis V. Lunev } else { 75032cb5b4eSDenis V. Lunev *prev = next; 75132cb5b4eSDenis V. Lunev rt_free(p); 75232cb5b4eSDenis V. Lunev } 75332cb5b4eSDenis V. Lunev } 75432cb5b4eSDenis V. Lunev } 75532cb5b4eSDenis V. Lunev #else 756beb659bdSEric Dumazet rth = rt_hash_table[i].chain; 757beb659bdSEric Dumazet rt_hash_table[i].chain = NULL; 75832cb5b4eSDenis V. Lunev tail = NULL; 75932cb5b4eSDenis V. Lunev #endif 760beb659bdSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(i)); 761beb659bdSEric Dumazet 76232cb5b4eSDenis V. Lunev for (; rth != tail; rth = next) { 763d8d1f30bSChangli Gao next = rth->dst.rt_next; 764beb659bdSEric Dumazet rt_free(rth); 765beb659bdSEric Dumazet } 766beb659bdSEric Dumazet } 767beb659bdSEric Dumazet } 768beb659bdSEric Dumazet 7691080d709SNeil Horman /* 7701080d709SNeil Horman * While freeing expired entries, we compute average chain length 7711080d709SNeil Horman * and standard deviation, using fixed-point arithmetic. 7721080d709SNeil Horman * This to have an estimation of rt_chain_length_max 7731080d709SNeil Horman * rt_chain_length_max = max(elasticity, AVG + 4*SD) 7741080d709SNeil Horman * We use 3 bits for frational part, and 29 (or 61) for magnitude. 7751080d709SNeil Horman */ 7761080d709SNeil Horman 7771080d709SNeil Horman #define FRACT_BITS 3 7781080d709SNeil Horman #define ONE (1UL << FRACT_BITS) 7791080d709SNeil Horman 78098376387SEric Dumazet /* 78198376387SEric Dumazet * Given a hash chain and an item in this hash chain, 78298376387SEric Dumazet * find if a previous entry has the same hash_inputs 78398376387SEric Dumazet * (but differs on tos, mark or oif) 78498376387SEric Dumazet * Returns 0 if an alias is found. 78598376387SEric Dumazet * Returns ONE if rth has no alias before itself. 78698376387SEric Dumazet */ 78798376387SEric Dumazet static int has_noalias(const struct rtable *head, const struct rtable *rth) 78898376387SEric Dumazet { 78998376387SEric Dumazet const struct rtable *aux = head; 79098376387SEric Dumazet 79198376387SEric Dumazet while (aux != rth) { 79298376387SEric Dumazet if (compare_hash_inputs(&aux->fl, &rth->fl)) 79398376387SEric Dumazet return 0; 794d8d1f30bSChangli Gao aux = aux->dst.rt_next; 79598376387SEric Dumazet } 79698376387SEric Dumazet return ONE; 79798376387SEric Dumazet } 79898376387SEric Dumazet 799beb659bdSEric Dumazet static void rt_check_expire(void) 8001da177e4SLinus Torvalds { 801bb1d23b0SEric Dumazet static unsigned int rover; 802bb1d23b0SEric Dumazet unsigned int i = rover, goal; 80398376387SEric Dumazet struct rtable *rth, **rthp; 804cf8da764SEric Dumazet unsigned long samples = 0; 8051080d709SNeil Horman unsigned long sum = 0, sum2 = 0; 806125bb8f5SEric Dumazet unsigned long delta; 807bb1d23b0SEric Dumazet u64 mult; 8081da177e4SLinus Torvalds 809125bb8f5SEric Dumazet delta = jiffies - expires_ljiffies; 810125bb8f5SEric Dumazet expires_ljiffies = jiffies; 811125bb8f5SEric Dumazet mult = ((u64)delta) << rt_hash_log; 812bb1d23b0SEric Dumazet if (ip_rt_gc_timeout > 1) 813bb1d23b0SEric Dumazet do_div(mult, ip_rt_gc_timeout); 814bb1d23b0SEric Dumazet goal = (unsigned int)mult; 81539c90eceSEric Dumazet if (goal > rt_hash_mask) 81639c90eceSEric Dumazet goal = rt_hash_mask + 1; 817bb1d23b0SEric Dumazet for (; goal > 0; goal--) { 8181da177e4SLinus Torvalds unsigned long tmo = ip_rt_gc_timeout; 819cf8da764SEric Dumazet unsigned long length; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds i = (i + 1) & rt_hash_mask; 8221da177e4SLinus Torvalds rthp = &rt_hash_table[i].chain; 8231da177e4SLinus Torvalds 824d90bf5a9SEric Dumazet if (need_resched()) 825d90bf5a9SEric Dumazet cond_resched(); 826d90bf5a9SEric Dumazet 8271080d709SNeil Horman samples++; 8281080d709SNeil Horman 829cfcabdccSStephen Hemminger if (*rthp == NULL) 830bb1d23b0SEric Dumazet continue; 831cf8da764SEric Dumazet length = 0; 83239c90eceSEric Dumazet spin_lock_bh(rt_hash_lock_addr(i)); 8331da177e4SLinus Torvalds while ((rth = *rthp) != NULL) { 834d8d1f30bSChangli Gao prefetch(rth->dst.rt_next); 835e84f84f2SDenis V. Lunev if (rt_is_expired(rth)) { 836d8d1f30bSChangli Gao *rthp = rth->dst.rt_next; 83729e75252SEric Dumazet rt_free(rth); 83829e75252SEric Dumazet continue; 83929e75252SEric Dumazet } 840d8d1f30bSChangli Gao if (rth->dst.expires) { 8411da177e4SLinus Torvalds /* Entry is expired even if it is in use */ 842d8d1f30bSChangli Gao if (time_before_eq(jiffies, rth->dst.expires)) { 8431ddbcb00SEric Dumazet nofree: 8441da177e4SLinus Torvalds tmo >>= 1; 845d8d1f30bSChangli Gao rthp = &rth->dst.rt_next; 8461080d709SNeil Horman /* 8471ddbcb00SEric Dumazet * We only count entries on 8481080d709SNeil Horman * a chain with equal hash inputs once 8491080d709SNeil Horman * so that entries for different QOS 8501080d709SNeil Horman * levels, and other non-hash input 8511080d709SNeil Horman * attributes don't unfairly skew 8521080d709SNeil Horman * the length computation 8531080d709SNeil Horman */ 85498376387SEric Dumazet length += has_noalias(rt_hash_table[i].chain, rth); 8551da177e4SLinus Torvalds continue; 8561da177e4SLinus Torvalds } 8571ddbcb00SEric Dumazet } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) 8581ddbcb00SEric Dumazet goto nofree; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds /* Cleanup aged off entries. */ 861d8d1f30bSChangli Gao *rthp = rth->dst.rt_next; 8621da177e4SLinus Torvalds rt_free(rth); 8631da177e4SLinus Torvalds } 86439c90eceSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(i)); 8651080d709SNeil Horman sum += length; 8661080d709SNeil Horman sum2 += length*length; 8671080d709SNeil Horman } 8681080d709SNeil Horman if (samples) { 8691080d709SNeil Horman unsigned long avg = sum / samples; 8701080d709SNeil Horman unsigned long sd = int_sqrt(sum2 / samples - avg*avg); 8711080d709SNeil Horman rt_chain_length_max = max_t(unsigned long, 8721080d709SNeil Horman ip_rt_gc_elasticity, 8731080d709SNeil Horman (avg + 4*sd) >> FRACT_BITS); 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds rover = i; 876beb659bdSEric Dumazet } 877beb659bdSEric Dumazet 878beb659bdSEric Dumazet /* 879beb659bdSEric Dumazet * rt_worker_func() is run in process context. 88029e75252SEric Dumazet * we call rt_check_expire() to scan part of the hash table 881beb659bdSEric Dumazet */ 882beb659bdSEric Dumazet static void rt_worker_func(struct work_struct *work) 883beb659bdSEric Dumazet { 884beb659bdSEric Dumazet rt_check_expire(); 88539c90eceSEric Dumazet schedule_delayed_work(&expires_work, ip_rt_gc_interval); 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88829e75252SEric Dumazet /* 88929e75252SEric Dumazet * Pertubation of rt_genid by a small quantity [1..256] 89029e75252SEric Dumazet * Using 8 bits of shuffling ensure we can call rt_cache_invalidate() 89129e75252SEric Dumazet * many times (2^24) without giving recent rt_genid. 89229e75252SEric Dumazet * Jenkins hash is strong enough that litle changes of rt_genid are OK. 8931da177e4SLinus Torvalds */ 89486c657f6SDenis V. Lunev static void rt_cache_invalidate(struct net *net) 8951da177e4SLinus Torvalds { 89629e75252SEric Dumazet unsigned char shuffle; 8971da177e4SLinus Torvalds 89829e75252SEric Dumazet get_random_bytes(&shuffle, sizeof(shuffle)); 899e84f84f2SDenis V. Lunev atomic_add(shuffle + 1U, &net->ipv4.rt_genid); 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 902beb659bdSEric Dumazet /* 90329e75252SEric Dumazet * delay < 0 : invalidate cache (fast : entries will be deleted later) 90429e75252SEric Dumazet * delay >= 0 : invalidate & flush cache (can be long) 90529e75252SEric Dumazet */ 90676e6ebfbSDenis V. Lunev void rt_cache_flush(struct net *net, int delay) 90729e75252SEric Dumazet { 90886c657f6SDenis V. Lunev rt_cache_invalidate(net); 90929e75252SEric Dumazet if (delay >= 0) 91029e75252SEric Dumazet rt_do_flush(!in_softirq()); 91129e75252SEric Dumazet } 91229e75252SEric Dumazet 913a5ee1551SEric W. Biederman /* Flush previous cache invalidated entries from the cache */ 914a5ee1551SEric W. Biederman void rt_cache_flush_batch(void) 915a5ee1551SEric W. Biederman { 916a5ee1551SEric W. Biederman rt_do_flush(!in_softirq()); 917a5ee1551SEric W. Biederman } 918a5ee1551SEric W. Biederman 9191080d709SNeil Horman static void rt_emergency_hash_rebuild(struct net *net) 9201080d709SNeil Horman { 9213ee94372SNeil Horman if (net_ratelimit()) 9221080d709SNeil Horman printk(KERN_WARNING "Route hash chain too long!\n"); 9233ee94372SNeil Horman rt_cache_invalidate(net); 9241080d709SNeil Horman } 9251080d709SNeil Horman 9261da177e4SLinus Torvalds /* 9271da177e4SLinus Torvalds Short description of GC goals. 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds We want to build algorithm, which will keep routing cache 9301da177e4SLinus Torvalds at some equilibrium point, when number of aged off entries 9311da177e4SLinus Torvalds is kept approximately equal to newly generated ones. 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds Current expiration strength is variable "expire". 9341da177e4SLinus Torvalds We try to adjust it dynamically, so that if networking 9351da177e4SLinus Torvalds is idle expires is large enough to keep enough of warm entries, 9361da177e4SLinus Torvalds and when load increases it reduces to limit cache size. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds 939569d3645SDaniel Lezcano static int rt_garbage_collect(struct dst_ops *ops) 9401da177e4SLinus Torvalds { 9411da177e4SLinus Torvalds static unsigned long expire = RT_GC_TIMEOUT; 9421da177e4SLinus Torvalds static unsigned long last_gc; 9431da177e4SLinus Torvalds static int rover; 9441da177e4SLinus Torvalds static int equilibrium; 9451da177e4SLinus Torvalds struct rtable *rth, **rthp; 9461da177e4SLinus Torvalds unsigned long now = jiffies; 9471da177e4SLinus Torvalds int goal; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * Garbage collection is pretty expensive, 9511da177e4SLinus Torvalds * do not make it too frequently. 9521da177e4SLinus Torvalds */ 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds RT_CACHE_STAT_INC(gc_total); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds if (now - last_gc < ip_rt_gc_min_interval && 9571da177e4SLinus Torvalds atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) { 9581da177e4SLinus Torvalds RT_CACHE_STAT_INC(gc_ignored); 9591da177e4SLinus Torvalds goto out; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* Calculate number of entries, which we want to expire now. */ 9631da177e4SLinus Torvalds goal = atomic_read(&ipv4_dst_ops.entries) - 9641da177e4SLinus Torvalds (ip_rt_gc_elasticity << rt_hash_log); 9651da177e4SLinus Torvalds if (goal <= 0) { 9661da177e4SLinus Torvalds if (equilibrium < ipv4_dst_ops.gc_thresh) 9671da177e4SLinus Torvalds equilibrium = ipv4_dst_ops.gc_thresh; 9681da177e4SLinus Torvalds goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium; 9691da177e4SLinus Torvalds if (goal > 0) { 970b790ceddSEric Dumazet equilibrium += min_t(unsigned int, goal >> 1, rt_hash_mask + 1); 9711da177e4SLinus Torvalds goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds } else { 9741da177e4SLinus Torvalds /* We are in dangerous area. Try to reduce cache really 9751da177e4SLinus Torvalds * aggressively. 9761da177e4SLinus Torvalds */ 977b790ceddSEric Dumazet goal = max_t(unsigned int, goal >> 1, rt_hash_mask + 1); 9781da177e4SLinus Torvalds equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds if (now - last_gc >= ip_rt_gc_min_interval) 9821da177e4SLinus Torvalds last_gc = now; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds if (goal <= 0) { 9851da177e4SLinus Torvalds equilibrium += goal; 9861da177e4SLinus Torvalds goto work_done; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds do { 9901da177e4SLinus Torvalds int i, k; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds for (i = rt_hash_mask, k = rover; i >= 0; i--) { 9931da177e4SLinus Torvalds unsigned long tmo = expire; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds k = (k + 1) & rt_hash_mask; 9961da177e4SLinus Torvalds rthp = &rt_hash_table[k].chain; 99722c047ccSEric Dumazet spin_lock_bh(rt_hash_lock_addr(k)); 9981da177e4SLinus Torvalds while ((rth = *rthp) != NULL) { 999e84f84f2SDenis V. Lunev if (!rt_is_expired(rth) && 100029e75252SEric Dumazet !rt_may_expire(rth, tmo, expire)) { 10011da177e4SLinus Torvalds tmo >>= 1; 1002d8d1f30bSChangli Gao rthp = &rth->dst.rt_next; 10031da177e4SLinus Torvalds continue; 10041da177e4SLinus Torvalds } 1005d8d1f30bSChangli Gao *rthp = rth->dst.rt_next; 10061da177e4SLinus Torvalds rt_free(rth); 10071da177e4SLinus Torvalds goal--; 10081da177e4SLinus Torvalds } 100922c047ccSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(k)); 10101da177e4SLinus Torvalds if (goal <= 0) 10111da177e4SLinus Torvalds break; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds rover = k; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds if (goal <= 0) 10161da177e4SLinus Torvalds goto work_done; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* Goal is not achieved. We stop process if: 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds - if expire reduced to zero. Otherwise, expire is halfed. 10211da177e4SLinus Torvalds - if table is not full. 10221da177e4SLinus Torvalds - if we are called from interrupt. 10231da177e4SLinus Torvalds - jiffies check is just fallback/debug loop breaker. 10241da177e4SLinus Torvalds We will not spin here for long time in any case. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds RT_CACHE_STAT_INC(gc_goal_miss); 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (expire == 0) 10301da177e4SLinus Torvalds break; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds expire >>= 1; 10331da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2 10341da177e4SLinus Torvalds printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire, 10351da177e4SLinus Torvalds atomic_read(&ipv4_dst_ops.entries), goal, i); 10361da177e4SLinus Torvalds #endif 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) 10391da177e4SLinus Torvalds goto out; 10401da177e4SLinus Torvalds } while (!in_softirq() && time_before_eq(jiffies, now)); 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) 10431da177e4SLinus Torvalds goto out; 10441da177e4SLinus Torvalds if (net_ratelimit()) 10451da177e4SLinus Torvalds printk(KERN_WARNING "dst cache overflow\n"); 10461da177e4SLinus Torvalds RT_CACHE_STAT_INC(gc_dst_overflow); 10471da177e4SLinus Torvalds return 1; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds work_done: 10501da177e4SLinus Torvalds expire += ip_rt_gc_min_interval; 10511da177e4SLinus Torvalds if (expire > ip_rt_gc_timeout || 10521da177e4SLinus Torvalds atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh) 10531da177e4SLinus Torvalds expire = ip_rt_gc_timeout; 10541da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2 10551da177e4SLinus Torvalds printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire, 10561da177e4SLinus Torvalds atomic_read(&ipv4_dst_ops.entries), goal, rover); 10571da177e4SLinus Torvalds #endif 10581da177e4SLinus Torvalds out: return 0; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 106198376387SEric Dumazet /* 106298376387SEric Dumazet * Returns number of entries in a hash chain that have different hash_inputs 106398376387SEric Dumazet */ 106498376387SEric Dumazet static int slow_chain_length(const struct rtable *head) 106598376387SEric Dumazet { 106698376387SEric Dumazet int length = 0; 106798376387SEric Dumazet const struct rtable *rth = head; 106898376387SEric Dumazet 106998376387SEric Dumazet while (rth) { 107098376387SEric Dumazet length += has_noalias(head, rth); 1071d8d1f30bSChangli Gao rth = rth->dst.rt_next; 107298376387SEric Dumazet } 107398376387SEric Dumazet return length >> FRACT_BITS; 107498376387SEric Dumazet } 107598376387SEric Dumazet 1076511c3f92SEric Dumazet static int rt_intern_hash(unsigned hash, struct rtable *rt, 10776a2bad70SPavel Emelyanov struct rtable **rp, struct sk_buff *skb, int ifindex) 10781da177e4SLinus Torvalds { 10791da177e4SLinus Torvalds struct rtable *rth, **rthp; 10801da177e4SLinus Torvalds unsigned long now; 10811da177e4SLinus Torvalds struct rtable *cand, **candp; 10821da177e4SLinus Torvalds u32 min_score; 10831da177e4SLinus Torvalds int chain_length; 10841da177e4SLinus Torvalds int attempts = !in_softirq(); 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds restart: 10871da177e4SLinus Torvalds chain_length = 0; 10881da177e4SLinus Torvalds min_score = ~(u32)0; 10891da177e4SLinus Torvalds cand = NULL; 10901da177e4SLinus Torvalds candp = NULL; 10911da177e4SLinus Torvalds now = jiffies; 10921da177e4SLinus Torvalds 1093d8d1f30bSChangli Gao if (!rt_caching(dev_net(rt->dst.dev))) { 109473e42897SNeil Horman /* 109573e42897SNeil Horman * If we're not caching, just tell the caller we 109673e42897SNeil Horman * were successful and don't touch the route. The 109773e42897SNeil Horman * caller hold the sole reference to the cache entry, and 109873e42897SNeil Horman * it will be released when the caller is done with it. 109973e42897SNeil Horman * If we drop it here, the callers have no way to resolve routes 110073e42897SNeil Horman * when we're not caching. Instead, just point *rp at rt, so 110173e42897SNeil Horman * the caller gets a single use out of the route 1102b6280b47SNeil Horman * Note that we do rt_free on this new route entry, so that 1103b6280b47SNeil Horman * once its refcount hits zero, we are still able to reap it 1104b6280b47SNeil Horman * (Thanks Alexey) 1105b6280b47SNeil Horman * Note also the rt_free uses call_rcu. We don't actually 1106b6280b47SNeil Horman * need rcu protection here, this is just our path to get 1107b6280b47SNeil Horman * on the route gc list. 110873e42897SNeil Horman */ 1109b6280b47SNeil Horman 1110b6280b47SNeil Horman if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) { 1111d8d1f30bSChangli Gao int err = arp_bind_neighbour(&rt->dst); 1112b6280b47SNeil Horman if (err) { 1113b6280b47SNeil Horman if (net_ratelimit()) 1114b6280b47SNeil Horman printk(KERN_WARNING 1115b6280b47SNeil Horman "Neighbour table failure & not caching routes.\n"); 1116b6280b47SNeil Horman rt_drop(rt); 1117b6280b47SNeil Horman return err; 1118b6280b47SNeil Horman } 1119b6280b47SNeil Horman } 1120b6280b47SNeil Horman 1121b6280b47SNeil Horman rt_free(rt); 1122b6280b47SNeil Horman goto skip_hashing; 11231080d709SNeil Horman } 11241080d709SNeil Horman 11251da177e4SLinus Torvalds rthp = &rt_hash_table[hash].chain; 11261da177e4SLinus Torvalds 112722c047ccSEric Dumazet spin_lock_bh(rt_hash_lock_addr(hash)); 11281da177e4SLinus Torvalds while ((rth = *rthp) != NULL) { 1129e84f84f2SDenis V. Lunev if (rt_is_expired(rth)) { 1130d8d1f30bSChangli Gao *rthp = rth->dst.rt_next; 113129e75252SEric Dumazet rt_free(rth); 113229e75252SEric Dumazet continue; 113329e75252SEric Dumazet } 1134b5921910SDenis V. Lunev if (compare_keys(&rth->fl, &rt->fl) && compare_netns(rth, rt)) { 11351da177e4SLinus Torvalds /* Put it first */ 1136d8d1f30bSChangli Gao *rthp = rth->dst.rt_next; 11371da177e4SLinus Torvalds /* 11381da177e4SLinus Torvalds * Since lookup is lockfree, the deletion 11391da177e4SLinus Torvalds * must be visible to another weakly ordered CPU before 11401da177e4SLinus Torvalds * the insertion at the start of the hash chain. 11411da177e4SLinus Torvalds */ 1142d8d1f30bSChangli Gao rcu_assign_pointer(rth->dst.rt_next, 11431da177e4SLinus Torvalds rt_hash_table[hash].chain); 11441da177e4SLinus Torvalds /* 11451da177e4SLinus Torvalds * Since lookup is lockfree, the update writes 11461da177e4SLinus Torvalds * must be ordered for consistency on SMP. 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds rcu_assign_pointer(rt_hash_table[hash].chain, rth); 11491da177e4SLinus Torvalds 1150d8d1f30bSChangli Gao dst_use(&rth->dst, now); 115122c047ccSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(hash)); 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds rt_drop(rt); 1154511c3f92SEric Dumazet if (rp) 11551da177e4SLinus Torvalds *rp = rth; 1156511c3f92SEric Dumazet else 1157d8d1f30bSChangli Gao skb_dst_set(skb, &rth->dst); 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 1161d8d1f30bSChangli Gao if (!atomic_read(&rth->dst.__refcnt)) { 11621da177e4SLinus Torvalds u32 score = rt_score(rth); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds if (score <= min_score) { 11651da177e4SLinus Torvalds cand = rth; 11661da177e4SLinus Torvalds candp = rthp; 11671da177e4SLinus Torvalds min_score = score; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds chain_length++; 11721da177e4SLinus Torvalds 1173d8d1f30bSChangli Gao rthp = &rth->dst.rt_next; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds if (cand) { 11771da177e4SLinus Torvalds /* ip_rt_gc_elasticity used to be average length of chain 11781da177e4SLinus Torvalds * length, when exceeded gc becomes really aggressive. 11791da177e4SLinus Torvalds * 11801da177e4SLinus Torvalds * The second limit is less certain. At the moment it allows 11811da177e4SLinus Torvalds * only 2 entries per bucket. We will see. 11821da177e4SLinus Torvalds */ 11831da177e4SLinus Torvalds if (chain_length > ip_rt_gc_elasticity) { 1184d8d1f30bSChangli Gao *candp = cand->dst.rt_next; 11851da177e4SLinus Torvalds rt_free(cand); 11861da177e4SLinus Torvalds } 11871080d709SNeil Horman } else { 118898376387SEric Dumazet if (chain_length > rt_chain_length_max && 118998376387SEric Dumazet slow_chain_length(rt_hash_table[hash].chain) > rt_chain_length_max) { 1190d8d1f30bSChangli Gao struct net *net = dev_net(rt->dst.dev); 11911080d709SNeil Horman int num = ++net->ipv4.current_rt_cache_rebuild_count; 1192b35ecb5dSPavel Emelyanov if (!rt_caching(net)) { 11931080d709SNeil Horman printk(KERN_WARNING "%s: %d rebuilds is over limit, route caching disabled\n", 1194d8d1f30bSChangli Gao rt->dst.dev->name, num); 11951080d709SNeil Horman } 1196b35ecb5dSPavel Emelyanov rt_emergency_hash_rebuild(net); 11976a2bad70SPavel Emelyanov spin_unlock_bh(rt_hash_lock_addr(hash)); 11986a2bad70SPavel Emelyanov 11996a2bad70SPavel Emelyanov hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src, 12006a2bad70SPavel Emelyanov ifindex, rt_genid(net)); 12016a2bad70SPavel Emelyanov goto restart; 12021080d709SNeil Horman } 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds /* Try to bind route to arp only if it is output 12061da177e4SLinus Torvalds route or unicast forwarding path. 12071da177e4SLinus Torvalds */ 12081da177e4SLinus Torvalds if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) { 1209d8d1f30bSChangli Gao int err = arp_bind_neighbour(&rt->dst); 12101da177e4SLinus Torvalds if (err) { 121122c047ccSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(hash)); 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds if (err != -ENOBUFS) { 12141da177e4SLinus Torvalds rt_drop(rt); 12151da177e4SLinus Torvalds return err; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds /* Neighbour tables are full and nothing 12191da177e4SLinus Torvalds can be released. Try to shrink route cache, 12201da177e4SLinus Torvalds it is most likely it holds some neighbour records. 12211da177e4SLinus Torvalds */ 12221da177e4SLinus Torvalds if (attempts-- > 0) { 12231da177e4SLinus Torvalds int saved_elasticity = ip_rt_gc_elasticity; 12241da177e4SLinus Torvalds int saved_int = ip_rt_gc_min_interval; 12251da177e4SLinus Torvalds ip_rt_gc_elasticity = 1; 12261da177e4SLinus Torvalds ip_rt_gc_min_interval = 0; 1227569d3645SDaniel Lezcano rt_garbage_collect(&ipv4_dst_ops); 12281da177e4SLinus Torvalds ip_rt_gc_min_interval = saved_int; 12291da177e4SLinus Torvalds ip_rt_gc_elasticity = saved_elasticity; 12301da177e4SLinus Torvalds goto restart; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds if (net_ratelimit()) 12341da177e4SLinus Torvalds printk(KERN_WARNING "Neighbour table overflow.\n"); 12351da177e4SLinus Torvalds rt_drop(rt); 12361da177e4SLinus Torvalds return -ENOBUFS; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 1240d8d1f30bSChangli Gao rt->dst.rt_next = rt_hash_table[hash].chain; 12411080d709SNeil Horman 12421da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 2 1243d8d1f30bSChangli Gao if (rt->dst.rt_next) { 12441da177e4SLinus Torvalds struct rtable *trt; 1245b6280b47SNeil Horman printk(KERN_DEBUG "rt_cache @%02x: %pI4", 1246b6280b47SNeil Horman hash, &rt->rt_dst); 1247d8d1f30bSChangli Gao for (trt = rt->dst.rt_next; trt; trt = trt->dst.rt_next) 1248673d57e7SHarvey Harrison printk(" . %pI4", &trt->rt_dst); 12491da177e4SLinus Torvalds printk("\n"); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds #endif 125200269b54SEric Dumazet /* 125300269b54SEric Dumazet * Since lookup is lockfree, we must make sure 125400269b54SEric Dumazet * previous writes to rt are comitted to memory 125500269b54SEric Dumazet * before making rt visible to other CPUS. 125600269b54SEric Dumazet */ 125700269b54SEric Dumazet rcu_assign_pointer(rt_hash_table[hash].chain, rt); 12581080d709SNeil Horman 125922c047ccSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(hash)); 126073e42897SNeil Horman 1261b6280b47SNeil Horman skip_hashing: 1262511c3f92SEric Dumazet if (rp) 12631da177e4SLinus Torvalds *rp = rt; 1264511c3f92SEric Dumazet else 1265d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 12661da177e4SLinus Torvalds return 0; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds void rt_bind_peer(struct rtable *rt, int create) 12701da177e4SLinus Torvalds { 12711da177e4SLinus Torvalds static DEFINE_SPINLOCK(rt_peer_lock); 12721da177e4SLinus Torvalds struct inet_peer *peer; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds peer = inet_getpeer(rt->rt_dst, create); 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds spin_lock_bh(&rt_peer_lock); 12771da177e4SLinus Torvalds if (rt->peer == NULL) { 12781da177e4SLinus Torvalds rt->peer = peer; 12791da177e4SLinus Torvalds peer = NULL; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds spin_unlock_bh(&rt_peer_lock); 12821da177e4SLinus Torvalds if (peer) 12831da177e4SLinus Torvalds inet_putpeer(peer); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds /* 12871da177e4SLinus Torvalds * Peer allocation may fail only in serious out-of-memory conditions. However 12881da177e4SLinus Torvalds * we still can generate some output. 12891da177e4SLinus Torvalds * Random ID selection looks a bit dangerous because we have no chances to 12901da177e4SLinus Torvalds * select ID being unique in a reasonable period of time. 12911da177e4SLinus Torvalds * But broken packet identifier may be better than no packet at all. 12921da177e4SLinus Torvalds */ 12931da177e4SLinus Torvalds static void ip_select_fb_ident(struct iphdr *iph) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds static DEFINE_SPINLOCK(ip_fb_id_lock); 12961da177e4SLinus Torvalds static u32 ip_fallback_id; 12971da177e4SLinus Torvalds u32 salt; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds spin_lock_bh(&ip_fb_id_lock); 1300e448515cSAl Viro salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr); 13011da177e4SLinus Torvalds iph->id = htons(salt & 0xFFFF); 13021da177e4SLinus Torvalds ip_fallback_id = salt; 13031da177e4SLinus Torvalds spin_unlock_bh(&ip_fb_id_lock); 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more) 13071da177e4SLinus Torvalds { 13081da177e4SLinus Torvalds struct rtable *rt = (struct rtable *) dst; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds if (rt) { 13111da177e4SLinus Torvalds if (rt->peer == NULL) 13121da177e4SLinus Torvalds rt_bind_peer(rt, 1); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* If peer is attached to destination, it is never detached, 13151da177e4SLinus Torvalds so that we need not to grab a lock to dereference it. 13161da177e4SLinus Torvalds */ 13171da177e4SLinus Torvalds if (rt->peer) { 13181da177e4SLinus Torvalds iph->id = htons(inet_getid(rt->peer, more)); 13191da177e4SLinus Torvalds return; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds } else 13229c2b3328SStephen Hemminger printk(KERN_DEBUG "rt_bind_peer(0) @%p\n", 13239c2b3328SStephen Hemminger __builtin_return_address(0)); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds ip_select_fb_ident(iph); 13261da177e4SLinus Torvalds } 13274bc2f18bSEric Dumazet EXPORT_SYMBOL(__ip_select_ident); 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds static void rt_del(unsigned hash, struct rtable *rt) 13301da177e4SLinus Torvalds { 133129e75252SEric Dumazet struct rtable **rthp, *aux; 13321da177e4SLinus Torvalds 133329e75252SEric Dumazet rthp = &rt_hash_table[hash].chain; 133422c047ccSEric Dumazet spin_lock_bh(rt_hash_lock_addr(hash)); 13351da177e4SLinus Torvalds ip_rt_put(rt); 133629e75252SEric Dumazet while ((aux = *rthp) != NULL) { 1337e84f84f2SDenis V. Lunev if (aux == rt || rt_is_expired(aux)) { 1338d8d1f30bSChangli Gao *rthp = aux->dst.rt_next; 133929e75252SEric Dumazet rt_free(aux); 134029e75252SEric Dumazet continue; 134129e75252SEric Dumazet } 1342d8d1f30bSChangli Gao rthp = &aux->dst.rt_next; 13431da177e4SLinus Torvalds } 134422c047ccSEric Dumazet spin_unlock_bh(rt_hash_lock_addr(hash)); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 1347ed7865a4SEric Dumazet /* called in rcu_read_lock() section */ 1348f7655229SAl Viro void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw, 1349f7655229SAl Viro __be32 saddr, struct net_device *dev) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds int i, k; 1352ed7865a4SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 13531da177e4SLinus Torvalds struct rtable *rth, **rthp; 1354f7655229SAl Viro __be32 skeys[2] = { saddr, 0 }; 13551da177e4SLinus Torvalds int ikeys[2] = { dev->ifindex, 0 }; 13568d71740cSTom Tucker struct netevent_redirect netevent; 1357317805b8SDenis V. Lunev struct net *net; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds if (!in_dev) 13601da177e4SLinus Torvalds return; 13611da177e4SLinus Torvalds 1362c346dca1SYOSHIFUJI Hideaki net = dev_net(dev); 13639d4fb27dSJoe Perches if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) || 13649d4fb27dSJoe Perches ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) || 13659d4fb27dSJoe Perches ipv4_is_zeronet(new_gw)) 13661da177e4SLinus Torvalds goto reject_redirect; 13671da177e4SLinus Torvalds 13681080d709SNeil Horman if (!rt_caching(net)) 13691080d709SNeil Horman goto reject_redirect; 13701080d709SNeil Horman 13711da177e4SLinus Torvalds if (!IN_DEV_SHARED_MEDIA(in_dev)) { 13721da177e4SLinus Torvalds if (!inet_addr_onlink(in_dev, new_gw, old_gw)) 13731da177e4SLinus Torvalds goto reject_redirect; 13741da177e4SLinus Torvalds if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev)) 13751da177e4SLinus Torvalds goto reject_redirect; 13761da177e4SLinus Torvalds } else { 1377317805b8SDenis V. Lunev if (inet_addr_type(net, new_gw) != RTN_UNICAST) 13781da177e4SLinus Torvalds goto reject_redirect; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 13821da177e4SLinus Torvalds for (k = 0; k < 2; k++) { 1383b00180deSDenis V. Lunev unsigned hash = rt_hash(daddr, skeys[i], ikeys[k], 1384e84f84f2SDenis V. Lunev rt_genid(net)); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds rthp=&rt_hash_table[hash].chain; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds while ((rth = rcu_dereference(*rthp)) != NULL) { 13891da177e4SLinus Torvalds struct rtable *rt; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if (rth->fl.fl4_dst != daddr || 13921da177e4SLinus Torvalds rth->fl.fl4_src != skeys[i] || 13931da177e4SLinus Torvalds rth->fl.oif != ikeys[k] || 139429e75252SEric Dumazet rth->fl.iif != 0 || 1395e84f84f2SDenis V. Lunev rt_is_expired(rth) || 1396d8d1f30bSChangli Gao !net_eq(dev_net(rth->dst.dev), net)) { 1397d8d1f30bSChangli Gao rthp = &rth->dst.rt_next; 13981da177e4SLinus Torvalds continue; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (rth->rt_dst != daddr || 14021da177e4SLinus Torvalds rth->rt_src != saddr || 1403d8d1f30bSChangli Gao rth->dst.error || 14041da177e4SLinus Torvalds rth->rt_gateway != old_gw || 1405d8d1f30bSChangli Gao rth->dst.dev != dev) 14061da177e4SLinus Torvalds break; 14071da177e4SLinus Torvalds 1408d8d1f30bSChangli Gao dst_hold(&rth->dst); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds rt = dst_alloc(&ipv4_dst_ops); 14111da177e4SLinus Torvalds if (rt == NULL) { 14121da177e4SLinus Torvalds ip_rt_put(rth); 14131da177e4SLinus Torvalds return; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds /* Copy all the information. */ 14171da177e4SLinus Torvalds *rt = *rth; 1418d8d1f30bSChangli Gao rt->dst.__use = 1; 1419d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 1420d8d1f30bSChangli Gao rt->dst.child = NULL; 1421d8d1f30bSChangli Gao if (rt->dst.dev) 1422d8d1f30bSChangli Gao dev_hold(rt->dst.dev); 14231da177e4SLinus Torvalds if (rt->idev) 14241da177e4SLinus Torvalds in_dev_hold(rt->idev); 1425d8d1f30bSChangli Gao rt->dst.obsolete = -1; 1426d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 1427d8d1f30bSChangli Gao rt->dst.path = &rt->dst; 1428d8d1f30bSChangli Gao rt->dst.neighbour = NULL; 1429d8d1f30bSChangli Gao rt->dst.hh = NULL; 1430def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 1431d8d1f30bSChangli Gao rt->dst.xfrm = NULL; 1432def8b4faSAlexey Dobriyan #endif 1433e84f84f2SDenis V. Lunev rt->rt_genid = rt_genid(net); 14341da177e4SLinus Torvalds rt->rt_flags |= RTCF_REDIRECTED; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds /* Gateway is different ... */ 14371da177e4SLinus Torvalds rt->rt_gateway = new_gw; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* Redirect received -> path was valid */ 1440d8d1f30bSChangli Gao dst_confirm(&rth->dst); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds if (rt->peer) 14431da177e4SLinus Torvalds atomic_inc(&rt->peer->refcnt); 14441da177e4SLinus Torvalds 1445d8d1f30bSChangli Gao if (arp_bind_neighbour(&rt->dst) || 1446d8d1f30bSChangli Gao !(rt->dst.neighbour->nud_state & 14471da177e4SLinus Torvalds NUD_VALID)) { 1448d8d1f30bSChangli Gao if (rt->dst.neighbour) 1449d8d1f30bSChangli Gao neigh_event_send(rt->dst.neighbour, NULL); 14501da177e4SLinus Torvalds ip_rt_put(rth); 14511da177e4SLinus Torvalds rt_drop(rt); 14521da177e4SLinus Torvalds goto do_next; 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 1455d8d1f30bSChangli Gao netevent.old = &rth->dst; 1456d8d1f30bSChangli Gao netevent.new = &rt->dst; 14578d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, 14588d71740cSTom Tucker &netevent); 14598d71740cSTom Tucker 14601da177e4SLinus Torvalds rt_del(hash, rth); 14616a2bad70SPavel Emelyanov if (!rt_intern_hash(hash, rt, &rt, NULL, rt->fl.oif)) 14621da177e4SLinus Torvalds ip_rt_put(rt); 14631da177e4SLinus Torvalds goto do_next; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds do_next: 14661da177e4SLinus Torvalds ; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds return; 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds reject_redirect: 14721da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 14731da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) 1474673d57e7SHarvey Harrison printk(KERN_INFO "Redirect from %pI4 on %s about %pI4 ignored.\n" 1475673d57e7SHarvey Harrison " Advised path = %pI4 -> %pI4\n", 1476673d57e7SHarvey Harrison &old_gw, dev->name, &new_gw, 1477673d57e7SHarvey Harrison &saddr, &daddr); 14781da177e4SLinus Torvalds #endif 1479ed7865a4SEric Dumazet ; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 14851da177e4SLinus Torvalds struct dst_entry *ret = dst; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds if (rt) { 1488d11a4dc1STimo Teräs if (dst->obsolete > 0) { 14891da177e4SLinus Torvalds ip_rt_put(rt); 14901da177e4SLinus Torvalds ret = NULL; 14911da177e4SLinus Torvalds } else if ((rt->rt_flags & RTCF_REDIRECTED) || 1492d8d1f30bSChangli Gao (rt->dst.expires && 1493d8d1f30bSChangli Gao time_after_eq(jiffies, rt->dst.expires))) { 14948c7bc840SAl Viro unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src, 1495b00180deSDenis V. Lunev rt->fl.oif, 1496e84f84f2SDenis V. Lunev rt_genid(dev_net(dst->dev))); 14971da177e4SLinus Torvalds #if RT_CACHE_DEBUG >= 1 1498673d57e7SHarvey Harrison printk(KERN_DEBUG "ipv4_negative_advice: redirect to %pI4/%02x dropped\n", 1499673d57e7SHarvey Harrison &rt->rt_dst, rt->fl.fl4_tos); 15001da177e4SLinus Torvalds #endif 15011da177e4SLinus Torvalds rt_del(hash, rt); 15021da177e4SLinus Torvalds ret = NULL; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds return ret; 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /* 15091da177e4SLinus Torvalds * Algorithm: 15101da177e4SLinus Torvalds * 1. The first ip_rt_redirect_number redirects are sent 15111da177e4SLinus Torvalds * with exponential backoff, then we stop sending them at all, 15121da177e4SLinus Torvalds * assuming that the host ignores our redirects. 15131da177e4SLinus Torvalds * 2. If we did not see packets requiring redirects 15141da177e4SLinus Torvalds * during ip_rt_redirect_silence, we assume that the host 15151da177e4SLinus Torvalds * forgot redirected route and start to send redirects again. 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * This algorithm is much cheaper and more intelligent than dumb load limiting 15181da177e4SLinus Torvalds * in icmp.c. 15191da177e4SLinus Torvalds * 15201da177e4SLinus Torvalds * NOTE. Do not forget to inhibit load limiting for redirects (redundant) 15211da177e4SLinus Torvalds * and "frag. need" (breaks PMTU discovery) in icmp.c. 15221da177e4SLinus Torvalds */ 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds void ip_rt_send_redirect(struct sk_buff *skb) 15251da177e4SLinus Torvalds { 1526511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 152730038fc6SEric Dumazet struct in_device *in_dev; 152830038fc6SEric Dumazet int log_martians; 15291da177e4SLinus Torvalds 153030038fc6SEric Dumazet rcu_read_lock(); 1531d8d1f30bSChangli Gao in_dev = __in_dev_get_rcu(rt->dst.dev); 153230038fc6SEric Dumazet if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) { 153330038fc6SEric Dumazet rcu_read_unlock(); 15341da177e4SLinus Torvalds return; 153530038fc6SEric Dumazet } 153630038fc6SEric Dumazet log_martians = IN_DEV_LOG_MARTIANS(in_dev); 153730038fc6SEric Dumazet rcu_read_unlock(); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds /* No redirected packets during ip_rt_redirect_silence; 15401da177e4SLinus Torvalds * reset the algorithm. 15411da177e4SLinus Torvalds */ 1542d8d1f30bSChangli Gao if (time_after(jiffies, rt->dst.rate_last + ip_rt_redirect_silence)) 1543d8d1f30bSChangli Gao rt->dst.rate_tokens = 0; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* Too many ignored redirects; do not send anything 1546d8d1f30bSChangli Gao * set dst.rate_last to the last seen redirected packet. 15471da177e4SLinus Torvalds */ 1548d8d1f30bSChangli Gao if (rt->dst.rate_tokens >= ip_rt_redirect_number) { 1549d8d1f30bSChangli Gao rt->dst.rate_last = jiffies; 155030038fc6SEric Dumazet return; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* Check for load limit; set rate_last to the latest sent 15541da177e4SLinus Torvalds * redirect. 15551da177e4SLinus Torvalds */ 1556d8d1f30bSChangli Gao if (rt->dst.rate_tokens == 0 || 155714fb8a76SLi Yewang time_after(jiffies, 1558d8d1f30bSChangli Gao (rt->dst.rate_last + 1559d8d1f30bSChangli Gao (ip_rt_redirect_load << rt->dst.rate_tokens)))) { 15601da177e4SLinus Torvalds icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway); 1561d8d1f30bSChangli Gao rt->dst.rate_last = jiffies; 1562d8d1f30bSChangli Gao ++rt->dst.rate_tokens; 15631da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 156430038fc6SEric Dumazet if (log_martians && 1565d8d1f30bSChangli Gao rt->dst.rate_tokens == ip_rt_redirect_number && 15661da177e4SLinus Torvalds net_ratelimit()) 1567673d57e7SHarvey Harrison printk(KERN_WARNING "host %pI4/if%d ignores redirects for %pI4 to %pI4.\n", 1568673d57e7SHarvey Harrison &rt->rt_src, rt->rt_iif, 1569673d57e7SHarvey Harrison &rt->rt_dst, &rt->rt_gateway); 15701da177e4SLinus Torvalds #endif 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds static int ip_error(struct sk_buff *skb) 15751da177e4SLinus Torvalds { 1576511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 15771da177e4SLinus Torvalds unsigned long now; 15781da177e4SLinus Torvalds int code; 15791da177e4SLinus Torvalds 1580d8d1f30bSChangli Gao switch (rt->dst.error) { 15811da177e4SLinus Torvalds case EINVAL: 15821da177e4SLinus Torvalds default: 15831da177e4SLinus Torvalds goto out; 15841da177e4SLinus Torvalds case EHOSTUNREACH: 15851da177e4SLinus Torvalds code = ICMP_HOST_UNREACH; 15861da177e4SLinus Torvalds break; 15871da177e4SLinus Torvalds case ENETUNREACH: 15881da177e4SLinus Torvalds code = ICMP_NET_UNREACH; 1589d8d1f30bSChangli Gao IP_INC_STATS_BH(dev_net(rt->dst.dev), 15907c73a6faSPavel Emelyanov IPSTATS_MIB_INNOROUTES); 15911da177e4SLinus Torvalds break; 15921da177e4SLinus Torvalds case EACCES: 15931da177e4SLinus Torvalds code = ICMP_PKT_FILTERED; 15941da177e4SLinus Torvalds break; 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds now = jiffies; 1598d8d1f30bSChangli Gao rt->dst.rate_tokens += now - rt->dst.rate_last; 1599d8d1f30bSChangli Gao if (rt->dst.rate_tokens > ip_rt_error_burst) 1600d8d1f30bSChangli Gao rt->dst.rate_tokens = ip_rt_error_burst; 1601d8d1f30bSChangli Gao rt->dst.rate_last = now; 1602d8d1f30bSChangli Gao if (rt->dst.rate_tokens >= ip_rt_error_cost) { 1603d8d1f30bSChangli Gao rt->dst.rate_tokens -= ip_rt_error_cost; 16041da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, code, 0); 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds out: kfree_skb(skb); 16081da177e4SLinus Torvalds return 0; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds /* 16121da177e4SLinus Torvalds * The last two values are not from the RFC but 16131da177e4SLinus Torvalds * are needed for AMPRnet AX.25 paths. 16141da177e4SLinus Torvalds */ 16151da177e4SLinus Torvalds 16169b5b5cffSArjan van de Ven static const unsigned short mtu_plateau[] = 16171da177e4SLinus Torvalds {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 }; 16181da177e4SLinus Torvalds 16195969f71dSStephen Hemminger static inline unsigned short guess_mtu(unsigned short old_mtu) 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds int i; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++) 16241da177e4SLinus Torvalds if (old_mtu > mtu_plateau[i]) 16251da177e4SLinus Torvalds return mtu_plateau[i]; 16261da177e4SLinus Torvalds return 68; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 1629b5921910SDenis V. Lunev unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph, 16300010e465STimo Teras unsigned short new_mtu, 16310010e465STimo Teras struct net_device *dev) 16321da177e4SLinus Torvalds { 16330010e465STimo Teras int i, k; 16341da177e4SLinus Torvalds unsigned short old_mtu = ntohs(iph->tot_len); 16351da177e4SLinus Torvalds struct rtable *rth; 16360010e465STimo Teras int ikeys[2] = { dev->ifindex, 0 }; 1637e448515cSAl Viro __be32 skeys[2] = { iph->saddr, 0, }; 1638e448515cSAl Viro __be32 daddr = iph->daddr; 16391da177e4SLinus Torvalds unsigned short est_mtu = 0; 16401da177e4SLinus Torvalds 16410010e465STimo Teras for (k = 0; k < 2; k++) { 16421da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 1643b00180deSDenis V. Lunev unsigned hash = rt_hash(daddr, skeys[i], ikeys[k], 1644e84f84f2SDenis V. Lunev rt_genid(net)); 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds rcu_read_lock(); 16471da177e4SLinus Torvalds for (rth = rcu_dereference(rt_hash_table[hash].chain); rth; 1648d8d1f30bSChangli Gao rth = rcu_dereference(rth->dst.rt_next)) { 16491da177e4SLinus Torvalds unsigned short mtu = new_mtu; 16501da177e4SLinus Torvalds 16510010e465STimo Teras if (rth->fl.fl4_dst != daddr || 16520010e465STimo Teras rth->fl.fl4_src != skeys[i] || 16530010e465STimo Teras rth->rt_dst != daddr || 16540010e465STimo Teras rth->rt_src != iph->saddr || 16550010e465STimo Teras rth->fl.oif != ikeys[k] || 16560010e465STimo Teras rth->fl.iif != 0 || 1657d8d1f30bSChangli Gao dst_metric_locked(&rth->dst, RTAX_MTU) || 1658d8d1f30bSChangli Gao !net_eq(dev_net(rth->dst.dev), net) || 16596c3b8fc6SHugh Dickins rt_is_expired(rth)) 16600010e465STimo Teras continue; 16610010e465STimo Teras 16621da177e4SLinus Torvalds if (new_mtu < 68 || new_mtu >= old_mtu) { 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds /* BSD 4.2 compatibility hack :-( */ 16651da177e4SLinus Torvalds if (mtu == 0 && 1666d8d1f30bSChangli Gao old_mtu >= dst_mtu(&rth->dst) && 16671da177e4SLinus Torvalds old_mtu >= 68 + (iph->ihl << 2)) 16681da177e4SLinus Torvalds old_mtu -= iph->ihl << 2; 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds mtu = guess_mtu(old_mtu); 16711da177e4SLinus Torvalds } 1672d8d1f30bSChangli Gao if (mtu <= dst_mtu(&rth->dst)) { 1673d8d1f30bSChangli Gao if (mtu < dst_mtu(&rth->dst)) { 1674d8d1f30bSChangli Gao dst_confirm(&rth->dst); 16751da177e4SLinus Torvalds if (mtu < ip_rt_min_pmtu) { 16761da177e4SLinus Torvalds mtu = ip_rt_min_pmtu; 1677d8d1f30bSChangli Gao rth->dst.metrics[RTAX_LOCK-1] |= 16781da177e4SLinus Torvalds (1 << RTAX_MTU); 16791da177e4SLinus Torvalds } 1680d8d1f30bSChangli Gao rth->dst.metrics[RTAX_MTU-1] = mtu; 1681d8d1f30bSChangli Gao dst_set_expires(&rth->dst, 16821da177e4SLinus Torvalds ip_rt_mtu_expires); 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds est_mtu = mtu; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds rcu_read_unlock(); 16881da177e4SLinus Torvalds } 16890010e465STimo Teras } 16901da177e4SLinus Torvalds return est_mtu ? : new_mtu; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 16941da177e4SLinus Torvalds { 16956d273f8dSRami Rosen if (dst_mtu(dst) > mtu && mtu >= 68 && 16961da177e4SLinus Torvalds !(dst_metric_locked(dst, RTAX_MTU))) { 16971da177e4SLinus Torvalds if (mtu < ip_rt_min_pmtu) { 16981da177e4SLinus Torvalds mtu = ip_rt_min_pmtu; 16991da177e4SLinus Torvalds dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU); 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds dst->metrics[RTAX_MTU-1] = mtu; 17021da177e4SLinus Torvalds dst_set_expires(dst, ip_rt_mtu_expires); 17038d71740cSTom Tucker call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst); 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie) 17081da177e4SLinus Torvalds { 1709d11a4dc1STimo Teräs if (rt_is_expired((struct rtable *)dst)) 17101da177e4SLinus Torvalds return NULL; 1711d11a4dc1STimo Teräs return dst; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds static void ipv4_dst_destroy(struct dst_entry *dst) 17151da177e4SLinus Torvalds { 17161da177e4SLinus Torvalds struct rtable *rt = (struct rtable *) dst; 17171da177e4SLinus Torvalds struct inet_peer *peer = rt->peer; 17181da177e4SLinus Torvalds struct in_device *idev = rt->idev; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds if (peer) { 17211da177e4SLinus Torvalds rt->peer = NULL; 17221da177e4SLinus Torvalds inet_putpeer(peer); 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds if (idev) { 17261da177e4SLinus Torvalds rt->idev = NULL; 17271da177e4SLinus Torvalds in_dev_put(idev); 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 17321da177e4SLinus Torvalds int how) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct rtable *rt = (struct rtable *) dst; 17351da177e4SLinus Torvalds struct in_device *idev = rt->idev; 1736c346dca1SYOSHIFUJI Hideaki if (dev != dev_net(dev)->loopback_dev && idev && idev->dev == dev) { 17375a3e55d6SDenis V. Lunev struct in_device *loopback_idev = 1738c346dca1SYOSHIFUJI Hideaki in_dev_get(dev_net(dev)->loopback_dev); 17391da177e4SLinus Torvalds if (loopback_idev) { 17401da177e4SLinus Torvalds rt->idev = loopback_idev; 17411da177e4SLinus Torvalds in_dev_put(idev); 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds static void ipv4_link_failure(struct sk_buff *skb) 17471da177e4SLinus Torvalds { 17481da177e4SLinus Torvalds struct rtable *rt; 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 17511da177e4SLinus Torvalds 1752511c3f92SEric Dumazet rt = skb_rtable(skb); 17531da177e4SLinus Torvalds if (rt) 1754d8d1f30bSChangli Gao dst_set_expires(&rt->dst, 0); 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds static int ip_rt_bug(struct sk_buff *skb) 17581da177e4SLinus Torvalds { 1759673d57e7SHarvey Harrison printk(KERN_DEBUG "ip_rt_bug: %pI4 -> %pI4, %s\n", 1760673d57e7SHarvey Harrison &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, 17611da177e4SLinus Torvalds skb->dev ? skb->dev->name : "?"); 17621da177e4SLinus Torvalds kfree_skb(skb); 17631da177e4SLinus Torvalds return 0; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* 17671da177e4SLinus Torvalds We do not cache source address of outgoing interface, 17681da177e4SLinus Torvalds because it is used only by IP RR, TS and SRR options, 17691da177e4SLinus Torvalds so that it out of fast path. 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds BTW remember: "addr" is allowed to be not aligned 17721da177e4SLinus Torvalds in IP options! 17731da177e4SLinus Torvalds */ 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds void ip_rt_get_source(u8 *addr, struct rtable *rt) 17761da177e4SLinus Torvalds { 1777a61ced5dSAl Viro __be32 src; 17781da177e4SLinus Torvalds struct fib_result res; 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds if (rt->fl.iif == 0) 17811da177e4SLinus Torvalds src = rt->rt_src; 1782d8d1f30bSChangli Gao else if (fib_lookup(dev_net(rt->dst.dev), &rt->fl, &res) == 0) { 17831da177e4SLinus Torvalds src = FIB_RES_PREFSRC(res); 17841da177e4SLinus Torvalds fib_res_put(&res); 17851da177e4SLinus Torvalds } else 1786d8d1f30bSChangli Gao src = inet_select_addr(rt->dst.dev, rt->rt_gateway, 17871da177e4SLinus Torvalds RT_SCOPE_UNIVERSE); 17881da177e4SLinus Torvalds memcpy(addr, &src, 4); 17891da177e4SLinus Torvalds } 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 17921da177e4SLinus Torvalds static void set_class_tag(struct rtable *rt, u32 tag) 17931da177e4SLinus Torvalds { 1794d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF)) 1795d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF; 1796d8d1f30bSChangli Gao if (!(rt->dst.tclassid & 0xFFFF0000)) 1797d8d1f30bSChangli Gao rt->dst.tclassid |= tag & 0xFFFF0000; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds #endif 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds struct fib_info *fi = res->fi; 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds if (fi) { 18061da177e4SLinus Torvalds if (FIB_RES_GW(*res) && 18071da177e4SLinus Torvalds FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) 18081da177e4SLinus Torvalds rt->rt_gateway = FIB_RES_GW(*res); 1809d8d1f30bSChangli Gao memcpy(rt->dst.metrics, fi->fib_metrics, 1810d8d1f30bSChangli Gao sizeof(rt->dst.metrics)); 18111da177e4SLinus Torvalds if (fi->fib_mtu == 0) { 1812d8d1f30bSChangli Gao rt->dst.metrics[RTAX_MTU-1] = rt->dst.dev->mtu; 1813d8d1f30bSChangli Gao if (dst_metric_locked(&rt->dst, RTAX_MTU) && 18141da177e4SLinus Torvalds rt->rt_gateway != rt->rt_dst && 1815d8d1f30bSChangli Gao rt->dst.dev->mtu > 576) 1816d8d1f30bSChangli Gao rt->dst.metrics[RTAX_MTU-1] = 576; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 1819d8d1f30bSChangli Gao rt->dst.tclassid = FIB_RES_NH(*res).nh_tclassid; 18201da177e4SLinus Torvalds #endif 18211da177e4SLinus Torvalds } else 1822d8d1f30bSChangli Gao rt->dst.metrics[RTAX_MTU-1]= rt->dst.dev->mtu; 18231da177e4SLinus Torvalds 1824d8d1f30bSChangli Gao if (dst_metric(&rt->dst, RTAX_HOPLIMIT) == 0) 1825d8d1f30bSChangli Gao rt->dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl; 1826d8d1f30bSChangli Gao if (dst_mtu(&rt->dst) > IP_MAX_MTU) 1827d8d1f30bSChangli Gao rt->dst.metrics[RTAX_MTU-1] = IP_MAX_MTU; 1828d8d1f30bSChangli Gao if (dst_metric(&rt->dst, RTAX_ADVMSS) == 0) 1829d8d1f30bSChangli Gao rt->dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->dst.dev->mtu - 40, 18301da177e4SLinus Torvalds ip_rt_min_advmss); 1831d8d1f30bSChangli Gao if (dst_metric(&rt->dst, RTAX_ADVMSS) > 65535 - 40) 1832d8d1f30bSChangli Gao rt->dst.metrics[RTAX_ADVMSS-1] = 65535 - 40; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 18351da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES 18361da177e4SLinus Torvalds set_class_tag(rt, fib_rules_tclass(res)); 18371da177e4SLinus Torvalds #endif 18381da177e4SLinus Torvalds set_class_tag(rt, itag); 18391da177e4SLinus Torvalds #endif 18401da177e4SLinus Torvalds rt->rt_type = res->type; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 184396d36220SEric Dumazet /* called in rcu_read_lock() section */ 18449e12bb22SAl Viro static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr, 18451da177e4SLinus Torvalds u8 tos, struct net_device *dev, int our) 18461da177e4SLinus Torvalds { 184796d36220SEric Dumazet unsigned int hash; 18481da177e4SLinus Torvalds struct rtable *rth; 1849a61ced5dSAl Viro __be32 spec_dst; 185096d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 18511da177e4SLinus Torvalds u32 itag = 0; 1852b5f7e755SEric Dumazet int err; 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* Primary sanity checks. */ 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds if (in_dev == NULL) 18571da177e4SLinus Torvalds return -EINVAL; 18581da177e4SLinus Torvalds 18591e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1860f97c1e0cSJoe Perches ipv4_is_loopback(saddr) || skb->protocol != htons(ETH_P_IP)) 18611da177e4SLinus Torvalds goto e_inval; 18621da177e4SLinus Torvalds 1863f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) { 1864f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr)) 18651da177e4SLinus Torvalds goto e_inval; 18661da177e4SLinus Torvalds spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK); 1867b5f7e755SEric Dumazet } else { 1868b5f7e755SEric Dumazet err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst, 1869b5f7e755SEric Dumazet &itag, 0); 1870b5f7e755SEric Dumazet if (err < 0) 1871b5f7e755SEric Dumazet goto e_err; 1872b5f7e755SEric Dumazet } 18731da177e4SLinus Torvalds rth = dst_alloc(&ipv4_dst_ops); 18741da177e4SLinus Torvalds if (!rth) 18751da177e4SLinus Torvalds goto e_nobufs; 18761da177e4SLinus Torvalds 1877d8d1f30bSChangli Gao rth->dst.output = ip_rt_bug; 1878d8d1f30bSChangli Gao rth->dst.obsolete = -1; 18791da177e4SLinus Torvalds 1880d8d1f30bSChangli Gao atomic_set(&rth->dst.__refcnt, 1); 1881d8d1f30bSChangli Gao rth->dst.flags= DST_HOST; 188242f811b8SHerbert Xu if (IN_DEV_CONF_GET(in_dev, NOPOLICY)) 1883d8d1f30bSChangli Gao rth->dst.flags |= DST_NOPOLICY; 18841da177e4SLinus Torvalds rth->fl.fl4_dst = daddr; 18851da177e4SLinus Torvalds rth->rt_dst = daddr; 18861da177e4SLinus Torvalds rth->fl.fl4_tos = tos; 188747dcf0cbSThomas Graf rth->fl.mark = skb->mark; 18881da177e4SLinus Torvalds rth->fl.fl4_src = saddr; 18891da177e4SLinus Torvalds rth->rt_src = saddr; 18901da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 1891d8d1f30bSChangli Gao rth->dst.tclassid = itag; 18921da177e4SLinus Torvalds #endif 18931da177e4SLinus Torvalds rth->rt_iif = 18941da177e4SLinus Torvalds rth->fl.iif = dev->ifindex; 1895d8d1f30bSChangli Gao rth->dst.dev = init_net.loopback_dev; 1896d8d1f30bSChangli Gao dev_hold(rth->dst.dev); 1897d8d1f30bSChangli Gao rth->idev = in_dev_get(rth->dst.dev); 18981da177e4SLinus Torvalds rth->fl.oif = 0; 18991da177e4SLinus Torvalds rth->rt_gateway = daddr; 19001da177e4SLinus Torvalds rth->rt_spec_dst= spec_dst; 1901e84f84f2SDenis V. Lunev rth->rt_genid = rt_genid(dev_net(dev)); 19021da177e4SLinus Torvalds rth->rt_flags = RTCF_MULTICAST; 190329e75252SEric Dumazet rth->rt_type = RTN_MULTICAST; 19041da177e4SLinus Torvalds if (our) { 1905d8d1f30bSChangli Gao rth->dst.input= ip_local_deliver; 19061da177e4SLinus Torvalds rth->rt_flags |= RTCF_LOCAL; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 1910f97c1e0cSJoe Perches if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev)) 1911d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 19121da177e4SLinus Torvalds #endif 19131da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_mc); 19141da177e4SLinus Torvalds 1915e84f84f2SDenis V. Lunev hash = rt_hash(daddr, saddr, dev->ifindex, rt_genid(dev_net(dev))); 19166a2bad70SPavel Emelyanov return rt_intern_hash(hash, rth, NULL, skb, dev->ifindex); 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds e_nobufs: 19191da177e4SLinus Torvalds return -ENOBUFS; 19201da177e4SLinus Torvalds e_inval: 192196d36220SEric Dumazet return -EINVAL; 1922b5f7e755SEric Dumazet e_err: 1923b5f7e755SEric Dumazet return err; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds static void ip_handle_martian_source(struct net_device *dev, 19281da177e4SLinus Torvalds struct in_device *in_dev, 19291da177e4SLinus Torvalds struct sk_buff *skb, 19309e12bb22SAl Viro __be32 daddr, 19319e12bb22SAl Viro __be32 saddr) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_src); 19341da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 19351da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) { 19361da177e4SLinus Torvalds /* 19371da177e4SLinus Torvalds * RFC1812 recommendation, if source is martian, 19381da177e4SLinus Torvalds * the only hint is MAC header. 19391da177e4SLinus Torvalds */ 1940673d57e7SHarvey Harrison printk(KERN_WARNING "martian source %pI4 from %pI4, on dev %s\n", 1941673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 194298e399f8SArnaldo Carvalho de Melo if (dev->hard_header_len && skb_mac_header_was_set(skb)) { 19431da177e4SLinus Torvalds int i; 194498e399f8SArnaldo Carvalho de Melo const unsigned char *p = skb_mac_header(skb); 19451da177e4SLinus Torvalds printk(KERN_WARNING "ll header: "); 19461da177e4SLinus Torvalds for (i = 0; i < dev->hard_header_len; i++, p++) { 19471da177e4SLinus Torvalds printk("%02x", *p); 19481da177e4SLinus Torvalds if (i < (dev->hard_header_len - 1)) 19491da177e4SLinus Torvalds printk(":"); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds printk("\n"); 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds #endif 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 195747360228SEric Dumazet /* called in rcu_read_lock() section */ 19585969f71dSStephen Hemminger static int __mkroute_input(struct sk_buff *skb, 19591da177e4SLinus Torvalds struct fib_result *res, 19601da177e4SLinus Torvalds struct in_device *in_dev, 19619e12bb22SAl Viro __be32 daddr, __be32 saddr, u32 tos, 19621da177e4SLinus Torvalds struct rtable **result) 19631da177e4SLinus Torvalds { 19641da177e4SLinus Torvalds struct rtable *rth; 19651da177e4SLinus Torvalds int err; 19661da177e4SLinus Torvalds struct in_device *out_dev; 196747360228SEric Dumazet unsigned int flags = 0; 1968d9c9df8cSAl Viro __be32 spec_dst; 1969d9c9df8cSAl Viro u32 itag; 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds /* get a working reference to the output device */ 197247360228SEric Dumazet out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res)); 19731da177e4SLinus Torvalds if (out_dev == NULL) { 19741da177e4SLinus Torvalds if (net_ratelimit()) 19751da177e4SLinus Torvalds printk(KERN_CRIT "Bug in ip_route_input" \ 19761da177e4SLinus Torvalds "_slow(). Please, report\n"); 19771da177e4SLinus Torvalds return -EINVAL; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res), 1982b0c110caSjamal in_dev->dev, &spec_dst, &itag, skb->mark); 19831da177e4SLinus Torvalds if (err < 0) { 19841da177e4SLinus Torvalds ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr, 19851da177e4SLinus Torvalds saddr); 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds goto cleanup; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (err) 19911da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 19921da177e4SLinus Torvalds 199351b77caeSThomas Graf if (out_dev == in_dev && err && 19941da177e4SLinus Torvalds (IN_DEV_SHARED_MEDIA(out_dev) || 19951da177e4SLinus Torvalds inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res)))) 19961da177e4SLinus Torvalds flags |= RTCF_DOREDIRECT; 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) { 19991da177e4SLinus Torvalds /* Not IP (i.e. ARP). Do not create route, if it is 20001da177e4SLinus Torvalds * invalid for proxy arp. DNAT routes are always valid. 200165324144SJesper Dangaard Brouer * 200265324144SJesper Dangaard Brouer * Proxy arp feature have been extended to allow, ARP 200365324144SJesper Dangaard Brouer * replies back to the same interface, to support 200465324144SJesper Dangaard Brouer * Private VLAN switch technologies. See arp.c. 20051da177e4SLinus Torvalds */ 200665324144SJesper Dangaard Brouer if (out_dev == in_dev && 200765324144SJesper Dangaard Brouer IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) { 20081da177e4SLinus Torvalds err = -EINVAL; 20091da177e4SLinus Torvalds goto cleanup; 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds rth = dst_alloc(&ipv4_dst_ops); 20151da177e4SLinus Torvalds if (!rth) { 20161da177e4SLinus Torvalds err = -ENOBUFS; 20171da177e4SLinus Torvalds goto cleanup; 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 2020d8d1f30bSChangli Gao atomic_set(&rth->dst.__refcnt, 1); 2021d8d1f30bSChangli Gao rth->dst.flags= DST_HOST; 202242f811b8SHerbert Xu if (IN_DEV_CONF_GET(in_dev, NOPOLICY)) 2023d8d1f30bSChangli Gao rth->dst.flags |= DST_NOPOLICY; 202442f811b8SHerbert Xu if (IN_DEV_CONF_GET(out_dev, NOXFRM)) 2025d8d1f30bSChangli Gao rth->dst.flags |= DST_NOXFRM; 20261da177e4SLinus Torvalds rth->fl.fl4_dst = daddr; 20271da177e4SLinus Torvalds rth->rt_dst = daddr; 20281da177e4SLinus Torvalds rth->fl.fl4_tos = tos; 202947dcf0cbSThomas Graf rth->fl.mark = skb->mark; 20301da177e4SLinus Torvalds rth->fl.fl4_src = saddr; 20311da177e4SLinus Torvalds rth->rt_src = saddr; 20321da177e4SLinus Torvalds rth->rt_gateway = daddr; 20331da177e4SLinus Torvalds rth->rt_iif = 20341da177e4SLinus Torvalds rth->fl.iif = in_dev->dev->ifindex; 2035d8d1f30bSChangli Gao rth->dst.dev = (out_dev)->dev; 2036d8d1f30bSChangli Gao dev_hold(rth->dst.dev); 2037d8d1f30bSChangli Gao rth->idev = in_dev_get(rth->dst.dev); 20381da177e4SLinus Torvalds rth->fl.oif = 0; 20391da177e4SLinus Torvalds rth->rt_spec_dst= spec_dst; 20401da177e4SLinus Torvalds 2041d8d1f30bSChangli Gao rth->dst.obsolete = -1; 2042d8d1f30bSChangli Gao rth->dst.input = ip_forward; 2043d8d1f30bSChangli Gao rth->dst.output = ip_output; 2044d8d1f30bSChangli Gao rth->rt_genid = rt_genid(dev_net(rth->dst.dev)); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds rt_set_nexthop(rth, res, itag); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds rth->rt_flags = flags; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds *result = rth; 20511da177e4SLinus Torvalds err = 0; 20521da177e4SLinus Torvalds cleanup: 20531da177e4SLinus Torvalds return err; 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 20565969f71dSStephen Hemminger static int ip_mkroute_input(struct sk_buff *skb, 20571da177e4SLinus Torvalds struct fib_result *res, 20581da177e4SLinus Torvalds const struct flowi *fl, 20591da177e4SLinus Torvalds struct in_device *in_dev, 20609e12bb22SAl Viro __be32 daddr, __be32 saddr, u32 tos) 20611da177e4SLinus Torvalds { 20627abaa27cSChuck Short struct rtable* rth = NULL; 20631da177e4SLinus Torvalds int err; 20641da177e4SLinus Torvalds unsigned hash; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 20671da177e4SLinus Torvalds if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0) 20681da177e4SLinus Torvalds fib_select_multipath(fl, res); 20691da177e4SLinus Torvalds #endif 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /* create a routing cache entry */ 20721da177e4SLinus Torvalds err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth); 20731da177e4SLinus Torvalds if (err) 20741da177e4SLinus Torvalds return err; 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds /* put it into the cache */ 2077e84f84f2SDenis V. Lunev hash = rt_hash(daddr, saddr, fl->iif, 2078d8d1f30bSChangli Gao rt_genid(dev_net(rth->dst.dev))); 20796a2bad70SPavel Emelyanov return rt_intern_hash(hash, rth, NULL, skb, fl->iif); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds /* 20831da177e4SLinus Torvalds * NOTE. We drop all the packets that has local source 20841da177e4SLinus Torvalds * addresses, because every properly looped back packet 20851da177e4SLinus Torvalds * must have correct destination already attached by output routine. 20861da177e4SLinus Torvalds * 20871da177e4SLinus Torvalds * Such approach solves two big problems: 20881da177e4SLinus Torvalds * 1. Not simplex devices are handled properly. 20891da177e4SLinus Torvalds * 2. IP spoofing attempts are filtered with 100% of guarantee. 20901da177e4SLinus Torvalds */ 20911da177e4SLinus Torvalds 20929e12bb22SAl Viro static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, 20931da177e4SLinus Torvalds u8 tos, struct net_device *dev) 20941da177e4SLinus Torvalds { 20951da177e4SLinus Torvalds struct fib_result res; 209696d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 20971da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = 20981da177e4SLinus Torvalds { .daddr = daddr, 20991da177e4SLinus Torvalds .saddr = saddr, 21001da177e4SLinus Torvalds .tos = tos, 21011da177e4SLinus Torvalds .scope = RT_SCOPE_UNIVERSE, 21021da177e4SLinus Torvalds } }, 210347dcf0cbSThomas Graf .mark = skb->mark, 21041da177e4SLinus Torvalds .iif = dev->ifindex }; 21051da177e4SLinus Torvalds unsigned flags = 0; 21061da177e4SLinus Torvalds u32 itag = 0; 21071da177e4SLinus Torvalds struct rtable * rth; 21081da177e4SLinus Torvalds unsigned hash; 21099e12bb22SAl Viro __be32 spec_dst; 21101da177e4SLinus Torvalds int err = -EINVAL; 21111da177e4SLinus Torvalds int free_res = 0; 2112c346dca1SYOSHIFUJI Hideaki struct net * net = dev_net(dev); 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds /* IP on this device is disabled. */ 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds if (!in_dev) 21171da177e4SLinus Torvalds goto out; 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /* Check for the most weird martians, which can be not detected 21201da177e4SLinus Torvalds by fib_lookup. 21211da177e4SLinus Torvalds */ 21221da177e4SLinus Torvalds 21231e637c74SJan Engelhardt if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 2124f97c1e0cSJoe Perches ipv4_is_loopback(saddr)) 21251da177e4SLinus Torvalds goto martian_source; 21261da177e4SLinus Torvalds 2127e448515cSAl Viro if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0)) 21281da177e4SLinus Torvalds goto brd_input; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* Accept zero addresses only to limited broadcast; 21311da177e4SLinus Torvalds * I even do not know to fix it or not. Waiting for complains :-) 21321da177e4SLinus Torvalds */ 2133f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) 21341da177e4SLinus Torvalds goto martian_source; 21351da177e4SLinus Torvalds 21361e637c74SJan Engelhardt if (ipv4_is_lbcast(daddr) || ipv4_is_zeronet(daddr) || 2137f97c1e0cSJoe Perches ipv4_is_loopback(daddr)) 21381da177e4SLinus Torvalds goto martian_destination; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds /* 21411da177e4SLinus Torvalds * Now we are ready to route packet. 21421da177e4SLinus Torvalds */ 214384a885f4SDenis V. Lunev if ((err = fib_lookup(net, &fl, &res)) != 0) { 21441da177e4SLinus Torvalds if (!IN_DEV_FORWARD(in_dev)) 21452c2910a4SDietmar Eggemann goto e_hostunreach; 21461da177e4SLinus Torvalds goto no_route; 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds free_res = 1; 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_slow_tot); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds if (res.type == RTN_BROADCAST) 21531da177e4SLinus Torvalds goto brd_input; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 2156b5f7e755SEric Dumazet err = fib_validate_source(saddr, daddr, tos, 215784a885f4SDenis V. Lunev net->loopback_dev->ifindex, 2158b0c110caSjamal dev, &spec_dst, &itag, skb->mark); 2159b5f7e755SEric Dumazet if (err < 0) 2160b5f7e755SEric Dumazet goto martian_source_keep_err; 2161b5f7e755SEric Dumazet if (err) 21621da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 21631da177e4SLinus Torvalds spec_dst = daddr; 21641da177e4SLinus Torvalds goto local_input; 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds if (!IN_DEV_FORWARD(in_dev)) 21682c2910a4SDietmar Eggemann goto e_hostunreach; 21691da177e4SLinus Torvalds if (res.type != RTN_UNICAST) 21701da177e4SLinus Torvalds goto martian_destination; 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos); 21731da177e4SLinus Torvalds done: 21741da177e4SLinus Torvalds if (free_res) 21751da177e4SLinus Torvalds fib_res_put(&res); 21761da177e4SLinus Torvalds out: return err; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds brd_input: 21791da177e4SLinus Torvalds if (skb->protocol != htons(ETH_P_IP)) 21801da177e4SLinus Torvalds goto e_inval; 21811da177e4SLinus Torvalds 2182f97c1e0cSJoe Perches if (ipv4_is_zeronet(saddr)) 21831da177e4SLinus Torvalds spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK); 21841da177e4SLinus Torvalds else { 21851da177e4SLinus Torvalds err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst, 2186b0c110caSjamal &itag, skb->mark); 21871da177e4SLinus Torvalds if (err < 0) 2188b5f7e755SEric Dumazet goto martian_source_keep_err; 21891da177e4SLinus Torvalds if (err) 21901da177e4SLinus Torvalds flags |= RTCF_DIRECTSRC; 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds flags |= RTCF_BROADCAST; 21931da177e4SLinus Torvalds res.type = RTN_BROADCAST; 21941da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_brd); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds local_input: 21971da177e4SLinus Torvalds rth = dst_alloc(&ipv4_dst_ops); 21981da177e4SLinus Torvalds if (!rth) 21991da177e4SLinus Torvalds goto e_nobufs; 22001da177e4SLinus Torvalds 2201d8d1f30bSChangli Gao rth->dst.output= ip_rt_bug; 2202d8d1f30bSChangli Gao rth->dst.obsolete = -1; 2203e84f84f2SDenis V. Lunev rth->rt_genid = rt_genid(net); 22041da177e4SLinus Torvalds 2205d8d1f30bSChangli Gao atomic_set(&rth->dst.__refcnt, 1); 2206d8d1f30bSChangli Gao rth->dst.flags= DST_HOST; 220742f811b8SHerbert Xu if (IN_DEV_CONF_GET(in_dev, NOPOLICY)) 2208d8d1f30bSChangli Gao rth->dst.flags |= DST_NOPOLICY; 22091da177e4SLinus Torvalds rth->fl.fl4_dst = daddr; 22101da177e4SLinus Torvalds rth->rt_dst = daddr; 22111da177e4SLinus Torvalds rth->fl.fl4_tos = tos; 221247dcf0cbSThomas Graf rth->fl.mark = skb->mark; 22131da177e4SLinus Torvalds rth->fl.fl4_src = saddr; 22141da177e4SLinus Torvalds rth->rt_src = saddr; 22151da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 2216d8d1f30bSChangli Gao rth->dst.tclassid = itag; 22171da177e4SLinus Torvalds #endif 22181da177e4SLinus Torvalds rth->rt_iif = 22191da177e4SLinus Torvalds rth->fl.iif = dev->ifindex; 2220d8d1f30bSChangli Gao rth->dst.dev = net->loopback_dev; 2221d8d1f30bSChangli Gao dev_hold(rth->dst.dev); 2222d8d1f30bSChangli Gao rth->idev = in_dev_get(rth->dst.dev); 22231da177e4SLinus Torvalds rth->rt_gateway = daddr; 22241da177e4SLinus Torvalds rth->rt_spec_dst= spec_dst; 2225d8d1f30bSChangli Gao rth->dst.input= ip_local_deliver; 22261da177e4SLinus Torvalds rth->rt_flags = flags|RTCF_LOCAL; 22271da177e4SLinus Torvalds if (res.type == RTN_UNREACHABLE) { 2228d8d1f30bSChangli Gao rth->dst.input= ip_error; 2229d8d1f30bSChangli Gao rth->dst.error= -err; 22301da177e4SLinus Torvalds rth->rt_flags &= ~RTCF_LOCAL; 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds rth->rt_type = res.type; 2233e84f84f2SDenis V. Lunev hash = rt_hash(daddr, saddr, fl.iif, rt_genid(net)); 22346a2bad70SPavel Emelyanov err = rt_intern_hash(hash, rth, NULL, skb, fl.iif); 22351da177e4SLinus Torvalds goto done; 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds no_route: 22381da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_no_route); 22391da177e4SLinus Torvalds spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE); 22401da177e4SLinus Torvalds res.type = RTN_UNREACHABLE; 22417f53878dSMitsuru Chinen if (err == -ESRCH) 22427f53878dSMitsuru Chinen err = -ENETUNREACH; 22431da177e4SLinus Torvalds goto local_input; 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds /* 22461da177e4SLinus Torvalds * Do not cache martian addresses: they should be logged (RFC1812) 22471da177e4SLinus Torvalds */ 22481da177e4SLinus Torvalds martian_destination: 22491da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_martian_dst); 22501da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_VERBOSE 22511da177e4SLinus Torvalds if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) 2252673d57e7SHarvey Harrison printk(KERN_WARNING "martian destination %pI4 from %pI4, dev %s\n", 2253673d57e7SHarvey Harrison &daddr, &saddr, dev->name); 22541da177e4SLinus Torvalds #endif 22552c2910a4SDietmar Eggemann 22562c2910a4SDietmar Eggemann e_hostunreach: 22572c2910a4SDietmar Eggemann err = -EHOSTUNREACH; 22582c2910a4SDietmar Eggemann goto done; 22592c2910a4SDietmar Eggemann 22601da177e4SLinus Torvalds e_inval: 22611da177e4SLinus Torvalds err = -EINVAL; 22621da177e4SLinus Torvalds goto done; 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds e_nobufs: 22651da177e4SLinus Torvalds err = -ENOBUFS; 22661da177e4SLinus Torvalds goto done; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds martian_source: 2269b5f7e755SEric Dumazet err = -EINVAL; 2270b5f7e755SEric Dumazet martian_source_keep_err: 22711da177e4SLinus Torvalds ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 2272b5f7e755SEric Dumazet goto done; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 2275407eadd9SEric Dumazet int ip_route_input_common(struct sk_buff *skb, __be32 daddr, __be32 saddr, 2276407eadd9SEric Dumazet u8 tos, struct net_device *dev, bool noref) 22771da177e4SLinus Torvalds { 22781da177e4SLinus Torvalds struct rtable * rth; 22791da177e4SLinus Torvalds unsigned hash; 22801da177e4SLinus Torvalds int iif = dev->ifindex; 2281b5921910SDenis V. Lunev struct net *net; 228296d36220SEric Dumazet int res; 22831da177e4SLinus Torvalds 2284c346dca1SYOSHIFUJI Hideaki net = dev_net(dev); 22851080d709SNeil Horman 228696d36220SEric Dumazet rcu_read_lock(); 228796d36220SEric Dumazet 22881080d709SNeil Horman if (!rt_caching(net)) 22891080d709SNeil Horman goto skip_cache; 22901080d709SNeil Horman 22911da177e4SLinus Torvalds tos &= IPTOS_RT_MASK; 2292e84f84f2SDenis V. Lunev hash = rt_hash(daddr, saddr, iif, rt_genid(net)); 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds for (rth = rcu_dereference(rt_hash_table[hash].chain); rth; 2295d8d1f30bSChangli Gao rth = rcu_dereference(rth->dst.rt_next)) { 22960eae88f3SEric Dumazet if ((((__force u32)rth->fl.fl4_dst ^ (__force u32)daddr) | 22970eae88f3SEric Dumazet ((__force u32)rth->fl.fl4_src ^ (__force u32)saddr) | 2298c0b8c32bSStephen Hemminger (rth->fl.iif ^ iif) | 2299c0b8c32bSStephen Hemminger rth->fl.oif | 2300c0b8c32bSStephen Hemminger (rth->fl.fl4_tos ^ tos)) == 0 && 230147dcf0cbSThomas Graf rth->fl.mark == skb->mark && 2302d8d1f30bSChangli Gao net_eq(dev_net(rth->dst.dev), net) && 2303e84f84f2SDenis V. Lunev !rt_is_expired(rth)) { 2304407eadd9SEric Dumazet if (noref) { 2305d8d1f30bSChangli Gao dst_use_noref(&rth->dst, jiffies); 2306d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rth->dst); 2307407eadd9SEric Dumazet } else { 2308d8d1f30bSChangli Gao dst_use(&rth->dst, jiffies); 2309d8d1f30bSChangli Gao skb_dst_set(skb, &rth->dst); 2310407eadd9SEric Dumazet } 23111da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_hit); 23121da177e4SLinus Torvalds rcu_read_unlock(); 23131da177e4SLinus Torvalds return 0; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds RT_CACHE_STAT_INC(in_hlist_search); 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 23181080d709SNeil Horman skip_cache: 23191da177e4SLinus Torvalds /* Multicast recognition logic is moved from route cache to here. 23201da177e4SLinus Torvalds The problem was that too many Ethernet cards have broken/missing 23211da177e4SLinus Torvalds hardware multicast filters :-( As result the host on multicasting 23221da177e4SLinus Torvalds network acquires a lot of useless route cache entries, sort of 23231da177e4SLinus Torvalds SDR messages from all the world. Now we try to get rid of them. 23241da177e4SLinus Torvalds Really, provided software IP multicast filter is organized 23251da177e4SLinus Torvalds reasonably (at least, hashed), it does not result in a slowdown 23261da177e4SLinus Torvalds comparing with route cache reject entries. 23271da177e4SLinus Torvalds Note, that multicast routers are not affected, because 23281da177e4SLinus Torvalds route cache entry is created eventually. 23291da177e4SLinus Torvalds */ 2330f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 233196d36220SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 23321da177e4SLinus Torvalds 233396d36220SEric Dumazet if (in_dev) { 23341da177e4SLinus Torvalds int our = ip_check_mc(in_dev, daddr, saddr, 2335eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol); 23361da177e4SLinus Torvalds if (our 23371da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 23389d4fb27dSJoe Perches || 23399d4fb27dSJoe Perches (!ipv4_is_local_multicast(daddr) && 2340f97c1e0cSJoe Perches IN_DEV_MFORWARD(in_dev)) 23411da177e4SLinus Torvalds #endif 23421da177e4SLinus Torvalds ) { 234396d36220SEric Dumazet int res = ip_route_input_mc(skb, daddr, saddr, 23441da177e4SLinus Torvalds tos, dev, our); 234596d36220SEric Dumazet rcu_read_unlock(); 234696d36220SEric Dumazet return res; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds } 23491da177e4SLinus Torvalds rcu_read_unlock(); 23501da177e4SLinus Torvalds return -EINVAL; 23511da177e4SLinus Torvalds } 235296d36220SEric Dumazet res = ip_route_input_slow(skb, daddr, saddr, tos, dev); 235396d36220SEric Dumazet rcu_read_unlock(); 235496d36220SEric Dumazet return res; 23551da177e4SLinus Torvalds } 2356407eadd9SEric Dumazet EXPORT_SYMBOL(ip_route_input_common); 23571da177e4SLinus Torvalds 23585969f71dSStephen Hemminger static int __mkroute_output(struct rtable **result, 23591da177e4SLinus Torvalds struct fib_result *res, 23601da177e4SLinus Torvalds const struct flowi *fl, 23611da177e4SLinus Torvalds const struct flowi *oldflp, 23621da177e4SLinus Torvalds struct net_device *dev_out, 23631da177e4SLinus Torvalds unsigned flags) 23641da177e4SLinus Torvalds { 23651da177e4SLinus Torvalds struct rtable *rth; 23661da177e4SLinus Torvalds struct in_device *in_dev; 23671da177e4SLinus Torvalds u32 tos = RT_FL_TOS(oldflp); 23681da177e4SLinus Torvalds int err = 0; 23691da177e4SLinus Torvalds 2370f97c1e0cSJoe Perches if (ipv4_is_loopback(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK)) 23711da177e4SLinus Torvalds return -EINVAL; 23721da177e4SLinus Torvalds 2373e448515cSAl Viro if (fl->fl4_dst == htonl(0xFFFFFFFF)) 23741da177e4SLinus Torvalds res->type = RTN_BROADCAST; 2375f97c1e0cSJoe Perches else if (ipv4_is_multicast(fl->fl4_dst)) 23761da177e4SLinus Torvalds res->type = RTN_MULTICAST; 23771e637c74SJan Engelhardt else if (ipv4_is_lbcast(fl->fl4_dst) || ipv4_is_zeronet(fl->fl4_dst)) 23781da177e4SLinus Torvalds return -EINVAL; 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds if (dev_out->flags & IFF_LOOPBACK) 23811da177e4SLinus Torvalds flags |= RTCF_LOCAL; 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds /* get work reference to inet device */ 23841da177e4SLinus Torvalds in_dev = in_dev_get(dev_out); 23851da177e4SLinus Torvalds if (!in_dev) 23861da177e4SLinus Torvalds return -EINVAL; 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds if (res->type == RTN_BROADCAST) { 23891da177e4SLinus Torvalds flags |= RTCF_BROADCAST | RTCF_LOCAL; 23901da177e4SLinus Torvalds if (res->fi) { 23911da177e4SLinus Torvalds fib_info_put(res->fi); 23921da177e4SLinus Torvalds res->fi = NULL; 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds } else if (res->type == RTN_MULTICAST) { 23951da177e4SLinus Torvalds flags |= RTCF_MULTICAST|RTCF_LOCAL; 23961da177e4SLinus Torvalds if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src, 23971da177e4SLinus Torvalds oldflp->proto)) 23981da177e4SLinus Torvalds flags &= ~RTCF_LOCAL; 23991da177e4SLinus Torvalds /* If multicast route do not exist use 24001da177e4SLinus Torvalds default one, but do not gateway in this case. 24011da177e4SLinus Torvalds Yes, it is hack. 24021da177e4SLinus Torvalds */ 24031da177e4SLinus Torvalds if (res->fi && res->prefixlen < 4) { 24041da177e4SLinus Torvalds fib_info_put(res->fi); 24051da177e4SLinus Torvalds res->fi = NULL; 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds rth = dst_alloc(&ipv4_dst_ops); 24111da177e4SLinus Torvalds if (!rth) { 24121da177e4SLinus Torvalds err = -ENOBUFS; 24131da177e4SLinus Torvalds goto cleanup; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 2416d8d1f30bSChangli Gao atomic_set(&rth->dst.__refcnt, 1); 2417d8d1f30bSChangli Gao rth->dst.flags= DST_HOST; 241842f811b8SHerbert Xu if (IN_DEV_CONF_GET(in_dev, NOXFRM)) 2419d8d1f30bSChangli Gao rth->dst.flags |= DST_NOXFRM; 242042f811b8SHerbert Xu if (IN_DEV_CONF_GET(in_dev, NOPOLICY)) 2421d8d1f30bSChangli Gao rth->dst.flags |= DST_NOPOLICY; 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds rth->fl.fl4_dst = oldflp->fl4_dst; 24241da177e4SLinus Torvalds rth->fl.fl4_tos = tos; 24251da177e4SLinus Torvalds rth->fl.fl4_src = oldflp->fl4_src; 24261da177e4SLinus Torvalds rth->fl.oif = oldflp->oif; 242747dcf0cbSThomas Graf rth->fl.mark = oldflp->mark; 24281da177e4SLinus Torvalds rth->rt_dst = fl->fl4_dst; 24291da177e4SLinus Torvalds rth->rt_src = fl->fl4_src; 24301da177e4SLinus Torvalds rth->rt_iif = oldflp->oif ? : dev_out->ifindex; 24311da177e4SLinus Torvalds /* get references to the devices that are to be hold by the routing 24321da177e4SLinus Torvalds cache entry */ 2433d8d1f30bSChangli Gao rth->dst.dev = dev_out; 24341da177e4SLinus Torvalds dev_hold(dev_out); 24351da177e4SLinus Torvalds rth->idev = in_dev_get(dev_out); 24361da177e4SLinus Torvalds rth->rt_gateway = fl->fl4_dst; 24371da177e4SLinus Torvalds rth->rt_spec_dst= fl->fl4_src; 24381da177e4SLinus Torvalds 2439d8d1f30bSChangli Gao rth->dst.output=ip_output; 2440d8d1f30bSChangli Gao rth->dst.obsolete = -1; 2441e84f84f2SDenis V. Lunev rth->rt_genid = rt_genid(dev_net(dev_out)); 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_tot); 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if (flags & RTCF_LOCAL) { 2446d8d1f30bSChangli Gao rth->dst.input = ip_local_deliver; 24471da177e4SLinus Torvalds rth->rt_spec_dst = fl->fl4_dst; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 24501da177e4SLinus Torvalds rth->rt_spec_dst = fl->fl4_src; 24511da177e4SLinus Torvalds if (flags & RTCF_LOCAL && 24521da177e4SLinus Torvalds !(dev_out->flags & IFF_LOOPBACK)) { 2453d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 24541da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_slow_mc); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 24571da177e4SLinus Torvalds if (res->type == RTN_MULTICAST) { 24581da177e4SLinus Torvalds if (IN_DEV_MFORWARD(in_dev) && 2459f97c1e0cSJoe Perches !ipv4_is_local_multicast(oldflp->fl4_dst)) { 2460d8d1f30bSChangli Gao rth->dst.input = ip_mr_input; 2461d8d1f30bSChangli Gao rth->dst.output = ip_mc_output; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds #endif 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds rt_set_nexthop(rth, res, 0); 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds rth->rt_flags = flags; 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds *result = rth; 24721da177e4SLinus Torvalds cleanup: 24731da177e4SLinus Torvalds /* release work reference to inet device */ 24741da177e4SLinus Torvalds in_dev_put(in_dev); 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds return err; 24771da177e4SLinus Torvalds } 24781da177e4SLinus Torvalds 24795969f71dSStephen Hemminger static int ip_mkroute_output(struct rtable **rp, 24801da177e4SLinus Torvalds struct fib_result *res, 24811da177e4SLinus Torvalds const struct flowi *fl, 24821da177e4SLinus Torvalds const struct flowi *oldflp, 24831da177e4SLinus Torvalds struct net_device *dev_out, 24841da177e4SLinus Torvalds unsigned flags) 24851da177e4SLinus Torvalds { 24867abaa27cSChuck Short struct rtable *rth = NULL; 24871da177e4SLinus Torvalds int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags); 24881da177e4SLinus Torvalds unsigned hash; 24891da177e4SLinus Torvalds if (err == 0) { 2490b00180deSDenis V. Lunev hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif, 2491e84f84f2SDenis V. Lunev rt_genid(dev_net(dev_out))); 24926a2bad70SPavel Emelyanov err = rt_intern_hash(hash, rth, rp, NULL, oldflp->oif); 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds return err; 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds /* 24991da177e4SLinus Torvalds * Major route resolver routine. 25001da177e4SLinus Torvalds */ 25011da177e4SLinus Torvalds 2502b40afd0eSDenis V. Lunev static int ip_route_output_slow(struct net *net, struct rtable **rp, 2503b40afd0eSDenis V. Lunev const struct flowi *oldflp) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds u32 tos = RT_FL_TOS(oldflp); 25061da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = 25071da177e4SLinus Torvalds { .daddr = oldflp->fl4_dst, 25081da177e4SLinus Torvalds .saddr = oldflp->fl4_src, 25091da177e4SLinus Torvalds .tos = tos & IPTOS_RT_MASK, 25101da177e4SLinus Torvalds .scope = ((tos & RTO_ONLINK) ? 25111da177e4SLinus Torvalds RT_SCOPE_LINK : 25121da177e4SLinus Torvalds RT_SCOPE_UNIVERSE), 25131da177e4SLinus Torvalds } }, 251447dcf0cbSThomas Graf .mark = oldflp->mark, 2515b40afd0eSDenis V. Lunev .iif = net->loopback_dev->ifindex, 25161da177e4SLinus Torvalds .oif = oldflp->oif }; 25171da177e4SLinus Torvalds struct fib_result res; 25181da177e4SLinus Torvalds unsigned flags = 0; 25191da177e4SLinus Torvalds struct net_device *dev_out = NULL; 25201da177e4SLinus Torvalds int free_res = 0; 25211da177e4SLinus Torvalds int err; 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds res.fi = NULL; 25251da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTIPLE_TABLES 25261da177e4SLinus Torvalds res.r = NULL; 25271da177e4SLinus Torvalds #endif 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds if (oldflp->fl4_src) { 25301da177e4SLinus Torvalds err = -EINVAL; 2531f97c1e0cSJoe Perches if (ipv4_is_multicast(oldflp->fl4_src) || 25321e637c74SJan Engelhardt ipv4_is_lbcast(oldflp->fl4_src) || 2533f97c1e0cSJoe Perches ipv4_is_zeronet(oldflp->fl4_src)) 25341da177e4SLinus Torvalds goto out; 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds /* I removed check for oif == dev_out->oif here. 25371da177e4SLinus Torvalds It was wrong for two reasons: 25381ab35276SDenis V. Lunev 1. ip_dev_find(net, saddr) can return wrong iface, if saddr 25391ab35276SDenis V. Lunev is assigned to multiple interfaces. 25401da177e4SLinus Torvalds 2. Moreover, we are allowed to send packets with saddr 25411da177e4SLinus Torvalds of another iface. --ANK 25421da177e4SLinus Torvalds */ 25431da177e4SLinus Torvalds 25449d4fb27dSJoe Perches if (oldflp->oif == 0 && 25459d4fb27dSJoe Perches (ipv4_is_multicast(oldflp->fl4_dst) || 2546f97c1e0cSJoe Perches oldflp->fl4_dst == htonl(0xFFFFFFFF))) { 2547a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2548a210d01aSJulian Anastasov dev_out = ip_dev_find(net, oldflp->fl4_src); 2549a210d01aSJulian Anastasov if (dev_out == NULL) 2550a210d01aSJulian Anastasov goto out; 2551a210d01aSJulian Anastasov 25521da177e4SLinus Torvalds /* Special hack: user can direct multicasts 25531da177e4SLinus Torvalds and limited broadcast via necessary interface 25541da177e4SLinus Torvalds without fiddling with IP_MULTICAST_IF or IP_PKTINFO. 25551da177e4SLinus Torvalds This hack is not just for fun, it allows 25561da177e4SLinus Torvalds vic,vat and friends to work. 25571da177e4SLinus Torvalds They bind socket to loopback, set ttl to zero 25581da177e4SLinus Torvalds and expect that it will work. 25591da177e4SLinus Torvalds From the viewpoint of routing cache they are broken, 25601da177e4SLinus Torvalds because we are not allowed to build multicast path 25611da177e4SLinus Torvalds with loopback source addr (look, routing cache 25621da177e4SLinus Torvalds cannot know, that ttl is zero, so that packet 25631da177e4SLinus Torvalds will not leave this host and route is valid). 25641da177e4SLinus Torvalds Luckily, this hack is good workaround. 25651da177e4SLinus Torvalds */ 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds fl.oif = dev_out->ifindex; 25681da177e4SLinus Torvalds goto make_route; 25691da177e4SLinus Torvalds } 2570a210d01aSJulian Anastasov 2571a210d01aSJulian Anastasov if (!(oldflp->flags & FLOWI_FLAG_ANYSRC)) { 2572a210d01aSJulian Anastasov /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2573a210d01aSJulian Anastasov dev_out = ip_dev_find(net, oldflp->fl4_src); 2574a210d01aSJulian Anastasov if (dev_out == NULL) 2575a210d01aSJulian Anastasov goto out; 25761da177e4SLinus Torvalds dev_put(dev_out); 25771da177e4SLinus Torvalds dev_out = NULL; 25781da177e4SLinus Torvalds } 2579a210d01aSJulian Anastasov } 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds if (oldflp->oif) { 2583b40afd0eSDenis V. Lunev dev_out = dev_get_by_index(net, oldflp->oif); 25841da177e4SLinus Torvalds err = -ENODEV; 25851da177e4SLinus Torvalds if (dev_out == NULL) 25861da177e4SLinus Torvalds goto out; 2587e5ed6399SHerbert Xu 2588e5ed6399SHerbert Xu /* RACE: Check return value of inet_select_addr instead. */ 2589e5ed6399SHerbert Xu if (__in_dev_get_rtnl(dev_out) == NULL) { 25901da177e4SLinus Torvalds dev_put(dev_out); 25911da177e4SLinus Torvalds goto out; /* Wrong error code */ 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 2594f97c1e0cSJoe Perches if (ipv4_is_local_multicast(oldflp->fl4_dst) || 2595f97c1e0cSJoe Perches oldflp->fl4_dst == htonl(0xFFFFFFFF)) { 25961da177e4SLinus Torvalds if (!fl.fl4_src) 25971da177e4SLinus Torvalds fl.fl4_src = inet_select_addr(dev_out, 0, 25981da177e4SLinus Torvalds RT_SCOPE_LINK); 25991da177e4SLinus Torvalds goto make_route; 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds if (!fl.fl4_src) { 2602f97c1e0cSJoe Perches if (ipv4_is_multicast(oldflp->fl4_dst)) 26031da177e4SLinus Torvalds fl.fl4_src = inet_select_addr(dev_out, 0, 26041da177e4SLinus Torvalds fl.fl4_scope); 26051da177e4SLinus Torvalds else if (!oldflp->fl4_dst) 26061da177e4SLinus Torvalds fl.fl4_src = inet_select_addr(dev_out, 0, 26071da177e4SLinus Torvalds RT_SCOPE_HOST); 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds if (!fl.fl4_dst) { 26121da177e4SLinus Torvalds fl.fl4_dst = fl.fl4_src; 26131da177e4SLinus Torvalds if (!fl.fl4_dst) 26141da177e4SLinus Torvalds fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK); 26151da177e4SLinus Torvalds if (dev_out) 26161da177e4SLinus Torvalds dev_put(dev_out); 2617b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 26181da177e4SLinus Torvalds dev_hold(dev_out); 2619b40afd0eSDenis V. Lunev fl.oif = net->loopback_dev->ifindex; 26201da177e4SLinus Torvalds res.type = RTN_LOCAL; 26211da177e4SLinus Torvalds flags |= RTCF_LOCAL; 26221da177e4SLinus Torvalds goto make_route; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 2625b40afd0eSDenis V. Lunev if (fib_lookup(net, &fl, &res)) { 26261da177e4SLinus Torvalds res.fi = NULL; 26271da177e4SLinus Torvalds if (oldflp->oif) { 26281da177e4SLinus Torvalds /* Apparently, routing tables are wrong. Assume, 26291da177e4SLinus Torvalds that the destination is on link. 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds WHY? DW. 26321da177e4SLinus Torvalds Because we are allowed to send to iface 26331da177e4SLinus Torvalds even if it has NO routes and NO assigned 26341da177e4SLinus Torvalds addresses. When oif is specified, routing 26351da177e4SLinus Torvalds tables are looked up with only one purpose: 26361da177e4SLinus Torvalds to catch if destination is gatewayed, rather than 26371da177e4SLinus Torvalds direct. Moreover, if MSG_DONTROUTE is set, 26381da177e4SLinus Torvalds we send packet, ignoring both routing tables 26391da177e4SLinus Torvalds and ifaddr state. --ANK 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds We could make it even if oif is unknown, 26431da177e4SLinus Torvalds likely IPv6, but we do not. 26441da177e4SLinus Torvalds */ 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds if (fl.fl4_src == 0) 26471da177e4SLinus Torvalds fl.fl4_src = inet_select_addr(dev_out, 0, 26481da177e4SLinus Torvalds RT_SCOPE_LINK); 26491da177e4SLinus Torvalds res.type = RTN_UNICAST; 26501da177e4SLinus Torvalds goto make_route; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds if (dev_out) 26531da177e4SLinus Torvalds dev_put(dev_out); 26541da177e4SLinus Torvalds err = -ENETUNREACH; 26551da177e4SLinus Torvalds goto out; 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds free_res = 1; 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds if (res.type == RTN_LOCAL) { 26601da177e4SLinus Torvalds if (!fl.fl4_src) 26611da177e4SLinus Torvalds fl.fl4_src = fl.fl4_dst; 26621da177e4SLinus Torvalds if (dev_out) 26631da177e4SLinus Torvalds dev_put(dev_out); 2664b40afd0eSDenis V. Lunev dev_out = net->loopback_dev; 26651da177e4SLinus Torvalds dev_hold(dev_out); 26661da177e4SLinus Torvalds fl.oif = dev_out->ifindex; 26671da177e4SLinus Torvalds if (res.fi) 26681da177e4SLinus Torvalds fib_info_put(res.fi); 26691da177e4SLinus Torvalds res.fi = NULL; 26701da177e4SLinus Torvalds flags |= RTCF_LOCAL; 26711da177e4SLinus Torvalds goto make_route; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_MULTIPATH 26751da177e4SLinus Torvalds if (res.fi->fib_nhs > 1 && fl.oif == 0) 26761da177e4SLinus Torvalds fib_select_multipath(&fl, &res); 26771da177e4SLinus Torvalds else 26781da177e4SLinus Torvalds #endif 26791da177e4SLinus Torvalds if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif) 2680b40afd0eSDenis V. Lunev fib_select_default(net, &fl, &res); 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds if (!fl.fl4_src) 26831da177e4SLinus Torvalds fl.fl4_src = FIB_RES_PREFSRC(res); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds if (dev_out) 26861da177e4SLinus Torvalds dev_put(dev_out); 26871da177e4SLinus Torvalds dev_out = FIB_RES_DEV(res); 26881da177e4SLinus Torvalds dev_hold(dev_out); 26891da177e4SLinus Torvalds fl.oif = dev_out->ifindex; 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds make_route: 26931da177e4SLinus Torvalds err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags); 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds if (free_res) 26971da177e4SLinus Torvalds fib_res_put(&res); 26981da177e4SLinus Torvalds if (dev_out) 26991da177e4SLinus Torvalds dev_put(dev_out); 27001da177e4SLinus Torvalds out: return err; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 2703611c183eSDenis V. Lunev int __ip_route_output_key(struct net *net, struct rtable **rp, 2704611c183eSDenis V. Lunev const struct flowi *flp) 27051da177e4SLinus Torvalds { 27061da177e4SLinus Torvalds unsigned hash; 27071da177e4SLinus Torvalds struct rtable *rth; 27081da177e4SLinus Torvalds 27091080d709SNeil Horman if (!rt_caching(net)) 27101080d709SNeil Horman goto slow_output; 27111080d709SNeil Horman 2712e84f84f2SDenis V. Lunev hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif, rt_genid(net)); 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds rcu_read_lock_bh(); 2715a898def2SPaul E. McKenney for (rth = rcu_dereference_bh(rt_hash_table[hash].chain); rth; 2716d8d1f30bSChangli Gao rth = rcu_dereference_bh(rth->dst.rt_next)) { 27171da177e4SLinus Torvalds if (rth->fl.fl4_dst == flp->fl4_dst && 27181da177e4SLinus Torvalds rth->fl.fl4_src == flp->fl4_src && 27191da177e4SLinus Torvalds rth->fl.iif == 0 && 27201da177e4SLinus Torvalds rth->fl.oif == flp->oif && 272147dcf0cbSThomas Graf rth->fl.mark == flp->mark && 27221da177e4SLinus Torvalds !((rth->fl.fl4_tos ^ flp->fl4_tos) & 2723b5921910SDenis V. Lunev (IPTOS_RT_MASK | RTO_ONLINK)) && 2724d8d1f30bSChangli Gao net_eq(dev_net(rth->dst.dev), net) && 2725e84f84f2SDenis V. Lunev !rt_is_expired(rth)) { 2726d8d1f30bSChangli Gao dst_use(&rth->dst, jiffies); 27271da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_hit); 27281da177e4SLinus Torvalds rcu_read_unlock_bh(); 27291da177e4SLinus Torvalds *rp = rth; 27301da177e4SLinus Torvalds return 0; 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds RT_CACHE_STAT_INC(out_hlist_search); 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds rcu_read_unlock_bh(); 27351da177e4SLinus Torvalds 27361080d709SNeil Horman slow_output: 2737611c183eSDenis V. Lunev return ip_route_output_slow(net, rp, flp); 27381da177e4SLinus Torvalds } 2739d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(__ip_route_output_key); 2740d8c97a94SArnaldo Carvalho de Melo 2741*ae2688d5SJianzhao Wang static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie) 2742*ae2688d5SJianzhao Wang { 2743*ae2688d5SJianzhao Wang return NULL; 2744*ae2688d5SJianzhao Wang } 2745*ae2688d5SJianzhao Wang 274614e50e57SDavid S. Miller static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 274714e50e57SDavid S. Miller { 274814e50e57SDavid S. Miller } 274914e50e57SDavid S. Miller 275014e50e57SDavid S. Miller static struct dst_ops ipv4_dst_blackhole_ops = { 275114e50e57SDavid S. Miller .family = AF_INET, 275209640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 275314e50e57SDavid S. Miller .destroy = ipv4_dst_destroy, 2754*ae2688d5SJianzhao Wang .check = ipv4_blackhole_dst_check, 275514e50e57SDavid S. Miller .update_pmtu = ipv4_rt_blackhole_update_pmtu, 2756e2422970SEric Dumazet .entries = ATOMIC_INIT(0), 275714e50e57SDavid S. Miller }; 275814e50e57SDavid S. Miller 275914e50e57SDavid S. Miller 2760e84f84f2SDenis V. Lunev static int ipv4_dst_blackhole(struct net *net, struct rtable **rp, struct flowi *flp) 276114e50e57SDavid S. Miller { 276214e50e57SDavid S. Miller struct rtable *ort = *rp; 276314e50e57SDavid S. Miller struct rtable *rt = (struct rtable *) 276414e50e57SDavid S. Miller dst_alloc(&ipv4_dst_blackhole_ops); 276514e50e57SDavid S. Miller 276614e50e57SDavid S. Miller if (rt) { 2767d8d1f30bSChangli Gao struct dst_entry *new = &rt->dst; 276814e50e57SDavid S. Miller 276914e50e57SDavid S. Miller atomic_set(&new->__refcnt, 1); 277014e50e57SDavid S. Miller new->__use = 1; 2771352e512cSHerbert Xu new->input = dst_discard; 2772352e512cSHerbert Xu new->output = dst_discard; 2773d8d1f30bSChangli Gao memcpy(new->metrics, ort->dst.metrics, RTAX_MAX*sizeof(u32)); 277414e50e57SDavid S. Miller 2775d8d1f30bSChangli Gao new->dev = ort->dst.dev; 277614e50e57SDavid S. Miller if (new->dev) 277714e50e57SDavid S. Miller dev_hold(new->dev); 277814e50e57SDavid S. Miller 277914e50e57SDavid S. Miller rt->fl = ort->fl; 278014e50e57SDavid S. Miller 278114e50e57SDavid S. Miller rt->idev = ort->idev; 278214e50e57SDavid S. Miller if (rt->idev) 278314e50e57SDavid S. Miller in_dev_hold(rt->idev); 2784e84f84f2SDenis V. Lunev rt->rt_genid = rt_genid(net); 278514e50e57SDavid S. Miller rt->rt_flags = ort->rt_flags; 278614e50e57SDavid S. Miller rt->rt_type = ort->rt_type; 278714e50e57SDavid S. Miller rt->rt_dst = ort->rt_dst; 278814e50e57SDavid S. Miller rt->rt_src = ort->rt_src; 278914e50e57SDavid S. Miller rt->rt_iif = ort->rt_iif; 279014e50e57SDavid S. Miller rt->rt_gateway = ort->rt_gateway; 279114e50e57SDavid S. Miller rt->rt_spec_dst = ort->rt_spec_dst; 279214e50e57SDavid S. Miller rt->peer = ort->peer; 279314e50e57SDavid S. Miller if (rt->peer) 279414e50e57SDavid S. Miller atomic_inc(&rt->peer->refcnt); 279514e50e57SDavid S. Miller 279614e50e57SDavid S. Miller dst_free(new); 279714e50e57SDavid S. Miller } 279814e50e57SDavid S. Miller 2799d8d1f30bSChangli Gao dst_release(&(*rp)->dst); 280014e50e57SDavid S. Miller *rp = rt; 280114e50e57SDavid S. Miller return (rt ? 0 : -ENOMEM); 280214e50e57SDavid S. Miller } 280314e50e57SDavid S. Miller 2804f1b050bfSDenis V. Lunev int ip_route_output_flow(struct net *net, struct rtable **rp, struct flowi *flp, 2805f1b050bfSDenis V. Lunev struct sock *sk, int flags) 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds int err; 28081da177e4SLinus Torvalds 2809f1b050bfSDenis V. Lunev if ((err = __ip_route_output_key(net, rp, flp)) != 0) 28101da177e4SLinus Torvalds return err; 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds if (flp->proto) { 28131da177e4SLinus Torvalds if (!flp->fl4_src) 28141da177e4SLinus Torvalds flp->fl4_src = (*rp)->rt_src; 28151da177e4SLinus Torvalds if (!flp->fl4_dst) 28161da177e4SLinus Torvalds flp->fl4_dst = (*rp)->rt_dst; 281752479b62SAlexey Dobriyan err = __xfrm_lookup(net, (struct dst_entry **)rp, flp, sk, 2818bb72845eSHerbert Xu flags ? XFRM_LOOKUP_WAIT : 0); 281914e50e57SDavid S. Miller if (err == -EREMOTE) 2820e84f84f2SDenis V. Lunev err = ipv4_dst_blackhole(net, rp, flp); 282114e50e57SDavid S. Miller 282214e50e57SDavid S. Miller return err; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds 28251da177e4SLinus Torvalds return 0; 28261da177e4SLinus Torvalds } 2827d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_route_output_flow); 2828d8c97a94SArnaldo Carvalho de Melo 2829f206351aSDenis V. Lunev int ip_route_output_key(struct net *net, struct rtable **rp, struct flowi *flp) 28301da177e4SLinus Torvalds { 2831f206351aSDenis V. Lunev return ip_route_output_flow(net, rp, flp, NULL, 0); 28321da177e4SLinus Torvalds } 28334bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_route_output_key); 28341da177e4SLinus Torvalds 28354feb88e5SBenjamin Thery static int rt_fill_info(struct net *net, 28364feb88e5SBenjamin Thery struct sk_buff *skb, u32 pid, u32 seq, int event, 2837b6544c0bSJamal Hadi Salim int nowait, unsigned int flags) 28381da177e4SLinus Torvalds { 2839511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 28401da177e4SLinus Torvalds struct rtmsg *r; 28411da177e4SLinus Torvalds struct nlmsghdr *nlh; 2842e3703b3dSThomas Graf long expires; 2843e3703b3dSThomas Graf u32 id = 0, ts = 0, tsage = 0, error; 2844be403ea1SThomas Graf 2845be403ea1SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags); 2846be403ea1SThomas Graf if (nlh == NULL) 284726932566SPatrick McHardy return -EMSGSIZE; 2848be403ea1SThomas Graf 2849be403ea1SThomas Graf r = nlmsg_data(nlh); 28501da177e4SLinus Torvalds r->rtm_family = AF_INET; 28511da177e4SLinus Torvalds r->rtm_dst_len = 32; 28521da177e4SLinus Torvalds r->rtm_src_len = 0; 28531da177e4SLinus Torvalds r->rtm_tos = rt->fl.fl4_tos; 28541da177e4SLinus Torvalds r->rtm_table = RT_TABLE_MAIN; 2855be403ea1SThomas Graf NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN); 28561da177e4SLinus Torvalds r->rtm_type = rt->rt_type; 28571da177e4SLinus Torvalds r->rtm_scope = RT_SCOPE_UNIVERSE; 28581da177e4SLinus Torvalds r->rtm_protocol = RTPROT_UNSPEC; 28591da177e4SLinus Torvalds r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; 28601da177e4SLinus Torvalds if (rt->rt_flags & RTCF_NOTIFY) 28611da177e4SLinus Torvalds r->rtm_flags |= RTM_F_NOTIFY; 2862be403ea1SThomas Graf 286317fb2c64SAl Viro NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst); 2864be403ea1SThomas Graf 28651da177e4SLinus Torvalds if (rt->fl.fl4_src) { 28661da177e4SLinus Torvalds r->rtm_src_len = 32; 286717fb2c64SAl Viro NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src); 28681da177e4SLinus Torvalds } 2869d8d1f30bSChangli Gao if (rt->dst.dev) 2870d8d1f30bSChangli Gao NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex); 28711da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 2872d8d1f30bSChangli Gao if (rt->dst.tclassid) 2873d8d1f30bSChangli Gao NLA_PUT_U32(skb, RTA_FLOW, rt->dst.tclassid); 28741da177e4SLinus Torvalds #endif 28751da177e4SLinus Torvalds if (rt->fl.iif) 287617fb2c64SAl Viro NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst); 28771da177e4SLinus Torvalds else if (rt->rt_src != rt->fl.fl4_src) 287817fb2c64SAl Viro NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src); 2879be403ea1SThomas Graf 28801da177e4SLinus Torvalds if (rt->rt_dst != rt->rt_gateway) 288117fb2c64SAl Viro NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway); 2882be403ea1SThomas Graf 2883d8d1f30bSChangli Gao if (rtnetlink_put_metrics(skb, rt->dst.metrics) < 0) 2884be403ea1SThomas Graf goto nla_put_failure; 2885be403ea1SThomas Graf 2886963bfeeeSEric Dumazet if (rt->fl.mark) 2887963bfeeeSEric Dumazet NLA_PUT_BE32(skb, RTA_MARK, rt->fl.mark); 2888963bfeeeSEric Dumazet 2889d8d1f30bSChangli Gao error = rt->dst.error; 2890d8d1f30bSChangli Gao expires = rt->dst.expires ? rt->dst.expires - jiffies : 0; 28911da177e4SLinus Torvalds if (rt->peer) { 2892317fe0e6SEric Dumazet inet_peer_refcheck(rt->peer); 28932c1409a0SEric Dumazet id = atomic_read(&rt->peer->ip_id_count) & 0xffff; 28941da177e4SLinus Torvalds if (rt->peer->tcp_ts_stamp) { 2895e3703b3dSThomas Graf ts = rt->peer->tcp_ts; 28969d729f72SJames Morris tsage = get_seconds() - rt->peer->tcp_ts_stamp; 28971da177e4SLinus Torvalds } 28981da177e4SLinus Torvalds } 2899be403ea1SThomas Graf 29001da177e4SLinus Torvalds if (rt->fl.iif) { 29011da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 2902e448515cSAl Viro __be32 dst = rt->rt_dst; 29031da177e4SLinus Torvalds 2904f97c1e0cSJoe Perches if (ipv4_is_multicast(dst) && !ipv4_is_local_multicast(dst) && 29054feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)) { 29064feb88e5SBenjamin Thery int err = ipmr_get_route(net, skb, r, nowait); 29071da177e4SLinus Torvalds if (err <= 0) { 29081da177e4SLinus Torvalds if (!nowait) { 29091da177e4SLinus Torvalds if (err == 0) 29101da177e4SLinus Torvalds return 0; 2911be403ea1SThomas Graf goto nla_put_failure; 29121da177e4SLinus Torvalds } else { 29131da177e4SLinus Torvalds if (err == -EMSGSIZE) 2914be403ea1SThomas Graf goto nla_put_failure; 2915e3703b3dSThomas Graf error = err; 29161da177e4SLinus Torvalds } 29171da177e4SLinus Torvalds } 29181da177e4SLinus Torvalds } else 29191da177e4SLinus Torvalds #endif 2920be403ea1SThomas Graf NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif); 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 2923d8d1f30bSChangli Gao if (rtnl_put_cacheinfo(skb, &rt->dst, id, ts, tsage, 2924e3703b3dSThomas Graf expires, error) < 0) 2925e3703b3dSThomas Graf goto nla_put_failure; 29261da177e4SLinus Torvalds 2927be403ea1SThomas Graf return nlmsg_end(skb, nlh); 2928be403ea1SThomas Graf 2929be403ea1SThomas Graf nla_put_failure: 293026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 293126932566SPatrick McHardy return -EMSGSIZE; 29321da177e4SLinus Torvalds } 29331da177e4SLinus Torvalds 293463f3444fSThomas Graf static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 29351da177e4SLinus Torvalds { 29363b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2937d889ce3bSThomas Graf struct rtmsg *rtm; 2938d889ce3bSThomas Graf struct nlattr *tb[RTA_MAX+1]; 29391da177e4SLinus Torvalds struct rtable *rt = NULL; 29409e12bb22SAl Viro __be32 dst = 0; 29419e12bb22SAl Viro __be32 src = 0; 29429e12bb22SAl Viro u32 iif; 2943d889ce3bSThomas Graf int err; 2944963bfeeeSEric Dumazet int mark; 29451da177e4SLinus Torvalds struct sk_buff *skb; 29461da177e4SLinus Torvalds 2947d889ce3bSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy); 2948d889ce3bSThomas Graf if (err < 0) 2949d889ce3bSThomas Graf goto errout; 2950d889ce3bSThomas Graf 2951d889ce3bSThomas Graf rtm = nlmsg_data(nlh); 2952d889ce3bSThomas Graf 29531da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2954d889ce3bSThomas Graf if (skb == NULL) { 2955d889ce3bSThomas Graf err = -ENOBUFS; 2956d889ce3bSThomas Graf goto errout; 2957d889ce3bSThomas Graf } 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 29601da177e4SLinus Torvalds through good chunk of routing engine. 29611da177e4SLinus Torvalds */ 2962459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 2963c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2964d2c962b8SStephen Hemminger 2965d2c962b8SStephen Hemminger /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */ 2966eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = IPPROTO_ICMP; 29671da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr)); 29681da177e4SLinus Torvalds 296917fb2c64SAl Viro src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0; 297017fb2c64SAl Viro dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0; 2971d889ce3bSThomas Graf iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 2972963bfeeeSEric Dumazet mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds if (iif) { 2975d889ce3bSThomas Graf struct net_device *dev; 2976d889ce3bSThomas Graf 29771937504dSDenis V. Lunev dev = __dev_get_by_index(net, iif); 2978d889ce3bSThomas Graf if (dev == NULL) { 29791da177e4SLinus Torvalds err = -ENODEV; 2980d889ce3bSThomas Graf goto errout_free; 2981d889ce3bSThomas Graf } 2982d889ce3bSThomas Graf 29831da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 29841da177e4SLinus Torvalds skb->dev = dev; 2985963bfeeeSEric Dumazet skb->mark = mark; 29861da177e4SLinus Torvalds local_bh_disable(); 29871da177e4SLinus Torvalds err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev); 29881da177e4SLinus Torvalds local_bh_enable(); 2989d889ce3bSThomas Graf 2990511c3f92SEric Dumazet rt = skb_rtable(skb); 2991d8d1f30bSChangli Gao if (err == 0 && rt->dst.error) 2992d8d1f30bSChangli Gao err = -rt->dst.error; 29931da177e4SLinus Torvalds } else { 2994d889ce3bSThomas Graf struct flowi fl = { 2995d889ce3bSThomas Graf .nl_u = { 2996d889ce3bSThomas Graf .ip4_u = { 2997d889ce3bSThomas Graf .daddr = dst, 29981da177e4SLinus Torvalds .saddr = src, 2999d889ce3bSThomas Graf .tos = rtm->rtm_tos, 3000d889ce3bSThomas Graf }, 3001d889ce3bSThomas Graf }, 3002d889ce3bSThomas Graf .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0, 3003963bfeeeSEric Dumazet .mark = mark, 3004d889ce3bSThomas Graf }; 30051937504dSDenis V. Lunev err = ip_route_output_key(net, &rt, &fl); 30061da177e4SLinus Torvalds } 3007d889ce3bSThomas Graf 30081da177e4SLinus Torvalds if (err) 3009d889ce3bSThomas Graf goto errout_free; 30101da177e4SLinus Torvalds 3011d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 30121da177e4SLinus Torvalds if (rtm->rtm_flags & RTM_F_NOTIFY) 30131da177e4SLinus Torvalds rt->rt_flags |= RTCF_NOTIFY; 30141da177e4SLinus Torvalds 30154feb88e5SBenjamin Thery err = rt_fill_info(net, skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, 3016b6544c0bSJamal Hadi Salim RTM_NEWROUTE, 0, 0); 3017d889ce3bSThomas Graf if (err <= 0) 3018d889ce3bSThomas Graf goto errout_free; 30191da177e4SLinus Torvalds 30201937504dSDenis V. Lunev err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 3021d889ce3bSThomas Graf errout: 30222942e900SThomas Graf return err; 30231da177e4SLinus Torvalds 3024d889ce3bSThomas Graf errout_free: 30251da177e4SLinus Torvalds kfree_skb(skb); 3026d889ce3bSThomas Graf goto errout; 30271da177e4SLinus Torvalds } 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb) 30301da177e4SLinus Torvalds { 30311da177e4SLinus Torvalds struct rtable *rt; 30321da177e4SLinus Torvalds int h, s_h; 30331da177e4SLinus Torvalds int idx, s_idx; 30341937504dSDenis V. Lunev struct net *net; 30351937504dSDenis V. Lunev 30363b1e0a65SYOSHIFUJI Hideaki net = sock_net(skb->sk); 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds s_h = cb->args[0]; 3039d8c92830SEric Dumazet if (s_h < 0) 3040d8c92830SEric Dumazet s_h = 0; 30411da177e4SLinus Torvalds s_idx = idx = cb->args[1]; 3042a6272665SEric Dumazet for (h = s_h; h <= rt_hash_mask; h++, s_idx = 0) { 3043a6272665SEric Dumazet if (!rt_hash_table[h].chain) 3044a6272665SEric Dumazet continue; 30451da177e4SLinus Torvalds rcu_read_lock_bh(); 3046a898def2SPaul E. McKenney for (rt = rcu_dereference_bh(rt_hash_table[h].chain), idx = 0; rt; 3047d8d1f30bSChangli Gao rt = rcu_dereference_bh(rt->dst.rt_next), idx++) { 3048d8d1f30bSChangli Gao if (!net_eq(dev_net(rt->dst.dev), net) || idx < s_idx) 30491da177e4SLinus Torvalds continue; 3050e84f84f2SDenis V. Lunev if (rt_is_expired(rt)) 305129e75252SEric Dumazet continue; 3052d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 30534feb88e5SBenjamin Thery if (rt_fill_info(net, skb, NETLINK_CB(cb->skb).pid, 3054b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_NEWROUTE, 3055b6544c0bSJamal Hadi Salim 1, NLM_F_MULTI) <= 0) { 3056adf30907SEric Dumazet skb_dst_drop(skb); 30571da177e4SLinus Torvalds rcu_read_unlock_bh(); 30581da177e4SLinus Torvalds goto done; 30591da177e4SLinus Torvalds } 3060adf30907SEric Dumazet skb_dst_drop(skb); 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds rcu_read_unlock_bh(); 30631da177e4SLinus Torvalds } 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds done: 30661da177e4SLinus Torvalds cb->args[0] = h; 30671da177e4SLinus Torvalds cb->args[1] = idx; 30681da177e4SLinus Torvalds return skb->len; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds void ip_rt_multicast_event(struct in_device *in_dev) 30721da177e4SLinus Torvalds { 307376e6ebfbSDenis V. Lunev rt_cache_flush(dev_net(in_dev->dev), 0); 30741da177e4SLinus Torvalds } 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 307781c684d1SDenis V. Lunev static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write, 30788d65af78SAlexey Dobriyan void __user *buffer, 30791da177e4SLinus Torvalds size_t *lenp, loff_t *ppos) 30801da177e4SLinus Torvalds { 30811da177e4SLinus Torvalds if (write) { 3082639e104fSDenis V. Lunev int flush_delay; 308381c684d1SDenis V. Lunev ctl_table ctl; 308439a23e75SDenis V. Lunev struct net *net; 3085639e104fSDenis V. Lunev 308681c684d1SDenis V. Lunev memcpy(&ctl, __ctl, sizeof(ctl)); 308781c684d1SDenis V. Lunev ctl.data = &flush_delay; 30888d65af78SAlexey Dobriyan proc_dointvec(&ctl, write, buffer, lenp, ppos); 3089639e104fSDenis V. Lunev 309081c684d1SDenis V. Lunev net = (struct net *)__ctl->extra1; 309139a23e75SDenis V. Lunev rt_cache_flush(net, flush_delay); 30921da177e4SLinus Torvalds return 0; 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds return -EINVAL; 30961da177e4SLinus Torvalds } 30971da177e4SLinus Torvalds 3098eeb61f71SAl Viro static ctl_table ipv4_route_table[] = { 30991da177e4SLinus Torvalds { 31001da177e4SLinus Torvalds .procname = "gc_thresh", 31011da177e4SLinus Torvalds .data = &ipv4_dst_ops.gc_thresh, 31021da177e4SLinus Torvalds .maxlen = sizeof(int), 31031da177e4SLinus Torvalds .mode = 0644, 31046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31051da177e4SLinus Torvalds }, 31061da177e4SLinus Torvalds { 31071da177e4SLinus Torvalds .procname = "max_size", 31081da177e4SLinus Torvalds .data = &ip_rt_max_size, 31091da177e4SLinus Torvalds .maxlen = sizeof(int), 31101da177e4SLinus Torvalds .mode = 0644, 31116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31121da177e4SLinus Torvalds }, 31131da177e4SLinus Torvalds { 31141da177e4SLinus Torvalds /* Deprecated. Use gc_min_interval_ms */ 31151da177e4SLinus Torvalds 31161da177e4SLinus Torvalds .procname = "gc_min_interval", 31171da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 31181da177e4SLinus Torvalds .maxlen = sizeof(int), 31191da177e4SLinus Torvalds .mode = 0644, 31206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 31211da177e4SLinus Torvalds }, 31221da177e4SLinus Torvalds { 31231da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 31241da177e4SLinus Torvalds .data = &ip_rt_gc_min_interval, 31251da177e4SLinus Torvalds .maxlen = sizeof(int), 31261da177e4SLinus Torvalds .mode = 0644, 31276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 31281da177e4SLinus Torvalds }, 31291da177e4SLinus Torvalds { 31301da177e4SLinus Torvalds .procname = "gc_timeout", 31311da177e4SLinus Torvalds .data = &ip_rt_gc_timeout, 31321da177e4SLinus Torvalds .maxlen = sizeof(int), 31331da177e4SLinus Torvalds .mode = 0644, 31346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 31351da177e4SLinus Torvalds }, 31361da177e4SLinus Torvalds { 31371da177e4SLinus Torvalds .procname = "gc_interval", 31381da177e4SLinus Torvalds .data = &ip_rt_gc_interval, 31391da177e4SLinus Torvalds .maxlen = sizeof(int), 31401da177e4SLinus Torvalds .mode = 0644, 31416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 31421da177e4SLinus Torvalds }, 31431da177e4SLinus Torvalds { 31441da177e4SLinus Torvalds .procname = "redirect_load", 31451da177e4SLinus Torvalds .data = &ip_rt_redirect_load, 31461da177e4SLinus Torvalds .maxlen = sizeof(int), 31471da177e4SLinus Torvalds .mode = 0644, 31486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31491da177e4SLinus Torvalds }, 31501da177e4SLinus Torvalds { 31511da177e4SLinus Torvalds .procname = "redirect_number", 31521da177e4SLinus Torvalds .data = &ip_rt_redirect_number, 31531da177e4SLinus Torvalds .maxlen = sizeof(int), 31541da177e4SLinus Torvalds .mode = 0644, 31556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31561da177e4SLinus Torvalds }, 31571da177e4SLinus Torvalds { 31581da177e4SLinus Torvalds .procname = "redirect_silence", 31591da177e4SLinus Torvalds .data = &ip_rt_redirect_silence, 31601da177e4SLinus Torvalds .maxlen = sizeof(int), 31611da177e4SLinus Torvalds .mode = 0644, 31626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31631da177e4SLinus Torvalds }, 31641da177e4SLinus Torvalds { 31651da177e4SLinus Torvalds .procname = "error_cost", 31661da177e4SLinus Torvalds .data = &ip_rt_error_cost, 31671da177e4SLinus Torvalds .maxlen = sizeof(int), 31681da177e4SLinus Torvalds .mode = 0644, 31696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31701da177e4SLinus Torvalds }, 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds .procname = "error_burst", 31731da177e4SLinus Torvalds .data = &ip_rt_error_burst, 31741da177e4SLinus Torvalds .maxlen = sizeof(int), 31751da177e4SLinus Torvalds .mode = 0644, 31766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31771da177e4SLinus Torvalds }, 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds .procname = "gc_elasticity", 31801da177e4SLinus Torvalds .data = &ip_rt_gc_elasticity, 31811da177e4SLinus Torvalds .maxlen = sizeof(int), 31821da177e4SLinus Torvalds .mode = 0644, 31836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31841da177e4SLinus Torvalds }, 31851da177e4SLinus Torvalds { 31861da177e4SLinus Torvalds .procname = "mtu_expires", 31871da177e4SLinus Torvalds .data = &ip_rt_mtu_expires, 31881da177e4SLinus Torvalds .maxlen = sizeof(int), 31891da177e4SLinus Torvalds .mode = 0644, 31906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 31911da177e4SLinus Torvalds }, 31921da177e4SLinus Torvalds { 31931da177e4SLinus Torvalds .procname = "min_pmtu", 31941da177e4SLinus Torvalds .data = &ip_rt_min_pmtu, 31951da177e4SLinus Torvalds .maxlen = sizeof(int), 31961da177e4SLinus Torvalds .mode = 0644, 31976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31981da177e4SLinus Torvalds }, 31991da177e4SLinus Torvalds { 32001da177e4SLinus Torvalds .procname = "min_adv_mss", 32011da177e4SLinus Torvalds .data = &ip_rt_min_advmss, 32021da177e4SLinus Torvalds .maxlen = sizeof(int), 32031da177e4SLinus Torvalds .mode = 0644, 32046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 32051da177e4SLinus Torvalds }, 3206f8572d8fSEric W. Biederman { } 32071da177e4SLinus Torvalds }; 320839a23e75SDenis V. Lunev 32092f4520d3SAl Viro static struct ctl_table empty[1]; 32102f4520d3SAl Viro 32112f4520d3SAl Viro static struct ctl_table ipv4_skeleton[] = 32122f4520d3SAl Viro { 3213f8572d8fSEric W. Biederman { .procname = "route", 3214d994af0dSHugh Dickins .mode = 0555, .child = ipv4_route_table}, 3215f8572d8fSEric W. Biederman { .procname = "neigh", 3216d994af0dSHugh Dickins .mode = 0555, .child = empty}, 32172f4520d3SAl Viro { } 321839a23e75SDenis V. Lunev }; 321939a23e75SDenis V. Lunev 32202f4520d3SAl Viro static __net_initdata struct ctl_path ipv4_path[] = { 3221f8572d8fSEric W. Biederman { .procname = "net", }, 3222f8572d8fSEric W. Biederman { .procname = "ipv4", }, 32232f4520d3SAl Viro { }, 32242f4520d3SAl Viro }; 322539a23e75SDenis V. Lunev 322639a23e75SDenis V. Lunev static struct ctl_table ipv4_route_flush_table[] = { 322739a23e75SDenis V. Lunev { 322839a23e75SDenis V. Lunev .procname = "flush", 322939a23e75SDenis V. Lunev .maxlen = sizeof(int), 323039a23e75SDenis V. Lunev .mode = 0200, 32316d9f239aSAlexey Dobriyan .proc_handler = ipv4_sysctl_rtcache_flush, 323239a23e75SDenis V. Lunev }, 3233f8572d8fSEric W. Biederman { }, 323439a23e75SDenis V. Lunev }; 323539a23e75SDenis V. Lunev 32362f4520d3SAl Viro static __net_initdata struct ctl_path ipv4_route_path[] = { 3237f8572d8fSEric W. Biederman { .procname = "net", }, 3238f8572d8fSEric W. Biederman { .procname = "ipv4", }, 3239f8572d8fSEric W. Biederman { .procname = "route", }, 32402f4520d3SAl Viro { }, 32412f4520d3SAl Viro }; 32422f4520d3SAl Viro 324339a23e75SDenis V. Lunev static __net_init int sysctl_route_net_init(struct net *net) 324439a23e75SDenis V. Lunev { 324539a23e75SDenis V. Lunev struct ctl_table *tbl; 324639a23e75SDenis V. Lunev 324739a23e75SDenis V. Lunev tbl = ipv4_route_flush_table; 324809ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 324939a23e75SDenis V. Lunev tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL); 325039a23e75SDenis V. Lunev if (tbl == NULL) 325139a23e75SDenis V. Lunev goto err_dup; 325239a23e75SDenis V. Lunev } 325339a23e75SDenis V. Lunev tbl[0].extra1 = net; 325439a23e75SDenis V. Lunev 325539a23e75SDenis V. Lunev net->ipv4.route_hdr = 325639a23e75SDenis V. Lunev register_net_sysctl_table(net, ipv4_route_path, tbl); 325739a23e75SDenis V. Lunev if (net->ipv4.route_hdr == NULL) 325839a23e75SDenis V. Lunev goto err_reg; 325939a23e75SDenis V. Lunev return 0; 326039a23e75SDenis V. Lunev 326139a23e75SDenis V. Lunev err_reg: 326239a23e75SDenis V. Lunev if (tbl != ipv4_route_flush_table) 326339a23e75SDenis V. Lunev kfree(tbl); 326439a23e75SDenis V. Lunev err_dup: 326539a23e75SDenis V. Lunev return -ENOMEM; 326639a23e75SDenis V. Lunev } 326739a23e75SDenis V. Lunev 326839a23e75SDenis V. Lunev static __net_exit void sysctl_route_net_exit(struct net *net) 326939a23e75SDenis V. Lunev { 327039a23e75SDenis V. Lunev struct ctl_table *tbl; 327139a23e75SDenis V. Lunev 327239a23e75SDenis V. Lunev tbl = net->ipv4.route_hdr->ctl_table_arg; 327339a23e75SDenis V. Lunev unregister_net_sysctl_table(net->ipv4.route_hdr); 327439a23e75SDenis V. Lunev BUG_ON(tbl == ipv4_route_flush_table); 327539a23e75SDenis V. Lunev kfree(tbl); 327639a23e75SDenis V. Lunev } 327739a23e75SDenis V. Lunev 327839a23e75SDenis V. Lunev static __net_initdata struct pernet_operations sysctl_route_ops = { 327939a23e75SDenis V. Lunev .init = sysctl_route_net_init, 328039a23e75SDenis V. Lunev .exit = sysctl_route_net_exit, 328139a23e75SDenis V. Lunev }; 32821da177e4SLinus Torvalds #endif 32831da177e4SLinus Torvalds 32843ee94372SNeil Horman static __net_init int rt_genid_init(struct net *net) 32859f5e97e5SDenis V. Lunev { 32863ee94372SNeil Horman get_random_bytes(&net->ipv4.rt_genid, 32873ee94372SNeil Horman sizeof(net->ipv4.rt_genid)); 32889f5e97e5SDenis V. Lunev return 0; 32899f5e97e5SDenis V. Lunev } 32909f5e97e5SDenis V. Lunev 32913ee94372SNeil Horman static __net_initdata struct pernet_operations rt_genid_ops = { 32923ee94372SNeil Horman .init = rt_genid_init, 32939f5e97e5SDenis V. Lunev }; 32949f5e97e5SDenis V. Lunev 32959f5e97e5SDenis V. Lunev 32961da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 32977d720c3eSTejun Heo struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; 32981da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_ROUTE */ 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds static __initdata unsigned long rhash_entries; 33011da177e4SLinus Torvalds static int __init set_rhash_entries(char *str) 33021da177e4SLinus Torvalds { 33031da177e4SLinus Torvalds if (!str) 33041da177e4SLinus Torvalds return 0; 33051da177e4SLinus Torvalds rhash_entries = simple_strtoul(str, &str, 0); 33061da177e4SLinus Torvalds return 1; 33071da177e4SLinus Torvalds } 33081da177e4SLinus Torvalds __setup("rhash_entries=", set_rhash_entries); 33091da177e4SLinus Torvalds 33101da177e4SLinus Torvalds int __init ip_rt_init(void) 33111da177e4SLinus Torvalds { 3312424c4b70SEric Dumazet int rc = 0; 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 33150dcec8c2SIngo Molnar ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct)); 33161da177e4SLinus Torvalds if (!ip_rt_acct) 33171da177e4SLinus Torvalds panic("IP: failed to allocate ip_rt_acct\n"); 33181da177e4SLinus Torvalds #endif 33191da177e4SLinus Torvalds 3320e5d679f3SAlexey Dobriyan ipv4_dst_ops.kmem_cachep = 3321e5d679f3SAlexey Dobriyan kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0, 332220c2df83SPaul Mundt SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 33231da177e4SLinus Torvalds 332414e50e57SDavid S. Miller ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep; 332514e50e57SDavid S. Miller 33261da177e4SLinus Torvalds rt_hash_table = (struct rt_hash_bucket *) 3327424c4b70SEric Dumazet alloc_large_system_hash("IP route cache", 3328424c4b70SEric Dumazet sizeof(struct rt_hash_bucket), 3329424c4b70SEric Dumazet rhash_entries, 33304481374cSJan Beulich (totalram_pages >= 128 * 1024) ? 333118955cfcSMike Stroyan 15 : 17, 33328d1502deSKirill Korotaev 0, 3333424c4b70SEric Dumazet &rt_hash_log, 3334424c4b70SEric Dumazet &rt_hash_mask, 3335c9503e0fSAnton Blanchard rhash_entries ? 0 : 512 * 1024); 333622c047ccSEric Dumazet memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket)); 333722c047ccSEric Dumazet rt_hash_lock_init(); 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1); 33401da177e4SLinus Torvalds ip_rt_max_size = (rt_hash_mask + 1) * 16; 33411da177e4SLinus Torvalds 33421da177e4SLinus Torvalds devinet_init(); 33431da177e4SLinus Torvalds ip_fib_init(); 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds /* All the timers, started at system startup tend 33461da177e4SLinus Torvalds to synchronize. Perturb it a bit. 33471da177e4SLinus Torvalds */ 3348125bb8f5SEric Dumazet INIT_DELAYED_WORK_DEFERRABLE(&expires_work, rt_worker_func); 3349125bb8f5SEric Dumazet expires_ljiffies = jiffies; 335039c90eceSEric Dumazet schedule_delayed_work(&expires_work, 335139c90eceSEric Dumazet net_random() % ip_rt_gc_interval + ip_rt_gc_interval); 33521da177e4SLinus Torvalds 335373b38711SDenis V. Lunev if (ip_rt_proc_init()) 3354107f1634SPavel Emelyanov printk(KERN_ERR "Unable to create route proc files\n"); 33551da177e4SLinus Torvalds #ifdef CONFIG_XFRM 33561da177e4SLinus Torvalds xfrm_init(); 3357a33bc5c1SNeil Horman xfrm4_init(ip_rt_max_size); 33581da177e4SLinus Torvalds #endif 335963f3444fSThomas Graf rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL); 336063f3444fSThomas Graf 336139a23e75SDenis V. Lunev #ifdef CONFIG_SYSCTL 336239a23e75SDenis V. Lunev register_pernet_subsys(&sysctl_route_ops); 336339a23e75SDenis V. Lunev #endif 33643ee94372SNeil Horman register_pernet_subsys(&rt_genid_ops); 33651da177e4SLinus Torvalds return rc; 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 3368a1bc6eb4SAl Viro #ifdef CONFIG_SYSCTL 3369eeb61f71SAl Viro /* 3370eeb61f71SAl Viro * We really need to sanitize the damn ipv4 init order, then all 3371eeb61f71SAl Viro * this nonsense will go away. 3372eeb61f71SAl Viro */ 3373eeb61f71SAl Viro void __init ip_static_sysctl_init(void) 3374eeb61f71SAl Viro { 33752f4520d3SAl Viro register_sysctl_paths(ipv4_path, ipv4_skeleton); 3376eeb61f71SAl Viro } 3377a1bc6eb4SAl Viro #endif 3378