connect.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  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/pagemap.h>
  27. #include <linux/ctype.h>
  28. #include <linux/utsname.h>
  29. #include <linux/mempool.h>
  30. #include <linux/delay.h>
  31. #include <linux/completion.h>
  32. #include <linux/kthread.h>
  33. #include <linux/pagevec.h>
  34. #include <linux/freezer.h>
  35. #include <linux/namei.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/processor.h>
  38. #include <net/ipv6.h>
  39. #include "cifspdu.h"
  40. #include "cifsglob.h"
  41. #include "cifsproto.h"
  42. #include "cifs_unicode.h"
  43. #include "cifs_debug.h"
  44. #include "cifs_fs_sb.h"
  45. #include "ntlmssp.h"
  46. #include "nterr.h"
  47. #include "rfc1002pdu.h"
  48. #include "cn_cifs.h"
  49. #define CIFS_PORT 445
  50. #define RFC1001_PORT 139
  51. extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
  52. unsigned char *p24);
  53. extern mempool_t *cifs_req_poolp;
  54. struct smb_vol {
  55. char *username;
  56. char *password;
  57. char *domainname;
  58. char *UNC;
  59. char *UNCip;
  60. char *in6_addr; /* ipv6 address as human readable form of in6_addr */
  61. char *iocharset; /* local code page for mapping to and from Unicode */
  62. char source_rfc1001_name[16]; /* netbios name of client */
  63. char target_rfc1001_name[16]; /* netbios name of server for Win9x/ME */
  64. uid_t linux_uid;
  65. gid_t linux_gid;
  66. mode_t file_mode;
  67. mode_t dir_mode;
  68. unsigned secFlg;
  69. bool rw:1;
  70. bool retry:1;
  71. bool intr:1;
  72. bool setuids:1;
  73. bool override_uid:1;
  74. bool override_gid:1;
  75. bool dynperm:1;
  76. bool noperm:1;
  77. bool no_psx_acl:1; /* set if posix acl support should be disabled */
  78. bool cifs_acl:1;
  79. bool no_xattr:1; /* set if xattr (EA) support should be disabled*/
  80. bool server_ino:1; /* use inode numbers from server ie UniqueId */
  81. bool direct_io:1;
  82. bool remap:1; /* set to remap seven reserved chars in filenames */
  83. bool posix_paths:1; /* unset to not ask for posix pathnames. */
  84. bool no_linux_ext:1;
  85. bool sfu_emul:1;
  86. bool nullauth:1; /* attempt to authenticate with null user */
  87. bool nocase:1; /* request case insensitive filenames */
  88. bool nobrl:1; /* disable sending byte range locks to srv */
  89. bool mand_lock:1; /* send mandatory not posix byte range lock reqs */
  90. bool seal:1; /* request transport encryption on share */
  91. bool nodfs:1; /* Do not request DFS, even if available */
  92. bool local_lease:1; /* check leases only on local system, not remote */
  93. bool noblocksnd:1;
  94. bool noautotune:1;
  95. bool nostrictsync:1; /* do not force expensive SMBflush on every sync */
  96. unsigned int rsize;
  97. unsigned int wsize;
  98. unsigned int sockopt;
  99. unsigned short int port;
  100. char *prepath;
  101. };
  102. static int ipv4_connect(struct TCP_Server_Info *server);
  103. static int ipv6_connect(struct TCP_Server_Info *server);
  104. /*
  105. * cifs tcp session reconnection
  106. *
  107. * mark tcp session as reconnecting so temporarily locked
  108. * mark all smb sessions as reconnecting for tcp session
  109. * reconnect tcp session
  110. * wake up waiters on reconnection? - (not needed currently)
  111. */
  112. static int
  113. cifs_reconnect(struct TCP_Server_Info *server)
  114. {
  115. int rc = 0;
  116. struct list_head *tmp, *tmp2;
  117. struct cifsSesInfo *ses;
  118. struct cifsTconInfo *tcon;
  119. struct mid_q_entry *mid_entry;
  120. spin_lock(&GlobalMid_Lock);
  121. if (server->tcpStatus == CifsExiting) {
  122. /* the demux thread will exit normally
  123. next time through the loop */
  124. spin_unlock(&GlobalMid_Lock);
  125. return rc;
  126. } else
  127. server->tcpStatus = CifsNeedReconnect;
  128. spin_unlock(&GlobalMid_Lock);
  129. server->maxBuf = 0;
  130. cFYI(1, ("Reconnecting tcp session"));
  131. /* before reconnecting the tcp session, mark the smb session (uid)
  132. and the tid bad so they are not used until reconnected */
  133. read_lock(&cifs_tcp_ses_lock);
  134. list_for_each(tmp, &server->smb_ses_list) {
  135. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  136. ses->need_reconnect = true;
  137. ses->ipc_tid = 0;
  138. list_for_each(tmp2, &ses->tcon_list) {
  139. tcon = list_entry(tmp2, struct cifsTconInfo, tcon_list);
  140. tcon->need_reconnect = true;
  141. }
  142. }
  143. read_unlock(&cifs_tcp_ses_lock);
  144. /* do not want to be sending data on a socket we are freeing */
  145. mutex_lock(&server->srv_mutex);
  146. if (server->ssocket) {
  147. cFYI(1, ("State: 0x%x Flags: 0x%lx", server->ssocket->state,
  148. server->ssocket->flags));
  149. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  150. cFYI(1, ("Post shutdown state: 0x%x Flags: 0x%lx",
  151. server->ssocket->state,
  152. server->ssocket->flags));
  153. sock_release(server->ssocket);
  154. server->ssocket = NULL;
  155. }
  156. spin_lock(&GlobalMid_Lock);
  157. list_for_each(tmp, &server->pending_mid_q) {
  158. mid_entry = list_entry(tmp, struct
  159. mid_q_entry,
  160. qhead);
  161. if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
  162. /* Mark other intransit requests as needing
  163. retry so we do not immediately mark the
  164. session bad again (ie after we reconnect
  165. below) as they timeout too */
  166. mid_entry->midState = MID_RETRY_NEEDED;
  167. }
  168. }
  169. spin_unlock(&GlobalMid_Lock);
  170. mutex_unlock(&server->srv_mutex);
  171. while ((server->tcpStatus != CifsExiting) &&
  172. (server->tcpStatus != CifsGood)) {
  173. try_to_freeze();
  174. if (server->addr.sockAddr6.sin6_family == AF_INET6)
  175. rc = ipv6_connect(server);
  176. else
  177. rc = ipv4_connect(server);
  178. if (rc) {
  179. cFYI(1, ("reconnect error %d", rc));
  180. msleep(3000);
  181. } else {
  182. atomic_inc(&tcpSesReconnectCount);
  183. spin_lock(&GlobalMid_Lock);
  184. if (server->tcpStatus != CifsExiting)
  185. server->tcpStatus = CifsGood;
  186. server->sequence_number = 0;
  187. spin_unlock(&GlobalMid_Lock);
  188. /* atomic_set(&server->inFlight,0);*/
  189. wake_up(&server->response_q);
  190. }
  191. }
  192. return rc;
  193. }
  194. /*
  195. return codes:
  196. 0 not a transact2, or all data present
  197. >0 transact2 with that much data missing
  198. -EINVAL = invalid transact2
  199. */
  200. static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
  201. {
  202. struct smb_t2_rsp *pSMBt;
  203. int total_data_size;
  204. int data_in_this_rsp;
  205. int remaining;
  206. if (pSMB->Command != SMB_COM_TRANSACTION2)
  207. return 0;
  208. /* check for plausible wct, bcc and t2 data and parm sizes */
  209. /* check for parm and data offset going beyond end of smb */
  210. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  211. cFYI(1, ("invalid transact2 word count"));
  212. return -EINVAL;
  213. }
  214. pSMBt = (struct smb_t2_rsp *)pSMB;
  215. total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
  216. data_in_this_rsp = le16_to_cpu(pSMBt->t2_rsp.DataCount);
  217. remaining = total_data_size - data_in_this_rsp;
  218. if (remaining == 0)
  219. return 0;
  220. else if (remaining < 0) {
  221. cFYI(1, ("total data %d smaller than data in frame %d",
  222. total_data_size, data_in_this_rsp));
  223. return -EINVAL;
  224. } else {
  225. cFYI(1, ("missing %d bytes from transact2, check next response",
  226. remaining));
  227. if (total_data_size > maxBufSize) {
  228. cERROR(1, ("TotalDataSize %d is over maximum buffer %d",
  229. total_data_size, maxBufSize));
  230. return -EINVAL;
  231. }
  232. return remaining;
  233. }
  234. }
  235. static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
  236. {
  237. struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
  238. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
  239. int total_data_size;
  240. int total_in_buf;
  241. int remaining;
  242. int total_in_buf2;
  243. char *data_area_of_target;
  244. char *data_area_of_buf2;
  245. __u16 byte_count;
  246. total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
  247. if (total_data_size != le16_to_cpu(pSMB2->t2_rsp.TotalDataCount)) {
  248. cFYI(1, ("total data size of primary and secondary t2 differ"));
  249. }
  250. total_in_buf = le16_to_cpu(pSMBt->t2_rsp.DataCount);
  251. remaining = total_data_size - total_in_buf;
  252. if (remaining < 0)
  253. return -EINVAL;
  254. if (remaining == 0) /* nothing to do, ignore */
  255. return 0;
  256. total_in_buf2 = le16_to_cpu(pSMB2->t2_rsp.DataCount);
  257. if (remaining < total_in_buf2) {
  258. cFYI(1, ("transact2 2nd response contains too much data"));
  259. }
  260. /* find end of first SMB data area */
  261. data_area_of_target = (char *)&pSMBt->hdr.Protocol +
  262. le16_to_cpu(pSMBt->t2_rsp.DataOffset);
  263. /* validate target area */
  264. data_area_of_buf2 = (char *) &pSMB2->hdr.Protocol +
  265. le16_to_cpu(pSMB2->t2_rsp.DataOffset);
  266. data_area_of_target += total_in_buf;
  267. /* copy second buffer into end of first buffer */
  268. memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
  269. total_in_buf += total_in_buf2;
  270. pSMBt->t2_rsp.DataCount = cpu_to_le16(total_in_buf);
  271. byte_count = le16_to_cpu(BCC_LE(pTargetSMB));
  272. byte_count += total_in_buf2;
  273. BCC_LE(pTargetSMB) = cpu_to_le16(byte_count);
  274. byte_count = pTargetSMB->smb_buf_length;
  275. byte_count += total_in_buf2;
  276. /* BB also add check that we are not beyond maximum buffer size */
  277. pTargetSMB->smb_buf_length = byte_count;
  278. if (remaining == total_in_buf2) {
  279. cFYI(1, ("found the last secondary response"));
  280. return 0; /* we are done */
  281. } else /* more responses to go */
  282. return 1;
  283. }
  284. static int
  285. cifs_demultiplex_thread(struct TCP_Server_Info *server)
  286. {
  287. int length;
  288. unsigned int pdu_length, total_read;
  289. struct smb_hdr *smb_buffer = NULL;
  290. struct smb_hdr *bigbuf = NULL;
  291. struct smb_hdr *smallbuf = NULL;
  292. struct msghdr smb_msg;
  293. struct kvec iov;
  294. struct socket *csocket = server->ssocket;
  295. struct list_head *tmp;
  296. struct cifsSesInfo *ses;
  297. struct task_struct *task_to_wake = NULL;
  298. struct mid_q_entry *mid_entry;
  299. char temp;
  300. bool isLargeBuf = false;
  301. bool isMultiRsp;
  302. int reconnect;
  303. current->flags |= PF_MEMALLOC;
  304. cFYI(1, ("Demultiplex PID: %d", task_pid_nr(current)));
  305. length = atomic_inc_return(&tcpSesAllocCount);
  306. if (length > 1)
  307. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  308. GFP_KERNEL);
  309. set_freezable();
  310. while (server->tcpStatus != CifsExiting) {
  311. if (try_to_freeze())
  312. continue;
  313. if (bigbuf == NULL) {
  314. bigbuf = cifs_buf_get();
  315. if (!bigbuf) {
  316. cERROR(1, ("No memory for large SMB response"));
  317. msleep(3000);
  318. /* retry will check if exiting */
  319. continue;
  320. }
  321. } else if (isLargeBuf) {
  322. /* we are reusing a dirty large buf, clear its start */
  323. memset(bigbuf, 0, sizeof(struct smb_hdr));
  324. }
  325. if (smallbuf == NULL) {
  326. smallbuf = cifs_small_buf_get();
  327. if (!smallbuf) {
  328. cERROR(1, ("No memory for SMB response"));
  329. msleep(1000);
  330. /* retry will check if exiting */
  331. continue;
  332. }
  333. /* beginning of smb buffer is cleared in our buf_get */
  334. } else /* if existing small buf clear beginning */
  335. memset(smallbuf, 0, sizeof(struct smb_hdr));
  336. isLargeBuf = false;
  337. isMultiRsp = false;
  338. smb_buffer = smallbuf;
  339. iov.iov_base = smb_buffer;
  340. iov.iov_len = 4;
  341. smb_msg.msg_control = NULL;
  342. smb_msg.msg_controllen = 0;
  343. pdu_length = 4; /* enough to get RFC1001 header */
  344. incomplete_rcv:
  345. length =
  346. kernel_recvmsg(csocket, &smb_msg,
  347. &iov, 1, pdu_length, 0 /* BB other flags? */);
  348. if (server->tcpStatus == CifsExiting) {
  349. break;
  350. } else if (server->tcpStatus == CifsNeedReconnect) {
  351. cFYI(1, ("Reconnect after server stopped responding"));
  352. cifs_reconnect(server);
  353. cFYI(1, ("call to reconnect done"));
  354. csocket = server->ssocket;
  355. continue;
  356. } else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) {
  357. msleep(1); /* minimum sleep to prevent looping
  358. allowing socket to clear and app threads to set
  359. tcpStatus CifsNeedReconnect if server hung */
  360. if (pdu_length < 4) {
  361. iov.iov_base = (4 - pdu_length) +
  362. (char *)smb_buffer;
  363. iov.iov_len = pdu_length;
  364. smb_msg.msg_control = NULL;
  365. smb_msg.msg_controllen = 0;
  366. goto incomplete_rcv;
  367. } else
  368. continue;
  369. } else if (length <= 0) {
  370. if (server->tcpStatus == CifsNew) {
  371. cFYI(1, ("tcp session abend after SMBnegprot"));
  372. /* some servers kill the TCP session rather than
  373. returning an SMB negprot error, in which
  374. case reconnecting here is not going to help,
  375. and so simply return error to mount */
  376. break;
  377. }
  378. if (!try_to_freeze() && (length == -EINTR)) {
  379. cFYI(1, ("cifsd thread killed"));
  380. break;
  381. }
  382. cFYI(1, ("Reconnect after unexpected peek error %d",
  383. length));
  384. cifs_reconnect(server);
  385. csocket = server->ssocket;
  386. wake_up(&server->response_q);
  387. continue;
  388. } else if (length < pdu_length) {
  389. cFYI(1, ("requested %d bytes but only got %d bytes",
  390. pdu_length, length));
  391. pdu_length -= length;
  392. msleep(1);
  393. goto incomplete_rcv;
  394. }
  395. /* The right amount was read from socket - 4 bytes */
  396. /* so we can now interpret the length field */
  397. /* the first byte big endian of the length field,
  398. is actually not part of the length but the type
  399. with the most common, zero, as regular data */
  400. temp = *((char *) smb_buffer);
  401. /* Note that FC 1001 length is big endian on the wire,
  402. but we convert it here so it is always manipulated
  403. as host byte order */
  404. pdu_length = be32_to_cpu((__force __be32)smb_buffer->smb_buf_length);
  405. smb_buffer->smb_buf_length = pdu_length;
  406. cFYI(1, ("rfc1002 length 0x%x", pdu_length+4));
  407. if (temp == (char) RFC1002_SESSION_KEEP_ALIVE) {
  408. continue;
  409. } else if (temp == (char)RFC1002_POSITIVE_SESSION_RESPONSE) {
  410. cFYI(1, ("Good RFC 1002 session rsp"));
  411. continue;
  412. } else if (temp == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) {
  413. /* we get this from Windows 98 instead of
  414. an error on SMB negprot response */
  415. cFYI(1, ("Negative RFC1002 Session Response Error 0x%x)",
  416. pdu_length));
  417. if (server->tcpStatus == CifsNew) {
  418. /* if nack on negprot (rather than
  419. ret of smb negprot error) reconnecting
  420. not going to help, ret error to mount */
  421. break;
  422. } else {
  423. /* give server a second to
  424. clean up before reconnect attempt */
  425. msleep(1000);
  426. /* always try 445 first on reconnect
  427. since we get NACK on some if we ever
  428. connected to port 139 (the NACK is
  429. since we do not begin with RFC1001
  430. session initialize frame) */
  431. server->addr.sockAddr.sin_port =
  432. htons(CIFS_PORT);
  433. cifs_reconnect(server);
  434. csocket = server->ssocket;
  435. wake_up(&server->response_q);
  436. continue;
  437. }
  438. } else if (temp != (char) 0) {
  439. cERROR(1, ("Unknown RFC 1002 frame"));
  440. cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
  441. length);
  442. cifs_reconnect(server);
  443. csocket = server->ssocket;
  444. continue;
  445. }
  446. /* else we have an SMB response */
  447. if ((pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) ||
  448. (pdu_length < sizeof(struct smb_hdr) - 1 - 4)) {
  449. cERROR(1, ("Invalid size SMB length %d pdu_length %d",
  450. length, pdu_length+4));
  451. cifs_reconnect(server);
  452. csocket = server->ssocket;
  453. wake_up(&server->response_q);
  454. continue;
  455. }
  456. /* else length ok */
  457. reconnect = 0;
  458. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  459. isLargeBuf = true;
  460. memcpy(bigbuf, smallbuf, 4);
  461. smb_buffer = bigbuf;
  462. }
  463. length = 0;
  464. iov.iov_base = 4 + (char *)smb_buffer;
  465. iov.iov_len = pdu_length;
  466. for (total_read = 0; total_read < pdu_length;
  467. total_read += length) {
  468. length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
  469. pdu_length - total_read, 0);
  470. if ((server->tcpStatus == CifsExiting) ||
  471. (length == -EINTR)) {
  472. /* then will exit */
  473. reconnect = 2;
  474. break;
  475. } else if (server->tcpStatus == CifsNeedReconnect) {
  476. cifs_reconnect(server);
  477. csocket = server->ssocket;
  478. /* Reconnect wakes up rspns q */
  479. /* Now we will reread sock */
  480. reconnect = 1;
  481. break;
  482. } else if ((length == -ERESTARTSYS) ||
  483. (length == -EAGAIN)) {
  484. msleep(1); /* minimum sleep to prevent looping,
  485. allowing socket to clear and app
  486. threads to set tcpStatus
  487. CifsNeedReconnect if server hung*/
  488. length = 0;
  489. continue;
  490. } else if (length <= 0) {
  491. cERROR(1, ("Received no data, expecting %d",
  492. pdu_length - total_read));
  493. cifs_reconnect(server);
  494. csocket = server->ssocket;
  495. reconnect = 1;
  496. break;
  497. }
  498. }
  499. if (reconnect == 2)
  500. break;
  501. else if (reconnect == 1)
  502. continue;
  503. length += 4; /* account for rfc1002 hdr */
  504. dump_smb(smb_buffer, length);
  505. if (checkSMB(smb_buffer, smb_buffer->Mid, total_read+4)) {
  506. cifs_dump_mem("Bad SMB: ", smb_buffer, 48);
  507. continue;
  508. }
  509. task_to_wake = NULL;
  510. spin_lock(&GlobalMid_Lock);
  511. list_for_each(tmp, &server->pending_mid_q) {
  512. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  513. if ((mid_entry->mid == smb_buffer->Mid) &&
  514. (mid_entry->midState == MID_REQUEST_SUBMITTED) &&
  515. (mid_entry->command == smb_buffer->Command)) {
  516. if (check2ndT2(smb_buffer,server->maxBuf) > 0) {
  517. /* We have a multipart transact2 resp */
  518. isMultiRsp = true;
  519. if (mid_entry->resp_buf) {
  520. /* merge response - fix up 1st*/
  521. if (coalesce_t2(smb_buffer,
  522. mid_entry->resp_buf)) {
  523. mid_entry->multiRsp =
  524. true;
  525. break;
  526. } else {
  527. /* all parts received */
  528. mid_entry->multiEnd =
  529. true;
  530. goto multi_t2_fnd;
  531. }
  532. } else {
  533. if (!isLargeBuf) {
  534. cERROR(1,("1st trans2 resp needs bigbuf"));
  535. /* BB maybe we can fix this up, switch
  536. to already allocated large buffer? */
  537. } else {
  538. /* Have first buffer */
  539. mid_entry->resp_buf =
  540. smb_buffer;
  541. mid_entry->largeBuf =
  542. true;
  543. bigbuf = NULL;
  544. }
  545. }
  546. break;
  547. }
  548. mid_entry->resp_buf = smb_buffer;
  549. mid_entry->largeBuf = isLargeBuf;
  550. multi_t2_fnd:
  551. task_to_wake = mid_entry->tsk;
  552. mid_entry->midState = MID_RESPONSE_RECEIVED;
  553. #ifdef CONFIG_CIFS_STATS2
  554. mid_entry->when_received = jiffies;
  555. #endif
  556. /* so we do not time out requests to server
  557. which is still responding (since server could
  558. be busy but not dead) */
  559. server->lstrp = jiffies;
  560. break;
  561. }
  562. }
  563. spin_unlock(&GlobalMid_Lock);
  564. if (task_to_wake) {
  565. /* Was previous buf put in mpx struct for multi-rsp? */
  566. if (!isMultiRsp) {
  567. /* smb buffer will be freed by user thread */
  568. if (isLargeBuf)
  569. bigbuf = NULL;
  570. else
  571. smallbuf = NULL;
  572. }
  573. wake_up_process(task_to_wake);
  574. } else if (!is_valid_oplock_break(smb_buffer, server) &&
  575. !isMultiRsp) {
  576. cERROR(1, ("No task to wake, unknown frame received! "
  577. "NumMids %d", midCount.counter));
  578. cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
  579. sizeof(struct smb_hdr));
  580. #ifdef CONFIG_CIFS_DEBUG2
  581. cifs_dump_detail(smb_buffer);
  582. cifs_dump_mids(server);
  583. #endif /* CIFS_DEBUG2 */
  584. }
  585. } /* end while !EXITING */
  586. /* take it off the list, if it's not already */
  587. write_lock(&cifs_tcp_ses_lock);
  588. list_del_init(&server->tcp_ses_list);
  589. write_unlock(&cifs_tcp_ses_lock);
  590. spin_lock(&GlobalMid_Lock);
  591. server->tcpStatus = CifsExiting;
  592. spin_unlock(&GlobalMid_Lock);
  593. wake_up_all(&server->response_q);
  594. /* check if we have blocked requests that need to free */
  595. /* Note that cifs_max_pending is normally 50, but
  596. can be set at module install time to as little as two */
  597. spin_lock(&GlobalMid_Lock);
  598. if (atomic_read(&server->inFlight) >= cifs_max_pending)
  599. atomic_set(&server->inFlight, cifs_max_pending - 1);
  600. /* We do not want to set the max_pending too low or we
  601. could end up with the counter going negative */
  602. spin_unlock(&GlobalMid_Lock);
  603. /* Although there should not be any requests blocked on
  604. this queue it can not hurt to be paranoid and try to wake up requests
  605. that may haven been blocked when more than 50 at time were on the wire
  606. to the same server - they now will see the session is in exit state
  607. and get out of SendReceive. */
  608. wake_up_all(&server->request_q);
  609. /* give those requests time to exit */
  610. msleep(125);
  611. if (server->ssocket) {
  612. sock_release(csocket);
  613. server->ssocket = NULL;
  614. }
  615. /* buffer usuallly freed in free_mid - need to free it here on exit */
  616. cifs_buf_release(bigbuf);
  617. if (smallbuf) /* no sense logging a debug message if NULL */
  618. cifs_small_buf_release(smallbuf);
  619. /*
  620. * BB: we shouldn't have to do any of this. It shouldn't be
  621. * possible to exit from the thread with active SMB sessions
  622. */
  623. read_lock(&cifs_tcp_ses_lock);
  624. if (list_empty(&server->pending_mid_q)) {
  625. /* loop through server session structures attached to this and
  626. mark them dead */
  627. list_for_each(tmp, &server->smb_ses_list) {
  628. ses = list_entry(tmp, struct cifsSesInfo,
  629. smb_ses_list);
  630. ses->status = CifsExiting;
  631. ses->server = NULL;
  632. }
  633. read_unlock(&cifs_tcp_ses_lock);
  634. } else {
  635. /* although we can not zero the server struct pointer yet,
  636. since there are active requests which may depnd on them,
  637. mark the corresponding SMB sessions as exiting too */
  638. list_for_each(tmp, &server->smb_ses_list) {
  639. ses = list_entry(tmp, struct cifsSesInfo,
  640. smb_ses_list);
  641. ses->status = CifsExiting;
  642. }
  643. spin_lock(&GlobalMid_Lock);
  644. list_for_each(tmp, &server->pending_mid_q) {
  645. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  646. if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
  647. cFYI(1, ("Clearing Mid 0x%x - waking up ",
  648. mid_entry->mid));
  649. task_to_wake = mid_entry->tsk;
  650. if (task_to_wake)
  651. wake_up_process(task_to_wake);
  652. }
  653. }
  654. spin_unlock(&GlobalMid_Lock);
  655. read_unlock(&cifs_tcp_ses_lock);
  656. /* 1/8th of sec is more than enough time for them to exit */
  657. msleep(125);
  658. }
  659. if (!list_empty(&server->pending_mid_q)) {
  660. /* mpx threads have not exited yet give them
  661. at least the smb send timeout time for long ops */
  662. /* due to delays on oplock break requests, we need
  663. to wait at least 45 seconds before giving up
  664. on a request getting a response and going ahead
  665. and killing cifsd */
  666. cFYI(1, ("Wait for exit from demultiplex thread"));
  667. msleep(46000);
  668. /* if threads still have not exited they are probably never
  669. coming home not much else we can do but free the memory */
  670. }
  671. /* last chance to mark ses pointers invalid
  672. if there are any pointing to this (e.g
  673. if a crazy root user tried to kill cifsd
  674. kernel thread explicitly this might happen) */
  675. /* BB: This shouldn't be necessary, see above */
  676. read_lock(&cifs_tcp_ses_lock);
  677. list_for_each(tmp, &server->smb_ses_list) {
  678. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  679. ses->server = NULL;
  680. }
  681. read_unlock(&cifs_tcp_ses_lock);
  682. kfree(server->hostname);
  683. task_to_wake = xchg(&server->tsk, NULL);
  684. kfree(server);
  685. length = atomic_dec_return(&tcpSesAllocCount);
  686. if (length > 0)
  687. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  688. GFP_KERNEL);
  689. /* if server->tsk was NULL then wait for a signal before exiting */
  690. if (!task_to_wake) {
  691. set_current_state(TASK_INTERRUPTIBLE);
  692. while (!signal_pending(current)) {
  693. schedule();
  694. set_current_state(TASK_INTERRUPTIBLE);
  695. }
  696. set_current_state(TASK_RUNNING);
  697. }
  698. module_put_and_exit(0);
  699. }
  700. /* extract the host portion of the UNC string */
  701. static char *
  702. extract_hostname(const char *unc)
  703. {
  704. const char *src;
  705. char *dst, *delim;
  706. unsigned int len;
  707. /* skip double chars at beginning of string */
  708. /* BB: check validity of these bytes? */
  709. src = unc + 2;
  710. /* delimiter between hostname and sharename is always '\\' now */
  711. delim = strchr(src, '\\');
  712. if (!delim)
  713. return ERR_PTR(-EINVAL);
  714. len = delim - src;
  715. dst = kmalloc((len + 1), GFP_KERNEL);
  716. if (dst == NULL)
  717. return ERR_PTR(-ENOMEM);
  718. memcpy(dst, src, len);
  719. dst[len] = '\0';
  720. return dst;
  721. }
  722. static int
  723. cifs_parse_mount_options(char *options, const char *devname,
  724. struct smb_vol *vol)
  725. {
  726. char *value;
  727. char *data;
  728. unsigned int temp_len, i, j;
  729. char separator[2];
  730. separator[0] = ',';
  731. separator[1] = 0;
  732. if (Local_System_Name[0] != 0)
  733. memcpy(vol->source_rfc1001_name, Local_System_Name, 15);
  734. else {
  735. char *nodename = utsname()->nodename;
  736. int n = strnlen(nodename, 15);
  737. memset(vol->source_rfc1001_name, 0x20, 15);
  738. for (i = 0; i < n; i++) {
  739. /* does not have to be perfect mapping since field is
  740. informational, only used for servers that do not support
  741. port 445 and it can be overridden at mount time */
  742. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  743. }
  744. }
  745. vol->source_rfc1001_name[15] = 0;
  746. /* null target name indicates to use *SMBSERVR default called name
  747. if we end up sending RFC1001 session initialize */
  748. vol->target_rfc1001_name[0] = 0;
  749. vol->linux_uid = current_uid(); /* use current_euid() instead? */
  750. vol->linux_gid = current_gid();
  751. vol->dir_mode = S_IRWXUGO;
  752. /* 2767 perms indicate mandatory locking support */
  753. vol->file_mode = (S_IRWXUGO | S_ISGID) & (~S_IXGRP);
  754. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  755. vol->rw = true;
  756. /* default is always to request posix paths. */
  757. vol->posix_paths = 1;
  758. if (!options)
  759. return 1;
  760. if (strncmp(options, "sep=", 4) == 0) {
  761. if (options[4] != 0) {
  762. separator[0] = options[4];
  763. options += 5;
  764. } else {
  765. cFYI(1, ("Null separator not allowed"));
  766. }
  767. }
  768. while ((data = strsep(&options, separator)) != NULL) {
  769. if (!*data)
  770. continue;
  771. if ((value = strchr(data, '=')) != NULL)
  772. *value++ = '\0';
  773. /* Have to parse this before we parse for "user" */
  774. if (strnicmp(data, "user_xattr", 10) == 0) {
  775. vol->no_xattr = 0;
  776. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  777. vol->no_xattr = 1;
  778. } else if (strnicmp(data, "user", 4) == 0) {
  779. if (!value) {
  780. printk(KERN_WARNING
  781. "CIFS: invalid or missing username\n");
  782. return 1; /* needs_arg; */
  783. } else if (!*value) {
  784. /* null user, ie anonymous, authentication */
  785. vol->nullauth = 1;
  786. }
  787. if (strnlen(value, 200) < 200) {
  788. vol->username = value;
  789. } else {
  790. printk(KERN_WARNING "CIFS: username too long\n");
  791. return 1;
  792. }
  793. } else if (strnicmp(data, "pass", 4) == 0) {
  794. if (!value) {
  795. vol->password = NULL;
  796. continue;
  797. } else if (value[0] == 0) {
  798. /* check if string begins with double comma
  799. since that would mean the password really
  800. does start with a comma, and would not
  801. indicate an empty string */
  802. if (value[1] != separator[0]) {
  803. vol->password = NULL;
  804. continue;
  805. }
  806. }
  807. temp_len = strlen(value);
  808. /* removed password length check, NTLM passwords
  809. can be arbitrarily long */
  810. /* if comma in password, the string will be
  811. prematurely null terminated. Commas in password are
  812. specified across the cifs mount interface by a double
  813. comma ie ,, and a comma used as in other cases ie ','
  814. as a parameter delimiter/separator is single and due
  815. to the strsep above is temporarily zeroed. */
  816. /* NB: password legally can have multiple commas and
  817. the only illegal character in a password is null */
  818. if ((value[temp_len] == 0) &&
  819. (value[temp_len+1] == separator[0])) {
  820. /* reinsert comma */
  821. value[temp_len] = separator[0];
  822. temp_len += 2; /* move after second comma */
  823. while (value[temp_len] != 0) {
  824. if (value[temp_len] == separator[0]) {
  825. if (value[temp_len+1] ==
  826. separator[0]) {
  827. /* skip second comma */
  828. temp_len++;
  829. } else {
  830. /* single comma indicating start
  831. of next parm */
  832. break;
  833. }
  834. }
  835. temp_len++;
  836. }
  837. if (value[temp_len] == 0) {
  838. options = NULL;
  839. } else {
  840. value[temp_len] = 0;
  841. /* point option to start of next parm */
  842. options = value + temp_len + 1;
  843. }
  844. /* go from value to value + temp_len condensing
  845. double commas to singles. Note that this ends up
  846. allocating a few bytes too many, which is ok */
  847. vol->password = kzalloc(temp_len, GFP_KERNEL);
  848. if (vol->password == NULL) {
  849. printk(KERN_WARNING "CIFS: no memory "
  850. "for password\n");
  851. return 1;
  852. }
  853. for (i = 0, j = 0; i < temp_len; i++, j++) {
  854. vol->password[j] = value[i];
  855. if (value[i] == separator[0]
  856. && value[i+1] == separator[0]) {
  857. /* skip second comma */
  858. i++;
  859. }
  860. }
  861. vol->password[j] = 0;
  862. } else {
  863. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  864. if (vol->password == NULL) {
  865. printk(KERN_WARNING "CIFS: no memory "
  866. "for password\n");
  867. return 1;
  868. }
  869. strcpy(vol->password, value);
  870. }
  871. } else if (strnicmp(data, "ip", 2) == 0) {
  872. if (!value || !*value) {
  873. vol->UNCip = NULL;
  874. } else if (strnlen(value, 35) < 35) {
  875. vol->UNCip = value;
  876. } else {
  877. printk(KERN_WARNING "CIFS: ip address "
  878. "too long\n");
  879. return 1;
  880. }
  881. } else if (strnicmp(data, "sec", 3) == 0) {
  882. if (!value || !*value) {
  883. cERROR(1, ("no security value specified"));
  884. continue;
  885. } else if (strnicmp(value, "krb5i", 5) == 0) {
  886. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  887. CIFSSEC_MUST_SIGN;
  888. } else if (strnicmp(value, "krb5p", 5) == 0) {
  889. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  890. CIFSSEC_MAY_KRB5; */
  891. cERROR(1, ("Krb5 cifs privacy not supported"));
  892. return 1;
  893. } else if (strnicmp(value, "krb5", 4) == 0) {
  894. vol->secFlg |= CIFSSEC_MAY_KRB5;
  895. #ifdef CONFIG_CIFS_EXPERIMENTAL
  896. } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
  897. vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
  898. CIFSSEC_MUST_SIGN;
  899. } else if (strnicmp(value, "ntlmssp", 7) == 0) {
  900. vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
  901. #endif
  902. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  903. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  904. CIFSSEC_MUST_SIGN;
  905. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  906. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  907. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  908. vol->secFlg |= CIFSSEC_MAY_NTLM |
  909. CIFSSEC_MUST_SIGN;
  910. } else if (strnicmp(value, "ntlm", 4) == 0) {
  911. /* ntlm is default so can be turned off too */
  912. vol->secFlg |= CIFSSEC_MAY_NTLM;
  913. } else if (strnicmp(value, "nontlm", 6) == 0) {
  914. /* BB is there a better way to do this? */
  915. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  916. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  917. } else if (strnicmp(value, "lanman", 6) == 0) {
  918. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  919. #endif
  920. } else if (strnicmp(value, "none", 4) == 0) {
  921. vol->nullauth = 1;
  922. } else {
  923. cERROR(1, ("bad security option: %s", value));
  924. return 1;
  925. }
  926. } else if ((strnicmp(data, "unc", 3) == 0)
  927. || (strnicmp(data, "target", 6) == 0)
  928. || (strnicmp(data, "path", 4) == 0)) {
  929. if (!value || !*value) {
  930. printk(KERN_WARNING "CIFS: invalid path to "
  931. "network resource\n");
  932. return 1; /* needs_arg; */
  933. }
  934. if ((temp_len = strnlen(value, 300)) < 300) {
  935. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  936. if (vol->UNC == NULL)
  937. return 1;
  938. strcpy(vol->UNC, value);
  939. if (strncmp(vol->UNC, "//", 2) == 0) {
  940. vol->UNC[0] = '\\';
  941. vol->UNC[1] = '\\';
  942. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  943. printk(KERN_WARNING
  944. "CIFS: UNC Path does not begin "
  945. "with // or \\\\ \n");
  946. return 1;
  947. }
  948. } else {
  949. printk(KERN_WARNING "CIFS: UNC name too long\n");
  950. return 1;
  951. }
  952. } else if ((strnicmp(data, "domain", 3) == 0)
  953. || (strnicmp(data, "workgroup", 5) == 0)) {
  954. if (!value || !*value) {
  955. printk(KERN_WARNING "CIFS: invalid domain name\n");
  956. return 1; /* needs_arg; */
  957. }
  958. /* BB are there cases in which a comma can be valid in
  959. a domain name and need special handling? */
  960. if (strnlen(value, 256) < 256) {
  961. vol->domainname = value;
  962. cFYI(1, ("Domain name set"));
  963. } else {
  964. printk(KERN_WARNING "CIFS: domain name too "
  965. "long\n");
  966. return 1;
  967. }
  968. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  969. if (!value || !*value) {
  970. printk(KERN_WARNING
  971. "CIFS: invalid path prefix\n");
  972. return 1; /* needs_argument */
  973. }
  974. if ((temp_len = strnlen(value, 1024)) < 1024) {
  975. if (value[0] != '/')
  976. temp_len++; /* missing leading slash */
  977. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  978. if (vol->prepath == NULL)
  979. return 1;
  980. if (value[0] != '/') {
  981. vol->prepath[0] = '/';
  982. strcpy(vol->prepath+1, value);
  983. } else
  984. strcpy(vol->prepath, value);
  985. cFYI(1, ("prefix path %s", vol->prepath));
  986. } else {
  987. printk(KERN_WARNING "CIFS: prefix too long\n");
  988. return 1;
  989. }
  990. } else if (strnicmp(data, "iocharset", 9) == 0) {
  991. if (!value || !*value) {
  992. printk(KERN_WARNING "CIFS: invalid iocharset "
  993. "specified\n");
  994. return 1; /* needs_arg; */
  995. }
  996. if (strnlen(value, 65) < 65) {
  997. if (strnicmp(value, "default", 7))
  998. vol->iocharset = value;
  999. /* if iocharset not set then load_nls_default
  1000. is used by caller */
  1001. cFYI(1, ("iocharset set to %s", value));
  1002. } else {
  1003. printk(KERN_WARNING "CIFS: iocharset name "
  1004. "too long.\n");
  1005. return 1;
  1006. }
  1007. } else if (strnicmp(data, "uid", 3) == 0) {
  1008. if (value && *value) {
  1009. vol->linux_uid =
  1010. simple_strtoul(value, &value, 0);
  1011. vol->override_uid = 1;
  1012. }
  1013. } else if (strnicmp(data, "gid", 3) == 0) {
  1014. if (value && *value) {
  1015. vol->linux_gid =
  1016. simple_strtoul(value, &value, 0);
  1017. vol->override_gid = 1;
  1018. }
  1019. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1020. if (value && *value) {
  1021. vol->file_mode =
  1022. simple_strtoul(value, &value, 0);
  1023. }
  1024. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1025. if (value && *value) {
  1026. vol->dir_mode =
  1027. simple_strtoul(value, &value, 0);
  1028. }
  1029. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1030. if (value && *value) {
  1031. vol->dir_mode =
  1032. simple_strtoul(value, &value, 0);
  1033. }
  1034. } else if (strnicmp(data, "port", 4) == 0) {
  1035. if (value && *value) {
  1036. vol->port =
  1037. simple_strtoul(value, &value, 0);
  1038. }
  1039. } else if (strnicmp(data, "rsize", 5) == 0) {
  1040. if (value && *value) {
  1041. vol->rsize =
  1042. simple_strtoul(value, &value, 0);
  1043. }
  1044. } else if (strnicmp(data, "wsize", 5) == 0) {
  1045. if (value && *value) {
  1046. vol->wsize =
  1047. simple_strtoul(value, &value, 0);
  1048. }
  1049. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1050. if (value && *value) {
  1051. vol->sockopt =
  1052. simple_strtoul(value, &value, 0);
  1053. }
  1054. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1055. if (!value || !*value || (*value == ' ')) {
  1056. cFYI(1, ("invalid (empty) netbiosname"));
  1057. } else {
  1058. memset(vol->source_rfc1001_name, 0x20, 15);
  1059. for (i = 0; i < 15; i++) {
  1060. /* BB are there cases in which a comma can be
  1061. valid in this workstation netbios name (and need
  1062. special handling)? */
  1063. /* We do not uppercase netbiosname for user */
  1064. if (value[i] == 0)
  1065. break;
  1066. else
  1067. vol->source_rfc1001_name[i] =
  1068. value[i];
  1069. }
  1070. /* The string has 16th byte zero still from
  1071. set at top of the function */
  1072. if ((i == 15) && (value[i] != 0))
  1073. printk(KERN_WARNING "CIFS: netbiosname"
  1074. " longer than 15 truncated.\n");
  1075. }
  1076. } else if (strnicmp(data, "servern", 7) == 0) {
  1077. /* servernetbiosname specified override *SMBSERVER */
  1078. if (!value || !*value || (*value == ' ')) {
  1079. cFYI(1, ("empty server netbiosname specified"));
  1080. } else {
  1081. /* last byte, type, is 0x20 for servr type */
  1082. memset(vol->target_rfc1001_name, 0x20, 16);
  1083. for (i = 0; i < 15; i++) {
  1084. /* BB are there cases in which a comma can be
  1085. valid in this workstation netbios name
  1086. (and need special handling)? */
  1087. /* user or mount helper must uppercase
  1088. the netbiosname */
  1089. if (value[i] == 0)
  1090. break;
  1091. else
  1092. vol->target_rfc1001_name[i] =
  1093. value[i];
  1094. }
  1095. /* The string has 16th byte zero still from
  1096. set at top of the function */
  1097. if ((i == 15) && (value[i] != 0))
  1098. printk(KERN_WARNING "CIFS: server net"
  1099. "biosname longer than 15 truncated.\n");
  1100. }
  1101. } else if (strnicmp(data, "credentials", 4) == 0) {
  1102. /* ignore */
  1103. } else if (strnicmp(data, "version", 3) == 0) {
  1104. /* ignore */
  1105. } else if (strnicmp(data, "guest", 5) == 0) {
  1106. /* ignore */
  1107. } else if (strnicmp(data, "rw", 2) == 0) {
  1108. vol->rw = true;
  1109. } else if (strnicmp(data, "noblocksend", 11) == 0) {
  1110. vol->noblocksnd = 1;
  1111. } else if (strnicmp(data, "noautotune", 10) == 0) {
  1112. vol->noautotune = 1;
  1113. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1114. (strnicmp(data, "nosuid", 6) == 0) ||
  1115. (strnicmp(data, "exec", 4) == 0) ||
  1116. (strnicmp(data, "noexec", 6) == 0) ||
  1117. (strnicmp(data, "nodev", 5) == 0) ||
  1118. (strnicmp(data, "noauto", 6) == 0) ||
  1119. (strnicmp(data, "dev", 3) == 0)) {
  1120. /* The mount tool or mount.cifs helper (if present)
  1121. uses these opts to set flags, and the flags are read
  1122. by the kernel vfs layer before we get here (ie
  1123. before read super) so there is no point trying to
  1124. parse these options again and set anything and it
  1125. is ok to just ignore them */
  1126. continue;
  1127. } else if (strnicmp(data, "ro", 2) == 0) {
  1128. vol->rw = false;
  1129. } else if (strnicmp(data, "hard", 4) == 0) {
  1130. vol->retry = 1;
  1131. } else if (strnicmp(data, "soft", 4) == 0) {
  1132. vol->retry = 0;
  1133. } else if (strnicmp(data, "perm", 4) == 0) {
  1134. vol->noperm = 0;
  1135. } else if (strnicmp(data, "noperm", 6) == 0) {
  1136. vol->noperm = 1;
  1137. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1138. vol->remap = 1;
  1139. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1140. vol->remap = 0;
  1141. } else if (strnicmp(data, "sfu", 3) == 0) {
  1142. vol->sfu_emul = 1;
  1143. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1144. vol->sfu_emul = 0;
  1145. } else if (strnicmp(data, "nodfs", 5) == 0) {
  1146. vol->nodfs = 1;
  1147. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1148. vol->posix_paths = 1;
  1149. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1150. vol->posix_paths = 0;
  1151. } else if (strnicmp(data, "nounix", 6) == 0) {
  1152. vol->no_linux_ext = 1;
  1153. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1154. vol->no_linux_ext = 1;
  1155. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1156. (strnicmp(data, "ignorecase", 10) == 0)) {
  1157. vol->nocase = 1;
  1158. } else if (strnicmp(data, "brl", 3) == 0) {
  1159. vol->nobrl = 0;
  1160. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1161. (strnicmp(data, "nolock", 6) == 0)) {
  1162. vol->nobrl = 1;
  1163. /* turn off mandatory locking in mode
  1164. if remote locking is turned off since the
  1165. local vfs will do advisory */
  1166. if (vol->file_mode ==
  1167. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1168. vol->file_mode = S_IALLUGO;
  1169. } else if (strnicmp(data, "forcemandatorylock", 9) == 0) {
  1170. /* will take the shorter form "forcemand" as well */
  1171. /* This mount option will force use of mandatory
  1172. (DOS/Windows style) byte range locks, instead of
  1173. using posix advisory byte range locks, even if the
  1174. Unix extensions are available and posix locks would
  1175. be supported otherwise. If Unix extensions are not
  1176. negotiated this has no effect since mandatory locks
  1177. would be used (mandatory locks is all that those
  1178. those servers support) */
  1179. vol->mand_lock = 1;
  1180. } else if (strnicmp(data, "setuids", 7) == 0) {
  1181. vol->setuids = 1;
  1182. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1183. vol->setuids = 0;
  1184. } else if (strnicmp(data, "dynperm", 7) == 0) {
  1185. vol->dynperm = true;
  1186. } else if (strnicmp(data, "nodynperm", 9) == 0) {
  1187. vol->dynperm = false;
  1188. } else if (strnicmp(data, "nohard", 6) == 0) {
  1189. vol->retry = 0;
  1190. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1191. vol->retry = 1;
  1192. } else if (strnicmp(data, "nointr", 6) == 0) {
  1193. vol->intr = 0;
  1194. } else if (strnicmp(data, "intr", 4) == 0) {
  1195. vol->intr = 1;
  1196. } else if (strnicmp(data, "nostrictsync", 12) == 0) {
  1197. vol->nostrictsync = 1;
  1198. } else if (strnicmp(data, "strictsync", 10) == 0) {
  1199. vol->nostrictsync = 0;
  1200. } else if (strnicmp(data, "serverino", 7) == 0) {
  1201. vol->server_ino = 1;
  1202. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1203. vol->server_ino = 0;
  1204. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1205. vol->cifs_acl = 1;
  1206. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1207. vol->cifs_acl = 0;
  1208. } else if (strnicmp(data, "acl", 3) == 0) {
  1209. vol->no_psx_acl = 0;
  1210. } else if (strnicmp(data, "noacl", 5) == 0) {
  1211. vol->no_psx_acl = 1;
  1212. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1213. } else if (strnicmp(data, "locallease", 6) == 0) {
  1214. vol->local_lease = 1;
  1215. #endif
  1216. } else if (strnicmp(data, "sign", 4) == 0) {
  1217. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1218. } else if (strnicmp(data, "seal", 4) == 0) {
  1219. /* we do not do the following in secFlags because seal
  1220. is a per tree connection (mount) not a per socket
  1221. or per-smb connection option in the protocol */
  1222. /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1223. vol->seal = 1;
  1224. } else if (strnicmp(data, "direct", 6) == 0) {
  1225. vol->direct_io = 1;
  1226. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1227. vol->direct_io = 1;
  1228. } else if (strnicmp(data, "in6_addr", 8) == 0) {
  1229. if (!value || !*value) {
  1230. vol->in6_addr = NULL;
  1231. } else if (strnlen(value, 49) == 48) {
  1232. vol->in6_addr = value;
  1233. } else {
  1234. printk(KERN_WARNING "CIFS: ip v6 address not "
  1235. "48 characters long\n");
  1236. return 1;
  1237. }
  1238. } else if (strnicmp(data, "noac", 4) == 0) {
  1239. printk(KERN_WARNING "CIFS: Mount option noac not "
  1240. "supported. Instead set "
  1241. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1242. } else
  1243. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1244. data);
  1245. }
  1246. if (vol->UNC == NULL) {
  1247. if (devname == NULL) {
  1248. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1249. "target\n");
  1250. return 1;
  1251. }
  1252. if ((temp_len = strnlen(devname, 300)) < 300) {
  1253. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1254. if (vol->UNC == NULL)
  1255. return 1;
  1256. strcpy(vol->UNC, devname);
  1257. if (strncmp(vol->UNC, "//", 2) == 0) {
  1258. vol->UNC[0] = '\\';
  1259. vol->UNC[1] = '\\';
  1260. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1261. printk(KERN_WARNING "CIFS: UNC Path does not "
  1262. "begin with // or \\\\ \n");
  1263. return 1;
  1264. }
  1265. value = strpbrk(vol->UNC+2, "/\\");
  1266. if (value)
  1267. *value = '\\';
  1268. } else {
  1269. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1270. return 1;
  1271. }
  1272. }
  1273. if (vol->UNCip == NULL)
  1274. vol->UNCip = &vol->UNC[2];
  1275. return 0;
  1276. }
  1277. static struct TCP_Server_Info *
  1278. cifs_find_tcp_session(struct sockaddr_storage *addr)
  1279. {
  1280. struct list_head *tmp;
  1281. struct TCP_Server_Info *server;
  1282. struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
  1283. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
  1284. write_lock(&cifs_tcp_ses_lock);
  1285. list_for_each(tmp, &cifs_tcp_ses_list) {
  1286. server = list_entry(tmp, struct TCP_Server_Info,
  1287. tcp_ses_list);
  1288. /*
  1289. * the demux thread can exit on its own while still in CifsNew
  1290. * so don't accept any sockets in that state. Since the
  1291. * tcpStatus never changes back to CifsNew it's safe to check
  1292. * for this without a lock.
  1293. */
  1294. if (server->tcpStatus == CifsNew)
  1295. continue;
  1296. if (addr->ss_family == AF_INET &&
  1297. (addr4->sin_addr.s_addr !=
  1298. server->addr.sockAddr.sin_addr.s_addr))
  1299. continue;
  1300. else if (addr->ss_family == AF_INET6 &&
  1301. !ipv6_addr_equal(&server->addr.sockAddr6.sin6_addr,
  1302. &addr6->sin6_addr))
  1303. continue;
  1304. ++server->srv_count;
  1305. write_unlock(&cifs_tcp_ses_lock);
  1306. cFYI(1, ("Existing tcp session with server found"));
  1307. return server;
  1308. }
  1309. write_unlock(&cifs_tcp_ses_lock);
  1310. return NULL;
  1311. }
  1312. static void
  1313. cifs_put_tcp_session(struct TCP_Server_Info *server)
  1314. {
  1315. struct task_struct *task;
  1316. write_lock(&cifs_tcp_ses_lock);
  1317. if (--server->srv_count > 0) {
  1318. write_unlock(&cifs_tcp_ses_lock);
  1319. return;
  1320. }
  1321. list_del_init(&server->tcp_ses_list);
  1322. write_unlock(&cifs_tcp_ses_lock);
  1323. spin_lock(&GlobalMid_Lock);
  1324. server->tcpStatus = CifsExiting;
  1325. spin_unlock(&GlobalMid_Lock);
  1326. task = xchg(&server->tsk, NULL);
  1327. if (task)
  1328. force_sig(SIGKILL, task);
  1329. }
  1330. static struct TCP_Server_Info *
  1331. cifs_get_tcp_session(struct smb_vol *volume_info)
  1332. {
  1333. struct TCP_Server_Info *tcp_ses = NULL;
  1334. struct sockaddr_storage addr;
  1335. struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
  1336. struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
  1337. int rc;
  1338. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1339. if (volume_info->UNCip && volume_info->UNC) {
  1340. rc = cifs_inet_pton(AF_INET, volume_info->UNCip,
  1341. &sin_server->sin_addr.s_addr);
  1342. if (rc <= 0) {
  1343. /* not ipv4 address, try ipv6 */
  1344. rc = cifs_inet_pton(AF_INET6, volume_info->UNCip,
  1345. &sin_server6->sin6_addr.in6_u);
  1346. if (rc > 0)
  1347. addr.ss_family = AF_INET6;
  1348. } else {
  1349. addr.ss_family = AF_INET;
  1350. }
  1351. if (rc <= 0) {
  1352. /* we failed translating address */
  1353. rc = -EINVAL;
  1354. goto out_err;
  1355. }
  1356. cFYI(1, ("UNC: %s ip: %s", volume_info->UNC,
  1357. volume_info->UNCip));
  1358. } else if (volume_info->UNCip) {
  1359. /* BB using ip addr as tcp_ses name to connect to the
  1360. DFS root below */
  1361. cERROR(1, ("Connecting to DFS root not implemented yet"));
  1362. rc = -EINVAL;
  1363. goto out_err;
  1364. } else /* which tcp_sess DFS root would we conect to */ {
  1365. cERROR(1,
  1366. ("CIFS mount error: No UNC path (e.g. -o "
  1367. "unc=//192.168.1.100/public) specified"));
  1368. rc = -EINVAL;
  1369. goto out_err;
  1370. }
  1371. /* see if we already have a matching tcp_ses */
  1372. tcp_ses = cifs_find_tcp_session(&addr);
  1373. if (tcp_ses)
  1374. return tcp_ses;
  1375. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1376. if (!tcp_ses) {
  1377. rc = -ENOMEM;
  1378. goto out_err;
  1379. }
  1380. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  1381. if (IS_ERR(tcp_ses->hostname)) {
  1382. rc = PTR_ERR(tcp_ses->hostname);
  1383. goto out_err;
  1384. }
  1385. tcp_ses->noblocksnd = volume_info->noblocksnd;
  1386. tcp_ses->noautotune = volume_info->noautotune;
  1387. atomic_set(&tcp_ses->inFlight, 0);
  1388. init_waitqueue_head(&tcp_ses->response_q);
  1389. init_waitqueue_head(&tcp_ses->request_q);
  1390. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1391. mutex_init(&tcp_ses->srv_mutex);
  1392. memcpy(tcp_ses->workstation_RFC1001_name,
  1393. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1394. memcpy(tcp_ses->server_RFC1001_name,
  1395. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1396. tcp_ses->sequence_number = 0;
  1397. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1398. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1399. /*
  1400. * at this point we are the only ones with the pointer
  1401. * to the struct since the kernel thread not created yet
  1402. * no need to spinlock this init of tcpStatus or srv_count
  1403. */
  1404. tcp_ses->tcpStatus = CifsNew;
  1405. ++tcp_ses->srv_count;
  1406. if (addr.ss_family == AF_INET6) {
  1407. cFYI(1, ("attempting ipv6 connect"));
  1408. /* BB should we allow ipv6 on port 139? */
  1409. /* other OS never observed in Wild doing 139 with v6 */
  1410. memcpy(&tcp_ses->addr.sockAddr6, sin_server6,
  1411. sizeof(struct sockaddr_in6));
  1412. sin_server6->sin6_port = htons(volume_info->port);
  1413. rc = ipv6_connect(tcp_ses);
  1414. } else {
  1415. memcpy(&tcp_ses->addr.sockAddr, sin_server,
  1416. sizeof(struct sockaddr_in));
  1417. sin_server->sin_port = htons(volume_info->port);
  1418. rc = ipv4_connect(tcp_ses);
  1419. }
  1420. if (rc < 0) {
  1421. cERROR(1, ("Error connecting to socket. Aborting operation"));
  1422. goto out_err;
  1423. }
  1424. /*
  1425. * since we're in a cifs function already, we know that
  1426. * this will succeed. No need for try_module_get().
  1427. */
  1428. __module_get(THIS_MODULE);
  1429. tcp_ses->tsk = kthread_run((void *)(void *)cifs_demultiplex_thread,
  1430. tcp_ses, "cifsd");
  1431. if (IS_ERR(tcp_ses->tsk)) {
  1432. rc = PTR_ERR(tcp_ses->tsk);
  1433. cERROR(1, ("error %d create cifsd thread", rc));
  1434. module_put(THIS_MODULE);
  1435. goto out_err;
  1436. }
  1437. /* thread spawned, put it on the list */
  1438. write_lock(&cifs_tcp_ses_lock);
  1439. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1440. write_unlock(&cifs_tcp_ses_lock);
  1441. return tcp_ses;
  1442. out_err:
  1443. if (tcp_ses) {
  1444. kfree(tcp_ses->hostname);
  1445. if (tcp_ses->ssocket)
  1446. sock_release(tcp_ses->ssocket);
  1447. kfree(tcp_ses);
  1448. }
  1449. return ERR_PTR(rc);
  1450. }
  1451. static struct cifsSesInfo *
  1452. cifs_find_smb_ses(struct TCP_Server_Info *server, char *username)
  1453. {
  1454. struct list_head *tmp;
  1455. struct cifsSesInfo *ses;
  1456. write_lock(&cifs_tcp_ses_lock);
  1457. list_for_each(tmp, &server->smb_ses_list) {
  1458. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  1459. if (strncmp(ses->userName, username, MAX_USERNAME_SIZE))
  1460. continue;
  1461. ++ses->ses_count;
  1462. write_unlock(&cifs_tcp_ses_lock);
  1463. return ses;
  1464. }
  1465. write_unlock(&cifs_tcp_ses_lock);
  1466. return NULL;
  1467. }
  1468. static void
  1469. cifs_put_smb_ses(struct cifsSesInfo *ses)
  1470. {
  1471. int xid;
  1472. struct TCP_Server_Info *server = ses->server;
  1473. write_lock(&cifs_tcp_ses_lock);
  1474. if (--ses->ses_count > 0) {
  1475. write_unlock(&cifs_tcp_ses_lock);
  1476. return;
  1477. }
  1478. list_del_init(&ses->smb_ses_list);
  1479. write_unlock(&cifs_tcp_ses_lock);
  1480. if (ses->status == CifsGood) {
  1481. xid = GetXid();
  1482. CIFSSMBLogoff(xid, ses);
  1483. _FreeXid(xid);
  1484. }
  1485. sesInfoFree(ses);
  1486. cifs_put_tcp_session(server);
  1487. }
  1488. static struct cifsTconInfo *
  1489. cifs_find_tcon(struct cifsSesInfo *ses, const char *unc)
  1490. {
  1491. struct list_head *tmp;
  1492. struct cifsTconInfo *tcon;
  1493. write_lock(&cifs_tcp_ses_lock);
  1494. list_for_each(tmp, &ses->tcon_list) {
  1495. tcon = list_entry(tmp, struct cifsTconInfo, tcon_list);
  1496. if (tcon->tidStatus == CifsExiting)
  1497. continue;
  1498. if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
  1499. continue;
  1500. ++tcon->tc_count;
  1501. write_unlock(&cifs_tcp_ses_lock);
  1502. return tcon;
  1503. }
  1504. write_unlock(&cifs_tcp_ses_lock);
  1505. return NULL;
  1506. }
  1507. static void
  1508. cifs_put_tcon(struct cifsTconInfo *tcon)
  1509. {
  1510. int xid;
  1511. struct cifsSesInfo *ses = tcon->ses;
  1512. write_lock(&cifs_tcp_ses_lock);
  1513. if (--tcon->tc_count > 0) {
  1514. write_unlock(&cifs_tcp_ses_lock);
  1515. return;
  1516. }
  1517. list_del_init(&tcon->tcon_list);
  1518. write_unlock(&cifs_tcp_ses_lock);
  1519. xid = GetXid();
  1520. CIFSSMBTDis(xid, tcon);
  1521. _FreeXid(xid);
  1522. DeleteTconOplockQEntries(tcon);
  1523. tconInfoFree(tcon);
  1524. cifs_put_smb_ses(ses);
  1525. }
  1526. int
  1527. get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path,
  1528. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  1529. struct dfs_info3_param **preferrals, int remap)
  1530. {
  1531. char *temp_unc;
  1532. int rc = 0;
  1533. *pnum_referrals = 0;
  1534. *preferrals = NULL;
  1535. if (pSesInfo->ipc_tid == 0) {
  1536. temp_unc = kmalloc(2 /* for slashes */ +
  1537. strnlen(pSesInfo->serverName,
  1538. SERVER_NAME_LEN_WITH_NULL * 2)
  1539. + 1 + 4 /* slash IPC$ */ + 2,
  1540. GFP_KERNEL);
  1541. if (temp_unc == NULL)
  1542. return -ENOMEM;
  1543. temp_unc[0] = '\\';
  1544. temp_unc[1] = '\\';
  1545. strcpy(temp_unc + 2, pSesInfo->serverName);
  1546. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  1547. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  1548. cFYI(1,
  1549. ("CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid));
  1550. kfree(temp_unc);
  1551. }
  1552. if (rc == 0)
  1553. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  1554. pnum_referrals, nls_codepage, remap);
  1555. /* BB map targetUNCs to dfs_info3 structures, here or
  1556. in CIFSGetDFSRefer BB */
  1557. return rc;
  1558. }
  1559. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1560. static struct lock_class_key cifs_key[2];
  1561. static struct lock_class_key cifs_slock_key[2];
  1562. static inline void
  1563. cifs_reclassify_socket4(struct socket *sock)
  1564. {
  1565. struct sock *sk = sock->sk;
  1566. BUG_ON(sock_owned_by_user(sk));
  1567. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  1568. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  1569. }
  1570. static inline void
  1571. cifs_reclassify_socket6(struct socket *sock)
  1572. {
  1573. struct sock *sk = sock->sk;
  1574. BUG_ON(sock_owned_by_user(sk));
  1575. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  1576. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  1577. }
  1578. #else
  1579. static inline void
  1580. cifs_reclassify_socket4(struct socket *sock)
  1581. {
  1582. }
  1583. static inline void
  1584. cifs_reclassify_socket6(struct socket *sock)
  1585. {
  1586. }
  1587. #endif
  1588. /* See RFC1001 section 14 on representation of Netbios names */
  1589. static void rfc1002mangle(char *target, char *source, unsigned int length)
  1590. {
  1591. unsigned int i, j;
  1592. for (i = 0, j = 0; i < (length); i++) {
  1593. /* mask a nibble at a time and encode */
  1594. target[j] = 'A' + (0x0F & (source[i] >> 4));
  1595. target[j+1] = 'A' + (0x0F & source[i]);
  1596. j += 2;
  1597. }
  1598. }
  1599. static int
  1600. ipv4_connect(struct TCP_Server_Info *server)
  1601. {
  1602. int rc = 0;
  1603. bool connected = false;
  1604. __be16 orig_port = 0;
  1605. struct socket *socket = server->ssocket;
  1606. if (socket == NULL) {
  1607. rc = sock_create_kern(PF_INET, SOCK_STREAM,
  1608. IPPROTO_TCP, &socket);
  1609. if (rc < 0) {
  1610. cERROR(1, ("Error %d creating socket", rc));
  1611. return rc;
  1612. }
  1613. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1614. cFYI(1, ("Socket created"));
  1615. server->ssocket = socket;
  1616. socket->sk->sk_allocation = GFP_NOFS;
  1617. cifs_reclassify_socket4(socket);
  1618. }
  1619. /* user overrode default port */
  1620. if (server->addr.sockAddr.sin_port) {
  1621. rc = socket->ops->connect(socket, (struct sockaddr *)
  1622. &server->addr.sockAddr,
  1623. sizeof(struct sockaddr_in), 0);
  1624. if (rc >= 0)
  1625. connected = true;
  1626. }
  1627. if (!connected) {
  1628. /* save original port so we can retry user specified port
  1629. later if fall back ports fail this time */
  1630. orig_port = server->addr.sockAddr.sin_port;
  1631. /* do not retry on the same port we just failed on */
  1632. if (server->addr.sockAddr.sin_port != htons(CIFS_PORT)) {
  1633. server->addr.sockAddr.sin_port = htons(CIFS_PORT);
  1634. rc = socket->ops->connect(socket,
  1635. (struct sockaddr *)
  1636. &server->addr.sockAddr,
  1637. sizeof(struct sockaddr_in), 0);
  1638. if (rc >= 0)
  1639. connected = true;
  1640. }
  1641. }
  1642. if (!connected) {
  1643. server->addr.sockAddr.sin_port = htons(RFC1001_PORT);
  1644. rc = socket->ops->connect(socket, (struct sockaddr *)
  1645. &server->addr.sockAddr,
  1646. sizeof(struct sockaddr_in), 0);
  1647. if (rc >= 0)
  1648. connected = true;
  1649. }
  1650. /* give up here - unless we want to retry on different
  1651. protocol families some day */
  1652. if (!connected) {
  1653. if (orig_port)
  1654. server->addr.sockAddr.sin_port = orig_port;
  1655. cFYI(1, ("Error %d connecting to server via ipv4", rc));
  1656. sock_release(socket);
  1657. server->ssocket = NULL;
  1658. return rc;
  1659. }
  1660. /*
  1661. * Eventually check for other socket options to change from
  1662. * the default. sock_setsockopt not used because it expects
  1663. * user space buffer
  1664. */
  1665. socket->sk->sk_rcvtimeo = 7 * HZ;
  1666. socket->sk->sk_sndtimeo = 5 * HZ;
  1667. /* make the bufsizes depend on wsize/rsize and max requests */
  1668. if (server->noautotune) {
  1669. if (socket->sk->sk_sndbuf < (200 * 1024))
  1670. socket->sk->sk_sndbuf = 200 * 1024;
  1671. if (socket->sk->sk_rcvbuf < (140 * 1024))
  1672. socket->sk->sk_rcvbuf = 140 * 1024;
  1673. }
  1674. cFYI(1, ("sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  1675. socket->sk->sk_sndbuf,
  1676. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo));
  1677. /* send RFC1001 sessinit */
  1678. if (server->addr.sockAddr.sin_port == htons(RFC1001_PORT)) {
  1679. /* some servers require RFC1001 sessinit before sending
  1680. negprot - BB check reconnection in case where second
  1681. sessinit is sent but no second negprot */
  1682. struct rfc1002_session_packet *ses_init_buf;
  1683. struct smb_hdr *smb_buf;
  1684. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  1685. GFP_KERNEL);
  1686. if (ses_init_buf) {
  1687. ses_init_buf->trailer.session_req.called_len = 32;
  1688. if (server->server_RFC1001_name &&
  1689. server->server_RFC1001_name[0] != 0)
  1690. rfc1002mangle(ses_init_buf->trailer.
  1691. session_req.called_name,
  1692. server->server_RFC1001_name,
  1693. RFC1001_NAME_LEN_WITH_NULL);
  1694. else
  1695. rfc1002mangle(ses_init_buf->trailer.
  1696. session_req.called_name,
  1697. DEFAULT_CIFS_CALLED_NAME,
  1698. RFC1001_NAME_LEN_WITH_NULL);
  1699. ses_init_buf->trailer.session_req.calling_len = 32;
  1700. /* calling name ends in null (byte 16) from old smb
  1701. convention. */
  1702. if (server->workstation_RFC1001_name &&
  1703. server->workstation_RFC1001_name[0] != 0)
  1704. rfc1002mangle(ses_init_buf->trailer.
  1705. session_req.calling_name,
  1706. server->workstation_RFC1001_name,
  1707. RFC1001_NAME_LEN_WITH_NULL);
  1708. else
  1709. rfc1002mangle(ses_init_buf->trailer.
  1710. session_req.calling_name,
  1711. "LINUX_CIFS_CLNT",
  1712. RFC1001_NAME_LEN_WITH_NULL);
  1713. ses_init_buf->trailer.session_req.scope1 = 0;
  1714. ses_init_buf->trailer.session_req.scope2 = 0;
  1715. smb_buf = (struct smb_hdr *)ses_init_buf;
  1716. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  1717. smb_buf->smb_buf_length = 0x81000044;
  1718. rc = smb_send(server, smb_buf, 0x44);
  1719. kfree(ses_init_buf);
  1720. msleep(1); /* RFC1001 layer in at least one server
  1721. requires very short break before negprot
  1722. presumably because not expecting negprot
  1723. to follow so fast. This is a simple
  1724. solution that works without
  1725. complicating the code and causes no
  1726. significant slowing down on mount
  1727. for everyone else */
  1728. }
  1729. /* else the negprot may still work without this
  1730. even though malloc failed */
  1731. }
  1732. return rc;
  1733. }
  1734. static int
  1735. ipv6_connect(struct TCP_Server_Info *server)
  1736. {
  1737. int rc = 0;
  1738. bool connected = false;
  1739. __be16 orig_port = 0;
  1740. struct socket *socket = server->ssocket;
  1741. if (socket == NULL) {
  1742. rc = sock_create_kern(PF_INET6, SOCK_STREAM,
  1743. IPPROTO_TCP, &socket);
  1744. if (rc < 0) {
  1745. cERROR(1, ("Error %d creating ipv6 socket", rc));
  1746. socket = NULL;
  1747. return rc;
  1748. }
  1749. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1750. cFYI(1, ("ipv6 Socket created"));
  1751. server->ssocket = socket;
  1752. socket->sk->sk_allocation = GFP_NOFS;
  1753. cifs_reclassify_socket6(socket);
  1754. }
  1755. /* user overrode default port */
  1756. if (server->addr.sockAddr6.sin6_port) {
  1757. rc = socket->ops->connect(socket,
  1758. (struct sockaddr *) &server->addr.sockAddr6,
  1759. sizeof(struct sockaddr_in6), 0);
  1760. if (rc >= 0)
  1761. connected = true;
  1762. }
  1763. if (!connected) {
  1764. /* save original port so we can retry user specified port
  1765. later if fall back ports fail this time */
  1766. orig_port = server->addr.sockAddr6.sin6_port;
  1767. /* do not retry on the same port we just failed on */
  1768. if (server->addr.sockAddr6.sin6_port != htons(CIFS_PORT)) {
  1769. server->addr.sockAddr6.sin6_port = htons(CIFS_PORT);
  1770. rc = socket->ops->connect(socket, (struct sockaddr *)
  1771. &server->addr.sockAddr6,
  1772. sizeof(struct sockaddr_in6), 0);
  1773. if (rc >= 0)
  1774. connected = true;
  1775. }
  1776. }
  1777. if (!connected) {
  1778. server->addr.sockAddr6.sin6_port = htons(RFC1001_PORT);
  1779. rc = socket->ops->connect(socket, (struct sockaddr *)
  1780. &server->addr.sockAddr6,
  1781. sizeof(struct sockaddr_in6), 0);
  1782. if (rc >= 0)
  1783. connected = true;
  1784. }
  1785. /* give up here - unless we want to retry on different
  1786. protocol families some day */
  1787. if (!connected) {
  1788. if (orig_port)
  1789. server->addr.sockAddr6.sin6_port = orig_port;
  1790. cFYI(1, ("Error %d connecting to server via ipv6", rc));
  1791. sock_release(socket);
  1792. server->ssocket = NULL;
  1793. return rc;
  1794. }
  1795. /*
  1796. * Eventually check for other socket options to change from
  1797. * the default. sock_setsockopt not used because it expects
  1798. * user space buffer
  1799. */
  1800. socket->sk->sk_rcvtimeo = 7 * HZ;
  1801. socket->sk->sk_sndtimeo = 5 * HZ;
  1802. server->ssocket = socket;
  1803. return rc;
  1804. }
  1805. void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
  1806. struct super_block *sb, struct smb_vol *vol_info)
  1807. {
  1808. /* if we are reconnecting then should we check to see if
  1809. * any requested capabilities changed locally e.g. via
  1810. * remount but we can not do much about it here
  1811. * if they have (even if we could detect it by the following)
  1812. * Perhaps we could add a backpointer to array of sb from tcon
  1813. * or if we change to make all sb to same share the same
  1814. * sb as NFS - then we only have one backpointer to sb.
  1815. * What if we wanted to mount the server share twice once with
  1816. * and once without posixacls or posix paths? */
  1817. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  1818. if (vol_info && vol_info->no_linux_ext) {
  1819. tcon->fsUnixInfo.Capability = 0;
  1820. tcon->unix_ext = 0; /* Unix Extensions disabled */
  1821. cFYI(1, ("Linux protocol extensions disabled"));
  1822. return;
  1823. } else if (vol_info)
  1824. tcon->unix_ext = 1; /* Unix Extensions supported */
  1825. if (tcon->unix_ext == 0) {
  1826. cFYI(1, ("Unix extensions disabled so not set on reconnect"));
  1827. return;
  1828. }
  1829. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  1830. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  1831. /* check for reconnect case in which we do not
  1832. want to change the mount behavior if we can avoid it */
  1833. if (vol_info == NULL) {
  1834. /* turn off POSIX ACL and PATHNAMES if not set
  1835. originally at mount time */
  1836. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  1837. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  1838. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  1839. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  1840. cERROR(1, ("POSIXPATH support change"));
  1841. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  1842. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  1843. cERROR(1, ("possible reconnect error"));
  1844. cERROR(1,
  1845. ("server disabled POSIX path support"));
  1846. }
  1847. }
  1848. cap &= CIFS_UNIX_CAP_MASK;
  1849. if (vol_info && vol_info->no_psx_acl)
  1850. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  1851. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  1852. cFYI(1, ("negotiated posix acl support"));
  1853. if (sb)
  1854. sb->s_flags |= MS_POSIXACL;
  1855. }
  1856. if (vol_info && vol_info->posix_paths == 0)
  1857. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  1858. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  1859. cFYI(1, ("negotiate posix pathnames"));
  1860. if (sb)
  1861. CIFS_SB(sb)->mnt_cifs_flags |=
  1862. CIFS_MOUNT_POSIX_PATHS;
  1863. }
  1864. /* We might be setting the path sep back to a different
  1865. form if we are reconnecting and the server switched its
  1866. posix path capability for this share */
  1867. if (sb && (CIFS_SB(sb)->prepathlen > 0))
  1868. CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
  1869. if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
  1870. if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
  1871. CIFS_SB(sb)->rsize = 127 * 1024;
  1872. cFYI(DBG2,
  1873. ("larger reads not supported by srv"));
  1874. }
  1875. }
  1876. cFYI(1, ("Negotiate caps 0x%x", (int)cap));
  1877. #ifdef CONFIG_CIFS_DEBUG2
  1878. if (cap & CIFS_UNIX_FCNTL_CAP)
  1879. cFYI(1, ("FCNTL cap"));
  1880. if (cap & CIFS_UNIX_EXTATTR_CAP)
  1881. cFYI(1, ("EXTATTR cap"));
  1882. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  1883. cFYI(1, ("POSIX path cap"));
  1884. if (cap & CIFS_UNIX_XATTR_CAP)
  1885. cFYI(1, ("XATTR cap"));
  1886. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  1887. cFYI(1, ("POSIX ACL cap"));
  1888. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  1889. cFYI(1, ("very large read cap"));
  1890. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  1891. cFYI(1, ("very large write cap"));
  1892. #endif /* CIFS_DEBUG2 */
  1893. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  1894. if (vol_info == NULL) {
  1895. cFYI(1, ("resetting capabilities failed"));
  1896. } else
  1897. cERROR(1, ("Negotiating Unix capabilities "
  1898. "with the server failed. Consider "
  1899. "mounting with the Unix Extensions\n"
  1900. "disabled, if problems are found, "
  1901. "by specifying the nounix mount "
  1902. "option."));
  1903. }
  1904. }
  1905. }
  1906. static void
  1907. convert_delimiter(char *path, char delim)
  1908. {
  1909. int i;
  1910. char old_delim;
  1911. if (path == NULL)
  1912. return;
  1913. if (delim == '/')
  1914. old_delim = '\\';
  1915. else
  1916. old_delim = '/';
  1917. for (i = 0; path[i] != '\0'; i++) {
  1918. if (path[i] == old_delim)
  1919. path[i] = delim;
  1920. }
  1921. }
  1922. static void setup_cifs_sb(struct smb_vol *pvolume_info,
  1923. struct cifs_sb_info *cifs_sb)
  1924. {
  1925. if (pvolume_info->rsize > CIFSMaxBufSize) {
  1926. cERROR(1, ("rsize %d too large, using MaxBufSize",
  1927. pvolume_info->rsize));
  1928. cifs_sb->rsize = CIFSMaxBufSize;
  1929. } else if ((pvolume_info->rsize) &&
  1930. (pvolume_info->rsize <= CIFSMaxBufSize))
  1931. cifs_sb->rsize = pvolume_info->rsize;
  1932. else /* default */
  1933. cifs_sb->rsize = CIFSMaxBufSize;
  1934. if (pvolume_info->wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
  1935. cERROR(1, ("wsize %d too large, using 4096 instead",
  1936. pvolume_info->wsize));
  1937. cifs_sb->wsize = 4096;
  1938. } else if (pvolume_info->wsize)
  1939. cifs_sb->wsize = pvolume_info->wsize;
  1940. else
  1941. cifs_sb->wsize = min_t(const int,
  1942. PAGEVEC_SIZE * PAGE_CACHE_SIZE,
  1943. 127*1024);
  1944. /* old default of CIFSMaxBufSize was too small now
  1945. that SMB Write2 can send multiple pages in kvec.
  1946. RFC1001 does not describe what happens when frame
  1947. bigger than 128K is sent so use that as max in
  1948. conjunction with 52K kvec constraint on arch with 4K
  1949. page size */
  1950. if (cifs_sb->rsize < 2048) {
  1951. cifs_sb->rsize = 2048;
  1952. /* Windows ME may prefer this */
  1953. cFYI(1, ("readsize set to minimum: 2048"));
  1954. }
  1955. /* calculate prepath */
  1956. cifs_sb->prepath = pvolume_info->prepath;
  1957. if (cifs_sb->prepath) {
  1958. cifs_sb->prepathlen = strlen(cifs_sb->prepath);
  1959. /* we can not convert the / to \ in the path
  1960. separators in the prefixpath yet because we do not
  1961. know (until reset_cifs_unix_caps is called later)
  1962. whether POSIX PATH CAP is available. We normalize
  1963. the / to \ after reset_cifs_unix_caps is called */
  1964. pvolume_info->prepath = NULL;
  1965. } else
  1966. cifs_sb->prepathlen = 0;
  1967. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  1968. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  1969. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  1970. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  1971. cFYI(1, ("file mode: 0x%x dir mode: 0x%x",
  1972. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode));
  1973. if (pvolume_info->noperm)
  1974. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  1975. if (pvolume_info->setuids)
  1976. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  1977. if (pvolume_info->server_ino)
  1978. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  1979. if (pvolume_info->remap)
  1980. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  1981. if (pvolume_info->no_xattr)
  1982. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  1983. if (pvolume_info->sfu_emul)
  1984. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  1985. if (pvolume_info->nobrl)
  1986. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  1987. if (pvolume_info->nostrictsync)
  1988. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  1989. if (pvolume_info->mand_lock)
  1990. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  1991. if (pvolume_info->cifs_acl)
  1992. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  1993. if (pvolume_info->override_uid)
  1994. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  1995. if (pvolume_info->override_gid)
  1996. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  1997. if (pvolume_info->dynperm)
  1998. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  1999. if (pvolume_info->direct_io) {
  2000. cFYI(1, ("mounting share using direct i/o"));
  2001. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  2002. }
  2003. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  2004. cERROR(1, ("mount option dynperm ignored if cifsacl "
  2005. "mount option supported"));
  2006. }
  2007. static int
  2008. is_path_accessible(int xid, struct cifsTconInfo *tcon,
  2009. struct cifs_sb_info *cifs_sb, const char *full_path)
  2010. {
  2011. int rc;
  2012. __u64 inode_num;
  2013. FILE_ALL_INFO *pfile_info;
  2014. rc = CIFSGetSrvInodeNumber(xid, tcon, full_path, &inode_num,
  2015. cifs_sb->local_nls,
  2016. cifs_sb->mnt_cifs_flags &
  2017. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2018. if (rc != -EOPNOTSUPP)
  2019. return rc;
  2020. pfile_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  2021. if (pfile_info == NULL)
  2022. return -ENOMEM;
  2023. rc = CIFSSMBQPathInfo(xid, tcon, full_path, pfile_info,
  2024. 0 /* not legacy */, cifs_sb->local_nls,
  2025. cifs_sb->mnt_cifs_flags &
  2026. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2027. kfree(pfile_info);
  2028. return rc;
  2029. }
  2030. static void
  2031. cleanup_volume_info(struct smb_vol **pvolume_info)
  2032. {
  2033. struct smb_vol *volume_info;
  2034. if (!pvolume_info && !*pvolume_info)
  2035. return;
  2036. volume_info = *pvolume_info;
  2037. kzfree(volume_info->password);
  2038. kfree(volume_info->UNC);
  2039. kfree(volume_info->prepath);
  2040. kfree(volume_info);
  2041. *pvolume_info = NULL;
  2042. return;
  2043. }
  2044. #ifdef CONFIG_CIFS_DFS_UPCALL
  2045. /* build_path_to_root returns full path to root when
  2046. * we do not have an exiting connection (tcon) */
  2047. static char *
  2048. build_unc_path_to_root(const struct smb_vol *volume_info,
  2049. const struct cifs_sb_info *cifs_sb)
  2050. {
  2051. char *full_path;
  2052. int unc_len = strnlen(volume_info->UNC, MAX_TREE_SIZE + 1);
  2053. full_path = kmalloc(unc_len + cifs_sb->prepathlen + 1, GFP_KERNEL);
  2054. if (full_path == NULL)
  2055. return ERR_PTR(-ENOMEM);
  2056. strncpy(full_path, volume_info->UNC, unc_len);
  2057. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  2058. int i;
  2059. for (i = 0; i < unc_len; i++) {
  2060. if (full_path[i] == '\\')
  2061. full_path[i] = '/';
  2062. }
  2063. }
  2064. if (cifs_sb->prepathlen)
  2065. strncpy(full_path + unc_len, cifs_sb->prepath,
  2066. cifs_sb->prepathlen);
  2067. full_path[unc_len + cifs_sb->prepathlen] = 0; /* add trailing null */
  2068. return full_path;
  2069. }
  2070. #endif
  2071. int
  2072. cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
  2073. char *mount_data_global, const char *devname)
  2074. {
  2075. int rc = 0;
  2076. int xid;
  2077. struct smb_vol *volume_info;
  2078. struct cifsSesInfo *pSesInfo = NULL;
  2079. struct cifsTconInfo *tcon = NULL;
  2080. struct TCP_Server_Info *srvTcp = NULL;
  2081. char *full_path;
  2082. char *mount_data = mount_data_global;
  2083. #ifdef CONFIG_CIFS_DFS_UPCALL
  2084. struct dfs_info3_param *referrals = NULL;
  2085. unsigned int num_referrals = 0;
  2086. int referral_walks_count = 0;
  2087. try_mount_again:
  2088. #endif
  2089. full_path = NULL;
  2090. xid = GetXid();
  2091. volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
  2092. if (!volume_info) {
  2093. rc = -ENOMEM;
  2094. goto out;
  2095. }
  2096. if (cifs_parse_mount_options(mount_data, devname, volume_info)) {
  2097. rc = -EINVAL;
  2098. goto out;
  2099. }
  2100. if (volume_info->nullauth) {
  2101. cFYI(1, ("null user"));
  2102. volume_info->username = "";
  2103. } else if (volume_info->username) {
  2104. /* BB fixme parse for domain name here */
  2105. cFYI(1, ("Username: %s", volume_info->username));
  2106. } else {
  2107. cifserror("No username specified");
  2108. /* In userspace mount helper we can get user name from alternate
  2109. locations such as env variables and files on disk */
  2110. rc = -EINVAL;
  2111. goto out;
  2112. }
  2113. /* this is needed for ASCII cp to Unicode converts */
  2114. if (volume_info->iocharset == NULL) {
  2115. cifs_sb->local_nls = load_nls_default();
  2116. /* load_nls_default can not return null */
  2117. } else {
  2118. cifs_sb->local_nls = load_nls(volume_info->iocharset);
  2119. if (cifs_sb->local_nls == NULL) {
  2120. cERROR(1, ("CIFS mount error: iocharset %s not found",
  2121. volume_info->iocharset));
  2122. rc = -ELIBACC;
  2123. goto out;
  2124. }
  2125. }
  2126. /* get a reference to a tcp session */
  2127. srvTcp = cifs_get_tcp_session(volume_info);
  2128. if (IS_ERR(srvTcp)) {
  2129. rc = PTR_ERR(srvTcp);
  2130. goto out;
  2131. }
  2132. pSesInfo = cifs_find_smb_ses(srvTcp, volume_info->username);
  2133. if (pSesInfo) {
  2134. cFYI(1, ("Existing smb sess found (status=%d)",
  2135. pSesInfo->status));
  2136. /*
  2137. * The existing SMB session already has a reference to srvTcp,
  2138. * so we can put back the extra one we got before
  2139. */
  2140. cifs_put_tcp_session(srvTcp);
  2141. down(&pSesInfo->sesSem);
  2142. if (pSesInfo->need_reconnect) {
  2143. cFYI(1, ("Session needs reconnect"));
  2144. rc = cifs_setup_session(xid, pSesInfo,
  2145. cifs_sb->local_nls);
  2146. }
  2147. up(&pSesInfo->sesSem);
  2148. } else if (!rc) {
  2149. cFYI(1, ("Existing smb sess not found"));
  2150. pSesInfo = sesInfoAlloc();
  2151. if (pSesInfo == NULL) {
  2152. rc = -ENOMEM;
  2153. goto mount_fail_check;
  2154. }
  2155. /* new SMB session uses our srvTcp ref */
  2156. pSesInfo->server = srvTcp;
  2157. if (srvTcp->addr.sockAddr6.sin6_family == AF_INET6)
  2158. sprintf(pSesInfo->serverName, "%pI6",
  2159. &srvTcp->addr.sockAddr6.sin6_addr);
  2160. else
  2161. sprintf(pSesInfo->serverName, "%pI4",
  2162. &srvTcp->addr.sockAddr.sin_addr.s_addr);
  2163. write_lock(&cifs_tcp_ses_lock);
  2164. list_add(&pSesInfo->smb_ses_list, &srvTcp->smb_ses_list);
  2165. write_unlock(&cifs_tcp_ses_lock);
  2166. /* volume_info->password freed at unmount */
  2167. if (volume_info->password) {
  2168. pSesInfo->password = kstrdup(volume_info->password,
  2169. GFP_KERNEL);
  2170. if (!pSesInfo->password) {
  2171. rc = -ENOMEM;
  2172. goto mount_fail_check;
  2173. }
  2174. }
  2175. if (volume_info->username)
  2176. strncpy(pSesInfo->userName, volume_info->username,
  2177. MAX_USERNAME_SIZE);
  2178. if (volume_info->domainname) {
  2179. int len = strlen(volume_info->domainname);
  2180. pSesInfo->domainName = kmalloc(len + 1, GFP_KERNEL);
  2181. if (pSesInfo->domainName)
  2182. strcpy(pSesInfo->domainName,
  2183. volume_info->domainname);
  2184. }
  2185. pSesInfo->linux_uid = volume_info->linux_uid;
  2186. pSesInfo->overrideSecFlg = volume_info->secFlg;
  2187. down(&pSesInfo->sesSem);
  2188. /* BB FIXME need to pass vol->secFlgs BB */
  2189. rc = cifs_setup_session(xid, pSesInfo,
  2190. cifs_sb->local_nls);
  2191. up(&pSesInfo->sesSem);
  2192. }
  2193. /* search for existing tcon to this server share */
  2194. if (!rc) {
  2195. setup_cifs_sb(volume_info, cifs_sb);
  2196. tcon = cifs_find_tcon(pSesInfo, volume_info->UNC);
  2197. if (tcon) {
  2198. cFYI(1, ("Found match on UNC path"));
  2199. /* existing tcon already has a reference */
  2200. cifs_put_smb_ses(pSesInfo);
  2201. if (tcon->seal != volume_info->seal)
  2202. cERROR(1, ("transport encryption setting "
  2203. "conflicts with existing tid"));
  2204. } else {
  2205. tcon = tconInfoAlloc();
  2206. if (tcon == NULL) {
  2207. rc = -ENOMEM;
  2208. goto mount_fail_check;
  2209. }
  2210. tcon->ses = pSesInfo;
  2211. if (volume_info->password) {
  2212. tcon->password = kstrdup(volume_info->password,
  2213. GFP_KERNEL);
  2214. if (!tcon->password) {
  2215. rc = -ENOMEM;
  2216. goto mount_fail_check;
  2217. }
  2218. }
  2219. if ((strchr(volume_info->UNC + 3, '\\') == NULL)
  2220. && (strchr(volume_info->UNC + 3, '/') == NULL)) {
  2221. cERROR(1, ("Missing share name"));
  2222. rc = -ENODEV;
  2223. goto mount_fail_check;
  2224. } else {
  2225. /* BB Do we need to wrap sesSem around
  2226. * this TCon call and Unix SetFS as
  2227. * we do on SessSetup and reconnect? */
  2228. rc = CIFSTCon(xid, pSesInfo, volume_info->UNC,
  2229. tcon, cifs_sb->local_nls);
  2230. cFYI(1, ("CIFS Tcon rc = %d", rc));
  2231. if (volume_info->nodfs) {
  2232. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  2233. cFYI(1, ("DFS disabled (%d)",
  2234. tcon->Flags));
  2235. }
  2236. }
  2237. if (rc)
  2238. goto remote_path_check;
  2239. tcon->seal = volume_info->seal;
  2240. write_lock(&cifs_tcp_ses_lock);
  2241. list_add(&tcon->tcon_list, &pSesInfo->tcon_list);
  2242. write_unlock(&cifs_tcp_ses_lock);
  2243. }
  2244. /* we can have only one retry value for a connection
  2245. to a share so for resources mounted more than once
  2246. to the same server share the last value passed in
  2247. for the retry flag is used */
  2248. tcon->retry = volume_info->retry;
  2249. tcon->nocase = volume_info->nocase;
  2250. tcon->local_lease = volume_info->local_lease;
  2251. }
  2252. if (pSesInfo) {
  2253. if (pSesInfo->capabilities & CAP_LARGE_FILES) {
  2254. sb->s_maxbytes = (u64) 1 << 63;
  2255. } else
  2256. sb->s_maxbytes = (u64) 1 << 31; /* 2 GB */
  2257. }
  2258. /* BB FIXME fix time_gran to be larger for LANMAN sessions */
  2259. sb->s_time_gran = 100;
  2260. if (rc)
  2261. goto remote_path_check;
  2262. cifs_sb->tcon = tcon;
  2263. /* do not care if following two calls succeed - informational */
  2264. if (!tcon->ipc) {
  2265. CIFSSMBQFSDeviceInfo(xid, tcon);
  2266. CIFSSMBQFSAttributeInfo(xid, tcon);
  2267. }
  2268. /* tell server which Unix caps we support */
  2269. if (tcon->ses->capabilities & CAP_UNIX)
  2270. /* reset of caps checks mount to see if unix extensions
  2271. disabled for just this mount */
  2272. reset_cifs_unix_caps(xid, tcon, sb, volume_info);
  2273. else
  2274. tcon->unix_ext = 0; /* server does not support them */
  2275. /* convert forward to back slashes in prepath here if needed */
  2276. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
  2277. convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
  2278. if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
  2279. cifs_sb->rsize = 1024 * 127;
  2280. cFYI(DBG2, ("no very large read support, rsize now 127K"));
  2281. }
  2282. if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
  2283. cifs_sb->wsize = min(cifs_sb->wsize,
  2284. (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
  2285. if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
  2286. cifs_sb->rsize = min(cifs_sb->rsize,
  2287. (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
  2288. remote_path_check:
  2289. /* check if a whole path (including prepath) is not remote */
  2290. if (!rc && cifs_sb->prepathlen && tcon) {
  2291. /* build_path_to_root works only when we have a valid tcon */
  2292. full_path = cifs_build_path_to_root(cifs_sb);
  2293. if (full_path == NULL) {
  2294. rc = -ENOMEM;
  2295. goto mount_fail_check;
  2296. }
  2297. rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
  2298. if (rc != -EREMOTE) {
  2299. kfree(full_path);
  2300. goto mount_fail_check;
  2301. }
  2302. kfree(full_path);
  2303. }
  2304. /* get referral if needed */
  2305. if (rc == -EREMOTE) {
  2306. #ifdef CONFIG_CIFS_DFS_UPCALL
  2307. if (referral_walks_count > MAX_NESTED_LINKS) {
  2308. /*
  2309. * BB: when we implement proper loop detection,
  2310. * we will remove this check. But now we need it
  2311. * to prevent an indefinite loop if 'DFS tree' is
  2312. * misconfigured (i.e. has loops).
  2313. */
  2314. rc = -ELOOP;
  2315. goto mount_fail_check;
  2316. }
  2317. /* convert forward to back slashes in prepath here if needed */
  2318. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
  2319. convert_delimiter(cifs_sb->prepath,
  2320. CIFS_DIR_SEP(cifs_sb));
  2321. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  2322. if (IS_ERR(full_path)) {
  2323. rc = PTR_ERR(full_path);
  2324. goto mount_fail_check;
  2325. }
  2326. cFYI(1, ("Getting referral for: %s", full_path));
  2327. rc = get_dfs_path(xid, pSesInfo , full_path + 1,
  2328. cifs_sb->local_nls, &num_referrals, &referrals,
  2329. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  2330. if (!rc && num_referrals > 0) {
  2331. char *fake_devname = NULL;
  2332. if (mount_data != mount_data_global)
  2333. kfree(mount_data);
  2334. mount_data = cifs_compose_mount_options(
  2335. cifs_sb->mountdata, full_path + 1,
  2336. referrals, &fake_devname);
  2337. kfree(fake_devname);
  2338. free_dfs_info_array(referrals, num_referrals);
  2339. if (tcon)
  2340. cifs_put_tcon(tcon);
  2341. else if (pSesInfo)
  2342. cifs_put_smb_ses(pSesInfo);
  2343. cleanup_volume_info(&volume_info);
  2344. FreeXid(xid);
  2345. kfree(full_path);
  2346. referral_walks_count++;
  2347. goto try_mount_again;
  2348. }
  2349. #else /* No DFS support, return error on mount */
  2350. rc = -EOPNOTSUPP;
  2351. #endif
  2352. }
  2353. mount_fail_check:
  2354. /* on error free sesinfo and tcon struct if needed */
  2355. if (rc) {
  2356. if (mount_data != mount_data_global)
  2357. kfree(mount_data);
  2358. /* If find_unc succeeded then rc == 0 so we can not end */
  2359. /* up accidently freeing someone elses tcon struct */
  2360. if (tcon)
  2361. cifs_put_tcon(tcon);
  2362. else if (pSesInfo)
  2363. cifs_put_smb_ses(pSesInfo);
  2364. else
  2365. cifs_put_tcp_session(srvTcp);
  2366. goto out;
  2367. }
  2368. /* volume_info->password is freed above when existing session found
  2369. (in which case it is not needed anymore) but when new sesion is created
  2370. the password ptr is put in the new session structure (in which case the
  2371. password will be freed at unmount time) */
  2372. out:
  2373. /* zero out password before freeing */
  2374. cleanup_volume_info(&volume_info);
  2375. FreeXid(xid);
  2376. return rc;
  2377. }
  2378. int
  2379. CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
  2380. const char *tree, struct cifsTconInfo *tcon,
  2381. const struct nls_table *nls_codepage)
  2382. {
  2383. struct smb_hdr *smb_buffer;
  2384. struct smb_hdr *smb_buffer_response;
  2385. TCONX_REQ *pSMB;
  2386. TCONX_RSP *pSMBr;
  2387. unsigned char *bcc_ptr;
  2388. int rc = 0;
  2389. int length, bytes_left;
  2390. __u16 count;
  2391. if (ses == NULL)
  2392. return -EIO;
  2393. smb_buffer = cifs_buf_get();
  2394. if (smb_buffer == NULL) {
  2395. return -ENOMEM;
  2396. }
  2397. smb_buffer_response = smb_buffer;
  2398. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  2399. NULL /*no tid */ , 4 /*wct */ );
  2400. smb_buffer->Mid = GetNextMid(ses->server);
  2401. smb_buffer->Uid = ses->Suid;
  2402. pSMB = (TCONX_REQ *) smb_buffer;
  2403. pSMBr = (TCONX_RSP *) smb_buffer_response;
  2404. pSMB->AndXCommand = 0xFF;
  2405. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  2406. bcc_ptr = &pSMB->Password[0];
  2407. if ((ses->server->secMode) & SECMODE_USER) {
  2408. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  2409. *bcc_ptr = 0; /* password is null byte */
  2410. bcc_ptr++; /* skip password */
  2411. /* already aligned so no need to do it below */
  2412. } else {
  2413. pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2414. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  2415. specified as required (when that support is added to
  2416. the vfs in the future) as only NTLM or the much
  2417. weaker LANMAN (which we do not send by default) is accepted
  2418. by Samba (not sure whether other servers allow
  2419. NTLMv2 password here) */
  2420. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  2421. if ((extended_security & CIFSSEC_MAY_LANMAN) &&
  2422. (ses->server->secType == LANMAN))
  2423. calc_lanman_hash(tcon->password, ses->server->cryptKey,
  2424. ses->server->secMode &
  2425. SECMODE_PW_ENCRYPT ? true : false,
  2426. bcc_ptr);
  2427. else
  2428. #endif /* CIFS_WEAK_PW_HASH */
  2429. SMBNTencrypt(tcon->password, ses->server->cryptKey,
  2430. bcc_ptr);
  2431. bcc_ptr += CIFS_SESS_KEY_SIZE;
  2432. if (ses->capabilities & CAP_UNICODE) {
  2433. /* must align unicode strings */
  2434. *bcc_ptr = 0; /* null byte password */
  2435. bcc_ptr++;
  2436. }
  2437. }
  2438. if (ses->server->secMode &
  2439. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2440. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2441. if (ses->capabilities & CAP_STATUS32) {
  2442. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2443. }
  2444. if (ses->capabilities & CAP_DFS) {
  2445. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2446. }
  2447. if (ses->capabilities & CAP_UNICODE) {
  2448. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2449. length =
  2450. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  2451. 6 /* max utf8 char length in bytes */ *
  2452. (/* server len*/ + 256 /* share len */), nls_codepage);
  2453. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  2454. bcc_ptr += 2; /* skip trailing null */
  2455. } else { /* ASCII */
  2456. strcpy(bcc_ptr, tree);
  2457. bcc_ptr += strlen(tree) + 1;
  2458. }
  2459. strcpy(bcc_ptr, "?????");
  2460. bcc_ptr += strlen("?????");
  2461. bcc_ptr += 1;
  2462. count = bcc_ptr - &pSMB->Password[0];
  2463. pSMB->hdr.smb_buf_length += count;
  2464. pSMB->ByteCount = cpu_to_le16(count);
  2465. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  2466. CIFS_STD_OP);
  2467. /* above now done in SendReceive */
  2468. if ((rc == 0) && (tcon != NULL)) {
  2469. bool is_unicode;
  2470. tcon->tidStatus = CifsGood;
  2471. tcon->need_reconnect = false;
  2472. tcon->tid = smb_buffer_response->Tid;
  2473. bcc_ptr = pByteArea(smb_buffer_response);
  2474. bytes_left = BCC(smb_buffer_response);
  2475. length = strnlen(bcc_ptr, bytes_left - 2);
  2476. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  2477. is_unicode = true;
  2478. else
  2479. is_unicode = false;
  2480. /* skip service field (NB: this field is always ASCII) */
  2481. if (length == 3) {
  2482. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  2483. (bcc_ptr[2] == 'C')) {
  2484. cFYI(1, ("IPC connection"));
  2485. tcon->ipc = 1;
  2486. }
  2487. } else if (length == 2) {
  2488. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  2489. /* the most common case */
  2490. cFYI(1, ("disk share connection"));
  2491. }
  2492. }
  2493. bcc_ptr += length + 1;
  2494. bytes_left -= (length + 1);
  2495. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  2496. /* mostly informational -- no need to fail on error here */
  2497. tcon->nativeFileSystem = cifs_strndup_from_ucs(bcc_ptr,
  2498. bytes_left, is_unicode,
  2499. nls_codepage);
  2500. cFYI(1, ("nativeFileSystem=%s", tcon->nativeFileSystem));
  2501. if ((smb_buffer_response->WordCount == 3) ||
  2502. (smb_buffer_response->WordCount == 7))
  2503. /* field is in same location */
  2504. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  2505. else
  2506. tcon->Flags = 0;
  2507. cFYI(1, ("Tcon flags: 0x%x ", tcon->Flags));
  2508. } else if ((rc == 0) && tcon == NULL) {
  2509. /* all we need to save for IPC$ connection */
  2510. ses->ipc_tid = smb_buffer_response->Tid;
  2511. }
  2512. cifs_buf_release(smb_buffer);
  2513. return rc;
  2514. }
  2515. int
  2516. cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
  2517. {
  2518. int rc = 0;
  2519. char *tmp;
  2520. if (cifs_sb->tcon)
  2521. cifs_put_tcon(cifs_sb->tcon);
  2522. cifs_sb->tcon = NULL;
  2523. tmp = cifs_sb->prepath;
  2524. cifs_sb->prepathlen = 0;
  2525. cifs_sb->prepath = NULL;
  2526. kfree(tmp);
  2527. return rc;
  2528. }
  2529. int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
  2530. struct nls_table *nls_info)
  2531. {
  2532. int rc = 0;
  2533. int first_time = 0;
  2534. struct TCP_Server_Info *server = pSesInfo->server;
  2535. /* what if server changes its buffer size after dropping the session? */
  2536. if (server->maxBuf == 0) /* no need to send on reconnect */ {
  2537. rc = CIFSSMBNegotiate(xid, pSesInfo);
  2538. if (rc == -EAGAIN) {
  2539. /* retry only once on 1st time connection */
  2540. rc = CIFSSMBNegotiate(xid, pSesInfo);
  2541. if (rc == -EAGAIN)
  2542. rc = -EHOSTDOWN;
  2543. }
  2544. if (rc == 0) {
  2545. spin_lock(&GlobalMid_Lock);
  2546. if (server->tcpStatus != CifsExiting)
  2547. server->tcpStatus = CifsGood;
  2548. else
  2549. rc = -EHOSTDOWN;
  2550. spin_unlock(&GlobalMid_Lock);
  2551. }
  2552. first_time = 1;
  2553. }
  2554. if (rc)
  2555. goto ss_err_exit;
  2556. pSesInfo->flags = 0;
  2557. pSesInfo->capabilities = server->capabilities;
  2558. if (linuxExtEnabled == 0)
  2559. pSesInfo->capabilities &= (~CAP_UNIX);
  2560. cFYI(1, ("Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  2561. server->secMode, server->capabilities, server->timeAdj));
  2562. rc = CIFS_SessSetup(xid, pSesInfo, first_time, nls_info);
  2563. if (rc) {
  2564. cERROR(1, ("Send error in SessSetup = %d", rc));
  2565. } else {
  2566. cFYI(1, ("CIFS Session Established successfully"));
  2567. spin_lock(&GlobalMid_Lock);
  2568. pSesInfo->status = CifsGood;
  2569. pSesInfo->need_reconnect = false;
  2570. spin_unlock(&GlobalMid_Lock);
  2571. }
  2572. ss_err_exit:
  2573. return rc;
  2574. }