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