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