connect.c 111 KB

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