connect.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2011
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/list.h>
  26. #include <linux/wait.h>
  27. #include <linux/slab.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/ctype.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mempool.h>
  32. #include <linux/delay.h>
  33. #include <linux/completion.h>
  34. #include <linux/kthread.h>
  35. #include <linux/pagevec.h>
  36. #include <linux/freezer.h>
  37. #include <linux/namei.h>
  38. #include <linux/uuid.h>
  39. #include <linux/uaccess.h>
  40. #include <asm/processor.h>
  41. #include <linux/inet.h>
  42. #include <linux/module.h>
  43. #include <keys/user-type.h>
  44. #include <net/ipv6.h>
  45. #include <linux/parser.h>
  46. #include <linux/bvec.h>
  47. #include "cifspdu.h"
  48. #include "cifsglob.h"
  49. #include "cifsproto.h"
  50. #include "cifs_unicode.h"
  51. #include "cifs_debug.h"
  52. #include "cifs_fs_sb.h"
  53. #include "ntlmssp.h"
  54. #include "nterr.h"
  55. #include "rfc1002pdu.h"
  56. #include "fscache.h"
  57. #include "smb2proto.h"
  58. #include "smbdirect.h"
  59. extern mempool_t *cifs_req_poolp;
  60. extern bool disable_legacy_dialects;
  61. /* FIXME: should these be tunable? */
  62. #define TLINK_ERROR_EXPIRE (1 * HZ)
  63. #define TLINK_IDLE_EXPIRE (600 * HZ)
  64. enum {
  65. /* Mount options that take no arguments */
  66. Opt_user_xattr, Opt_nouser_xattr,
  67. Opt_forceuid, Opt_noforceuid,
  68. Opt_forcegid, Opt_noforcegid,
  69. Opt_noblocksend, Opt_noautotune,
  70. Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
  71. Opt_mapposix, Opt_nomapposix,
  72. Opt_mapchars, Opt_nomapchars, Opt_sfu,
  73. Opt_nosfu, Opt_nodfs, Opt_posixpaths,
  74. Opt_noposixpaths, Opt_nounix, Opt_unix,
  75. Opt_nocase,
  76. Opt_brl, Opt_nobrl,
  77. Opt_handlecache, Opt_nohandlecache,
  78. Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
  79. Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
  80. Opt_nohard, Opt_nosoft,
  81. Opt_nointr, Opt_intr,
  82. Opt_nostrictsync, Opt_strictsync,
  83. Opt_serverino, Opt_noserverino,
  84. Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
  85. Opt_acl, Opt_noacl, Opt_locallease,
  86. Opt_sign, Opt_seal, Opt_noac,
  87. Opt_fsc, Opt_mfsymlinks,
  88. Opt_multiuser, Opt_sloppy, Opt_nosharesock,
  89. Opt_persistent, Opt_nopersistent,
  90. Opt_resilient, Opt_noresilient,
  91. Opt_domainauto, Opt_rdma,
  92. /* Mount options which take numeric value */
  93. Opt_backupuid, Opt_backupgid, Opt_uid,
  94. Opt_cruid, Opt_gid, Opt_file_mode,
  95. Opt_dirmode, Opt_port,
  96. Opt_rsize, Opt_wsize, Opt_actimeo,
  97. Opt_echo_interval, Opt_max_credits,
  98. Opt_snapshot,
  99. /* Mount options which take string value */
  100. Opt_user, Opt_pass, Opt_ip,
  101. Opt_domain, Opt_srcaddr, Opt_iocharset,
  102. Opt_netbiosname, Opt_servern,
  103. Opt_ver, Opt_vers, Opt_sec, Opt_cache,
  104. /* Mount options to be ignored */
  105. Opt_ignore,
  106. /* Options which could be blank */
  107. Opt_blank_pass,
  108. Opt_blank_user,
  109. Opt_blank_ip,
  110. Opt_err
  111. };
  112. static const match_table_t cifs_mount_option_tokens = {
  113. { Opt_user_xattr, "user_xattr" },
  114. { Opt_nouser_xattr, "nouser_xattr" },
  115. { Opt_forceuid, "forceuid" },
  116. { Opt_noforceuid, "noforceuid" },
  117. { Opt_forcegid, "forcegid" },
  118. { Opt_noforcegid, "noforcegid" },
  119. { Opt_noblocksend, "noblocksend" },
  120. { Opt_noautotune, "noautotune" },
  121. { Opt_hard, "hard" },
  122. { Opt_soft, "soft" },
  123. { Opt_perm, "perm" },
  124. { Opt_noperm, "noperm" },
  125. { Opt_mapchars, "mapchars" }, /* SFU style */
  126. { Opt_nomapchars, "nomapchars" },
  127. { Opt_mapposix, "mapposix" }, /* SFM style */
  128. { Opt_nomapposix, "nomapposix" },
  129. { Opt_sfu, "sfu" },
  130. { Opt_nosfu, "nosfu" },
  131. { Opt_nodfs, "nodfs" },
  132. { Opt_posixpaths, "posixpaths" },
  133. { Opt_noposixpaths, "noposixpaths" },
  134. { Opt_nounix, "nounix" },
  135. { Opt_nounix, "nolinux" },
  136. { Opt_nounix, "noposix" },
  137. { Opt_unix, "unix" },
  138. { Opt_unix, "linux" },
  139. { Opt_unix, "posix" },
  140. { Opt_nocase, "nocase" },
  141. { Opt_nocase, "ignorecase" },
  142. { Opt_brl, "brl" },
  143. { Opt_nobrl, "nobrl" },
  144. { Opt_handlecache, "handlecache" },
  145. { Opt_nohandlecache, "nohandlecache" },
  146. { Opt_nobrl, "nolock" },
  147. { Opt_forcemandatorylock, "forcemandatorylock" },
  148. { Opt_forcemandatorylock, "forcemand" },
  149. { Opt_setuids, "setuids" },
  150. { Opt_nosetuids, "nosetuids" },
  151. { Opt_setuidfromacl, "idsfromsid" },
  152. { Opt_dynperm, "dynperm" },
  153. { Opt_nodynperm, "nodynperm" },
  154. { Opt_nohard, "nohard" },
  155. { Opt_nosoft, "nosoft" },
  156. { Opt_nointr, "nointr" },
  157. { Opt_intr, "intr" },
  158. { Opt_nostrictsync, "nostrictsync" },
  159. { Opt_strictsync, "strictsync" },
  160. { Opt_serverino, "serverino" },
  161. { Opt_noserverino, "noserverino" },
  162. { Opt_rwpidforward, "rwpidforward" },
  163. { Opt_cifsacl, "cifsacl" },
  164. { Opt_nocifsacl, "nocifsacl" },
  165. { Opt_acl, "acl" },
  166. { Opt_noacl, "noacl" },
  167. { Opt_locallease, "locallease" },
  168. { Opt_sign, "sign" },
  169. { Opt_seal, "seal" },
  170. { Opt_noac, "noac" },
  171. { Opt_fsc, "fsc" },
  172. { Opt_mfsymlinks, "mfsymlinks" },
  173. { Opt_multiuser, "multiuser" },
  174. { Opt_sloppy, "sloppy" },
  175. { Opt_nosharesock, "nosharesock" },
  176. { Opt_persistent, "persistenthandles"},
  177. { Opt_nopersistent, "nopersistenthandles"},
  178. { Opt_resilient, "resilienthandles"},
  179. { Opt_noresilient, "noresilienthandles"},
  180. { Opt_domainauto, "domainauto"},
  181. { Opt_rdma, "rdma"},
  182. { Opt_backupuid, "backupuid=%s" },
  183. { Opt_backupgid, "backupgid=%s" },
  184. { Opt_uid, "uid=%s" },
  185. { Opt_cruid, "cruid=%s" },
  186. { Opt_gid, "gid=%s" },
  187. { Opt_file_mode, "file_mode=%s" },
  188. { Opt_dirmode, "dirmode=%s" },
  189. { Opt_dirmode, "dir_mode=%s" },
  190. { Opt_port, "port=%s" },
  191. { Opt_rsize, "rsize=%s" },
  192. { Opt_wsize, "wsize=%s" },
  193. { Opt_actimeo, "actimeo=%s" },
  194. { Opt_echo_interval, "echo_interval=%s" },
  195. { Opt_max_credits, "max_credits=%s" },
  196. { Opt_snapshot, "snapshot=%s" },
  197. { Opt_blank_user, "user=" },
  198. { Opt_blank_user, "username=" },
  199. { Opt_user, "user=%s" },
  200. { Opt_user, "username=%s" },
  201. { Opt_blank_pass, "pass=" },
  202. { Opt_blank_pass, "password=" },
  203. { Opt_pass, "pass=%s" },
  204. { Opt_pass, "password=%s" },
  205. { Opt_blank_ip, "ip=" },
  206. { Opt_blank_ip, "addr=" },
  207. { Opt_ip, "ip=%s" },
  208. { Opt_ip, "addr=%s" },
  209. { Opt_ignore, "unc=%s" },
  210. { Opt_ignore, "target=%s" },
  211. { Opt_ignore, "path=%s" },
  212. { Opt_domain, "dom=%s" },
  213. { Opt_domain, "domain=%s" },
  214. { Opt_domain, "workgroup=%s" },
  215. { Opt_srcaddr, "srcaddr=%s" },
  216. { Opt_ignore, "prefixpath=%s" },
  217. { Opt_iocharset, "iocharset=%s" },
  218. { Opt_netbiosname, "netbiosname=%s" },
  219. { Opt_servern, "servern=%s" },
  220. { Opt_ver, "ver=%s" },
  221. { Opt_vers, "vers=%s" },
  222. { Opt_sec, "sec=%s" },
  223. { Opt_cache, "cache=%s" },
  224. { Opt_ignore, "cred" },
  225. { Opt_ignore, "credentials" },
  226. { Opt_ignore, "cred=%s" },
  227. { Opt_ignore, "credentials=%s" },
  228. { Opt_ignore, "guest" },
  229. { Opt_ignore, "rw" },
  230. { Opt_ignore, "ro" },
  231. { Opt_ignore, "suid" },
  232. { Opt_ignore, "nosuid" },
  233. { Opt_ignore, "exec" },
  234. { Opt_ignore, "noexec" },
  235. { Opt_ignore, "nodev" },
  236. { Opt_ignore, "noauto" },
  237. { Opt_ignore, "dev" },
  238. { Opt_ignore, "mand" },
  239. { Opt_ignore, "nomand" },
  240. { Opt_ignore, "_netdev" },
  241. { Opt_err, NULL }
  242. };
  243. enum {
  244. Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
  245. Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
  246. Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
  247. Opt_sec_ntlmv2i, Opt_sec_lanman,
  248. Opt_sec_none,
  249. Opt_sec_err
  250. };
  251. static const match_table_t cifs_secflavor_tokens = {
  252. { Opt_sec_krb5, "krb5" },
  253. { Opt_sec_krb5i, "krb5i" },
  254. { Opt_sec_krb5p, "krb5p" },
  255. { Opt_sec_ntlmsspi, "ntlmsspi" },
  256. { Opt_sec_ntlmssp, "ntlmssp" },
  257. { Opt_ntlm, "ntlm" },
  258. { Opt_sec_ntlmi, "ntlmi" },
  259. { Opt_sec_ntlmv2, "nontlm" },
  260. { Opt_sec_ntlmv2, "ntlmv2" },
  261. { Opt_sec_ntlmv2i, "ntlmv2i" },
  262. { Opt_sec_lanman, "lanman" },
  263. { Opt_sec_none, "none" },
  264. { Opt_sec_err, NULL }
  265. };
  266. /* cache flavors */
  267. enum {
  268. Opt_cache_loose,
  269. Opt_cache_strict,
  270. Opt_cache_none,
  271. Opt_cache_err
  272. };
  273. static const match_table_t cifs_cacheflavor_tokens = {
  274. { Opt_cache_loose, "loose" },
  275. { Opt_cache_strict, "strict" },
  276. { Opt_cache_none, "none" },
  277. { Opt_cache_err, NULL }
  278. };
  279. static const match_table_t cifs_smb_version_tokens = {
  280. { Smb_1, SMB1_VERSION_STRING },
  281. { Smb_20, SMB20_VERSION_STRING},
  282. { Smb_21, SMB21_VERSION_STRING },
  283. { Smb_30, SMB30_VERSION_STRING },
  284. { Smb_302, SMB302_VERSION_STRING },
  285. { Smb_311, SMB311_VERSION_STRING },
  286. { Smb_311, ALT_SMB311_VERSION_STRING },
  287. { Smb_3any, SMB3ANY_VERSION_STRING },
  288. { Smb_default, SMBDEFAULT_VERSION_STRING },
  289. { Smb_version_err, NULL }
  290. };
  291. static int ip_connect(struct TCP_Server_Info *server);
  292. static int generic_ip_connect(struct TCP_Server_Info *server);
  293. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  294. static void cifs_prune_tlinks(struct work_struct *work);
  295. static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  296. const char *devname, bool is_smb3);
  297. /*
  298. * cifs tcp session reconnection
  299. *
  300. * mark tcp session as reconnecting so temporarily locked
  301. * mark all smb sessions as reconnecting for tcp session
  302. * reconnect tcp session
  303. * wake up waiters on reconnection? - (not needed currently)
  304. */
  305. int
  306. cifs_reconnect(struct TCP_Server_Info *server)
  307. {
  308. int rc = 0;
  309. struct list_head *tmp, *tmp2;
  310. struct cifs_ses *ses;
  311. struct cifs_tcon *tcon;
  312. struct mid_q_entry *mid_entry;
  313. struct list_head retry_list;
  314. spin_lock(&GlobalMid_Lock);
  315. if (server->tcpStatus == CifsExiting) {
  316. /* the demux thread will exit normally
  317. next time through the loop */
  318. spin_unlock(&GlobalMid_Lock);
  319. return rc;
  320. } else
  321. server->tcpStatus = CifsNeedReconnect;
  322. spin_unlock(&GlobalMid_Lock);
  323. server->maxBuf = 0;
  324. server->max_read = 0;
  325. cifs_dbg(FYI, "Reconnecting tcp session\n");
  326. trace_smb3_reconnect(server->CurrentMid, server->hostname);
  327. /* before reconnecting the tcp session, mark the smb session (uid)
  328. and the tid bad so they are not used until reconnected */
  329. cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
  330. __func__);
  331. spin_lock(&cifs_tcp_ses_lock);
  332. list_for_each(tmp, &server->smb_ses_list) {
  333. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  334. ses->need_reconnect = true;
  335. list_for_each(tmp2, &ses->tcon_list) {
  336. tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
  337. tcon->need_reconnect = true;
  338. }
  339. if (ses->tcon_ipc)
  340. ses->tcon_ipc->need_reconnect = true;
  341. }
  342. spin_unlock(&cifs_tcp_ses_lock);
  343. /* do not want to be sending data on a socket we are freeing */
  344. cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
  345. mutex_lock(&server->srv_mutex);
  346. if (server->ssocket) {
  347. cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
  348. server->ssocket->state, server->ssocket->flags);
  349. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  350. cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
  351. server->ssocket->state, server->ssocket->flags);
  352. sock_release(server->ssocket);
  353. server->ssocket = NULL;
  354. }
  355. server->sequence_number = 0;
  356. server->session_estab = false;
  357. kfree(server->session_key.response);
  358. server->session_key.response = NULL;
  359. server->session_key.len = 0;
  360. server->lstrp = jiffies;
  361. /* mark submitted MIDs for retry and issue callback */
  362. INIT_LIST_HEAD(&retry_list);
  363. cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
  364. spin_lock(&GlobalMid_Lock);
  365. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  366. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  367. if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
  368. mid_entry->mid_state = MID_RETRY_NEEDED;
  369. list_move(&mid_entry->qhead, &retry_list);
  370. }
  371. spin_unlock(&GlobalMid_Lock);
  372. mutex_unlock(&server->srv_mutex);
  373. cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
  374. list_for_each_safe(tmp, tmp2, &retry_list) {
  375. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  376. list_del_init(&mid_entry->qhead);
  377. mid_entry->callback(mid_entry);
  378. }
  379. do {
  380. try_to_freeze();
  381. /* we should try only the port we connected to before */
  382. mutex_lock(&server->srv_mutex);
  383. if (cifs_rdma_enabled(server))
  384. rc = smbd_reconnect(server);
  385. else
  386. rc = generic_ip_connect(server);
  387. if (rc) {
  388. cifs_dbg(FYI, "reconnect error %d\n", rc);
  389. mutex_unlock(&server->srv_mutex);
  390. msleep(3000);
  391. } else {
  392. atomic_inc(&tcpSesReconnectCount);
  393. spin_lock(&GlobalMid_Lock);
  394. if (server->tcpStatus != CifsExiting)
  395. server->tcpStatus = CifsNeedNegotiate;
  396. spin_unlock(&GlobalMid_Lock);
  397. mutex_unlock(&server->srv_mutex);
  398. }
  399. } while (server->tcpStatus == CifsNeedReconnect);
  400. if (server->tcpStatus == CifsNeedNegotiate)
  401. mod_delayed_work(cifsiod_wq, &server->echo, 0);
  402. return rc;
  403. }
  404. static void
  405. cifs_echo_request(struct work_struct *work)
  406. {
  407. int rc;
  408. struct TCP_Server_Info *server = container_of(work,
  409. struct TCP_Server_Info, echo.work);
  410. unsigned long echo_interval;
  411. /*
  412. * If we need to renegotiate, set echo interval to zero to
  413. * immediately call echo service where we can renegotiate.
  414. */
  415. if (server->tcpStatus == CifsNeedNegotiate)
  416. echo_interval = 0;
  417. else
  418. echo_interval = server->echo_interval;
  419. /*
  420. * We cannot send an echo if it is disabled.
  421. * Also, no need to ping if we got a response recently.
  422. */
  423. if (server->tcpStatus == CifsNeedReconnect ||
  424. server->tcpStatus == CifsExiting ||
  425. server->tcpStatus == CifsNew ||
  426. (server->ops->can_echo && !server->ops->can_echo(server)) ||
  427. time_before(jiffies, server->lstrp + echo_interval - HZ))
  428. goto requeue_echo;
  429. rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
  430. if (rc)
  431. cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
  432. server->hostname);
  433. requeue_echo:
  434. queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
  435. }
  436. static bool
  437. allocate_buffers(struct TCP_Server_Info *server)
  438. {
  439. if (!server->bigbuf) {
  440. server->bigbuf = (char *)cifs_buf_get();
  441. if (!server->bigbuf) {
  442. cifs_dbg(VFS, "No memory for large SMB response\n");
  443. msleep(3000);
  444. /* retry will check if exiting */
  445. return false;
  446. }
  447. } else if (server->large_buf) {
  448. /* we are reusing a dirty large buf, clear its start */
  449. memset(server->bigbuf, 0, HEADER_SIZE(server));
  450. }
  451. if (!server->smallbuf) {
  452. server->smallbuf = (char *)cifs_small_buf_get();
  453. if (!server->smallbuf) {
  454. cifs_dbg(VFS, "No memory for SMB response\n");
  455. msleep(1000);
  456. /* retry will check if exiting */
  457. return false;
  458. }
  459. /* beginning of smb buffer is cleared in our buf_get */
  460. } else {
  461. /* if existing small buf clear beginning */
  462. memset(server->smallbuf, 0, HEADER_SIZE(server));
  463. }
  464. return true;
  465. }
  466. static bool
  467. server_unresponsive(struct TCP_Server_Info *server)
  468. {
  469. /*
  470. * We need to wait 2 echo intervals to make sure we handle such
  471. * situations right:
  472. * 1s client sends a normal SMB request
  473. * 2s client gets a response
  474. * 30s echo workqueue job pops, and decides we got a response recently
  475. * and don't need to send another
  476. * ...
  477. * 65s kernel_recvmsg times out, and we see that we haven't gotten
  478. * a response in >60s.
  479. */
  480. if ((server->tcpStatus == CifsGood ||
  481. server->tcpStatus == CifsNeedNegotiate) &&
  482. time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
  483. cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
  484. server->hostname, (2 * server->echo_interval) / HZ);
  485. cifs_reconnect(server);
  486. wake_up(&server->response_q);
  487. return true;
  488. }
  489. return false;
  490. }
  491. static int
  492. cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
  493. {
  494. int length = 0;
  495. int total_read;
  496. smb_msg->msg_control = NULL;
  497. smb_msg->msg_controllen = 0;
  498. for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
  499. try_to_freeze();
  500. if (server_unresponsive(server))
  501. return -ECONNABORTED;
  502. if (cifs_rdma_enabled(server) && server->smbd_conn)
  503. length = smbd_recv(server->smbd_conn, smb_msg);
  504. else
  505. length = sock_recvmsg(server->ssocket, smb_msg, 0);
  506. if (server->tcpStatus == CifsExiting)
  507. return -ESHUTDOWN;
  508. if (server->tcpStatus == CifsNeedReconnect) {
  509. cifs_reconnect(server);
  510. return -ECONNABORTED;
  511. }
  512. if (length == -ERESTARTSYS ||
  513. length == -EAGAIN ||
  514. length == -EINTR) {
  515. /*
  516. * Minimum sleep to prevent looping, allowing socket
  517. * to clear and app threads to set tcpStatus
  518. * CifsNeedReconnect if server hung.
  519. */
  520. usleep_range(1000, 2000);
  521. length = 0;
  522. continue;
  523. }
  524. if (length <= 0) {
  525. cifs_dbg(FYI, "Received no data or error: %d\n", length);
  526. cifs_reconnect(server);
  527. return -ECONNABORTED;
  528. }
  529. }
  530. return total_read;
  531. }
  532. int
  533. cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
  534. unsigned int to_read)
  535. {
  536. struct msghdr smb_msg;
  537. struct kvec iov = {.iov_base = buf, .iov_len = to_read};
  538. iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
  539. return cifs_readv_from_socket(server, &smb_msg);
  540. }
  541. int
  542. cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
  543. unsigned int page_offset, unsigned int to_read)
  544. {
  545. struct msghdr smb_msg;
  546. struct bio_vec bv = {
  547. .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
  548. iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
  549. return cifs_readv_from_socket(server, &smb_msg);
  550. }
  551. static bool
  552. is_smb_response(struct TCP_Server_Info *server, unsigned char type)
  553. {
  554. /*
  555. * The first byte big endian of the length field,
  556. * is actually not part of the length but the type
  557. * with the most common, zero, as regular data.
  558. */
  559. switch (type) {
  560. case RFC1002_SESSION_MESSAGE:
  561. /* Regular SMB response */
  562. return true;
  563. case RFC1002_SESSION_KEEP_ALIVE:
  564. cifs_dbg(FYI, "RFC 1002 session keep alive\n");
  565. break;
  566. case RFC1002_POSITIVE_SESSION_RESPONSE:
  567. cifs_dbg(FYI, "RFC 1002 positive session response\n");
  568. break;
  569. case RFC1002_NEGATIVE_SESSION_RESPONSE:
  570. /*
  571. * We get this from Windows 98 instead of an error on
  572. * SMB negprot response.
  573. */
  574. cifs_dbg(FYI, "RFC 1002 negative session response\n");
  575. /* give server a second to clean up */
  576. msleep(1000);
  577. /*
  578. * Always try 445 first on reconnect since we get NACK
  579. * on some if we ever connected to port 139 (the NACK
  580. * is since we do not begin with RFC1001 session
  581. * initialize frame).
  582. */
  583. cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
  584. cifs_reconnect(server);
  585. wake_up(&server->response_q);
  586. break;
  587. default:
  588. cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
  589. cifs_reconnect(server);
  590. }
  591. return false;
  592. }
  593. void
  594. dequeue_mid(struct mid_q_entry *mid, bool malformed)
  595. {
  596. #ifdef CONFIG_CIFS_STATS2
  597. mid->when_received = jiffies;
  598. #endif
  599. spin_lock(&GlobalMid_Lock);
  600. if (!malformed)
  601. mid->mid_state = MID_RESPONSE_RECEIVED;
  602. else
  603. mid->mid_state = MID_RESPONSE_MALFORMED;
  604. /*
  605. * Trying to handle/dequeue a mid after the send_recv()
  606. * function has finished processing it is a bug.
  607. */
  608. if (mid->mid_flags & MID_DELETED)
  609. printk_once(KERN_WARNING
  610. "trying to dequeue a deleted mid\n");
  611. else
  612. list_del_init(&mid->qhead);
  613. spin_unlock(&GlobalMid_Lock);
  614. }
  615. static void
  616. handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  617. char *buf, int malformed)
  618. {
  619. if (server->ops->check_trans2 &&
  620. server->ops->check_trans2(mid, server, buf, malformed))
  621. return;
  622. mid->resp_buf = buf;
  623. mid->large_buf = server->large_buf;
  624. /* Was previous buf put in mpx struct for multi-rsp? */
  625. if (!mid->multiRsp) {
  626. /* smb buffer will be freed by user thread */
  627. if (server->large_buf)
  628. server->bigbuf = NULL;
  629. else
  630. server->smallbuf = NULL;
  631. }
  632. dequeue_mid(mid, malformed);
  633. }
  634. static void clean_demultiplex_info(struct TCP_Server_Info *server)
  635. {
  636. int length;
  637. /* take it off the list, if it's not already */
  638. spin_lock(&cifs_tcp_ses_lock);
  639. list_del_init(&server->tcp_ses_list);
  640. spin_unlock(&cifs_tcp_ses_lock);
  641. spin_lock(&GlobalMid_Lock);
  642. server->tcpStatus = CifsExiting;
  643. spin_unlock(&GlobalMid_Lock);
  644. wake_up_all(&server->response_q);
  645. /* check if we have blocked requests that need to free */
  646. spin_lock(&server->req_lock);
  647. if (server->credits <= 0)
  648. server->credits = 1;
  649. spin_unlock(&server->req_lock);
  650. /*
  651. * Although there should not be any requests blocked on this queue it
  652. * can not hurt to be paranoid and try to wake up requests that may
  653. * haven been blocked when more than 50 at time were on the wire to the
  654. * same server - they now will see the session is in exit state and get
  655. * out of SendReceive.
  656. */
  657. wake_up_all(&server->request_q);
  658. /* give those requests time to exit */
  659. msleep(125);
  660. if (cifs_rdma_enabled(server) && server->smbd_conn) {
  661. smbd_destroy(server->smbd_conn);
  662. server->smbd_conn = NULL;
  663. }
  664. if (server->ssocket) {
  665. sock_release(server->ssocket);
  666. server->ssocket = NULL;
  667. }
  668. if (!list_empty(&server->pending_mid_q)) {
  669. struct list_head dispose_list;
  670. struct mid_q_entry *mid_entry;
  671. struct list_head *tmp, *tmp2;
  672. INIT_LIST_HEAD(&dispose_list);
  673. spin_lock(&GlobalMid_Lock);
  674. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  675. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  676. cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
  677. mid_entry->mid_state = MID_SHUTDOWN;
  678. list_move(&mid_entry->qhead, &dispose_list);
  679. }
  680. spin_unlock(&GlobalMid_Lock);
  681. /* now walk dispose list and issue callbacks */
  682. list_for_each_safe(tmp, tmp2, &dispose_list) {
  683. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  684. cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
  685. list_del_init(&mid_entry->qhead);
  686. mid_entry->callback(mid_entry);
  687. }
  688. /* 1/8th of sec is more than enough time for them to exit */
  689. msleep(125);
  690. }
  691. if (!list_empty(&server->pending_mid_q)) {
  692. /*
  693. * mpx threads have not exited yet give them at least the smb
  694. * send timeout time for long ops.
  695. *
  696. * Due to delays on oplock break requests, we need to wait at
  697. * least 45 seconds before giving up on a request getting a
  698. * response and going ahead and killing cifsd.
  699. */
  700. cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
  701. msleep(46000);
  702. /*
  703. * If threads still have not exited they are probably never
  704. * coming home not much else we can do but free the memory.
  705. */
  706. }
  707. kfree(server->hostname);
  708. kfree(server);
  709. length = atomic_dec_return(&tcpSesAllocCount);
  710. if (length > 0)
  711. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  712. }
  713. static int
  714. standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  715. {
  716. int length;
  717. char *buf = server->smallbuf;
  718. unsigned int pdu_length = server->pdu_size;
  719. /* make sure this will fit in a large buffer */
  720. if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
  721. server->vals->header_preamble_size) {
  722. cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
  723. cifs_reconnect(server);
  724. wake_up(&server->response_q);
  725. return -ECONNABORTED;
  726. }
  727. /* switch to large buffer if too big for a small one */
  728. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  729. server->large_buf = true;
  730. memcpy(server->bigbuf, buf, server->total_read);
  731. buf = server->bigbuf;
  732. }
  733. /* now read the rest */
  734. length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
  735. pdu_length - HEADER_SIZE(server) + 1
  736. + server->vals->header_preamble_size);
  737. if (length < 0)
  738. return length;
  739. server->total_read += length;
  740. dump_smb(buf, server->total_read);
  741. return cifs_handle_standard(server, mid);
  742. }
  743. int
  744. cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  745. {
  746. char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
  747. int length;
  748. /*
  749. * We know that we received enough to get to the MID as we
  750. * checked the pdu_length earlier. Now check to see
  751. * if the rest of the header is OK. We borrow the length
  752. * var for the rest of the loop to avoid a new stack var.
  753. *
  754. * 48 bytes is enough to display the header and a little bit
  755. * into the payload for debugging purposes.
  756. */
  757. length = server->ops->check_message(buf, server->total_read, server);
  758. if (length != 0)
  759. cifs_dump_mem("Bad SMB: ", buf,
  760. min_t(unsigned int, server->total_read, 48));
  761. if (server->ops->is_session_expired &&
  762. server->ops->is_session_expired(buf)) {
  763. cifs_reconnect(server);
  764. wake_up(&server->response_q);
  765. return -1;
  766. }
  767. if (server->ops->is_status_pending &&
  768. server->ops->is_status_pending(buf, server, length))
  769. return -1;
  770. if (!mid)
  771. return length;
  772. handle_mid(mid, server, buf, length);
  773. return 0;
  774. }
  775. static int
  776. cifs_demultiplex_thread(void *p)
  777. {
  778. int i, num_mids, length;
  779. struct TCP_Server_Info *server = p;
  780. unsigned int pdu_length;
  781. unsigned int next_offset;
  782. char *buf = NULL;
  783. struct task_struct *task_to_wake = NULL;
  784. struct mid_q_entry *mids[MAX_COMPOUND];
  785. char *bufs[MAX_COMPOUND];
  786. current->flags |= PF_MEMALLOC;
  787. cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
  788. length = atomic_inc_return(&tcpSesAllocCount);
  789. if (length > 1)
  790. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  791. set_freezable();
  792. while (server->tcpStatus != CifsExiting) {
  793. if (try_to_freeze())
  794. continue;
  795. if (!allocate_buffers(server))
  796. continue;
  797. server->large_buf = false;
  798. buf = server->smallbuf;
  799. pdu_length = 4; /* enough to get RFC1001 header */
  800. length = cifs_read_from_socket(server, buf, pdu_length);
  801. if (length < 0)
  802. continue;
  803. if (server->vals->header_preamble_size == 0)
  804. server->total_read = 0;
  805. else
  806. server->total_read = length;
  807. /*
  808. * The right amount was read from socket - 4 bytes,
  809. * so we can now interpret the length field.
  810. */
  811. pdu_length = get_rfc1002_length(buf);
  812. cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
  813. if (!is_smb_response(server, buf[0]))
  814. continue;
  815. next_pdu:
  816. server->pdu_size = pdu_length;
  817. /* make sure we have enough to get to the MID */
  818. if (server->pdu_size < HEADER_SIZE(server) - 1 -
  819. server->vals->header_preamble_size) {
  820. cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
  821. server->pdu_size);
  822. cifs_reconnect(server);
  823. wake_up(&server->response_q);
  824. continue;
  825. }
  826. /* read down to the MID */
  827. length = cifs_read_from_socket(server,
  828. buf + server->vals->header_preamble_size,
  829. HEADER_SIZE(server) - 1
  830. - server->vals->header_preamble_size);
  831. if (length < 0)
  832. continue;
  833. server->total_read += length;
  834. if (server->ops->next_header) {
  835. next_offset = server->ops->next_header(buf);
  836. if (next_offset)
  837. server->pdu_size = next_offset;
  838. }
  839. memset(mids, 0, sizeof(mids));
  840. memset(bufs, 0, sizeof(bufs));
  841. num_mids = 0;
  842. if (server->ops->is_transform_hdr &&
  843. server->ops->receive_transform &&
  844. server->ops->is_transform_hdr(buf)) {
  845. length = server->ops->receive_transform(server,
  846. mids,
  847. bufs,
  848. &num_mids);
  849. } else {
  850. mids[0] = server->ops->find_mid(server, buf);
  851. bufs[0] = buf;
  852. num_mids = 1;
  853. if (!mids[0] || !mids[0]->receive)
  854. length = standard_receive3(server, mids[0]);
  855. else
  856. length = mids[0]->receive(server, mids[0]);
  857. }
  858. if (length < 0) {
  859. for (i = 0; i < num_mids; i++)
  860. if (mids[i])
  861. cifs_mid_q_entry_release(mids[i]);
  862. continue;
  863. }
  864. if (server->large_buf)
  865. buf = server->bigbuf;
  866. server->lstrp = jiffies;
  867. for (i = 0; i < num_mids; i++) {
  868. if (mids[i] != NULL) {
  869. mids[i]->resp_buf_size = server->pdu_size;
  870. if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
  871. mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
  872. server->ops->handle_cancelled_mid)
  873. server->ops->handle_cancelled_mid(
  874. mids[i]->resp_buf,
  875. server);
  876. if (!mids[i]->multiRsp || mids[i]->multiEnd)
  877. mids[i]->callback(mids[i]);
  878. cifs_mid_q_entry_release(mids[i]);
  879. } else if (server->ops->is_oplock_break &&
  880. server->ops->is_oplock_break(bufs[i],
  881. server)) {
  882. cifs_dbg(FYI, "Received oplock break\n");
  883. } else {
  884. cifs_dbg(VFS, "No task to wake, unknown frame "
  885. "received! NumMids %d\n",
  886. atomic_read(&midCount));
  887. cifs_dump_mem("Received Data is: ", bufs[i],
  888. HEADER_SIZE(server));
  889. #ifdef CONFIG_CIFS_DEBUG2
  890. if (server->ops->dump_detail)
  891. server->ops->dump_detail(bufs[i],
  892. server);
  893. cifs_dump_mids(server);
  894. #endif /* CIFS_DEBUG2 */
  895. }
  896. }
  897. if (pdu_length > server->pdu_size) {
  898. if (!allocate_buffers(server))
  899. continue;
  900. pdu_length -= server->pdu_size;
  901. server->total_read = 0;
  902. server->large_buf = false;
  903. buf = server->smallbuf;
  904. goto next_pdu;
  905. }
  906. } /* end while !EXITING */
  907. /* buffer usually freed in free_mid - need to free it here on exit */
  908. cifs_buf_release(server->bigbuf);
  909. if (server->smallbuf) /* no sense logging a debug message if NULL */
  910. cifs_small_buf_release(server->smallbuf);
  911. task_to_wake = xchg(&server->tsk, NULL);
  912. clean_demultiplex_info(server);
  913. /* if server->tsk was NULL then wait for a signal before exiting */
  914. if (!task_to_wake) {
  915. set_current_state(TASK_INTERRUPTIBLE);
  916. while (!signal_pending(current)) {
  917. schedule();
  918. set_current_state(TASK_INTERRUPTIBLE);
  919. }
  920. set_current_state(TASK_RUNNING);
  921. }
  922. module_put_and_exit(0);
  923. }
  924. /* extract the host portion of the UNC string */
  925. static char *
  926. extract_hostname(const char *unc)
  927. {
  928. const char *src;
  929. char *dst, *delim;
  930. unsigned int len;
  931. /* skip double chars at beginning of string */
  932. /* BB: check validity of these bytes? */
  933. src = unc + 2;
  934. /* delimiter between hostname and sharename is always '\\' now */
  935. delim = strchr(src, '\\');
  936. if (!delim)
  937. return ERR_PTR(-EINVAL);
  938. len = delim - src;
  939. dst = kmalloc((len + 1), GFP_KERNEL);
  940. if (dst == NULL)
  941. return ERR_PTR(-ENOMEM);
  942. memcpy(dst, src, len);
  943. dst[len] = '\0';
  944. return dst;
  945. }
  946. static int get_option_ul(substring_t args[], unsigned long *option)
  947. {
  948. int rc;
  949. char *string;
  950. string = match_strdup(args);
  951. if (string == NULL)
  952. return -ENOMEM;
  953. rc = kstrtoul(string, 0, option);
  954. kfree(string);
  955. return rc;
  956. }
  957. static int get_option_uid(substring_t args[], kuid_t *result)
  958. {
  959. unsigned long value;
  960. kuid_t uid;
  961. int rc;
  962. rc = get_option_ul(args, &value);
  963. if (rc)
  964. return rc;
  965. uid = make_kuid(current_user_ns(), value);
  966. if (!uid_valid(uid))
  967. return -EINVAL;
  968. *result = uid;
  969. return 0;
  970. }
  971. static int get_option_gid(substring_t args[], kgid_t *result)
  972. {
  973. unsigned long value;
  974. kgid_t gid;
  975. int rc;
  976. rc = get_option_ul(args, &value);
  977. if (rc)
  978. return rc;
  979. gid = make_kgid(current_user_ns(), value);
  980. if (!gid_valid(gid))
  981. return -EINVAL;
  982. *result = gid;
  983. return 0;
  984. }
  985. static int cifs_parse_security_flavors(char *value,
  986. struct smb_vol *vol)
  987. {
  988. substring_t args[MAX_OPT_ARGS];
  989. /*
  990. * With mount options, the last one should win. Reset any existing
  991. * settings back to default.
  992. */
  993. vol->sectype = Unspecified;
  994. vol->sign = false;
  995. switch (match_token(value, cifs_secflavor_tokens, args)) {
  996. case Opt_sec_krb5p:
  997. cifs_dbg(VFS, "sec=krb5p is not supported!\n");
  998. return 1;
  999. case Opt_sec_krb5i:
  1000. vol->sign = true;
  1001. /* Fallthrough */
  1002. case Opt_sec_krb5:
  1003. vol->sectype = Kerberos;
  1004. break;
  1005. case Opt_sec_ntlmsspi:
  1006. vol->sign = true;
  1007. /* Fallthrough */
  1008. case Opt_sec_ntlmssp:
  1009. vol->sectype = RawNTLMSSP;
  1010. break;
  1011. case Opt_sec_ntlmi:
  1012. vol->sign = true;
  1013. /* Fallthrough */
  1014. case Opt_ntlm:
  1015. vol->sectype = NTLM;
  1016. break;
  1017. case Opt_sec_ntlmv2i:
  1018. vol->sign = true;
  1019. /* Fallthrough */
  1020. case Opt_sec_ntlmv2:
  1021. vol->sectype = NTLMv2;
  1022. break;
  1023. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1024. case Opt_sec_lanman:
  1025. vol->sectype = LANMAN;
  1026. break;
  1027. #endif
  1028. case Opt_sec_none:
  1029. vol->nullauth = 1;
  1030. break;
  1031. default:
  1032. cifs_dbg(VFS, "bad security option: %s\n", value);
  1033. return 1;
  1034. }
  1035. return 0;
  1036. }
  1037. static int
  1038. cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
  1039. {
  1040. substring_t args[MAX_OPT_ARGS];
  1041. switch (match_token(value, cifs_cacheflavor_tokens, args)) {
  1042. case Opt_cache_loose:
  1043. vol->direct_io = false;
  1044. vol->strict_io = false;
  1045. break;
  1046. case Opt_cache_strict:
  1047. vol->direct_io = false;
  1048. vol->strict_io = true;
  1049. break;
  1050. case Opt_cache_none:
  1051. vol->direct_io = true;
  1052. vol->strict_io = false;
  1053. break;
  1054. default:
  1055. cifs_dbg(VFS, "bad cache= option: %s\n", value);
  1056. return 1;
  1057. }
  1058. return 0;
  1059. }
  1060. static int
  1061. cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
  1062. {
  1063. substring_t args[MAX_OPT_ARGS];
  1064. switch (match_token(value, cifs_smb_version_tokens, args)) {
  1065. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1066. case Smb_1:
  1067. if (disable_legacy_dialects) {
  1068. cifs_dbg(VFS, "mount with legacy dialect disabled\n");
  1069. return 1;
  1070. }
  1071. if (is_smb3) {
  1072. cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
  1073. return 1;
  1074. }
  1075. vol->ops = &smb1_operations;
  1076. vol->vals = &smb1_values;
  1077. break;
  1078. case Smb_20:
  1079. if (disable_legacy_dialects) {
  1080. cifs_dbg(VFS, "mount with legacy dialect disabled\n");
  1081. return 1;
  1082. }
  1083. if (is_smb3) {
  1084. cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
  1085. return 1;
  1086. }
  1087. vol->ops = &smb20_operations;
  1088. vol->vals = &smb20_values;
  1089. break;
  1090. #else
  1091. case Smb_1:
  1092. cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
  1093. return 1;
  1094. case Smb_20:
  1095. cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
  1096. return 1;
  1097. #endif /* CIFS_ALLOW_INSECURE_LEGACY */
  1098. case Smb_21:
  1099. vol->ops = &smb21_operations;
  1100. vol->vals = &smb21_values;
  1101. break;
  1102. case Smb_30:
  1103. vol->ops = &smb30_operations;
  1104. vol->vals = &smb30_values;
  1105. break;
  1106. case Smb_302:
  1107. vol->ops = &smb30_operations; /* currently identical with 3.0 */
  1108. vol->vals = &smb302_values;
  1109. break;
  1110. case Smb_311:
  1111. vol->ops = &smb311_operations;
  1112. vol->vals = &smb311_values;
  1113. break;
  1114. case Smb_3any:
  1115. vol->ops = &smb30_operations; /* currently identical with 3.0 */
  1116. vol->vals = &smb3any_values;
  1117. break;
  1118. case Smb_default:
  1119. vol->ops = &smb30_operations; /* currently identical with 3.0 */
  1120. vol->vals = &smbdefault_values;
  1121. break;
  1122. default:
  1123. cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
  1124. return 1;
  1125. }
  1126. return 0;
  1127. }
  1128. /*
  1129. * Parse a devname into substrings and populate the vol->UNC and vol->prepath
  1130. * fields with the result. Returns 0 on success and an error otherwise.
  1131. */
  1132. static int
  1133. cifs_parse_devname(const char *devname, struct smb_vol *vol)
  1134. {
  1135. char *pos;
  1136. const char *delims = "/\\";
  1137. size_t len;
  1138. /* make sure we have a valid UNC double delimiter prefix */
  1139. len = strspn(devname, delims);
  1140. if (len != 2)
  1141. return -EINVAL;
  1142. /* find delimiter between host and sharename */
  1143. pos = strpbrk(devname + 2, delims);
  1144. if (!pos)
  1145. return -EINVAL;
  1146. /* skip past delimiter */
  1147. ++pos;
  1148. /* now go until next delimiter or end of string */
  1149. len = strcspn(pos, delims);
  1150. /* move "pos" up to delimiter or NULL */
  1151. pos += len;
  1152. vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
  1153. if (!vol->UNC)
  1154. return -ENOMEM;
  1155. convert_delimiter(vol->UNC, '\\');
  1156. /* skip any delimiter */
  1157. if (*pos == '/' || *pos == '\\')
  1158. pos++;
  1159. /* If pos is NULL then no prepath */
  1160. if (!*pos)
  1161. return 0;
  1162. vol->prepath = kstrdup(pos, GFP_KERNEL);
  1163. if (!vol->prepath)
  1164. return -ENOMEM;
  1165. return 0;
  1166. }
  1167. static int
  1168. cifs_parse_mount_options(const char *mountdata, const char *devname,
  1169. struct smb_vol *vol, bool is_smb3)
  1170. {
  1171. char *data, *end;
  1172. char *mountdata_copy = NULL, *options;
  1173. unsigned int temp_len, i, j;
  1174. char separator[2];
  1175. short int override_uid = -1;
  1176. short int override_gid = -1;
  1177. bool uid_specified = false;
  1178. bool gid_specified = false;
  1179. bool sloppy = false;
  1180. char *invalid = NULL;
  1181. char *nodename = utsname()->nodename;
  1182. char *string = NULL;
  1183. char *tmp_end, *value;
  1184. char delim;
  1185. bool got_ip = false;
  1186. bool got_version = false;
  1187. unsigned short port = 0;
  1188. struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
  1189. separator[0] = ',';
  1190. separator[1] = 0;
  1191. delim = separator[0];
  1192. /* ensure we always start with zeroed-out smb_vol */
  1193. memset(vol, 0, sizeof(*vol));
  1194. /*
  1195. * does not have to be perfect mapping since field is
  1196. * informational, only used for servers that do not support
  1197. * port 445 and it can be overridden at mount time
  1198. */
  1199. memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
  1200. for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
  1201. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  1202. vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
  1203. /* null target name indicates to use *SMBSERVR default called name
  1204. if we end up sending RFC1001 session initialize */
  1205. vol->target_rfc1001_name[0] = 0;
  1206. vol->cred_uid = current_uid();
  1207. vol->linux_uid = current_uid();
  1208. vol->linux_gid = current_gid();
  1209. /*
  1210. * default to SFM style remapping of seven reserved characters
  1211. * unless user overrides it or we negotiate CIFS POSIX where
  1212. * it is unnecessary. Can not simultaneously use more than one mapping
  1213. * since then readdir could list files that open could not open
  1214. */
  1215. vol->remap = true;
  1216. /* default to only allowing write access to owner of the mount */
  1217. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  1218. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  1219. /* default is always to request posix paths. */
  1220. vol->posix_paths = 1;
  1221. /* default to using server inode numbers where available */
  1222. vol->server_ino = 1;
  1223. /* default is to use strict cifs caching semantics */
  1224. vol->strict_io = true;
  1225. vol->actimeo = CIFS_DEF_ACTIMEO;
  1226. /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
  1227. vol->ops = &smb30_operations;
  1228. vol->vals = &smbdefault_values;
  1229. vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
  1230. if (!mountdata)
  1231. goto cifs_parse_mount_err;
  1232. mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
  1233. if (!mountdata_copy)
  1234. goto cifs_parse_mount_err;
  1235. options = mountdata_copy;
  1236. end = options + strlen(options);
  1237. if (strncmp(options, "sep=", 4) == 0) {
  1238. if (options[4] != 0) {
  1239. separator[0] = options[4];
  1240. options += 5;
  1241. } else {
  1242. cifs_dbg(FYI, "Null separator not allowed\n");
  1243. }
  1244. }
  1245. vol->backupuid_specified = false; /* no backup intent for a user */
  1246. vol->backupgid_specified = false; /* no backup intent for a group */
  1247. switch (cifs_parse_devname(devname, vol)) {
  1248. case 0:
  1249. break;
  1250. case -ENOMEM:
  1251. cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
  1252. goto cifs_parse_mount_err;
  1253. case -EINVAL:
  1254. cifs_dbg(VFS, "Malformed UNC in devname.\n");
  1255. goto cifs_parse_mount_err;
  1256. default:
  1257. cifs_dbg(VFS, "Unknown error parsing devname.\n");
  1258. goto cifs_parse_mount_err;
  1259. }
  1260. while ((data = strsep(&options, separator)) != NULL) {
  1261. substring_t args[MAX_OPT_ARGS];
  1262. unsigned long option;
  1263. int token;
  1264. if (!*data)
  1265. continue;
  1266. token = match_token(data, cifs_mount_option_tokens, args);
  1267. switch (token) {
  1268. /* Ingnore the following */
  1269. case Opt_ignore:
  1270. break;
  1271. /* Boolean values */
  1272. case Opt_user_xattr:
  1273. vol->no_xattr = 0;
  1274. break;
  1275. case Opt_nouser_xattr:
  1276. vol->no_xattr = 1;
  1277. break;
  1278. case Opt_forceuid:
  1279. override_uid = 1;
  1280. break;
  1281. case Opt_noforceuid:
  1282. override_uid = 0;
  1283. break;
  1284. case Opt_forcegid:
  1285. override_gid = 1;
  1286. break;
  1287. case Opt_noforcegid:
  1288. override_gid = 0;
  1289. break;
  1290. case Opt_noblocksend:
  1291. vol->noblocksnd = 1;
  1292. break;
  1293. case Opt_noautotune:
  1294. vol->noautotune = 1;
  1295. break;
  1296. case Opt_hard:
  1297. vol->retry = 1;
  1298. break;
  1299. case Opt_soft:
  1300. vol->retry = 0;
  1301. break;
  1302. case Opt_perm:
  1303. vol->noperm = 0;
  1304. break;
  1305. case Opt_noperm:
  1306. vol->noperm = 1;
  1307. break;
  1308. case Opt_mapchars:
  1309. vol->sfu_remap = true;
  1310. vol->remap = false; /* disable SFM mapping */
  1311. break;
  1312. case Opt_nomapchars:
  1313. vol->sfu_remap = false;
  1314. break;
  1315. case Opt_mapposix:
  1316. vol->remap = true;
  1317. vol->sfu_remap = false; /* disable SFU mapping */
  1318. break;
  1319. case Opt_nomapposix:
  1320. vol->remap = false;
  1321. break;
  1322. case Opt_sfu:
  1323. vol->sfu_emul = 1;
  1324. break;
  1325. case Opt_nosfu:
  1326. vol->sfu_emul = 0;
  1327. break;
  1328. case Opt_nodfs:
  1329. vol->nodfs = 1;
  1330. break;
  1331. case Opt_posixpaths:
  1332. vol->posix_paths = 1;
  1333. break;
  1334. case Opt_noposixpaths:
  1335. vol->posix_paths = 0;
  1336. break;
  1337. case Opt_nounix:
  1338. if (vol->linux_ext)
  1339. cifs_dbg(VFS,
  1340. "conflicting unix mount options\n");
  1341. vol->no_linux_ext = 1;
  1342. break;
  1343. case Opt_unix:
  1344. if (vol->no_linux_ext)
  1345. cifs_dbg(VFS,
  1346. "conflicting unix mount options\n");
  1347. vol->linux_ext = 1;
  1348. break;
  1349. case Opt_nocase:
  1350. vol->nocase = 1;
  1351. break;
  1352. case Opt_brl:
  1353. vol->nobrl = 0;
  1354. break;
  1355. case Opt_nobrl:
  1356. vol->nobrl = 1;
  1357. /*
  1358. * turn off mandatory locking in mode
  1359. * if remote locking is turned off since the
  1360. * local vfs will do advisory
  1361. */
  1362. if (vol->file_mode ==
  1363. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1364. vol->file_mode = S_IALLUGO;
  1365. break;
  1366. case Opt_nohandlecache:
  1367. vol->nohandlecache = 1;
  1368. break;
  1369. case Opt_handlecache:
  1370. vol->nohandlecache = 0;
  1371. break;
  1372. case Opt_forcemandatorylock:
  1373. vol->mand_lock = 1;
  1374. break;
  1375. case Opt_setuids:
  1376. vol->setuids = 1;
  1377. break;
  1378. case Opt_nosetuids:
  1379. vol->setuids = 0;
  1380. break;
  1381. case Opt_setuidfromacl:
  1382. vol->setuidfromacl = 1;
  1383. break;
  1384. case Opt_dynperm:
  1385. vol->dynperm = true;
  1386. break;
  1387. case Opt_nodynperm:
  1388. vol->dynperm = false;
  1389. break;
  1390. case Opt_nohard:
  1391. vol->retry = 0;
  1392. break;
  1393. case Opt_nosoft:
  1394. vol->retry = 1;
  1395. break;
  1396. case Opt_nointr:
  1397. vol->intr = 0;
  1398. break;
  1399. case Opt_intr:
  1400. vol->intr = 1;
  1401. break;
  1402. case Opt_nostrictsync:
  1403. vol->nostrictsync = 1;
  1404. break;
  1405. case Opt_strictsync:
  1406. vol->nostrictsync = 0;
  1407. break;
  1408. case Opt_serverino:
  1409. vol->server_ino = 1;
  1410. break;
  1411. case Opt_noserverino:
  1412. vol->server_ino = 0;
  1413. break;
  1414. case Opt_rwpidforward:
  1415. vol->rwpidforward = 1;
  1416. break;
  1417. case Opt_cifsacl:
  1418. vol->cifs_acl = 1;
  1419. break;
  1420. case Opt_nocifsacl:
  1421. vol->cifs_acl = 0;
  1422. break;
  1423. case Opt_acl:
  1424. vol->no_psx_acl = 0;
  1425. break;
  1426. case Opt_noacl:
  1427. vol->no_psx_acl = 1;
  1428. break;
  1429. case Opt_locallease:
  1430. vol->local_lease = 1;
  1431. break;
  1432. case Opt_sign:
  1433. vol->sign = true;
  1434. break;
  1435. case Opt_seal:
  1436. /* we do not do the following in secFlags because seal
  1437. * is a per tree connection (mount) not a per socket
  1438. * or per-smb connection option in the protocol
  1439. * vol->secFlg |= CIFSSEC_MUST_SEAL;
  1440. */
  1441. vol->seal = 1;
  1442. break;
  1443. case Opt_noac:
  1444. pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
  1445. break;
  1446. case Opt_fsc:
  1447. #ifndef CONFIG_CIFS_FSCACHE
  1448. cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
  1449. goto cifs_parse_mount_err;
  1450. #endif
  1451. vol->fsc = true;
  1452. break;
  1453. case Opt_mfsymlinks:
  1454. vol->mfsymlinks = true;
  1455. break;
  1456. case Opt_multiuser:
  1457. vol->multiuser = true;
  1458. break;
  1459. case Opt_sloppy:
  1460. sloppy = true;
  1461. break;
  1462. case Opt_nosharesock:
  1463. vol->nosharesock = true;
  1464. break;
  1465. case Opt_nopersistent:
  1466. vol->nopersistent = true;
  1467. if (vol->persistent) {
  1468. cifs_dbg(VFS,
  1469. "persistenthandles mount options conflict\n");
  1470. goto cifs_parse_mount_err;
  1471. }
  1472. break;
  1473. case Opt_persistent:
  1474. vol->persistent = true;
  1475. if ((vol->nopersistent) || (vol->resilient)) {
  1476. cifs_dbg(VFS,
  1477. "persistenthandles mount options conflict\n");
  1478. goto cifs_parse_mount_err;
  1479. }
  1480. break;
  1481. case Opt_resilient:
  1482. vol->resilient = true;
  1483. if (vol->persistent) {
  1484. cifs_dbg(VFS,
  1485. "persistenthandles mount options conflict\n");
  1486. goto cifs_parse_mount_err;
  1487. }
  1488. break;
  1489. case Opt_noresilient:
  1490. vol->resilient = false; /* already the default */
  1491. break;
  1492. case Opt_domainauto:
  1493. vol->domainauto = true;
  1494. break;
  1495. case Opt_rdma:
  1496. vol->rdma = true;
  1497. break;
  1498. /* Numeric Values */
  1499. case Opt_backupuid:
  1500. if (get_option_uid(args, &vol->backupuid)) {
  1501. cifs_dbg(VFS, "%s: Invalid backupuid value\n",
  1502. __func__);
  1503. goto cifs_parse_mount_err;
  1504. }
  1505. vol->backupuid_specified = true;
  1506. break;
  1507. case Opt_backupgid:
  1508. if (get_option_gid(args, &vol->backupgid)) {
  1509. cifs_dbg(VFS, "%s: Invalid backupgid value\n",
  1510. __func__);
  1511. goto cifs_parse_mount_err;
  1512. }
  1513. vol->backupgid_specified = true;
  1514. break;
  1515. case Opt_uid:
  1516. if (get_option_uid(args, &vol->linux_uid)) {
  1517. cifs_dbg(VFS, "%s: Invalid uid value\n",
  1518. __func__);
  1519. goto cifs_parse_mount_err;
  1520. }
  1521. uid_specified = true;
  1522. break;
  1523. case Opt_cruid:
  1524. if (get_option_uid(args, &vol->cred_uid)) {
  1525. cifs_dbg(VFS, "%s: Invalid cruid value\n",
  1526. __func__);
  1527. goto cifs_parse_mount_err;
  1528. }
  1529. break;
  1530. case Opt_gid:
  1531. if (get_option_gid(args, &vol->linux_gid)) {
  1532. cifs_dbg(VFS, "%s: Invalid gid value\n",
  1533. __func__);
  1534. goto cifs_parse_mount_err;
  1535. }
  1536. gid_specified = true;
  1537. break;
  1538. case Opt_file_mode:
  1539. if (get_option_ul(args, &option)) {
  1540. cifs_dbg(VFS, "%s: Invalid file_mode value\n",
  1541. __func__);
  1542. goto cifs_parse_mount_err;
  1543. }
  1544. vol->file_mode = option;
  1545. break;
  1546. case Opt_dirmode:
  1547. if (get_option_ul(args, &option)) {
  1548. cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
  1549. __func__);
  1550. goto cifs_parse_mount_err;
  1551. }
  1552. vol->dir_mode = option;
  1553. break;
  1554. case Opt_port:
  1555. if (get_option_ul(args, &option) ||
  1556. option > USHRT_MAX) {
  1557. cifs_dbg(VFS, "%s: Invalid port value\n",
  1558. __func__);
  1559. goto cifs_parse_mount_err;
  1560. }
  1561. port = (unsigned short)option;
  1562. break;
  1563. case Opt_rsize:
  1564. if (get_option_ul(args, &option)) {
  1565. cifs_dbg(VFS, "%s: Invalid rsize value\n",
  1566. __func__);
  1567. goto cifs_parse_mount_err;
  1568. }
  1569. vol->rsize = option;
  1570. break;
  1571. case Opt_wsize:
  1572. if (get_option_ul(args, &option)) {
  1573. cifs_dbg(VFS, "%s: Invalid wsize value\n",
  1574. __func__);
  1575. goto cifs_parse_mount_err;
  1576. }
  1577. vol->wsize = option;
  1578. break;
  1579. case Opt_actimeo:
  1580. if (get_option_ul(args, &option)) {
  1581. cifs_dbg(VFS, "%s: Invalid actimeo value\n",
  1582. __func__);
  1583. goto cifs_parse_mount_err;
  1584. }
  1585. vol->actimeo = HZ * option;
  1586. if (vol->actimeo > CIFS_MAX_ACTIMEO) {
  1587. cifs_dbg(VFS, "attribute cache timeout too large\n");
  1588. goto cifs_parse_mount_err;
  1589. }
  1590. break;
  1591. case Opt_echo_interval:
  1592. if (get_option_ul(args, &option)) {
  1593. cifs_dbg(VFS, "%s: Invalid echo interval value\n",
  1594. __func__);
  1595. goto cifs_parse_mount_err;
  1596. }
  1597. vol->echo_interval = option;
  1598. break;
  1599. case Opt_snapshot:
  1600. if (get_option_ul(args, &option)) {
  1601. cifs_dbg(VFS, "%s: Invalid snapshot time\n",
  1602. __func__);
  1603. goto cifs_parse_mount_err;
  1604. }
  1605. vol->snapshot_time = option;
  1606. break;
  1607. case Opt_max_credits:
  1608. if (get_option_ul(args, &option) || (option < 20) ||
  1609. (option > 60000)) {
  1610. cifs_dbg(VFS, "%s: Invalid max_credits value\n",
  1611. __func__);
  1612. goto cifs_parse_mount_err;
  1613. }
  1614. vol->max_credits = option;
  1615. break;
  1616. /* String Arguments */
  1617. case Opt_blank_user:
  1618. /* null user, ie. anonymous authentication */
  1619. vol->nullauth = 1;
  1620. vol->username = NULL;
  1621. break;
  1622. case Opt_user:
  1623. string = match_strdup(args);
  1624. if (string == NULL)
  1625. goto out_nomem;
  1626. if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
  1627. CIFS_MAX_USERNAME_LEN) {
  1628. pr_warn("CIFS: username too long\n");
  1629. goto cifs_parse_mount_err;
  1630. }
  1631. kfree(vol->username);
  1632. vol->username = kstrdup(string, GFP_KERNEL);
  1633. if (!vol->username)
  1634. goto cifs_parse_mount_err;
  1635. break;
  1636. case Opt_blank_pass:
  1637. /* passwords have to be handled differently
  1638. * to allow the character used for deliminator
  1639. * to be passed within them
  1640. */
  1641. /*
  1642. * Check if this is a case where the password
  1643. * starts with a delimiter
  1644. */
  1645. tmp_end = strchr(data, '=');
  1646. tmp_end++;
  1647. if (!(tmp_end < end && tmp_end[1] == delim)) {
  1648. /* No it is not. Set the password to NULL */
  1649. kzfree(vol->password);
  1650. vol->password = NULL;
  1651. break;
  1652. }
  1653. /* Yes it is. Drop down to Opt_pass below.*/
  1654. case Opt_pass:
  1655. /* Obtain the value string */
  1656. value = strchr(data, '=');
  1657. value++;
  1658. /* Set tmp_end to end of the string */
  1659. tmp_end = (char *) value + strlen(value);
  1660. /* Check if following character is the deliminator
  1661. * If yes, we have encountered a double deliminator
  1662. * reset the NULL character to the deliminator
  1663. */
  1664. if (tmp_end < end && tmp_end[1] == delim) {
  1665. tmp_end[0] = delim;
  1666. /* Keep iterating until we get to a single
  1667. * deliminator OR the end
  1668. */
  1669. while ((tmp_end = strchr(tmp_end, delim))
  1670. != NULL && (tmp_end[1] == delim)) {
  1671. tmp_end = (char *) &tmp_end[2];
  1672. }
  1673. /* Reset var options to point to next element */
  1674. if (tmp_end) {
  1675. tmp_end[0] = '\0';
  1676. options = (char *) &tmp_end[1];
  1677. } else
  1678. /* Reached the end of the mount option
  1679. * string */
  1680. options = end;
  1681. }
  1682. kzfree(vol->password);
  1683. /* Now build new password string */
  1684. temp_len = strlen(value);
  1685. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  1686. if (vol->password == NULL) {
  1687. pr_warn("CIFS: no memory for password\n");
  1688. goto cifs_parse_mount_err;
  1689. }
  1690. for (i = 0, j = 0; i < temp_len; i++, j++) {
  1691. vol->password[j] = value[i];
  1692. if ((value[i] == delim) &&
  1693. value[i+1] == delim)
  1694. /* skip the second deliminator */
  1695. i++;
  1696. }
  1697. vol->password[j] = '\0';
  1698. break;
  1699. case Opt_blank_ip:
  1700. /* FIXME: should this be an error instead? */
  1701. got_ip = false;
  1702. break;
  1703. case Opt_ip:
  1704. string = match_strdup(args);
  1705. if (string == NULL)
  1706. goto out_nomem;
  1707. if (!cifs_convert_address(dstaddr, string,
  1708. strlen(string))) {
  1709. pr_err("CIFS: bad ip= option (%s).\n", string);
  1710. goto cifs_parse_mount_err;
  1711. }
  1712. got_ip = true;
  1713. break;
  1714. case Opt_domain:
  1715. string = match_strdup(args);
  1716. if (string == NULL)
  1717. goto out_nomem;
  1718. if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
  1719. == CIFS_MAX_DOMAINNAME_LEN) {
  1720. pr_warn("CIFS: domain name too long\n");
  1721. goto cifs_parse_mount_err;
  1722. }
  1723. kfree(vol->domainname);
  1724. vol->domainname = kstrdup(string, GFP_KERNEL);
  1725. if (!vol->domainname) {
  1726. pr_warn("CIFS: no memory for domainname\n");
  1727. goto cifs_parse_mount_err;
  1728. }
  1729. cifs_dbg(FYI, "Domain name set\n");
  1730. break;
  1731. case Opt_srcaddr:
  1732. string = match_strdup(args);
  1733. if (string == NULL)
  1734. goto out_nomem;
  1735. if (!cifs_convert_address(
  1736. (struct sockaddr *)&vol->srcaddr,
  1737. string, strlen(string))) {
  1738. pr_warn("CIFS: Could not parse srcaddr: %s\n",
  1739. string);
  1740. goto cifs_parse_mount_err;
  1741. }
  1742. break;
  1743. case Opt_iocharset:
  1744. string = match_strdup(args);
  1745. if (string == NULL)
  1746. goto out_nomem;
  1747. if (strnlen(string, 1024) >= 65) {
  1748. pr_warn("CIFS: iocharset name too long.\n");
  1749. goto cifs_parse_mount_err;
  1750. }
  1751. if (strncasecmp(string, "default", 7) != 0) {
  1752. kfree(vol->iocharset);
  1753. vol->iocharset = kstrdup(string,
  1754. GFP_KERNEL);
  1755. if (!vol->iocharset) {
  1756. pr_warn("CIFS: no memory for charset\n");
  1757. goto cifs_parse_mount_err;
  1758. }
  1759. }
  1760. /* if iocharset not set then load_nls_default
  1761. * is used by caller
  1762. */
  1763. cifs_dbg(FYI, "iocharset set to %s\n", string);
  1764. break;
  1765. case Opt_netbiosname:
  1766. string = match_strdup(args);
  1767. if (string == NULL)
  1768. goto out_nomem;
  1769. memset(vol->source_rfc1001_name, 0x20,
  1770. RFC1001_NAME_LEN);
  1771. /*
  1772. * FIXME: are there cases in which a comma can
  1773. * be valid in workstation netbios name (and
  1774. * need special handling)?
  1775. */
  1776. for (i = 0; i < RFC1001_NAME_LEN; i++) {
  1777. /* don't ucase netbiosname for user */
  1778. if (string[i] == 0)
  1779. break;
  1780. vol->source_rfc1001_name[i] = string[i];
  1781. }
  1782. /* The string has 16th byte zero still from
  1783. * set at top of the function
  1784. */
  1785. if (i == RFC1001_NAME_LEN && string[i] != 0)
  1786. pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
  1787. break;
  1788. case Opt_servern:
  1789. /* servernetbiosname specified override *SMBSERVER */
  1790. string = match_strdup(args);
  1791. if (string == NULL)
  1792. goto out_nomem;
  1793. /* last byte, type, is 0x20 for servr type */
  1794. memset(vol->target_rfc1001_name, 0x20,
  1795. RFC1001_NAME_LEN_WITH_NULL);
  1796. /* BB are there cases in which a comma can be
  1797. valid in this workstation netbios name
  1798. (and need special handling)? */
  1799. /* user or mount helper must uppercase the
  1800. netbios name */
  1801. for (i = 0; i < 15; i++) {
  1802. if (string[i] == 0)
  1803. break;
  1804. vol->target_rfc1001_name[i] = string[i];
  1805. }
  1806. /* The string has 16th byte zero still from
  1807. set at top of the function */
  1808. if (i == RFC1001_NAME_LEN && string[i] != 0)
  1809. pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
  1810. break;
  1811. case Opt_ver:
  1812. /* version of mount userspace tools, not dialect */
  1813. string = match_strdup(args);
  1814. if (string == NULL)
  1815. goto out_nomem;
  1816. /* If interface changes in mount.cifs bump to new ver */
  1817. if (strncasecmp(string, "1", 1) == 0) {
  1818. if (strlen(string) > 1) {
  1819. pr_warn("Bad mount helper ver=%s. Did "
  1820. "you want SMB1 (CIFS) dialect "
  1821. "and mean to type vers=1.0 "
  1822. "instead?\n", string);
  1823. goto cifs_parse_mount_err;
  1824. }
  1825. /* This is the default */
  1826. break;
  1827. }
  1828. /* For all other value, error */
  1829. pr_warn("CIFS: Invalid mount helper version specified\n");
  1830. goto cifs_parse_mount_err;
  1831. case Opt_vers:
  1832. /* protocol version (dialect) */
  1833. string = match_strdup(args);
  1834. if (string == NULL)
  1835. goto out_nomem;
  1836. if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
  1837. goto cifs_parse_mount_err;
  1838. got_version = true;
  1839. break;
  1840. case Opt_sec:
  1841. string = match_strdup(args);
  1842. if (string == NULL)
  1843. goto out_nomem;
  1844. if (cifs_parse_security_flavors(string, vol) != 0)
  1845. goto cifs_parse_mount_err;
  1846. break;
  1847. case Opt_cache:
  1848. string = match_strdup(args);
  1849. if (string == NULL)
  1850. goto out_nomem;
  1851. if (cifs_parse_cache_flavor(string, vol) != 0)
  1852. goto cifs_parse_mount_err;
  1853. break;
  1854. default:
  1855. /*
  1856. * An option we don't recognize. Save it off for later
  1857. * if we haven't already found one
  1858. */
  1859. if (!invalid)
  1860. invalid = data;
  1861. break;
  1862. }
  1863. /* Free up any allocated string */
  1864. kfree(string);
  1865. string = NULL;
  1866. }
  1867. if (!sloppy && invalid) {
  1868. pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
  1869. goto cifs_parse_mount_err;
  1870. }
  1871. if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
  1872. cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
  1873. goto cifs_parse_mount_err;
  1874. }
  1875. #ifndef CONFIG_KEYS
  1876. /* Muliuser mounts require CONFIG_KEYS support */
  1877. if (vol->multiuser) {
  1878. cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
  1879. goto cifs_parse_mount_err;
  1880. }
  1881. #endif
  1882. if (!vol->UNC) {
  1883. cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
  1884. goto cifs_parse_mount_err;
  1885. }
  1886. /* make sure UNC has a share name */
  1887. if (!strchr(vol->UNC + 3, '\\')) {
  1888. cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
  1889. goto cifs_parse_mount_err;
  1890. }
  1891. if (!got_ip) {
  1892. int len;
  1893. const char *slash;
  1894. /* No ip= option specified? Try to get it from UNC */
  1895. /* Use the address part of the UNC. */
  1896. slash = strchr(&vol->UNC[2], '\\');
  1897. len = slash - &vol->UNC[2];
  1898. if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
  1899. pr_err("Unable to determine destination address.\n");
  1900. goto cifs_parse_mount_err;
  1901. }
  1902. }
  1903. /* set the port that we got earlier */
  1904. cifs_set_port(dstaddr, port);
  1905. if (uid_specified)
  1906. vol->override_uid = override_uid;
  1907. else if (override_uid == 1)
  1908. pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
  1909. if (gid_specified)
  1910. vol->override_gid = override_gid;
  1911. else if (override_gid == 1)
  1912. pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
  1913. if (got_version == false)
  1914. pr_warn("No dialect specified on mount. Default has changed to "
  1915. "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
  1916. "(SMB1). To use the less secure SMB1 dialect to access "
  1917. "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
  1918. " on mount.\n");
  1919. kfree(mountdata_copy);
  1920. return 0;
  1921. out_nomem:
  1922. pr_warn("Could not allocate temporary buffer\n");
  1923. cifs_parse_mount_err:
  1924. kfree(string);
  1925. kfree(mountdata_copy);
  1926. return 1;
  1927. }
  1928. /** Returns true if srcaddr isn't specified and rhs isn't
  1929. * specified, or if srcaddr is specified and
  1930. * matches the IP address of the rhs argument.
  1931. */
  1932. static bool
  1933. srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1934. {
  1935. switch (srcaddr->sa_family) {
  1936. case AF_UNSPEC:
  1937. return (rhs->sa_family == AF_UNSPEC);
  1938. case AF_INET: {
  1939. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1940. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1941. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  1942. }
  1943. case AF_INET6: {
  1944. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1945. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
  1946. return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
  1947. }
  1948. default:
  1949. WARN_ON(1);
  1950. return false; /* don't expect to be here */
  1951. }
  1952. }
  1953. /*
  1954. * If no port is specified in addr structure, we try to match with 445 port
  1955. * and if it fails - with 139 ports. It should be called only if address
  1956. * families of server and addr are equal.
  1957. */
  1958. static bool
  1959. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  1960. {
  1961. __be16 port, *sport;
  1962. switch (addr->sa_family) {
  1963. case AF_INET:
  1964. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  1965. port = ((struct sockaddr_in *) addr)->sin_port;
  1966. break;
  1967. case AF_INET6:
  1968. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  1969. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  1970. break;
  1971. default:
  1972. WARN_ON(1);
  1973. return false;
  1974. }
  1975. if (!port) {
  1976. port = htons(CIFS_PORT);
  1977. if (port == *sport)
  1978. return true;
  1979. port = htons(RFC1001_PORT);
  1980. }
  1981. return port == *sport;
  1982. }
  1983. static bool
  1984. match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
  1985. struct sockaddr *srcaddr)
  1986. {
  1987. switch (addr->sa_family) {
  1988. case AF_INET: {
  1989. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  1990. struct sockaddr_in *srv_addr4 =
  1991. (struct sockaddr_in *)&server->dstaddr;
  1992. if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
  1993. return false;
  1994. break;
  1995. }
  1996. case AF_INET6: {
  1997. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  1998. struct sockaddr_in6 *srv_addr6 =
  1999. (struct sockaddr_in6 *)&server->dstaddr;
  2000. if (!ipv6_addr_equal(&addr6->sin6_addr,
  2001. &srv_addr6->sin6_addr))
  2002. return false;
  2003. if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
  2004. return false;
  2005. break;
  2006. }
  2007. default:
  2008. WARN_ON(1);
  2009. return false; /* don't expect to be here */
  2010. }
  2011. if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
  2012. return false;
  2013. return true;
  2014. }
  2015. static bool
  2016. match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
  2017. {
  2018. /*
  2019. * The select_sectype function should either return the vol->sectype
  2020. * that was specified, or "Unspecified" if that sectype was not
  2021. * compatible with the given NEGOTIATE request.
  2022. */
  2023. if (server->ops->select_sectype(server, vol->sectype)
  2024. == Unspecified)
  2025. return false;
  2026. /*
  2027. * Now check if signing mode is acceptable. No need to check
  2028. * global_secflags at this point since if MUST_SIGN is set then
  2029. * the server->sign had better be too.
  2030. */
  2031. if (vol->sign && !server->sign)
  2032. return false;
  2033. return true;
  2034. }
  2035. static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
  2036. {
  2037. struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
  2038. if (vol->nosharesock)
  2039. return 0;
  2040. /* BB update this for smb3any and default case */
  2041. if ((server->vals != vol->vals) || (server->ops != vol->ops))
  2042. return 0;
  2043. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  2044. return 0;
  2045. if (!match_address(server, addr,
  2046. (struct sockaddr *)&vol->srcaddr))
  2047. return 0;
  2048. if (!match_port(server, addr))
  2049. return 0;
  2050. if (!match_security(server, vol))
  2051. return 0;
  2052. if (server->echo_interval != vol->echo_interval * HZ)
  2053. return 0;
  2054. if (server->rdma != vol->rdma)
  2055. return 0;
  2056. return 1;
  2057. }
  2058. static struct TCP_Server_Info *
  2059. cifs_find_tcp_session(struct smb_vol *vol)
  2060. {
  2061. struct TCP_Server_Info *server;
  2062. spin_lock(&cifs_tcp_ses_lock);
  2063. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  2064. if (!match_server(server, vol))
  2065. continue;
  2066. ++server->srv_count;
  2067. spin_unlock(&cifs_tcp_ses_lock);
  2068. cifs_dbg(FYI, "Existing tcp session with server found\n");
  2069. return server;
  2070. }
  2071. spin_unlock(&cifs_tcp_ses_lock);
  2072. return NULL;
  2073. }
  2074. void
  2075. cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
  2076. {
  2077. struct task_struct *task;
  2078. spin_lock(&cifs_tcp_ses_lock);
  2079. if (--server->srv_count > 0) {
  2080. spin_unlock(&cifs_tcp_ses_lock);
  2081. return;
  2082. }
  2083. put_net(cifs_net_ns(server));
  2084. list_del_init(&server->tcp_ses_list);
  2085. spin_unlock(&cifs_tcp_ses_lock);
  2086. cancel_delayed_work_sync(&server->echo);
  2087. if (from_reconnect)
  2088. /*
  2089. * Avoid deadlock here: reconnect work calls
  2090. * cifs_put_tcp_session() at its end. Need to be sure
  2091. * that reconnect work does nothing with server pointer after
  2092. * that step.
  2093. */
  2094. cancel_delayed_work(&server->reconnect);
  2095. else
  2096. cancel_delayed_work_sync(&server->reconnect);
  2097. spin_lock(&GlobalMid_Lock);
  2098. server->tcpStatus = CifsExiting;
  2099. spin_unlock(&GlobalMid_Lock);
  2100. cifs_crypto_secmech_release(server);
  2101. cifs_fscache_release_client_cookie(server);
  2102. kfree(server->session_key.response);
  2103. server->session_key.response = NULL;
  2104. server->session_key.len = 0;
  2105. task = xchg(&server->tsk, NULL);
  2106. if (task)
  2107. force_sig(SIGKILL, task);
  2108. }
  2109. static struct TCP_Server_Info *
  2110. cifs_get_tcp_session(struct smb_vol *volume_info)
  2111. {
  2112. struct TCP_Server_Info *tcp_ses = NULL;
  2113. int rc;
  2114. cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
  2115. /* see if we already have a matching tcp_ses */
  2116. tcp_ses = cifs_find_tcp_session(volume_info);
  2117. if (tcp_ses)
  2118. return tcp_ses;
  2119. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  2120. if (!tcp_ses) {
  2121. rc = -ENOMEM;
  2122. goto out_err;
  2123. }
  2124. tcp_ses->ops = volume_info->ops;
  2125. tcp_ses->vals = volume_info->vals;
  2126. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  2127. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  2128. if (IS_ERR(tcp_ses->hostname)) {
  2129. rc = PTR_ERR(tcp_ses->hostname);
  2130. goto out_err_crypto_release;
  2131. }
  2132. tcp_ses->noblocksnd = volume_info->noblocksnd;
  2133. tcp_ses->noautotune = volume_info->noautotune;
  2134. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  2135. tcp_ses->rdma = volume_info->rdma;
  2136. tcp_ses->in_flight = 0;
  2137. tcp_ses->credits = 1;
  2138. init_waitqueue_head(&tcp_ses->response_q);
  2139. init_waitqueue_head(&tcp_ses->request_q);
  2140. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  2141. mutex_init(&tcp_ses->srv_mutex);
  2142. memcpy(tcp_ses->workstation_RFC1001_name,
  2143. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  2144. memcpy(tcp_ses->server_RFC1001_name,
  2145. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  2146. tcp_ses->session_estab = false;
  2147. tcp_ses->sequence_number = 0;
  2148. tcp_ses->lstrp = jiffies;
  2149. spin_lock_init(&tcp_ses->req_lock);
  2150. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  2151. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  2152. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  2153. INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
  2154. mutex_init(&tcp_ses->reconnect_mutex);
  2155. memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
  2156. sizeof(tcp_ses->srcaddr));
  2157. memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
  2158. sizeof(tcp_ses->dstaddr));
  2159. generate_random_uuid(tcp_ses->client_guid);
  2160. /*
  2161. * at this point we are the only ones with the pointer
  2162. * to the struct since the kernel thread not created yet
  2163. * no need to spinlock this init of tcpStatus or srv_count
  2164. */
  2165. tcp_ses->tcpStatus = CifsNew;
  2166. ++tcp_ses->srv_count;
  2167. if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
  2168. volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
  2169. tcp_ses->echo_interval = volume_info->echo_interval * HZ;
  2170. else
  2171. tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
  2172. if (tcp_ses->rdma) {
  2173. #ifndef CONFIG_CIFS_SMB_DIRECT
  2174. cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
  2175. rc = -ENOENT;
  2176. goto out_err_crypto_release;
  2177. #endif
  2178. tcp_ses->smbd_conn = smbd_get_connection(
  2179. tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
  2180. if (tcp_ses->smbd_conn) {
  2181. cifs_dbg(VFS, "RDMA transport established\n");
  2182. rc = 0;
  2183. goto smbd_connected;
  2184. } else {
  2185. rc = -ENOENT;
  2186. goto out_err_crypto_release;
  2187. }
  2188. }
  2189. rc = ip_connect(tcp_ses);
  2190. if (rc < 0) {
  2191. cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
  2192. goto out_err_crypto_release;
  2193. }
  2194. smbd_connected:
  2195. /*
  2196. * since we're in a cifs function already, we know that
  2197. * this will succeed. No need for try_module_get().
  2198. */
  2199. __module_get(THIS_MODULE);
  2200. tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
  2201. tcp_ses, "cifsd");
  2202. if (IS_ERR(tcp_ses->tsk)) {
  2203. rc = PTR_ERR(tcp_ses->tsk);
  2204. cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
  2205. module_put(THIS_MODULE);
  2206. goto out_err_crypto_release;
  2207. }
  2208. tcp_ses->tcpStatus = CifsNeedNegotiate;
  2209. /* thread spawned, put it on the list */
  2210. spin_lock(&cifs_tcp_ses_lock);
  2211. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  2212. spin_unlock(&cifs_tcp_ses_lock);
  2213. cifs_fscache_get_client_cookie(tcp_ses);
  2214. /* queue echo request delayed work */
  2215. queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
  2216. return tcp_ses;
  2217. out_err_crypto_release:
  2218. cifs_crypto_secmech_release(tcp_ses);
  2219. put_net(cifs_net_ns(tcp_ses));
  2220. out_err:
  2221. if (tcp_ses) {
  2222. if (!IS_ERR(tcp_ses->hostname))
  2223. kfree(tcp_ses->hostname);
  2224. if (tcp_ses->ssocket)
  2225. sock_release(tcp_ses->ssocket);
  2226. kfree(tcp_ses);
  2227. }
  2228. return ERR_PTR(rc);
  2229. }
  2230. static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
  2231. {
  2232. if (vol->sectype != Unspecified &&
  2233. vol->sectype != ses->sectype)
  2234. return 0;
  2235. switch (ses->sectype) {
  2236. case Kerberos:
  2237. if (!uid_eq(vol->cred_uid, ses->cred_uid))
  2238. return 0;
  2239. break;
  2240. default:
  2241. /* NULL username means anonymous session */
  2242. if (ses->user_name == NULL) {
  2243. if (!vol->nullauth)
  2244. return 0;
  2245. break;
  2246. }
  2247. /* anything else takes username/password */
  2248. if (strncmp(ses->user_name,
  2249. vol->username ? vol->username : "",
  2250. CIFS_MAX_USERNAME_LEN))
  2251. return 0;
  2252. if ((vol->username && strlen(vol->username) != 0) &&
  2253. ses->password != NULL &&
  2254. strncmp(ses->password,
  2255. vol->password ? vol->password : "",
  2256. CIFS_MAX_PASSWORD_LEN))
  2257. return 0;
  2258. }
  2259. return 1;
  2260. }
  2261. /**
  2262. * cifs_setup_ipc - helper to setup the IPC tcon for the session
  2263. *
  2264. * A new IPC connection is made and stored in the session
  2265. * tcon_ipc. The IPC tcon has the same lifetime as the session.
  2266. */
  2267. static int
  2268. cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
  2269. {
  2270. int rc = 0, xid;
  2271. struct cifs_tcon *tcon;
  2272. struct nls_table *nls_codepage;
  2273. char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
  2274. bool seal = false;
  2275. /*
  2276. * If the mount request that resulted in the creation of the
  2277. * session requires encryption, force IPC to be encrypted too.
  2278. */
  2279. if (volume_info->seal) {
  2280. if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
  2281. seal = true;
  2282. else {
  2283. cifs_dbg(VFS,
  2284. "IPC: server doesn't support encryption\n");
  2285. return -EOPNOTSUPP;
  2286. }
  2287. }
  2288. tcon = tconInfoAlloc();
  2289. if (tcon == NULL)
  2290. return -ENOMEM;
  2291. snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
  2292. /* cannot fail */
  2293. nls_codepage = load_nls_default();
  2294. xid = get_xid();
  2295. tcon->ses = ses;
  2296. tcon->ipc = true;
  2297. tcon->seal = seal;
  2298. rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
  2299. free_xid(xid);
  2300. if (rc) {
  2301. cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
  2302. tconInfoFree(tcon);
  2303. goto out;
  2304. }
  2305. cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
  2306. ses->tcon_ipc = tcon;
  2307. out:
  2308. unload_nls(nls_codepage);
  2309. return rc;
  2310. }
  2311. /**
  2312. * cifs_free_ipc - helper to release the session IPC tcon
  2313. *
  2314. * Needs to be called everytime a session is destroyed
  2315. */
  2316. static int
  2317. cifs_free_ipc(struct cifs_ses *ses)
  2318. {
  2319. int rc = 0, xid;
  2320. struct cifs_tcon *tcon = ses->tcon_ipc;
  2321. if (tcon == NULL)
  2322. return 0;
  2323. if (ses->server->ops->tree_disconnect) {
  2324. xid = get_xid();
  2325. rc = ses->server->ops->tree_disconnect(xid, tcon);
  2326. free_xid(xid);
  2327. }
  2328. if (rc)
  2329. cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
  2330. tconInfoFree(tcon);
  2331. ses->tcon_ipc = NULL;
  2332. return rc;
  2333. }
  2334. static struct cifs_ses *
  2335. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
  2336. {
  2337. struct cifs_ses *ses;
  2338. spin_lock(&cifs_tcp_ses_lock);
  2339. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  2340. if (ses->status == CifsExiting)
  2341. continue;
  2342. if (!match_session(ses, vol))
  2343. continue;
  2344. ++ses->ses_count;
  2345. spin_unlock(&cifs_tcp_ses_lock);
  2346. return ses;
  2347. }
  2348. spin_unlock(&cifs_tcp_ses_lock);
  2349. return NULL;
  2350. }
  2351. static void
  2352. cifs_put_smb_ses(struct cifs_ses *ses)
  2353. {
  2354. unsigned int rc, xid;
  2355. struct TCP_Server_Info *server = ses->server;
  2356. cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
  2357. spin_lock(&cifs_tcp_ses_lock);
  2358. if (ses->status == CifsExiting) {
  2359. spin_unlock(&cifs_tcp_ses_lock);
  2360. return;
  2361. }
  2362. if (--ses->ses_count > 0) {
  2363. spin_unlock(&cifs_tcp_ses_lock);
  2364. return;
  2365. }
  2366. if (ses->status == CifsGood)
  2367. ses->status = CifsExiting;
  2368. spin_unlock(&cifs_tcp_ses_lock);
  2369. cifs_free_ipc(ses);
  2370. if (ses->status == CifsExiting && server->ops->logoff) {
  2371. xid = get_xid();
  2372. rc = server->ops->logoff(xid, ses);
  2373. if (rc)
  2374. cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
  2375. __func__, rc);
  2376. _free_xid(xid);
  2377. }
  2378. spin_lock(&cifs_tcp_ses_lock);
  2379. list_del_init(&ses->smb_ses_list);
  2380. spin_unlock(&cifs_tcp_ses_lock);
  2381. sesInfoFree(ses);
  2382. cifs_put_tcp_session(server, 0);
  2383. }
  2384. #ifdef CONFIG_KEYS
  2385. /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
  2386. #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
  2387. /* Populate username and pw fields from keyring if possible */
  2388. static int
  2389. cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
  2390. {
  2391. int rc = 0;
  2392. const char *delim, *payload;
  2393. char *desc;
  2394. ssize_t len;
  2395. struct key *key;
  2396. struct TCP_Server_Info *server = ses->server;
  2397. struct sockaddr_in *sa;
  2398. struct sockaddr_in6 *sa6;
  2399. const struct user_key_payload *upayload;
  2400. desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
  2401. if (!desc)
  2402. return -ENOMEM;
  2403. /* try to find an address key first */
  2404. switch (server->dstaddr.ss_family) {
  2405. case AF_INET:
  2406. sa = (struct sockaddr_in *)&server->dstaddr;
  2407. sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
  2408. break;
  2409. case AF_INET6:
  2410. sa6 = (struct sockaddr_in6 *)&server->dstaddr;
  2411. sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
  2412. break;
  2413. default:
  2414. cifs_dbg(FYI, "Bad ss_family (%hu)\n",
  2415. server->dstaddr.ss_family);
  2416. rc = -EINVAL;
  2417. goto out_err;
  2418. }
  2419. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  2420. key = request_key(&key_type_logon, desc, "");
  2421. if (IS_ERR(key)) {
  2422. if (!ses->domainName) {
  2423. cifs_dbg(FYI, "domainName is NULL\n");
  2424. rc = PTR_ERR(key);
  2425. goto out_err;
  2426. }
  2427. /* didn't work, try to find a domain key */
  2428. sprintf(desc, "cifs:d:%s", ses->domainName);
  2429. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  2430. key = request_key(&key_type_logon, desc, "");
  2431. if (IS_ERR(key)) {
  2432. rc = PTR_ERR(key);
  2433. goto out_err;
  2434. }
  2435. }
  2436. down_read(&key->sem);
  2437. upayload = user_key_payload_locked(key);
  2438. if (IS_ERR_OR_NULL(upayload)) {
  2439. rc = upayload ? PTR_ERR(upayload) : -EINVAL;
  2440. goto out_key_put;
  2441. }
  2442. /* find first : in payload */
  2443. payload = upayload->data;
  2444. delim = strnchr(payload, upayload->datalen, ':');
  2445. cifs_dbg(FYI, "payload=%s\n", payload);
  2446. if (!delim) {
  2447. cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
  2448. upayload->datalen);
  2449. rc = -EINVAL;
  2450. goto out_key_put;
  2451. }
  2452. len = delim - payload;
  2453. if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
  2454. cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
  2455. len);
  2456. rc = -EINVAL;
  2457. goto out_key_put;
  2458. }
  2459. vol->username = kstrndup(payload, len, GFP_KERNEL);
  2460. if (!vol->username) {
  2461. cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
  2462. len);
  2463. rc = -ENOMEM;
  2464. goto out_key_put;
  2465. }
  2466. cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
  2467. len = key->datalen - (len + 1);
  2468. if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
  2469. cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
  2470. rc = -EINVAL;
  2471. kfree(vol->username);
  2472. vol->username = NULL;
  2473. goto out_key_put;
  2474. }
  2475. ++delim;
  2476. vol->password = kstrndup(delim, len, GFP_KERNEL);
  2477. if (!vol->password) {
  2478. cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
  2479. len);
  2480. rc = -ENOMEM;
  2481. kfree(vol->username);
  2482. vol->username = NULL;
  2483. goto out_key_put;
  2484. }
  2485. out_key_put:
  2486. up_read(&key->sem);
  2487. key_put(key);
  2488. out_err:
  2489. kfree(desc);
  2490. cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
  2491. return rc;
  2492. }
  2493. #else /* ! CONFIG_KEYS */
  2494. static inline int
  2495. cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
  2496. struct cifs_ses *ses __attribute__((unused)))
  2497. {
  2498. return -ENOSYS;
  2499. }
  2500. #endif /* CONFIG_KEYS */
  2501. /**
  2502. * cifs_get_smb_ses - get a session matching @volume_info data from @server
  2503. *
  2504. * This function assumes it is being called from cifs_mount() where we
  2505. * already got a server reference (server refcount +1). See
  2506. * cifs_get_tcon() for refcount explanations.
  2507. */
  2508. static struct cifs_ses *
  2509. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  2510. {
  2511. int rc = -ENOMEM;
  2512. unsigned int xid;
  2513. struct cifs_ses *ses;
  2514. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2515. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2516. xid = get_xid();
  2517. ses = cifs_find_smb_ses(server, volume_info);
  2518. if (ses) {
  2519. cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
  2520. ses->status);
  2521. mutex_lock(&ses->session_mutex);
  2522. rc = cifs_negotiate_protocol(xid, ses);
  2523. if (rc) {
  2524. mutex_unlock(&ses->session_mutex);
  2525. /* problem -- put our ses reference */
  2526. cifs_put_smb_ses(ses);
  2527. free_xid(xid);
  2528. return ERR_PTR(rc);
  2529. }
  2530. if (ses->need_reconnect) {
  2531. cifs_dbg(FYI, "Session needs reconnect\n");
  2532. rc = cifs_setup_session(xid, ses,
  2533. volume_info->local_nls);
  2534. if (rc) {
  2535. mutex_unlock(&ses->session_mutex);
  2536. /* problem -- put our reference */
  2537. cifs_put_smb_ses(ses);
  2538. free_xid(xid);
  2539. return ERR_PTR(rc);
  2540. }
  2541. }
  2542. mutex_unlock(&ses->session_mutex);
  2543. /* existing SMB ses has a server reference already */
  2544. cifs_put_tcp_session(server, 0);
  2545. free_xid(xid);
  2546. return ses;
  2547. }
  2548. cifs_dbg(FYI, "Existing smb sess not found\n");
  2549. ses = sesInfoAlloc();
  2550. if (ses == NULL)
  2551. goto get_ses_fail;
  2552. /* new SMB session uses our server ref */
  2553. ses->server = server;
  2554. if (server->dstaddr.ss_family == AF_INET6)
  2555. sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
  2556. else
  2557. sprintf(ses->serverName, "%pI4", &addr->sin_addr);
  2558. if (volume_info->username) {
  2559. ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
  2560. if (!ses->user_name)
  2561. goto get_ses_fail;
  2562. }
  2563. /* volume_info->password freed at unmount */
  2564. if (volume_info->password) {
  2565. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  2566. if (!ses->password)
  2567. goto get_ses_fail;
  2568. }
  2569. if (volume_info->domainname) {
  2570. ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
  2571. if (!ses->domainName)
  2572. goto get_ses_fail;
  2573. }
  2574. if (volume_info->domainauto)
  2575. ses->domainAuto = volume_info->domainauto;
  2576. ses->cred_uid = volume_info->cred_uid;
  2577. ses->linux_uid = volume_info->linux_uid;
  2578. ses->sectype = volume_info->sectype;
  2579. ses->sign = volume_info->sign;
  2580. mutex_lock(&ses->session_mutex);
  2581. rc = cifs_negotiate_protocol(xid, ses);
  2582. if (!rc)
  2583. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  2584. mutex_unlock(&ses->session_mutex);
  2585. if (rc)
  2586. goto get_ses_fail;
  2587. /* success, put it on the list */
  2588. spin_lock(&cifs_tcp_ses_lock);
  2589. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  2590. spin_unlock(&cifs_tcp_ses_lock);
  2591. free_xid(xid);
  2592. cifs_setup_ipc(ses, volume_info);
  2593. return ses;
  2594. get_ses_fail:
  2595. sesInfoFree(ses);
  2596. free_xid(xid);
  2597. return ERR_PTR(rc);
  2598. }
  2599. static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  2600. {
  2601. if (tcon->tidStatus == CifsExiting)
  2602. return 0;
  2603. if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
  2604. return 0;
  2605. if (tcon->seal != volume_info->seal)
  2606. return 0;
  2607. if (tcon->snapshot_time != volume_info->snapshot_time)
  2608. return 0;
  2609. return 1;
  2610. }
  2611. static struct cifs_tcon *
  2612. cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  2613. {
  2614. struct list_head *tmp;
  2615. struct cifs_tcon *tcon;
  2616. spin_lock(&cifs_tcp_ses_lock);
  2617. list_for_each(tmp, &ses->tcon_list) {
  2618. tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
  2619. if (!match_tcon(tcon, volume_info))
  2620. continue;
  2621. ++tcon->tc_count;
  2622. spin_unlock(&cifs_tcp_ses_lock);
  2623. return tcon;
  2624. }
  2625. spin_unlock(&cifs_tcp_ses_lock);
  2626. return NULL;
  2627. }
  2628. void
  2629. cifs_put_tcon(struct cifs_tcon *tcon)
  2630. {
  2631. unsigned int xid;
  2632. struct cifs_ses *ses;
  2633. /*
  2634. * IPC tcon share the lifetime of their session and are
  2635. * destroyed in the session put function
  2636. */
  2637. if (tcon == NULL || tcon->ipc)
  2638. return;
  2639. ses = tcon->ses;
  2640. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  2641. spin_lock(&cifs_tcp_ses_lock);
  2642. if (--tcon->tc_count > 0) {
  2643. spin_unlock(&cifs_tcp_ses_lock);
  2644. return;
  2645. }
  2646. list_del_init(&tcon->tcon_list);
  2647. spin_unlock(&cifs_tcp_ses_lock);
  2648. xid = get_xid();
  2649. if (ses->server->ops->tree_disconnect)
  2650. ses->server->ops->tree_disconnect(xid, tcon);
  2651. _free_xid(xid);
  2652. cifs_fscache_release_super_cookie(tcon);
  2653. tconInfoFree(tcon);
  2654. cifs_put_smb_ses(ses);
  2655. }
  2656. /**
  2657. * cifs_get_tcon - get a tcon matching @volume_info data from @ses
  2658. *
  2659. * - tcon refcount is the number of mount points using the tcon.
  2660. * - ses refcount is the number of tcon using the session.
  2661. *
  2662. * 1. This function assumes it is being called from cifs_mount() where
  2663. * we already got a session reference (ses refcount +1).
  2664. *
  2665. * 2. Since we're in the context of adding a mount point, the end
  2666. * result should be either:
  2667. *
  2668. * a) a new tcon already allocated with refcount=1 (1 mount point) and
  2669. * its session refcount incremented (1 new tcon). This +1 was
  2670. * already done in (1).
  2671. *
  2672. * b) an existing tcon with refcount+1 (add a mount point to it) and
  2673. * identical ses refcount (no new tcon). Because of (1) we need to
  2674. * decrement the ses refcount.
  2675. */
  2676. static struct cifs_tcon *
  2677. cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  2678. {
  2679. int rc, xid;
  2680. struct cifs_tcon *tcon;
  2681. tcon = cifs_find_tcon(ses, volume_info);
  2682. if (tcon) {
  2683. /*
  2684. * tcon has refcount already incremented but we need to
  2685. * decrement extra ses reference gotten by caller (case b)
  2686. */
  2687. cifs_dbg(FYI, "Found match on UNC path\n");
  2688. cifs_put_smb_ses(ses);
  2689. return tcon;
  2690. }
  2691. if (!ses->server->ops->tree_connect) {
  2692. rc = -ENOSYS;
  2693. goto out_fail;
  2694. }
  2695. tcon = tconInfoAlloc();
  2696. if (tcon == NULL) {
  2697. rc = -ENOMEM;
  2698. goto out_fail;
  2699. }
  2700. if (volume_info->snapshot_time) {
  2701. if (ses->server->vals->protocol_id == 0) {
  2702. cifs_dbg(VFS,
  2703. "Use SMB2 or later for snapshot mount option\n");
  2704. rc = -EOPNOTSUPP;
  2705. goto out_fail;
  2706. } else
  2707. tcon->snapshot_time = volume_info->snapshot_time;
  2708. }
  2709. tcon->ses = ses;
  2710. if (volume_info->password) {
  2711. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  2712. if (!tcon->password) {
  2713. rc = -ENOMEM;
  2714. goto out_fail;
  2715. }
  2716. }
  2717. if (volume_info->seal) {
  2718. if (ses->server->vals->protocol_id == 0) {
  2719. cifs_dbg(VFS,
  2720. "SMB3 or later required for encryption\n");
  2721. rc = -EOPNOTSUPP;
  2722. goto out_fail;
  2723. } else if (tcon->ses->server->capabilities &
  2724. SMB2_GLOBAL_CAP_ENCRYPTION)
  2725. tcon->seal = true;
  2726. else {
  2727. cifs_dbg(VFS, "Encryption is not supported on share\n");
  2728. rc = -EOPNOTSUPP;
  2729. goto out_fail;
  2730. }
  2731. }
  2732. if (volume_info->linux_ext) {
  2733. if (ses->server->posix_ext_supported) {
  2734. tcon->posix_extensions = true;
  2735. printk_once(KERN_WARNING
  2736. "SMB3.11 POSIX Extensions are experimental\n");
  2737. } else {
  2738. cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
  2739. rc = -EOPNOTSUPP;
  2740. goto out_fail;
  2741. }
  2742. }
  2743. /*
  2744. * BB Do we need to wrap session_mutex around this TCon call and Unix
  2745. * SetFS as we do on SessSetup and reconnect?
  2746. */
  2747. xid = get_xid();
  2748. rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
  2749. volume_info->local_nls);
  2750. free_xid(xid);
  2751. cifs_dbg(FYI, "Tcon rc = %d\n", rc);
  2752. if (rc)
  2753. goto out_fail;
  2754. if (volume_info->nodfs) {
  2755. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  2756. cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
  2757. }
  2758. tcon->use_persistent = false;
  2759. /* check if SMB2 or later, CIFS does not support persistent handles */
  2760. if (volume_info->persistent) {
  2761. if (ses->server->vals->protocol_id == 0) {
  2762. cifs_dbg(VFS,
  2763. "SMB3 or later required for persistent handles\n");
  2764. rc = -EOPNOTSUPP;
  2765. goto out_fail;
  2766. } else if (ses->server->capabilities &
  2767. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2768. tcon->use_persistent = true;
  2769. else /* persistent handles requested but not supported */ {
  2770. cifs_dbg(VFS,
  2771. "Persistent handles not supported on share\n");
  2772. rc = -EOPNOTSUPP;
  2773. goto out_fail;
  2774. }
  2775. } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  2776. && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2777. && (volume_info->nopersistent == false)) {
  2778. cifs_dbg(FYI, "enabling persistent handles\n");
  2779. tcon->use_persistent = true;
  2780. } else if (volume_info->resilient) {
  2781. if (ses->server->vals->protocol_id == 0) {
  2782. cifs_dbg(VFS,
  2783. "SMB2.1 or later required for resilient handles\n");
  2784. rc = -EOPNOTSUPP;
  2785. goto out_fail;
  2786. }
  2787. tcon->use_resilient = true;
  2788. }
  2789. /*
  2790. * We can have only one retry value for a connection to a share so for
  2791. * resources mounted more than once to the same server share the last
  2792. * value passed in for the retry flag is used.
  2793. */
  2794. tcon->retry = volume_info->retry;
  2795. tcon->nocase = volume_info->nocase;
  2796. tcon->nohandlecache = volume_info->nohandlecache;
  2797. tcon->local_lease = volume_info->local_lease;
  2798. INIT_LIST_HEAD(&tcon->pending_opens);
  2799. spin_lock(&cifs_tcp_ses_lock);
  2800. list_add(&tcon->tcon_list, &ses->tcon_list);
  2801. spin_unlock(&cifs_tcp_ses_lock);
  2802. cifs_fscache_get_super_cookie(tcon);
  2803. return tcon;
  2804. out_fail:
  2805. tconInfoFree(tcon);
  2806. return ERR_PTR(rc);
  2807. }
  2808. void
  2809. cifs_put_tlink(struct tcon_link *tlink)
  2810. {
  2811. if (!tlink || IS_ERR(tlink))
  2812. return;
  2813. if (!atomic_dec_and_test(&tlink->tl_count) ||
  2814. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  2815. tlink->tl_time = jiffies;
  2816. return;
  2817. }
  2818. if (!IS_ERR(tlink_tcon(tlink)))
  2819. cifs_put_tcon(tlink_tcon(tlink));
  2820. kfree(tlink);
  2821. return;
  2822. }
  2823. static int
  2824. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  2825. {
  2826. struct cifs_sb_info *old = CIFS_SB(sb);
  2827. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2828. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  2829. return 0;
  2830. if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
  2831. (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
  2832. return 0;
  2833. /*
  2834. * We want to share sb only if we don't specify an r/wsize or
  2835. * specified r/wsize is greater than or equal to existing one.
  2836. */
  2837. if (new->wsize && new->wsize < old->wsize)
  2838. return 0;
  2839. if (new->rsize && new->rsize < old->rsize)
  2840. return 0;
  2841. if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
  2842. return 0;
  2843. if (old->mnt_file_mode != new->mnt_file_mode ||
  2844. old->mnt_dir_mode != new->mnt_dir_mode)
  2845. return 0;
  2846. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  2847. return 0;
  2848. if (old->actimeo != new->actimeo)
  2849. return 0;
  2850. return 1;
  2851. }
  2852. static int
  2853. match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  2854. {
  2855. struct cifs_sb_info *old = CIFS_SB(sb);
  2856. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2857. bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
  2858. bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
  2859. if (old_set && new_set && !strcmp(new->prepath, old->prepath))
  2860. return 1;
  2861. else if (!old_set && !new_set)
  2862. return 1;
  2863. return 0;
  2864. }
  2865. int
  2866. cifs_match_super(struct super_block *sb, void *data)
  2867. {
  2868. struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
  2869. struct smb_vol *volume_info;
  2870. struct cifs_sb_info *cifs_sb;
  2871. struct TCP_Server_Info *tcp_srv;
  2872. struct cifs_ses *ses;
  2873. struct cifs_tcon *tcon;
  2874. struct tcon_link *tlink;
  2875. int rc = 0;
  2876. spin_lock(&cifs_tcp_ses_lock);
  2877. cifs_sb = CIFS_SB(sb);
  2878. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  2879. if (IS_ERR(tlink)) {
  2880. spin_unlock(&cifs_tcp_ses_lock);
  2881. return rc;
  2882. }
  2883. tcon = tlink_tcon(tlink);
  2884. ses = tcon->ses;
  2885. tcp_srv = ses->server;
  2886. volume_info = mnt_data->vol;
  2887. if (!match_server(tcp_srv, volume_info) ||
  2888. !match_session(ses, volume_info) ||
  2889. !match_tcon(tcon, volume_info) ||
  2890. !match_prepath(sb, mnt_data)) {
  2891. rc = 0;
  2892. goto out;
  2893. }
  2894. rc = compare_mount_options(sb, mnt_data);
  2895. out:
  2896. spin_unlock(&cifs_tcp_ses_lock);
  2897. cifs_put_tlink(tlink);
  2898. return rc;
  2899. }
  2900. int
  2901. get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
  2902. const struct nls_table *nls_codepage, unsigned int *num_referrals,
  2903. struct dfs_info3_param **referrals, int remap)
  2904. {
  2905. int rc = 0;
  2906. if (!ses->server->ops->get_dfs_refer)
  2907. return -ENOSYS;
  2908. *num_referrals = 0;
  2909. *referrals = NULL;
  2910. rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
  2911. referrals, num_referrals,
  2912. nls_codepage, remap);
  2913. return rc;
  2914. }
  2915. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2916. static struct lock_class_key cifs_key[2];
  2917. static struct lock_class_key cifs_slock_key[2];
  2918. static inline void
  2919. cifs_reclassify_socket4(struct socket *sock)
  2920. {
  2921. struct sock *sk = sock->sk;
  2922. BUG_ON(!sock_allow_reclassification(sk));
  2923. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  2924. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  2925. }
  2926. static inline void
  2927. cifs_reclassify_socket6(struct socket *sock)
  2928. {
  2929. struct sock *sk = sock->sk;
  2930. BUG_ON(!sock_allow_reclassification(sk));
  2931. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  2932. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  2933. }
  2934. #else
  2935. static inline void
  2936. cifs_reclassify_socket4(struct socket *sock)
  2937. {
  2938. }
  2939. static inline void
  2940. cifs_reclassify_socket6(struct socket *sock)
  2941. {
  2942. }
  2943. #endif
  2944. /* See RFC1001 section 14 on representation of Netbios names */
  2945. static void rfc1002mangle(char *target, char *source, unsigned int length)
  2946. {
  2947. unsigned int i, j;
  2948. for (i = 0, j = 0; i < (length); i++) {
  2949. /* mask a nibble at a time and encode */
  2950. target[j] = 'A' + (0x0F & (source[i] >> 4));
  2951. target[j+1] = 'A' + (0x0F & source[i]);
  2952. j += 2;
  2953. }
  2954. }
  2955. static int
  2956. bind_socket(struct TCP_Server_Info *server)
  2957. {
  2958. int rc = 0;
  2959. if (server->srcaddr.ss_family != AF_UNSPEC) {
  2960. /* Bind to the specified local IP address */
  2961. struct socket *socket = server->ssocket;
  2962. rc = socket->ops->bind(socket,
  2963. (struct sockaddr *) &server->srcaddr,
  2964. sizeof(server->srcaddr));
  2965. if (rc < 0) {
  2966. struct sockaddr_in *saddr4;
  2967. struct sockaddr_in6 *saddr6;
  2968. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  2969. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  2970. if (saddr6->sin6_family == AF_INET6)
  2971. cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
  2972. &saddr6->sin6_addr, rc);
  2973. else
  2974. cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
  2975. &saddr4->sin_addr.s_addr, rc);
  2976. }
  2977. }
  2978. return rc;
  2979. }
  2980. static int
  2981. ip_rfc1001_connect(struct TCP_Server_Info *server)
  2982. {
  2983. int rc = 0;
  2984. /*
  2985. * some servers require RFC1001 sessinit before sending
  2986. * negprot - BB check reconnection in case where second
  2987. * sessinit is sent but no second negprot
  2988. */
  2989. struct rfc1002_session_packet *ses_init_buf;
  2990. struct smb_hdr *smb_buf;
  2991. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  2992. GFP_KERNEL);
  2993. if (ses_init_buf) {
  2994. ses_init_buf->trailer.session_req.called_len = 32;
  2995. if (server->server_RFC1001_name[0] != 0)
  2996. rfc1002mangle(ses_init_buf->trailer.
  2997. session_req.called_name,
  2998. server->server_RFC1001_name,
  2999. RFC1001_NAME_LEN_WITH_NULL);
  3000. else
  3001. rfc1002mangle(ses_init_buf->trailer.
  3002. session_req.called_name,
  3003. DEFAULT_CIFS_CALLED_NAME,
  3004. RFC1001_NAME_LEN_WITH_NULL);
  3005. ses_init_buf->trailer.session_req.calling_len = 32;
  3006. /*
  3007. * calling name ends in null (byte 16) from old smb
  3008. * convention.
  3009. */
  3010. if (server->workstation_RFC1001_name[0] != 0)
  3011. rfc1002mangle(ses_init_buf->trailer.
  3012. session_req.calling_name,
  3013. server->workstation_RFC1001_name,
  3014. RFC1001_NAME_LEN_WITH_NULL);
  3015. else
  3016. rfc1002mangle(ses_init_buf->trailer.
  3017. session_req.calling_name,
  3018. "LINUX_CIFS_CLNT",
  3019. RFC1001_NAME_LEN_WITH_NULL);
  3020. ses_init_buf->trailer.session_req.scope1 = 0;
  3021. ses_init_buf->trailer.session_req.scope2 = 0;
  3022. smb_buf = (struct smb_hdr *)ses_init_buf;
  3023. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  3024. smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
  3025. rc = smb_send(server, smb_buf, 0x44);
  3026. kfree(ses_init_buf);
  3027. /*
  3028. * RFC1001 layer in at least one server
  3029. * requires very short break before negprot
  3030. * presumably because not expecting negprot
  3031. * to follow so fast. This is a simple
  3032. * solution that works without
  3033. * complicating the code and causes no
  3034. * significant slowing down on mount
  3035. * for everyone else
  3036. */
  3037. usleep_range(1000, 2000);
  3038. }
  3039. /*
  3040. * else the negprot may still work without this
  3041. * even though malloc failed
  3042. */
  3043. return rc;
  3044. }
  3045. static int
  3046. generic_ip_connect(struct TCP_Server_Info *server)
  3047. {
  3048. int rc = 0;
  3049. __be16 sport;
  3050. int slen, sfamily;
  3051. struct socket *socket = server->ssocket;
  3052. struct sockaddr *saddr;
  3053. saddr = (struct sockaddr *) &server->dstaddr;
  3054. if (server->dstaddr.ss_family == AF_INET6) {
  3055. sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
  3056. slen = sizeof(struct sockaddr_in6);
  3057. sfamily = AF_INET6;
  3058. } else {
  3059. sport = ((struct sockaddr_in *) saddr)->sin_port;
  3060. slen = sizeof(struct sockaddr_in);
  3061. sfamily = AF_INET;
  3062. }
  3063. if (socket == NULL) {
  3064. rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
  3065. IPPROTO_TCP, &socket, 1);
  3066. if (rc < 0) {
  3067. cifs_dbg(VFS, "Error %d creating socket\n", rc);
  3068. server->ssocket = NULL;
  3069. return rc;
  3070. }
  3071. /* BB other socket options to set KEEPALIVE, NODELAY? */
  3072. cifs_dbg(FYI, "Socket created\n");
  3073. server->ssocket = socket;
  3074. socket->sk->sk_allocation = GFP_NOFS;
  3075. if (sfamily == AF_INET6)
  3076. cifs_reclassify_socket6(socket);
  3077. else
  3078. cifs_reclassify_socket4(socket);
  3079. }
  3080. rc = bind_socket(server);
  3081. if (rc < 0)
  3082. return rc;
  3083. /*
  3084. * Eventually check for other socket options to change from
  3085. * the default. sock_setsockopt not used because it expects
  3086. * user space buffer
  3087. */
  3088. socket->sk->sk_rcvtimeo = 7 * HZ;
  3089. socket->sk->sk_sndtimeo = 5 * HZ;
  3090. /* make the bufsizes depend on wsize/rsize and max requests */
  3091. if (server->noautotune) {
  3092. if (socket->sk->sk_sndbuf < (200 * 1024))
  3093. socket->sk->sk_sndbuf = 200 * 1024;
  3094. if (socket->sk->sk_rcvbuf < (140 * 1024))
  3095. socket->sk->sk_rcvbuf = 140 * 1024;
  3096. }
  3097. if (server->tcp_nodelay) {
  3098. int val = 1;
  3099. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  3100. (char *)&val, sizeof(val));
  3101. if (rc)
  3102. cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
  3103. rc);
  3104. }
  3105. cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
  3106. socket->sk->sk_sndbuf,
  3107. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  3108. rc = socket->ops->connect(socket, saddr, slen, 0);
  3109. if (rc < 0) {
  3110. cifs_dbg(FYI, "Error %d connecting to server\n", rc);
  3111. sock_release(socket);
  3112. server->ssocket = NULL;
  3113. return rc;
  3114. }
  3115. if (sport == htons(RFC1001_PORT))
  3116. rc = ip_rfc1001_connect(server);
  3117. return rc;
  3118. }
  3119. static int
  3120. ip_connect(struct TCP_Server_Info *server)
  3121. {
  3122. __be16 *sport;
  3123. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  3124. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  3125. if (server->dstaddr.ss_family == AF_INET6)
  3126. sport = &addr6->sin6_port;
  3127. else
  3128. sport = &addr->sin_port;
  3129. if (*sport == 0) {
  3130. int rc;
  3131. /* try with 445 port at first */
  3132. *sport = htons(CIFS_PORT);
  3133. rc = generic_ip_connect(server);
  3134. if (rc >= 0)
  3135. return rc;
  3136. /* if it failed, try with 139 port */
  3137. *sport = htons(RFC1001_PORT);
  3138. }
  3139. return generic_ip_connect(server);
  3140. }
  3141. void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
  3142. struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
  3143. {
  3144. /* if we are reconnecting then should we check to see if
  3145. * any requested capabilities changed locally e.g. via
  3146. * remount but we can not do much about it here
  3147. * if they have (even if we could detect it by the following)
  3148. * Perhaps we could add a backpointer to array of sb from tcon
  3149. * or if we change to make all sb to same share the same
  3150. * sb as NFS - then we only have one backpointer to sb.
  3151. * What if we wanted to mount the server share twice once with
  3152. * and once without posixacls or posix paths? */
  3153. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  3154. if (vol_info && vol_info->no_linux_ext) {
  3155. tcon->fsUnixInfo.Capability = 0;
  3156. tcon->unix_ext = 0; /* Unix Extensions disabled */
  3157. cifs_dbg(FYI, "Linux protocol extensions disabled\n");
  3158. return;
  3159. } else if (vol_info)
  3160. tcon->unix_ext = 1; /* Unix Extensions supported */
  3161. if (tcon->unix_ext == 0) {
  3162. cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
  3163. return;
  3164. }
  3165. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  3166. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  3167. cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
  3168. /* check for reconnect case in which we do not
  3169. want to change the mount behavior if we can avoid it */
  3170. if (vol_info == NULL) {
  3171. /* turn off POSIX ACL and PATHNAMES if not set
  3172. originally at mount time */
  3173. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  3174. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  3175. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  3176. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  3177. cifs_dbg(VFS, "POSIXPATH support change\n");
  3178. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  3179. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  3180. cifs_dbg(VFS, "possible reconnect error\n");
  3181. cifs_dbg(VFS, "server disabled POSIX path support\n");
  3182. }
  3183. }
  3184. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  3185. cifs_dbg(VFS, "per-share encryption not supported yet\n");
  3186. cap &= CIFS_UNIX_CAP_MASK;
  3187. if (vol_info && vol_info->no_psx_acl)
  3188. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  3189. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  3190. cifs_dbg(FYI, "negotiated posix acl support\n");
  3191. if (cifs_sb)
  3192. cifs_sb->mnt_cifs_flags |=
  3193. CIFS_MOUNT_POSIXACL;
  3194. }
  3195. if (vol_info && vol_info->posix_paths == 0)
  3196. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  3197. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  3198. cifs_dbg(FYI, "negotiate posix pathnames\n");
  3199. if (cifs_sb)
  3200. cifs_sb->mnt_cifs_flags |=
  3201. CIFS_MOUNT_POSIX_PATHS;
  3202. }
  3203. cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
  3204. #ifdef CONFIG_CIFS_DEBUG2
  3205. if (cap & CIFS_UNIX_FCNTL_CAP)
  3206. cifs_dbg(FYI, "FCNTL cap\n");
  3207. if (cap & CIFS_UNIX_EXTATTR_CAP)
  3208. cifs_dbg(FYI, "EXTATTR cap\n");
  3209. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  3210. cifs_dbg(FYI, "POSIX path cap\n");
  3211. if (cap & CIFS_UNIX_XATTR_CAP)
  3212. cifs_dbg(FYI, "XATTR cap\n");
  3213. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  3214. cifs_dbg(FYI, "POSIX ACL cap\n");
  3215. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  3216. cifs_dbg(FYI, "very large read cap\n");
  3217. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  3218. cifs_dbg(FYI, "very large write cap\n");
  3219. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  3220. cifs_dbg(FYI, "transport encryption cap\n");
  3221. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  3222. cifs_dbg(FYI, "mandatory transport encryption cap\n");
  3223. #endif /* CIFS_DEBUG2 */
  3224. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  3225. if (vol_info == NULL) {
  3226. cifs_dbg(FYI, "resetting capabilities failed\n");
  3227. } else
  3228. cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
  3229. }
  3230. }
  3231. }
  3232. int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
  3233. struct cifs_sb_info *cifs_sb)
  3234. {
  3235. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  3236. spin_lock_init(&cifs_sb->tlink_tree_lock);
  3237. cifs_sb->tlink_tree = RB_ROOT;
  3238. /*
  3239. * Temporarily set r/wsize for matching superblock. If we end up using
  3240. * new sb then client will later negotiate it downward if needed.
  3241. */
  3242. cifs_sb->rsize = pvolume_info->rsize;
  3243. cifs_sb->wsize = pvolume_info->wsize;
  3244. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  3245. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  3246. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  3247. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  3248. cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
  3249. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  3250. cifs_sb->actimeo = pvolume_info->actimeo;
  3251. cifs_sb->local_nls = pvolume_info->local_nls;
  3252. if (pvolume_info->noperm)
  3253. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  3254. if (pvolume_info->setuids)
  3255. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  3256. if (pvolume_info->setuidfromacl)
  3257. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
  3258. if (pvolume_info->server_ino)
  3259. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  3260. if (pvolume_info->remap)
  3261. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
  3262. if (pvolume_info->sfu_remap)
  3263. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  3264. if (pvolume_info->no_xattr)
  3265. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  3266. if (pvolume_info->sfu_emul)
  3267. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  3268. if (pvolume_info->nobrl)
  3269. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  3270. if (pvolume_info->nohandlecache)
  3271. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
  3272. if (pvolume_info->nostrictsync)
  3273. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  3274. if (pvolume_info->mand_lock)
  3275. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  3276. if (pvolume_info->rwpidforward)
  3277. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
  3278. if (pvolume_info->cifs_acl)
  3279. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  3280. if (pvolume_info->backupuid_specified) {
  3281. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
  3282. cifs_sb->mnt_backupuid = pvolume_info->backupuid;
  3283. }
  3284. if (pvolume_info->backupgid_specified) {
  3285. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
  3286. cifs_sb->mnt_backupgid = pvolume_info->backupgid;
  3287. }
  3288. if (pvolume_info->override_uid)
  3289. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  3290. if (pvolume_info->override_gid)
  3291. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  3292. if (pvolume_info->dynperm)
  3293. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  3294. if (pvolume_info->fsc)
  3295. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
  3296. if (pvolume_info->multiuser)
  3297. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
  3298. CIFS_MOUNT_NO_PERM);
  3299. if (pvolume_info->strict_io)
  3300. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
  3301. if (pvolume_info->direct_io) {
  3302. cifs_dbg(FYI, "mounting share using direct i/o\n");
  3303. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  3304. }
  3305. if (pvolume_info->mfsymlinks) {
  3306. if (pvolume_info->sfu_emul) {
  3307. /*
  3308. * Our SFU ("Services for Unix" emulation does not allow
  3309. * creating symlinks but does allow reading existing SFU
  3310. * symlinks (it does allow both creating and reading SFU
  3311. * style mknod and FIFOs though). When "mfsymlinks" and
  3312. * "sfu" are both enabled at the same time, it allows
  3313. * reading both types of symlinks, but will only create
  3314. * them with mfsymlinks format. This allows better
  3315. * Apple compatibility (probably better for Samba too)
  3316. * while still recognizing old Windows style symlinks.
  3317. */
  3318. cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
  3319. }
  3320. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
  3321. }
  3322. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  3323. cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
  3324. if (pvolume_info->prepath) {
  3325. cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
  3326. if (cifs_sb->prepath == NULL)
  3327. return -ENOMEM;
  3328. }
  3329. return 0;
  3330. }
  3331. static void
  3332. cleanup_volume_info_contents(struct smb_vol *volume_info)
  3333. {
  3334. kfree(volume_info->username);
  3335. kzfree(volume_info->password);
  3336. kfree(volume_info->UNC);
  3337. kfree(volume_info->domainname);
  3338. kfree(volume_info->iocharset);
  3339. kfree(volume_info->prepath);
  3340. }
  3341. void
  3342. cifs_cleanup_volume_info(struct smb_vol *volume_info)
  3343. {
  3344. if (!volume_info)
  3345. return;
  3346. cleanup_volume_info_contents(volume_info);
  3347. kfree(volume_info);
  3348. }
  3349. #ifdef CONFIG_CIFS_DFS_UPCALL
  3350. /*
  3351. * cifs_build_path_to_root returns full path to root when we do not have an
  3352. * exiting connection (tcon)
  3353. */
  3354. static char *
  3355. build_unc_path_to_root(const struct smb_vol *vol,
  3356. const struct cifs_sb_info *cifs_sb)
  3357. {
  3358. char *full_path, *pos;
  3359. unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
  3360. unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
  3361. full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
  3362. if (full_path == NULL)
  3363. return ERR_PTR(-ENOMEM);
  3364. strncpy(full_path, vol->UNC, unc_len);
  3365. pos = full_path + unc_len;
  3366. if (pplen) {
  3367. *pos = CIFS_DIR_SEP(cifs_sb);
  3368. strncpy(pos + 1, vol->prepath, pplen);
  3369. pos += pplen;
  3370. }
  3371. *pos = '\0'; /* add trailing null */
  3372. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  3373. cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
  3374. return full_path;
  3375. }
  3376. /*
  3377. * Perform a dfs referral query for a share and (optionally) prefix
  3378. *
  3379. * If a referral is found, cifs_sb->mountdata will be (re-)allocated
  3380. * to a string containing updated options for the submount. Otherwise it
  3381. * will be left untouched.
  3382. *
  3383. * Returns the rc from get_dfs_path to the caller, which can be used to
  3384. * determine whether there were referrals.
  3385. */
  3386. static int
  3387. expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
  3388. struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
  3389. int check_prefix)
  3390. {
  3391. int rc;
  3392. unsigned int num_referrals = 0;
  3393. struct dfs_info3_param *referrals = NULL;
  3394. char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
  3395. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  3396. if (IS_ERR(full_path))
  3397. return PTR_ERR(full_path);
  3398. /* For DFS paths, skip the first '\' of the UNC */
  3399. ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
  3400. rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
  3401. &num_referrals, &referrals, cifs_remap(cifs_sb));
  3402. if (!rc && num_referrals > 0) {
  3403. char *fake_devname = NULL;
  3404. mdata = cifs_compose_mount_options(cifs_sb->mountdata,
  3405. full_path + 1, referrals,
  3406. &fake_devname);
  3407. free_dfs_info_array(referrals, num_referrals);
  3408. if (IS_ERR(mdata)) {
  3409. rc = PTR_ERR(mdata);
  3410. mdata = NULL;
  3411. } else {
  3412. cleanup_volume_info_contents(volume_info);
  3413. rc = cifs_setup_volume_info(volume_info, mdata,
  3414. fake_devname, false);
  3415. }
  3416. kfree(fake_devname);
  3417. kfree(cifs_sb->mountdata);
  3418. cifs_sb->mountdata = mdata;
  3419. }
  3420. kfree(full_path);
  3421. return rc;
  3422. }
  3423. #endif
  3424. static int
  3425. cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  3426. const char *devname, bool is_smb3)
  3427. {
  3428. int rc = 0;
  3429. if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
  3430. return -EINVAL;
  3431. if (volume_info->nullauth) {
  3432. cifs_dbg(FYI, "Anonymous login\n");
  3433. kfree(volume_info->username);
  3434. volume_info->username = NULL;
  3435. } else if (volume_info->username) {
  3436. /* BB fixme parse for domain name here */
  3437. cifs_dbg(FYI, "Username: %s\n", volume_info->username);
  3438. } else {
  3439. cifs_dbg(VFS, "No username specified\n");
  3440. /* In userspace mount helper we can get user name from alternate
  3441. locations such as env variables and files on disk */
  3442. return -EINVAL;
  3443. }
  3444. /* this is needed for ASCII cp to Unicode converts */
  3445. if (volume_info->iocharset == NULL) {
  3446. /* load_nls_default cannot return null */
  3447. volume_info->local_nls = load_nls_default();
  3448. } else {
  3449. volume_info->local_nls = load_nls(volume_info->iocharset);
  3450. if (volume_info->local_nls == NULL) {
  3451. cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
  3452. volume_info->iocharset);
  3453. return -ELIBACC;
  3454. }
  3455. }
  3456. return rc;
  3457. }
  3458. struct smb_vol *
  3459. cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
  3460. {
  3461. int rc;
  3462. struct smb_vol *volume_info;
  3463. volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
  3464. if (!volume_info)
  3465. return ERR_PTR(-ENOMEM);
  3466. rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
  3467. if (rc) {
  3468. cifs_cleanup_volume_info(volume_info);
  3469. volume_info = ERR_PTR(rc);
  3470. }
  3471. return volume_info;
  3472. }
  3473. static int
  3474. cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
  3475. unsigned int xid,
  3476. struct cifs_tcon *tcon,
  3477. struct cifs_sb_info *cifs_sb,
  3478. char *full_path)
  3479. {
  3480. int rc;
  3481. char *s;
  3482. char sep, tmp;
  3483. sep = CIFS_DIR_SEP(cifs_sb);
  3484. s = full_path;
  3485. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
  3486. while (rc == 0) {
  3487. /* skip separators */
  3488. while (*s == sep)
  3489. s++;
  3490. if (!*s)
  3491. break;
  3492. /* next separator */
  3493. while (*s && *s != sep)
  3494. s++;
  3495. /*
  3496. * temporarily null-terminate the path at the end of
  3497. * the current component
  3498. */
  3499. tmp = *s;
  3500. *s = 0;
  3501. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  3502. full_path);
  3503. *s = tmp;
  3504. }
  3505. return rc;
  3506. }
  3507. int
  3508. cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
  3509. {
  3510. int rc;
  3511. unsigned int xid;
  3512. struct cifs_ses *ses;
  3513. struct cifs_tcon *tcon;
  3514. struct TCP_Server_Info *server;
  3515. char *full_path;
  3516. struct tcon_link *tlink;
  3517. #ifdef CONFIG_CIFS_DFS_UPCALL
  3518. int referral_walks_count = 0;
  3519. #endif
  3520. #ifdef CONFIG_CIFS_DFS_UPCALL
  3521. try_mount_again:
  3522. /* cleanup activities if we're chasing a referral */
  3523. if (referral_walks_count) {
  3524. if (tcon)
  3525. cifs_put_tcon(tcon);
  3526. else if (ses)
  3527. cifs_put_smb_ses(ses);
  3528. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
  3529. free_xid(xid);
  3530. }
  3531. #endif
  3532. rc = 0;
  3533. tcon = NULL;
  3534. ses = NULL;
  3535. server = NULL;
  3536. full_path = NULL;
  3537. tlink = NULL;
  3538. xid = get_xid();
  3539. /* get a reference to a tcp session */
  3540. server = cifs_get_tcp_session(volume_info);
  3541. if (IS_ERR(server)) {
  3542. rc = PTR_ERR(server);
  3543. goto out;
  3544. }
  3545. if ((volume_info->max_credits < 20) ||
  3546. (volume_info->max_credits > 60000))
  3547. server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
  3548. else
  3549. server->max_credits = volume_info->max_credits;
  3550. /* get a reference to a SMB session */
  3551. ses = cifs_get_smb_ses(server, volume_info);
  3552. if (IS_ERR(ses)) {
  3553. rc = PTR_ERR(ses);
  3554. ses = NULL;
  3555. goto mount_fail_check;
  3556. }
  3557. if ((volume_info->persistent == true) && ((ses->server->capabilities &
  3558. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
  3559. cifs_dbg(VFS, "persistent handles not supported by server\n");
  3560. rc = -EOPNOTSUPP;
  3561. goto mount_fail_check;
  3562. }
  3563. /* search for existing tcon to this server share */
  3564. tcon = cifs_get_tcon(ses, volume_info);
  3565. if (IS_ERR(tcon)) {
  3566. rc = PTR_ERR(tcon);
  3567. tcon = NULL;
  3568. if (rc == -EACCES)
  3569. goto mount_fail_check;
  3570. goto remote_path_check;
  3571. }
  3572. /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
  3573. if (tcon->posix_extensions)
  3574. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
  3575. /* tell server which Unix caps we support */
  3576. if (cap_unix(tcon->ses)) {
  3577. /* reset of caps checks mount to see if unix extensions
  3578. disabled for just this mount */
  3579. reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
  3580. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  3581. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  3582. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
  3583. rc = -EACCES;
  3584. goto mount_fail_check;
  3585. }
  3586. } else
  3587. tcon->unix_ext = 0; /* server does not support them */
  3588. /* do not care if a following call succeed - informational */
  3589. if (!tcon->pipe && server->ops->qfs_tcon)
  3590. server->ops->qfs_tcon(xid, tcon);
  3591. cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
  3592. cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
  3593. remote_path_check:
  3594. #ifdef CONFIG_CIFS_DFS_UPCALL
  3595. /*
  3596. * Perform an unconditional check for whether there are DFS
  3597. * referrals for this path without prefix, to provide support
  3598. * for DFS referrals from w2k8 servers which don't seem to respond
  3599. * with PATH_NOT_COVERED to requests that include the prefix.
  3600. * Chase the referral if found, otherwise continue normally.
  3601. */
  3602. if (referral_walks_count == 0) {
  3603. int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
  3604. false);
  3605. if (!refrc) {
  3606. referral_walks_count++;
  3607. goto try_mount_again;
  3608. }
  3609. }
  3610. #endif
  3611. /* check if a whole path is not remote */
  3612. if (!rc && tcon) {
  3613. if (!server->ops->is_path_accessible) {
  3614. rc = -ENOSYS;
  3615. goto mount_fail_check;
  3616. }
  3617. /*
  3618. * cifs_build_path_to_root works only when we have a valid tcon
  3619. */
  3620. full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
  3621. tcon->Flags & SMB_SHARE_IS_IN_DFS);
  3622. if (full_path == NULL) {
  3623. rc = -ENOMEM;
  3624. goto mount_fail_check;
  3625. }
  3626. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  3627. full_path);
  3628. if (rc != 0 && rc != -EREMOTE) {
  3629. kfree(full_path);
  3630. goto mount_fail_check;
  3631. }
  3632. if (rc != -EREMOTE) {
  3633. rc = cifs_are_all_path_components_accessible(server,
  3634. xid, tcon, cifs_sb,
  3635. full_path);
  3636. if (rc != 0) {
  3637. cifs_dbg(VFS, "cannot query dirs between root and final path, "
  3638. "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
  3639. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  3640. rc = 0;
  3641. }
  3642. }
  3643. kfree(full_path);
  3644. }
  3645. /* get referral if needed */
  3646. if (rc == -EREMOTE) {
  3647. #ifdef CONFIG_CIFS_DFS_UPCALL
  3648. if (referral_walks_count > MAX_NESTED_LINKS) {
  3649. /*
  3650. * BB: when we implement proper loop detection,
  3651. * we will remove this check. But now we need it
  3652. * to prevent an indefinite loop if 'DFS tree' is
  3653. * misconfigured (i.e. has loops).
  3654. */
  3655. rc = -ELOOP;
  3656. goto mount_fail_check;
  3657. }
  3658. rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
  3659. if (!rc) {
  3660. referral_walks_count++;
  3661. goto try_mount_again;
  3662. }
  3663. goto mount_fail_check;
  3664. #else /* No DFS support, return error on mount */
  3665. rc = -EOPNOTSUPP;
  3666. #endif
  3667. }
  3668. if (rc)
  3669. goto mount_fail_check;
  3670. /* now, hang the tcon off of the superblock */
  3671. tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
  3672. if (tlink == NULL) {
  3673. rc = -ENOMEM;
  3674. goto mount_fail_check;
  3675. }
  3676. tlink->tl_uid = ses->linux_uid;
  3677. tlink->tl_tcon = tcon;
  3678. tlink->tl_time = jiffies;
  3679. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  3680. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3681. cifs_sb->master_tlink = tlink;
  3682. spin_lock(&cifs_sb->tlink_tree_lock);
  3683. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3684. spin_unlock(&cifs_sb->tlink_tree_lock);
  3685. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  3686. TLINK_IDLE_EXPIRE);
  3687. mount_fail_check:
  3688. /* on error free sesinfo and tcon struct if needed */
  3689. if (rc) {
  3690. /* If find_unc succeeded then rc == 0 so we can not end */
  3691. /* up accidentally freeing someone elses tcon struct */
  3692. if (tcon)
  3693. cifs_put_tcon(tcon);
  3694. else if (ses)
  3695. cifs_put_smb_ses(ses);
  3696. else
  3697. cifs_put_tcp_session(server, 0);
  3698. }
  3699. out:
  3700. free_xid(xid);
  3701. return rc;
  3702. }
  3703. /*
  3704. * Issue a TREE_CONNECT request.
  3705. */
  3706. int
  3707. CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
  3708. const char *tree, struct cifs_tcon *tcon,
  3709. const struct nls_table *nls_codepage)
  3710. {
  3711. struct smb_hdr *smb_buffer;
  3712. struct smb_hdr *smb_buffer_response;
  3713. TCONX_REQ *pSMB;
  3714. TCONX_RSP *pSMBr;
  3715. unsigned char *bcc_ptr;
  3716. int rc = 0;
  3717. int length;
  3718. __u16 bytes_left, count;
  3719. if (ses == NULL)
  3720. return -EIO;
  3721. smb_buffer = cifs_buf_get();
  3722. if (smb_buffer == NULL)
  3723. return -ENOMEM;
  3724. smb_buffer_response = smb_buffer;
  3725. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  3726. NULL /*no tid */ , 4 /*wct */ );
  3727. smb_buffer->Mid = get_next_mid(ses->server);
  3728. smb_buffer->Uid = ses->Suid;
  3729. pSMB = (TCONX_REQ *) smb_buffer;
  3730. pSMBr = (TCONX_RSP *) smb_buffer_response;
  3731. pSMB->AndXCommand = 0xFF;
  3732. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  3733. bcc_ptr = &pSMB->Password[0];
  3734. if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
  3735. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  3736. *bcc_ptr = 0; /* password is null byte */
  3737. bcc_ptr++; /* skip password */
  3738. /* already aligned so no need to do it below */
  3739. } else {
  3740. pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  3741. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  3742. specified as required (when that support is added to
  3743. the vfs in the future) as only NTLM or the much
  3744. weaker LANMAN (which we do not send by default) is accepted
  3745. by Samba (not sure whether other servers allow
  3746. NTLMv2 password here) */
  3747. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  3748. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  3749. (ses->sectype == LANMAN))
  3750. calc_lanman_hash(tcon->password, ses->server->cryptkey,
  3751. ses->server->sec_mode &
  3752. SECMODE_PW_ENCRYPT ? true : false,
  3753. bcc_ptr);
  3754. else
  3755. #endif /* CIFS_WEAK_PW_HASH */
  3756. rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
  3757. bcc_ptr, nls_codepage);
  3758. if (rc) {
  3759. cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
  3760. __func__, rc);
  3761. cifs_buf_release(smb_buffer);
  3762. return rc;
  3763. }
  3764. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  3765. if (ses->capabilities & CAP_UNICODE) {
  3766. /* must align unicode strings */
  3767. *bcc_ptr = 0; /* null byte password */
  3768. bcc_ptr++;
  3769. }
  3770. }
  3771. if (ses->server->sign)
  3772. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  3773. if (ses->capabilities & CAP_STATUS32) {
  3774. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  3775. }
  3776. if (ses->capabilities & CAP_DFS) {
  3777. smb_buffer->Flags2 |= SMBFLG2_DFS;
  3778. }
  3779. if (ses->capabilities & CAP_UNICODE) {
  3780. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  3781. length =
  3782. cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
  3783. 6 /* max utf8 char length in bytes */ *
  3784. (/* server len*/ + 256 /* share len */), nls_codepage);
  3785. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  3786. bcc_ptr += 2; /* skip trailing null */
  3787. } else { /* ASCII */
  3788. strcpy(bcc_ptr, tree);
  3789. bcc_ptr += strlen(tree) + 1;
  3790. }
  3791. strcpy(bcc_ptr, "?????");
  3792. bcc_ptr += strlen("?????");
  3793. bcc_ptr += 1;
  3794. count = bcc_ptr - &pSMB->Password[0];
  3795. pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
  3796. pSMB->hdr.smb_buf_length) + count);
  3797. pSMB->ByteCount = cpu_to_le16(count);
  3798. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  3799. 0);
  3800. /* above now done in SendReceive */
  3801. if (rc == 0) {
  3802. bool is_unicode;
  3803. tcon->tidStatus = CifsGood;
  3804. tcon->need_reconnect = false;
  3805. tcon->tid = smb_buffer_response->Tid;
  3806. bcc_ptr = pByteArea(smb_buffer_response);
  3807. bytes_left = get_bcc(smb_buffer_response);
  3808. length = strnlen(bcc_ptr, bytes_left - 2);
  3809. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  3810. is_unicode = true;
  3811. else
  3812. is_unicode = false;
  3813. /* skip service field (NB: this field is always ASCII) */
  3814. if (length == 3) {
  3815. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  3816. (bcc_ptr[2] == 'C')) {
  3817. cifs_dbg(FYI, "IPC connection\n");
  3818. tcon->ipc = true;
  3819. tcon->pipe = true;
  3820. }
  3821. } else if (length == 2) {
  3822. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  3823. /* the most common case */
  3824. cifs_dbg(FYI, "disk share connection\n");
  3825. }
  3826. }
  3827. bcc_ptr += length + 1;
  3828. bytes_left -= (length + 1);
  3829. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  3830. /* mostly informational -- no need to fail on error here */
  3831. kfree(tcon->nativeFileSystem);
  3832. tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
  3833. bytes_left, is_unicode,
  3834. nls_codepage);
  3835. cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
  3836. if ((smb_buffer_response->WordCount == 3) ||
  3837. (smb_buffer_response->WordCount == 7))
  3838. /* field is in same location */
  3839. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  3840. else
  3841. tcon->Flags = 0;
  3842. cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
  3843. }
  3844. cifs_buf_release(smb_buffer);
  3845. return rc;
  3846. }
  3847. static void delayed_free(struct rcu_head *p)
  3848. {
  3849. struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
  3850. unload_nls(sbi->local_nls);
  3851. kfree(sbi);
  3852. }
  3853. void
  3854. cifs_umount(struct cifs_sb_info *cifs_sb)
  3855. {
  3856. struct rb_root *root = &cifs_sb->tlink_tree;
  3857. struct rb_node *node;
  3858. struct tcon_link *tlink;
  3859. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  3860. spin_lock(&cifs_sb->tlink_tree_lock);
  3861. while ((node = rb_first(root))) {
  3862. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3863. cifs_get_tlink(tlink);
  3864. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3865. rb_erase(node, root);
  3866. spin_unlock(&cifs_sb->tlink_tree_lock);
  3867. cifs_put_tlink(tlink);
  3868. spin_lock(&cifs_sb->tlink_tree_lock);
  3869. }
  3870. spin_unlock(&cifs_sb->tlink_tree_lock);
  3871. kfree(cifs_sb->mountdata);
  3872. kfree(cifs_sb->prepath);
  3873. call_rcu(&cifs_sb->rcu, delayed_free);
  3874. }
  3875. int
  3876. cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
  3877. {
  3878. int rc = 0;
  3879. struct TCP_Server_Info *server = ses->server;
  3880. if (!server->ops->need_neg || !server->ops->negotiate)
  3881. return -ENOSYS;
  3882. /* only send once per connect */
  3883. if (!server->ops->need_neg(server))
  3884. return 0;
  3885. set_credits(server, 1);
  3886. rc = server->ops->negotiate(xid, ses);
  3887. if (rc == 0) {
  3888. spin_lock(&GlobalMid_Lock);
  3889. if (server->tcpStatus == CifsNeedNegotiate)
  3890. server->tcpStatus = CifsGood;
  3891. else
  3892. rc = -EHOSTDOWN;
  3893. spin_unlock(&GlobalMid_Lock);
  3894. }
  3895. return rc;
  3896. }
  3897. int
  3898. cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
  3899. struct nls_table *nls_info)
  3900. {
  3901. int rc = -ENOSYS;
  3902. struct TCP_Server_Info *server = ses->server;
  3903. ses->capabilities = server->capabilities;
  3904. if (linuxExtEnabled == 0)
  3905. ses->capabilities &= (~server->vals->cap_unix);
  3906. cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
  3907. server->sec_mode, server->capabilities, server->timeAdj);
  3908. if (ses->auth_key.response) {
  3909. cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
  3910. ses->auth_key.response);
  3911. kfree(ses->auth_key.response);
  3912. ses->auth_key.response = NULL;
  3913. ses->auth_key.len = 0;
  3914. }
  3915. if (server->ops->sess_setup)
  3916. rc = server->ops->sess_setup(xid, ses, nls_info);
  3917. if (rc)
  3918. cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
  3919. return rc;
  3920. }
  3921. static int
  3922. cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
  3923. {
  3924. vol->sectype = ses->sectype;
  3925. /* krb5 is special, since we don't need username or pw */
  3926. if (vol->sectype == Kerberos)
  3927. return 0;
  3928. return cifs_set_cifscreds(vol, ses);
  3929. }
  3930. static struct cifs_tcon *
  3931. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
  3932. {
  3933. int rc;
  3934. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  3935. struct cifs_ses *ses;
  3936. struct cifs_tcon *tcon = NULL;
  3937. struct smb_vol *vol_info;
  3938. vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
  3939. if (vol_info == NULL)
  3940. return ERR_PTR(-ENOMEM);
  3941. vol_info->local_nls = cifs_sb->local_nls;
  3942. vol_info->linux_uid = fsuid;
  3943. vol_info->cred_uid = fsuid;
  3944. vol_info->UNC = master_tcon->treeName;
  3945. vol_info->retry = master_tcon->retry;
  3946. vol_info->nocase = master_tcon->nocase;
  3947. vol_info->nohandlecache = master_tcon->nohandlecache;
  3948. vol_info->local_lease = master_tcon->local_lease;
  3949. vol_info->no_linux_ext = !master_tcon->unix_ext;
  3950. vol_info->sectype = master_tcon->ses->sectype;
  3951. vol_info->sign = master_tcon->ses->sign;
  3952. rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
  3953. if (rc) {
  3954. tcon = ERR_PTR(rc);
  3955. goto out;
  3956. }
  3957. /* get a reference for the same TCP session */
  3958. spin_lock(&cifs_tcp_ses_lock);
  3959. ++master_tcon->ses->server->srv_count;
  3960. spin_unlock(&cifs_tcp_ses_lock);
  3961. ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
  3962. if (IS_ERR(ses)) {
  3963. tcon = (struct cifs_tcon *)ses;
  3964. cifs_put_tcp_session(master_tcon->ses->server, 0);
  3965. goto out;
  3966. }
  3967. tcon = cifs_get_tcon(ses, vol_info);
  3968. if (IS_ERR(tcon)) {
  3969. cifs_put_smb_ses(ses);
  3970. goto out;
  3971. }
  3972. /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
  3973. if (tcon->posix_extensions)
  3974. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
  3975. if (cap_unix(ses))
  3976. reset_cifs_unix_caps(0, tcon, NULL, vol_info);
  3977. out:
  3978. kfree(vol_info->username);
  3979. kzfree(vol_info->password);
  3980. kfree(vol_info);
  3981. return tcon;
  3982. }
  3983. struct cifs_tcon *
  3984. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  3985. {
  3986. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  3987. }
  3988. /* find and return a tlink with given uid */
  3989. static struct tcon_link *
  3990. tlink_rb_search(struct rb_root *root, kuid_t uid)
  3991. {
  3992. struct rb_node *node = root->rb_node;
  3993. struct tcon_link *tlink;
  3994. while (node) {
  3995. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3996. if (uid_gt(tlink->tl_uid, uid))
  3997. node = node->rb_left;
  3998. else if (uid_lt(tlink->tl_uid, uid))
  3999. node = node->rb_right;
  4000. else
  4001. return tlink;
  4002. }
  4003. return NULL;
  4004. }
  4005. /* insert a tcon_link into the tree */
  4006. static void
  4007. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  4008. {
  4009. struct rb_node **new = &(root->rb_node), *parent = NULL;
  4010. struct tcon_link *tlink;
  4011. while (*new) {
  4012. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  4013. parent = *new;
  4014. if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
  4015. new = &((*new)->rb_left);
  4016. else
  4017. new = &((*new)->rb_right);
  4018. }
  4019. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  4020. rb_insert_color(&new_tlink->tl_rbnode, root);
  4021. }
  4022. /*
  4023. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  4024. * current task.
  4025. *
  4026. * If the superblock doesn't refer to a multiuser mount, then just return
  4027. * the master tcon for the mount.
  4028. *
  4029. * First, search the rbtree for an existing tcon for this fsuid. If one
  4030. * exists, then check to see if it's pending construction. If it is then wait
  4031. * for construction to complete. Once it's no longer pending, check to see if
  4032. * it failed and either return an error or retry construction, depending on
  4033. * the timeout.
  4034. *
  4035. * If one doesn't exist then insert a new tcon_link struct into the tree and
  4036. * try to construct a new one.
  4037. */
  4038. struct tcon_link *
  4039. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  4040. {
  4041. int ret;
  4042. kuid_t fsuid = current_fsuid();
  4043. struct tcon_link *tlink, *newtlink;
  4044. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  4045. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  4046. spin_lock(&cifs_sb->tlink_tree_lock);
  4047. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  4048. if (tlink)
  4049. cifs_get_tlink(tlink);
  4050. spin_unlock(&cifs_sb->tlink_tree_lock);
  4051. if (tlink == NULL) {
  4052. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  4053. if (newtlink == NULL)
  4054. return ERR_PTR(-ENOMEM);
  4055. newtlink->tl_uid = fsuid;
  4056. newtlink->tl_tcon = ERR_PTR(-EACCES);
  4057. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  4058. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  4059. cifs_get_tlink(newtlink);
  4060. spin_lock(&cifs_sb->tlink_tree_lock);
  4061. /* was one inserted after previous search? */
  4062. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  4063. if (tlink) {
  4064. cifs_get_tlink(tlink);
  4065. spin_unlock(&cifs_sb->tlink_tree_lock);
  4066. kfree(newtlink);
  4067. goto wait_for_construction;
  4068. }
  4069. tlink = newtlink;
  4070. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  4071. spin_unlock(&cifs_sb->tlink_tree_lock);
  4072. } else {
  4073. wait_for_construction:
  4074. ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  4075. TASK_INTERRUPTIBLE);
  4076. if (ret) {
  4077. cifs_put_tlink(tlink);
  4078. return ERR_PTR(-ERESTARTSYS);
  4079. }
  4080. /* if it's good, return it */
  4081. if (!IS_ERR(tlink->tl_tcon))
  4082. return tlink;
  4083. /* return error if we tried this already recently */
  4084. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  4085. cifs_put_tlink(tlink);
  4086. return ERR_PTR(-EACCES);
  4087. }
  4088. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  4089. goto wait_for_construction;
  4090. }
  4091. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  4092. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  4093. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  4094. if (IS_ERR(tlink->tl_tcon)) {
  4095. cifs_put_tlink(tlink);
  4096. return ERR_PTR(-EACCES);
  4097. }
  4098. return tlink;
  4099. }
  4100. /*
  4101. * periodic workqueue job that scans tcon_tree for a superblock and closes
  4102. * out tcons.
  4103. */
  4104. static void
  4105. cifs_prune_tlinks(struct work_struct *work)
  4106. {
  4107. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  4108. prune_tlinks.work);
  4109. struct rb_root *root = &cifs_sb->tlink_tree;
  4110. struct rb_node *node;
  4111. struct rb_node *tmp;
  4112. struct tcon_link *tlink;
  4113. /*
  4114. * Because we drop the spinlock in the loop in order to put the tlink
  4115. * it's not guarded against removal of links from the tree. The only
  4116. * places that remove entries from the tree are this function and
  4117. * umounts. Because this function is non-reentrant and is canceled
  4118. * before umount can proceed, this is safe.
  4119. */
  4120. spin_lock(&cifs_sb->tlink_tree_lock);
  4121. node = rb_first(root);
  4122. while (node != NULL) {
  4123. tmp = node;
  4124. node = rb_next(tmp);
  4125. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  4126. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  4127. atomic_read(&tlink->tl_count) != 0 ||
  4128. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  4129. continue;
  4130. cifs_get_tlink(tlink);
  4131. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  4132. rb_erase(tmp, root);
  4133. spin_unlock(&cifs_sb->tlink_tree_lock);
  4134. cifs_put_tlink(tlink);
  4135. spin_lock(&cifs_sb->tlink_tree_lock);
  4136. }
  4137. spin_unlock(&cifs_sb->tlink_tree_lock);
  4138. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  4139. TLINK_IDLE_EXPIRE);
  4140. }