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