connect.c 104 KB

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