fanotify: merge duplicate events on parent and child
[linux/fpc-iii.git] / fs / cifs / smb2transport.c
blob08b703b7a15ef0a1b0441a60060d91d31b38e05b
1 /*
2 * fs/cifs/smb2transport.c
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
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.
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.
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
25 #include <linux/fs.h>
26 #include <linux/list.h>
27 #include <linux/wait.h>
28 #include <linux/net.h>
29 #include <linux/delay.h>
30 #include <linux/uaccess.h>
31 #include <asm/processor.h>
32 #include <linux/mempool.h>
33 #include <linux/highmem.h>
34 #include <crypto/aead.h>
35 #include "smb2pdu.h"
36 #include "cifsglob.h"
37 #include "cifsproto.h"
38 #include "smb2proto.h"
39 #include "cifs_debug.h"
40 #include "smb2status.h"
41 #include "smb2glob.h"
43 static int
44 smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
46 return cifs_alloc_hash("hmac(sha256)",
47 &server->secmech.hmacsha256,
48 &server->secmech.sdeschmacsha256);
51 static int
52 smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
54 struct cifs_secmech *p = &server->secmech;
55 int rc;
57 rc = cifs_alloc_hash("hmac(sha256)",
58 &p->hmacsha256,
59 &p->sdeschmacsha256);
60 if (rc)
61 goto err;
63 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
64 if (rc)
65 goto err;
67 return 0;
68 err:
69 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
70 return rc;
73 int
74 smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
76 struct cifs_secmech *p = &server->secmech;
77 int rc = 0;
79 rc = cifs_alloc_hash("hmac(sha256)",
80 &p->hmacsha256,
81 &p->sdeschmacsha256);
82 if (rc)
83 return rc;
85 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
86 if (rc)
87 goto err;
89 rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
90 if (rc)
91 goto err;
93 return 0;
95 err:
96 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
97 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
98 return rc;
102 static
103 int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
105 struct cifs_chan *chan;
106 struct cifs_ses *ses = NULL;
107 struct TCP_Server_Info *it = NULL;
108 int i;
109 int rc = 0;
111 spin_lock(&cifs_tcp_ses_lock);
113 list_for_each_entry(it, &cifs_tcp_ses_list, tcp_ses_list) {
114 list_for_each_entry(ses, &it->smb_ses_list, smb_ses_list) {
115 if (ses->Suid == ses_id)
116 goto found;
119 cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
120 __func__, ses_id);
121 rc = -ENOENT;
122 goto out;
124 found:
125 if (ses->binding) {
127 * If we are in the process of binding a new channel
128 * to an existing session, use the master connection
129 * session key
131 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
132 goto out;
136 * Otherwise, use the channel key.
139 for (i = 0; i < ses->chan_count; i++) {
140 chan = ses->chans + i;
141 if (chan->server == server) {
142 memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
143 goto out;
147 cifs_dbg(VFS,
148 "%s: Could not find channel signing key for session 0x%llx\n",
149 __func__, ses_id);
150 rc = -ENOENT;
152 out:
153 spin_unlock(&cifs_tcp_ses_lock);
154 return rc;
157 static struct cifs_ses *
158 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
160 struct cifs_ses *ses;
162 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
163 if (ses->Suid != ses_id)
164 continue;
165 return ses;
168 return NULL;
171 struct cifs_ses *
172 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
174 struct cifs_ses *ses;
176 spin_lock(&cifs_tcp_ses_lock);
177 ses = smb2_find_smb_ses_unlocked(server, ses_id);
178 spin_unlock(&cifs_tcp_ses_lock);
180 return ses;
183 static struct cifs_tcon *
184 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
186 struct cifs_tcon *tcon;
188 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
189 if (tcon->tid != tid)
190 continue;
191 ++tcon->tc_count;
192 return tcon;
195 return NULL;
199 * Obtain tcon corresponding to the tid in the given
200 * cifs_ses
203 struct cifs_tcon *
204 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
206 struct cifs_ses *ses;
207 struct cifs_tcon *tcon;
209 spin_lock(&cifs_tcp_ses_lock);
210 ses = smb2_find_smb_ses_unlocked(server, ses_id);
211 if (!ses) {
212 spin_unlock(&cifs_tcp_ses_lock);
213 return NULL;
215 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
216 spin_unlock(&cifs_tcp_ses_lock);
218 return tcon;
222 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
224 int rc;
225 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
226 unsigned char *sigptr = smb2_signature;
227 struct kvec *iov = rqst->rq_iov;
228 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
229 struct cifs_ses *ses;
230 struct shash_desc *shash;
231 struct smb_rqst drqst;
233 ses = smb2_find_smb_ses(server, shdr->SessionId);
234 if (!ses) {
235 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
236 return 0;
239 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
240 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
242 rc = smb2_crypto_shash_allocate(server);
243 if (rc) {
244 cifs_server_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
245 return rc;
248 rc = crypto_shash_setkey(server->secmech.hmacsha256,
249 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
250 if (rc) {
251 cifs_server_dbg(VFS, "%s: Could not update with response\n", __func__);
252 return rc;
255 shash = &server->secmech.sdeschmacsha256->shash;
256 rc = crypto_shash_init(shash);
257 if (rc) {
258 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
259 return rc;
263 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
264 * data, that is, iov[0] should not contain a rfc1002 length.
266 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
267 * __cifs_calc_signature().
269 drqst = *rqst;
270 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
271 rc = crypto_shash_update(shash, iov[0].iov_base,
272 iov[0].iov_len);
273 if (rc) {
274 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
275 __func__);
276 return rc;
278 drqst.rq_iov++;
279 drqst.rq_nvec--;
282 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
283 if (!rc)
284 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
286 return rc;
289 static int generate_key(struct cifs_ses *ses, struct kvec label,
290 struct kvec context, __u8 *key, unsigned int key_size)
292 unsigned char zero = 0x0;
293 __u8 i[4] = {0, 0, 0, 1};
294 __u8 L[4] = {0, 0, 0, 128};
295 int rc = 0;
296 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
297 unsigned char *hashptr = prfhash;
298 struct TCP_Server_Info *server = ses->server;
300 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
301 memset(key, 0x0, key_size);
303 rc = smb3_crypto_shash_allocate(server);
304 if (rc) {
305 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
306 goto smb3signkey_ret;
309 rc = crypto_shash_setkey(server->secmech.hmacsha256,
310 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
311 if (rc) {
312 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
313 goto smb3signkey_ret;
316 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
317 if (rc) {
318 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
319 goto smb3signkey_ret;
322 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
323 i, 4);
324 if (rc) {
325 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
326 goto smb3signkey_ret;
329 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
330 label.iov_base, label.iov_len);
331 if (rc) {
332 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
333 goto smb3signkey_ret;
336 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
337 &zero, 1);
338 if (rc) {
339 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
340 goto smb3signkey_ret;
343 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
344 context.iov_base, context.iov_len);
345 if (rc) {
346 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
347 goto smb3signkey_ret;
350 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
351 L, 4);
352 if (rc) {
353 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
354 goto smb3signkey_ret;
357 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
358 hashptr);
359 if (rc) {
360 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
361 goto smb3signkey_ret;
364 memcpy(key, hashptr, key_size);
366 smb3signkey_ret:
367 return rc;
370 struct derivation {
371 struct kvec label;
372 struct kvec context;
375 struct derivation_triplet {
376 struct derivation signing;
377 struct derivation encryption;
378 struct derivation decryption;
381 static int
382 generate_smb3signingkey(struct cifs_ses *ses,
383 const struct derivation_triplet *ptriplet)
385 int rc;
388 * All channels use the same encryption/decryption keys but
389 * they have their own signing key.
391 * When we generate the keys, check if it is for a new channel
392 * (binding) in which case we only need to generate a signing
393 * key and store it in the channel as to not overwrite the
394 * master connection signing key stored in the session
397 if (ses->binding) {
398 rc = generate_key(ses, ptriplet->signing.label,
399 ptriplet->signing.context,
400 cifs_ses_binding_channel(ses)->signkey,
401 SMB3_SIGN_KEY_SIZE);
402 if (rc)
403 return rc;
404 } else {
405 rc = generate_key(ses, ptriplet->signing.label,
406 ptriplet->signing.context,
407 ses->smb3signingkey,
408 SMB3_SIGN_KEY_SIZE);
409 if (rc)
410 return rc;
412 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
413 SMB3_SIGN_KEY_SIZE);
415 rc = generate_key(ses, ptriplet->encryption.label,
416 ptriplet->encryption.context,
417 ses->smb3encryptionkey,
418 SMB3_SIGN_KEY_SIZE);
419 rc = generate_key(ses, ptriplet->decryption.label,
420 ptriplet->decryption.context,
421 ses->smb3decryptionkey,
422 SMB3_SIGN_KEY_SIZE);
423 if (rc)
424 return rc;
427 if (rc)
428 return rc;
430 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
431 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
433 * The session id is opaque in terms of endianness, so we can't
434 * print it as a long long. we dump it as we got it on the wire
436 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
437 &ses->Suid);
438 cifs_dbg(VFS, "Session Key %*ph\n",
439 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
440 cifs_dbg(VFS, "Signing Key %*ph\n",
441 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
442 cifs_dbg(VFS, "ServerIn Key %*ph\n",
443 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
444 cifs_dbg(VFS, "ServerOut Key %*ph\n",
445 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
446 #endif
447 return rc;
451 generate_smb30signingkey(struct cifs_ses *ses)
454 struct derivation_triplet triplet;
455 struct derivation *d;
457 d = &triplet.signing;
458 d->label.iov_base = "SMB2AESCMAC";
459 d->label.iov_len = 12;
460 d->context.iov_base = "SmbSign";
461 d->context.iov_len = 8;
463 d = &triplet.encryption;
464 d->label.iov_base = "SMB2AESCCM";
465 d->label.iov_len = 11;
466 d->context.iov_base = "ServerIn ";
467 d->context.iov_len = 10;
469 d = &triplet.decryption;
470 d->label.iov_base = "SMB2AESCCM";
471 d->label.iov_len = 11;
472 d->context.iov_base = "ServerOut";
473 d->context.iov_len = 10;
475 return generate_smb3signingkey(ses, &triplet);
479 generate_smb311signingkey(struct cifs_ses *ses)
482 struct derivation_triplet triplet;
483 struct derivation *d;
485 d = &triplet.signing;
486 d->label.iov_base = "SMBSigningKey";
487 d->label.iov_len = 14;
488 d->context.iov_base = ses->preauth_sha_hash;
489 d->context.iov_len = 64;
491 d = &triplet.encryption;
492 d->label.iov_base = "SMBC2SCipherKey";
493 d->label.iov_len = 16;
494 d->context.iov_base = ses->preauth_sha_hash;
495 d->context.iov_len = 64;
497 d = &triplet.decryption;
498 d->label.iov_base = "SMBS2CCipherKey";
499 d->label.iov_len = 16;
500 d->context.iov_base = ses->preauth_sha_hash;
501 d->context.iov_len = 64;
503 return generate_smb3signingkey(ses, &triplet);
507 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
509 int rc;
510 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
511 unsigned char *sigptr = smb3_signature;
512 struct kvec *iov = rqst->rq_iov;
513 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
514 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
515 struct smb_rqst drqst;
516 u8 key[SMB3_SIGN_KEY_SIZE];
518 rc = smb2_get_sign_key(shdr->SessionId, server, key);
519 if (rc)
520 return 0;
522 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
523 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
525 rc = crypto_shash_setkey(server->secmech.cmacaes,
526 key, SMB2_CMACAES_SIZE);
527 if (rc) {
528 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
529 return rc;
533 * we already allocate sdesccmacaes when we init smb3 signing key,
534 * so unlike smb2 case we do not have to check here if secmech are
535 * initialized
537 rc = crypto_shash_init(shash);
538 if (rc) {
539 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
540 return rc;
544 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
545 * data, that is, iov[0] should not contain a rfc1002 length.
547 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
548 * __cifs_calc_signature().
550 drqst = *rqst;
551 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
552 rc = crypto_shash_update(shash, iov[0].iov_base,
553 iov[0].iov_len);
554 if (rc) {
555 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
556 __func__);
557 return rc;
559 drqst.rq_iov++;
560 drqst.rq_nvec--;
563 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
564 if (!rc)
565 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
567 return rc;
570 /* must be called with server->srv_mutex held */
571 static int
572 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
574 int rc = 0;
575 struct smb2_sync_hdr *shdr;
576 struct smb2_sess_setup_req *ssr;
577 bool is_binding;
578 bool is_signed;
580 shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
581 ssr = (struct smb2_sess_setup_req *)shdr;
583 is_binding = shdr->Command == SMB2_SESSION_SETUP &&
584 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
585 is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
587 if (!is_signed)
588 return 0;
589 if (server->tcpStatus == CifsNeedNegotiate)
590 return 0;
591 if (!is_binding && !server->session_estab) {
592 strncpy(shdr->Signature, "BSRSPYL", 8);
593 return 0;
596 rc = server->ops->calc_signature(rqst, server);
598 return rc;
602 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
604 unsigned int rc;
605 char server_response_sig[16];
606 struct smb2_sync_hdr *shdr =
607 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
609 if ((shdr->Command == SMB2_NEGOTIATE) ||
610 (shdr->Command == SMB2_SESSION_SETUP) ||
611 (shdr->Command == SMB2_OPLOCK_BREAK) ||
612 server->ignore_signature ||
613 (!server->session_estab))
614 return 0;
617 * BB what if signatures are supposed to be on for session but
618 * server does not send one? BB
621 /* Do not need to verify session setups with signature "BSRSPYL " */
622 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
623 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
624 shdr->Command);
627 * Save off the origiginal signature so we can modify the smb and check
628 * our calculated signature against what the server sent.
630 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
632 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
634 mutex_lock(&server->srv_mutex);
635 rc = server->ops->calc_signature(rqst, server);
636 mutex_unlock(&server->srv_mutex);
638 if (rc)
639 return rc;
641 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
642 return -EACCES;
643 else
644 return 0;
648 * Set message id for the request. Should be called after wait_for_free_request
649 * and when srv_mutex is held.
651 static inline void
652 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
653 struct smb2_sync_hdr *shdr)
655 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
657 shdr->MessageId = get_next_mid64(server);
658 /* skip message numbers according to CreditCharge field */
659 for (i = 1; i < num; i++)
660 get_next_mid(server);
663 static struct mid_q_entry *
664 smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
665 struct TCP_Server_Info *server)
667 struct mid_q_entry *temp;
668 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
670 if (server == NULL) {
671 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
672 return NULL;
675 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
676 memset(temp, 0, sizeof(struct mid_q_entry));
677 kref_init(&temp->refcount);
678 temp->mid = le64_to_cpu(shdr->MessageId);
679 temp->credits = credits > 0 ? credits : 1;
680 temp->pid = current->pid;
681 temp->command = shdr->Command; /* Always LE */
682 temp->when_alloc = jiffies;
683 temp->server = server;
686 * The default is for the mid to be synchronous, so the
687 * default callback just wakes up the current task.
689 get_task_struct(current);
690 temp->creator = current;
691 temp->callback = cifs_wake_up_task;
692 temp->callback_data = current;
694 atomic_inc(&midCount);
695 temp->mid_state = MID_REQUEST_ALLOCATED;
696 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
697 le16_to_cpu(shdr->Command), temp->mid);
698 return temp;
701 static int
702 smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
703 struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
705 if (server->tcpStatus == CifsExiting)
706 return -ENOENT;
708 if (server->tcpStatus == CifsNeedReconnect) {
709 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
710 return -EAGAIN;
713 if (server->tcpStatus == CifsNeedNegotiate &&
714 shdr->Command != SMB2_NEGOTIATE)
715 return -EAGAIN;
717 if (ses->status == CifsNew) {
718 if ((shdr->Command != SMB2_SESSION_SETUP) &&
719 (shdr->Command != SMB2_NEGOTIATE))
720 return -EAGAIN;
721 /* else ok - we are setting up session */
724 if (ses->status == CifsExiting) {
725 if (shdr->Command != SMB2_LOGOFF)
726 return -EAGAIN;
727 /* else ok - we are shutting down the session */
730 *mid = smb2_mid_entry_alloc(shdr, server);
731 if (*mid == NULL)
732 return -ENOMEM;
733 spin_lock(&GlobalMid_Lock);
734 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
735 spin_unlock(&GlobalMid_Lock);
737 return 0;
741 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
742 bool log_error)
744 unsigned int len = mid->resp_buf_size;
745 struct kvec iov[1];
746 struct smb_rqst rqst = { .rq_iov = iov,
747 .rq_nvec = 1 };
749 iov[0].iov_base = (char *)mid->resp_buf;
750 iov[0].iov_len = len;
752 dump_smb(mid->resp_buf, min_t(u32, 80, len));
753 /* convert the length into a more usable form */
754 if (len > 24 && server->sign && !mid->decrypted) {
755 int rc;
757 rc = smb2_verify_signature(&rqst, server);
758 if (rc)
759 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
760 rc);
763 return map_smb2_to_linux_error(mid->resp_buf, log_error);
766 struct mid_q_entry *
767 smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
768 struct smb_rqst *rqst)
770 int rc;
771 struct smb2_sync_hdr *shdr =
772 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
773 struct mid_q_entry *mid;
775 smb2_seq_num_into_buf(server, shdr);
777 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
778 if (rc) {
779 revert_current_mid_from_hdr(server, shdr);
780 return ERR_PTR(rc);
783 rc = smb2_sign_rqst(rqst, server);
784 if (rc) {
785 revert_current_mid_from_hdr(server, shdr);
786 cifs_delete_mid(mid);
787 return ERR_PTR(rc);
790 return mid;
793 struct mid_q_entry *
794 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
796 int rc;
797 struct smb2_sync_hdr *shdr =
798 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
799 struct mid_q_entry *mid;
801 if (server->tcpStatus == CifsNeedNegotiate &&
802 shdr->Command != SMB2_NEGOTIATE)
803 return ERR_PTR(-EAGAIN);
805 smb2_seq_num_into_buf(server, shdr);
807 mid = smb2_mid_entry_alloc(shdr, server);
808 if (mid == NULL) {
809 revert_current_mid_from_hdr(server, shdr);
810 return ERR_PTR(-ENOMEM);
813 rc = smb2_sign_rqst(rqst, server);
814 if (rc) {
815 revert_current_mid_from_hdr(server, shdr);
816 DeleteMidQEntry(mid);
817 return ERR_PTR(rc);
820 return mid;
824 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
826 struct crypto_aead *tfm;
828 if (!server->secmech.ccmaesencrypt) {
829 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
830 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
831 else
832 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
833 if (IS_ERR(tfm)) {
834 cifs_server_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
835 __func__);
836 return PTR_ERR(tfm);
838 server->secmech.ccmaesencrypt = tfm;
841 if (!server->secmech.ccmaesdecrypt) {
842 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
843 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
844 else
845 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
846 if (IS_ERR(tfm)) {
847 crypto_free_aead(server->secmech.ccmaesencrypt);
848 server->secmech.ccmaesencrypt = NULL;
849 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
850 __func__);
851 return PTR_ERR(tfm);
853 server->secmech.ccmaesdecrypt = tfm;
856 return 0;