smb2pdu.c 58 KB

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