route.c 82 KB

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