smb2pdu.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2012
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * Contains the routines for constructing the SMB2 PDUs themselves
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
  26. /* Note that there are handle based routines which must be */
  27. /* treated slightly differently for reconnection purposes since we never */
  28. /* want to reuse a stale file handle and only the caller knows the file info */
  29. #include <linux/fs.h>
  30. #include <linux/kernel.h>
  31. #include <linux/vfs.h>
  32. #include <linux/task_io_accounting_ops.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/xattr.h>
  36. #include "smb2pdu.h"
  37. #include "cifsglob.h"
  38. #include "cifsacl.h"
  39. #include "cifsproto.h"
  40. #include "smb2proto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "ntlmssp.h"
  44. #include "smb2status.h"
  45. #include "smb2glob.h"
  46. #include "cifspdu.h"
  47. /*
  48. * The following table defines the expected "StructureSize" of SMB2 requests
  49. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  50. *
  51. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  52. * indexed by command in host byte order.
  53. */
  54. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  55. /* SMB2_NEGOTIATE */ 36,
  56. /* SMB2_SESSION_SETUP */ 25,
  57. /* SMB2_LOGOFF */ 4,
  58. /* SMB2_TREE_CONNECT */ 9,
  59. /* SMB2_TREE_DISCONNECT */ 4,
  60. /* SMB2_CREATE */ 57,
  61. /* SMB2_CLOSE */ 24,
  62. /* SMB2_FLUSH */ 24,
  63. /* SMB2_READ */ 49,
  64. /* SMB2_WRITE */ 49,
  65. /* SMB2_LOCK */ 48,
  66. /* SMB2_IOCTL */ 57,
  67. /* SMB2_CANCEL */ 4,
  68. /* SMB2_ECHO */ 4,
  69. /* SMB2_QUERY_DIRECTORY */ 33,
  70. /* SMB2_CHANGE_NOTIFY */ 32,
  71. /* SMB2_QUERY_INFO */ 41,
  72. /* SMB2_SET_INFO */ 33,
  73. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  74. };
  75. static void
  76. smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
  77. const struct cifs_tcon *tcon)
  78. {
  79. struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
  80. char *temp = (char *)hdr;
  81. /* lookup word count ie StructureSize from table */
  82. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
  83. /*
  84. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  85. * largest operations (Create)
  86. */
  87. memset(temp, 0, 256);
  88. /* Note this is only network field converted to big endian */
  89. hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
  90. - 4 /* RFC 1001 length field itself not counted */);
  91. hdr->ProtocolId[0] = 0xFE;
  92. hdr->ProtocolId[1] = 'S';
  93. hdr->ProtocolId[2] = 'M';
  94. hdr->ProtocolId[3] = 'B';
  95. hdr->StructureSize = cpu_to_le16(64);
  96. hdr->Command = smb2_cmd;
  97. hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
  98. hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
  99. if (!tcon)
  100. goto out;
  101. hdr->TreeId = tcon->tid;
  102. /* Uid is not converted */
  103. if (tcon->ses)
  104. hdr->SessionId = tcon->ses->Suid;
  105. /* BB check following DFS flags BB */
  106. /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
  107. if (tcon->share_flags & SHI1005_FLAGS_DFS)
  108. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS;
  109. /* BB how does SMB2 do case sensitive? */
  110. /* if (tcon->nocase)
  111. hdr->Flags |= SMBFLG_CASELESS; */
  112. if (tcon->ses && tcon->ses->server &&
  113. (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
  114. hdr->Flags |= SMB2_FLAGS_SIGNED;
  115. out:
  116. pdu->StructureSize2 = cpu_to_le16(parmsize);
  117. return;
  118. }
  119. static int
  120. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  121. {
  122. int rc = 0;
  123. struct nls_table *nls_codepage;
  124. struct cifs_ses *ses;
  125. struct TCP_Server_Info *server;
  126. /*
  127. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  128. * check for tcp and smb session status done differently
  129. * for those three - in the calling routine.
  130. */
  131. if (tcon == NULL)
  132. return rc;
  133. if (smb2_command == SMB2_TREE_CONNECT)
  134. return rc;
  135. if (tcon->tidStatus == CifsExiting) {
  136. /*
  137. * only tree disconnect, open, and write,
  138. * (and ulogoff which does not have tcon)
  139. * are allowed as we start force umount.
  140. */
  141. if ((smb2_command != SMB2_WRITE) &&
  142. (smb2_command != SMB2_CREATE) &&
  143. (smb2_command != SMB2_TREE_DISCONNECT)) {
  144. cifs_dbg(FYI, "can not send cmd %d while umounting\n",
  145. smb2_command);
  146. return -ENODEV;
  147. }
  148. }
  149. if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
  150. (!tcon->ses->server))
  151. return -EIO;
  152. ses = tcon->ses;
  153. server = ses->server;
  154. /*
  155. * Give demultiplex thread up to 10 seconds to reconnect, should be
  156. * greater than cifs socket timeout which is 7 seconds
  157. */
  158. while (server->tcpStatus == CifsNeedReconnect) {
  159. /*
  160. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  161. * here since they are implicitly done when session drops.
  162. */
  163. switch (smb2_command) {
  164. /*
  165. * BB Should we keep oplock break and add flush to exceptions?
  166. */
  167. case SMB2_TREE_DISCONNECT:
  168. case SMB2_CANCEL:
  169. case SMB2_CLOSE:
  170. case SMB2_OPLOCK_BREAK:
  171. return -EAGAIN;
  172. }
  173. wait_event_interruptible_timeout(server->response_q,
  174. (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
  175. /* are we still trying to reconnect? */
  176. if (server->tcpStatus != CifsNeedReconnect)
  177. break;
  178. /*
  179. * on "soft" mounts we wait once. Hard mounts keep
  180. * retrying until process is killed or server comes
  181. * back on-line
  182. */
  183. if (!tcon->retry) {
  184. cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
  185. return -EHOSTDOWN;
  186. }
  187. }
  188. if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
  189. return rc;
  190. nls_codepage = load_nls_default();
  191. /*
  192. * need to prevent multiple threads trying to simultaneously reconnect
  193. * the same SMB session
  194. */
  195. mutex_lock(&tcon->ses->session_mutex);
  196. rc = cifs_negotiate_protocol(0, tcon->ses);
  197. if (!rc && tcon->ses->need_reconnect)
  198. rc = cifs_setup_session(0, tcon->ses, nls_codepage);
  199. if (rc || !tcon->need_reconnect) {
  200. mutex_unlock(&tcon->ses->session_mutex);
  201. goto out;
  202. }
  203. cifs_mark_open_files_invalid(tcon);
  204. rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
  205. mutex_unlock(&tcon->ses->session_mutex);
  206. cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
  207. if (rc)
  208. goto out;
  209. atomic_inc(&tconInfoReconnectCount);
  210. /*
  211. * BB FIXME add code to check if wsize needs update due to negotiated
  212. * smb buffer size shrinking.
  213. */
  214. out:
  215. /*
  216. * Check if handle based operation so we know whether we can continue
  217. * or not without returning to caller to reset file handle.
  218. */
  219. /*
  220. * BB Is flush done by server on drop of tcp session? Should we special
  221. * case it and skip above?
  222. */
  223. switch (smb2_command) {
  224. case SMB2_FLUSH:
  225. case SMB2_READ:
  226. case SMB2_WRITE:
  227. case SMB2_LOCK:
  228. case SMB2_IOCTL:
  229. case SMB2_QUERY_DIRECTORY:
  230. case SMB2_CHANGE_NOTIFY:
  231. case SMB2_QUERY_INFO:
  232. case SMB2_SET_INFO:
  233. return -EAGAIN;
  234. }
  235. unload_nls(nls_codepage);
  236. return rc;
  237. }
  238. /*
  239. * Allocate and return pointer to an SMB request hdr, and set basic
  240. * SMB information in the SMB header. If the return code is zero, this
  241. * function must have filled in request_buf pointer.
  242. */
  243. static int
  244. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  245. void **request_buf)
  246. {
  247. int rc = 0;
  248. rc = smb2_reconnect(smb2_command, tcon);
  249. if (rc)
  250. return rc;
  251. /* BB eventually switch this to SMB2 specific small buf size */
  252. *request_buf = cifs_small_buf_get();
  253. if (*request_buf == NULL) {
  254. /* BB should we add a retry in here if not a writepage? */
  255. return -ENOMEM;
  256. }
  257. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  258. if (tcon != NULL) {
  259. #ifdef CONFIG_CIFS_STATS2
  260. uint16_t com_code = le16_to_cpu(smb2_command);
  261. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  262. #endif
  263. cifs_stats_inc(&tcon->num_smbs_sent);
  264. }
  265. return rc;
  266. }
  267. static void
  268. free_rsp_buf(int resp_buftype, void *rsp)
  269. {
  270. if (resp_buftype == CIFS_SMALL_BUFFER)
  271. cifs_small_buf_release(rsp);
  272. else if (resp_buftype == CIFS_LARGE_BUFFER)
  273. cifs_buf_release(rsp);
  274. }
  275. /*
  276. *
  277. * SMB2 Worker functions follow:
  278. *
  279. * The general structure of the worker functions is:
  280. * 1) Call smb2_init (assembles SMB2 header)
  281. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  282. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  283. * 4) Decode SMB2 command specific fields in the fixed length area
  284. * 5) Decode variable length data area (if any for this SMB2 command type)
  285. * 6) Call free smb buffer
  286. * 7) return
  287. *
  288. */
  289. int
  290. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  291. {
  292. struct smb2_negotiate_req *req;
  293. struct smb2_negotiate_rsp *rsp;
  294. struct kvec iov[1];
  295. int rc = 0;
  296. int resp_buftype;
  297. struct TCP_Server_Info *server = ses->server;
  298. unsigned int sec_flags;
  299. u16 temp = 0;
  300. int blob_offset, blob_length;
  301. char *security_blob;
  302. int flags = CIFS_NEG_OP;
  303. cifs_dbg(FYI, "Negotiate protocol\n");
  304. if (!server) {
  305. WARN(1, "%s: server is NULL!\n", __func__);
  306. return -EIO;
  307. }
  308. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  309. if (rc)
  310. return rc;
  311. /* if any of auth flags (ie not sign or seal) are overriden use them */
  312. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  313. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  314. else /* if override flags set only sign/seal OR them with global auth */
  315. sec_flags = global_secflags | ses->overrideSecFlg;
  316. cifs_dbg(FYI, "sec_flags 0x%x\n", sec_flags);
  317. req->hdr.SessionId = 0;
  318. req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
  319. req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
  320. inc_rfc1001_len(req, 2);
  321. /* only one of SMB2 signing flags may be set in SMB2 request */
  322. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  323. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  324. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  325. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  326. req->SecurityMode = cpu_to_le16(temp);
  327. req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
  328. memcpy(req->ClientGUID, cifs_client_guid, SMB2_CLIENT_GUID_SIZE);
  329. iov[0].iov_base = (char *)req;
  330. /* 4 for rfc1002 length field */
  331. iov[0].iov_len = get_rfc1002_length(req) + 4;
  332. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  333. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  334. /*
  335. * No tcon so can't do
  336. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  337. */
  338. if (rc != 0)
  339. goto neg_exit;
  340. cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
  341. /* BB we may eventually want to match the negotiated vs. requested
  342. dialect, even though we are only requesting one at a time */
  343. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
  344. cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
  345. else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
  346. cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
  347. else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
  348. cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
  349. else {
  350. cifs_dbg(VFS, "Illegal dialect returned by server %d\n",
  351. le16_to_cpu(rsp->DialectRevision));
  352. rc = -EIO;
  353. goto neg_exit;
  354. }
  355. server->dialect = le16_to_cpu(rsp->DialectRevision);
  356. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  357. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  358. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  359. /* BB Do we need to validate the SecurityMode? */
  360. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  361. server->capabilities = le32_to_cpu(rsp->Capabilities);
  362. /* Internal types */
  363. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  364. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  365. &rsp->hdr);
  366. if (blob_length == 0) {
  367. cifs_dbg(VFS, "missing security blob on negprot\n");
  368. rc = -EIO;
  369. goto neg_exit;
  370. }
  371. cifs_dbg(FYI, "sec_flags 0x%x\n", sec_flags);
  372. rc = cifs_enable_signing(server, sec_flags);
  373. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  374. if (rc)
  375. goto neg_exit;
  376. rc = decode_neg_token_init(security_blob, blob_length,
  377. &server->sec_type);
  378. if (rc == 1)
  379. rc = 0;
  380. else if (rc == 0) {
  381. rc = -EIO;
  382. goto neg_exit;
  383. }
  384. #endif
  385. neg_exit:
  386. free_rsp_buf(resp_buftype, rsp);
  387. return rc;
  388. }
  389. int
  390. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  391. const struct nls_table *nls_cp)
  392. {
  393. struct smb2_sess_setup_req *req;
  394. struct smb2_sess_setup_rsp *rsp = NULL;
  395. struct kvec iov[2];
  396. int rc = 0;
  397. int resp_buftype;
  398. __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
  399. struct TCP_Server_Info *server = ses->server;
  400. unsigned int sec_flags;
  401. u8 temp = 0;
  402. u16 blob_length = 0;
  403. char *security_blob;
  404. char *ntlmssp_blob = NULL;
  405. bool use_spnego = false; /* else use raw ntlmssp */
  406. cifs_dbg(FYI, "Session Setup\n");
  407. if (!server) {
  408. WARN(1, "%s: server is NULL!\n", __func__);
  409. return -EIO;
  410. }
  411. /*
  412. * If memory allocation is successful, caller of this function
  413. * frees it.
  414. */
  415. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  416. if (!ses->ntlmssp)
  417. return -ENOMEM;
  418. ses->server->secType = RawNTLMSSP;
  419. ssetup_ntlmssp_authenticate:
  420. if (phase == NtLmChallenge)
  421. phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
  422. rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
  423. if (rc)
  424. return rc;
  425. /* if any of auth flags (ie not sign or seal) are overriden use them */
  426. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  427. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  428. else /* if override flags set only sign/seal OR them with global auth */
  429. sec_flags = global_secflags | ses->overrideSecFlg;
  430. cifs_dbg(FYI, "sec_flags 0x%x\n", sec_flags);
  431. req->hdr.SessionId = 0; /* First session, not a reauthenticate */
  432. req->VcNumber = 0; /* MBZ */
  433. /* to enable echos and oplocks */
  434. req->hdr.CreditRequest = cpu_to_le16(3);
  435. /* only one of SMB2 signing flags may be set in SMB2 request */
  436. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  437. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  438. else if (ses->server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED)
  439. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  440. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  441. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  442. req->SecurityMode = temp;
  443. req->Capabilities = 0;
  444. req->Channel = 0; /* MBZ */
  445. iov[0].iov_base = (char *)req;
  446. /* 4 for rfc1002 length field and 1 for pad */
  447. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  448. if (phase == NtLmNegotiate) {
  449. ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
  450. GFP_KERNEL);
  451. if (ntlmssp_blob == NULL) {
  452. rc = -ENOMEM;
  453. goto ssetup_exit;
  454. }
  455. build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
  456. if (use_spnego) {
  457. /* blob_length = build_spnego_ntlmssp_blob(
  458. &security_blob,
  459. sizeof(struct _NEGOTIATE_MESSAGE),
  460. ntlmssp_blob); */
  461. /* BB eventually need to add this */
  462. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  463. rc = -EOPNOTSUPP;
  464. kfree(ntlmssp_blob);
  465. goto ssetup_exit;
  466. } else {
  467. blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
  468. /* with raw NTLMSSP we don't encapsulate in SPNEGO */
  469. security_blob = ntlmssp_blob;
  470. }
  471. } else if (phase == NtLmAuthenticate) {
  472. req->hdr.SessionId = ses->Suid;
  473. ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
  474. GFP_KERNEL);
  475. if (ntlmssp_blob == NULL) {
  476. rc = -ENOMEM;
  477. goto ssetup_exit;
  478. }
  479. rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
  480. nls_cp);
  481. if (rc) {
  482. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
  483. rc);
  484. goto ssetup_exit; /* BB double check error handling */
  485. }
  486. if (use_spnego) {
  487. /* blob_length = build_spnego_ntlmssp_blob(
  488. &security_blob,
  489. blob_length,
  490. ntlmssp_blob); */
  491. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  492. rc = -EOPNOTSUPP;
  493. kfree(ntlmssp_blob);
  494. goto ssetup_exit;
  495. } else {
  496. security_blob = ntlmssp_blob;
  497. }
  498. } else {
  499. cifs_dbg(VFS, "illegal ntlmssp phase\n");
  500. rc = -EIO;
  501. goto ssetup_exit;
  502. }
  503. /* Testing shows that buffer offset must be at location of Buffer[0] */
  504. req->SecurityBufferOffset =
  505. cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
  506. 1 /* pad */ - 4 /* rfc1001 len */);
  507. req->SecurityBufferLength = cpu_to_le16(blob_length);
  508. iov[1].iov_base = security_blob;
  509. iov[1].iov_len = blob_length;
  510. inc_rfc1001_len(req, blob_length - 1 /* pad */);
  511. /* BB add code to build os and lm fields */
  512. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
  513. CIFS_LOG_ERROR | CIFS_NEG_OP);
  514. kfree(security_blob);
  515. rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
  516. if (resp_buftype != CIFS_NO_BUFFER &&
  517. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
  518. if (phase != NtLmNegotiate) {
  519. cifs_dbg(VFS, "Unexpected more processing error\n");
  520. goto ssetup_exit;
  521. }
  522. if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
  523. le16_to_cpu(rsp->SecurityBufferOffset)) {
  524. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  525. le16_to_cpu(rsp->SecurityBufferOffset));
  526. rc = -EIO;
  527. goto ssetup_exit;
  528. }
  529. /* NTLMSSP Negotiate sent now processing challenge (response) */
  530. phase = NtLmChallenge; /* process ntlmssp challenge */
  531. rc = 0; /* MORE_PROCESSING is not an error here but expected */
  532. ses->Suid = rsp->hdr.SessionId;
  533. rc = decode_ntlmssp_challenge(rsp->Buffer,
  534. le16_to_cpu(rsp->SecurityBufferLength), ses);
  535. }
  536. /*
  537. * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
  538. * but at least the raw NTLMSSP case works.
  539. */
  540. /*
  541. * No tcon so can't do
  542. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  543. */
  544. if (rc != 0)
  545. goto ssetup_exit;
  546. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  547. ssetup_exit:
  548. free_rsp_buf(resp_buftype, rsp);
  549. /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
  550. if ((phase == NtLmChallenge) && (rc == 0))
  551. goto ssetup_ntlmssp_authenticate;
  552. return rc;
  553. }
  554. int
  555. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  556. {
  557. struct smb2_logoff_req *req; /* response is also trivial struct */
  558. int rc = 0;
  559. struct TCP_Server_Info *server;
  560. cifs_dbg(FYI, "disconnect session %p\n", ses);
  561. if (ses && (ses->server))
  562. server = ses->server;
  563. else
  564. return -EIO;
  565. rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
  566. if (rc)
  567. return rc;
  568. /* since no tcon, smb2_init can not do this, so do here */
  569. req->hdr.SessionId = ses->Suid;
  570. if (server->sec_mode & SECMODE_SIGN_REQUIRED)
  571. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  572. rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
  573. /*
  574. * No tcon so can't do
  575. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  576. */
  577. return rc;
  578. }
  579. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  580. {
  581. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  582. }
  583. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  584. int
  585. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  586. struct cifs_tcon *tcon, const struct nls_table *cp)
  587. {
  588. struct smb2_tree_connect_req *req;
  589. struct smb2_tree_connect_rsp *rsp = NULL;
  590. struct kvec iov[2];
  591. int rc = 0;
  592. int resp_buftype;
  593. int unc_path_len;
  594. struct TCP_Server_Info *server;
  595. __le16 *unc_path = NULL;
  596. cifs_dbg(FYI, "TCON\n");
  597. if ((ses->server) && tree)
  598. server = ses->server;
  599. else
  600. return -EIO;
  601. if (tcon && tcon->bad_network_name)
  602. return -ENOENT;
  603. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  604. if (unc_path == NULL)
  605. return -ENOMEM;
  606. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  607. unc_path_len *= 2;
  608. if (unc_path_len < 2) {
  609. kfree(unc_path);
  610. return -EINVAL;
  611. }
  612. rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
  613. if (rc) {
  614. kfree(unc_path);
  615. return rc;
  616. }
  617. if (tcon == NULL) {
  618. /* since no tcon, smb2_init can not do this, so do here */
  619. req->hdr.SessionId = ses->Suid;
  620. /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
  621. req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
  622. }
  623. iov[0].iov_base = (char *)req;
  624. /* 4 for rfc1002 length field and 1 for pad */
  625. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  626. /* Testing shows that buffer offset must be at location of Buffer[0] */
  627. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  628. - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
  629. req->PathLength = cpu_to_le16(unc_path_len - 2);
  630. iov[1].iov_base = unc_path;
  631. iov[1].iov_len = unc_path_len;
  632. inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
  633. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  634. rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
  635. if (rc != 0) {
  636. if (tcon) {
  637. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  638. tcon->need_reconnect = true;
  639. }
  640. goto tcon_error_exit;
  641. }
  642. if (tcon == NULL) {
  643. ses->ipc_tid = rsp->hdr.TreeId;
  644. goto tcon_exit;
  645. }
  646. if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
  647. cifs_dbg(FYI, "connection to disk share\n");
  648. else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
  649. tcon->ipc = true;
  650. cifs_dbg(FYI, "connection to pipe share\n");
  651. } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
  652. tcon->print = true;
  653. cifs_dbg(FYI, "connection to printer\n");
  654. } else {
  655. cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  656. rc = -EOPNOTSUPP;
  657. goto tcon_error_exit;
  658. }
  659. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  660. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  661. tcon->tidStatus = CifsGood;
  662. tcon->need_reconnect = false;
  663. tcon->tid = rsp->hdr.TreeId;
  664. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  665. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  666. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  667. cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
  668. tcon_exit:
  669. free_rsp_buf(resp_buftype, rsp);
  670. kfree(unc_path);
  671. return rc;
  672. tcon_error_exit:
  673. if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
  674. cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  675. tcon->bad_network_name = true;
  676. }
  677. goto tcon_exit;
  678. }
  679. int
  680. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  681. {
  682. struct smb2_tree_disconnect_req *req; /* response is trivial */
  683. int rc = 0;
  684. struct TCP_Server_Info *server;
  685. struct cifs_ses *ses = tcon->ses;
  686. cifs_dbg(FYI, "Tree Disconnect\n");
  687. if (ses && (ses->server))
  688. server = ses->server;
  689. else
  690. return -EIO;
  691. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  692. return 0;
  693. rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
  694. if (rc)
  695. return rc;
  696. rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
  697. if (rc)
  698. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  699. return rc;
  700. }
  701. static struct create_lease *
  702. create_lease_buf(u8 *lease_key, u8 oplock)
  703. {
  704. struct create_lease *buf;
  705. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  706. if (!buf)
  707. return NULL;
  708. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  709. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  710. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  711. buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING |
  712. SMB2_LEASE_READ_CACHING;
  713. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  714. buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING;
  715. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  716. buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING |
  717. SMB2_LEASE_READ_CACHING |
  718. SMB2_LEASE_WRITE_CACHING;
  719. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  720. (struct create_lease, lcontext));
  721. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  722. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  723. (struct create_lease, Name));
  724. buf->ccontext.NameLength = cpu_to_le16(4);
  725. buf->Name[0] = 'R';
  726. buf->Name[1] = 'q';
  727. buf->Name[2] = 'L';
  728. buf->Name[3] = 's';
  729. return buf;
  730. }
  731. static __u8
  732. parse_lease_state(struct smb2_create_rsp *rsp)
  733. {
  734. char *data_offset;
  735. struct create_lease *lc;
  736. bool found = false;
  737. data_offset = (char *)rsp;
  738. data_offset += 4 + le32_to_cpu(rsp->CreateContextsOffset);
  739. lc = (struct create_lease *)data_offset;
  740. do {
  741. char *name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
  742. if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
  743. strncmp(name, "RqLs", 4)) {
  744. lc = (struct create_lease *)((char *)lc
  745. + le32_to_cpu(lc->ccontext.Next));
  746. continue;
  747. }
  748. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  749. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  750. found = true;
  751. break;
  752. } while (le32_to_cpu(lc->ccontext.Next) != 0);
  753. if (!found)
  754. return 0;
  755. return smb2_map_lease_to_oplock(lc->lcontext.LeaseState);
  756. }
  757. int
  758. SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
  759. u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
  760. __u32 create_disposition, __u32 file_attributes, __u32 create_options,
  761. __u8 *oplock, struct smb2_file_all_info *buf)
  762. {
  763. struct smb2_create_req *req;
  764. struct smb2_create_rsp *rsp;
  765. struct TCP_Server_Info *server;
  766. struct cifs_ses *ses = tcon->ses;
  767. struct kvec iov[3];
  768. int resp_buftype;
  769. int uni_path_len;
  770. __le16 *copy_path = NULL;
  771. int copy_size;
  772. int rc = 0;
  773. int num_iovecs = 2;
  774. cifs_dbg(FYI, "create/open\n");
  775. if (ses && (ses->server))
  776. server = ses->server;
  777. else
  778. return -EIO;
  779. rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
  780. if (rc)
  781. return rc;
  782. req->ImpersonationLevel = IL_IMPERSONATION;
  783. req->DesiredAccess = cpu_to_le32(desired_access);
  784. /* File attributes ignored on open (used in create though) */
  785. req->FileAttributes = cpu_to_le32(file_attributes);
  786. req->ShareAccess = FILE_SHARE_ALL_LE;
  787. req->CreateDisposition = cpu_to_le32(create_disposition);
  788. req->CreateOptions = cpu_to_le32(create_options);
  789. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  790. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)
  791. - 8 /* pad */ - 4 /* do not count rfc1001 len field */);
  792. iov[0].iov_base = (char *)req;
  793. /* 4 for rfc1002 length field */
  794. iov[0].iov_len = get_rfc1002_length(req) + 4;
  795. /* MUST set path len (NameLength) to 0 opening root of share */
  796. if (uni_path_len >= 4) {
  797. req->NameLength = cpu_to_le16(uni_path_len - 2);
  798. /* -1 since last byte is buf[0] which is sent below (path) */
  799. iov[0].iov_len--;
  800. if (uni_path_len % 8 != 0) {
  801. copy_size = uni_path_len / 8 * 8;
  802. if (copy_size < uni_path_len)
  803. copy_size += 8;
  804. copy_path = kzalloc(copy_size, GFP_KERNEL);
  805. if (!copy_path)
  806. return -ENOMEM;
  807. memcpy((char *)copy_path, (const char *)path,
  808. uni_path_len);
  809. uni_path_len = copy_size;
  810. path = copy_path;
  811. }
  812. iov[1].iov_len = uni_path_len;
  813. iov[1].iov_base = path;
  814. /*
  815. * -1 since last byte is buf[0] which was counted in
  816. * smb2_buf_len.
  817. */
  818. inc_rfc1001_len(req, uni_path_len - 1);
  819. } else {
  820. iov[0].iov_len += 7;
  821. req->hdr.smb2_buf_length = cpu_to_be32(be32_to_cpu(
  822. req->hdr.smb2_buf_length) + 8 - 1);
  823. num_iovecs = 1;
  824. req->NameLength = 0;
  825. }
  826. if (!server->oplocks)
  827. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  828. if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  829. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  830. req->RequestedOplockLevel = *oplock;
  831. else {
  832. iov[num_iovecs].iov_base = create_lease_buf(oplock+1, *oplock);
  833. if (iov[num_iovecs].iov_base == NULL) {
  834. cifs_small_buf_release(req);
  835. kfree(copy_path);
  836. return -ENOMEM;
  837. }
  838. iov[num_iovecs].iov_len = sizeof(struct create_lease);
  839. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  840. req->CreateContextsOffset = cpu_to_le32(
  841. sizeof(struct smb2_create_req) - 4 - 8 +
  842. iov[num_iovecs-1].iov_len);
  843. req->CreateContextsLength = cpu_to_le32(
  844. sizeof(struct create_lease));
  845. inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
  846. num_iovecs++;
  847. }
  848. rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
  849. rsp = (struct smb2_create_rsp *)iov[0].iov_base;
  850. if (rc != 0) {
  851. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  852. goto creat_exit;
  853. }
  854. *persistent_fid = rsp->PersistentFileId;
  855. *volatile_fid = rsp->VolatileFileId;
  856. if (buf) {
  857. memcpy(buf, &rsp->CreationTime, 32);
  858. buf->AllocationSize = rsp->AllocationSize;
  859. buf->EndOfFile = rsp->EndofFile;
  860. buf->Attributes = rsp->FileAttributes;
  861. buf->NumberOfLinks = cpu_to_le32(1);
  862. buf->DeletePending = 0;
  863. }
  864. if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
  865. *oplock = parse_lease_state(rsp);
  866. else
  867. *oplock = rsp->OplockLevel;
  868. creat_exit:
  869. kfree(copy_path);
  870. free_rsp_buf(resp_buftype, rsp);
  871. return rc;
  872. }
  873. int
  874. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  875. u64 persistent_fid, u64 volatile_fid)
  876. {
  877. struct smb2_close_req *req;
  878. struct smb2_close_rsp *rsp;
  879. struct TCP_Server_Info *server;
  880. struct cifs_ses *ses = tcon->ses;
  881. struct kvec iov[1];
  882. int resp_buftype;
  883. int rc = 0;
  884. cifs_dbg(FYI, "Close\n");
  885. if (ses && (ses->server))
  886. server = ses->server;
  887. else
  888. return -EIO;
  889. rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
  890. if (rc)
  891. return rc;
  892. req->PersistentFileId = persistent_fid;
  893. req->VolatileFileId = volatile_fid;
  894. iov[0].iov_base = (char *)req;
  895. /* 4 for rfc1002 length field */
  896. iov[0].iov_len = get_rfc1002_length(req) + 4;
  897. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  898. rsp = (struct smb2_close_rsp *)iov[0].iov_base;
  899. if (rc != 0) {
  900. if (tcon)
  901. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  902. goto close_exit;
  903. }
  904. /* BB FIXME - decode close response, update inode for caching */
  905. close_exit:
  906. free_rsp_buf(resp_buftype, rsp);
  907. return rc;
  908. }
  909. static int
  910. validate_buf(unsigned int offset, unsigned int buffer_length,
  911. struct smb2_hdr *hdr, unsigned int min_buf_size)
  912. {
  913. unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
  914. char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
  915. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  916. char *end_of_buf = begin_of_buf + buffer_length;
  917. if (buffer_length < min_buf_size) {
  918. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  919. buffer_length, min_buf_size);
  920. return -EINVAL;
  921. }
  922. /* check if beyond RFC1001 maximum length */
  923. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  924. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  925. buffer_length, smb_len);
  926. return -EINVAL;
  927. }
  928. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  929. cifs_dbg(VFS, "illegal server response, bad offset to data\n");
  930. return -EINVAL;
  931. }
  932. return 0;
  933. }
  934. /*
  935. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  936. * Caller must free buffer.
  937. */
  938. static int
  939. validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
  940. struct smb2_hdr *hdr, unsigned int minbufsize,
  941. char *data)
  942. {
  943. char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
  944. int rc;
  945. if (!data)
  946. return -EINVAL;
  947. rc = validate_buf(offset, buffer_length, hdr, minbufsize);
  948. if (rc)
  949. return rc;
  950. memcpy(data, begin_of_buf, buffer_length);
  951. return 0;
  952. }
  953. static int
  954. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  955. u64 persistent_fid, u64 volatile_fid, u8 info_class,
  956. size_t output_len, size_t min_len, void *data)
  957. {
  958. struct smb2_query_info_req *req;
  959. struct smb2_query_info_rsp *rsp = NULL;
  960. struct kvec iov[2];
  961. int rc = 0;
  962. int resp_buftype;
  963. struct TCP_Server_Info *server;
  964. struct cifs_ses *ses = tcon->ses;
  965. cifs_dbg(FYI, "Query Info\n");
  966. if (ses && (ses->server))
  967. server = ses->server;
  968. else
  969. return -EIO;
  970. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  971. if (rc)
  972. return rc;
  973. req->InfoType = SMB2_O_INFO_FILE;
  974. req->FileInfoClass = info_class;
  975. req->PersistentFileId = persistent_fid;
  976. req->VolatileFileId = volatile_fid;
  977. /* 4 for rfc1002 length field and 1 for Buffer */
  978. req->InputBufferOffset =
  979. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  980. req->OutputBufferLength = cpu_to_le32(output_len);
  981. iov[0].iov_base = (char *)req;
  982. /* 4 for rfc1002 length field */
  983. iov[0].iov_len = get_rfc1002_length(req) + 4;
  984. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  985. rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
  986. if (rc) {
  987. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  988. goto qinf_exit;
  989. }
  990. rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
  991. le32_to_cpu(rsp->OutputBufferLength),
  992. &rsp->hdr, min_len, data);
  993. qinf_exit:
  994. free_rsp_buf(resp_buftype, rsp);
  995. return rc;
  996. }
  997. int
  998. SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  999. u64 persistent_fid, u64 volatile_fid,
  1000. struct smb2_file_all_info *data)
  1001. {
  1002. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1003. FILE_ALL_INFORMATION,
  1004. sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  1005. sizeof(struct smb2_file_all_info), data);
  1006. }
  1007. int
  1008. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  1009. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  1010. {
  1011. return query_info(xid, tcon, persistent_fid, volatile_fid,
  1012. FILE_INTERNAL_INFORMATION,
  1013. sizeof(struct smb2_file_internal_info),
  1014. sizeof(struct smb2_file_internal_info), uniqueid);
  1015. }
  1016. /*
  1017. * This is a no-op for now. We're not really interested in the reply, but
  1018. * rather in the fact that the server sent one and that server->lstrp
  1019. * gets updated.
  1020. *
  1021. * FIXME: maybe we should consider checking that the reply matches request?
  1022. */
  1023. static void
  1024. smb2_echo_callback(struct mid_q_entry *mid)
  1025. {
  1026. struct TCP_Server_Info *server = mid->callback_data;
  1027. struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
  1028. unsigned int credits_received = 1;
  1029. if (mid->mid_state == MID_RESPONSE_RECEIVED)
  1030. credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
  1031. DeleteMidQEntry(mid);
  1032. add_credits(server, credits_received, CIFS_ECHO_OP);
  1033. }
  1034. int
  1035. SMB2_echo(struct TCP_Server_Info *server)
  1036. {
  1037. struct smb2_echo_req *req;
  1038. int rc = 0;
  1039. struct kvec iov;
  1040. struct smb_rqst rqst = { .rq_iov = &iov,
  1041. .rq_nvec = 1 };
  1042. cifs_dbg(FYI, "In echo request\n");
  1043. rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
  1044. if (rc)
  1045. return rc;
  1046. req->hdr.CreditRequest = cpu_to_le16(1);
  1047. iov.iov_base = (char *)req;
  1048. /* 4 for rfc1002 length field */
  1049. iov.iov_len = get_rfc1002_length(req) + 4;
  1050. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
  1051. CIFS_ECHO_OP);
  1052. if (rc)
  1053. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  1054. cifs_small_buf_release(req);
  1055. return rc;
  1056. }
  1057. int
  1058. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1059. u64 volatile_fid)
  1060. {
  1061. struct smb2_flush_req *req;
  1062. struct TCP_Server_Info *server;
  1063. struct cifs_ses *ses = tcon->ses;
  1064. struct kvec iov[1];
  1065. int resp_buftype;
  1066. int rc = 0;
  1067. cifs_dbg(FYI, "Flush\n");
  1068. if (ses && (ses->server))
  1069. server = ses->server;
  1070. else
  1071. return -EIO;
  1072. rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
  1073. if (rc)
  1074. return rc;
  1075. req->PersistentFileId = persistent_fid;
  1076. req->VolatileFileId = volatile_fid;
  1077. iov[0].iov_base = (char *)req;
  1078. /* 4 for rfc1002 length field */
  1079. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1080. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
  1081. if ((rc != 0) && tcon)
  1082. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  1083. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1084. return rc;
  1085. }
  1086. /*
  1087. * To form a chain of read requests, any read requests after the first should
  1088. * have the end_of_chain boolean set to true.
  1089. */
  1090. static int
  1091. smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
  1092. unsigned int remaining_bytes, int request_type)
  1093. {
  1094. int rc = -EACCES;
  1095. struct smb2_read_req *req = NULL;
  1096. rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
  1097. if (rc)
  1098. return rc;
  1099. if (io_parms->tcon->ses->server == NULL)
  1100. return -ECONNABORTED;
  1101. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1102. req->PersistentFileId = io_parms->persistent_fid;
  1103. req->VolatileFileId = io_parms->volatile_fid;
  1104. req->ReadChannelInfoOffset = 0; /* reserved */
  1105. req->ReadChannelInfoLength = 0; /* reserved */
  1106. req->Channel = 0; /* reserved */
  1107. req->MinimumCount = 0;
  1108. req->Length = cpu_to_le32(io_parms->length);
  1109. req->Offset = cpu_to_le64(io_parms->offset);
  1110. if (request_type & CHAINED_REQUEST) {
  1111. if (!(request_type & END_OF_CHAIN)) {
  1112. /* 4 for rfc1002 length field */
  1113. req->hdr.NextCommand =
  1114. cpu_to_le32(get_rfc1002_length(req) + 4);
  1115. } else /* END_OF_CHAIN */
  1116. req->hdr.NextCommand = 0;
  1117. if (request_type & RELATED_REQUEST) {
  1118. req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  1119. /*
  1120. * Related requests use info from previous read request
  1121. * in chain.
  1122. */
  1123. req->hdr.SessionId = 0xFFFFFFFF;
  1124. req->hdr.TreeId = 0xFFFFFFFF;
  1125. req->PersistentFileId = 0xFFFFFFFF;
  1126. req->VolatileFileId = 0xFFFFFFFF;
  1127. }
  1128. }
  1129. if (remaining_bytes > io_parms->length)
  1130. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  1131. else
  1132. req->RemainingBytes = 0;
  1133. iov[0].iov_base = (char *)req;
  1134. /* 4 for rfc1002 length field */
  1135. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1136. return rc;
  1137. }
  1138. static void
  1139. smb2_readv_callback(struct mid_q_entry *mid)
  1140. {
  1141. struct cifs_readdata *rdata = mid->callback_data;
  1142. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1143. struct TCP_Server_Info *server = tcon->ses->server;
  1144. struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1145. unsigned int credits_received = 1;
  1146. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1147. .rq_nvec = 1,
  1148. .rq_pages = rdata->pages,
  1149. .rq_npages = rdata->nr_pages,
  1150. .rq_pagesz = rdata->pagesz,
  1151. .rq_tailsz = rdata->tailsz };
  1152. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1153. __func__, mid->mid, mid->mid_state, rdata->result,
  1154. rdata->bytes);
  1155. switch (mid->mid_state) {
  1156. case MID_RESPONSE_RECEIVED:
  1157. credits_received = le16_to_cpu(buf->CreditRequest);
  1158. /* result already set, check signature */
  1159. if (server->sec_mode &
  1160. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  1161. int rc;
  1162. rc = smb2_verify_signature(&rqst, server);
  1163. if (rc)
  1164. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1165. rc);
  1166. }
  1167. /* FIXME: should this be counted toward the initiating task? */
  1168. task_io_account_read(rdata->bytes);
  1169. cifs_stats_bytes_read(tcon, rdata->bytes);
  1170. break;
  1171. case MID_REQUEST_SUBMITTED:
  1172. case MID_RETRY_NEEDED:
  1173. rdata->result = -EAGAIN;
  1174. break;
  1175. default:
  1176. if (rdata->result != -ENODATA)
  1177. rdata->result = -EIO;
  1178. }
  1179. if (rdata->result)
  1180. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  1181. queue_work(cifsiod_wq, &rdata->work);
  1182. DeleteMidQEntry(mid);
  1183. add_credits(server, credits_received, 0);
  1184. }
  1185. /* smb2_async_readv - send an async write, and set up mid to handle result */
  1186. int
  1187. smb2_async_readv(struct cifs_readdata *rdata)
  1188. {
  1189. int rc;
  1190. struct smb2_hdr *buf;
  1191. struct cifs_io_parms io_parms;
  1192. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1193. .rq_nvec = 1 };
  1194. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1195. __func__, rdata->offset, rdata->bytes);
  1196. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  1197. io_parms.offset = rdata->offset;
  1198. io_parms.length = rdata->bytes;
  1199. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  1200. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  1201. io_parms.pid = rdata->pid;
  1202. rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
  1203. if (rc)
  1204. return rc;
  1205. buf = (struct smb2_hdr *)rdata->iov.iov_base;
  1206. /* 4 for rfc1002 length field */
  1207. rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
  1208. kref_get(&rdata->refcount);
  1209. rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
  1210. cifs_readv_receive, smb2_readv_callback,
  1211. rdata, 0);
  1212. if (rc) {
  1213. kref_put(&rdata->refcount, cifs_readdata_release);
  1214. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  1215. }
  1216. cifs_small_buf_release(buf);
  1217. return rc;
  1218. }
  1219. int
  1220. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  1221. unsigned int *nbytes, char **buf, int *buf_type)
  1222. {
  1223. int resp_buftype, rc = -EACCES;
  1224. struct smb2_read_rsp *rsp = NULL;
  1225. struct kvec iov[1];
  1226. *nbytes = 0;
  1227. rc = smb2_new_read_req(iov, io_parms, 0, 0);
  1228. if (rc)
  1229. return rc;
  1230. rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
  1231. &resp_buftype, CIFS_LOG_ERROR);
  1232. rsp = (struct smb2_read_rsp *)iov[0].iov_base;
  1233. if (rsp->hdr.Status == STATUS_END_OF_FILE) {
  1234. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1235. return 0;
  1236. }
  1237. if (rc) {
  1238. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  1239. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1240. } else {
  1241. *nbytes = le32_to_cpu(rsp->DataLength);
  1242. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  1243. (*nbytes > io_parms->length)) {
  1244. cifs_dbg(FYI, "bad length %d for count %d\n",
  1245. *nbytes, io_parms->length);
  1246. rc = -EIO;
  1247. *nbytes = 0;
  1248. }
  1249. }
  1250. if (*buf) {
  1251. memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
  1252. *nbytes);
  1253. free_rsp_buf(resp_buftype, iov[0].iov_base);
  1254. } else if (resp_buftype != CIFS_NO_BUFFER) {
  1255. *buf = iov[0].iov_base;
  1256. if (resp_buftype == CIFS_SMALL_BUFFER)
  1257. *buf_type = CIFS_SMALL_BUFFER;
  1258. else if (resp_buftype == CIFS_LARGE_BUFFER)
  1259. *buf_type = CIFS_LARGE_BUFFER;
  1260. }
  1261. return rc;
  1262. }
  1263. /*
  1264. * Check the mid_state and signature on received buffer (if any), and queue the
  1265. * workqueue completion task.
  1266. */
  1267. static void
  1268. smb2_writev_callback(struct mid_q_entry *mid)
  1269. {
  1270. struct cifs_writedata *wdata = mid->callback_data;
  1271. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1272. unsigned int written;
  1273. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  1274. unsigned int credits_received = 1;
  1275. switch (mid->mid_state) {
  1276. case MID_RESPONSE_RECEIVED:
  1277. credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
  1278. wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
  1279. if (wdata->result != 0)
  1280. break;
  1281. written = le32_to_cpu(rsp->DataLength);
  1282. /*
  1283. * Mask off high 16 bits when bytes written as returned
  1284. * by the server is greater than bytes requested by the
  1285. * client. OS/2 servers are known to set incorrect
  1286. * CountHigh values.
  1287. */
  1288. if (written > wdata->bytes)
  1289. written &= 0xFFFF;
  1290. if (written < wdata->bytes)
  1291. wdata->result = -ENOSPC;
  1292. else
  1293. wdata->bytes = written;
  1294. break;
  1295. case MID_REQUEST_SUBMITTED:
  1296. case MID_RETRY_NEEDED:
  1297. wdata->result = -EAGAIN;
  1298. break;
  1299. default:
  1300. wdata->result = -EIO;
  1301. break;
  1302. }
  1303. if (wdata->result)
  1304. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1305. queue_work(cifsiod_wq, &wdata->work);
  1306. DeleteMidQEntry(mid);
  1307. add_credits(tcon->ses->server, credits_received, 0);
  1308. }
  1309. /* smb2_async_writev - send an async write, and set up mid to handle result */
  1310. int
  1311. smb2_async_writev(struct cifs_writedata *wdata)
  1312. {
  1313. int rc = -EACCES;
  1314. struct smb2_write_req *req = NULL;
  1315. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1316. struct kvec iov;
  1317. struct smb_rqst rqst;
  1318. rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
  1319. if (rc)
  1320. goto async_writev_out;
  1321. req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
  1322. req->PersistentFileId = wdata->cfile->fid.persistent_fid;
  1323. req->VolatileFileId = wdata->cfile->fid.volatile_fid;
  1324. req->WriteChannelInfoOffset = 0;
  1325. req->WriteChannelInfoLength = 0;
  1326. req->Channel = 0;
  1327. req->Offset = cpu_to_le64(wdata->offset);
  1328. /* 4 for rfc1002 length field */
  1329. req->DataOffset = cpu_to_le16(
  1330. offsetof(struct smb2_write_req, Buffer) - 4);
  1331. req->RemainingBytes = 0;
  1332. /* 4 for rfc1002 length field and 1 for Buffer */
  1333. iov.iov_len = get_rfc1002_length(req) + 4 - 1;
  1334. iov.iov_base = req;
  1335. rqst.rq_iov = &iov;
  1336. rqst.rq_nvec = 1;
  1337. rqst.rq_pages = wdata->pages;
  1338. rqst.rq_npages = wdata->nr_pages;
  1339. rqst.rq_pagesz = wdata->pagesz;
  1340. rqst.rq_tailsz = wdata->tailsz;
  1341. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1342. wdata->offset, wdata->bytes);
  1343. req->Length = cpu_to_le32(wdata->bytes);
  1344. inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
  1345. kref_get(&wdata->refcount);
  1346. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1347. smb2_writev_callback, wdata, 0);
  1348. if (rc) {
  1349. kref_put(&wdata->refcount, cifs_writedata_release);
  1350. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  1351. }
  1352. async_writev_out:
  1353. cifs_small_buf_release(req);
  1354. return rc;
  1355. }
  1356. /*
  1357. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  1358. * The length field from io_parms must be at least 1 and indicates a number of
  1359. * elements with data to write that begins with position 1 in iov array. All
  1360. * data length is specified by count.
  1361. */
  1362. int
  1363. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  1364. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1365. {
  1366. int rc = 0;
  1367. struct smb2_write_req *req = NULL;
  1368. struct smb2_write_rsp *rsp = NULL;
  1369. int resp_buftype;
  1370. *nbytes = 0;
  1371. if (n_vec < 1)
  1372. return rc;
  1373. rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
  1374. if (rc)
  1375. return rc;
  1376. if (io_parms->tcon->ses->server == NULL)
  1377. return -ECONNABORTED;
  1378. req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
  1379. req->PersistentFileId = io_parms->persistent_fid;
  1380. req->VolatileFileId = io_parms->volatile_fid;
  1381. req->WriteChannelInfoOffset = 0;
  1382. req->WriteChannelInfoLength = 0;
  1383. req->Channel = 0;
  1384. req->Length = cpu_to_le32(io_parms->length);
  1385. req->Offset = cpu_to_le64(io_parms->offset);
  1386. /* 4 for rfc1002 length field */
  1387. req->DataOffset = cpu_to_le16(
  1388. offsetof(struct smb2_write_req, Buffer) - 4);
  1389. req->RemainingBytes = 0;
  1390. iov[0].iov_base = (char *)req;
  1391. /* 4 for rfc1002 length field and 1 for Buffer */
  1392. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1393. /* length of entire message including data to be written */
  1394. inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
  1395. rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
  1396. &resp_buftype, 0);
  1397. rsp = (struct smb2_write_rsp *)iov[0].iov_base;
  1398. if (rc) {
  1399. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  1400. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  1401. } else
  1402. *nbytes = le32_to_cpu(rsp->DataLength);
  1403. free_rsp_buf(resp_buftype, rsp);
  1404. return rc;
  1405. }
  1406. static unsigned int
  1407. num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
  1408. {
  1409. int len;
  1410. unsigned int entrycount = 0;
  1411. unsigned int next_offset = 0;
  1412. FILE_DIRECTORY_INFO *entryptr;
  1413. if (bufstart == NULL)
  1414. return 0;
  1415. entryptr = (FILE_DIRECTORY_INFO *)bufstart;
  1416. while (1) {
  1417. entryptr = (FILE_DIRECTORY_INFO *)
  1418. ((char *)entryptr + next_offset);
  1419. if ((char *)entryptr + size > end_of_buf) {
  1420. cifs_dbg(VFS, "malformed search entry would overflow\n");
  1421. break;
  1422. }
  1423. len = le32_to_cpu(entryptr->FileNameLength);
  1424. if ((char *)entryptr + len + size > end_of_buf) {
  1425. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  1426. end_of_buf);
  1427. break;
  1428. }
  1429. *lastentry = (char *)entryptr;
  1430. entrycount++;
  1431. next_offset = le32_to_cpu(entryptr->NextEntryOffset);
  1432. if (!next_offset)
  1433. break;
  1434. }
  1435. return entrycount;
  1436. }
  1437. /*
  1438. * Readdir/FindFirst
  1439. */
  1440. int
  1441. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  1442. u64 persistent_fid, u64 volatile_fid, int index,
  1443. struct cifs_search_info *srch_inf)
  1444. {
  1445. struct smb2_query_directory_req *req;
  1446. struct smb2_query_directory_rsp *rsp = NULL;
  1447. struct kvec iov[2];
  1448. int rc = 0;
  1449. int len;
  1450. int resp_buftype;
  1451. unsigned char *bufptr;
  1452. struct TCP_Server_Info *server;
  1453. struct cifs_ses *ses = tcon->ses;
  1454. __le16 asteriks = cpu_to_le16('*');
  1455. char *end_of_smb;
  1456. unsigned int output_size = CIFSMaxBufSize;
  1457. size_t info_buf_size;
  1458. if (ses && (ses->server))
  1459. server = ses->server;
  1460. else
  1461. return -EIO;
  1462. rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
  1463. if (rc)
  1464. return rc;
  1465. switch (srch_inf->info_level) {
  1466. case SMB_FIND_FILE_DIRECTORY_INFO:
  1467. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  1468. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  1469. break;
  1470. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  1471. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  1472. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  1473. break;
  1474. default:
  1475. cifs_dbg(VFS, "info level %u isn't supported\n",
  1476. srch_inf->info_level);
  1477. rc = -EINVAL;
  1478. goto qdir_exit;
  1479. }
  1480. req->FileIndex = cpu_to_le32(index);
  1481. req->PersistentFileId = persistent_fid;
  1482. req->VolatileFileId = volatile_fid;
  1483. len = 0x2;
  1484. bufptr = req->Buffer;
  1485. memcpy(bufptr, &asteriks, len);
  1486. req->FileNameOffset =
  1487. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
  1488. req->FileNameLength = cpu_to_le16(len);
  1489. /*
  1490. * BB could be 30 bytes or so longer if we used SMB2 specific
  1491. * buffer lengths, but this is safe and close enough.
  1492. */
  1493. output_size = min_t(unsigned int, output_size, server->maxBuf);
  1494. output_size = min_t(unsigned int, output_size, 2 << 15);
  1495. req->OutputBufferLength = cpu_to_le32(output_size);
  1496. iov[0].iov_base = (char *)req;
  1497. /* 4 for RFC1001 length and 1 for Buffer */
  1498. iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
  1499. iov[1].iov_base = (char *)(req->Buffer);
  1500. iov[1].iov_len = len;
  1501. inc_rfc1001_len(req, len - 1 /* Buffer */);
  1502. rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
  1503. rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
  1504. if (rc) {
  1505. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  1506. goto qdir_exit;
  1507. }
  1508. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1509. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1510. info_buf_size);
  1511. if (rc)
  1512. goto qdir_exit;
  1513. srch_inf->unicode = true;
  1514. if (srch_inf->ntwrk_buf_start) {
  1515. if (srch_inf->smallBuf)
  1516. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  1517. else
  1518. cifs_buf_release(srch_inf->ntwrk_buf_start);
  1519. }
  1520. srch_inf->ntwrk_buf_start = (char *)rsp;
  1521. srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
  1522. (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
  1523. /* 4 for rfc1002 length field */
  1524. end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
  1525. srch_inf->entries_in_buffer =
  1526. num_entries(srch_inf->srch_entries_start, end_of_smb,
  1527. &srch_inf->last_entry, info_buf_size);
  1528. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  1529. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  1530. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  1531. srch_inf->srch_entries_start, srch_inf->last_entry);
  1532. if (resp_buftype == CIFS_LARGE_BUFFER)
  1533. srch_inf->smallBuf = false;
  1534. else if (resp_buftype == CIFS_SMALL_BUFFER)
  1535. srch_inf->smallBuf = true;
  1536. else
  1537. cifs_dbg(VFS, "illegal search buffer type\n");
  1538. if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
  1539. srch_inf->endOfSearch = 1;
  1540. else
  1541. srch_inf->endOfSearch = 0;
  1542. return rc;
  1543. qdir_exit:
  1544. free_rsp_buf(resp_buftype, rsp);
  1545. return rc;
  1546. }
  1547. static int
  1548. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1549. u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
  1550. unsigned int num, void **data, unsigned int *size)
  1551. {
  1552. struct smb2_set_info_req *req;
  1553. struct smb2_set_info_rsp *rsp = NULL;
  1554. struct kvec *iov;
  1555. int rc = 0;
  1556. int resp_buftype;
  1557. unsigned int i;
  1558. struct TCP_Server_Info *server;
  1559. struct cifs_ses *ses = tcon->ses;
  1560. if (ses && (ses->server))
  1561. server = ses->server;
  1562. else
  1563. return -EIO;
  1564. if (!num)
  1565. return -EINVAL;
  1566. iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
  1567. if (!iov)
  1568. return -ENOMEM;
  1569. rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
  1570. if (rc) {
  1571. kfree(iov);
  1572. return rc;
  1573. }
  1574. req->hdr.ProcessId = cpu_to_le32(pid);
  1575. req->InfoType = SMB2_O_INFO_FILE;
  1576. req->FileInfoClass = info_class;
  1577. req->PersistentFileId = persistent_fid;
  1578. req->VolatileFileId = volatile_fid;
  1579. /* 4 for RFC1001 length and 1 for Buffer */
  1580. req->BufferOffset =
  1581. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
  1582. req->BufferLength = cpu_to_le32(*size);
  1583. inc_rfc1001_len(req, *size - 1 /* Buffer */);
  1584. memcpy(req->Buffer, *data, *size);
  1585. iov[0].iov_base = (char *)req;
  1586. /* 4 for RFC1001 length */
  1587. iov[0].iov_len = get_rfc1002_length(req) + 4;
  1588. for (i = 1; i < num; i++) {
  1589. inc_rfc1001_len(req, size[i]);
  1590. le32_add_cpu(&req->BufferLength, size[i]);
  1591. iov[i].iov_base = (char *)data[i];
  1592. iov[i].iov_len = size[i];
  1593. }
  1594. rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
  1595. rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
  1596. if (rc != 0) {
  1597. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  1598. goto out;
  1599. }
  1600. out:
  1601. free_rsp_buf(resp_buftype, rsp);
  1602. kfree(iov);
  1603. return rc;
  1604. }
  1605. int
  1606. SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
  1607. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1608. {
  1609. struct smb2_file_rename_info info;
  1610. void **data;
  1611. unsigned int size[2];
  1612. int rc;
  1613. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1614. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1615. if (!data)
  1616. return -ENOMEM;
  1617. info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
  1618. /* 0 = fail if target already exists */
  1619. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1620. info.FileNameLength = cpu_to_le32(len);
  1621. data[0] = &info;
  1622. size[0] = sizeof(struct smb2_file_rename_info);
  1623. data[1] = target_file;
  1624. size[1] = len + 2 /* null */;
  1625. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1626. current->tgid, FILE_RENAME_INFORMATION, 2, data,
  1627. size);
  1628. kfree(data);
  1629. return rc;
  1630. }
  1631. int
  1632. SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
  1633. u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
  1634. {
  1635. struct smb2_file_link_info info;
  1636. void **data;
  1637. unsigned int size[2];
  1638. int rc;
  1639. int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
  1640. data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
  1641. if (!data)
  1642. return -ENOMEM;
  1643. info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
  1644. /* 0 = fail if link already exists */
  1645. info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
  1646. info.FileNameLength = cpu_to_le32(len);
  1647. data[0] = &info;
  1648. size[0] = sizeof(struct smb2_file_link_info);
  1649. data[1] = target_file;
  1650. size[1] = len + 2 /* null */;
  1651. rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1652. current->tgid, FILE_LINK_INFORMATION, 2, data, size);
  1653. kfree(data);
  1654. return rc;
  1655. }
  1656. int
  1657. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  1658. u64 volatile_fid, u32 pid, __le64 *eof)
  1659. {
  1660. struct smb2_file_eof_info info;
  1661. void *data;
  1662. unsigned int size;
  1663. info.EndOfFile = *eof;
  1664. data = &info;
  1665. size = sizeof(struct smb2_file_eof_info);
  1666. return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
  1667. FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
  1668. }
  1669. int
  1670. SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  1671. u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
  1672. {
  1673. unsigned int size;
  1674. size = sizeof(FILE_BASIC_INFO);
  1675. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  1676. current->tgid, FILE_BASIC_INFORMATION, 1,
  1677. (void **)&buf, &size);
  1678. }
  1679. int
  1680. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  1681. const u64 persistent_fid, const u64 volatile_fid,
  1682. __u8 oplock_level)
  1683. {
  1684. int rc;
  1685. struct smb2_oplock_break *req = NULL;
  1686. cifs_dbg(FYI, "SMB2_oplock_break\n");
  1687. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1688. if (rc)
  1689. return rc;
  1690. req->VolatileFid = volatile_fid;
  1691. req->PersistentFid = persistent_fid;
  1692. req->OplockLevel = oplock_level;
  1693. req->hdr.CreditRequest = cpu_to_le16(1);
  1694. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1695. /* SMB2 buffer freed by function above */
  1696. if (rc) {
  1697. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1698. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  1699. }
  1700. return rc;
  1701. }
  1702. static void
  1703. copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  1704. struct kstatfs *kst)
  1705. {
  1706. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  1707. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  1708. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  1709. kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
  1710. kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  1711. return;
  1712. }
  1713. static int
  1714. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
  1715. int outbuf_len, u64 persistent_fid, u64 volatile_fid)
  1716. {
  1717. int rc;
  1718. struct smb2_query_info_req *req;
  1719. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  1720. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  1721. return -EIO;
  1722. rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
  1723. if (rc)
  1724. return rc;
  1725. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  1726. req->FileInfoClass = level;
  1727. req->PersistentFileId = persistent_fid;
  1728. req->VolatileFileId = volatile_fid;
  1729. /* 4 for rfc1002 length field and 1 for pad */
  1730. req->InputBufferOffset =
  1731. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
  1732. req->OutputBufferLength = cpu_to_le32(
  1733. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
  1734. iov->iov_base = (char *)req;
  1735. /* 4 for rfc1002 length field */
  1736. iov->iov_len = get_rfc1002_length(req) + 4;
  1737. return 0;
  1738. }
  1739. int
  1740. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  1741. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  1742. {
  1743. struct smb2_query_info_rsp *rsp = NULL;
  1744. struct kvec iov;
  1745. int rc = 0;
  1746. int resp_buftype;
  1747. struct cifs_ses *ses = tcon->ses;
  1748. struct smb2_fs_full_size_info *info = NULL;
  1749. rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
  1750. sizeof(struct smb2_fs_full_size_info),
  1751. persistent_fid, volatile_fid);
  1752. if (rc)
  1753. return rc;
  1754. rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
  1755. if (rc) {
  1756. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  1757. goto qinf_exit;
  1758. }
  1759. rsp = (struct smb2_query_info_rsp *)iov.iov_base;
  1760. info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
  1761. le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
  1762. rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
  1763. le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
  1764. sizeof(struct smb2_fs_full_size_info));
  1765. if (!rc)
  1766. copy_fs_info_to_kstatfs(info, fsdata);
  1767. qinf_exit:
  1768. free_rsp_buf(resp_buftype, iov.iov_base);
  1769. return rc;
  1770. }
  1771. int
  1772. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1773. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1774. const __u32 num_lock, struct smb2_lock_element *buf)
  1775. {
  1776. int rc = 0;
  1777. struct smb2_lock_req *req = NULL;
  1778. struct kvec iov[2];
  1779. int resp_buf_type;
  1780. unsigned int count;
  1781. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  1782. rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
  1783. if (rc)
  1784. return rc;
  1785. req->hdr.ProcessId = cpu_to_le32(pid);
  1786. req->LockCount = cpu_to_le16(num_lock);
  1787. req->PersistentFileId = persist_fid;
  1788. req->VolatileFileId = volatile_fid;
  1789. count = num_lock * sizeof(struct smb2_lock_element);
  1790. inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
  1791. iov[0].iov_base = (char *)req;
  1792. /* 4 for rfc1002 length field and count for all locks */
  1793. iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
  1794. iov[1].iov_base = (char *)buf;
  1795. iov[1].iov_len = count;
  1796. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  1797. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  1798. if (rc) {
  1799. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  1800. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  1801. }
  1802. return rc;
  1803. }
  1804. int
  1805. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  1806. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  1807. const __u64 length, const __u64 offset, const __u32 lock_flags,
  1808. const bool wait)
  1809. {
  1810. struct smb2_lock_element lock;
  1811. lock.Offset = cpu_to_le64(offset);
  1812. lock.Length = cpu_to_le64(length);
  1813. lock.Flags = cpu_to_le32(lock_flags);
  1814. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  1815. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  1816. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  1817. }
  1818. int
  1819. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  1820. __u8 *lease_key, const __le32 lease_state)
  1821. {
  1822. int rc;
  1823. struct smb2_lease_ack *req = NULL;
  1824. cifs_dbg(FYI, "SMB2_lease_break\n");
  1825. rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
  1826. if (rc)
  1827. return rc;
  1828. req->hdr.CreditRequest = cpu_to_le16(1);
  1829. req->StructureSize = cpu_to_le16(36);
  1830. inc_rfc1001_len(req, 12);
  1831. memcpy(req->LeaseKey, lease_key, 16);
  1832. req->LeaseState = lease_state;
  1833. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
  1834. /* SMB2 buffer freed by function above */
  1835. if (rc) {
  1836. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  1837. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  1838. }
  1839. return rc;
  1840. }