smb2transport.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * fs/cifs/smb2transport.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Jeremy Allison (jra@samba.org) 2006
  8. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/list.h>
  26. #include <linux/wait.h>
  27. #include <linux/net.h>
  28. #include <linux/delay.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/processor.h>
  31. #include <linux/mempool.h>
  32. #include <linux/highmem.h>
  33. #include "smb2pdu.h"
  34. #include "cifsglob.h"
  35. #include "cifsproto.h"
  36. #include "smb2proto.h"
  37. #include "cifs_debug.h"
  38. #include "smb2status.h"
  39. #include "smb2glob.h"
  40. static int
  41. smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
  42. {
  43. int rc;
  44. unsigned int size;
  45. if (server->secmech.sdeschmacsha256 != NULL)
  46. return 0; /* already allocated */
  47. server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0);
  48. if (IS_ERR(server->secmech.hmacsha256)) {
  49. cifs_dbg(VFS, "could not allocate crypto hmacsha256\n");
  50. rc = PTR_ERR(server->secmech.hmacsha256);
  51. server->secmech.hmacsha256 = NULL;
  52. return rc;
  53. }
  54. size = sizeof(struct shash_desc) +
  55. crypto_shash_descsize(server->secmech.hmacsha256);
  56. server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL);
  57. if (!server->secmech.sdeschmacsha256) {
  58. crypto_free_shash(server->secmech.hmacsha256);
  59. server->secmech.hmacsha256 = NULL;
  60. return -ENOMEM;
  61. }
  62. server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256;
  63. server->secmech.sdeschmacsha256->shash.flags = 0x0;
  64. return 0;
  65. }
  66. static int
  67. smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
  68. {
  69. unsigned int size;
  70. int rc;
  71. if (server->secmech.sdesccmacaes != NULL)
  72. return 0; /* already allocated */
  73. rc = smb2_crypto_shash_allocate(server);
  74. if (rc)
  75. return rc;
  76. server->secmech.cmacaes = crypto_alloc_shash("cmac(aes)", 0, 0);
  77. if (IS_ERR(server->secmech.cmacaes)) {
  78. cifs_dbg(VFS, "could not allocate crypto cmac-aes");
  79. kfree(server->secmech.sdeschmacsha256);
  80. server->secmech.sdeschmacsha256 = NULL;
  81. crypto_free_shash(server->secmech.hmacsha256);
  82. server->secmech.hmacsha256 = NULL;
  83. rc = PTR_ERR(server->secmech.cmacaes);
  84. server->secmech.cmacaes = NULL;
  85. return rc;
  86. }
  87. size = sizeof(struct shash_desc) +
  88. crypto_shash_descsize(server->secmech.cmacaes);
  89. server->secmech.sdesccmacaes = kmalloc(size, GFP_KERNEL);
  90. if (!server->secmech.sdesccmacaes) {
  91. cifs_dbg(VFS, "%s: Can't alloc cmacaes\n", __func__);
  92. kfree(server->secmech.sdeschmacsha256);
  93. server->secmech.sdeschmacsha256 = NULL;
  94. crypto_free_shash(server->secmech.hmacsha256);
  95. crypto_free_shash(server->secmech.cmacaes);
  96. server->secmech.hmacsha256 = NULL;
  97. server->secmech.cmacaes = NULL;
  98. return -ENOMEM;
  99. }
  100. server->secmech.sdesccmacaes->shash.tfm = server->secmech.cmacaes;
  101. server->secmech.sdesccmacaes->shash.flags = 0x0;
  102. return 0;
  103. }
  104. static struct cifs_ses *
  105. smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
  106. {
  107. struct cifs_ses *ses;
  108. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  109. if (ses->Suid != ses_id)
  110. continue;
  111. return ses;
  112. }
  113. return NULL;
  114. }
  115. struct cifs_ses *
  116. smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
  117. {
  118. struct cifs_ses *ses;
  119. spin_lock(&cifs_tcp_ses_lock);
  120. ses = smb2_find_smb_ses_unlocked(server, ses_id);
  121. spin_unlock(&cifs_tcp_ses_lock);
  122. return ses;
  123. }
  124. static struct cifs_tcon *
  125. smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
  126. {
  127. struct cifs_tcon *tcon;
  128. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  129. if (tcon->tid != tid)
  130. continue;
  131. ++tcon->tc_count;
  132. return tcon;
  133. }
  134. return NULL;
  135. }
  136. /*
  137. * Obtain tcon corresponding to the tid in the given
  138. * cifs_ses
  139. */
  140. struct cifs_tcon *
  141. smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
  142. {
  143. struct cifs_ses *ses;
  144. struct cifs_tcon *tcon;
  145. spin_lock(&cifs_tcp_ses_lock);
  146. ses = smb2_find_smb_ses_unlocked(server, ses_id);
  147. if (!ses) {
  148. spin_unlock(&cifs_tcp_ses_lock);
  149. return NULL;
  150. }
  151. tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
  152. spin_unlock(&cifs_tcp_ses_lock);
  153. return tcon;
  154. }
  155. int
  156. smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  157. {
  158. int rc;
  159. unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
  160. unsigned char *sigptr = smb2_signature;
  161. struct kvec *iov = rqst->rq_iov;
  162. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  163. struct cifs_ses *ses;
  164. ses = smb2_find_smb_ses(server, smb2_pdu->SessionId);
  165. if (!ses) {
  166. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  167. return 0;
  168. }
  169. memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
  170. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  171. rc = smb2_crypto_shash_allocate(server);
  172. if (rc) {
  173. cifs_dbg(VFS, "%s: shah256 alloc failed\n", __func__);
  174. return rc;
  175. }
  176. rc = crypto_shash_setkey(server->secmech.hmacsha256,
  177. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  178. if (rc) {
  179. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  180. return rc;
  181. }
  182. rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
  183. if (rc) {
  184. cifs_dbg(VFS, "%s: Could not init sha256", __func__);
  185. return rc;
  186. }
  187. rc = __cifs_calc_signature(rqst, server, sigptr,
  188. &server->secmech.sdeschmacsha256->shash);
  189. if (!rc)
  190. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  191. return rc;
  192. }
  193. static int generate_key(struct cifs_ses *ses, struct kvec label,
  194. struct kvec context, __u8 *key, unsigned int key_size)
  195. {
  196. unsigned char zero = 0x0;
  197. __u8 i[4] = {0, 0, 0, 1};
  198. __u8 L[4] = {0, 0, 0, 128};
  199. int rc = 0;
  200. unsigned char prfhash[SMB2_HMACSHA256_SIZE];
  201. unsigned char *hashptr = prfhash;
  202. memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
  203. memset(key, 0x0, key_size);
  204. rc = smb3_crypto_shash_allocate(ses->server);
  205. if (rc) {
  206. cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
  207. goto smb3signkey_ret;
  208. }
  209. rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
  210. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  211. if (rc) {
  212. cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
  213. goto smb3signkey_ret;
  214. }
  215. rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
  216. if (rc) {
  217. cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
  218. goto smb3signkey_ret;
  219. }
  220. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  221. i, 4);
  222. if (rc) {
  223. cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
  224. goto smb3signkey_ret;
  225. }
  226. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  227. label.iov_base, label.iov_len);
  228. if (rc) {
  229. cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
  230. goto smb3signkey_ret;
  231. }
  232. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  233. &zero, 1);
  234. if (rc) {
  235. cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
  236. goto smb3signkey_ret;
  237. }
  238. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  239. context.iov_base, context.iov_len);
  240. if (rc) {
  241. cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
  242. goto smb3signkey_ret;
  243. }
  244. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  245. L, 4);
  246. if (rc) {
  247. cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
  248. goto smb3signkey_ret;
  249. }
  250. rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
  251. hashptr);
  252. if (rc) {
  253. cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
  254. goto smb3signkey_ret;
  255. }
  256. memcpy(key, hashptr, key_size);
  257. smb3signkey_ret:
  258. return rc;
  259. }
  260. struct derivation {
  261. struct kvec label;
  262. struct kvec context;
  263. };
  264. struct derivation_triplet {
  265. struct derivation signing;
  266. struct derivation encryption;
  267. struct derivation decryption;
  268. };
  269. static int
  270. generate_smb3signingkey(struct cifs_ses *ses,
  271. const struct derivation_triplet *ptriplet)
  272. {
  273. int rc;
  274. rc = generate_key(ses, ptriplet->signing.label,
  275. ptriplet->signing.context, ses->smb3signingkey,
  276. SMB3_SIGN_KEY_SIZE);
  277. if (rc)
  278. return rc;
  279. rc = generate_key(ses, ptriplet->encryption.label,
  280. ptriplet->encryption.context, ses->smb3encryptionkey,
  281. SMB3_SIGN_KEY_SIZE);
  282. if (rc)
  283. return rc;
  284. return generate_key(ses, ptriplet->decryption.label,
  285. ptriplet->decryption.context,
  286. ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
  287. }
  288. int
  289. generate_smb30signingkey(struct cifs_ses *ses)
  290. {
  291. struct derivation_triplet triplet;
  292. struct derivation *d;
  293. d = &triplet.signing;
  294. d->label.iov_base = "SMB2AESCMAC";
  295. d->label.iov_len = 12;
  296. d->context.iov_base = "SmbSign";
  297. d->context.iov_len = 8;
  298. d = &triplet.encryption;
  299. d->label.iov_base = "SMB2AESCCM";
  300. d->label.iov_len = 11;
  301. d->context.iov_base = "ServerIn ";
  302. d->context.iov_len = 10;
  303. d = &triplet.decryption;
  304. d->label.iov_base = "SMB2AESCCM";
  305. d->label.iov_len = 11;
  306. d->context.iov_base = "ServerOut";
  307. d->context.iov_len = 10;
  308. return generate_smb3signingkey(ses, &triplet);
  309. }
  310. int
  311. generate_smb311signingkey(struct cifs_ses *ses)
  312. {
  313. struct derivation_triplet triplet;
  314. struct derivation *d;
  315. d = &triplet.signing;
  316. d->label.iov_base = "SMB2AESCMAC";
  317. d->label.iov_len = 12;
  318. d->context.iov_base = "SmbSign";
  319. d->context.iov_len = 8;
  320. d = &triplet.encryption;
  321. d->label.iov_base = "SMB2AESCCM";
  322. d->label.iov_len = 11;
  323. d->context.iov_base = "ServerIn ";
  324. d->context.iov_len = 10;
  325. d = &triplet.decryption;
  326. d->label.iov_base = "SMB2AESCCM";
  327. d->label.iov_len = 11;
  328. d->context.iov_base = "ServerOut";
  329. d->context.iov_len = 10;
  330. return generate_smb3signingkey(ses, &triplet);
  331. }
  332. int
  333. smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  334. {
  335. int rc = 0;
  336. unsigned char smb3_signature[SMB2_CMACAES_SIZE];
  337. unsigned char *sigptr = smb3_signature;
  338. struct kvec *iov = rqst->rq_iov;
  339. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  340. struct cifs_ses *ses;
  341. ses = smb2_find_smb_ses(server, smb2_pdu->SessionId);
  342. if (!ses) {
  343. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  344. return 0;
  345. }
  346. memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
  347. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  348. rc = crypto_shash_setkey(server->secmech.cmacaes,
  349. ses->smb3signingkey, SMB2_CMACAES_SIZE);
  350. if (rc) {
  351. cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
  352. return rc;
  353. }
  354. /*
  355. * we already allocate sdesccmacaes when we init smb3 signing key,
  356. * so unlike smb2 case we do not have to check here if secmech are
  357. * initialized
  358. */
  359. rc = crypto_shash_init(&server->secmech.sdesccmacaes->shash);
  360. if (rc) {
  361. cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
  362. return rc;
  363. }
  364. rc = __cifs_calc_signature(rqst, server, sigptr,
  365. &server->secmech.sdesccmacaes->shash);
  366. if (!rc)
  367. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  368. return rc;
  369. }
  370. /* must be called with server->srv_mutex held */
  371. static int
  372. smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  373. {
  374. int rc = 0;
  375. struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
  376. if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
  377. server->tcpStatus == CifsNeedNegotiate)
  378. return rc;
  379. if (!server->session_estab) {
  380. strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
  381. return rc;
  382. }
  383. rc = server->ops->calc_signature(rqst, server);
  384. return rc;
  385. }
  386. int
  387. smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  388. {
  389. unsigned int rc;
  390. char server_response_sig[16];
  391. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  392. if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
  393. (smb2_pdu->Command == SMB2_SESSION_SETUP) ||
  394. (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
  395. (!server->session_estab))
  396. return 0;
  397. /*
  398. * BB what if signatures are supposed to be on for session but
  399. * server does not send one? BB
  400. */
  401. /* Do not need to verify session setups with signature "BSRSPYL " */
  402. if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
  403. cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
  404. smb2_pdu->Command);
  405. /*
  406. * Save off the origiginal signature so we can modify the smb and check
  407. * our calculated signature against what the server sent.
  408. */
  409. memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
  410. memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
  411. mutex_lock(&server->srv_mutex);
  412. rc = server->ops->calc_signature(rqst, server);
  413. mutex_unlock(&server->srv_mutex);
  414. if (rc)
  415. return rc;
  416. if (memcmp(server_response_sig, smb2_pdu->Signature,
  417. SMB2_SIGNATURE_SIZE))
  418. return -EACCES;
  419. else
  420. return 0;
  421. }
  422. /*
  423. * Set message id for the request. Should be called after wait_for_free_request
  424. * and when srv_mutex is held.
  425. */
  426. static inline void
  427. smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
  428. {
  429. unsigned int i, num = le16_to_cpu(hdr->CreditCharge);
  430. hdr->MessageId = get_next_mid64(server);
  431. /* skip message numbers according to CreditCharge field */
  432. for (i = 1; i < num; i++)
  433. get_next_mid(server);
  434. }
  435. static struct mid_q_entry *
  436. smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
  437. struct TCP_Server_Info *server)
  438. {
  439. struct mid_q_entry *temp;
  440. if (server == NULL) {
  441. cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
  442. return NULL;
  443. }
  444. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  445. if (temp == NULL)
  446. return temp;
  447. else {
  448. memset(temp, 0, sizeof(struct mid_q_entry));
  449. temp->mid = le64_to_cpu(smb_buffer->MessageId);
  450. temp->pid = current->pid;
  451. temp->command = smb_buffer->Command; /* Always LE */
  452. temp->when_alloc = jiffies;
  453. temp->server = server;
  454. /*
  455. * The default is for the mid to be synchronous, so the
  456. * default callback just wakes up the current task.
  457. */
  458. temp->callback = cifs_wake_up_task;
  459. temp->callback_data = current;
  460. }
  461. atomic_inc(&midCount);
  462. temp->mid_state = MID_REQUEST_ALLOCATED;
  463. return temp;
  464. }
  465. static int
  466. smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
  467. struct mid_q_entry **mid)
  468. {
  469. if (ses->server->tcpStatus == CifsExiting)
  470. return -ENOENT;
  471. if (ses->server->tcpStatus == CifsNeedReconnect) {
  472. cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
  473. return -EAGAIN;
  474. }
  475. if (ses->status == CifsNew) {
  476. if ((buf->Command != SMB2_SESSION_SETUP) &&
  477. (buf->Command != SMB2_NEGOTIATE))
  478. return -EAGAIN;
  479. /* else ok - we are setting up session */
  480. }
  481. if (ses->status == CifsExiting) {
  482. if (buf->Command != SMB2_LOGOFF)
  483. return -EAGAIN;
  484. /* else ok - we are shutting down the session */
  485. }
  486. *mid = smb2_mid_entry_alloc(buf, ses->server);
  487. if (*mid == NULL)
  488. return -ENOMEM;
  489. spin_lock(&GlobalMid_Lock);
  490. list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
  491. spin_unlock(&GlobalMid_Lock);
  492. return 0;
  493. }
  494. int
  495. smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  496. bool log_error)
  497. {
  498. unsigned int len = get_rfc1002_length(mid->resp_buf);
  499. struct kvec iov;
  500. struct smb_rqst rqst = { .rq_iov = &iov,
  501. .rq_nvec = 1 };
  502. iov.iov_base = (char *)mid->resp_buf;
  503. iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
  504. dump_smb(mid->resp_buf, min_t(u32, 80, len));
  505. /* convert the length into a more usable form */
  506. if (len > 24 && server->sign) {
  507. int rc;
  508. rc = smb2_verify_signature(&rqst, server);
  509. if (rc)
  510. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  511. rc);
  512. }
  513. return map_smb2_to_linux_error(mid->resp_buf, log_error);
  514. }
  515. struct mid_q_entry *
  516. smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  517. {
  518. int rc;
  519. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  520. struct mid_q_entry *mid;
  521. smb2_seq_num_into_buf(ses->server, hdr);
  522. rc = smb2_get_mid_entry(ses, hdr, &mid);
  523. if (rc)
  524. return ERR_PTR(rc);
  525. rc = smb2_sign_rqst(rqst, ses->server);
  526. if (rc) {
  527. cifs_delete_mid(mid);
  528. return ERR_PTR(rc);
  529. }
  530. return mid;
  531. }
  532. struct mid_q_entry *
  533. smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  534. {
  535. int rc;
  536. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  537. struct mid_q_entry *mid;
  538. smb2_seq_num_into_buf(server, hdr);
  539. mid = smb2_mid_entry_alloc(hdr, server);
  540. if (mid == NULL)
  541. return ERR_PTR(-ENOMEM);
  542. rc = smb2_sign_rqst(rqst, server);
  543. if (rc) {
  544. DeleteMidQEntry(mid);
  545. return ERR_PTR(rc);
  546. }
  547. return mid;
  548. }