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