smb2transport.c 18 KB

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