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