smb2transport.c 17 KB

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