route.c 87 KB

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