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