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