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