route.c 72 KB

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