Merge branch 'dmi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvar...
[linux-2.6-microblaze.git] / fs / cifs / smb2transport.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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  */
11
12 #include <linux/fs.h>
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>
22 #include "cifsglob.h"
23 #include "cifsproto.h"
24 #include "smb2proto.h"
25 #include "cifs_debug.h"
26 #include "smb2status.h"
27 #include "smb2glob.h"
28
29 static int
30 smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
31 {
32         struct cifs_secmech *p = &server->secmech;
33         int rc;
34
35         rc = cifs_alloc_hash("hmac(sha256)", &p->hmacsha256);
36         if (rc)
37                 goto err;
38
39         rc = cifs_alloc_hash("cmac(aes)", &p->aes_cmac);
40         if (rc)
41                 goto err;
42
43         return 0;
44 err:
45         cifs_free_hash(&p->hmacsha256);
46         return rc;
47 }
48
49 int
50 smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
51 {
52         struct cifs_secmech *p = &server->secmech;
53         int rc = 0;
54
55         rc = cifs_alloc_hash("hmac(sha256)", &p->hmacsha256);
56         if (rc)
57                 return rc;
58
59         rc = cifs_alloc_hash("cmac(aes)", &p->aes_cmac);
60         if (rc)
61                 goto err;
62
63         rc = cifs_alloc_hash("sha512", &p->sha512);
64         if (rc)
65                 goto err;
66
67         return 0;
68
69 err:
70         cifs_free_hash(&p->aes_cmac);
71         cifs_free_hash(&p->hmacsha256);
72         return rc;
73 }
74
75
76 static
77 int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
78 {
79         struct cifs_chan *chan;
80         struct cifs_ses *ses = NULL;
81         struct TCP_Server_Info *it = NULL;
82         int i;
83         int rc = 0;
84
85         spin_lock(&cifs_tcp_ses_lock);
86
87         list_for_each_entry(it, &cifs_tcp_ses_list, tcp_ses_list) {
88                 list_for_each_entry(ses, &it->smb_ses_list, smb_ses_list) {
89                         if (ses->Suid == ses_id)
90                                 goto found;
91                 }
92         }
93         cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
94                         __func__, ses_id);
95         rc = -ENOENT;
96         goto out;
97
98 found:
99         spin_lock(&ses->chan_lock);
100         if (cifs_chan_needs_reconnect(ses, server) &&
101             !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
102                 /*
103                  * If we are in the process of binding a new channel
104                  * to an existing session, use the master connection
105                  * session key
106                  */
107                 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
108                 spin_unlock(&ses->chan_lock);
109                 goto out;
110         }
111
112         /*
113          * Otherwise, use the channel key.
114          */
115
116         for (i = 0; i < ses->chan_count; i++) {
117                 chan = ses->chans + i;
118                 if (chan->server == server) {
119                         memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
120                         spin_unlock(&ses->chan_lock);
121                         goto out;
122                 }
123         }
124         spin_unlock(&ses->chan_lock);
125
126         cifs_dbg(VFS,
127                  "%s: Could not find channel signing key for session 0x%llx\n",
128                  __func__, ses_id);
129         rc = -ENOENT;
130
131 out:
132         spin_unlock(&cifs_tcp_ses_lock);
133         return rc;
134 }
135
136 static struct cifs_ses *
137 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
138 {
139         struct cifs_ses *ses;
140
141         list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
142                 if (ses->Suid != ses_id)
143                         continue;
144                 ++ses->ses_count;
145                 return ses;
146         }
147
148         return NULL;
149 }
150
151 struct cifs_ses *
152 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
153 {
154         struct cifs_ses *ses;
155
156         spin_lock(&cifs_tcp_ses_lock);
157         ses = smb2_find_smb_ses_unlocked(server, ses_id);
158         spin_unlock(&cifs_tcp_ses_lock);
159
160         return ses;
161 }
162
163 static struct cifs_tcon *
164 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32  tid)
165 {
166         struct cifs_tcon *tcon;
167
168         list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
169                 if (tcon->tid != tid)
170                         continue;
171                 ++tcon->tc_count;
172                 return tcon;
173         }
174
175         return NULL;
176 }
177
178 /*
179  * Obtain tcon corresponding to the tid in the given
180  * cifs_ses
181  */
182
183 struct cifs_tcon *
184 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32  tid)
185 {
186         struct cifs_ses *ses;
187         struct cifs_tcon *tcon;
188
189         spin_lock(&cifs_tcp_ses_lock);
190         ses = smb2_find_smb_ses_unlocked(server, ses_id);
191         if (!ses) {
192                 spin_unlock(&cifs_tcp_ses_lock);
193                 return NULL;
194         }
195         tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
196         if (!tcon) {
197                 cifs_put_smb_ses(ses);
198                 spin_unlock(&cifs_tcp_ses_lock);
199                 return NULL;
200         }
201         spin_unlock(&cifs_tcp_ses_lock);
202         /* tcon already has a ref to ses, so we don't need ses anymore */
203         cifs_put_smb_ses(ses);
204
205         return tcon;
206 }
207
208 int
209 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
210                         bool allocate_crypto)
211 {
212         int rc;
213         unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
214         unsigned char *sigptr = smb2_signature;
215         struct kvec *iov = rqst->rq_iov;
216         struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
217         struct cifs_ses *ses;
218         struct shash_desc *shash = NULL;
219         struct smb_rqst drqst;
220
221         ses = smb2_find_smb_ses(server, le64_to_cpu(shdr->SessionId));
222         if (unlikely(!ses)) {
223                 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
224                 return -ENOENT;
225         }
226
227         memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
228         memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
229
230         if (allocate_crypto) {
231                 rc = cifs_alloc_hash("hmac(sha256)", &shash);
232                 if (rc) {
233                         cifs_server_dbg(VFS,
234                                         "%s: sha256 alloc failed\n", __func__);
235                         goto out;
236                 }
237         } else {
238                 shash = server->secmech.hmacsha256;
239         }
240
241         rc = crypto_shash_setkey(shash->tfm, ses->auth_key.response,
242                         SMB2_NTLMV2_SESSKEY_SIZE);
243         if (rc) {
244                 cifs_server_dbg(VFS,
245                                 "%s: Could not update with response\n",
246                                 __func__);
247                 goto out;
248         }
249
250         rc = crypto_shash_init(shash);
251         if (rc) {
252                 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
253                 goto out;
254         }
255
256         /*
257          * For SMB2+, __cifs_calc_signature() expects to sign only the actual
258          * data, that is, iov[0] should not contain a rfc1002 length.
259          *
260          * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
261          * __cifs_calc_signature().
262          */
263         drqst = *rqst;
264         if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
265                 rc = crypto_shash_update(shash, iov[0].iov_base,
266                                          iov[0].iov_len);
267                 if (rc) {
268                         cifs_server_dbg(VFS,
269                                         "%s: Could not update with payload\n",
270                                         __func__);
271                         goto out;
272                 }
273                 drqst.rq_iov++;
274                 drqst.rq_nvec--;
275         }
276
277         rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
278         if (!rc)
279                 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
280
281 out:
282         if (allocate_crypto)
283                 cifs_free_hash(&shash);
284         if (ses)
285                 cifs_put_smb_ses(ses);
286         return rc;
287 }
288
289 static int generate_key(struct cifs_ses *ses, struct kvec label,
290                         struct kvec context, __u8 *key, unsigned int key_size)
291 {
292         unsigned char zero = 0x0;
293         __u8 i[4] = {0, 0, 0, 1};
294         __u8 L128[4] = {0, 0, 0, 128};
295         __u8 L256[4] = {0, 0, 1, 0};
296         int rc = 0;
297         unsigned char prfhash[SMB2_HMACSHA256_SIZE];
298         unsigned char *hashptr = prfhash;
299         struct TCP_Server_Info *server = ses->server;
300
301         memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
302         memset(key, 0x0, key_size);
303
304         rc = smb3_crypto_shash_allocate(server);
305         if (rc) {
306                 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
307                 goto smb3signkey_ret;
308         }
309
310         rc = crypto_shash_setkey(server->secmech.hmacsha256->tfm,
311                 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
312         if (rc) {
313                 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
314                 goto smb3signkey_ret;
315         }
316
317         rc = crypto_shash_init(server->secmech.hmacsha256);
318         if (rc) {
319                 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
320                 goto smb3signkey_ret;
321         }
322
323         rc = crypto_shash_update(server->secmech.hmacsha256, i, 4);
324         if (rc) {
325                 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
326                 goto smb3signkey_ret;
327         }
328
329         rc = crypto_shash_update(server->secmech.hmacsha256, label.iov_base, label.iov_len);
330         if (rc) {
331                 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
332                 goto smb3signkey_ret;
333         }
334
335         rc = crypto_shash_update(server->secmech.hmacsha256, &zero, 1);
336         if (rc) {
337                 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
338                 goto smb3signkey_ret;
339         }
340
341         rc = crypto_shash_update(server->secmech.hmacsha256, context.iov_base, context.iov_len);
342         if (rc) {
343                 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
344                 goto smb3signkey_ret;
345         }
346
347         if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
348                 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) {
349                 rc = crypto_shash_update(server->secmech.hmacsha256, L256, 4);
350         } else {
351                 rc = crypto_shash_update(server->secmech.hmacsha256, L128, 4);
352         }
353         if (rc) {
354                 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
355                 goto smb3signkey_ret;
356         }
357
358         rc = crypto_shash_final(server->secmech.hmacsha256, hashptr);
359         if (rc) {
360                 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
361                 goto smb3signkey_ret;
362         }
363
364         memcpy(key, hashptr, key_size);
365
366 smb3signkey_ret:
367         return rc;
368 }
369
370 struct derivation {
371         struct kvec label;
372         struct kvec context;
373 };
374
375 struct derivation_triplet {
376         struct derivation signing;
377         struct derivation encryption;
378         struct derivation decryption;
379 };
380
381 static int
382 generate_smb3signingkey(struct cifs_ses *ses,
383                         struct TCP_Server_Info *server,
384                         const struct derivation_triplet *ptriplet)
385 {
386         int rc;
387         bool is_binding = false;
388         int chan_index = 0;
389
390         spin_lock(&ses->chan_lock);
391         is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
392         chan_index = cifs_ses_get_chan_index(ses, server);
393         /* TODO: introduce ref counting for channels when the can be freed */
394         spin_unlock(&ses->chan_lock);
395
396         /*
397          * All channels use the same encryption/decryption keys but
398          * they have their own signing key.
399          *
400          * When we generate the keys, check if it is for a new channel
401          * (binding) in which case we only need to generate a signing
402          * key and store it in the channel as to not overwrite the
403          * master connection signing key stored in the session
404          */
405
406         if (is_binding) {
407                 rc = generate_key(ses, ptriplet->signing.label,
408                                   ptriplet->signing.context,
409                                   ses->chans[chan_index].signkey,
410                                   SMB3_SIGN_KEY_SIZE);
411                 if (rc)
412                         return rc;
413         } else {
414                 rc = generate_key(ses, ptriplet->signing.label,
415                                   ptriplet->signing.context,
416                                   ses->smb3signingkey,
417                                   SMB3_SIGN_KEY_SIZE);
418                 if (rc)
419                         return rc;
420
421                 /* safe to access primary channel, since it will never go away */
422                 spin_lock(&ses->chan_lock);
423                 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
424                        SMB3_SIGN_KEY_SIZE);
425                 spin_unlock(&ses->chan_lock);
426
427                 rc = generate_key(ses, ptriplet->encryption.label,
428                                   ptriplet->encryption.context,
429                                   ses->smb3encryptionkey,
430                                   SMB3_ENC_DEC_KEY_SIZE);
431                 rc = generate_key(ses, ptriplet->decryption.label,
432                                   ptriplet->decryption.context,
433                                   ses->smb3decryptionkey,
434                                   SMB3_ENC_DEC_KEY_SIZE);
435                 if (rc)
436                         return rc;
437         }
438
439         if (rc)
440                 return rc;
441
442 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
443         cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
444         /*
445          * The session id is opaque in terms of endianness, so we can't
446          * print it as a long long. we dump it as we got it on the wire
447          */
448         cifs_dbg(VFS, "Session Id    %*ph\n", (int)sizeof(ses->Suid),
449                         &ses->Suid);
450         cifs_dbg(VFS, "Cipher type   %d\n", server->cipher_type);
451         cifs_dbg(VFS, "Session Key   %*ph\n",
452                  SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
453         cifs_dbg(VFS, "Signing Key   %*ph\n",
454                  SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
455         if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
456                 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) {
457                 cifs_dbg(VFS, "ServerIn Key  %*ph\n",
458                                 SMB3_GCM256_CRYPTKEY_SIZE, ses->smb3encryptionkey);
459                 cifs_dbg(VFS, "ServerOut Key %*ph\n",
460                                 SMB3_GCM256_CRYPTKEY_SIZE, ses->smb3decryptionkey);
461         } else {
462                 cifs_dbg(VFS, "ServerIn Key  %*ph\n",
463                                 SMB3_GCM128_CRYPTKEY_SIZE, ses->smb3encryptionkey);
464                 cifs_dbg(VFS, "ServerOut Key %*ph\n",
465                                 SMB3_GCM128_CRYPTKEY_SIZE, ses->smb3decryptionkey);
466         }
467 #endif
468         return rc;
469 }
470
471 int
472 generate_smb30signingkey(struct cifs_ses *ses,
473                          struct TCP_Server_Info *server)
474
475 {
476         struct derivation_triplet triplet;
477         struct derivation *d;
478
479         d = &triplet.signing;
480         d->label.iov_base = "SMB2AESCMAC";
481         d->label.iov_len = 12;
482         d->context.iov_base = "SmbSign";
483         d->context.iov_len = 8;
484
485         d = &triplet.encryption;
486         d->label.iov_base = "SMB2AESCCM";
487         d->label.iov_len = 11;
488         d->context.iov_base = "ServerIn ";
489         d->context.iov_len = 10;
490
491         d = &triplet.decryption;
492         d->label.iov_base = "SMB2AESCCM";
493         d->label.iov_len = 11;
494         d->context.iov_base = "ServerOut";
495         d->context.iov_len = 10;
496
497         return generate_smb3signingkey(ses, server, &triplet);
498 }
499
500 int
501 generate_smb311signingkey(struct cifs_ses *ses,
502                           struct TCP_Server_Info *server)
503
504 {
505         struct derivation_triplet triplet;
506         struct derivation *d;
507
508         d = &triplet.signing;
509         d->label.iov_base = "SMBSigningKey";
510         d->label.iov_len = 14;
511         d->context.iov_base = ses->preauth_sha_hash;
512         d->context.iov_len = 64;
513
514         d = &triplet.encryption;
515         d->label.iov_base = "SMBC2SCipherKey";
516         d->label.iov_len = 16;
517         d->context.iov_base = ses->preauth_sha_hash;
518         d->context.iov_len = 64;
519
520         d = &triplet.decryption;
521         d->label.iov_base = "SMBS2CCipherKey";
522         d->label.iov_len = 16;
523         d->context.iov_base = ses->preauth_sha_hash;
524         d->context.iov_len = 64;
525
526         return generate_smb3signingkey(ses, server, &triplet);
527 }
528
529 int
530 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
531                         bool allocate_crypto)
532 {
533         int rc;
534         unsigned char smb3_signature[SMB2_CMACAES_SIZE];
535         unsigned char *sigptr = smb3_signature;
536         struct kvec *iov = rqst->rq_iov;
537         struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
538         struct shash_desc *shash = NULL;
539         struct smb_rqst drqst;
540         u8 key[SMB3_SIGN_KEY_SIZE];
541
542         rc = smb2_get_sign_key(le64_to_cpu(shdr->SessionId), server, key);
543         if (unlikely(rc)) {
544                 cifs_server_dbg(VFS, "%s: Could not get signing key\n", __func__);
545                 return rc;
546         }
547
548         if (allocate_crypto) {
549                 rc = cifs_alloc_hash("cmac(aes)", &shash);
550                 if (rc)
551                         return rc;
552         } else {
553                 shash = server->secmech.aes_cmac;
554         }
555
556         memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
557         memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
558
559         rc = crypto_shash_setkey(shash->tfm, key, SMB2_CMACAES_SIZE);
560         if (rc) {
561                 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
562                 goto out;
563         }
564
565         /*
566          * we already allocate aes_cmac when we init smb3 signing key,
567          * so unlike smb2 case we do not have to check here if secmech are
568          * initialized
569          */
570         rc = crypto_shash_init(shash);
571         if (rc) {
572                 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
573                 goto out;
574         }
575
576         /*
577          * For SMB2+, __cifs_calc_signature() expects to sign only the actual
578          * data, that is, iov[0] should not contain a rfc1002 length.
579          *
580          * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
581          * __cifs_calc_signature().
582          */
583         drqst = *rqst;
584         if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
585                 rc = crypto_shash_update(shash, iov[0].iov_base,
586                                          iov[0].iov_len);
587                 if (rc) {
588                         cifs_server_dbg(VFS, "%s: Could not update with payload\n",
589                                  __func__);
590                         goto out;
591                 }
592                 drqst.rq_iov++;
593                 drqst.rq_nvec--;
594         }
595
596         rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
597         if (!rc)
598                 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
599
600 out:
601         if (allocate_crypto)
602                 cifs_free_hash(&shash);
603         return rc;
604 }
605
606 /* must be called with server->srv_mutex held */
607 static int
608 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
609 {
610         int rc = 0;
611         struct smb2_hdr *shdr;
612         struct smb2_sess_setup_req *ssr;
613         bool is_binding;
614         bool is_signed;
615
616         shdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
617         ssr = (struct smb2_sess_setup_req *)shdr;
618
619         is_binding = shdr->Command == SMB2_SESSION_SETUP &&
620                 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
621         is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
622
623         if (!is_signed)
624                 return 0;
625         spin_lock(&server->srv_lock);
626         if (server->ops->need_neg &&
627             server->ops->need_neg(server)) {
628                 spin_unlock(&server->srv_lock);
629                 return 0;
630         }
631         spin_unlock(&server->srv_lock);
632         if (!is_binding && !server->session_estab) {
633                 strncpy(shdr->Signature, "BSRSPYL", 8);
634                 return 0;
635         }
636
637         rc = server->ops->calc_signature(rqst, server, false);
638
639         return rc;
640 }
641
642 int
643 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
644 {
645         unsigned int rc;
646         char server_response_sig[SMB2_SIGNATURE_SIZE];
647         struct smb2_hdr *shdr =
648                         (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
649
650         if ((shdr->Command == SMB2_NEGOTIATE) ||
651             (shdr->Command == SMB2_SESSION_SETUP) ||
652             (shdr->Command == SMB2_OPLOCK_BREAK) ||
653             server->ignore_signature ||
654             (!server->session_estab))
655                 return 0;
656
657         /*
658          * BB what if signatures are supposed to be on for session but
659          * server does not send one? BB
660          */
661
662         /* Do not need to verify session setups with signature "BSRSPYL " */
663         if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
664                 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
665                          shdr->Command);
666
667         /*
668          * Save off the origiginal signature so we can modify the smb and check
669          * our calculated signature against what the server sent.
670          */
671         memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
672
673         memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
674
675         rc = server->ops->calc_signature(rqst, server, true);
676
677         if (rc)
678                 return rc;
679
680         if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE)) {
681                 cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx\n",
682                         shdr->Command, shdr->MessageId);
683                 return -EACCES;
684         } else
685                 return 0;
686 }
687
688 /*
689  * Set message id for the request. Should be called after wait_for_free_request
690  * and when srv_mutex is held.
691  */
692 static inline void
693 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
694                       struct smb2_hdr *shdr)
695 {
696         unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
697
698         shdr->MessageId = get_next_mid64(server);
699         /* skip message numbers according to CreditCharge field */
700         for (i = 1; i < num; i++)
701                 get_next_mid(server);
702 }
703
704 static struct mid_q_entry *
705 smb2_mid_entry_alloc(const struct smb2_hdr *shdr,
706                      struct TCP_Server_Info *server)
707 {
708         struct mid_q_entry *temp;
709         unsigned int credits = le16_to_cpu(shdr->CreditCharge);
710
711         if (server == NULL) {
712                 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
713                 return NULL;
714         }
715
716         temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
717         memset(temp, 0, sizeof(struct mid_q_entry));
718         kref_init(&temp->refcount);
719         temp->mid = le64_to_cpu(shdr->MessageId);
720         temp->credits = credits > 0 ? credits : 1;
721         temp->pid = current->pid;
722         temp->command = shdr->Command; /* Always LE */
723         temp->when_alloc = jiffies;
724         temp->server = server;
725
726         /*
727          * The default is for the mid to be synchronous, so the
728          * default callback just wakes up the current task.
729          */
730         get_task_struct(current);
731         temp->creator = current;
732         temp->callback = cifs_wake_up_task;
733         temp->callback_data = current;
734
735         atomic_inc(&mid_count);
736         temp->mid_state = MID_REQUEST_ALLOCATED;
737         trace_smb3_cmd_enter(le32_to_cpu(shdr->Id.SyncId.TreeId),
738                              le64_to_cpu(shdr->SessionId),
739                              le16_to_cpu(shdr->Command), temp->mid);
740         return temp;
741 }
742
743 static int
744 smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
745                    struct smb2_hdr *shdr, struct mid_q_entry **mid)
746 {
747         spin_lock(&server->srv_lock);
748         if (server->tcpStatus == CifsExiting) {
749                 spin_unlock(&server->srv_lock);
750                 return -ENOENT;
751         }
752
753         if (server->tcpStatus == CifsNeedReconnect) {
754                 spin_unlock(&server->srv_lock);
755                 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
756                 return -EAGAIN;
757         }
758
759         if (server->tcpStatus == CifsNeedNegotiate &&
760            shdr->Command != SMB2_NEGOTIATE) {
761                 spin_unlock(&server->srv_lock);
762                 return -EAGAIN;
763         }
764         spin_unlock(&server->srv_lock);
765
766         spin_lock(&ses->ses_lock);
767         if (ses->ses_status == SES_NEW) {
768                 if ((shdr->Command != SMB2_SESSION_SETUP) &&
769                     (shdr->Command != SMB2_NEGOTIATE)) {
770                         spin_unlock(&ses->ses_lock);
771                         return -EAGAIN;
772                 }
773                 /* else ok - we are setting up session */
774         }
775
776         if (ses->ses_status == SES_EXITING) {
777                 if (shdr->Command != SMB2_LOGOFF) {
778                         spin_unlock(&ses->ses_lock);
779                         return -EAGAIN;
780                 }
781                 /* else ok - we are shutting down the session */
782         }
783         spin_unlock(&ses->ses_lock);
784
785         *mid = smb2_mid_entry_alloc(shdr, server);
786         if (*mid == NULL)
787                 return -ENOMEM;
788         spin_lock(&server->mid_lock);
789         list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
790         spin_unlock(&server->mid_lock);
791
792         return 0;
793 }
794
795 int
796 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
797                    bool log_error)
798 {
799         unsigned int len = mid->resp_buf_size;
800         struct kvec iov[1];
801         struct smb_rqst rqst = { .rq_iov = iov,
802                                  .rq_nvec = 1 };
803
804         iov[0].iov_base = (char *)mid->resp_buf;
805         iov[0].iov_len = len;
806
807         dump_smb(mid->resp_buf, min_t(u32, 80, len));
808         /* convert the length into a more usable form */
809         if (len > 24 && server->sign && !mid->decrypted) {
810                 int rc;
811
812                 rc = smb2_verify_signature(&rqst, server);
813                 if (rc)
814                         cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
815                                  rc);
816         }
817
818         return map_smb2_to_linux_error(mid->resp_buf, log_error);
819 }
820
821 struct mid_q_entry *
822 smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
823                    struct smb_rqst *rqst)
824 {
825         int rc;
826         struct smb2_hdr *shdr =
827                         (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
828         struct mid_q_entry *mid;
829
830         smb2_seq_num_into_buf(server, shdr);
831
832         rc = smb2_get_mid_entry(ses, server, shdr, &mid);
833         if (rc) {
834                 revert_current_mid_from_hdr(server, shdr);
835                 return ERR_PTR(rc);
836         }
837
838         rc = smb2_sign_rqst(rqst, server);
839         if (rc) {
840                 revert_current_mid_from_hdr(server, shdr);
841                 delete_mid(mid);
842                 return ERR_PTR(rc);
843         }
844
845         return mid;
846 }
847
848 struct mid_q_entry *
849 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
850 {
851         int rc;
852         struct smb2_hdr *shdr =
853                         (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
854         struct mid_q_entry *mid;
855
856         spin_lock(&server->srv_lock);
857         if (server->tcpStatus == CifsNeedNegotiate &&
858            shdr->Command != SMB2_NEGOTIATE) {
859                 spin_unlock(&server->srv_lock);
860                 return ERR_PTR(-EAGAIN);
861         }
862         spin_unlock(&server->srv_lock);
863
864         smb2_seq_num_into_buf(server, shdr);
865
866         mid = smb2_mid_entry_alloc(shdr, server);
867         if (mid == NULL) {
868                 revert_current_mid_from_hdr(server, shdr);
869                 return ERR_PTR(-ENOMEM);
870         }
871
872         rc = smb2_sign_rqst(rqst, server);
873         if (rc) {
874                 revert_current_mid_from_hdr(server, shdr);
875                 release_mid(mid);
876                 return ERR_PTR(rc);
877         }
878
879         return mid;
880 }
881
882 int
883 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
884 {
885         struct crypto_aead *tfm;
886
887         if (!server->secmech.enc) {
888                 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
889                     (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
890                         tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
891                 else
892                         tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
893                 if (IS_ERR(tfm)) {
894                         cifs_server_dbg(VFS, "%s: Failed alloc encrypt aead\n",
895                                  __func__);
896                         return PTR_ERR(tfm);
897                 }
898                 server->secmech.enc = tfm;
899         }
900
901         if (!server->secmech.dec) {
902                 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
903                     (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
904                         tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
905                 else
906                         tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
907                 if (IS_ERR(tfm)) {
908                         crypto_free_aead(server->secmech.enc);
909                         server->secmech.enc = NULL;
910                         cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
911                                  __func__);
912                         return PTR_ERR(tfm);
913                 }
914                 server->secmech.dec = tfm;
915         }
916
917         return 0;
918 }