smb2pdu.c 58 KB

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