connect.c 81 KB

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