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