route.c 79 KB

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