connect.c 85 KB

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