connect.c 112 KB

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