route.c 84 KB

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