1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/net.h>
16 #include <linux/delay.h>
17 #include <linux/uaccess.h>
18 #include <asm/processor.h>
19 #include <linux/mempool.h>
20 #include <linux/highmem.h>
21 #include <crypto/aead.h>
23 #include "cifsproto.h"
24 #include "smb2proto.h"
25 #include "cifs_debug.h"
26 #include "smb2status.h"
30 smb3_crypto_shash_allocate(struct TCP_Server_Info
*server
)
32 struct cifs_secmech
*p
= &server
->secmech
;
35 rc
= cifs_alloc_hash("hmac(sha256)",
41 rc
= cifs_alloc_hash("cmac(aes)", &p
->cmacaes
, &p
->sdesccmacaes
);
47 cifs_free_hash(&p
->hmacsha256
, &p
->sdeschmacsha256
);
52 smb311_crypto_shash_allocate(struct TCP_Server_Info
*server
)
54 struct cifs_secmech
*p
= &server
->secmech
;
57 rc
= cifs_alloc_hash("hmac(sha256)",
63 rc
= cifs_alloc_hash("cmac(aes)", &p
->cmacaes
, &p
->sdesccmacaes
);
67 rc
= cifs_alloc_hash("sha512", &p
->sha512
, &p
->sdescsha512
);
74 cifs_free_hash(&p
->cmacaes
, &p
->sdesccmacaes
);
75 cifs_free_hash(&p
->hmacsha256
, &p
->sdeschmacsha256
);
81 int smb2_get_sign_key(__u64 ses_id
, struct TCP_Server_Info
*server
, u8
*key
)
83 struct cifs_chan
*chan
;
84 struct cifs_ses
*ses
= NULL
;
85 struct TCP_Server_Info
*it
= NULL
;
89 spin_lock(&cifs_tcp_ses_lock
);
91 list_for_each_entry(it
, &cifs_tcp_ses_list
, tcp_ses_list
) {
92 list_for_each_entry(ses
, &it
->smb_ses_list
, smb_ses_list
) {
93 if (ses
->Suid
== ses_id
)
97 cifs_server_dbg(VFS
, "%s: Could not find session 0x%llx\n",
103 spin_lock(&ses
->chan_lock
);
104 if (cifs_chan_needs_reconnect(ses
, server
) &&
105 !CIFS_ALL_CHANS_NEED_RECONNECT(ses
)) {
107 * If we are in the process of binding a new channel
108 * to an existing session, use the master connection
111 memcpy(key
, ses
->smb3signingkey
, SMB3_SIGN_KEY_SIZE
);
112 spin_unlock(&ses
->chan_lock
);
117 * Otherwise, use the channel key.
120 for (i
= 0; i
< ses
->chan_count
; i
++) {
121 chan
= ses
->chans
+ i
;
122 if (chan
->server
== server
) {
123 memcpy(key
, chan
->signkey
, SMB3_SIGN_KEY_SIZE
);
124 spin_unlock(&ses
->chan_lock
);
128 spin_unlock(&ses
->chan_lock
);
131 "%s: Could not find channel signing key for session 0x%llx\n",
136 spin_unlock(&cifs_tcp_ses_lock
);
140 static struct cifs_ses
*
141 smb2_find_smb_ses_unlocked(struct TCP_Server_Info
*server
, __u64 ses_id
)
143 struct cifs_ses
*ses
;
145 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
146 if (ses
->Suid
!= ses_id
)
156 smb2_find_smb_ses(struct TCP_Server_Info
*server
, __u64 ses_id
)
158 struct cifs_ses
*ses
;
160 spin_lock(&cifs_tcp_ses_lock
);
161 ses
= smb2_find_smb_ses_unlocked(server
, ses_id
);
162 spin_unlock(&cifs_tcp_ses_lock
);
167 static struct cifs_tcon
*
168 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses
*ses
, __u32 tid
)
170 struct cifs_tcon
*tcon
;
172 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
173 if (tcon
->tid
!= tid
)
183 * Obtain tcon corresponding to the tid in the given
188 smb2_find_smb_tcon(struct TCP_Server_Info
*server
, __u64 ses_id
, __u32 tid
)
190 struct cifs_ses
*ses
;
191 struct cifs_tcon
*tcon
;
193 spin_lock(&cifs_tcp_ses_lock
);
194 ses
= smb2_find_smb_ses_unlocked(server
, ses_id
);
196 spin_unlock(&cifs_tcp_ses_lock
);
199 tcon
= smb2_find_smb_sess_tcon_unlocked(ses
, tid
);
201 cifs_put_smb_ses(ses
);
202 spin_unlock(&cifs_tcp_ses_lock
);
205 spin_unlock(&cifs_tcp_ses_lock
);
206 /* tcon already has a ref to ses, so we don't need ses anymore */
207 cifs_put_smb_ses(ses
);
213 smb2_calc_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
,
214 bool allocate_crypto
)
217 unsigned char smb2_signature
[SMB2_HMACSHA256_SIZE
];
218 unsigned char *sigptr
= smb2_signature
;
219 struct kvec
*iov
= rqst
->rq_iov
;
220 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)iov
[0].iov_base
;
221 struct cifs_ses
*ses
;
222 struct shash_desc
*shash
;
223 struct crypto_shash
*hash
;
224 struct sdesc
*sdesc
= NULL
;
225 struct smb_rqst drqst
;
227 ses
= smb2_find_smb_ses(server
, le64_to_cpu(shdr
->SessionId
));
229 cifs_server_dbg(VFS
, "%s: Could not find session\n", __func__
);
233 memset(smb2_signature
, 0x0, SMB2_HMACSHA256_SIZE
);
234 memset(shdr
->Signature
, 0x0, SMB2_SIGNATURE_SIZE
);
236 if (allocate_crypto
) {
237 rc
= cifs_alloc_hash("hmac(sha256)", &hash
, &sdesc
);
240 "%s: sha256 alloc failed\n", __func__
);
243 shash
= &sdesc
->shash
;
245 hash
= server
->secmech
.hmacsha256
;
246 shash
= &server
->secmech
.sdeschmacsha256
->shash
;
249 rc
= crypto_shash_setkey(hash
, ses
->auth_key
.response
,
250 SMB2_NTLMV2_SESSKEY_SIZE
);
253 "%s: Could not update with response\n",
258 rc
= crypto_shash_init(shash
);
260 cifs_server_dbg(VFS
, "%s: Could not init sha256", __func__
);
265 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
266 * data, that is, iov[0] should not contain a rfc1002 length.
268 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
269 * __cifs_calc_signature().
272 if (drqst
.rq_nvec
>= 2 && iov
[0].iov_len
== 4) {
273 rc
= crypto_shash_update(shash
, iov
[0].iov_base
,
277 "%s: Could not update with payload\n",
285 rc
= __cifs_calc_signature(&drqst
, server
, sigptr
, shash
);
287 memcpy(shdr
->Signature
, sigptr
, SMB2_SIGNATURE_SIZE
);
291 cifs_free_hash(&hash
, &sdesc
);
293 cifs_put_smb_ses(ses
);
297 static int generate_key(struct cifs_ses
*ses
, struct kvec label
,
298 struct kvec context
, __u8
*key
, unsigned int key_size
)
300 unsigned char zero
= 0x0;
301 __u8 i
[4] = {0, 0, 0, 1};
302 __u8 L128
[4] = {0, 0, 0, 128};
303 __u8 L256
[4] = {0, 0, 1, 0};
305 unsigned char prfhash
[SMB2_HMACSHA256_SIZE
];
306 unsigned char *hashptr
= prfhash
;
307 struct TCP_Server_Info
*server
= ses
->server
;
309 memset(prfhash
, 0x0, SMB2_HMACSHA256_SIZE
);
310 memset(key
, 0x0, key_size
);
312 rc
= smb3_crypto_shash_allocate(server
);
314 cifs_server_dbg(VFS
, "%s: crypto alloc failed\n", __func__
);
315 goto smb3signkey_ret
;
318 rc
= crypto_shash_setkey(server
->secmech
.hmacsha256
,
319 ses
->auth_key
.response
, SMB2_NTLMV2_SESSKEY_SIZE
);
321 cifs_server_dbg(VFS
, "%s: Could not set with session key\n", __func__
);
322 goto smb3signkey_ret
;
325 rc
= crypto_shash_init(&server
->secmech
.sdeschmacsha256
->shash
);
327 cifs_server_dbg(VFS
, "%s: Could not init sign hmac\n", __func__
);
328 goto smb3signkey_ret
;
331 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
334 cifs_server_dbg(VFS
, "%s: Could not update with n\n", __func__
);
335 goto smb3signkey_ret
;
338 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
339 label
.iov_base
, label
.iov_len
);
341 cifs_server_dbg(VFS
, "%s: Could not update with label\n", __func__
);
342 goto smb3signkey_ret
;
345 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
348 cifs_server_dbg(VFS
, "%s: Could not update with zero\n", __func__
);
349 goto smb3signkey_ret
;
352 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
353 context
.iov_base
, context
.iov_len
);
355 cifs_server_dbg(VFS
, "%s: Could not update with context\n", __func__
);
356 goto smb3signkey_ret
;
359 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES256_CCM
) ||
360 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
)) {
361 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
364 rc
= crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
368 cifs_server_dbg(VFS
, "%s: Could not update with L\n", __func__
);
369 goto smb3signkey_ret
;
372 rc
= crypto_shash_final(&server
->secmech
.sdeschmacsha256
->shash
,
375 cifs_server_dbg(VFS
, "%s: Could not generate sha256 hash\n", __func__
);
376 goto smb3signkey_ret
;
379 memcpy(key
, hashptr
, key_size
);
390 struct derivation_triplet
{
391 struct derivation signing
;
392 struct derivation encryption
;
393 struct derivation decryption
;
397 generate_smb3signingkey(struct cifs_ses
*ses
,
398 struct TCP_Server_Info
*server
,
399 const struct derivation_triplet
*ptriplet
)
402 bool is_binding
= false;
405 spin_lock(&ses
->chan_lock
);
406 is_binding
= !CIFS_ALL_CHANS_NEED_RECONNECT(ses
);
407 chan_index
= cifs_ses_get_chan_index(ses
, server
);
408 /* TODO: introduce ref counting for channels when the can be freed */
409 spin_unlock(&ses
->chan_lock
);
412 * All channels use the same encryption/decryption keys but
413 * they have their own signing key.
415 * When we generate the keys, check if it is for a new channel
416 * (binding) in which case we only need to generate a signing
417 * key and store it in the channel as to not overwrite the
418 * master connection signing key stored in the session
422 rc
= generate_key(ses
, ptriplet
->signing
.label
,
423 ptriplet
->signing
.context
,
424 ses
->chans
[chan_index
].signkey
,
429 rc
= generate_key(ses
, ptriplet
->signing
.label
,
430 ptriplet
->signing
.context
,
436 /* safe to access primary channel, since it will never go away */
437 spin_lock(&ses
->chan_lock
);
438 memcpy(ses
->chans
[0].signkey
, ses
->smb3signingkey
,
440 spin_unlock(&ses
->chan_lock
);
442 rc
= generate_key(ses
, ptriplet
->encryption
.label
,
443 ptriplet
->encryption
.context
,
444 ses
->smb3encryptionkey
,
445 SMB3_ENC_DEC_KEY_SIZE
);
446 rc
= generate_key(ses
, ptriplet
->decryption
.label
,
447 ptriplet
->decryption
.context
,
448 ses
->smb3decryptionkey
,
449 SMB3_ENC_DEC_KEY_SIZE
);
457 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
458 cifs_dbg(VFS
, "%s: dumping generated AES session keys\n", __func__
);
460 * The session id is opaque in terms of endianness, so we can't
461 * print it as a long long. we dump it as we got it on the wire
463 cifs_dbg(VFS
, "Session Id %*ph\n", (int)sizeof(ses
->Suid
),
465 cifs_dbg(VFS
, "Cipher type %d\n", server
->cipher_type
);
466 cifs_dbg(VFS
, "Session Key %*ph\n",
467 SMB2_NTLMV2_SESSKEY_SIZE
, ses
->auth_key
.response
);
468 cifs_dbg(VFS
, "Signing Key %*ph\n",
469 SMB3_SIGN_KEY_SIZE
, ses
->smb3signingkey
);
470 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES256_CCM
) ||
471 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
)) {
472 cifs_dbg(VFS
, "ServerIn Key %*ph\n",
473 SMB3_GCM256_CRYPTKEY_SIZE
, ses
->smb3encryptionkey
);
474 cifs_dbg(VFS
, "ServerOut Key %*ph\n",
475 SMB3_GCM256_CRYPTKEY_SIZE
, ses
->smb3decryptionkey
);
477 cifs_dbg(VFS
, "ServerIn Key %*ph\n",
478 SMB3_GCM128_CRYPTKEY_SIZE
, ses
->smb3encryptionkey
);
479 cifs_dbg(VFS
, "ServerOut Key %*ph\n",
480 SMB3_GCM128_CRYPTKEY_SIZE
, ses
->smb3decryptionkey
);
487 generate_smb30signingkey(struct cifs_ses
*ses
,
488 struct TCP_Server_Info
*server
)
491 struct derivation_triplet triplet
;
492 struct derivation
*d
;
494 d
= &triplet
.signing
;
495 d
->label
.iov_base
= "SMB2AESCMAC";
496 d
->label
.iov_len
= 12;
497 d
->context
.iov_base
= "SmbSign";
498 d
->context
.iov_len
= 8;
500 d
= &triplet
.encryption
;
501 d
->label
.iov_base
= "SMB2AESCCM";
502 d
->label
.iov_len
= 11;
503 d
->context
.iov_base
= "ServerIn ";
504 d
->context
.iov_len
= 10;
506 d
= &triplet
.decryption
;
507 d
->label
.iov_base
= "SMB2AESCCM";
508 d
->label
.iov_len
= 11;
509 d
->context
.iov_base
= "ServerOut";
510 d
->context
.iov_len
= 10;
512 return generate_smb3signingkey(ses
, server
, &triplet
);
516 generate_smb311signingkey(struct cifs_ses
*ses
,
517 struct TCP_Server_Info
*server
)
520 struct derivation_triplet triplet
;
521 struct derivation
*d
;
523 d
= &triplet
.signing
;
524 d
->label
.iov_base
= "SMBSigningKey";
525 d
->label
.iov_len
= 14;
526 d
->context
.iov_base
= ses
->preauth_sha_hash
;
527 d
->context
.iov_len
= 64;
529 d
= &triplet
.encryption
;
530 d
->label
.iov_base
= "SMBC2SCipherKey";
531 d
->label
.iov_len
= 16;
532 d
->context
.iov_base
= ses
->preauth_sha_hash
;
533 d
->context
.iov_len
= 64;
535 d
= &triplet
.decryption
;
536 d
->label
.iov_base
= "SMBS2CCipherKey";
537 d
->label
.iov_len
= 16;
538 d
->context
.iov_base
= ses
->preauth_sha_hash
;
539 d
->context
.iov_len
= 64;
541 return generate_smb3signingkey(ses
, server
, &triplet
);
545 smb3_calc_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
,
546 bool allocate_crypto
)
549 unsigned char smb3_signature
[SMB2_CMACAES_SIZE
];
550 unsigned char *sigptr
= smb3_signature
;
551 struct kvec
*iov
= rqst
->rq_iov
;
552 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)iov
[0].iov_base
;
553 struct shash_desc
*shash
;
554 struct crypto_shash
*hash
;
555 struct sdesc
*sdesc
= NULL
;
556 struct smb_rqst drqst
;
557 u8 key
[SMB3_SIGN_KEY_SIZE
];
559 rc
= smb2_get_sign_key(le64_to_cpu(shdr
->SessionId
), server
, key
);
563 if (allocate_crypto
) {
564 rc
= cifs_alloc_hash("cmac(aes)", &hash
, &sdesc
);
568 shash
= &sdesc
->shash
;
570 hash
= server
->secmech
.cmacaes
;
571 shash
= &server
->secmech
.sdesccmacaes
->shash
;
574 memset(smb3_signature
, 0x0, SMB2_CMACAES_SIZE
);
575 memset(shdr
->Signature
, 0x0, SMB2_SIGNATURE_SIZE
);
577 rc
= crypto_shash_setkey(hash
, key
, SMB2_CMACAES_SIZE
);
579 cifs_server_dbg(VFS
, "%s: Could not set key for cmac aes\n", __func__
);
584 * we already allocate sdesccmacaes when we init smb3 signing key,
585 * so unlike smb2 case we do not have to check here if secmech are
588 rc
= crypto_shash_init(shash
);
590 cifs_server_dbg(VFS
, "%s: Could not init cmac aes\n", __func__
);
595 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
596 * data, that is, iov[0] should not contain a rfc1002 length.
598 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
599 * __cifs_calc_signature().
602 if (drqst
.rq_nvec
>= 2 && iov
[0].iov_len
== 4) {
603 rc
= crypto_shash_update(shash
, iov
[0].iov_base
,
606 cifs_server_dbg(VFS
, "%s: Could not update with payload\n",
614 rc
= __cifs_calc_signature(&drqst
, server
, sigptr
, shash
);
616 memcpy(shdr
->Signature
, sigptr
, SMB2_SIGNATURE_SIZE
);
620 cifs_free_hash(&hash
, &sdesc
);
624 /* must be called with server->srv_mutex held */
626 smb2_sign_rqst(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
629 struct smb2_hdr
*shdr
;
630 struct smb2_sess_setup_req
*ssr
;
634 shdr
= (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
635 ssr
= (struct smb2_sess_setup_req
*)shdr
;
637 is_binding
= shdr
->Command
== SMB2_SESSION_SETUP
&&
638 (ssr
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
);
639 is_signed
= shdr
->Flags
& SMB2_FLAGS_SIGNED
;
643 spin_lock(&server
->srv_lock
);
644 if (server
->ops
->need_neg
&&
645 server
->ops
->need_neg(server
)) {
646 spin_unlock(&server
->srv_lock
);
649 spin_unlock(&server
->srv_lock
);
650 if (!is_binding
&& !server
->session_estab
) {
651 strncpy(shdr
->Signature
, "BSRSPYL", 8);
655 rc
= server
->ops
->calc_signature(rqst
, server
, false);
661 smb2_verify_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
664 char server_response_sig
[SMB2_SIGNATURE_SIZE
];
665 struct smb2_hdr
*shdr
=
666 (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
668 if ((shdr
->Command
== SMB2_NEGOTIATE
) ||
669 (shdr
->Command
== SMB2_SESSION_SETUP
) ||
670 (shdr
->Command
== SMB2_OPLOCK_BREAK
) ||
671 server
->ignore_signature
||
672 (!server
->session_estab
))
676 * BB what if signatures are supposed to be on for session but
677 * server does not send one? BB
680 /* Do not need to verify session setups with signature "BSRSPYL " */
681 if (memcmp(shdr
->Signature
, "BSRSPYL ", 8) == 0)
682 cifs_dbg(FYI
, "dummy signature received for smb command 0x%x\n",
686 * Save off the origiginal signature so we can modify the smb and check
687 * our calculated signature against what the server sent.
689 memcpy(server_response_sig
, shdr
->Signature
, SMB2_SIGNATURE_SIZE
);
691 memset(shdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
693 rc
= server
->ops
->calc_signature(rqst
, server
, true);
698 if (memcmp(server_response_sig
, shdr
->Signature
, SMB2_SIGNATURE_SIZE
)) {
699 cifs_dbg(VFS
, "sign fail cmd 0x%x message id 0x%llx\n",
700 shdr
->Command
, shdr
->MessageId
);
707 * Set message id for the request. Should be called after wait_for_free_request
708 * and when srv_mutex is held.
711 smb2_seq_num_into_buf(struct TCP_Server_Info
*server
,
712 struct smb2_hdr
*shdr
)
714 unsigned int i
, num
= le16_to_cpu(shdr
->CreditCharge
);
716 shdr
->MessageId
= get_next_mid64(server
);
717 /* skip message numbers according to CreditCharge field */
718 for (i
= 1; i
< num
; i
++)
719 get_next_mid(server
);
722 static struct mid_q_entry
*
723 smb2_mid_entry_alloc(const struct smb2_hdr
*shdr
,
724 struct TCP_Server_Info
*server
)
726 struct mid_q_entry
*temp
;
727 unsigned int credits
= le16_to_cpu(shdr
->CreditCharge
);
729 if (server
== NULL
) {
730 cifs_dbg(VFS
, "Null TCP session in smb2_mid_entry_alloc\n");
734 temp
= mempool_alloc(cifs_mid_poolp
, GFP_NOFS
);
735 memset(temp
, 0, sizeof(struct mid_q_entry
));
736 kref_init(&temp
->refcount
);
737 temp
->mid
= le64_to_cpu(shdr
->MessageId
);
738 temp
->credits
= credits
> 0 ? credits
: 1;
739 temp
->pid
= current
->pid
;
740 temp
->command
= shdr
->Command
; /* Always LE */
741 temp
->when_alloc
= jiffies
;
742 temp
->server
= server
;
745 * The default is for the mid to be synchronous, so the
746 * default callback just wakes up the current task.
748 get_task_struct(current
);
749 temp
->creator
= current
;
750 temp
->callback
= cifs_wake_up_task
;
751 temp
->callback_data
= current
;
753 atomic_inc(&mid_count
);
754 temp
->mid_state
= MID_REQUEST_ALLOCATED
;
755 trace_smb3_cmd_enter(le32_to_cpu(shdr
->Id
.SyncId
.TreeId
),
756 le64_to_cpu(shdr
->SessionId
),
757 le16_to_cpu(shdr
->Command
), temp
->mid
);
762 smb2_get_mid_entry(struct cifs_ses
*ses
, struct TCP_Server_Info
*server
,
763 struct smb2_hdr
*shdr
, struct mid_q_entry
**mid
)
765 spin_lock(&server
->srv_lock
);
766 if (server
->tcpStatus
== CifsExiting
) {
767 spin_unlock(&server
->srv_lock
);
771 if (server
->tcpStatus
== CifsNeedReconnect
) {
772 spin_unlock(&server
->srv_lock
);
773 cifs_dbg(FYI
, "tcp session dead - return to caller to retry\n");
777 if (server
->tcpStatus
== CifsNeedNegotiate
&&
778 shdr
->Command
!= SMB2_NEGOTIATE
) {
779 spin_unlock(&server
->srv_lock
);
782 spin_unlock(&server
->srv_lock
);
784 spin_lock(&ses
->ses_lock
);
785 if (ses
->ses_status
== SES_NEW
) {
786 if ((shdr
->Command
!= SMB2_SESSION_SETUP
) &&
787 (shdr
->Command
!= SMB2_NEGOTIATE
)) {
788 spin_unlock(&ses
->ses_lock
);
791 /* else ok - we are setting up session */
794 if (ses
->ses_status
== SES_EXITING
) {
795 if (shdr
->Command
!= SMB2_LOGOFF
) {
796 spin_unlock(&ses
->ses_lock
);
799 /* else ok - we are shutting down the session */
801 spin_unlock(&ses
->ses_lock
);
803 *mid
= smb2_mid_entry_alloc(shdr
, server
);
806 spin_lock(&server
->mid_lock
);
807 list_add_tail(&(*mid
)->qhead
, &server
->pending_mid_q
);
808 spin_unlock(&server
->mid_lock
);
814 smb2_check_receive(struct mid_q_entry
*mid
, struct TCP_Server_Info
*server
,
817 unsigned int len
= mid
->resp_buf_size
;
819 struct smb_rqst rqst
= { .rq_iov
= iov
,
822 iov
[0].iov_base
= (char *)mid
->resp_buf
;
823 iov
[0].iov_len
= len
;
825 dump_smb(mid
->resp_buf
, min_t(u32
, 80, len
));
826 /* convert the length into a more usable form */
827 if (len
> 24 && server
->sign
&& !mid
->decrypted
) {
830 rc
= smb2_verify_signature(&rqst
, server
);
832 cifs_server_dbg(VFS
, "SMB signature verification returned error = %d\n",
836 return map_smb2_to_linux_error(mid
->resp_buf
, log_error
);
840 smb2_setup_request(struct cifs_ses
*ses
, struct TCP_Server_Info
*server
,
841 struct smb_rqst
*rqst
)
844 struct smb2_hdr
*shdr
=
845 (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
846 struct mid_q_entry
*mid
;
848 smb2_seq_num_into_buf(server
, shdr
);
850 rc
= smb2_get_mid_entry(ses
, server
, shdr
, &mid
);
852 revert_current_mid_from_hdr(server
, shdr
);
856 rc
= smb2_sign_rqst(rqst
, server
);
858 revert_current_mid_from_hdr(server
, shdr
);
867 smb2_setup_async_request(struct TCP_Server_Info
*server
, struct smb_rqst
*rqst
)
870 struct smb2_hdr
*shdr
=
871 (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
872 struct mid_q_entry
*mid
;
874 spin_lock(&server
->srv_lock
);
875 if (server
->tcpStatus
== CifsNeedNegotiate
&&
876 shdr
->Command
!= SMB2_NEGOTIATE
) {
877 spin_unlock(&server
->srv_lock
);
878 return ERR_PTR(-EAGAIN
);
880 spin_unlock(&server
->srv_lock
);
882 smb2_seq_num_into_buf(server
, shdr
);
884 mid
= smb2_mid_entry_alloc(shdr
, server
);
886 revert_current_mid_from_hdr(server
, shdr
);
887 return ERR_PTR(-ENOMEM
);
890 rc
= smb2_sign_rqst(rqst
, server
);
892 revert_current_mid_from_hdr(server
, shdr
);
901 smb3_crypto_aead_allocate(struct TCP_Server_Info
*server
)
903 struct crypto_aead
*tfm
;
905 if (!server
->secmech
.ccmaesencrypt
) {
906 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
907 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
908 tfm
= crypto_alloc_aead("gcm(aes)", 0, 0);
910 tfm
= crypto_alloc_aead("ccm(aes)", 0, 0);
912 cifs_server_dbg(VFS
, "%s: Failed alloc encrypt aead\n",
916 server
->secmech
.ccmaesencrypt
= tfm
;
919 if (!server
->secmech
.ccmaesdecrypt
) {
920 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
921 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
922 tfm
= crypto_alloc_aead("gcm(aes)", 0, 0);
924 tfm
= crypto_alloc_aead("ccm(aes)", 0, 0);
926 crypto_free_aead(server
->secmech
.ccmaesencrypt
);
927 server
->secmech
.ccmaesencrypt
= NULL
;
928 cifs_server_dbg(VFS
, "%s: Failed to alloc decrypt aead\n",
932 server
->secmech
.ccmaesdecrypt
= tfm
;