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