route.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804
  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/string.h>
  72. #include <linux/socket.h>
  73. #include <linux/sockios.h>
  74. #include <linux/errno.h>
  75. #include <linux/in.h>
  76. #include <linux/inet.h>
  77. #include <linux/netdevice.h>
  78. #include <linux/proc_fs.h>
  79. #include <linux/init.h>
  80. #include <linux/skbuff.h>
  81. #include <linux/inetdevice.h>
  82. #include <linux/igmp.h>
  83. #include <linux/pkt_sched.h>
  84. #include <linux/mroute.h>
  85. #include <linux/netfilter_ipv4.h>
  86. #include <linux/random.h>
  87. #include <linux/rcupdate.h>
  88. #include <linux/times.h>
  89. #include <linux/slab.h>
  90. #include <linux/jhash.h>
  91. #include <net/dst.h>
  92. #include <net/net_namespace.h>
  93. #include <net/protocol.h>
  94. #include <net/ip.h>
  95. #include <net/route.h>
  96. #include <net/inetpeer.h>
  97. #include <net/sock.h>
  98. #include <net/ip_fib.h>
  99. #include <net/arp.h>
  100. #include <net/tcp.h>
  101. #include <net/icmp.h>
  102. #include <net/xfrm.h>
  103. #include <net/netevent.h>
  104. #include <net/rtnetlink.h>
  105. #ifdef CONFIG_SYSCTL
  106. #include <linux/sysctl.h>
  107. #include <linux/kmemleak.h>
  108. #endif
  109. #include <net/secure_seq.h>
  110. #define RT_FL_TOS(oldflp4) \
  111. ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
  112. #define RT_GC_TIMEOUT (300*HZ)
  113. static int ip_rt_max_size;
  114. static int ip_rt_redirect_number __read_mostly = 9;
  115. static int ip_rt_redirect_load __read_mostly = HZ / 50;
  116. static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1));
  117. static int ip_rt_error_cost __read_mostly = HZ;
  118. static int ip_rt_error_burst __read_mostly = 5 * HZ;
  119. static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ;
  120. static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20;
  121. static int ip_rt_min_advmss __read_mostly = 256;
  122. /*
  123. * Interface to generic destination cache.
  124. */
  125. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
  126. static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
  127. static unsigned int ipv4_mtu(const struct dst_entry *dst);
  128. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
  129. static void ipv4_link_failure(struct sk_buff *skb);
  130. static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  131. struct sk_buff *skb, u32 mtu);
  132. static void ip_do_redirect(struct dst_entry *dst, struct sock *sk,
  133. struct sk_buff *skb);
  134. static void ipv4_dst_destroy(struct dst_entry *dst);
  135. static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
  136. {
  137. WARN_ON(1);
  138. return NULL;
  139. }
  140. static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
  141. struct sk_buff *skb,
  142. const void *daddr);
  143. static struct dst_ops ipv4_dst_ops = {
  144. .family = AF_INET,
  145. .check = ipv4_dst_check,
  146. .default_advmss = ipv4_default_advmss,
  147. .mtu = ipv4_mtu,
  148. .cow_metrics = ipv4_cow_metrics,
  149. .destroy = ipv4_dst_destroy,
  150. .negative_advice = ipv4_negative_advice,
  151. .link_failure = ipv4_link_failure,
  152. .update_pmtu = ip_rt_update_pmtu,
  153. .redirect = ip_do_redirect,
  154. .local_out = __ip_local_out,
  155. .neigh_lookup = ipv4_neigh_lookup,
  156. };
  157. #define ECN_OR_COST(class) TC_PRIO_##class
  158. const __u8 ip_tos2prio[16] = {
  159. TC_PRIO_BESTEFFORT,
  160. ECN_OR_COST(BESTEFFORT),
  161. TC_PRIO_BESTEFFORT,
  162. ECN_OR_COST(BESTEFFORT),
  163. TC_PRIO_BULK,
  164. ECN_OR_COST(BULK),
  165. TC_PRIO_BULK,
  166. ECN_OR_COST(BULK),
  167. TC_PRIO_INTERACTIVE,
  168. ECN_OR_COST(INTERACTIVE),
  169. TC_PRIO_INTERACTIVE,
  170. ECN_OR_COST(INTERACTIVE),
  171. TC_PRIO_INTERACTIVE_BULK,
  172. ECN_OR_COST(INTERACTIVE_BULK),
  173. TC_PRIO_INTERACTIVE_BULK,
  174. ECN_OR_COST(INTERACTIVE_BULK)
  175. };
  176. EXPORT_SYMBOL(ip_tos2prio);
  177. static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
  178. #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field)
  179. #ifdef CONFIG_PROC_FS
  180. static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  181. {
  182. if (*pos)
  183. return NULL;
  184. return SEQ_START_TOKEN;
  185. }
  186. static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  187. {
  188. ++*pos;
  189. return NULL;
  190. }
  191. static void rt_cache_seq_stop(struct seq_file *seq, void *v)
  192. {
  193. }
  194. static int rt_cache_seq_show(struct seq_file *seq, void *v)
  195. {
  196. if (v == SEQ_START_TOKEN)
  197. seq_printf(seq, "%-127s\n",
  198. "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
  199. "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
  200. "HHUptod\tSpecDst");
  201. return 0;
  202. }
  203. static const struct seq_operations rt_cache_seq_ops = {
  204. .start = rt_cache_seq_start,
  205. .next = rt_cache_seq_next,
  206. .stop = rt_cache_seq_stop,
  207. .show = rt_cache_seq_show,
  208. };
  209. static int rt_cache_seq_open(struct inode *inode, struct file *file)
  210. {
  211. return seq_open(file, &rt_cache_seq_ops);
  212. }
  213. static const struct file_operations rt_cache_seq_fops = {
  214. .owner = THIS_MODULE,
  215. .open = rt_cache_seq_open,
  216. .read = seq_read,
  217. .llseek = seq_lseek,
  218. .release = seq_release,
  219. };
  220. static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
  221. {
  222. int cpu;
  223. if (*pos == 0)
  224. return SEQ_START_TOKEN;
  225. for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
  226. if (!cpu_possible(cpu))
  227. continue;
  228. *pos = cpu+1;
  229. return &per_cpu(rt_cache_stat, cpu);
  230. }
  231. return NULL;
  232. }
  233. static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  234. {
  235. int cpu;
  236. for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
  237. if (!cpu_possible(cpu))
  238. continue;
  239. *pos = cpu+1;
  240. return &per_cpu(rt_cache_stat, cpu);
  241. }
  242. return NULL;
  243. }
  244. static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
  245. {
  246. }
  247. static int rt_cpu_seq_show(struct seq_file *seq, void *v)
  248. {
  249. struct rt_cache_stat *st = v;
  250. if (v == SEQ_START_TOKEN) {
  251. 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");
  252. return 0;
  253. }
  254. seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
  255. " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
  256. dst_entries_get_slow(&ipv4_dst_ops),
  257. 0, /* st->in_hit */
  258. st->in_slow_tot,
  259. st->in_slow_mc,
  260. st->in_no_route,
  261. st->in_brd,
  262. st->in_martian_dst,
  263. st->in_martian_src,
  264. 0, /* st->out_hit */
  265. st->out_slow_tot,
  266. st->out_slow_mc,
  267. 0, /* st->gc_total */
  268. 0, /* st->gc_ignored */
  269. 0, /* st->gc_goal_miss */
  270. 0, /* st->gc_dst_overflow */
  271. 0, /* st->in_hlist_search */
  272. 0 /* st->out_hlist_search */
  273. );
  274. return 0;
  275. }
  276. static const struct seq_operations rt_cpu_seq_ops = {
  277. .start = rt_cpu_seq_start,
  278. .next = rt_cpu_seq_next,
  279. .stop = rt_cpu_seq_stop,
  280. .show = rt_cpu_seq_show,
  281. };
  282. static int rt_cpu_seq_open(struct inode *inode, struct file *file)
  283. {
  284. return seq_open(file, &rt_cpu_seq_ops);
  285. }
  286. static const struct file_operations rt_cpu_seq_fops = {
  287. .owner = THIS_MODULE,
  288. .open = rt_cpu_seq_open,
  289. .read = seq_read,
  290. .llseek = seq_lseek,
  291. .release = seq_release,
  292. };
  293. #ifdef CONFIG_IP_ROUTE_CLASSID
  294. static int rt_acct_proc_show(struct seq_file *m, void *v)
  295. {
  296. struct ip_rt_acct *dst, *src;
  297. unsigned int i, j;
  298. dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
  299. if (!dst)
  300. return -ENOMEM;
  301. for_each_possible_cpu(i) {
  302. src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
  303. for (j = 0; j < 256; j++) {
  304. dst[j].o_bytes += src[j].o_bytes;
  305. dst[j].o_packets += src[j].o_packets;
  306. dst[j].i_bytes += src[j].i_bytes;
  307. dst[j].i_packets += src[j].i_packets;
  308. }
  309. }
  310. seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
  311. kfree(dst);
  312. return 0;
  313. }
  314. static int rt_acct_proc_open(struct inode *inode, struct file *file)
  315. {
  316. return single_open(file, rt_acct_proc_show, NULL);
  317. }
  318. static const struct file_operations rt_acct_proc_fops = {
  319. .owner = THIS_MODULE,
  320. .open = rt_acct_proc_open,
  321. .read = seq_read,
  322. .llseek = seq_lseek,
  323. .release = single_release,
  324. };
  325. #endif
  326. static int __net_init ip_rt_do_proc_init(struct net *net)
  327. {
  328. struct proc_dir_entry *pde;
  329. pde = proc_create("rt_cache", S_IRUGO, net->proc_net,
  330. &rt_cache_seq_fops);
  331. if (!pde)
  332. goto err1;
  333. pde = proc_create("rt_cache", S_IRUGO,
  334. net->proc_net_stat, &rt_cpu_seq_fops);
  335. if (!pde)
  336. goto err2;
  337. #ifdef CONFIG_IP_ROUTE_CLASSID
  338. pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops);
  339. if (!pde)
  340. goto err3;
  341. #endif
  342. return 0;
  343. #ifdef CONFIG_IP_ROUTE_CLASSID
  344. err3:
  345. remove_proc_entry("rt_cache", net->proc_net_stat);
  346. #endif
  347. err2:
  348. remove_proc_entry("rt_cache", net->proc_net);
  349. err1:
  350. return -ENOMEM;
  351. }
  352. static void __net_exit ip_rt_do_proc_exit(struct net *net)
  353. {
  354. remove_proc_entry("rt_cache", net->proc_net_stat);
  355. remove_proc_entry("rt_cache", net->proc_net);
  356. #ifdef CONFIG_IP_ROUTE_CLASSID
  357. remove_proc_entry("rt_acct", net->proc_net);
  358. #endif
  359. }
  360. static struct pernet_operations ip_rt_proc_ops __net_initdata = {
  361. .init = ip_rt_do_proc_init,
  362. .exit = ip_rt_do_proc_exit,
  363. };
  364. static int __init ip_rt_proc_init(void)
  365. {
  366. return register_pernet_subsys(&ip_rt_proc_ops);
  367. }
  368. #else
  369. static inline int ip_rt_proc_init(void)
  370. {
  371. return 0;
  372. }
  373. #endif /* CONFIG_PROC_FS */
  374. static inline bool rt_is_expired(const struct rtable *rth)
  375. {
  376. return rth->rt_genid != rt_genid_ipv4(dev_net(rth->dst.dev));
  377. }
  378. void rt_cache_flush(struct net *net)
  379. {
  380. rt_genid_bump_ipv4(net);
  381. }
  382. static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
  383. struct sk_buff *skb,
  384. const void *daddr)
  385. {
  386. struct net_device *dev = dst->dev;
  387. const __be32 *pkey = daddr;
  388. const struct rtable *rt;
  389. struct neighbour *n;
  390. rt = (const struct rtable *) dst;
  391. if (rt->rt_gateway)
  392. pkey = (const __be32 *) &rt->rt_gateway;
  393. else if (skb)
  394. pkey = &ip_hdr(skb)->daddr;
  395. n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey);
  396. if (n)
  397. return n;
  398. return neigh_create(&arp_tbl, pkey, dev);
  399. }
  400. #define IP_IDENTS_SZ 2048u
  401. static atomic_t *ip_idents __read_mostly;
  402. static u32 *ip_tstamps __read_mostly;
  403. /* In order to protect privacy, we add a perturbation to identifiers
  404. * if one generator is seldom used. This makes hard for an attacker
  405. * to infer how many packets were sent between two points in time.
  406. */
  407. u32 ip_idents_reserve(u32 hash, int segs)
  408. {
  409. u32 *p_tstamp = ip_tstamps + hash % IP_IDENTS_SZ;
  410. atomic_t *p_id = ip_idents + hash % IP_IDENTS_SZ;
  411. u32 old = ACCESS_ONCE(*p_tstamp);
  412. u32 now = (u32)jiffies;
  413. u32 delta = 0;
  414. if (old != now && cmpxchg(p_tstamp, old, now) == old)
  415. delta = prandom_u32_max(now - old);
  416. return atomic_add_return(segs + delta, p_id) - segs;
  417. }
  418. EXPORT_SYMBOL(ip_idents_reserve);
  419. void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
  420. {
  421. static u32 ip_idents_hashrnd __read_mostly;
  422. u32 hash, id;
  423. net_get_random_once(&ip_idents_hashrnd, sizeof(ip_idents_hashrnd));
  424. hash = jhash_3words((__force u32)iph->daddr,
  425. (__force u32)iph->saddr,
  426. iph->protocol ^ net_hash_mix(net),
  427. ip_idents_hashrnd);
  428. id = ip_idents_reserve(hash, segs);
  429. iph->id = htons(id);
  430. }
  431. EXPORT_SYMBOL(__ip_select_ident);
  432. static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk,
  433. const struct iphdr *iph,
  434. int oif, u8 tos,
  435. u8 prot, u32 mark, int flow_flags)
  436. {
  437. if (sk) {
  438. const struct inet_sock *inet = inet_sk(sk);
  439. oif = sk->sk_bound_dev_if;
  440. mark = sk->sk_mark;
  441. tos = RT_CONN_FLAGS(sk);
  442. prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol;
  443. }
  444. flowi4_init_output(fl4, oif, mark, tos,
  445. RT_SCOPE_UNIVERSE, prot,
  446. flow_flags,
  447. iph->daddr, iph->saddr, 0, 0);
  448. }
  449. static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
  450. const struct sock *sk)
  451. {
  452. const struct iphdr *iph = ip_hdr(skb);
  453. int oif = skb->dev->ifindex;
  454. u8 tos = RT_TOS(iph->tos);
  455. u8 prot = iph->protocol;
  456. u32 mark = skb->mark;
  457. __build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0);
  458. }
  459. static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
  460. {
  461. const struct inet_sock *inet = inet_sk(sk);
  462. const struct ip_options_rcu *inet_opt;
  463. __be32 daddr = inet->inet_daddr;
  464. rcu_read_lock();
  465. inet_opt = rcu_dereference(inet->inet_opt);
  466. if (inet_opt && inet_opt->opt.srr)
  467. daddr = inet_opt->opt.faddr;
  468. flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
  469. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  470. inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
  471. inet_sk_flowi_flags(sk),
  472. daddr, inet->inet_saddr, 0, 0);
  473. rcu_read_unlock();
  474. }
  475. static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
  476. const struct sk_buff *skb)
  477. {
  478. if (skb)
  479. build_skb_flow_key(fl4, skb, sk);
  480. else
  481. build_sk_flow_key(fl4, sk);
  482. }
  483. static inline void rt_free(struct rtable *rt)
  484. {
  485. call_rcu(&rt->dst.rcu_head, dst_rcu_free);
  486. }
  487. static DEFINE_SPINLOCK(fnhe_lock);
  488. static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
  489. {
  490. struct rtable *rt;
  491. rt = rcu_dereference(fnhe->fnhe_rth_input);
  492. if (rt) {
  493. RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
  494. rt_free(rt);
  495. }
  496. rt = rcu_dereference(fnhe->fnhe_rth_output);
  497. if (rt) {
  498. RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
  499. rt_free(rt);
  500. }
  501. }
  502. static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
  503. {
  504. struct fib_nh_exception *fnhe, *oldest;
  505. oldest = rcu_dereference(hash->chain);
  506. for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe;
  507. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  508. if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp))
  509. oldest = fnhe;
  510. }
  511. fnhe_flush_routes(oldest);
  512. return oldest;
  513. }
  514. static inline u32 fnhe_hashfun(__be32 daddr)
  515. {
  516. static u32 fnhe_hashrnd __read_mostly;
  517. u32 hval;
  518. net_get_random_once(&fnhe_hashrnd, sizeof(fnhe_hashrnd));
  519. hval = jhash_1word((__force u32) daddr, fnhe_hashrnd);
  520. return hash_32(hval, FNHE_HASH_SHIFT);
  521. }
  522. static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
  523. {
  524. rt->rt_pmtu = fnhe->fnhe_pmtu;
  525. rt->dst.expires = fnhe->fnhe_expires;
  526. if (fnhe->fnhe_gw) {
  527. rt->rt_flags |= RTCF_REDIRECTED;
  528. rt->rt_gateway = fnhe->fnhe_gw;
  529. rt->rt_uses_gateway = 1;
  530. }
  531. }
  532. static void update_or_create_fnhe(struct fib_nh *nh, __be32 daddr, __be32 gw,
  533. u32 pmtu, unsigned long expires)
  534. {
  535. struct fnhe_hash_bucket *hash;
  536. struct fib_nh_exception *fnhe;
  537. struct rtable *rt;
  538. unsigned int i;
  539. int depth;
  540. u32 hval = fnhe_hashfun(daddr);
  541. spin_lock_bh(&fnhe_lock);
  542. hash = rcu_dereference(nh->nh_exceptions);
  543. if (!hash) {
  544. hash = kzalloc(FNHE_HASH_SIZE * sizeof(*hash), GFP_ATOMIC);
  545. if (!hash)
  546. goto out_unlock;
  547. rcu_assign_pointer(nh->nh_exceptions, hash);
  548. }
  549. hash += hval;
  550. depth = 0;
  551. for (fnhe = rcu_dereference(hash->chain); fnhe;
  552. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  553. if (fnhe->fnhe_daddr == daddr)
  554. break;
  555. depth++;
  556. }
  557. if (fnhe) {
  558. if (gw)
  559. fnhe->fnhe_gw = gw;
  560. if (pmtu) {
  561. fnhe->fnhe_pmtu = pmtu;
  562. fnhe->fnhe_expires = max(1UL, expires);
  563. }
  564. /* Update all cached dsts too */
  565. rt = rcu_dereference(fnhe->fnhe_rth_input);
  566. if (rt)
  567. fill_route_from_fnhe(rt, fnhe);
  568. rt = rcu_dereference(fnhe->fnhe_rth_output);
  569. if (rt)
  570. fill_route_from_fnhe(rt, fnhe);
  571. } else {
  572. if (depth > FNHE_RECLAIM_DEPTH)
  573. fnhe = fnhe_oldest(hash);
  574. else {
  575. fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
  576. if (!fnhe)
  577. goto out_unlock;
  578. fnhe->fnhe_next = hash->chain;
  579. rcu_assign_pointer(hash->chain, fnhe);
  580. }
  581. fnhe->fnhe_genid = fnhe_genid(dev_net(nh->nh_dev));
  582. fnhe->fnhe_daddr = daddr;
  583. fnhe->fnhe_gw = gw;
  584. fnhe->fnhe_pmtu = pmtu;
  585. fnhe->fnhe_expires = expires;
  586. /* Exception created; mark the cached routes for the nexthop
  587. * stale, so anyone caching it rechecks if this exception
  588. * applies to them.
  589. */
  590. rt = rcu_dereference(nh->nh_rth_input);
  591. if (rt)
  592. rt->dst.obsolete = DST_OBSOLETE_KILL;
  593. for_each_possible_cpu(i) {
  594. struct rtable __rcu **prt;
  595. prt = per_cpu_ptr(nh->nh_pcpu_rth_output, i);
  596. rt = rcu_dereference(*prt);
  597. if (rt)
  598. rt->dst.obsolete = DST_OBSOLETE_KILL;
  599. }
  600. }
  601. fnhe->fnhe_stamp = jiffies;
  602. out_unlock:
  603. spin_unlock_bh(&fnhe_lock);
  604. }
  605. static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
  606. bool kill_route)
  607. {
  608. __be32 new_gw = icmp_hdr(skb)->un.gateway;
  609. __be32 old_gw = ip_hdr(skb)->saddr;
  610. struct net_device *dev = skb->dev;
  611. struct in_device *in_dev;
  612. struct fib_result res;
  613. struct neighbour *n;
  614. struct net *net;
  615. switch (icmp_hdr(skb)->code & 7) {
  616. case ICMP_REDIR_NET:
  617. case ICMP_REDIR_NETTOS:
  618. case ICMP_REDIR_HOST:
  619. case ICMP_REDIR_HOSTTOS:
  620. break;
  621. default:
  622. return;
  623. }
  624. if (rt->rt_gateway != old_gw)
  625. return;
  626. in_dev = __in_dev_get_rcu(dev);
  627. if (!in_dev)
  628. return;
  629. net = dev_net(dev);
  630. if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
  631. ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
  632. ipv4_is_zeronet(new_gw))
  633. goto reject_redirect;
  634. if (!IN_DEV_SHARED_MEDIA(in_dev)) {
  635. if (!inet_addr_onlink(in_dev, new_gw, old_gw))
  636. goto reject_redirect;
  637. if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
  638. goto reject_redirect;
  639. } else {
  640. if (inet_addr_type(net, new_gw) != RTN_UNICAST)
  641. goto reject_redirect;
  642. }
  643. n = ipv4_neigh_lookup(&rt->dst, NULL, &new_gw);
  644. if (!IS_ERR(n)) {
  645. if (!(n->nud_state & NUD_VALID)) {
  646. neigh_event_send(n, NULL);
  647. } else {
  648. if (fib_lookup(net, fl4, &res, 0) == 0) {
  649. struct fib_nh *nh = &FIB_RES_NH(res);
  650. update_or_create_fnhe(nh, fl4->daddr, new_gw,
  651. 0, 0);
  652. }
  653. if (kill_route)
  654. rt->dst.obsolete = DST_OBSOLETE_KILL;
  655. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
  656. }
  657. neigh_release(n);
  658. }
  659. return;
  660. reject_redirect:
  661. #ifdef CONFIG_IP_ROUTE_VERBOSE
  662. if (IN_DEV_LOG_MARTIANS(in_dev)) {
  663. const struct iphdr *iph = (const struct iphdr *) skb->data;
  664. __be32 daddr = iph->daddr;
  665. __be32 saddr = iph->saddr;
  666. net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
  667. " Advised path = %pI4 -> %pI4\n",
  668. &old_gw, dev->name, &new_gw,
  669. &saddr, &daddr);
  670. }
  671. #endif
  672. ;
  673. }
  674. static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
  675. {
  676. struct rtable *rt;
  677. struct flowi4 fl4;
  678. const struct iphdr *iph = (const struct iphdr *) skb->data;
  679. int oif = skb->dev->ifindex;
  680. u8 tos = RT_TOS(iph->tos);
  681. u8 prot = iph->protocol;
  682. u32 mark = skb->mark;
  683. rt = (struct rtable *) dst;
  684. __build_flow_key(&fl4, sk, iph, oif, tos, prot, mark, 0);
  685. __ip_do_redirect(rt, skb, &fl4, true);
  686. }
  687. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
  688. {
  689. struct rtable *rt = (struct rtable *)dst;
  690. struct dst_entry *ret = dst;
  691. if (rt) {
  692. if (dst->obsolete > 0) {
  693. ip_rt_put(rt);
  694. ret = NULL;
  695. } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
  696. rt->dst.expires) {
  697. ip_rt_put(rt);
  698. ret = NULL;
  699. }
  700. }
  701. return ret;
  702. }
  703. /*
  704. * Algorithm:
  705. * 1. The first ip_rt_redirect_number redirects are sent
  706. * with exponential backoff, then we stop sending them at all,
  707. * assuming that the host ignores our redirects.
  708. * 2. If we did not see packets requiring redirects
  709. * during ip_rt_redirect_silence, we assume that the host
  710. * forgot redirected route and start to send redirects again.
  711. *
  712. * This algorithm is much cheaper and more intelligent than dumb load limiting
  713. * in icmp.c.
  714. *
  715. * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
  716. * and "frag. need" (breaks PMTU discovery) in icmp.c.
  717. */
  718. void ip_rt_send_redirect(struct sk_buff *skb)
  719. {
  720. struct rtable *rt = skb_rtable(skb);
  721. struct in_device *in_dev;
  722. struct inet_peer *peer;
  723. struct net *net;
  724. int log_martians;
  725. rcu_read_lock();
  726. in_dev = __in_dev_get_rcu(rt->dst.dev);
  727. if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
  728. rcu_read_unlock();
  729. return;
  730. }
  731. log_martians = IN_DEV_LOG_MARTIANS(in_dev);
  732. rcu_read_unlock();
  733. net = dev_net(rt->dst.dev);
  734. peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
  735. if (!peer) {
  736. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST,
  737. rt_nexthop(rt, ip_hdr(skb)->daddr));
  738. return;
  739. }
  740. /* No redirected packets during ip_rt_redirect_silence;
  741. * reset the algorithm.
  742. */
  743. if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence))
  744. peer->rate_tokens = 0;
  745. /* Too many ignored redirects; do not send anything
  746. * set dst.rate_last to the last seen redirected packet.
  747. */
  748. if (peer->rate_tokens >= ip_rt_redirect_number) {
  749. peer->rate_last = jiffies;
  750. goto out_put_peer;
  751. }
  752. /* Check for load limit; set rate_last to the latest sent
  753. * redirect.
  754. */
  755. if (peer->rate_tokens == 0 ||
  756. time_after(jiffies,
  757. (peer->rate_last +
  758. (ip_rt_redirect_load << peer->rate_tokens)))) {
  759. __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr);
  760. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw);
  761. peer->rate_last = jiffies;
  762. ++peer->rate_tokens;
  763. #ifdef CONFIG_IP_ROUTE_VERBOSE
  764. if (log_martians &&
  765. peer->rate_tokens == ip_rt_redirect_number)
  766. net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
  767. &ip_hdr(skb)->saddr, inet_iif(skb),
  768. &ip_hdr(skb)->daddr, &gw);
  769. #endif
  770. }
  771. out_put_peer:
  772. inet_putpeer(peer);
  773. }
  774. static int ip_error(struct sk_buff *skb)
  775. {
  776. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  777. struct rtable *rt = skb_rtable(skb);
  778. struct inet_peer *peer;
  779. unsigned long now;
  780. struct net *net;
  781. bool send;
  782. int code;
  783. /* IP on this device is disabled. */
  784. if (!in_dev)
  785. goto out;
  786. net = dev_net(rt->dst.dev);
  787. if (!IN_DEV_FORWARD(in_dev)) {
  788. switch (rt->dst.error) {
  789. case EHOSTUNREACH:
  790. IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS);
  791. break;
  792. case ENETUNREACH:
  793. IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
  794. break;
  795. }
  796. goto out;
  797. }
  798. switch (rt->dst.error) {
  799. case EINVAL:
  800. default:
  801. goto out;
  802. case EHOSTUNREACH:
  803. code = ICMP_HOST_UNREACH;
  804. break;
  805. case ENETUNREACH:
  806. code = ICMP_NET_UNREACH;
  807. IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
  808. break;
  809. case EACCES:
  810. code = ICMP_PKT_FILTERED;
  811. break;
  812. }
  813. peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 1);
  814. send = true;
  815. if (peer) {
  816. now = jiffies;
  817. peer->rate_tokens += now - peer->rate_last;
  818. if (peer->rate_tokens > ip_rt_error_burst)
  819. peer->rate_tokens = ip_rt_error_burst;
  820. peer->rate_last = now;
  821. if (peer->rate_tokens >= ip_rt_error_cost)
  822. peer->rate_tokens -= ip_rt_error_cost;
  823. else
  824. send = false;
  825. inet_putpeer(peer);
  826. }
  827. if (send)
  828. icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
  829. out: kfree_skb(skb);
  830. return 0;
  831. }
  832. static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
  833. {
  834. struct dst_entry *dst = &rt->dst;
  835. struct fib_result res;
  836. if (dst_metric_locked(dst, RTAX_MTU))
  837. return;
  838. if (ipv4_mtu(dst) < mtu)
  839. return;
  840. if (mtu < ip_rt_min_pmtu)
  841. mtu = ip_rt_min_pmtu;
  842. if (rt->rt_pmtu == mtu &&
  843. time_before(jiffies, dst->expires - ip_rt_mtu_expires / 2))
  844. return;
  845. rcu_read_lock();
  846. if (fib_lookup(dev_net(dst->dev), fl4, &res, 0) == 0) {
  847. struct fib_nh *nh = &FIB_RES_NH(res);
  848. update_or_create_fnhe(nh, fl4->daddr, 0, mtu,
  849. jiffies + ip_rt_mtu_expires);
  850. }
  851. rcu_read_unlock();
  852. }
  853. static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  854. struct sk_buff *skb, u32 mtu)
  855. {
  856. struct rtable *rt = (struct rtable *) dst;
  857. struct flowi4 fl4;
  858. ip_rt_build_flow_key(&fl4, sk, skb);
  859. __ip_rt_update_pmtu(rt, &fl4, mtu);
  860. }
  861. void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
  862. int oif, u32 mark, u8 protocol, int flow_flags)
  863. {
  864. const struct iphdr *iph = (const struct iphdr *) skb->data;
  865. struct flowi4 fl4;
  866. struct rtable *rt;
  867. if (!mark)
  868. mark = IP4_REPLY_MARK(net, skb->mark);
  869. __build_flow_key(&fl4, NULL, iph, oif,
  870. RT_TOS(iph->tos), protocol, mark, flow_flags);
  871. rt = __ip_route_output_key(net, &fl4);
  872. if (!IS_ERR(rt)) {
  873. __ip_rt_update_pmtu(rt, &fl4, mtu);
  874. ip_rt_put(rt);
  875. }
  876. }
  877. EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
  878. static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
  879. {
  880. const struct iphdr *iph = (const struct iphdr *) skb->data;
  881. struct flowi4 fl4;
  882. struct rtable *rt;
  883. __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
  884. if (!fl4.flowi4_mark)
  885. fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark);
  886. rt = __ip_route_output_key(sock_net(sk), &fl4);
  887. if (!IS_ERR(rt)) {
  888. __ip_rt_update_pmtu(rt, &fl4, mtu);
  889. ip_rt_put(rt);
  890. }
  891. }
  892. void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
  893. {
  894. const struct iphdr *iph = (const struct iphdr *) skb->data;
  895. struct flowi4 fl4;
  896. struct rtable *rt;
  897. struct dst_entry *odst = NULL;
  898. bool new = false;
  899. bh_lock_sock(sk);
  900. if (!ip_sk_accept_pmtu(sk))
  901. goto out;
  902. odst = sk_dst_get(sk);
  903. if (sock_owned_by_user(sk) || !odst) {
  904. __ipv4_sk_update_pmtu(skb, sk, mtu);
  905. goto out;
  906. }
  907. __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
  908. rt = (struct rtable *)odst;
  909. if (odst->obsolete && !odst->ops->check(odst, 0)) {
  910. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  911. if (IS_ERR(rt))
  912. goto out;
  913. new = true;
  914. }
  915. __ip_rt_update_pmtu((struct rtable *) rt->dst.path, &fl4, mtu);
  916. if (!dst_check(&rt->dst, 0)) {
  917. if (new)
  918. dst_release(&rt->dst);
  919. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  920. if (IS_ERR(rt))
  921. goto out;
  922. new = true;
  923. }
  924. if (new)
  925. sk_dst_set(sk, &rt->dst);
  926. out:
  927. bh_unlock_sock(sk);
  928. dst_release(odst);
  929. }
  930. EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
  931. void ipv4_redirect(struct sk_buff *skb, struct net *net,
  932. int oif, u32 mark, u8 protocol, int flow_flags)
  933. {
  934. const struct iphdr *iph = (const struct iphdr *) skb->data;
  935. struct flowi4 fl4;
  936. struct rtable *rt;
  937. __build_flow_key(&fl4, NULL, iph, oif,
  938. RT_TOS(iph->tos), protocol, mark, flow_flags);
  939. rt = __ip_route_output_key(net, &fl4);
  940. if (!IS_ERR(rt)) {
  941. __ip_do_redirect(rt, skb, &fl4, false);
  942. ip_rt_put(rt);
  943. }
  944. }
  945. EXPORT_SYMBOL_GPL(ipv4_redirect);
  946. void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
  947. {
  948. const struct iphdr *iph = (const struct iphdr *) skb->data;
  949. struct flowi4 fl4;
  950. struct rtable *rt;
  951. __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
  952. rt = __ip_route_output_key(sock_net(sk), &fl4);
  953. if (!IS_ERR(rt)) {
  954. __ip_do_redirect(rt, skb, &fl4, false);
  955. ip_rt_put(rt);
  956. }
  957. }
  958. EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
  959. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
  960. {
  961. struct rtable *rt = (struct rtable *) dst;
  962. /* All IPV4 dsts are created with ->obsolete set to the value
  963. * DST_OBSOLETE_FORCE_CHK which forces validation calls down
  964. * into this function always.
  965. *
  966. * When a PMTU/redirect information update invalidates a route,
  967. * this is indicated by setting obsolete to DST_OBSOLETE_KILL or
  968. * DST_OBSOLETE_DEAD by dst_free().
  969. */
  970. if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt))
  971. return NULL;
  972. return dst;
  973. }
  974. static void ipv4_link_failure(struct sk_buff *skb)
  975. {
  976. struct rtable *rt;
  977. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
  978. rt = skb_rtable(skb);
  979. if (rt)
  980. dst_set_expires(&rt->dst, 0);
  981. }
  982. static int ip_rt_bug(struct sock *sk, struct sk_buff *skb)
  983. {
  984. pr_debug("%s: %pI4 -> %pI4, %s\n",
  985. __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
  986. skb->dev ? skb->dev->name : "?");
  987. kfree_skb(skb);
  988. WARN_ON(1);
  989. return 0;
  990. }
  991. /*
  992. We do not cache source address of outgoing interface,
  993. because it is used only by IP RR, TS and SRR options,
  994. so that it out of fast path.
  995. BTW remember: "addr" is allowed to be not aligned
  996. in IP options!
  997. */
  998. void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
  999. {
  1000. __be32 src;
  1001. if (rt_is_output_route(rt))
  1002. src = ip_hdr(skb)->saddr;
  1003. else {
  1004. struct fib_result res;
  1005. struct flowi4 fl4;
  1006. struct iphdr *iph;
  1007. iph = ip_hdr(skb);
  1008. memset(&fl4, 0, sizeof(fl4));
  1009. fl4.daddr = iph->daddr;
  1010. fl4.saddr = iph->saddr;
  1011. fl4.flowi4_tos = RT_TOS(iph->tos);
  1012. fl4.flowi4_oif = rt->dst.dev->ifindex;
  1013. fl4.flowi4_iif = skb->dev->ifindex;
  1014. fl4.flowi4_mark = skb->mark;
  1015. rcu_read_lock();
  1016. if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0)
  1017. src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res);
  1018. else
  1019. src = inet_select_addr(rt->dst.dev,
  1020. rt_nexthop(rt, iph->daddr),
  1021. RT_SCOPE_UNIVERSE);
  1022. rcu_read_unlock();
  1023. }
  1024. memcpy(addr, &src, 4);
  1025. }
  1026. #ifdef CONFIG_IP_ROUTE_CLASSID
  1027. static void set_class_tag(struct rtable *rt, u32 tag)
  1028. {
  1029. if (!(rt->dst.tclassid & 0xFFFF))
  1030. rt->dst.tclassid |= tag & 0xFFFF;
  1031. if (!(rt->dst.tclassid & 0xFFFF0000))
  1032. rt->dst.tclassid |= tag & 0xFFFF0000;
  1033. }
  1034. #endif
  1035. static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
  1036. {
  1037. unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS);
  1038. if (advmss == 0) {
  1039. advmss = max_t(unsigned int, dst->dev->mtu - 40,
  1040. ip_rt_min_advmss);
  1041. if (advmss > 65535 - 40)
  1042. advmss = 65535 - 40;
  1043. }
  1044. return advmss;
  1045. }
  1046. static unsigned int ipv4_mtu(const struct dst_entry *dst)
  1047. {
  1048. const struct rtable *rt = (const struct rtable *) dst;
  1049. unsigned int mtu = rt->rt_pmtu;
  1050. if (!mtu || time_after_eq(jiffies, rt->dst.expires))
  1051. mtu = dst_metric_raw(dst, RTAX_MTU);
  1052. if (mtu)
  1053. return mtu;
  1054. mtu = dst->dev->mtu;
  1055. if (unlikely(dst_metric_locked(dst, RTAX_MTU))) {
  1056. if (rt->rt_uses_gateway && mtu > 576)
  1057. mtu = 576;
  1058. }
  1059. return min_t(unsigned int, mtu, IP_MAX_MTU);
  1060. }
  1061. static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr)
  1062. {
  1063. struct fnhe_hash_bucket *hash = rcu_dereference(nh->nh_exceptions);
  1064. struct fib_nh_exception *fnhe;
  1065. u32 hval;
  1066. if (!hash)
  1067. return NULL;
  1068. hval = fnhe_hashfun(daddr);
  1069. for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
  1070. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  1071. if (fnhe->fnhe_daddr == daddr)
  1072. return fnhe;
  1073. }
  1074. return NULL;
  1075. }
  1076. static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
  1077. __be32 daddr)
  1078. {
  1079. bool ret = false;
  1080. spin_lock_bh(&fnhe_lock);
  1081. if (daddr == fnhe->fnhe_daddr) {
  1082. struct rtable __rcu **porig;
  1083. struct rtable *orig;
  1084. int genid = fnhe_genid(dev_net(rt->dst.dev));
  1085. if (rt_is_input_route(rt))
  1086. porig = &fnhe->fnhe_rth_input;
  1087. else
  1088. porig = &fnhe->fnhe_rth_output;
  1089. orig = rcu_dereference(*porig);
  1090. if (fnhe->fnhe_genid != genid) {
  1091. fnhe->fnhe_genid = genid;
  1092. fnhe->fnhe_gw = 0;
  1093. fnhe->fnhe_pmtu = 0;
  1094. fnhe->fnhe_expires = 0;
  1095. fnhe_flush_routes(fnhe);
  1096. orig = NULL;
  1097. }
  1098. fill_route_from_fnhe(rt, fnhe);
  1099. if (!rt->rt_gateway)
  1100. rt->rt_gateway = daddr;
  1101. if (!(rt->dst.flags & DST_NOCACHE)) {
  1102. rcu_assign_pointer(*porig, rt);
  1103. if (orig)
  1104. rt_free(orig);
  1105. ret = true;
  1106. }
  1107. fnhe->fnhe_stamp = jiffies;
  1108. }
  1109. spin_unlock_bh(&fnhe_lock);
  1110. return ret;
  1111. }
  1112. static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
  1113. {
  1114. struct rtable *orig, *prev, **p;
  1115. bool ret = true;
  1116. if (rt_is_input_route(rt)) {
  1117. p = (struct rtable **)&nh->nh_rth_input;
  1118. } else {
  1119. p = (struct rtable **)raw_cpu_ptr(nh->nh_pcpu_rth_output);
  1120. }
  1121. orig = *p;
  1122. prev = cmpxchg(p, orig, rt);
  1123. if (prev == orig) {
  1124. if (orig)
  1125. rt_free(orig);
  1126. } else
  1127. ret = false;
  1128. return ret;
  1129. }
  1130. struct uncached_list {
  1131. spinlock_t lock;
  1132. struct list_head head;
  1133. };
  1134. static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
  1135. static void rt_add_uncached_list(struct rtable *rt)
  1136. {
  1137. struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
  1138. rt->rt_uncached_list = ul;
  1139. spin_lock_bh(&ul->lock);
  1140. list_add_tail(&rt->rt_uncached, &ul->head);
  1141. spin_unlock_bh(&ul->lock);
  1142. }
  1143. static void ipv4_dst_destroy(struct dst_entry *dst)
  1144. {
  1145. struct rtable *rt = (struct rtable *) dst;
  1146. if (!list_empty(&rt->rt_uncached)) {
  1147. struct uncached_list *ul = rt->rt_uncached_list;
  1148. spin_lock_bh(&ul->lock);
  1149. list_del(&rt->rt_uncached);
  1150. spin_unlock_bh(&ul->lock);
  1151. }
  1152. }
  1153. void rt_flush_dev(struct net_device *dev)
  1154. {
  1155. struct net *net = dev_net(dev);
  1156. struct rtable *rt;
  1157. int cpu;
  1158. for_each_possible_cpu(cpu) {
  1159. struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
  1160. spin_lock_bh(&ul->lock);
  1161. list_for_each_entry(rt, &ul->head, rt_uncached) {
  1162. if (rt->dst.dev != dev)
  1163. continue;
  1164. rt->dst.dev = net->loopback_dev;
  1165. dev_hold(rt->dst.dev);
  1166. dev_put(dev);
  1167. }
  1168. spin_unlock_bh(&ul->lock);
  1169. }
  1170. }
  1171. static bool rt_cache_valid(const struct rtable *rt)
  1172. {
  1173. return rt &&
  1174. rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
  1175. !rt_is_expired(rt);
  1176. }
  1177. static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
  1178. const struct fib_result *res,
  1179. struct fib_nh_exception *fnhe,
  1180. struct fib_info *fi, u16 type, u32 itag)
  1181. {
  1182. bool cached = false;
  1183. if (fi) {
  1184. struct fib_nh *nh = &FIB_RES_NH(*res);
  1185. if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK) {
  1186. rt->rt_gateway = nh->nh_gw;
  1187. rt->rt_uses_gateway = 1;
  1188. }
  1189. dst_init_metrics(&rt->dst, fi->fib_metrics, true);
  1190. #ifdef CONFIG_IP_ROUTE_CLASSID
  1191. rt->dst.tclassid = nh->nh_tclassid;
  1192. #endif
  1193. if (unlikely(fnhe))
  1194. cached = rt_bind_exception(rt, fnhe, daddr);
  1195. else if (!(rt->dst.flags & DST_NOCACHE))
  1196. cached = rt_cache_route(nh, rt);
  1197. if (unlikely(!cached)) {
  1198. /* Routes we intend to cache in nexthop exception or
  1199. * FIB nexthop have the DST_NOCACHE bit clear.
  1200. * However, if we are unsuccessful at storing this
  1201. * route into the cache we really need to set it.
  1202. */
  1203. rt->dst.flags |= DST_NOCACHE;
  1204. if (!rt->rt_gateway)
  1205. rt->rt_gateway = daddr;
  1206. rt_add_uncached_list(rt);
  1207. }
  1208. } else
  1209. rt_add_uncached_list(rt);
  1210. #ifdef CONFIG_IP_ROUTE_CLASSID
  1211. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1212. set_class_tag(rt, res->tclassid);
  1213. #endif
  1214. set_class_tag(rt, itag);
  1215. #endif
  1216. }
  1217. static struct rtable *rt_dst_alloc(struct net_device *dev,
  1218. bool nopolicy, bool noxfrm, bool will_cache)
  1219. {
  1220. return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
  1221. (will_cache ? 0 : (DST_HOST | DST_NOCACHE)) |
  1222. (nopolicy ? DST_NOPOLICY : 0) |
  1223. (noxfrm ? DST_NOXFRM : 0));
  1224. }
  1225. /* called in rcu_read_lock() section */
  1226. static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1227. u8 tos, struct net_device *dev, int our)
  1228. {
  1229. struct rtable *rth;
  1230. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1231. u32 itag = 0;
  1232. int err;
  1233. /* Primary sanity checks. */
  1234. if (!in_dev)
  1235. return -EINVAL;
  1236. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
  1237. skb->protocol != htons(ETH_P_IP))
  1238. goto e_inval;
  1239. if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
  1240. if (ipv4_is_loopback(saddr))
  1241. goto e_inval;
  1242. if (ipv4_is_zeronet(saddr)) {
  1243. if (!ipv4_is_local_multicast(daddr))
  1244. goto e_inval;
  1245. } else {
  1246. err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
  1247. in_dev, &itag);
  1248. if (err < 0)
  1249. goto e_err;
  1250. }
  1251. rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
  1252. IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
  1253. if (!rth)
  1254. goto e_nobufs;
  1255. #ifdef CONFIG_IP_ROUTE_CLASSID
  1256. rth->dst.tclassid = itag;
  1257. #endif
  1258. rth->dst.output = ip_rt_bug;
  1259. rth->rt_genid = rt_genid_ipv4(dev_net(dev));
  1260. rth->rt_flags = RTCF_MULTICAST;
  1261. rth->rt_type = RTN_MULTICAST;
  1262. rth->rt_is_input= 1;
  1263. rth->rt_iif = 0;
  1264. rth->rt_pmtu = 0;
  1265. rth->rt_gateway = 0;
  1266. rth->rt_uses_gateway = 0;
  1267. INIT_LIST_HEAD(&rth->rt_uncached);
  1268. if (our) {
  1269. rth->dst.input= ip_local_deliver;
  1270. rth->rt_flags |= RTCF_LOCAL;
  1271. }
  1272. #ifdef CONFIG_IP_MROUTE
  1273. if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
  1274. rth->dst.input = ip_mr_input;
  1275. #endif
  1276. RT_CACHE_STAT_INC(in_slow_mc);
  1277. skb_dst_set(skb, &rth->dst);
  1278. return 0;
  1279. e_nobufs:
  1280. return -ENOBUFS;
  1281. e_inval:
  1282. return -EINVAL;
  1283. e_err:
  1284. return err;
  1285. }
  1286. static void ip_handle_martian_source(struct net_device *dev,
  1287. struct in_device *in_dev,
  1288. struct sk_buff *skb,
  1289. __be32 daddr,
  1290. __be32 saddr)
  1291. {
  1292. RT_CACHE_STAT_INC(in_martian_src);
  1293. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1294. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
  1295. /*
  1296. * RFC1812 recommendation, if source is martian,
  1297. * the only hint is MAC header.
  1298. */
  1299. pr_warn("martian source %pI4 from %pI4, on dev %s\n",
  1300. &daddr, &saddr, dev->name);
  1301. if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
  1302. print_hex_dump(KERN_WARNING, "ll header: ",
  1303. DUMP_PREFIX_OFFSET, 16, 1,
  1304. skb_mac_header(skb),
  1305. dev->hard_header_len, true);
  1306. }
  1307. }
  1308. #endif
  1309. }
  1310. /* called in rcu_read_lock() section */
  1311. static int __mkroute_input(struct sk_buff *skb,
  1312. const struct fib_result *res,
  1313. struct in_device *in_dev,
  1314. __be32 daddr, __be32 saddr, u32 tos)
  1315. {
  1316. struct fib_nh_exception *fnhe;
  1317. struct rtable *rth;
  1318. int err;
  1319. struct in_device *out_dev;
  1320. unsigned int flags = 0;
  1321. bool do_cache;
  1322. u32 itag = 0;
  1323. /* get a working reference to the output device */
  1324. out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res));
  1325. if (!out_dev) {
  1326. net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
  1327. return -EINVAL;
  1328. }
  1329. err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
  1330. in_dev->dev, in_dev, &itag);
  1331. if (err < 0) {
  1332. ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
  1333. saddr);
  1334. goto cleanup;
  1335. }
  1336. do_cache = res->fi && !itag;
  1337. if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
  1338. skb->protocol == htons(ETH_P_IP) &&
  1339. (IN_DEV_SHARED_MEDIA(out_dev) ||
  1340. inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
  1341. IPCB(skb)->flags |= IPSKB_DOREDIRECT;
  1342. if (skb->protocol != htons(ETH_P_IP)) {
  1343. /* Not IP (i.e. ARP). Do not create route, if it is
  1344. * invalid for proxy arp. DNAT routes are always valid.
  1345. *
  1346. * Proxy arp feature have been extended to allow, ARP
  1347. * replies back to the same interface, to support
  1348. * Private VLAN switch technologies. See arp.c.
  1349. */
  1350. if (out_dev == in_dev &&
  1351. IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
  1352. err = -EINVAL;
  1353. goto cleanup;
  1354. }
  1355. }
  1356. fnhe = find_exception(&FIB_RES_NH(*res), daddr);
  1357. if (do_cache) {
  1358. if (fnhe)
  1359. rth = rcu_dereference(fnhe->fnhe_rth_input);
  1360. else
  1361. rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input);
  1362. if (rt_cache_valid(rth)) {
  1363. skb_dst_set_noref(skb, &rth->dst);
  1364. goto out;
  1365. }
  1366. }
  1367. rth = rt_dst_alloc(out_dev->dev,
  1368. IN_DEV_CONF_GET(in_dev, NOPOLICY),
  1369. IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
  1370. if (!rth) {
  1371. err = -ENOBUFS;
  1372. goto cleanup;
  1373. }
  1374. rth->rt_genid = rt_genid_ipv4(dev_net(rth->dst.dev));
  1375. rth->rt_flags = flags;
  1376. rth->rt_type = res->type;
  1377. rth->rt_is_input = 1;
  1378. rth->rt_iif = 0;
  1379. rth->rt_pmtu = 0;
  1380. rth->rt_gateway = 0;
  1381. rth->rt_uses_gateway = 0;
  1382. INIT_LIST_HEAD(&rth->rt_uncached);
  1383. RT_CACHE_STAT_INC(in_slow_tot);
  1384. rth->dst.input = ip_forward;
  1385. rth->dst.output = ip_output;
  1386. rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag);
  1387. skb_dst_set(skb, &rth->dst);
  1388. out:
  1389. err = 0;
  1390. cleanup:
  1391. return err;
  1392. }
  1393. static int ip_mkroute_input(struct sk_buff *skb,
  1394. struct fib_result *res,
  1395. const struct flowi4 *fl4,
  1396. struct in_device *in_dev,
  1397. __be32 daddr, __be32 saddr, u32 tos)
  1398. {
  1399. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1400. if (res->fi && res->fi->fib_nhs > 1)
  1401. fib_select_multipath(res);
  1402. #endif
  1403. /* create a routing cache entry */
  1404. return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
  1405. }
  1406. /*
  1407. * NOTE. We drop all the packets that has local source
  1408. * addresses, because every properly looped back packet
  1409. * must have correct destination already attached by output routine.
  1410. *
  1411. * Such approach solves two big problems:
  1412. * 1. Not simplex devices are handled properly.
  1413. * 2. IP spoofing attempts are filtered with 100% of guarantee.
  1414. * called with rcu_read_lock()
  1415. */
  1416. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1417. u8 tos, struct net_device *dev)
  1418. {
  1419. struct fib_result res;
  1420. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1421. struct flowi4 fl4;
  1422. unsigned int flags = 0;
  1423. u32 itag = 0;
  1424. struct rtable *rth;
  1425. int err = -EINVAL;
  1426. struct net *net = dev_net(dev);
  1427. bool do_cache;
  1428. /* IP on this device is disabled. */
  1429. if (!in_dev)
  1430. goto out;
  1431. /* Check for the most weird martians, which can be not detected
  1432. by fib_lookup.
  1433. */
  1434. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
  1435. goto martian_source;
  1436. res.fi = NULL;
  1437. if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
  1438. goto brd_input;
  1439. /* Accept zero addresses only to limited broadcast;
  1440. * I even do not know to fix it or not. Waiting for complains :-)
  1441. */
  1442. if (ipv4_is_zeronet(saddr))
  1443. goto martian_source;
  1444. if (ipv4_is_zeronet(daddr))
  1445. goto martian_destination;
  1446. /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
  1447. * and call it once if daddr or/and saddr are loopback addresses
  1448. */
  1449. if (ipv4_is_loopback(daddr)) {
  1450. if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
  1451. goto martian_destination;
  1452. } else if (ipv4_is_loopback(saddr)) {
  1453. if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
  1454. goto martian_source;
  1455. }
  1456. /*
  1457. * Now we are ready to route packet.
  1458. */
  1459. fl4.flowi4_oif = 0;
  1460. fl4.flowi4_iif = dev->ifindex;
  1461. fl4.flowi4_mark = skb->mark;
  1462. fl4.flowi4_tos = tos;
  1463. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  1464. fl4.daddr = daddr;
  1465. fl4.saddr = saddr;
  1466. err = fib_lookup(net, &fl4, &res, 0);
  1467. if (err != 0) {
  1468. if (!IN_DEV_FORWARD(in_dev))
  1469. err = -EHOSTUNREACH;
  1470. goto no_route;
  1471. }
  1472. if (res.type == RTN_BROADCAST)
  1473. goto brd_input;
  1474. if (res.type == RTN_LOCAL) {
  1475. err = fib_validate_source(skb, saddr, daddr, tos,
  1476. 0, dev, in_dev, &itag);
  1477. if (err < 0)
  1478. goto martian_source_keep_err;
  1479. goto local_input;
  1480. }
  1481. if (!IN_DEV_FORWARD(in_dev)) {
  1482. err = -EHOSTUNREACH;
  1483. goto no_route;
  1484. }
  1485. if (res.type != RTN_UNICAST)
  1486. goto martian_destination;
  1487. err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos);
  1488. out: return err;
  1489. brd_input:
  1490. if (skb->protocol != htons(ETH_P_IP))
  1491. goto e_inval;
  1492. if (!ipv4_is_zeronet(saddr)) {
  1493. err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
  1494. in_dev, &itag);
  1495. if (err < 0)
  1496. goto martian_source_keep_err;
  1497. }
  1498. flags |= RTCF_BROADCAST;
  1499. res.type = RTN_BROADCAST;
  1500. RT_CACHE_STAT_INC(in_brd);
  1501. local_input:
  1502. do_cache = false;
  1503. if (res.fi) {
  1504. if (!itag) {
  1505. rth = rcu_dereference(FIB_RES_NH(res).nh_rth_input);
  1506. if (rt_cache_valid(rth)) {
  1507. skb_dst_set_noref(skb, &rth->dst);
  1508. err = 0;
  1509. goto out;
  1510. }
  1511. do_cache = true;
  1512. }
  1513. }
  1514. rth = rt_dst_alloc(net->loopback_dev,
  1515. IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache);
  1516. if (!rth)
  1517. goto e_nobufs;
  1518. rth->dst.input= ip_local_deliver;
  1519. rth->dst.output= ip_rt_bug;
  1520. #ifdef CONFIG_IP_ROUTE_CLASSID
  1521. rth->dst.tclassid = itag;
  1522. #endif
  1523. rth->rt_genid = rt_genid_ipv4(net);
  1524. rth->rt_flags = flags|RTCF_LOCAL;
  1525. rth->rt_type = res.type;
  1526. rth->rt_is_input = 1;
  1527. rth->rt_iif = 0;
  1528. rth->rt_pmtu = 0;
  1529. rth->rt_gateway = 0;
  1530. rth->rt_uses_gateway = 0;
  1531. INIT_LIST_HEAD(&rth->rt_uncached);
  1532. RT_CACHE_STAT_INC(in_slow_tot);
  1533. if (res.type == RTN_UNREACHABLE) {
  1534. rth->dst.input= ip_error;
  1535. rth->dst.error= -err;
  1536. rth->rt_flags &= ~RTCF_LOCAL;
  1537. }
  1538. if (do_cache) {
  1539. if (unlikely(!rt_cache_route(&FIB_RES_NH(res), rth))) {
  1540. rth->dst.flags |= DST_NOCACHE;
  1541. rt_add_uncached_list(rth);
  1542. }
  1543. }
  1544. skb_dst_set(skb, &rth->dst);
  1545. err = 0;
  1546. goto out;
  1547. no_route:
  1548. RT_CACHE_STAT_INC(in_no_route);
  1549. res.type = RTN_UNREACHABLE;
  1550. res.fi = NULL;
  1551. goto local_input;
  1552. /*
  1553. * Do not cache martian addresses: they should be logged (RFC1812)
  1554. */
  1555. martian_destination:
  1556. RT_CACHE_STAT_INC(in_martian_dst);
  1557. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1558. if (IN_DEV_LOG_MARTIANS(in_dev))
  1559. net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
  1560. &daddr, &saddr, dev->name);
  1561. #endif
  1562. e_inval:
  1563. err = -EINVAL;
  1564. goto out;
  1565. e_nobufs:
  1566. err = -ENOBUFS;
  1567. goto out;
  1568. martian_source:
  1569. err = -EINVAL;
  1570. martian_source_keep_err:
  1571. ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
  1572. goto out;
  1573. }
  1574. int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1575. u8 tos, struct net_device *dev)
  1576. {
  1577. int res;
  1578. rcu_read_lock();
  1579. /* Multicast recognition logic is moved from route cache to here.
  1580. The problem was that too many Ethernet cards have broken/missing
  1581. hardware multicast filters :-( As result the host on multicasting
  1582. network acquires a lot of useless route cache entries, sort of
  1583. SDR messages from all the world. Now we try to get rid of them.
  1584. Really, provided software IP multicast filter is organized
  1585. reasonably (at least, hashed), it does not result in a slowdown
  1586. comparing with route cache reject entries.
  1587. Note, that multicast routers are not affected, because
  1588. route cache entry is created eventually.
  1589. */
  1590. if (ipv4_is_multicast(daddr)) {
  1591. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1592. if (in_dev) {
  1593. int our = ip_check_mc_rcu(in_dev, daddr, saddr,
  1594. ip_hdr(skb)->protocol);
  1595. if (our
  1596. #ifdef CONFIG_IP_MROUTE
  1597. ||
  1598. (!ipv4_is_local_multicast(daddr) &&
  1599. IN_DEV_MFORWARD(in_dev))
  1600. #endif
  1601. ) {
  1602. int res = ip_route_input_mc(skb, daddr, saddr,
  1603. tos, dev, our);
  1604. rcu_read_unlock();
  1605. return res;
  1606. }
  1607. }
  1608. rcu_read_unlock();
  1609. return -EINVAL;
  1610. }
  1611. res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
  1612. rcu_read_unlock();
  1613. return res;
  1614. }
  1615. EXPORT_SYMBOL(ip_route_input_noref);
  1616. /* called with rcu_read_lock() */
  1617. static struct rtable *__mkroute_output(const struct fib_result *res,
  1618. const struct flowi4 *fl4, int orig_oif,
  1619. struct net_device *dev_out,
  1620. unsigned int flags)
  1621. {
  1622. struct fib_info *fi = res->fi;
  1623. struct fib_nh_exception *fnhe;
  1624. struct in_device *in_dev;
  1625. u16 type = res->type;
  1626. struct rtable *rth;
  1627. bool do_cache;
  1628. in_dev = __in_dev_get_rcu(dev_out);
  1629. if (!in_dev)
  1630. return ERR_PTR(-EINVAL);
  1631. if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
  1632. if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK))
  1633. return ERR_PTR(-EINVAL);
  1634. if (ipv4_is_lbcast(fl4->daddr))
  1635. type = RTN_BROADCAST;
  1636. else if (ipv4_is_multicast(fl4->daddr))
  1637. type = RTN_MULTICAST;
  1638. else if (ipv4_is_zeronet(fl4->daddr))
  1639. return ERR_PTR(-EINVAL);
  1640. if (dev_out->flags & IFF_LOOPBACK)
  1641. flags |= RTCF_LOCAL;
  1642. do_cache = true;
  1643. if (type == RTN_BROADCAST) {
  1644. flags |= RTCF_BROADCAST | RTCF_LOCAL;
  1645. fi = NULL;
  1646. } else if (type == RTN_MULTICAST) {
  1647. flags |= RTCF_MULTICAST | RTCF_LOCAL;
  1648. if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
  1649. fl4->flowi4_proto))
  1650. flags &= ~RTCF_LOCAL;
  1651. else
  1652. do_cache = false;
  1653. /* If multicast route do not exist use
  1654. * default one, but do not gateway in this case.
  1655. * Yes, it is hack.
  1656. */
  1657. if (fi && res->prefixlen < 4)
  1658. fi = NULL;
  1659. }
  1660. fnhe = NULL;
  1661. do_cache &= fi != NULL;
  1662. if (do_cache) {
  1663. struct rtable __rcu **prth;
  1664. struct fib_nh *nh = &FIB_RES_NH(*res);
  1665. fnhe = find_exception(nh, fl4->daddr);
  1666. if (fnhe)
  1667. prth = &fnhe->fnhe_rth_output;
  1668. else {
  1669. if (unlikely(fl4->flowi4_flags &
  1670. FLOWI_FLAG_KNOWN_NH &&
  1671. !(nh->nh_gw &&
  1672. nh->nh_scope == RT_SCOPE_LINK))) {
  1673. do_cache = false;
  1674. goto add;
  1675. }
  1676. prth = raw_cpu_ptr(nh->nh_pcpu_rth_output);
  1677. }
  1678. rth = rcu_dereference(*prth);
  1679. if (rt_cache_valid(rth)) {
  1680. dst_hold(&rth->dst);
  1681. return rth;
  1682. }
  1683. }
  1684. add:
  1685. rth = rt_dst_alloc(dev_out,
  1686. IN_DEV_CONF_GET(in_dev, NOPOLICY),
  1687. IN_DEV_CONF_GET(in_dev, NOXFRM),
  1688. do_cache);
  1689. if (!rth)
  1690. return ERR_PTR(-ENOBUFS);
  1691. rth->dst.output = ip_output;
  1692. rth->rt_genid = rt_genid_ipv4(dev_net(dev_out));
  1693. rth->rt_flags = flags;
  1694. rth->rt_type = type;
  1695. rth->rt_is_input = 0;
  1696. rth->rt_iif = orig_oif ? : 0;
  1697. rth->rt_pmtu = 0;
  1698. rth->rt_gateway = 0;
  1699. rth->rt_uses_gateway = 0;
  1700. INIT_LIST_HEAD(&rth->rt_uncached);
  1701. RT_CACHE_STAT_INC(out_slow_tot);
  1702. if (flags & RTCF_LOCAL)
  1703. rth->dst.input = ip_local_deliver;
  1704. if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  1705. if (flags & RTCF_LOCAL &&
  1706. !(dev_out->flags & IFF_LOOPBACK)) {
  1707. rth->dst.output = ip_mc_output;
  1708. RT_CACHE_STAT_INC(out_slow_mc);
  1709. }
  1710. #ifdef CONFIG_IP_MROUTE
  1711. if (type == RTN_MULTICAST) {
  1712. if (IN_DEV_MFORWARD(in_dev) &&
  1713. !ipv4_is_local_multicast(fl4->daddr)) {
  1714. rth->dst.input = ip_mr_input;
  1715. rth->dst.output = ip_mc_output;
  1716. }
  1717. }
  1718. #endif
  1719. }
  1720. rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0);
  1721. return rth;
  1722. }
  1723. /*
  1724. * Major route resolver routine.
  1725. */
  1726. struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
  1727. {
  1728. struct net_device *dev_out = NULL;
  1729. __u8 tos = RT_FL_TOS(fl4);
  1730. unsigned int flags = 0;
  1731. struct fib_result res;
  1732. struct rtable *rth;
  1733. int orig_oif;
  1734. res.tclassid = 0;
  1735. res.fi = NULL;
  1736. res.table = NULL;
  1737. orig_oif = fl4->flowi4_oif;
  1738. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  1739. fl4->flowi4_tos = tos & IPTOS_RT_MASK;
  1740. fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
  1741. RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
  1742. rcu_read_lock();
  1743. if (fl4->saddr) {
  1744. rth = ERR_PTR(-EINVAL);
  1745. if (ipv4_is_multicast(fl4->saddr) ||
  1746. ipv4_is_lbcast(fl4->saddr) ||
  1747. ipv4_is_zeronet(fl4->saddr))
  1748. goto out;
  1749. /* I removed check for oif == dev_out->oif here.
  1750. It was wrong for two reasons:
  1751. 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
  1752. is assigned to multiple interfaces.
  1753. 2. Moreover, we are allowed to send packets with saddr
  1754. of another iface. --ANK
  1755. */
  1756. if (fl4->flowi4_oif == 0 &&
  1757. (ipv4_is_multicast(fl4->daddr) ||
  1758. ipv4_is_lbcast(fl4->daddr))) {
  1759. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  1760. dev_out = __ip_dev_find(net, fl4->saddr, false);
  1761. if (!dev_out)
  1762. goto out;
  1763. /* Special hack: user can direct multicasts
  1764. and limited broadcast via necessary interface
  1765. without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
  1766. This hack is not just for fun, it allows
  1767. vic,vat and friends to work.
  1768. They bind socket to loopback, set ttl to zero
  1769. and expect that it will work.
  1770. From the viewpoint of routing cache they are broken,
  1771. because we are not allowed to build multicast path
  1772. with loopback source addr (look, routing cache
  1773. cannot know, that ttl is zero, so that packet
  1774. will not leave this host and route is valid).
  1775. Luckily, this hack is good workaround.
  1776. */
  1777. fl4->flowi4_oif = dev_out->ifindex;
  1778. goto make_route;
  1779. }
  1780. if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
  1781. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  1782. if (!__ip_dev_find(net, fl4->saddr, false))
  1783. goto out;
  1784. }
  1785. }
  1786. if (fl4->flowi4_oif) {
  1787. dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
  1788. rth = ERR_PTR(-ENODEV);
  1789. if (!dev_out)
  1790. goto out;
  1791. /* RACE: Check return value of inet_select_addr instead. */
  1792. if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
  1793. rth = ERR_PTR(-ENETUNREACH);
  1794. goto out;
  1795. }
  1796. if (ipv4_is_local_multicast(fl4->daddr) ||
  1797. ipv4_is_lbcast(fl4->daddr) ||
  1798. fl4->flowi4_proto == IPPROTO_IGMP) {
  1799. if (!fl4->saddr)
  1800. fl4->saddr = inet_select_addr(dev_out, 0,
  1801. RT_SCOPE_LINK);
  1802. goto make_route;
  1803. }
  1804. if (!fl4->saddr) {
  1805. if (ipv4_is_multicast(fl4->daddr))
  1806. fl4->saddr = inet_select_addr(dev_out, 0,
  1807. fl4->flowi4_scope);
  1808. else if (!fl4->daddr)
  1809. fl4->saddr = inet_select_addr(dev_out, 0,
  1810. RT_SCOPE_HOST);
  1811. }
  1812. }
  1813. if (!fl4->daddr) {
  1814. fl4->daddr = fl4->saddr;
  1815. if (!fl4->daddr)
  1816. fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
  1817. dev_out = net->loopback_dev;
  1818. fl4->flowi4_oif = LOOPBACK_IFINDEX;
  1819. res.type = RTN_LOCAL;
  1820. flags |= RTCF_LOCAL;
  1821. goto make_route;
  1822. }
  1823. if (fib_lookup(net, fl4, &res, 0)) {
  1824. res.fi = NULL;
  1825. res.table = NULL;
  1826. if (fl4->flowi4_oif) {
  1827. /* Apparently, routing tables are wrong. Assume,
  1828. that the destination is on link.
  1829. WHY? DW.
  1830. Because we are allowed to send to iface
  1831. even if it has NO routes and NO assigned
  1832. addresses. When oif is specified, routing
  1833. tables are looked up with only one purpose:
  1834. to catch if destination is gatewayed, rather than
  1835. direct. Moreover, if MSG_DONTROUTE is set,
  1836. we send packet, ignoring both routing tables
  1837. and ifaddr state. --ANK
  1838. We could make it even if oif is unknown,
  1839. likely IPv6, but we do not.
  1840. */
  1841. if (fl4->saddr == 0)
  1842. fl4->saddr = inet_select_addr(dev_out, 0,
  1843. RT_SCOPE_LINK);
  1844. res.type = RTN_UNICAST;
  1845. goto make_route;
  1846. }
  1847. rth = ERR_PTR(-ENETUNREACH);
  1848. goto out;
  1849. }
  1850. if (res.type == RTN_LOCAL) {
  1851. if (!fl4->saddr) {
  1852. if (res.fi->fib_prefsrc)
  1853. fl4->saddr = res.fi->fib_prefsrc;
  1854. else
  1855. fl4->saddr = fl4->daddr;
  1856. }
  1857. dev_out = net->loopback_dev;
  1858. fl4->flowi4_oif = dev_out->ifindex;
  1859. flags |= RTCF_LOCAL;
  1860. goto make_route;
  1861. }
  1862. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1863. if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0)
  1864. fib_select_multipath(&res);
  1865. else
  1866. #endif
  1867. if (!res.prefixlen &&
  1868. res.table->tb_num_default > 1 &&
  1869. res.type == RTN_UNICAST && !fl4->flowi4_oif)
  1870. fib_select_default(&res);
  1871. if (!fl4->saddr)
  1872. fl4->saddr = FIB_RES_PREFSRC(net, res);
  1873. dev_out = FIB_RES_DEV(res);
  1874. fl4->flowi4_oif = dev_out->ifindex;
  1875. make_route:
  1876. rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags);
  1877. out:
  1878. rcu_read_unlock();
  1879. return rth;
  1880. }
  1881. EXPORT_SYMBOL_GPL(__ip_route_output_key);
  1882. static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie)
  1883. {
  1884. return NULL;
  1885. }
  1886. static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst)
  1887. {
  1888. unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
  1889. return mtu ? : dst->dev->mtu;
  1890. }
  1891. static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
  1892. struct sk_buff *skb, u32 mtu)
  1893. {
  1894. }
  1895. static void ipv4_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
  1896. struct sk_buff *skb)
  1897. {
  1898. }
  1899. static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst,
  1900. unsigned long old)
  1901. {
  1902. return NULL;
  1903. }
  1904. static struct dst_ops ipv4_dst_blackhole_ops = {
  1905. .family = AF_INET,
  1906. .check = ipv4_blackhole_dst_check,
  1907. .mtu = ipv4_blackhole_mtu,
  1908. .default_advmss = ipv4_default_advmss,
  1909. .update_pmtu = ipv4_rt_blackhole_update_pmtu,
  1910. .redirect = ipv4_rt_blackhole_redirect,
  1911. .cow_metrics = ipv4_rt_blackhole_cow_metrics,
  1912. .neigh_lookup = ipv4_neigh_lookup,
  1913. };
  1914. struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  1915. {
  1916. struct rtable *ort = (struct rtable *) dst_orig;
  1917. struct rtable *rt;
  1918. rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
  1919. if (rt) {
  1920. struct dst_entry *new = &rt->dst;
  1921. new->__use = 1;
  1922. new->input = dst_discard;
  1923. new->output = dst_discard_sk;
  1924. new->dev = ort->dst.dev;
  1925. if (new->dev)
  1926. dev_hold(new->dev);
  1927. rt->rt_is_input = ort->rt_is_input;
  1928. rt->rt_iif = ort->rt_iif;
  1929. rt->rt_pmtu = ort->rt_pmtu;
  1930. rt->rt_genid = rt_genid_ipv4(net);
  1931. rt->rt_flags = ort->rt_flags;
  1932. rt->rt_type = ort->rt_type;
  1933. rt->rt_gateway = ort->rt_gateway;
  1934. rt->rt_uses_gateway = ort->rt_uses_gateway;
  1935. INIT_LIST_HEAD(&rt->rt_uncached);
  1936. dst_free(new);
  1937. }
  1938. dst_release(dst_orig);
  1939. return rt ? &rt->dst : ERR_PTR(-ENOMEM);
  1940. }
  1941. struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
  1942. struct sock *sk)
  1943. {
  1944. struct rtable *rt = __ip_route_output_key(net, flp4);
  1945. if (IS_ERR(rt))
  1946. return rt;
  1947. if (flp4->flowi4_proto)
  1948. rt = (struct rtable *)xfrm_lookup_route(net, &rt->dst,
  1949. flowi4_to_flowi(flp4),
  1950. sk, 0);
  1951. return rt;
  1952. }
  1953. EXPORT_SYMBOL_GPL(ip_route_output_flow);
  1954. static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
  1955. struct flowi4 *fl4, struct sk_buff *skb, u32 portid,
  1956. u32 seq, int event, int nowait, unsigned int flags)
  1957. {
  1958. struct rtable *rt = skb_rtable(skb);
  1959. struct rtmsg *r;
  1960. struct nlmsghdr *nlh;
  1961. unsigned long expires = 0;
  1962. u32 error;
  1963. u32 metrics[RTAX_MAX];
  1964. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags);
  1965. if (!nlh)
  1966. return -EMSGSIZE;
  1967. r = nlmsg_data(nlh);
  1968. r->rtm_family = AF_INET;
  1969. r->rtm_dst_len = 32;
  1970. r->rtm_src_len = 0;
  1971. r->rtm_tos = fl4->flowi4_tos;
  1972. r->rtm_table = RT_TABLE_MAIN;
  1973. if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN))
  1974. goto nla_put_failure;
  1975. r->rtm_type = rt->rt_type;
  1976. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1977. r->rtm_protocol = RTPROT_UNSPEC;
  1978. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  1979. if (rt->rt_flags & RTCF_NOTIFY)
  1980. r->rtm_flags |= RTM_F_NOTIFY;
  1981. if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
  1982. r->rtm_flags |= RTCF_DOREDIRECT;
  1983. if (nla_put_in_addr(skb, RTA_DST, dst))
  1984. goto nla_put_failure;
  1985. if (src) {
  1986. r->rtm_src_len = 32;
  1987. if (nla_put_in_addr(skb, RTA_SRC, src))
  1988. goto nla_put_failure;
  1989. }
  1990. if (rt->dst.dev &&
  1991. nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
  1992. goto nla_put_failure;
  1993. #ifdef CONFIG_IP_ROUTE_CLASSID
  1994. if (rt->dst.tclassid &&
  1995. nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
  1996. goto nla_put_failure;
  1997. #endif
  1998. if (!rt_is_input_route(rt) &&
  1999. fl4->saddr != src) {
  2000. if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
  2001. goto nla_put_failure;
  2002. }
  2003. if (rt->rt_uses_gateway &&
  2004. nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gateway))
  2005. goto nla_put_failure;
  2006. expires = rt->dst.expires;
  2007. if (expires) {
  2008. unsigned long now = jiffies;
  2009. if (time_before(now, expires))
  2010. expires -= now;
  2011. else
  2012. expires = 0;
  2013. }
  2014. memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
  2015. if (rt->rt_pmtu && expires)
  2016. metrics[RTAX_MTU - 1] = rt->rt_pmtu;
  2017. if (rtnetlink_put_metrics(skb, metrics) < 0)
  2018. goto nla_put_failure;
  2019. if (fl4->flowi4_mark &&
  2020. nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark))
  2021. goto nla_put_failure;
  2022. error = rt->dst.error;
  2023. if (rt_is_input_route(rt)) {
  2024. #ifdef CONFIG_IP_MROUTE
  2025. if (ipv4_is_multicast(dst) && !ipv4_is_local_multicast(dst) &&
  2026. IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
  2027. int err = ipmr_get_route(net, skb,
  2028. fl4->saddr, fl4->daddr,
  2029. r, nowait);
  2030. if (err <= 0) {
  2031. if (!nowait) {
  2032. if (err == 0)
  2033. return 0;
  2034. goto nla_put_failure;
  2035. } else {
  2036. if (err == -EMSGSIZE)
  2037. goto nla_put_failure;
  2038. error = err;
  2039. }
  2040. }
  2041. } else
  2042. #endif
  2043. if (nla_put_u32(skb, RTA_IIF, skb->dev->ifindex))
  2044. goto nla_put_failure;
  2045. }
  2046. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
  2047. goto nla_put_failure;
  2048. nlmsg_end(skb, nlh);
  2049. return 0;
  2050. nla_put_failure:
  2051. nlmsg_cancel(skb, nlh);
  2052. return -EMSGSIZE;
  2053. }
  2054. static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
  2055. {
  2056. struct net *net = sock_net(in_skb->sk);
  2057. struct rtmsg *rtm;
  2058. struct nlattr *tb[RTA_MAX+1];
  2059. struct rtable *rt = NULL;
  2060. struct flowi4 fl4;
  2061. __be32 dst = 0;
  2062. __be32 src = 0;
  2063. u32 iif;
  2064. int err;
  2065. int mark;
  2066. struct sk_buff *skb;
  2067. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
  2068. if (err < 0)
  2069. goto errout;
  2070. rtm = nlmsg_data(nlh);
  2071. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2072. if (!skb) {
  2073. err = -ENOBUFS;
  2074. goto errout;
  2075. }
  2076. /* Reserve room for dummy headers, this skb can pass
  2077. through good chunk of routing engine.
  2078. */
  2079. skb_reset_mac_header(skb);
  2080. skb_reset_network_header(skb);
  2081. /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
  2082. ip_hdr(skb)->protocol = IPPROTO_ICMP;
  2083. skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
  2084. src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
  2085. dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
  2086. iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
  2087. mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
  2088. memset(&fl4, 0, sizeof(fl4));
  2089. fl4.daddr = dst;
  2090. fl4.saddr = src;
  2091. fl4.flowi4_tos = rtm->rtm_tos;
  2092. fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
  2093. fl4.flowi4_mark = mark;
  2094. if (iif) {
  2095. struct net_device *dev;
  2096. dev = __dev_get_by_index(net, iif);
  2097. if (!dev) {
  2098. err = -ENODEV;
  2099. goto errout_free;
  2100. }
  2101. skb->protocol = htons(ETH_P_IP);
  2102. skb->dev = dev;
  2103. skb->mark = mark;
  2104. local_bh_disable();
  2105. err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
  2106. local_bh_enable();
  2107. rt = skb_rtable(skb);
  2108. if (err == 0 && rt->dst.error)
  2109. err = -rt->dst.error;
  2110. } else {
  2111. rt = ip_route_output_key(net, &fl4);
  2112. err = 0;
  2113. if (IS_ERR(rt))
  2114. err = PTR_ERR(rt);
  2115. }
  2116. if (err)
  2117. goto errout_free;
  2118. skb_dst_set(skb, &rt->dst);
  2119. if (rtm->rtm_flags & RTM_F_NOTIFY)
  2120. rt->rt_flags |= RTCF_NOTIFY;
  2121. err = rt_fill_info(net, dst, src, &fl4, skb,
  2122. NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  2123. RTM_NEWROUTE, 0, 0);
  2124. if (err < 0)
  2125. goto errout_free;
  2126. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  2127. errout:
  2128. return err;
  2129. errout_free:
  2130. kfree_skb(skb);
  2131. goto errout;
  2132. }
  2133. void ip_rt_multicast_event(struct in_device *in_dev)
  2134. {
  2135. rt_cache_flush(dev_net(in_dev->dev));
  2136. }
  2137. #ifdef CONFIG_SYSCTL
  2138. static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT;
  2139. static int ip_rt_gc_interval __read_mostly = 60 * HZ;
  2140. static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
  2141. static int ip_rt_gc_elasticity __read_mostly = 8;
  2142. static int ipv4_sysctl_rtcache_flush(struct ctl_table *__ctl, int write,
  2143. void __user *buffer,
  2144. size_t *lenp, loff_t *ppos)
  2145. {
  2146. struct net *net = (struct net *)__ctl->extra1;
  2147. if (write) {
  2148. rt_cache_flush(net);
  2149. fnhe_genid_bump(net);
  2150. return 0;
  2151. }
  2152. return -EINVAL;
  2153. }
  2154. static struct ctl_table ipv4_route_table[] = {
  2155. {
  2156. .procname = "gc_thresh",
  2157. .data = &ipv4_dst_ops.gc_thresh,
  2158. .maxlen = sizeof(int),
  2159. .mode = 0644,
  2160. .proc_handler = proc_dointvec,
  2161. },
  2162. {
  2163. .procname = "max_size",
  2164. .data = &ip_rt_max_size,
  2165. .maxlen = sizeof(int),
  2166. .mode = 0644,
  2167. .proc_handler = proc_dointvec,
  2168. },
  2169. {
  2170. /* Deprecated. Use gc_min_interval_ms */
  2171. .procname = "gc_min_interval",
  2172. .data = &ip_rt_gc_min_interval,
  2173. .maxlen = sizeof(int),
  2174. .mode = 0644,
  2175. .proc_handler = proc_dointvec_jiffies,
  2176. },
  2177. {
  2178. .procname = "gc_min_interval_ms",
  2179. .data = &ip_rt_gc_min_interval,
  2180. .maxlen = sizeof(int),
  2181. .mode = 0644,
  2182. .proc_handler = proc_dointvec_ms_jiffies,
  2183. },
  2184. {
  2185. .procname = "gc_timeout",
  2186. .data = &ip_rt_gc_timeout,
  2187. .maxlen = sizeof(int),
  2188. .mode = 0644,
  2189. .proc_handler = proc_dointvec_jiffies,
  2190. },
  2191. {
  2192. .procname = "gc_interval",
  2193. .data = &ip_rt_gc_interval,
  2194. .maxlen = sizeof(int),
  2195. .mode = 0644,
  2196. .proc_handler = proc_dointvec_jiffies,
  2197. },
  2198. {
  2199. .procname = "redirect_load",
  2200. .data = &ip_rt_redirect_load,
  2201. .maxlen = sizeof(int),
  2202. .mode = 0644,
  2203. .proc_handler = proc_dointvec,
  2204. },
  2205. {
  2206. .procname = "redirect_number",
  2207. .data = &ip_rt_redirect_number,
  2208. .maxlen = sizeof(int),
  2209. .mode = 0644,
  2210. .proc_handler = proc_dointvec,
  2211. },
  2212. {
  2213. .procname = "redirect_silence",
  2214. .data = &ip_rt_redirect_silence,
  2215. .maxlen = sizeof(int),
  2216. .mode = 0644,
  2217. .proc_handler = proc_dointvec,
  2218. },
  2219. {
  2220. .procname = "error_cost",
  2221. .data = &ip_rt_error_cost,
  2222. .maxlen = sizeof(int),
  2223. .mode = 0644,
  2224. .proc_handler = proc_dointvec,
  2225. },
  2226. {
  2227. .procname = "error_burst",
  2228. .data = &ip_rt_error_burst,
  2229. .maxlen = sizeof(int),
  2230. .mode = 0644,
  2231. .proc_handler = proc_dointvec,
  2232. },
  2233. {
  2234. .procname = "gc_elasticity",
  2235. .data = &ip_rt_gc_elasticity,
  2236. .maxlen = sizeof(int),
  2237. .mode = 0644,
  2238. .proc_handler = proc_dointvec,
  2239. },
  2240. {
  2241. .procname = "mtu_expires",
  2242. .data = &ip_rt_mtu_expires,
  2243. .maxlen = sizeof(int),
  2244. .mode = 0644,
  2245. .proc_handler = proc_dointvec_jiffies,
  2246. },
  2247. {
  2248. .procname = "min_pmtu",
  2249. .data = &ip_rt_min_pmtu,
  2250. .maxlen = sizeof(int),
  2251. .mode = 0644,
  2252. .proc_handler = proc_dointvec,
  2253. },
  2254. {
  2255. .procname = "min_adv_mss",
  2256. .data = &ip_rt_min_advmss,
  2257. .maxlen = sizeof(int),
  2258. .mode = 0644,
  2259. .proc_handler = proc_dointvec,
  2260. },
  2261. { }
  2262. };
  2263. static struct ctl_table ipv4_route_flush_table[] = {
  2264. {
  2265. .procname = "flush",
  2266. .maxlen = sizeof(int),
  2267. .mode = 0200,
  2268. .proc_handler = ipv4_sysctl_rtcache_flush,
  2269. },
  2270. { },
  2271. };
  2272. static __net_init int sysctl_route_net_init(struct net *net)
  2273. {
  2274. struct ctl_table *tbl;
  2275. tbl = ipv4_route_flush_table;
  2276. if (!net_eq(net, &init_net)) {
  2277. tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
  2278. if (!tbl)
  2279. goto err_dup;
  2280. /* Don't export sysctls to unprivileged users */
  2281. if (net->user_ns != &init_user_ns)
  2282. tbl[0].procname = NULL;
  2283. }
  2284. tbl[0].extra1 = net;
  2285. net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
  2286. if (!net->ipv4.route_hdr)
  2287. goto err_reg;
  2288. return 0;
  2289. err_reg:
  2290. if (tbl != ipv4_route_flush_table)
  2291. kfree(tbl);
  2292. err_dup:
  2293. return -ENOMEM;
  2294. }
  2295. static __net_exit void sysctl_route_net_exit(struct net *net)
  2296. {
  2297. struct ctl_table *tbl;
  2298. tbl = net->ipv4.route_hdr->ctl_table_arg;
  2299. unregister_net_sysctl_table(net->ipv4.route_hdr);
  2300. BUG_ON(tbl == ipv4_route_flush_table);
  2301. kfree(tbl);
  2302. }
  2303. static __net_initdata struct pernet_operations sysctl_route_ops = {
  2304. .init = sysctl_route_net_init,
  2305. .exit = sysctl_route_net_exit,
  2306. };
  2307. #endif
  2308. static __net_init int rt_genid_init(struct net *net)
  2309. {
  2310. atomic_set(&net->ipv4.rt_genid, 0);
  2311. atomic_set(&net->fnhe_genid, 0);
  2312. get_random_bytes(&net->ipv4.dev_addr_genid,
  2313. sizeof(net->ipv4.dev_addr_genid));
  2314. return 0;
  2315. }
  2316. static __net_initdata struct pernet_operations rt_genid_ops = {
  2317. .init = rt_genid_init,
  2318. };
  2319. static int __net_init ipv4_inetpeer_init(struct net *net)
  2320. {
  2321. struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  2322. if (!bp)
  2323. return -ENOMEM;
  2324. inet_peer_base_init(bp);
  2325. net->ipv4.peers = bp;
  2326. return 0;
  2327. }
  2328. static void __net_exit ipv4_inetpeer_exit(struct net *net)
  2329. {
  2330. struct inet_peer_base *bp = net->ipv4.peers;
  2331. net->ipv4.peers = NULL;
  2332. inetpeer_invalidate_tree(bp);
  2333. kfree(bp);
  2334. }
  2335. static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
  2336. .init = ipv4_inetpeer_init,
  2337. .exit = ipv4_inetpeer_exit,
  2338. };
  2339. #ifdef CONFIG_IP_ROUTE_CLASSID
  2340. struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
  2341. #endif /* CONFIG_IP_ROUTE_CLASSID */
  2342. int __init ip_rt_init(void)
  2343. {
  2344. int rc = 0;
  2345. int cpu;
  2346. ip_idents = kmalloc(IP_IDENTS_SZ * sizeof(*ip_idents), GFP_KERNEL);
  2347. if (!ip_idents)
  2348. panic("IP: failed to allocate ip_idents\n");
  2349. prandom_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents));
  2350. ip_tstamps = kcalloc(IP_IDENTS_SZ, sizeof(*ip_tstamps), GFP_KERNEL);
  2351. if (!ip_tstamps)
  2352. panic("IP: failed to allocate ip_tstamps\n");
  2353. for_each_possible_cpu(cpu) {
  2354. struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
  2355. INIT_LIST_HEAD(&ul->head);
  2356. spin_lock_init(&ul->lock);
  2357. }
  2358. #ifdef CONFIG_IP_ROUTE_CLASSID
  2359. ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
  2360. if (!ip_rt_acct)
  2361. panic("IP: failed to allocate ip_rt_acct\n");
  2362. #endif
  2363. ipv4_dst_ops.kmem_cachep =
  2364. kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
  2365. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2366. ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
  2367. if (dst_entries_init(&ipv4_dst_ops) < 0)
  2368. panic("IP: failed to allocate ipv4_dst_ops counter\n");
  2369. if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
  2370. panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
  2371. ipv4_dst_ops.gc_thresh = ~0;
  2372. ip_rt_max_size = INT_MAX;
  2373. devinet_init();
  2374. ip_fib_init();
  2375. if (ip_rt_proc_init())
  2376. pr_err("Unable to create route proc files\n");
  2377. #ifdef CONFIG_XFRM
  2378. xfrm_init();
  2379. xfrm4_init();
  2380. #endif
  2381. rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL);
  2382. #ifdef CONFIG_SYSCTL
  2383. register_pernet_subsys(&sysctl_route_ops);
  2384. #endif
  2385. register_pernet_subsys(&rt_genid_ops);
  2386. register_pernet_subsys(&ipv4_inetpeer_ops);
  2387. return rc;
  2388. }
  2389. #ifdef CONFIG_SYSCTL
  2390. /*
  2391. * We really need to sanitize the damn ipv4 init order, then all
  2392. * this nonsense will go away.
  2393. */
  2394. void __init ip_static_sysctl_init(void)
  2395. {
  2396. register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
  2397. }
  2398. #endif