]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/cifs/smb2pdu.c
Merge branch 'akpm' (patches from Andrew)
[thirdparty/kernel/linux.git] / fs / cifs / smb2pdu.c
CommitLineData
ec2e4523
PS
1/*
2 * fs/cifs/smb2pdu.c
3 *
2b80d049 4 * Copyright (C) International Business Machines Corp., 2009, 2013
ec2e4523
PS
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
09a4707e 34#include <linux/task_io_accounting_ops.h>
ec2e4523 35#include <linux/uaccess.h>
c6e970a0 36#include <linux/uuid.h>
33319141 37#include <linux/pagemap.h>
ec2e4523
PS
38#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
09a4707e 48#include "smb2glob.h"
d324f08d 49#include "cifspdu.h"
ceb1b0b9 50#include "cifs_spnego.h"
db223a59 51#include "smbdirect.h"
ec2e4523
PS
52
53/*
54 * The following table defines the expected "StructureSize" of SMB2 requests
55 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 *
57 * Note that commands are defined in smb2pdu.h in le16 but the array below is
58 * indexed by command in host byte order.
59 */
60static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
61 /* SMB2_NEGOTIATE */ 36,
62 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_LOGOFF */ 4,
64 /* SMB2_TREE_CONNECT */ 9,
65 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_CREATE */ 57,
67 /* SMB2_CLOSE */ 24,
68 /* SMB2_FLUSH */ 24,
69 /* SMB2_READ */ 49,
70 /* SMB2_WRITE */ 49,
71 /* SMB2_LOCK */ 48,
72 /* SMB2_IOCTL */ 57,
73 /* SMB2_CANCEL */ 4,
74 /* SMB2_ECHO */ 4,
75 /* SMB2_QUERY_DIRECTORY */ 33,
76 /* SMB2_CHANGE_NOTIFY */ 32,
77 /* SMB2_QUERY_INFO */ 41,
78 /* SMB2_SET_INFO */ 33,
79 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
80};
81
7fb8986e
PS
82static int encryption_required(const struct cifs_tcon *tcon)
83{
ae6f8dd4
PS
84 if (!tcon)
85 return 0;
7fb8986e
PS
86 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
87 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
88 return 1;
ae6f8dd4
PS
89 if (tcon->seal &&
90 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
91 return 1;
7fb8986e
PS
92 return 0;
93}
ec2e4523
PS
94
95static void
cb200bd6 96smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
ec2e4523
PS
97 const struct cifs_tcon *tcon)
98{
31473fc4
PS
99 shdr->ProtocolId = SMB2_PROTO_NUMBER;
100 shdr->StructureSize = cpu_to_le16(64);
101 shdr->Command = smb2_cmd;
7d414f39
RL
102 if (tcon && tcon->ses && tcon->ses->server) {
103 struct TCP_Server_Info *server = tcon->ses->server;
104
105 spin_lock(&server->req_lock);
106 /* Request up to 2 credits but don't go over the limit. */
141891f4 107 if (server->credits >= server->max_credits)
31473fc4 108 shdr->CreditRequest = cpu_to_le16(0);
7d414f39 109 else
31473fc4 110 shdr->CreditRequest = cpu_to_le16(
141891f4 111 min_t(int, server->max_credits -
7d414f39
RL
112 server->credits, 2));
113 spin_unlock(&server->req_lock);
114 } else {
31473fc4 115 shdr->CreditRequest = cpu_to_le16(2);
7d414f39 116 }
31473fc4 117 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
ec2e4523
PS
118
119 if (!tcon)
120 goto out;
121
2b80d049
SF
122 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
123 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
1dc92c45 124 if ((tcon->ses) && (tcon->ses->server) &&
84ceeb96 125 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
31473fc4 126 shdr->CreditCharge = cpu_to_le16(1);
2b80d049
SF
127 /* else CreditCharge MBZ */
128
31473fc4 129 shdr->TreeId = tcon->tid;
ec2e4523
PS
130 /* Uid is not converted */
131 if (tcon->ses)
31473fc4 132 shdr->SessionId = tcon->ses->Suid;
f87ab88b
SF
133
134 /*
135 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
136 * to pass the path on the Open SMB prefixed by \\server\share.
137 * Not sure when we would need to do the augmented path (if ever) and
138 * setting this flag breaks the SMB2 open operation since it is
139 * illegal to send an empty path name (without \\server\share prefix)
140 * when the DFS flag is set in the SMB open header. We could
141 * consider setting the flag on all operations other than open
142 * but it is safer to net set it for now.
143 */
144/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
31473fc4 145 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
f87ab88b 146
7fb8986e
PS
147 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
148 !encryption_required(tcon))
31473fc4 149 shdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523 150out:
ec2e4523
PS
151 return;
152}
153
154static int
155smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
156{
157 int rc = 0;
aa24d1e9
PS
158 struct nls_table *nls_codepage;
159 struct cifs_ses *ses;
160 struct TCP_Server_Info *server;
161
162 /*
163 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
164 * check for tcp and smb session status done differently
165 * for those three - in the calling routine.
166 */
167 if (tcon == NULL)
168 return rc;
169
170 if (smb2_command == SMB2_TREE_CONNECT)
171 return rc;
172
173 if (tcon->tidStatus == CifsExiting) {
174 /*
175 * only tree disconnect, open, and write,
176 * (and ulogoff which does not have tcon)
177 * are allowed as we start force umount.
178 */
179 if ((smb2_command != SMB2_WRITE) &&
180 (smb2_command != SMB2_CREATE) &&
181 (smb2_command != SMB2_TREE_DISCONNECT)) {
f96637be
JP
182 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
183 smb2_command);
aa24d1e9
PS
184 return -ENODEV;
185 }
186 }
187 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
188 (!tcon->ses->server))
189 return -EIO;
190
191 ses = tcon->ses;
192 server = ses->server;
193
194 /*
195 * Give demultiplex thread up to 10 seconds to reconnect, should be
196 * greater than cifs socket timeout which is 7 seconds
197 */
198 while (server->tcpStatus == CifsNeedReconnect) {
199 /*
200 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
201 * here since they are implicitly done when session drops.
202 */
203 switch (smb2_command) {
204 /*
205 * BB Should we keep oplock break and add flush to exceptions?
206 */
207 case SMB2_TREE_DISCONNECT:
208 case SMB2_CANCEL:
209 case SMB2_CLOSE:
210 case SMB2_OPLOCK_BREAK:
211 return -EAGAIN;
212 }
213
214 wait_event_interruptible_timeout(server->response_q,
215 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216
217 /* are we still trying to reconnect? */
218 if (server->tcpStatus != CifsNeedReconnect)
219 break;
220
221 /*
222 * on "soft" mounts we wait once. Hard mounts keep
223 * retrying until process is killed or server comes
224 * back on-line
225 */
226 if (!tcon->retry) {
f96637be 227 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
aa24d1e9
PS
228 return -EHOSTDOWN;
229 }
230 }
231
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
233 return rc;
234
235 nls_codepage = load_nls_default();
236
237 /*
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
240 */
241 mutex_lock(&tcon->ses->session_mutex);
76e75270
SC
242
243 /*
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
247 */
248 if (server->tcpStatus == CifsNeedReconnect) {
249 rc = -EHOSTDOWN;
250 mutex_unlock(&tcon->ses->session_mutex);
251 goto out;
252 }
253
aa24d1e9
PS
254 rc = cifs_negotiate_protocol(0, tcon->ses);
255 if (!rc && tcon->ses->need_reconnect)
256 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
257
258 if (rc || !tcon->need_reconnect) {
259 mutex_unlock(&tcon->ses->session_mutex);
260 goto out;
261 }
262
263 cifs_mark_open_files_invalid(tcon);
96a988ff
PS
264 if (tcon->use_persistent)
265 tcon->need_reopen_files = true;
52ace1ef 266
aa24d1e9
PS
267 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
268 mutex_unlock(&tcon->ses->session_mutex);
52ace1ef 269
f96637be 270 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
aa24d1e9
PS
271 if (rc)
272 goto out;
96a988ff
PS
273
274 if (smb2_command != SMB2_INTERNAL_CMD)
275 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
276
aa24d1e9 277 atomic_inc(&tconInfoReconnectCount);
aa24d1e9
PS
278out:
279 /*
280 * Check if handle based operation so we know whether we can continue
281 * or not without returning to caller to reset file handle.
282 */
283 /*
284 * BB Is flush done by server on drop of tcp session? Should we special
285 * case it and skip above?
286 */
287 switch (smb2_command) {
288 case SMB2_FLUSH:
289 case SMB2_READ:
290 case SMB2_WRITE:
291 case SMB2_LOCK:
292 case SMB2_IOCTL:
293 case SMB2_QUERY_DIRECTORY:
294 case SMB2_CHANGE_NOTIFY:
295 case SMB2_QUERY_INFO:
296 case SMB2_SET_INFO:
4772c795 297 rc = -EAGAIN;
aa24d1e9
PS
298 }
299 unload_nls(nls_codepage);
ec2e4523
PS
300 return rc;
301}
302
cb200bd6
PS
303static void
304fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
305 unsigned int *total_len)
306{
307 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
308 /* lookup word count ie StructureSize from table */
309 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
310
311 /*
312 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
313 * largest operations (Create)
314 */
315 memset(buf, 0, 256);
316
317 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
318 spdu->StructureSize2 = cpu_to_le16(parmsize);
319
320 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
321}
322
ec2e4523
PS
323/*
324 * Allocate and return pointer to an SMB request hdr, and set basic
325 * SMB information in the SMB header. If the return code is zero, this
305428ac 326 * function must have filled in request_buf pointer.
ec2e4523
PS
327 */
328static int
305428ac
RS
329smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
330 void **request_buf, unsigned int *total_len)
ec2e4523 331{
cb200bd6 332 int rc;
ec2e4523
PS
333
334 rc = smb2_reconnect(smb2_command, tcon);
335 if (rc)
336 return rc;
337
338 /* BB eventually switch this to SMB2 specific small buf size */
339 *request_buf = cifs_small_buf_get();
340 if (*request_buf == NULL) {
341 /* BB should we add a retry in here if not a writepage? */
342 return -ENOMEM;
343 }
344
305428ac
RS
345 fill_small_buf(smb2_command, tcon,
346 (struct smb2_sync_hdr *)(*request_buf),
347 total_len);
ec2e4523
PS
348
349 if (tcon != NULL) {
350#ifdef CONFIG_CIFS_STATS2
ec2e4523
PS
351 uint16_t com_code = le16_to_cpu(smb2_command);
352 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
353#endif
354 cifs_stats_inc(&tcon->num_smbs_sent);
355 }
356
357 return rc;
358}
359
ebb3a9d4 360#ifdef CONFIG_CIFS_SMB311
13cacea7
RS
361/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
362#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
ebb3a9d4
SF
363
364
365#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
366#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
367
368static void
369build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
370{
371 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
372 pneg_ctxt->DataLength = cpu_to_le16(38);
373 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
374 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
375 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
376 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
377}
378
379static void
380build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
381{
382 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
383 pneg_ctxt->DataLength = cpu_to_le16(6);
384 pneg_ctxt->CipherCount = cpu_to_le16(2);
385 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
386 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
387}
388
389static void
13cacea7
RS
390assemble_neg_contexts(struct smb2_negotiate_req *req,
391 unsigned int *total_len)
ebb3a9d4 392{
13cacea7 393 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
ebb3a9d4
SF
394
395 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
396 /* Add 2 to size to round to 8 byte boundary */
13cacea7 397
ebb3a9d4
SF
398 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
399 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
400 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
401 req->NegotiateContextCount = cpu_to_le16(2);
13cacea7
RS
402
403 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
404 + sizeof(struct smb2_encryption_neg_context);
ebb3a9d4
SF
405}
406#else
13cacea7
RS
407static void assemble_neg_contexts(struct smb2_negotiate_req *req,
408 unsigned int *total_len)
ebb3a9d4
SF
409{
410 return;
411}
412#endif /* SMB311 */
413
ec2e4523
PS
414/*
415 *
416 * SMB2 Worker functions follow:
417 *
418 * The general structure of the worker functions is:
419 * 1) Call smb2_init (assembles SMB2 header)
420 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
421 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
422 * 4) Decode SMB2 command specific fields in the fixed length area
423 * 5) Decode variable length data area (if any for this SMB2 command type)
424 * 6) Call free smb buffer
425 * 7) return
426 *
427 */
428
429int
430SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
431{
432 struct smb2_negotiate_req *req;
433 struct smb2_negotiate_rsp *rsp;
434 struct kvec iov[1];
da502f7d 435 struct kvec rsp_iov;
ec2e4523
PS
436 int rc = 0;
437 int resp_buftype;
3534b850 438 struct TCP_Server_Info *server = ses->server;
ec2e4523
PS
439 int blob_offset, blob_length;
440 char *security_blob;
441 int flags = CIFS_NEG_OP;
13cacea7 442 unsigned int total_len;
ec2e4523 443
f96637be 444 cifs_dbg(FYI, "Negotiate protocol\n");
ec2e4523 445
3534b850
JL
446 if (!server) {
447 WARN(1, "%s: server is NULL!\n", __func__);
448 return -EIO;
ec2e4523
PS
449 }
450
13cacea7 451 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
ec2e4523
PS
452 if (rc)
453 return rc;
454
13cacea7 455 req->sync_hdr.SessionId = 0;
8bd68c6e
AA
456#ifdef CONFIG_CIFS_SMB311
457 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
458 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
459#endif
ec2e4523 460
9764c02f
SF
461 if (strcmp(ses->server->vals->version_string,
462 SMB3ANY_VERSION_STRING) == 0) {
463 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
464 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
465 req->DialectCount = cpu_to_le16(2);
13cacea7 466 total_len += 4;
9764c02f
SF
467 } else if (strcmp(ses->server->vals->version_string,
468 SMBDEFAULT_VERSION_STRING) == 0) {
469 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
470 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
471 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
472 req->DialectCount = cpu_to_le16(3);
13cacea7 473 total_len += 6;
9764c02f
SF
474 } else {
475 /* otherwise send specific dialect */
476 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
477 req->DialectCount = cpu_to_le16(1);
13cacea7 478 total_len += 2;
9764c02f 479 }
ec2e4523
PS
480
481 /* only one of SMB2 signing flags may be set in SMB2 request */
38d77c50 482 if (ses->sign)
9cd2e62c 483 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
38d77c50 484 else if (global_secflags & CIFSSEC_MAY_SIGN)
9cd2e62c 485 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
38d77c50
JL
486 else
487 req->SecurityMode = 0;
ec2e4523 488
e4aa25e7 489 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
ec2e4523 490
3c5f9be1
SF
491 /* ClientGUID must be zero for SMB2.02 dialect */
492 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
493 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
ebb3a9d4 494 else {
3c5f9be1
SF
495 memcpy(req->ClientGUID, server->client_guid,
496 SMB2_CLIENT_GUID_SIZE);
ebb3a9d4 497 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
13cacea7 498 assemble_neg_contexts(req, &total_len);
ebb3a9d4 499 }
ec2e4523 500 iov[0].iov_base = (char *)req;
13cacea7 501 iov[0].iov_len = total_len;
ec2e4523 502
13cacea7 503 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
504 cifs_small_buf_release(req);
505 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
ec2e4523
PS
506 /*
507 * No tcon so can't do
508 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
509 */
7e682f76
SF
510 if (rc == -EOPNOTSUPP) {
511 cifs_dbg(VFS, "Dialect not supported by server. Consider "
9764c02f 512 "specifying vers=1.0 or vers=2.0 on mount for accessing"
7e682f76
SF
513 " older servers\n");
514 goto neg_exit;
515 } else if (rc != 0)
ec2e4523
PS
516 goto neg_exit;
517
9764c02f
SF
518 if (strcmp(ses->server->vals->version_string,
519 SMB3ANY_VERSION_STRING) == 0) {
520 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
521 cifs_dbg(VFS,
522 "SMB2 dialect returned but not requested\n");
523 return -EIO;
524 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
525 cifs_dbg(VFS,
526 "SMB2.1 dialect returned but not requested\n");
527 return -EIO;
528 }
529 } else if (strcmp(ses->server->vals->version_string,
530 SMBDEFAULT_VERSION_STRING) == 0) {
531 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
532 cifs_dbg(VFS,
533 "SMB2 dialect returned but not requested\n");
534 return -EIO;
535 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
536 /* ops set to 3.0 by default for default so update */
537 ses->server->ops = &smb21_operations;
538 }
590d08d3
SF
539 } else if (le16_to_cpu(rsp->DialectRevision) !=
540 ses->server->vals->protocol_id) {
9764c02f
SF
541 /* if requested single dialect ensure returned dialect matched */
542 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
590d08d3 543 le16_to_cpu(rsp->DialectRevision));
9764c02f
SF
544 return -EIO;
545 }
546
f96637be 547 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
ec2e4523 548
e4aa25e7 549 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
f96637be 550 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
e4aa25e7 551 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
f96637be 552 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
e4aa25e7 553 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
f96637be 554 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
20b6d8b4
SF
555 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
556 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
5f7fbf73
SF
557#ifdef CONFIG_CIFS_SMB311
558 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
559 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
560#endif /* SMB311 */
ec2e4523 561 else {
f799d623 562 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
f96637be 563 le16_to_cpu(rsp->DialectRevision));
ec2e4523
PS
564 rc = -EIO;
565 goto neg_exit;
566 }
567 server->dialect = le16_to_cpu(rsp->DialectRevision);
568
9764c02f
SF
569 /* BB: add check that dialect was valid given dialect(s) we asked for */
570
8bd68c6e
AA
571#ifdef CONFIG_CIFS_SMB311
572 /*
573 * Keep a copy of the hash after negprot. This hash will be
574 * the starting hash value for all sessions made from this
575 * server.
576 */
577 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
578 SMB2_PREAUTH_HASH_SIZE);
579#endif
e598d1d8
JL
580 /* SMB2 only has an extended negflavor */
581 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
2365c4ea
PS
582 /* set it to the maximum buffer size value we can send with 1 credit */
583 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
584 SMB2_MAX_BUFFER_SIZE);
ec2e4523
PS
585 server->max_read = le32_to_cpu(rsp->MaxReadSize);
586 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
ec2e4523 587 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
07108d0e
SF
588 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
589 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
590 server->sec_mode);
ec2e4523 591 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
592 /* Internal types */
593 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523
PS
594
595 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
596 &rsp->hdr);
5d875cc9
SF
597 /*
598 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
599 * for us will be
600 * ses->sectype = RawNTLMSSP;
601 * but for time being this is our only auth choice so doesn't matter.
602 * We just found a server which sets blob length to zero expecting raw.
603 */
67dbea2c 604 if (blob_length == 0) {
5d875cc9 605 cifs_dbg(FYI, "missing security blob on negprot\n");
67dbea2c
PS
606 server->sec_ntlmssp = true;
607 }
3c1bf7e4 608
38d77c50 609 rc = cifs_enable_signing(server, ses->sign);
9ddec561
JL
610 if (rc)
611 goto neg_exit;
ceb1b0b9 612 if (blob_length) {
ebdd207e 613 rc = decode_negTokenInit(security_blob, blob_length, server);
ceb1b0b9
SF
614 if (rc == 1)
615 rc = 0;
616 else if (rc == 0)
617 rc = -EIO;
ec2e4523 618 }
ec2e4523
PS
619neg_exit:
620 free_rsp_buf(resp_buftype, rsp);
621 return rc;
622}
5478f9ba 623
ff1c038a
SF
624int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
625{
626 int rc = 0;
627 struct validate_negotiate_info_req vneg_inbuf;
fe83bebc 628 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
ff1c038a 629 u32 rsplen;
9764c02f 630 u32 inbuflen; /* max of 4 dialects */
ff1c038a
SF
631
632 cifs_dbg(FYI, "validate negotiate\n");
633
8801e902
LL
634#ifdef CONFIG_CIFS_SMB_DIRECT
635 if (tcon->ses->server->rdma)
636 return 0;
637#endif
638
8bd68c6e
AA
639 /* In SMB3.11 preauth integrity supersedes validate negotiate */
640 if (tcon->ses->server->dialect == SMB311_PROT_ID)
641 return 0;
642
ff1c038a
SF
643 /*
644 * validation ioctl must be signed, so no point sending this if we
0603c96f
SF
645 * can not sign it (ie are not known user). Even if signing is not
646 * required (enabled but not negotiated), in those cases we selectively
ff1c038a 647 * sign just this, the first and only signed request on a connection.
0603c96f 648 * Having validation of negotiate info helps reduce attack vectors.
ff1c038a 649 */
0603c96f 650 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
ff1c038a
SF
651 return 0; /* validation requires signing */
652
0603c96f
SF
653 if (tcon->ses->user_name == NULL) {
654 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
655 return 0; /* validation requires signing */
656 }
657
658 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
659 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
660
ff1c038a
SF
661 vneg_inbuf.Capabilities =
662 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
39552ea8
SP
663 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
664 SMB2_CLIENT_GUID_SIZE);
ff1c038a
SF
665
666 if (tcon->ses->sign)
667 vneg_inbuf.SecurityMode =
668 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
669 else if (global_secflags & CIFSSEC_MAY_SIGN)
670 vneg_inbuf.SecurityMode =
671 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
672 else
673 vneg_inbuf.SecurityMode = 0;
674
9764c02f
SF
675
676 if (strcmp(tcon->ses->server->vals->version_string,
677 SMB3ANY_VERSION_STRING) == 0) {
678 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
679 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
680 vneg_inbuf.DialectCount = cpu_to_le16(2);
681 /* structure is big enough for 3 dialects, sending only 2 */
682 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
683 } else if (strcmp(tcon->ses->server->vals->version_string,
684 SMBDEFAULT_VERSION_STRING) == 0) {
685 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
686 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
687 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
688 vneg_inbuf.DialectCount = cpu_to_le16(3);
689 /* structure is big enough for 3 dialects */
690 inbuflen = sizeof(struct validate_negotiate_info_req);
691 } else {
692 /* otherwise specific dialect was requested */
693 vneg_inbuf.Dialects[0] =
694 cpu_to_le16(tcon->ses->server->vals->protocol_id);
695 vneg_inbuf.DialectCount = cpu_to_le16(1);
696 /* structure is big enough for 3 dialects, sending only 1 */
697 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
698 }
ff1c038a
SF
699
700 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
701 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
702 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
703 (char **)&pneg_rsp, &rsplen);
704
705 if (rc != 0) {
706 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
707 return -EIO;
708 }
709
710 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
7db0a6ef
SF
711 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
712 rsplen);
713
714 /* relax check since Mac returns max bufsize allowed on ioctl */
a2d9daad
DD
715 if ((rsplen > CIFSMaxBufSize)
716 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
fe83bebc 717 goto err_rsp_free;
ff1c038a
SF
718 }
719
720 /* check validate negotiate info response matches what we got earlier */
9aca7e45 721 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
ff1c038a
SF
722 goto vneg_out;
723
724 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
725 goto vneg_out;
726
727 /* do not validate server guid because not saved at negprot time yet */
728
729 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
730 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
731 goto vneg_out;
732
733 /* validate negotiate successful */
734 cifs_dbg(FYI, "validate negotiate info successful\n");
fe83bebc 735 kfree(pneg_rsp);
ff1c038a
SF
736 return 0;
737
738vneg_out:
739 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
fe83bebc
DD
740err_rsp_free:
741 kfree(pneg_rsp);
ff1c038a
SF
742 return -EIO;
743}
744
ef65aaed
SP
745enum securityEnum
746smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
747{
748 switch (requested) {
749 case Kerberos:
750 case RawNTLMSSP:
751 return requested;
752 case NTLMv2:
753 return RawNTLMSSP;
754 case Unspecified:
755 if (server->sec_ntlmssp &&
756 (global_secflags & CIFSSEC_MAY_NTLMSSP))
757 return RawNTLMSSP;
758 if ((server->sec_kerberos || server->sec_mskerberos) &&
759 (global_secflags & CIFSSEC_MAY_KRB5))
760 return Kerberos;
761 /* Fallthrough */
762 default:
763 return Unspecified;
764 }
765}
766
3baf1a7b
SP
767struct SMB2_sess_data {
768 unsigned int xid;
769 struct cifs_ses *ses;
770 struct nls_table *nls_cp;
771 void (*func)(struct SMB2_sess_data *);
772 int result;
773 u64 previous_session;
774
775 /* we will send the SMB in three pieces:
776 * a fixed length beginning part, an optional
777 * SPNEGO blob (which can be zero length), and a
778 * last part which will include the strings
779 * and rest of bcc area. This allows us to avoid
780 * a large buffer 17K allocation
781 */
782 int buf0_type;
783 struct kvec iov[2];
784};
785
786static int
787SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
788{
789 int rc;
790 struct cifs_ses *ses = sess_data->ses;
791 struct smb2_sess_setup_req *req;
792 struct TCP_Server_Info *server = ses->server;
88ea5cb7 793 unsigned int total_len;
3baf1a7b 794
88ea5cb7
RS
795 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
796 &total_len);
3baf1a7b
SP
797 if (rc)
798 return rc;
799
31473fc4 800 /* First session, not a reauthenticate */
88ea5cb7 801 req->sync_hdr.SessionId = 0;
3baf1a7b
SP
802
803 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
804 req->PreviousSessionId = sess_data->previous_session;
805
806 req->Flags = 0; /* MBZ */
807 /* to enable echos and oplocks */
88ea5cb7 808 req->sync_hdr.CreditRequest = cpu_to_le16(3);
3baf1a7b
SP
809
810 /* only one of SMB2 signing flags may be set in SMB2 request */
811 if (server->sign)
812 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
813 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
814 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
815 else
816 req->SecurityMode = 0;
817
818 req->Capabilities = 0;
819 req->Channel = 0; /* MBZ */
820
821 sess_data->iov[0].iov_base = (char *)req;
88ea5cb7
RS
822 /* 1 for pad */
823 sess_data->iov[0].iov_len = total_len - 1;
3baf1a7b
SP
824 /*
825 * This variable will be used to clear the buffer
826 * allocated above in case of any error in the calling function.
827 */
828 sess_data->buf0_type = CIFS_SMALL_BUFFER;
829
830 return 0;
831}
832
833static void
834SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
835{
836 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
837 sess_data->buf0_type = CIFS_NO_BUFFER;
838}
839
840static int
841SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
842{
843 int rc;
844 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
da502f7d 845 struct kvec rsp_iov = { NULL, 0 };
3baf1a7b
SP
846
847 /* Testing shows that buffer offset must be at location of Buffer[0] */
848 req->SecurityBufferOffset =
88ea5cb7 849 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
3baf1a7b
SP
850 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
851
3baf1a7b
SP
852 /* BB add code to build os and lm fields */
853
88ea5cb7
RS
854 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
855 sess_data->iov, 2,
856 &sess_data->buf0_type,
857 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
da502f7d
PS
858 cifs_small_buf_release(sess_data->iov[0].iov_base);
859 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
3baf1a7b
SP
860
861 return rc;
862}
863
864static int
865SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
866{
867 int rc = 0;
868 struct cifs_ses *ses = sess_data->ses;
869
870 mutex_lock(&ses->server->srv_mutex);
cabfb368 871 if (ses->server->ops->generate_signingkey) {
3baf1a7b 872 rc = ses->server->ops->generate_signingkey(ses);
3baf1a7b
SP
873 if (rc) {
874 cifs_dbg(FYI,
875 "SMB3 session key generation failed\n");
876 mutex_unlock(&ses->server->srv_mutex);
cabfb368 877 return rc;
3baf1a7b
SP
878 }
879 }
880 if (!ses->server->session_estab) {
881 ses->server->sequence_number = 0x2;
882 ses->server->session_estab = true;
883 }
884 mutex_unlock(&ses->server->srv_mutex);
885
886 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
887 spin_lock(&GlobalMid_Lock);
888 ses->status = CifsGood;
889 ses->need_reconnect = false;
890 spin_unlock(&GlobalMid_Lock);
3baf1a7b
SP
891 return rc;
892}
893
894#ifdef CONFIG_CIFS_UPCALL
895static void
896SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
897{
898 int rc;
899 struct cifs_ses *ses = sess_data->ses;
900 struct cifs_spnego_msg *msg;
901 struct key *spnego_key = NULL;
902 struct smb2_sess_setup_rsp *rsp = NULL;
903
904 rc = SMB2_sess_alloc_buffer(sess_data);
905 if (rc)
906 goto out;
907
908 spnego_key = cifs_get_spnego_key(ses);
909 if (IS_ERR(spnego_key)) {
910 rc = PTR_ERR(spnego_key);
911 spnego_key = NULL;
912 goto out;
913 }
914
915 msg = spnego_key->payload.data[0];
916 /*
917 * check version field to make sure that cifs.upcall is
918 * sending us a response in an expected form
919 */
920 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
921 cifs_dbg(VFS,
922 "bad cifs.upcall version. Expected %d got %d",
923 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
924 rc = -EKEYREJECTED;
925 goto out_put_spnego_key;
926 }
927
928 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
929 GFP_KERNEL);
930 if (!ses->auth_key.response) {
931 cifs_dbg(VFS,
932 "Kerberos can't allocate (%u bytes) memory",
933 msg->sesskey_len);
934 rc = -ENOMEM;
935 goto out_put_spnego_key;
936 }
937 ses->auth_key.len = msg->sesskey_len;
938
939 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
940 sess_data->iov[1].iov_len = msg->secblob_len;
941
942 rc = SMB2_sess_sendreceive(sess_data);
943 if (rc)
944 goto out_put_spnego_key;
945
946 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
31473fc4 947 ses->Suid = rsp->hdr.sync_hdr.SessionId;
3baf1a7b
SP
948
949 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
3baf1a7b
SP
950
951 rc = SMB2_sess_establish_session(sess_data);
952out_put_spnego_key:
953 key_invalidate(spnego_key);
954 key_put(spnego_key);
955out:
956 sess_data->result = rc;
957 sess_data->func = NULL;
958 SMB2_sess_free_buffer(sess_data);
959}
960#else
961static void
962SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
963{
964 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
965 sess_data->result = -EOPNOTSUPP;
966 sess_data->func = NULL;
967}
968#endif
969
166cea4d
SP
970static void
971SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
972
973static void
974SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
5478f9ba 975{
166cea4d
SP
976 int rc;
977 struct cifs_ses *ses = sess_data->ses;
5478f9ba 978 struct smb2_sess_setup_rsp *rsp = NULL;
166cea4d 979 char *ntlmssp_blob = NULL;
5478f9ba 980 bool use_spnego = false; /* else use raw ntlmssp */
166cea4d 981 u16 blob_length = 0;
d4e63bd6 982
5478f9ba
PS
983 /*
984 * If memory allocation is successful, caller of this function
985 * frees it.
986 */
987 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
166cea4d
SP
988 if (!ses->ntlmssp) {
989 rc = -ENOMEM;
990 goto out_err;
991 }
5c234aa5 992 ses->ntlmssp->sesskey_per_smbsess = true;
5478f9ba 993
166cea4d 994 rc = SMB2_sess_alloc_buffer(sess_data);
5478f9ba 995 if (rc)
166cea4d 996 goto out_err;
5478f9ba 997
166cea4d
SP
998 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
999 GFP_KERNEL);
1000 if (ntlmssp_blob == NULL) {
1001 rc = -ENOMEM;
1002 goto out;
1003 }
c2afb814 1004
166cea4d
SP
1005 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1006 if (use_spnego) {
1007 /* BB eventually need to add this */
1008 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1009 rc = -EOPNOTSUPP;
1010 goto out;
1011 } else {
1012 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1013 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1014 }
1015 sess_data->iov[1].iov_base = ntlmssp_blob;
1016 sess_data->iov[1].iov_len = blob_length;
c2afb814 1017
166cea4d
SP
1018 rc = SMB2_sess_sendreceive(sess_data);
1019 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
5478f9ba 1020
166cea4d
SP
1021 /* If true, rc here is expected and not an error */
1022 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
31473fc4 1023 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
166cea4d 1024 rc = 0;
38d77c50 1025
166cea4d
SP
1026 if (rc)
1027 goto out;
5478f9ba 1028
166cea4d
SP
1029 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1030 le16_to_cpu(rsp->SecurityBufferOffset)) {
1031 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1032 le16_to_cpu(rsp->SecurityBufferOffset));
5478f9ba 1033 rc = -EIO;
166cea4d 1034 goto out;
5478f9ba 1035 }
166cea4d
SP
1036 rc = decode_ntlmssp_challenge(rsp->Buffer,
1037 le16_to_cpu(rsp->SecurityBufferLength), ses);
1038 if (rc)
1039 goto out;
5478f9ba 1040
166cea4d 1041 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
5478f9ba 1042
5478f9ba 1043
31473fc4 1044 ses->Suid = rsp->hdr.sync_hdr.SessionId;
166cea4d 1045 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
166cea4d
SP
1046
1047out:
1048 kfree(ntlmssp_blob);
1049 SMB2_sess_free_buffer(sess_data);
1050 if (!rc) {
1051 sess_data->result = 0;
1052 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1053 return;
1054 }
1055out_err:
1056 kfree(ses->ntlmssp);
1057 ses->ntlmssp = NULL;
1058 sess_data->result = rc;
1059 sess_data->func = NULL;
1060}
5478f9ba 1061
166cea4d
SP
1062static void
1063SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1064{
1065 int rc;
1066 struct cifs_ses *ses = sess_data->ses;
1067 struct smb2_sess_setup_req *req;
1068 struct smb2_sess_setup_rsp *rsp = NULL;
1069 unsigned char *ntlmssp_blob = NULL;
1070 bool use_spnego = false; /* else use raw ntlmssp */
1071 u16 blob_length = 0;
5478f9ba 1072
166cea4d
SP
1073 rc = SMB2_sess_alloc_buffer(sess_data);
1074 if (rc)
1075 goto out;
5478f9ba 1076
166cea4d 1077 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
88ea5cb7 1078 req->sync_hdr.SessionId = ses->Suid;
166cea4d
SP
1079
1080 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1081 sess_data->nls_cp);
1082 if (rc) {
1083 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1084 goto out;
5478f9ba
PS
1085 }
1086
166cea4d
SP
1087 if (use_spnego) {
1088 /* BB eventually need to add this */
1089 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1090 rc = -EOPNOTSUPP;
1091 goto out;
1092 }
1093 sess_data->iov[1].iov_base = ntlmssp_blob;
1094 sess_data->iov[1].iov_len = blob_length;
5478f9ba 1095
166cea4d
SP
1096 rc = SMB2_sess_sendreceive(sess_data);
1097 if (rc)
1098 goto out;
1099
1100 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1101
31473fc4 1102 ses->Suid = rsp->hdr.sync_hdr.SessionId;
5478f9ba 1103 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
5478f9ba 1104
166cea4d
SP
1105 rc = SMB2_sess_establish_session(sess_data);
1106out:
1107 kfree(ntlmssp_blob);
1108 SMB2_sess_free_buffer(sess_data);
1109 kfree(ses->ntlmssp);
1110 ses->ntlmssp = NULL;
1111 sess_data->result = rc;
1112 sess_data->func = NULL;
1113}
d4e63bd6 1114
166cea4d
SP
1115static int
1116SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1117{
ef65aaed
SP
1118 int type;
1119
1120 type = smb2_select_sectype(ses->server, ses->sectype);
1121 cifs_dbg(FYI, "sess setup type %d\n", type);
1122 if (type == Unspecified) {
1123 cifs_dbg(VFS,
1124 "Unable to select appropriate authentication method!");
1125 return -EINVAL;
1126 }
d4e63bd6 1127
ef65aaed 1128 switch (type) {
166cea4d
SP
1129 case Kerberos:
1130 sess_data->func = SMB2_auth_kerberos;
1131 break;
1132 case RawNTLMSSP:
1133 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1134 break;
1135 default:
ef65aaed 1136 cifs_dbg(VFS, "secType %d not supported!\n", type);
166cea4d 1137 return -EOPNOTSUPP;
d4e63bd6
SP
1138 }
1139
166cea4d
SP
1140 return 0;
1141}
1142
1143int
1144SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1145 const struct nls_table *nls_cp)
1146{
1147 int rc = 0;
1148 struct TCP_Server_Info *server = ses->server;
1149 struct SMB2_sess_data *sess_data;
1150
1151 cifs_dbg(FYI, "Session Setup\n");
1152
1153 if (!server) {
1154 WARN(1, "%s: server is NULL!\n", __func__);
1155 return -EIO;
d4e63bd6 1156 }
d4e63bd6 1157
166cea4d
SP
1158 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1159 if (!sess_data)
1160 return -ENOMEM;
1161
1162 rc = SMB2_select_sec(ses, sess_data);
1163 if (rc)
1164 goto out;
1165 sess_data->xid = xid;
1166 sess_data->ses = ses;
1167 sess_data->buf0_type = CIFS_NO_BUFFER;
1168 sess_data->nls_cp = (struct nls_table *) nls_cp;
1169
8bd68c6e
AA
1170#ifdef CONFIG_CIFS_SMB311
1171 /*
1172 * Initialize the session hash with the server one.
1173 */
1174 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1175 SMB2_PREAUTH_HASH_SIZE);
1176#endif
1177
166cea4d
SP
1178 while (sess_data->func)
1179 sess_data->func(sess_data);
1180
c721c389
SF
1181 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1182 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
3baf1a7b 1183 rc = sess_data->result;
166cea4d 1184out:
3baf1a7b 1185 kfree(sess_data);
5478f9ba
PS
1186 return rc;
1187}
1188
1189int
1190SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1191{
1192 struct smb2_logoff_req *req; /* response is also trivial struct */
1193 int rc = 0;
1194 struct TCP_Server_Info *server;
7fb8986e 1195 int flags = 0;
45305eda
RS
1196 unsigned int total_len;
1197 struct kvec iov[1];
1198 struct kvec rsp_iov;
1199 int resp_buf_type;
5478f9ba 1200
f96637be 1201 cifs_dbg(FYI, "disconnect session %p\n", ses);
5478f9ba
PS
1202
1203 if (ses && (ses->server))
1204 server = ses->server;
1205 else
1206 return -EIO;
1207
eb4c7df6
SP
1208 /* no need to send SMB logoff if uid already closed due to reconnect */
1209 if (ses->need_reconnect)
1210 goto smb2_session_already_dead;
1211
45305eda 1212 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
5478f9ba
PS
1213 if (rc)
1214 return rc;
1215
1216 /* since no tcon, smb2_init can not do this, so do here */
45305eda 1217 req->sync_hdr.SessionId = ses->Suid;
7fb8986e
PS
1218
1219 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1220 flags |= CIFS_TRANSFORM_REQ;
1221 else if (server->sign)
45305eda
RS
1222 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1223
1224 flags |= CIFS_NO_RESP;
1225
1226 iov[0].iov_base = (char *)req;
1227 iov[0].iov_len = total_len;
5478f9ba 1228
45305eda 1229 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
da502f7d 1230 cifs_small_buf_release(req);
5478f9ba
PS
1231 /*
1232 * No tcon so can't do
1233 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1234 */
eb4c7df6
SP
1235
1236smb2_session_already_dead:
5478f9ba
PS
1237 return rc;
1238}
faaf946a
PS
1239
1240static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1241{
d60622eb 1242 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
1243}
1244
1245#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1246
de9f68df
SF
1247/* These are similar values to what Windows uses */
1248static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1249{
1250 tcon->max_chunks = 256;
1251 tcon->max_bytes_chunk = 1048576;
1252 tcon->max_bytes_copy = 16777216;
1253}
1254
faaf946a
PS
1255int
1256SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1257 struct cifs_tcon *tcon, const struct nls_table *cp)
1258{
1259 struct smb2_tree_connect_req *req;
1260 struct smb2_tree_connect_rsp *rsp = NULL;
1261 struct kvec iov[2];
db3b5474 1262 struct kvec rsp_iov = { NULL, 0 };
faaf946a
PS
1263 int rc = 0;
1264 int resp_buftype;
1265 int unc_path_len;
faaf946a 1266 __le16 *unc_path = NULL;
7fb8986e 1267 int flags = 0;
661bb943 1268 unsigned int total_len;
faaf946a 1269
f96637be 1270 cifs_dbg(FYI, "TCON\n");
faaf946a 1271
68a6afa7 1272 if (!(ses->server) || !tree)
faaf946a
PS
1273 return -EIO;
1274
faaf946a
PS
1275 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1276 if (unc_path == NULL)
1277 return -ENOMEM;
1278
1279 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1280 unc_path_len *= 2;
1281 if (unc_path_len < 2) {
1282 kfree(unc_path);
1283 return -EINVAL;
1284 }
1285
806a28ef 1286 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
b327a717 1287 tcon->tid = 0;
806a28ef 1288
661bb943
RS
1289 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1290 &total_len);
faaf946a
PS
1291 if (rc) {
1292 kfree(unc_path);
1293 return rc;
1294 }
1295
b327a717 1296 if (encryption_required(tcon))
ae6f8dd4 1297 flags |= CIFS_TRANSFORM_REQ;
faaf946a
PS
1298
1299 iov[0].iov_base = (char *)req;
661bb943
RS
1300 /* 1 for pad */
1301 iov[0].iov_len = total_len - 1;
faaf946a
PS
1302
1303 /* Testing shows that buffer offset must be at location of Buffer[0] */
1304 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
661bb943 1305 - 1 /* pad */);
faaf946a
PS
1306 req->PathLength = cpu_to_le16(unc_path_len - 2);
1307 iov[1].iov_base = unc_path;
1308 iov[1].iov_len = unc_path_len;
1309
6188f28b
SF
1310 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1311 if ((ses->server->dialect == SMB311_PROT_ID) &&
1312 !encryption_required(tcon))
1313 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1314
661bb943 1315 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
1316 cifs_small_buf_release(req);
1317 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
faaf946a
PS
1318
1319 if (rc != 0) {
1320 if (tcon) {
1321 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1322 tcon->need_reconnect = true;
1323 }
1324 goto tcon_error_exit;
1325 }
1326
cd123007
CJ
1327 switch (rsp->ShareType) {
1328 case SMB2_SHARE_TYPE_DISK:
f96637be 1329 cifs_dbg(FYI, "connection to disk share\n");
cd123007
CJ
1330 break;
1331 case SMB2_SHARE_TYPE_PIPE:
b327a717 1332 tcon->pipe = true;
f96637be 1333 cifs_dbg(FYI, "connection to pipe share\n");
cd123007
CJ
1334 break;
1335 case SMB2_SHARE_TYPE_PRINT:
b327a717 1336 tcon->print = true;
f96637be 1337 cifs_dbg(FYI, "connection to printer\n");
cd123007
CJ
1338 break;
1339 default:
f96637be 1340 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
faaf946a
PS
1341 rc = -EOPNOTSUPP;
1342 goto tcon_error_exit;
1343 }
1344
1345 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
769ee6a4 1346 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
faaf946a
PS
1347 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1348 tcon->tidStatus = CifsGood;
1349 tcon->need_reconnect = false;
31473fc4 1350 tcon->tid = rsp->hdr.sync_hdr.TreeId;
46b51d08 1351 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
faaf946a
PS
1352
1353 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1354 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
f96637be 1355 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
ae6f8dd4
PS
1356
1357 if (tcon->seal &&
1358 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1359 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1360
de9f68df 1361 init_copy_chunk_defaults(tcon);
ff1c038a
SF
1362 if (tcon->ses->server->ops->validate_negotiate)
1363 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
faaf946a
PS
1364tcon_exit:
1365 free_rsp_buf(resp_buftype, rsp);
1366 kfree(unc_path);
1367 return rc;
1368
1369tcon_error_exit:
db3b5474 1370 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
f96637be 1371 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
faaf946a
PS
1372 }
1373 goto tcon_exit;
1374}
1375
1376int
1377SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1378{
1379 struct smb2_tree_disconnect_req *req; /* response is trivial */
1380 int rc = 0;
faaf946a 1381 struct cifs_ses *ses = tcon->ses;
7fb8986e 1382 int flags = 0;
4eecf4cf
RS
1383 unsigned int total_len;
1384 struct kvec iov[1];
1385 struct kvec rsp_iov;
1386 int resp_buf_type;
faaf946a 1387
f96637be 1388 cifs_dbg(FYI, "Tree Disconnect\n");
faaf946a 1389
68a6afa7 1390 if (!ses || !(ses->server))
faaf946a
PS
1391 return -EIO;
1392
1393 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1394 return 0;
1395
4eecf4cf
RS
1396 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1397 &total_len);
faaf946a
PS
1398 if (rc)
1399 return rc;
1400
7fb8986e
PS
1401 if (encryption_required(tcon))
1402 flags |= CIFS_TRANSFORM_REQ;
1403
4eecf4cf
RS
1404 flags |= CIFS_NO_RESP;
1405
1406 iov[0].iov_base = (char *)req;
1407 iov[0].iov_len = total_len;
1408
1409 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
da502f7d 1410 cifs_small_buf_release(req);
faaf946a
PS
1411 if (rc)
1412 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1413
1414 return rc;
1415}
2503a0db 1416
b8c32dbb 1417
63eb3def
PS
1418static struct create_durable *
1419create_durable_buf(void)
1420{
1421 struct create_durable *buf;
1422
1423 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1424 if (!buf)
1425 return NULL;
1426
1427 buf->ccontext.DataOffset = cpu_to_le16(offsetof
9cbc0b73 1428 (struct create_durable, Data));
63eb3def
PS
1429 buf->ccontext.DataLength = cpu_to_le32(16);
1430 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1431 (struct create_durable, Name));
1432 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1433 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
63eb3def
PS
1434 buf->Name[0] = 'D';
1435 buf->Name[1] = 'H';
1436 buf->Name[2] = 'n';
1437 buf->Name[3] = 'Q';
1438 return buf;
1439}
1440
9cbc0b73
PS
1441static struct create_durable *
1442create_reconnect_durable_buf(struct cifs_fid *fid)
1443{
1444 struct create_durable *buf;
1445
1446 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1447 if (!buf)
1448 return NULL;
1449
1450 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1451 (struct create_durable, Data));
1452 buf->ccontext.DataLength = cpu_to_le32(16);
1453 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1454 (struct create_durable, Name));
1455 buf->ccontext.NameLength = cpu_to_le16(4);
1456 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1457 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
12197a7f 1458 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
9cbc0b73
PS
1459 buf->Name[0] = 'D';
1460 buf->Name[1] = 'H';
1461 buf->Name[2] = 'n';
1462 buf->Name[3] = 'C';
1463 return buf;
1464}
1465
b8c32dbb 1466static __u8
42873b0a
PS
1467parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1468 unsigned int *epoch)
b8c32dbb
PS
1469{
1470 char *data_offset;
b5c7cde3 1471 struct create_context *cc;
deb7deff
JM
1472 unsigned int next;
1473 unsigned int remaining;
fd554396 1474 char *name;
b8c32dbb 1475
93012bf9 1476 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
deb7deff 1477 remaining = le32_to_cpu(rsp->CreateContextsLength);
b5c7cde3 1478 cc = (struct create_context *)data_offset;
deb7deff 1479 while (remaining >= sizeof(struct create_context)) {
b5c7cde3 1480 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
deb7deff
JM
1481 if (le16_to_cpu(cc->NameLength) == 4 &&
1482 strncmp(name, "RqLs", 4) == 0)
1483 return server->ops->parse_lease_buf(cc, epoch);
1484
1485 next = le32_to_cpu(cc->Next);
1486 if (!next)
1487 break;
1488 remaining -= next;
1489 cc = (struct create_context *)((char *)cc + next);
1490 }
b8c32dbb 1491
b5c7cde3 1492 return 0;
b8c32dbb
PS
1493}
1494
d22cbfec 1495static int
a41a28bd
PS
1496add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1497 unsigned int *num_iovec, __u8 *oplock)
d22cbfec
PS
1498{
1499 struct smb2_create_req *req = iov[0].iov_base;
1500 unsigned int num = *num_iovec;
1501
a41a28bd 1502 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
d22cbfec
PS
1503 if (iov[num].iov_base == NULL)
1504 return -ENOMEM;
a41a28bd 1505 iov[num].iov_len = server->vals->create_lease_size;
d22cbfec
PS
1506 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1507 if (!req->CreateContextsOffset)
1508 req->CreateContextsOffset = cpu_to_le32(
4f33bc35 1509 sizeof(struct smb2_create_req) +
d22cbfec 1510 iov[num - 1].iov_len);
a41a28bd
PS
1511 le32_add_cpu(&req->CreateContextsLength,
1512 server->vals->create_lease_size);
d22cbfec
PS
1513 *num_iovec = num + 1;
1514 return 0;
1515}
1516
b56eae4d
SF
1517static struct create_durable_v2 *
1518create_durable_v2_buf(struct cifs_fid *pfid)
1519{
1520 struct create_durable_v2 *buf;
1521
1522 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1523 if (!buf)
1524 return NULL;
1525
1526 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1527 (struct create_durable_v2, dcontext));
1528 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1529 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1530 (struct create_durable_v2, Name));
1531 buf->ccontext.NameLength = cpu_to_le16(4);
1532
1533 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1534 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
fa70b87c 1535 generate_random_uuid(buf->dcontext.CreateGuid);
b56eae4d
SF
1536 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1537
1538 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1539 buf->Name[0] = 'D';
1540 buf->Name[1] = 'H';
1541 buf->Name[2] = '2';
1542 buf->Name[3] = 'Q';
1543 return buf;
1544}
1545
1546static struct create_durable_handle_reconnect_v2 *
1547create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1548{
1549 struct create_durable_handle_reconnect_v2 *buf;
1550
1551 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1552 GFP_KERNEL);
1553 if (!buf)
1554 return NULL;
1555
1556 buf->ccontext.DataOffset =
1557 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1558 dcontext));
1559 buf->ccontext.DataLength =
1560 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1561 buf->ccontext.NameOffset =
1562 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1563 Name));
1564 buf->ccontext.NameLength = cpu_to_le16(4);
1565
1566 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1567 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1568 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1569 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1570
1571 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1572 buf->Name[0] = 'D';
1573 buf->Name[1] = 'H';
1574 buf->Name[2] = '2';
1575 buf->Name[3] = 'C';
1576 return buf;
1577}
1578
63eb3def 1579static int
b56eae4d
SF
1580add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1581 struct cifs_open_parms *oparms)
1582{
1583 struct smb2_create_req *req = iov[0].iov_base;
1584 unsigned int num = *num_iovec;
1585
1586 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1587 if (iov[num].iov_base == NULL)
1588 return -ENOMEM;
1589 iov[num].iov_len = sizeof(struct create_durable_v2);
1590 if (!req->CreateContextsOffset)
1591 req->CreateContextsOffset =
4f33bc35 1592 cpu_to_le32(sizeof(struct smb2_create_req) +
b56eae4d
SF
1593 iov[1].iov_len);
1594 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
b56eae4d
SF
1595 *num_iovec = num + 1;
1596 return 0;
1597}
1598
1599static int
1600add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
9cbc0b73 1601 struct cifs_open_parms *oparms)
63eb3def
PS
1602{
1603 struct smb2_create_req *req = iov[0].iov_base;
1604 unsigned int num = *num_iovec;
1605
b56eae4d
SF
1606 /* indicate that we don't need to relock the file */
1607 oparms->reconnect = false;
1608
1609 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1610 if (iov[num].iov_base == NULL)
1611 return -ENOMEM;
1612 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1613 if (!req->CreateContextsOffset)
1614 req->CreateContextsOffset =
4f33bc35 1615 cpu_to_le32(sizeof(struct smb2_create_req) +
b56eae4d
SF
1616 iov[1].iov_len);
1617 le32_add_cpu(&req->CreateContextsLength,
1618 sizeof(struct create_durable_handle_reconnect_v2));
b56eae4d
SF
1619 *num_iovec = num + 1;
1620 return 0;
1621}
1622
1623static int
1624add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1625 struct cifs_open_parms *oparms, bool use_persistent)
1626{
1627 struct smb2_create_req *req = iov[0].iov_base;
1628 unsigned int num = *num_iovec;
1629
1630 if (use_persistent) {
1631 if (oparms->reconnect)
1632 return add_durable_reconnect_v2_context(iov, num_iovec,
1633 oparms);
1634 else
1635 return add_durable_v2_context(iov, num_iovec, oparms);
1636 }
1637
9cbc0b73
PS
1638 if (oparms->reconnect) {
1639 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1640 /* indicate that we don't need to relock the file */
1641 oparms->reconnect = false;
1642 } else
1643 iov[num].iov_base = create_durable_buf();
63eb3def
PS
1644 if (iov[num].iov_base == NULL)
1645 return -ENOMEM;
1646 iov[num].iov_len = sizeof(struct create_durable);
1647 if (!req->CreateContextsOffset)
1648 req->CreateContextsOffset =
4f33bc35 1649 cpu_to_le32(sizeof(struct smb2_create_req) +
63eb3def 1650 iov[1].iov_len);
31f92e9a 1651 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
63eb3def
PS
1652 *num_iovec = num + 1;
1653 return 0;
1654}
1655
f0712928
AA
1656static int
1657alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1658 const char *treename, const __le16 *path)
1659{
1660 int treename_len, path_len;
1661 struct nls_table *cp;
1662 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1663
1664 /*
1665 * skip leading "\\"
1666 */
1667 treename_len = strlen(treename);
1668 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1669 return -EINVAL;
1670
1671 treename += 2;
1672 treename_len -= 2;
1673
1674 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1675
1676 /*
1677 * make room for one path separator between the treename and
1678 * path
1679 */
1680 *out_len = treename_len + 1 + path_len;
1681
1682 /*
1683 * final path needs to be null-terminated UTF16 with a
1684 * size aligned to 8
1685 */
1686
1687 *out_size = roundup((*out_len+1)*2, 8);
1688 *out_path = kzalloc(*out_size, GFP_KERNEL);
1689 if (!*out_path)
1690 return -ENOMEM;
1691
1692 cp = load_nls_default();
1693 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1694 UniStrcat(*out_path, sep);
1695 UniStrcat(*out_path, path);
1696 unload_nls(cp);
1697
1698 return 0;
1699}
1700
2503a0db 1701int
064f6047 1702SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
b42bf888
PS
1703 __u8 *oplock, struct smb2_file_all_info *buf,
1704 struct smb2_err_rsp **err_buf)
2503a0db
PS
1705{
1706 struct smb2_create_req *req;
1707 struct smb2_create_rsp *rsp;
1708 struct TCP_Server_Info *server;
064f6047 1709 struct cifs_tcon *tcon = oparms->tcon;
2503a0db 1710 struct cifs_ses *ses = tcon->ses;
63eb3def 1711 struct kvec iov[4];
bf2afee1 1712 struct kvec rsp_iov = {NULL, 0};
2503a0db
PS
1713 int resp_buftype;
1714 int uni_path_len;
b8c32dbb
PS
1715 __le16 *copy_path = NULL;
1716 int copy_size;
2503a0db 1717 int rc = 0;
da502f7d 1718 unsigned int n_iov = 2;
ca81983f 1719 __u32 file_attributes = 0;
663a9621 1720 char *dhc_buf = NULL, *lc_buf = NULL;
7fb8986e 1721 int flags = 0;
4f33bc35 1722 unsigned int total_len;
2503a0db 1723
f96637be 1724 cifs_dbg(FYI, "create/open\n");
2503a0db
PS
1725
1726 if (ses && (ses->server))
1727 server = ses->server;
1728 else
1729 return -EIO;
1730
4f33bc35
RS
1731 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1732
2503a0db
PS
1733 if (rc)
1734 return rc;
1735
7fb8986e
PS
1736 if (encryption_required(tcon))
1737 flags |= CIFS_TRANSFORM_REQ;
1738
064f6047 1739 if (oparms->create_options & CREATE_OPTION_READONLY)
ca81983f 1740 file_attributes |= ATTR_READONLY;
db8b631d
SF
1741 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1742 file_attributes |= ATTR_SYSTEM;
ca81983f 1743
2503a0db 1744 req->ImpersonationLevel = IL_IMPERSONATION;
064f6047 1745 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2503a0db
PS
1746 /* File attributes ignored on open (used in create though) */
1747 req->FileAttributes = cpu_to_le32(file_attributes);
1748 req->ShareAccess = FILE_SHARE_ALL_LE;
064f6047
PS
1749 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1750 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2503a0db
PS
1751
1752 iov[0].iov_base = (char *)req;
59aa3718 1753 /* -1 since last byte is buf[0] which is sent below (path) */
4f33bc35 1754 iov[0].iov_len = total_len - 1;
f0712928 1755
4f33bc35 1756 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
f0712928
AA
1757
1758 /* [MS-SMB2] 2.2.13 NameOffset:
1759 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1760 * the SMB2 header, the file name includes a prefix that will
1761 * be processed during DFS name normalization as specified in
1762 * section 3.3.5.9. Otherwise, the file name is relative to
1763 * the share that is identified by the TreeId in the SMB2
1764 * header.
1765 */
1766 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1767 int name_len;
1768
4f33bc35 1769 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
f0712928
AA
1770 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1771 &name_len,
1772 tcon->treeName, path);
b7a73c84
RS
1773 if (rc) {
1774 cifs_small_buf_release(req);
f0712928 1775 return rc;
b7a73c84 1776 }
f0712928 1777 req->NameLength = cpu_to_le16(name_len * 2);
59aa3718
PS
1778 uni_path_len = copy_size;
1779 path = copy_path;
f0712928
AA
1780 } else {
1781 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1782 /* MUST set path len (NameLength) to 0 opening root of share */
1783 req->NameLength = cpu_to_le16(uni_path_len - 2);
1784 if (uni_path_len % 8 != 0) {
1785 copy_size = roundup(uni_path_len, 8);
1786 copy_path = kzalloc(copy_size, GFP_KERNEL);
b7a73c84
RS
1787 if (!copy_path) {
1788 cifs_small_buf_release(req);
f0712928 1789 return -ENOMEM;
b7a73c84 1790 }
f0712928
AA
1791 memcpy((char *)copy_path, (const char *)path,
1792 uni_path_len);
1793 uni_path_len = copy_size;
1794 path = copy_path;
1795 }
2503a0db
PS
1796 }
1797
59aa3718
PS
1798 iov[1].iov_len = uni_path_len;
1799 iov[1].iov_base = path;
59aa3718 1800
b8c32dbb
PS
1801 if (!server->oplocks)
1802 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1803
a41a28bd 1804 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
b8c32dbb
PS
1805 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1806 req->RequestedOplockLevel = *oplock;
1807 else {
da502f7d 1808 rc = add_lease_context(server, iov, &n_iov, oplock);
d22cbfec 1809 if (rc) {
b8c32dbb
PS
1810 cifs_small_buf_release(req);
1811 kfree(copy_path);
d22cbfec 1812 return rc;
b8c32dbb 1813 }
da502f7d 1814 lc_buf = iov[n_iov-1].iov_base;
b8c32dbb
PS
1815 }
1816
63eb3def
PS
1817 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1818 /* need to set Next field of lease context if we request it */
a41a28bd 1819 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
63eb3def 1820 struct create_context *ccontext =
da502f7d 1821 (struct create_context *)iov[n_iov-1].iov_base;
1c46943f 1822 ccontext->Next =
a41a28bd 1823 cpu_to_le32(server->vals->create_lease_size);
63eb3def 1824 }
b56eae4d 1825
da502f7d 1826 rc = add_durable_context(iov, &n_iov, oparms,
b56eae4d 1827 tcon->use_persistent);
63eb3def
PS
1828 if (rc) {
1829 cifs_small_buf_release(req);
1830 kfree(copy_path);
663a9621 1831 kfree(lc_buf);
63eb3def
PS
1832 return rc;
1833 }
da502f7d 1834 dhc_buf = iov[n_iov-1].iov_base;
63eb3def
PS
1835 }
1836
4f33bc35
RS
1837 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1838 &rsp_iov);
da502f7d
PS
1839 cifs_small_buf_release(req);
1840 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2503a0db
PS
1841
1842 if (rc != 0) {
1843 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
bf2afee1 1844 if (err_buf && rsp)
b42bf888
PS
1845 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1846 GFP_KERNEL);
2503a0db
PS
1847 goto creat_exit;
1848 }
1849
064f6047
PS
1850 oparms->fid->persistent_fid = rsp->PersistentFileId;
1851 oparms->fid->volatile_fid = rsp->VolatileFileId;
f0df737e
PS
1852
1853 if (buf) {
1854 memcpy(buf, &rsp->CreationTime, 32);
1855 buf->AllocationSize = rsp->AllocationSize;
1856 buf->EndOfFile = rsp->EndofFile;
1857 buf->Attributes = rsp->FileAttributes;
1858 buf->NumberOfLinks = cpu_to_le32(1);
1859 buf->DeletePending = 0;
1860 }
2e44b288 1861
b8c32dbb 1862 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
42873b0a 1863 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
b8c32dbb
PS
1864 else
1865 *oplock = rsp->OplockLevel;
2503a0db 1866creat_exit:
b8c32dbb 1867 kfree(copy_path);
663a9621
PS
1868 kfree(lc_buf);
1869 kfree(dhc_buf);
2503a0db
PS
1870 free_rsp_buf(resp_buftype, rsp);
1871 return rc;
1872}
1873
4a72dafa
SF
1874/*
1875 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1876 */
1877int
1878SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
63a83b86 1879 u64 volatile_fid, u32 opcode, bool is_fsctl,
51146625
AA
1880 char *in_data, u32 indatalen,
1881 char **out_data, u32 *plen /* returned data len */)
4a72dafa
SF
1882{
1883 struct smb2_ioctl_req *req;
1884 struct smb2_ioctl_rsp *rsp;
31473fc4 1885 struct smb2_sync_hdr *shdr;
8e353106 1886 struct cifs_ses *ses;
4a72dafa 1887 struct kvec iov[2];
da502f7d 1888 struct kvec rsp_iov;
4a72dafa 1889 int resp_buftype;
da502f7d 1890 int n_iov;
4a72dafa 1891 int rc = 0;
7fb8986e 1892 int flags = 0;
97754680 1893 unsigned int total_len;
4a72dafa
SF
1894
1895 cifs_dbg(FYI, "SMB2 IOCTL\n");
1896
3d1a3745
SF
1897 if (out_data != NULL)
1898 *out_data = NULL;
1899
4a72dafa
SF
1900 /* zero out returned data len, in case of error */
1901 if (plen)
1902 *plen = 0;
1903
8e353106
SF
1904 if (tcon)
1905 ses = tcon->ses;
1906 else
1907 return -EIO;
1908
68a6afa7 1909 if (!ses || !(ses->server))
4a72dafa
SF
1910 return -EIO;
1911
97754680 1912 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
4a72dafa
SF
1913 if (rc)
1914 return rc;
1915
7fb8986e
PS
1916 if (encryption_required(tcon))
1917 flags |= CIFS_TRANSFORM_REQ;
1918
4a72dafa
SF
1919 req->CtlCode = cpu_to_le32(opcode);
1920 req->PersistentFileId = persistent_fid;
1921 req->VolatileFileId = volatile_fid;
1922
1923 if (indatalen) {
1924 req->InputCount = cpu_to_le32(indatalen);
1925 /* do not set InputOffset if no input data */
1926 req->InputOffset =
97754680 1927 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
4a72dafa
SF
1928 iov[1].iov_base = in_data;
1929 iov[1].iov_len = indatalen;
da502f7d 1930 n_iov = 2;
4a72dafa 1931 } else
da502f7d 1932 n_iov = 1;
4a72dafa
SF
1933
1934 req->OutputOffset = 0;
1935 req->OutputCount = 0; /* MBZ */
1936
1937 /*
1938 * Could increase MaxOutputResponse, but that would require more
1939 * than one credit. Windows typically sets this smaller, but for some
1940 * ioctls it may be useful to allow server to send more. No point
1941 * limiting what the server can send as long as fits in one credit
7db0a6ef
SF
1942 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1943 * (by default, note that it can be overridden to make max larger)
1944 * in responses (except for read responses which can be bigger.
1945 * We may want to bump this limit up
4a72dafa 1946 */
7db0a6ef 1947 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
4a72dafa
SF
1948
1949 if (is_fsctl)
1950 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1951 else
1952 req->Flags = 0;
1953
1954 iov[0].iov_base = (char *)req;
4a72dafa 1955
7ff8d45c
SF
1956 /*
1957 * If no input data, the size of ioctl struct in
1958 * protocol spec still includes a 1 byte data buffer,
1959 * but if input data passed to ioctl, we do not
1960 * want to double count this, so we do not send
1961 * the dummy one byte of data in iovec[0] if sending
97754680 1962 * input data (in iovec[1]).
7ff8d45c
SF
1963 */
1964
1965 if (indatalen) {
97754680 1966 iov[0].iov_len = total_len - 1;
7ff8d45c 1967 } else
97754680 1968 iov[0].iov_len = total_len;
7ff8d45c 1969
4587eee0
SF
1970 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1971 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
97754680 1972 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
4a72dafa 1973
97754680
RS
1974 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1975 &rsp_iov);
da502f7d
PS
1976 cifs_small_buf_release(req);
1977 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
4a72dafa 1978
9bf0c9cd 1979 if ((rc != 0) && (rc != -EINVAL)) {
8e353106 1980 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
4a72dafa 1981 goto ioctl_exit;
9bf0c9cd
SF
1982 } else if (rc == -EINVAL) {
1983 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1984 (opcode != FSCTL_SRV_COPYCHUNK)) {
8e353106 1985 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
9bf0c9cd
SF
1986 goto ioctl_exit;
1987 }
4a72dafa
SF
1988 }
1989
1990 /* check if caller wants to look at return data or just return rc */
1991 if ((plen == NULL) || (out_data == NULL))
1992 goto ioctl_exit;
1993
1994 *plen = le32_to_cpu(rsp->OutputCount);
1995
1996 /* We check for obvious errors in the output buffer length and offset */
1997 if (*plen == 0)
1998 goto ioctl_exit; /* server returned no data */
1999 else if (*plen > 0xFF00) {
2000 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2001 *plen = 0;
2002 rc = -EIO;
2003 goto ioctl_exit;
2004 }
2005
2006 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2007 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2008 le32_to_cpu(rsp->OutputOffset));
2009 *plen = 0;
2010 rc = -EIO;
2011 goto ioctl_exit;
2012 }
2013
2014 *out_data = kmalloc(*plen, GFP_KERNEL);
2015 if (*out_data == NULL) {
2016 rc = -ENOMEM;
2017 goto ioctl_exit;
2018 }
2019
31473fc4
PS
2020 shdr = get_sync_hdr(rsp);
2021 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
4a72dafa
SF
2022ioctl_exit:
2023 free_rsp_buf(resp_buftype, rsp);
2024 return rc;
2025}
2026
64a5cfa6
SF
2027/*
2028 * Individual callers to ioctl worker function follow
2029 */
2030
2031int
2032SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2033 u64 persistent_fid, u64 volatile_fid)
2034{
2035 int rc;
64a5cfa6
SF
2036 struct compress_ioctl fsctl_input;
2037 char *ret_data = NULL;
2038
2039 fsctl_input.CompressionState =
bc09d141 2040 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
64a5cfa6
SF
2041
2042 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2043 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2044 (char *)&fsctl_input /* data input */,
2045 2 /* in data len */, &ret_data /* out data */, NULL);
2046
2047 cifs_dbg(FYI, "set compression rc %d\n", rc);
64a5cfa6
SF
2048
2049 return rc;
2050}
2051
2503a0db
PS
2052int
2053SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2054 u64 persistent_fid, u64 volatile_fid)
2055{
2056 struct smb2_close_req *req;
2057 struct smb2_close_rsp *rsp;
2503a0db
PS
2058 struct cifs_ses *ses = tcon->ses;
2059 struct kvec iov[1];
da502f7d 2060 struct kvec rsp_iov;
2503a0db
PS
2061 int resp_buftype;
2062 int rc = 0;
7fb8986e 2063 int flags = 0;
afcccefd 2064 unsigned int total_len;
2503a0db 2065
f96637be 2066 cifs_dbg(FYI, "Close\n");
2503a0db 2067
68a6afa7 2068 if (!ses || !(ses->server))
2503a0db
PS
2069 return -EIO;
2070
afcccefd 2071 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2503a0db
PS
2072 if (rc)
2073 return rc;
2074
7fb8986e
PS
2075 if (encryption_required(tcon))
2076 flags |= CIFS_TRANSFORM_REQ;
2077
2503a0db
PS
2078 req->PersistentFileId = persistent_fid;
2079 req->VolatileFileId = volatile_fid;
2080
2081 iov[0].iov_base = (char *)req;
afcccefd 2082 iov[0].iov_len = total_len;
2503a0db 2083
afcccefd 2084 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
2085 cifs_small_buf_release(req);
2086 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2503a0db
PS
2087
2088 if (rc != 0) {
d4a029d2 2089 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2503a0db
PS
2090 goto close_exit;
2091 }
2092
2503a0db
PS
2093 /* BB FIXME - decode close response, update inode for caching */
2094
2095close_exit:
2096 free_rsp_buf(resp_buftype, rsp);
2097 return rc;
2098}
be4cb9e3
PS
2099
2100static int
2101validate_buf(unsigned int offset, unsigned int buffer_length,
2102 struct smb2_hdr *hdr, unsigned int min_buf_size)
2103
2104{
2105 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2106 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2107 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2108 char *end_of_buf = begin_of_buf + buffer_length;
2109
2110
2111 if (buffer_length < min_buf_size) {
f96637be
JP
2112 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2113 buffer_length, min_buf_size);
be4cb9e3
PS
2114 return -EINVAL;
2115 }
2116
2117 /* check if beyond RFC1001 maximum length */
2118 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
f96637be
JP
2119 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2120 buffer_length, smb_len);
be4cb9e3
PS
2121 return -EINVAL;
2122 }
2123
2124 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
f96637be 2125 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
be4cb9e3
PS
2126 return -EINVAL;
2127 }
2128
2129 return 0;
2130}
2131
2132/*
2133 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2134 * Caller must free buffer.
2135 */
2136static int
2137validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2138 struct smb2_hdr *hdr, unsigned int minbufsize,
2139 char *data)
2140
2141{
2142 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2143 int rc;
2144
2145 if (!data)
2146 return -EINVAL;
2147
2148 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2149 if (rc)
2150 return rc;
2151
2152 memcpy(data, begin_of_buf, buffer_length);
2153
2154 return 0;
2155}
2156
f0df737e
PS
2157static int
2158query_info(const unsigned int xid, struct cifs_tcon *tcon,
42c493c1
SP
2159 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2160 u32 additional_info, size_t output_len, size_t min_len, void **data,
2161 u32 *dlen)
be4cb9e3
PS
2162{
2163 struct smb2_query_info_req *req;
2164 struct smb2_query_info_rsp *rsp = NULL;
2165 struct kvec iov[2];
da502f7d 2166 struct kvec rsp_iov;
be4cb9e3
PS
2167 int rc = 0;
2168 int resp_buftype;
be4cb9e3 2169 struct cifs_ses *ses = tcon->ses;
7fb8986e 2170 int flags = 0;
b2fb7fec 2171 unsigned int total_len;
be4cb9e3 2172
f96637be 2173 cifs_dbg(FYI, "Query Info\n");
be4cb9e3 2174
68a6afa7 2175 if (!ses || !(ses->server))
be4cb9e3
PS
2176 return -EIO;
2177
b2fb7fec
RS
2178 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2179 &total_len);
be4cb9e3
PS
2180 if (rc)
2181 return rc;
2182
7fb8986e
PS
2183 if (encryption_required(tcon))
2184 flags |= CIFS_TRANSFORM_REQ;
2185
42c493c1 2186 req->InfoType = info_type;
f0df737e 2187 req->FileInfoClass = info_class;
be4cb9e3
PS
2188 req->PersistentFileId = persistent_fid;
2189 req->VolatileFileId = volatile_fid;
42c493c1 2190 req->AdditionalInformation = cpu_to_le32(additional_info);
48923d2a
AA
2191
2192 /*
2193 * We do not use the input buffer (do not send extra byte)
2194 */
2195 req->InputBufferOffset = 0;
48923d2a 2196
f0df737e 2197 req->OutputBufferLength = cpu_to_le32(output_len);
be4cb9e3
PS
2198
2199 iov[0].iov_base = (char *)req;
b2fb7fec
RS
2200 /* 1 for Buffer */
2201 iov[0].iov_len = total_len - 1;
be4cb9e3 2202
b2fb7fec 2203 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
2204 cifs_small_buf_release(req);
2205 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
e5d04887 2206
be4cb9e3
PS
2207 if (rc) {
2208 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2209 goto qinf_exit;
2210 }
2211
42c493c1
SP
2212 if (dlen) {
2213 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2214 if (!*data) {
2215 *data = kmalloc(*dlen, GFP_KERNEL);
2216 if (!*data) {
2217 cifs_dbg(VFS,
2218 "Error %d allocating memory for acl\n",
2219 rc);
2220 *dlen = 0;
2221 goto qinf_exit;
2222 }
2223 }
2224 }
2225
be4cb9e3
PS
2226 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2227 le32_to_cpu(rsp->OutputBufferLength),
42c493c1 2228 &rsp->hdr, min_len, *data);
be4cb9e3
PS
2229
2230qinf_exit:
2231 free_rsp_buf(resp_buftype, rsp);
2232 return rc;
2233}
9094fad1 2234
95907fea 2235int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
7cb3def4
RS
2236 u64 persistent_fid, u64 volatile_fid,
2237 int ea_buf_size, struct smb2_file_full_ea_info *data)
95907fea
RS
2238{
2239 return query_info(xid, tcon, persistent_fid, volatile_fid,
2240 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
7cb3def4 2241 ea_buf_size,
95907fea
RS
2242 sizeof(struct smb2_file_full_ea_info),
2243 (void **)&data,
2244 NULL);
2245}
2246
42c493c1
SP
2247int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2248 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2249{
2250 return query_info(xid, tcon, persistent_fid, volatile_fid,
2251 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2252 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2253 sizeof(struct smb2_file_all_info), (void **)&data,
2254 NULL);
2255}
2256
f0df737e 2257int
42c493c1 2258SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
f0df737e 2259 u64 persistent_fid, u64 volatile_fid,
42c493c1 2260 void **data, u32 *plen)
f0df737e 2261{
42c493c1
SP
2262 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2263 *plen = 0;
2264
f0df737e 2265 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1
SP
2266 0, SMB2_O_INFO_SECURITY, additional_info,
2267 SMB2_MAX_BUFFER_SIZE,
2268 sizeof(struct smb2_file_all_info), data, plen);
f0df737e
PS
2269}
2270
2271int
2272SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2273 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2274{
2275 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1
SP
2276 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2277 sizeof(struct smb2_file_internal_info),
f0df737e 2278 sizeof(struct smb2_file_internal_info),
42c493c1 2279 (void **)&uniqueid, NULL);
f0df737e
PS
2280}
2281
9094fad1
PS
2282/*
2283 * This is a no-op for now. We're not really interested in the reply, but
2284 * rather in the fact that the server sent one and that server->lstrp
2285 * gets updated.
2286 *
2287 * FIXME: maybe we should consider checking that the reply matches request?
2288 */
2289static void
2290smb2_echo_callback(struct mid_q_entry *mid)
2291{
2292 struct TCP_Server_Info *server = mid->callback_data;
31473fc4 2293 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
9094fad1
PS
2294 unsigned int credits_received = 1;
2295
2296 if (mid->mid_state == MID_RESPONSE_RECEIVED)
31473fc4 2297 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
9094fad1
PS
2298
2299 DeleteMidQEntry(mid);
2300 add_credits(server, credits_received, CIFS_ECHO_OP);
2301}
2302
53e0e11e
PS
2303void smb2_reconnect_server(struct work_struct *work)
2304{
2305 struct TCP_Server_Info *server = container_of(work,
2306 struct TCP_Server_Info, reconnect.work);
2307 struct cifs_ses *ses;
2308 struct cifs_tcon *tcon, *tcon2;
2309 struct list_head tmp_list;
2310 int tcon_exist = false;
18ea4311
GP
2311 int rc;
2312 int resched = false;
2313
53e0e11e
PS
2314
2315 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2316 mutex_lock(&server->reconnect_mutex);
2317
2318 INIT_LIST_HEAD(&tmp_list);
2319 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2320
2321 spin_lock(&cifs_tcp_ses_lock);
2322 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2323 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
96a988ff 2324 if (tcon->need_reconnect || tcon->need_reopen_files) {
53e0e11e
PS
2325 tcon->tc_count++;
2326 list_add_tail(&tcon->rlist, &tmp_list);
2327 tcon_exist = true;
2328 }
2329 }
b327a717
AA
2330 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2331 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2332 tcon_exist = true;
2333 }
53e0e11e
PS
2334 }
2335 /*
2336 * Get the reference to server struct to be sure that the last call of
2337 * cifs_put_tcon() in the loop below won't release the server pointer.
2338 */
2339 if (tcon_exist)
2340 server->srv_count++;
2341
2342 spin_unlock(&cifs_tcp_ses_lock);
2343
2344 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
18ea4311
GP
2345 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2346 if (!rc)
96a988ff 2347 cifs_reopen_persistent_handles(tcon);
18ea4311
GP
2348 else
2349 resched = true;
53e0e11e
PS
2350 list_del_init(&tcon->rlist);
2351 cifs_put_tcon(tcon);
2352 }
2353
2354 cifs_dbg(FYI, "Reconnecting tcons finished\n");
18ea4311
GP
2355 if (resched)
2356 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
53e0e11e
PS
2357 mutex_unlock(&server->reconnect_mutex);
2358
2359 /* now we can safely release srv struct */
2360 if (tcon_exist)
2361 cifs_put_tcp_session(server, 1);
2362}
2363
9094fad1
PS
2364int
2365SMB2_echo(struct TCP_Server_Info *server)
2366{
2367 struct smb2_echo_req *req;
2368 int rc = 0;
738f9de5
PS
2369 struct kvec iov[2];
2370 struct smb_rqst rqst = { .rq_iov = iov,
2371 .rq_nvec = 2 };
7f7ae759
RS
2372 unsigned int total_len;
2373 __be32 rfc1002_marker;
9094fad1 2374
f96637be 2375 cifs_dbg(FYI, "In echo request\n");
9094fad1 2376
4fcd1813 2377 if (server->tcpStatus == CifsNeedNegotiate) {
53e0e11e
PS
2378 /* No need to send echo on newly established connections */
2379 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2380 return rc;
4fcd1813
SF
2381 }
2382
7f7ae759 2383 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
9094fad1
PS
2384 if (rc)
2385 return rc;
2386
7f7ae759 2387 req->sync_hdr.CreditRequest = cpu_to_le16(1);
9094fad1 2388
738f9de5 2389 iov[0].iov_len = 4;
7f7ae759
RS
2390 rfc1002_marker = cpu_to_be32(total_len);
2391 iov[0].iov_base = &rfc1002_marker;
2392 iov[1].iov_len = total_len;
2393 iov[1].iov_base = (char *)req;
9094fad1 2394
9b7c18a2
PS
2395 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2396 server, CIFS_ECHO_OP);
9094fad1 2397 if (rc)
f96637be 2398 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
9094fad1
PS
2399
2400 cifs_small_buf_release(req);
2401 return rc;
2402}
7a5cfb19
PS
2403
2404int
2405SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2406 u64 volatile_fid)
2407{
2408 struct smb2_flush_req *req;
7a5cfb19
PS
2409 struct cifs_ses *ses = tcon->ses;
2410 struct kvec iov[1];
da502f7d 2411 struct kvec rsp_iov;
7a5cfb19
PS
2412 int resp_buftype;
2413 int rc = 0;
7fb8986e 2414 int flags = 0;
1f444e4c 2415 unsigned int total_len;
7a5cfb19 2416
f96637be 2417 cifs_dbg(FYI, "Flush\n");
7a5cfb19 2418
68a6afa7 2419 if (!ses || !(ses->server))
7a5cfb19
PS
2420 return -EIO;
2421
1f444e4c 2422 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
7a5cfb19
PS
2423 if (rc)
2424 return rc;
2425
7fb8986e
PS
2426 if (encryption_required(tcon))
2427 flags |= CIFS_TRANSFORM_REQ;
2428
7a5cfb19
PS
2429 req->PersistentFileId = persistent_fid;
2430 req->VolatileFileId = volatile_fid;
2431
2432 iov[0].iov_base = (char *)req;
1f444e4c 2433 iov[0].iov_len = total_len;
7a5cfb19 2434
1f444e4c 2435 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d 2436 cifs_small_buf_release(req);
7a5cfb19 2437
dfebe400 2438 if (rc != 0)
7a5cfb19
PS
2439 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2440
da502f7d 2441 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
7a5cfb19
PS
2442 return rc;
2443}
09a4707e
PS
2444
2445/*
2446 * To form a chain of read requests, any read requests after the first should
2447 * have the end_of_chain boolean set to true.
2448 */
2449static int
738f9de5 2450smb2_new_read_req(void **buf, unsigned int *total_len,
2dabfd5b
LL
2451 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2452 unsigned int remaining_bytes, int request_type)
09a4707e
PS
2453{
2454 int rc = -EACCES;
b8f57ee8 2455 struct smb2_read_plain_req *req = NULL;
31473fc4 2456 struct smb2_sync_hdr *shdr;
2dabfd5b 2457 struct TCP_Server_Info *server;
09a4707e 2458
b8f57ee8
PS
2459 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2460 total_len);
09a4707e
PS
2461 if (rc)
2462 return rc;
2dabfd5b
LL
2463
2464 server = io_parms->tcon->ses->server;
2465 if (server == NULL)
09a4707e
PS
2466 return -ECONNABORTED;
2467
b8f57ee8 2468 shdr = &req->sync_hdr;
31473fc4 2469 shdr->ProcessId = cpu_to_le32(io_parms->pid);
09a4707e
PS
2470
2471 req->PersistentFileId = io_parms->persistent_fid;
2472 req->VolatileFileId = io_parms->volatile_fid;
2473 req->ReadChannelInfoOffset = 0; /* reserved */
2474 req->ReadChannelInfoLength = 0; /* reserved */
2475 req->Channel = 0; /* reserved */
2476 req->MinimumCount = 0;
2477 req->Length = cpu_to_le32(io_parms->length);
2478 req->Offset = cpu_to_le64(io_parms->offset);
bd3dcc6a
LL
2479#ifdef CONFIG_CIFS_SMB_DIRECT
2480 /*
2481 * If we want to do a RDMA write, fill in and append
2482 * smbd_buffer_descriptor_v1 to the end of read request
2483 */
2484 if (server->rdma && rdata &&
2485 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
2486
2487 struct smbd_buffer_descriptor_v1 *v1;
2488 bool need_invalidate =
2489 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2490
2491 rdata->mr = smbd_register_mr(
2492 server->smbd_conn, rdata->pages,
2493 rdata->nr_pages, rdata->tailsz,
2494 true, need_invalidate);
2495 if (!rdata->mr)
2496 return -ENOBUFS;
2497
2498 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2499 if (need_invalidate)
2500 req->Channel = SMB2_CHANNEL_RDMA_V1;
2501 req->ReadChannelInfoOffset =
2026b06e 2502 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
bd3dcc6a 2503 req->ReadChannelInfoLength =
2026b06e 2504 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
bd3dcc6a 2505 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
2506 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2507 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2508 v1->length = cpu_to_le32(rdata->mr->mr->length);
bd3dcc6a
LL
2509
2510 *total_len += sizeof(*v1) - 1;
2511 }
2512#endif
09a4707e
PS
2513 if (request_type & CHAINED_REQUEST) {
2514 if (!(request_type & END_OF_CHAIN)) {
b8f57ee8
PS
2515 /* next 8-byte aligned request */
2516 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2517 shdr->NextCommand = cpu_to_le32(*total_len);
09a4707e 2518 } else /* END_OF_CHAIN */
31473fc4 2519 shdr->NextCommand = 0;
09a4707e 2520 if (request_type & RELATED_REQUEST) {
31473fc4 2521 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
09a4707e
PS
2522 /*
2523 * Related requests use info from previous read request
2524 * in chain.
2525 */
31473fc4
PS
2526 shdr->SessionId = 0xFFFFFFFF;
2527 shdr->TreeId = 0xFFFFFFFF;
09a4707e
PS
2528 req->PersistentFileId = 0xFFFFFFFF;
2529 req->VolatileFileId = 0xFFFFFFFF;
2530 }
2531 }
2532 if (remaining_bytes > io_parms->length)
2533 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2534 else
2535 req->RemainingBytes = 0;
2536
738f9de5 2537 *buf = req;
09a4707e
PS
2538 return rc;
2539}
2540
2541static void
2542smb2_readv_callback(struct mid_q_entry *mid)
2543{
2544 struct cifs_readdata *rdata = mid->callback_data;
2545 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2546 struct TCP_Server_Info *server = tcon->ses->server;
738f9de5
PS
2547 struct smb2_sync_hdr *shdr =
2548 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
09a4707e 2549 unsigned int credits_received = 1;
738f9de5
PS
2550 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2551 .rq_nvec = 2,
8321fec4
JL
2552 .rq_pages = rdata->pages,
2553 .rq_npages = rdata->nr_pages,
2554 .rq_pagesz = rdata->pagesz,
2555 .rq_tailsz = rdata->tailsz };
09a4707e 2556
f96637be
JP
2557 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2558 __func__, mid->mid, mid->mid_state, rdata->result,
2559 rdata->bytes);
09a4707e
PS
2560
2561 switch (mid->mid_state) {
2562 case MID_RESPONSE_RECEIVED:
31473fc4 2563 credits_received = le16_to_cpu(shdr->CreditRequest);
09a4707e 2564 /* result already set, check signature */
4326ed2f 2565 if (server->sign && !mid->decrypted) {
3c1bf7e4
PS
2566 int rc;
2567
0b688cfc 2568 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4 2569 if (rc)
f96637be
JP
2570 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2571 rc);
3c1bf7e4 2572 }
09a4707e 2573 /* FIXME: should this be counted toward the initiating task? */
34a54d61
PS
2574 task_io_account_read(rdata->got_bytes);
2575 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e
PS
2576 break;
2577 case MID_REQUEST_SUBMITTED:
2578 case MID_RETRY_NEEDED:
2579 rdata->result = -EAGAIN;
d913ed17
PS
2580 if (server->sign && rdata->got_bytes)
2581 /* reset bytes number since we can not check a sign */
2582 rdata->got_bytes = 0;
2583 /* FIXME: should this be counted toward the initiating task? */
2584 task_io_account_read(rdata->got_bytes);
2585 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e
PS
2586 break;
2587 default:
2588 if (rdata->result != -ENODATA)
2589 rdata->result = -EIO;
2590 }
bd3dcc6a
LL
2591#ifdef CONFIG_CIFS_SMB_DIRECT
2592 /*
2593 * If this rdata has a memmory registered, the MR can be freed
2594 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2595 * because they have limited number and are used for future I/Os
2596 */
2597 if (rdata->mr) {
2598 smbd_deregister_mr(rdata->mr);
2599 rdata->mr = NULL;
2600 }
2601#endif
09a4707e
PS
2602 if (rdata->result)
2603 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2604
2605 queue_work(cifsiod_wq, &rdata->work);
2606 DeleteMidQEntry(mid);
2607 add_credits(server, credits_received, 0);
2608}
2609
738f9de5 2610/* smb2_async_readv - send an async read, and set up mid to handle result */
09a4707e
PS
2611int
2612smb2_async_readv(struct cifs_readdata *rdata)
2613{
bed9da02 2614 int rc, flags = 0;
31473fc4
PS
2615 char *buf;
2616 struct smb2_sync_hdr *shdr;
09a4707e 2617 struct cifs_io_parms io_parms;
738f9de5
PS
2618 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2619 .rq_nvec = 2 };
bed9da02 2620 struct TCP_Server_Info *server;
738f9de5 2621 unsigned int total_len;
b8f57ee8 2622 __be32 req_len;
09a4707e 2623
f96637be
JP
2624 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2625 __func__, rdata->offset, rdata->bytes);
09a4707e
PS
2626
2627 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2628 io_parms.offset = rdata->offset;
2629 io_parms.length = rdata->bytes;
2630 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2631 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2632 io_parms.pid = rdata->pid;
bed9da02
PS
2633
2634 server = io_parms.tcon->ses->server;
2635
2dabfd5b
LL
2636 rc = smb2_new_read_req(
2637 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
bed9da02
PS
2638 if (rc) {
2639 if (rc == -EAGAIN && rdata->credits) {
2640 /* credits was reset by reconnect */
2641 rdata->credits = 0;
2642 /* reduce in_flight value since we won't send the req */
2643 spin_lock(&server->req_lock);
2644 server->in_flight--;
2645 spin_unlock(&server->req_lock);
2646 }
09a4707e 2647 return rc;
bed9da02 2648 }
09a4707e 2649
7fb8986e
PS
2650 if (encryption_required(io_parms.tcon))
2651 flags |= CIFS_TRANSFORM_REQ;
2652
b8f57ee8
PS
2653 req_len = cpu_to_be32(total_len);
2654
2655 rdata->iov[0].iov_base = &req_len;
2656 rdata->iov[0].iov_len = sizeof(__be32);
2657 rdata->iov[1].iov_base = buf;
2658 rdata->iov[1].iov_len = total_len;
2659
2660 shdr = (struct smb2_sync_hdr *)buf;
09a4707e 2661
bed9da02 2662 if (rdata->credits) {
31473fc4 2663 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
bed9da02 2664 SMB2_MAX_BUFFER_SIZE));
31473fc4 2665 shdr->CreditRequest = shdr->CreditCharge;
bed9da02
PS
2666 spin_lock(&server->req_lock);
2667 server->credits += rdata->credits -
31473fc4 2668 le16_to_cpu(shdr->CreditCharge);
bed9da02
PS
2669 spin_unlock(&server->req_lock);
2670 wake_up(&server->request_q);
7fb8986e 2671 flags |= CIFS_HAS_CREDITS;
bed9da02
PS
2672 }
2673
09a4707e 2674 kref_get(&rdata->refcount);
fec344e3 2675 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
09a4707e 2676 cifs_readv_receive, smb2_readv_callback,
4326ed2f 2677 smb3_handle_read_data, rdata, flags);
e5d04887 2678 if (rc) {
09a4707e 2679 kref_put(&rdata->refcount, cifs_readdata_release);
e5d04887
PS
2680 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2681 }
09a4707e
PS
2682
2683 cifs_small_buf_release(buf);
2684 return rc;
2685}
33319141 2686
d8e05039
PS
2687int
2688SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2689 unsigned int *nbytes, char **buf, int *buf_type)
2690{
2691 int resp_buftype, rc = -EACCES;
b8f57ee8 2692 struct smb2_read_plain_req *req = NULL;
d8e05039 2693 struct smb2_read_rsp *rsp = NULL;
31473fc4 2694 struct smb2_sync_hdr *shdr;
f5688a6d 2695 struct kvec iov[1];
da502f7d 2696 struct kvec rsp_iov;
738f9de5 2697 unsigned int total_len;
7fb8986e
PS
2698 int flags = CIFS_LOG_ERROR;
2699 struct cifs_ses *ses = io_parms->tcon->ses;
d8e05039
PS
2700
2701 *nbytes = 0;
2dabfd5b 2702 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
d8e05039
PS
2703 if (rc)
2704 return rc;
2705
7fb8986e
PS
2706 if (encryption_required(io_parms->tcon))
2707 flags |= CIFS_TRANSFORM_REQ;
2708
f5688a6d
RS
2709 iov[0].iov_base = (char *)req;
2710 iov[0].iov_len = total_len;
b8f57ee8 2711
f5688a6d 2712 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
b8f57ee8 2713 cifs_small_buf_release(req);
d8e05039 2714
da502f7d 2715 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
d8e05039 2716
a821df3f
RS
2717 if (rc) {
2718 if (rc != -ENODATA) {
2719 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2720 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2721 }
da502f7d 2722 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
a821df3f 2723 return rc == -ENODATA ? 0 : rc;
d8e05039
PS
2724 }
2725
a821df3f
RS
2726 *nbytes = le32_to_cpu(rsp->DataLength);
2727 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2728 (*nbytes > io_parms->length)) {
2729 cifs_dbg(FYI, "bad length %d for count %d\n",
2730 *nbytes, io_parms->length);
2731 rc = -EIO;
2732 *nbytes = 0;
d8e05039
PS
2733 }
2734
a821df3f
RS
2735 shdr = get_sync_hdr(rsp);
2736
d8e05039 2737 if (*buf) {
31473fc4 2738 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
da502f7d 2739 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
d8e05039 2740 } else if (resp_buftype != CIFS_NO_BUFFER) {
da502f7d 2741 *buf = rsp_iov.iov_base;
d8e05039
PS
2742 if (resp_buftype == CIFS_SMALL_BUFFER)
2743 *buf_type = CIFS_SMALL_BUFFER;
2744 else if (resp_buftype == CIFS_LARGE_BUFFER)
2745 *buf_type = CIFS_LARGE_BUFFER;
2746 }
2747 return rc;
2748}
2749
33319141
PS
2750/*
2751 * Check the mid_state and signature on received buffer (if any), and queue the
2752 * workqueue completion task.
2753 */
2754static void
2755smb2_writev_callback(struct mid_q_entry *mid)
2756{
2757 struct cifs_writedata *wdata = mid->callback_data;
2758 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2759 unsigned int written;
2760 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2761 unsigned int credits_received = 1;
2762
2763 switch (mid->mid_state) {
2764 case MID_RESPONSE_RECEIVED:
31473fc4 2765 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
33319141
PS
2766 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2767 if (wdata->result != 0)
2768 break;
2769
2770 written = le32_to_cpu(rsp->DataLength);
2771 /*
2772 * Mask off high 16 bits when bytes written as returned
2773 * by the server is greater than bytes requested by the
2774 * client. OS/2 servers are known to set incorrect
2775 * CountHigh values.
2776 */
2777 if (written > wdata->bytes)
2778 written &= 0xFFFF;
2779
2780 if (written < wdata->bytes)
2781 wdata->result = -ENOSPC;
2782 else
2783 wdata->bytes = written;
2784 break;
2785 case MID_REQUEST_SUBMITTED:
2786 case MID_RETRY_NEEDED:
2787 wdata->result = -EAGAIN;
2788 break;
2789 default:
2790 wdata->result = -EIO;
2791 break;
2792 }
db223a59
LL
2793#ifdef CONFIG_CIFS_SMB_DIRECT
2794 /*
2795 * If this wdata has a memory registered, the MR can be freed
2796 * The number of MRs available is limited, it's important to recover
2797 * used MR as soon as I/O is finished. Hold MR longer in the later
2798 * I/O process can possibly result in I/O deadlock due to lack of MR
2799 * to send request on I/O retry
2800 */
2801 if (wdata->mr) {
2802 smbd_deregister_mr(wdata->mr);
2803 wdata->mr = NULL;
2804 }
2805#endif
33319141
PS
2806 if (wdata->result)
2807 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2808
2809 queue_work(cifsiod_wq, &wdata->work);
2810 DeleteMidQEntry(mid);
2811 add_credits(tcon->ses->server, credits_received, 0);
2812}
2813
2814/* smb2_async_writev - send an async write, and set up mid to handle result */
2815int
4a5c80d7
SF
2816smb2_async_writev(struct cifs_writedata *wdata,
2817 void (*release)(struct kref *kref))
33319141 2818{
cb7e9eab 2819 int rc = -EACCES, flags = 0;
33319141 2820 struct smb2_write_req *req = NULL;
31473fc4 2821 struct smb2_sync_hdr *shdr;
33319141 2822 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
cb7e9eab 2823 struct TCP_Server_Info *server = tcon->ses->server;
738f9de5
PS
2824 struct kvec iov[2];
2825 struct smb_rqst rqst = { };
f5688a6d
RS
2826 unsigned int total_len;
2827 __be32 rfc1002_marker;
33319141 2828
f5688a6d 2829 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
cb7e9eab
PS
2830 if (rc) {
2831 if (rc == -EAGAIN && wdata->credits) {
2832 /* credits was reset by reconnect */
2833 wdata->credits = 0;
2834 /* reduce in_flight value since we won't send the req */
2835 spin_lock(&server->req_lock);
2836 server->in_flight--;
2837 spin_unlock(&server->req_lock);
2838 }
33319141 2839 goto async_writev_out;
cb7e9eab 2840 }
33319141 2841
7fb8986e
PS
2842 if (encryption_required(tcon))
2843 flags |= CIFS_TRANSFORM_REQ;
2844
f5688a6d 2845 shdr = (struct smb2_sync_hdr *)req;
31473fc4 2846 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
33319141
PS
2847
2848 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2849 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2850 req->WriteChannelInfoOffset = 0;
2851 req->WriteChannelInfoLength = 0;
2852 req->Channel = 0;
2853 req->Offset = cpu_to_le64(wdata->offset);
33319141 2854 req->DataOffset = cpu_to_le16(
f5688a6d 2855 offsetof(struct smb2_write_req, Buffer));
33319141 2856 req->RemainingBytes = 0;
db223a59
LL
2857#ifdef CONFIG_CIFS_SMB_DIRECT
2858 /*
2859 * If we want to do a server RDMA read, fill in and append
2860 * smbd_buffer_descriptor_v1 to the end of write request
2861 */
2862 if (server->rdma && wdata->bytes >=
2863 server->smbd_conn->rdma_readwrite_threshold) {
2864
2865 struct smbd_buffer_descriptor_v1 *v1;
2866 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2867
2868 wdata->mr = smbd_register_mr(
2869 server->smbd_conn, wdata->pages,
2870 wdata->nr_pages, wdata->tailsz,
2871 false, need_invalidate);
2872 if (!wdata->mr) {
2873 rc = -ENOBUFS;
2874 goto async_writev_out;
2875 }
2876 req->Length = 0;
2877 req->DataOffset = 0;
2878 req->RemainingBytes =
2026b06e 2879 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
db223a59
LL
2880 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2881 if (need_invalidate)
2882 req->Channel = SMB2_CHANNEL_RDMA_V1;
2883 req->WriteChannelInfoOffset =
2026b06e 2884 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
db223a59 2885 req->WriteChannelInfoLength =
2026b06e 2886 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
db223a59 2887 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
2888 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2889 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
2890 v1->length = cpu_to_le32(wdata->mr->mr->length);
db223a59
LL
2891 }
2892#endif
33319141 2893 /* 4 for rfc1002 length field and 1 for Buffer */
738f9de5 2894 iov[0].iov_len = 4;
f5688a6d
RS
2895 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2896 iov[0].iov_base = &rfc1002_marker;
2897 iov[1].iov_len = total_len - 1;
2898 iov[1].iov_base = (char *)req;
33319141 2899
738f9de5
PS
2900 rqst.rq_iov = iov;
2901 rqst.rq_nvec = 2;
eddb079d
JL
2902 rqst.rq_pages = wdata->pages;
2903 rqst.rq_npages = wdata->nr_pages;
2904 rqst.rq_pagesz = wdata->pagesz;
2905 rqst.rq_tailsz = wdata->tailsz;
db223a59
LL
2906#ifdef CONFIG_CIFS_SMB_DIRECT
2907 if (wdata->mr) {
2908 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
2909 rqst.rq_npages = 0;
2910 }
2911#endif
f96637be
JP
2912 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2913 wdata->offset, wdata->bytes);
33319141 2914
db223a59
LL
2915#ifdef CONFIG_CIFS_SMB_DIRECT
2916 /* For RDMA read, I/O size is in RemainingBytes not in Length */
2917 if (!wdata->mr)
2918 req->Length = cpu_to_le32(wdata->bytes);
2919#else
33319141 2920 req->Length = cpu_to_le32(wdata->bytes);
db223a59 2921#endif
33319141 2922
cb7e9eab 2923 if (wdata->credits) {
31473fc4 2924 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
cb7e9eab 2925 SMB2_MAX_BUFFER_SIZE));
31473fc4 2926 shdr->CreditRequest = shdr->CreditCharge;
cb7e9eab
PS
2927 spin_lock(&server->req_lock);
2928 server->credits += wdata->credits -
31473fc4 2929 le16_to_cpu(shdr->CreditCharge);
cb7e9eab
PS
2930 spin_unlock(&server->req_lock);
2931 wake_up(&server->request_q);
7fb8986e 2932 flags |= CIFS_HAS_CREDITS;
cb7e9eab
PS
2933 }
2934
33319141 2935 kref_get(&wdata->refcount);
9b7c18a2
PS
2936 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2937 wdata, flags);
33319141 2938
e5d04887 2939 if (rc) {
4a5c80d7 2940 kref_put(&wdata->refcount, release);
e5d04887
PS
2941 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2942 }
33319141 2943
33319141
PS
2944async_writev_out:
2945 cifs_small_buf_release(req);
33319141
PS
2946 return rc;
2947}
009d3443
PS
2948
2949/*
2950 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2951 * The length field from io_parms must be at least 1 and indicates a number of
2952 * elements with data to write that begins with position 1 in iov array. All
2953 * data length is specified by count.
2954 */
2955int
2956SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2957 unsigned int *nbytes, struct kvec *iov, int n_vec)
2958{
2959 int rc = 0;
2960 struct smb2_write_req *req = NULL;
2961 struct smb2_write_rsp *rsp = NULL;
2962 int resp_buftype;
da502f7d 2963 struct kvec rsp_iov;
7fb8986e 2964 int flags = 0;
f5688a6d 2965 unsigned int total_len;
da502f7d 2966
009d3443
PS
2967 *nbytes = 0;
2968
2969 if (n_vec < 1)
2970 return rc;
2971
f5688a6d
RS
2972 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2973 &total_len);
009d3443
PS
2974 if (rc)
2975 return rc;
2976
2977 if (io_parms->tcon->ses->server == NULL)
2978 return -ECONNABORTED;
2979
7fb8986e
PS
2980 if (encryption_required(io_parms->tcon))
2981 flags |= CIFS_TRANSFORM_REQ;
2982
f5688a6d 2983 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
009d3443
PS
2984
2985 req->PersistentFileId = io_parms->persistent_fid;
2986 req->VolatileFileId = io_parms->volatile_fid;
2987 req->WriteChannelInfoOffset = 0;
2988 req->WriteChannelInfoLength = 0;
2989 req->Channel = 0;
2990 req->Length = cpu_to_le32(io_parms->length);
2991 req->Offset = cpu_to_le64(io_parms->offset);
009d3443 2992 req->DataOffset = cpu_to_le16(
f5688a6d 2993 offsetof(struct smb2_write_req, Buffer));
009d3443
PS
2994 req->RemainingBytes = 0;
2995
2996 iov[0].iov_base = (char *)req;
f5688a6d
RS
2997 /* 1 for Buffer */
2998 iov[0].iov_len = total_len - 1;
009d3443 2999
f5688a6d
RS
3000 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3001 &resp_buftype, flags, &rsp_iov);
da502f7d
PS
3002 cifs_small_buf_release(req);
3003 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
009d3443
PS
3004
3005 if (rc) {
3006 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
f96637be 3007 cifs_dbg(VFS, "Send error in write = %d\n", rc);
e5d04887 3008 } else
009d3443 3009 *nbytes = le32_to_cpu(rsp->DataLength);
e5d04887
PS
3010
3011 free_rsp_buf(resp_buftype, rsp);
009d3443
PS
3012 return rc;
3013}
35143eb5 3014
d324f08d
PS
3015static unsigned int
3016num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3017{
3018 int len;
3019 unsigned int entrycount = 0;
3020 unsigned int next_offset = 0;
3021 FILE_DIRECTORY_INFO *entryptr;
3022
3023 if (bufstart == NULL)
3024 return 0;
3025
3026 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3027
3028 while (1) {
3029 entryptr = (FILE_DIRECTORY_INFO *)
3030 ((char *)entryptr + next_offset);
3031
3032 if ((char *)entryptr + size > end_of_buf) {
f96637be 3033 cifs_dbg(VFS, "malformed search entry would overflow\n");
d324f08d
PS
3034 break;
3035 }
3036
3037 len = le32_to_cpu(entryptr->FileNameLength);
3038 if ((char *)entryptr + len + size > end_of_buf) {
f96637be
JP
3039 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3040 end_of_buf);
d324f08d
PS
3041 break;
3042 }
3043
3044 *lastentry = (char *)entryptr;
3045 entrycount++;
3046
3047 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3048 if (!next_offset)
3049 break;
3050 }
3051
3052 return entrycount;
3053}
3054
3055/*
3056 * Readdir/FindFirst
3057 */
3058int
3059SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3060 u64 persistent_fid, u64 volatile_fid, int index,
3061 struct cifs_search_info *srch_inf)
3062{
3063 struct smb2_query_directory_req *req;
3064 struct smb2_query_directory_rsp *rsp = NULL;
3065 struct kvec iov[2];
da502f7d 3066 struct kvec rsp_iov;
d324f08d
PS
3067 int rc = 0;
3068 int len;
75fdfc84 3069 int resp_buftype = CIFS_NO_BUFFER;
d324f08d
PS
3070 unsigned char *bufptr;
3071 struct TCP_Server_Info *server;
3072 struct cifs_ses *ses = tcon->ses;
3073 __le16 asteriks = cpu_to_le16('*');
3074 char *end_of_smb;
3075 unsigned int output_size = CIFSMaxBufSize;
3076 size_t info_buf_size;
7fb8986e 3077 int flags = 0;
7c00c3a6 3078 unsigned int total_len;
d324f08d
PS
3079
3080 if (ses && (ses->server))
3081 server = ses->server;
3082 else
3083 return -EIO;
3084
7c00c3a6
RS
3085 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3086 &total_len);
d324f08d
PS
3087 if (rc)
3088 return rc;
3089
7fb8986e
PS
3090 if (encryption_required(tcon))
3091 flags |= CIFS_TRANSFORM_REQ;
3092
d324f08d
PS
3093 switch (srch_inf->info_level) {
3094 case SMB_FIND_FILE_DIRECTORY_INFO:
3095 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3096 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3097 break;
3098 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3099 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3100 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3101 break;
3102 default:
f96637be
JP
3103 cifs_dbg(VFS, "info level %u isn't supported\n",
3104 srch_inf->info_level);
d324f08d
PS
3105 rc = -EINVAL;
3106 goto qdir_exit;
3107 }
3108
3109 req->FileIndex = cpu_to_le32(index);
3110 req->PersistentFileId = persistent_fid;
3111 req->VolatileFileId = volatile_fid;
3112
3113 len = 0x2;
3114 bufptr = req->Buffer;
3115 memcpy(bufptr, &asteriks, len);
3116
3117 req->FileNameOffset =
7c00c3a6 3118 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
d324f08d
PS
3119 req->FileNameLength = cpu_to_le16(len);
3120 /*
3121 * BB could be 30 bytes or so longer if we used SMB2 specific
3122 * buffer lengths, but this is safe and close enough.
3123 */
3124 output_size = min_t(unsigned int, output_size, server->maxBuf);
3125 output_size = min_t(unsigned int, output_size, 2 << 15);
3126 req->OutputBufferLength = cpu_to_le32(output_size);
3127
3128 iov[0].iov_base = (char *)req;
7c00c3a6
RS
3129 /* 1 for Buffer */
3130 iov[0].iov_len = total_len - 1;
d324f08d
PS
3131
3132 iov[1].iov_base = (char *)(req->Buffer);
3133 iov[1].iov_len = len;
3134
7c00c3a6 3135 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
3136 cifs_small_buf_release(req);
3137 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
e5d04887 3138
d324f08d 3139 if (rc) {
31473fc4
PS
3140 if (rc == -ENODATA &&
3141 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
52755808
PS
3142 srch_inf->endOfSearch = true;
3143 rc = 0;
3144 }
d324f08d
PS
3145 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3146 goto qdir_exit;
3147 }
d324f08d
PS
3148
3149 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3150 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3151 info_buf_size);
3152 if (rc)
3153 goto qdir_exit;
3154
3155 srch_inf->unicode = true;
3156
3157 if (srch_inf->ntwrk_buf_start) {
3158 if (srch_inf->smallBuf)
3159 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3160 else
3161 cifs_buf_release(srch_inf->ntwrk_buf_start);
3162 }
3163 srch_inf->ntwrk_buf_start = (char *)rsp;
3164 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3165 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3166 /* 4 for rfc1002 length field */
3167 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3168 srch_inf->entries_in_buffer =
3169 num_entries(srch_inf->srch_entries_start, end_of_smb,
3170 &srch_inf->last_entry, info_buf_size);
3171 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
f96637be
JP
3172 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3173 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3174 srch_inf->srch_entries_start, srch_inf->last_entry);
d324f08d
PS
3175 if (resp_buftype == CIFS_LARGE_BUFFER)
3176 srch_inf->smallBuf = false;
3177 else if (resp_buftype == CIFS_SMALL_BUFFER)
3178 srch_inf->smallBuf = true;
3179 else
f96637be 3180 cifs_dbg(VFS, "illegal search buffer type\n");
d324f08d 3181
d324f08d
PS
3182 return rc;
3183
3184qdir_exit:
3185 free_rsp_buf(resp_buftype, rsp);
3186 return rc;
3187}
3188
35143eb5
PS
3189static int
3190send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
dac95340
SP
3191 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3192 u8 info_type, u32 additional_info, unsigned int num,
3193 void **data, unsigned int *size)
35143eb5
PS
3194{
3195 struct smb2_set_info_req *req;
3196 struct smb2_set_info_rsp *rsp = NULL;
3197 struct kvec *iov;
da502f7d 3198 struct kvec rsp_iov;
35143eb5
PS
3199 int rc = 0;
3200 int resp_buftype;
3201 unsigned int i;
35143eb5 3202 struct cifs_ses *ses = tcon->ses;
7fb8986e 3203 int flags = 0;
2fc803ef 3204 unsigned int total_len;
35143eb5 3205
68a6afa7 3206 if (!ses || !(ses->server))
35143eb5
PS
3207 return -EIO;
3208
3209 if (!num)
3210 return -EINVAL;
3211
3212 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3213 if (!iov)
3214 return -ENOMEM;
3215
2fc803ef 3216 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
35143eb5
PS
3217 if (rc) {
3218 kfree(iov);
3219 return rc;
3220 }
3221
7fb8986e
PS
3222 if (encryption_required(tcon))
3223 flags |= CIFS_TRANSFORM_REQ;
3224
2fc803ef 3225 req->sync_hdr.ProcessId = cpu_to_le32(pid);
c839ff24 3226
dac95340 3227 req->InfoType = info_type;
35143eb5
PS
3228 req->FileInfoClass = info_class;
3229 req->PersistentFileId = persistent_fid;
3230 req->VolatileFileId = volatile_fid;
dac95340 3231 req->AdditionalInformation = cpu_to_le32(additional_info);
35143eb5 3232
35143eb5 3233 req->BufferOffset =
2fc803ef 3234 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
35143eb5
PS
3235 req->BufferLength = cpu_to_le32(*size);
3236
35143eb5 3237 memcpy(req->Buffer, *data, *size);
2fc803ef 3238 total_len += *size;
35143eb5
PS
3239
3240 iov[0].iov_base = (char *)req;
2fc803ef
RS
3241 /* 1 for Buffer */
3242 iov[0].iov_len = total_len - 1;
35143eb5
PS
3243
3244 for (i = 1; i < num; i++) {
35143eb5
PS
3245 le32_add_cpu(&req->BufferLength, size[i]);
3246 iov[i].iov_base = (char *)data[i];
3247 iov[i].iov_len = size[i];
3248 }
3249
2fc803ef
RS
3250 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3251 &rsp_iov);
da502f7d
PS
3252 cifs_small_buf_release(req);
3253 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
35143eb5 3254
7d3fb24b 3255 if (rc != 0)
35143eb5 3256 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
7d3fb24b 3257
35143eb5
PS
3258 free_rsp_buf(resp_buftype, rsp);
3259 kfree(iov);
3260 return rc;
3261}
3262
3263int
3264SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3265 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3266{
3267 struct smb2_file_rename_info info;
3268 void **data;
3269 unsigned int size[2];
3270 int rc;
3271 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3272
3273 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3274 if (!data)
3275 return -ENOMEM;
3276
3277 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3278 /* 0 = fail if target already exists */
3279 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3280 info.FileNameLength = cpu_to_le32(len);
3281
3282 data[0] = &info;
3283 size[0] = sizeof(struct smb2_file_rename_info);
3284
3285 data[1] = target_file;
3286 size[1] = len + 2 /* null */;
3287
3288 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3289 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3290 0, 2, data, size);
35143eb5
PS
3291 kfree(data);
3292 return rc;
3293}
568798cc 3294
897fba11
SF
3295int
3296SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3297 u64 persistent_fid, u64 volatile_fid)
3298{
3299 __u8 delete_pending = 1;
3300 void *data;
3301 unsigned int size;
3302
3303 data = &delete_pending;
3304 size = 1; /* sizeof __u8 */
3305
3306 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3307 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3308 0, 1, &data, &size);
897fba11
SF
3309}
3310
568798cc
PS
3311int
3312SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3313 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3314{
3315 struct smb2_file_link_info info;
3316 void **data;
3317 unsigned int size[2];
3318 int rc;
3319 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3320
3321 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3322 if (!data)
3323 return -ENOMEM;
3324
3325 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3326 /* 0 = fail if link already exists */
3327 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3328 info.FileNameLength = cpu_to_le32(len);
3329
3330 data[0] = &info;
3331 size[0] = sizeof(struct smb2_file_link_info);
3332
3333 data[1] = target_file;
3334 size[1] = len + 2 /* null */;
3335
3336 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3337 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3338 0, 2, data, size);
568798cc
PS
3339 kfree(data);
3340 return rc;
3341}
c839ff24
PS
3342
3343int
3344SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
f29ebb47 3345 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
c839ff24
PS
3346{
3347 struct smb2_file_eof_info info;
3348 void *data;
3349 unsigned int size;
3350
3351 info.EndOfFile = *eof;
3352
3353 data = &info;
3354 size = sizeof(struct smb2_file_eof_info);
3355
f29ebb47
SF
3356 if (is_falloc)
3357 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3358 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3359 0, 1, &data, &size);
f29ebb47
SF
3360 else
3361 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3362 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3363 0, 1, &data, &size);
c839ff24 3364}
1feeaac7
PS
3365
3366int
3367SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3368 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3369{
3370 unsigned int size;
3371 size = sizeof(FILE_BASIC_INFO);
3372 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
3373 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3374 0, 1, (void **)&buf, &size);
3375}
3376
3377int
3378SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3379 u64 persistent_fid, u64 volatile_fid,
3380 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3381{
3382 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3383 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3384 1, (void **)&pnntsd, &pacllen);
1feeaac7 3385}
983c88a4 3386
5517554e
RS
3387int
3388SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3389 u64 persistent_fid, u64 volatile_fid,
3390 struct smb2_file_full_ea_info *buf, int len)
3391{
3392 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3393 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3394 0, 1, (void **)&buf, &len);
3395}
3396
983c88a4
PS
3397int
3398SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3399 const u64 persistent_fid, const u64 volatile_fid,
3400 __u8 oplock_level)
3401{
3402 int rc;
21ad9487
RS
3403 struct smb2_oplock_break_req *req = NULL;
3404 struct cifs_ses *ses = tcon->ses;
7fb8986e 3405 int flags = CIFS_OBREAK_OP;
21ad9487
RS
3406 unsigned int total_len;
3407 struct kvec iov[1];
3408 struct kvec rsp_iov;
3409 int resp_buf_type;
983c88a4 3410
f96637be 3411 cifs_dbg(FYI, "SMB2_oplock_break\n");
21ad9487
RS
3412 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3413 &total_len);
983c88a4
PS
3414 if (rc)
3415 return rc;
3416
7fb8986e
PS
3417 if (encryption_required(tcon))
3418 flags |= CIFS_TRANSFORM_REQ;
3419
983c88a4
PS
3420 req->VolatileFid = volatile_fid;
3421 req->PersistentFid = persistent_fid;
3422 req->OplockLevel = oplock_level;
21ad9487 3423 req->sync_hdr.CreditRequest = cpu_to_le16(1);
983c88a4 3424
21ad9487
RS
3425 flags |= CIFS_NO_RESP;
3426
3427 iov[0].iov_base = (char *)req;
3428 iov[0].iov_len = total_len;
3429
3430 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
da502f7d 3431 cifs_small_buf_release(req);
983c88a4
PS
3432
3433 if (rc) {
3434 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 3435 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
983c88a4
PS
3436 }
3437
3438 return rc;
3439}
6fc05c25
PS
3440
3441static void
3442copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3443 struct kstatfs *kst)
3444{
3445 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3446 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3447 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
42bec214
SP
3448 kst->f_bfree = kst->f_bavail =
3449 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
6fc05c25
PS
3450 return;
3451}
3452
3453static int
3454build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3455 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3456{
93012bf9 3457 struct TCP_Server_Info *server = tcon->ses->server;
6fc05c25
PS
3458 int rc;
3459 struct smb2_query_info_req *req;
b2fb7fec 3460 unsigned int total_len;
6fc05c25 3461
f96637be 3462 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
6fc05c25
PS
3463
3464 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3465 return -EIO;
3466
b2fb7fec
RS
3467 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3468 &total_len);
6fc05c25
PS
3469 if (rc)
3470 return rc;
3471
3472 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3473 req->FileInfoClass = level;
3474 req->PersistentFileId = persistent_fid;
3475 req->VolatileFileId = volatile_fid;
b2fb7fec 3476 /* 1 for pad */
6fc05c25 3477 req->InputBufferOffset =
b2fb7fec 3478 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
6fc05c25 3479 req->OutputBufferLength = cpu_to_le32(
93012bf9 3480 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
6fc05c25
PS
3481
3482 iov->iov_base = (char *)req;
b2fb7fec 3483 iov->iov_len = total_len;
6fc05c25
PS
3484 return 0;
3485}
3486
3487int
3488SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3489 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3490{
3491 struct smb2_query_info_rsp *rsp = NULL;
3492 struct kvec iov;
da502f7d 3493 struct kvec rsp_iov;
6fc05c25
PS
3494 int rc = 0;
3495 int resp_buftype;
3496 struct cifs_ses *ses = tcon->ses;
93012bf9 3497 struct TCP_Server_Info *server = ses->server;
6fc05c25 3498 struct smb2_fs_full_size_info *info = NULL;
7fb8986e 3499 int flags = 0;
6fc05c25
PS
3500
3501 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3502 sizeof(struct smb2_fs_full_size_info),
3503 persistent_fid, volatile_fid);
3504 if (rc)
3505 return rc;
3506
7fb8986e
PS
3507 if (encryption_required(tcon))
3508 flags |= CIFS_TRANSFORM_REQ;
3509
b2fb7fec 3510 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d 3511 cifs_small_buf_release(iov.iov_base);
6fc05c25
PS
3512 if (rc) {
3513 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
34f62640 3514 goto qfsinf_exit;
6fc05c25 3515 }
da502f7d 3516 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6fc05c25 3517
93012bf9 3518 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
6fc05c25
PS
3519 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3520 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3521 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3522 sizeof(struct smb2_fs_full_size_info));
3523 if (!rc)
3524 copy_fs_info_to_kstatfs(info, fsdata);
3525
34f62640 3526qfsinf_exit:
da502f7d 3527 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
34f62640
SF
3528 return rc;
3529}
3530
3531int
3532SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2167114c 3533 u64 persistent_fid, u64 volatile_fid, int level)
34f62640
SF
3534{
3535 struct smb2_query_info_rsp *rsp = NULL;
3536 struct kvec iov;
da502f7d 3537 struct kvec rsp_iov;
34f62640 3538 int rc = 0;
2167114c 3539 int resp_buftype, max_len, min_len;
34f62640 3540 struct cifs_ses *ses = tcon->ses;
93012bf9 3541 struct TCP_Server_Info *server = ses->server;
34f62640 3542 unsigned int rsp_len, offset;
7fb8986e 3543 int flags = 0;
34f62640 3544
2167114c
SF
3545 if (level == FS_DEVICE_INFORMATION) {
3546 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3547 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3548 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3549 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3550 min_len = MIN_FS_ATTR_INFO_SIZE;
af6a12ea
SF
3551 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3552 max_len = sizeof(struct smb3_fs_ss_info);
3553 min_len = sizeof(struct smb3_fs_ss_info);
2167114c 3554 } else {
af6a12ea 3555 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2167114c
SF
3556 return -EINVAL;
3557 }
3558
3559 rc = build_qfs_info_req(&iov, tcon, level, max_len,
34f62640
SF
3560 persistent_fid, volatile_fid);
3561 if (rc)
3562 return rc;
3563
7fb8986e
PS
3564 if (encryption_required(tcon))
3565 flags |= CIFS_TRANSFORM_REQ;
3566
b2fb7fec 3567 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
da502f7d 3568 cifs_small_buf_release(iov.iov_base);
34f62640
SF
3569 if (rc) {
3570 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3571 goto qfsattr_exit;
3572 }
da502f7d 3573 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
34f62640
SF
3574
3575 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3576 offset = le16_to_cpu(rsp->OutputBufferOffset);
2167114c
SF
3577 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3578 if (rc)
3579 goto qfsattr_exit;
3580
3581 if (level == FS_ATTRIBUTE_INFORMATION)
93012bf9 3582 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
34f62640 3583 + (char *)&rsp->hdr, min_t(unsigned int,
2167114c
SF
3584 rsp_len, max_len));
3585 else if (level == FS_DEVICE_INFORMATION)
93012bf9 3586 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
2167114c 3587 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
af6a12ea
SF
3588 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3589 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
93012bf9 3590 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
af6a12ea
SF
3591 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3592 tcon->perf_sector_size =
3593 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3594 }
34f62640
SF
3595
3596qfsattr_exit:
da502f7d 3597 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
6fc05c25
PS
3598 return rc;
3599}
f7ba7fe6
PS
3600
3601int
3602smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3603 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3604 const __u32 num_lock, struct smb2_lock_element *buf)
3605{
3606 int rc = 0;
3607 struct smb2_lock_req *req = NULL;
3608 struct kvec iov[2];
da502f7d 3609 struct kvec rsp_iov;
f7ba7fe6
PS
3610 int resp_buf_type;
3611 unsigned int count;
7fb8986e 3612 int flags = CIFS_NO_RESP;
ced93679 3613 unsigned int total_len;
f7ba7fe6 3614
f96637be 3615 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
f7ba7fe6 3616
ced93679 3617 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
f7ba7fe6
PS
3618 if (rc)
3619 return rc;
3620
7fb8986e
PS
3621 if (encryption_required(tcon))
3622 flags |= CIFS_TRANSFORM_REQ;
3623
ced93679 3624 req->sync_hdr.ProcessId = cpu_to_le32(pid);
f7ba7fe6
PS
3625 req->LockCount = cpu_to_le16(num_lock);
3626
3627 req->PersistentFileId = persist_fid;
3628 req->VolatileFileId = volatile_fid;
3629
3630 count = num_lock * sizeof(struct smb2_lock_element);
f7ba7fe6
PS
3631
3632 iov[0].iov_base = (char *)req;
ced93679 3633 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
f7ba7fe6
PS
3634 iov[1].iov_base = (char *)buf;
3635 iov[1].iov_len = count;
3636
3637 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
ced93679
RS
3638 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3639 &rsp_iov);
da502f7d 3640 cifs_small_buf_release(req);
f7ba7fe6 3641 if (rc) {
f96637be 3642 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
f7ba7fe6
PS
3643 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3644 }
3645
3646 return rc;
3647}
3648
3649int
3650SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3651 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3652 const __u64 length, const __u64 offset, const __u32 lock_flags,
3653 const bool wait)
3654{
3655 struct smb2_lock_element lock;
3656
3657 lock.Offset = cpu_to_le64(offset);
3658 lock.Length = cpu_to_le64(length);
3659 lock.Flags = cpu_to_le32(lock_flags);
3660 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3661 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3662
3663 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3664}
0822f514
PS
3665
3666int
3667SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3668 __u8 *lease_key, const __le32 lease_state)
3669{
3670 int rc;
3671 struct smb2_lease_ack *req = NULL;
8eb7998e 3672 struct cifs_ses *ses = tcon->ses;
7fb8986e 3673 int flags = CIFS_OBREAK_OP;
8eb7998e
RS
3674 unsigned int total_len;
3675 struct kvec iov[1];
3676 struct kvec rsp_iov;
3677 int resp_buf_type;
0822f514 3678
f96637be 3679 cifs_dbg(FYI, "SMB2_lease_break\n");
8eb7998e
RS
3680 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3681 &total_len);
0822f514
PS
3682 if (rc)
3683 return rc;
3684
7fb8986e
PS
3685 if (encryption_required(tcon))
3686 flags |= CIFS_TRANSFORM_REQ;
3687
8eb7998e 3688 req->sync_hdr.CreditRequest = cpu_to_le16(1);
0822f514 3689 req->StructureSize = cpu_to_le16(36);
8eb7998e 3690 total_len += 12;
0822f514
PS
3691
3692 memcpy(req->LeaseKey, lease_key, 16);
3693 req->LeaseState = lease_state;
3694
8eb7998e
RS
3695 flags |= CIFS_NO_RESP;
3696
3697 iov[0].iov_base = (char *)req;
3698 iov[0].iov_len = total_len;
3699
3700 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
da502f7d 3701 cifs_small_buf_release(req);
0822f514
PS
3702
3703 if (rc) {
3704 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 3705 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
0822f514
PS
3706 }
3707
3708 return rc;
3709}