route.c 82 KB

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