cifs: when insecure legacy is disabled shrink amount of SMB1 code
[linux-block.git] / fs / cifs / smb2pdu.c
CommitLineData
929be906 1// SPDX-License-Identifier: LGPL-2.1
ec2e4523 2/*
ec2e4523 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 *
ec2e4523
PS
11 */
12
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
17
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/vfs.h>
09a4707e 21#include <linux/task_io_accounting_ops.h>
ec2e4523 22#include <linux/uaccess.h>
c6e970a0 23#include <linux/uuid.h>
33319141 24#include <linux/pagemap.h>
ec2e4523 25#include <linux/xattr.h>
ec2e4523
PS
26#include "cifsglob.h"
27#include "cifsacl.h"
28#include "cifsproto.h"
29#include "smb2proto.h"
30#include "cifs_unicode.h"
31#include "cifs_debug.h"
32#include "ntlmssp.h"
33#include "smb2status.h"
09a4707e 34#include "smb2glob.h"
d324f08d 35#include "cifspdu.h"
ceb1b0b9 36#include "cifs_spnego.h"
db223a59 37#include "smbdirect.h"
eccb4422 38#include "trace.h"
a3a53b76
PA
39#ifdef CONFIG_CIFS_DFS_UPCALL
40#include "dfs_cache.h"
41#endif
ec2e4523
PS
42
43/*
44 * The following table defines the expected "StructureSize" of SMB2 requests
45 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
46 *
47 * Note that commands are defined in smb2pdu.h in le16 but the array below is
48 * indexed by command in host byte order.
49 */
50static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
51 /* SMB2_NEGOTIATE */ 36,
52 /* SMB2_SESSION_SETUP */ 25,
53 /* SMB2_LOGOFF */ 4,
54 /* SMB2_TREE_CONNECT */ 9,
55 /* SMB2_TREE_DISCONNECT */ 4,
56 /* SMB2_CREATE */ 57,
57 /* SMB2_CLOSE */ 24,
58 /* SMB2_FLUSH */ 24,
59 /* SMB2_READ */ 49,
60 /* SMB2_WRITE */ 49,
61 /* SMB2_LOCK */ 48,
62 /* SMB2_IOCTL */ 57,
63 /* SMB2_CANCEL */ 4,
64 /* SMB2_ECHO */ 4,
65 /* SMB2_QUERY_DIRECTORY */ 33,
66 /* SMB2_CHANGE_NOTIFY */ 32,
67 /* SMB2_QUERY_INFO */ 41,
68 /* SMB2_SET_INFO */ 33,
69 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
70};
71
730928c8 72int smb3_encryption_required(const struct cifs_tcon *tcon)
7fb8986e 73{
edb16135 74 if (!tcon || !tcon->ses)
ae6f8dd4 75 return 0;
7fb8986e
PS
76 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
77 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
78 return 1;
ae6f8dd4
PS
79 if (tcon->seal &&
80 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
81 return 1;
7fb8986e
PS
82 return 0;
83}
ec2e4523
PS
84
85static void
0d35e382 86smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
352d96f3
AA
87 const struct cifs_tcon *tcon,
88 struct TCP_Server_Info *server)
ec2e4523 89{
31473fc4
PS
90 shdr->ProtocolId = SMB2_PROTO_NUMBER;
91 shdr->StructureSize = cpu_to_le16(64);
92 shdr->Command = smb2_cmd;
352d96f3 93 if (server) {
7d414f39 94 spin_lock(&server->req_lock);
69dc4b18 95 /* Request up to 10 credits but don't go over the limit. */
141891f4 96 if (server->credits >= server->max_credits)
31473fc4 97 shdr->CreditRequest = cpu_to_le16(0);
7d414f39 98 else
31473fc4 99 shdr->CreditRequest = cpu_to_le16(
141891f4 100 min_t(int, server->max_credits -
69dc4b18 101 server->credits, 10));
7d414f39
RL
102 spin_unlock(&server->req_lock);
103 } else {
31473fc4 104 shdr->CreditRequest = cpu_to_le16(2);
7d414f39 105 }
0d35e382 106 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
ec2e4523
PS
107
108 if (!tcon)
109 goto out;
110
2b80d049
SF
111 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
112 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
352d96f3 113 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
31473fc4 114 shdr->CreditCharge = cpu_to_le16(1);
2b80d049
SF
115 /* else CreditCharge MBZ */
116
0d35e382 117 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
ec2e4523
PS
118 /* Uid is not converted */
119 if (tcon->ses)
0d35e382 120 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
f87ab88b
SF
121
122 /*
123 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
124 * to pass the path on the Open SMB prefixed by \\server\share.
125 * Not sure when we would need to do the augmented path (if ever) and
126 * setting this flag breaks the SMB2 open operation since it is
127 * illegal to send an empty path name (without \\server\share prefix)
128 * when the DFS flag is set in the SMB open header. We could
129 * consider setting the flag on all operations other than open
130 * but it is safer to net set it for now.
131 */
132/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
31473fc4 133 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
f87ab88b 134
352d96f3 135 if (server && server->sign && !smb3_encryption_required(tcon))
31473fc4 136 shdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523 137out:
ec2e4523
PS
138 return;
139}
140
141static int
352d96f3
AA
142smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
143 struct TCP_Server_Info *server)
ec2e4523 144{
350f4a56 145 int rc = 0;
aa24d1e9
PS
146 struct nls_table *nls_codepage;
147 struct cifs_ses *ses;
a3a53b76 148 int retries;
aa24d1e9
PS
149
150 /*
151 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
152 * check for tcp and smb session status done differently
153 * for those three - in the calling routine.
154 */
155 if (tcon == NULL)
7ffbe655 156 return 0;
aa24d1e9 157
c88f7dcd
PA
158 /*
159 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
160 * cifs_tree_connect().
161 */
162 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
7ffbe655 163 return 0;
aa24d1e9 164
d7d7a66a 165 spin_lock(&tcon->tc_lock);
fdf59eb5 166 if (tcon->status == TID_EXITING) {
aa24d1e9
PS
167 /*
168 * only tree disconnect, open, and write,
169 * (and ulogoff which does not have tcon)
170 * are allowed as we start force umount.
171 */
172 if ((smb2_command != SMB2_WRITE) &&
173 (smb2_command != SMB2_CREATE) &&
174 (smb2_command != SMB2_TREE_DISCONNECT)) {
d7d7a66a 175 spin_unlock(&tcon->tc_lock);
f96637be
JP
176 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
177 smb2_command);
aa24d1e9
PS
178 return -ENODEV;
179 }
180 }
d7d7a66a 181 spin_unlock(&tcon->tc_lock);
dd3cd870 182 if ((!tcon->ses) || (tcon->ses->ses_status == SES_EXITING) ||
352d96f3 183 (!tcon->ses->server) || !server)
aa24d1e9
PS
184 return -EIO;
185
186 ses = tcon->ses;
a3a53b76
PA
187 retries = server->nr_targets;
188
aa24d1e9 189 /*
a3a53b76
PA
190 * Give demultiplex thread up to 10 seconds to each target available for
191 * reconnect -- should be greater than cifs socket timeout which is 7
192 * seconds.
aa24d1e9
PS
193 */
194 while (server->tcpStatus == CifsNeedReconnect) {
195 /*
196 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
197 * here since they are implicitly done when session drops.
198 */
199 switch (smb2_command) {
200 /*
201 * BB Should we keep oplock break and add flush to exceptions?
202 */
203 case SMB2_TREE_DISCONNECT:
204 case SMB2_CANCEL:
205 case SMB2_CLOSE:
206 case SMB2_OPLOCK_BREAK:
207 return -EAGAIN;
208 }
209
7ffbe655
PA
210 rc = wait_event_interruptible_timeout(server->response_q,
211 (server->tcpStatus != CifsNeedReconnect),
212 10 * HZ);
213 if (rc < 0) {
a0a3036b
JP
214 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
215 __func__);
7ffbe655
PA
216 return -ERESTARTSYS;
217 }
aa24d1e9
PS
218
219 /* are we still trying to reconnect? */
d7d7a66a 220 spin_lock(&server->srv_lock);
080dc5e5 221 if (server->tcpStatus != CifsNeedReconnect) {
d7d7a66a 222 spin_unlock(&server->srv_lock);
aa24d1e9 223 break;
080dc5e5 224 }
d7d7a66a 225 spin_unlock(&server->srv_lock);
aa24d1e9 226
c54849dd 227 if (retries && --retries)
a3a53b76
PA
228 continue;
229
aa24d1e9
PS
230 /*
231 * on "soft" mounts we wait once. Hard mounts keep
232 * retrying until process is killed or server comes
233 * back on-line
234 */
235 if (!tcon->retry) {
f96637be 236 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
aa24d1e9
PS
237 return -EHOSTDOWN;
238 }
a3a53b76 239 retries = server->nr_targets;
aa24d1e9
PS
240 }
241
d1a931ce
SP
242 spin_lock(&ses->chan_lock);
243 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
244 spin_unlock(&ses->chan_lock);
7ffbe655 245 return 0;
d1a931ce 246 }
88b024f5 247 spin_unlock(&ses->chan_lock);
d1a931ce
SP
248 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
249 tcon->ses->chans_need_reconnect,
250 tcon->need_reconnect);
aa24d1e9
PS
251
252 nls_codepage = load_nls_default();
253
76e75270
SC
254 /*
255 * Recheck after acquire mutex. If another thread is negotiating
256 * and the server never sends an answer the socket will be closed
257 * and tcpStatus set to reconnect.
258 */
d7d7a66a 259 spin_lock(&server->srv_lock);
76e75270 260 if (server->tcpStatus == CifsNeedReconnect) {
d7d7a66a 261 spin_unlock(&server->srv_lock);
76e75270 262 rc = -EHOSTDOWN;
76e75270
SC
263 goto out;
264 }
d7d7a66a 265 spin_unlock(&server->srv_lock);
76e75270 266
d1a931ce
SP
267 /*
268 * need to prevent multiple threads trying to simultaneously
269 * reconnect the same SMB session
270 */
271 spin_lock(&ses->chan_lock);
272 if (!cifs_chan_needs_reconnect(ses, server)) {
273 spin_unlock(&ses->chan_lock);
274
f486ef8e 275 /* this means that we only need to tree connect */
d1a931ce
SP
276 if (tcon->need_reconnect)
277 goto skip_sess_setup;
278
d1a931ce
SP
279 goto out;
280 }
281 spin_unlock(&ses->chan_lock);
282
73f9bfbe 283 mutex_lock(&ses->session_mutex);
f486ef8e 284 rc = cifs_negotiate_protocol(0, ses, server);
d1a931ce 285 if (!rc) {
f486ef8e 286 rc = cifs_setup_session(0, ses, server, nls_codepage);
b0dd940e 287 if ((rc == -EACCES) && !tcon->retry) {
f486ef8e 288 mutex_unlock(&ses->session_mutex);
73f9bfbe 289 rc = -EHOSTDOWN;
b0dd940e 290 goto failed;
8ea21823
SP
291 } else if (rc) {
292 mutex_unlock(&ses->session_mutex);
293 goto out;
b0dd940e 294 }
3663c904 295 } else {
73f9bfbe 296 mutex_unlock(&ses->session_mutex);
aa24d1e9
PS
297 goto out;
298 }
3663c904 299 mutex_unlock(&ses->session_mutex);
aa24d1e9 300
d1a931ce 301skip_sess_setup:
3663c904
SP
302 mutex_lock(&ses->session_mutex);
303 if (!tcon->need_reconnect) {
304 mutex_unlock(&ses->session_mutex);
305 goto out;
306 }
aa24d1e9 307 cifs_mark_open_files_invalid(tcon);
96a988ff
PS
308 if (tcon->use_persistent)
309 tcon->need_reopen_files = true;
52ace1ef 310
565674d6 311 rc = cifs_tree_connect(0, tcon, nls_codepage);
73f9bfbe 312 mutex_unlock(&ses->session_mutex);
52ace1ef 313
f96637be 314 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
c318e6c2
SF
315 if (rc) {
316 /* If sess reconnected but tcon didn't, something strange ... */
a0a3036b 317 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
aa24d1e9 318 goto out;
c318e6c2 319 }
96a988ff
PS
320
321 if (smb2_command != SMB2_INTERNAL_CMD)
b08484d7 322 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
96a988ff 323
aa24d1e9 324 atomic_inc(&tconInfoReconnectCount);
aa24d1e9
PS
325out:
326 /*
327 * Check if handle based operation so we know whether we can continue
328 * or not without returning to caller to reset file handle.
329 */
330 /*
331 * BB Is flush done by server on drop of tcp session? Should we special
332 * case it and skip above?
333 */
334 switch (smb2_command) {
335 case SMB2_FLUSH:
336 case SMB2_READ:
337 case SMB2_WRITE:
338 case SMB2_LOCK:
339 case SMB2_IOCTL:
340 case SMB2_QUERY_DIRECTORY:
341 case SMB2_CHANGE_NOTIFY:
342 case SMB2_QUERY_INFO:
343 case SMB2_SET_INFO:
4772c795 344 rc = -EAGAIN;
aa24d1e9 345 }
b0dd940e 346failed:
aa24d1e9 347 unload_nls(nls_codepage);
ec2e4523
PS
348 return rc;
349}
350
cb200bd6 351static void
352d96f3
AA
352fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
353 struct TCP_Server_Info *server,
354 void *buf,
cb200bd6
PS
355 unsigned int *total_len)
356{
0f46608a 357 struct smb2_pdu *spdu = buf;
cb200bd6
PS
358 /* lookup word count ie StructureSize from table */
359 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
360
361 /*
362 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
363 * largest operations (Create)
364 */
365 memset(buf, 0, 256);
366
0d35e382 367 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
cb200bd6
PS
368 spdu->StructureSize2 = cpu_to_le16(parmsize);
369
0d35e382 370 *total_len = parmsize + sizeof(struct smb2_hdr);
cb200bd6
PS
371}
372
ec2e4523
PS
373/*
374 * Allocate and return pointer to an SMB request hdr, and set basic
375 * SMB information in the SMB header. If the return code is zero, this
305428ac 376 * function must have filled in request_buf pointer.
ec2e4523 377 */
84a1f5b1 378static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
352d96f3
AA
379 struct TCP_Server_Info *server,
380 void **request_buf, unsigned int *total_len)
ec2e4523 381{
ec2e4523 382 /* BB eventually switch this to SMB2 specific small buf size */
f46ecbd9
SB
383 if (smb2_command == SMB2_SET_INFO)
384 *request_buf = cifs_buf_get();
385 else
386 *request_buf = cifs_small_buf_get();
ec2e4523
PS
387 if (*request_buf == NULL) {
388 /* BB should we add a retry in here if not a writepage? */
389 return -ENOMEM;
390 }
391
352d96f3 392 fill_small_buf(smb2_command, tcon, server,
0d35e382 393 (struct smb2_hdr *)(*request_buf),
305428ac 394 total_len);
ec2e4523
PS
395
396 if (tcon != NULL) {
ec2e4523
PS
397 uint16_t com_code = le16_to_cpu(smb2_command);
398 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
399 cifs_stats_inc(&tcon->num_smbs_sent);
400 }
401
84a1f5b1
PAS
402 return 0;
403}
404
405static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
352d96f3 406 struct TCP_Server_Info *server,
84a1f5b1
PAS
407 void **request_buf, unsigned int *total_len)
408{
409 int rc;
410
352d96f3 411 rc = smb2_reconnect(smb2_command, tcon, server);
84a1f5b1
PAS
412 if (rc)
413 return rc;
414
352d96f3 415 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
84a1f5b1
PAS
416 total_len);
417}
418
419static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
352d96f3 420 struct TCP_Server_Info *server,
84a1f5b1
PAS
421 void **request_buf, unsigned int *total_len)
422{
423 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
424 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
352d96f3
AA
425 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
426 request_buf, total_len);
84a1f5b1 427 }
352d96f3
AA
428 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
429 request_buf, total_len);
ec2e4523
PS
430}
431
d7bef4c4 432/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
ebb3a9d4
SF
433
434static void
435build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
436{
437 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
438 pneg_ctxt->DataLength = cpu_to_le16(38);
439 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
fc0b3844
RS
440 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
441 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
ebb3a9d4
SF
442 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
443}
444
26ea888f
SF
445static void
446build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
447{
448 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
449 pneg_ctxt->DataLength =
450 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
451 - sizeof(struct smb2_neg_context));
452 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
453 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
454 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
455 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
456}
457
53d31a3f
SF
458static unsigned int
459build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
460{
461 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
462 unsigned short num_algs = 1; /* number of signing algorithms sent */
463
464 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
465 /*
466 * Context Data length must be rounded to multiple of 8 for some servers
467 */
468 pneg_ctxt->DataLength = cpu_to_le16(DIV_ROUND_UP(
469 sizeof(struct smb2_signing_capabilities) -
470 sizeof(struct smb2_neg_context) +
471 (num_algs * 2 /* sizeof u16 */), 8) * 8);
472 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
473 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
474
475 ctxt_len += 2 /* sizeof le16 */ * num_algs;
476 ctxt_len = DIV_ROUND_UP(ctxt_len, 8) * 8;
477 return ctxt_len;
478 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
479}
480
ebb3a9d4
SF
481static void
482build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
483{
484 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
fbfd0b46
SF
485 if (require_gcm_256) {
486 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
487 pneg_ctxt->CipherCount = cpu_to_le16(1);
488 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
29e27923
SF
489 } else if (enable_gcm_256) {
490 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
491 pneg_ctxt->CipherCount = cpu_to_le16(3);
492 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
493 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
494 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
fbfd0b46
SF
495 } else {
496 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
497 pneg_ctxt->CipherCount = cpu_to_le16(2);
498 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
499 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
500 }
ebb3a9d4
SF
501}
502
96d3cca1
SF
503static unsigned int
504build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
505{
506 struct nls_table *cp = load_nls_default();
507
508 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
509
510 /* copy up to max of first 100 bytes of server name to NetName field */
df58fae7 511 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
96d3cca1
SF
512 /* context size is DataLength + minimal smb2_neg_context */
513 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
514 sizeof(struct smb2_neg_context), 8) * 8;
515}
516
fcef0db6
SF
517static void
518build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
519{
520 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
521 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
0d481325
SF
522 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
523 pneg_ctxt->Name[0] = 0x93;
524 pneg_ctxt->Name[1] = 0xAD;
525 pneg_ctxt->Name[2] = 0x25;
526 pneg_ctxt->Name[3] = 0x50;
527 pneg_ctxt->Name[4] = 0x9C;
528 pneg_ctxt->Name[5] = 0xB4;
529 pneg_ctxt->Name[6] = 0x11;
530 pneg_ctxt->Name[7] = 0xE7;
531 pneg_ctxt->Name[8] = 0xB4;
532 pneg_ctxt->Name[9] = 0x23;
533 pneg_ctxt->Name[10] = 0x83;
534 pneg_ctxt->Name[11] = 0xDE;
535 pneg_ctxt->Name[12] = 0x96;
536 pneg_ctxt->Name[13] = 0x8B;
537 pneg_ctxt->Name[14] = 0xCD;
538 pneg_ctxt->Name[15] = 0x7C;
fcef0db6
SF
539}
540
ebb3a9d4 541static void
13cacea7 542assemble_neg_contexts(struct smb2_negotiate_req *req,
9fe5ff1c 543 struct TCP_Server_Info *server, unsigned int *total_len)
ebb3a9d4 544{
a9f76cf8 545 char *pneg_ctxt;
9de74996 546 char *hostname = NULL;
53d31a3f 547 unsigned int ctxt_len, neg_context_count;
ebb3a9d4 548
d5c7076b
SF
549 if (*total_len > 200) {
550 /* In case length corrupted don't want to overrun smb buffer */
afe6f653 551 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
d5c7076b
SF
552 return;
553 }
554
555 /*
556 * round up total_len of fixed part of SMB3 negotiate request to 8
557 * byte boundary before adding negotiate contexts
558 */
559 *total_len = roundup(*total_len, 8);
560
561 pneg_ctxt = (*total_len) + (char *)req;
562 req->NegotiateContextOffset = cpu_to_le32(*total_len);
563
ebb3a9d4 564 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
fcef0db6
SF
565 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
566 *total_len += ctxt_len;
567 pneg_ctxt += ctxt_len;
13cacea7 568
ebb3a9d4 569 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
fcef0db6
SF
570 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
571 *total_len += ctxt_len;
572 pneg_ctxt += ctxt_len;
573
9de74996
SP
574 /*
575 * secondary channels don't have the hostname field populated
576 * use the hostname field in the primary channel instead
577 */
578 hostname = CIFS_SERVER_IS_CHAN(server) ?
579 server->primary_server->hostname : server->hostname;
580 if (hostname && (hostname[0] != 0)) {
73130a7b 581 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
9de74996 582 hostname);
73130a7b
SF
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
73130a7b 585 neg_context_count = 3;
32f31918
SF
586 } else
587 neg_context_count = 2;
588
589 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
590 *total_len += sizeof(struct smb2_posix_neg_context);
591 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
592 neg_context_count++;
53d31a3f 593
9fe5ff1c
SF
594 if (server->compress_algorithm) {
595 build_compression_ctxt((struct smb2_compression_capabilities_context *)
26ea888f 596 pneg_ctxt);
9fe5ff1c
SF
597 ctxt_len = DIV_ROUND_UP(
598 sizeof(struct smb2_compression_capabilities_context),
599 8) * 8;
600 *total_len += ctxt_len;
601 pneg_ctxt += ctxt_len;
53d31a3f
SF
602 neg_context_count++;
603 }
96d3cca1 604
53d31a3f
SF
605 if (enable_negotiate_signing) {
606 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
607 pneg_ctxt);
608 *total_len += ctxt_len;
609 pneg_ctxt += ctxt_len;
610 neg_context_count++;
611 }
612
613 /* check for and add transport_capabilities and signing capabilities */
614 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
96d3cca1 615
ebb3a9d4 616}
5100d8a3
SF
617
618static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
619{
620 unsigned int len = le16_to_cpu(ctxt->DataLength);
621
622 /* If invalid preauth context warn but use what we requested, SHA-512 */
623 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
a0a3036b 624 pr_warn_once("server sent bad preauth context\n");
5100d8a3 625 return;
7955f105
SF
626 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
627 pr_warn_once("server sent invalid SaltLength\n");
628 return;
5100d8a3
SF
629 }
630 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
a0a3036b 631 pr_warn_once("Invalid SMB3 hash algorithm count\n");
5100d8a3 632 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
a0a3036b 633 pr_warn_once("unknown SMB3 hash algorithm\n");
5100d8a3
SF
634}
635
26ea888f
SF
636static void decode_compress_ctx(struct TCP_Server_Info *server,
637 struct smb2_compression_capabilities_context *ctxt)
638{
639 unsigned int len = le16_to_cpu(ctxt->DataLength);
640
641 /* sizeof compress context is a one element compression capbility struct */
642 if (len < 10) {
a0a3036b 643 pr_warn_once("server sent bad compression cntxt\n");
26ea888f
SF
644 return;
645 }
646 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
a0a3036b 647 pr_warn_once("Invalid SMB3 compress algorithm count\n");
26ea888f
SF
648 return;
649 }
650 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
a0a3036b 651 pr_warn_once("unknown compression algorithm\n");
26ea888f
SF
652 return;
653 }
654 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
655}
656
5100d8a3
SF
657static int decode_encrypt_ctx(struct TCP_Server_Info *server,
658 struct smb2_encryption_neg_context *ctxt)
659{
660 unsigned int len = le16_to_cpu(ctxt->DataLength);
661
662 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
663 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
a0a3036b 664 pr_warn_once("server sent bad crypto ctxt len\n");
5100d8a3
SF
665 return -EINVAL;
666 }
667
668 if (le16_to_cpu(ctxt->CipherCount) != 1) {
a0a3036b 669 pr_warn_once("Invalid SMB3.11 cipher count\n");
5100d8a3
SF
670 return -EINVAL;
671 }
672 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
511ac89e
SF
673 if (require_gcm_256) {
674 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
675 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
676 return -EOPNOTSUPP;
677 }
678 } else if (ctxt->Ciphers[0] == 0) {
acf96fef
SF
679 /*
680 * e.g. if server only supported AES256_CCM (very unlikely)
681 * or server supported no encryption types or had all disabled.
682 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
683 * in which mount requested encryption ("seal") checks later
684 * on during tree connection will return proper rc, but if
685 * seal not requested by client, since server is allowed to
686 * return 0 to indicate no supported cipher, we can't fail here
687 */
688 server->cipher_type = 0;
689 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
690 pr_warn_once("Server does not support requested encryption types\n");
691 return 0;
511ac89e
SF
692 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
693 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
694 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
695 /* server returned a cipher we didn't ask for */
a0a3036b 696 pr_warn_once("Invalid SMB3.11 cipher returned\n");
5100d8a3
SF
697 return -EINVAL;
698 }
699 server->cipher_type = ctxt->Ciphers[0];
23657ad7 700 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
5100d8a3
SF
701 return 0;
702}
703
53d31a3f
SF
704static void decode_signing_ctx(struct TCP_Server_Info *server,
705 struct smb2_signing_capabilities *pctxt)
706{
707 unsigned int len = le16_to_cpu(pctxt->DataLength);
708
709 if ((len < 4) || (len > 16)) {
710 pr_warn_once("server sent bad signing negcontext\n");
711 return;
712 }
713 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
714 pr_warn_once("Invalid signing algorithm count\n");
715 return;
716 }
717 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
718 pr_warn_once("unknown signing algorithm\n");
719 return;
720 }
721
722 server->signing_negotiated = true;
723 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
724 cifs_dbg(FYI, "signing algorithm %d chosen\n",
725 server->signing_algorithm);
726}
727
728
5100d8a3 729static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
977b6170
RS
730 struct TCP_Server_Info *server,
731 unsigned int len_of_smb)
5100d8a3
SF
732{
733 struct smb2_neg_context *pctx;
734 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
735 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
5100d8a3
SF
736 unsigned int len_of_ctxts, i;
737 int rc = 0;
738
739 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
740 if (len_of_smb <= offset) {
afe6f653 741 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
5100d8a3
SF
742 return -EINVAL;
743 }
744
745 len_of_ctxts = len_of_smb - offset;
746
747 for (i = 0; i < ctxt_cnt; i++) {
748 int clen;
749 /* check that offset is not beyond end of SMB */
750 if (len_of_ctxts == 0)
751 break;
752
753 if (len_of_ctxts < sizeof(struct smb2_neg_context))
754 break;
755
1fc6ad2f 756 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
5100d8a3
SF
757 clen = le16_to_cpu(pctx->DataLength);
758 if (clen > len_of_ctxts)
759 break;
760
761 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
762 decode_preauth_context(
763 (struct smb2_preauth_neg_context *)pctx);
764 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
765 rc = decode_encrypt_ctx(server,
766 (struct smb2_encryption_neg_context *)pctx);
26ea888f
SF
767 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
768 decode_compress_ctx(server,
769 (struct smb2_compression_capabilities_context *)pctx);
fcef0db6
SF
770 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
771 server->posix_ext_supported = true;
53d31a3f
SF
772 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
773 decode_signing_ctx(server,
774 (struct smb2_signing_capabilities *)pctx);
5100d8a3 775 else
afe6f653 776 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
5100d8a3
SF
777 le16_to_cpu(pctx->ContextType));
778
779 if (rc)
780 break;
781 /* offsets must be 8 byte aligned */
782 clen = (clen + 7) & ~0x7;
783 offset += clen + sizeof(struct smb2_neg_context);
784 len_of_ctxts -= clen;
785 }
786 return rc;
787}
788
ce558b0e
SF
789static struct create_posix *
790create_posix_buf(umode_t mode)
791{
792 struct create_posix *buf;
793
794 buf = kzalloc(sizeof(struct create_posix),
795 GFP_KERNEL);
796 if (!buf)
797 return NULL;
798
799 buf->ccontext.DataOffset =
800 cpu_to_le16(offsetof(struct create_posix, Mode));
801 buf->ccontext.DataLength = cpu_to_le32(4);
802 buf->ccontext.NameOffset =
803 cpu_to_le16(offsetof(struct create_posix, Name));
804 buf->ccontext.NameLength = cpu_to_le16(16);
805
806 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
807 buf->Name[0] = 0x93;
808 buf->Name[1] = 0xAD;
809 buf->Name[2] = 0x25;
810 buf->Name[3] = 0x50;
811 buf->Name[4] = 0x9C;
812 buf->Name[5] = 0xB4;
813 buf->Name[6] = 0x11;
814 buf->Name[7] = 0xE7;
815 buf->Name[8] = 0xB4;
816 buf->Name[9] = 0x23;
817 buf->Name[10] = 0x83;
818 buf->Name[11] = 0xDE;
819 buf->Name[12] = 0x96;
820 buf->Name[13] = 0x8B;
821 buf->Name[14] = 0xCD;
822 buf->Name[15] = 0x7C;
823 buf->Mode = cpu_to_le32(mode);
a0a3036b 824 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
ce558b0e
SF
825 return buf;
826}
827
828static int
829add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
830{
831 struct smb2_create_req *req = iov[0].iov_base;
832 unsigned int num = *num_iovec;
833
834 iov[num].iov_base = create_posix_buf(mode);
d0959b08 835 if (mode == ACL_NO_MODE)
a0a3036b 836 cifs_dbg(FYI, "Invalid mode\n");
ce558b0e
SF
837 if (iov[num].iov_base == NULL)
838 return -ENOMEM;
839 iov[num].iov_len = sizeof(struct create_posix);
840 if (!req->CreateContextsOffset)
841 req->CreateContextsOffset = cpu_to_le32(
842 sizeof(struct smb2_create_req) +
843 iov[num - 1].iov_len);
844 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
845 *num_iovec = num + 1;
846 return 0;
847}
848
ebb3a9d4 849
ec2e4523
PS
850/*
851 *
852 * SMB2 Worker functions follow:
853 *
854 * The general structure of the worker functions is:
855 * 1) Call smb2_init (assembles SMB2 header)
856 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
857 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
858 * 4) Decode SMB2 command specific fields in the fixed length area
859 * 5) Decode variable length data area (if any for this SMB2 command type)
860 * 6) Call free smb buffer
861 * 7) return
862 *
863 */
864
865int
f486ef8e
SP
866SMB2_negotiate(const unsigned int xid,
867 struct cifs_ses *ses,
868 struct TCP_Server_Info *server)
ec2e4523 869{
40eff45b 870 struct smb_rqst rqst;
ec2e4523
PS
871 struct smb2_negotiate_req *req;
872 struct smb2_negotiate_rsp *rsp;
873 struct kvec iov[1];
da502f7d 874 struct kvec rsp_iov;
ec2e4523
PS
875 int rc = 0;
876 int resp_buftype;
ec2e4523
PS
877 int blob_offset, blob_length;
878 char *security_blob;
879 int flags = CIFS_NEG_OP;
13cacea7 880 unsigned int total_len;
ec2e4523 881
f96637be 882 cifs_dbg(FYI, "Negotiate protocol\n");
ec2e4523 883
3534b850
JL
884 if (!server) {
885 WARN(1, "%s: server is NULL!\n", __func__);
886 return -EIO;
ec2e4523
PS
887 }
888
352d96f3
AA
889 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
890 (void **) &req, &total_len);
ec2e4523
PS
891 if (rc)
892 return rc;
893
0d35e382 894 req->hdr.SessionId = 0;
0fdfef9a 895
8bd68c6e
AA
896 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
897 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
ec2e4523 898
f6a6bf7c 899 if (strcmp(server->vals->version_string,
9764c02f
SF
900 SMB3ANY_VERSION_STRING) == 0) {
901 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
902 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
6dffa4c2
SF
903 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
904 req->DialectCount = cpu_to_le16(3);
905 total_len += 6;
afe6f653 906 } else if (strcmp(server->vals->version_string,
9764c02f
SF
907 SMBDEFAULT_VERSION_STRING) == 0) {
908 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
909 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
910 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
d5c7076b
SF
911 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
912 req->DialectCount = cpu_to_le16(4);
913 total_len += 8;
9764c02f
SF
914 } else {
915 /* otherwise send specific dialect */
f6a6bf7c 916 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
9764c02f 917 req->DialectCount = cpu_to_le16(1);
13cacea7 918 total_len += 2;
9764c02f 919 }
ec2e4523
PS
920
921 /* only one of SMB2 signing flags may be set in SMB2 request */
38d77c50 922 if (ses->sign)
9cd2e62c 923 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
38d77c50 924 else if (global_secflags & CIFSSEC_MAY_SIGN)
9cd2e62c 925 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
38d77c50
JL
926 else
927 req->SecurityMode = 0;
ec2e4523 928
afe6f653 929 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
679971e7
SF
930 if (ses->chan_max > 1)
931 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
ec2e4523 932
3c5f9be1 933 /* ClientGUID must be zero for SMB2.02 dialect */
afe6f653 934 if (server->vals->protocol_id == SMB20_PROT_ID)
3c5f9be1 935 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
ebb3a9d4 936 else {
3c5f9be1
SF
937 memcpy(req->ClientGUID, server->client_guid,
938 SMB2_CLIENT_GUID_SIZE);
afe6f653 939 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
6dffa4c2
SF
940 (strcmp(server->vals->version_string,
941 SMB3ANY_VERSION_STRING) == 0) ||
afe6f653 942 (strcmp(server->vals->version_string,
d5c7076b 943 SMBDEFAULT_VERSION_STRING) == 0))
9fe5ff1c 944 assemble_neg_contexts(req, server, &total_len);
ebb3a9d4 945 }
ec2e4523 946 iov[0].iov_base = (char *)req;
13cacea7 947 iov[0].iov_len = total_len;
ec2e4523 948
40eff45b
RS
949 memset(&rqst, 0, sizeof(struct smb_rqst));
950 rqst.rq_iov = iov;
951 rqst.rq_nvec = 1;
952
352d96f3
AA
953 rc = cifs_send_recv(xid, ses, server,
954 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
955 cifs_small_buf_release(req);
956 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
ec2e4523
PS
957 /*
958 * No tcon so can't do
959 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
960 */
7e682f76 961 if (rc == -EOPNOTSUPP) {
a0a3036b 962 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
7e682f76
SF
963 goto neg_exit;
964 } else if (rc != 0)
ec2e4523
PS
965 goto neg_exit;
966
afe6f653 967 if (strcmp(server->vals->version_string,
9764c02f
SF
968 SMB3ANY_VERSION_STRING) == 0) {
969 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
afe6f653 970 cifs_server_dbg(VFS,
9764c02f
SF
971 "SMB2 dialect returned but not requested\n");
972 return -EIO;
973 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
afe6f653 974 cifs_server_dbg(VFS,
9764c02f
SF
975 "SMB2.1 dialect returned but not requested\n");
976 return -EIO;
6dffa4c2
SF
977 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
978 /* ops set to 3.0 by default for default so update */
979 server->ops = &smb311_operations;
980 server->vals = &smb311_values;
9764c02f 981 }
afe6f653 982 } else if (strcmp(server->vals->version_string,
9764c02f
SF
983 SMBDEFAULT_VERSION_STRING) == 0) {
984 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
afe6f653 985 cifs_server_dbg(VFS,
9764c02f
SF
986 "SMB2 dialect returned but not requested\n");
987 return -EIO;
988 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
989 /* ops set to 3.0 by default for default so update */
afe6f653
RS
990 server->ops = &smb21_operations;
991 server->vals = &smb21_values;
b57a55e2 992 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
afe6f653
RS
993 server->ops = &smb311_operations;
994 server->vals = &smb311_values;
b57a55e2 995 }
590d08d3 996 } else if (le16_to_cpu(rsp->DialectRevision) !=
afe6f653 997 server->vals->protocol_id) {
9764c02f 998 /* if requested single dialect ensure returned dialect matched */
a0a3036b
JP
999 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1000 le16_to_cpu(rsp->DialectRevision));
9764c02f
SF
1001 return -EIO;
1002 }
1003
f96637be 1004 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
ec2e4523 1005
e4aa25e7 1006 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
f96637be 1007 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
e4aa25e7 1008 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
f96637be 1009 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
e4aa25e7 1010 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
f96637be 1011 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
20b6d8b4
SF
1012 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1013 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
5f7fbf73
SF
1014 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1015 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
ec2e4523 1016 else {
a0a3036b
JP
1017 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1018 le16_to_cpu(rsp->DialectRevision));
ec2e4523
PS
1019 rc = -EIO;
1020 goto neg_exit;
1021 }
1022 server->dialect = le16_to_cpu(rsp->DialectRevision);
1023
8bd68c6e
AA
1024 /*
1025 * Keep a copy of the hash after negprot. This hash will be
1026 * the starting hash value for all sessions made from this
1027 * server.
1028 */
1029 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1030 SMB2_PREAUTH_HASH_SIZE);
0fdfef9a 1031
e598d1d8
JL
1032 /* SMB2 only has an extended negflavor */
1033 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
2365c4ea
PS
1034 /* set it to the maximum buffer size value we can send with 1 credit */
1035 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1036 SMB2_MAX_BUFFER_SIZE);
ec2e4523
PS
1037 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1038 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
ec2e4523 1039 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
07108d0e
SF
1040 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1041 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1042 server->sec_mode);
ec2e4523 1043 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
1044 /* Internal types */
1045 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523 1046
6d2fcfe6
AA
1047 /*
1048 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1049 * Set the cipher type manually.
1050 */
1051 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1052 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1053
ec2e4523 1054 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
0d35e382 1055 (struct smb2_hdr *)rsp);
5d875cc9
SF
1056 /*
1057 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1058 * for us will be
1059 * ses->sectype = RawNTLMSSP;
1060 * but for time being this is our only auth choice so doesn't matter.
1061 * We just found a server which sets blob length to zero expecting raw.
1062 */
67dbea2c 1063 if (blob_length == 0) {
5d875cc9 1064 cifs_dbg(FYI, "missing security blob on negprot\n");
67dbea2c
PS
1065 server->sec_ntlmssp = true;
1066 }
3c1bf7e4 1067
38d77c50 1068 rc = cifs_enable_signing(server, ses->sign);
9ddec561
JL
1069 if (rc)
1070 goto neg_exit;
ceb1b0b9 1071 if (blob_length) {
ebdd207e 1072 rc = decode_negTokenInit(security_blob, blob_length, server);
ceb1b0b9
SF
1073 if (rc == 1)
1074 rc = 0;
1075 else if (rc == 0)
1076 rc = -EIO;
ec2e4523 1077 }
5100d8a3 1078
5100d8a3
SF
1079 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1080 if (rsp->NegotiateContextCount)
977b6170
RS
1081 rc = smb311_decode_neg_context(rsp, server,
1082 rsp_iov.iov_len);
5100d8a3 1083 else
afe6f653 1084 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
5100d8a3 1085 }
ec2e4523
PS
1086neg_exit:
1087 free_rsp_buf(resp_buftype, rsp);
1088 return rc;
1089}
5478f9ba 1090
ff1c038a
SF
1091int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1092{
2796d303
LL
1093 int rc;
1094 struct validate_negotiate_info_req *pneg_inbuf;
fe83bebc 1095 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
ff1c038a 1096 u32 rsplen;
9764c02f 1097 u32 inbuflen; /* max of 4 dialects */
afe6f653 1098 struct TCP_Server_Info *server = tcon->ses->server;
ff1c038a
SF
1099
1100 cifs_dbg(FYI, "validate negotiate\n");
1101
8bd68c6e 1102 /* In SMB3.11 preauth integrity supersedes validate negotiate */
afe6f653 1103 if (server->dialect == SMB311_PROT_ID)
8bd68c6e
AA
1104 return 0;
1105
ff1c038a
SF
1106 /*
1107 * validation ioctl must be signed, so no point sending this if we
0603c96f
SF
1108 * can not sign it (ie are not known user). Even if signing is not
1109 * required (enabled but not negotiated), in those cases we selectively
ff1c038a 1110 * sign just this, the first and only signed request on a connection.
0603c96f 1111 * Having validation of negotiate info helps reduce attack vectors.
ff1c038a 1112 */
0603c96f 1113 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
ff1c038a
SF
1114 return 0; /* validation requires signing */
1115
0603c96f
SF
1116 if (tcon->ses->user_name == NULL) {
1117 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1118 return 0; /* validation requires signing */
1119 }
1120
1121 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
3175eb9b 1122 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
0603c96f 1123
2796d303
LL
1124 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1125 if (!pneg_inbuf)
1126 return -ENOMEM;
1127
1128 pneg_inbuf->Capabilities =
afe6f653 1129 cpu_to_le32(server->vals->req_capabilities);
679971e7
SF
1130 if (tcon->ses->chan_max > 1)
1131 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1132
afe6f653 1133 memcpy(pneg_inbuf->Guid, server->client_guid,
39552ea8 1134 SMB2_CLIENT_GUID_SIZE);
ff1c038a
SF
1135
1136 if (tcon->ses->sign)
2796d303 1137 pneg_inbuf->SecurityMode =
ff1c038a
SF
1138 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1139 else if (global_secflags & CIFSSEC_MAY_SIGN)
2796d303 1140 pneg_inbuf->SecurityMode =
ff1c038a
SF
1141 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1142 else
2796d303 1143 pneg_inbuf->SecurityMode = 0;
ff1c038a 1144
9764c02f 1145
afe6f653 1146 if (strcmp(server->vals->version_string,
9764c02f 1147 SMB3ANY_VERSION_STRING) == 0) {
2796d303
LL
1148 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1149 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
6dffa4c2
SF
1150 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1151 pneg_inbuf->DialectCount = cpu_to_le16(3);
1152 /* SMB 2.1 not included so subtract one dialect from len */
2796d303 1153 inbuflen = sizeof(*pneg_inbuf) -
6dffa4c2 1154 (sizeof(pneg_inbuf->Dialects[0]));
afe6f653 1155 } else if (strcmp(server->vals->version_string,
9764c02f 1156 SMBDEFAULT_VERSION_STRING) == 0) {
2796d303
LL
1157 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1158 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1159 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
d5c7076b
SF
1160 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1161 pneg_inbuf->DialectCount = cpu_to_le16(4);
6dffa4c2 1162 /* structure is big enough for 4 dialects */
2796d303 1163 inbuflen = sizeof(*pneg_inbuf);
9764c02f
SF
1164 } else {
1165 /* otherwise specific dialect was requested */
2796d303 1166 pneg_inbuf->Dialects[0] =
afe6f653 1167 cpu_to_le16(server->vals->protocol_id);
2796d303 1168 pneg_inbuf->DialectCount = cpu_to_le16(1);
9764c02f 1169 /* structure is big enough for 3 dialects, sending only 1 */
2796d303
LL
1170 inbuflen = sizeof(*pneg_inbuf) -
1171 sizeof(pneg_inbuf->Dialects[0]) * 2;
9764c02f 1172 }
ff1c038a
SF
1173
1174 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1175 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
153322f7
SF
1176 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1177 (char **)&pneg_rsp, &rsplen);
969ae8e8
NJ
1178 if (rc == -EOPNOTSUPP) {
1179 /*
1180 * Old Windows versions or Netapp SMB server can return
1181 * not supported error. Client should accept it.
1182 */
3175eb9b 1183 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
21078203
CIK
1184 rc = 0;
1185 goto out_free_inbuf;
969ae8e8 1186 } else if (rc != 0) {
a0a3036b
JP
1187 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1188 rc);
2796d303
LL
1189 rc = -EIO;
1190 goto out_free_inbuf;
ff1c038a
SF
1191 }
1192
2796d303
LL
1193 rc = -EIO;
1194 if (rsplen != sizeof(*pneg_rsp)) {
a0a3036b
JP
1195 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1196 rsplen);
7db0a6ef
SF
1197
1198 /* relax check since Mac returns max bufsize allowed on ioctl */
2796d303
LL
1199 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1200 goto out_free_rsp;
ff1c038a
SF
1201 }
1202
1203 /* check validate negotiate info response matches what we got earlier */
afe6f653 1204 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
ff1c038a
SF
1205 goto vneg_out;
1206
afe6f653 1207 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
ff1c038a
SF
1208 goto vneg_out;
1209
1210 /* do not validate server guid because not saved at negprot time yet */
1211
1212 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
afe6f653 1213 SMB2_LARGE_FILES) != server->capabilities)
ff1c038a
SF
1214 goto vneg_out;
1215
1216 /* validate negotiate successful */
2796d303 1217 rc = 0;
ff1c038a 1218 cifs_dbg(FYI, "validate negotiate info successful\n");
2796d303 1219 goto out_free_rsp;
ff1c038a
SF
1220
1221vneg_out:
3175eb9b 1222 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
2796d303 1223out_free_rsp:
fe83bebc 1224 kfree(pneg_rsp);
2796d303
LL
1225out_free_inbuf:
1226 kfree(pneg_inbuf);
1227 return rc;
ff1c038a
SF
1228}
1229
ef65aaed
SP
1230enum securityEnum
1231smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1232{
1233 switch (requested) {
1234 case Kerberos:
1235 case RawNTLMSSP:
1236 return requested;
1237 case NTLMv2:
1238 return RawNTLMSSP;
1239 case Unspecified:
1240 if (server->sec_ntlmssp &&
1241 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1242 return RawNTLMSSP;
1243 if ((server->sec_kerberos || server->sec_mskerberos) &&
1244 (global_secflags & CIFSSEC_MAY_KRB5))
1245 return Kerberos;
df561f66 1246 fallthrough;
ef65aaed
SP
1247 default:
1248 return Unspecified;
1249 }
1250}
1251
3baf1a7b
SP
1252struct SMB2_sess_data {
1253 unsigned int xid;
1254 struct cifs_ses *ses;
f486ef8e 1255 struct TCP_Server_Info *server;
3baf1a7b
SP
1256 struct nls_table *nls_cp;
1257 void (*func)(struct SMB2_sess_data *);
1258 int result;
1259 u64 previous_session;
1260
1261 /* we will send the SMB in three pieces:
1262 * a fixed length beginning part, an optional
1263 * SPNEGO blob (which can be zero length), and a
1264 * last part which will include the strings
1265 * and rest of bcc area. This allows us to avoid
1266 * a large buffer 17K allocation
1267 */
1268 int buf0_type;
1269 struct kvec iov[2];
1270};
1271
1272static int
1273SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1274{
1275 int rc;
1276 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1277 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b 1278 struct smb2_sess_setup_req *req;
88ea5cb7 1279 unsigned int total_len;
f486ef8e 1280 bool is_binding = false;
3baf1a7b 1281
352d96f3
AA
1282 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1283 (void **) &req,
1284 &total_len);
3baf1a7b
SP
1285 if (rc)
1286 return rc;
1287
f486ef8e
SP
1288 spin_lock(&ses->chan_lock);
1289 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1290 spin_unlock(&ses->chan_lock);
1291
1292 if (is_binding) {
1293 req->hdr.SessionId = cpu_to_le64(ses->Suid);
0d35e382 1294 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
d70e9fa5
AA
1295 req->PreviousSessionId = 0;
1296 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
f486ef8e 1297 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
d70e9fa5
AA
1298 } else {
1299 /* First session, not a reauthenticate */
0d35e382 1300 req->hdr.SessionId = 0;
d70e9fa5
AA
1301 /*
1302 * if reconnect, we need to send previous sess id
1303 * otherwise it is 0
1304 */
d8d9de53 1305 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
d70e9fa5 1306 req->Flags = 0; /* MBZ */
f486ef8e
SP
1307 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1308 sess_data->previous_session);
d70e9fa5 1309 }
d409014e
SF
1310
1311 /* enough to enable echos and oplocks and one max size write */
0d35e382 1312 req->hdr.CreditRequest = cpu_to_le16(130);
3baf1a7b
SP
1313
1314 /* only one of SMB2 signing flags may be set in SMB2 request */
1315 if (server->sign)
1316 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1317 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1318 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1319 else
1320 req->SecurityMode = 0;
1321
8d33096a
SF
1322#ifdef CONFIG_CIFS_DFS_UPCALL
1323 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1324#else
3baf1a7b 1325 req->Capabilities = 0;
8d33096a
SF
1326#endif /* DFS_UPCALL */
1327
3baf1a7b
SP
1328 req->Channel = 0; /* MBZ */
1329
1330 sess_data->iov[0].iov_base = (char *)req;
88ea5cb7
RS
1331 /* 1 for pad */
1332 sess_data->iov[0].iov_len = total_len - 1;
3baf1a7b
SP
1333 /*
1334 * This variable will be used to clear the buffer
1335 * allocated above in case of any error in the calling function.
1336 */
1337 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1338
1339 return 0;
1340}
1341
1342static void
1343SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1344{
1345 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1346 sess_data->buf0_type = CIFS_NO_BUFFER;
1347}
1348
1349static int
1350SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1351{
1352 int rc;
40eff45b 1353 struct smb_rqst rqst;
3baf1a7b 1354 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
da502f7d 1355 struct kvec rsp_iov = { NULL, 0 };
3baf1a7b
SP
1356
1357 /* Testing shows that buffer offset must be at location of Buffer[0] */
1358 req->SecurityBufferOffset =
88ea5cb7 1359 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
3baf1a7b
SP
1360 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1361
40eff45b
RS
1362 memset(&rqst, 0, sizeof(struct smb_rqst));
1363 rqst.rq_iov = sess_data->iov;
1364 rqst.rq_nvec = 2;
3baf1a7b 1365
40eff45b
RS
1366 /* BB add code to build os and lm fields */
1367 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
f486ef8e 1368 sess_data->server,
40eff45b 1369 &rqst,
88ea5cb7 1370 &sess_data->buf0_type,
0f56db83 1371 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
da502f7d
PS
1372 cifs_small_buf_release(sess_data->iov[0].iov_base);
1373 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
3baf1a7b
SP
1374
1375 return rc;
1376}
1377
1378static int
1379SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1380{
1381 int rc = 0;
1382 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1383 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b 1384
cc391b69 1385 cifs_server_lock(server);
f6a6bf7c 1386 if (server->ops->generate_signingkey) {
f486ef8e 1387 rc = server->ops->generate_signingkey(ses, server);
3baf1a7b
SP
1388 if (rc) {
1389 cifs_dbg(FYI,
1390 "SMB3 session key generation failed\n");
cc391b69 1391 cifs_server_unlock(server);
cabfb368 1392 return rc;
3baf1a7b
SP
1393 }
1394 }
f6a6bf7c
AA
1395 if (!server->session_estab) {
1396 server->sequence_number = 0x2;
1397 server->session_estab = true;
3baf1a7b 1398 }
cc391b69 1399 cifs_server_unlock(server);
3baf1a7b
SP
1400
1401 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
3baf1a7b
SP
1402 return rc;
1403}
1404
1405#ifdef CONFIG_CIFS_UPCALL
1406static void
1407SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1408{
1409 int rc;
1410 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1411 struct TCP_Server_Info *server = sess_data->server;
3baf1a7b
SP
1412 struct cifs_spnego_msg *msg;
1413 struct key *spnego_key = NULL;
1414 struct smb2_sess_setup_rsp *rsp = NULL;
f486ef8e 1415 bool is_binding = false;
3baf1a7b
SP
1416
1417 rc = SMB2_sess_alloc_buffer(sess_data);
1418 if (rc)
1419 goto out;
1420
f486ef8e 1421 spnego_key = cifs_get_spnego_key(ses, server);
3baf1a7b
SP
1422 if (IS_ERR(spnego_key)) {
1423 rc = PTR_ERR(spnego_key);
0a018944
SF
1424 if (rc == -ENOKEY)
1425 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
3baf1a7b
SP
1426 spnego_key = NULL;
1427 goto out;
1428 }
1429
1430 msg = spnego_key->payload.data[0];
1431 /*
1432 * check version field to make sure that cifs.upcall is
1433 * sending us a response in an expected form
1434 */
1435 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
a0a3036b
JP
1436 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1437 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
3baf1a7b
SP
1438 rc = -EKEYREJECTED;
1439 goto out_put_spnego_key;
1440 }
1441
f486ef8e
SP
1442 spin_lock(&ses->chan_lock);
1443 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1444 spin_unlock(&ses->chan_lock);
1445
d70e9fa5 1446 /* keep session key if binding */
f486ef8e 1447 if (!is_binding) {
d70e9fa5
AA
1448 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1449 GFP_KERNEL);
1450 if (!ses->auth_key.response) {
a0a3036b 1451 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
d70e9fa5
AA
1452 msg->sesskey_len);
1453 rc = -ENOMEM;
1454 goto out_put_spnego_key;
1455 }
1456 ses->auth_key.len = msg->sesskey_len;
3baf1a7b 1457 }
3baf1a7b
SP
1458
1459 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1460 sess_data->iov[1].iov_len = msg->secblob_len;
1461
1462 rc = SMB2_sess_sendreceive(sess_data);
1463 if (rc)
1464 goto out_put_spnego_key;
1465
1466 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
d70e9fa5 1467 /* keep session id and flags if binding */
f486ef8e 1468 if (!is_binding) {
0d35e382 1469 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1470 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1471 }
3baf1a7b
SP
1472
1473 rc = SMB2_sess_establish_session(sess_data);
1474out_put_spnego_key:
1475 key_invalidate(spnego_key);
1476 key_put(spnego_key);
1477out:
1478 sess_data->result = rc;
1479 sess_data->func = NULL;
1480 SMB2_sess_free_buffer(sess_data);
1481}
1482#else
1483static void
1484SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1485{
1486 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1487 sess_data->result = -EOPNOTSUPP;
1488 sess_data->func = NULL;
1489}
1490#endif
1491
166cea4d
SP
1492static void
1493SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1494
1495static void
1496SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
5478f9ba 1497{
166cea4d
SP
1498 int rc;
1499 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1500 struct TCP_Server_Info *server = sess_data->server;
5478f9ba 1501 struct smb2_sess_setup_rsp *rsp = NULL;
49bd49f9 1502 unsigned char *ntlmssp_blob = NULL;
5478f9ba 1503 bool use_spnego = false; /* else use raw ntlmssp */
166cea4d 1504 u16 blob_length = 0;
f486ef8e 1505 bool is_binding = false;
d4e63bd6 1506
5478f9ba
PS
1507 /*
1508 * If memory allocation is successful, caller of this function
1509 * frees it.
1510 */
1511 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
166cea4d
SP
1512 if (!ses->ntlmssp) {
1513 rc = -ENOMEM;
1514 goto out_err;
1515 }
5c234aa5 1516 ses->ntlmssp->sesskey_per_smbsess = true;
5478f9ba 1517
166cea4d 1518 rc = SMB2_sess_alloc_buffer(sess_data);
5478f9ba 1519 if (rc)
166cea4d 1520 goto out_err;
5478f9ba 1521
52d00533 1522 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
f486ef8e 1523 &blob_length, ses, server,
49bd49f9
SP
1524 sess_data->nls_cp);
1525 if (rc)
1526 goto out_err;
c2afb814 1527
166cea4d
SP
1528 if (use_spnego) {
1529 /* BB eventually need to add this */
1530 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1531 rc = -EOPNOTSUPP;
1532 goto out;
166cea4d
SP
1533 }
1534 sess_data->iov[1].iov_base = ntlmssp_blob;
1535 sess_data->iov[1].iov_len = blob_length;
c2afb814 1536
166cea4d
SP
1537 rc = SMB2_sess_sendreceive(sess_data);
1538 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
5478f9ba 1539
166cea4d
SP
1540 /* If true, rc here is expected and not an error */
1541 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
0d35e382 1542 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
166cea4d 1543 rc = 0;
38d77c50 1544
166cea4d
SP
1545 if (rc)
1546 goto out;
5478f9ba 1547
1fc6ad2f 1548 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
166cea4d
SP
1549 le16_to_cpu(rsp->SecurityBufferOffset)) {
1550 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1551 le16_to_cpu(rsp->SecurityBufferOffset));
5478f9ba 1552 rc = -EIO;
166cea4d 1553 goto out;
5478f9ba 1554 }
166cea4d
SP
1555 rc = decode_ntlmssp_challenge(rsp->Buffer,
1556 le16_to_cpu(rsp->SecurityBufferLength), ses);
1557 if (rc)
1558 goto out;
5478f9ba 1559
166cea4d 1560 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
5478f9ba 1561
f486ef8e
SP
1562 spin_lock(&ses->chan_lock);
1563 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1564 spin_unlock(&ses->chan_lock);
1565
d70e9fa5 1566 /* keep existing ses id and flags if binding */
f486ef8e 1567 if (!is_binding) {
0d35e382 1568 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1569 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1570 }
166cea4d
SP
1571
1572out:
1573 kfree(ntlmssp_blob);
1574 SMB2_sess_free_buffer(sess_data);
1575 if (!rc) {
1576 sess_data->result = 0;
1577 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1578 return;
1579 }
1580out_err:
1581 kfree(ses->ntlmssp);
1582 ses->ntlmssp = NULL;
1583 sess_data->result = rc;
1584 sess_data->func = NULL;
1585}
5478f9ba 1586
166cea4d
SP
1587static void
1588SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1589{
1590 int rc;
1591 struct cifs_ses *ses = sess_data->ses;
f486ef8e 1592 struct TCP_Server_Info *server = sess_data->server;
166cea4d
SP
1593 struct smb2_sess_setup_req *req;
1594 struct smb2_sess_setup_rsp *rsp = NULL;
1595 unsigned char *ntlmssp_blob = NULL;
1596 bool use_spnego = false; /* else use raw ntlmssp */
1597 u16 blob_length = 0;
f486ef8e 1598 bool is_binding = false;
5478f9ba 1599
166cea4d
SP
1600 rc = SMB2_sess_alloc_buffer(sess_data);
1601 if (rc)
1602 goto out;
5478f9ba 1603
166cea4d 1604 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
0d35e382 1605 req->hdr.SessionId = cpu_to_le64(ses->Suid);
166cea4d 1606
f486ef8e
SP
1607 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1608 ses, server,
1609 sess_data->nls_cp);
166cea4d
SP
1610 if (rc) {
1611 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1612 goto out;
5478f9ba
PS
1613 }
1614
166cea4d
SP
1615 if (use_spnego) {
1616 /* BB eventually need to add this */
1617 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1618 rc = -EOPNOTSUPP;
1619 goto out;
1620 }
1621 sess_data->iov[1].iov_base = ntlmssp_blob;
1622 sess_data->iov[1].iov_len = blob_length;
5478f9ba 1623
166cea4d
SP
1624 rc = SMB2_sess_sendreceive(sess_data);
1625 if (rc)
1626 goto out;
1627
1628 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1629
f486ef8e
SP
1630 spin_lock(&ses->chan_lock);
1631 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1632 spin_unlock(&ses->chan_lock);
1633
d70e9fa5 1634 /* keep existing ses id and flags if binding */
f486ef8e 1635 if (!is_binding) {
0d35e382 1636 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
d70e9fa5
AA
1637 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1638 }
5478f9ba 1639
166cea4d 1640 rc = SMB2_sess_establish_session(sess_data);
f560cda9
RS
1641#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1642 if (ses->server->dialect < SMB30_PROT_ID) {
1643 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1644 /*
1645 * The session id is opaque in terms of endianness, so we can't
1646 * print it as a long long. we dump it as we got it on the wire
1647 */
1648 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1649 &ses->Suid);
1650 cifs_dbg(VFS, "Session Key %*ph\n",
1651 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1652 cifs_dbg(VFS, "Signing Key %*ph\n",
1653 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1654 }
1655#endif
166cea4d
SP
1656out:
1657 kfree(ntlmssp_blob);
1658 SMB2_sess_free_buffer(sess_data);
1659 kfree(ses->ntlmssp);
1660 ses->ntlmssp = NULL;
1661 sess_data->result = rc;
1662 sess_data->func = NULL;
1663}
d4e63bd6 1664
166cea4d 1665static int
f486ef8e 1666SMB2_select_sec(struct SMB2_sess_data *sess_data)
166cea4d 1667{
ef65aaed 1668 int type;
f486ef8e
SP
1669 struct cifs_ses *ses = sess_data->ses;
1670 struct TCP_Server_Info *server = sess_data->server;
ef65aaed 1671
f486ef8e 1672 type = smb2_select_sectype(server, ses->sectype);
ef65aaed
SP
1673 cifs_dbg(FYI, "sess setup type %d\n", type);
1674 if (type == Unspecified) {
a0a3036b 1675 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
ef65aaed
SP
1676 return -EINVAL;
1677 }
d4e63bd6 1678
ef65aaed 1679 switch (type) {
166cea4d
SP
1680 case Kerberos:
1681 sess_data->func = SMB2_auth_kerberos;
1682 break;
1683 case RawNTLMSSP:
1684 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1685 break;
1686 default:
ef65aaed 1687 cifs_dbg(VFS, "secType %d not supported!\n", type);
166cea4d 1688 return -EOPNOTSUPP;
d4e63bd6
SP
1689 }
1690
166cea4d
SP
1691 return 0;
1692}
1693
1694int
1695SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
f486ef8e 1696 struct TCP_Server_Info *server,
166cea4d
SP
1697 const struct nls_table *nls_cp)
1698{
1699 int rc = 0;
166cea4d
SP
1700 struct SMB2_sess_data *sess_data;
1701
1702 cifs_dbg(FYI, "Session Setup\n");
1703
1704 if (!server) {
1705 WARN(1, "%s: server is NULL!\n", __func__);
1706 return -EIO;
d4e63bd6 1707 }
d4e63bd6 1708
166cea4d
SP
1709 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1710 if (!sess_data)
1711 return -ENOMEM;
1712
166cea4d
SP
1713 sess_data->xid = xid;
1714 sess_data->ses = ses;
f486ef8e 1715 sess_data->server = server;
166cea4d
SP
1716 sess_data->buf0_type = CIFS_NO_BUFFER;
1717 sess_data->nls_cp = (struct nls_table *) nls_cp;
b2adf22f 1718 sess_data->previous_session = ses->Suid;
166cea4d 1719
f486ef8e
SP
1720 rc = SMB2_select_sec(sess_data);
1721 if (rc)
1722 goto out;
1723
8bd68c6e
AA
1724 /*
1725 * Initialize the session hash with the server one.
1726 */
f6a6bf7c 1727 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
8bd68c6e 1728 SMB2_PREAUTH_HASH_SIZE);
8bd68c6e 1729
166cea4d
SP
1730 while (sess_data->func)
1731 sess_data->func(sess_data);
1732
c721c389 1733 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
afe6f653 1734 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
3baf1a7b 1735 rc = sess_data->result;
166cea4d 1736out:
3baf1a7b 1737 kfree(sess_data);
5478f9ba
PS
1738 return rc;
1739}
1740
1741int
1742SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1743{
40eff45b 1744 struct smb_rqst rqst;
5478f9ba
PS
1745 struct smb2_logoff_req *req; /* response is also trivial struct */
1746 int rc = 0;
1747 struct TCP_Server_Info *server;
7fb8986e 1748 int flags = 0;
45305eda
RS
1749 unsigned int total_len;
1750 struct kvec iov[1];
1751 struct kvec rsp_iov;
1752 int resp_buf_type;
5478f9ba 1753
f96637be 1754 cifs_dbg(FYI, "disconnect session %p\n", ses);
5478f9ba
PS
1755
1756 if (ses && (ses->server))
1757 server = ses->server;
1758 else
1759 return -EIO;
1760
eb4c7df6 1761 /* no need to send SMB logoff if uid already closed due to reconnect */
d1a931ce
SP
1762 spin_lock(&ses->chan_lock);
1763 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1764 spin_unlock(&ses->chan_lock);
eb4c7df6 1765 goto smb2_session_already_dead;
d1a931ce
SP
1766 }
1767 spin_unlock(&ses->chan_lock);
eb4c7df6 1768
352d96f3
AA
1769 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1770 (void **) &req, &total_len);
5478f9ba
PS
1771 if (rc)
1772 return rc;
1773
1774 /* since no tcon, smb2_init can not do this, so do here */
0d35e382 1775 req->hdr.SessionId = cpu_to_le64(ses->Suid);
7fb8986e
PS
1776
1777 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1778 flags |= CIFS_TRANSFORM_REQ;
1779 else if (server->sign)
0d35e382 1780 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
45305eda 1781
392e1c5d 1782 flags |= CIFS_NO_RSP_BUF;
45305eda
RS
1783
1784 iov[0].iov_base = (char *)req;
1785 iov[0].iov_len = total_len;
5478f9ba 1786
40eff45b
RS
1787 memset(&rqst, 0, sizeof(struct smb_rqst));
1788 rqst.rq_iov = iov;
1789 rqst.rq_nvec = 1;
1790
352d96f3
AA
1791 rc = cifs_send_recv(xid, ses, ses->server,
1792 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 1793 cifs_small_buf_release(req);
5478f9ba
PS
1794 /*
1795 * No tcon so can't do
1796 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1797 */
eb4c7df6
SP
1798
1799smb2_session_already_dead:
5478f9ba
PS
1800 return rc;
1801}
faaf946a
PS
1802
1803static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1804{
d60622eb 1805 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
1806}
1807
1808#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1809
de9f68df
SF
1810/* These are similar values to what Windows uses */
1811static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1812{
1813 tcon->max_chunks = 256;
1814 tcon->max_bytes_chunk = 1048576;
1815 tcon->max_bytes_copy = 16777216;
1816}
1817
faaf946a
PS
1818int
1819SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1820 struct cifs_tcon *tcon, const struct nls_table *cp)
1821{
40eff45b 1822 struct smb_rqst rqst;
faaf946a
PS
1823 struct smb2_tree_connect_req *req;
1824 struct smb2_tree_connect_rsp *rsp = NULL;
1825 struct kvec iov[2];
db3b5474 1826 struct kvec rsp_iov = { NULL, 0 };
faaf946a
PS
1827 int rc = 0;
1828 int resp_buftype;
1829 int unc_path_len;
faaf946a 1830 __le16 *unc_path = NULL;
7fb8986e 1831 int flags = 0;
661bb943 1832 unsigned int total_len;
352d96f3
AA
1833 struct TCP_Server_Info *server;
1834
1835 /* always use master channel */
1836 server = ses->server;
faaf946a 1837
f96637be 1838 cifs_dbg(FYI, "TCON\n");
faaf946a 1839
afe6f653 1840 if (!server || !tree)
faaf946a
PS
1841 return -EIO;
1842
faaf946a
PS
1843 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1844 if (unc_path == NULL)
1845 return -ENOMEM;
1846
1847 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1848 unc_path_len *= 2;
1849 if (unc_path_len < 2) {
1850 kfree(unc_path);
1851 return -EINVAL;
1852 }
1853
806a28ef 1854 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
b327a717 1855 tcon->tid = 0;
fae8044c 1856 atomic_set(&tcon->num_remote_opens, 0);
352d96f3
AA
1857 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1858 (void **) &req, &total_len);
faaf946a
PS
1859 if (rc) {
1860 kfree(unc_path);
1861 return rc;
1862 }
1863
5a77e75f 1864 if (smb3_encryption_required(tcon))
ae6f8dd4 1865 flags |= CIFS_TRANSFORM_REQ;
faaf946a
PS
1866
1867 iov[0].iov_base = (char *)req;
661bb943
RS
1868 /* 1 for pad */
1869 iov[0].iov_len = total_len - 1;
faaf946a
PS
1870
1871 /* Testing shows that buffer offset must be at location of Buffer[0] */
1872 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
661bb943 1873 - 1 /* pad */);
faaf946a
PS
1874 req->PathLength = cpu_to_le16(unc_path_len - 2);
1875 iov[1].iov_base = unc_path;
1876 iov[1].iov_len = unc_path_len;
1877
e71ab2aa
RS
1878 /*
1879 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1880 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
8c11a607 1881 * (Samba servers don't always set the flag so also check if null user)
e71ab2aa 1882 */
afe6f653 1883 if ((server->dialect == SMB311_PROT_ID) &&
e71ab2aa 1884 !smb3_encryption_required(tcon) &&
8c11a607
SF
1885 !(ses->session_flags &
1886 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1887 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
0d35e382 1888 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
6188f28b 1889
40eff45b
RS
1890 memset(&rqst, 0, sizeof(struct smb_rqst));
1891 rqst.rq_iov = iov;
1892 rqst.rq_nvec = 2;
1893
4fe75c4e 1894 /* Need 64 for max size write so ask for more in case not there yet */
0d35e382 1895 req->hdr.CreditRequest = cpu_to_le16(64);
4fe75c4e 1896
352d96f3
AA
1897 rc = cifs_send_recv(xid, ses, server,
1898 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d
PS
1899 cifs_small_buf_release(req);
1900 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
f8af49dd 1901 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
bac35395 1902 if ((rc != 0) || (rsp == NULL)) {
3559134e
SF
1903 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1904 tcon->need_reconnect = true;
faaf946a
PS
1905 goto tcon_error_exit;
1906 }
1907
cd123007
CJ
1908 switch (rsp->ShareType) {
1909 case SMB2_SHARE_TYPE_DISK:
f96637be 1910 cifs_dbg(FYI, "connection to disk share\n");
cd123007
CJ
1911 break;
1912 case SMB2_SHARE_TYPE_PIPE:
b327a717 1913 tcon->pipe = true;
f96637be 1914 cifs_dbg(FYI, "connection to pipe share\n");
cd123007
CJ
1915 break;
1916 case SMB2_SHARE_TYPE_PRINT:
b327a717 1917 tcon->print = true;
f96637be 1918 cifs_dbg(FYI, "connection to printer\n");
cd123007
CJ
1919 break;
1920 default:
afe6f653 1921 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
faaf946a
PS
1922 rc = -EOPNOTSUPP;
1923 goto tcon_error_exit;
1924 }
1925
1926 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
769ee6a4 1927 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
faaf946a 1928 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
0d35e382 1929 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
46b51d08 1930 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
faaf946a
PS
1931
1932 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1933 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
3175eb9b 1934 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
ae6f8dd4
PS
1935
1936 if (tcon->seal &&
afe6f653 1937 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
3175eb9b 1938 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
ae6f8dd4 1939
de9f68df 1940 init_copy_chunk_defaults(tcon);
afe6f653
RS
1941 if (server->ops->validate_negotiate)
1942 rc = server->ops->validate_negotiate(xid, tcon);
faaf946a 1943tcon_exit:
f8af49dd 1944
faaf946a
PS
1945 free_rsp_buf(resp_buftype, rsp);
1946 kfree(unc_path);
1947 return rc;
1948
1949tcon_error_exit:
0d35e382 1950 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
3175eb9b 1951 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
faaf946a
PS
1952 goto tcon_exit;
1953}
1954
1955int
1956SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1957{
40eff45b 1958 struct smb_rqst rqst;
faaf946a
PS
1959 struct smb2_tree_disconnect_req *req; /* response is trivial */
1960 int rc = 0;
faaf946a 1961 struct cifs_ses *ses = tcon->ses;
7fb8986e 1962 int flags = 0;
4eecf4cf
RS
1963 unsigned int total_len;
1964 struct kvec iov[1];
1965 struct kvec rsp_iov;
1966 int resp_buf_type;
faaf946a 1967
f96637be 1968 cifs_dbg(FYI, "Tree Disconnect\n");
faaf946a 1969
68a6afa7 1970 if (!ses || !(ses->server))
faaf946a
PS
1971 return -EIO;
1972
d1a931ce
SP
1973 spin_lock(&ses->chan_lock);
1974 if ((tcon->need_reconnect) ||
1975 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
1976 spin_unlock(&ses->chan_lock);
faaf946a 1977 return 0;
d1a931ce
SP
1978 }
1979 spin_unlock(&ses->chan_lock);
faaf946a 1980
45c0f1aa 1981 close_cached_dir_lease(&tcon->crfid);
72e73c78 1982
352d96f3
AA
1983 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1984 (void **) &req,
1985 &total_len);
faaf946a
PS
1986 if (rc)
1987 return rc;
1988
5a77e75f 1989 if (smb3_encryption_required(tcon))
7fb8986e
PS
1990 flags |= CIFS_TRANSFORM_REQ;
1991
392e1c5d 1992 flags |= CIFS_NO_RSP_BUF;
4eecf4cf
RS
1993
1994 iov[0].iov_base = (char *)req;
1995 iov[0].iov_len = total_len;
1996
40eff45b
RS
1997 memset(&rqst, 0, sizeof(struct smb_rqst));
1998 rqst.rq_iov = iov;
1999 rqst.rq_nvec = 1;
2000
352d96f3
AA
2001 rc = cifs_send_recv(xid, ses, ses->server,
2002 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 2003 cifs_small_buf_release(req);
faaf946a
PS
2004 if (rc)
2005 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2006
2007 return rc;
2008}
2503a0db 2009
b8c32dbb 2010
63eb3def
PS
2011static struct create_durable *
2012create_durable_buf(void)
2013{
2014 struct create_durable *buf;
2015
2016 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2017 if (!buf)
2018 return NULL;
2019
2020 buf->ccontext.DataOffset = cpu_to_le16(offsetof
9cbc0b73 2021 (struct create_durable, Data));
63eb3def
PS
2022 buf->ccontext.DataLength = cpu_to_le32(16);
2023 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2024 (struct create_durable, Name));
2025 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 2026 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
63eb3def
PS
2027 buf->Name[0] = 'D';
2028 buf->Name[1] = 'H';
2029 buf->Name[2] = 'n';
2030 buf->Name[3] = 'Q';
2031 return buf;
2032}
2033
9cbc0b73
PS
2034static struct create_durable *
2035create_reconnect_durable_buf(struct cifs_fid *fid)
2036{
2037 struct create_durable *buf;
2038
2039 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2040 if (!buf)
2041 return NULL;
2042
2043 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2044 (struct create_durable, Data));
2045 buf->ccontext.DataLength = cpu_to_le32(16);
2046 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2047 (struct create_durable, Name));
2048 buf->ccontext.NameLength = cpu_to_le16(4);
2049 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2050 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
12197a7f 2051 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
9cbc0b73
PS
2052 buf->Name[0] = 'D';
2053 buf->Name[1] = 'H';
2054 buf->Name[2] = 'n';
2055 buf->Name[3] = 'C';
2056 return buf;
2057}
2058
89a5bfa3
SF
2059static void
2060parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2061{
2062 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2063
2064 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2065 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2066 buf->IndexNumber = pdisk_id->DiskFileId;
2067}
2068
ab3459d8 2069static void
69dda305
AA
2070parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2071 struct create_posix_rsp *posix)
ab3459d8 2072{
69dda305
AA
2073 int sid_len;
2074 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2075 u8 *end = beg + le32_to_cpu(cc->DataLength);
2076 u8 *sid;
ab3459d8 2077
69dda305
AA
2078 memset(posix, 0, sizeof(*posix));
2079
2080 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2081 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2082 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2083
2084 sid = beg + 12;
2085 sid_len = posix_info_sid_size(sid, end);
2086 if (sid_len < 0) {
2087 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2088 return;
2089 }
2090 memcpy(&posix->owner, sid, sid_len);
2091
2092 sid = sid + sid_len;
2093 sid_len = posix_info_sid_size(sid, end);
2094 if (sid_len < 0) {
2095 cifs_dbg(VFS, "bad group sid in posix create response\n");
2096 return;
2097 }
2098 memcpy(&posix->group, sid, sid_len);
2e8af978 2099
69dda305
AA
2100 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2101 posix->nlink, posix->mode, posix->reparse_tag);
ab3459d8
SF
2102}
2103
89a5bfa3
SF
2104void
2105smb2_parse_contexts(struct TCP_Server_Info *server,
69dda305
AA
2106 struct smb2_create_rsp *rsp,
2107 unsigned int *epoch, char *lease_key, __u8 *oplock,
2108 struct smb2_file_all_info *buf,
2109 struct create_posix_rsp *posix)
b8c32dbb
PS
2110{
2111 char *data_offset;
b5c7cde3 2112 struct create_context *cc;
deb7deff
JM
2113 unsigned int next;
2114 unsigned int remaining;
fd554396 2115 char *name;
3ece60e3
CIK
2116 static const char smb3_create_tag_posix[] = {
2117 0x93, 0xAD, 0x25, 0x50, 0x9C,
2118 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2119 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2120 };
b8c32dbb 2121
89a5bfa3 2122 *oplock = 0;
1fc6ad2f 2123 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
deb7deff 2124 remaining = le32_to_cpu(rsp->CreateContextsLength);
b5c7cde3 2125 cc = (struct create_context *)data_offset;
89a5bfa3
SF
2126
2127 /* Initialize inode number to 0 in case no valid data in qfid context */
2128 if (buf)
2129 buf->IndexNumber = 0;
2130
deb7deff 2131 while (remaining >= sizeof(struct create_context)) {
b5c7cde3 2132 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
deb7deff 2133 if (le16_to_cpu(cc->NameLength) == 4 &&
89a5bfa3
SF
2134 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2135 *oplock = server->ops->parse_lease_buf(cc, epoch,
2136 lease_key);
2137 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2138 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2139 parse_query_id_ctxt(cc, buf);
ab3459d8 2140 else if ((le16_to_cpu(cc->NameLength) == 16)) {
69dda305
AA
2141 if (posix &&
2142 memcmp(name, smb3_create_tag_posix, 16) == 0)
2143 parse_posix_ctxt(cc, buf, posix);
ab3459d8
SF
2144 }
2145 /* else {
2146 cifs_dbg(FYI, "Context not matched with len %d\n",
2147 le16_to_cpu(cc->NameLength));
2148 cifs_dump_mem("Cctxt name: ", name, 4);
2149 } */
deb7deff
JM
2150
2151 next = le32_to_cpu(cc->Next);
2152 if (!next)
2153 break;
2154 remaining -= next;
2155 cc = (struct create_context *)((char *)cc + next);
2156 }
b8c32dbb 2157
89a5bfa3
SF
2158 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2159 *oplock = rsp->OplockLevel;
2160
2161 return;
b8c32dbb
PS
2162}
2163
d22cbfec 2164static int
a41a28bd 2165add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
729c0c9d 2166 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
d22cbfec
PS
2167{
2168 struct smb2_create_req *req = iov[0].iov_base;
2169 unsigned int num = *num_iovec;
2170
729c0c9d 2171 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
d22cbfec
PS
2172 if (iov[num].iov_base == NULL)
2173 return -ENOMEM;
a41a28bd 2174 iov[num].iov_len = server->vals->create_lease_size;
d22cbfec
PS
2175 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2176 if (!req->CreateContextsOffset)
2177 req->CreateContextsOffset = cpu_to_le32(
4f33bc35 2178 sizeof(struct smb2_create_req) +
d22cbfec 2179 iov[num - 1].iov_len);
a41a28bd
PS
2180 le32_add_cpu(&req->CreateContextsLength,
2181 server->vals->create_lease_size);
d22cbfec
PS
2182 *num_iovec = num + 1;
2183 return 0;
2184}
2185
b56eae4d 2186static struct create_durable_v2 *
ca567eb2 2187create_durable_v2_buf(struct cifs_open_parms *oparms)
b56eae4d 2188{
ca567eb2 2189 struct cifs_fid *pfid = oparms->fid;
b56eae4d
SF
2190 struct create_durable_v2 *buf;
2191
2192 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2193 if (!buf)
2194 return NULL;
2195
2196 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2197 (struct create_durable_v2, dcontext));
2198 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2199 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2200 (struct create_durable_v2, Name));
2201 buf->ccontext.NameLength = cpu_to_le16(4);
2202
ca567eb2
SF
2203 /*
2204 * NB: Handle timeout defaults to 0, which allows server to choose
2205 * (most servers default to 120 seconds) and most clients default to 0.
2206 * This can be overridden at mount ("handletimeout=") if the user wants
2207 * a different persistent (or resilient) handle timeout for all opens
2208 * opens on a particular SMB3 mount.
2209 */
2210 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
b56eae4d 2211 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
fa70b87c 2212 generate_random_uuid(buf->dcontext.CreateGuid);
b56eae4d
SF
2213 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2214
2215 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2216 buf->Name[0] = 'D';
2217 buf->Name[1] = 'H';
2218 buf->Name[2] = '2';
2219 buf->Name[3] = 'Q';
2220 return buf;
2221}
2222
2223static struct create_durable_handle_reconnect_v2 *
2224create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2225{
2226 struct create_durable_handle_reconnect_v2 *buf;
2227
2228 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2229 GFP_KERNEL);
2230 if (!buf)
2231 return NULL;
2232
2233 buf->ccontext.DataOffset =
2234 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2235 dcontext));
2236 buf->ccontext.DataLength =
2237 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2238 buf->ccontext.NameOffset =
2239 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2240 Name));
2241 buf->ccontext.NameLength = cpu_to_le16(4);
2242
2243 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2244 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2245 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2246 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2247
2248 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2249 buf->Name[0] = 'D';
2250 buf->Name[1] = 'H';
2251 buf->Name[2] = '2';
2252 buf->Name[3] = 'C';
2253 return buf;
2254}
2255
63eb3def 2256static int
b56eae4d
SF
2257add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2258 struct cifs_open_parms *oparms)
2259{
2260 struct smb2_create_req *req = iov[0].iov_base;
2261 unsigned int num = *num_iovec;
2262
ca567eb2 2263 iov[num].iov_base = create_durable_v2_buf(oparms);
b56eae4d
SF
2264 if (iov[num].iov_base == NULL)
2265 return -ENOMEM;
2266 iov[num].iov_len = sizeof(struct create_durable_v2);
2267 if (!req->CreateContextsOffset)
2268 req->CreateContextsOffset =
4f33bc35 2269 cpu_to_le32(sizeof(struct smb2_create_req) +
b56eae4d
SF
2270 iov[1].iov_len);
2271 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
b56eae4d
SF
2272 *num_iovec = num + 1;
2273 return 0;
2274}
2275
2276static int
2277add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
9cbc0b73 2278 struct cifs_open_parms *oparms)
63eb3def
PS
2279{
2280 struct smb2_create_req *req = iov[0].iov_base;
2281 unsigned int num = *num_iovec;
2282
b56eae4d
SF
2283 /* indicate that we don't need to relock the file */
2284 oparms->reconnect = false;
2285
2286 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2287 if (iov[num].iov_base == NULL)
2288 return -ENOMEM;
2289 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2290 if (!req->CreateContextsOffset)
2291 req->CreateContextsOffset =
4f33bc35 2292 cpu_to_le32(sizeof(struct smb2_create_req) +
b56eae4d
SF
2293 iov[1].iov_len);
2294 le32_add_cpu(&req->CreateContextsLength,
2295 sizeof(struct create_durable_handle_reconnect_v2));
b56eae4d
SF
2296 *num_iovec = num + 1;
2297 return 0;
2298}
2299
2300static int
2301add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2302 struct cifs_open_parms *oparms, bool use_persistent)
2303{
2304 struct smb2_create_req *req = iov[0].iov_base;
2305 unsigned int num = *num_iovec;
2306
2307 if (use_persistent) {
2308 if (oparms->reconnect)
2309 return add_durable_reconnect_v2_context(iov, num_iovec,
2310 oparms);
2311 else
2312 return add_durable_v2_context(iov, num_iovec, oparms);
2313 }
2314
9cbc0b73
PS
2315 if (oparms->reconnect) {
2316 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2317 /* indicate that we don't need to relock the file */
2318 oparms->reconnect = false;
2319 } else
2320 iov[num].iov_base = create_durable_buf();
63eb3def
PS
2321 if (iov[num].iov_base == NULL)
2322 return -ENOMEM;
2323 iov[num].iov_len = sizeof(struct create_durable);
2324 if (!req->CreateContextsOffset)
2325 req->CreateContextsOffset =
4f33bc35 2326 cpu_to_le32(sizeof(struct smb2_create_req) +
63eb3def 2327 iov[1].iov_len);
31f92e9a 2328 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
63eb3def
PS
2329 *num_iovec = num + 1;
2330 return 0;
2331}
2332
cdeaf9d0
SF
2333/* See MS-SMB2 2.2.13.2.7 */
2334static struct crt_twarp_ctxt *
2335create_twarp_buf(__u64 timewarp)
2336{
2337 struct crt_twarp_ctxt *buf;
2338
2339 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2340 if (!buf)
2341 return NULL;
2342
2343 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2344 (struct crt_twarp_ctxt, Timestamp));
2345 buf->ccontext.DataLength = cpu_to_le32(8);
2346 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2347 (struct crt_twarp_ctxt, Name));
2348 buf->ccontext.NameLength = cpu_to_le16(4);
2349 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2350 buf->Name[0] = 'T';
2351 buf->Name[1] = 'W';
2352 buf->Name[2] = 'r';
2353 buf->Name[3] = 'p';
2354 buf->Timestamp = cpu_to_le64(timewarp);
2355 return buf;
2356}
2357
2358/* See MS-SMB2 2.2.13.2.7 */
2359static int
2360add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2361{
2362 struct smb2_create_req *req = iov[0].iov_base;
2363 unsigned int num = *num_iovec;
2364
2365 iov[num].iov_base = create_twarp_buf(timewarp);
2366 if (iov[num].iov_base == NULL)
2367 return -ENOMEM;
2368 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2369 if (!req->CreateContextsOffset)
2370 req->CreateContextsOffset = cpu_to_le32(
2371 sizeof(struct smb2_create_req) +
2372 iov[num - 1].iov_len);
2373 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2374 *num_iovec = num + 1;
2375 return 0;
2376}
2377
975221ec
SF
2378/* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2379static void setup_owner_group_sids(char *buf)
2380{
2381 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2382
2383 /* Populate the user ownership fields S-1-5-88-1 */
2384 sids->owner.Revision = 1;
2385 sids->owner.NumAuth = 3;
2386 sids->owner.Authority[5] = 5;
2387 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2388 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2389 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2390
2391 /* Populate the group ownership fields S-1-5-88-2 */
2392 sids->group.Revision = 1;
2393 sids->group.NumAuth = 3;
2394 sids->group.Authority[5] = 5;
2395 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2396 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2397 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
a7a519a4
SF
2398
2399 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
975221ec
SF
2400}
2401
fdef665b
SF
2402/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2403static struct crt_sd_ctxt *
975221ec 2404create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
fdef665b
SF
2405{
2406 struct crt_sd_ctxt *buf;
ea64370b
RS
2407 __u8 *ptr, *aclptr;
2408 unsigned int acelen, acl_size, ace_count;
975221ec
SF
2409 unsigned int owner_offset = 0;
2410 unsigned int group_offset = 0;
ea64370b 2411 struct smb3_acl acl;
975221ec 2412
ea64370b 2413 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
975221ec
SF
2414
2415 if (set_owner) {
975221ec
SF
2416 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2417 *len += sizeof(struct owner_group_sids);
2418 }
fdef665b 2419
fdef665b
SF
2420 buf = kzalloc(*len, GFP_KERNEL);
2421 if (buf == NULL)
2422 return buf;
2423
ea64370b 2424 ptr = (__u8 *)&buf[1];
975221ec 2425 if (set_owner) {
ea64370b
RS
2426 /* offset fields are from beginning of security descriptor not of create context */
2427 owner_offset = ptr - (__u8 *)&buf->sd;
975221ec 2428 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
ea64370b 2429 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
975221ec 2430 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
ea64370b
RS
2431
2432 setup_owner_group_sids(ptr);
2433 ptr += sizeof(struct owner_group_sids);
975221ec
SF
2434 } else {
2435 buf->sd.OffsetOwner = 0;
2436 buf->sd.OffsetGroup = 0;
2437 }
2438
ea64370b 2439 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
975221ec 2440 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
fdef665b
SF
2441 buf->ccontext.NameLength = cpu_to_le16(4);
2442 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2443 buf->Name[0] = 'S';
2444 buf->Name[1] = 'e';
2445 buf->Name[2] = 'c';
2446 buf->Name[3] = 'D';
2447 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
ea64370b 2448
fdef665b
SF
2449 /*
2450 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2451 * and "DP" ie the DACL is present
2452 */
2453 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2454
2455 /* offset owner, group and Sbz1 and SACL are all zero */
ea64370b
RS
2456 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2457 /* Ship the ACL for now. we will copy it into buf later. */
2458 aclptr = ptr;
b06d893e 2459 ptr += sizeof(struct smb3_acl);
fdef665b
SF
2460
2461 /* create one ACE to hold the mode embedded in reserved special SID */
ea64370b
RS
2462 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2463 ptr += acelen;
2464 acl_size = acelen + sizeof(struct smb3_acl);
2465 ace_count = 1;
975221ec
SF
2466
2467 if (set_owner) {
2468 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
ea64370b
RS
2469 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2470 ptr += acelen;
2471 acl_size += acelen;
2472 ace_count += 1;
2473 }
975221ec 2474
643fbcee 2475 /* and one more ACE to allow access for authenticated users */
ea64370b
RS
2476 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2477 ptr += acelen;
2478 acl_size += acelen;
2479 ace_count += 1;
2480
2481 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2482 acl.AclSize = cpu_to_le16(acl_size);
2483 acl.AceCount = cpu_to_le16(ace_count);
b06d893e 2484 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
ea64370b
RS
2485
2486 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
7d3fc017 2487 *len = roundup(ptr - (__u8 *)buf, 8);
975221ec 2488
fdef665b
SF
2489 return buf;
2490}
2491
2492static int
975221ec 2493add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
fdef665b
SF
2494{
2495 struct smb2_create_req *req = iov[0].iov_base;
2496 unsigned int num = *num_iovec;
2497 unsigned int len = 0;
2498
975221ec 2499 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
fdef665b
SF
2500 if (iov[num].iov_base == NULL)
2501 return -ENOMEM;
2502 iov[num].iov_len = len;
2503 if (!req->CreateContextsOffset)
2504 req->CreateContextsOffset = cpu_to_le32(
2505 sizeof(struct smb2_create_req) +
2506 iov[num - 1].iov_len);
2507 le32_add_cpu(&req->CreateContextsLength, len);
2508 *num_iovec = num + 1;
2509 return 0;
2510}
2511
ff2a09e9
SF
2512static struct crt_query_id_ctxt *
2513create_query_id_buf(void)
2514{
2515 struct crt_query_id_ctxt *buf;
2516
2517 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2518 if (!buf)
2519 return NULL;
2520
2521 buf->ccontext.DataOffset = cpu_to_le16(0);
2522 buf->ccontext.DataLength = cpu_to_le32(0);
2523 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2524 (struct crt_query_id_ctxt, Name));
2525 buf->ccontext.NameLength = cpu_to_le16(4);
2526 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2527 buf->Name[0] = 'Q';
2528 buf->Name[1] = 'F';
2529 buf->Name[2] = 'i';
2530 buf->Name[3] = 'd';
2531 return buf;
2532}
2533
2534/* See MS-SMB2 2.2.13.2.9 */
2535static int
2536add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2537{
2538 struct smb2_create_req *req = iov[0].iov_base;
2539 unsigned int num = *num_iovec;
2540
2541 iov[num].iov_base = create_query_id_buf();
2542 if (iov[num].iov_base == NULL)
2543 return -ENOMEM;
2544 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2545 if (!req->CreateContextsOffset)
2546 req->CreateContextsOffset = cpu_to_le32(
2547 sizeof(struct smb2_create_req) +
2548 iov[num - 1].iov_len);
2549 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2550 *num_iovec = num + 1;
2551 return 0;
2552}
2553
f0712928
AA
2554static int
2555alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2556 const char *treename, const __le16 *path)
2557{
2558 int treename_len, path_len;
2559 struct nls_table *cp;
2560 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2561
2562 /*
2563 * skip leading "\\"
2564 */
2565 treename_len = strlen(treename);
2566 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2567 return -EINVAL;
2568
2569 treename += 2;
2570 treename_len -= 2;
2571
2572 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2573
2574 /*
2575 * make room for one path separator between the treename and
2576 * path
2577 */
2578 *out_len = treename_len + 1 + path_len;
2579
2580 /*
2581 * final path needs to be null-terminated UTF16 with a
2582 * size aligned to 8
2583 */
2584
2585 *out_size = roundup((*out_len+1)*2, 8);
2586 *out_path = kzalloc(*out_size, GFP_KERNEL);
2587 if (!*out_path)
2588 return -ENOMEM;
2589
2590 cp = load_nls_default();
2591 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
7eacba3b
EK
2592
2593 /* Do not append the separator if the path is empty */
2594 if (path[0] != cpu_to_le16(0x0000)) {
2595 UniStrcat(*out_path, sep);
2596 UniStrcat(*out_path, path);
2597 }
2598
f0712928
AA
2599 unload_nls(cp);
2600
2601 return 0;
2602}
2603
bea851b8
SF
2604int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2605 umode_t mode, struct cifs_tcon *tcon,
2606 const char *full_path,
2607 struct cifs_sb_info *cifs_sb)
2608{
2609 struct smb_rqst rqst;
2610 struct smb2_create_req *req;
256b4c3f 2611 struct smb2_create_rsp *rsp = NULL;
bea851b8
SF
2612 struct cifs_ses *ses = tcon->ses;
2613 struct kvec iov[3]; /* make sure at least one for each open context */
2614 struct kvec rsp_iov = {NULL, 0};
2615 int resp_buftype;
2616 int uni_path_len;
2617 __le16 *copy_path = NULL;
2618 int copy_size;
2619 int rc = 0;
2620 unsigned int n_iov = 2;
2621 __u32 file_attributes = 0;
2622 char *pc_buf = NULL;
2623 int flags = 0;
2624 unsigned int total_len;
256b4c3f 2625 __le16 *utf16_path = NULL;
352d96f3 2626 struct TCP_Server_Info *server = cifs_pick_channel(ses);
bea851b8
SF
2627
2628 cifs_dbg(FYI, "mkdir\n");
2629
256b4c3f
AA
2630 /* resource #1: path allocation */
2631 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2632 if (!utf16_path)
2633 return -ENOMEM;
2634
352d96f3 2635 if (!ses || !server) {
256b4c3f
AA
2636 rc = -EIO;
2637 goto err_free_path;
2638 }
bea851b8 2639
256b4c3f 2640 /* resource #2: request */
352d96f3
AA
2641 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2642 (void **) &req, &total_len);
bea851b8 2643 if (rc)
256b4c3f
AA
2644 goto err_free_path;
2645
bea851b8
SF
2646
2647 if (smb3_encryption_required(tcon))
2648 flags |= CIFS_TRANSFORM_REQ;
2649
bea851b8
SF
2650 req->ImpersonationLevel = IL_IMPERSONATION;
2651 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2652 /* File attributes ignored on open (used in create though) */
2653 req->FileAttributes = cpu_to_le32(file_attributes);
2654 req->ShareAccess = FILE_SHARE_ALL_LE;
2655 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2656 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2657
2658 iov[0].iov_base = (char *)req;
2659 /* -1 since last byte is buf[0] which is sent below (path) */
2660 iov[0].iov_len = total_len - 1;
2661
2662 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2663
2664 /* [MS-SMB2] 2.2.13 NameOffset:
2665 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2666 * the SMB2 header, the file name includes a prefix that will
2667 * be processed during DFS name normalization as specified in
2668 * section 3.3.5.9. Otherwise, the file name is relative to
2669 * the share that is identified by the TreeId in the SMB2
2670 * header.
2671 */
2672 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2673 int name_len;
2674
0d35e382 2675 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
bea851b8
SF
2676 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2677 &name_len,
256b4c3f
AA
2678 tcon->treeName, utf16_path);
2679 if (rc)
2680 goto err_free_req;
2681
bea851b8
SF
2682 req->NameLength = cpu_to_le16(name_len * 2);
2683 uni_path_len = copy_size;
256b4c3f
AA
2684 /* free before overwriting resource */
2685 kfree(utf16_path);
2686 utf16_path = copy_path;
bea851b8 2687 } else {
256b4c3f 2688 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
bea851b8
SF
2689 /* MUST set path len (NameLength) to 0 opening root of share */
2690 req->NameLength = cpu_to_le16(uni_path_len - 2);
2691 if (uni_path_len % 8 != 0) {
2692 copy_size = roundup(uni_path_len, 8);
2693 copy_path = kzalloc(copy_size, GFP_KERNEL);
2694 if (!copy_path) {
256b4c3f
AA
2695 rc = -ENOMEM;
2696 goto err_free_req;
bea851b8 2697 }
256b4c3f 2698 memcpy((char *)copy_path, (const char *)utf16_path,
bea851b8
SF
2699 uni_path_len);
2700 uni_path_len = copy_size;
256b4c3f
AA
2701 /* free before overwriting resource */
2702 kfree(utf16_path);
2703 utf16_path = copy_path;
bea851b8
SF
2704 }
2705 }
2706
2707 iov[1].iov_len = uni_path_len;
256b4c3f 2708 iov[1].iov_base = utf16_path;
bea851b8
SF
2709 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2710
2711 if (tcon->posix_extensions) {
256b4c3f 2712 /* resource #3: posix buf */
bea851b8 2713 rc = add_posix_context(iov, &n_iov, mode);
256b4c3f
AA
2714 if (rc)
2715 goto err_free_req;
bea851b8
SF
2716 pc_buf = iov[n_iov-1].iov_base;
2717 }
2718
2719
2720 memset(&rqst, 0, sizeof(struct smb_rqst));
2721 rqst.rq_iov = iov;
2722 rqst.rq_nvec = n_iov;
2723
d2f15428 2724 /* no need to inc num_remote_opens because we close it just below */
efe2e9f3
SF
2725 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2726 FILE_WRITE_ATTRIBUTES);
256b4c3f 2727 /* resource #4: response buffer */
352d96f3
AA
2728 rc = cifs_send_recv(xid, ses, server,
2729 &rqst, &resp_buftype, flags, &rsp_iov);
256b4c3f 2730 if (rc) {
bea851b8
SF
2731 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2732 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
256b4c3f
AA
2733 CREATE_NOT_FILE,
2734 FILE_WRITE_ATTRIBUTES, rc);
2735 goto err_free_rsp_buf;
2736 }
2737
ca780da5
SF
2738 /*
2739 * Although unlikely to be possible for rsp to be null and rc not set,
2740 * adding check below is slightly safer long term (and quiets Coverity
2741 * warning)
2742 */
256b4c3f 2743 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
ca780da5
SF
2744 if (rsp == NULL) {
2745 rc = -EIO;
2746 kfree(pc_buf);
2747 goto err_free_req;
2748 }
2749
351a59da
PA
2750 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2751 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
bea851b8 2752
351a59da 2753 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
bea851b8
SF
2754
2755 /* Eventually save off posix specific response info and timestaps */
2756
256b4c3f 2757err_free_rsp_buf:
bea851b8 2758 free_rsp_buf(resp_buftype, rsp);
256b4c3f
AA
2759 kfree(pc_buf);
2760err_free_req:
2761 cifs_small_buf_release(req);
2762err_free_path:
2763 kfree(utf16_path);
bea851b8 2764 return rc;
bea851b8 2765}
bea851b8 2766
2503a0db 2767int
352d96f3
AA
2768SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2769 struct smb_rqst *rqst, __u8 *oplock,
1eb9fb52 2770 struct cifs_open_parms *oparms, __le16 *path)
2503a0db
PS
2771{
2772 struct smb2_create_req *req;
da502f7d 2773 unsigned int n_iov = 2;
ca81983f 2774 __u32 file_attributes = 0;
1eb9fb52
RS
2775 int copy_size;
2776 int uni_path_len;
4f33bc35 2777 unsigned int total_len;
1eb9fb52
RS
2778 struct kvec *iov = rqst->rq_iov;
2779 __le16 *copy_path;
2780 int rc;
2503a0db 2781
352d96f3
AA
2782 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2783 (void **) &req, &total_len);
2503a0db
PS
2784 if (rc)
2785 return rc;
2786
1eb9fb52
RS
2787 iov[0].iov_base = (char *)req;
2788 /* -1 since last byte is buf[0] which is sent below (path) */
2789 iov[0].iov_len = total_len - 1;
7fb8986e 2790
064f6047 2791 if (oparms->create_options & CREATE_OPTION_READONLY)
ca81983f 2792 file_attributes |= ATTR_READONLY;
db8b631d
SF
2793 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2794 file_attributes |= ATTR_SYSTEM;
ca81983f 2795
2503a0db 2796 req->ImpersonationLevel = IL_IMPERSONATION;
064f6047 2797 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2503a0db
PS
2798 /* File attributes ignored on open (used in create though) */
2799 req->FileAttributes = cpu_to_le32(file_attributes);
2800 req->ShareAccess = FILE_SHARE_ALL_LE;
c3ca78e2 2801
064f6047
PS
2802 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2803 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
4f33bc35 2804 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
f0712928
AA
2805
2806 /* [MS-SMB2] 2.2.13 NameOffset:
2807 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2808 * the SMB2 header, the file name includes a prefix that will
2809 * be processed during DFS name normalization as specified in
2810 * section 3.3.5.9. Otherwise, the file name is relative to
2811 * the share that is identified by the TreeId in the SMB2
2812 * header.
2813 */
2814 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2815 int name_len;
2816
0d35e382 2817 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
f0712928
AA
2818 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2819 &name_len,
2820 tcon->treeName, path);
1eb9fb52 2821 if (rc)
f0712928
AA
2822 return rc;
2823 req->NameLength = cpu_to_le16(name_len * 2);
59aa3718
PS
2824 uni_path_len = copy_size;
2825 path = copy_path;
f0712928
AA
2826 } else {
2827 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2828 /* MUST set path len (NameLength) to 0 opening root of share */
2829 req->NameLength = cpu_to_le16(uni_path_len - 2);
1eb9fb52
RS
2830 copy_size = uni_path_len;
2831 if (copy_size % 8 != 0)
2832 copy_size = roundup(copy_size, 8);
2833 copy_path = kzalloc(copy_size, GFP_KERNEL);
2834 if (!copy_path)
2835 return -ENOMEM;
2836 memcpy((char *)copy_path, (const char *)path,
2837 uni_path_len);
2838 uni_path_len = copy_size;
2839 path = copy_path;
2503a0db
PS
2840 }
2841
59aa3718
PS
2842 iov[1].iov_len = uni_path_len;
2843 iov[1].iov_base = path;
59aa3718 2844
3e7a02d4 2845 if ((!server->oplocks) || (tcon->no_lease))
b8c32dbb
PS
2846 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2847
a41a28bd 2848 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
b8c32dbb
PS
2849 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2850 req->RequestedOplockLevel = *oplock;
f8015683
SF
2851 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2852 (oparms->create_options & CREATE_NOT_FILE))
2853 req->RequestedOplockLevel = *oplock; /* no srv lease support */
b8c32dbb 2854 else {
729c0c9d
SB
2855 rc = add_lease_context(server, iov, &n_iov,
2856 oparms->fid->lease_key, oplock);
1eb9fb52 2857 if (rc)
d22cbfec 2858 return rc;
b8c32dbb
PS
2859 }
2860
63eb3def
PS
2861 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2862 /* need to set Next field of lease context if we request it */
a41a28bd 2863 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
63eb3def 2864 struct create_context *ccontext =
da502f7d 2865 (struct create_context *)iov[n_iov-1].iov_base;
1c46943f 2866 ccontext->Next =
a41a28bd 2867 cpu_to_le32(server->vals->create_lease_size);
63eb3def 2868 }
b56eae4d 2869
da502f7d 2870 rc = add_durable_context(iov, &n_iov, oparms,
b56eae4d 2871 tcon->use_persistent);
1eb9fb52 2872 if (rc)
63eb3def 2873 return rc;
63eb3def
PS
2874 }
2875
ce558b0e
SF
2876 if (tcon->posix_extensions) {
2877 if (n_iov > 2) {
2878 struct create_context *ccontext =
2879 (struct create_context *)iov[n_iov-1].iov_base;
2880 ccontext->Next =
2881 cpu_to_le32(iov[n_iov-1].iov_len);
2882 }
2883
2884 rc = add_posix_context(iov, &n_iov, oparms->mode);
1eb9fb52 2885 if (rc)
ce558b0e 2886 return rc;
ce558b0e 2887 }
ce558b0e 2888
cdeaf9d0
SF
2889 if (tcon->snapshot_time) {
2890 cifs_dbg(FYI, "adding snapshot context\n");
2891 if (n_iov > 2) {
2892 struct create_context *ccontext =
2893 (struct create_context *)iov[n_iov-1].iov_base;
2894 ccontext->Next =
2895 cpu_to_le32(iov[n_iov-1].iov_len);
2896 }
2897
2898 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2899 if (rc)
2900 return rc;
2901 }
2902
975221ec
SF
2903 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2904 bool set_mode;
2905 bool set_owner;
2906
2907 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2908 (oparms->mode != ACL_NO_MODE))
2909 set_mode = true;
2910 else {
2911 set_mode = false;
2912 oparms->mode = ACL_NO_MODE;
c3ca78e2
SF
2913 }
2914
975221ec
SF
2915 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2916 set_owner = true;
2917 else
2918 set_owner = false;
2919
2920 if (set_owner | set_mode) {
2921 if (n_iov > 2) {
2922 struct create_context *ccontext =
2923 (struct create_context *)iov[n_iov-1].iov_base;
2924 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2925 }
2926
2927 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2928 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2929 if (rc)
2930 return rc;
2931 }
c3ca78e2
SF
2932 }
2933
ff2a09e9
SF
2934 if (n_iov > 2) {
2935 struct create_context *ccontext =
2936 (struct create_context *)iov[n_iov-1].iov_base;
2937 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2938 }
2939 add_query_id_context(iov, &n_iov);
cdeaf9d0 2940
1eb9fb52
RS
2941 rqst->rq_nvec = n_iov;
2942 return 0;
2943}
2944
2945/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2946 * All other vectors are freed by kfree().
2947 */
2948void
2949SMB2_open_free(struct smb_rqst *rqst)
2950{
2951 int i;
2952
32a1fb36
RS
2953 if (rqst && rqst->rq_iov) {
2954 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2955 for (i = 1; i < rqst->rq_nvec; i++)
2956 if (rqst->rq_iov[i].iov_base != smb2_padding)
2957 kfree(rqst->rq_iov[i].iov_base);
2958 }
1eb9fb52
RS
2959}
2960
2961int
2962SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2963 __u8 *oplock, struct smb2_file_all_info *buf,
69dda305 2964 struct create_posix_rsp *posix,
1eb9fb52
RS
2965 struct kvec *err_iov, int *buftype)
2966{
2967 struct smb_rqst rqst;
2968 struct smb2_create_rsp *rsp = NULL;
1eb9fb52
RS
2969 struct cifs_tcon *tcon = oparms->tcon;
2970 struct cifs_ses *ses = tcon->ses;
352d96f3 2971 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4d8dfafc 2972 struct kvec iov[SMB2_CREATE_IOV_SIZE];
1eb9fb52 2973 struct kvec rsp_iov = {NULL, 0};
ef2298a0 2974 int resp_buftype = CIFS_NO_BUFFER;
1eb9fb52
RS
2975 int rc = 0;
2976 int flags = 0;
2977
2978 cifs_dbg(FYI, "create/open\n");
352d96f3 2979 if (!ses || !server)
1eb9fb52
RS
2980 return -EIO;
2981
2982 if (smb3_encryption_required(tcon))
2983 flags |= CIFS_TRANSFORM_REQ;
2984
40eff45b 2985 memset(&rqst, 0, sizeof(struct smb_rqst));
1eb9fb52 2986 memset(&iov, 0, sizeof(iov));
40eff45b 2987 rqst.rq_iov = iov;
4d8dfafc 2988 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
1eb9fb52 2989
352d96f3
AA
2990 rc = SMB2_open_init(tcon, server,
2991 &rqst, oplock, oparms, path);
1eb9fb52
RS
2992 if (rc)
2993 goto creat_exit;
40eff45b 2994
efe2e9f3
SF
2995 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2996 oparms->create_options, oparms->desired_access);
2997
352d96f3
AA
2998 rc = cifs_send_recv(xid, ses, server,
2999 &rqst, &resp_buftype, flags,
4f33bc35 3000 &rsp_iov);
da502f7d 3001 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2503a0db
PS
3002
3003 if (rc != 0) {
3004 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
91cb74f5
RS
3005 if (err_iov && rsp) {
3006 *err_iov = rsp_iov;
9d874c36 3007 *buftype = resp_buftype;
91cb74f5
RS
3008 resp_buftype = CIFS_NO_BUFFER;
3009 rsp = NULL;
3010 }
28d59363
SF
3011 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3012 oparms->create_options, oparms->desired_access, rc);
7dcc82c2 3013 if (rc == -EREMCHG) {
a0a3036b
JP
3014 pr_warn_once("server share %s deleted\n",
3015 tcon->treeName);
7dcc82c2
SF
3016 tcon->need_reconnect = true;
3017 }
2503a0db 3018 goto creat_exit;
6b789518
SF
3019 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3020 goto creat_exit;
3021 else
351a59da
PA
3022 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3023 oparms->create_options, oparms->desired_access);
2503a0db 3024
fae8044c 3025 atomic_inc(&tcon->num_remote_opens);
351a59da
PA
3026 oparms->fid->persistent_fid = rsp->PersistentFileId;
3027 oparms->fid->volatile_fid = rsp->VolatileFileId;
86f740f2 3028 oparms->fid->access = oparms->desired_access;
dfe33f9a 3029#ifdef CONFIG_CIFS_DEBUG2
0d35e382 3030 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
dfe33f9a 3031#endif /* CIFS_DEBUG2 */
f0df737e
PS
3032
3033 if (buf) {
fbcff33d
KC
3034 buf->CreationTime = rsp->CreationTime;
3035 buf->LastAccessTime = rsp->LastAccessTime;
3036 buf->LastWriteTime = rsp->LastWriteTime;
3037 buf->ChangeTime = rsp->ChangeTime;
f0df737e
PS
3038 buf->AllocationSize = rsp->AllocationSize;
3039 buf->EndOfFile = rsp->EndofFile;
3040 buf->Attributes = rsp->FileAttributes;
3041 buf->NumberOfLinks = cpu_to_le32(1);
3042 buf->DeletePending = 0;
3043 }
2e44b288 3044
89a5bfa3
SF
3045
3046 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
69dda305 3047 oparms->fid->lease_key, oplock, buf, posix);
2503a0db 3048creat_exit:
1eb9fb52 3049 SMB2_open_free(&rqst);
2503a0db
PS
3050 free_rsp_buf(resp_buftype, rsp);
3051 return rc;
3052}
3053
4a72dafa 3054int
352d96f3
AA
3055SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3056 struct smb_rqst *rqst,
ccdc77a3 3057 u64 persistent_fid, u64 volatile_fid, u32 opcode,
153322f7
SF
3058 bool is_fsctl, char *in_data, u32 indatalen,
3059 __u32 max_response_size)
4a72dafa
SF
3060{
3061 struct smb2_ioctl_req *req;
ccdc77a3 3062 struct kvec *iov = rqst->rq_iov;
97754680 3063 unsigned int total_len;
ccdc77a3 3064 int rc;
2c87d6a9 3065 char *in_data_buf;
4a72dafa 3066
352d96f3
AA
3067 rc = smb2_ioctl_req_init(opcode, tcon, server,
3068 (void **) &req, &total_len);
4a72dafa
SF
3069 if (rc)
3070 return rc;
3071
2c87d6a9
LL
3072 if (indatalen) {
3073 /*
3074 * indatalen is usually small at a couple of bytes max, so
3075 * just allocate through generic pool
3076 */
d81f0974 3077 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2c87d6a9
LL
3078 if (!in_data_buf) {
3079 cifs_small_buf_release(req);
3080 return -ENOMEM;
3081 }
2c87d6a9
LL
3082 }
3083
4a72dafa
SF
3084 req->CtlCode = cpu_to_le32(opcode);
3085 req->PersistentFileId = persistent_fid;
3086 req->VolatileFileId = volatile_fid;
3087
ccdc77a3
RS
3088 iov[0].iov_base = (char *)req;
3089 /*
3090 * If no input data, the size of ioctl struct in
3091 * protocol spec still includes a 1 byte data buffer,
3092 * but if input data passed to ioctl, we do not
3093 * want to double count this, so we do not send
3094 * the dummy one byte of data in iovec[0] if sending
3095 * input data (in iovec[1]).
3096 */
4a72dafa
SF
3097 if (indatalen) {
3098 req->InputCount = cpu_to_le32(indatalen);
3099 /* do not set InputOffset if no input data */
3100 req->InputOffset =
97754680 3101 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
ccdc77a3
RS
3102 rqst->rq_nvec = 2;
3103 iov[0].iov_len = total_len - 1;
2c87d6a9 3104 iov[1].iov_base = in_data_buf;
4a72dafa 3105 iov[1].iov_len = indatalen;
ccdc77a3
RS
3106 } else {
3107 rqst->rq_nvec = 1;
3108 iov[0].iov_len = total_len;
3109 }
4a72dafa
SF
3110
3111 req->OutputOffset = 0;
3112 req->OutputCount = 0; /* MBZ */
3113
3114 /*
153322f7
SF
3115 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3116 * We Could increase default MaxOutputResponse, but that could require
3117 * more credits. Windows typically sets this smaller, but for some
4a72dafa
SF
3118 * ioctls it may be useful to allow server to send more. No point
3119 * limiting what the server can send as long as fits in one credit
153322f7
SF
3120 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3121 * to increase this limit up in the future.
3122 * Note that for snapshot queries that servers like Azure expect that
3123 * the first query be minimal size (and just used to get the number/size
3124 * of previous versions) so response size must be specified as EXACTLY
3125 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3126 * of eight bytes. Currently that is the only case where we set max
3127 * response size smaller.
4a72dafa 3128 */
153322f7 3129 req->MaxOutputResponse = cpu_to_le32(max_response_size);
0d35e382 3130 req->hdr.CreditCharge =
ebf57440
NJ
3131 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3132 SMB2_MAX_BUFFER_SIZE));
4a72dafa
SF
3133 if (is_fsctl)
3134 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3135 else
3136 req->Flags = 0;
3137
4587eee0
SF
3138 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3139 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
0d35e382 3140 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
4a72dafa 3141
ccdc77a3
RS
3142 return 0;
3143}
3144
3145void
3146SMB2_ioctl_free(struct smb_rqst *rqst)
3147{
6457c20e 3148 int i;
2c87d6a9 3149 if (rqst && rqst->rq_iov) {
ccdc77a3 3150 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
6457c20e
MZ
3151 for (i = 1; i < rqst->rq_nvec; i++)
3152 if (rqst->rq_iov[i].iov_base != smb2_padding)
3153 kfree(rqst->rq_iov[i].iov_base);
2c87d6a9 3154 }
ccdc77a3
RS
3155}
3156
153322f7 3157
ccdc77a3
RS
3158/*
3159 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3160 */
3161int
3162SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3163 u64 volatile_fid, u32 opcode, bool is_fsctl,
153322f7 3164 char *in_data, u32 indatalen, u32 max_out_data_len,
ccdc77a3
RS
3165 char **out_data, u32 *plen /* returned data len */)
3166{
3167 struct smb_rqst rqst;
3168 struct smb2_ioctl_rsp *rsp = NULL;
3169 struct cifs_ses *ses;
352d96f3 3170 struct TCP_Server_Info *server;
72c419d9 3171 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
ccdc77a3
RS
3172 struct kvec rsp_iov = {NULL, 0};
3173 int resp_buftype = CIFS_NO_BUFFER;
3174 int rc = 0;
3175 int flags = 0;
3176
3177 cifs_dbg(FYI, "SMB2 IOCTL\n");
3178
3179 if (out_data != NULL)
3180 *out_data = NULL;
3181
3182 /* zero out returned data len, in case of error */
3183 if (plen)
3184 *plen = 0;
3185
352d96f3 3186 if (!tcon)
ccdc77a3
RS
3187 return -EIO;
3188
352d96f3 3189 ses = tcon->ses;
ac6ad7a8
CIK
3190 if (!ses)
3191 return -EIO;
352d96f3
AA
3192
3193 server = cifs_pick_channel(ses);
ac6ad7a8 3194 if (!server)
ccdc77a3
RS
3195 return -EIO;
3196
3197 if (smb3_encryption_required(tcon))
3198 flags |= CIFS_TRANSFORM_REQ;
3199
40eff45b 3200 memset(&rqst, 0, sizeof(struct smb_rqst));
ccdc77a3 3201 memset(&iov, 0, sizeof(iov));
40eff45b 3202 rqst.rq_iov = iov;
72c419d9 3203 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
ccdc77a3 3204
352d96f3
AA
3205 rc = SMB2_ioctl_init(tcon, server,
3206 &rqst, persistent_fid, volatile_fid, opcode,
153322f7 3207 is_fsctl, in_data, indatalen, max_out_data_len);
ccdc77a3
RS
3208 if (rc)
3209 goto ioctl_exit;
40eff45b 3210
352d96f3
AA
3211 rc = cifs_send_recv(xid, ses, server,
3212 &rqst, &resp_buftype, flags,
97754680 3213 &rsp_iov);
da502f7d 3214 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
4a72dafa 3215
eccb4422
SF
3216 if (rc != 0)
3217 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3218 ses->Suid, 0, opcode, rc);
3219
2f3ebaba 3220 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
8e353106 3221 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
4a72dafa 3222 goto ioctl_exit;
9bf0c9cd
SF
3223 } else if (rc == -EINVAL) {
3224 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3225 (opcode != FSCTL_SRV_COPYCHUNK)) {
8e353106 3226 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
9bf0c9cd
SF
3227 goto ioctl_exit;
3228 }
2f3ebaba
RS
3229 } else if (rc == -E2BIG) {
3230 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3231 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3232 goto ioctl_exit;
3233 }
4a72dafa
SF
3234 }
3235
3236 /* check if caller wants to look at return data or just return rc */
3237 if ((plen == NULL) || (out_data == NULL))
3238 goto ioctl_exit;
3239
4d9beec2
SF
3240 /*
3241 * Although unlikely to be possible for rsp to be null and rc not set,
3242 * adding check below is slightly safer long term (and quiets Coverity
3243 * warning)
3244 */
3245 if (rsp == NULL) {
3246 rc = -EIO;
3247 goto ioctl_exit;
3248 }
3249
4a72dafa
SF
3250 *plen = le32_to_cpu(rsp->OutputCount);
3251
3252 /* We check for obvious errors in the output buffer length and offset */
3253 if (*plen == 0)
3254 goto ioctl_exit; /* server returned no data */
2d204ee9 3255 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3175eb9b 3256 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
4a72dafa
SF
3257 *plen = 0;
3258 rc = -EIO;
3259 goto ioctl_exit;
3260 }
3261
2d204ee9 3262 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3175eb9b 3263 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
4a72dafa
SF
3264 le32_to_cpu(rsp->OutputOffset));
3265 *plen = 0;
3266 rc = -EIO;
3267 goto ioctl_exit;
3268 }
3269
d034feeb
Y
3270 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3271 *plen, GFP_KERNEL);
4a72dafa
SF
3272 if (*out_data == NULL) {
3273 rc = -ENOMEM;
3274 goto ioctl_exit;
3275 }
3276
4a72dafa 3277ioctl_exit:
ccdc77a3 3278 SMB2_ioctl_free(&rqst);
4a72dafa
SF
3279 free_rsp_buf(resp_buftype, rsp);
3280 return rc;
3281}
3282
64a5cfa6
SF
3283/*
3284 * Individual callers to ioctl worker function follow
3285 */
3286
3287int
3288SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3289 u64 persistent_fid, u64 volatile_fid)
3290{
3291 int rc;
64a5cfa6
SF
3292 struct compress_ioctl fsctl_input;
3293 char *ret_data = NULL;
3294
3295 fsctl_input.CompressionState =
bc09d141 3296 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
64a5cfa6
SF
3297
3298 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3299 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3300 (char *)&fsctl_input /* data input */,
153322f7
SF
3301 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3302 &ret_data /* out data */, NULL);
64a5cfa6
SF
3303
3304 cifs_dbg(FYI, "set compression rc %d\n", rc);
64a5cfa6
SF
3305
3306 return rc;
3307}
3308
8eb4ecfa 3309int
352d96f3
AA
3310SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3311 struct smb_rqst *rqst,
43f8a6a7 3312 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
8eb4ecfa
RS
3313{
3314 struct smb2_close_req *req;
3315 struct kvec *iov = rqst->rq_iov;
3316 unsigned int total_len;
3317 int rc;
3318
352d96f3
AA
3319 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3320 (void **) &req, &total_len);
8eb4ecfa
RS
3321 if (rc)
3322 return rc;
3323
351a59da
PA
3324 req->PersistentFileId = persistent_fid;
3325 req->VolatileFileId = volatile_fid;
43f8a6a7
SF
3326 if (query_attrs)
3327 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3328 else
3329 req->Flags = 0;
8eb4ecfa
RS
3330 iov[0].iov_base = (char *)req;
3331 iov[0].iov_len = total_len;
3332
3333 return 0;
3334}
3335
3336void
3337SMB2_close_free(struct smb_rqst *rqst)
3338{
32a1fb36
RS
3339 if (rqst && rqst->rq_iov)
3340 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
8eb4ecfa
RS
3341}
3342
2503a0db 3343int
43f8a6a7
SF
3344__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3345 u64 persistent_fid, u64 volatile_fid,
3346 struct smb2_file_network_open_info *pbuf)
2503a0db 3347{
40eff45b 3348 struct smb_rqst rqst;
8eb4ecfa 3349 struct smb2_close_rsp *rsp = NULL;
2503a0db 3350 struct cifs_ses *ses = tcon->ses;
352d96f3 3351 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2503a0db 3352 struct kvec iov[1];
da502f7d 3353 struct kvec rsp_iov;
ef2298a0 3354 int resp_buftype = CIFS_NO_BUFFER;
2503a0db 3355 int rc = 0;
9e8fae25 3356 int flags = 0;
43f8a6a7 3357 bool query_attrs = false;
2503a0db 3358
f96637be 3359 cifs_dbg(FYI, "Close\n");
2503a0db 3360
352d96f3 3361 if (!ses || !server)
2503a0db
PS
3362 return -EIO;
3363
5a77e75f 3364 if (smb3_encryption_required(tcon))
7fb8986e
PS
3365 flags |= CIFS_TRANSFORM_REQ;
3366
40eff45b 3367 memset(&rqst, 0, sizeof(struct smb_rqst));
8eb4ecfa 3368 memset(&iov, 0, sizeof(iov));
40eff45b
RS
3369 rqst.rq_iov = iov;
3370 rqst.rq_nvec = 1;
3371
43f8a6a7
SF
3372 /* check if need to ask server to return timestamps in close response */
3373 if (pbuf)
3374 query_attrs = true;
3375
f90f9797 3376 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
352d96f3
AA
3377 rc = SMB2_close_init(tcon, server,
3378 &rqst, persistent_fid, volatile_fid,
43f8a6a7 3379 query_attrs);
8eb4ecfa
RS
3380 if (rc)
3381 goto close_exit;
3382
352d96f3
AA
3383 rc = cifs_send_recv(xid, ses, server,
3384 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 3385 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2503a0db
PS
3386
3387 if (rc != 0) {
d4a029d2 3388 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
eccb4422
SF
3389 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3390 rc);
2503a0db 3391 goto close_exit;
43f8a6a7 3392 } else {
f90f9797
SF
3393 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3394 ses->Suid);
43f8a6a7
SF
3395 /*
3396 * Note that have to subtract 4 since struct network_open_info
3397 * has a final 4 byte pad that close response does not have
3398 */
3399 if (pbuf)
3400 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3401 }
2503a0db 3402
fae8044c 3403 atomic_dec(&tcon->num_remote_opens);
2503a0db 3404close_exit:
8eb4ecfa 3405 SMB2_close_free(&rqst);
2503a0db 3406 free_rsp_buf(resp_buftype, rsp);
9150c3ad
PS
3407
3408 /* retry close in a worker thread if this one is interrupted */
2659d3bf 3409 if (is_interrupt_error(rc)) {
9e8fae25
SF
3410 int tmp_rc;
3411
9150c3ad
PS
3412 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3413 volatile_fid);
3414 if (tmp_rc)
3415 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3416 persistent_fid, tmp_rc);
3417 }
9150c3ad 3418 return rc;
97ca1762
RS
3419}
3420
43f8a6a7
SF
3421int
3422SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3423 u64 persistent_fid, u64 volatile_fid)
3424{
3425 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3426}
3427
730928c8
RS
3428int
3429smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3430 struct kvec *iov, unsigned int min_buf_size)
be4cb9e3 3431{
c1596ff5 3432 unsigned int smb_len = iov->iov_len;
1fc6ad2f
RS
3433 char *end_of_smb = smb_len + (char *)iov->iov_base;
3434 char *begin_of_buf = offset + (char *)iov->iov_base;
be4cb9e3
PS
3435 char *end_of_buf = begin_of_buf + buffer_length;
3436
3437
3438 if (buffer_length < min_buf_size) {
f96637be
JP
3439 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3440 buffer_length, min_buf_size);
be4cb9e3
PS
3441 return -EINVAL;
3442 }
3443
3444 /* check if beyond RFC1001 maximum length */
3445 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
f96637be
JP
3446 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3447 buffer_length, smb_len);
be4cb9e3
PS
3448 return -EINVAL;
3449 }
3450
3451 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
a0a3036b 3452 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
be4cb9e3
PS
3453 return -EINVAL;
3454 }
3455
3456 return 0;
3457}
3458
3459/*
3460 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3461 * Caller must free buffer.
3462 */
c5a5f38f
RS
3463int
3464smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3465 struct kvec *iov, unsigned int minbufsize,
3466 char *data)
be4cb9e3 3467{
1fc6ad2f 3468 char *begin_of_buf = offset + (char *)iov->iov_base;
be4cb9e3
PS
3469 int rc;
3470
3471 if (!data)
3472 return -EINVAL;
3473
730928c8 3474 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
be4cb9e3
PS
3475 if (rc)
3476 return rc;
3477
3478 memcpy(data, begin_of_buf, buffer_length);
3479
3480 return 0;
3481}
3482
296ecbae 3483int
352d96f3
AA
3484SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3485 struct smb_rqst *rqst,
296ecbae
RS
3486 u64 persistent_fid, u64 volatile_fid,
3487 u8 info_class, u8 info_type, u32 additional_info,
f5b05d62 3488 size_t output_len, size_t input_len, void *input)
be4cb9e3
PS
3489{
3490 struct smb2_query_info_req *req;
296ecbae 3491 struct kvec *iov = rqst->rq_iov;
b2fb7fec 3492 unsigned int total_len;
296ecbae 3493 int rc;
be4cb9e3 3494
352d96f3
AA
3495 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3496 (void **) &req, &total_len);
be4cb9e3
PS
3497 if (rc)
3498 return rc;
3499
42c493c1 3500 req->InfoType = info_type;
f0df737e 3501 req->FileInfoClass = info_class;
be4cb9e3
PS
3502 req->PersistentFileId = persistent_fid;
3503 req->VolatileFileId = volatile_fid;
42c493c1 3504 req->AdditionalInformation = cpu_to_le32(additional_info);
48923d2a 3505
f0df737e 3506 req->OutputBufferLength = cpu_to_le32(output_len);
f5b05d62
RS
3507 if (input_len) {
3508 req->InputBufferLength = cpu_to_le32(input_len);
3509 /* total_len for smb query request never close to le16 max */
3510 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3511 memcpy(req->Buffer, input, input_len);
3512 }
be4cb9e3
PS
3513
3514 iov[0].iov_base = (char *)req;
b2fb7fec 3515 /* 1 for Buffer */
f5b05d62 3516 iov[0].iov_len = total_len - 1 + input_len;
296ecbae
RS
3517 return 0;
3518}
3519
3520void
3521SMB2_query_info_free(struct smb_rqst *rqst)
3522{
32a1fb36
RS
3523 if (rqst && rqst->rq_iov)
3524 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
296ecbae
RS
3525}
3526
3527static int
3528query_info(const unsigned int xid, struct cifs_tcon *tcon,
3529 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3530 u32 additional_info, size_t output_len, size_t min_len, void **data,
3531 u32 *dlen)
3532{
3533 struct smb_rqst rqst;
3534 struct smb2_query_info_rsp *rsp = NULL;
3535 struct kvec iov[1];
3536 struct kvec rsp_iov;
3537 int rc = 0;
ef2298a0 3538 int resp_buftype = CIFS_NO_BUFFER;
296ecbae 3539 struct cifs_ses *ses = tcon->ses;
ac6ad7a8 3540 struct TCP_Server_Info *server;
296ecbae 3541 int flags = 0;
73aaf920 3542 bool allocated = false;
296ecbae
RS
3543
3544 cifs_dbg(FYI, "Query Info\n");
3545
ac6ad7a8
CIK
3546 if (!ses)
3547 return -EIO;
352d96f3 3548 server = cifs_pick_channel(ses);
ac6ad7a8 3549 if (!server)
296ecbae
RS
3550 return -EIO;
3551
3552 if (smb3_encryption_required(tcon))
3553 flags |= CIFS_TRANSFORM_REQ;
be4cb9e3 3554
40eff45b 3555 memset(&rqst, 0, sizeof(struct smb_rqst));
296ecbae 3556 memset(&iov, 0, sizeof(iov));
40eff45b
RS
3557 rqst.rq_iov = iov;
3558 rqst.rq_nvec = 1;
3559
352d96f3
AA
3560 rc = SMB2_query_info_init(tcon, server,
3561 &rqst, persistent_fid, volatile_fid,
296ecbae 3562 info_class, info_type, additional_info,
f5b05d62 3563 output_len, 0, NULL);
296ecbae
RS
3564 if (rc)
3565 goto qinf_exit;
3566
d42043a6
SF
3567 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3568 ses->Suid, info_class, (__u32)info_type);
3569
352d96f3
AA
3570 rc = cifs_send_recv(xid, ses, server,
3571 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 3572 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
e5d04887 3573
be4cb9e3
PS
3574 if (rc) {
3575 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
eccb4422
SF
3576 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3577 ses->Suid, info_class, (__u32)info_type, rc);
be4cb9e3
PS
3578 goto qinf_exit;
3579 }
3580
d42043a6
SF
3581 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3582 ses->Suid, info_class, (__u32)info_type);
3583
42c493c1
SP
3584 if (dlen) {
3585 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3586 if (!*data) {
3587 *data = kmalloc(*dlen, GFP_KERNEL);
3588 if (!*data) {
3175eb9b 3589 cifs_tcon_dbg(VFS,
42c493c1
SP
3590 "Error %d allocating memory for acl\n",
3591 rc);
3592 *dlen = 0;
73aaf920 3593 rc = -ENOMEM;
42c493c1
SP
3594 goto qinf_exit;
3595 }
73aaf920 3596 allocated = true;
42c493c1
SP
3597 }
3598 }
3599
c5a5f38f
RS
3600 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3601 le32_to_cpu(rsp->OutputBufferLength),
3602 &rsp_iov, min_len, *data);
73aaf920
CIK
3603 if (rc && allocated) {
3604 kfree(*data);
3605 *data = NULL;
3606 *dlen = 0;
3607 }
be4cb9e3
PS
3608
3609qinf_exit:
296ecbae 3610 SMB2_query_info_free(&rqst);
be4cb9e3
PS
3611 free_rsp_buf(resp_buftype, rsp);
3612 return rc;
3613}
9094fad1 3614
42c493c1
SP
3615int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3616 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3617{
3618 return query_info(xid, tcon, persistent_fid, volatile_fid,
3619 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3620 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3621 sizeof(struct smb2_file_all_info), (void **)&data,
3622 NULL);
3623}
3624
e0ae8a9a
SF
3625#if 0
3626/* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
b1bc1874
SF
3627int
3628SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3629 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3630{
3631 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3632 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3633 *plen = 0;
3634
3635 return query_info(xid, tcon, persistent_fid, volatile_fid,
3636 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3637 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
e0ae8a9a 3638 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
b1bc1874 3639}
e0ae8a9a 3640#endif
b1bc1874 3641
f0df737e 3642int
42c493c1 3643SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3970acf7 3644 u64 persistent_fid, u64 volatile_fid,
9541b813 3645 void **data, u32 *plen, u32 extra_info)
f0df737e 3646{
9541b813
BP
3647 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3648 extra_info;
42c493c1
SP
3649 *plen = 0;
3650
f0df737e 3651 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1 3652 0, SMB2_O_INFO_SECURITY, additional_info,
ee25c6dd 3653 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
f0df737e
PS
3654}
3655
3656int
3657SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3658 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3659{
3660 return query_info(xid, tcon, persistent_fid, volatile_fid,
42c493c1
SP
3661 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3662 sizeof(struct smb2_file_internal_info),
f0df737e 3663 sizeof(struct smb2_file_internal_info),
42c493c1 3664 (void **)&uniqueid, NULL);
f0df737e
PS
3665}
3666
c3498185
SF
3667/*
3668 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3669 * See MS-SMB2 2.2.35 and 2.2.36
3670 */
3671
388962e8 3672static int
c3498185 3673SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
352d96f3
AA
3674 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3675 u64 persistent_fid, u64 volatile_fid,
3676 u32 completion_filter, bool watch_tree)
c3498185
SF
3677{
3678 struct smb2_change_notify_req *req;
3679 struct kvec *iov = rqst->rq_iov;
3680 unsigned int total_len;
3681 int rc;
3682
352d96f3
AA
3683 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3684 (void **) &req, &total_len);
c3498185
SF
3685 if (rc)
3686 return rc;
3687
351a59da
PA
3688 req->PersistentFileId = persistent_fid;
3689 req->VolatileFileId = volatile_fid;
d26c2ddd 3690 /* See note 354 of MS-SMB2, 64K max */
52870d50
SF
3691 req->OutputBufferLength =
3692 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
c3498185
SF
3693 req->CompletionFilter = cpu_to_le32(completion_filter);
3694 if (watch_tree)
3695 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3696 else
3697 req->Flags = 0;
3698
3699 iov[0].iov_base = (char *)req;
3700 iov[0].iov_len = total_len;
3701
3702 return 0;
3703}
3704
3705int
3706SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3707 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3708 u32 completion_filter)
3709{
3710 struct cifs_ses *ses = tcon->ses;
352d96f3 3711 struct TCP_Server_Info *server = cifs_pick_channel(ses);
c3498185
SF
3712 struct smb_rqst rqst;
3713 struct kvec iov[1];
3714 struct kvec rsp_iov = {NULL, 0};
3715 int resp_buftype = CIFS_NO_BUFFER;
3716 int flags = 0;
3717 int rc = 0;
3718
3719 cifs_dbg(FYI, "change notify\n");
352d96f3 3720 if (!ses || !server)
c3498185
SF
3721 return -EIO;
3722
3723 if (smb3_encryption_required(tcon))
3724 flags |= CIFS_TRANSFORM_REQ;
3725
3726 memset(&rqst, 0, sizeof(struct smb_rqst));
3727 memset(&iov, 0, sizeof(iov));
3728 rqst.rq_iov = iov;
3729 rqst.rq_nvec = 1;
3730
352d96f3
AA
3731 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3732 persistent_fid, volatile_fid,
c3498185
SF
3733 completion_filter, watch_tree);
3734 if (rc)
3735 goto cnotify_exit;
3736
3737 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3738 (u8)watch_tree, completion_filter);
352d96f3
AA
3739 rc = cifs_send_recv(xid, ses, server,
3740 &rqst, &resp_buftype, flags, &rsp_iov);
c3498185
SF
3741
3742 if (rc != 0) {
3743 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3744 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3745 (u8)watch_tree, completion_filter, rc);
3746 } else
3747 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3748 ses->Suid, (u8)watch_tree, completion_filter);
3749
3750 cnotify_exit:
3751 if (rqst.rq_iov)
3752 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3753 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3754 return rc;
3755}
3756
3757
3758
9094fad1
PS
3759/*
3760 * This is a no-op for now. We're not really interested in the reply, but
3761 * rather in the fact that the server sent one and that server->lstrp
3762 * gets updated.
3763 *
3764 * FIXME: maybe we should consider checking that the reply matches request?
3765 */
3766static void
3767smb2_echo_callback(struct mid_q_entry *mid)
3768{
3769 struct TCP_Server_Info *server = mid->callback_data;
31473fc4 3770 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
34f4deb7 3771 struct cifs_credits credits = { .value = 0, .instance = 0 };
9094fad1 3772
0fd1d37b 3773 if (mid->mid_state == MID_RESPONSE_RECEIVED
34f4deb7 3774 || mid->mid_state == MID_RESPONSE_MALFORMED) {
0d35e382 3775 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7
PS
3776 credits.instance = server->reconnect_instance;
3777 }
9094fad1
PS
3778
3779 DeleteMidQEntry(mid);
34f4deb7 3780 add_credits(server, &credits, CIFS_ECHO_OP);
9094fad1
PS
3781}
3782
53e0e11e
PS
3783void smb2_reconnect_server(struct work_struct *work)
3784{
3785 struct TCP_Server_Info *server = container_of(work,
3786 struct TCP_Server_Info, reconnect.work);
3663c904
SP
3787 struct TCP_Server_Info *pserver;
3788 struct cifs_ses *ses, *ses2;
53e0e11e 3789 struct cifs_tcon *tcon, *tcon2;
3663c904
SP
3790 struct list_head tmp_list, tmp_ses_list;
3791 bool tcon_exist = false, ses_exist = false;
8a409cda 3792 bool tcon_selected = false;
18ea4311 3793 int rc;
3663c904 3794 bool resched = false;
18ea4311 3795
3663c904
SP
3796 /* If server is a channel, select the primary channel */
3797 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
53e0e11e
PS
3798
3799 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3663c904 3800 mutex_lock(&pserver->reconnect_mutex);
53e0e11e
PS
3801
3802 INIT_LIST_HEAD(&tmp_list);
3663c904
SP
3803 INIT_LIST_HEAD(&tmp_ses_list);
3804 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
53e0e11e
PS
3805
3806 spin_lock(&cifs_tcp_ses_lock);
3663c904
SP
3807 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3808
8a409cda 3809 tcon_selected = false;
3663c904 3810
53e0e11e 3811 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
96a988ff 3812 if (tcon->need_reconnect || tcon->need_reopen_files) {
53e0e11e
PS
3813 tcon->tc_count++;
3814 list_add_tail(&tcon->rlist, &tmp_list);
3663c904 3815 tcon_selected = tcon_exist = true;
53e0e11e
PS
3816 }
3817 }
0ff2b018
RS
3818 /*
3819 * IPC has the same lifetime as its session and uses its
3820 * refcount.
3821 */
b327a717
AA
3822 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3823 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3663c904
SP
3824 tcon_selected = tcon_exist = true;
3825 ses->ses_count++;
3826 }
3827 /*
3828 * handle the case where channel needs to reconnect
3829 * binding session, but tcon is healthy (some other channel
3830 * is active)
3831 */
88b024f5 3832 spin_lock(&ses->chan_lock);
3663c904
SP
3833 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3834 list_add_tail(&ses->rlist, &tmp_ses_list);
8a409cda 3835 ses_exist = true;
0ff2b018 3836 ses->ses_count++;
b327a717 3837 }
88b024f5 3838 spin_unlock(&ses->chan_lock);
53e0e11e
PS
3839 }
3840 /*
3841 * Get the reference to server struct to be sure that the last call of
3842 * cifs_put_tcon() in the loop below won't release the server pointer.
3843 */
3663c904 3844 if (tcon_exist || ses_exist)
53e0e11e
PS
3845 server->srv_count++;
3846
3847 spin_unlock(&cifs_tcp_ses_lock);
3848
3849 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
352d96f3 3850 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
18ea4311 3851 if (!rc)
96a988ff 3852 cifs_reopen_persistent_handles(tcon);
18ea4311
GP
3853 else
3854 resched = true;
53e0e11e 3855 list_del_init(&tcon->rlist);
0ff2b018
RS
3856 if (tcon->ipc)
3857 cifs_put_smb_ses(tcon->ses);
3858 else
3859 cifs_put_tcon(tcon);
53e0e11e
PS
3860 }
3861
3663c904
SP
3862 if (!ses_exist)
3863 goto done;
3864
3865 /* allocate a dummy tcon struct used for reconnect */
3866 tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
3867 if (!tcon) {
3868 resched = true;
a96c9448
XT
3869 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3870 list_del_init(&ses->rlist);
3871 cifs_put_smb_ses(ses);
3872 }
3663c904
SP
3873 goto done;
3874 }
3875
fdf59eb5 3876 tcon->status = TID_GOOD;
3663c904
SP
3877 tcon->retry = false;
3878 tcon->need_reconnect = false;
3879
3880 /* now reconnect sessions for necessary channels */
3881 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3882 tcon->ses = ses;
3883 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3884 if (rc)
3885 resched = true;
3886 list_del_init(&ses->rlist);
3887 cifs_put_smb_ses(ses);
3888 }
3889 kfree(tcon);
3890
3891done:
3892 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
18ea4311
GP
3893 if (resched)
3894 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3663c904 3895 mutex_unlock(&pserver->reconnect_mutex);
53e0e11e
PS
3896
3897 /* now we can safely release srv struct */
3663c904 3898 if (tcon_exist || ses_exist)
53e0e11e
PS
3899 cifs_put_tcp_session(server, 1);
3900}
3901
9094fad1
PS
3902int
3903SMB2_echo(struct TCP_Server_Info *server)
3904{
3905 struct smb2_echo_req *req;
3906 int rc = 0;
c713c877 3907 struct kvec iov[1];
738f9de5 3908 struct smb_rqst rqst = { .rq_iov = iov,
c713c877 3909 .rq_nvec = 1 };
7f7ae759 3910 unsigned int total_len;
9094fad1 3911
bda487ac 3912 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
9094fad1 3913
d7d7a66a 3914 spin_lock(&server->srv_lock);
1a6a41d4
SP
3915 if (server->ops->need_neg &&
3916 server->ops->need_neg(server)) {
d7d7a66a 3917 spin_unlock(&server->srv_lock);
53e0e11e 3918 /* No need to send echo on newly established connections */
b08484d7 3919 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
53e0e11e 3920 return rc;
4fcd1813 3921 }
d7d7a66a 3922 spin_unlock(&server->srv_lock);
4fcd1813 3923
352d96f3
AA
3924 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3925 (void **)&req, &total_len);
9094fad1
PS
3926 if (rc)
3927 return rc;
3928
0d35e382 3929 req->hdr.CreditRequest = cpu_to_le16(1);
9094fad1 3930
c713c877
RS
3931 iov[0].iov_len = total_len;
3932 iov[0].iov_base = (char *)req;
9094fad1 3933
9b7c18a2 3934 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3349c3a7 3935 server, CIFS_ECHO_OP, NULL);
9094fad1 3936 if (rc)
f96637be 3937 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
9094fad1
PS
3938
3939 cifs_small_buf_release(req);
3940 return rc;
3941}
7a5cfb19 3942
86e14e12
RS
3943void
3944SMB2_flush_free(struct smb_rqst *rqst)
3945{
3946 if (rqst && rqst->rq_iov)
3947 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3948}
3949
7a5cfb19 3950int
86e14e12 3951SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
352d96f3
AA
3952 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3953 u64 persistent_fid, u64 volatile_fid)
7a5cfb19
PS
3954{
3955 struct smb2_flush_req *req;
86e14e12 3956 struct kvec *iov = rqst->rq_iov;
1f444e4c 3957 unsigned int total_len;
86e14e12 3958 int rc;
7a5cfb19 3959
352d96f3
AA
3960 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3961 (void **) &req, &total_len);
7a5cfb19
PS
3962 if (rc)
3963 return rc;
3964
351a59da
PA
3965 req->PersistentFileId = persistent_fid;
3966 req->VolatileFileId = volatile_fid;
7a5cfb19
PS
3967
3968 iov[0].iov_base = (char *)req;
1f444e4c 3969 iov[0].iov_len = total_len;
7a5cfb19 3970
86e14e12
RS
3971 return 0;
3972}
3973
3974int
3975SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3976 u64 volatile_fid)
3977{
3978 struct cifs_ses *ses = tcon->ses;
3979 struct smb_rqst rqst;
3980 struct kvec iov[1];
3981 struct kvec rsp_iov = {NULL, 0};
352d96f3 3982 struct TCP_Server_Info *server = cifs_pick_channel(ses);
86e14e12
RS
3983 int resp_buftype = CIFS_NO_BUFFER;
3984 int flags = 0;
3985 int rc = 0;
3986
3987 cifs_dbg(FYI, "flush\n");
3988 if (!ses || !(ses->server))
3989 return -EIO;
3990
3991 if (smb3_encryption_required(tcon))
3992 flags |= CIFS_TRANSFORM_REQ;
3993
40eff45b 3994 memset(&rqst, 0, sizeof(struct smb_rqst));
86e14e12 3995 memset(&iov, 0, sizeof(iov));
40eff45b
RS
3996 rqst.rq_iov = iov;
3997 rqst.rq_nvec = 1;
3998
352d96f3
AA
3999 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4000 persistent_fid, volatile_fid);
86e14e12
RS
4001 if (rc)
4002 goto flush_exit;
4003
f90f9797 4004 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
352d96f3
AA
4005 rc = cifs_send_recv(xid, ses, server,
4006 &rqst, &resp_buftype, flags, &rsp_iov);
7a5cfb19 4007
eccb4422 4008 if (rc != 0) {
7a5cfb19 4009 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
eccb4422
SF
4010 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4011 rc);
f90f9797
SF
4012 } else
4013 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4014 ses->Suid);
7a5cfb19 4015
86e14e12
RS
4016 flush_exit:
4017 SMB2_flush_free(&rqst);
da502f7d 4018 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
7a5cfb19
PS
4019 return rc;
4020}
09a4707e
PS
4021
4022/*
4023 * To form a chain of read requests, any read requests after the first should
4024 * have the end_of_chain boolean set to true.
4025 */
4026static int
738f9de5 4027smb2_new_read_req(void **buf, unsigned int *total_len,
2dabfd5b
LL
4028 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4029 unsigned int remaining_bytes, int request_type)
09a4707e
PS
4030{
4031 int rc = -EACCES;
d8d9de53 4032 struct smb2_read_req *req = NULL;
0d35e382 4033 struct smb2_hdr *shdr;
352d96f3 4034 struct TCP_Server_Info *server = io_parms->server;
09a4707e 4035
352d96f3
AA
4036 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4037 (void **) &req, total_len);
09a4707e
PS
4038 if (rc)
4039 return rc;
2dabfd5b 4040
2dabfd5b 4041 if (server == NULL)
09a4707e
PS
4042 return -ECONNABORTED;
4043
0d35e382
RS
4044 shdr = &req->hdr;
4045 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
09a4707e 4046
351a59da
PA
4047 req->PersistentFileId = io_parms->persistent_fid;
4048 req->VolatileFileId = io_parms->volatile_fid;
09a4707e
PS
4049 req->ReadChannelInfoOffset = 0; /* reserved */
4050 req->ReadChannelInfoLength = 0; /* reserved */
4051 req->Channel = 0; /* reserved */
4052 req->MinimumCount = 0;
4053 req->Length = cpu_to_le32(io_parms->length);
4054 req->Offset = cpu_to_le64(io_parms->offset);
d323c246
SF
4055
4056 trace_smb3_read_enter(0 /* xid */,
4057 io_parms->persistent_fid,
4058 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4059 io_parms->offset, io_parms->length);
bd3dcc6a
LL
4060#ifdef CONFIG_CIFS_SMB_DIRECT
4061 /*
4062 * If we want to do a RDMA write, fill in and append
4063 * smbd_buffer_descriptor_v1 to the end of read request
4064 */
bb4c0419 4065 if (server->rdma && rdata && !server->sign &&
bd3dcc6a
LL
4066 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
4067
4068 struct smbd_buffer_descriptor_v1 *v1;
352d96f3 4069 bool need_invalidate = server->dialect == SMB30_PROT_ID;
bd3dcc6a
LL
4070
4071 rdata->mr = smbd_register_mr(
4072 server->smbd_conn, rdata->pages,
7cf20bce
LL
4073 rdata->nr_pages, rdata->page_offset,
4074 rdata->tailsz, true, need_invalidate);
bd3dcc6a 4075 if (!rdata->mr)
b7972092 4076 return -EAGAIN;
bd3dcc6a
LL
4077
4078 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4079 if (need_invalidate)
4080 req->Channel = SMB2_CHANNEL_RDMA_V1;
4081 req->ReadChannelInfoOffset =
d8d9de53 4082 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
bd3dcc6a 4083 req->ReadChannelInfoLength =
2026b06e 4084 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
bd3dcc6a 4085 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
4086 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4087 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4088 v1->length = cpu_to_le32(rdata->mr->mr->length);
bd3dcc6a
LL
4089
4090 *total_len += sizeof(*v1) - 1;
4091 }
4092#endif
09a4707e
PS
4093 if (request_type & CHAINED_REQUEST) {
4094 if (!(request_type & END_OF_CHAIN)) {
b8f57ee8
PS
4095 /* next 8-byte aligned request */
4096 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
4097 shdr->NextCommand = cpu_to_le32(*total_len);
09a4707e 4098 } else /* END_OF_CHAIN */
31473fc4 4099 shdr->NextCommand = 0;
09a4707e 4100 if (request_type & RELATED_REQUEST) {
31473fc4 4101 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
09a4707e
PS
4102 /*
4103 * Related requests use info from previous read request
4104 * in chain.
4105 */
0d35e382
RS
4106 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4107 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
351a59da
PA
4108 req->PersistentFileId = (u64)-1;
4109 req->VolatileFileId = (u64)-1;
09a4707e
PS
4110 }
4111 }
4112 if (remaining_bytes > io_parms->length)
4113 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4114 else
4115 req->RemainingBytes = 0;
4116
738f9de5 4117 *buf = req;
09a4707e
PS
4118 return rc;
4119}
4120
4121static void
4122smb2_readv_callback(struct mid_q_entry *mid)
4123{
4124 struct cifs_readdata *rdata = mid->callback_data;
4125 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
352d96f3 4126 struct TCP_Server_Info *server = rdata->server;
0d35e382
RS
4127 struct smb2_hdr *shdr =
4128 (struct smb2_hdr *)rdata->iov[0].iov_base;
34f4deb7 4129 struct cifs_credits credits = { .value = 0, .instance = 0 };
46f17d17
SF
4130 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
4131 .rq_nvec = 1,
8321fec4 4132 .rq_pages = rdata->pages,
1dbe3466 4133 .rq_offset = rdata->page_offset,
8321fec4
JL
4134 .rq_npages = rdata->nr_pages,
4135 .rq_pagesz = rdata->pagesz,
4136 .rq_tailsz = rdata->tailsz };
09a4707e 4137
352d96f3
AA
4138 WARN_ONCE(rdata->server != mid->server,
4139 "rdata server %p != mid server %p",
4140 rdata->server, mid->server);
4141
f96637be
JP
4142 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4143 __func__, mid->mid, mid->mid_state, rdata->result,
4144 rdata->bytes);
09a4707e
PS
4145
4146 switch (mid->mid_state) {
4147 case MID_RESPONSE_RECEIVED:
34f4deb7
PS
4148 credits.value = le16_to_cpu(shdr->CreditRequest);
4149 credits.instance = server->reconnect_instance;
09a4707e 4150 /* result already set, check signature */
4326ed2f 4151 if (server->sign && !mid->decrypted) {
3c1bf7e4
PS
4152 int rc;
4153
0b688cfc 4154 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4 4155 if (rc)
3175eb9b 4156 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
f96637be 4157 rc);
3c1bf7e4 4158 }
09a4707e 4159 /* FIXME: should this be counted toward the initiating task? */
34a54d61
PS
4160 task_io_account_read(rdata->got_bytes);
4161 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e
PS
4162 break;
4163 case MID_REQUEST_SUBMITTED:
4164 case MID_RETRY_NEEDED:
4165 rdata->result = -EAGAIN;
d913ed17
PS
4166 if (server->sign && rdata->got_bytes)
4167 /* reset bytes number since we can not check a sign */
4168 rdata->got_bytes = 0;
4169 /* FIXME: should this be counted toward the initiating task? */
4170 task_io_account_read(rdata->got_bytes);
4171 cifs_stats_bytes_read(tcon, rdata->got_bytes);
09a4707e 4172 break;
0fd1d37b 4173 case MID_RESPONSE_MALFORMED:
34f4deb7
PS
4174 credits.value = le16_to_cpu(shdr->CreditRequest);
4175 credits.instance = server->reconnect_instance;
30b5ae21 4176 fallthrough;
09a4707e 4177 default:
6b15eb18 4178 rdata->result = -EIO;
09a4707e 4179 }
bd3dcc6a
LL
4180#ifdef CONFIG_CIFS_SMB_DIRECT
4181 /*
4182 * If this rdata has a memmory registered, the MR can be freed
4183 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4184 * because they have limited number and are used for future I/Os
4185 */
4186 if (rdata->mr) {
4187 smbd_deregister_mr(rdata->mr);
4188 rdata->mr = NULL;
4189 }
4190#endif
082aaa87 4191 if (rdata->result && rdata->result != -ENODATA) {
09a4707e 4192 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
7d42e72f
PS
4193 trace_smb3_read_err(0 /* xid */,
4194 rdata->cfile->fid.persistent_fid,
4195 tcon->tid, tcon->ses->Suid, rdata->offset,
4196 rdata->bytes, rdata->result);
4197 } else
4198 trace_smb3_read_done(0 /* xid */,
4199 rdata->cfile->fid.persistent_fid,
4200 tcon->tid, tcon->ses->Suid,
4201 rdata->offset, rdata->got_bytes);
09a4707e
PS
4202
4203 queue_work(cifsiod_wq, &rdata->work);
4204 DeleteMidQEntry(mid);
34f4deb7 4205 add_credits(server, &credits, 0);
09a4707e
PS
4206}
4207
738f9de5 4208/* smb2_async_readv - send an async read, and set up mid to handle result */
09a4707e
PS
4209int
4210smb2_async_readv(struct cifs_readdata *rdata)
4211{
bed9da02 4212 int rc, flags = 0;
31473fc4 4213 char *buf;
0d35e382 4214 struct smb2_hdr *shdr;
09a4707e 4215 struct cifs_io_parms io_parms;
738f9de5 4216 struct smb_rqst rqst = { .rq_iov = rdata->iov,
c713c877 4217 .rq_nvec = 1 };
bed9da02 4218 struct TCP_Server_Info *server;
352d96f3 4219 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
738f9de5 4220 unsigned int total_len;
09a4707e 4221
f96637be
JP
4222 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4223 __func__, rdata->offset, rdata->bytes);
09a4707e 4224
352d96f3
AA
4225 if (!rdata->server)
4226 rdata->server = cifs_pick_channel(tcon->ses);
4227
09a4707e 4228 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
352d96f3 4229 io_parms.server = server = rdata->server;
09a4707e
PS
4230 io_parms.offset = rdata->offset;
4231 io_parms.length = rdata->bytes;
4232 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4233 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4234 io_parms.pid = rdata->pid;
bed9da02 4235
2dabfd5b
LL
4236 rc = smb2_new_read_req(
4237 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
f0b93cb9 4238 if (rc)
09a4707e
PS
4239 return rc;
4240
5a77e75f 4241 if (smb3_encryption_required(io_parms.tcon))
7fb8986e
PS
4242 flags |= CIFS_TRANSFORM_REQ;
4243
c713c877
RS
4244 rdata->iov[0].iov_base = buf;
4245 rdata->iov[0].iov_len = total_len;
b8f57ee8 4246
0d35e382 4247 shdr = (struct smb2_hdr *)buf;
09a4707e 4248
335b7b62 4249 if (rdata->credits.value > 0) {
31473fc4 4250 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
bed9da02 4251 SMB2_MAX_BUFFER_SIZE));
88fd98a2 4252 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
9a1c67e8
PS
4253
4254 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4255 if (rc)
335b7b62 4256 goto async_readv_out;
9a1c67e8 4257
7fb8986e 4258 flags |= CIFS_HAS_CREDITS;
bed9da02
PS
4259 }
4260
09a4707e 4261 kref_get(&rdata->refcount);
352d96f3 4262 rc = cifs_call_async(server, &rqst,
09a4707e 4263 cifs_readv_receive, smb2_readv_callback,
3349c3a7
PS
4264 smb3_handle_read_data, rdata, flags,
4265 &rdata->credits);
e5d04887 4266 if (rc) {
09a4707e 4267 kref_put(&rdata->refcount, cifs_readdata_release);
e5d04887 4268 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
7d42e72f
PS
4269 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4270 io_parms.tcon->tid,
4271 io_parms.tcon->ses->Suid,
4272 io_parms.offset, io_parms.length, rc);
4273 }
09a4707e 4274
335b7b62 4275async_readv_out:
09a4707e
PS
4276 cifs_small_buf_release(buf);
4277 return rc;
4278}
33319141 4279
d8e05039
PS
4280int
4281SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4282 unsigned int *nbytes, char **buf, int *buf_type)
4283{
40eff45b 4284 struct smb_rqst rqst;
1efd4fc7 4285 int resp_buftype, rc;
d8d9de53 4286 struct smb2_read_req *req = NULL;
d8e05039 4287 struct smb2_read_rsp *rsp = NULL;
f5688a6d 4288 struct kvec iov[1];
da502f7d 4289 struct kvec rsp_iov;
738f9de5 4290 unsigned int total_len;
7fb8986e
PS
4291 int flags = CIFS_LOG_ERROR;
4292 struct cifs_ses *ses = io_parms->tcon->ses;
d8e05039 4293
352d96f3
AA
4294 if (!io_parms->server)
4295 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4296
d8e05039 4297 *nbytes = 0;
2dabfd5b 4298 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
d8e05039
PS
4299 if (rc)
4300 return rc;
4301
5a77e75f 4302 if (smb3_encryption_required(io_parms->tcon))
7fb8986e
PS
4303 flags |= CIFS_TRANSFORM_REQ;
4304
f5688a6d
RS
4305 iov[0].iov_base = (char *)req;
4306 iov[0].iov_len = total_len;
b8f57ee8 4307
40eff45b
RS
4308 memset(&rqst, 0, sizeof(struct smb_rqst));
4309 rqst.rq_iov = iov;
4310 rqst.rq_nvec = 1;
4311
352d96f3
AA
4312 rc = cifs_send_recv(xid, ses, io_parms->server,
4313 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 4314 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
d8e05039 4315
a821df3f
RS
4316 if (rc) {
4317 if (rc != -ENODATA) {
4318 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4319 cifs_dbg(VFS, "Send error in read = %d\n", rc);
d8d9de53 4320 trace_smb3_read_err(xid,
351a59da 4321 req->PersistentFileId,
7d42e72f
PS
4322 io_parms->tcon->tid, ses->Suid,
4323 io_parms->offset, io_parms->length,
4324 rc);
b0a42f2a 4325 } else
351a59da
PA
4326 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4327 ses->Suid, io_parms->offset, 0);
da502f7d 4328 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
05fd5c2c 4329 cifs_small_buf_release(req);
a821df3f 4330 return rc == -ENODATA ? 0 : rc;
eccb4422 4331 } else
d8d9de53 4332 trace_smb3_read_done(xid,
351a59da 4333 req->PersistentFileId,
eccb4422
SF
4334 io_parms->tcon->tid, ses->Suid,
4335 io_parms->offset, io_parms->length);
d8e05039 4336
088aaf17
Z
4337 cifs_small_buf_release(req);
4338
a821df3f
RS
4339 *nbytes = le32_to_cpu(rsp->DataLength);
4340 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4341 (*nbytes > io_parms->length)) {
4342 cifs_dbg(FYI, "bad length %d for count %d\n",
4343 *nbytes, io_parms->length);
4344 rc = -EIO;
4345 *nbytes = 0;
d8e05039
PS
4346 }
4347
4348 if (*buf) {
977b6170 4349 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
da502f7d 4350 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
d8e05039 4351 } else if (resp_buftype != CIFS_NO_BUFFER) {
da502f7d 4352 *buf = rsp_iov.iov_base;
d8e05039
PS
4353 if (resp_buftype == CIFS_SMALL_BUFFER)
4354 *buf_type = CIFS_SMALL_BUFFER;
4355 else if (resp_buftype == CIFS_LARGE_BUFFER)
4356 *buf_type = CIFS_LARGE_BUFFER;
4357 }
4358 return rc;
4359}
4360
33319141
PS
4361/*
4362 * Check the mid_state and signature on received buffer (if any), and queue the
4363 * workqueue completion task.
4364 */
4365static void
4366smb2_writev_callback(struct mid_q_entry *mid)
4367{
4368 struct cifs_writedata *wdata = mid->callback_data;
4369 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
352d96f3 4370 struct TCP_Server_Info *server = wdata->server;
33319141
PS
4371 unsigned int written;
4372 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
34f4deb7 4373 struct cifs_credits credits = { .value = 0, .instance = 0 };
33319141 4374
352d96f3
AA
4375 WARN_ONCE(wdata->server != mid->server,
4376 "wdata server %p != mid server %p",
4377 wdata->server, mid->server);
4378
33319141
PS
4379 switch (mid->mid_state) {
4380 case MID_RESPONSE_RECEIVED:
0d35e382 4381 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7
PS
4382 credits.instance = server->reconnect_instance;
4383 wdata->result = smb2_check_receive(mid, server, 0);
33319141
PS
4384 if (wdata->result != 0)
4385 break;
4386
4387 written = le32_to_cpu(rsp->DataLength);
4388 /*
4389 * Mask off high 16 bits when bytes written as returned
4390 * by the server is greater than bytes requested by the
4391 * client. OS/2 servers are known to set incorrect
4392 * CountHigh values.
4393 */
4394 if (written > wdata->bytes)
4395 written &= 0xFFFF;
4396
4397 if (written < wdata->bytes)
4398 wdata->result = -ENOSPC;
4399 else
4400 wdata->bytes = written;
4401 break;
4402 case MID_REQUEST_SUBMITTED:
4403 case MID_RETRY_NEEDED:
4404 wdata->result = -EAGAIN;
4405 break;
0fd1d37b 4406 case MID_RESPONSE_MALFORMED:
0d35e382 4407 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
34f4deb7 4408 credits.instance = server->reconnect_instance;
30b5ae21 4409 fallthrough;
33319141
PS
4410 default:
4411 wdata->result = -EIO;
4412 break;
4413 }
db223a59
LL
4414#ifdef CONFIG_CIFS_SMB_DIRECT
4415 /*
4416 * If this wdata has a memory registered, the MR can be freed
4417 * The number of MRs available is limited, it's important to recover
4418 * used MR as soon as I/O is finished. Hold MR longer in the later
4419 * I/O process can possibly result in I/O deadlock due to lack of MR
4420 * to send request on I/O retry
4421 */
4422 if (wdata->mr) {
4423 smbd_deregister_mr(wdata->mr);
4424 wdata->mr = NULL;
4425 }
4426#endif
7d42e72f 4427 if (wdata->result) {
33319141 4428 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
7d42e72f
PS
4429 trace_smb3_write_err(0 /* no xid */,
4430 wdata->cfile->fid.persistent_fid,
4431 tcon->tid, tcon->ses->Suid, wdata->offset,
4432 wdata->bytes, wdata->result);
d6fd4190 4433 if (wdata->result == -ENOSPC)
a0a3036b
JP
4434 pr_warn_once("Out of space writing to %s\n",
4435 tcon->treeName);
7d42e72f
PS
4436 } else
4437 trace_smb3_write_done(0 /* no xid */,
4438 wdata->cfile->fid.persistent_fid,
4439 tcon->tid, tcon->ses->Suid,
4440 wdata->offset, wdata->bytes);
33319141
PS
4441
4442 queue_work(cifsiod_wq, &wdata->work);
4443 DeleteMidQEntry(mid);
34f4deb7 4444 add_credits(server, &credits, 0);
33319141
PS
4445}
4446
4447/* smb2_async_writev - send an async write, and set up mid to handle result */
4448int
4a5c80d7
SF
4449smb2_async_writev(struct cifs_writedata *wdata,
4450 void (*release)(struct kref *kref))
33319141 4451{
cb7e9eab 4452 int rc = -EACCES, flags = 0;
33319141 4453 struct smb2_write_req *req = NULL;
0d35e382 4454 struct smb2_hdr *shdr;
33319141 4455 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
352d96f3 4456 struct TCP_Server_Info *server = wdata->server;
c713c877 4457 struct kvec iov[1];
738f9de5 4458 struct smb_rqst rqst = { };
f5688a6d 4459 unsigned int total_len;
33319141 4460
352d96f3
AA
4461 if (!wdata->server)
4462 server = wdata->server = cifs_pick_channel(tcon->ses);
4463
4464 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4465 (void **) &req, &total_len);
f0b93cb9
PS
4466 if (rc)
4467 return rc;
33319141 4468
5a77e75f 4469 if (smb3_encryption_required(tcon))
7fb8986e
PS
4470 flags |= CIFS_TRANSFORM_REQ;
4471
0d35e382
RS
4472 shdr = (struct smb2_hdr *)req;
4473 shdr->Id.SyncId.ProcessId = cpu_to_le32(wdata->cfile->pid);
33319141 4474
351a59da
PA
4475 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4476 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
33319141
PS
4477 req->WriteChannelInfoOffset = 0;
4478 req->WriteChannelInfoLength = 0;
4479 req->Channel = 0;
4480 req->Offset = cpu_to_le64(wdata->offset);
33319141 4481 req->DataOffset = cpu_to_le16(
f5688a6d 4482 offsetof(struct smb2_write_req, Buffer));
33319141 4483 req->RemainingBytes = 0;
d323c246
SF
4484
4485 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4486 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
db223a59
LL
4487#ifdef CONFIG_CIFS_SMB_DIRECT
4488 /*
4489 * If we want to do a server RDMA read, fill in and append
4490 * smbd_buffer_descriptor_v1 to the end of write request
4491 */
bb4c0419 4492 if (server->rdma && !server->sign && wdata->bytes >=
db223a59
LL
4493 server->smbd_conn->rdma_readwrite_threshold) {
4494
4495 struct smbd_buffer_descriptor_v1 *v1;
4496 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4497
4498 wdata->mr = smbd_register_mr(
4499 server->smbd_conn, wdata->pages,
7cf20bce
LL
4500 wdata->nr_pages, wdata->page_offset,
4501 wdata->tailsz, false, need_invalidate);
db223a59 4502 if (!wdata->mr) {
b7972092 4503 rc = -EAGAIN;
db223a59
LL
4504 goto async_writev_out;
4505 }
4506 req->Length = 0;
4507 req->DataOffset = 0;
7cf20bce
LL
4508 if (wdata->nr_pages > 1)
4509 req->RemainingBytes =
4510 cpu_to_le32(
4511 (wdata->nr_pages - 1) * wdata->pagesz -
4512 wdata->page_offset + wdata->tailsz
4513 );
4514 else
4515 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
db223a59
LL
4516 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4517 if (need_invalidate)
4518 req->Channel = SMB2_CHANNEL_RDMA_V1;
4519 req->WriteChannelInfoOffset =
2026b06e 4520 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
db223a59 4521 req->WriteChannelInfoLength =
2026b06e 4522 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
db223a59 4523 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2026b06e
SF
4524 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4525 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4526 v1->length = cpu_to_le32(wdata->mr->mr->length);
db223a59
LL
4527 }
4528#endif
c713c877
RS
4529 iov[0].iov_len = total_len - 1;
4530 iov[0].iov_base = (char *)req;
33319141 4531
738f9de5 4532 rqst.rq_iov = iov;
c713c877 4533 rqst.rq_nvec = 1;
eddb079d 4534 rqst.rq_pages = wdata->pages;
57a929a6 4535 rqst.rq_offset = wdata->page_offset;
eddb079d
JL
4536 rqst.rq_npages = wdata->nr_pages;
4537 rqst.rq_pagesz = wdata->pagesz;
4538 rqst.rq_tailsz = wdata->tailsz;
db223a59
LL
4539#ifdef CONFIG_CIFS_SMB_DIRECT
4540 if (wdata->mr) {
c713c877 4541 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
db223a59
LL
4542 rqst.rq_npages = 0;
4543 }
4544#endif
f96637be
JP
4545 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4546 wdata->offset, wdata->bytes);
33319141 4547
db223a59
LL
4548#ifdef CONFIG_CIFS_SMB_DIRECT
4549 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4550 if (!wdata->mr)
4551 req->Length = cpu_to_le32(wdata->bytes);
4552#else
33319141 4553 req->Length = cpu_to_le32(wdata->bytes);
db223a59 4554#endif
33319141 4555
335b7b62 4556 if (wdata->credits.value > 0) {
31473fc4 4557 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
cb7e9eab 4558 SMB2_MAX_BUFFER_SIZE));
88fd98a2 4559 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
9a1c67e8
PS
4560
4561 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4562 if (rc)
335b7b62 4563 goto async_writev_out;
9a1c67e8 4564
7fb8986e 4565 flags |= CIFS_HAS_CREDITS;
cb7e9eab
PS
4566 }
4567
33319141 4568 kref_get(&wdata->refcount);
9b7c18a2 4569 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3349c3a7 4570 wdata, flags, &wdata->credits);
33319141 4571
e5d04887 4572 if (rc) {
d8d9de53 4573 trace_smb3_write_err(0 /* no xid */,
351a59da 4574 req->PersistentFileId,
eccb4422
SF
4575 tcon->tid, tcon->ses->Suid, wdata->offset,
4576 wdata->bytes, rc);
4a5c80d7 4577 kref_put(&wdata->refcount, release);
e5d04887 4578 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
7d42e72f 4579 }
33319141 4580
33319141
PS
4581async_writev_out:
4582 cifs_small_buf_release(req);
33319141
PS
4583 return rc;
4584}
009d3443
PS
4585
4586/*
4587 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4588 * The length field from io_parms must be at least 1 and indicates a number of
4589 * elements with data to write that begins with position 1 in iov array. All
4590 * data length is specified by count.
4591 */
4592int
4593SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4594 unsigned int *nbytes, struct kvec *iov, int n_vec)
4595{
40eff45b 4596 struct smb_rqst rqst;
009d3443
PS
4597 int rc = 0;
4598 struct smb2_write_req *req = NULL;
4599 struct smb2_write_rsp *rsp = NULL;
4600 int resp_buftype;
da502f7d 4601 struct kvec rsp_iov;
7fb8986e 4602 int flags = 0;
f5688a6d 4603 unsigned int total_len;
352d96f3 4604 struct TCP_Server_Info *server;
da502f7d 4605
009d3443
PS
4606 *nbytes = 0;
4607
4608 if (n_vec < 1)
4609 return rc;
4610
352d96f3
AA
4611 if (!io_parms->server)
4612 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4613 server = io_parms->server;
4614 if (server == NULL)
4615 return -ECONNABORTED;
4616
4617 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4618 (void **) &req, &total_len);
009d3443
PS
4619 if (rc)
4620 return rc;
4621
5a77e75f 4622 if (smb3_encryption_required(io_parms->tcon))
7fb8986e
PS
4623 flags |= CIFS_TRANSFORM_REQ;
4624
0d35e382 4625 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
009d3443 4626
351a59da
PA
4627 req->PersistentFileId = io_parms->persistent_fid;
4628 req->VolatileFileId = io_parms->volatile_fid;
009d3443
PS
4629 req->WriteChannelInfoOffset = 0;
4630 req->WriteChannelInfoLength = 0;
4631 req->Channel = 0;
4632 req->Length = cpu_to_le32(io_parms->length);
4633 req->Offset = cpu_to_le64(io_parms->offset);
009d3443 4634 req->DataOffset = cpu_to_le16(
f5688a6d 4635 offsetof(struct smb2_write_req, Buffer));
009d3443
PS
4636 req->RemainingBytes = 0;
4637
d323c246
SF
4638 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4639 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4640 io_parms->offset, io_parms->length);
4641
009d3443 4642 iov[0].iov_base = (char *)req;
f5688a6d
RS
4643 /* 1 for Buffer */
4644 iov[0].iov_len = total_len - 1;
009d3443 4645
40eff45b
RS
4646 memset(&rqst, 0, sizeof(struct smb_rqst));
4647 rqst.rq_iov = iov;
4648 rqst.rq_nvec = n_vec + 1;
4649
352d96f3
AA
4650 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4651 &rqst,
f5688a6d 4652 &resp_buftype, flags, &rsp_iov);
da502f7d 4653 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
009d3443
PS
4654
4655 if (rc) {
d8d9de53 4656 trace_smb3_write_err(xid,
351a59da 4657 req->PersistentFileId,
eccb4422
SF
4658 io_parms->tcon->tid,
4659 io_parms->tcon->ses->Suid,
4660 io_parms->offset, io_parms->length, rc);
009d3443 4661 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
f96637be 4662 cifs_dbg(VFS, "Send error in write = %d\n", rc);
eccb4422 4663 } else {
009d3443 4664 *nbytes = le32_to_cpu(rsp->DataLength);
d8d9de53 4665 trace_smb3_write_done(xid,
351a59da 4666 req->PersistentFileId,
d8d9de53
RS
4667 io_parms->tcon->tid,
4668 io_parms->tcon->ses->Suid,
4669 io_parms->offset, *nbytes);
eccb4422 4670 }
e5d04887 4671
6a3eb336 4672 cifs_small_buf_release(req);
e5d04887 4673 free_rsp_buf(resp_buftype, rsp);
009d3443
PS
4674 return rc;
4675}
35143eb5 4676
69dda305 4677int posix_info_sid_size(const void *beg, const void *end)
349e13ad
AA
4678{
4679 size_t subauth;
4680 int total;
4681
4682 if (beg + 1 > end)
4683 return -1;
4684
4685 subauth = *(u8 *)(beg+1);
4686 if (subauth < 1 || subauth > 15)
4687 return -1;
4688
4689 total = 1 + 1 + 6 + 4*subauth;
4690 if (beg + total > end)
4691 return -1;
4692
4693 return total;
4694}
4695
4696int posix_info_parse(const void *beg, const void *end,
4697 struct smb2_posix_info_parsed *out)
4698
4699{
4700 int total_len = 0;
ca38fabc 4701 int owner_len, group_len;
349e13ad
AA
4702 int name_len;
4703 const void *owner_sid;
4704 const void *group_sid;
4705 const void *name;
4706
4707 /* if no end bound given, assume payload to be correct */
4708 if (!end) {
4709 const struct smb2_posix_info *p = beg;
4710
4711 end = beg + le32_to_cpu(p->NextEntryOffset);
4712 /* last element will have a 0 offset, pick a sensible bound */
4713 if (end == beg)
4714 end += 0xFFFF;
4715 }
4716
4717 /* check base buf */
4718 if (beg + sizeof(struct smb2_posix_info) > end)
4719 return -1;
4720 total_len = sizeof(struct smb2_posix_info);
4721
4722 /* check owner sid */
4723 owner_sid = beg + total_len;
ca38fabc
RS
4724 owner_len = posix_info_sid_size(owner_sid, end);
4725 if (owner_len < 0)
349e13ad 4726 return -1;
ca38fabc 4727 total_len += owner_len;
349e13ad
AA
4728
4729 /* check group sid */
4730 group_sid = beg + total_len;
ca38fabc
RS
4731 group_len = posix_info_sid_size(group_sid, end);
4732 if (group_len < 0)
349e13ad 4733 return -1;
ca38fabc 4734 total_len += group_len;
349e13ad
AA
4735
4736 /* check name len */
4737 if (beg + total_len + 4 > end)
4738 return -1;
4739 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4740 if (name_len < 1 || name_len > 0xFFFF)
4741 return -1;
4742 total_len += 4;
4743
4744 /* check name */
4745 name = beg + total_len;
4746 if (name + name_len > end)
4747 return -1;
4748 total_len += name_len;
4749
4750 if (out) {
4751 out->base = beg;
4752 out->size = total_len;
4753 out->name_len = name_len;
4754 out->name = name;
ca38fabc
RS
4755 memcpy(&out->owner, owner_sid, owner_len);
4756 memcpy(&out->group, group_sid, group_len);
349e13ad
AA
4757 }
4758 return total_len;
4759}
4760
4761static int posix_info_extra_size(const void *beg, const void *end)
4762{
4763 int len = posix_info_parse(beg, end, NULL);
4764
4765 if (len < 0)
4766 return -1;
4767 return len - sizeof(struct smb2_posix_info);
4768}
4769
d324f08d 4770static unsigned int
3d519bd1
AA
4771num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4772 size_t size)
d324f08d
PS
4773{
4774 int len;
4775 unsigned int entrycount = 0;
4776 unsigned int next_offset = 0;
56446f21
DC
4777 char *entryptr;
4778 FILE_DIRECTORY_INFO *dir_info;
d324f08d
PS
4779
4780 if (bufstart == NULL)
4781 return 0;
4782
56446f21 4783 entryptr = bufstart;
d324f08d
PS
4784
4785 while (1) {
56446f21
DC
4786 if (entryptr + next_offset < entryptr ||
4787 entryptr + next_offset > end_of_buf ||
4788 entryptr + next_offset + size > end_of_buf) {
f96637be 4789 cifs_dbg(VFS, "malformed search entry would overflow\n");
d324f08d
PS
4790 break;
4791 }
4792
56446f21
DC
4793 entryptr = entryptr + next_offset;
4794 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4795
3d519bd1
AA
4796 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4797 len = posix_info_extra_size(entryptr, end_of_buf);
4798 else
4799 len = le32_to_cpu(dir_info->FileNameLength);
4800
4801 if (len < 0 ||
4802 entryptr + len < entryptr ||
56446f21
DC
4803 entryptr + len > end_of_buf ||
4804 entryptr + len + size > end_of_buf) {
f96637be
JP
4805 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4806 end_of_buf);
d324f08d
PS
4807 break;
4808 }
4809
56446f21 4810 *lastentry = entryptr;
d324f08d
PS
4811 entrycount++;
4812
56446f21 4813 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
d324f08d
PS
4814 if (!next_offset)
4815 break;
4816 }
4817
4818 return entrycount;
4819}
4820
4821/*
4822 * Readdir/FindFirst
4823 */
0a17799c 4824int SMB2_query_directory_init(const unsigned int xid,
352d96f3
AA
4825 struct cifs_tcon *tcon,
4826 struct TCP_Server_Info *server,
4827 struct smb_rqst *rqst,
0a17799c
RS
4828 u64 persistent_fid, u64 volatile_fid,
4829 int index, int info_level)
d324f08d
PS
4830{
4831 struct smb2_query_directory_req *req;
d324f08d 4832 unsigned char *bufptr;
d324f08d 4833 __le16 asteriks = cpu_to_le16('*');
0a17799c
RS
4834 unsigned int output_size = CIFSMaxBufSize -
4835 MAX_SMB2_CREATE_RESPONSE_SIZE -
4836 MAX_SMB2_CLOSE_RESPONSE_SIZE;
7c00c3a6 4837 unsigned int total_len;
0a17799c
RS
4838 struct kvec *iov = rqst->rq_iov;
4839 int len, rc;
d324f08d 4840
352d96f3
AA
4841 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4842 (void **) &req, &total_len);
d324f08d
PS
4843 if (rc)
4844 return rc;
4845
0a17799c 4846 switch (info_level) {
d324f08d
PS
4847 case SMB_FIND_FILE_DIRECTORY_INFO:
4848 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
d324f08d
PS
4849 break;
4850 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4851 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
d324f08d 4852 break;
3d519bd1
AA
4853 case SMB_FIND_FILE_POSIX_INFO:
4854 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4855 break;
d324f08d 4856 default:
3175eb9b 4857 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
0a17799c
RS
4858 info_level);
4859 return -EINVAL;
d324f08d
PS
4860 }
4861
4862 req->FileIndex = cpu_to_le32(index);
4863 req->PersistentFileId = persistent_fid;
4864 req->VolatileFileId = volatile_fid;
4865
4866 len = 0x2;
4867 bufptr = req->Buffer;
4868 memcpy(bufptr, &asteriks, len);
4869
4870 req->FileNameOffset =
7c00c3a6 4871 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
d324f08d
PS
4872 req->FileNameLength = cpu_to_le16(len);
4873 /*
4874 * BB could be 30 bytes or so longer if we used SMB2 specific
4875 * buffer lengths, but this is safe and close enough.
4876 */
4877 output_size = min_t(unsigned int, output_size, server->maxBuf);
4878 output_size = min_t(unsigned int, output_size, 2 << 15);
4879 req->OutputBufferLength = cpu_to_le32(output_size);
4880
4881 iov[0].iov_base = (char *)req;
7c00c3a6
RS
4882 /* 1 for Buffer */
4883 iov[0].iov_len = total_len - 1;
d324f08d
PS
4884
4885 iov[1].iov_base = (char *)(req->Buffer);
4886 iov[1].iov_len = len;
4887
0a17799c
RS
4888 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4889 tcon->ses->Suid, index, output_size);
4890
4891 return 0;
4892}
4893
4894void SMB2_query_directory_free(struct smb_rqst *rqst)
4895{
4896 if (rqst && rqst->rq_iov) {
4897 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4898 }
4899}
4900
af08f9e7
RS
4901int
4902smb2_parse_query_directory(struct cifs_tcon *tcon,
4903 struct kvec *rsp_iov,
4904 int resp_buftype,
4905 struct cifs_search_info *srch_inf)
4906{
4907 struct smb2_query_directory_rsp *rsp;
4908 size_t info_buf_size;
4909 char *end_of_smb;
4910 int rc;
4911
4912 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4913
4914 switch (srch_inf->info_level) {
4915 case SMB_FIND_FILE_DIRECTORY_INFO:
4916 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4917 break;
4918 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4919 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4920 break;
3d519bd1
AA
4921 case SMB_FIND_FILE_POSIX_INFO:
4922 /* note that posix payload are variable size */
4923 info_buf_size = sizeof(struct smb2_posix_info);
4924 break;
af08f9e7
RS
4925 default:
4926 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4927 srch_inf->info_level);
4928 return -EINVAL;
4929 }
4930
4931 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4932 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4933 info_buf_size);
3d519bd1
AA
4934 if (rc) {
4935 cifs_tcon_dbg(VFS, "bad info payload");
af08f9e7 4936 return rc;
3d519bd1 4937 }
af08f9e7
RS
4938
4939 srch_inf->unicode = true;
4940
4941 if (srch_inf->ntwrk_buf_start) {
4942 if (srch_inf->smallBuf)
4943 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4944 else
4945 cifs_buf_release(srch_inf->ntwrk_buf_start);
4946 }
4947 srch_inf->ntwrk_buf_start = (char *)rsp;
4948 srch_inf->srch_entries_start = srch_inf->last_entry =
4949 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4950 end_of_smb = rsp_iov->iov_len + (char *)rsp;
3d519bd1
AA
4951
4952 srch_inf->entries_in_buffer = num_entries(
4953 srch_inf->info_level,
4954 srch_inf->srch_entries_start,
4955 end_of_smb,
4956 &srch_inf->last_entry,
4957 info_buf_size);
4958
af08f9e7
RS
4959 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4960 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4961 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4962 srch_inf->srch_entries_start, srch_inf->last_entry);
4963 if (resp_buftype == CIFS_LARGE_BUFFER)
4964 srch_inf->smallBuf = false;
4965 else if (resp_buftype == CIFS_SMALL_BUFFER)
4966 srch_inf->smallBuf = true;
4967 else
a0a3036b 4968 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
af08f9e7
RS
4969
4970 return 0;
4971}
4972
0a17799c
RS
4973int
4974SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4975 u64 persistent_fid, u64 volatile_fid, int index,
4976 struct cifs_search_info *srch_inf)
4977{
4978 struct smb_rqst rqst;
4979 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4980 struct smb2_query_directory_rsp *rsp = NULL;
4981 int resp_buftype = CIFS_NO_BUFFER;
4982 struct kvec rsp_iov;
4983 int rc = 0;
0a17799c 4984 struct cifs_ses *ses = tcon->ses;
352d96f3 4985 struct TCP_Server_Info *server = cifs_pick_channel(ses);
0a17799c
RS
4986 int flags = 0;
4987
c4985c3d 4988 if (!ses || !(ses->server))
0a17799c
RS
4989 return -EIO;
4990
4991 if (smb3_encryption_required(tcon))
4992 flags |= CIFS_TRANSFORM_REQ;
4993
40eff45b 4994 memset(&rqst, 0, sizeof(struct smb_rqst));
0a17799c 4995 memset(&iov, 0, sizeof(iov));
40eff45b 4996 rqst.rq_iov = iov;
0a17799c 4997 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
40eff45b 4998
352d96f3
AA
4999 rc = SMB2_query_directory_init(xid, tcon, server,
5000 &rqst, persistent_fid,
0a17799c
RS
5001 volatile_fid, index,
5002 srch_inf->info_level);
5003 if (rc)
5004 goto qdir_exit;
d323c246 5005
352d96f3
AA
5006 rc = cifs_send_recv(xid, ses, server,
5007 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 5008 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
e5d04887 5009
d324f08d 5010 if (rc) {
31473fc4 5011 if (rc == -ENODATA &&
0d35e382 5012 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
adb3b4e9
SF
5013 trace_smb3_query_dir_done(xid, persistent_fid,
5014 tcon->tid, tcon->ses->Suid, index, 0);
52755808
PS
5015 srch_inf->endOfSearch = true;
5016 rc = 0;
adb3b4e9
SF
5017 } else {
5018 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5019 tcon->ses->Suid, index, 0, rc);
8e6e72ae 5020 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
adb3b4e9 5021 }
d324f08d
PS
5022 goto qdir_exit;
5023 }
d324f08d 5024
af08f9e7
RS
5025 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5026 srch_inf);
adb3b4e9
SF
5027 if (rc) {
5028 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5029 tcon->ses->Suid, index, 0, rc);
d324f08d 5030 goto qdir_exit;
adb3b4e9 5031 }
0a17799c
RS
5032 resp_buftype = CIFS_NO_BUFFER;
5033
adb3b4e9
SF
5034 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5035 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
d324f08d
PS
5036
5037qdir_exit:
0a17799c 5038 SMB2_query_directory_free(&rqst);
d324f08d
PS
5039 free_rsp_buf(resp_buftype, rsp);
5040 return rc;
5041}
5042
ba8ca116 5043int
352d96f3
AA
5044SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5045 struct smb_rqst *rqst,
5046 u64 persistent_fid, u64 volatile_fid, u32 pid,
5047 u8 info_class, u8 info_type, u32 additional_info,
5048 void **data, unsigned int *size)
35143eb5
PS
5049{
5050 struct smb2_set_info_req *req;
ba8ca116
RS
5051 struct kvec *iov = rqst->rq_iov;
5052 unsigned int i, total_len;
5053 int rc;
35143eb5 5054
352d96f3
AA
5055 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5056 (void **) &req, &total_len);
ba8ca116 5057 if (rc)
35143eb5 5058 return rc;
7fb8986e 5059
0d35e382 5060 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
dac95340 5061 req->InfoType = info_type;
35143eb5
PS
5062 req->FileInfoClass = info_class;
5063 req->PersistentFileId = persistent_fid;
5064 req->VolatileFileId = volatile_fid;
dac95340 5065 req->AdditionalInformation = cpu_to_le32(additional_info);
35143eb5 5066
35143eb5 5067 req->BufferOffset =
2fc803ef 5068 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
35143eb5
PS
5069 req->BufferLength = cpu_to_le32(*size);
5070
35143eb5 5071 memcpy(req->Buffer, *data, *size);
2fc803ef 5072 total_len += *size;
35143eb5
PS
5073
5074 iov[0].iov_base = (char *)req;
2fc803ef
RS
5075 /* 1 for Buffer */
5076 iov[0].iov_len = total_len - 1;
35143eb5 5077
ba8ca116 5078 for (i = 1; i < rqst->rq_nvec; i++) {
35143eb5
PS
5079 le32_add_cpu(&req->BufferLength, size[i]);
5080 iov[i].iov_base = (char *)data[i];
5081 iov[i].iov_len = size[i];
5082 }
5083
ba8ca116
RS
5084 return 0;
5085}
5086
5087void
5088SMB2_set_info_free(struct smb_rqst *rqst)
5089{
32a1fb36
RS
5090 if (rqst && rqst->rq_iov)
5091 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
ba8ca116
RS
5092}
5093
5094static int
5095send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5096 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5097 u8 info_type, u32 additional_info, unsigned int num,
5098 void **data, unsigned int *size)
5099{
5100 struct smb_rqst rqst;
5101 struct smb2_set_info_rsp *rsp = NULL;
5102 struct kvec *iov;
5103 struct kvec rsp_iov;
5104 int rc = 0;
5105 int resp_buftype;
5106 struct cifs_ses *ses = tcon->ses;
352d96f3 5107 struct TCP_Server_Info *server = cifs_pick_channel(ses);
ba8ca116
RS
5108 int flags = 0;
5109
352d96f3 5110 if (!ses || !server)
ba8ca116
RS
5111 return -EIO;
5112
5113 if (!num)
5114 return -EINVAL;
5115
5116 if (smb3_encryption_required(tcon))
5117 flags |= CIFS_TRANSFORM_REQ;
5118
5119 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5120 if (!iov)
5121 return -ENOMEM;
5122
40eff45b
RS
5123 memset(&rqst, 0, sizeof(struct smb_rqst));
5124 rqst.rq_iov = iov;
5125 rqst.rq_nvec = num;
5126
352d96f3
AA
5127 rc = SMB2_set_info_init(tcon, server,
5128 &rqst, persistent_fid, volatile_fid, pid,
ba8ca116
RS
5129 info_class, info_type, additional_info,
5130 data, size);
5131 if (rc) {
5132 kfree(iov);
5133 return rc;
5134 }
5135
5136
352d96f3
AA
5137 rc = cifs_send_recv(xid, ses, server,
5138 &rqst, &resp_buftype, flags,
2fc803ef 5139 &rsp_iov);
ba8ca116 5140 SMB2_set_info_free(&rqst);
da502f7d 5141 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
35143eb5 5142
eccb4422 5143 if (rc != 0) {
35143eb5 5144 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
eccb4422
SF
5145 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5146 ses->Suid, info_class, (__u32)info_type, rc);
5147 }
7d3fb24b 5148
35143eb5
PS
5149 free_rsp_buf(resp_buftype, rsp);
5150 kfree(iov);
5151 return rc;
5152}
5153
c839ff24
PS
5154int
5155SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3764cbd1 5156 u64 volatile_fid, u32 pid, __le64 *eof)
c839ff24
PS
5157{
5158 struct smb2_file_eof_info info;
5159 void *data;
5160 unsigned int size;
5161
5162 info.EndOfFile = *eof;
5163
5164 data = &info;
5165 size = sizeof(struct smb2_file_eof_info);
5166
7c05eae8
SF
5167 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5168
3764cbd1 5169 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
dac95340
SP
5170 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5171 0, 1, &data, &size);
c839ff24 5172}
1feeaac7 5173
dac95340
SP
5174int
5175SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5176 u64 persistent_fid, u64 volatile_fid,
5177 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5178{
5179 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5180 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5181 1, (void **)&pnntsd, &pacllen);
1feeaac7 5182}
983c88a4 5183
5517554e
RS
5184int
5185SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5186 u64 persistent_fid, u64 volatile_fid,
5187 struct smb2_file_full_ea_info *buf, int len)
5188{
5189 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5190 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5191 0, 1, (void **)&buf, &len);
5192}
5193
983c88a4
PS
5194int
5195SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5196 const u64 persistent_fid, const u64 volatile_fid,
5197 __u8 oplock_level)
5198{
40eff45b 5199 struct smb_rqst rqst;
983c88a4 5200 int rc;
0d5a288d 5201 struct smb2_oplock_break *req = NULL;
21ad9487 5202 struct cifs_ses *ses = tcon->ses;
352d96f3 5203 struct TCP_Server_Info *server = cifs_pick_channel(ses);
7fb8986e 5204 int flags = CIFS_OBREAK_OP;
21ad9487
RS
5205 unsigned int total_len;
5206 struct kvec iov[1];
5207 struct kvec rsp_iov;
5208 int resp_buf_type;
983c88a4 5209
f96637be 5210 cifs_dbg(FYI, "SMB2_oplock_break\n");
352d96f3
AA
5211 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5212 (void **) &req, &total_len);
983c88a4
PS
5213 if (rc)
5214 return rc;
5215
5a77e75f 5216 if (smb3_encryption_required(tcon))
7fb8986e
PS
5217 flags |= CIFS_TRANSFORM_REQ;
5218
983c88a4
PS
5219 req->VolatileFid = volatile_fid;
5220 req->PersistentFid = persistent_fid;
5221 req->OplockLevel = oplock_level;
0d35e382 5222 req->hdr.CreditRequest = cpu_to_le16(1);
983c88a4 5223
392e1c5d 5224 flags |= CIFS_NO_RSP_BUF;
21ad9487
RS
5225
5226 iov[0].iov_base = (char *)req;
5227 iov[0].iov_len = total_len;
5228
40eff45b
RS
5229 memset(&rqst, 0, sizeof(struct smb_rqst));
5230 rqst.rq_iov = iov;
5231 rqst.rq_nvec = 1;
5232
352d96f3
AA
5233 rc = cifs_send_recv(xid, ses, server,
5234 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 5235 cifs_small_buf_release(req);
983c88a4
PS
5236
5237 if (rc) {
5238 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 5239 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
983c88a4
PS
5240 }
5241
5242 return rc;
5243}
6fc05c25 5244
730928c8
RS
5245void
5246smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5247 struct kstatfs *kst)
6fc05c25
PS
5248{
5249 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5250 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5251 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
42bec214
SP
5252 kst->f_bfree = kst->f_bavail =
5253 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
6fc05c25
PS
5254 return;
5255}
5256
2d304217
SF
5257static void
5258copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5259 struct kstatfs *kst)
5260{
5261 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5262 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5263 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5264 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5265 kst->f_bavail = kst->f_bfree;
5266 else
5267 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5268 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5269 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5270 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5271 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5272
5273 return;
5274}
2d304217 5275
6fc05c25 5276static int
352d96f3
AA
5277build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5278 struct TCP_Server_Info *server,
5279 int level, int outbuf_len, u64 persistent_fid,
5280 u64 volatile_fid)
6fc05c25
PS
5281{
5282 int rc;
5283 struct smb2_query_info_req *req;
b2fb7fec 5284 unsigned int total_len;
6fc05c25 5285
f96637be 5286 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
6fc05c25 5287
352d96f3 5288 if ((tcon->ses == NULL) || server == NULL)
6fc05c25
PS
5289 return -EIO;
5290
352d96f3
AA
5291 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5292 (void **) &req, &total_len);
6fc05c25
PS
5293 if (rc)
5294 return rc;
5295
5296 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5297 req->FileInfoClass = level;
5298 req->PersistentFileId = persistent_fid;
5299 req->VolatileFileId = volatile_fid;
b2fb7fec 5300 /* 1 for pad */
6fc05c25 5301 req->InputBufferOffset =
b2fb7fec 5302 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
6fc05c25 5303 req->OutputBufferLength = cpu_to_le32(
1fc6ad2f 5304 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
6fc05c25
PS
5305
5306 iov->iov_base = (char *)req;
b2fb7fec 5307 iov->iov_len = total_len;
6fc05c25
PS
5308 return 0;
5309}
5310
2d304217
SF
5311int
5312SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5313 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5314{
5315 struct smb_rqst rqst;
5316 struct smb2_query_info_rsp *rsp = NULL;
5317 struct kvec iov;
5318 struct kvec rsp_iov;
5319 int rc = 0;
5320 int resp_buftype;
5321 struct cifs_ses *ses = tcon->ses;
352d96f3 5322 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2d304217
SF
5323 FILE_SYSTEM_POSIX_INFO *info = NULL;
5324 int flags = 0;
5325
352d96f3
AA
5326 rc = build_qfs_info_req(&iov, tcon, server,
5327 FS_POSIX_INFORMATION,
2d304217
SF
5328 sizeof(FILE_SYSTEM_POSIX_INFO),
5329 persistent_fid, volatile_fid);
5330 if (rc)
5331 return rc;
5332
5333 if (smb3_encryption_required(tcon))
5334 flags |= CIFS_TRANSFORM_REQ;
5335
5336 memset(&rqst, 0, sizeof(struct smb_rqst));
5337 rqst.rq_iov = &iov;
5338 rqst.rq_nvec = 1;
5339
352d96f3
AA
5340 rc = cifs_send_recv(xid, ses, server,
5341 &rqst, &resp_buftype, flags, &rsp_iov);
2d304217
SF
5342 cifs_small_buf_release(iov.iov_base);
5343 if (rc) {
5344 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5345 goto posix_qfsinf_exit;
5346 }
5347 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5348
5349 info = (FILE_SYSTEM_POSIX_INFO *)(
5350 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
730928c8
RS
5351 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5352 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5353 sizeof(FILE_SYSTEM_POSIX_INFO));
2d304217
SF
5354 if (!rc)
5355 copy_posix_fs_info_to_kstatfs(info, fsdata);
5356
5357posix_qfsinf_exit:
5358 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5359 return rc;
5360}
2d304217 5361
6fc05c25
PS
5362int
5363SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5364 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5365{
40eff45b 5366 struct smb_rqst rqst;
6fc05c25
PS
5367 struct smb2_query_info_rsp *rsp = NULL;
5368 struct kvec iov;
da502f7d 5369 struct kvec rsp_iov;
6fc05c25
PS
5370 int rc = 0;
5371 int resp_buftype;
5372 struct cifs_ses *ses = tcon->ses;
352d96f3 5373 struct TCP_Server_Info *server = cifs_pick_channel(ses);
6fc05c25 5374 struct smb2_fs_full_size_info *info = NULL;
7fb8986e 5375 int flags = 0;
6fc05c25 5376
352d96f3
AA
5377 rc = build_qfs_info_req(&iov, tcon, server,
5378 FS_FULL_SIZE_INFORMATION,
6fc05c25
PS
5379 sizeof(struct smb2_fs_full_size_info),
5380 persistent_fid, volatile_fid);
5381 if (rc)
5382 return rc;
5383
5a77e75f 5384 if (smb3_encryption_required(tcon))
7fb8986e
PS
5385 flags |= CIFS_TRANSFORM_REQ;
5386
40eff45b
RS
5387 memset(&rqst, 0, sizeof(struct smb_rqst));
5388 rqst.rq_iov = &iov;
5389 rqst.rq_nvec = 1;
5390
352d96f3
AA
5391 rc = cifs_send_recv(xid, ses, server,
5392 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 5393 cifs_small_buf_release(iov.iov_base);
6fc05c25
PS
5394 if (rc) {
5395 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
34f62640 5396 goto qfsinf_exit;
6fc05c25 5397 }
da502f7d 5398 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6fc05c25 5399
1fc6ad2f 5400 info = (struct smb2_fs_full_size_info *)(
49f466bd 5401 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
730928c8
RS
5402 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5403 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5404 sizeof(struct smb2_fs_full_size_info));
6fc05c25 5405 if (!rc)
730928c8 5406 smb2_copy_fs_info_to_kstatfs(info, fsdata);
6fc05c25 5407
34f62640 5408qfsinf_exit:
da502f7d 5409 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
34f62640
SF
5410 return rc;
5411}
5412
5413int
5414SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2167114c 5415 u64 persistent_fid, u64 volatile_fid, int level)
34f62640 5416{
40eff45b 5417 struct smb_rqst rqst;
34f62640
SF
5418 struct smb2_query_info_rsp *rsp = NULL;
5419 struct kvec iov;
da502f7d 5420 struct kvec rsp_iov;
34f62640 5421 int rc = 0;
2167114c 5422 int resp_buftype, max_len, min_len;
34f62640 5423 struct cifs_ses *ses = tcon->ses;
352d96f3 5424 struct TCP_Server_Info *server = cifs_pick_channel(ses);
34f62640 5425 unsigned int rsp_len, offset;
7fb8986e 5426 int flags = 0;
34f62640 5427
2167114c
SF
5428 if (level == FS_DEVICE_INFORMATION) {
5429 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5430 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5431 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5432 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5433 min_len = MIN_FS_ATTR_INFO_SIZE;
af6a12ea
SF
5434 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5435 max_len = sizeof(struct smb3_fs_ss_info);
5436 min_len = sizeof(struct smb3_fs_ss_info);
21ba3845
SF
5437 } else if (level == FS_VOLUME_INFORMATION) {
5438 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5439 min_len = sizeof(struct smb3_fs_vol_info);
2167114c 5440 } else {
af6a12ea 5441 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2167114c
SF
5442 return -EINVAL;
5443 }
5444
352d96f3
AA
5445 rc = build_qfs_info_req(&iov, tcon, server,
5446 level, max_len,
34f62640
SF
5447 persistent_fid, volatile_fid);
5448 if (rc)
5449 return rc;
5450
5a77e75f 5451 if (smb3_encryption_required(tcon))
7fb8986e
PS
5452 flags |= CIFS_TRANSFORM_REQ;
5453
40eff45b
RS
5454 memset(&rqst, 0, sizeof(struct smb_rqst));
5455 rqst.rq_iov = &iov;
5456 rqst.rq_nvec = 1;
5457
352d96f3
AA
5458 rc = cifs_send_recv(xid, ses, server,
5459 &rqst, &resp_buftype, flags, &rsp_iov);
da502f7d 5460 cifs_small_buf_release(iov.iov_base);
34f62640
SF
5461 if (rc) {
5462 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5463 goto qfsattr_exit;
5464 }
da502f7d 5465 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
34f62640
SF
5466
5467 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5468 offset = le16_to_cpu(rsp->OutputBufferOffset);
730928c8 5469 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
2167114c
SF
5470 if (rc)
5471 goto qfsattr_exit;
5472
5473 if (level == FS_ATTRIBUTE_INFORMATION)
1fc6ad2f 5474 memcpy(&tcon->fsAttrInfo, offset
49f466bd 5475 + (char *)rsp, min_t(unsigned int,
2167114c
SF
5476 rsp_len, max_len));
5477 else if (level == FS_DEVICE_INFORMATION)
1fc6ad2f 5478 memcpy(&tcon->fsDevInfo, offset
49f466bd 5479 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
af6a12ea
SF
5480 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5481 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
1fc6ad2f 5482 (offset + (char *)rsp);
af6a12ea
SF
5483 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5484 tcon->perf_sector_size =
5485 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
21ba3845
SF
5486 } else if (level == FS_VOLUME_INFORMATION) {
5487 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5488 (offset + (char *)rsp);
5489 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5490 tcon->vol_create_time = vol_info->VolumeCreationTime;
af6a12ea 5491 }
34f62640
SF
5492
5493qfsattr_exit:
da502f7d 5494 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
6fc05c25
PS
5495 return rc;
5496}
f7ba7fe6
PS
5497
5498int
5499smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5500 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5501 const __u32 num_lock, struct smb2_lock_element *buf)
5502{
40eff45b 5503 struct smb_rqst rqst;
f7ba7fe6
PS
5504 int rc = 0;
5505 struct smb2_lock_req *req = NULL;
5506 struct kvec iov[2];
da502f7d 5507 struct kvec rsp_iov;
f7ba7fe6
PS
5508 int resp_buf_type;
5509 unsigned int count;
392e1c5d 5510 int flags = CIFS_NO_RSP_BUF;
ced93679 5511 unsigned int total_len;
352d96f3 5512 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
f7ba7fe6 5513
f96637be 5514 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
f7ba7fe6 5515
352d96f3
AA
5516 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5517 (void **) &req, &total_len);
f7ba7fe6
PS
5518 if (rc)
5519 return rc;
5520
5a77e75f 5521 if (smb3_encryption_required(tcon))
7fb8986e
PS
5522 flags |= CIFS_TRANSFORM_REQ;
5523
0d35e382 5524 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
f7ba7fe6
PS
5525 req->LockCount = cpu_to_le16(num_lock);
5526
5527 req->PersistentFileId = persist_fid;
5528 req->VolatileFileId = volatile_fid;
5529
5530 count = num_lock * sizeof(struct smb2_lock_element);
f7ba7fe6
PS
5531
5532 iov[0].iov_base = (char *)req;
ced93679 5533 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
f7ba7fe6
PS
5534 iov[1].iov_base = (char *)buf;
5535 iov[1].iov_len = count;
5536
5537 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
40eff45b
RS
5538
5539 memset(&rqst, 0, sizeof(struct smb_rqst));
5540 rqst.rq_iov = iov;
5541 rqst.rq_nvec = 2;
5542
352d96f3
AA
5543 rc = cifs_send_recv(xid, tcon->ses, server,
5544 &rqst, &resp_buf_type, flags,
ced93679 5545 &rsp_iov);
da502f7d 5546 cifs_small_buf_release(req);
f7ba7fe6 5547 if (rc) {
f96637be 5548 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
f7ba7fe6 5549 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
eccb4422
SF
5550 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5551 tcon->ses->Suid, rc);
f7ba7fe6
PS
5552 }
5553
5554 return rc;
5555}
5556
5557int
5558SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5559 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5560 const __u64 length, const __u64 offset, const __u32 lock_flags,
5561 const bool wait)
5562{
5563 struct smb2_lock_element lock;
5564
5565 lock.Offset = cpu_to_le64(offset);
5566 lock.Length = cpu_to_le64(length);
5567 lock.Flags = cpu_to_le32(lock_flags);
5568 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5569 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5570
5571 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5572}
0822f514
PS
5573
5574int
5575SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5576 __u8 *lease_key, const __le32 lease_state)
5577{
40eff45b 5578 struct smb_rqst rqst;
0822f514
PS
5579 int rc;
5580 struct smb2_lease_ack *req = NULL;
8eb7998e 5581 struct cifs_ses *ses = tcon->ses;
7fb8986e 5582 int flags = CIFS_OBREAK_OP;
8eb7998e
RS
5583 unsigned int total_len;
5584 struct kvec iov[1];
5585 struct kvec rsp_iov;
5586 int resp_buf_type;
179e44d4
SF
5587 __u64 *please_key_high;
5588 __u64 *please_key_low;
352d96f3 5589 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
0822f514 5590
f96637be 5591 cifs_dbg(FYI, "SMB2_lease_break\n");
352d96f3
AA
5592 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5593 (void **) &req, &total_len);
0822f514
PS
5594 if (rc)
5595 return rc;
5596
5a77e75f 5597 if (smb3_encryption_required(tcon))
7fb8986e
PS
5598 flags |= CIFS_TRANSFORM_REQ;
5599
0d35e382 5600 req->hdr.CreditRequest = cpu_to_le16(1);
0822f514 5601 req->StructureSize = cpu_to_le16(36);
8eb7998e 5602 total_len += 12;
0822f514
PS
5603
5604 memcpy(req->LeaseKey, lease_key, 16);
5605 req->LeaseState = lease_state;
5606
392e1c5d 5607 flags |= CIFS_NO_RSP_BUF;
8eb7998e
RS
5608
5609 iov[0].iov_base = (char *)req;
5610 iov[0].iov_len = total_len;
5611
40eff45b
RS
5612 memset(&rqst, 0, sizeof(struct smb_rqst));
5613 rqst.rq_iov = iov;
5614 rqst.rq_nvec = 1;
5615
352d96f3
AA
5616 rc = cifs_send_recv(xid, ses, server,
5617 &rqst, &resp_buf_type, flags, &rsp_iov);
da502f7d 5618 cifs_small_buf_release(req);
0822f514 5619
d339adc1
AA
5620 please_key_low = (__u64 *)lease_key;
5621 please_key_high = (__u64 *)(lease_key+8);
0822f514
PS
5622 if (rc) {
5623 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
179e44d4
SF
5624 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5625 ses->Suid, *please_key_low, *please_key_high, rc);
f96637be 5626 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
179e44d4
SF
5627 } else
5628 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5629 ses->Suid, *please_key_low, *please_key_high);
0822f514
PS
5630
5631 return rc;
5632}