connect.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2011
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/net.h>
  10. #include <linux/string.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/list.h>
  14. #include <linux/wait.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/ctype.h>
  18. #include <linux/utsname.h>
  19. #include <linux/mempool.h>
  20. #include <linux/delay.h>
  21. #include <linux/completion.h>
  22. #include <linux/kthread.h>
  23. #include <linux/pagevec.h>
  24. #include <linux/freezer.h>
  25. #include <linux/namei.h>
  26. #include <linux/uuid.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/processor.h>
  29. #include <linux/inet.h>
  30. #include <linux/module.h>
  31. #include <keys/user-type.h>
  32. #include <net/ipv6.h>
  33. #include <linux/parser.h>
  34. #include <linux/bvec.h>
  35. #include "cifspdu.h"
  36. #include "cifsglob.h"
  37. #include "cifsproto.h"
  38. #include "cifs_unicode.h"
  39. #include "cifs_debug.h"
  40. #include "cifs_fs_sb.h"
  41. #include "ntlmssp.h"
  42. #include "nterr.h"
  43. #include "rfc1002pdu.h"
  44. #include "fscache.h"
  45. #include "smb2proto.h"
  46. #include "smbdirect.h"
  47. #include "dns_resolve.h"
  48. #ifdef CONFIG_CIFS_DFS_UPCALL
  49. #include "dfs.h"
  50. #include "dfs_cache.h"
  51. #endif
  52. #include "fs_context.h"
  53. #include "cifs_swn.h"
  54. /* FIXME: should these be tunable? */
  55. #define TLINK_ERROR_EXPIRE (1 * HZ)
  56. #define TLINK_IDLE_EXPIRE (600 * HZ)
  57. /* Drop the connection to not overload the server */
  58. #define MAX_STATUS_IO_TIMEOUT 5
  59. static int ip_connect(struct TCP_Server_Info *server);
  60. static int generic_ip_connect(struct TCP_Server_Info *server);
  61. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  62. static void cifs_prune_tlinks(struct work_struct *work);
  63. /*
  64. * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
  65. * get their ip addresses changed at some point.
  66. *
  67. * This should be called with server->srv_mutex held.
  68. */
  69. static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
  70. {
  71. int rc;
  72. int len;
  73. char *unc;
  74. struct sockaddr_storage ss;
  75. if (!server->hostname)
  76. return -EINVAL;
  77. /* if server hostname isn't populated, there's nothing to do here */
  78. if (server->hostname[0] == '\0')
  79. return 0;
  80. len = strlen(server->hostname) + 3;
  81. unc = kmalloc(len, GFP_KERNEL);
  82. if (!unc) {
  83. cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
  84. return -ENOMEM;
  85. }
  86. scnprintf(unc, len, "\\\\%s", server->hostname);
  87. spin_lock(&server->srv_lock);
  88. ss = server->dstaddr;
  89. spin_unlock(&server->srv_lock);
  90. rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
  91. kfree(unc);
  92. if (rc < 0) {
  93. cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
  94. __func__, server->hostname, rc);
  95. } else {
  96. spin_lock(&server->srv_lock);
  97. memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
  98. spin_unlock(&server->srv_lock);
  99. rc = 0;
  100. }
  101. return rc;
  102. }
  103. void smb2_query_server_interfaces(struct work_struct *work)
  104. {
  105. int rc;
  106. int xid;
  107. struct cifs_tcon *tcon = container_of(work,
  108. struct cifs_tcon,
  109. query_interfaces.work);
  110. struct TCP_Server_Info *server = tcon->ses->server;
  111. /*
  112. * query server network interfaces, in case they change
  113. */
  114. if (!server->ops->query_server_interfaces)
  115. return;
  116. xid = get_xid();
  117. rc = server->ops->query_server_interfaces(xid, tcon, false);
  118. free_xid(xid);
  119. if (rc)
  120. cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
  121. __func__, rc);
  122. queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
  123. (SMB_INTERFACE_POLL_INTERVAL * HZ));
  124. }
  125. #define set_need_reco(server) \
  126. do { \
  127. spin_lock(&server->srv_lock); \
  128. if (server->tcpStatus != CifsExiting) \
  129. server->tcpStatus = CifsNeedReconnect; \
  130. spin_unlock(&server->srv_lock); \
  131. } while (0)
  132. /*
  133. * Update the tcpStatus for the server.
  134. * This is used to signal the cifsd thread to call cifs_reconnect
  135. * ONLY cifsd thread should call cifs_reconnect. For any other
  136. * thread, use this function
  137. *
  138. * @server: the tcp ses for which reconnect is needed
  139. * @all_channels: if this needs to be done for all channels
  140. */
  141. void
  142. cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
  143. bool all_channels)
  144. {
  145. struct TCP_Server_Info *nserver;
  146. struct cifs_ses *ses;
  147. LIST_HEAD(reco);
  148. int i;
  149. /* if we need to signal just this channel */
  150. if (!all_channels) {
  151. set_need_reco(server);
  152. return;
  153. }
  154. if (SERVER_IS_CHAN(server))
  155. server = server->primary_server;
  156. scoped_guard(spinlock, &cifs_tcp_ses_lock) {
  157. set_need_reco(server);
  158. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  159. spin_lock(&ses->ses_lock);
  160. if (ses->ses_status == SES_EXITING) {
  161. spin_unlock(&ses->ses_lock);
  162. continue;
  163. }
  164. spin_lock(&ses->chan_lock);
  165. for (i = 1; i < ses->chan_count; i++) {
  166. nserver = ses->chans[i].server;
  167. if (!nserver)
  168. continue;
  169. nserver->srv_count++;
  170. list_add(&nserver->rlist, &reco);
  171. }
  172. spin_unlock(&ses->chan_lock);
  173. spin_unlock(&ses->ses_lock);
  174. }
  175. }
  176. list_for_each_entry_safe(server, nserver, &reco, rlist) {
  177. list_del_init(&server->rlist);
  178. set_need_reco(server);
  179. cifs_put_tcp_session(server, 0);
  180. }
  181. }
  182. /*
  183. * Mark all sessions and tcons for reconnect.
  184. * IMPORTANT: make sure that this gets called only from
  185. * cifsd thread. For any other thread, use
  186. * cifs_signal_cifsd_for_reconnect
  187. *
  188. * @server: the tcp ses for which reconnect is needed
  189. * @server needs to be previously set to CifsNeedReconnect.
  190. * @mark_smb_session: whether even sessions need to be marked
  191. */
  192. void
  193. cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
  194. bool mark_smb_session)
  195. {
  196. struct TCP_Server_Info *pserver;
  197. struct cifs_ses *ses, *nses;
  198. struct cifs_tcon *tcon;
  199. /*
  200. * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
  201. * are not used until reconnected.
  202. */
  203. cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
  204. /* If server is a channel, select the primary channel */
  205. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  206. /*
  207. * if the server has been marked for termination, there is a
  208. * chance that the remaining channels all need reconnect. To be
  209. * on the safer side, mark the session and trees for reconnect
  210. * for this scenario. This might cause a few redundant session
  211. * setup and tree connect requests, but it is better than not doing
  212. * a tree connect when needed, and all following requests failing
  213. */
  214. if (server->terminate) {
  215. mark_smb_session = true;
  216. server = pserver;
  217. }
  218. spin_lock(&cifs_tcp_ses_lock);
  219. list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
  220. spin_lock(&ses->ses_lock);
  221. if (ses->ses_status == SES_EXITING) {
  222. spin_unlock(&ses->ses_lock);
  223. continue;
  224. }
  225. spin_unlock(&ses->ses_lock);
  226. spin_lock(&ses->chan_lock);
  227. if (cifs_ses_get_chan_index(ses, server) ==
  228. CIFS_INVAL_CHAN_INDEX) {
  229. spin_unlock(&ses->chan_lock);
  230. continue;
  231. }
  232. if (!cifs_chan_is_iface_active(ses, server)) {
  233. spin_unlock(&ses->chan_lock);
  234. cifs_chan_update_iface(ses, server);
  235. spin_lock(&ses->chan_lock);
  236. }
  237. if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
  238. spin_unlock(&ses->chan_lock);
  239. continue;
  240. }
  241. if (mark_smb_session)
  242. CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
  243. else
  244. cifs_chan_set_need_reconnect(ses, server);
  245. cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
  246. __func__, ses->chans_need_reconnect);
  247. /* If all channels need reconnect, then tcon needs reconnect */
  248. if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
  249. spin_unlock(&ses->chan_lock);
  250. continue;
  251. }
  252. spin_unlock(&ses->chan_lock);
  253. spin_lock(&ses->ses_lock);
  254. ses->ses_status = SES_NEED_RECON;
  255. spin_unlock(&ses->ses_lock);
  256. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  257. tcon->need_reconnect = true;
  258. spin_lock(&tcon->tc_lock);
  259. tcon->status = TID_NEED_RECON;
  260. spin_unlock(&tcon->tc_lock);
  261. cancel_delayed_work(&tcon->query_interfaces);
  262. }
  263. if (ses->tcon_ipc) {
  264. ses->tcon_ipc->need_reconnect = true;
  265. spin_lock(&ses->tcon_ipc->tc_lock);
  266. ses->tcon_ipc->status = TID_NEED_RECON;
  267. spin_unlock(&ses->tcon_ipc->tc_lock);
  268. }
  269. }
  270. spin_unlock(&cifs_tcp_ses_lock);
  271. }
  272. static void
  273. cifs_abort_connection(struct TCP_Server_Info *server)
  274. {
  275. struct mid_q_entry *mid, *nmid;
  276. struct list_head retry_list;
  277. server->maxBuf = 0;
  278. server->max_read = 0;
  279. /* do not want to be sending data on a socket we are freeing */
  280. cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
  281. cifs_server_lock(server);
  282. if (server->ssocket) {
  283. cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
  284. server->ssocket->flags);
  285. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  286. cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
  287. server->ssocket->flags);
  288. sock_release(server->ssocket);
  289. server->ssocket = NULL;
  290. }
  291. server->sequence_number = 0;
  292. server->session_estab = false;
  293. kfree_sensitive(server->session_key.response);
  294. server->session_key.response = NULL;
  295. server->session_key.len = 0;
  296. server->lstrp = jiffies;
  297. /* mark submitted MIDs for retry and issue callback */
  298. INIT_LIST_HEAD(&retry_list);
  299. cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
  300. spin_lock(&server->mid_lock);
  301. list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
  302. kref_get(&mid->refcount);
  303. if (mid->mid_state == MID_REQUEST_SUBMITTED)
  304. mid->mid_state = MID_RETRY_NEEDED;
  305. list_move(&mid->qhead, &retry_list);
  306. mid->mid_flags |= MID_DELETED;
  307. }
  308. spin_unlock(&server->mid_lock);
  309. cifs_server_unlock(server);
  310. cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
  311. list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
  312. list_del_init(&mid->qhead);
  313. mid->callback(mid);
  314. release_mid(mid);
  315. }
  316. if (cifs_rdma_enabled(server)) {
  317. cifs_server_lock(server);
  318. smbd_destroy(server);
  319. cifs_server_unlock(server);
  320. }
  321. }
  322. static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
  323. {
  324. spin_lock(&server->srv_lock);
  325. server->nr_targets = num_targets;
  326. if (server->tcpStatus == CifsExiting) {
  327. /* the demux thread will exit normally next time through the loop */
  328. spin_unlock(&server->srv_lock);
  329. wake_up(&server->response_q);
  330. return false;
  331. }
  332. cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
  333. trace_smb3_reconnect(server->CurrentMid, server->conn_id,
  334. server->hostname);
  335. server->tcpStatus = CifsNeedReconnect;
  336. spin_unlock(&server->srv_lock);
  337. return true;
  338. }
  339. /*
  340. * cifs tcp session reconnection
  341. *
  342. * mark tcp session as reconnecting so temporarily locked
  343. * mark all smb sessions as reconnecting for tcp session
  344. * reconnect tcp session
  345. * wake up waiters on reconnection? - (not needed currently)
  346. *
  347. * if mark_smb_session is passed as true, unconditionally mark
  348. * the smb session (and tcon) for reconnect as well. This value
  349. * doesn't really matter for non-multichannel scenario.
  350. *
  351. */
  352. static int __cifs_reconnect(struct TCP_Server_Info *server,
  353. bool mark_smb_session)
  354. {
  355. int rc = 0;
  356. if (!cifs_tcp_ses_needs_reconnect(server, 1))
  357. return 0;
  358. /*
  359. * if smb session has been marked for reconnect, also reconnect all
  360. * connections. This way, the other connections do not end up bad.
  361. */
  362. if (mark_smb_session)
  363. cifs_signal_cifsd_for_reconnect(server, mark_smb_session);
  364. cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
  365. cifs_abort_connection(server);
  366. do {
  367. try_to_freeze();
  368. cifs_server_lock(server);
  369. if (!cifs_swn_set_server_dstaddr(server) &&
  370. !SERVER_IS_CHAN(server)) {
  371. /* resolve the hostname again to make sure that IP address is up-to-date */
  372. rc = reconn_set_ipaddr_from_hostname(server);
  373. cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
  374. }
  375. if (cifs_rdma_enabled(server))
  376. rc = smbd_reconnect(server);
  377. else
  378. rc = generic_ip_connect(server);
  379. if (rc) {
  380. cifs_server_unlock(server);
  381. cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
  382. msleep(3000);
  383. } else {
  384. atomic_inc(&tcpSesReconnectCount);
  385. set_credits(server, 1);
  386. spin_lock(&server->srv_lock);
  387. if (server->tcpStatus != CifsExiting)
  388. server->tcpStatus = CifsNeedNegotiate;
  389. spin_unlock(&server->srv_lock);
  390. cifs_swn_reset_server_dstaddr(server);
  391. cifs_server_unlock(server);
  392. mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
  393. }
  394. } while (server->tcpStatus == CifsNeedReconnect);
  395. spin_lock(&server->srv_lock);
  396. if (server->tcpStatus == CifsNeedNegotiate)
  397. mod_delayed_work(cifsiod_wq, &server->echo, 0);
  398. spin_unlock(&server->srv_lock);
  399. wake_up(&server->response_q);
  400. return rc;
  401. }
  402. #ifdef CONFIG_CIFS_DFS_UPCALL
  403. static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
  404. {
  405. int rc;
  406. char *hostname;
  407. if (!cifs_swn_set_server_dstaddr(server)) {
  408. if (server->hostname != target) {
  409. hostname = extract_hostname(target);
  410. if (!IS_ERR(hostname)) {
  411. spin_lock(&server->srv_lock);
  412. kfree(server->hostname);
  413. server->hostname = hostname;
  414. spin_unlock(&server->srv_lock);
  415. } else {
  416. cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
  417. __func__, PTR_ERR(hostname));
  418. cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
  419. server->hostname);
  420. }
  421. }
  422. /* resolve the hostname again to make sure that IP address is up-to-date. */
  423. rc = reconn_set_ipaddr_from_hostname(server);
  424. cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
  425. }
  426. /* Reconnect the socket */
  427. if (cifs_rdma_enabled(server))
  428. rc = smbd_reconnect(server);
  429. else
  430. rc = generic_ip_connect(server);
  431. return rc;
  432. }
  433. static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
  434. struct dfs_cache_tgt_iterator **target_hint)
  435. {
  436. int rc;
  437. struct dfs_cache_tgt_iterator *tit;
  438. *target_hint = NULL;
  439. /* If dfs target list is empty, then reconnect to last server */
  440. tit = dfs_cache_get_tgt_iterator(tl);
  441. if (!tit)
  442. return __reconnect_target_unlocked(server, server->hostname);
  443. /* Otherwise, try every dfs target in @tl */
  444. for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
  445. rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
  446. if (!rc) {
  447. *target_hint = tit;
  448. break;
  449. }
  450. }
  451. return rc;
  452. }
  453. static int reconnect_dfs_server(struct TCP_Server_Info *server)
  454. {
  455. struct dfs_cache_tgt_iterator *target_hint = NULL;
  456. DFS_CACHE_TGT_LIST(tl);
  457. int num_targets = 0;
  458. int rc = 0;
  459. /*
  460. * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
  461. *
  462. * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
  463. * targets (server->nr_targets). It's also possible that the cached referral was cleared
  464. * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
  465. * refreshing the referral, so, in this case, default it to 1.
  466. */
  467. mutex_lock(&server->refpath_lock);
  468. if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
  469. num_targets = dfs_cache_get_nr_tgts(&tl);
  470. mutex_unlock(&server->refpath_lock);
  471. if (!num_targets)
  472. num_targets = 1;
  473. if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
  474. return 0;
  475. /*
  476. * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
  477. * different server or share during failover. It could be improved by adding some logic to
  478. * only do that in case it connects to a different server or share, though.
  479. */
  480. cifs_mark_tcp_ses_conns_for_reconnect(server, true);
  481. cifs_abort_connection(server);
  482. do {
  483. try_to_freeze();
  484. cifs_server_lock(server);
  485. rc = reconnect_target_unlocked(server, &tl, &target_hint);
  486. if (rc) {
  487. /* Failed to reconnect socket */
  488. cifs_server_unlock(server);
  489. cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
  490. msleep(3000);
  491. continue;
  492. }
  493. /*
  494. * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
  495. * process waiting for reconnect will know it needs to re-establish session and tcon
  496. * through the reconnected target server.
  497. */
  498. atomic_inc(&tcpSesReconnectCount);
  499. set_credits(server, 1);
  500. spin_lock(&server->srv_lock);
  501. if (server->tcpStatus != CifsExiting)
  502. server->tcpStatus = CifsNeedNegotiate;
  503. spin_unlock(&server->srv_lock);
  504. cifs_swn_reset_server_dstaddr(server);
  505. cifs_server_unlock(server);
  506. mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
  507. } while (server->tcpStatus == CifsNeedReconnect);
  508. mutex_lock(&server->refpath_lock);
  509. dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
  510. mutex_unlock(&server->refpath_lock);
  511. dfs_cache_free_tgts(&tl);
  512. /* Need to set up echo worker again once connection has been established */
  513. spin_lock(&server->srv_lock);
  514. if (server->tcpStatus == CifsNeedNegotiate)
  515. mod_delayed_work(cifsiod_wq, &server->echo, 0);
  516. spin_unlock(&server->srv_lock);
  517. wake_up(&server->response_q);
  518. return rc;
  519. }
  520. int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
  521. {
  522. mutex_lock(&server->refpath_lock);
  523. if (!server->leaf_fullpath) {
  524. mutex_unlock(&server->refpath_lock);
  525. return __cifs_reconnect(server, mark_smb_session);
  526. }
  527. mutex_unlock(&server->refpath_lock);
  528. return reconnect_dfs_server(server);
  529. }
  530. #else
  531. int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
  532. {
  533. return __cifs_reconnect(server, mark_smb_session);
  534. }
  535. #endif
  536. static void
  537. cifs_echo_request(struct work_struct *work)
  538. {
  539. int rc;
  540. struct TCP_Server_Info *server = container_of(work,
  541. struct TCP_Server_Info, echo.work);
  542. /*
  543. * We cannot send an echo if it is disabled.
  544. * Also, no need to ping if we got a response recently.
  545. */
  546. if (server->tcpStatus == CifsNeedReconnect ||
  547. server->tcpStatus == CifsExiting ||
  548. server->tcpStatus == CifsNew ||
  549. (server->ops->can_echo && !server->ops->can_echo(server)) ||
  550. time_before(jiffies, server->lstrp + server->echo_interval - HZ))
  551. goto requeue_echo;
  552. rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
  553. cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
  554. /* Check witness registrations */
  555. cifs_swn_check();
  556. requeue_echo:
  557. queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
  558. }
  559. static bool
  560. allocate_buffers(struct TCP_Server_Info *server)
  561. {
  562. if (!server->bigbuf) {
  563. server->bigbuf = (char *)cifs_buf_get();
  564. if (!server->bigbuf) {
  565. cifs_server_dbg(VFS, "No memory for large SMB response\n");
  566. msleep(3000);
  567. /* retry will check if exiting */
  568. return false;
  569. }
  570. } else if (server->large_buf) {
  571. /* we are reusing a dirty large buf, clear its start */
  572. memset(server->bigbuf, 0, HEADER_SIZE(server));
  573. }
  574. if (!server->smallbuf) {
  575. server->smallbuf = (char *)cifs_small_buf_get();
  576. if (!server->smallbuf) {
  577. cifs_server_dbg(VFS, "No memory for SMB response\n");
  578. msleep(1000);
  579. /* retry will check if exiting */
  580. return false;
  581. }
  582. /* beginning of smb buffer is cleared in our buf_get */
  583. } else {
  584. /* if existing small buf clear beginning */
  585. memset(server->smallbuf, 0, HEADER_SIZE(server));
  586. }
  587. return true;
  588. }
  589. static bool
  590. server_unresponsive(struct TCP_Server_Info *server)
  591. {
  592. /*
  593. * If we're in the process of mounting a share or reconnecting a session
  594. * and the server abruptly shut down (e.g. socket wasn't closed, packet
  595. * had been ACK'ed but no SMB response), don't wait longer than 20s from
  596. * when negotiate actually started.
  597. */
  598. spin_lock(&server->srv_lock);
  599. if (server->tcpStatus == CifsInNegotiate &&
  600. time_after(jiffies, server->neg_start + 20 * HZ)) {
  601. spin_unlock(&server->srv_lock);
  602. cifs_reconnect(server, false);
  603. return true;
  604. }
  605. /*
  606. * We need to wait 3 echo intervals to make sure we handle such
  607. * situations right:
  608. * 1s client sends a normal SMB request
  609. * 2s client gets a response
  610. * 30s echo workqueue job pops, and decides we got a response recently
  611. * and don't need to send another
  612. * ...
  613. * 65s kernel_recvmsg times out, and we see that we haven't gotten
  614. * a response in >60s.
  615. */
  616. if ((server->tcpStatus == CifsGood ||
  617. server->tcpStatus == CifsNeedNegotiate) &&
  618. (!server->ops->can_echo || server->ops->can_echo(server)) &&
  619. time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
  620. spin_unlock(&server->srv_lock);
  621. cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
  622. (3 * server->echo_interval) / HZ);
  623. cifs_reconnect(server, false);
  624. return true;
  625. }
  626. spin_unlock(&server->srv_lock);
  627. return false;
  628. }
  629. static inline bool
  630. zero_credits(struct TCP_Server_Info *server)
  631. {
  632. int val;
  633. spin_lock(&server->req_lock);
  634. val = server->credits + server->echo_credits + server->oplock_credits;
  635. if (server->in_flight == 0 && val == 0) {
  636. spin_unlock(&server->req_lock);
  637. return true;
  638. }
  639. spin_unlock(&server->req_lock);
  640. return false;
  641. }
  642. static int
  643. cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
  644. {
  645. int length = 0;
  646. int total_read;
  647. for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
  648. try_to_freeze();
  649. /* reconnect if no credits and no requests in flight */
  650. if (zero_credits(server)) {
  651. cifs_reconnect(server, false);
  652. return -ECONNABORTED;
  653. }
  654. if (server_unresponsive(server))
  655. return -ECONNABORTED;
  656. if (cifs_rdma_enabled(server) && server->smbd_conn)
  657. length = smbd_recv(server->smbd_conn, smb_msg);
  658. else
  659. length = sock_recvmsg(server->ssocket, smb_msg, 0);
  660. spin_lock(&server->srv_lock);
  661. if (server->tcpStatus == CifsExiting) {
  662. spin_unlock(&server->srv_lock);
  663. return -ESHUTDOWN;
  664. }
  665. if (server->tcpStatus == CifsNeedReconnect) {
  666. spin_unlock(&server->srv_lock);
  667. cifs_reconnect(server, false);
  668. return -ECONNABORTED;
  669. }
  670. spin_unlock(&server->srv_lock);
  671. if (length == -ERESTARTSYS ||
  672. length == -EAGAIN ||
  673. length == -EINTR) {
  674. /*
  675. * Minimum sleep to prevent looping, allowing socket
  676. * to clear and app threads to set tcpStatus
  677. * CifsNeedReconnect if server hung.
  678. */
  679. usleep_range(1000, 2000);
  680. length = 0;
  681. continue;
  682. }
  683. if (length <= 0) {
  684. cifs_dbg(FYI, "Received no data or error: %d\n", length);
  685. cifs_reconnect(server, false);
  686. return -ECONNABORTED;
  687. }
  688. }
  689. return total_read;
  690. }
  691. int
  692. cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
  693. unsigned int to_read)
  694. {
  695. struct msghdr smb_msg = {};
  696. struct kvec iov = {.iov_base = buf, .iov_len = to_read};
  697. iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
  698. return cifs_readv_from_socket(server, &smb_msg);
  699. }
  700. ssize_t
  701. cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
  702. {
  703. struct msghdr smb_msg = {};
  704. /*
  705. * iov_iter_discard already sets smb_msg.type and count and iov_offset
  706. * and cifs_readv_from_socket sets msg_control and msg_controllen
  707. * so little to initialize in struct msghdr
  708. */
  709. iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
  710. return cifs_readv_from_socket(server, &smb_msg);
  711. }
  712. int
  713. cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
  714. unsigned int to_read)
  715. {
  716. struct msghdr smb_msg = { .msg_iter = *iter };
  717. iov_iter_truncate(&smb_msg.msg_iter, to_read);
  718. return cifs_readv_from_socket(server, &smb_msg);
  719. }
  720. static bool
  721. is_smb_response(struct TCP_Server_Info *server, unsigned char type)
  722. {
  723. /*
  724. * The first byte big endian of the length field,
  725. * is actually not part of the length but the type
  726. * with the most common, zero, as regular data.
  727. */
  728. switch (type) {
  729. case RFC1002_SESSION_MESSAGE:
  730. /* Regular SMB response */
  731. return true;
  732. case RFC1002_SESSION_KEEP_ALIVE:
  733. cifs_dbg(FYI, "RFC 1002 session keep alive\n");
  734. break;
  735. case RFC1002_POSITIVE_SESSION_RESPONSE:
  736. cifs_dbg(FYI, "RFC 1002 positive session response\n");
  737. break;
  738. case RFC1002_NEGATIVE_SESSION_RESPONSE:
  739. /*
  740. * We get this from Windows 98 instead of an error on
  741. * SMB negprot response.
  742. */
  743. cifs_dbg(FYI, "RFC 1002 negative session response\n");
  744. /* give server a second to clean up */
  745. msleep(1000);
  746. /*
  747. * Always try 445 first on reconnect since we get NACK
  748. * on some if we ever connected to port 139 (the NACK
  749. * is since we do not begin with RFC1001 session
  750. * initialize frame).
  751. */
  752. cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
  753. cifs_reconnect(server, true);
  754. break;
  755. default:
  756. cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
  757. cifs_reconnect(server, true);
  758. }
  759. return false;
  760. }
  761. void
  762. dequeue_mid(struct mid_q_entry *mid, bool malformed)
  763. {
  764. #ifdef CONFIG_CIFS_STATS2
  765. mid->when_received = jiffies;
  766. #endif
  767. spin_lock(&mid->server->mid_lock);
  768. if (!malformed)
  769. mid->mid_state = MID_RESPONSE_RECEIVED;
  770. else
  771. mid->mid_state = MID_RESPONSE_MALFORMED;
  772. /*
  773. * Trying to handle/dequeue a mid after the send_recv()
  774. * function has finished processing it is a bug.
  775. */
  776. if (mid->mid_flags & MID_DELETED) {
  777. spin_unlock(&mid->server->mid_lock);
  778. pr_warn_once("trying to dequeue a deleted mid\n");
  779. } else {
  780. list_del_init(&mid->qhead);
  781. mid->mid_flags |= MID_DELETED;
  782. spin_unlock(&mid->server->mid_lock);
  783. }
  784. }
  785. static unsigned int
  786. smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
  787. {
  788. struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
  789. /*
  790. * SMB1 does not use credits.
  791. */
  792. if (is_smb1(server))
  793. return 0;
  794. return le16_to_cpu(shdr->CreditRequest);
  795. }
  796. static void
  797. handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  798. char *buf, int malformed)
  799. {
  800. if (server->ops->check_trans2 &&
  801. server->ops->check_trans2(mid, server, buf, malformed))
  802. return;
  803. mid->credits_received = smb2_get_credits_from_hdr(buf, server);
  804. mid->resp_buf = buf;
  805. mid->large_buf = server->large_buf;
  806. /* Was previous buf put in mpx struct for multi-rsp? */
  807. if (!mid->multiRsp) {
  808. /* smb buffer will be freed by user thread */
  809. if (server->large_buf)
  810. server->bigbuf = NULL;
  811. else
  812. server->smallbuf = NULL;
  813. }
  814. dequeue_mid(mid, malformed);
  815. }
  816. int
  817. cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
  818. {
  819. bool srv_sign_required = server->sec_mode & server->vals->signing_required;
  820. bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
  821. bool mnt_sign_enabled;
  822. /*
  823. * Is signing required by mnt options? If not then check
  824. * global_secflags to see if it is there.
  825. */
  826. if (!mnt_sign_required)
  827. mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
  828. CIFSSEC_MUST_SIGN);
  829. /*
  830. * If signing is required then it's automatically enabled too,
  831. * otherwise, check to see if the secflags allow it.
  832. */
  833. mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
  834. (global_secflags & CIFSSEC_MAY_SIGN);
  835. /* If server requires signing, does client allow it? */
  836. if (srv_sign_required) {
  837. if (!mnt_sign_enabled) {
  838. cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
  839. return -EOPNOTSUPP;
  840. }
  841. server->sign = true;
  842. }
  843. /* If client requires signing, does server allow it? */
  844. if (mnt_sign_required) {
  845. if (!srv_sign_enabled) {
  846. cifs_dbg(VFS, "Server does not support signing!\n");
  847. return -EOPNOTSUPP;
  848. }
  849. server->sign = true;
  850. }
  851. if (cifs_rdma_enabled(server) && server->sign)
  852. cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
  853. return 0;
  854. }
  855. static noinline_for_stack void
  856. clean_demultiplex_info(struct TCP_Server_Info *server)
  857. {
  858. int length;
  859. /* take it off the list, if it's not already */
  860. spin_lock(&server->srv_lock);
  861. list_del_init(&server->tcp_ses_list);
  862. spin_unlock(&server->srv_lock);
  863. cancel_delayed_work_sync(&server->echo);
  864. spin_lock(&server->srv_lock);
  865. server->tcpStatus = CifsExiting;
  866. spin_unlock(&server->srv_lock);
  867. wake_up_all(&server->response_q);
  868. /* check if we have blocked requests that need to free */
  869. spin_lock(&server->req_lock);
  870. if (server->credits <= 0)
  871. server->credits = 1;
  872. spin_unlock(&server->req_lock);
  873. /*
  874. * Although there should not be any requests blocked on this queue it
  875. * can not hurt to be paranoid and try to wake up requests that may
  876. * haven been blocked when more than 50 at time were on the wire to the
  877. * same server - they now will see the session is in exit state and get
  878. * out of SendReceive.
  879. */
  880. wake_up_all(&server->request_q);
  881. /* give those requests time to exit */
  882. msleep(125);
  883. if (cifs_rdma_enabled(server))
  884. smbd_destroy(server);
  885. if (server->ssocket) {
  886. sock_release(server->ssocket);
  887. server->ssocket = NULL;
  888. }
  889. if (!list_empty(&server->pending_mid_q)) {
  890. struct mid_q_entry *mid_entry;
  891. struct list_head *tmp, *tmp2;
  892. LIST_HEAD(dispose_list);
  893. spin_lock(&server->mid_lock);
  894. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  895. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  896. cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
  897. kref_get(&mid_entry->refcount);
  898. mid_entry->mid_state = MID_SHUTDOWN;
  899. list_move(&mid_entry->qhead, &dispose_list);
  900. mid_entry->mid_flags |= MID_DELETED;
  901. }
  902. spin_unlock(&server->mid_lock);
  903. /* now walk dispose list and issue callbacks */
  904. list_for_each_safe(tmp, tmp2, &dispose_list) {
  905. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  906. cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
  907. list_del_init(&mid_entry->qhead);
  908. mid_entry->callback(mid_entry);
  909. release_mid(mid_entry);
  910. }
  911. /* 1/8th of sec is more than enough time for them to exit */
  912. msleep(125);
  913. }
  914. if (!list_empty(&server->pending_mid_q)) {
  915. /*
  916. * mpx threads have not exited yet give them at least the smb
  917. * send timeout time for long ops.
  918. *
  919. * Due to delays on oplock break requests, we need to wait at
  920. * least 45 seconds before giving up on a request getting a
  921. * response and going ahead and killing cifsd.
  922. */
  923. cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
  924. msleep(46000);
  925. /*
  926. * If threads still have not exited they are probably never
  927. * coming home not much else we can do but free the memory.
  928. */
  929. }
  930. put_net(cifs_net_ns(server));
  931. kfree(server->leaf_fullpath);
  932. kfree(server->hostname);
  933. kfree(server);
  934. length = atomic_dec_return(&tcpSesAllocCount);
  935. if (length > 0)
  936. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  937. }
  938. static int
  939. standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  940. {
  941. int length;
  942. char *buf = server->smallbuf;
  943. unsigned int pdu_length = server->pdu_size;
  944. /* make sure this will fit in a large buffer */
  945. if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
  946. HEADER_PREAMBLE_SIZE(server)) {
  947. cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
  948. cifs_reconnect(server, true);
  949. return -ECONNABORTED;
  950. }
  951. /* switch to large buffer if too big for a small one */
  952. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  953. server->large_buf = true;
  954. memcpy(server->bigbuf, buf, server->total_read);
  955. buf = server->bigbuf;
  956. }
  957. /* now read the rest */
  958. length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
  959. pdu_length - MID_HEADER_SIZE(server));
  960. if (length < 0)
  961. return length;
  962. server->total_read += length;
  963. dump_smb(buf, server->total_read);
  964. return cifs_handle_standard(server, mid);
  965. }
  966. int
  967. cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  968. {
  969. char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
  970. int rc;
  971. /*
  972. * We know that we received enough to get to the MID as we
  973. * checked the pdu_length earlier. Now check to see
  974. * if the rest of the header is OK.
  975. *
  976. * 48 bytes is enough to display the header and a little bit
  977. * into the payload for debugging purposes.
  978. */
  979. rc = server->ops->check_message(buf, server->total_read, server);
  980. if (rc)
  981. cifs_dump_mem("Bad SMB: ", buf,
  982. min_t(unsigned int, server->total_read, 48));
  983. if (server->ops->is_session_expired &&
  984. server->ops->is_session_expired(buf)) {
  985. cifs_reconnect(server, true);
  986. return -1;
  987. }
  988. if (server->ops->is_status_pending &&
  989. server->ops->is_status_pending(buf, server))
  990. return -1;
  991. if (!mid)
  992. return rc;
  993. handle_mid(mid, server, buf, rc);
  994. return 0;
  995. }
  996. static void
  997. smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
  998. {
  999. struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
  1000. int scredits, in_flight;
  1001. /*
  1002. * SMB1 does not use credits.
  1003. */
  1004. if (is_smb1(server))
  1005. return;
  1006. if (shdr->CreditRequest) {
  1007. spin_lock(&server->req_lock);
  1008. server->credits += le16_to_cpu(shdr->CreditRequest);
  1009. scredits = server->credits;
  1010. in_flight = server->in_flight;
  1011. spin_unlock(&server->req_lock);
  1012. wake_up(&server->request_q);
  1013. trace_smb3_hdr_credits(server->CurrentMid,
  1014. server->conn_id, server->hostname, scredits,
  1015. le16_to_cpu(shdr->CreditRequest), in_flight);
  1016. cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
  1017. __func__, le16_to_cpu(shdr->CreditRequest),
  1018. scredits);
  1019. }
  1020. }
  1021. static int
  1022. cifs_demultiplex_thread(void *p)
  1023. {
  1024. int i, num_mids, length;
  1025. struct TCP_Server_Info *server = p;
  1026. unsigned int pdu_length;
  1027. unsigned int next_offset;
  1028. char *buf = NULL;
  1029. struct task_struct *task_to_wake = NULL;
  1030. struct mid_q_entry *mids[MAX_COMPOUND];
  1031. char *bufs[MAX_COMPOUND];
  1032. unsigned int noreclaim_flag, num_io_timeout = 0;
  1033. bool pending_reconnect = false;
  1034. noreclaim_flag = memalloc_noreclaim_save();
  1035. cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
  1036. length = atomic_inc_return(&tcpSesAllocCount);
  1037. if (length > 1)
  1038. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  1039. set_freezable();
  1040. allow_kernel_signal(SIGKILL);
  1041. while (server->tcpStatus != CifsExiting) {
  1042. if (try_to_freeze())
  1043. continue;
  1044. if (!allocate_buffers(server))
  1045. continue;
  1046. server->large_buf = false;
  1047. buf = server->smallbuf;
  1048. pdu_length = 4; /* enough to get RFC1001 header */
  1049. length = cifs_read_from_socket(server, buf, pdu_length);
  1050. if (length < 0)
  1051. continue;
  1052. if (is_smb1(server))
  1053. server->total_read = length;
  1054. else
  1055. server->total_read = 0;
  1056. /*
  1057. * The right amount was read from socket - 4 bytes,
  1058. * so we can now interpret the length field.
  1059. */
  1060. pdu_length = get_rfc1002_length(buf);
  1061. cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
  1062. if (!is_smb_response(server, buf[0]))
  1063. continue;
  1064. pending_reconnect = false;
  1065. next_pdu:
  1066. server->pdu_size = pdu_length;
  1067. /* make sure we have enough to get to the MID */
  1068. if (server->pdu_size < MID_HEADER_SIZE(server)) {
  1069. cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
  1070. server->pdu_size);
  1071. cifs_reconnect(server, true);
  1072. continue;
  1073. }
  1074. /* read down to the MID */
  1075. length = cifs_read_from_socket(server,
  1076. buf + HEADER_PREAMBLE_SIZE(server),
  1077. MID_HEADER_SIZE(server));
  1078. if (length < 0)
  1079. continue;
  1080. server->total_read += length;
  1081. if (server->ops->next_header) {
  1082. if (server->ops->next_header(server, buf, &next_offset)) {
  1083. cifs_dbg(VFS, "%s: malformed response (next_offset=%u)\n",
  1084. __func__, next_offset);
  1085. cifs_reconnect(server, true);
  1086. continue;
  1087. }
  1088. if (next_offset)
  1089. server->pdu_size = next_offset;
  1090. }
  1091. memset(mids, 0, sizeof(mids));
  1092. memset(bufs, 0, sizeof(bufs));
  1093. num_mids = 0;
  1094. if (server->ops->is_transform_hdr &&
  1095. server->ops->receive_transform &&
  1096. server->ops->is_transform_hdr(buf)) {
  1097. length = server->ops->receive_transform(server,
  1098. mids,
  1099. bufs,
  1100. &num_mids);
  1101. } else {
  1102. mids[0] = server->ops->find_mid(server, buf);
  1103. bufs[0] = buf;
  1104. num_mids = 1;
  1105. if (!mids[0] || !mids[0]->receive)
  1106. length = standard_receive3(server, mids[0]);
  1107. else
  1108. length = mids[0]->receive(server, mids[0]);
  1109. }
  1110. if (length < 0) {
  1111. for (i = 0; i < num_mids; i++)
  1112. if (mids[i])
  1113. release_mid(mids[i]);
  1114. continue;
  1115. }
  1116. if (server->ops->is_status_io_timeout &&
  1117. server->ops->is_status_io_timeout(buf)) {
  1118. num_io_timeout++;
  1119. if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) {
  1120. cifs_server_dbg(VFS,
  1121. "Number of request timeouts exceeded %d. Reconnecting",
  1122. MAX_STATUS_IO_TIMEOUT);
  1123. pending_reconnect = true;
  1124. num_io_timeout = 0;
  1125. }
  1126. }
  1127. server->lstrp = jiffies;
  1128. for (i = 0; i < num_mids; i++) {
  1129. if (mids[i] != NULL) {
  1130. mids[i]->resp_buf_size = server->pdu_size;
  1131. if (bufs[i] != NULL) {
  1132. if (server->ops->is_network_name_deleted &&
  1133. server->ops->is_network_name_deleted(bufs[i],
  1134. server)) {
  1135. cifs_server_dbg(FYI,
  1136. "Share deleted. Reconnect needed");
  1137. }
  1138. }
  1139. if (!mids[i]->multiRsp || mids[i]->multiEnd)
  1140. mids[i]->callback(mids[i]);
  1141. release_mid(mids[i]);
  1142. } else if (server->ops->is_oplock_break &&
  1143. server->ops->is_oplock_break(bufs[i],
  1144. server)) {
  1145. smb2_add_credits_from_hdr(bufs[i], server);
  1146. cifs_dbg(FYI, "Received oplock break\n");
  1147. } else {
  1148. cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
  1149. atomic_read(&mid_count));
  1150. cifs_dump_mem("Received Data is: ", bufs[i],
  1151. HEADER_SIZE(server));
  1152. smb2_add_credits_from_hdr(bufs[i], server);
  1153. #ifdef CONFIG_CIFS_DEBUG2
  1154. if (server->ops->dump_detail)
  1155. server->ops->dump_detail(bufs[i],
  1156. server);
  1157. cifs_dump_mids(server);
  1158. #endif /* CIFS_DEBUG2 */
  1159. }
  1160. }
  1161. if (pdu_length > server->pdu_size) {
  1162. if (!allocate_buffers(server))
  1163. continue;
  1164. pdu_length -= server->pdu_size;
  1165. server->total_read = 0;
  1166. server->large_buf = false;
  1167. buf = server->smallbuf;
  1168. goto next_pdu;
  1169. }
  1170. /* do this reconnect at the very end after processing all MIDs */
  1171. if (pending_reconnect)
  1172. cifs_reconnect(server, true);
  1173. } /* end while !EXITING */
  1174. /* buffer usually freed in free_mid - need to free it here on exit */
  1175. cifs_buf_release(server->bigbuf);
  1176. if (server->smallbuf) /* no sense logging a debug message if NULL */
  1177. cifs_small_buf_release(server->smallbuf);
  1178. task_to_wake = xchg(&server->tsk, NULL);
  1179. clean_demultiplex_info(server);
  1180. /* if server->tsk was NULL then wait for a signal before exiting */
  1181. if (!task_to_wake) {
  1182. set_current_state(TASK_INTERRUPTIBLE);
  1183. while (!signal_pending(current)) {
  1184. schedule();
  1185. set_current_state(TASK_INTERRUPTIBLE);
  1186. }
  1187. set_current_state(TASK_RUNNING);
  1188. }
  1189. memalloc_noreclaim_restore(noreclaim_flag);
  1190. module_put_and_kthread_exit(0);
  1191. }
  1192. int
  1193. cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1194. {
  1195. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1196. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1197. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1198. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
  1199. switch (srcaddr->sa_family) {
  1200. case AF_UNSPEC:
  1201. switch (rhs->sa_family) {
  1202. case AF_UNSPEC:
  1203. return 0;
  1204. case AF_INET:
  1205. case AF_INET6:
  1206. return 1;
  1207. default:
  1208. return -1;
  1209. }
  1210. case AF_INET: {
  1211. switch (rhs->sa_family) {
  1212. case AF_UNSPEC:
  1213. return -1;
  1214. case AF_INET:
  1215. return memcmp(saddr4, vaddr4,
  1216. sizeof(struct sockaddr_in));
  1217. case AF_INET6:
  1218. return 1;
  1219. default:
  1220. return -1;
  1221. }
  1222. }
  1223. case AF_INET6: {
  1224. switch (rhs->sa_family) {
  1225. case AF_UNSPEC:
  1226. case AF_INET:
  1227. return -1;
  1228. case AF_INET6:
  1229. return memcmp(saddr6,
  1230. vaddr6,
  1231. sizeof(struct sockaddr_in6));
  1232. default:
  1233. return -1;
  1234. }
  1235. }
  1236. default:
  1237. return -1; /* don't expect to be here */
  1238. }
  1239. }
  1240. /*
  1241. * Returns true if srcaddr isn't specified and rhs isn't specified, or
  1242. * if srcaddr is specified and matches the IP address of the rhs argument
  1243. */
  1244. bool
  1245. cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1246. {
  1247. switch (srcaddr->sa_family) {
  1248. case AF_UNSPEC:
  1249. return (rhs->sa_family == AF_UNSPEC);
  1250. case AF_INET: {
  1251. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1252. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1253. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  1254. }
  1255. case AF_INET6: {
  1256. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1257. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
  1258. return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
  1259. && saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
  1260. }
  1261. default:
  1262. WARN_ON(1);
  1263. return false; /* don't expect to be here */
  1264. }
  1265. }
  1266. /*
  1267. * If no port is specified in addr structure, we try to match with 445 port
  1268. * and if it fails - with 139 ports. It should be called only if address
  1269. * families of server and addr are equal.
  1270. */
  1271. static bool
  1272. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  1273. {
  1274. __be16 port, *sport;
  1275. /* SMBDirect manages its own ports, don't match it here */
  1276. if (server->rdma)
  1277. return true;
  1278. switch (addr->sa_family) {
  1279. case AF_INET:
  1280. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  1281. port = ((struct sockaddr_in *) addr)->sin_port;
  1282. break;
  1283. case AF_INET6:
  1284. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  1285. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  1286. break;
  1287. default:
  1288. WARN_ON(1);
  1289. return false;
  1290. }
  1291. if (!port) {
  1292. port = htons(CIFS_PORT);
  1293. if (port == *sport)
  1294. return true;
  1295. port = htons(RFC1001_PORT);
  1296. }
  1297. return port == *sport;
  1298. }
  1299. static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
  1300. {
  1301. if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
  1302. return false;
  1303. return true;
  1304. }
  1305. static bool
  1306. match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
  1307. {
  1308. /*
  1309. * The select_sectype function should either return the ctx->sectype
  1310. * that was specified, or "Unspecified" if that sectype was not
  1311. * compatible with the given NEGOTIATE request.
  1312. */
  1313. if (server->ops->select_sectype(server, ctx->sectype)
  1314. == Unspecified)
  1315. return false;
  1316. /*
  1317. * Now check if signing mode is acceptable. No need to check
  1318. * global_secflags at this point since if MUST_SIGN is set then
  1319. * the server->sign had better be too.
  1320. */
  1321. if (ctx->sign && !server->sign)
  1322. return false;
  1323. return true;
  1324. }
  1325. /* this function must be called with srv_lock held */
  1326. static int match_server(struct TCP_Server_Info *server,
  1327. struct smb3_fs_context *ctx,
  1328. bool match_super)
  1329. {
  1330. struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
  1331. lockdep_assert_held(&server->srv_lock);
  1332. if (ctx->nosharesock)
  1333. return 0;
  1334. /* this server does not share socket */
  1335. if (server->nosharesock)
  1336. return 0;
  1337. if (!match_super && (ctx->dfs_conn || server->dfs_conn))
  1338. return 0;
  1339. /* If multidialect negotiation see if existing sessions match one */
  1340. if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
  1341. if (server->vals->protocol_id < SMB30_PROT_ID)
  1342. return 0;
  1343. } else if (strcmp(ctx->vals->version_string,
  1344. SMBDEFAULT_VERSION_STRING) == 0) {
  1345. if (server->vals->protocol_id < SMB21_PROT_ID)
  1346. return 0;
  1347. } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
  1348. return 0;
  1349. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  1350. return 0;
  1351. if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
  1352. (struct sockaddr *)&server->srcaddr))
  1353. return 0;
  1354. /*
  1355. * When matching cifs.ko superblocks (@match_super == true), we can't
  1356. * really match either @server->leaf_fullpath or @server->dstaddr
  1357. * directly since this @server might belong to a completely different
  1358. * server -- in case of domain-based DFS referrals or DFS links -- as
  1359. * provided earlier by mount(2) through 'source' and 'ip' options.
  1360. *
  1361. * Otherwise, match the DFS referral in @server->leaf_fullpath or the
  1362. * destination address in @server->dstaddr.
  1363. *
  1364. * When using 'nodfs' mount option, we avoid sharing it with DFS
  1365. * connections as they might failover.
  1366. */
  1367. if (!match_super) {
  1368. if (!ctx->nodfs) {
  1369. if (server->leaf_fullpath) {
  1370. if (!ctx->leaf_fullpath ||
  1371. strcasecmp(server->leaf_fullpath,
  1372. ctx->leaf_fullpath))
  1373. return 0;
  1374. } else if (ctx->leaf_fullpath) {
  1375. return 0;
  1376. }
  1377. } else if (server->leaf_fullpath) {
  1378. return 0;
  1379. }
  1380. }
  1381. /*
  1382. * Match for a regular connection (address/hostname/port) which has no
  1383. * DFS referrals set.
  1384. */
  1385. if (!server->leaf_fullpath &&
  1386. (strcasecmp(server->hostname, ctx->server_hostname) ||
  1387. !match_server_address(server, addr) ||
  1388. !match_port(server, addr)))
  1389. return 0;
  1390. if (!match_security(server, ctx))
  1391. return 0;
  1392. if (server->echo_interval != ctx->echo_interval * HZ)
  1393. return 0;
  1394. if (server->rdma != ctx->rdma)
  1395. return 0;
  1396. if (server->ignore_signature != ctx->ignore_signature)
  1397. return 0;
  1398. if (server->min_offload != ctx->min_offload)
  1399. return 0;
  1400. if (server->retrans != ctx->retrans)
  1401. return 0;
  1402. return 1;
  1403. }
  1404. struct TCP_Server_Info *
  1405. cifs_find_tcp_session(struct smb3_fs_context *ctx)
  1406. {
  1407. struct TCP_Server_Info *server;
  1408. spin_lock(&cifs_tcp_ses_lock);
  1409. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  1410. spin_lock(&server->srv_lock);
  1411. /*
  1412. * Skip ses channels since they're only handled in lower layers
  1413. * (e.g. cifs_send_recv).
  1414. */
  1415. if (SERVER_IS_CHAN(server) ||
  1416. !match_server(server, ctx, false)) {
  1417. spin_unlock(&server->srv_lock);
  1418. continue;
  1419. }
  1420. spin_unlock(&server->srv_lock);
  1421. ++server->srv_count;
  1422. spin_unlock(&cifs_tcp_ses_lock);
  1423. cifs_dbg(FYI, "Existing tcp session with server found\n");
  1424. return server;
  1425. }
  1426. spin_unlock(&cifs_tcp_ses_lock);
  1427. return NULL;
  1428. }
  1429. void
  1430. cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
  1431. {
  1432. struct task_struct *task;
  1433. spin_lock(&cifs_tcp_ses_lock);
  1434. if (--server->srv_count > 0) {
  1435. spin_unlock(&cifs_tcp_ses_lock);
  1436. return;
  1437. }
  1438. /* srv_count can never go negative */
  1439. WARN_ON(server->srv_count < 0);
  1440. list_del_init(&server->tcp_ses_list);
  1441. spin_unlock(&cifs_tcp_ses_lock);
  1442. cancel_delayed_work_sync(&server->echo);
  1443. if (from_reconnect)
  1444. /*
  1445. * Avoid deadlock here: reconnect work calls
  1446. * cifs_put_tcp_session() at its end. Need to be sure
  1447. * that reconnect work does nothing with server pointer after
  1448. * that step.
  1449. */
  1450. cancel_delayed_work(&server->reconnect);
  1451. else
  1452. cancel_delayed_work_sync(&server->reconnect);
  1453. /* For secondary channels, we pick up ref-count on the primary server */
  1454. if (SERVER_IS_CHAN(server))
  1455. cifs_put_tcp_session(server->primary_server, from_reconnect);
  1456. spin_lock(&server->srv_lock);
  1457. server->tcpStatus = CifsExiting;
  1458. spin_unlock(&server->srv_lock);
  1459. cifs_crypto_secmech_release(server);
  1460. kfree_sensitive(server->session_key.response);
  1461. server->session_key.response = NULL;
  1462. server->session_key.len = 0;
  1463. task = xchg(&server->tsk, NULL);
  1464. if (task)
  1465. send_sig(SIGKILL, task, 1);
  1466. }
  1467. struct TCP_Server_Info *
  1468. cifs_get_tcp_session(struct smb3_fs_context *ctx,
  1469. struct TCP_Server_Info *primary_server)
  1470. {
  1471. struct TCP_Server_Info *tcp_ses = NULL;
  1472. int rc;
  1473. cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
  1474. /* see if we already have a matching tcp_ses */
  1475. tcp_ses = cifs_find_tcp_session(ctx);
  1476. if (tcp_ses)
  1477. return tcp_ses;
  1478. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1479. if (!tcp_ses) {
  1480. rc = -ENOMEM;
  1481. goto out_err;
  1482. }
  1483. tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
  1484. if (!tcp_ses->hostname) {
  1485. rc = -ENOMEM;
  1486. goto out_err;
  1487. }
  1488. if (ctx->leaf_fullpath) {
  1489. tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
  1490. if (!tcp_ses->leaf_fullpath) {
  1491. rc = -ENOMEM;
  1492. goto out_err;
  1493. }
  1494. }
  1495. if (ctx->nosharesock)
  1496. tcp_ses->nosharesock = true;
  1497. tcp_ses->dfs_conn = ctx->dfs_conn;
  1498. tcp_ses->ops = ctx->ops;
  1499. tcp_ses->vals = ctx->vals;
  1500. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  1501. tcp_ses->sign = ctx->sign;
  1502. tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
  1503. tcp_ses->noblockcnt = ctx->rootfs;
  1504. tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
  1505. tcp_ses->noautotune = ctx->noautotune;
  1506. tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
  1507. tcp_ses->rdma = ctx->rdma;
  1508. tcp_ses->in_flight = 0;
  1509. tcp_ses->max_in_flight = 0;
  1510. tcp_ses->credits = 1;
  1511. if (primary_server) {
  1512. spin_lock(&cifs_tcp_ses_lock);
  1513. ++primary_server->srv_count;
  1514. spin_unlock(&cifs_tcp_ses_lock);
  1515. tcp_ses->primary_server = primary_server;
  1516. }
  1517. init_waitqueue_head(&tcp_ses->response_q);
  1518. init_waitqueue_head(&tcp_ses->request_q);
  1519. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1520. mutex_init(&tcp_ses->_srv_mutex);
  1521. memcpy(tcp_ses->workstation_RFC1001_name,
  1522. ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1523. memcpy(tcp_ses->server_RFC1001_name,
  1524. ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1525. tcp_ses->session_estab = false;
  1526. tcp_ses->sequence_number = 0;
  1527. tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */
  1528. tcp_ses->reconnect_instance = 1;
  1529. tcp_ses->lstrp = jiffies;
  1530. tcp_ses->compression.requested = ctx->compress;
  1531. spin_lock_init(&tcp_ses->req_lock);
  1532. spin_lock_init(&tcp_ses->srv_lock);
  1533. spin_lock_init(&tcp_ses->mid_lock);
  1534. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1535. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1536. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  1537. INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
  1538. mutex_init(&tcp_ses->reconnect_mutex);
  1539. #ifdef CONFIG_CIFS_DFS_UPCALL
  1540. mutex_init(&tcp_ses->refpath_lock);
  1541. #endif
  1542. memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
  1543. sizeof(tcp_ses->srcaddr));
  1544. memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
  1545. sizeof(tcp_ses->dstaddr));
  1546. if (ctx->use_client_guid)
  1547. memcpy(tcp_ses->client_guid, ctx->client_guid,
  1548. SMB2_CLIENT_GUID_SIZE);
  1549. else
  1550. generate_random_uuid(tcp_ses->client_guid);
  1551. /*
  1552. * at this point we are the only ones with the pointer
  1553. * to the struct since the kernel thread not created yet
  1554. * no need to spinlock this init of tcpStatus or srv_count
  1555. */
  1556. tcp_ses->tcpStatus = CifsNew;
  1557. ++tcp_ses->srv_count;
  1558. if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
  1559. ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
  1560. tcp_ses->echo_interval = ctx->echo_interval * HZ;
  1561. else
  1562. tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
  1563. if (tcp_ses->rdma) {
  1564. #ifndef CONFIG_CIFS_SMB_DIRECT
  1565. cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
  1566. rc = -ENOENT;
  1567. goto out_err_crypto_release;
  1568. #endif
  1569. tcp_ses->smbd_conn = smbd_get_connection(
  1570. tcp_ses, (struct sockaddr *)&ctx->dstaddr);
  1571. if (tcp_ses->smbd_conn) {
  1572. cifs_dbg(VFS, "RDMA transport established\n");
  1573. rc = 0;
  1574. goto smbd_connected;
  1575. } else {
  1576. rc = -ENOENT;
  1577. goto out_err_crypto_release;
  1578. }
  1579. }
  1580. rc = ip_connect(tcp_ses);
  1581. if (rc < 0) {
  1582. cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
  1583. goto out_err_crypto_release;
  1584. }
  1585. smbd_connected:
  1586. /*
  1587. * since we're in a cifs function already, we know that
  1588. * this will succeed. No need for try_module_get().
  1589. */
  1590. __module_get(THIS_MODULE);
  1591. tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
  1592. tcp_ses, "cifsd");
  1593. if (IS_ERR(tcp_ses->tsk)) {
  1594. rc = PTR_ERR(tcp_ses->tsk);
  1595. cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
  1596. module_put(THIS_MODULE);
  1597. goto out_err_crypto_release;
  1598. }
  1599. tcp_ses->min_offload = ctx->min_offload;
  1600. tcp_ses->retrans = ctx->retrans;
  1601. /*
  1602. * at this point we are the only ones with the pointer
  1603. * to the struct since the kernel thread not created yet
  1604. * no need to spinlock this update of tcpStatus
  1605. */
  1606. spin_lock(&tcp_ses->srv_lock);
  1607. tcp_ses->tcpStatus = CifsNeedNegotiate;
  1608. spin_unlock(&tcp_ses->srv_lock);
  1609. if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
  1610. tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
  1611. else
  1612. tcp_ses->max_credits = ctx->max_credits;
  1613. tcp_ses->nr_targets = 1;
  1614. tcp_ses->ignore_signature = ctx->ignore_signature;
  1615. /* thread spawned, put it on the list */
  1616. spin_lock(&cifs_tcp_ses_lock);
  1617. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1618. spin_unlock(&cifs_tcp_ses_lock);
  1619. /* queue echo request delayed work */
  1620. queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
  1621. return tcp_ses;
  1622. out_err_crypto_release:
  1623. cifs_crypto_secmech_release(tcp_ses);
  1624. put_net(cifs_net_ns(tcp_ses));
  1625. out_err:
  1626. if (tcp_ses) {
  1627. if (SERVER_IS_CHAN(tcp_ses))
  1628. cifs_put_tcp_session(tcp_ses->primary_server, false);
  1629. kfree(tcp_ses->hostname);
  1630. kfree(tcp_ses->leaf_fullpath);
  1631. if (tcp_ses->ssocket)
  1632. sock_release(tcp_ses->ssocket);
  1633. kfree(tcp_ses);
  1634. }
  1635. return ERR_PTR(rc);
  1636. }
  1637. /* this function must be called with ses_lock and chan_lock held */
  1638. static int match_session(struct cifs_ses *ses,
  1639. struct smb3_fs_context *ctx,
  1640. bool match_super)
  1641. {
  1642. struct TCP_Server_Info *server = ses->server;
  1643. enum securityEnum ctx_sec, ses_sec;
  1644. if (!match_super && ctx->dfs_root_ses != ses->dfs_root_ses)
  1645. return 0;
  1646. /*
  1647. * If an existing session is limited to less channels than
  1648. * requested, it should not be reused
  1649. */
  1650. if (ses->chan_max < ctx->max_channels)
  1651. return 0;
  1652. ctx_sec = server->ops->select_sectype(server, ctx->sectype);
  1653. ses_sec = server->ops->select_sectype(server, ses->sectype);
  1654. if (ctx_sec != ses_sec)
  1655. return 0;
  1656. switch (ctx_sec) {
  1657. case IAKerb:
  1658. case Kerberos:
  1659. if (!uid_eq(ctx->cred_uid, ses->cred_uid))
  1660. return 0;
  1661. break;
  1662. case NTLMv2:
  1663. case RawNTLMSSP:
  1664. default:
  1665. /* NULL username means anonymous session */
  1666. if (ses->user_name == NULL) {
  1667. if (!ctx->nullauth)
  1668. return 0;
  1669. break;
  1670. }
  1671. /* anything else takes username/password */
  1672. if (strncmp(ses->user_name,
  1673. ctx->username ? ctx->username : "",
  1674. CIFS_MAX_USERNAME_LEN))
  1675. return 0;
  1676. if ((ctx->username && strlen(ctx->username) != 0) &&
  1677. ses->password != NULL) {
  1678. /* New mount can only share sessions with an existing mount if:
  1679. * 1. Both password and password2 match, or
  1680. * 2. password2 of the old mount matches password of the new mount
  1681. * and password of the old mount matches password2 of the new
  1682. * mount
  1683. */
  1684. if (ses->password2 != NULL && ctx->password2 != NULL) {
  1685. if (!((strncmp(ses->password, ctx->password ?
  1686. ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0 &&
  1687. strncmp(ses->password2, ctx->password2,
  1688. CIFS_MAX_PASSWORD_LEN) == 0) ||
  1689. (strncmp(ses->password, ctx->password2,
  1690. CIFS_MAX_PASSWORD_LEN) == 0 &&
  1691. strncmp(ses->password2, ctx->password ?
  1692. ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0)))
  1693. return 0;
  1694. } else if ((ses->password2 == NULL && ctx->password2 != NULL) ||
  1695. (ses->password2 != NULL && ctx->password2 == NULL)) {
  1696. return 0;
  1697. } else {
  1698. if (strncmp(ses->password, ctx->password ?
  1699. ctx->password : "", CIFS_MAX_PASSWORD_LEN))
  1700. return 0;
  1701. }
  1702. }
  1703. }
  1704. if (strcmp(ctx->local_nls->charset, ses->local_nls->charset))
  1705. return 0;
  1706. return 1;
  1707. }
  1708. /**
  1709. * cifs_setup_ipc - helper to setup the IPC tcon for the session
  1710. * @ses: smb session to issue the request on
  1711. * @ctx: the superblock configuration context to use for building the
  1712. * new tree connection for the IPC (interprocess communication RPC)
  1713. *
  1714. * A new IPC connection is made and stored in the session
  1715. * tcon_ipc. The IPC tcon has the same lifetime as the session.
  1716. */
  1717. static int
  1718. cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
  1719. {
  1720. int rc = 0, xid;
  1721. struct cifs_tcon *tcon;
  1722. char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
  1723. bool seal = false;
  1724. struct TCP_Server_Info *server = ses->server;
  1725. /*
  1726. * If the mount request that resulted in the creation of the
  1727. * session requires encryption, force IPC to be encrypted too.
  1728. */
  1729. if (ctx->seal) {
  1730. if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
  1731. seal = true;
  1732. else {
  1733. cifs_server_dbg(VFS,
  1734. "IPC: server doesn't support encryption\n");
  1735. return -EOPNOTSUPP;
  1736. }
  1737. }
  1738. /* no need to setup directory caching on IPC share, so pass in false */
  1739. tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_ipc);
  1740. if (tcon == NULL)
  1741. return -ENOMEM;
  1742. spin_lock(&server->srv_lock);
  1743. scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
  1744. spin_unlock(&server->srv_lock);
  1745. xid = get_xid();
  1746. tcon->ses = ses;
  1747. tcon->ipc = true;
  1748. tcon->seal = seal;
  1749. rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
  1750. free_xid(xid);
  1751. if (rc) {
  1752. cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
  1753. tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc_fail);
  1754. goto out;
  1755. }
  1756. cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
  1757. spin_lock(&tcon->tc_lock);
  1758. tcon->status = TID_GOOD;
  1759. spin_unlock(&tcon->tc_lock);
  1760. ses->tcon_ipc = tcon;
  1761. out:
  1762. return rc;
  1763. }
  1764. static struct cifs_ses *
  1765. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
  1766. {
  1767. struct cifs_ses *ses, *ret = NULL;
  1768. spin_lock(&cifs_tcp_ses_lock);
  1769. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1770. spin_lock(&ses->ses_lock);
  1771. if (ses->ses_status == SES_EXITING) {
  1772. spin_unlock(&ses->ses_lock);
  1773. continue;
  1774. }
  1775. spin_lock(&ses->chan_lock);
  1776. if (match_session(ses, ctx, false)) {
  1777. spin_unlock(&ses->chan_lock);
  1778. spin_unlock(&ses->ses_lock);
  1779. ret = ses;
  1780. break;
  1781. }
  1782. spin_unlock(&ses->chan_lock);
  1783. spin_unlock(&ses->ses_lock);
  1784. }
  1785. if (ret)
  1786. cifs_smb_ses_inc_refcount(ret);
  1787. spin_unlock(&cifs_tcp_ses_lock);
  1788. return ret;
  1789. }
  1790. void __cifs_put_smb_ses(struct cifs_ses *ses)
  1791. {
  1792. struct TCP_Server_Info *server = ses->server;
  1793. struct cifs_tcon *tcon;
  1794. unsigned int xid;
  1795. size_t i;
  1796. bool do_logoff;
  1797. int rc;
  1798. spin_lock(&cifs_tcp_ses_lock);
  1799. spin_lock(&ses->ses_lock);
  1800. cifs_dbg(FYI, "%s: id=0x%llx ses_count=%d ses_status=%u ipc=%s\n",
  1801. __func__, ses->Suid, ses->ses_count, ses->ses_status,
  1802. ses->tcon_ipc ? ses->tcon_ipc->tree_name : "none");
  1803. if (ses->ses_status == SES_EXITING || --ses->ses_count > 0) {
  1804. spin_unlock(&ses->ses_lock);
  1805. spin_unlock(&cifs_tcp_ses_lock);
  1806. return;
  1807. }
  1808. /* ses_count can never go negative */
  1809. WARN_ON(ses->ses_count < 0);
  1810. spin_lock(&ses->chan_lock);
  1811. cifs_chan_clear_need_reconnect(ses, server);
  1812. spin_unlock(&ses->chan_lock);
  1813. do_logoff = ses->ses_status == SES_GOOD && server->ops->logoff;
  1814. ses->ses_status = SES_EXITING;
  1815. tcon = ses->tcon_ipc;
  1816. ses->tcon_ipc = NULL;
  1817. spin_unlock(&ses->ses_lock);
  1818. spin_unlock(&cifs_tcp_ses_lock);
  1819. /*
  1820. * On session close, the IPC is closed and the server must release all
  1821. * tcons of the session. No need to send a tree disconnect here.
  1822. *
  1823. * Besides, it will make the server to not close durable and resilient
  1824. * files on session close, as specified in MS-SMB2 3.3.5.6 Receiving an
  1825. * SMB2 LOGOFF Request.
  1826. */
  1827. tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc);
  1828. if (do_logoff) {
  1829. xid = get_xid();
  1830. rc = server->ops->logoff(xid, ses);
  1831. cifs_server_dbg(FYI, "%s: Session Logoff: rc=%d\n",
  1832. __func__, rc);
  1833. _free_xid(xid);
  1834. }
  1835. spin_lock(&cifs_tcp_ses_lock);
  1836. list_del_init(&ses->smb_ses_list);
  1837. spin_unlock(&cifs_tcp_ses_lock);
  1838. /* close any extra channels */
  1839. for (i = 1; i < ses->chan_count; i++) {
  1840. if (ses->chans[i].iface) {
  1841. kref_put(&ses->chans[i].iface->refcount, release_iface);
  1842. ses->chans[i].iface = NULL;
  1843. }
  1844. cifs_put_tcp_session(ses->chans[i].server, 0);
  1845. ses->chans[i].server = NULL;
  1846. }
  1847. /* we now account for primary channel in iface->refcount */
  1848. if (ses->chans[0].iface) {
  1849. kref_put(&ses->chans[0].iface->refcount, release_iface);
  1850. ses->chans[0].server = NULL;
  1851. }
  1852. sesInfoFree(ses);
  1853. cifs_put_tcp_session(server, 0);
  1854. }
  1855. #ifdef CONFIG_KEYS
  1856. /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
  1857. #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
  1858. /* Populate username and pw fields from keyring if possible */
  1859. static int
  1860. cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
  1861. {
  1862. int rc = 0;
  1863. int is_domain = 0;
  1864. const char *delim, *payload;
  1865. char *desc;
  1866. ssize_t len;
  1867. struct key *key;
  1868. struct TCP_Server_Info *server = ses->server;
  1869. struct sockaddr_in *sa;
  1870. struct sockaddr_in6 *sa6;
  1871. const struct user_key_payload *upayload;
  1872. desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
  1873. if (!desc)
  1874. return -ENOMEM;
  1875. /* try to find an address key first */
  1876. switch (server->dstaddr.ss_family) {
  1877. case AF_INET:
  1878. sa = (struct sockaddr_in *)&server->dstaddr;
  1879. sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
  1880. break;
  1881. case AF_INET6:
  1882. sa6 = (struct sockaddr_in6 *)&server->dstaddr;
  1883. sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
  1884. break;
  1885. default:
  1886. cifs_dbg(FYI, "Bad ss_family (%hu)\n",
  1887. server->dstaddr.ss_family);
  1888. rc = -EINVAL;
  1889. goto out_err;
  1890. }
  1891. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  1892. key = request_key(&key_type_logon, desc, "");
  1893. if (IS_ERR(key)) {
  1894. if (!ses->domainName) {
  1895. cifs_dbg(FYI, "domainName is NULL\n");
  1896. rc = PTR_ERR(key);
  1897. goto out_err;
  1898. }
  1899. /* didn't work, try to find a domain key */
  1900. sprintf(desc, "cifs:d:%s", ses->domainName);
  1901. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  1902. key = request_key(&key_type_logon, desc, "");
  1903. if (IS_ERR(key)) {
  1904. rc = PTR_ERR(key);
  1905. goto out_err;
  1906. }
  1907. is_domain = 1;
  1908. }
  1909. down_read(&key->sem);
  1910. upayload = user_key_payload_locked(key);
  1911. if (IS_ERR_OR_NULL(upayload)) {
  1912. rc = upayload ? PTR_ERR(upayload) : -EINVAL;
  1913. goto out_key_put;
  1914. }
  1915. /* find first : in payload */
  1916. payload = upayload->data;
  1917. delim = strnchr(payload, upayload->datalen, ':');
  1918. cifs_dbg(FYI, "payload=%s\n", payload);
  1919. if (!delim) {
  1920. cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
  1921. upayload->datalen);
  1922. rc = -EINVAL;
  1923. goto out_key_put;
  1924. }
  1925. len = delim - payload;
  1926. if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
  1927. cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
  1928. len);
  1929. rc = -EINVAL;
  1930. goto out_key_put;
  1931. }
  1932. ctx->username = kstrndup(payload, len, GFP_KERNEL);
  1933. if (!ctx->username) {
  1934. cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
  1935. len);
  1936. rc = -ENOMEM;
  1937. goto out_key_put;
  1938. }
  1939. cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
  1940. len = key->datalen - (len + 1);
  1941. if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
  1942. cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
  1943. rc = -EINVAL;
  1944. kfree(ctx->username);
  1945. ctx->username = NULL;
  1946. goto out_key_put;
  1947. }
  1948. ++delim;
  1949. /* BB consider adding support for password2 (Key Rotation) for multiuser in future */
  1950. ctx->password = kstrndup(delim, len, GFP_KERNEL);
  1951. if (!ctx->password) {
  1952. cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
  1953. len);
  1954. rc = -ENOMEM;
  1955. kfree(ctx->username);
  1956. ctx->username = NULL;
  1957. goto out_key_put;
  1958. }
  1959. /*
  1960. * If we have a domain key then we must set the domainName in the
  1961. * for the request.
  1962. */
  1963. if (is_domain && ses->domainName) {
  1964. ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
  1965. if (!ctx->domainname) {
  1966. cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
  1967. len);
  1968. rc = -ENOMEM;
  1969. kfree(ctx->username);
  1970. ctx->username = NULL;
  1971. kfree_sensitive(ctx->password);
  1972. /* no need to free ctx->password2 since not allocated in this path */
  1973. ctx->password = NULL;
  1974. goto out_key_put;
  1975. }
  1976. }
  1977. strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
  1978. out_key_put:
  1979. up_read(&key->sem);
  1980. key_put(key);
  1981. out_err:
  1982. kfree(desc);
  1983. cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
  1984. return rc;
  1985. }
  1986. #else /* ! CONFIG_KEYS */
  1987. static inline int
  1988. cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
  1989. struct cifs_ses *ses __attribute__((unused)))
  1990. {
  1991. return -ENOSYS;
  1992. }
  1993. #endif /* CONFIG_KEYS */
  1994. /**
  1995. * cifs_get_smb_ses - get a session matching @ctx data from @server
  1996. * @server: server to setup the session to
  1997. * @ctx: superblock configuration context to use to setup the session
  1998. *
  1999. * This function assumes it is being called from cifs_mount() where we
  2000. * already got a server reference (server refcount +1). See
  2001. * cifs_get_tcon() for refcount explanations.
  2002. */
  2003. struct cifs_ses *
  2004. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
  2005. {
  2006. int rc = 0;
  2007. int retries = 0;
  2008. unsigned int xid;
  2009. struct cifs_ses *ses;
  2010. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2011. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2012. xid = get_xid();
  2013. ses = cifs_find_smb_ses(server, ctx);
  2014. if (ses) {
  2015. cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
  2016. ses->ses_status);
  2017. spin_lock(&ses->chan_lock);
  2018. if (cifs_chan_needs_reconnect(ses, server)) {
  2019. spin_unlock(&ses->chan_lock);
  2020. cifs_dbg(FYI, "Session needs reconnect\n");
  2021. mutex_lock(&ses->session_mutex);
  2022. retry_old_session:
  2023. rc = cifs_negotiate_protocol(xid, ses, server);
  2024. if (rc) {
  2025. mutex_unlock(&ses->session_mutex);
  2026. /* problem -- put our ses reference */
  2027. cifs_put_smb_ses(ses);
  2028. free_xid(xid);
  2029. return ERR_PTR(rc);
  2030. }
  2031. rc = cifs_setup_session(xid, ses, server,
  2032. ctx->local_nls);
  2033. if (rc) {
  2034. if (((rc == -EACCES) || (rc == -EKEYEXPIRED) ||
  2035. (rc == -EKEYREVOKED)) && !retries && ses->password2) {
  2036. retries++;
  2037. cifs_dbg(FYI, "Session reconnect failed, retrying with alternate password\n");
  2038. swap(ses->password, ses->password2);
  2039. goto retry_old_session;
  2040. }
  2041. mutex_unlock(&ses->session_mutex);
  2042. /* problem -- put our reference */
  2043. cifs_put_smb_ses(ses);
  2044. free_xid(xid);
  2045. return ERR_PTR(rc);
  2046. }
  2047. mutex_unlock(&ses->session_mutex);
  2048. spin_lock(&ses->chan_lock);
  2049. }
  2050. spin_unlock(&ses->chan_lock);
  2051. /* existing SMB ses has a server reference already */
  2052. cifs_put_tcp_session(server, 0);
  2053. free_xid(xid);
  2054. return ses;
  2055. }
  2056. rc = -ENOMEM;
  2057. cifs_dbg(FYI, "Existing smb sess not found\n");
  2058. ses = sesInfoAlloc();
  2059. if (ses == NULL)
  2060. goto get_ses_fail;
  2061. /* new SMB session uses our server ref */
  2062. ses->server = server;
  2063. if (server->dstaddr.ss_family == AF_INET6)
  2064. sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
  2065. else
  2066. sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
  2067. if (ctx->username) {
  2068. ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
  2069. if (!ses->user_name)
  2070. goto get_ses_fail;
  2071. }
  2072. /* ctx->password freed at unmount */
  2073. if (ctx->password) {
  2074. ses->password = kstrdup(ctx->password, GFP_KERNEL);
  2075. if (!ses->password)
  2076. goto get_ses_fail;
  2077. }
  2078. /* ctx->password freed at unmount */
  2079. if (ctx->password2) {
  2080. ses->password2 = kstrdup(ctx->password2, GFP_KERNEL);
  2081. if (!ses->password2)
  2082. goto get_ses_fail;
  2083. }
  2084. if (ctx->domainname) {
  2085. ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
  2086. if (!ses->domainName)
  2087. goto get_ses_fail;
  2088. }
  2089. strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
  2090. if (ctx->domainauto)
  2091. ses->domainAuto = ctx->domainauto;
  2092. ses->cred_uid = ctx->cred_uid;
  2093. ses->linux_uid = ctx->linux_uid;
  2094. ses->sectype = ctx->sectype;
  2095. ses->sign = ctx->sign;
  2096. /*
  2097. *Explicitly marking upcall_target mount option for easier handling
  2098. * by cifs_spnego.c and eventually cifs.upcall.c
  2099. */
  2100. switch (ctx->upcall_target) {
  2101. case UPTARGET_UNSPECIFIED: /* default to app */
  2102. case UPTARGET_APP:
  2103. ses->upcall_target = UPTARGET_APP;
  2104. break;
  2105. case UPTARGET_MOUNT:
  2106. ses->upcall_target = UPTARGET_MOUNT;
  2107. break;
  2108. default:
  2109. // should never happen
  2110. ses->upcall_target = UPTARGET_APP;
  2111. break;
  2112. }
  2113. ses->local_nls = load_nls(ctx->local_nls->charset);
  2114. /* add server as first channel */
  2115. spin_lock(&ses->chan_lock);
  2116. ses->chans[0].server = server;
  2117. ses->chan_count = 1;
  2118. ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
  2119. ses->chans_need_reconnect = 1;
  2120. spin_unlock(&ses->chan_lock);
  2121. retry_new_session:
  2122. mutex_lock(&ses->session_mutex);
  2123. rc = cifs_negotiate_protocol(xid, ses, server);
  2124. if (!rc)
  2125. rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
  2126. mutex_unlock(&ses->session_mutex);
  2127. /* each channel uses a different signing key */
  2128. spin_lock(&ses->chan_lock);
  2129. memcpy(ses->chans[0].signkey, ses->smb3signingkey,
  2130. sizeof(ses->smb3signingkey));
  2131. spin_unlock(&ses->chan_lock);
  2132. if (rc) {
  2133. if (((rc == -EACCES) || (rc == -EKEYEXPIRED) ||
  2134. (rc == -EKEYREVOKED)) && !retries && ses->password2) {
  2135. retries++;
  2136. cifs_dbg(FYI, "Session setup failed, retrying with alternate password\n");
  2137. swap(ses->password, ses->password2);
  2138. goto retry_new_session;
  2139. } else
  2140. goto get_ses_fail;
  2141. }
  2142. /*
  2143. * success, put it on the list and add it as first channel
  2144. * note: the session becomes active soon after this. So you'll
  2145. * need to lock before changing something in the session.
  2146. */
  2147. spin_lock(&cifs_tcp_ses_lock);
  2148. ses->dfs_root_ses = ctx->dfs_root_ses;
  2149. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  2150. spin_unlock(&cifs_tcp_ses_lock);
  2151. cifs_setup_ipc(ses, ctx);
  2152. free_xid(xid);
  2153. return ses;
  2154. get_ses_fail:
  2155. sesInfoFree(ses);
  2156. free_xid(xid);
  2157. return ERR_PTR(rc);
  2158. }
  2159. /* this function must be called with tc_lock held */
  2160. static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  2161. {
  2162. struct TCP_Server_Info *server = tcon->ses->server;
  2163. if (tcon->status == TID_EXITING)
  2164. return 0;
  2165. if (tcon->origin_fullpath) {
  2166. if (!ctx->source ||
  2167. !dfs_src_pathname_equal(ctx->source,
  2168. tcon->origin_fullpath))
  2169. return 0;
  2170. } else if (!server->leaf_fullpath &&
  2171. strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) {
  2172. return 0;
  2173. }
  2174. if (tcon->seal != ctx->seal)
  2175. return 0;
  2176. if (tcon->snapshot_time != ctx->snapshot_time)
  2177. return 0;
  2178. if (tcon->handle_timeout != ctx->handle_timeout)
  2179. return 0;
  2180. if (tcon->no_lease != ctx->no_lease)
  2181. return 0;
  2182. if (tcon->nodelete != ctx->nodelete)
  2183. return 0;
  2184. if (tcon->posix_extensions != ctx->linux_ext)
  2185. return 0;
  2186. return 1;
  2187. }
  2188. static struct cifs_tcon *
  2189. cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
  2190. {
  2191. struct cifs_tcon *tcon;
  2192. spin_lock(&cifs_tcp_ses_lock);
  2193. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  2194. spin_lock(&tcon->tc_lock);
  2195. if (!match_tcon(tcon, ctx)) {
  2196. spin_unlock(&tcon->tc_lock);
  2197. continue;
  2198. }
  2199. ++tcon->tc_count;
  2200. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  2201. netfs_trace_tcon_ref_get_find);
  2202. spin_unlock(&tcon->tc_lock);
  2203. spin_unlock(&cifs_tcp_ses_lock);
  2204. return tcon;
  2205. }
  2206. spin_unlock(&cifs_tcp_ses_lock);
  2207. return NULL;
  2208. }
  2209. void
  2210. cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace)
  2211. {
  2212. unsigned int xid;
  2213. struct cifs_ses *ses;
  2214. LIST_HEAD(ses_list);
  2215. /*
  2216. * IPC tcon share the lifetime of their session and are
  2217. * destroyed in the session put function
  2218. */
  2219. if (tcon == NULL || tcon->ipc)
  2220. return;
  2221. ses = tcon->ses;
  2222. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  2223. spin_lock(&cifs_tcp_ses_lock);
  2224. spin_lock(&tcon->tc_lock);
  2225. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count - 1, trace);
  2226. if (--tcon->tc_count > 0) {
  2227. spin_unlock(&tcon->tc_lock);
  2228. spin_unlock(&cifs_tcp_ses_lock);
  2229. return;
  2230. }
  2231. /* tc_count can never go negative */
  2232. WARN_ON(tcon->tc_count < 0);
  2233. list_del_init(&tcon->tcon_list);
  2234. tcon->status = TID_EXITING;
  2235. spin_unlock(&tcon->tc_lock);
  2236. spin_unlock(&cifs_tcp_ses_lock);
  2237. /* cancel polling of interfaces */
  2238. cancel_delayed_work_sync(&tcon->query_interfaces);
  2239. #ifdef CONFIG_CIFS_DFS_UPCALL
  2240. cancel_delayed_work_sync(&tcon->dfs_cache_work);
  2241. list_replace_init(&tcon->dfs_ses_list, &ses_list);
  2242. #endif
  2243. if (tcon->use_witness) {
  2244. int rc;
  2245. rc = cifs_swn_unregister(tcon);
  2246. if (rc < 0) {
  2247. cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
  2248. __func__, rc);
  2249. }
  2250. }
  2251. xid = get_xid();
  2252. if (ses->server->ops->tree_disconnect)
  2253. ses->server->ops->tree_disconnect(xid, tcon);
  2254. _free_xid(xid);
  2255. cifs_fscache_release_super_cookie(tcon);
  2256. tconInfoFree(tcon, netfs_trace_tcon_ref_free);
  2257. cifs_put_smb_ses(ses);
  2258. #ifdef CONFIG_CIFS_DFS_UPCALL
  2259. dfs_put_root_smb_sessions(&ses_list);
  2260. #endif
  2261. }
  2262. /**
  2263. * cifs_get_tcon - get a tcon matching @ctx data from @ses
  2264. * @ses: smb session to issue the request on
  2265. * @ctx: the superblock configuration context to use for building the
  2266. *
  2267. * - tcon refcount is the number of mount points using the tcon.
  2268. * - ses refcount is the number of tcon using the session.
  2269. *
  2270. * 1. This function assumes it is being called from cifs_mount() where
  2271. * we already got a session reference (ses refcount +1).
  2272. *
  2273. * 2. Since we're in the context of adding a mount point, the end
  2274. * result should be either:
  2275. *
  2276. * a) a new tcon already allocated with refcount=1 (1 mount point) and
  2277. * its session refcount incremented (1 new tcon). This +1 was
  2278. * already done in (1).
  2279. *
  2280. * b) an existing tcon with refcount+1 (add a mount point to it) and
  2281. * identical ses refcount (no new tcon). Because of (1) we need to
  2282. * decrement the ses refcount.
  2283. */
  2284. static struct cifs_tcon *
  2285. cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
  2286. {
  2287. struct cifs_tcon *tcon;
  2288. bool nohandlecache;
  2289. int rc, xid;
  2290. tcon = cifs_find_tcon(ses, ctx);
  2291. if (tcon) {
  2292. /*
  2293. * tcon has refcount already incremented but we need to
  2294. * decrement extra ses reference gotten by caller (case b)
  2295. */
  2296. cifs_dbg(FYI, "Found match on UNC path\n");
  2297. cifs_put_smb_ses(ses);
  2298. return tcon;
  2299. }
  2300. if (!ses->server->ops->tree_connect) {
  2301. rc = -ENOSYS;
  2302. goto out_fail;
  2303. }
  2304. if (ses->server->dialect >= SMB20_PROT_ID &&
  2305. (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING))
  2306. nohandlecache = ctx->nohandlecache || !dir_cache_timeout;
  2307. else
  2308. nohandlecache = true;
  2309. tcon = tcon_info_alloc(!nohandlecache, netfs_trace_tcon_ref_new);
  2310. if (tcon == NULL) {
  2311. rc = -ENOMEM;
  2312. goto out_fail;
  2313. }
  2314. tcon->nohandlecache = nohandlecache;
  2315. if (ctx->snapshot_time) {
  2316. if (ses->server->vals->protocol_id == 0) {
  2317. cifs_dbg(VFS,
  2318. "Use SMB2 or later for snapshot mount option\n");
  2319. rc = -EOPNOTSUPP;
  2320. goto out_fail;
  2321. } else
  2322. tcon->snapshot_time = ctx->snapshot_time;
  2323. }
  2324. if (ctx->handle_timeout) {
  2325. if (ses->server->vals->protocol_id == 0) {
  2326. cifs_dbg(VFS,
  2327. "Use SMB2.1 or later for handle timeout option\n");
  2328. rc = -EOPNOTSUPP;
  2329. goto out_fail;
  2330. } else
  2331. tcon->handle_timeout = ctx->handle_timeout;
  2332. }
  2333. tcon->ses = ses;
  2334. if (ctx->password) {
  2335. tcon->password = kstrdup(ctx->password, GFP_KERNEL);
  2336. if (!tcon->password) {
  2337. rc = -ENOMEM;
  2338. goto out_fail;
  2339. }
  2340. }
  2341. if (ctx->seal) {
  2342. if (ses->server->vals->protocol_id == 0) {
  2343. cifs_dbg(VFS,
  2344. "SMB3 or later required for encryption\n");
  2345. rc = -EOPNOTSUPP;
  2346. goto out_fail;
  2347. } else if (tcon->ses->server->capabilities &
  2348. SMB2_GLOBAL_CAP_ENCRYPTION)
  2349. tcon->seal = true;
  2350. else {
  2351. cifs_dbg(VFS, "Encryption is not supported on share\n");
  2352. rc = -EOPNOTSUPP;
  2353. goto out_fail;
  2354. }
  2355. }
  2356. if (ctx->linux_ext) {
  2357. if (ses->server->posix_ext_supported) {
  2358. tcon->posix_extensions = true;
  2359. pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
  2360. } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
  2361. (strcmp(ses->server->vals->version_string,
  2362. SMB3ANY_VERSION_STRING) == 0) ||
  2363. (strcmp(ses->server->vals->version_string,
  2364. SMBDEFAULT_VERSION_STRING) == 0)) {
  2365. cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
  2366. rc = -EOPNOTSUPP;
  2367. goto out_fail;
  2368. } else if (ses->server->vals->protocol_id == SMB10_PROT_ID)
  2369. if (cap_unix(ses))
  2370. cifs_dbg(FYI, "Unix Extensions requested on SMB1 mount\n");
  2371. else {
  2372. cifs_dbg(VFS, "SMB1 Unix Extensions not supported by server\n");
  2373. rc = -EOPNOTSUPP;
  2374. goto out_fail;
  2375. } else {
  2376. cifs_dbg(VFS,
  2377. "Check vers= mount option. SMB3.11 disabled but required for POSIX extensions\n");
  2378. rc = -EOPNOTSUPP;
  2379. goto out_fail;
  2380. }
  2381. }
  2382. xid = get_xid();
  2383. rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
  2384. ctx->local_nls);
  2385. free_xid(xid);
  2386. cifs_dbg(FYI, "Tcon rc = %d\n", rc);
  2387. if (rc)
  2388. goto out_fail;
  2389. tcon->use_persistent = false;
  2390. /* check if SMB2 or later, CIFS does not support persistent handles */
  2391. if (ctx->persistent) {
  2392. if (ses->server->vals->protocol_id == 0) {
  2393. cifs_dbg(VFS,
  2394. "SMB3 or later required for persistent handles\n");
  2395. rc = -EOPNOTSUPP;
  2396. goto out_fail;
  2397. } else if (ses->server->capabilities &
  2398. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2399. tcon->use_persistent = true;
  2400. else /* persistent handles requested but not supported */ {
  2401. cifs_dbg(VFS,
  2402. "Persistent handles not supported on share\n");
  2403. rc = -EOPNOTSUPP;
  2404. goto out_fail;
  2405. }
  2406. } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  2407. && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2408. && (ctx->nopersistent == false)) {
  2409. cifs_dbg(FYI, "enabling persistent handles\n");
  2410. tcon->use_persistent = true;
  2411. } else if (ctx->resilient) {
  2412. if (ses->server->vals->protocol_id == 0) {
  2413. cifs_dbg(VFS,
  2414. "SMB2.1 or later required for resilient handles\n");
  2415. rc = -EOPNOTSUPP;
  2416. goto out_fail;
  2417. }
  2418. tcon->use_resilient = true;
  2419. }
  2420. tcon->use_witness = false;
  2421. if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
  2422. if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
  2423. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
  2424. /*
  2425. * Set witness in use flag in first place
  2426. * to retry registration in the echo task
  2427. */
  2428. tcon->use_witness = true;
  2429. /* And try to register immediately */
  2430. rc = cifs_swn_register(tcon);
  2431. if (rc < 0) {
  2432. cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
  2433. goto out_fail;
  2434. }
  2435. } else {
  2436. /* TODO: try to extend for non-cluster uses (eg multichannel) */
  2437. cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
  2438. rc = -EOPNOTSUPP;
  2439. goto out_fail;
  2440. }
  2441. } else {
  2442. cifs_dbg(VFS, "SMB3 or later required for witness option\n");
  2443. rc = -EOPNOTSUPP;
  2444. goto out_fail;
  2445. }
  2446. }
  2447. /* If the user really knows what they are doing they can override */
  2448. if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
  2449. if (ctx->cache_ro)
  2450. cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
  2451. else if (ctx->cache_rw)
  2452. cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
  2453. }
  2454. if (ctx->no_lease) {
  2455. if (ses->server->vals->protocol_id == 0) {
  2456. cifs_dbg(VFS,
  2457. "SMB2 or later required for nolease option\n");
  2458. rc = -EOPNOTSUPP;
  2459. goto out_fail;
  2460. } else
  2461. tcon->no_lease = ctx->no_lease;
  2462. }
  2463. /*
  2464. * We can have only one retry value for a connection to a share so for
  2465. * resources mounted more than once to the same server share the last
  2466. * value passed in for the retry flag is used.
  2467. */
  2468. tcon->retry = ctx->retry;
  2469. tcon->nocase = ctx->nocase;
  2470. tcon->broken_sparse_sup = ctx->no_sparse;
  2471. tcon->max_cached_dirs = ctx->max_cached_dirs;
  2472. tcon->nodelete = ctx->nodelete;
  2473. tcon->local_lease = ctx->local_lease;
  2474. tcon->status = TID_GOOD;
  2475. if (ses->server->dialect >= SMB30_PROT_ID &&
  2476. (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
  2477. /* schedule query interfaces poll */
  2478. queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
  2479. (SMB_INTERFACE_POLL_INTERVAL * HZ));
  2480. }
  2481. spin_lock(&cifs_tcp_ses_lock);
  2482. list_add(&tcon->tcon_list, &ses->tcon_list);
  2483. spin_unlock(&cifs_tcp_ses_lock);
  2484. return tcon;
  2485. out_fail:
  2486. tconInfoFree(tcon, netfs_trace_tcon_ref_free_fail);
  2487. return ERR_PTR(rc);
  2488. }
  2489. void
  2490. cifs_put_tlink(struct tcon_link *tlink)
  2491. {
  2492. if (!tlink || IS_ERR(tlink))
  2493. return;
  2494. if (!atomic_dec_and_test(&tlink->tl_count) ||
  2495. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  2496. tlink->tl_time = jiffies;
  2497. return;
  2498. }
  2499. if (!IS_ERR(tlink_tcon(tlink)))
  2500. cifs_put_tcon(tlink_tcon(tlink), netfs_trace_tcon_ref_put_tlink);
  2501. kfree(tlink);
  2502. }
  2503. static int
  2504. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  2505. {
  2506. struct cifs_sb_info *old = CIFS_SB(sb);
  2507. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2508. unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
  2509. unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
  2510. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  2511. return 0;
  2512. if (old->mnt_cifs_serverino_autodisabled)
  2513. newflags &= ~CIFS_MOUNT_SERVER_INUM;
  2514. if (oldflags != newflags)
  2515. return 0;
  2516. /*
  2517. * We want to share sb only if we don't specify an r/wsize or
  2518. * specified r/wsize is greater than or equal to existing one.
  2519. */
  2520. if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
  2521. return 0;
  2522. if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
  2523. return 0;
  2524. if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
  2525. !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
  2526. return 0;
  2527. if (old->ctx->file_mode != new->ctx->file_mode ||
  2528. old->ctx->dir_mode != new->ctx->dir_mode)
  2529. return 0;
  2530. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  2531. return 0;
  2532. if (old->ctx->acregmax != new->ctx->acregmax)
  2533. return 0;
  2534. if (old->ctx->acdirmax != new->ctx->acdirmax)
  2535. return 0;
  2536. if (old->ctx->closetimeo != new->ctx->closetimeo)
  2537. return 0;
  2538. if (old->ctx->reparse_type != new->ctx->reparse_type)
  2539. return 0;
  2540. return 1;
  2541. }
  2542. static int match_prepath(struct super_block *sb,
  2543. struct cifs_tcon *tcon,
  2544. struct cifs_mnt_data *mnt_data)
  2545. {
  2546. struct smb3_fs_context *ctx = mnt_data->ctx;
  2547. struct cifs_sb_info *old = CIFS_SB(sb);
  2548. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2549. bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
  2550. old->prepath;
  2551. bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
  2552. new->prepath;
  2553. if (tcon->origin_fullpath &&
  2554. dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source))
  2555. return 1;
  2556. if (old_set && new_set && !strcmp(new->prepath, old->prepath))
  2557. return 1;
  2558. else if (!old_set && !new_set)
  2559. return 1;
  2560. return 0;
  2561. }
  2562. int
  2563. cifs_match_super(struct super_block *sb, void *data)
  2564. {
  2565. struct cifs_mnt_data *mnt_data = data;
  2566. struct smb3_fs_context *ctx;
  2567. struct cifs_sb_info *cifs_sb;
  2568. struct TCP_Server_Info *tcp_srv;
  2569. struct cifs_ses *ses;
  2570. struct cifs_tcon *tcon;
  2571. struct tcon_link *tlink;
  2572. int rc = 0;
  2573. spin_lock(&cifs_tcp_ses_lock);
  2574. cifs_sb = CIFS_SB(sb);
  2575. /* We do not want to use a superblock that has been shutdown */
  2576. if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
  2577. spin_unlock(&cifs_tcp_ses_lock);
  2578. return 0;
  2579. }
  2580. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  2581. if (IS_ERR_OR_NULL(tlink)) {
  2582. pr_warn_once("%s: skip super matching due to bad tlink(%p)\n",
  2583. __func__, tlink);
  2584. spin_unlock(&cifs_tcp_ses_lock);
  2585. return 0;
  2586. }
  2587. tcon = tlink_tcon(tlink);
  2588. ses = tcon->ses;
  2589. tcp_srv = ses->server;
  2590. ctx = mnt_data->ctx;
  2591. spin_lock(&tcp_srv->srv_lock);
  2592. spin_lock(&ses->ses_lock);
  2593. spin_lock(&ses->chan_lock);
  2594. spin_lock(&tcon->tc_lock);
  2595. if (!match_server(tcp_srv, ctx, true) ||
  2596. !match_session(ses, ctx, true) ||
  2597. !match_tcon(tcon, ctx) ||
  2598. !match_prepath(sb, tcon, mnt_data)) {
  2599. rc = 0;
  2600. goto out;
  2601. }
  2602. rc = compare_mount_options(sb, mnt_data);
  2603. out:
  2604. spin_unlock(&tcon->tc_lock);
  2605. spin_unlock(&ses->chan_lock);
  2606. spin_unlock(&ses->ses_lock);
  2607. spin_unlock(&tcp_srv->srv_lock);
  2608. spin_unlock(&cifs_tcp_ses_lock);
  2609. cifs_put_tlink(tlink);
  2610. return rc;
  2611. }
  2612. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2613. static struct lock_class_key cifs_key[2];
  2614. static struct lock_class_key cifs_slock_key[2];
  2615. static inline void
  2616. cifs_reclassify_socket4(struct socket *sock)
  2617. {
  2618. struct sock *sk = sock->sk;
  2619. BUG_ON(!sock_allow_reclassification(sk));
  2620. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  2621. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  2622. }
  2623. static inline void
  2624. cifs_reclassify_socket6(struct socket *sock)
  2625. {
  2626. struct sock *sk = sock->sk;
  2627. BUG_ON(!sock_allow_reclassification(sk));
  2628. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  2629. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  2630. }
  2631. #else
  2632. static inline void
  2633. cifs_reclassify_socket4(struct socket *sock)
  2634. {
  2635. }
  2636. static inline void
  2637. cifs_reclassify_socket6(struct socket *sock)
  2638. {
  2639. }
  2640. #endif
  2641. /* See RFC1001 section 14 on representation of Netbios names */
  2642. static void rfc1002mangle(char *target, char *source, unsigned int length)
  2643. {
  2644. unsigned int i, j;
  2645. for (i = 0, j = 0; i < (length); i++) {
  2646. /* mask a nibble at a time and encode */
  2647. target[j] = 'A' + (0x0F & (source[i] >> 4));
  2648. target[j+1] = 'A' + (0x0F & source[i]);
  2649. j += 2;
  2650. }
  2651. }
  2652. static int
  2653. bind_socket(struct TCP_Server_Info *server)
  2654. {
  2655. int rc = 0;
  2656. if (server->srcaddr.ss_family != AF_UNSPEC) {
  2657. /* Bind to the specified local IP address */
  2658. struct socket *socket = server->ssocket;
  2659. rc = kernel_bind(socket,
  2660. (struct sockaddr *) &server->srcaddr,
  2661. sizeof(server->srcaddr));
  2662. if (rc < 0) {
  2663. struct sockaddr_in *saddr4;
  2664. struct sockaddr_in6 *saddr6;
  2665. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  2666. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  2667. if (saddr6->sin6_family == AF_INET6)
  2668. cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
  2669. &saddr6->sin6_addr, rc);
  2670. else
  2671. cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
  2672. &saddr4->sin_addr.s_addr, rc);
  2673. }
  2674. }
  2675. return rc;
  2676. }
  2677. static int
  2678. ip_rfc1001_connect(struct TCP_Server_Info *server)
  2679. {
  2680. int rc = 0;
  2681. /*
  2682. * some servers require RFC1001 sessinit before sending
  2683. * negprot - BB check reconnection in case where second
  2684. * sessinit is sent but no second negprot
  2685. */
  2686. struct rfc1002_session_packet req = {};
  2687. struct msghdr msg = {};
  2688. struct kvec iov = {};
  2689. unsigned int len;
  2690. size_t sent;
  2691. req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
  2692. if (server->server_RFC1001_name[0] != 0)
  2693. rfc1002mangle(req.trailer.session_req.called_name,
  2694. server->server_RFC1001_name,
  2695. RFC1001_NAME_LEN_WITH_NULL);
  2696. else
  2697. rfc1002mangle(req.trailer.session_req.called_name,
  2698. DEFAULT_CIFS_CALLED_NAME,
  2699. RFC1001_NAME_LEN_WITH_NULL);
  2700. req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
  2701. /* calling name ends in null (byte 16) from old smb convention */
  2702. if (server->workstation_RFC1001_name[0] != 0)
  2703. rfc1002mangle(req.trailer.session_req.calling_name,
  2704. server->workstation_RFC1001_name,
  2705. RFC1001_NAME_LEN_WITH_NULL);
  2706. else
  2707. rfc1002mangle(req.trailer.session_req.calling_name,
  2708. "LINUX_CIFS_CLNT",
  2709. RFC1001_NAME_LEN_WITH_NULL);
  2710. /*
  2711. * As per rfc1002, @len must be the number of bytes that follows the
  2712. * length field of a rfc1002 session request payload.
  2713. */
  2714. len = sizeof(req.trailer.session_req);
  2715. req.type = RFC1002_SESSION_REQUEST;
  2716. req.flags = 0;
  2717. req.length = cpu_to_be16(len);
  2718. len += offsetof(typeof(req), trailer.session_req);
  2719. iov.iov_base = &req;
  2720. iov.iov_len = len;
  2721. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, len);
  2722. rc = smb_send_kvec(server, &msg, &sent);
  2723. if (rc < 0 || len != sent)
  2724. return (rc == -EINTR || rc == -EAGAIN) ? rc : -ECONNABORTED;
  2725. /*
  2726. * RFC1001 layer in at least one server requires very short break before
  2727. * negprot presumably because not expecting negprot to follow so fast.
  2728. * This is a simple solution that works without complicating the code
  2729. * and causes no significant slowing down on mount for everyone else
  2730. */
  2731. usleep_range(1000, 2000);
  2732. return 0;
  2733. }
  2734. static int
  2735. generic_ip_connect(struct TCP_Server_Info *server)
  2736. {
  2737. struct sockaddr *saddr;
  2738. struct socket *socket;
  2739. int slen, sfamily;
  2740. __be16 sport;
  2741. int rc = 0;
  2742. saddr = (struct sockaddr *) &server->dstaddr;
  2743. if (server->dstaddr.ss_family == AF_INET6) {
  2744. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
  2745. sport = ipv6->sin6_port;
  2746. slen = sizeof(struct sockaddr_in6);
  2747. sfamily = AF_INET6;
  2748. cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
  2749. ntohs(sport));
  2750. } else {
  2751. struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
  2752. sport = ipv4->sin_port;
  2753. slen = sizeof(struct sockaddr_in);
  2754. sfamily = AF_INET;
  2755. cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
  2756. ntohs(sport));
  2757. }
  2758. if (server->ssocket) {
  2759. socket = server->ssocket;
  2760. } else {
  2761. struct net *net = cifs_net_ns(server);
  2762. struct sock *sk;
  2763. rc = sock_create_kern(net, sfamily, SOCK_STREAM,
  2764. IPPROTO_TCP, &server->ssocket);
  2765. if (rc < 0) {
  2766. cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
  2767. return rc;
  2768. }
  2769. sk = server->ssocket->sk;
  2770. sk_net_refcnt_upgrade(sk);
  2771. /* BB other socket options to set KEEPALIVE, NODELAY? */
  2772. cifs_dbg(FYI, "Socket created\n");
  2773. socket = server->ssocket;
  2774. socket->sk->sk_allocation = GFP_NOFS;
  2775. socket->sk->sk_use_task_frag = false;
  2776. if (sfamily == AF_INET6)
  2777. cifs_reclassify_socket6(socket);
  2778. else
  2779. cifs_reclassify_socket4(socket);
  2780. }
  2781. rc = bind_socket(server);
  2782. if (rc < 0)
  2783. return rc;
  2784. /*
  2785. * Eventually check for other socket options to change from
  2786. * the default. sock_setsockopt not used because it expects
  2787. * user space buffer
  2788. */
  2789. socket->sk->sk_rcvtimeo = 7 * HZ;
  2790. socket->sk->sk_sndtimeo = 5 * HZ;
  2791. /* make the bufsizes depend on wsize/rsize and max requests */
  2792. if (server->noautotune) {
  2793. if (socket->sk->sk_sndbuf < (200 * 1024))
  2794. socket->sk->sk_sndbuf = 200 * 1024;
  2795. if (socket->sk->sk_rcvbuf < (140 * 1024))
  2796. socket->sk->sk_rcvbuf = 140 * 1024;
  2797. }
  2798. if (server->tcp_nodelay)
  2799. tcp_sock_set_nodelay(socket->sk);
  2800. cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
  2801. socket->sk->sk_sndbuf,
  2802. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  2803. rc = kernel_connect(socket, saddr, slen,
  2804. server->noblockcnt ? O_NONBLOCK : 0);
  2805. /*
  2806. * When mounting SMB root file systems, we do not want to block in
  2807. * connect. Otherwise bail out and then let cifs_reconnect() perform
  2808. * reconnect failover - if possible.
  2809. */
  2810. if (server->noblockcnt && rc == -EINPROGRESS)
  2811. rc = 0;
  2812. if (rc < 0) {
  2813. cifs_dbg(FYI, "Error %d connecting to server\n", rc);
  2814. trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
  2815. sock_release(socket);
  2816. server->ssocket = NULL;
  2817. return rc;
  2818. }
  2819. trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
  2820. if (sport == htons(RFC1001_PORT))
  2821. rc = ip_rfc1001_connect(server);
  2822. return rc;
  2823. }
  2824. static int
  2825. ip_connect(struct TCP_Server_Info *server)
  2826. {
  2827. __be16 *sport;
  2828. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2829. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2830. if (server->dstaddr.ss_family == AF_INET6)
  2831. sport = &addr6->sin6_port;
  2832. else
  2833. sport = &addr->sin_port;
  2834. if (*sport == 0) {
  2835. int rc;
  2836. /* try with 445 port at first */
  2837. *sport = htons(CIFS_PORT);
  2838. rc = generic_ip_connect(server);
  2839. if (rc >= 0)
  2840. return rc;
  2841. /* if it failed, try with 139 port */
  2842. *sport = htons(RFC1001_PORT);
  2843. }
  2844. return generic_ip_connect(server);
  2845. }
  2846. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  2847. void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
  2848. struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
  2849. {
  2850. /*
  2851. * If we are reconnecting then should we check to see if
  2852. * any requested capabilities changed locally e.g. via
  2853. * remount but we can not do much about it here
  2854. * if they have (even if we could detect it by the following)
  2855. * Perhaps we could add a backpointer to array of sb from tcon
  2856. * or if we change to make all sb to same share the same
  2857. * sb as NFS - then we only have one backpointer to sb.
  2858. * What if we wanted to mount the server share twice once with
  2859. * and once without posixacls or posix paths?
  2860. */
  2861. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2862. if (ctx && ctx->no_linux_ext) {
  2863. tcon->fsUnixInfo.Capability = 0;
  2864. tcon->unix_ext = 0; /* Unix Extensions disabled */
  2865. cifs_dbg(FYI, "Linux protocol extensions disabled\n");
  2866. return;
  2867. } else if (ctx)
  2868. tcon->unix_ext = 1; /* Unix Extensions supported */
  2869. if (!tcon->unix_ext) {
  2870. cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
  2871. return;
  2872. }
  2873. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  2874. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2875. cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
  2876. /*
  2877. * check for reconnect case in which we do not
  2878. * want to change the mount behavior if we can avoid it
  2879. */
  2880. if (ctx == NULL) {
  2881. /*
  2882. * turn off POSIX ACL and PATHNAMES if not set
  2883. * originally at mount time
  2884. */
  2885. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  2886. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2887. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2888. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2889. cifs_dbg(VFS, "POSIXPATH support change\n");
  2890. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2891. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2892. cifs_dbg(VFS, "possible reconnect error\n");
  2893. cifs_dbg(VFS, "server disabled POSIX path support\n");
  2894. }
  2895. }
  2896. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2897. cifs_dbg(VFS, "per-share encryption not supported yet\n");
  2898. cap &= CIFS_UNIX_CAP_MASK;
  2899. if (ctx && ctx->no_psx_acl)
  2900. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2901. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  2902. cifs_dbg(FYI, "negotiated posix acl support\n");
  2903. if (cifs_sb)
  2904. cifs_sb->mnt_cifs_flags |=
  2905. CIFS_MOUNT_POSIXACL;
  2906. }
  2907. if (ctx && ctx->posix_paths == 0)
  2908. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2909. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  2910. cifs_dbg(FYI, "negotiate posix pathnames\n");
  2911. if (cifs_sb)
  2912. cifs_sb->mnt_cifs_flags |=
  2913. CIFS_MOUNT_POSIX_PATHS;
  2914. }
  2915. cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
  2916. #ifdef CONFIG_CIFS_DEBUG2
  2917. if (cap & CIFS_UNIX_FCNTL_CAP)
  2918. cifs_dbg(FYI, "FCNTL cap\n");
  2919. if (cap & CIFS_UNIX_EXTATTR_CAP)
  2920. cifs_dbg(FYI, "EXTATTR cap\n");
  2921. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2922. cifs_dbg(FYI, "POSIX path cap\n");
  2923. if (cap & CIFS_UNIX_XATTR_CAP)
  2924. cifs_dbg(FYI, "XATTR cap\n");
  2925. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  2926. cifs_dbg(FYI, "POSIX ACL cap\n");
  2927. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  2928. cifs_dbg(FYI, "very large read cap\n");
  2929. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  2930. cifs_dbg(FYI, "very large write cap\n");
  2931. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  2932. cifs_dbg(FYI, "transport encryption cap\n");
  2933. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2934. cifs_dbg(FYI, "mandatory transport encryption cap\n");
  2935. #endif /* CIFS_DEBUG2 */
  2936. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  2937. if (ctx == NULL)
  2938. cifs_dbg(FYI, "resetting capabilities failed\n");
  2939. else
  2940. cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
  2941. }
  2942. }
  2943. }
  2944. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  2945. int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
  2946. {
  2947. struct smb3_fs_context *ctx = cifs_sb->ctx;
  2948. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  2949. spin_lock_init(&cifs_sb->tlink_tree_lock);
  2950. cifs_sb->tlink_tree = RB_ROOT;
  2951. cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
  2952. ctx->file_mode, ctx->dir_mode);
  2953. /* this is needed for ASCII cp to Unicode converts */
  2954. if (ctx->iocharset == NULL) {
  2955. /* load_nls_default cannot return null */
  2956. cifs_sb->local_nls = load_nls_default();
  2957. } else {
  2958. cifs_sb->local_nls = load_nls(ctx->iocharset);
  2959. if (cifs_sb->local_nls == NULL) {
  2960. cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
  2961. ctx->iocharset);
  2962. return -ELIBACC;
  2963. }
  2964. }
  2965. ctx->local_nls = cifs_sb->local_nls;
  2966. smb3_update_mnt_flags(cifs_sb);
  2967. if (ctx->direct_io)
  2968. cifs_dbg(FYI, "mounting share using direct i/o\n");
  2969. if (ctx->cache_ro) {
  2970. cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
  2971. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
  2972. } else if (ctx->cache_rw) {
  2973. cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
  2974. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
  2975. CIFS_MOUNT_RW_CACHE);
  2976. }
  2977. if ((ctx->cifs_acl) && (ctx->dynperm))
  2978. cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
  2979. if (ctx->prepath) {
  2980. cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
  2981. if (cifs_sb->prepath == NULL)
  2982. return -ENOMEM;
  2983. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  2984. }
  2985. return 0;
  2986. }
  2987. /* Release all succeed connections */
  2988. void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
  2989. {
  2990. int rc = 0;
  2991. if (mnt_ctx->tcon)
  2992. cifs_put_tcon(mnt_ctx->tcon, netfs_trace_tcon_ref_put_mnt_ctx);
  2993. else if (mnt_ctx->ses)
  2994. cifs_put_smb_ses(mnt_ctx->ses);
  2995. else if (mnt_ctx->server)
  2996. cifs_put_tcp_session(mnt_ctx->server, 0);
  2997. mnt_ctx->ses = NULL;
  2998. mnt_ctx->tcon = NULL;
  2999. mnt_ctx->server = NULL;
  3000. mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
  3001. free_xid(mnt_ctx->xid);
  3002. }
  3003. int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
  3004. {
  3005. struct TCP_Server_Info *server = NULL;
  3006. struct smb3_fs_context *ctx;
  3007. struct cifs_ses *ses = NULL;
  3008. unsigned int xid;
  3009. int rc = 0;
  3010. xid = get_xid();
  3011. if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
  3012. rc = -EINVAL;
  3013. goto out;
  3014. }
  3015. ctx = mnt_ctx->fs_ctx;
  3016. /* get a reference to a tcp session */
  3017. server = cifs_get_tcp_session(ctx, NULL);
  3018. if (IS_ERR(server)) {
  3019. rc = PTR_ERR(server);
  3020. server = NULL;
  3021. goto out;
  3022. }
  3023. /* get a reference to a SMB session */
  3024. ses = cifs_get_smb_ses(server, ctx);
  3025. if (IS_ERR(ses)) {
  3026. rc = PTR_ERR(ses);
  3027. ses = NULL;
  3028. goto out;
  3029. }
  3030. if ((ctx->persistent == true) && (!(ses->server->capabilities &
  3031. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
  3032. cifs_server_dbg(VFS, "persistent handles not supported by server\n");
  3033. rc = -EOPNOTSUPP;
  3034. }
  3035. out:
  3036. mnt_ctx->xid = xid;
  3037. mnt_ctx->server = server;
  3038. mnt_ctx->ses = ses;
  3039. mnt_ctx->tcon = NULL;
  3040. return rc;
  3041. }
  3042. int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
  3043. {
  3044. struct TCP_Server_Info *server;
  3045. struct cifs_sb_info *cifs_sb;
  3046. struct smb3_fs_context *ctx;
  3047. struct cifs_tcon *tcon = NULL;
  3048. int rc = 0;
  3049. if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
  3050. !mnt_ctx->cifs_sb)) {
  3051. rc = -EINVAL;
  3052. goto out;
  3053. }
  3054. server = mnt_ctx->server;
  3055. ctx = mnt_ctx->fs_ctx;
  3056. cifs_sb = mnt_ctx->cifs_sb;
  3057. /* search for existing tcon to this server share */
  3058. tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
  3059. if (IS_ERR(tcon)) {
  3060. rc = PTR_ERR(tcon);
  3061. tcon = NULL;
  3062. goto out;
  3063. }
  3064. /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
  3065. if (tcon->posix_extensions)
  3066. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
  3067. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  3068. /* tell server which Unix caps we support */
  3069. if (cap_unix(tcon->ses)) {
  3070. /*
  3071. * reset of caps checks mount to see if unix extensions disabled
  3072. * for just this mount.
  3073. */
  3074. reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
  3075. spin_lock(&tcon->ses->server->srv_lock);
  3076. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  3077. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  3078. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
  3079. spin_unlock(&tcon->ses->server->srv_lock);
  3080. rc = -EACCES;
  3081. goto out;
  3082. }
  3083. spin_unlock(&tcon->ses->server->srv_lock);
  3084. } else
  3085. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  3086. tcon->unix_ext = 0; /* server does not support them */
  3087. /* do not care if a following call succeed - informational */
  3088. if (!tcon->pipe && server->ops->qfs_tcon) {
  3089. server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
  3090. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
  3091. if (tcon->fsDevInfo.DeviceCharacteristics &
  3092. cpu_to_le32(FILE_READ_ONLY_DEVICE))
  3093. cifs_dbg(VFS, "mounted to read only share\n");
  3094. else if ((cifs_sb->mnt_cifs_flags &
  3095. CIFS_MOUNT_RW_CACHE) == 0)
  3096. cifs_dbg(VFS, "read only mount of RW share\n");
  3097. /* no need to log a RW mount of a typical RW share */
  3098. }
  3099. }
  3100. /*
  3101. * Clamp the rsize/wsize mount arguments if they are too big for the server
  3102. * and set the rsize/wsize to the negotiated values if not passed in by
  3103. * the user on mount
  3104. */
  3105. if ((cifs_sb->ctx->wsize == 0) ||
  3106. (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) {
  3107. cifs_sb->ctx->wsize =
  3108. round_down(server->ops->negotiate_wsize(tcon, ctx), PAGE_SIZE);
  3109. /*
  3110. * in the very unlikely event that the server sent a max write size under PAGE_SIZE,
  3111. * (which would get rounded down to 0) then reset wsize to absolute minimum eg 4096
  3112. */
  3113. if (cifs_sb->ctx->wsize == 0) {
  3114. cifs_sb->ctx->wsize = PAGE_SIZE;
  3115. cifs_dbg(VFS, "wsize too small, reset to minimum ie PAGE_SIZE, usually 4096\n");
  3116. }
  3117. }
  3118. if ((cifs_sb->ctx->rsize == 0) ||
  3119. (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
  3120. cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
  3121. /*
  3122. * The cookie is initialized from volume info returned above.
  3123. * Inside cifs_fscache_get_super_cookie it checks
  3124. * that we do not get super cookie twice.
  3125. */
  3126. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
  3127. cifs_fscache_get_super_cookie(tcon);
  3128. out:
  3129. mnt_ctx->tcon = tcon;
  3130. return rc;
  3131. }
  3132. static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
  3133. struct cifs_tcon *tcon)
  3134. {
  3135. struct tcon_link *tlink;
  3136. /* hang the tcon off of the superblock */
  3137. tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3138. if (tlink == NULL)
  3139. return -ENOMEM;
  3140. tlink->tl_uid = ses->linux_uid;
  3141. tlink->tl_tcon = tcon;
  3142. tlink->tl_time = jiffies;
  3143. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  3144. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3145. cifs_sb->master_tlink = tlink;
  3146. spin_lock(&cifs_sb->tlink_tree_lock);
  3147. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3148. spin_unlock(&cifs_sb->tlink_tree_lock);
  3149. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  3150. TLINK_IDLE_EXPIRE);
  3151. return 0;
  3152. }
  3153. static int
  3154. cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
  3155. unsigned int xid,
  3156. struct cifs_tcon *tcon,
  3157. struct cifs_sb_info *cifs_sb,
  3158. char *full_path,
  3159. int added_treename)
  3160. {
  3161. int rc;
  3162. char *s;
  3163. char sep, tmp;
  3164. int skip = added_treename ? 1 : 0;
  3165. sep = CIFS_DIR_SEP(cifs_sb);
  3166. s = full_path;
  3167. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
  3168. while (rc == 0) {
  3169. /* skip separators */
  3170. while (*s == sep)
  3171. s++;
  3172. if (!*s)
  3173. break;
  3174. /* next separator */
  3175. while (*s && *s != sep)
  3176. s++;
  3177. /*
  3178. * if the treename is added, we then have to skip the first
  3179. * part within the separators
  3180. */
  3181. if (skip) {
  3182. skip = 0;
  3183. continue;
  3184. }
  3185. /*
  3186. * temporarily null-terminate the path at the end of
  3187. * the current component
  3188. */
  3189. tmp = *s;
  3190. *s = 0;
  3191. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  3192. full_path);
  3193. *s = tmp;
  3194. }
  3195. return rc;
  3196. }
  3197. /*
  3198. * Check if path is remote (i.e. a DFS share).
  3199. *
  3200. * Return -EREMOTE if it is, otherwise 0 or -errno.
  3201. */
  3202. int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
  3203. {
  3204. int rc;
  3205. struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
  3206. struct TCP_Server_Info *server = mnt_ctx->server;
  3207. unsigned int xid = mnt_ctx->xid;
  3208. struct cifs_tcon *tcon = mnt_ctx->tcon;
  3209. struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
  3210. char *full_path;
  3211. if (!server->ops->is_path_accessible)
  3212. return -EOPNOTSUPP;
  3213. /*
  3214. * cifs_build_path_to_root works only when we have a valid tcon
  3215. */
  3216. full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
  3217. tcon->Flags & SMB_SHARE_IS_IN_DFS);
  3218. if (full_path == NULL)
  3219. return -ENOMEM;
  3220. cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
  3221. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  3222. full_path);
  3223. if (rc != 0 && rc != -EREMOTE)
  3224. goto out;
  3225. if (rc != -EREMOTE) {
  3226. rc = cifs_are_all_path_components_accessible(server, xid, tcon,
  3227. cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
  3228. if (rc != 0) {
  3229. cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
  3230. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  3231. rc = 0;
  3232. }
  3233. }
  3234. out:
  3235. kfree(full_path);
  3236. return rc;
  3237. }
  3238. #ifdef CONFIG_CIFS_DFS_UPCALL
  3239. int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
  3240. {
  3241. struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
  3242. int rc;
  3243. rc = dfs_mount_share(&mnt_ctx);
  3244. if (rc)
  3245. goto error;
  3246. if (!ctx->dfs_conn)
  3247. goto out;
  3248. /*
  3249. * After reconnecting to a different server, unique ids won't match anymore, so we disable
  3250. * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
  3251. */
  3252. cifs_autodisable_serverino(cifs_sb);
  3253. /*
  3254. * Force the use of prefix path to support failover on DFS paths that resolve to targets
  3255. * that have different prefix paths.
  3256. */
  3257. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  3258. kfree(cifs_sb->prepath);
  3259. cifs_sb->prepath = ctx->prepath;
  3260. ctx->prepath = NULL;
  3261. out:
  3262. cifs_try_adding_channels(mnt_ctx.ses);
  3263. rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
  3264. if (rc)
  3265. goto error;
  3266. free_xid(mnt_ctx.xid);
  3267. return rc;
  3268. error:
  3269. cifs_mount_put_conns(&mnt_ctx);
  3270. return rc;
  3271. }
  3272. #else
  3273. int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
  3274. {
  3275. int rc = 0;
  3276. struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
  3277. rc = cifs_mount_get_session(&mnt_ctx);
  3278. if (rc)
  3279. goto error;
  3280. rc = cifs_mount_get_tcon(&mnt_ctx);
  3281. if (!rc) {
  3282. /*
  3283. * Prevent superblock from being created with any missing
  3284. * connections.
  3285. */
  3286. if (WARN_ON(!mnt_ctx.server))
  3287. rc = -EHOSTDOWN;
  3288. else if (WARN_ON(!mnt_ctx.ses))
  3289. rc = -EACCES;
  3290. else if (WARN_ON(!mnt_ctx.tcon))
  3291. rc = -ENOENT;
  3292. }
  3293. if (rc)
  3294. goto error;
  3295. rc = cifs_is_path_remote(&mnt_ctx);
  3296. if (rc == -EREMOTE)
  3297. rc = -EOPNOTSUPP;
  3298. if (rc)
  3299. goto error;
  3300. rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
  3301. if (rc)
  3302. goto error;
  3303. free_xid(mnt_ctx.xid);
  3304. return rc;
  3305. error:
  3306. cifs_mount_put_conns(&mnt_ctx);
  3307. return rc;
  3308. }
  3309. #endif
  3310. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  3311. /*
  3312. * Issue a TREE_CONNECT request.
  3313. */
  3314. int
  3315. CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
  3316. const char *tree, struct cifs_tcon *tcon,
  3317. const struct nls_table *nls_codepage)
  3318. {
  3319. struct smb_hdr *smb_buffer;
  3320. struct smb_hdr *smb_buffer_response;
  3321. TCONX_REQ *pSMB;
  3322. TCONX_RSP *pSMBr;
  3323. unsigned char *bcc_ptr;
  3324. int rc = 0;
  3325. int length;
  3326. __u16 bytes_left, count;
  3327. if (ses == NULL)
  3328. return -EIO;
  3329. smb_buffer = cifs_buf_get();
  3330. if (smb_buffer == NULL)
  3331. return -ENOMEM;
  3332. smb_buffer_response = smb_buffer;
  3333. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  3334. NULL /*no tid */, 4 /*wct */);
  3335. smb_buffer->Mid = get_next_mid(ses->server);
  3336. smb_buffer->Uid = ses->Suid;
  3337. pSMB = (TCONX_REQ *) smb_buffer;
  3338. pSMBr = (TCONX_RSP *) smb_buffer_response;
  3339. pSMB->AndXCommand = 0xFF;
  3340. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  3341. bcc_ptr = &pSMB->Password[0];
  3342. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  3343. *bcc_ptr = 0; /* password is null byte */
  3344. bcc_ptr++; /* skip password */
  3345. /* already aligned so no need to do it below */
  3346. if (ses->server->sign)
  3347. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  3348. if (ses->capabilities & CAP_STATUS32)
  3349. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  3350. if (ses->capabilities & CAP_DFS)
  3351. smb_buffer->Flags2 |= SMBFLG2_DFS;
  3352. if (ses->capabilities & CAP_UNICODE) {
  3353. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  3354. length =
  3355. cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
  3356. 6 /* max utf8 char length in bytes */ *
  3357. (/* server len*/ + 256 /* share len */), nls_codepage);
  3358. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  3359. bcc_ptr += 2; /* skip trailing null */
  3360. } else { /* ASCII */
  3361. strcpy(bcc_ptr, tree);
  3362. bcc_ptr += strlen(tree) + 1;
  3363. }
  3364. strcpy(bcc_ptr, "?????");
  3365. bcc_ptr += strlen("?????");
  3366. bcc_ptr += 1;
  3367. count = bcc_ptr - &pSMB->Password[0];
  3368. be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
  3369. pSMB->ByteCount = cpu_to_le16(count);
  3370. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  3371. 0);
  3372. /* above now done in SendReceive */
  3373. if (rc == 0) {
  3374. bool is_unicode;
  3375. tcon->tid = smb_buffer_response->Tid;
  3376. bcc_ptr = pByteArea(smb_buffer_response);
  3377. bytes_left = get_bcc(smb_buffer_response);
  3378. length = strnlen(bcc_ptr, bytes_left - 2);
  3379. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  3380. is_unicode = true;
  3381. else
  3382. is_unicode = false;
  3383. /* skip service field (NB: this field is always ASCII) */
  3384. if (length == 3) {
  3385. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  3386. (bcc_ptr[2] == 'C')) {
  3387. cifs_dbg(FYI, "IPC connection\n");
  3388. tcon->ipc = true;
  3389. tcon->pipe = true;
  3390. }
  3391. } else if (length == 2) {
  3392. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  3393. /* the most common case */
  3394. cifs_dbg(FYI, "disk share connection\n");
  3395. }
  3396. }
  3397. bcc_ptr += length + 1;
  3398. bytes_left -= (length + 1);
  3399. strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
  3400. /* mostly informational -- no need to fail on error here */
  3401. kfree(tcon->nativeFileSystem);
  3402. tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
  3403. bytes_left, is_unicode,
  3404. nls_codepage);
  3405. cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
  3406. if ((smb_buffer_response->WordCount == 3) ||
  3407. (smb_buffer_response->WordCount == 7))
  3408. /* field is in same location */
  3409. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  3410. else
  3411. tcon->Flags = 0;
  3412. cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
  3413. /*
  3414. * reset_cifs_unix_caps calls QFSInfo which requires
  3415. * need_reconnect to be false, but we would not need to call
  3416. * reset_caps if this were not a reconnect case so must check
  3417. * need_reconnect flag here. The caller will also clear
  3418. * need_reconnect when tcon was successful but needed to be
  3419. * cleared earlier in the case of unix extensions reconnect
  3420. */
  3421. if (tcon->need_reconnect && tcon->unix_ext) {
  3422. cifs_dbg(FYI, "resetting caps for %s\n", tcon->tree_name);
  3423. tcon->need_reconnect = false;
  3424. reset_cifs_unix_caps(xid, tcon, NULL, NULL);
  3425. }
  3426. }
  3427. cifs_buf_release(smb_buffer);
  3428. return rc;
  3429. }
  3430. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  3431. static void delayed_free(struct rcu_head *p)
  3432. {
  3433. struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
  3434. unload_nls(cifs_sb->local_nls);
  3435. smb3_cleanup_fs_context(cifs_sb->ctx);
  3436. kfree(cifs_sb);
  3437. }
  3438. void
  3439. cifs_umount(struct cifs_sb_info *cifs_sb)
  3440. {
  3441. struct rb_root *root = &cifs_sb->tlink_tree;
  3442. struct rb_node *node;
  3443. struct tcon_link *tlink;
  3444. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  3445. spin_lock(&cifs_sb->tlink_tree_lock);
  3446. while ((node = rb_first(root))) {
  3447. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3448. cifs_get_tlink(tlink);
  3449. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3450. rb_erase(node, root);
  3451. spin_unlock(&cifs_sb->tlink_tree_lock);
  3452. cifs_put_tlink(tlink);
  3453. spin_lock(&cifs_sb->tlink_tree_lock);
  3454. }
  3455. spin_unlock(&cifs_sb->tlink_tree_lock);
  3456. kfree(cifs_sb->prepath);
  3457. call_rcu(&cifs_sb->rcu, delayed_free);
  3458. }
  3459. int
  3460. cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
  3461. struct TCP_Server_Info *server)
  3462. {
  3463. bool in_retry = false;
  3464. int rc = 0;
  3465. if (!server->ops->need_neg || !server->ops->negotiate)
  3466. return -ENOSYS;
  3467. retry:
  3468. /* only send once per connect */
  3469. spin_lock(&server->srv_lock);
  3470. if (server->tcpStatus != CifsGood &&
  3471. server->tcpStatus != CifsNew &&
  3472. server->tcpStatus != CifsNeedNegotiate) {
  3473. spin_unlock(&server->srv_lock);
  3474. return -EHOSTDOWN;
  3475. }
  3476. if (!server->ops->need_neg(server) &&
  3477. server->tcpStatus == CifsGood) {
  3478. spin_unlock(&server->srv_lock);
  3479. return 0;
  3480. }
  3481. server->tcpStatus = CifsInNegotiate;
  3482. server->neg_start = jiffies;
  3483. spin_unlock(&server->srv_lock);
  3484. rc = server->ops->negotiate(xid, ses, server);
  3485. if (rc == -EAGAIN) {
  3486. /* Allow one retry attempt */
  3487. if (!in_retry) {
  3488. in_retry = true;
  3489. goto retry;
  3490. }
  3491. rc = -EHOSTDOWN;
  3492. }
  3493. if (rc == 0) {
  3494. spin_lock(&server->srv_lock);
  3495. if (server->tcpStatus == CifsInNegotiate)
  3496. server->tcpStatus = CifsGood;
  3497. else
  3498. rc = -EHOSTDOWN;
  3499. spin_unlock(&server->srv_lock);
  3500. } else {
  3501. spin_lock(&server->srv_lock);
  3502. if (server->tcpStatus == CifsInNegotiate)
  3503. server->tcpStatus = CifsNeedNegotiate;
  3504. spin_unlock(&server->srv_lock);
  3505. }
  3506. return rc;
  3507. }
  3508. int
  3509. cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
  3510. struct TCP_Server_Info *server,
  3511. struct nls_table *nls_info)
  3512. {
  3513. int rc = -ENOSYS;
  3514. struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  3515. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
  3516. struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
  3517. bool is_binding = false;
  3518. spin_lock(&ses->ses_lock);
  3519. cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
  3520. __func__, ses->chans_need_reconnect);
  3521. if (ses->ses_status != SES_GOOD &&
  3522. ses->ses_status != SES_NEW &&
  3523. ses->ses_status != SES_NEED_RECON) {
  3524. spin_unlock(&ses->ses_lock);
  3525. return -EHOSTDOWN;
  3526. }
  3527. /* only send once per connect */
  3528. spin_lock(&ses->chan_lock);
  3529. if (CIFS_ALL_CHANS_GOOD(ses)) {
  3530. if (ses->ses_status == SES_NEED_RECON)
  3531. ses->ses_status = SES_GOOD;
  3532. spin_unlock(&ses->chan_lock);
  3533. spin_unlock(&ses->ses_lock);
  3534. return 0;
  3535. }
  3536. cifs_chan_set_in_reconnect(ses, server);
  3537. is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
  3538. spin_unlock(&ses->chan_lock);
  3539. if (!is_binding) {
  3540. ses->ses_status = SES_IN_SETUP;
  3541. /* force iface_list refresh */
  3542. ses->iface_last_update = 0;
  3543. }
  3544. spin_unlock(&ses->ses_lock);
  3545. /* update ses ip_addr only for primary chan */
  3546. if (server == pserver) {
  3547. if (server->dstaddr.ss_family == AF_INET6)
  3548. scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
  3549. else
  3550. scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
  3551. }
  3552. if (!is_binding) {
  3553. ses->capabilities = server->capabilities;
  3554. if (!linuxExtEnabled)
  3555. ses->capabilities &= (~server->vals->cap_unix);
  3556. if (ses->auth_key.response) {
  3557. cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
  3558. ses->auth_key.response);
  3559. kfree_sensitive(ses->auth_key.response);
  3560. ses->auth_key.response = NULL;
  3561. ses->auth_key.len = 0;
  3562. }
  3563. }
  3564. cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
  3565. server->sec_mode, server->capabilities, server->timeAdj);
  3566. if (server->ops->sess_setup)
  3567. rc = server->ops->sess_setup(xid, ses, server, nls_info);
  3568. if (rc) {
  3569. cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
  3570. spin_lock(&ses->ses_lock);
  3571. if (ses->ses_status == SES_IN_SETUP)
  3572. ses->ses_status = SES_NEED_RECON;
  3573. spin_lock(&ses->chan_lock);
  3574. cifs_chan_clear_in_reconnect(ses, server);
  3575. spin_unlock(&ses->chan_lock);
  3576. spin_unlock(&ses->ses_lock);
  3577. } else {
  3578. spin_lock(&ses->ses_lock);
  3579. if (ses->ses_status == SES_IN_SETUP)
  3580. ses->ses_status = SES_GOOD;
  3581. spin_lock(&ses->chan_lock);
  3582. cifs_chan_clear_in_reconnect(ses, server);
  3583. cifs_chan_clear_need_reconnect(ses, server);
  3584. spin_unlock(&ses->chan_lock);
  3585. spin_unlock(&ses->ses_lock);
  3586. }
  3587. return rc;
  3588. }
  3589. static int
  3590. cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
  3591. {
  3592. ctx->sectype = ses->sectype;
  3593. /* krb5 is special, since we don't need username or pw */
  3594. if (ctx->sectype == Kerberos)
  3595. return 0;
  3596. return cifs_set_cifscreds(ctx, ses);
  3597. }
  3598. static struct cifs_tcon *
  3599. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
  3600. {
  3601. int rc;
  3602. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  3603. struct cifs_ses *ses;
  3604. struct cifs_tcon *tcon = NULL;
  3605. struct smb3_fs_context *ctx;
  3606. char *origin_fullpath = NULL;
  3607. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  3608. if (ctx == NULL)
  3609. return ERR_PTR(-ENOMEM);
  3610. ctx->local_nls = cifs_sb->local_nls;
  3611. ctx->linux_uid = fsuid;
  3612. ctx->cred_uid = fsuid;
  3613. ctx->UNC = master_tcon->tree_name;
  3614. ctx->retry = master_tcon->retry;
  3615. ctx->nocase = master_tcon->nocase;
  3616. ctx->nohandlecache = master_tcon->nohandlecache;
  3617. ctx->local_lease = master_tcon->local_lease;
  3618. ctx->no_lease = master_tcon->no_lease;
  3619. ctx->resilient = master_tcon->use_resilient;
  3620. ctx->persistent = master_tcon->use_persistent;
  3621. ctx->handle_timeout = master_tcon->handle_timeout;
  3622. ctx->no_linux_ext = !master_tcon->unix_ext;
  3623. ctx->linux_ext = master_tcon->posix_extensions;
  3624. ctx->sectype = master_tcon->ses->sectype;
  3625. ctx->sign = master_tcon->ses->sign;
  3626. ctx->seal = master_tcon->seal;
  3627. ctx->witness = master_tcon->use_witness;
  3628. ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses;
  3629. rc = cifs_set_vol_auth(ctx, master_tcon->ses);
  3630. if (rc) {
  3631. tcon = ERR_PTR(rc);
  3632. goto out;
  3633. }
  3634. /* get a reference for the same TCP session */
  3635. spin_lock(&cifs_tcp_ses_lock);
  3636. ++master_tcon->ses->server->srv_count;
  3637. spin_unlock(&cifs_tcp_ses_lock);
  3638. ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
  3639. if (IS_ERR(ses)) {
  3640. tcon = ERR_CAST(ses);
  3641. cifs_put_tcp_session(master_tcon->ses->server, 0);
  3642. goto out;
  3643. }
  3644. #ifdef CONFIG_CIFS_DFS_UPCALL
  3645. spin_lock(&master_tcon->tc_lock);
  3646. if (master_tcon->origin_fullpath) {
  3647. spin_unlock(&master_tcon->tc_lock);
  3648. origin_fullpath = dfs_get_path(cifs_sb, cifs_sb->ctx->source);
  3649. if (IS_ERR(origin_fullpath)) {
  3650. tcon = ERR_CAST(origin_fullpath);
  3651. origin_fullpath = NULL;
  3652. cifs_put_smb_ses(ses);
  3653. goto out;
  3654. }
  3655. } else {
  3656. spin_unlock(&master_tcon->tc_lock);
  3657. }
  3658. #endif
  3659. tcon = cifs_get_tcon(ses, ctx);
  3660. if (IS_ERR(tcon)) {
  3661. cifs_put_smb_ses(ses);
  3662. goto out;
  3663. }
  3664. #ifdef CONFIG_CIFS_DFS_UPCALL
  3665. if (origin_fullpath) {
  3666. spin_lock(&tcon->tc_lock);
  3667. tcon->origin_fullpath = origin_fullpath;
  3668. spin_unlock(&tcon->tc_lock);
  3669. origin_fullpath = NULL;
  3670. queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
  3671. dfs_cache_get_ttl() * HZ);
  3672. }
  3673. #endif
  3674. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  3675. if (cap_unix(ses))
  3676. reset_cifs_unix_caps(0, tcon, NULL, ctx);
  3677. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  3678. out:
  3679. kfree(ctx->username);
  3680. kfree_sensitive(ctx->password);
  3681. kfree(origin_fullpath);
  3682. kfree(ctx);
  3683. return tcon;
  3684. }
  3685. struct cifs_tcon *
  3686. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  3687. {
  3688. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  3689. }
  3690. /* find and return a tlink with given uid */
  3691. static struct tcon_link *
  3692. tlink_rb_search(struct rb_root *root, kuid_t uid)
  3693. {
  3694. struct rb_node *node = root->rb_node;
  3695. struct tcon_link *tlink;
  3696. while (node) {
  3697. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3698. if (uid_gt(tlink->tl_uid, uid))
  3699. node = node->rb_left;
  3700. else if (uid_lt(tlink->tl_uid, uid))
  3701. node = node->rb_right;
  3702. else
  3703. return tlink;
  3704. }
  3705. return NULL;
  3706. }
  3707. /* insert a tcon_link into the tree */
  3708. static void
  3709. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  3710. {
  3711. struct rb_node **new = &(root->rb_node), *parent = NULL;
  3712. struct tcon_link *tlink;
  3713. while (*new) {
  3714. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  3715. parent = *new;
  3716. if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
  3717. new = &((*new)->rb_left);
  3718. else
  3719. new = &((*new)->rb_right);
  3720. }
  3721. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  3722. rb_insert_color(&new_tlink->tl_rbnode, root);
  3723. }
  3724. /*
  3725. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  3726. * current task.
  3727. *
  3728. * If the superblock doesn't refer to a multiuser mount, then just return
  3729. * the master tcon for the mount.
  3730. *
  3731. * First, search the rbtree for an existing tcon for this fsuid. If one
  3732. * exists, then check to see if it's pending construction. If it is then wait
  3733. * for construction to complete. Once it's no longer pending, check to see if
  3734. * it failed and either return an error or retry construction, depending on
  3735. * the timeout.
  3736. *
  3737. * If one doesn't exist then insert a new tcon_link struct into the tree and
  3738. * try to construct a new one.
  3739. *
  3740. * REMEMBER to call cifs_put_tlink() after successful calls to cifs_sb_tlink,
  3741. * to avoid refcount issues
  3742. */
  3743. struct tcon_link *
  3744. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  3745. {
  3746. struct tcon_link *tlink, *newtlink;
  3747. kuid_t fsuid = current_fsuid();
  3748. int err;
  3749. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  3750. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  3751. spin_lock(&cifs_sb->tlink_tree_lock);
  3752. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3753. if (tlink)
  3754. cifs_get_tlink(tlink);
  3755. spin_unlock(&cifs_sb->tlink_tree_lock);
  3756. if (tlink == NULL) {
  3757. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3758. if (newtlink == NULL)
  3759. return ERR_PTR(-ENOMEM);
  3760. newtlink->tl_uid = fsuid;
  3761. newtlink->tl_tcon = ERR_PTR(-EACCES);
  3762. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  3763. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  3764. cifs_get_tlink(newtlink);
  3765. spin_lock(&cifs_sb->tlink_tree_lock);
  3766. /* was one inserted after previous search? */
  3767. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3768. if (tlink) {
  3769. cifs_get_tlink(tlink);
  3770. spin_unlock(&cifs_sb->tlink_tree_lock);
  3771. kfree(newtlink);
  3772. goto wait_for_construction;
  3773. }
  3774. tlink = newtlink;
  3775. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3776. spin_unlock(&cifs_sb->tlink_tree_lock);
  3777. } else {
  3778. wait_for_construction:
  3779. err = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  3780. TASK_INTERRUPTIBLE);
  3781. if (err) {
  3782. cifs_put_tlink(tlink);
  3783. return ERR_PTR(-ERESTARTSYS);
  3784. }
  3785. /* if it's good, return it */
  3786. if (!IS_ERR(tlink->tl_tcon))
  3787. return tlink;
  3788. /* return error if we tried this already recently */
  3789. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  3790. err = PTR_ERR(tlink->tl_tcon);
  3791. cifs_put_tlink(tlink);
  3792. return ERR_PTR(err);
  3793. }
  3794. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  3795. goto wait_for_construction;
  3796. }
  3797. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  3798. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  3799. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  3800. if (IS_ERR(tlink->tl_tcon)) {
  3801. err = PTR_ERR(tlink->tl_tcon);
  3802. if (err == -ENOKEY)
  3803. err = -EACCES;
  3804. cifs_put_tlink(tlink);
  3805. return ERR_PTR(err);
  3806. }
  3807. return tlink;
  3808. }
  3809. /*
  3810. * periodic workqueue job that scans tcon_tree for a superblock and closes
  3811. * out tcons.
  3812. */
  3813. static void
  3814. cifs_prune_tlinks(struct work_struct *work)
  3815. {
  3816. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  3817. prune_tlinks.work);
  3818. struct rb_root *root = &cifs_sb->tlink_tree;
  3819. struct rb_node *node;
  3820. struct rb_node *tmp;
  3821. struct tcon_link *tlink;
  3822. /*
  3823. * Because we drop the spinlock in the loop in order to put the tlink
  3824. * it's not guarded against removal of links from the tree. The only
  3825. * places that remove entries from the tree are this function and
  3826. * umounts. Because this function is non-reentrant and is canceled
  3827. * before umount can proceed, this is safe.
  3828. */
  3829. spin_lock(&cifs_sb->tlink_tree_lock);
  3830. node = rb_first(root);
  3831. while (node != NULL) {
  3832. tmp = node;
  3833. node = rb_next(tmp);
  3834. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  3835. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  3836. atomic_read(&tlink->tl_count) != 0 ||
  3837. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  3838. continue;
  3839. cifs_get_tlink(tlink);
  3840. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3841. rb_erase(tmp, root);
  3842. spin_unlock(&cifs_sb->tlink_tree_lock);
  3843. cifs_put_tlink(tlink);
  3844. spin_lock(&cifs_sb->tlink_tree_lock);
  3845. }
  3846. spin_unlock(&cifs_sb->tlink_tree_lock);
  3847. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  3848. TLINK_IDLE_EXPIRE);
  3849. }
  3850. #ifndef CONFIG_CIFS_DFS_UPCALL
  3851. int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
  3852. {
  3853. int rc;
  3854. const struct smb_version_operations *ops = tcon->ses->server->ops;
  3855. /* only send once per connect */
  3856. spin_lock(&tcon->tc_lock);
  3857. /* if tcon is marked for needing reconnect, update state */
  3858. if (tcon->need_reconnect)
  3859. tcon->status = TID_NEED_TCON;
  3860. if (tcon->status == TID_GOOD) {
  3861. spin_unlock(&tcon->tc_lock);
  3862. return 0;
  3863. }
  3864. if (tcon->status != TID_NEW &&
  3865. tcon->status != TID_NEED_TCON) {
  3866. spin_unlock(&tcon->tc_lock);
  3867. return -EHOSTDOWN;
  3868. }
  3869. tcon->status = TID_IN_TCON;
  3870. spin_unlock(&tcon->tc_lock);
  3871. rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
  3872. if (rc) {
  3873. spin_lock(&tcon->tc_lock);
  3874. if (tcon->status == TID_IN_TCON)
  3875. tcon->status = TID_NEED_TCON;
  3876. spin_unlock(&tcon->tc_lock);
  3877. } else {
  3878. spin_lock(&tcon->tc_lock);
  3879. if (tcon->status == TID_IN_TCON)
  3880. tcon->status = TID_GOOD;
  3881. tcon->need_reconnect = false;
  3882. spin_unlock(&tcon->tc_lock);
  3883. }
  3884. return rc;
  3885. }
  3886. #endif