route.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * ROUTE - implementation of the IP router.
  7. *
  8. * Version: $Id: route.c,v 1.103 2002/01/12 07:44:09 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  13. * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
  14. * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  15. *
  16. * Fixes:
  17. * Alan Cox : Verify area fixes.
  18. * Alan Cox : cli() protects routing changes
  19. * Rui Oliveira : ICMP routing table updates
  20. * (rco@di.uminho.pt) Routing table insertion and update
  21. * Linus Torvalds : Rewrote bits to be sensible
  22. * Alan Cox : Added BSD route gw semantics
  23. * Alan Cox : Super /proc >4K
  24. * Alan Cox : MTU in route table
  25. * Alan Cox : MSS actually. Also added the window
  26. * clamper.
  27. * Sam Lantinga : Fixed route matching in rt_del()
  28. * Alan Cox : Routing cache support.
  29. * Alan Cox : Removed compatibility cruft.
  30. * Alan Cox : RTF_REJECT support.
  31. * Alan Cox : TCP irtt support.
  32. * Jonathan Naylor : Added Metric support.
  33. * Miquel van Smoorenburg : BSD API fixes.
  34. * Miquel van Smoorenburg : Metrics.
  35. * Alan Cox : Use __u32 properly
  36. * Alan Cox : Aligned routing errors more closely with BSD
  37. * our system is still very different.
  38. * Alan Cox : Faster /proc handling
  39. * Alexey Kuznetsov : Massive rework to support tree based routing,
  40. * routing caches and better behaviour.
  41. *
  42. * Olaf Erb : irtt wasn't being copied right.
  43. * Bjorn Ekwall : Kerneld route support.
  44. * Alan Cox : Multicast fixed (I hope)
  45. * Pavel Krauz : Limited broadcast fixed
  46. * Mike McLagan : Routing by source
  47. * Alexey Kuznetsov : End of old history. Split to fib.c and
  48. * route.c and rewritten from scratch.
  49. * Andi Kleen : Load-limit warning messages.
  50. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  51. * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
  52. * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
  53. * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
  54. * Marc Boucher : routing by fwmark
  55. * Robert Olsson : Added rt_cache statistics
  56. * Arnaldo C. Melo : Convert proc stuff to seq_file
  57. * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
  58. * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
  59. * Ilia Sotnikov : Removed TOS from hash calculations
  60. *
  61. * This program is free software; you can redistribute it and/or
  62. * modify it under the terms of the GNU General Public License
  63. * as published by the Free Software Foundation; either version
  64. * 2 of the License, or (at your option) any later version.
  65. */
  66. #include <linux/module.h>
  67. #include <asm/uaccess.h>
  68. #include <asm/system.h>
  69. #include <linux/bitops.h>
  70. #include <linux/types.h>
  71. #include <linux/kernel.h>
  72. #include <linux/sched.h>
  73. #include <linux/mm.h>
  74. #include <linux/bootmem.h>
  75. #include <linux/string.h>
  76. #include <linux/socket.h>
  77. #include <linux/sockios.h>
  78. #include <linux/errno.h>
  79. #include <linux/in.h>
  80. #include <linux/inet.h>
  81. #include <linux/netdevice.h>
  82. #include <linux/proc_fs.h>
  83. #include <linux/init.h>
  84. #include <linux/skbuff.h>
  85. #include <linux/rtnetlink.h>
  86. #include <linux/inetdevice.h>
  87. #include <linux/igmp.h>
  88. #include <linux/pkt_sched.h>
  89. #include <linux/mroute.h>
  90. #include <linux/netfilter_ipv4.h>
  91. #include <linux/random.h>
  92. #include <linux/jhash.h>
  93. #include <linux/rcupdate.h>
  94. #include <linux/times.h>
  95. #include <net/protocol.h>
  96. #include <net/ip.h>
  97. #include <net/route.h>
  98. #include <net/inetpeer.h>
  99. #include <net/sock.h>
  100. #include <net/ip_fib.h>
  101. #include <net/arp.h>
  102. #include <net/tcp.h>
  103. #include <net/icmp.h>
  104. #include <net/xfrm.h>
  105. #include <net/ip_mp_alg.h>
  106. #include <net/netevent.h>
  107. #ifdef CONFIG_SYSCTL
  108. #include <linux/sysctl.h>
  109. #endif
  110. #define RT_FL_TOS(oldflp) \
  111. ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
  112. #define IP_MAX_MTU 0xFFF0
  113. #define RT_GC_TIMEOUT (300*HZ)
  114. static int ip_rt_min_delay = 2 * HZ;
  115. static int ip_rt_max_delay = 10 * HZ;
  116. static int ip_rt_max_size;
  117. static int ip_rt_gc_timeout = RT_GC_TIMEOUT;
  118. static int ip_rt_gc_interval = 60 * HZ;
  119. static int ip_rt_gc_min_interval = HZ / 2;
  120. static int ip_rt_redirect_number = 9;
  121. static int ip_rt_redirect_load = HZ / 50;
  122. static int ip_rt_redirect_silence = ((HZ / 50) << (9 + 1));
  123. static int ip_rt_error_cost = HZ;
  124. static int ip_rt_error_burst = 5 * HZ;
  125. static int ip_rt_gc_elasticity = 8;
  126. static int ip_rt_mtu_expires = 10 * 60 * HZ;
  127. static int ip_rt_min_pmtu = 512 + 20 + 20;
  128. static int ip_rt_min_advmss = 256;
  129. static int ip_rt_secret_interval = 10 * 60 * HZ;
  130. static unsigned long rt_deadline;
  131. #define RTprint(a...) printk(KERN_DEBUG a)
  132. static struct timer_list rt_flush_timer;
  133. static struct timer_list rt_periodic_timer;
  134. static struct timer_list rt_secret_timer;
  135. /*
  136. * Interface to generic destination cache.
  137. */
  138. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
  139. static void ipv4_dst_destroy(struct dst_entry *dst);
  140. static void ipv4_dst_ifdown(struct dst_entry *dst,
  141. struct net_device *dev, int how);
  142. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
  143. static void ipv4_link_failure(struct sk_buff *skb);
  144. static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
  145. static int rt_garbage_collect(void);
  146. static struct dst_ops ipv4_dst_ops = {
  147. .family = AF_INET,
  148. .protocol = __constant_htons(ETH_P_IP),
  149. .gc = rt_garbage_collect,
  150. .check = ipv4_dst_check,
  151. .destroy = ipv4_dst_destroy,
  152. .ifdown = ipv4_dst_ifdown,
  153. .negative_advice = ipv4_negative_advice,
  154. .link_failure = ipv4_link_failure,
  155. .update_pmtu = ip_rt_update_pmtu,
  156. .entry_size = sizeof(struct rtable),
  157. };
  158. #define ECN_OR_COST(class) TC_PRIO_##class
  159. __u8 ip_tos2prio[16] = {
  160. TC_PRIO_BESTEFFORT,
  161. ECN_OR_COST(FILLER),
  162. TC_PRIO_BESTEFFORT,
  163. ECN_OR_COST(BESTEFFORT),
  164. TC_PRIO_BULK,
  165. ECN_OR_COST(BULK),
  166. TC_PRIO_BULK,
  167. ECN_OR_COST(BULK),
  168. TC_PRIO_INTERACTIVE,
  169. ECN_OR_COST(INTERACTIVE),
  170. TC_PRIO_INTERACTIVE,
  171. ECN_OR_COST(INTERACTIVE),
  172. TC_PRIO_INTERACTIVE_BULK,
  173. ECN_OR_COST(INTERACTIVE_BULK),
  174. TC_PRIO_INTERACTIVE_BULK,
  175. ECN_OR_COST(INTERACTIVE_BULK)
  176. };
  177. /*
  178. * Route cache.
  179. */
  180. /* The locking scheme is rather straight forward:
  181. *
  182. * 1) Read-Copy Update protects the buckets of the central route hash.
  183. * 2) Only writers remove entries, and they hold the lock
  184. * as they look at rtable reference counts.
  185. * 3) Only readers acquire references to rtable entries,
  186. * they do so with atomic increments and with the
  187. * lock held.
  188. */
  189. struct rt_hash_bucket {
  190. struct rtable *chain;
  191. };
  192. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
  193. defined(CONFIG_PROVE_LOCKING)
  194. /*
  195. * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
  196. * The size of this table is a power of two and depends on the number of CPUS.
  197. * (on lockdep we have a quite big spinlock_t, so keep the size down there)
  198. */
  199. #ifdef CONFIG_LOCKDEP
  200. # define RT_HASH_LOCK_SZ 256
  201. #else
  202. # if NR_CPUS >= 32
  203. # define RT_HASH_LOCK_SZ 4096
  204. # elif NR_CPUS >= 16
  205. # define RT_HASH_LOCK_SZ 2048
  206. # elif NR_CPUS >= 8
  207. # define RT_HASH_LOCK_SZ 1024
  208. # elif NR_CPUS >= 4
  209. # define RT_HASH_LOCK_SZ 512
  210. # else
  211. # define RT_HASH_LOCK_SZ 256
  212. # endif
  213. #endif
  214. static spinlock_t *rt_hash_locks;
  215. # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
  216. # define rt_hash_lock_init() { \
  217. int i; \
  218. rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, GFP_KERNEL); \
  219. if (!rt_hash_locks) panic("IP: failed to allocate rt_hash_locks\n"); \
  220. for (i = 0; i < RT_HASH_LOCK_SZ; i++) \
  221. spin_lock_init(&rt_hash_locks[i]); \
  222. }
  223. #else
  224. # define rt_hash_lock_addr(slot) NULL
  225. # define rt_hash_lock_init()
  226. #endif
  227. static struct rt_hash_bucket *rt_hash_table;
  228. static unsigned rt_hash_mask;
  229. static int rt_hash_log;
  230. static unsigned int rt_hash_rnd;
  231. static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
  232. #define RT_CACHE_STAT_INC(field) \
  233. (__raw_get_cpu_var(rt_cache_stat).field++)
  234. static int rt_intern_hash(unsigned hash, struct rtable *rth,
  235. struct rtable **res);
  236. static unsigned int rt_hash_code(u32 daddr, u32 saddr)
  237. {
  238. return (jhash_2words(daddr, saddr, rt_hash_rnd)
  239. & rt_hash_mask);
  240. }
  241. #define rt_hash(daddr, saddr, idx) \
  242. rt_hash_code((__force u32)(__be32)(daddr),\
  243. (__force u32)(__be32)(saddr) ^ ((idx) << 5))
  244. #ifdef CONFIG_PROC_FS
  245. struct rt_cache_iter_state {
  246. int bucket;
  247. };
  248. static struct rtable *rt_cache_get_first(struct seq_file *seq)
  249. {
  250. struct rtable *r = NULL;
  251. struct rt_cache_iter_state *st = seq->private;
  252. for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
  253. rcu_read_lock_bh();
  254. r = rt_hash_table[st->bucket].chain;
  255. if (r)
  256. break;
  257. rcu_read_unlock_bh();
  258. }
  259. return r;
  260. }
  261. static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
  262. {
  263. struct rt_cache_iter_state *st = rcu_dereference(seq->private);
  264. r = r->u.rt_next;
  265. while (!r) {
  266. rcu_read_unlock_bh();
  267. if (--st->bucket < 0)
  268. break;
  269. rcu_read_lock_bh();
  270. r = rt_hash_table[st->bucket].chain;
  271. }
  272. return r;
  273. }
  274. static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
  275. {
  276. struct rtable *r = rt_cache_get_first(seq);
  277. if (r)
  278. while (pos && (r = rt_cache_get_next(seq, r)))
  279. --pos;
  280. return pos ? NULL : r;
  281. }
  282. static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  283. {
  284. return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  285. }
  286. static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  287. {
  288. struct rtable *r = NULL;
  289. if (v == SEQ_START_TOKEN)
  290. r = rt_cache_get_first(seq);
  291. else
  292. r = rt_cache_get_next(seq, v);
  293. ++*pos;
  294. return r;
  295. }
  296. static void rt_cache_seq_stop(struct seq_file *seq, void *v)
  297. {
  298. if (v && v != SEQ_START_TOKEN)
  299. rcu_read_unlock_bh();
  300. }
  301. static int rt_cache_seq_show(struct seq_file *seq, void *v)
  302. {
  303. if (v == SEQ_START_TOKEN)
  304. seq_printf(seq, "%-127s\n",
  305. "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
  306. "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
  307. "HHUptod\tSpecDst");
  308. else {
  309. struct rtable *r = v;
  310. char temp[256];
  311. sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
  312. "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
  313. r->u.dst.dev ? r->u.dst.dev->name : "*",
  314. (unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
  315. r->rt_flags, atomic_read(&r->u.dst.__refcnt),
  316. r->u.dst.__use, 0, (unsigned long)r->rt_src,
  317. (dst_metric(&r->u.dst, RTAX_ADVMSS) ?
  318. (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
  319. dst_metric(&r->u.dst, RTAX_WINDOW),
  320. (int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
  321. dst_metric(&r->u.dst, RTAX_RTTVAR)),
  322. r->fl.fl4_tos,
  323. r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
  324. r->u.dst.hh ? (r->u.dst.hh->hh_output ==
  325. dev_queue_xmit) : 0,
  326. r->rt_spec_dst);
  327. seq_printf(seq, "%-127s\n", temp);
  328. }
  329. return 0;
  330. }
  331. static struct seq_operations rt_cache_seq_ops = {
  332. .start = rt_cache_seq_start,
  333. .next = rt_cache_seq_next,
  334. .stop = rt_cache_seq_stop,
  335. .show = rt_cache_seq_show,
  336. };
  337. static int rt_cache_seq_open(struct inode *inode, struct file *file)
  338. {
  339. struct seq_file *seq;
  340. int rc = -ENOMEM;
  341. struct rt_cache_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  342. if (!s)
  343. goto out;
  344. rc = seq_open(file, &rt_cache_seq_ops);
  345. if (rc)
  346. goto out_kfree;
  347. seq = file->private_data;
  348. seq->private = s;
  349. memset(s, 0, sizeof(*s));
  350. out:
  351. return rc;
  352. out_kfree:
  353. kfree(s);
  354. goto out;
  355. }
  356. static struct file_operations rt_cache_seq_fops = {
  357. .owner = THIS_MODULE,
  358. .open = rt_cache_seq_open,
  359. .read = seq_read,
  360. .llseek = seq_lseek,
  361. .release = seq_release_private,
  362. };
  363. static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
  364. {
  365. int cpu;
  366. if (*pos == 0)
  367. return SEQ_START_TOKEN;
  368. for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
  369. if (!cpu_possible(cpu))
  370. continue;
  371. *pos = cpu+1;
  372. return &per_cpu(rt_cache_stat, cpu);
  373. }
  374. return NULL;
  375. }
  376. static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  377. {
  378. int cpu;
  379. for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
  380. if (!cpu_possible(cpu))
  381. continue;
  382. *pos = cpu+1;
  383. return &per_cpu(rt_cache_stat, cpu);
  384. }
  385. return NULL;
  386. }
  387. static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
  388. {
  389. }
  390. static int rt_cpu_seq_show(struct seq_file *seq, void *v)
  391. {
  392. struct rt_cache_stat *st = v;
  393. if (v == SEQ_START_TOKEN) {
  394. 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");
  395. return 0;
  396. }
  397. seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
  398. " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
  399. atomic_read(&ipv4_dst_ops.entries),
  400. st->in_hit,
  401. st->in_slow_tot,
  402. st->in_slow_mc,
  403. st->in_no_route,
  404. st->in_brd,
  405. st->in_martian_dst,
  406. st->in_martian_src,
  407. st->out_hit,
  408. st->out_slow_tot,
  409. st->out_slow_mc,
  410. st->gc_total,
  411. st->gc_ignored,
  412. st->gc_goal_miss,
  413. st->gc_dst_overflow,
  414. st->in_hlist_search,
  415. st->out_hlist_search
  416. );
  417. return 0;
  418. }
  419. static struct seq_operations rt_cpu_seq_ops = {
  420. .start = rt_cpu_seq_start,
  421. .next = rt_cpu_seq_next,
  422. .stop = rt_cpu_seq_stop,
  423. .show = rt_cpu_seq_show,
  424. };
  425. static int rt_cpu_seq_open(struct inode *inode, struct file *file)
  426. {
  427. return seq_open(file, &rt_cpu_seq_ops);
  428. }
  429. static struct file_operations rt_cpu_seq_fops = {
  430. .owner = THIS_MODULE,
  431. .open = rt_cpu_seq_open,
  432. .read = seq_read,
  433. .llseek = seq_lseek,
  434. .release = seq_release,
  435. };
  436. #endif /* CONFIG_PROC_FS */
  437. static __inline__ void rt_free(struct rtable *rt)
  438. {
  439. multipath_remove(rt);
  440. call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
  441. }
  442. static __inline__ void rt_drop(struct rtable *rt)
  443. {
  444. multipath_remove(rt);
  445. ip_rt_put(rt);
  446. call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
  447. }
  448. static __inline__ int rt_fast_clean(struct rtable *rth)
  449. {
  450. /* Kill broadcast/multicast entries very aggresively, if they
  451. collide in hash table with more useful entries */
  452. return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
  453. rth->fl.iif && rth->u.rt_next;
  454. }
  455. static __inline__ int rt_valuable(struct rtable *rth)
  456. {
  457. return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
  458. rth->u.dst.expires;
  459. }
  460. static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
  461. {
  462. unsigned long age;
  463. int ret = 0;
  464. if (atomic_read(&rth->u.dst.__refcnt))
  465. goto out;
  466. ret = 1;
  467. if (rth->u.dst.expires &&
  468. time_after_eq(jiffies, rth->u.dst.expires))
  469. goto out;
  470. age = jiffies - rth->u.dst.lastuse;
  471. ret = 0;
  472. if ((age <= tmo1 && !rt_fast_clean(rth)) ||
  473. (age <= tmo2 && rt_valuable(rth)))
  474. goto out;
  475. ret = 1;
  476. out: return ret;
  477. }
  478. /* Bits of score are:
  479. * 31: very valuable
  480. * 30: not quite useless
  481. * 29..0: usage counter
  482. */
  483. static inline u32 rt_score(struct rtable *rt)
  484. {
  485. u32 score = jiffies - rt->u.dst.lastuse;
  486. score = ~score & ~(3<<30);
  487. if (rt_valuable(rt))
  488. score |= (1<<31);
  489. if (!rt->fl.iif ||
  490. !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
  491. score |= (1<<30);
  492. return score;
  493. }
  494. static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
  495. {
  496. return ((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
  497. (fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr) |
  498. #ifdef CONFIG_IP_ROUTE_FWMARK
  499. (fl1->nl_u.ip4_u.fwmark ^ fl2->nl_u.ip4_u.fwmark) |
  500. #endif
  501. (*(u16 *)&fl1->nl_u.ip4_u.tos ^
  502. *(u16 *)&fl2->nl_u.ip4_u.tos) |
  503. (fl1->oif ^ fl2->oif) |
  504. (fl1->iif ^ fl2->iif)) == 0;
  505. }
  506. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  507. static struct rtable **rt_remove_balanced_route(struct rtable **chain_head,
  508. struct rtable *expentry,
  509. int *removed_count)
  510. {
  511. int passedexpired = 0;
  512. struct rtable **nextstep = NULL;
  513. struct rtable **rthp = chain_head;
  514. struct rtable *rth;
  515. if (removed_count)
  516. *removed_count = 0;
  517. while ((rth = *rthp) != NULL) {
  518. if (rth == expentry)
  519. passedexpired = 1;
  520. if (((*rthp)->u.dst.flags & DST_BALANCED) != 0 &&
  521. compare_keys(&(*rthp)->fl, &expentry->fl)) {
  522. if (*rthp == expentry) {
  523. *rthp = rth->u.rt_next;
  524. continue;
  525. } else {
  526. *rthp = rth->u.rt_next;
  527. rt_free(rth);
  528. if (removed_count)
  529. ++(*removed_count);
  530. }
  531. } else {
  532. if (!((*rthp)->u.dst.flags & DST_BALANCED) &&
  533. passedexpired && !nextstep)
  534. nextstep = &rth->u.rt_next;
  535. rthp = &rth->u.rt_next;
  536. }
  537. }
  538. rt_free(expentry);
  539. if (removed_count)
  540. ++(*removed_count);
  541. return nextstep;
  542. }
  543. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  544. /* This runs via a timer and thus is always in BH context. */
  545. static void rt_check_expire(unsigned long dummy)
  546. {
  547. static unsigned int rover;
  548. unsigned int i = rover, goal;
  549. struct rtable *rth, **rthp;
  550. unsigned long now = jiffies;
  551. u64 mult;
  552. mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
  553. if (ip_rt_gc_timeout > 1)
  554. do_div(mult, ip_rt_gc_timeout);
  555. goal = (unsigned int)mult;
  556. if (goal > rt_hash_mask) goal = rt_hash_mask + 1;
  557. for (; goal > 0; goal--) {
  558. unsigned long tmo = ip_rt_gc_timeout;
  559. i = (i + 1) & rt_hash_mask;
  560. rthp = &rt_hash_table[i].chain;
  561. if (*rthp == 0)
  562. continue;
  563. spin_lock(rt_hash_lock_addr(i));
  564. while ((rth = *rthp) != NULL) {
  565. if (rth->u.dst.expires) {
  566. /* Entry is expired even if it is in use */
  567. if (time_before_eq(now, rth->u.dst.expires)) {
  568. tmo >>= 1;
  569. rthp = &rth->u.rt_next;
  570. continue;
  571. }
  572. } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
  573. tmo >>= 1;
  574. rthp = &rth->u.rt_next;
  575. continue;
  576. }
  577. /* Cleanup aged off entries. */
  578. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  579. /* remove all related balanced entries if necessary */
  580. if (rth->u.dst.flags & DST_BALANCED) {
  581. rthp = rt_remove_balanced_route(
  582. &rt_hash_table[i].chain,
  583. rth, NULL);
  584. if (!rthp)
  585. break;
  586. } else {
  587. *rthp = rth->u.rt_next;
  588. rt_free(rth);
  589. }
  590. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  591. *rthp = rth->u.rt_next;
  592. rt_free(rth);
  593. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  594. }
  595. spin_unlock(rt_hash_lock_addr(i));
  596. /* Fallback loop breaker. */
  597. if (time_after(jiffies, now))
  598. break;
  599. }
  600. rover = i;
  601. mod_timer(&rt_periodic_timer, jiffies + ip_rt_gc_interval);
  602. }
  603. /* This can run from both BH and non-BH contexts, the latter
  604. * in the case of a forced flush event.
  605. */
  606. static void rt_run_flush(unsigned long dummy)
  607. {
  608. int i;
  609. struct rtable *rth, *next;
  610. rt_deadline = 0;
  611. get_random_bytes(&rt_hash_rnd, 4);
  612. for (i = rt_hash_mask; i >= 0; i--) {
  613. spin_lock_bh(rt_hash_lock_addr(i));
  614. rth = rt_hash_table[i].chain;
  615. if (rth)
  616. rt_hash_table[i].chain = NULL;
  617. spin_unlock_bh(rt_hash_lock_addr(i));
  618. for (; rth; rth = next) {
  619. next = rth->u.rt_next;
  620. rt_free(rth);
  621. }
  622. }
  623. }
  624. static DEFINE_SPINLOCK(rt_flush_lock);
  625. void rt_cache_flush(int delay)
  626. {
  627. unsigned long now = jiffies;
  628. int user_mode = !in_softirq();
  629. if (delay < 0)
  630. delay = ip_rt_min_delay;
  631. /* flush existing multipath state*/
  632. multipath_flush();
  633. spin_lock_bh(&rt_flush_lock);
  634. if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
  635. long tmo = (long)(rt_deadline - now);
  636. /* If flush timer is already running
  637. and flush request is not immediate (delay > 0):
  638. if deadline is not achieved, prolongate timer to "delay",
  639. otherwise fire it at deadline time.
  640. */
  641. if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
  642. tmo = 0;
  643. if (delay > tmo)
  644. delay = tmo;
  645. }
  646. if (delay <= 0) {
  647. spin_unlock_bh(&rt_flush_lock);
  648. rt_run_flush(0);
  649. return;
  650. }
  651. if (rt_deadline == 0)
  652. rt_deadline = now + ip_rt_max_delay;
  653. mod_timer(&rt_flush_timer, now+delay);
  654. spin_unlock_bh(&rt_flush_lock);
  655. }
  656. static void rt_secret_rebuild(unsigned long dummy)
  657. {
  658. unsigned long now = jiffies;
  659. rt_cache_flush(0);
  660. mod_timer(&rt_secret_timer, now + ip_rt_secret_interval);
  661. }
  662. /*
  663. Short description of GC goals.
  664. We want to build algorithm, which will keep routing cache
  665. at some equilibrium point, when number of aged off entries
  666. is kept approximately equal to newly generated ones.
  667. Current expiration strength is variable "expire".
  668. We try to adjust it dynamically, so that if networking
  669. is idle expires is large enough to keep enough of warm entries,
  670. and when load increases it reduces to limit cache size.
  671. */
  672. static int rt_garbage_collect(void)
  673. {
  674. static unsigned long expire = RT_GC_TIMEOUT;
  675. static unsigned long last_gc;
  676. static int rover;
  677. static int equilibrium;
  678. struct rtable *rth, **rthp;
  679. unsigned long now = jiffies;
  680. int goal;
  681. /*
  682. * Garbage collection is pretty expensive,
  683. * do not make it too frequently.
  684. */
  685. RT_CACHE_STAT_INC(gc_total);
  686. if (now - last_gc < ip_rt_gc_min_interval &&
  687. atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
  688. RT_CACHE_STAT_INC(gc_ignored);
  689. goto out;
  690. }
  691. /* Calculate number of entries, which we want to expire now. */
  692. goal = atomic_read(&ipv4_dst_ops.entries) -
  693. (ip_rt_gc_elasticity << rt_hash_log);
  694. if (goal <= 0) {
  695. if (equilibrium < ipv4_dst_ops.gc_thresh)
  696. equilibrium = ipv4_dst_ops.gc_thresh;
  697. goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
  698. if (goal > 0) {
  699. equilibrium += min_t(unsigned int, goal / 2, rt_hash_mask + 1);
  700. goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
  701. }
  702. } else {
  703. /* We are in dangerous area. Try to reduce cache really
  704. * aggressively.
  705. */
  706. goal = max_t(unsigned int, goal / 2, rt_hash_mask + 1);
  707. equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
  708. }
  709. if (now - last_gc >= ip_rt_gc_min_interval)
  710. last_gc = now;
  711. if (goal <= 0) {
  712. equilibrium += goal;
  713. goto work_done;
  714. }
  715. do {
  716. int i, k;
  717. for (i = rt_hash_mask, k = rover; i >= 0; i--) {
  718. unsigned long tmo = expire;
  719. k = (k + 1) & rt_hash_mask;
  720. rthp = &rt_hash_table[k].chain;
  721. spin_lock_bh(rt_hash_lock_addr(k));
  722. while ((rth = *rthp) != NULL) {
  723. if (!rt_may_expire(rth, tmo, expire)) {
  724. tmo >>= 1;
  725. rthp = &rth->u.rt_next;
  726. continue;
  727. }
  728. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  729. /* remove all related balanced entries
  730. * if necessary
  731. */
  732. if (rth->u.dst.flags & DST_BALANCED) {
  733. int r;
  734. rthp = rt_remove_balanced_route(
  735. &rt_hash_table[k].chain,
  736. rth,
  737. &r);
  738. goal -= r;
  739. if (!rthp)
  740. break;
  741. } else {
  742. *rthp = rth->u.rt_next;
  743. rt_free(rth);
  744. goal--;
  745. }
  746. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  747. *rthp = rth->u.rt_next;
  748. rt_free(rth);
  749. goal--;
  750. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  751. }
  752. spin_unlock_bh(rt_hash_lock_addr(k));
  753. if (goal <= 0)
  754. break;
  755. }
  756. rover = k;
  757. if (goal <= 0)
  758. goto work_done;
  759. /* Goal is not achieved. We stop process if:
  760. - if expire reduced to zero. Otherwise, expire is halfed.
  761. - if table is not full.
  762. - if we are called from interrupt.
  763. - jiffies check is just fallback/debug loop breaker.
  764. We will not spin here for long time in any case.
  765. */
  766. RT_CACHE_STAT_INC(gc_goal_miss);
  767. if (expire == 0)
  768. break;
  769. expire >>= 1;
  770. #if RT_CACHE_DEBUG >= 2
  771. printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
  772. atomic_read(&ipv4_dst_ops.entries), goal, i);
  773. #endif
  774. if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
  775. goto out;
  776. } while (!in_softirq() && time_before_eq(jiffies, now));
  777. if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
  778. goto out;
  779. if (net_ratelimit())
  780. printk(KERN_WARNING "dst cache overflow\n");
  781. RT_CACHE_STAT_INC(gc_dst_overflow);
  782. return 1;
  783. work_done:
  784. expire += ip_rt_gc_min_interval;
  785. if (expire > ip_rt_gc_timeout ||
  786. atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
  787. expire = ip_rt_gc_timeout;
  788. #if RT_CACHE_DEBUG >= 2
  789. printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
  790. atomic_read(&ipv4_dst_ops.entries), goal, rover);
  791. #endif
  792. out: return 0;
  793. }
  794. static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
  795. {
  796. struct rtable *rth, **rthp;
  797. unsigned long now;
  798. struct rtable *cand, **candp;
  799. u32 min_score;
  800. int chain_length;
  801. int attempts = !in_softirq();
  802. restart:
  803. chain_length = 0;
  804. min_score = ~(u32)0;
  805. cand = NULL;
  806. candp = NULL;
  807. now = jiffies;
  808. rthp = &rt_hash_table[hash].chain;
  809. spin_lock_bh(rt_hash_lock_addr(hash));
  810. while ((rth = *rthp) != NULL) {
  811. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  812. if (!(rth->u.dst.flags & DST_BALANCED) &&
  813. compare_keys(&rth->fl, &rt->fl)) {
  814. #else
  815. if (compare_keys(&rth->fl, &rt->fl)) {
  816. #endif
  817. /* Put it first */
  818. *rthp = rth->u.rt_next;
  819. /*
  820. * Since lookup is lockfree, the deletion
  821. * must be visible to another weakly ordered CPU before
  822. * the insertion at the start of the hash chain.
  823. */
  824. rcu_assign_pointer(rth->u.rt_next,
  825. rt_hash_table[hash].chain);
  826. /*
  827. * Since lookup is lockfree, the update writes
  828. * must be ordered for consistency on SMP.
  829. */
  830. rcu_assign_pointer(rt_hash_table[hash].chain, rth);
  831. rth->u.dst.__use++;
  832. dst_hold(&rth->u.dst);
  833. rth->u.dst.lastuse = now;
  834. spin_unlock_bh(rt_hash_lock_addr(hash));
  835. rt_drop(rt);
  836. *rp = rth;
  837. return 0;
  838. }
  839. if (!atomic_read(&rth->u.dst.__refcnt)) {
  840. u32 score = rt_score(rth);
  841. if (score <= min_score) {
  842. cand = rth;
  843. candp = rthp;
  844. min_score = score;
  845. }
  846. }
  847. chain_length++;
  848. rthp = &rth->u.rt_next;
  849. }
  850. if (cand) {
  851. /* ip_rt_gc_elasticity used to be average length of chain
  852. * length, when exceeded gc becomes really aggressive.
  853. *
  854. * The second limit is less certain. At the moment it allows
  855. * only 2 entries per bucket. We will see.
  856. */
  857. if (chain_length > ip_rt_gc_elasticity) {
  858. *candp = cand->u.rt_next;
  859. rt_free(cand);
  860. }
  861. }
  862. /* Try to bind route to arp only if it is output
  863. route or unicast forwarding path.
  864. */
  865. if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
  866. int err = arp_bind_neighbour(&rt->u.dst);
  867. if (err) {
  868. spin_unlock_bh(rt_hash_lock_addr(hash));
  869. if (err != -ENOBUFS) {
  870. rt_drop(rt);
  871. return err;
  872. }
  873. /* Neighbour tables are full and nothing
  874. can be released. Try to shrink route cache,
  875. it is most likely it holds some neighbour records.
  876. */
  877. if (attempts-- > 0) {
  878. int saved_elasticity = ip_rt_gc_elasticity;
  879. int saved_int = ip_rt_gc_min_interval;
  880. ip_rt_gc_elasticity = 1;
  881. ip_rt_gc_min_interval = 0;
  882. rt_garbage_collect();
  883. ip_rt_gc_min_interval = saved_int;
  884. ip_rt_gc_elasticity = saved_elasticity;
  885. goto restart;
  886. }
  887. if (net_ratelimit())
  888. printk(KERN_WARNING "Neighbour table overflow.\n");
  889. rt_drop(rt);
  890. return -ENOBUFS;
  891. }
  892. }
  893. rt->u.rt_next = rt_hash_table[hash].chain;
  894. #if RT_CACHE_DEBUG >= 2
  895. if (rt->u.rt_next) {
  896. struct rtable *trt;
  897. printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
  898. NIPQUAD(rt->rt_dst));
  899. for (trt = rt->u.rt_next; trt; trt = trt->u.rt_next)
  900. printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
  901. printk("\n");
  902. }
  903. #endif
  904. rt_hash_table[hash].chain = rt;
  905. spin_unlock_bh(rt_hash_lock_addr(hash));
  906. *rp = rt;
  907. return 0;
  908. }
  909. void rt_bind_peer(struct rtable *rt, int create)
  910. {
  911. static DEFINE_SPINLOCK(rt_peer_lock);
  912. struct inet_peer *peer;
  913. peer = inet_getpeer(rt->rt_dst, create);
  914. spin_lock_bh(&rt_peer_lock);
  915. if (rt->peer == NULL) {
  916. rt->peer = peer;
  917. peer = NULL;
  918. }
  919. spin_unlock_bh(&rt_peer_lock);
  920. if (peer)
  921. inet_putpeer(peer);
  922. }
  923. /*
  924. * Peer allocation may fail only in serious out-of-memory conditions. However
  925. * we still can generate some output.
  926. * Random ID selection looks a bit dangerous because we have no chances to
  927. * select ID being unique in a reasonable period of time.
  928. * But broken packet identifier may be better than no packet at all.
  929. */
  930. static void ip_select_fb_ident(struct iphdr *iph)
  931. {
  932. static DEFINE_SPINLOCK(ip_fb_id_lock);
  933. static u32 ip_fallback_id;
  934. u32 salt;
  935. spin_lock_bh(&ip_fb_id_lock);
  936. salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
  937. iph->id = htons(salt & 0xFFFF);
  938. ip_fallback_id = salt;
  939. spin_unlock_bh(&ip_fb_id_lock);
  940. }
  941. void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
  942. {
  943. struct rtable *rt = (struct rtable *) dst;
  944. if (rt) {
  945. if (rt->peer == NULL)
  946. rt_bind_peer(rt, 1);
  947. /* If peer is attached to destination, it is never detached,
  948. so that we need not to grab a lock to dereference it.
  949. */
  950. if (rt->peer) {
  951. iph->id = htons(inet_getid(rt->peer, more));
  952. return;
  953. }
  954. } else
  955. printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
  956. __builtin_return_address(0));
  957. ip_select_fb_ident(iph);
  958. }
  959. static void rt_del(unsigned hash, struct rtable *rt)
  960. {
  961. struct rtable **rthp;
  962. spin_lock_bh(rt_hash_lock_addr(hash));
  963. ip_rt_put(rt);
  964. for (rthp = &rt_hash_table[hash].chain; *rthp;
  965. rthp = &(*rthp)->u.rt_next)
  966. if (*rthp == rt) {
  967. *rthp = rt->u.rt_next;
  968. rt_free(rt);
  969. break;
  970. }
  971. spin_unlock_bh(rt_hash_lock_addr(hash));
  972. }
  973. void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
  974. __be32 saddr, struct net_device *dev)
  975. {
  976. int i, k;
  977. struct in_device *in_dev = in_dev_get(dev);
  978. struct rtable *rth, **rthp;
  979. __be32 skeys[2] = { saddr, 0 };
  980. int ikeys[2] = { dev->ifindex, 0 };
  981. struct netevent_redirect netevent;
  982. if (!in_dev)
  983. return;
  984. if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
  985. || MULTICAST(new_gw) || BADCLASS(new_gw) || ZERONET(new_gw))
  986. goto reject_redirect;
  987. if (!IN_DEV_SHARED_MEDIA(in_dev)) {
  988. if (!inet_addr_onlink(in_dev, new_gw, old_gw))
  989. goto reject_redirect;
  990. if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
  991. goto reject_redirect;
  992. } else {
  993. if (inet_addr_type(new_gw) != RTN_UNICAST)
  994. goto reject_redirect;
  995. }
  996. for (i = 0; i < 2; i++) {
  997. for (k = 0; k < 2; k++) {
  998. unsigned hash = rt_hash(daddr, skeys[i], ikeys[k]);
  999. rthp=&rt_hash_table[hash].chain;
  1000. rcu_read_lock();
  1001. while ((rth = rcu_dereference(*rthp)) != NULL) {
  1002. struct rtable *rt;
  1003. if (rth->fl.fl4_dst != daddr ||
  1004. rth->fl.fl4_src != skeys[i] ||
  1005. rth->fl.oif != ikeys[k] ||
  1006. rth->fl.iif != 0) {
  1007. rthp = &rth->u.rt_next;
  1008. continue;
  1009. }
  1010. if (rth->rt_dst != daddr ||
  1011. rth->rt_src != saddr ||
  1012. rth->u.dst.error ||
  1013. rth->rt_gateway != old_gw ||
  1014. rth->u.dst.dev != dev)
  1015. break;
  1016. dst_hold(&rth->u.dst);
  1017. rcu_read_unlock();
  1018. rt = dst_alloc(&ipv4_dst_ops);
  1019. if (rt == NULL) {
  1020. ip_rt_put(rth);
  1021. in_dev_put(in_dev);
  1022. return;
  1023. }
  1024. /* Copy all the information. */
  1025. *rt = *rth;
  1026. INIT_RCU_HEAD(&rt->u.dst.rcu_head);
  1027. rt->u.dst.__use = 1;
  1028. atomic_set(&rt->u.dst.__refcnt, 1);
  1029. rt->u.dst.child = NULL;
  1030. if (rt->u.dst.dev)
  1031. dev_hold(rt->u.dst.dev);
  1032. if (rt->idev)
  1033. in_dev_hold(rt->idev);
  1034. rt->u.dst.obsolete = 0;
  1035. rt->u.dst.lastuse = jiffies;
  1036. rt->u.dst.path = &rt->u.dst;
  1037. rt->u.dst.neighbour = NULL;
  1038. rt->u.dst.hh = NULL;
  1039. rt->u.dst.xfrm = NULL;
  1040. rt->rt_flags |= RTCF_REDIRECTED;
  1041. /* Gateway is different ... */
  1042. rt->rt_gateway = new_gw;
  1043. /* Redirect received -> path was valid */
  1044. dst_confirm(&rth->u.dst);
  1045. if (rt->peer)
  1046. atomic_inc(&rt->peer->refcnt);
  1047. if (arp_bind_neighbour(&rt->u.dst) ||
  1048. !(rt->u.dst.neighbour->nud_state &
  1049. NUD_VALID)) {
  1050. if (rt->u.dst.neighbour)
  1051. neigh_event_send(rt->u.dst.neighbour, NULL);
  1052. ip_rt_put(rth);
  1053. rt_drop(rt);
  1054. goto do_next;
  1055. }
  1056. netevent.old = &rth->u.dst;
  1057. netevent.new = &rt->u.dst;
  1058. call_netevent_notifiers(NETEVENT_REDIRECT,
  1059. &netevent);
  1060. rt_del(hash, rth);
  1061. if (!rt_intern_hash(hash, rt, &rt))
  1062. ip_rt_put(rt);
  1063. goto do_next;
  1064. }
  1065. rcu_read_unlock();
  1066. do_next:
  1067. ;
  1068. }
  1069. }
  1070. in_dev_put(in_dev);
  1071. return;
  1072. reject_redirect:
  1073. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1074. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
  1075. printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
  1076. "%u.%u.%u.%u ignored.\n"
  1077. " Advised path = %u.%u.%u.%u -> %u.%u.%u.%u\n",
  1078. NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
  1079. NIPQUAD(saddr), NIPQUAD(daddr));
  1080. #endif
  1081. in_dev_put(in_dev);
  1082. }
  1083. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
  1084. {
  1085. struct rtable *rt = (struct rtable*)dst;
  1086. struct dst_entry *ret = dst;
  1087. if (rt) {
  1088. if (dst->obsolete) {
  1089. ip_rt_put(rt);
  1090. ret = NULL;
  1091. } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
  1092. rt->u.dst.expires) {
  1093. unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
  1094. rt->fl.oif);
  1095. #if RT_CACHE_DEBUG >= 1
  1096. printk(KERN_DEBUG "ip_rt_advice: redirect to "
  1097. "%u.%u.%u.%u/%02x dropped\n",
  1098. NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
  1099. #endif
  1100. rt_del(hash, rt);
  1101. ret = NULL;
  1102. }
  1103. }
  1104. return ret;
  1105. }
  1106. /*
  1107. * Algorithm:
  1108. * 1. The first ip_rt_redirect_number redirects are sent
  1109. * with exponential backoff, then we stop sending them at all,
  1110. * assuming that the host ignores our redirects.
  1111. * 2. If we did not see packets requiring redirects
  1112. * during ip_rt_redirect_silence, we assume that the host
  1113. * forgot redirected route and start to send redirects again.
  1114. *
  1115. * This algorithm is much cheaper and more intelligent than dumb load limiting
  1116. * in icmp.c.
  1117. *
  1118. * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
  1119. * and "frag. need" (breaks PMTU discovery) in icmp.c.
  1120. */
  1121. void ip_rt_send_redirect(struct sk_buff *skb)
  1122. {
  1123. struct rtable *rt = (struct rtable*)skb->dst;
  1124. struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
  1125. if (!in_dev)
  1126. return;
  1127. if (!IN_DEV_TX_REDIRECTS(in_dev))
  1128. goto out;
  1129. /* No redirected packets during ip_rt_redirect_silence;
  1130. * reset the algorithm.
  1131. */
  1132. if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
  1133. rt->u.dst.rate_tokens = 0;
  1134. /* Too many ignored redirects; do not send anything
  1135. * set u.dst.rate_last to the last seen redirected packet.
  1136. */
  1137. if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
  1138. rt->u.dst.rate_last = jiffies;
  1139. goto out;
  1140. }
  1141. /* Check for load limit; set rate_last to the latest sent
  1142. * redirect.
  1143. */
  1144. if (time_after(jiffies,
  1145. (rt->u.dst.rate_last +
  1146. (ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
  1147. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
  1148. rt->u.dst.rate_last = jiffies;
  1149. ++rt->u.dst.rate_tokens;
  1150. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1151. if (IN_DEV_LOG_MARTIANS(in_dev) &&
  1152. rt->u.dst.rate_tokens == ip_rt_redirect_number &&
  1153. net_ratelimit())
  1154. printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
  1155. "redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
  1156. NIPQUAD(rt->rt_src), rt->rt_iif,
  1157. NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
  1158. #endif
  1159. }
  1160. out:
  1161. in_dev_put(in_dev);
  1162. }
  1163. static int ip_error(struct sk_buff *skb)
  1164. {
  1165. struct rtable *rt = (struct rtable*)skb->dst;
  1166. unsigned long now;
  1167. int code;
  1168. switch (rt->u.dst.error) {
  1169. case EINVAL:
  1170. default:
  1171. goto out;
  1172. case EHOSTUNREACH:
  1173. code = ICMP_HOST_UNREACH;
  1174. break;
  1175. case ENETUNREACH:
  1176. code = ICMP_NET_UNREACH;
  1177. break;
  1178. case EACCES:
  1179. code = ICMP_PKT_FILTERED;
  1180. break;
  1181. }
  1182. now = jiffies;
  1183. rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
  1184. if (rt->u.dst.rate_tokens > ip_rt_error_burst)
  1185. rt->u.dst.rate_tokens = ip_rt_error_burst;
  1186. rt->u.dst.rate_last = now;
  1187. if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
  1188. rt->u.dst.rate_tokens -= ip_rt_error_cost;
  1189. icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
  1190. }
  1191. out: kfree_skb(skb);
  1192. return 0;
  1193. }
  1194. /*
  1195. * The last two values are not from the RFC but
  1196. * are needed for AMPRnet AX.25 paths.
  1197. */
  1198. static const unsigned short mtu_plateau[] =
  1199. {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
  1200. static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
  1201. {
  1202. int i;
  1203. for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
  1204. if (old_mtu > mtu_plateau[i])
  1205. return mtu_plateau[i];
  1206. return 68;
  1207. }
  1208. unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
  1209. {
  1210. int i;
  1211. unsigned short old_mtu = ntohs(iph->tot_len);
  1212. struct rtable *rth;
  1213. __be32 skeys[2] = { iph->saddr, 0, };
  1214. __be32 daddr = iph->daddr;
  1215. unsigned short est_mtu = 0;
  1216. if (ipv4_config.no_pmtu_disc)
  1217. return 0;
  1218. for (i = 0; i < 2; i++) {
  1219. unsigned hash = rt_hash(daddr, skeys[i], 0);
  1220. rcu_read_lock();
  1221. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  1222. rth = rcu_dereference(rth->u.rt_next)) {
  1223. if (rth->fl.fl4_dst == daddr &&
  1224. rth->fl.fl4_src == skeys[i] &&
  1225. rth->rt_dst == daddr &&
  1226. rth->rt_src == iph->saddr &&
  1227. rth->fl.iif == 0 &&
  1228. !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
  1229. unsigned short mtu = new_mtu;
  1230. if (new_mtu < 68 || new_mtu >= old_mtu) {
  1231. /* BSD 4.2 compatibility hack :-( */
  1232. if (mtu == 0 &&
  1233. old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
  1234. old_mtu >= 68 + (iph->ihl << 2))
  1235. old_mtu -= iph->ihl << 2;
  1236. mtu = guess_mtu(old_mtu);
  1237. }
  1238. if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
  1239. if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
  1240. dst_confirm(&rth->u.dst);
  1241. if (mtu < ip_rt_min_pmtu) {
  1242. mtu = ip_rt_min_pmtu;
  1243. rth->u.dst.metrics[RTAX_LOCK-1] |=
  1244. (1 << RTAX_MTU);
  1245. }
  1246. rth->u.dst.metrics[RTAX_MTU-1] = mtu;
  1247. dst_set_expires(&rth->u.dst,
  1248. ip_rt_mtu_expires);
  1249. }
  1250. est_mtu = mtu;
  1251. }
  1252. }
  1253. }
  1254. rcu_read_unlock();
  1255. }
  1256. return est_mtu ? : new_mtu;
  1257. }
  1258. static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
  1259. {
  1260. if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
  1261. !(dst_metric_locked(dst, RTAX_MTU))) {
  1262. if (mtu < ip_rt_min_pmtu) {
  1263. mtu = ip_rt_min_pmtu;
  1264. dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
  1265. }
  1266. dst->metrics[RTAX_MTU-1] = mtu;
  1267. dst_set_expires(dst, ip_rt_mtu_expires);
  1268. call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
  1269. }
  1270. }
  1271. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
  1272. {
  1273. return NULL;
  1274. }
  1275. static void ipv4_dst_destroy(struct dst_entry *dst)
  1276. {
  1277. struct rtable *rt = (struct rtable *) dst;
  1278. struct inet_peer *peer = rt->peer;
  1279. struct in_device *idev = rt->idev;
  1280. if (peer) {
  1281. rt->peer = NULL;
  1282. inet_putpeer(peer);
  1283. }
  1284. if (idev) {
  1285. rt->idev = NULL;
  1286. in_dev_put(idev);
  1287. }
  1288. }
  1289. static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  1290. int how)
  1291. {
  1292. struct rtable *rt = (struct rtable *) dst;
  1293. struct in_device *idev = rt->idev;
  1294. if (dev != &loopback_dev && idev && idev->dev == dev) {
  1295. struct in_device *loopback_idev = in_dev_get(&loopback_dev);
  1296. if (loopback_idev) {
  1297. rt->idev = loopback_idev;
  1298. in_dev_put(idev);
  1299. }
  1300. }
  1301. }
  1302. static void ipv4_link_failure(struct sk_buff *skb)
  1303. {
  1304. struct rtable *rt;
  1305. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
  1306. rt = (struct rtable *) skb->dst;
  1307. if (rt)
  1308. dst_set_expires(&rt->u.dst, 0);
  1309. }
  1310. static int ip_rt_bug(struct sk_buff *skb)
  1311. {
  1312. printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
  1313. NIPQUAD(skb->nh.iph->saddr), NIPQUAD(skb->nh.iph->daddr),
  1314. skb->dev ? skb->dev->name : "?");
  1315. kfree_skb(skb);
  1316. return 0;
  1317. }
  1318. /*
  1319. We do not cache source address of outgoing interface,
  1320. because it is used only by IP RR, TS and SRR options,
  1321. so that it out of fast path.
  1322. BTW remember: "addr" is allowed to be not aligned
  1323. in IP options!
  1324. */
  1325. void ip_rt_get_source(u8 *addr, struct rtable *rt)
  1326. {
  1327. __be32 src;
  1328. struct fib_result res;
  1329. if (rt->fl.iif == 0)
  1330. src = rt->rt_src;
  1331. else if (fib_lookup(&rt->fl, &res) == 0) {
  1332. src = FIB_RES_PREFSRC(res);
  1333. fib_res_put(&res);
  1334. } else
  1335. src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
  1336. RT_SCOPE_UNIVERSE);
  1337. memcpy(addr, &src, 4);
  1338. }
  1339. #ifdef CONFIG_NET_CLS_ROUTE
  1340. static void set_class_tag(struct rtable *rt, u32 tag)
  1341. {
  1342. if (!(rt->u.dst.tclassid & 0xFFFF))
  1343. rt->u.dst.tclassid |= tag & 0xFFFF;
  1344. if (!(rt->u.dst.tclassid & 0xFFFF0000))
  1345. rt->u.dst.tclassid |= tag & 0xFFFF0000;
  1346. }
  1347. #endif
  1348. static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
  1349. {
  1350. struct fib_info *fi = res->fi;
  1351. if (fi) {
  1352. if (FIB_RES_GW(*res) &&
  1353. FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  1354. rt->rt_gateway = FIB_RES_GW(*res);
  1355. memcpy(rt->u.dst.metrics, fi->fib_metrics,
  1356. sizeof(rt->u.dst.metrics));
  1357. if (fi->fib_mtu == 0) {
  1358. rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
  1359. if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
  1360. rt->rt_gateway != rt->rt_dst &&
  1361. rt->u.dst.dev->mtu > 576)
  1362. rt->u.dst.metrics[RTAX_MTU-1] = 576;
  1363. }
  1364. #ifdef CONFIG_NET_CLS_ROUTE
  1365. rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
  1366. #endif
  1367. } else
  1368. rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
  1369. if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
  1370. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
  1371. if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
  1372. rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
  1373. if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
  1374. rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
  1375. ip_rt_min_advmss);
  1376. if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
  1377. rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
  1378. #ifdef CONFIG_NET_CLS_ROUTE
  1379. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1380. set_class_tag(rt, fib_rules_tclass(res));
  1381. #endif
  1382. set_class_tag(rt, itag);
  1383. #endif
  1384. rt->rt_type = res->type;
  1385. }
  1386. static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1387. u8 tos, struct net_device *dev, int our)
  1388. {
  1389. unsigned hash;
  1390. struct rtable *rth;
  1391. __be32 spec_dst;
  1392. struct in_device *in_dev = in_dev_get(dev);
  1393. u32 itag = 0;
  1394. /* Primary sanity checks. */
  1395. if (in_dev == NULL)
  1396. return -EINVAL;
  1397. if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr) ||
  1398. skb->protocol != htons(ETH_P_IP))
  1399. goto e_inval;
  1400. if (ZERONET(saddr)) {
  1401. if (!LOCAL_MCAST(daddr))
  1402. goto e_inval;
  1403. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  1404. } else if (fib_validate_source(saddr, 0, tos, 0,
  1405. dev, &spec_dst, &itag) < 0)
  1406. goto e_inval;
  1407. rth = dst_alloc(&ipv4_dst_ops);
  1408. if (!rth)
  1409. goto e_nobufs;
  1410. rth->u.dst.output= ip_rt_bug;
  1411. atomic_set(&rth->u.dst.__refcnt, 1);
  1412. rth->u.dst.flags= DST_HOST;
  1413. if (in_dev->cnf.no_policy)
  1414. rth->u.dst.flags |= DST_NOPOLICY;
  1415. rth->fl.fl4_dst = daddr;
  1416. rth->rt_dst = daddr;
  1417. rth->fl.fl4_tos = tos;
  1418. #ifdef CONFIG_IP_ROUTE_FWMARK
  1419. rth->fl.fl4_fwmark= skb->nfmark;
  1420. #endif
  1421. rth->fl.fl4_src = saddr;
  1422. rth->rt_src = saddr;
  1423. #ifdef CONFIG_NET_CLS_ROUTE
  1424. rth->u.dst.tclassid = itag;
  1425. #endif
  1426. rth->rt_iif =
  1427. rth->fl.iif = dev->ifindex;
  1428. rth->u.dst.dev = &loopback_dev;
  1429. dev_hold(rth->u.dst.dev);
  1430. rth->idev = in_dev_get(rth->u.dst.dev);
  1431. rth->fl.oif = 0;
  1432. rth->rt_gateway = daddr;
  1433. rth->rt_spec_dst= spec_dst;
  1434. rth->rt_type = RTN_MULTICAST;
  1435. rth->rt_flags = RTCF_MULTICAST;
  1436. if (our) {
  1437. rth->u.dst.input= ip_local_deliver;
  1438. rth->rt_flags |= RTCF_LOCAL;
  1439. }
  1440. #ifdef CONFIG_IP_MROUTE
  1441. if (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
  1442. rth->u.dst.input = ip_mr_input;
  1443. #endif
  1444. RT_CACHE_STAT_INC(in_slow_mc);
  1445. in_dev_put(in_dev);
  1446. hash = rt_hash(daddr, saddr, dev->ifindex);
  1447. return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
  1448. e_nobufs:
  1449. in_dev_put(in_dev);
  1450. return -ENOBUFS;
  1451. e_inval:
  1452. in_dev_put(in_dev);
  1453. return -EINVAL;
  1454. }
  1455. static void ip_handle_martian_source(struct net_device *dev,
  1456. struct in_device *in_dev,
  1457. struct sk_buff *skb,
  1458. __be32 daddr,
  1459. __be32 saddr)
  1460. {
  1461. RT_CACHE_STAT_INC(in_martian_src);
  1462. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1463. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
  1464. /*
  1465. * RFC1812 recommendation, if source is martian,
  1466. * the only hint is MAC header.
  1467. */
  1468. printk(KERN_WARNING "martian source %u.%u.%u.%u from "
  1469. "%u.%u.%u.%u, on dev %s\n",
  1470. NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
  1471. if (dev->hard_header_len && skb->mac.raw) {
  1472. int i;
  1473. unsigned char *p = skb->mac.raw;
  1474. printk(KERN_WARNING "ll header: ");
  1475. for (i = 0; i < dev->hard_header_len; i++, p++) {
  1476. printk("%02x", *p);
  1477. if (i < (dev->hard_header_len - 1))
  1478. printk(":");
  1479. }
  1480. printk("\n");
  1481. }
  1482. }
  1483. #endif
  1484. }
  1485. static inline int __mkroute_input(struct sk_buff *skb,
  1486. struct fib_result* res,
  1487. struct in_device *in_dev,
  1488. __be32 daddr, __be32 saddr, u32 tos,
  1489. struct rtable **result)
  1490. {
  1491. struct rtable *rth;
  1492. int err;
  1493. struct in_device *out_dev;
  1494. unsigned flags = 0;
  1495. __be32 spec_dst;
  1496. u32 itag;
  1497. /* get a working reference to the output device */
  1498. out_dev = in_dev_get(FIB_RES_DEV(*res));
  1499. if (out_dev == NULL) {
  1500. if (net_ratelimit())
  1501. printk(KERN_CRIT "Bug in ip_route_input" \
  1502. "_slow(). Please, report\n");
  1503. return -EINVAL;
  1504. }
  1505. err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
  1506. in_dev->dev, &spec_dst, &itag);
  1507. if (err < 0) {
  1508. ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
  1509. saddr);
  1510. err = -EINVAL;
  1511. goto cleanup;
  1512. }
  1513. if (err)
  1514. flags |= RTCF_DIRECTSRC;
  1515. if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
  1516. (IN_DEV_SHARED_MEDIA(out_dev) ||
  1517. inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
  1518. flags |= RTCF_DOREDIRECT;
  1519. if (skb->protocol != htons(ETH_P_IP)) {
  1520. /* Not IP (i.e. ARP). Do not create route, if it is
  1521. * invalid for proxy arp. DNAT routes are always valid.
  1522. */
  1523. if (out_dev == in_dev && !(flags & RTCF_DNAT)) {
  1524. err = -EINVAL;
  1525. goto cleanup;
  1526. }
  1527. }
  1528. rth = dst_alloc(&ipv4_dst_ops);
  1529. if (!rth) {
  1530. err = -ENOBUFS;
  1531. goto cleanup;
  1532. }
  1533. atomic_set(&rth->u.dst.__refcnt, 1);
  1534. rth->u.dst.flags= DST_HOST;
  1535. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  1536. if (res->fi->fib_nhs > 1)
  1537. rth->u.dst.flags |= DST_BALANCED;
  1538. #endif
  1539. if (in_dev->cnf.no_policy)
  1540. rth->u.dst.flags |= DST_NOPOLICY;
  1541. if (in_dev->cnf.no_xfrm)
  1542. rth->u.dst.flags |= DST_NOXFRM;
  1543. rth->fl.fl4_dst = daddr;
  1544. rth->rt_dst = daddr;
  1545. rth->fl.fl4_tos = tos;
  1546. #ifdef CONFIG_IP_ROUTE_FWMARK
  1547. rth->fl.fl4_fwmark= skb->nfmark;
  1548. #endif
  1549. rth->fl.fl4_src = saddr;
  1550. rth->rt_src = saddr;
  1551. rth->rt_gateway = daddr;
  1552. rth->rt_iif =
  1553. rth->fl.iif = in_dev->dev->ifindex;
  1554. rth->u.dst.dev = (out_dev)->dev;
  1555. dev_hold(rth->u.dst.dev);
  1556. rth->idev = in_dev_get(rth->u.dst.dev);
  1557. rth->fl.oif = 0;
  1558. rth->rt_spec_dst= spec_dst;
  1559. rth->u.dst.input = ip_forward;
  1560. rth->u.dst.output = ip_output;
  1561. rt_set_nexthop(rth, res, itag);
  1562. rth->rt_flags = flags;
  1563. *result = rth;
  1564. err = 0;
  1565. cleanup:
  1566. /* release the working reference to the output device */
  1567. in_dev_put(out_dev);
  1568. return err;
  1569. }
  1570. static inline int ip_mkroute_input_def(struct sk_buff *skb,
  1571. struct fib_result* res,
  1572. const struct flowi *fl,
  1573. struct in_device *in_dev,
  1574. __be32 daddr, __be32 saddr, u32 tos)
  1575. {
  1576. struct rtable* rth = NULL;
  1577. int err;
  1578. unsigned hash;
  1579. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1580. if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
  1581. fib_select_multipath(fl, res);
  1582. #endif
  1583. /* create a routing cache entry */
  1584. err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
  1585. if (err)
  1586. return err;
  1587. /* put it into the cache */
  1588. hash = rt_hash(daddr, saddr, fl->iif);
  1589. return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
  1590. }
  1591. static inline int ip_mkroute_input(struct sk_buff *skb,
  1592. struct fib_result* res,
  1593. const struct flowi *fl,
  1594. struct in_device *in_dev,
  1595. __be32 daddr, __be32 saddr, u32 tos)
  1596. {
  1597. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  1598. struct rtable* rth = NULL, *rtres;
  1599. unsigned char hop, hopcount;
  1600. int err = -EINVAL;
  1601. unsigned int hash;
  1602. if (res->fi)
  1603. hopcount = res->fi->fib_nhs;
  1604. else
  1605. hopcount = 1;
  1606. /* distinguish between multipath and singlepath */
  1607. if (hopcount < 2)
  1608. return ip_mkroute_input_def(skb, res, fl, in_dev, daddr,
  1609. saddr, tos);
  1610. /* add all alternatives to the routing cache */
  1611. for (hop = 0; hop < hopcount; hop++) {
  1612. res->nh_sel = hop;
  1613. /* put reference to previous result */
  1614. if (hop)
  1615. ip_rt_put(rtres);
  1616. /* create a routing cache entry */
  1617. err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos,
  1618. &rth);
  1619. if (err)
  1620. return err;
  1621. /* put it into the cache */
  1622. hash = rt_hash(daddr, saddr, fl->iif);
  1623. err = rt_intern_hash(hash, rth, &rtres);
  1624. if (err)
  1625. return err;
  1626. /* forward hop information to multipath impl. */
  1627. multipath_set_nhinfo(rth,
  1628. FIB_RES_NETWORK(*res),
  1629. FIB_RES_NETMASK(*res),
  1630. res->prefixlen,
  1631. &FIB_RES_NH(*res));
  1632. }
  1633. skb->dst = &rtres->u.dst;
  1634. return err;
  1635. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  1636. return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos);
  1637. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  1638. }
  1639. /*
  1640. * NOTE. We drop all the packets that has local source
  1641. * addresses, because every properly looped back packet
  1642. * must have correct destination already attached by output routine.
  1643. *
  1644. * Such approach solves two big problems:
  1645. * 1. Not simplex devices are handled properly.
  1646. * 2. IP spoofing attempts are filtered with 100% of guarantee.
  1647. */
  1648. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1649. u8 tos, struct net_device *dev)
  1650. {
  1651. struct fib_result res;
  1652. struct in_device *in_dev = in_dev_get(dev);
  1653. struct flowi fl = { .nl_u = { .ip4_u =
  1654. { .daddr = daddr,
  1655. .saddr = saddr,
  1656. .tos = tos,
  1657. .scope = RT_SCOPE_UNIVERSE,
  1658. #ifdef CONFIG_IP_ROUTE_FWMARK
  1659. .fwmark = skb->nfmark
  1660. #endif
  1661. } },
  1662. .iif = dev->ifindex };
  1663. unsigned flags = 0;
  1664. u32 itag = 0;
  1665. struct rtable * rth;
  1666. unsigned hash;
  1667. __be32 spec_dst;
  1668. int err = -EINVAL;
  1669. int free_res = 0;
  1670. /* IP on this device is disabled. */
  1671. if (!in_dev)
  1672. goto out;
  1673. /* Check for the most weird martians, which can be not detected
  1674. by fib_lookup.
  1675. */
  1676. if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr))
  1677. goto martian_source;
  1678. if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
  1679. goto brd_input;
  1680. /* Accept zero addresses only to limited broadcast;
  1681. * I even do not know to fix it or not. Waiting for complains :-)
  1682. */
  1683. if (ZERONET(saddr))
  1684. goto martian_source;
  1685. if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
  1686. goto martian_destination;
  1687. /*
  1688. * Now we are ready to route packet.
  1689. */
  1690. if ((err = fib_lookup(&fl, &res)) != 0) {
  1691. if (!IN_DEV_FORWARD(in_dev))
  1692. goto e_hostunreach;
  1693. goto no_route;
  1694. }
  1695. free_res = 1;
  1696. RT_CACHE_STAT_INC(in_slow_tot);
  1697. if (res.type == RTN_BROADCAST)
  1698. goto brd_input;
  1699. if (res.type == RTN_LOCAL) {
  1700. int result;
  1701. result = fib_validate_source(saddr, daddr, tos,
  1702. loopback_dev.ifindex,
  1703. dev, &spec_dst, &itag);
  1704. if (result < 0)
  1705. goto martian_source;
  1706. if (result)
  1707. flags |= RTCF_DIRECTSRC;
  1708. spec_dst = daddr;
  1709. goto local_input;
  1710. }
  1711. if (!IN_DEV_FORWARD(in_dev))
  1712. goto e_hostunreach;
  1713. if (res.type != RTN_UNICAST)
  1714. goto martian_destination;
  1715. err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
  1716. if (err == -ENOBUFS)
  1717. goto e_nobufs;
  1718. if (err == -EINVAL)
  1719. goto e_inval;
  1720. done:
  1721. in_dev_put(in_dev);
  1722. if (free_res)
  1723. fib_res_put(&res);
  1724. out: return err;
  1725. brd_input:
  1726. if (skb->protocol != htons(ETH_P_IP))
  1727. goto e_inval;
  1728. if (ZERONET(saddr))
  1729. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  1730. else {
  1731. err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
  1732. &itag);
  1733. if (err < 0)
  1734. goto martian_source;
  1735. if (err)
  1736. flags |= RTCF_DIRECTSRC;
  1737. }
  1738. flags |= RTCF_BROADCAST;
  1739. res.type = RTN_BROADCAST;
  1740. RT_CACHE_STAT_INC(in_brd);
  1741. local_input:
  1742. rth = dst_alloc(&ipv4_dst_ops);
  1743. if (!rth)
  1744. goto e_nobufs;
  1745. rth->u.dst.output= ip_rt_bug;
  1746. atomic_set(&rth->u.dst.__refcnt, 1);
  1747. rth->u.dst.flags= DST_HOST;
  1748. if (in_dev->cnf.no_policy)
  1749. rth->u.dst.flags |= DST_NOPOLICY;
  1750. rth->fl.fl4_dst = daddr;
  1751. rth->rt_dst = daddr;
  1752. rth->fl.fl4_tos = tos;
  1753. #ifdef CONFIG_IP_ROUTE_FWMARK
  1754. rth->fl.fl4_fwmark= skb->nfmark;
  1755. #endif
  1756. rth->fl.fl4_src = saddr;
  1757. rth->rt_src = saddr;
  1758. #ifdef CONFIG_NET_CLS_ROUTE
  1759. rth->u.dst.tclassid = itag;
  1760. #endif
  1761. rth->rt_iif =
  1762. rth->fl.iif = dev->ifindex;
  1763. rth->u.dst.dev = &loopback_dev;
  1764. dev_hold(rth->u.dst.dev);
  1765. rth->idev = in_dev_get(rth->u.dst.dev);
  1766. rth->rt_gateway = daddr;
  1767. rth->rt_spec_dst= spec_dst;
  1768. rth->u.dst.input= ip_local_deliver;
  1769. rth->rt_flags = flags|RTCF_LOCAL;
  1770. if (res.type == RTN_UNREACHABLE) {
  1771. rth->u.dst.input= ip_error;
  1772. rth->u.dst.error= -err;
  1773. rth->rt_flags &= ~RTCF_LOCAL;
  1774. }
  1775. rth->rt_type = res.type;
  1776. hash = rt_hash(daddr, saddr, fl.iif);
  1777. err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
  1778. goto done;
  1779. no_route:
  1780. RT_CACHE_STAT_INC(in_no_route);
  1781. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  1782. res.type = RTN_UNREACHABLE;
  1783. goto local_input;
  1784. /*
  1785. * Do not cache martian addresses: they should be logged (RFC1812)
  1786. */
  1787. martian_destination:
  1788. RT_CACHE_STAT_INC(in_martian_dst);
  1789. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1790. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
  1791. printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
  1792. "%u.%u.%u.%u, dev %s\n",
  1793. NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
  1794. #endif
  1795. e_hostunreach:
  1796. err = -EHOSTUNREACH;
  1797. goto done;
  1798. e_inval:
  1799. err = -EINVAL;
  1800. goto done;
  1801. e_nobufs:
  1802. err = -ENOBUFS;
  1803. goto done;
  1804. martian_source:
  1805. ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
  1806. goto e_inval;
  1807. }
  1808. int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1809. u8 tos, struct net_device *dev)
  1810. {
  1811. struct rtable * rth;
  1812. unsigned hash;
  1813. int iif = dev->ifindex;
  1814. tos &= IPTOS_RT_MASK;
  1815. hash = rt_hash(daddr, saddr, iif);
  1816. rcu_read_lock();
  1817. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  1818. rth = rcu_dereference(rth->u.rt_next)) {
  1819. if (rth->fl.fl4_dst == daddr &&
  1820. rth->fl.fl4_src == saddr &&
  1821. rth->fl.iif == iif &&
  1822. rth->fl.oif == 0 &&
  1823. #ifdef CONFIG_IP_ROUTE_FWMARK
  1824. rth->fl.fl4_fwmark == skb->nfmark &&
  1825. #endif
  1826. rth->fl.fl4_tos == tos) {
  1827. rth->u.dst.lastuse = jiffies;
  1828. dst_hold(&rth->u.dst);
  1829. rth->u.dst.__use++;
  1830. RT_CACHE_STAT_INC(in_hit);
  1831. rcu_read_unlock();
  1832. skb->dst = (struct dst_entry*)rth;
  1833. return 0;
  1834. }
  1835. RT_CACHE_STAT_INC(in_hlist_search);
  1836. }
  1837. rcu_read_unlock();
  1838. /* Multicast recognition logic is moved from route cache to here.
  1839. The problem was that too many Ethernet cards have broken/missing
  1840. hardware multicast filters :-( As result the host on multicasting
  1841. network acquires a lot of useless route cache entries, sort of
  1842. SDR messages from all the world. Now we try to get rid of them.
  1843. Really, provided software IP multicast filter is organized
  1844. reasonably (at least, hashed), it does not result in a slowdown
  1845. comparing with route cache reject entries.
  1846. Note, that multicast routers are not affected, because
  1847. route cache entry is created eventually.
  1848. */
  1849. if (MULTICAST(daddr)) {
  1850. struct in_device *in_dev;
  1851. rcu_read_lock();
  1852. if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
  1853. int our = ip_check_mc(in_dev, daddr, saddr,
  1854. skb->nh.iph->protocol);
  1855. if (our
  1856. #ifdef CONFIG_IP_MROUTE
  1857. || (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
  1858. #endif
  1859. ) {
  1860. rcu_read_unlock();
  1861. return ip_route_input_mc(skb, daddr, saddr,
  1862. tos, dev, our);
  1863. }
  1864. }
  1865. rcu_read_unlock();
  1866. return -EINVAL;
  1867. }
  1868. return ip_route_input_slow(skb, daddr, saddr, tos, dev);
  1869. }
  1870. static inline int __mkroute_output(struct rtable **result,
  1871. struct fib_result* res,
  1872. const struct flowi *fl,
  1873. const struct flowi *oldflp,
  1874. struct net_device *dev_out,
  1875. unsigned flags)
  1876. {
  1877. struct rtable *rth;
  1878. struct in_device *in_dev;
  1879. u32 tos = RT_FL_TOS(oldflp);
  1880. int err = 0;
  1881. if (LOOPBACK(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
  1882. return -EINVAL;
  1883. if (fl->fl4_dst == htonl(0xFFFFFFFF))
  1884. res->type = RTN_BROADCAST;
  1885. else if (MULTICAST(fl->fl4_dst))
  1886. res->type = RTN_MULTICAST;
  1887. else if (BADCLASS(fl->fl4_dst) || ZERONET(fl->fl4_dst))
  1888. return -EINVAL;
  1889. if (dev_out->flags & IFF_LOOPBACK)
  1890. flags |= RTCF_LOCAL;
  1891. /* get work reference to inet device */
  1892. in_dev = in_dev_get(dev_out);
  1893. if (!in_dev)
  1894. return -EINVAL;
  1895. if (res->type == RTN_BROADCAST) {
  1896. flags |= RTCF_BROADCAST | RTCF_LOCAL;
  1897. if (res->fi) {
  1898. fib_info_put(res->fi);
  1899. res->fi = NULL;
  1900. }
  1901. } else if (res->type == RTN_MULTICAST) {
  1902. flags |= RTCF_MULTICAST|RTCF_LOCAL;
  1903. if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
  1904. oldflp->proto))
  1905. flags &= ~RTCF_LOCAL;
  1906. /* If multicast route do not exist use
  1907. default one, but do not gateway in this case.
  1908. Yes, it is hack.
  1909. */
  1910. if (res->fi && res->prefixlen < 4) {
  1911. fib_info_put(res->fi);
  1912. res->fi = NULL;
  1913. }
  1914. }
  1915. rth = dst_alloc(&ipv4_dst_ops);
  1916. if (!rth) {
  1917. err = -ENOBUFS;
  1918. goto cleanup;
  1919. }
  1920. atomic_set(&rth->u.dst.__refcnt, 1);
  1921. rth->u.dst.flags= DST_HOST;
  1922. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  1923. if (res->fi) {
  1924. rth->rt_multipath_alg = res->fi->fib_mp_alg;
  1925. if (res->fi->fib_nhs > 1)
  1926. rth->u.dst.flags |= DST_BALANCED;
  1927. }
  1928. #endif
  1929. if (in_dev->cnf.no_xfrm)
  1930. rth->u.dst.flags |= DST_NOXFRM;
  1931. if (in_dev->cnf.no_policy)
  1932. rth->u.dst.flags |= DST_NOPOLICY;
  1933. rth->fl.fl4_dst = oldflp->fl4_dst;
  1934. rth->fl.fl4_tos = tos;
  1935. rth->fl.fl4_src = oldflp->fl4_src;
  1936. rth->fl.oif = oldflp->oif;
  1937. #ifdef CONFIG_IP_ROUTE_FWMARK
  1938. rth->fl.fl4_fwmark= oldflp->fl4_fwmark;
  1939. #endif
  1940. rth->rt_dst = fl->fl4_dst;
  1941. rth->rt_src = fl->fl4_src;
  1942. rth->rt_iif = oldflp->oif ? : dev_out->ifindex;
  1943. /* get references to the devices that are to be hold by the routing
  1944. cache entry */
  1945. rth->u.dst.dev = dev_out;
  1946. dev_hold(dev_out);
  1947. rth->idev = in_dev_get(dev_out);
  1948. rth->rt_gateway = fl->fl4_dst;
  1949. rth->rt_spec_dst= fl->fl4_src;
  1950. rth->u.dst.output=ip_output;
  1951. RT_CACHE_STAT_INC(out_slow_tot);
  1952. if (flags & RTCF_LOCAL) {
  1953. rth->u.dst.input = ip_local_deliver;
  1954. rth->rt_spec_dst = fl->fl4_dst;
  1955. }
  1956. if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  1957. rth->rt_spec_dst = fl->fl4_src;
  1958. if (flags & RTCF_LOCAL &&
  1959. !(dev_out->flags & IFF_LOOPBACK)) {
  1960. rth->u.dst.output = ip_mc_output;
  1961. RT_CACHE_STAT_INC(out_slow_mc);
  1962. }
  1963. #ifdef CONFIG_IP_MROUTE
  1964. if (res->type == RTN_MULTICAST) {
  1965. if (IN_DEV_MFORWARD(in_dev) &&
  1966. !LOCAL_MCAST(oldflp->fl4_dst)) {
  1967. rth->u.dst.input = ip_mr_input;
  1968. rth->u.dst.output = ip_mc_output;
  1969. }
  1970. }
  1971. #endif
  1972. }
  1973. rt_set_nexthop(rth, res, 0);
  1974. rth->rt_flags = flags;
  1975. *result = rth;
  1976. cleanup:
  1977. /* release work reference to inet device */
  1978. in_dev_put(in_dev);
  1979. return err;
  1980. }
  1981. static inline int ip_mkroute_output_def(struct rtable **rp,
  1982. struct fib_result* res,
  1983. const struct flowi *fl,
  1984. const struct flowi *oldflp,
  1985. struct net_device *dev_out,
  1986. unsigned flags)
  1987. {
  1988. struct rtable *rth = NULL;
  1989. int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
  1990. unsigned hash;
  1991. if (err == 0) {
  1992. hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif);
  1993. err = rt_intern_hash(hash, rth, rp);
  1994. }
  1995. return err;
  1996. }
  1997. static inline int ip_mkroute_output(struct rtable** rp,
  1998. struct fib_result* res,
  1999. const struct flowi *fl,
  2000. const struct flowi *oldflp,
  2001. struct net_device *dev_out,
  2002. unsigned flags)
  2003. {
  2004. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  2005. unsigned char hop;
  2006. unsigned hash;
  2007. int err = -EINVAL;
  2008. struct rtable *rth = NULL;
  2009. if (res->fi && res->fi->fib_nhs > 1) {
  2010. unsigned char hopcount = res->fi->fib_nhs;
  2011. for (hop = 0; hop < hopcount; hop++) {
  2012. struct net_device *dev2nexthop;
  2013. res->nh_sel = hop;
  2014. /* hold a work reference to the output device */
  2015. dev2nexthop = FIB_RES_DEV(*res);
  2016. dev_hold(dev2nexthop);
  2017. /* put reference to previous result */
  2018. if (hop)
  2019. ip_rt_put(*rp);
  2020. err = __mkroute_output(&rth, res, fl, oldflp,
  2021. dev2nexthop, flags);
  2022. if (err != 0)
  2023. goto cleanup;
  2024. hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src,
  2025. oldflp->oif);
  2026. err = rt_intern_hash(hash, rth, rp);
  2027. /* forward hop information to multipath impl. */
  2028. multipath_set_nhinfo(rth,
  2029. FIB_RES_NETWORK(*res),
  2030. FIB_RES_NETMASK(*res),
  2031. res->prefixlen,
  2032. &FIB_RES_NH(*res));
  2033. cleanup:
  2034. /* release work reference to output device */
  2035. dev_put(dev2nexthop);
  2036. if (err != 0)
  2037. return err;
  2038. }
  2039. return err;
  2040. } else {
  2041. return ip_mkroute_output_def(rp, res, fl, oldflp, dev_out,
  2042. flags);
  2043. }
  2044. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  2045. return ip_mkroute_output_def(rp, res, fl, oldflp, dev_out, flags);
  2046. #endif
  2047. }
  2048. /*
  2049. * Major route resolver routine.
  2050. */
  2051. static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
  2052. {
  2053. u32 tos = RT_FL_TOS(oldflp);
  2054. struct flowi fl = { .nl_u = { .ip4_u =
  2055. { .daddr = oldflp->fl4_dst,
  2056. .saddr = oldflp->fl4_src,
  2057. .tos = tos & IPTOS_RT_MASK,
  2058. .scope = ((tos & RTO_ONLINK) ?
  2059. RT_SCOPE_LINK :
  2060. RT_SCOPE_UNIVERSE),
  2061. #ifdef CONFIG_IP_ROUTE_FWMARK
  2062. .fwmark = oldflp->fl4_fwmark
  2063. #endif
  2064. } },
  2065. .iif = loopback_dev.ifindex,
  2066. .oif = oldflp->oif };
  2067. struct fib_result res;
  2068. unsigned flags = 0;
  2069. struct net_device *dev_out = NULL;
  2070. int free_res = 0;
  2071. int err;
  2072. res.fi = NULL;
  2073. #ifdef CONFIG_IP_MULTIPLE_TABLES
  2074. res.r = NULL;
  2075. #endif
  2076. if (oldflp->fl4_src) {
  2077. err = -EINVAL;
  2078. if (MULTICAST(oldflp->fl4_src) ||
  2079. BADCLASS(oldflp->fl4_src) ||
  2080. ZERONET(oldflp->fl4_src))
  2081. goto out;
  2082. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  2083. dev_out = ip_dev_find(oldflp->fl4_src);
  2084. if (dev_out == NULL)
  2085. goto out;
  2086. /* I removed check for oif == dev_out->oif here.
  2087. It was wrong for two reasons:
  2088. 1. ip_dev_find(saddr) can return wrong iface, if saddr is
  2089. assigned to multiple interfaces.
  2090. 2. Moreover, we are allowed to send packets with saddr
  2091. of another iface. --ANK
  2092. */
  2093. if (oldflp->oif == 0
  2094. && (MULTICAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
  2095. /* Special hack: user can direct multicasts
  2096. and limited broadcast via necessary interface
  2097. without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
  2098. This hack is not just for fun, it allows
  2099. vic,vat and friends to work.
  2100. They bind socket to loopback, set ttl to zero
  2101. and expect that it will work.
  2102. From the viewpoint of routing cache they are broken,
  2103. because we are not allowed to build multicast path
  2104. with loopback source addr (look, routing cache
  2105. cannot know, that ttl is zero, so that packet
  2106. will not leave this host and route is valid).
  2107. Luckily, this hack is good workaround.
  2108. */
  2109. fl.oif = dev_out->ifindex;
  2110. goto make_route;
  2111. }
  2112. if (dev_out)
  2113. dev_put(dev_out);
  2114. dev_out = NULL;
  2115. }
  2116. if (oldflp->oif) {
  2117. dev_out = dev_get_by_index(oldflp->oif);
  2118. err = -ENODEV;
  2119. if (dev_out == NULL)
  2120. goto out;
  2121. /* RACE: Check return value of inet_select_addr instead. */
  2122. if (__in_dev_get_rtnl(dev_out) == NULL) {
  2123. dev_put(dev_out);
  2124. goto out; /* Wrong error code */
  2125. }
  2126. if (LOCAL_MCAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
  2127. if (!fl.fl4_src)
  2128. fl.fl4_src = inet_select_addr(dev_out, 0,
  2129. RT_SCOPE_LINK);
  2130. goto make_route;
  2131. }
  2132. if (!fl.fl4_src) {
  2133. if (MULTICAST(oldflp->fl4_dst))
  2134. fl.fl4_src = inet_select_addr(dev_out, 0,
  2135. fl.fl4_scope);
  2136. else if (!oldflp->fl4_dst)
  2137. fl.fl4_src = inet_select_addr(dev_out, 0,
  2138. RT_SCOPE_HOST);
  2139. }
  2140. }
  2141. if (!fl.fl4_dst) {
  2142. fl.fl4_dst = fl.fl4_src;
  2143. if (!fl.fl4_dst)
  2144. fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
  2145. if (dev_out)
  2146. dev_put(dev_out);
  2147. dev_out = &loopback_dev;
  2148. dev_hold(dev_out);
  2149. fl.oif = loopback_dev.ifindex;
  2150. res.type = RTN_LOCAL;
  2151. flags |= RTCF_LOCAL;
  2152. goto make_route;
  2153. }
  2154. if (fib_lookup(&fl, &res)) {
  2155. res.fi = NULL;
  2156. if (oldflp->oif) {
  2157. /* Apparently, routing tables are wrong. Assume,
  2158. that the destination is on link.
  2159. WHY? DW.
  2160. Because we are allowed to send to iface
  2161. even if it has NO routes and NO assigned
  2162. addresses. When oif is specified, routing
  2163. tables are looked up with only one purpose:
  2164. to catch if destination is gatewayed, rather than
  2165. direct. Moreover, if MSG_DONTROUTE is set,
  2166. we send packet, ignoring both routing tables
  2167. and ifaddr state. --ANK
  2168. We could make it even if oif is unknown,
  2169. likely IPv6, but we do not.
  2170. */
  2171. if (fl.fl4_src == 0)
  2172. fl.fl4_src = inet_select_addr(dev_out, 0,
  2173. RT_SCOPE_LINK);
  2174. res.type = RTN_UNICAST;
  2175. goto make_route;
  2176. }
  2177. if (dev_out)
  2178. dev_put(dev_out);
  2179. err = -ENETUNREACH;
  2180. goto out;
  2181. }
  2182. free_res = 1;
  2183. if (res.type == RTN_LOCAL) {
  2184. if (!fl.fl4_src)
  2185. fl.fl4_src = fl.fl4_dst;
  2186. if (dev_out)
  2187. dev_put(dev_out);
  2188. dev_out = &loopback_dev;
  2189. dev_hold(dev_out);
  2190. fl.oif = dev_out->ifindex;
  2191. if (res.fi)
  2192. fib_info_put(res.fi);
  2193. res.fi = NULL;
  2194. flags |= RTCF_LOCAL;
  2195. goto make_route;
  2196. }
  2197. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  2198. if (res.fi->fib_nhs > 1 && fl.oif == 0)
  2199. fib_select_multipath(&fl, &res);
  2200. else
  2201. #endif
  2202. if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
  2203. fib_select_default(&fl, &res);
  2204. if (!fl.fl4_src)
  2205. fl.fl4_src = FIB_RES_PREFSRC(res);
  2206. if (dev_out)
  2207. dev_put(dev_out);
  2208. dev_out = FIB_RES_DEV(res);
  2209. dev_hold(dev_out);
  2210. fl.oif = dev_out->ifindex;
  2211. make_route:
  2212. err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
  2213. if (free_res)
  2214. fib_res_put(&res);
  2215. if (dev_out)
  2216. dev_put(dev_out);
  2217. out: return err;
  2218. }
  2219. int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
  2220. {
  2221. unsigned hash;
  2222. struct rtable *rth;
  2223. hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif);
  2224. rcu_read_lock_bh();
  2225. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  2226. rth = rcu_dereference(rth->u.rt_next)) {
  2227. if (rth->fl.fl4_dst == flp->fl4_dst &&
  2228. rth->fl.fl4_src == flp->fl4_src &&
  2229. rth->fl.iif == 0 &&
  2230. rth->fl.oif == flp->oif &&
  2231. #ifdef CONFIG_IP_ROUTE_FWMARK
  2232. rth->fl.fl4_fwmark == flp->fl4_fwmark &&
  2233. #endif
  2234. !((rth->fl.fl4_tos ^ flp->fl4_tos) &
  2235. (IPTOS_RT_MASK | RTO_ONLINK))) {
  2236. /* check for multipath routes and choose one if
  2237. * necessary
  2238. */
  2239. if (multipath_select_route(flp, rth, rp)) {
  2240. dst_hold(&(*rp)->u.dst);
  2241. RT_CACHE_STAT_INC(out_hit);
  2242. rcu_read_unlock_bh();
  2243. return 0;
  2244. }
  2245. rth->u.dst.lastuse = jiffies;
  2246. dst_hold(&rth->u.dst);
  2247. rth->u.dst.__use++;
  2248. RT_CACHE_STAT_INC(out_hit);
  2249. rcu_read_unlock_bh();
  2250. *rp = rth;
  2251. return 0;
  2252. }
  2253. RT_CACHE_STAT_INC(out_hlist_search);
  2254. }
  2255. rcu_read_unlock_bh();
  2256. return ip_route_output_slow(rp, flp);
  2257. }
  2258. EXPORT_SYMBOL_GPL(__ip_route_output_key);
  2259. int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
  2260. {
  2261. int err;
  2262. if ((err = __ip_route_output_key(rp, flp)) != 0)
  2263. return err;
  2264. if (flp->proto) {
  2265. if (!flp->fl4_src)
  2266. flp->fl4_src = (*rp)->rt_src;
  2267. if (!flp->fl4_dst)
  2268. flp->fl4_dst = (*rp)->rt_dst;
  2269. return xfrm_lookup((struct dst_entry **)rp, flp, sk, flags);
  2270. }
  2271. return 0;
  2272. }
  2273. EXPORT_SYMBOL_GPL(ip_route_output_flow);
  2274. int ip_route_output_key(struct rtable **rp, struct flowi *flp)
  2275. {
  2276. return ip_route_output_flow(rp, flp, NULL, 0);
  2277. }
  2278. static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
  2279. int nowait, unsigned int flags)
  2280. {
  2281. struct rtable *rt = (struct rtable*)skb->dst;
  2282. struct rtmsg *r;
  2283. struct nlmsghdr *nlh;
  2284. struct rta_cacheinfo ci;
  2285. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
  2286. if (nlh == NULL)
  2287. return -ENOBUFS;
  2288. r = nlmsg_data(nlh);
  2289. r->rtm_family = AF_INET;
  2290. r->rtm_dst_len = 32;
  2291. r->rtm_src_len = 0;
  2292. r->rtm_tos = rt->fl.fl4_tos;
  2293. r->rtm_table = RT_TABLE_MAIN;
  2294. NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
  2295. r->rtm_type = rt->rt_type;
  2296. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2297. r->rtm_protocol = RTPROT_UNSPEC;
  2298. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  2299. if (rt->rt_flags & RTCF_NOTIFY)
  2300. r->rtm_flags |= RTM_F_NOTIFY;
  2301. NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
  2302. if (rt->fl.fl4_src) {
  2303. r->rtm_src_len = 32;
  2304. NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
  2305. }
  2306. if (rt->u.dst.dev)
  2307. NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
  2308. #ifdef CONFIG_NET_CLS_ROUTE
  2309. if (rt->u.dst.tclassid)
  2310. NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
  2311. #endif
  2312. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  2313. if (rt->rt_multipath_alg != IP_MP_ALG_NONE)
  2314. NLA_PUT_U32(skb, RTA_MP_ALGO, rt->rt_multipath_alg);
  2315. #endif
  2316. if (rt->fl.iif)
  2317. NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
  2318. else if (rt->rt_src != rt->fl.fl4_src)
  2319. NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
  2320. if (rt->rt_dst != rt->rt_gateway)
  2321. NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
  2322. if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
  2323. goto nla_put_failure;
  2324. ci.rta_lastuse = jiffies_to_clock_t(jiffies - rt->u.dst.lastuse);
  2325. ci.rta_used = rt->u.dst.__use;
  2326. ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt);
  2327. if (rt->u.dst.expires)
  2328. ci.rta_expires = jiffies_to_clock_t(rt->u.dst.expires - jiffies);
  2329. else
  2330. ci.rta_expires = 0;
  2331. ci.rta_error = rt->u.dst.error;
  2332. ci.rta_id = ci.rta_ts = ci.rta_tsage = 0;
  2333. if (rt->peer) {
  2334. ci.rta_id = rt->peer->ip_id_count;
  2335. if (rt->peer->tcp_ts_stamp) {
  2336. ci.rta_ts = rt->peer->tcp_ts;
  2337. ci.rta_tsage = xtime.tv_sec - rt->peer->tcp_ts_stamp;
  2338. }
  2339. }
  2340. if (rt->fl.iif) {
  2341. #ifdef CONFIG_IP_MROUTE
  2342. __be32 dst = rt->rt_dst;
  2343. if (MULTICAST(dst) && !LOCAL_MCAST(dst) &&
  2344. ipv4_devconf.mc_forwarding) {
  2345. int err = ipmr_get_route(skb, r, nowait);
  2346. if (err <= 0) {
  2347. if (!nowait) {
  2348. if (err == 0)
  2349. return 0;
  2350. goto nla_put_failure;
  2351. } else {
  2352. if (err == -EMSGSIZE)
  2353. goto nla_put_failure;
  2354. ci.rta_error = err;
  2355. }
  2356. }
  2357. } else
  2358. #endif
  2359. NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
  2360. }
  2361. NLA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  2362. return nlmsg_end(skb, nlh);
  2363. nla_put_failure:
  2364. return nlmsg_cancel(skb, nlh);
  2365. }
  2366. int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
  2367. {
  2368. struct rtmsg *rtm;
  2369. struct nlattr *tb[RTA_MAX+1];
  2370. struct rtable *rt = NULL;
  2371. __be32 dst = 0;
  2372. __be32 src = 0;
  2373. u32 iif;
  2374. int err;
  2375. struct sk_buff *skb;
  2376. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
  2377. if (err < 0)
  2378. goto errout;
  2379. rtm = nlmsg_data(nlh);
  2380. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2381. if (skb == NULL) {
  2382. err = -ENOBUFS;
  2383. goto errout;
  2384. }
  2385. /* Reserve room for dummy headers, this skb can pass
  2386. through good chunk of routing engine.
  2387. */
  2388. skb->mac.raw = skb->nh.raw = skb->data;
  2389. /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
  2390. skb->nh.iph->protocol = IPPROTO_ICMP;
  2391. skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
  2392. src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
  2393. dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
  2394. iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
  2395. if (iif) {
  2396. struct net_device *dev;
  2397. dev = __dev_get_by_index(iif);
  2398. if (dev == NULL) {
  2399. err = -ENODEV;
  2400. goto errout_free;
  2401. }
  2402. skb->protocol = htons(ETH_P_IP);
  2403. skb->dev = dev;
  2404. local_bh_disable();
  2405. err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
  2406. local_bh_enable();
  2407. rt = (struct rtable*) skb->dst;
  2408. if (err == 0 && rt->u.dst.error)
  2409. err = -rt->u.dst.error;
  2410. } else {
  2411. struct flowi fl = {
  2412. .nl_u = {
  2413. .ip4_u = {
  2414. .daddr = dst,
  2415. .saddr = src,
  2416. .tos = rtm->rtm_tos,
  2417. },
  2418. },
  2419. .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
  2420. };
  2421. err = ip_route_output_key(&rt, &fl);
  2422. }
  2423. if (err)
  2424. goto errout_free;
  2425. skb->dst = &rt->u.dst;
  2426. if (rtm->rtm_flags & RTM_F_NOTIFY)
  2427. rt->rt_flags |= RTCF_NOTIFY;
  2428. err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  2429. RTM_NEWROUTE, 0, 0);
  2430. if (err <= 0)
  2431. goto errout_free;
  2432. err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
  2433. errout:
  2434. return err;
  2435. errout_free:
  2436. kfree_skb(skb);
  2437. goto errout;
  2438. }
  2439. int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2440. {
  2441. struct rtable *rt;
  2442. int h, s_h;
  2443. int idx, s_idx;
  2444. s_h = cb->args[0];
  2445. s_idx = idx = cb->args[1];
  2446. for (h = 0; h <= rt_hash_mask; h++) {
  2447. if (h < s_h) continue;
  2448. if (h > s_h)
  2449. s_idx = 0;
  2450. rcu_read_lock_bh();
  2451. for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
  2452. rt = rcu_dereference(rt->u.rt_next), idx++) {
  2453. if (idx < s_idx)
  2454. continue;
  2455. skb->dst = dst_clone(&rt->u.dst);
  2456. if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  2457. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  2458. 1, NLM_F_MULTI) <= 0) {
  2459. dst_release(xchg(&skb->dst, NULL));
  2460. rcu_read_unlock_bh();
  2461. goto done;
  2462. }
  2463. dst_release(xchg(&skb->dst, NULL));
  2464. }
  2465. rcu_read_unlock_bh();
  2466. }
  2467. done:
  2468. cb->args[0] = h;
  2469. cb->args[1] = idx;
  2470. return skb->len;
  2471. }
  2472. void ip_rt_multicast_event(struct in_device *in_dev)
  2473. {
  2474. rt_cache_flush(0);
  2475. }
  2476. #ifdef CONFIG_SYSCTL
  2477. static int flush_delay;
  2478. static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
  2479. struct file *filp, void __user *buffer,
  2480. size_t *lenp, loff_t *ppos)
  2481. {
  2482. if (write) {
  2483. proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  2484. rt_cache_flush(flush_delay);
  2485. return 0;
  2486. }
  2487. return -EINVAL;
  2488. }
  2489. static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
  2490. int __user *name,
  2491. int nlen,
  2492. void __user *oldval,
  2493. size_t __user *oldlenp,
  2494. void __user *newval,
  2495. size_t newlen,
  2496. void **context)
  2497. {
  2498. int delay;
  2499. if (newlen != sizeof(int))
  2500. return -EINVAL;
  2501. if (get_user(delay, (int __user *)newval))
  2502. return -EFAULT;
  2503. rt_cache_flush(delay);
  2504. return 0;
  2505. }
  2506. ctl_table ipv4_route_table[] = {
  2507. {
  2508. .ctl_name = NET_IPV4_ROUTE_FLUSH,
  2509. .procname = "flush",
  2510. .data = &flush_delay,
  2511. .maxlen = sizeof(int),
  2512. .mode = 0200,
  2513. .proc_handler = &ipv4_sysctl_rtcache_flush,
  2514. .strategy = &ipv4_sysctl_rtcache_flush_strategy,
  2515. },
  2516. {
  2517. .ctl_name = NET_IPV4_ROUTE_MIN_DELAY,
  2518. .procname = "min_delay",
  2519. .data = &ip_rt_min_delay,
  2520. .maxlen = sizeof(int),
  2521. .mode = 0644,
  2522. .proc_handler = &proc_dointvec_jiffies,
  2523. .strategy = &sysctl_jiffies,
  2524. },
  2525. {
  2526. .ctl_name = NET_IPV4_ROUTE_MAX_DELAY,
  2527. .procname = "max_delay",
  2528. .data = &ip_rt_max_delay,
  2529. .maxlen = sizeof(int),
  2530. .mode = 0644,
  2531. .proc_handler = &proc_dointvec_jiffies,
  2532. .strategy = &sysctl_jiffies,
  2533. },
  2534. {
  2535. .ctl_name = NET_IPV4_ROUTE_GC_THRESH,
  2536. .procname = "gc_thresh",
  2537. .data = &ipv4_dst_ops.gc_thresh,
  2538. .maxlen = sizeof(int),
  2539. .mode = 0644,
  2540. .proc_handler = &proc_dointvec,
  2541. },
  2542. {
  2543. .ctl_name = NET_IPV4_ROUTE_MAX_SIZE,
  2544. .procname = "max_size",
  2545. .data = &ip_rt_max_size,
  2546. .maxlen = sizeof(int),
  2547. .mode = 0644,
  2548. .proc_handler = &proc_dointvec,
  2549. },
  2550. {
  2551. /* Deprecated. Use gc_min_interval_ms */
  2552. .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL,
  2553. .procname = "gc_min_interval",
  2554. .data = &ip_rt_gc_min_interval,
  2555. .maxlen = sizeof(int),
  2556. .mode = 0644,
  2557. .proc_handler = &proc_dointvec_jiffies,
  2558. .strategy = &sysctl_jiffies,
  2559. },
  2560. {
  2561. .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
  2562. .procname = "gc_min_interval_ms",
  2563. .data = &ip_rt_gc_min_interval,
  2564. .maxlen = sizeof(int),
  2565. .mode = 0644,
  2566. .proc_handler = &proc_dointvec_ms_jiffies,
  2567. .strategy = &sysctl_ms_jiffies,
  2568. },
  2569. {
  2570. .ctl_name = NET_IPV4_ROUTE_GC_TIMEOUT,
  2571. .procname = "gc_timeout",
  2572. .data = &ip_rt_gc_timeout,
  2573. .maxlen = sizeof(int),
  2574. .mode = 0644,
  2575. .proc_handler = &proc_dointvec_jiffies,
  2576. .strategy = &sysctl_jiffies,
  2577. },
  2578. {
  2579. .ctl_name = NET_IPV4_ROUTE_GC_INTERVAL,
  2580. .procname = "gc_interval",
  2581. .data = &ip_rt_gc_interval,
  2582. .maxlen = sizeof(int),
  2583. .mode = 0644,
  2584. .proc_handler = &proc_dointvec_jiffies,
  2585. .strategy = &sysctl_jiffies,
  2586. },
  2587. {
  2588. .ctl_name = NET_IPV4_ROUTE_REDIRECT_LOAD,
  2589. .procname = "redirect_load",
  2590. .data = &ip_rt_redirect_load,
  2591. .maxlen = sizeof(int),
  2592. .mode = 0644,
  2593. .proc_handler = &proc_dointvec,
  2594. },
  2595. {
  2596. .ctl_name = NET_IPV4_ROUTE_REDIRECT_NUMBER,
  2597. .procname = "redirect_number",
  2598. .data = &ip_rt_redirect_number,
  2599. .maxlen = sizeof(int),
  2600. .mode = 0644,
  2601. .proc_handler = &proc_dointvec,
  2602. },
  2603. {
  2604. .ctl_name = NET_IPV4_ROUTE_REDIRECT_SILENCE,
  2605. .procname = "redirect_silence",
  2606. .data = &ip_rt_redirect_silence,
  2607. .maxlen = sizeof(int),
  2608. .mode = 0644,
  2609. .proc_handler = &proc_dointvec,
  2610. },
  2611. {
  2612. .ctl_name = NET_IPV4_ROUTE_ERROR_COST,
  2613. .procname = "error_cost",
  2614. .data = &ip_rt_error_cost,
  2615. .maxlen = sizeof(int),
  2616. .mode = 0644,
  2617. .proc_handler = &proc_dointvec,
  2618. },
  2619. {
  2620. .ctl_name = NET_IPV4_ROUTE_ERROR_BURST,
  2621. .procname = "error_burst",
  2622. .data = &ip_rt_error_burst,
  2623. .maxlen = sizeof(int),
  2624. .mode = 0644,
  2625. .proc_handler = &proc_dointvec,
  2626. },
  2627. {
  2628. .ctl_name = NET_IPV4_ROUTE_GC_ELASTICITY,
  2629. .procname = "gc_elasticity",
  2630. .data = &ip_rt_gc_elasticity,
  2631. .maxlen = sizeof(int),
  2632. .mode = 0644,
  2633. .proc_handler = &proc_dointvec,
  2634. },
  2635. {
  2636. .ctl_name = NET_IPV4_ROUTE_MTU_EXPIRES,
  2637. .procname = "mtu_expires",
  2638. .data = &ip_rt_mtu_expires,
  2639. .maxlen = sizeof(int),
  2640. .mode = 0644,
  2641. .proc_handler = &proc_dointvec_jiffies,
  2642. .strategy = &sysctl_jiffies,
  2643. },
  2644. {
  2645. .ctl_name = NET_IPV4_ROUTE_MIN_PMTU,
  2646. .procname = "min_pmtu",
  2647. .data = &ip_rt_min_pmtu,
  2648. .maxlen = sizeof(int),
  2649. .mode = 0644,
  2650. .proc_handler = &proc_dointvec,
  2651. },
  2652. {
  2653. .ctl_name = NET_IPV4_ROUTE_MIN_ADVMSS,
  2654. .procname = "min_adv_mss",
  2655. .data = &ip_rt_min_advmss,
  2656. .maxlen = sizeof(int),
  2657. .mode = 0644,
  2658. .proc_handler = &proc_dointvec,
  2659. },
  2660. {
  2661. .ctl_name = NET_IPV4_ROUTE_SECRET_INTERVAL,
  2662. .procname = "secret_interval",
  2663. .data = &ip_rt_secret_interval,
  2664. .maxlen = sizeof(int),
  2665. .mode = 0644,
  2666. .proc_handler = &proc_dointvec_jiffies,
  2667. .strategy = &sysctl_jiffies,
  2668. },
  2669. { .ctl_name = 0 }
  2670. };
  2671. #endif
  2672. #ifdef CONFIG_NET_CLS_ROUTE
  2673. struct ip_rt_acct *ip_rt_acct;
  2674. /* This code sucks. But you should have seen it before! --RR */
  2675. /* IP route accounting ptr for this logical cpu number. */
  2676. #define IP_RT_ACCT_CPU(i) (ip_rt_acct + i * 256)
  2677. #ifdef CONFIG_PROC_FS
  2678. static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
  2679. int length, int *eof, void *data)
  2680. {
  2681. unsigned int i;
  2682. if ((offset & 3) || (length & 3))
  2683. return -EIO;
  2684. if (offset >= sizeof(struct ip_rt_acct) * 256) {
  2685. *eof = 1;
  2686. return 0;
  2687. }
  2688. if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
  2689. length = sizeof(struct ip_rt_acct) * 256 - offset;
  2690. *eof = 1;
  2691. }
  2692. offset /= sizeof(u32);
  2693. if (length > 0) {
  2694. u32 *src = ((u32 *) IP_RT_ACCT_CPU(0)) + offset;
  2695. u32 *dst = (u32 *) buffer;
  2696. /* Copy first cpu. */
  2697. *start = buffer;
  2698. memcpy(dst, src, length);
  2699. /* Add the other cpus in, one int at a time */
  2700. for_each_possible_cpu(i) {
  2701. unsigned int j;
  2702. src = ((u32 *) IP_RT_ACCT_CPU(i)) + offset;
  2703. for (j = 0; j < length/4; j++)
  2704. dst[j] += src[j];
  2705. }
  2706. }
  2707. return length;
  2708. }
  2709. #endif /* CONFIG_PROC_FS */
  2710. #endif /* CONFIG_NET_CLS_ROUTE */
  2711. static __initdata unsigned long rhash_entries;
  2712. static int __init set_rhash_entries(char *str)
  2713. {
  2714. if (!str)
  2715. return 0;
  2716. rhash_entries = simple_strtoul(str, &str, 0);
  2717. return 1;
  2718. }
  2719. __setup("rhash_entries=", set_rhash_entries);
  2720. int __init ip_rt_init(void)
  2721. {
  2722. int rc = 0;
  2723. rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
  2724. (jiffies ^ (jiffies >> 7)));
  2725. #ifdef CONFIG_NET_CLS_ROUTE
  2726. {
  2727. int order;
  2728. for (order = 0;
  2729. (PAGE_SIZE << order) < 256 * sizeof(struct ip_rt_acct) * NR_CPUS; order++)
  2730. /* NOTHING */;
  2731. ip_rt_acct = (struct ip_rt_acct *)__get_free_pages(GFP_KERNEL, order);
  2732. if (!ip_rt_acct)
  2733. panic("IP: failed to allocate ip_rt_acct\n");
  2734. memset(ip_rt_acct, 0, PAGE_SIZE << order);
  2735. }
  2736. #endif
  2737. ipv4_dst_ops.kmem_cachep =
  2738. kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
  2739. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  2740. rt_hash_table = (struct rt_hash_bucket *)
  2741. alloc_large_system_hash("IP route cache",
  2742. sizeof(struct rt_hash_bucket),
  2743. rhash_entries,
  2744. (num_physpages >= 128 * 1024) ?
  2745. 15 : 17,
  2746. 0,
  2747. &rt_hash_log,
  2748. &rt_hash_mask,
  2749. 0);
  2750. memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
  2751. rt_hash_lock_init();
  2752. ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
  2753. ip_rt_max_size = (rt_hash_mask + 1) * 16;
  2754. devinet_init();
  2755. ip_fib_init();
  2756. init_timer(&rt_flush_timer);
  2757. rt_flush_timer.function = rt_run_flush;
  2758. init_timer(&rt_periodic_timer);
  2759. rt_periodic_timer.function = rt_check_expire;
  2760. init_timer(&rt_secret_timer);
  2761. rt_secret_timer.function = rt_secret_rebuild;
  2762. /* All the timers, started at system startup tend
  2763. to synchronize. Perturb it a bit.
  2764. */
  2765. rt_periodic_timer.expires = jiffies + net_random() % ip_rt_gc_interval +
  2766. ip_rt_gc_interval;
  2767. add_timer(&rt_periodic_timer);
  2768. rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
  2769. ip_rt_secret_interval;
  2770. add_timer(&rt_secret_timer);
  2771. #ifdef CONFIG_PROC_FS
  2772. {
  2773. struct proc_dir_entry *rtstat_pde = NULL; /* keep gcc happy */
  2774. if (!proc_net_fops_create("rt_cache", S_IRUGO, &rt_cache_seq_fops) ||
  2775. !(rtstat_pde = create_proc_entry("rt_cache", S_IRUGO,
  2776. proc_net_stat))) {
  2777. return -ENOMEM;
  2778. }
  2779. rtstat_pde->proc_fops = &rt_cpu_seq_fops;
  2780. }
  2781. #ifdef CONFIG_NET_CLS_ROUTE
  2782. create_proc_read_entry("rt_acct", 0, proc_net, ip_rt_acct_read, NULL);
  2783. #endif
  2784. #endif
  2785. #ifdef CONFIG_XFRM
  2786. xfrm_init();
  2787. xfrm4_init();
  2788. #endif
  2789. return rc;
  2790. }
  2791. EXPORT_SYMBOL(__ip_select_ident);
  2792. EXPORT_SYMBOL(ip_route_input);
  2793. EXPORT_SYMBOL(ip_route_output_key);