route.c 77 KB

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