blob: bc9a7b63464347ad4036f41a60fc2a2fdc1bfe6e [file] [log] [blame]
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001/*
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>
Jeff Laytonfb308a62012-09-18 16:20:35 -070033#include <linux/highmem.h>
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040034#include "smb2pdu.h"
35#include "cifsglob.h"
36#include "cifsproto.h"
37#include "smb2proto.h"
38#include "cifs_debug.h"
39#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070040#include "smb2glob.h"
41
Steve French95dc8dd2013-07-04 10:35:21 -050042static int
43smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
44{
Jeff Laytonba482022013-07-31 13:48:00 -040045 int rc;
Steve French95dc8dd2013-07-04 10:35:21 -050046 unsigned int size;
47
48 if (server->secmech.sdeschmacsha256 != NULL)
49 return 0; /* already allocated */
50
51 server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0);
52 if (IS_ERR(server->secmech.hmacsha256)) {
53 cifs_dbg(VFS, "could not allocate crypto hmacsha256\n");
Jeff Laytonba482022013-07-31 13:48:00 -040054 rc = PTR_ERR(server->secmech.hmacsha256);
55 server->secmech.hmacsha256 = NULL;
56 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050057 }
58
59 size = sizeof(struct shash_desc) +
60 crypto_shash_descsize(server->secmech.hmacsha256);
61 server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL);
62 if (!server->secmech.sdeschmacsha256) {
63 crypto_free_shash(server->secmech.hmacsha256);
64 server->secmech.hmacsha256 = NULL;
65 return -ENOMEM;
66 }
67 server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256;
68 server->secmech.sdeschmacsha256->shash.flags = 0x0;
69
70 return 0;
71}
72
73static int
74smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
75{
76 unsigned int size;
77 int rc;
78
79 if (server->secmech.sdesccmacaes != NULL)
80 return 0; /* already allocated */
81
82 rc = smb2_crypto_shash_allocate(server);
83 if (rc)
84 return rc;
85
86 server->secmech.cmacaes = crypto_alloc_shash("cmac(aes)", 0, 0);
87 if (IS_ERR(server->secmech.cmacaes)) {
88 cifs_dbg(VFS, "could not allocate crypto cmac-aes");
89 kfree(server->secmech.sdeschmacsha256);
90 server->secmech.sdeschmacsha256 = NULL;
91 crypto_free_shash(server->secmech.hmacsha256);
92 server->secmech.hmacsha256 = NULL;
Jeff Laytonba482022013-07-31 13:48:00 -040093 rc = PTR_ERR(server->secmech.cmacaes);
94 server->secmech.cmacaes = NULL;
95 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050096 }
97
98 size = sizeof(struct shash_desc) +
99 crypto_shash_descsize(server->secmech.cmacaes);
100 server->secmech.sdesccmacaes = kmalloc(size, GFP_KERNEL);
101 if (!server->secmech.sdesccmacaes) {
102 cifs_dbg(VFS, "%s: Can't alloc cmacaes\n", __func__);
103 kfree(server->secmech.sdeschmacsha256);
104 server->secmech.sdeschmacsha256 = NULL;
105 crypto_free_shash(server->secmech.hmacsha256);
106 crypto_free_shash(server->secmech.cmacaes);
107 server->secmech.hmacsha256 = NULL;
108 server->secmech.cmacaes = NULL;
109 return -ENOMEM;
110 }
111 server->secmech.sdesccmacaes->shash.tfm = server->secmech.cmacaes;
112 server->secmech.sdesccmacaes->shash.flags = 0x0;
113
114 return 0;
115}
116
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500117static struct cifs_ses *
118smb2_find_smb_ses(struct smb2_hdr *smb2hdr, struct TCP_Server_Info *server)
119{
120 struct cifs_ses *ses;
121
122 spin_lock(&cifs_tcp_ses_lock);
123 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
124 if (ses->Suid != smb2hdr->SessionId)
125 continue;
126 spin_unlock(&cifs_tcp_ses_lock);
127 return ses;
128 }
129 spin_unlock(&cifs_tcp_ses_lock);
130
131 return NULL;
132}
133
Steve French95dc8dd2013-07-04 10:35:21 -0500134
Steve French38107d42012-12-08 22:08:06 -0600135int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700136smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700137{
Al Viro16c568e2015-11-12 22:46:49 -0500138 int rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700139 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
140 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700141 struct kvec *iov = rqst->rq_iov;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700142 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500143 struct cifs_ses *ses;
144
145 ses = smb2_find_smb_ses(smb2_pdu, server);
146 if (!ses) {
147 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
148 return 0;
149 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700150
151 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
152 memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
153
Steve French95dc8dd2013-07-04 10:35:21 -0500154 rc = smb2_crypto_shash_allocate(server);
155 if (rc) {
156 cifs_dbg(VFS, "%s: shah256 alloc failed\n", __func__);
157 return rc;
158 }
159
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700160 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500161 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700162 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500163 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700164 return rc;
165 }
166
167 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
168 if (rc) {
Steve French95dc8dd2013-07-04 10:35:21 -0500169 cifs_dbg(VFS, "%s: Could not init sha256", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700170 return rc;
171 }
172
Al Viro16c568e2015-11-12 22:46:49 -0500173 rc = __cifs_calc_signature(rqst, server, sigptr,
174 &server->secmech.sdeschmacsha256->shash);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700175
Al Viro16c568e2015-11-12 22:46:49 -0500176 if (!rc)
177 memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700178
179 return rc;
180}
181
Steve French373512e2015-12-18 13:05:30 -0600182static int generate_key(struct cifs_ses *ses, struct kvec label,
183 struct kvec context, __u8 *key, unsigned int key_size)
Steve French429b46f2013-06-26 23:45:05 -0500184{
185 unsigned char zero = 0x0;
186 __u8 i[4] = {0, 0, 0, 1};
187 __u8 L[4] = {0, 0, 0, 128};
188 int rc = 0;
189 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
190 unsigned char *hashptr = prfhash;
191
192 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600193 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500194
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500195 rc = smb3_crypto_shash_allocate(ses->server);
Steve French95dc8dd2013-07-04 10:35:21 -0500196 if (rc) {
197 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
198 goto smb3signkey_ret;
199 }
200
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500201 rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
202 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500203 if (rc) {
204 cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
205 goto smb3signkey_ret;
206 }
207
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500208 rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500209 if (rc) {
210 cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
211 goto smb3signkey_ret;
212 }
213
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500214 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500215 i, 4);
216 if (rc) {
217 cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
218 goto smb3signkey_ret;
219 }
220
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500221 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600222 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500223 if (rc) {
224 cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
225 goto smb3signkey_ret;
226 }
227
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500228 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500229 &zero, 1);
230 if (rc) {
231 cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
232 goto smb3signkey_ret;
233 }
234
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500235 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600236 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500237 if (rc) {
238 cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
239 goto smb3signkey_ret;
240 }
241
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500242 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500243 L, 4);
244 if (rc) {
245 cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
246 goto smb3signkey_ret;
247 }
248
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500249 rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500250 hashptr);
251 if (rc) {
252 cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
253 goto smb3signkey_ret;
254 }
255
Steve French373512e2015-12-18 13:05:30 -0600256 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500257
258smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500259 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500260}
261
Steve French373512e2015-12-18 13:05:30 -0600262struct derivation {
263 struct kvec label;
264 struct kvec context;
265};
266
267struct derivation_triplet {
268 struct derivation signing;
269 struct derivation encryption;
270 struct derivation decryption;
271};
272
273static int
274generate_smb3signingkey(struct cifs_ses *ses,
275 const struct derivation_triplet *ptriplet)
276{
277 int rc;
278
279 rc = generate_key(ses, ptriplet->signing.label,
280 ptriplet->signing.context, ses->smb3signingkey,
281 SMB3_SIGN_KEY_SIZE);
282 if (rc)
283 return rc;
284
285 rc = generate_key(ses, ptriplet->encryption.label,
286 ptriplet->encryption.context, ses->smb3encryptionkey,
287 SMB3_SIGN_KEY_SIZE);
288 if (rc)
289 return rc;
290
291 return generate_key(ses, ptriplet->decryption.label,
292 ptriplet->decryption.context,
293 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
294}
295
296int
297generate_smb30signingkey(struct cifs_ses *ses)
298
299{
300 struct derivation_triplet triplet;
301 struct derivation *d;
302
303 d = &triplet.signing;
304 d->label.iov_base = "SMB2AESCMAC";
305 d->label.iov_len = 12;
306 d->context.iov_base = "SmbSign";
307 d->context.iov_len = 8;
308
309 d = &triplet.encryption;
310 d->label.iov_base = "SMB2AESCCM";
311 d->label.iov_len = 11;
312 d->context.iov_base = "ServerIn ";
313 d->context.iov_len = 10;
314
315 d = &triplet.decryption;
316 d->label.iov_base = "SMB2AESCCM";
317 d->label.iov_len = 11;
318 d->context.iov_base = "ServerOut";
319 d->context.iov_len = 10;
320
321 return generate_smb3signingkey(ses, &triplet);
322}
323
324int
325generate_smb311signingkey(struct cifs_ses *ses)
326
327{
328 struct derivation_triplet triplet;
329 struct derivation *d;
330
331 d = &triplet.signing;
332 d->label.iov_base = "SMB2AESCMAC";
333 d->label.iov_len = 12;
334 d->context.iov_base = "SmbSign";
335 d->context.iov_len = 8;
336
337 d = &triplet.encryption;
338 d->label.iov_base = "SMB2AESCCM";
339 d->label.iov_len = 11;
340 d->context.iov_base = "ServerIn ";
341 d->context.iov_len = 10;
342
343 d = &triplet.decryption;
344 d->label.iov_base = "SMB2AESCCM";
345 d->label.iov_len = 11;
346 d->context.iov_base = "ServerOut";
347 d->context.iov_len = 10;
348
349 return generate_smb3signingkey(ses, &triplet);
350}
351
Steve French429b46f2013-06-26 23:45:05 -0500352int
Steve French38107d42012-12-08 22:08:06 -0600353smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
354{
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500355 int rc = 0;
Steve French429b46f2013-06-26 23:45:05 -0500356 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
357 unsigned char *sigptr = smb3_signature;
358 struct kvec *iov = rqst->rq_iov;
Steve French429b46f2013-06-26 23:45:05 -0500359 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500360 struct cifs_ses *ses;
361
362 ses = smb2_find_smb_ses(smb2_pdu, server);
363 if (!ses) {
364 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
365 return 0;
366 }
Steve French429b46f2013-06-26 23:45:05 -0500367
368 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
369 memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
370
371 rc = crypto_shash_setkey(server->secmech.cmacaes,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500372 ses->smb3signingkey, SMB2_CMACAES_SIZE);
373
Steve French429b46f2013-06-26 23:45:05 -0500374 if (rc) {
375 cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
376 return rc;
377 }
378
Steve French95dc8dd2013-07-04 10:35:21 -0500379 /*
380 * we already allocate sdesccmacaes when we init smb3 signing key,
381 * so unlike smb2 case we do not have to check here if secmech are
382 * initialized
383 */
Steve French429b46f2013-06-26 23:45:05 -0500384 rc = crypto_shash_init(&server->secmech.sdesccmacaes->shash);
385 if (rc) {
386 cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
387 return rc;
388 }
Al Viro16c568e2015-11-12 22:46:49 -0500389
390 rc = __cifs_calc_signature(rqst, server, sigptr,
391 &server->secmech.sdesccmacaes->shash);
Steve French429b46f2013-06-26 23:45:05 -0500392
Al Viro16c568e2015-11-12 22:46:49 -0500393 if (!rc)
394 memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500395
396 return rc;
Steve French38107d42012-12-08 22:08:06 -0600397}
398
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700399/* must be called with server->srv_mutex held */
400static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700401smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700402{
403 int rc = 0;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700404 struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700405
406 if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
407 server->tcpStatus == CifsNeedNegotiate)
408 return rc;
409
410 if (!server->session_estab) {
411 strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
412 return rc;
413 }
414
Steve French38107d42012-12-08 22:08:06 -0600415 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700416
417 return rc;
418}
419
420int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700421smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700422{
423 unsigned int rc;
424 char server_response_sig[16];
Jeff Layton0b688cf2012-09-18 16:20:34 -0700425 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700426
427 if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500428 (smb2_pdu->Command == SMB2_SESSION_SETUP) ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700429 (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
430 (!server->session_estab))
431 return 0;
432
433 /*
434 * BB what if signatures are supposed to be on for session but
435 * server does not send one? BB
436 */
437
438 /* Do not need to verify session setups with signature "BSRSPYL " */
439 if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500440 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
441 smb2_pdu->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700442
443 /*
444 * Save off the origiginal signature so we can modify the smb and check
445 * our calculated signature against what the server sent.
446 */
447 memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
448
449 memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
450
451 mutex_lock(&server->srv_mutex);
Steve French38107d42012-12-08 22:08:06 -0600452 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700453 mutex_unlock(&server->srv_mutex);
454
455 if (rc)
456 return rc;
457
458 if (memcmp(server_response_sig, smb2_pdu->Signature,
459 SMB2_SIGNATURE_SIZE))
460 return -EACCES;
461 else
462 return 0;
463}
464
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400465/*
466 * Set message id for the request. Should be called after wait_for_free_request
467 * and when srv_mutex is held.
468 */
469static inline void
470smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
471{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400472 unsigned int i, num = le16_to_cpu(hdr->CreditCharge);
473
Tim Gardner3d378d32013-11-02 12:50:34 -0500474 hdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400475 /* skip message numbers according to CreditCharge field */
476 for (i = 1; i < num; i++)
477 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400478}
479
480static struct mid_q_entry *
481smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
482 struct TCP_Server_Info *server)
483{
484 struct mid_q_entry *temp;
485
486 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500487 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400488 return NULL;
489 }
490
491 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
492 if (temp == NULL)
493 return temp;
494 else {
495 memset(temp, 0, sizeof(struct mid_q_entry));
Sachin Prabhu9235d092014-12-09 17:37:00 +0000496 temp->mid = le64_to_cpu(smb_buffer->MessageId);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400497 temp->pid = current->pid;
498 temp->command = smb_buffer->Command; /* Always LE */
499 temp->when_alloc = jiffies;
500 temp->server = server;
501
502 /*
503 * The default is for the mid to be synchronous, so the
504 * default callback just wakes up the current task.
505 */
506 temp->callback = cifs_wake_up_task;
507 temp->callback_data = current;
508 }
509
510 atomic_inc(&midCount);
511 temp->mid_state = MID_REQUEST_ALLOCATED;
512 return temp;
513}
514
515static int
516smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
517 struct mid_q_entry **mid)
518{
519 if (ses->server->tcpStatus == CifsExiting)
520 return -ENOENT;
521
522 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500523 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400524 return -EAGAIN;
525 }
526
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500527 if (ses->status == CifsNew) {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400528 if ((buf->Command != SMB2_SESSION_SETUP) &&
529 (buf->Command != SMB2_NEGOTIATE))
530 return -EAGAIN;
531 /* else ok - we are setting up session */
532 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500533
534 if (ses->status == CifsExiting) {
535 if (buf->Command != SMB2_LOGOFF)
536 return -EAGAIN;
537 /* else ok - we are shutting down the session */
538 }
539
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400540 *mid = smb2_mid_entry_alloc(buf, ses->server);
541 if (*mid == NULL)
542 return -ENOMEM;
543 spin_lock(&GlobalMid_Lock);
544 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
545 spin_unlock(&GlobalMid_Lock);
546 return 0;
547}
548
549int
550smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
551 bool log_error)
552{
553 unsigned int len = get_rfc1002_length(mid->resp_buf);
Jeff Layton0b688cf2012-09-18 16:20:34 -0700554 struct kvec iov;
555 struct smb_rqst rqst = { .rq_iov = &iov,
556 .rq_nvec = 1 };
557
558 iov.iov_base = (char *)mid->resp_buf;
559 iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400560
561 dump_smb(mid->resp_buf, min_t(u32, 80, len));
562 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400563 if (len > 24 && server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700564 int rc;
565
Jeff Layton0b688cf2012-09-18 16:20:34 -0700566 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700567 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500568 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
569 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700570 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400571
572 return map_smb2_to_linux_error(mid->resp_buf, log_error);
573}
574
Jeff Laytonfec344e2012-09-18 16:20:35 -0700575struct mid_q_entry *
576smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400577{
578 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700579 struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400580 struct mid_q_entry *mid;
581
582 smb2_seq_num_into_buf(ses->server, hdr);
583
584 rc = smb2_get_mid_entry(ses, hdr, &mid);
585 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700586 return ERR_PTR(rc);
587 rc = smb2_sign_rqst(rqst, ses->server);
588 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700589 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700590 return ERR_PTR(rc);
591 }
592 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400593}
594
Jeff Laytonfec344e2012-09-18 16:20:35 -0700595struct mid_q_entry *
596smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400597{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700598 int rc;
599 struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400600 struct mid_q_entry *mid;
601
602 smb2_seq_num_into_buf(server, hdr);
603
604 mid = smb2_mid_entry_alloc(hdr, server);
605 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700606 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400607
Jeff Laytonfec344e2012-09-18 16:20:35 -0700608 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400609 if (rc) {
610 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700611 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700612 }
613
Jeff Laytonfec344e2012-09-18 16:20:35 -0700614 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400615}