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