smb2transport.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 i, rc;
  159. unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
  160. unsigned char *sigptr = smb2_signature;
  161. struct kvec *iov = rqst->rq_iov;
  162. int n_vec = rqst->rq_nvec;
  163. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  164. struct cifs_ses *ses;
  165. ses = smb2_find_smb_ses(server, smb2_pdu->SessionId);
  166. if (!ses) {
  167. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  168. return 0;
  169. }
  170. memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
  171. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  172. rc = smb2_crypto_shash_allocate(server);
  173. if (rc) {
  174. cifs_dbg(VFS, "%s: shah256 alloc failed\n", __func__);
  175. return rc;
  176. }
  177. rc = crypto_shash_setkey(server->secmech.hmacsha256,
  178. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  179. if (rc) {
  180. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  181. return rc;
  182. }
  183. rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
  184. if (rc) {
  185. cifs_dbg(VFS, "%s: Could not init sha256", __func__);
  186. return rc;
  187. }
  188. for (i = 0; i < n_vec; i++) {
  189. if (iov[i].iov_len == 0)
  190. continue;
  191. if (iov[i].iov_base == NULL) {
  192. cifs_dbg(VFS, "null iovec entry\n");
  193. return -EIO;
  194. }
  195. /*
  196. * The first entry includes a length field (which does not get
  197. * signed that occupies the first 4 bytes before the header).
  198. */
  199. if (i == 0) {
  200. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  201. break; /* nothing to sign or corrupt header */
  202. rc =
  203. crypto_shash_update(
  204. &server->secmech.sdeschmacsha256->shash,
  205. iov[i].iov_base + 4, iov[i].iov_len - 4);
  206. } else {
  207. rc =
  208. crypto_shash_update(
  209. &server->secmech.sdeschmacsha256->shash,
  210. iov[i].iov_base, iov[i].iov_len);
  211. }
  212. if (rc) {
  213. cifs_dbg(VFS, "%s: Could not update with payload\n",
  214. __func__);
  215. return rc;
  216. }
  217. }
  218. /* now hash over the rq_pages array */
  219. for (i = 0; i < rqst->rq_npages; i++) {
  220. struct kvec p_iov;
  221. cifs_rqst_page_to_kvec(rqst, i, &p_iov);
  222. crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
  223. p_iov.iov_base, p_iov.iov_len);
  224. kunmap(rqst->rq_pages[i]);
  225. }
  226. rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
  227. sigptr);
  228. if (rc)
  229. cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
  230. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  231. return rc;
  232. }
  233. int
  234. generate_smb3signingkey(struct cifs_ses *ses)
  235. {
  236. unsigned char zero = 0x0;
  237. __u8 i[4] = {0, 0, 0, 1};
  238. __u8 L[4] = {0, 0, 0, 128};
  239. int rc = 0;
  240. unsigned char prfhash[SMB2_HMACSHA256_SIZE];
  241. unsigned char *hashptr = prfhash;
  242. memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
  243. memset(ses->smb3signingkey, 0x0, SMB3_SIGNKEY_SIZE);
  244. rc = smb3_crypto_shash_allocate(ses->server);
  245. if (rc) {
  246. cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
  247. goto smb3signkey_ret;
  248. }
  249. rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
  250. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  251. if (rc) {
  252. cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
  253. goto smb3signkey_ret;
  254. }
  255. rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
  256. if (rc) {
  257. cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
  258. goto smb3signkey_ret;
  259. }
  260. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  261. i, 4);
  262. if (rc) {
  263. cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
  264. goto smb3signkey_ret;
  265. }
  266. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  267. "SMB2AESCMAC", 12);
  268. if (rc) {
  269. cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
  270. goto smb3signkey_ret;
  271. }
  272. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  273. &zero, 1);
  274. if (rc) {
  275. cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
  276. goto smb3signkey_ret;
  277. }
  278. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  279. "SmbSign", 8);
  280. if (rc) {
  281. cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
  282. goto smb3signkey_ret;
  283. }
  284. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  285. L, 4);
  286. if (rc) {
  287. cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
  288. goto smb3signkey_ret;
  289. }
  290. rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
  291. hashptr);
  292. if (rc) {
  293. cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
  294. goto smb3signkey_ret;
  295. }
  296. memcpy(ses->smb3signingkey, hashptr, SMB3_SIGNKEY_SIZE);
  297. smb3signkey_ret:
  298. return rc;
  299. }
  300. int
  301. smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  302. {
  303. int i;
  304. int rc = 0;
  305. unsigned char smb3_signature[SMB2_CMACAES_SIZE];
  306. unsigned char *sigptr = smb3_signature;
  307. struct kvec *iov = rqst->rq_iov;
  308. int n_vec = rqst->rq_nvec;
  309. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  310. struct cifs_ses *ses;
  311. ses = smb2_find_smb_ses(server, smb2_pdu->SessionId);
  312. if (!ses) {
  313. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  314. return 0;
  315. }
  316. memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
  317. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  318. rc = crypto_shash_setkey(server->secmech.cmacaes,
  319. ses->smb3signingkey, SMB2_CMACAES_SIZE);
  320. if (rc) {
  321. cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
  322. return rc;
  323. }
  324. /*
  325. * we already allocate sdesccmacaes when we init smb3 signing key,
  326. * so unlike smb2 case we do not have to check here if secmech are
  327. * initialized
  328. */
  329. rc = crypto_shash_init(&server->secmech.sdesccmacaes->shash);
  330. if (rc) {
  331. cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
  332. return rc;
  333. }
  334. for (i = 0; i < n_vec; i++) {
  335. if (iov[i].iov_len == 0)
  336. continue;
  337. if (iov[i].iov_base == NULL) {
  338. cifs_dbg(VFS, "null iovec entry");
  339. return -EIO;
  340. }
  341. /*
  342. * The first entry includes a length field (which does not get
  343. * signed that occupies the first 4 bytes before the header).
  344. */
  345. if (i == 0) {
  346. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  347. break; /* nothing to sign or corrupt header */
  348. rc =
  349. crypto_shash_update(
  350. &server->secmech.sdesccmacaes->shash,
  351. iov[i].iov_base + 4, iov[i].iov_len - 4);
  352. } else {
  353. rc =
  354. crypto_shash_update(
  355. &server->secmech.sdesccmacaes->shash,
  356. iov[i].iov_base, iov[i].iov_len);
  357. }
  358. if (rc) {
  359. cifs_dbg(VFS, "%s: Couldn't update cmac aes with payload\n",
  360. __func__);
  361. return rc;
  362. }
  363. }
  364. /* now hash over the rq_pages array */
  365. for (i = 0; i < rqst->rq_npages; i++) {
  366. struct kvec p_iov;
  367. cifs_rqst_page_to_kvec(rqst, i, &p_iov);
  368. crypto_shash_update(&server->secmech.sdesccmacaes->shash,
  369. p_iov.iov_base, p_iov.iov_len);
  370. kunmap(rqst->rq_pages[i]);
  371. }
  372. rc = crypto_shash_final(&server->secmech.sdesccmacaes->shash,
  373. sigptr);
  374. if (rc)
  375. cifs_dbg(VFS, "%s: Could not generate cmac aes\n", __func__);
  376. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  377. return rc;
  378. }
  379. /* must be called with server->srv_mutex held */
  380. static int
  381. smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  382. {
  383. int rc = 0;
  384. struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
  385. if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
  386. server->tcpStatus == CifsNeedNegotiate)
  387. return rc;
  388. if (!server->session_estab) {
  389. strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
  390. return rc;
  391. }
  392. rc = server->ops->calc_signature(rqst, server);
  393. return rc;
  394. }
  395. int
  396. smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  397. {
  398. unsigned int rc;
  399. char server_response_sig[16];
  400. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  401. if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
  402. (smb2_pdu->Command == SMB2_SESSION_SETUP) ||
  403. (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
  404. (!server->session_estab))
  405. return 0;
  406. /*
  407. * BB what if signatures are supposed to be on for session but
  408. * server does not send one? BB
  409. */
  410. /* Do not need to verify session setups with signature "BSRSPYL " */
  411. if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
  412. cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
  413. smb2_pdu->Command);
  414. /*
  415. * Save off the origiginal signature so we can modify the smb and check
  416. * our calculated signature against what the server sent.
  417. */
  418. memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
  419. memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
  420. mutex_lock(&server->srv_mutex);
  421. rc = server->ops->calc_signature(rqst, server);
  422. mutex_unlock(&server->srv_mutex);
  423. if (rc)
  424. return rc;
  425. if (memcmp(server_response_sig, smb2_pdu->Signature,
  426. SMB2_SIGNATURE_SIZE))
  427. return -EACCES;
  428. else
  429. return 0;
  430. }
  431. /*
  432. * Set message id for the request. Should be called after wait_for_free_request
  433. * and when srv_mutex is held.
  434. */
  435. static inline void
  436. smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
  437. {
  438. unsigned int i, num = le16_to_cpu(hdr->CreditCharge);
  439. hdr->MessageId = get_next_mid64(server);
  440. /* skip message numbers according to CreditCharge field */
  441. for (i = 1; i < num; i++)
  442. get_next_mid(server);
  443. }
  444. static struct mid_q_entry *
  445. smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
  446. struct TCP_Server_Info *server)
  447. {
  448. struct mid_q_entry *temp;
  449. if (server == NULL) {
  450. cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
  451. return NULL;
  452. }
  453. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  454. if (temp == NULL)
  455. return temp;
  456. else {
  457. memset(temp, 0, sizeof(struct mid_q_entry));
  458. temp->mid = le64_to_cpu(smb_buffer->MessageId);
  459. temp->pid = current->pid;
  460. temp->command = smb_buffer->Command; /* Always LE */
  461. temp->when_alloc = jiffies;
  462. temp->server = server;
  463. /*
  464. * The default is for the mid to be synchronous, so the
  465. * default callback just wakes up the current task.
  466. */
  467. temp->callback = cifs_wake_up_task;
  468. temp->callback_data = current;
  469. }
  470. atomic_inc(&midCount);
  471. temp->mid_state = MID_REQUEST_ALLOCATED;
  472. return temp;
  473. }
  474. static int
  475. smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
  476. struct mid_q_entry **mid)
  477. {
  478. if (ses->server->tcpStatus == CifsExiting)
  479. return -ENOENT;
  480. if (ses->server->tcpStatus == CifsNeedReconnect) {
  481. cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
  482. return -EAGAIN;
  483. }
  484. if (ses->status == CifsNew) {
  485. if ((buf->Command != SMB2_SESSION_SETUP) &&
  486. (buf->Command != SMB2_NEGOTIATE))
  487. return -EAGAIN;
  488. /* else ok - we are setting up session */
  489. }
  490. if (ses->status == CifsExiting) {
  491. if (buf->Command != SMB2_LOGOFF)
  492. return -EAGAIN;
  493. /* else ok - we are shutting down the session */
  494. }
  495. *mid = smb2_mid_entry_alloc(buf, ses->server);
  496. if (*mid == NULL)
  497. return -ENOMEM;
  498. spin_lock(&GlobalMid_Lock);
  499. list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
  500. spin_unlock(&GlobalMid_Lock);
  501. return 0;
  502. }
  503. int
  504. smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  505. bool log_error)
  506. {
  507. unsigned int len = get_rfc1002_length(mid->resp_buf);
  508. struct kvec iov;
  509. struct smb_rqst rqst = { .rq_iov = &iov,
  510. .rq_nvec = 1 };
  511. iov.iov_base = (char *)mid->resp_buf;
  512. iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
  513. dump_smb(mid->resp_buf, min_t(u32, 80, len));
  514. /* convert the length into a more usable form */
  515. if (len > 24 && server->sign) {
  516. int rc;
  517. rc = smb2_verify_signature(&rqst, server);
  518. if (rc)
  519. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  520. rc);
  521. }
  522. return map_smb2_to_linux_error(mid->resp_buf, log_error);
  523. }
  524. struct mid_q_entry *
  525. smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  526. {
  527. int rc;
  528. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  529. struct mid_q_entry *mid;
  530. smb2_seq_num_into_buf(ses->server, hdr);
  531. rc = smb2_get_mid_entry(ses, hdr, &mid);
  532. if (rc)
  533. return ERR_PTR(rc);
  534. rc = smb2_sign_rqst(rqst, ses->server);
  535. if (rc) {
  536. cifs_delete_mid(mid);
  537. return ERR_PTR(rc);
  538. }
  539. return mid;
  540. }
  541. struct mid_q_entry *
  542. smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  543. {
  544. int rc;
  545. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  546. struct mid_q_entry *mid;
  547. smb2_seq_num_into_buf(server, hdr);
  548. mid = smb2_mid_entry_alloc(hdr, server);
  549. if (mid == NULL)
  550. return ERR_PTR(-ENOMEM);
  551. rc = smb2_sign_rqst(rqst, server);
  552. if (rc) {
  553. DeleteMidQEntry(mid);
  554. return ERR_PTR(rc);
  555. }
  556. return mid;
  557. }