route.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * ROUTE - implementation of the IP router.
  8. *
  9. * Authors: Ross Biro
  10. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  11. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  12. * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
  13. * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  14. *
  15. * Fixes:
  16. * Alan Cox : Verify area fixes.
  17. * Alan Cox : cli() protects routing changes
  18. * Rui Oliveira : ICMP routing table updates
  19. * (rco@di.uminho.pt) Routing table insertion and update
  20. * Linus Torvalds : Rewrote bits to be sensible
  21. * Alan Cox : Added BSD route gw semantics
  22. * Alan Cox : Super /proc >4K
  23. * Alan Cox : MTU in route table
  24. * Alan Cox : MSS actually. Also added the window
  25. * clamper.
  26. * Sam Lantinga : Fixed route matching in rt_del()
  27. * Alan Cox : Routing cache support.
  28. * Alan Cox : Removed compatibility cruft.
  29. * Alan Cox : RTF_REJECT support.
  30. * Alan Cox : TCP irtt support.
  31. * Jonathan Naylor : Added Metric support.
  32. * Miquel van Smoorenburg : BSD API fixes.
  33. * Miquel van Smoorenburg : Metrics.
  34. * Alan Cox : Use __u32 properly
  35. * Alan Cox : Aligned routing errors more closely with BSD
  36. * our system is still very different.
  37. * Alan Cox : Faster /proc handling
  38. * Alexey Kuznetsov : Massive rework to support tree based routing,
  39. * routing caches and better behaviour.
  40. *
  41. * Olaf Erb : irtt wasn't being copied right.
  42. * Bjorn Ekwall : Kerneld route support.
  43. * Alan Cox : Multicast fixed (I hope)
  44. * Pavel Krauz : Limited broadcast fixed
  45. * Mike McLagan : Routing by source
  46. * Alexey Kuznetsov : End of old history. Split to fib.c and
  47. * route.c and rewritten from scratch.
  48. * Andi Kleen : Load-limit warning messages.
  49. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  50. * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
  51. * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
  52. * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
  53. * Marc Boucher : routing by fwmark
  54. * Robert Olsson : Added rt_cache statistics
  55. * Arnaldo C. Melo : Convert proc stuff to seq_file
  56. * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
  57. * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
  58. * Ilia Sotnikov : Removed TOS from hash calculations
  59. */
  60. #define pr_fmt(fmt) "IPv4: " fmt
  61. #include <linux/module.h>
  62. #include <linux/bitops.h>
  63. #include <linux/kernel.h>
  64. #include <linux/mm.h>
  65. #include <linux/memblock.h>
  66. #include <linux/socket.h>
  67. #include <linux/errno.h>
  68. #include <linux/in.h>
  69. #include <linux/inet.h>
  70. #include <linux/netdevice.h>
  71. #include <linux/proc_fs.h>
  72. #include <linux/init.h>
  73. #include <linux/skbuff.h>
  74. #include <linux/inetdevice.h>
  75. #include <linux/igmp.h>
  76. #include <linux/pkt_sched.h>
  77. #include <linux/mroute.h>
  78. #include <linux/netfilter_ipv4.h>
  79. #include <linux/random.h>
  80. #include <linux/rcupdate.h>
  81. #include <linux/slab.h>
  82. #include <linux/jhash.h>
  83. #include <net/dst.h>
  84. #include <net/dst_metadata.h>
  85. #include <net/flow.h>
  86. #include <net/inet_dscp.h>
  87. #include <net/net_namespace.h>
  88. #include <net/ip.h>
  89. #include <net/route.h>
  90. #include <net/inetpeer.h>
  91. #include <net/sock.h>
  92. #include <net/ip_fib.h>
  93. #include <net/nexthop.h>
  94. #include <net/tcp.h>
  95. #include <net/icmp.h>
  96. #include <net/xfrm.h>
  97. #include <net/lwtunnel.h>
  98. #include <net/netevent.h>
  99. #include <net/rtnetlink.h>
  100. #ifdef CONFIG_SYSCTL
  101. #include <linux/sysctl.h>
  102. #endif
  103. #include <net/secure_seq.h>
  104. #include <net/ip_tunnels.h>
  105. #include "fib_lookup.h"
  106. #define RT_GC_TIMEOUT (300*HZ)
  107. #define DEFAULT_MIN_PMTU (512 + 20 + 20)
  108. #define DEFAULT_MTU_EXPIRES (10 * 60 * HZ)
  109. #define DEFAULT_MIN_ADVMSS 256
  110. static int ip_rt_max_size;
  111. static int ip_rt_redirect_number __read_mostly = 9;
  112. static int ip_rt_redirect_load __read_mostly = HZ / 50;
  113. static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1));
  114. static int ip_rt_error_cost __read_mostly = HZ;
  115. static int ip_rt_error_burst __read_mostly = 5 * HZ;
  116. static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT;
  117. /*
  118. * Interface to generic destination cache.
  119. */
  120. INDIRECT_CALLABLE_SCOPE
  121. struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
  122. static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
  123. INDIRECT_CALLABLE_SCOPE
  124. unsigned int ipv4_mtu(const struct dst_entry *dst);
  125. static void ipv4_negative_advice(struct sock *sk,
  126. struct dst_entry *dst);
  127. static void ipv4_link_failure(struct sk_buff *skb);
  128. static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  129. struct sk_buff *skb, u32 mtu,
  130. bool confirm_neigh);
  131. static void ip_do_redirect(struct dst_entry *dst, struct sock *sk,
  132. struct sk_buff *skb);
  133. static void ipv4_dst_destroy(struct dst_entry *dst);
  134. static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
  135. {
  136. WARN_ON(1);
  137. return NULL;
  138. }
  139. static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
  140. struct sk_buff *skb,
  141. const void *daddr);
  142. static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr);
  143. static struct dst_ops ipv4_dst_ops = {
  144. .family = AF_INET,
  145. .check = ipv4_dst_check,
  146. .default_advmss = ipv4_default_advmss,
  147. .mtu = ipv4_mtu,
  148. .cow_metrics = ipv4_cow_metrics,
  149. .destroy = ipv4_dst_destroy,
  150. .negative_advice = ipv4_negative_advice,
  151. .link_failure = ipv4_link_failure,
  152. .update_pmtu = ip_rt_update_pmtu,
  153. .redirect = ip_do_redirect,
  154. .local_out = __ip_local_out,
  155. .neigh_lookup = ipv4_neigh_lookup,
  156. .confirm_neigh = ipv4_confirm_neigh,
  157. };
  158. #define ECN_OR_COST(class) TC_PRIO_##class
  159. const __u8 ip_tos2prio[16] = {
  160. TC_PRIO_BESTEFFORT,
  161. ECN_OR_COST(BESTEFFORT),
  162. TC_PRIO_BESTEFFORT,
  163. ECN_OR_COST(BESTEFFORT),
  164. TC_PRIO_BULK,
  165. ECN_OR_COST(BULK),
  166. TC_PRIO_BULK,
  167. ECN_OR_COST(BULK),
  168. TC_PRIO_INTERACTIVE,
  169. ECN_OR_COST(INTERACTIVE),
  170. TC_PRIO_INTERACTIVE,
  171. ECN_OR_COST(INTERACTIVE),
  172. TC_PRIO_INTERACTIVE_BULK,
  173. ECN_OR_COST(INTERACTIVE_BULK),
  174. TC_PRIO_INTERACTIVE_BULK,
  175. ECN_OR_COST(INTERACTIVE_BULK)
  176. };
  177. EXPORT_SYMBOL(ip_tos2prio);
  178. static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
  179. #ifndef CONFIG_PREEMPT_RT
  180. #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field)
  181. #else
  182. #define RT_CACHE_STAT_INC(field) this_cpu_inc(rt_cache_stat.field)
  183. #endif
  184. #ifdef CONFIG_PROC_FS
  185. static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  186. {
  187. if (*pos)
  188. return NULL;
  189. return SEQ_START_TOKEN;
  190. }
  191. static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  192. {
  193. ++*pos;
  194. return NULL;
  195. }
  196. static void rt_cache_seq_stop(struct seq_file *seq, void *v)
  197. {
  198. }
  199. static int rt_cache_seq_show(struct seq_file *seq, void *v)
  200. {
  201. if (v == SEQ_START_TOKEN)
  202. seq_printf(seq, "%-127s\n",
  203. "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
  204. "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
  205. "HHUptod\tSpecDst");
  206. return 0;
  207. }
  208. static const struct seq_operations rt_cache_seq_ops = {
  209. .start = rt_cache_seq_start,
  210. .next = rt_cache_seq_next,
  211. .stop = rt_cache_seq_stop,
  212. .show = rt_cache_seq_show,
  213. };
  214. static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
  215. {
  216. int cpu;
  217. if (*pos == 0)
  218. return SEQ_START_TOKEN;
  219. for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
  220. if (!cpu_possible(cpu))
  221. continue;
  222. *pos = cpu+1;
  223. return &per_cpu(rt_cache_stat, cpu);
  224. }
  225. return NULL;
  226. }
  227. static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  228. {
  229. int cpu;
  230. for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
  231. if (!cpu_possible(cpu))
  232. continue;
  233. *pos = cpu+1;
  234. return &per_cpu(rt_cache_stat, cpu);
  235. }
  236. (*pos)++;
  237. return NULL;
  238. }
  239. static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
  240. {
  241. }
  242. static int rt_cpu_seq_show(struct seq_file *seq, void *v)
  243. {
  244. struct rt_cache_stat *st = v;
  245. if (v == SEQ_START_TOKEN) {
  246. seq_puts(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");
  247. return 0;
  248. }
  249. seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x "
  250. "%08x %08x %08x %08x %08x %08x "
  251. "%08x %08x %08x %08x\n",
  252. dst_entries_get_slow(&ipv4_dst_ops),
  253. 0, /* st->in_hit */
  254. st->in_slow_tot,
  255. st->in_slow_mc,
  256. st->in_no_route,
  257. st->in_brd,
  258. st->in_martian_dst,
  259. st->in_martian_src,
  260. 0, /* st->out_hit */
  261. st->out_slow_tot,
  262. st->out_slow_mc,
  263. 0, /* st->gc_total */
  264. 0, /* st->gc_ignored */
  265. 0, /* st->gc_goal_miss */
  266. 0, /* st->gc_dst_overflow */
  267. 0, /* st->in_hlist_search */
  268. 0 /* st->out_hlist_search */
  269. );
  270. return 0;
  271. }
  272. static const struct seq_operations rt_cpu_seq_ops = {
  273. .start = rt_cpu_seq_start,
  274. .next = rt_cpu_seq_next,
  275. .stop = rt_cpu_seq_stop,
  276. .show = rt_cpu_seq_show,
  277. };
  278. #ifdef CONFIG_IP_ROUTE_CLASSID
  279. static int rt_acct_proc_show(struct seq_file *m, void *v)
  280. {
  281. struct ip_rt_acct *dst, *src;
  282. unsigned int i, j;
  283. dst = kzalloc_objs(struct ip_rt_acct, 256);
  284. if (!dst)
  285. return -ENOMEM;
  286. for_each_possible_cpu(i) {
  287. src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
  288. for (j = 0; j < 256; j++) {
  289. dst[j].o_bytes += src[j].o_bytes;
  290. dst[j].o_packets += src[j].o_packets;
  291. dst[j].i_bytes += src[j].i_bytes;
  292. dst[j].i_packets += src[j].i_packets;
  293. }
  294. }
  295. seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
  296. kfree(dst);
  297. return 0;
  298. }
  299. #endif
  300. static int __net_init ip_rt_do_proc_init(struct net *net)
  301. {
  302. struct proc_dir_entry *pde;
  303. pde = proc_create_seq("rt_cache", 0444, net->proc_net,
  304. &rt_cache_seq_ops);
  305. if (!pde)
  306. goto err1;
  307. pde = proc_create_seq("rt_cache", 0444, net->proc_net_stat,
  308. &rt_cpu_seq_ops);
  309. if (!pde)
  310. goto err2;
  311. #ifdef CONFIG_IP_ROUTE_CLASSID
  312. pde = proc_create_single("rt_acct", 0, net->proc_net,
  313. rt_acct_proc_show);
  314. if (!pde)
  315. goto err3;
  316. #endif
  317. return 0;
  318. #ifdef CONFIG_IP_ROUTE_CLASSID
  319. err3:
  320. remove_proc_entry("rt_cache", net->proc_net_stat);
  321. #endif
  322. err2:
  323. remove_proc_entry("rt_cache", net->proc_net);
  324. err1:
  325. return -ENOMEM;
  326. }
  327. static void __net_exit ip_rt_do_proc_exit(struct net *net)
  328. {
  329. remove_proc_entry("rt_cache", net->proc_net_stat);
  330. remove_proc_entry("rt_cache", net->proc_net);
  331. #ifdef CONFIG_IP_ROUTE_CLASSID
  332. remove_proc_entry("rt_acct", net->proc_net);
  333. #endif
  334. }
  335. static struct pernet_operations ip_rt_proc_ops __net_initdata = {
  336. .init = ip_rt_do_proc_init,
  337. .exit = ip_rt_do_proc_exit,
  338. };
  339. static int __init ip_rt_proc_init(void)
  340. {
  341. return register_pernet_subsys(&ip_rt_proc_ops);
  342. }
  343. #else
  344. static inline int ip_rt_proc_init(void)
  345. {
  346. return 0;
  347. }
  348. #endif /* CONFIG_PROC_FS */
  349. static inline bool rt_is_expired(const struct rtable *rth)
  350. {
  351. bool res;
  352. rcu_read_lock();
  353. res = rth->rt_genid != rt_genid_ipv4(dev_net_rcu(rth->dst.dev));
  354. rcu_read_unlock();
  355. return res;
  356. }
  357. void rt_cache_flush(struct net *net)
  358. {
  359. rt_genid_bump_ipv4(net);
  360. }
  361. static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
  362. struct sk_buff *skb,
  363. const void *daddr)
  364. {
  365. const struct rtable *rt = container_of(dst, struct rtable, dst);
  366. struct net_device *dev;
  367. struct neighbour *n;
  368. rcu_read_lock();
  369. dev = dst_dev_rcu(dst);
  370. if (likely(rt->rt_gw_family == AF_INET)) {
  371. n = ip_neigh_gw4(dev, rt->rt_gw4);
  372. } else if (rt->rt_gw_family == AF_INET6) {
  373. n = ip_neigh_gw6(dev, &rt->rt_gw6);
  374. } else {
  375. __be32 pkey;
  376. pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
  377. n = ip_neigh_gw4(dev, pkey);
  378. }
  379. if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
  380. n = NULL;
  381. rcu_read_unlock();
  382. return n;
  383. }
  384. static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
  385. {
  386. const struct rtable *rt = container_of(dst, struct rtable, dst);
  387. struct net_device *dev = dst_dev(dst);
  388. const __be32 *pkey = daddr;
  389. if (rt->rt_gw_family == AF_INET) {
  390. pkey = (const __be32 *)&rt->rt_gw4;
  391. } else if (rt->rt_gw_family == AF_INET6) {
  392. return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6);
  393. } else if (!daddr ||
  394. (rt->rt_flags &
  395. (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) {
  396. return;
  397. }
  398. __ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
  399. }
  400. /* Hash tables of size 2048..262144 depending on RAM size.
  401. * Each bucket uses 8 bytes.
  402. */
  403. static u32 ip_idents_mask __read_mostly;
  404. static atomic_t *ip_idents __read_mostly;
  405. static u32 *ip_tstamps __read_mostly;
  406. /* In order to protect privacy, we add a perturbation to identifiers
  407. * if one generator is seldom used. This makes hard for an attacker
  408. * to infer how many packets were sent between two points in time.
  409. */
  410. static u32 ip_idents_reserve(u32 hash, int segs)
  411. {
  412. u32 bucket, old, now = (u32)jiffies;
  413. atomic_t *p_id;
  414. u32 *p_tstamp;
  415. u32 delta = 0;
  416. bucket = hash & ip_idents_mask;
  417. p_tstamp = ip_tstamps + bucket;
  418. p_id = ip_idents + bucket;
  419. old = READ_ONCE(*p_tstamp);
  420. if (old != now && cmpxchg(p_tstamp, old, now) == old)
  421. delta = get_random_u32_below(now - old);
  422. /* If UBSAN reports an error there, please make sure your compiler
  423. * supports -fno-strict-overflow before reporting it that was a bug
  424. * in UBSAN, and it has been fixed in GCC-8.
  425. */
  426. return atomic_add_return(segs + delta, p_id) - segs;
  427. }
  428. void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
  429. {
  430. u32 hash, id;
  431. /* Note the following code is not safe, but this is okay. */
  432. if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key)))
  433. get_random_bytes(&net->ipv4.ip_id_key,
  434. sizeof(net->ipv4.ip_id_key));
  435. hash = siphash_3u32((__force u32)iph->daddr,
  436. (__force u32)iph->saddr,
  437. iph->protocol,
  438. &net->ipv4.ip_id_key);
  439. id = ip_idents_reserve(hash, segs);
  440. iph->id = htons(id);
  441. }
  442. EXPORT_SYMBOL(__ip_select_ident);
  443. static void __build_flow_key(const struct net *net, struct flowi4 *fl4,
  444. const struct sock *sk, const struct iphdr *iph,
  445. int oif, __u8 tos, u8 prot, u32 mark,
  446. int flow_flags)
  447. {
  448. __u8 scope = RT_SCOPE_UNIVERSE;
  449. if (sk) {
  450. oif = sk->sk_bound_dev_if;
  451. mark = READ_ONCE(sk->sk_mark);
  452. tos = ip_sock_rt_tos(sk);
  453. scope = ip_sock_rt_scope(sk);
  454. prot = inet_test_bit(HDRINCL, sk) ? IPPROTO_RAW :
  455. sk->sk_protocol;
  456. }
  457. flowi4_init_output(fl4, oif, mark, tos & INET_DSCP_MASK, scope,
  458. prot, flow_flags, iph->daddr, iph->saddr, 0, 0,
  459. sock_net_uid(net, sk));
  460. }
  461. static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
  462. const struct sock *sk)
  463. {
  464. const struct net *net = dev_net(skb->dev);
  465. const struct iphdr *iph = ip_hdr(skb);
  466. int oif = skb->dev->ifindex;
  467. u8 prot = iph->protocol;
  468. u32 mark = skb->mark;
  469. __u8 tos = iph->tos;
  470. __build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0);
  471. }
  472. static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
  473. {
  474. const struct inet_sock *inet = inet_sk(sk);
  475. const struct ip_options_rcu *inet_opt;
  476. __be32 daddr = inet->inet_daddr;
  477. rcu_read_lock();
  478. inet_opt = rcu_dereference(inet->inet_opt);
  479. if (inet_opt && inet_opt->opt.srr)
  480. daddr = inet_opt->opt.faddr;
  481. flowi4_init_output(fl4, sk->sk_bound_dev_if, READ_ONCE(sk->sk_mark),
  482. ip_sock_rt_tos(sk),
  483. ip_sock_rt_scope(sk),
  484. inet_test_bit(HDRINCL, sk) ?
  485. IPPROTO_RAW : sk->sk_protocol,
  486. inet_sk_flowi_flags(sk),
  487. daddr, inet->inet_saddr, 0, 0,
  488. sk_uid(sk));
  489. rcu_read_unlock();
  490. }
  491. static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
  492. const struct sk_buff *skb)
  493. {
  494. if (skb)
  495. build_skb_flow_key(fl4, skb, sk);
  496. else
  497. build_sk_flow_key(fl4, sk);
  498. }
  499. static DEFINE_SPINLOCK(fnhe_lock);
  500. static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
  501. {
  502. struct rtable *rt;
  503. rt = rcu_dereference(fnhe->fnhe_rth_input);
  504. if (rt) {
  505. RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
  506. dst_dev_put(&rt->dst);
  507. dst_release(&rt->dst);
  508. }
  509. rt = rcu_dereference(fnhe->fnhe_rth_output);
  510. if (rt) {
  511. RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
  512. dst_dev_put(&rt->dst);
  513. dst_release(&rt->dst);
  514. }
  515. }
  516. static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
  517. {
  518. struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
  519. struct fib_nh_exception *fnhe, *oldest = NULL;
  520. for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
  521. fnhe = rcu_dereference_protected(*fnhe_p,
  522. lockdep_is_held(&fnhe_lock));
  523. if (!fnhe)
  524. break;
  525. if (!oldest ||
  526. time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
  527. oldest = fnhe;
  528. oldest_p = fnhe_p;
  529. }
  530. }
  531. /* Clear oldest->fnhe_daddr to prevent this fnhe from being
  532. * rebound with new dsts in rt_bind_exception().
  533. */
  534. oldest->fnhe_daddr = 0;
  535. fnhe_flush_routes(oldest);
  536. *oldest_p = oldest->fnhe_next;
  537. kfree_rcu(oldest, rcu);
  538. }
  539. static u32 fnhe_hashfun(__be32 daddr)
  540. {
  541. static siphash_aligned_key_t fnhe_hash_key;
  542. u64 hval;
  543. net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key));
  544. hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key);
  545. return hash_64(hval, FNHE_HASH_SHIFT);
  546. }
  547. static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
  548. {
  549. rt->rt_pmtu = fnhe->fnhe_pmtu;
  550. rt->rt_mtu_locked = fnhe->fnhe_mtu_locked;
  551. rt->dst.expires = fnhe->fnhe_expires;
  552. if (fnhe->fnhe_gw) {
  553. rt->rt_flags |= RTCF_REDIRECTED;
  554. rt->rt_uses_gateway = 1;
  555. rt->rt_gw_family = AF_INET;
  556. rt->rt_gw4 = fnhe->fnhe_gw;
  557. }
  558. }
  559. static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr,
  560. __be32 gw, u32 pmtu, bool lock,
  561. unsigned long expires)
  562. {
  563. struct fnhe_hash_bucket *hash;
  564. struct fib_nh_exception *fnhe;
  565. struct rtable *rt;
  566. u32 genid, hval;
  567. unsigned int i;
  568. int depth;
  569. genid = fnhe_genid(dev_net(nhc->nhc_dev));
  570. hval = fnhe_hashfun(daddr);
  571. spin_lock_bh(&fnhe_lock);
  572. hash = rcu_dereference(nhc->nhc_exceptions);
  573. if (!hash) {
  574. hash = kzalloc_objs(*hash, FNHE_HASH_SIZE, GFP_ATOMIC);
  575. if (!hash)
  576. goto out_unlock;
  577. rcu_assign_pointer(nhc->nhc_exceptions, hash);
  578. }
  579. hash += hval;
  580. depth = 0;
  581. for (fnhe = rcu_dereference(hash->chain); fnhe;
  582. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  583. if (fnhe->fnhe_daddr == daddr)
  584. break;
  585. depth++;
  586. }
  587. if (fnhe) {
  588. if (fnhe->fnhe_genid != genid)
  589. fnhe->fnhe_genid = genid;
  590. if (gw)
  591. fnhe->fnhe_gw = gw;
  592. if (pmtu) {
  593. fnhe->fnhe_pmtu = pmtu;
  594. fnhe->fnhe_mtu_locked = lock;
  595. }
  596. fnhe->fnhe_expires = max(1UL, expires);
  597. /* Update all cached dsts too */
  598. rt = rcu_dereference(fnhe->fnhe_rth_input);
  599. if (rt)
  600. fill_route_from_fnhe(rt, fnhe);
  601. rt = rcu_dereference(fnhe->fnhe_rth_output);
  602. if (rt)
  603. fill_route_from_fnhe(rt, fnhe);
  604. } else {
  605. /* Randomize max depth to avoid some side channels attacks. */
  606. int max_depth = FNHE_RECLAIM_DEPTH +
  607. get_random_u32_below(FNHE_RECLAIM_DEPTH);
  608. while (depth > max_depth) {
  609. fnhe_remove_oldest(hash);
  610. depth--;
  611. }
  612. fnhe = kzalloc_obj(*fnhe, GFP_ATOMIC);
  613. if (!fnhe)
  614. goto out_unlock;
  615. fnhe->fnhe_next = hash->chain;
  616. fnhe->fnhe_genid = genid;
  617. fnhe->fnhe_daddr = daddr;
  618. fnhe->fnhe_gw = gw;
  619. fnhe->fnhe_pmtu = pmtu;
  620. fnhe->fnhe_mtu_locked = lock;
  621. fnhe->fnhe_expires = max(1UL, expires);
  622. rcu_assign_pointer(hash->chain, fnhe);
  623. /* Exception created; mark the cached routes for the nexthop
  624. * stale, so anyone caching it rechecks if this exception
  625. * applies to them.
  626. */
  627. rt = rcu_dereference(nhc->nhc_rth_input);
  628. if (rt)
  629. WRITE_ONCE(rt->dst.obsolete, DST_OBSOLETE_KILL);
  630. for_each_possible_cpu(i) {
  631. struct rtable __rcu **prt;
  632. prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i);
  633. rt = rcu_dereference(*prt);
  634. if (rt)
  635. WRITE_ONCE(rt->dst.obsolete, DST_OBSOLETE_KILL);
  636. }
  637. }
  638. fnhe->fnhe_stamp = jiffies;
  639. out_unlock:
  640. spin_unlock_bh(&fnhe_lock);
  641. }
  642. static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
  643. bool kill_route)
  644. {
  645. __be32 new_gw = icmp_hdr(skb)->un.gateway;
  646. __be32 old_gw = ip_hdr(skb)->saddr;
  647. struct net_device *dev = skb->dev;
  648. struct in_device *in_dev;
  649. struct fib_result res;
  650. struct neighbour *n;
  651. struct net *net;
  652. switch (icmp_hdr(skb)->code & 7) {
  653. case ICMP_REDIR_NET:
  654. case ICMP_REDIR_NETTOS:
  655. case ICMP_REDIR_HOST:
  656. case ICMP_REDIR_HOSTTOS:
  657. break;
  658. default:
  659. return;
  660. }
  661. if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw)
  662. return;
  663. in_dev = __in_dev_get_rcu(dev);
  664. if (!in_dev)
  665. return;
  666. net = dev_net(dev);
  667. if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
  668. ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
  669. ipv4_is_zeronet(new_gw))
  670. goto reject_redirect;
  671. if (!IN_DEV_SHARED_MEDIA(in_dev)) {
  672. if (!inet_addr_onlink(in_dev, new_gw, old_gw))
  673. goto reject_redirect;
  674. if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
  675. goto reject_redirect;
  676. } else {
  677. if (inet_addr_type(net, new_gw) != RTN_UNICAST)
  678. goto reject_redirect;
  679. }
  680. n = __ipv4_neigh_lookup(rt->dst.dev, (__force u32)new_gw);
  681. if (!n)
  682. n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev);
  683. if (!IS_ERR(n)) {
  684. if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
  685. neigh_event_send(n, NULL);
  686. } else {
  687. if (fib_lookup(net, fl4, &res, 0) == 0) {
  688. struct fib_nh_common *nhc;
  689. fib_select_path(net, &res, fl4, skb);
  690. nhc = FIB_RES_NHC(res);
  691. update_or_create_fnhe(nhc, fl4->daddr, new_gw,
  692. 0, false,
  693. jiffies + ip_rt_gc_timeout);
  694. }
  695. if (kill_route)
  696. WRITE_ONCE(rt->dst.obsolete, DST_OBSOLETE_KILL);
  697. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
  698. }
  699. neigh_release(n);
  700. }
  701. return;
  702. reject_redirect:
  703. #ifdef CONFIG_IP_ROUTE_VERBOSE
  704. if (IN_DEV_LOG_MARTIANS(in_dev)) {
  705. const struct iphdr *iph = (const struct iphdr *) skb->data;
  706. __be32 daddr = iph->daddr;
  707. __be32 saddr = iph->saddr;
  708. net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
  709. " Advised path = %pI4 -> %pI4\n",
  710. &old_gw, dev->name, &new_gw,
  711. &saddr, &daddr);
  712. }
  713. #endif
  714. ;
  715. }
  716. static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
  717. {
  718. struct rtable *rt;
  719. struct flowi4 fl4;
  720. const struct iphdr *iph = (const struct iphdr *) skb->data;
  721. struct net *net = dev_net(skb->dev);
  722. int oif = skb->dev->ifindex;
  723. u8 prot = iph->protocol;
  724. u32 mark = skb->mark;
  725. __u8 tos = iph->tos;
  726. rt = dst_rtable(dst);
  727. __build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0);
  728. __ip_do_redirect(rt, skb, &fl4, true);
  729. }
  730. static void ipv4_negative_advice(struct sock *sk,
  731. struct dst_entry *dst)
  732. {
  733. struct rtable *rt = dst_rtable(dst);
  734. if ((READ_ONCE(dst->obsolete) > 0) ||
  735. (rt->rt_flags & RTCF_REDIRECTED) ||
  736. READ_ONCE(rt->dst.expires))
  737. sk_dst_reset(sk);
  738. }
  739. /*
  740. * Algorithm:
  741. * 1. The first ip_rt_redirect_number redirects are sent
  742. * with exponential backoff, then we stop sending them at all,
  743. * assuming that the host ignores our redirects.
  744. * 2. If we did not see packets requiring redirects
  745. * during ip_rt_redirect_silence, we assume that the host
  746. * forgot redirected route and start to send redirects again.
  747. *
  748. * This algorithm is much cheaper and more intelligent than dumb load limiting
  749. * in icmp.c.
  750. *
  751. * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
  752. * and "frag. need" (breaks PMTU discovery) in icmp.c.
  753. */
  754. void ip_rt_send_redirect(struct sk_buff *skb)
  755. {
  756. struct rtable *rt = skb_rtable(skb);
  757. struct in_device *in_dev;
  758. struct inet_peer *peer;
  759. struct net *net;
  760. int log_martians;
  761. int vif;
  762. rcu_read_lock();
  763. in_dev = __in_dev_get_rcu(rt->dst.dev);
  764. if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
  765. rcu_read_unlock();
  766. return;
  767. }
  768. log_martians = IN_DEV_LOG_MARTIANS(in_dev);
  769. vif = l3mdev_master_ifindex_rcu(rt->dst.dev);
  770. net = dev_net(rt->dst.dev);
  771. peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif);
  772. if (!peer) {
  773. rcu_read_unlock();
  774. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST,
  775. rt_nexthop(rt, ip_hdr(skb)->daddr));
  776. return;
  777. }
  778. /* No redirected packets during ip_rt_redirect_silence;
  779. * reset the algorithm.
  780. */
  781. if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) {
  782. peer->rate_tokens = 0;
  783. peer->n_redirects = 0;
  784. }
  785. /* Too many ignored redirects; do not send anything
  786. * set dst.rate_last to the last seen redirected packet.
  787. */
  788. if (peer->n_redirects >= ip_rt_redirect_number) {
  789. peer->rate_last = jiffies;
  790. goto out_unlock;
  791. }
  792. /* Check for load limit; set rate_last to the latest sent
  793. * redirect.
  794. */
  795. if (peer->n_redirects == 0 ||
  796. time_after(jiffies,
  797. (peer->rate_last +
  798. (ip_rt_redirect_load << peer->n_redirects)))) {
  799. __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr);
  800. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw);
  801. peer->rate_last = jiffies;
  802. ++peer->n_redirects;
  803. if (IS_ENABLED(CONFIG_IP_ROUTE_VERBOSE) && log_martians &&
  804. peer->n_redirects == ip_rt_redirect_number)
  805. net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
  806. &ip_hdr(skb)->saddr, inet_iif(skb),
  807. &ip_hdr(skb)->daddr, &gw);
  808. }
  809. out_unlock:
  810. rcu_read_unlock();
  811. }
  812. static int ip_error(struct sk_buff *skb)
  813. {
  814. struct rtable *rt = skb_rtable(skb);
  815. struct net_device *dev = skb->dev;
  816. struct in_device *in_dev;
  817. struct inet_peer *peer;
  818. unsigned long now;
  819. struct net *net;
  820. SKB_DR(reason);
  821. bool send;
  822. int code;
  823. if (netif_is_l3_master(skb->dev)) {
  824. dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
  825. if (!dev)
  826. goto out;
  827. }
  828. in_dev = __in_dev_get_rcu(dev);
  829. /* IP on this device is disabled. */
  830. if (!in_dev)
  831. goto out;
  832. net = dev_net(rt->dst.dev);
  833. if (!IN_DEV_FORWARD(in_dev)) {
  834. switch (rt->dst.error) {
  835. case EHOSTUNREACH:
  836. SKB_DR_SET(reason, IP_INADDRERRORS);
  837. __IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS);
  838. break;
  839. case ENETUNREACH:
  840. SKB_DR_SET(reason, IP_INNOROUTES);
  841. __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
  842. break;
  843. }
  844. goto out;
  845. }
  846. switch (rt->dst.error) {
  847. case EINVAL:
  848. default:
  849. goto out;
  850. case EHOSTUNREACH:
  851. code = ICMP_HOST_UNREACH;
  852. break;
  853. case ENETUNREACH:
  854. code = ICMP_NET_UNREACH;
  855. SKB_DR_SET(reason, IP_INNOROUTES);
  856. __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
  857. break;
  858. case EACCES:
  859. code = ICMP_PKT_FILTERED;
  860. break;
  861. }
  862. rcu_read_lock();
  863. peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr,
  864. l3mdev_master_ifindex_rcu(skb->dev));
  865. send = true;
  866. if (peer) {
  867. now = jiffies;
  868. peer->rate_tokens += now - peer->rate_last;
  869. if (peer->rate_tokens > ip_rt_error_burst)
  870. peer->rate_tokens = ip_rt_error_burst;
  871. peer->rate_last = now;
  872. if (peer->rate_tokens >= ip_rt_error_cost)
  873. peer->rate_tokens -= ip_rt_error_cost;
  874. else
  875. send = false;
  876. }
  877. rcu_read_unlock();
  878. if (send)
  879. icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
  880. out: kfree_skb_reason(skb, reason);
  881. return 0;
  882. }
  883. static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
  884. {
  885. struct dst_entry *dst = &rt->dst;
  886. struct fib_result res;
  887. bool lock = false;
  888. struct net *net;
  889. u32 old_mtu;
  890. if (ip_mtu_locked(dst))
  891. return;
  892. old_mtu = ipv4_mtu(dst);
  893. if (old_mtu < mtu)
  894. return;
  895. rcu_read_lock();
  896. net = dst_dev_net_rcu(dst);
  897. if (mtu < net->ipv4.ip_rt_min_pmtu) {
  898. lock = true;
  899. mtu = min(old_mtu, net->ipv4.ip_rt_min_pmtu);
  900. }
  901. if (rt->rt_pmtu == mtu && !lock &&
  902. time_before(jiffies, READ_ONCE(dst->expires) -
  903. net->ipv4.ip_rt_mtu_expires / 2))
  904. goto out;
  905. if (fib_lookup(net, fl4, &res, 0) == 0) {
  906. struct fib_nh_common *nhc;
  907. fib_select_path(net, &res, fl4, NULL);
  908. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  909. if (fib_info_num_path(res.fi) > 1) {
  910. int nhsel;
  911. for (nhsel = 0; nhsel < fib_info_num_path(res.fi); nhsel++) {
  912. nhc = fib_info_nhc(res.fi, nhsel);
  913. update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
  914. jiffies + net->ipv4.ip_rt_mtu_expires);
  915. }
  916. goto out;
  917. }
  918. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  919. nhc = FIB_RES_NHC(res);
  920. update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
  921. jiffies + net->ipv4.ip_rt_mtu_expires);
  922. }
  923. out:
  924. rcu_read_unlock();
  925. }
  926. static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  927. struct sk_buff *skb, u32 mtu,
  928. bool confirm_neigh)
  929. {
  930. struct rtable *rt = dst_rtable(dst);
  931. struct flowi4 fl4;
  932. ip_rt_build_flow_key(&fl4, sk, skb);
  933. /* Don't make lookup fail for bridged encapsulations */
  934. if (skb && netif_is_any_bridge_port(skb->dev))
  935. fl4.flowi4_oif = 0;
  936. __ip_rt_update_pmtu(rt, &fl4, mtu);
  937. }
  938. void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
  939. int oif, u8 protocol)
  940. {
  941. const struct iphdr *iph = (const struct iphdr *)skb->data;
  942. struct flowi4 fl4;
  943. struct rtable *rt;
  944. u32 mark = IP4_REPLY_MARK(net, skb->mark);
  945. __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, mark,
  946. 0);
  947. rt = __ip_route_output_key(net, &fl4);
  948. if (!IS_ERR(rt)) {
  949. __ip_rt_update_pmtu(rt, &fl4, mtu);
  950. ip_rt_put(rt);
  951. }
  952. }
  953. EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
  954. static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
  955. {
  956. const struct iphdr *iph = (const struct iphdr *)skb->data;
  957. struct flowi4 fl4;
  958. struct rtable *rt;
  959. __build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0);
  960. if (!fl4.flowi4_mark)
  961. fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark);
  962. rt = __ip_route_output_key(sock_net(sk), &fl4);
  963. if (!IS_ERR(rt)) {
  964. __ip_rt_update_pmtu(rt, &fl4, mtu);
  965. ip_rt_put(rt);
  966. }
  967. }
  968. void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
  969. {
  970. const struct iphdr *iph = (const struct iphdr *)skb->data;
  971. struct flowi4 fl4;
  972. struct rtable *rt;
  973. struct dst_entry *odst = NULL;
  974. bool new = false;
  975. struct net *net = sock_net(sk);
  976. bh_lock_sock(sk);
  977. if (!ip_sk_accept_pmtu(sk))
  978. goto out;
  979. odst = sk_dst_get(sk);
  980. if (sock_owned_by_user(sk) || !odst) {
  981. __ipv4_sk_update_pmtu(skb, sk, mtu);
  982. goto out;
  983. }
  984. __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
  985. rt = dst_rtable(odst);
  986. if (READ_ONCE(odst->obsolete) && !odst->ops->check(odst, 0)) {
  987. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  988. if (IS_ERR(rt))
  989. goto out;
  990. new = true;
  991. }
  992. __ip_rt_update_pmtu(dst_rtable(xfrm_dst_path(&rt->dst)), &fl4, mtu);
  993. if (!dst_check(&rt->dst, 0)) {
  994. if (new)
  995. dst_release(&rt->dst);
  996. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  997. if (IS_ERR(rt))
  998. goto out;
  999. new = true;
  1000. }
  1001. if (new)
  1002. sk_dst_set(sk, &rt->dst);
  1003. out:
  1004. bh_unlock_sock(sk);
  1005. dst_release(odst);
  1006. }
  1007. EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
  1008. void ipv4_redirect(struct sk_buff *skb, struct net *net,
  1009. int oif, u8 protocol)
  1010. {
  1011. const struct iphdr *iph = (const struct iphdr *)skb->data;
  1012. struct flowi4 fl4;
  1013. struct rtable *rt;
  1014. __build_flow_key(net, &fl4, NULL, iph, oif, iph->tos, protocol, 0, 0);
  1015. rt = __ip_route_output_key(net, &fl4);
  1016. if (!IS_ERR(rt)) {
  1017. __ip_do_redirect(rt, skb, &fl4, false);
  1018. ip_rt_put(rt);
  1019. }
  1020. }
  1021. EXPORT_SYMBOL_GPL(ipv4_redirect);
  1022. void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
  1023. {
  1024. const struct iphdr *iph = (const struct iphdr *)skb->data;
  1025. struct flowi4 fl4;
  1026. struct rtable *rt;
  1027. struct net *net = sock_net(sk);
  1028. __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
  1029. rt = __ip_route_output_key(net, &fl4);
  1030. if (!IS_ERR(rt)) {
  1031. __ip_do_redirect(rt, skb, &fl4, false);
  1032. ip_rt_put(rt);
  1033. }
  1034. }
  1035. EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
  1036. INDIRECT_CALLABLE_SCOPE struct dst_entry *ipv4_dst_check(struct dst_entry *dst,
  1037. u32 cookie)
  1038. {
  1039. struct rtable *rt = dst_rtable(dst);
  1040. /* All IPV4 dsts are created with ->obsolete set to the value
  1041. * DST_OBSOLETE_FORCE_CHK which forces validation calls down
  1042. * into this function always.
  1043. *
  1044. * When a PMTU/redirect information update invalidates a route,
  1045. * this is indicated by setting obsolete to DST_OBSOLETE_KILL or
  1046. * DST_OBSOLETE_DEAD.
  1047. */
  1048. if (READ_ONCE(dst->obsolete) != DST_OBSOLETE_FORCE_CHK ||
  1049. rt_is_expired(rt))
  1050. return NULL;
  1051. return dst;
  1052. }
  1053. EXPORT_INDIRECT_CALLABLE(ipv4_dst_check);
  1054. static void ipv4_send_dest_unreach(struct sk_buff *skb)
  1055. {
  1056. struct inet_skb_parm parm;
  1057. struct net_device *dev;
  1058. int res;
  1059. /* Recompile ip options since IPCB may not be valid anymore.
  1060. * Also check we have a reasonable ipv4 header.
  1061. */
  1062. if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) ||
  1063. ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5)
  1064. return;
  1065. memset(&parm, 0, sizeof(parm));
  1066. if (ip_hdr(skb)->ihl > 5) {
  1067. if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4))
  1068. return;
  1069. parm.opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr);
  1070. rcu_read_lock();
  1071. dev = skb->dev ? skb->dev : skb_rtable(skb)->dst.dev;
  1072. res = __ip_options_compile(dev_net(dev), &parm.opt, skb, NULL);
  1073. rcu_read_unlock();
  1074. if (res)
  1075. return;
  1076. }
  1077. __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &parm);
  1078. }
  1079. static void ipv4_link_failure(struct sk_buff *skb)
  1080. {
  1081. struct rtable *rt;
  1082. ipv4_send_dest_unreach(skb);
  1083. rt = skb_rtable(skb);
  1084. if (rt)
  1085. dst_set_expires(&rt->dst, 0);
  1086. }
  1087. static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb)
  1088. {
  1089. pr_debug("%s: %pI4 -> %pI4, %s\n",
  1090. __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
  1091. skb->dev ? skb->dev->name : "?");
  1092. kfree_skb(skb);
  1093. WARN_ON(1);
  1094. return 0;
  1095. }
  1096. /*
  1097. * We do not cache source address of outgoing interface,
  1098. * because it is used only by IP RR, TS and SRR options,
  1099. * so that it out of fast path.
  1100. *
  1101. * BTW remember: "addr" is allowed to be not aligned
  1102. * in IP options!
  1103. */
  1104. void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
  1105. {
  1106. __be32 src;
  1107. if (rt_is_output_route(rt))
  1108. src = ip_hdr(skb)->saddr;
  1109. else {
  1110. struct fib_result res;
  1111. struct iphdr *iph = ip_hdr(skb);
  1112. struct flowi4 fl4 = {
  1113. .daddr = iph->daddr,
  1114. .saddr = iph->saddr,
  1115. .flowi4_dscp = ip4h_dscp(iph),
  1116. .flowi4_oif = rt->dst.dev->ifindex,
  1117. .flowi4_iif = skb->dev->ifindex,
  1118. .flowi4_mark = skb->mark,
  1119. };
  1120. rcu_read_lock();
  1121. if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0)
  1122. src = fib_result_prefsrc(dev_net(rt->dst.dev), &res);
  1123. else
  1124. src = inet_select_addr(rt->dst.dev,
  1125. rt_nexthop(rt, iph->daddr),
  1126. RT_SCOPE_UNIVERSE);
  1127. rcu_read_unlock();
  1128. }
  1129. memcpy(addr, &src, 4);
  1130. }
  1131. #ifdef CONFIG_IP_ROUTE_CLASSID
  1132. static void set_class_tag(struct rtable *rt, u32 tag)
  1133. {
  1134. if (!(rt->dst.tclassid & 0xFFFF))
  1135. rt->dst.tclassid |= tag & 0xFFFF;
  1136. if (!(rt->dst.tclassid & 0xFFFF0000))
  1137. rt->dst.tclassid |= tag & 0xFFFF0000;
  1138. }
  1139. #endif
  1140. static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
  1141. {
  1142. unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
  1143. unsigned int advmss;
  1144. struct net *net;
  1145. rcu_read_lock();
  1146. net = dst_dev_net_rcu(dst);
  1147. advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size,
  1148. net->ipv4.ip_rt_min_advmss);
  1149. rcu_read_unlock();
  1150. return min(advmss, IPV4_MAX_PMTU - header_size);
  1151. }
  1152. INDIRECT_CALLABLE_SCOPE unsigned int ipv4_mtu(const struct dst_entry *dst)
  1153. {
  1154. return ip_dst_mtu_maybe_forward(dst, false);
  1155. }
  1156. EXPORT_INDIRECT_CALLABLE(ipv4_mtu);
  1157. static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr)
  1158. {
  1159. struct fnhe_hash_bucket *hash;
  1160. struct fib_nh_exception *fnhe, __rcu **fnhe_p;
  1161. u32 hval = fnhe_hashfun(daddr);
  1162. spin_lock_bh(&fnhe_lock);
  1163. hash = rcu_dereference_protected(nhc->nhc_exceptions,
  1164. lockdep_is_held(&fnhe_lock));
  1165. hash += hval;
  1166. fnhe_p = &hash->chain;
  1167. fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock));
  1168. while (fnhe) {
  1169. if (fnhe->fnhe_daddr == daddr) {
  1170. rcu_assign_pointer(*fnhe_p, rcu_dereference_protected(
  1171. fnhe->fnhe_next, lockdep_is_held(&fnhe_lock)));
  1172. /* set fnhe_daddr to 0 to ensure it won't bind with
  1173. * new dsts in rt_bind_exception().
  1174. */
  1175. fnhe->fnhe_daddr = 0;
  1176. fnhe_flush_routes(fnhe);
  1177. kfree_rcu(fnhe, rcu);
  1178. break;
  1179. }
  1180. fnhe_p = &fnhe->fnhe_next;
  1181. fnhe = rcu_dereference_protected(fnhe->fnhe_next,
  1182. lockdep_is_held(&fnhe_lock));
  1183. }
  1184. spin_unlock_bh(&fnhe_lock);
  1185. }
  1186. static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc,
  1187. __be32 daddr)
  1188. {
  1189. struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions);
  1190. struct fib_nh_exception *fnhe;
  1191. u32 hval;
  1192. if (!hash)
  1193. return NULL;
  1194. hval = fnhe_hashfun(daddr);
  1195. for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
  1196. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  1197. if (fnhe->fnhe_daddr == daddr) {
  1198. if (fnhe->fnhe_expires &&
  1199. time_after(jiffies, fnhe->fnhe_expires)) {
  1200. ip_del_fnhe(nhc, daddr);
  1201. break;
  1202. }
  1203. return fnhe;
  1204. }
  1205. }
  1206. return NULL;
  1207. }
  1208. /* MTU selection:
  1209. * 1. mtu on route is locked - use it
  1210. * 2. mtu from nexthop exception
  1211. * 3. mtu from egress device
  1212. */
  1213. u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr)
  1214. {
  1215. struct fib_nh_common *nhc = res->nhc;
  1216. struct net_device *dev = nhc->nhc_dev;
  1217. struct fib_info *fi = res->fi;
  1218. u32 mtu = 0;
  1219. if (READ_ONCE(dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu) ||
  1220. fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU))
  1221. mtu = fi->fib_mtu;
  1222. if (likely(!mtu)) {
  1223. struct fib_nh_exception *fnhe;
  1224. fnhe = find_exception(nhc, daddr);
  1225. if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires))
  1226. mtu = fnhe->fnhe_pmtu;
  1227. }
  1228. if (likely(!mtu))
  1229. mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU);
  1230. return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu);
  1231. }
  1232. static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
  1233. __be32 daddr, const bool do_cache)
  1234. {
  1235. bool ret = false;
  1236. spin_lock_bh(&fnhe_lock);
  1237. if (daddr == fnhe->fnhe_daddr) {
  1238. struct rtable __rcu **porig;
  1239. struct rtable *orig;
  1240. int genid = fnhe_genid(dev_net(rt->dst.dev));
  1241. if (rt_is_input_route(rt))
  1242. porig = &fnhe->fnhe_rth_input;
  1243. else
  1244. porig = &fnhe->fnhe_rth_output;
  1245. orig = rcu_dereference(*porig);
  1246. if (fnhe->fnhe_genid != genid) {
  1247. fnhe->fnhe_genid = genid;
  1248. fnhe->fnhe_gw = 0;
  1249. fnhe->fnhe_pmtu = 0;
  1250. fnhe->fnhe_expires = 0;
  1251. fnhe->fnhe_mtu_locked = false;
  1252. fnhe_flush_routes(fnhe);
  1253. orig = NULL;
  1254. }
  1255. fill_route_from_fnhe(rt, fnhe);
  1256. if (!rt->rt_gw4) {
  1257. rt->rt_gw4 = daddr;
  1258. rt->rt_gw_family = AF_INET;
  1259. }
  1260. if (do_cache) {
  1261. dst_hold(&rt->dst);
  1262. rcu_assign_pointer(*porig, rt);
  1263. if (orig) {
  1264. dst_dev_put(&orig->dst);
  1265. dst_release(&orig->dst);
  1266. }
  1267. ret = true;
  1268. }
  1269. fnhe->fnhe_stamp = jiffies;
  1270. }
  1271. spin_unlock_bh(&fnhe_lock);
  1272. return ret;
  1273. }
  1274. static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt)
  1275. {
  1276. struct rtable *orig, *prev, **p;
  1277. bool ret = true;
  1278. if (rt_is_input_route(rt)) {
  1279. p = (struct rtable **)&nhc->nhc_rth_input;
  1280. } else {
  1281. p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
  1282. }
  1283. orig = *p;
  1284. /* hold dst before doing cmpxchg() to avoid race condition
  1285. * on this dst
  1286. */
  1287. dst_hold(&rt->dst);
  1288. prev = cmpxchg(p, orig, rt);
  1289. if (prev == orig) {
  1290. if (orig) {
  1291. rt_add_uncached_list(orig);
  1292. dst_release(&orig->dst);
  1293. }
  1294. } else {
  1295. dst_release(&rt->dst);
  1296. ret = false;
  1297. }
  1298. return ret;
  1299. }
  1300. struct uncached_list {
  1301. spinlock_t lock;
  1302. struct list_head head;
  1303. };
  1304. static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
  1305. void rt_add_uncached_list(struct rtable *rt)
  1306. {
  1307. struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
  1308. rt->dst.rt_uncached_list = ul;
  1309. spin_lock_bh(&ul->lock);
  1310. list_add_tail(&rt->dst.rt_uncached, &ul->head);
  1311. spin_unlock_bh(&ul->lock);
  1312. }
  1313. void rt_del_uncached_list(struct rtable *rt)
  1314. {
  1315. struct uncached_list *ul = rt->dst.rt_uncached_list;
  1316. if (ul) {
  1317. spin_lock_bh(&ul->lock);
  1318. list_del_init(&rt->dst.rt_uncached);
  1319. spin_unlock_bh(&ul->lock);
  1320. }
  1321. }
  1322. static void ipv4_dst_destroy(struct dst_entry *dst)
  1323. {
  1324. ip_dst_metrics_put(dst);
  1325. rt_del_uncached_list(dst_rtable(dst));
  1326. }
  1327. void rt_flush_dev(struct net_device *dev)
  1328. {
  1329. struct rtable *rt, *safe;
  1330. int cpu;
  1331. for_each_possible_cpu(cpu) {
  1332. struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
  1333. if (list_empty(&ul->head))
  1334. continue;
  1335. spin_lock_bh(&ul->lock);
  1336. list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
  1337. if (rt->dst.dev != dev)
  1338. continue;
  1339. rt->dst.dev = blackhole_netdev;
  1340. netdev_ref_replace(dev, blackhole_netdev,
  1341. &rt->dst.dev_tracker, GFP_ATOMIC);
  1342. list_del_init(&rt->dst.rt_uncached);
  1343. }
  1344. spin_unlock_bh(&ul->lock);
  1345. }
  1346. }
  1347. static bool rt_cache_valid(const struct rtable *rt)
  1348. {
  1349. return rt &&
  1350. READ_ONCE(rt->dst.obsolete) == DST_OBSOLETE_FORCE_CHK &&
  1351. !rt_is_expired(rt);
  1352. }
  1353. static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
  1354. const struct fib_result *res,
  1355. struct fib_nh_exception *fnhe,
  1356. struct fib_info *fi, u16 type, u32 itag,
  1357. const bool do_cache)
  1358. {
  1359. bool cached = false;
  1360. if (fi) {
  1361. struct fib_nh_common *nhc = FIB_RES_NHC(*res);
  1362. if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) {
  1363. rt->rt_uses_gateway = 1;
  1364. rt->rt_gw_family = nhc->nhc_gw_family;
  1365. /* only INET and INET6 are supported */
  1366. if (likely(nhc->nhc_gw_family == AF_INET))
  1367. rt->rt_gw4 = nhc->nhc_gw.ipv4;
  1368. else
  1369. rt->rt_gw6 = nhc->nhc_gw.ipv6;
  1370. }
  1371. ip_dst_init_metrics(&rt->dst, fi->fib_metrics);
  1372. #ifdef CONFIG_IP_ROUTE_CLASSID
  1373. if (nhc->nhc_family == AF_INET) {
  1374. struct fib_nh *nh;
  1375. nh = container_of(nhc, struct fib_nh, nh_common);
  1376. rt->dst.tclassid = nh->nh_tclassid;
  1377. }
  1378. #endif
  1379. rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
  1380. if (unlikely(fnhe))
  1381. cached = rt_bind_exception(rt, fnhe, daddr, do_cache);
  1382. else if (do_cache)
  1383. cached = rt_cache_route(nhc, rt);
  1384. if (unlikely(!cached)) {
  1385. /* Routes we intend to cache in nexthop exception or
  1386. * FIB nexthop have the DST_NOCACHE bit clear.
  1387. * However, if we are unsuccessful at storing this
  1388. * route into the cache we really need to set it.
  1389. */
  1390. if (!rt->rt_gw4) {
  1391. rt->rt_gw_family = AF_INET;
  1392. rt->rt_gw4 = daddr;
  1393. }
  1394. rt_add_uncached_list(rt);
  1395. }
  1396. } else
  1397. rt_add_uncached_list(rt);
  1398. #ifdef CONFIG_IP_ROUTE_CLASSID
  1399. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1400. set_class_tag(rt, res->tclassid);
  1401. #endif
  1402. set_class_tag(rt, itag);
  1403. #endif
  1404. }
  1405. struct rtable *rt_dst_alloc(struct net_device *dev,
  1406. unsigned int flags, u16 type,
  1407. bool noxfrm)
  1408. {
  1409. struct rtable *rt;
  1410. rt = dst_alloc(&ipv4_dst_ops, dev, DST_OBSOLETE_FORCE_CHK,
  1411. (noxfrm ? DST_NOXFRM : 0));
  1412. if (rt) {
  1413. rt->rt_genid = rt_genid_ipv4(dev_net(dev));
  1414. rt->rt_flags = flags;
  1415. rt->rt_type = type;
  1416. rt->rt_is_input = 0;
  1417. rt->rt_iif = 0;
  1418. rt->rt_pmtu = 0;
  1419. rt->rt_mtu_locked = 0;
  1420. rt->rt_uses_gateway = 0;
  1421. rt->rt_gw_family = 0;
  1422. rt->rt_gw4 = 0;
  1423. rt->dst.output = ip_output;
  1424. if (flags & RTCF_LOCAL)
  1425. rt->dst.input = ip_local_deliver;
  1426. }
  1427. return rt;
  1428. }
  1429. EXPORT_SYMBOL(rt_dst_alloc);
  1430. struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt)
  1431. {
  1432. struct rtable *new_rt;
  1433. new_rt = dst_alloc(&ipv4_dst_ops, dev, DST_OBSOLETE_FORCE_CHK,
  1434. rt->dst.flags);
  1435. if (new_rt) {
  1436. new_rt->rt_genid = rt_genid_ipv4(dev_net(dev));
  1437. new_rt->rt_flags = rt->rt_flags;
  1438. new_rt->rt_type = rt->rt_type;
  1439. new_rt->rt_is_input = rt->rt_is_input;
  1440. new_rt->rt_iif = rt->rt_iif;
  1441. new_rt->rt_pmtu = rt->rt_pmtu;
  1442. new_rt->rt_mtu_locked = rt->rt_mtu_locked;
  1443. new_rt->rt_gw_family = rt->rt_gw_family;
  1444. if (rt->rt_gw_family == AF_INET)
  1445. new_rt->rt_gw4 = rt->rt_gw4;
  1446. else if (rt->rt_gw_family == AF_INET6)
  1447. new_rt->rt_gw6 = rt->rt_gw6;
  1448. new_rt->dst.input = READ_ONCE(rt->dst.input);
  1449. new_rt->dst.output = READ_ONCE(rt->dst.output);
  1450. new_rt->dst.error = rt->dst.error;
  1451. new_rt->dst.lastuse = jiffies;
  1452. new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate);
  1453. }
  1454. return new_rt;
  1455. }
  1456. EXPORT_SYMBOL(rt_dst_clone);
  1457. /* called in rcu_read_lock() section */
  1458. enum skb_drop_reason
  1459. ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1460. dscp_t dscp, struct net_device *dev,
  1461. struct in_device *in_dev, u32 *itag)
  1462. {
  1463. enum skb_drop_reason reason;
  1464. /* Primary sanity checks. */
  1465. if (!in_dev)
  1466. return SKB_DROP_REASON_NOT_SPECIFIED;
  1467. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
  1468. return SKB_DROP_REASON_IP_INVALID_SOURCE;
  1469. if (skb->protocol != htons(ETH_P_IP))
  1470. return SKB_DROP_REASON_INVALID_PROTO;
  1471. if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev))
  1472. return SKB_DROP_REASON_IP_LOCALNET;
  1473. if (ipv4_is_zeronet(saddr)) {
  1474. if (!ipv4_is_local_multicast(daddr) &&
  1475. ip_hdr(skb)->protocol != IPPROTO_IGMP)
  1476. return SKB_DROP_REASON_IP_INVALID_SOURCE;
  1477. } else {
  1478. reason = fib_validate_source_reason(skb, saddr, 0, dscp, 0,
  1479. dev, in_dev, itag);
  1480. if (reason)
  1481. return reason;
  1482. }
  1483. return SKB_NOT_DROPPED_YET;
  1484. }
  1485. /* called in rcu_read_lock() section */
  1486. static enum skb_drop_reason
  1487. ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1488. dscp_t dscp, struct net_device *dev, int our)
  1489. {
  1490. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1491. unsigned int flags = RTCF_MULTICAST;
  1492. enum skb_drop_reason reason;
  1493. struct rtable *rth;
  1494. u32 itag = 0;
  1495. reason = ip_mc_validate_source(skb, daddr, saddr, dscp, dev, in_dev,
  1496. &itag);
  1497. if (reason)
  1498. return reason;
  1499. if (our)
  1500. flags |= RTCF_LOCAL;
  1501. if (IN_DEV_ORCONF(in_dev, NOPOLICY))
  1502. IPCB(skb)->flags |= IPSKB_NOPOLICY;
  1503. rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST,
  1504. false);
  1505. if (!rth)
  1506. return SKB_DROP_REASON_NOMEM;
  1507. #ifdef CONFIG_IP_ROUTE_CLASSID
  1508. rth->dst.tclassid = itag;
  1509. #endif
  1510. rth->dst.output = ip_rt_bug;
  1511. rth->rt_is_input= 1;
  1512. #ifdef CONFIG_IP_MROUTE
  1513. if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
  1514. rth->dst.input = ip_mr_input;
  1515. #endif
  1516. RT_CACHE_STAT_INC(in_slow_mc);
  1517. skb_dst_drop(skb);
  1518. skb_dst_set(skb, &rth->dst);
  1519. return SKB_NOT_DROPPED_YET;
  1520. }
  1521. static void ip_handle_martian_source(struct net_device *dev,
  1522. struct in_device *in_dev,
  1523. struct sk_buff *skb,
  1524. __be32 daddr,
  1525. __be32 saddr)
  1526. {
  1527. RT_CACHE_STAT_INC(in_martian_src);
  1528. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1529. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
  1530. /*
  1531. * RFC1812 recommendation, if source is martian,
  1532. * the only hint is MAC header.
  1533. */
  1534. pr_warn("martian source (src=%pI4, dst=%pI4, dev=%s)\n",
  1535. &saddr, &daddr, dev->name);
  1536. if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
  1537. print_hex_dump(KERN_WARNING, "ll header: ",
  1538. DUMP_PREFIX_OFFSET, 16, 1,
  1539. skb_mac_header(skb),
  1540. dev->hard_header_len, false);
  1541. }
  1542. }
  1543. #endif
  1544. }
  1545. /* called in rcu_read_lock() section */
  1546. static enum skb_drop_reason
  1547. __mkroute_input(struct sk_buff *skb, const struct fib_result *res,
  1548. struct in_device *in_dev, __be32 daddr,
  1549. __be32 saddr, dscp_t dscp)
  1550. {
  1551. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1552. struct fib_nh_common *nhc = FIB_RES_NHC(*res);
  1553. struct net_device *dev = nhc->nhc_dev;
  1554. struct fib_nh_exception *fnhe;
  1555. struct rtable *rth;
  1556. int err;
  1557. struct in_device *out_dev;
  1558. bool do_cache;
  1559. u32 itag = 0;
  1560. /* get a working reference to the output device */
  1561. out_dev = __in_dev_get_rcu(dev);
  1562. if (!out_dev) {
  1563. net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
  1564. return reason;
  1565. }
  1566. err = fib_validate_source(skb, saddr, daddr, dscp, FIB_RES_OIF(*res),
  1567. in_dev->dev, in_dev, &itag);
  1568. if (err < 0) {
  1569. reason = -err;
  1570. ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
  1571. saddr);
  1572. goto cleanup;
  1573. }
  1574. do_cache = res->fi && !itag;
  1575. if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
  1576. skb->protocol == htons(ETH_P_IP)) {
  1577. __be32 gw;
  1578. gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0;
  1579. if (IN_DEV_SHARED_MEDIA(out_dev) ||
  1580. inet_addr_onlink(out_dev, saddr, gw))
  1581. IPCB(skb)->flags |= IPSKB_DOREDIRECT;
  1582. }
  1583. if (skb->protocol != htons(ETH_P_IP)) {
  1584. /* Not IP (i.e. ARP). Do not create route, if it is
  1585. * invalid for proxy arp. DNAT routes are always valid.
  1586. *
  1587. * Proxy arp feature have been extended to allow, ARP
  1588. * replies back to the same interface, to support
  1589. * Private VLAN switch technologies. See arp.c.
  1590. */
  1591. if (out_dev == in_dev &&
  1592. IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
  1593. reason = SKB_DROP_REASON_ARP_PVLAN_DISABLE;
  1594. goto cleanup;
  1595. }
  1596. }
  1597. if (IN_DEV_ORCONF(in_dev, NOPOLICY))
  1598. IPCB(skb)->flags |= IPSKB_NOPOLICY;
  1599. fnhe = find_exception(nhc, daddr);
  1600. if (do_cache) {
  1601. if (fnhe)
  1602. rth = rcu_dereference(fnhe->fnhe_rth_input);
  1603. else
  1604. rth = rcu_dereference(nhc->nhc_rth_input);
  1605. if (rt_cache_valid(rth)) {
  1606. skb_dst_set_noref(skb, &rth->dst);
  1607. goto out;
  1608. }
  1609. }
  1610. rth = rt_dst_alloc(out_dev->dev, 0, res->type,
  1611. IN_DEV_ORCONF(out_dev, NOXFRM));
  1612. if (!rth) {
  1613. reason = SKB_DROP_REASON_NOMEM;
  1614. goto cleanup;
  1615. }
  1616. rth->rt_is_input = 1;
  1617. RT_CACHE_STAT_INC(in_slow_tot);
  1618. rth->dst.input = ip_forward;
  1619. rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag,
  1620. do_cache);
  1621. lwtunnel_set_redirect(&rth->dst);
  1622. skb_dst_set(skb, &rth->dst);
  1623. out:
  1624. reason = SKB_NOT_DROPPED_YET;
  1625. cleanup:
  1626. return reason;
  1627. }
  1628. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1629. /* To make ICMP packets follow the right flow, the multipath hash is
  1630. * calculated from the inner IP addresses.
  1631. */
  1632. static void ip_multipath_l3_keys(const struct sk_buff *skb,
  1633. struct flow_keys *hash_keys)
  1634. {
  1635. const struct iphdr *outer_iph = ip_hdr(skb);
  1636. const struct iphdr *key_iph = outer_iph;
  1637. const struct iphdr *inner_iph;
  1638. const struct icmphdr *icmph;
  1639. struct iphdr _inner_iph;
  1640. struct icmphdr _icmph;
  1641. if (likely(outer_iph->protocol != IPPROTO_ICMP))
  1642. goto out;
  1643. if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0))
  1644. goto out;
  1645. icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph),
  1646. &_icmph);
  1647. if (!icmph)
  1648. goto out;
  1649. if (!icmp_is_err(icmph->type))
  1650. goto out;
  1651. inner_iph = skb_header_pointer(skb,
  1652. outer_iph->ihl * 4 + sizeof(_icmph),
  1653. sizeof(_inner_iph), &_inner_iph);
  1654. if (!inner_iph)
  1655. goto out;
  1656. key_iph = inner_iph;
  1657. out:
  1658. hash_keys->addrs.v4addrs.src = key_iph->saddr;
  1659. hash_keys->addrs.v4addrs.dst = key_iph->daddr;
  1660. }
  1661. static u32 fib_multipath_custom_hash_outer(const struct net *net,
  1662. const struct sk_buff *skb,
  1663. bool *p_has_inner)
  1664. {
  1665. u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
  1666. struct flow_keys keys, hash_keys;
  1667. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
  1668. return 0;
  1669. memset(&hash_keys, 0, sizeof(hash_keys));
  1670. skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
  1671. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1672. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
  1673. hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
  1674. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
  1675. hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
  1676. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
  1677. hash_keys.basic.ip_proto = keys.basic.ip_proto;
  1678. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
  1679. hash_keys.ports.src = keys.ports.src;
  1680. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
  1681. hash_keys.ports.dst = keys.ports.dst;
  1682. *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
  1683. return fib_multipath_hash_from_keys(net, &hash_keys);
  1684. }
  1685. static u32 fib_multipath_custom_hash_inner(const struct net *net,
  1686. const struct sk_buff *skb,
  1687. bool has_inner)
  1688. {
  1689. u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
  1690. struct flow_keys keys, hash_keys;
  1691. /* We assume the packet carries an encapsulation, but if none was
  1692. * encountered during dissection of the outer flow, then there is no
  1693. * point in calling the flow dissector again.
  1694. */
  1695. if (!has_inner)
  1696. return 0;
  1697. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
  1698. return 0;
  1699. memset(&hash_keys, 0, sizeof(hash_keys));
  1700. skb_flow_dissect_flow_keys(skb, &keys, 0);
  1701. if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
  1702. return 0;
  1703. if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  1704. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1705. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
  1706. hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
  1707. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
  1708. hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
  1709. } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  1710. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  1711. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
  1712. hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
  1713. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
  1714. hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
  1715. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
  1716. hash_keys.tags.flow_label = keys.tags.flow_label;
  1717. }
  1718. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
  1719. hash_keys.basic.ip_proto = keys.basic.ip_proto;
  1720. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
  1721. hash_keys.ports.src = keys.ports.src;
  1722. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
  1723. hash_keys.ports.dst = keys.ports.dst;
  1724. return fib_multipath_hash_from_keys(net, &hash_keys);
  1725. }
  1726. static u32 fib_multipath_custom_hash_skb(const struct net *net,
  1727. const struct sk_buff *skb)
  1728. {
  1729. u32 mhash, mhash_inner;
  1730. bool has_inner = true;
  1731. mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner);
  1732. mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner);
  1733. return jhash_2words(mhash, mhash_inner, 0);
  1734. }
  1735. static u32 fib_multipath_custom_hash_fl4(const struct net *net,
  1736. const struct flowi4 *fl4)
  1737. {
  1738. u32 hash_fields = READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_fields);
  1739. struct flow_keys hash_keys;
  1740. if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
  1741. return 0;
  1742. memset(&hash_keys, 0, sizeof(hash_keys));
  1743. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1744. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
  1745. hash_keys.addrs.v4addrs.src = fl4->saddr;
  1746. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
  1747. hash_keys.addrs.v4addrs.dst = fl4->daddr;
  1748. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
  1749. hash_keys.basic.ip_proto = fl4->flowi4_proto;
  1750. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) {
  1751. if (fl4->flowi4_flags & FLOWI_FLAG_ANY_SPORT)
  1752. hash_keys.ports.src = (__force __be16)get_random_u16();
  1753. else
  1754. hash_keys.ports.src = fl4->fl4_sport;
  1755. }
  1756. if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
  1757. hash_keys.ports.dst = fl4->fl4_dport;
  1758. return fib_multipath_hash_from_keys(net, &hash_keys);
  1759. }
  1760. /* if skb is set it will be used and fl4 can be NULL */
  1761. int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
  1762. const struct sk_buff *skb, struct flow_keys *flkeys)
  1763. {
  1764. u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
  1765. struct flow_keys hash_keys;
  1766. u32 mhash = 0;
  1767. switch (READ_ONCE(net->ipv4.sysctl_fib_multipath_hash_policy)) {
  1768. case 0:
  1769. memset(&hash_keys, 0, sizeof(hash_keys));
  1770. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1771. if (skb) {
  1772. ip_multipath_l3_keys(skb, &hash_keys);
  1773. } else {
  1774. hash_keys.addrs.v4addrs.src = fl4->saddr;
  1775. hash_keys.addrs.v4addrs.dst = fl4->daddr;
  1776. }
  1777. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  1778. break;
  1779. case 1:
  1780. /* skb is currently provided only when forwarding */
  1781. if (skb) {
  1782. unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
  1783. struct flow_keys keys;
  1784. /* short-circuit if we already have L4 hash present */
  1785. if (skb->l4_hash)
  1786. return skb_get_hash_raw(skb) >> 1;
  1787. memset(&hash_keys, 0, sizeof(hash_keys));
  1788. if (!flkeys) {
  1789. skb_flow_dissect_flow_keys(skb, &keys, flag);
  1790. flkeys = &keys;
  1791. }
  1792. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1793. hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
  1794. hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
  1795. hash_keys.ports.src = flkeys->ports.src;
  1796. hash_keys.ports.dst = flkeys->ports.dst;
  1797. hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
  1798. } else {
  1799. memset(&hash_keys, 0, sizeof(hash_keys));
  1800. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1801. hash_keys.addrs.v4addrs.src = fl4->saddr;
  1802. hash_keys.addrs.v4addrs.dst = fl4->daddr;
  1803. if (fl4->flowi4_flags & FLOWI_FLAG_ANY_SPORT)
  1804. hash_keys.ports.src = (__force __be16)get_random_u16();
  1805. else
  1806. hash_keys.ports.src = fl4->fl4_sport;
  1807. hash_keys.ports.dst = fl4->fl4_dport;
  1808. hash_keys.basic.ip_proto = fl4->flowi4_proto;
  1809. }
  1810. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  1811. break;
  1812. case 2:
  1813. memset(&hash_keys, 0, sizeof(hash_keys));
  1814. /* skb is currently provided only when forwarding */
  1815. if (skb) {
  1816. struct flow_keys keys;
  1817. skb_flow_dissect_flow_keys(skb, &keys, 0);
  1818. /* Inner can be v4 or v6 */
  1819. if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  1820. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1821. hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
  1822. hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
  1823. } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  1824. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  1825. hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
  1826. hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
  1827. hash_keys.tags.flow_label = keys.tags.flow_label;
  1828. hash_keys.basic.ip_proto = keys.basic.ip_proto;
  1829. } else {
  1830. /* Same as case 0 */
  1831. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1832. ip_multipath_l3_keys(skb, &hash_keys);
  1833. }
  1834. } else {
  1835. /* Same as case 0 */
  1836. hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  1837. hash_keys.addrs.v4addrs.src = fl4->saddr;
  1838. hash_keys.addrs.v4addrs.dst = fl4->daddr;
  1839. }
  1840. mhash = fib_multipath_hash_from_keys(net, &hash_keys);
  1841. break;
  1842. case 3:
  1843. if (skb)
  1844. mhash = fib_multipath_custom_hash_skb(net, skb);
  1845. else
  1846. mhash = fib_multipath_custom_hash_fl4(net, fl4);
  1847. break;
  1848. }
  1849. if (multipath_hash)
  1850. mhash = jhash_2words(mhash, multipath_hash, 0);
  1851. return mhash >> 1;
  1852. }
  1853. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  1854. static enum skb_drop_reason
  1855. ip_mkroute_input(struct sk_buff *skb, struct fib_result *res,
  1856. struct in_device *in_dev, __be32 daddr,
  1857. __be32 saddr, dscp_t dscp, struct flow_keys *hkeys)
  1858. {
  1859. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1860. if (res->fi && fib_info_num_path(res->fi) > 1) {
  1861. int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys);
  1862. fib_select_multipath(res, h, NULL);
  1863. IPCB(skb)->flags |= IPSKB_MULTIPATH;
  1864. }
  1865. #endif
  1866. /* create a routing cache entry */
  1867. return __mkroute_input(skb, res, in_dev, daddr, saddr, dscp);
  1868. }
  1869. /* Implements all the saddr-related checks as ip_route_input_slow(),
  1870. * assuming daddr is valid and the destination is not a local broadcast one.
  1871. * Uses the provided hint instead of performing a route lookup.
  1872. */
  1873. enum skb_drop_reason
  1874. ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1875. dscp_t dscp, struct net_device *dev,
  1876. const struct sk_buff *hint)
  1877. {
  1878. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1879. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1880. struct rtable *rt = skb_rtable(hint);
  1881. struct net *net = dev_net(dev);
  1882. u32 tag = 0;
  1883. if (!in_dev)
  1884. return reason;
  1885. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) {
  1886. reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
  1887. goto martian_source;
  1888. }
  1889. if (ipv4_is_zeronet(saddr)) {
  1890. reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
  1891. goto martian_source;
  1892. }
  1893. if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
  1894. reason = SKB_DROP_REASON_IP_LOCALNET;
  1895. goto martian_source;
  1896. }
  1897. if (!(rt->rt_flags & RTCF_LOCAL))
  1898. goto skip_validate_source;
  1899. reason = fib_validate_source_reason(skb, saddr, daddr, dscp, 0, dev,
  1900. in_dev, &tag);
  1901. if (reason)
  1902. goto martian_source;
  1903. skip_validate_source:
  1904. skb_dst_copy(skb, hint);
  1905. return SKB_NOT_DROPPED_YET;
  1906. martian_source:
  1907. ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
  1908. return reason;
  1909. }
  1910. /* get device for dst_alloc with local routes */
  1911. static struct net_device *ip_rt_get_dev(struct net *net,
  1912. const struct fib_result *res)
  1913. {
  1914. struct fib_nh_common *nhc = res->fi ? res->nhc : NULL;
  1915. struct net_device *dev = NULL;
  1916. if (nhc)
  1917. dev = l3mdev_master_dev_rcu(nhc->nhc_dev);
  1918. return dev ? : net->loopback_dev;
  1919. }
  1920. /*
  1921. * NOTE. We drop all the packets that has local source
  1922. * addresses, because every properly looped back packet
  1923. * must have correct destination already attached by output routine.
  1924. * Changes in the enforced policies must be applied also to
  1925. * ip_route_use_hint().
  1926. *
  1927. * Such approach solves two big problems:
  1928. * 1. Not simplex devices are handled properly.
  1929. * 2. IP spoofing attempts are filtered with 100% of guarantee.
  1930. * called with rcu_read_lock()
  1931. */
  1932. static enum skb_drop_reason
  1933. ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1934. dscp_t dscp, struct net_device *dev,
  1935. struct fib_result *res)
  1936. {
  1937. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1938. struct in_device *in_dev = __in_dev_get_rcu(dev);
  1939. struct flow_keys *flkeys = NULL, _flkeys;
  1940. struct net *net = dev_net(dev);
  1941. struct ip_tunnel_info *tun_info;
  1942. int err = -EINVAL;
  1943. unsigned int flags = 0;
  1944. u32 itag = 0;
  1945. struct rtable *rth;
  1946. struct flowi4 fl4;
  1947. bool do_cache = true;
  1948. /* IP on this device is disabled. */
  1949. if (!in_dev)
  1950. goto out;
  1951. /* Check for the most weird martians, which can be not detected
  1952. * by fib_lookup.
  1953. */
  1954. tun_info = skb_tunnel_info(skb);
  1955. if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
  1956. fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id;
  1957. else
  1958. fl4.flowi4_tun_key.tun_id = 0;
  1959. skb_dst_drop(skb);
  1960. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) {
  1961. reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
  1962. goto martian_source;
  1963. }
  1964. res->fi = NULL;
  1965. res->table = NULL;
  1966. if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
  1967. goto brd_input;
  1968. /* Accept zero addresses only to limited broadcast;
  1969. * I even do not know to fix it or not. Waiting for complains :-)
  1970. */
  1971. if (ipv4_is_zeronet(saddr)) {
  1972. reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
  1973. goto martian_source;
  1974. }
  1975. if (ipv4_is_zeronet(daddr)) {
  1976. reason = SKB_DROP_REASON_IP_INVALID_DEST;
  1977. goto martian_destination;
  1978. }
  1979. /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
  1980. * and call it once if daddr or/and saddr are loopback addresses
  1981. */
  1982. if (ipv4_is_loopback(daddr)) {
  1983. if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
  1984. reason = SKB_DROP_REASON_IP_LOCALNET;
  1985. goto martian_destination;
  1986. }
  1987. } else if (ipv4_is_loopback(saddr)) {
  1988. if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
  1989. reason = SKB_DROP_REASON_IP_LOCALNET;
  1990. goto martian_source;
  1991. }
  1992. }
  1993. /*
  1994. * Now we are ready to route packet.
  1995. */
  1996. fl4.flowi4_l3mdev = 0;
  1997. fl4.flowi4_oif = 0;
  1998. fl4.flowi4_iif = dev->ifindex;
  1999. fl4.flowi4_mark = skb->mark;
  2000. fl4.flowi4_dscp = dscp;
  2001. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  2002. fl4.flowi4_flags = 0;
  2003. fl4.daddr = daddr;
  2004. fl4.saddr = saddr;
  2005. fl4.flowi4_uid = sock_net_uid(net, NULL);
  2006. fl4.flowi4_multipath_hash = 0;
  2007. if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) {
  2008. flkeys = &_flkeys;
  2009. } else {
  2010. fl4.flowi4_proto = 0;
  2011. fl4.fl4_sport = 0;
  2012. fl4.fl4_dport = 0;
  2013. }
  2014. err = fib_lookup(net, &fl4, res, 0);
  2015. if (err != 0) {
  2016. if (!IN_DEV_FORWARD(in_dev))
  2017. err = -EHOSTUNREACH;
  2018. goto no_route;
  2019. }
  2020. if (res->type == RTN_BROADCAST) {
  2021. if (IN_DEV_BFORWARD(in_dev))
  2022. goto make_route;
  2023. /* not do cache if bc_forwarding is enabled */
  2024. if (IPV4_DEVCONF_ALL_RO(net, BC_FORWARDING))
  2025. do_cache = false;
  2026. goto brd_input;
  2027. }
  2028. err = -EINVAL;
  2029. if (res->type == RTN_LOCAL) {
  2030. reason = fib_validate_source_reason(skb, saddr, daddr, dscp,
  2031. 0, dev, in_dev, &itag);
  2032. if (reason)
  2033. goto martian_source;
  2034. goto local_input;
  2035. }
  2036. if (!IN_DEV_FORWARD(in_dev)) {
  2037. err = -EHOSTUNREACH;
  2038. goto no_route;
  2039. }
  2040. if (res->type != RTN_UNICAST) {
  2041. reason = SKB_DROP_REASON_IP_INVALID_DEST;
  2042. goto martian_destination;
  2043. }
  2044. make_route:
  2045. reason = ip_mkroute_input(skb, res, in_dev, daddr, saddr, dscp,
  2046. flkeys);
  2047. out:
  2048. return reason;
  2049. brd_input:
  2050. if (skb->protocol != htons(ETH_P_IP)) {
  2051. reason = SKB_DROP_REASON_INVALID_PROTO;
  2052. goto out;
  2053. }
  2054. if (!ipv4_is_zeronet(saddr)) {
  2055. reason = fib_validate_source_reason(skb, saddr, 0, dscp, 0,
  2056. dev, in_dev, &itag);
  2057. if (reason)
  2058. goto martian_source;
  2059. }
  2060. flags |= RTCF_BROADCAST;
  2061. res->type = RTN_BROADCAST;
  2062. RT_CACHE_STAT_INC(in_brd);
  2063. local_input:
  2064. if (IN_DEV_ORCONF(in_dev, NOPOLICY))
  2065. IPCB(skb)->flags |= IPSKB_NOPOLICY;
  2066. do_cache &= res->fi && !itag;
  2067. if (do_cache) {
  2068. struct fib_nh_common *nhc = FIB_RES_NHC(*res);
  2069. rth = rcu_dereference(nhc->nhc_rth_input);
  2070. if (rt_cache_valid(rth)) {
  2071. skb_dst_set_noref(skb, &rth->dst);
  2072. reason = SKB_NOT_DROPPED_YET;
  2073. goto out;
  2074. }
  2075. }
  2076. rth = rt_dst_alloc(ip_rt_get_dev(net, res),
  2077. flags | RTCF_LOCAL, res->type, false);
  2078. if (!rth)
  2079. goto e_nobufs;
  2080. rth->dst.output= ip_rt_bug;
  2081. #ifdef CONFIG_IP_ROUTE_CLASSID
  2082. rth->dst.tclassid = itag;
  2083. #endif
  2084. rth->rt_is_input = 1;
  2085. RT_CACHE_STAT_INC(in_slow_tot);
  2086. if (res->type == RTN_UNREACHABLE) {
  2087. rth->dst.input= ip_error;
  2088. rth->dst.error= -err;
  2089. rth->rt_flags &= ~RTCF_LOCAL;
  2090. }
  2091. if (do_cache) {
  2092. struct fib_nh_common *nhc = FIB_RES_NHC(*res);
  2093. rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
  2094. if (lwtunnel_input_redirect(rth->dst.lwtstate)) {
  2095. WARN_ON(rth->dst.input == lwtunnel_input);
  2096. rth->dst.lwtstate->orig_input = rth->dst.input;
  2097. rth->dst.input = lwtunnel_input;
  2098. }
  2099. if (unlikely(!rt_cache_route(nhc, rth)))
  2100. rt_add_uncached_list(rth);
  2101. }
  2102. skb_dst_set(skb, &rth->dst);
  2103. reason = SKB_NOT_DROPPED_YET;
  2104. goto out;
  2105. no_route:
  2106. RT_CACHE_STAT_INC(in_no_route);
  2107. res->type = RTN_UNREACHABLE;
  2108. res->fi = NULL;
  2109. res->table = NULL;
  2110. goto local_input;
  2111. /*
  2112. * Do not cache martian addresses: they should be logged (RFC1812)
  2113. */
  2114. martian_destination:
  2115. RT_CACHE_STAT_INC(in_martian_dst);
  2116. #ifdef CONFIG_IP_ROUTE_VERBOSE
  2117. if (IN_DEV_LOG_MARTIANS(in_dev))
  2118. net_warn_ratelimited("martian destination (src=%pI4, dst=%pI4, dev=%s)\n",
  2119. &saddr, &daddr, dev->name);
  2120. #endif
  2121. goto out;
  2122. e_nobufs:
  2123. reason = SKB_DROP_REASON_NOMEM;
  2124. goto out;
  2125. martian_source:
  2126. ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
  2127. goto out;
  2128. }
  2129. /* called with rcu_read_lock held */
  2130. static enum skb_drop_reason
  2131. ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  2132. dscp_t dscp, struct net_device *dev,
  2133. struct fib_result *res)
  2134. {
  2135. /* Multicast recognition logic is moved from route cache to here.
  2136. * The problem was that too many Ethernet cards have broken/missing
  2137. * hardware multicast filters :-( As result the host on multicasting
  2138. * network acquires a lot of useless route cache entries, sort of
  2139. * SDR messages from all the world. Now we try to get rid of them.
  2140. * Really, provided software IP multicast filter is organized
  2141. * reasonably (at least, hashed), it does not result in a slowdown
  2142. * comparing with route cache reject entries.
  2143. * Note, that multicast routers are not affected, because
  2144. * route cache entry is created eventually.
  2145. */
  2146. if (ipv4_is_multicast(daddr)) {
  2147. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  2148. struct in_device *in_dev = __in_dev_get_rcu(dev);
  2149. int our = 0;
  2150. if (!in_dev)
  2151. return reason;
  2152. our = ip_check_mc_rcu(in_dev, daddr, saddr,
  2153. ip_hdr(skb)->protocol);
  2154. /* check l3 master if no match yet */
  2155. if (!our && netif_is_l3_slave(dev)) {
  2156. struct in_device *l3_in_dev;
  2157. l3_in_dev = __in_dev_get_rcu(skb->dev);
  2158. if (l3_in_dev)
  2159. our = ip_check_mc_rcu(l3_in_dev, daddr, saddr,
  2160. ip_hdr(skb)->protocol);
  2161. }
  2162. if (our
  2163. #ifdef CONFIG_IP_MROUTE
  2164. ||
  2165. (!ipv4_is_local_multicast(daddr) &&
  2166. IN_DEV_MFORWARD(in_dev))
  2167. #endif
  2168. ) {
  2169. reason = ip_route_input_mc(skb, daddr, saddr, dscp,
  2170. dev, our);
  2171. }
  2172. return reason;
  2173. }
  2174. return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res);
  2175. }
  2176. enum skb_drop_reason ip_route_input_noref(struct sk_buff *skb, __be32 daddr,
  2177. __be32 saddr, dscp_t dscp,
  2178. struct net_device *dev)
  2179. {
  2180. enum skb_drop_reason reason;
  2181. struct fib_result res;
  2182. rcu_read_lock();
  2183. reason = ip_route_input_rcu(skb, daddr, saddr, dscp, dev, &res);
  2184. rcu_read_unlock();
  2185. return reason;
  2186. }
  2187. EXPORT_SYMBOL(ip_route_input_noref);
  2188. /* called with rcu_read_lock() */
  2189. static struct rtable *__mkroute_output(const struct fib_result *res,
  2190. const struct flowi4 *fl4, int orig_oif,
  2191. struct net_device *dev_out,
  2192. unsigned int flags)
  2193. {
  2194. struct fib_info *fi = res->fi;
  2195. struct fib_nh_exception *fnhe;
  2196. struct in_device *in_dev;
  2197. u16 type = res->type;
  2198. struct rtable *rth;
  2199. bool do_cache;
  2200. in_dev = __in_dev_get_rcu(dev_out);
  2201. if (!in_dev)
  2202. return ERR_PTR(-EINVAL);
  2203. if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
  2204. if (ipv4_is_loopback(fl4->saddr) &&
  2205. !(dev_out->flags & IFF_LOOPBACK) &&
  2206. !netif_is_l3_master(dev_out))
  2207. return ERR_PTR(-EINVAL);
  2208. if (ipv4_is_lbcast(fl4->daddr)) {
  2209. type = RTN_BROADCAST;
  2210. /* reset fi to prevent gateway resolution */
  2211. fi = NULL;
  2212. } else if (ipv4_is_multicast(fl4->daddr)) {
  2213. type = RTN_MULTICAST;
  2214. } else if (ipv4_is_zeronet(fl4->daddr)) {
  2215. return ERR_PTR(-EINVAL);
  2216. }
  2217. if (dev_out->flags & IFF_LOOPBACK)
  2218. flags |= RTCF_LOCAL;
  2219. do_cache = true;
  2220. if (type == RTN_BROADCAST) {
  2221. flags |= RTCF_BROADCAST | RTCF_LOCAL;
  2222. } else if (type == RTN_MULTICAST) {
  2223. flags |= RTCF_MULTICAST | RTCF_LOCAL;
  2224. if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
  2225. fl4->flowi4_proto))
  2226. flags &= ~RTCF_LOCAL;
  2227. else
  2228. do_cache = false;
  2229. /* If multicast route do not exist use
  2230. * default one, but do not gateway in this case.
  2231. * Yes, it is hack.
  2232. */
  2233. if (fi && res->prefixlen < 4)
  2234. fi = NULL;
  2235. } else if ((type == RTN_LOCAL) && (orig_oif != 0) &&
  2236. (orig_oif != dev_out->ifindex)) {
  2237. /* For local routes that require a particular output interface
  2238. * we do not want to cache the result. Caching the result
  2239. * causes incorrect behaviour when there are multiple source
  2240. * addresses on the interface, the end result being that if the
  2241. * intended recipient is waiting on that interface for the
  2242. * packet he won't receive it because it will be delivered on
  2243. * the loopback interface and the IP_PKTINFO ipi_ifindex will
  2244. * be set to the loopback interface as well.
  2245. */
  2246. do_cache = false;
  2247. }
  2248. fnhe = NULL;
  2249. do_cache &= fi != NULL;
  2250. if (fi) {
  2251. struct fib_nh_common *nhc = FIB_RES_NHC(*res);
  2252. struct rtable __rcu **prth;
  2253. fnhe = find_exception(nhc, fl4->daddr);
  2254. if (!do_cache)
  2255. goto add;
  2256. if (fnhe) {
  2257. prth = &fnhe->fnhe_rth_output;
  2258. } else {
  2259. if (unlikely(fl4->flowi4_flags &
  2260. FLOWI_FLAG_KNOWN_NH &&
  2261. !(nhc->nhc_gw_family &&
  2262. nhc->nhc_scope == RT_SCOPE_LINK))) {
  2263. do_cache = false;
  2264. goto add;
  2265. }
  2266. prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
  2267. }
  2268. rth = rcu_dereference(*prth);
  2269. if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst))
  2270. return rth;
  2271. }
  2272. add:
  2273. rth = rt_dst_alloc(dev_out, flags, type,
  2274. IN_DEV_ORCONF(in_dev, NOXFRM));
  2275. if (!rth)
  2276. return ERR_PTR(-ENOBUFS);
  2277. rth->rt_iif = orig_oif;
  2278. RT_CACHE_STAT_INC(out_slow_tot);
  2279. if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  2280. if (flags & RTCF_LOCAL &&
  2281. !(dev_out->flags & IFF_LOOPBACK)) {
  2282. rth->dst.output = ip_mc_output;
  2283. RT_CACHE_STAT_INC(out_slow_mc);
  2284. }
  2285. #ifdef CONFIG_IP_MROUTE
  2286. if (type == RTN_MULTICAST) {
  2287. if (IN_DEV_MFORWARD(in_dev) &&
  2288. !ipv4_is_local_multicast(fl4->daddr)) {
  2289. rth->dst.input = ip_mr_input;
  2290. rth->dst.output = ip_mr_output;
  2291. }
  2292. }
  2293. #endif
  2294. }
  2295. rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache);
  2296. lwtunnel_set_redirect(&rth->dst);
  2297. return rth;
  2298. }
  2299. /*
  2300. * Major route resolver routine.
  2301. */
  2302. struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
  2303. const struct sk_buff *skb)
  2304. {
  2305. struct fib_result res = {
  2306. .type = RTN_UNSPEC,
  2307. .fi = NULL,
  2308. .table = NULL,
  2309. .tclassid = 0,
  2310. };
  2311. struct rtable *rth;
  2312. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  2313. rcu_read_lock();
  2314. rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb);
  2315. rcu_read_unlock();
  2316. return rth;
  2317. }
  2318. EXPORT_SYMBOL_GPL(ip_route_output_key_hash);
  2319. struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4,
  2320. struct fib_result *res,
  2321. const struct sk_buff *skb)
  2322. {
  2323. struct net_device *dev_out = NULL;
  2324. int orig_oif = fl4->flowi4_oif;
  2325. unsigned int flags = 0;
  2326. struct rtable *rth;
  2327. int err;
  2328. if (fl4->saddr) {
  2329. if (ipv4_is_multicast(fl4->saddr) ||
  2330. ipv4_is_lbcast(fl4->saddr)) {
  2331. rth = ERR_PTR(-EINVAL);
  2332. goto out;
  2333. }
  2334. rth = ERR_PTR(-ENETUNREACH);
  2335. /* I removed check for oif == dev_out->oif here.
  2336. * It was wrong for two reasons:
  2337. * 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
  2338. * is assigned to multiple interfaces.
  2339. * 2. Moreover, we are allowed to send packets with saddr
  2340. * of another iface. --ANK
  2341. */
  2342. if (fl4->flowi4_oif == 0 &&
  2343. (ipv4_is_multicast(fl4->daddr) ||
  2344. ipv4_is_lbcast(fl4->daddr))) {
  2345. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  2346. dev_out = __ip_dev_find(net, fl4->saddr, false);
  2347. if (!dev_out)
  2348. goto out;
  2349. /* Special hack: user can direct multicasts
  2350. * and limited broadcast via necessary interface
  2351. * without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
  2352. * This hack is not just for fun, it allows
  2353. * vic,vat and friends to work.
  2354. * They bind socket to loopback, set ttl to zero
  2355. * and expect that it will work.
  2356. * From the viewpoint of routing cache they are broken,
  2357. * because we are not allowed to build multicast path
  2358. * with loopback source addr (look, routing cache
  2359. * cannot know, that ttl is zero, so that packet
  2360. * will not leave this host and route is valid).
  2361. * Luckily, this hack is good workaround.
  2362. */
  2363. fl4->flowi4_oif = dev_out->ifindex;
  2364. goto make_route;
  2365. }
  2366. if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
  2367. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  2368. if (!__ip_dev_find(net, fl4->saddr, false))
  2369. goto out;
  2370. }
  2371. }
  2372. if (fl4->flowi4_oif) {
  2373. dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
  2374. rth = ERR_PTR(-ENODEV);
  2375. if (!dev_out)
  2376. goto out;
  2377. /* RACE: Check return value of inet_select_addr instead. */
  2378. if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
  2379. rth = ERR_PTR(-ENETUNREACH);
  2380. goto out;
  2381. }
  2382. if (ipv4_is_local_multicast(fl4->daddr) ||
  2383. ipv4_is_lbcast(fl4->daddr) ||
  2384. fl4->flowi4_proto == IPPROTO_IGMP) {
  2385. if (!fl4->saddr)
  2386. fl4->saddr = inet_select_addr(dev_out, 0,
  2387. RT_SCOPE_LINK);
  2388. goto make_route;
  2389. }
  2390. if (!fl4->saddr) {
  2391. if (ipv4_is_multicast(fl4->daddr))
  2392. fl4->saddr = inet_select_addr(dev_out, 0,
  2393. fl4->flowi4_scope);
  2394. else if (!fl4->daddr)
  2395. fl4->saddr = inet_select_addr(dev_out, 0,
  2396. RT_SCOPE_HOST);
  2397. }
  2398. }
  2399. if (!fl4->daddr) {
  2400. fl4->daddr = fl4->saddr;
  2401. if (!fl4->daddr)
  2402. fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
  2403. dev_out = net->loopback_dev;
  2404. fl4->flowi4_oif = LOOPBACK_IFINDEX;
  2405. res->type = RTN_LOCAL;
  2406. flags |= RTCF_LOCAL;
  2407. goto make_route;
  2408. }
  2409. err = fib_lookup(net, fl4, res, 0);
  2410. if (err) {
  2411. res->fi = NULL;
  2412. res->table = NULL;
  2413. if (fl4->flowi4_oif &&
  2414. (ipv4_is_multicast(fl4->daddr) || !fl4->flowi4_l3mdev)) {
  2415. /* Apparently, routing tables are wrong. Assume,
  2416. * that the destination is on link.
  2417. *
  2418. * WHY? DW.
  2419. * Because we are allowed to send to iface
  2420. * even if it has NO routes and NO assigned
  2421. * addresses. When oif is specified, routing
  2422. * tables are looked up with only one purpose:
  2423. * to catch if destination is gatewayed, rather than
  2424. * direct. Moreover, if MSG_DONTROUTE is set,
  2425. * we send packet, ignoring both routing tables
  2426. * and ifaddr state. --ANK
  2427. *
  2428. *
  2429. * We could make it even if oif is unknown,
  2430. * likely IPv6, but we do not.
  2431. */
  2432. if (fl4->saddr == 0)
  2433. fl4->saddr = inet_select_addr(dev_out, 0,
  2434. RT_SCOPE_LINK);
  2435. res->type = RTN_UNICAST;
  2436. goto make_route;
  2437. }
  2438. rth = ERR_PTR(err);
  2439. goto out;
  2440. }
  2441. if (res->type == RTN_LOCAL) {
  2442. if (!fl4->saddr) {
  2443. if (res->fi->fib_prefsrc)
  2444. fl4->saddr = res->fi->fib_prefsrc;
  2445. else
  2446. fl4->saddr = fl4->daddr;
  2447. }
  2448. /* L3 master device is the loopback for that domain */
  2449. dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? :
  2450. net->loopback_dev;
  2451. /* make sure orig_oif points to fib result device even
  2452. * though packet rx/tx happens over loopback or l3mdev
  2453. */
  2454. orig_oif = FIB_RES_OIF(*res);
  2455. fl4->flowi4_oif = dev_out->ifindex;
  2456. flags |= RTCF_LOCAL;
  2457. goto make_route;
  2458. }
  2459. fib_select_path(net, res, fl4, skb);
  2460. dev_out = FIB_RES_DEV(*res);
  2461. make_route:
  2462. rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags);
  2463. out:
  2464. return rth;
  2465. }
  2466. static struct dst_ops ipv4_dst_blackhole_ops = {
  2467. .family = AF_INET,
  2468. .default_advmss = ipv4_default_advmss,
  2469. .neigh_lookup = ipv4_neigh_lookup,
  2470. .check = dst_blackhole_check,
  2471. .cow_metrics = dst_blackhole_cow_metrics,
  2472. .update_pmtu = dst_blackhole_update_pmtu,
  2473. .redirect = dst_blackhole_redirect,
  2474. .mtu = dst_blackhole_mtu,
  2475. };
  2476. struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  2477. {
  2478. struct rtable *ort = dst_rtable(dst_orig);
  2479. struct rtable *rt;
  2480. rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, DST_OBSOLETE_DEAD, 0);
  2481. if (rt) {
  2482. struct dst_entry *new = &rt->dst;
  2483. new->__use = 1;
  2484. new->input = dst_discard;
  2485. new->output = dst_discard_out;
  2486. new->dev = net->loopback_dev;
  2487. netdev_hold(new->dev, &new->dev_tracker, GFP_ATOMIC);
  2488. rt->rt_is_input = ort->rt_is_input;
  2489. rt->rt_iif = ort->rt_iif;
  2490. rt->rt_pmtu = ort->rt_pmtu;
  2491. rt->rt_mtu_locked = ort->rt_mtu_locked;
  2492. rt->rt_genid = rt_genid_ipv4(net);
  2493. rt->rt_flags = ort->rt_flags;
  2494. rt->rt_type = ort->rt_type;
  2495. rt->rt_uses_gateway = ort->rt_uses_gateway;
  2496. rt->rt_gw_family = ort->rt_gw_family;
  2497. if (rt->rt_gw_family == AF_INET)
  2498. rt->rt_gw4 = ort->rt_gw4;
  2499. else if (rt->rt_gw_family == AF_INET6)
  2500. rt->rt_gw6 = ort->rt_gw6;
  2501. }
  2502. dst_release(dst_orig);
  2503. return rt ? &rt->dst : ERR_PTR(-ENOMEM);
  2504. }
  2505. struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
  2506. const struct sock *sk)
  2507. {
  2508. struct rtable *rt = __ip_route_output_key(net, flp4);
  2509. if (IS_ERR(rt))
  2510. return rt;
  2511. if (flp4->flowi4_proto) {
  2512. flp4->flowi4_oif = rt->dst.dev->ifindex;
  2513. rt = dst_rtable(xfrm_lookup_route(net, &rt->dst,
  2514. flowi4_to_flowi(flp4),
  2515. sk, 0));
  2516. }
  2517. return rt;
  2518. }
  2519. EXPORT_SYMBOL_GPL(ip_route_output_flow);
  2520. /* called with rcu_read_lock held */
  2521. static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
  2522. struct rtable *rt, u32 table_id, dscp_t dscp,
  2523. struct flowi4 *fl4, struct sk_buff *skb, u32 portid,
  2524. u32 seq, unsigned int flags)
  2525. {
  2526. struct rtmsg *r;
  2527. struct nlmsghdr *nlh;
  2528. unsigned long expires = 0;
  2529. u32 error;
  2530. u32 metrics[RTAX_MAX];
  2531. nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags);
  2532. if (!nlh)
  2533. return -EMSGSIZE;
  2534. r = nlmsg_data(nlh);
  2535. r->rtm_family = AF_INET;
  2536. r->rtm_dst_len = 32;
  2537. r->rtm_src_len = 0;
  2538. r->rtm_tos = inet_dscp_to_dsfield(dscp);
  2539. r->rtm_table = table_id < 256 ? table_id : RT_TABLE_COMPAT;
  2540. if (nla_put_u32(skb, RTA_TABLE, table_id))
  2541. goto nla_put_failure;
  2542. r->rtm_type = rt->rt_type;
  2543. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2544. r->rtm_protocol = RTPROT_UNSPEC;
  2545. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  2546. if (rt->rt_flags & RTCF_NOTIFY)
  2547. r->rtm_flags |= RTM_F_NOTIFY;
  2548. if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
  2549. r->rtm_flags |= RTCF_DOREDIRECT;
  2550. if (nla_put_in_addr(skb, RTA_DST, dst))
  2551. goto nla_put_failure;
  2552. if (src) {
  2553. r->rtm_src_len = 32;
  2554. if (nla_put_in_addr(skb, RTA_SRC, src))
  2555. goto nla_put_failure;
  2556. }
  2557. if (rt->dst.dev &&
  2558. nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
  2559. goto nla_put_failure;
  2560. if (lwtunnel_fill_encap(skb, rt->dst.lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
  2561. goto nla_put_failure;
  2562. #ifdef CONFIG_IP_ROUTE_CLASSID
  2563. if (rt->dst.tclassid &&
  2564. nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
  2565. goto nla_put_failure;
  2566. #endif
  2567. if (fl4 && !rt_is_input_route(rt) &&
  2568. fl4->saddr != src) {
  2569. if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
  2570. goto nla_put_failure;
  2571. }
  2572. if (rt->rt_uses_gateway) {
  2573. if (rt->rt_gw_family == AF_INET &&
  2574. nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) {
  2575. goto nla_put_failure;
  2576. } else if (rt->rt_gw_family == AF_INET6) {
  2577. int alen = sizeof(struct in6_addr);
  2578. struct nlattr *nla;
  2579. struct rtvia *via;
  2580. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  2581. if (!nla)
  2582. goto nla_put_failure;
  2583. via = nla_data(nla);
  2584. via->rtvia_family = AF_INET6;
  2585. memcpy(via->rtvia_addr, &rt->rt_gw6, alen);
  2586. }
  2587. }
  2588. expires = READ_ONCE(rt->dst.expires);
  2589. if (expires) {
  2590. unsigned long now = jiffies;
  2591. if (time_before(now, expires))
  2592. expires -= now;
  2593. else
  2594. expires = 0;
  2595. }
  2596. memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
  2597. if (rt->rt_pmtu && expires)
  2598. metrics[RTAX_MTU - 1] = rt->rt_pmtu;
  2599. if (rt->rt_mtu_locked && expires)
  2600. metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU);
  2601. if (rtnetlink_put_metrics(skb, metrics) < 0)
  2602. goto nla_put_failure;
  2603. if (fl4) {
  2604. if (fl4->flowi4_mark &&
  2605. nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark))
  2606. goto nla_put_failure;
  2607. if (!uid_eq(fl4->flowi4_uid, INVALID_UID) &&
  2608. nla_put_u32(skb, RTA_UID,
  2609. from_kuid_munged(current_user_ns(),
  2610. fl4->flowi4_uid)))
  2611. goto nla_put_failure;
  2612. if (rt_is_input_route(rt)) {
  2613. #ifdef CONFIG_IP_MROUTE
  2614. if (ipv4_is_multicast(dst) &&
  2615. !ipv4_is_local_multicast(dst) &&
  2616. IPV4_DEVCONF_ALL_RO(net, MC_FORWARDING)) {
  2617. int err = ipmr_get_route(net, skb,
  2618. fl4->saddr, fl4->daddr,
  2619. r, portid);
  2620. if (err <= 0) {
  2621. if (err == 0)
  2622. return 0;
  2623. goto nla_put_failure;
  2624. }
  2625. } else
  2626. #endif
  2627. if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif))
  2628. goto nla_put_failure;
  2629. }
  2630. }
  2631. error = rt->dst.error;
  2632. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
  2633. goto nla_put_failure;
  2634. nlmsg_end(skb, nlh);
  2635. return 0;
  2636. nla_put_failure:
  2637. nlmsg_cancel(skb, nlh);
  2638. return -EMSGSIZE;
  2639. }
  2640. static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb,
  2641. struct netlink_callback *cb, u32 table_id,
  2642. struct fnhe_hash_bucket *bucket, int genid,
  2643. int *fa_index, int fa_start, unsigned int flags)
  2644. {
  2645. int i;
  2646. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  2647. struct fib_nh_exception *fnhe;
  2648. for (fnhe = rcu_dereference(bucket[i].chain); fnhe;
  2649. fnhe = rcu_dereference(fnhe->fnhe_next)) {
  2650. struct rtable *rt;
  2651. int err;
  2652. if (*fa_index < fa_start)
  2653. goto next;
  2654. if (fnhe->fnhe_genid != genid)
  2655. goto next;
  2656. if (fnhe->fnhe_expires &&
  2657. time_after(jiffies, fnhe->fnhe_expires))
  2658. goto next;
  2659. rt = rcu_dereference(fnhe->fnhe_rth_input);
  2660. if (!rt)
  2661. rt = rcu_dereference(fnhe->fnhe_rth_output);
  2662. if (!rt)
  2663. goto next;
  2664. err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt,
  2665. table_id, 0, NULL, skb,
  2666. NETLINK_CB(cb->skb).portid,
  2667. cb->nlh->nlmsg_seq, flags);
  2668. if (err)
  2669. return err;
  2670. next:
  2671. (*fa_index)++;
  2672. }
  2673. }
  2674. return 0;
  2675. }
  2676. int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
  2677. u32 table_id, struct fib_info *fi,
  2678. int *fa_index, int fa_start, unsigned int flags)
  2679. {
  2680. struct net *net = sock_net(cb->skb->sk);
  2681. int nhsel, genid = fnhe_genid(net);
  2682. for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
  2683. struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel);
  2684. struct fnhe_hash_bucket *bucket;
  2685. int err;
  2686. if (nhc->nhc_flags & RTNH_F_DEAD)
  2687. continue;
  2688. rcu_read_lock();
  2689. bucket = rcu_dereference(nhc->nhc_exceptions);
  2690. err = 0;
  2691. if (bucket)
  2692. err = fnhe_dump_bucket(net, skb, cb, table_id, bucket,
  2693. genid, fa_index, fa_start,
  2694. flags);
  2695. rcu_read_unlock();
  2696. if (err)
  2697. return err;
  2698. }
  2699. return 0;
  2700. }
  2701. static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst,
  2702. u8 ip_proto, __be16 sport,
  2703. __be16 dport)
  2704. {
  2705. struct sk_buff *skb;
  2706. struct iphdr *iph;
  2707. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2708. if (!skb)
  2709. return NULL;
  2710. /* Reserve room for dummy headers, this skb can pass
  2711. * through good chunk of routing engine.
  2712. */
  2713. skb_reset_mac_header(skb);
  2714. skb_reset_network_header(skb);
  2715. skb->protocol = htons(ETH_P_IP);
  2716. iph = skb_put(skb, sizeof(struct iphdr));
  2717. iph->protocol = ip_proto;
  2718. iph->saddr = src;
  2719. iph->daddr = dst;
  2720. iph->version = 0x4;
  2721. iph->frag_off = 0;
  2722. iph->ihl = 0x5;
  2723. skb_set_transport_header(skb, skb->len);
  2724. switch (iph->protocol) {
  2725. case IPPROTO_UDP: {
  2726. struct udphdr *udph;
  2727. udph = skb_put_zero(skb, sizeof(struct udphdr));
  2728. udph->source = sport;
  2729. udph->dest = dport;
  2730. udph->len = htons(sizeof(struct udphdr));
  2731. udph->check = 0;
  2732. break;
  2733. }
  2734. case IPPROTO_TCP: {
  2735. struct tcphdr *tcph;
  2736. tcph = skb_put_zero(skb, sizeof(struct tcphdr));
  2737. tcph->source = sport;
  2738. tcph->dest = dport;
  2739. tcph->doff = sizeof(struct tcphdr) / 4;
  2740. tcph->rst = 1;
  2741. tcph->check = ~tcp_v4_check(sizeof(struct tcphdr),
  2742. src, dst, 0);
  2743. break;
  2744. }
  2745. case IPPROTO_ICMP: {
  2746. struct icmphdr *icmph;
  2747. icmph = skb_put_zero(skb, sizeof(struct icmphdr));
  2748. icmph->type = ICMP_ECHO;
  2749. icmph->code = 0;
  2750. }
  2751. }
  2752. return skb;
  2753. }
  2754. static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
  2755. const struct nlmsghdr *nlh,
  2756. struct nlattr **tb,
  2757. struct netlink_ext_ack *extack)
  2758. {
  2759. struct rtmsg *rtm;
  2760. int i, err;
  2761. rtm = nlmsg_payload(nlh, sizeof(*rtm));
  2762. if (!rtm) {
  2763. NL_SET_ERR_MSG(extack,
  2764. "ipv4: Invalid header for route get request");
  2765. return -EINVAL;
  2766. }
  2767. if (!netlink_strict_get_check(skb))
  2768. return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  2769. rtm_ipv4_policy, extack);
  2770. if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
  2771. (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
  2772. rtm->rtm_table || rtm->rtm_protocol ||
  2773. rtm->rtm_scope || rtm->rtm_type) {
  2774. NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request");
  2775. return -EINVAL;
  2776. }
  2777. if (rtm->rtm_flags & ~(RTM_F_NOTIFY |
  2778. RTM_F_LOOKUP_TABLE |
  2779. RTM_F_FIB_MATCH)) {
  2780. NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request");
  2781. return -EINVAL;
  2782. }
  2783. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  2784. rtm_ipv4_policy, extack);
  2785. if (err)
  2786. return err;
  2787. if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
  2788. (tb[RTA_DST] && !rtm->rtm_dst_len)) {
  2789. NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
  2790. return -EINVAL;
  2791. }
  2792. for (i = 0; i <= RTA_MAX; i++) {
  2793. if (!tb[i])
  2794. continue;
  2795. switch (i) {
  2796. case RTA_IIF:
  2797. case RTA_OIF:
  2798. case RTA_SRC:
  2799. case RTA_DST:
  2800. case RTA_IP_PROTO:
  2801. case RTA_SPORT:
  2802. case RTA_DPORT:
  2803. case RTA_MARK:
  2804. case RTA_UID:
  2805. break;
  2806. default:
  2807. NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request");
  2808. return -EINVAL;
  2809. }
  2810. }
  2811. return 0;
  2812. }
  2813. static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  2814. struct netlink_ext_ack *extack)
  2815. {
  2816. struct net *net = sock_net(in_skb->sk);
  2817. struct nlattr *tb[RTA_MAX+1];
  2818. u32 table_id = RT_TABLE_MAIN;
  2819. __be16 sport = 0, dport = 0;
  2820. struct fib_result res = {};
  2821. u8 ip_proto = IPPROTO_UDP;
  2822. struct rtable *rt = NULL;
  2823. struct sk_buff *skb;
  2824. struct rtmsg *rtm;
  2825. struct flowi4 fl4 = {};
  2826. __be32 dst = 0;
  2827. __be32 src = 0;
  2828. dscp_t dscp;
  2829. kuid_t uid;
  2830. u32 iif;
  2831. int err;
  2832. int mark;
  2833. err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
  2834. if (err < 0)
  2835. return err;
  2836. rtm = nlmsg_data(nlh);
  2837. src = nla_get_in_addr_default(tb[RTA_SRC], 0);
  2838. dst = nla_get_in_addr_default(tb[RTA_DST], 0);
  2839. iif = nla_get_u32_default(tb[RTA_IIF], 0);
  2840. mark = nla_get_u32_default(tb[RTA_MARK], 0);
  2841. dscp = inet_dsfield_to_dscp(rtm->rtm_tos);
  2842. if (tb[RTA_UID])
  2843. uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID]));
  2844. else
  2845. uid = (iif ? INVALID_UID : current_uid());
  2846. if (tb[RTA_IP_PROTO]) {
  2847. err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
  2848. &ip_proto, AF_INET, extack);
  2849. if (err)
  2850. return err;
  2851. }
  2852. if (tb[RTA_SPORT])
  2853. sport = nla_get_be16(tb[RTA_SPORT]);
  2854. if (tb[RTA_DPORT])
  2855. dport = nla_get_be16(tb[RTA_DPORT]);
  2856. skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport);
  2857. if (!skb)
  2858. return -ENOBUFS;
  2859. fl4.daddr = dst;
  2860. fl4.saddr = src;
  2861. fl4.flowi4_dscp = dscp;
  2862. fl4.flowi4_oif = nla_get_u32_default(tb[RTA_OIF], 0);
  2863. fl4.flowi4_mark = mark;
  2864. fl4.flowi4_uid = uid;
  2865. if (sport)
  2866. fl4.fl4_sport = sport;
  2867. if (dport)
  2868. fl4.fl4_dport = dport;
  2869. fl4.flowi4_proto = ip_proto;
  2870. rcu_read_lock();
  2871. if (iif) {
  2872. struct net_device *dev;
  2873. dev = dev_get_by_index_rcu(net, iif);
  2874. if (!dev) {
  2875. err = -ENODEV;
  2876. goto errout_rcu;
  2877. }
  2878. fl4.flowi4_iif = iif; /* for rt_fill_info */
  2879. skb->dev = dev;
  2880. skb->mark = mark;
  2881. err = ip_route_input_rcu(skb, dst, src, dscp, dev,
  2882. &res) ? -EINVAL : 0;
  2883. rt = skb_rtable(skb);
  2884. if (err == 0 && rt->dst.error)
  2885. err = -rt->dst.error;
  2886. } else {
  2887. fl4.flowi4_iif = LOOPBACK_IFINDEX;
  2888. skb->dev = net->loopback_dev;
  2889. rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb);
  2890. err = 0;
  2891. if (IS_ERR(rt))
  2892. err = PTR_ERR(rt);
  2893. else
  2894. skb_dst_set(skb, &rt->dst);
  2895. }
  2896. if (err)
  2897. goto errout_rcu;
  2898. if (rtm->rtm_flags & RTM_F_NOTIFY)
  2899. rt->rt_flags |= RTCF_NOTIFY;
  2900. if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE)
  2901. table_id = res.table ? res.table->tb_id : 0;
  2902. /* reset skb for netlink reply msg */
  2903. skb_trim(skb, 0);
  2904. skb_reset_network_header(skb);
  2905. skb_reset_transport_header(skb);
  2906. skb_reset_mac_header(skb);
  2907. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  2908. struct fib_rt_info fri;
  2909. if (!res.fi) {
  2910. err = fib_props[res.type].error;
  2911. if (!err)
  2912. err = -EHOSTUNREACH;
  2913. goto errout_rcu;
  2914. }
  2915. fri.fi = res.fi;
  2916. fri.tb_id = table_id;
  2917. fri.dst = res.prefix;
  2918. fri.dst_len = res.prefixlen;
  2919. fri.dscp = res.dscp;
  2920. fri.type = rt->rt_type;
  2921. fri.offload = 0;
  2922. fri.trap = 0;
  2923. fri.offload_failed = 0;
  2924. if (res.fa_head) {
  2925. struct fib_alias *fa;
  2926. hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) {
  2927. u8 slen = 32 - fri.dst_len;
  2928. if (fa->fa_slen == slen &&
  2929. fa->tb_id == fri.tb_id &&
  2930. fa->fa_dscp == fri.dscp &&
  2931. fa->fa_info == res.fi &&
  2932. fa->fa_type == fri.type) {
  2933. fri.offload = READ_ONCE(fa->offload);
  2934. fri.trap = READ_ONCE(fa->trap);
  2935. fri.offload_failed =
  2936. READ_ONCE(fa->offload_failed);
  2937. break;
  2938. }
  2939. }
  2940. }
  2941. err = fib_dump_info(skb, NETLINK_CB(in_skb).portid,
  2942. nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0);
  2943. } else {
  2944. err = rt_fill_info(net, dst, src, rt, table_id, res.dscp, &fl4,
  2945. skb, NETLINK_CB(in_skb).portid,
  2946. nlh->nlmsg_seq, 0);
  2947. }
  2948. if (err < 0)
  2949. goto errout_rcu;
  2950. rcu_read_unlock();
  2951. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  2952. errout_free:
  2953. return err;
  2954. errout_rcu:
  2955. rcu_read_unlock();
  2956. kfree_skb(skb);
  2957. goto errout_free;
  2958. }
  2959. void ip_rt_multicast_event(struct in_device *in_dev)
  2960. {
  2961. rt_cache_flush(dev_net(in_dev->dev));
  2962. }
  2963. #ifdef CONFIG_SYSCTL
  2964. static int ip_rt_gc_interval __read_mostly = 60 * HZ;
  2965. static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
  2966. static int ip_rt_gc_elasticity __read_mostly = 8;
  2967. static int ip_min_valid_pmtu __read_mostly = IPV4_MIN_MTU;
  2968. static int ipv4_sysctl_rtcache_flush(const struct ctl_table *__ctl, int write,
  2969. void *buffer, size_t *lenp, loff_t *ppos)
  2970. {
  2971. struct net *net = (struct net *)__ctl->extra1;
  2972. if (write) {
  2973. rt_cache_flush(net);
  2974. fnhe_genid_bump(net);
  2975. return 0;
  2976. }
  2977. return -EINVAL;
  2978. }
  2979. static struct ctl_table ipv4_route_table[] = {
  2980. {
  2981. .procname = "gc_thresh",
  2982. .data = &ipv4_dst_ops.gc_thresh,
  2983. .maxlen = sizeof(int),
  2984. .mode = 0644,
  2985. .proc_handler = proc_dointvec,
  2986. },
  2987. {
  2988. .procname = "max_size",
  2989. .data = &ip_rt_max_size,
  2990. .maxlen = sizeof(int),
  2991. .mode = 0644,
  2992. .proc_handler = proc_dointvec,
  2993. },
  2994. {
  2995. /* Deprecated. Use gc_min_interval_ms */
  2996. .procname = "gc_min_interval",
  2997. .data = &ip_rt_gc_min_interval,
  2998. .maxlen = sizeof(int),
  2999. .mode = 0644,
  3000. .proc_handler = proc_dointvec_jiffies,
  3001. },
  3002. {
  3003. .procname = "gc_min_interval_ms",
  3004. .data = &ip_rt_gc_min_interval,
  3005. .maxlen = sizeof(int),
  3006. .mode = 0644,
  3007. .proc_handler = proc_dointvec_ms_jiffies,
  3008. },
  3009. {
  3010. .procname = "gc_timeout",
  3011. .data = &ip_rt_gc_timeout,
  3012. .maxlen = sizeof(int),
  3013. .mode = 0644,
  3014. .proc_handler = proc_dointvec_jiffies,
  3015. },
  3016. {
  3017. .procname = "gc_interval",
  3018. .data = &ip_rt_gc_interval,
  3019. .maxlen = sizeof(int),
  3020. .mode = 0644,
  3021. .proc_handler = proc_dointvec_jiffies,
  3022. },
  3023. {
  3024. .procname = "redirect_load",
  3025. .data = &ip_rt_redirect_load,
  3026. .maxlen = sizeof(int),
  3027. .mode = 0644,
  3028. .proc_handler = proc_dointvec,
  3029. },
  3030. {
  3031. .procname = "redirect_number",
  3032. .data = &ip_rt_redirect_number,
  3033. .maxlen = sizeof(int),
  3034. .mode = 0644,
  3035. .proc_handler = proc_dointvec,
  3036. },
  3037. {
  3038. .procname = "redirect_silence",
  3039. .data = &ip_rt_redirect_silence,
  3040. .maxlen = sizeof(int),
  3041. .mode = 0644,
  3042. .proc_handler = proc_dointvec,
  3043. },
  3044. {
  3045. .procname = "error_cost",
  3046. .data = &ip_rt_error_cost,
  3047. .maxlen = sizeof(int),
  3048. .mode = 0644,
  3049. .proc_handler = proc_dointvec,
  3050. },
  3051. {
  3052. .procname = "error_burst",
  3053. .data = &ip_rt_error_burst,
  3054. .maxlen = sizeof(int),
  3055. .mode = 0644,
  3056. .proc_handler = proc_dointvec,
  3057. },
  3058. {
  3059. .procname = "gc_elasticity",
  3060. .data = &ip_rt_gc_elasticity,
  3061. .maxlen = sizeof(int),
  3062. .mode = 0644,
  3063. .proc_handler = proc_dointvec,
  3064. },
  3065. };
  3066. static const char ipv4_route_flush_procname[] = "flush";
  3067. static struct ctl_table ipv4_route_netns_table[] = {
  3068. {
  3069. .procname = ipv4_route_flush_procname,
  3070. .maxlen = sizeof(int),
  3071. .mode = 0200,
  3072. .proc_handler = ipv4_sysctl_rtcache_flush,
  3073. },
  3074. {
  3075. .procname = "min_pmtu",
  3076. .data = &init_net.ipv4.ip_rt_min_pmtu,
  3077. .maxlen = sizeof(int),
  3078. .mode = 0644,
  3079. .proc_handler = proc_dointvec_minmax,
  3080. .extra1 = &ip_min_valid_pmtu,
  3081. },
  3082. {
  3083. .procname = "mtu_expires",
  3084. .data = &init_net.ipv4.ip_rt_mtu_expires,
  3085. .maxlen = sizeof(int),
  3086. .mode = 0644,
  3087. .proc_handler = proc_dointvec_jiffies,
  3088. },
  3089. {
  3090. .procname = "min_adv_mss",
  3091. .data = &init_net.ipv4.ip_rt_min_advmss,
  3092. .maxlen = sizeof(int),
  3093. .mode = 0644,
  3094. .proc_handler = proc_dointvec,
  3095. },
  3096. };
  3097. static __net_init int sysctl_route_net_init(struct net *net)
  3098. {
  3099. struct ctl_table *tbl;
  3100. size_t table_size = ARRAY_SIZE(ipv4_route_netns_table);
  3101. tbl = ipv4_route_netns_table;
  3102. if (!net_eq(net, &init_net)) {
  3103. int i;
  3104. tbl = kmemdup(tbl, sizeof(ipv4_route_netns_table), GFP_KERNEL);
  3105. if (!tbl)
  3106. goto err_dup;
  3107. /* Don't export non-whitelisted sysctls to unprivileged users */
  3108. if (net->user_ns != &init_user_ns) {
  3109. if (tbl[0].procname != ipv4_route_flush_procname)
  3110. table_size = 0;
  3111. }
  3112. /* Update the variables to point into the current struct net
  3113. * except for the first element flush
  3114. */
  3115. for (i = 1; i < table_size; i++)
  3116. tbl[i].data += (void *)net - (void *)&init_net;
  3117. }
  3118. tbl[0].extra1 = net;
  3119. net->ipv4.route_hdr = register_net_sysctl_sz(net, "net/ipv4/route",
  3120. tbl, table_size);
  3121. if (!net->ipv4.route_hdr)
  3122. goto err_reg;
  3123. return 0;
  3124. err_reg:
  3125. if (tbl != ipv4_route_netns_table)
  3126. kfree(tbl);
  3127. err_dup:
  3128. return -ENOMEM;
  3129. }
  3130. static __net_exit void sysctl_route_net_exit(struct net *net)
  3131. {
  3132. const struct ctl_table *tbl;
  3133. tbl = net->ipv4.route_hdr->ctl_table_arg;
  3134. unregister_net_sysctl_table(net->ipv4.route_hdr);
  3135. BUG_ON(tbl == ipv4_route_netns_table);
  3136. kfree(tbl);
  3137. }
  3138. static __net_initdata struct pernet_operations sysctl_route_ops = {
  3139. .init = sysctl_route_net_init,
  3140. .exit = sysctl_route_net_exit,
  3141. };
  3142. #endif
  3143. static __net_init int netns_ip_rt_init(struct net *net)
  3144. {
  3145. /* Set default value for namespaceified sysctls */
  3146. net->ipv4.ip_rt_min_pmtu = DEFAULT_MIN_PMTU;
  3147. net->ipv4.ip_rt_mtu_expires = DEFAULT_MTU_EXPIRES;
  3148. net->ipv4.ip_rt_min_advmss = DEFAULT_MIN_ADVMSS;
  3149. return 0;
  3150. }
  3151. static struct pernet_operations __net_initdata ip_rt_ops = {
  3152. .init = netns_ip_rt_init,
  3153. };
  3154. static __net_init int rt_genid_init(struct net *net)
  3155. {
  3156. atomic_set(&net->ipv4.rt_genid, 0);
  3157. atomic_set(&net->fnhe_genid, 0);
  3158. atomic_set(&net->ipv4.dev_addr_genid, get_random_u32());
  3159. return 0;
  3160. }
  3161. static __net_initdata struct pernet_operations rt_genid_ops = {
  3162. .init = rt_genid_init,
  3163. };
  3164. static int __net_init ipv4_inetpeer_init(struct net *net)
  3165. {
  3166. struct inet_peer_base *bp = kmalloc_obj(*bp);
  3167. if (!bp)
  3168. return -ENOMEM;
  3169. inet_peer_base_init(bp);
  3170. net->ipv4.peers = bp;
  3171. return 0;
  3172. }
  3173. static void __net_exit ipv4_inetpeer_exit(struct net *net)
  3174. {
  3175. struct inet_peer_base *bp = net->ipv4.peers;
  3176. net->ipv4.peers = NULL;
  3177. inetpeer_invalidate_tree(bp);
  3178. kfree(bp);
  3179. }
  3180. static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
  3181. .init = ipv4_inetpeer_init,
  3182. .exit = ipv4_inetpeer_exit,
  3183. };
  3184. #ifdef CONFIG_IP_ROUTE_CLASSID
  3185. struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
  3186. #endif /* CONFIG_IP_ROUTE_CLASSID */
  3187. static const struct rtnl_msg_handler ip_rt_rtnl_msg_handlers[] __initconst = {
  3188. {.protocol = PF_INET, .msgtype = RTM_GETROUTE,
  3189. .doit = inet_rtm_getroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
  3190. };
  3191. int __init ip_rt_init(void)
  3192. {
  3193. void *idents_hash;
  3194. int cpu;
  3195. /* For modern hosts, this will use 2 MB of memory */
  3196. idents_hash = alloc_large_system_hash("IP idents",
  3197. sizeof(*ip_idents) + sizeof(*ip_tstamps),
  3198. 0,
  3199. 16, /* one bucket per 64 KB */
  3200. HASH_ZERO,
  3201. NULL,
  3202. &ip_idents_mask,
  3203. 2048,
  3204. 256*1024);
  3205. ip_idents = idents_hash;
  3206. get_random_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
  3207. ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
  3208. for_each_possible_cpu(cpu) {
  3209. struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
  3210. INIT_LIST_HEAD(&ul->head);
  3211. spin_lock_init(&ul->lock);
  3212. }
  3213. #ifdef CONFIG_IP_ROUTE_CLASSID
  3214. ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
  3215. if (!ip_rt_acct)
  3216. panic("IP: failed to allocate ip_rt_acct\n");
  3217. #endif
  3218. ipv4_dst_ops.kmem_cachep = KMEM_CACHE(rtable,
  3219. SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  3220. ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
  3221. if (dst_entries_init(&ipv4_dst_ops) < 0)
  3222. panic("IP: failed to allocate ipv4_dst_ops counter\n");
  3223. if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
  3224. panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
  3225. ipv4_dst_ops.gc_thresh = ~0;
  3226. ip_rt_max_size = INT_MAX;
  3227. devinet_init();
  3228. ip_fib_init();
  3229. if (ip_rt_proc_init())
  3230. pr_err("Unable to create route proc files\n");
  3231. #ifdef CONFIG_XFRM
  3232. xfrm_init();
  3233. xfrm4_init();
  3234. #endif
  3235. rtnl_register_many(ip_rt_rtnl_msg_handlers);
  3236. #ifdef CONFIG_SYSCTL
  3237. register_pernet_subsys(&sysctl_route_ops);
  3238. #endif
  3239. register_pernet_subsys(&ip_rt_ops);
  3240. register_pernet_subsys(&rt_genid_ops);
  3241. register_pernet_subsys(&ipv4_inetpeer_ops);
  3242. return 0;
  3243. }
  3244. #ifdef CONFIG_SYSCTL
  3245. /*
  3246. * We really need to sanitize the damn ipv4 init order, then all
  3247. * this nonsense will go away.
  3248. */
  3249. void __init ip_static_sysctl_init(void)
  3250. {
  3251. register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
  3252. }
  3253. #endif