route.c 85 KB

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