4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
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.
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.
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
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 */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
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.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 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 */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
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 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
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.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
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);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
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",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
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.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
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
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
315 if (retries && --retries)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
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.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
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 if ((rc == -EACCES) && !tcon->retry) {
357 mutex_unlock(&tcon->ses->session_mutex);
360 mutex_unlock(&ses->session_mutex);
364 if (rc || !tcon->need_reconnect) {
365 mutex_unlock(&tcon->ses->session_mutex);
369 cifs_mark_open_files_invalid(tcon);
370 if (tcon->use_persistent)
371 tcon->need_reopen_files = true;
373 rc = __smb2_reconnect(nls_codepage, tcon);
374 mutex_unlock(&tcon->ses->session_mutex);
376 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
378 /* If sess reconnected but tcon didn't, something strange ... */
379 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
383 if (smb2_command != SMB2_INTERNAL_CMD)
384 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
386 atomic_inc(&tconInfoReconnectCount);
389 * Check if handle based operation so we know whether we can continue
390 * or not without returning to caller to reset file handle.
393 * BB Is flush done by server on drop of tcp session? Should we special
394 * case it and skip above?
396 switch (smb2_command) {
402 case SMB2_QUERY_DIRECTORY:
403 case SMB2_CHANGE_NOTIFY:
404 case SMB2_QUERY_INFO:
409 unload_nls(nls_codepage);
414 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
415 unsigned int *total_len)
417 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
418 /* lookup word count ie StructureSize from table */
419 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
422 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
423 * largest operations (Create)
427 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
428 spdu->StructureSize2 = cpu_to_le16(parmsize);
430 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
434 * Allocate and return pointer to an SMB request hdr, and set basic
435 * SMB information in the SMB header. If the return code is zero, this
436 * function must have filled in request_buf pointer.
438 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
439 void **request_buf, unsigned int *total_len)
441 /* BB eventually switch this to SMB2 specific small buf size */
442 if (smb2_command == SMB2_SET_INFO)
443 *request_buf = cifs_buf_get();
445 *request_buf = cifs_small_buf_get();
446 if (*request_buf == NULL) {
447 /* BB should we add a retry in here if not a writepage? */
451 fill_small_buf(smb2_command, tcon,
452 (struct smb2_sync_hdr *)(*request_buf),
456 uint16_t com_code = le16_to_cpu(smb2_command);
457 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
458 cifs_stats_inc(&tcon->num_smbs_sent);
464 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
465 void **request_buf, unsigned int *total_len)
469 rc = smb2_reconnect(smb2_command, tcon);
473 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
477 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
478 void **request_buf, unsigned int *total_len)
480 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
481 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
482 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
485 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
488 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
491 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
493 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
494 pneg_ctxt->DataLength = cpu_to_le16(38);
495 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
496 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_LINUX_CLIENT_SALT_SIZE);
497 get_random_bytes(pneg_ctxt->Salt, SMB311_LINUX_CLIENT_SALT_SIZE);
498 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
502 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
504 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
505 pneg_ctxt->DataLength =
506 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
507 - sizeof(struct smb2_neg_context));
508 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
509 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
510 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
511 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
515 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
517 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
518 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
519 pneg_ctxt->CipherCount = cpu_to_le16(2);
520 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
521 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
525 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
527 struct nls_table *cp = load_nls_default();
529 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
531 /* copy up to max of first 100 bytes of server name to NetName field */
532 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
533 /* context size is DataLength + minimal smb2_neg_context */
534 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
535 sizeof(struct smb2_neg_context), 8) * 8;
539 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
541 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
542 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
543 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
544 pneg_ctxt->Name[0] = 0x93;
545 pneg_ctxt->Name[1] = 0xAD;
546 pneg_ctxt->Name[2] = 0x25;
547 pneg_ctxt->Name[3] = 0x50;
548 pneg_ctxt->Name[4] = 0x9C;
549 pneg_ctxt->Name[5] = 0xB4;
550 pneg_ctxt->Name[6] = 0x11;
551 pneg_ctxt->Name[7] = 0xE7;
552 pneg_ctxt->Name[8] = 0xB4;
553 pneg_ctxt->Name[9] = 0x23;
554 pneg_ctxt->Name[10] = 0x83;
555 pneg_ctxt->Name[11] = 0xDE;
556 pneg_ctxt->Name[12] = 0x96;
557 pneg_ctxt->Name[13] = 0x8B;
558 pneg_ctxt->Name[14] = 0xCD;
559 pneg_ctxt->Name[15] = 0x7C;
563 assemble_neg_contexts(struct smb2_negotiate_req *req,
564 struct TCP_Server_Info *server, unsigned int *total_len)
566 char *pneg_ctxt = (char *)req;
567 unsigned int ctxt_len;
569 if (*total_len > 200) {
570 /* In case length corrupted don't want to overrun smb buffer */
571 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
576 * round up total_len of fixed part of SMB3 negotiate request to 8
577 * byte boundary before adding negotiate contexts
579 *total_len = roundup(*total_len, 8);
581 pneg_ctxt = (*total_len) + (char *)req;
582 req->NegotiateContextOffset = cpu_to_le32(*total_len);
584 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
585 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
586 *total_len += ctxt_len;
587 pneg_ctxt += ctxt_len;
589 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
590 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
591 *total_len += ctxt_len;
592 pneg_ctxt += ctxt_len;
594 if (server->compress_algorithm) {
595 build_compression_ctxt((struct smb2_compression_capabilities_context *)
597 ctxt_len = DIV_ROUND_UP(
598 sizeof(struct smb2_compression_capabilities_context),
600 *total_len += ctxt_len;
601 pneg_ctxt += ctxt_len;
602 req->NegotiateContextCount = cpu_to_le16(5);
604 req->NegotiateContextCount = cpu_to_le16(4);
606 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
608 *total_len += ctxt_len;
609 pneg_ctxt += ctxt_len;
611 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
612 *total_len += sizeof(struct smb2_posix_neg_context);
615 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
617 unsigned int len = le16_to_cpu(ctxt->DataLength);
619 /* If invalid preauth context warn but use what we requested, SHA-512 */
620 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
621 printk_once(KERN_WARNING "server sent bad preauth context\n");
623 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
624 pr_warn_once("server sent invalid SaltLength\n");
627 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
628 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
629 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
630 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
633 static void decode_compress_ctx(struct TCP_Server_Info *server,
634 struct smb2_compression_capabilities_context *ctxt)
636 unsigned int len = le16_to_cpu(ctxt->DataLength);
638 /* sizeof compress context is a one element compression capbility struct */
640 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
643 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
644 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
647 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
648 printk_once(KERN_WARNING "unknown compression algorithm\n");
651 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
654 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
655 struct smb2_encryption_neg_context *ctxt)
657 unsigned int len = le16_to_cpu(ctxt->DataLength);
659 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
660 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
661 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
665 if (le16_to_cpu(ctxt->CipherCount) != 1) {
666 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
669 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
670 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
671 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
672 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
675 server->cipher_type = ctxt->Ciphers[0];
676 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
680 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
681 struct TCP_Server_Info *server,
682 unsigned int len_of_smb)
684 struct smb2_neg_context *pctx;
685 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
686 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
687 unsigned int len_of_ctxts, i;
690 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
691 if (len_of_smb <= offset) {
692 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
696 len_of_ctxts = len_of_smb - offset;
698 for (i = 0; i < ctxt_cnt; i++) {
700 /* check that offset is not beyond end of SMB */
701 if (len_of_ctxts == 0)
704 if (len_of_ctxts < sizeof(struct smb2_neg_context))
707 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
708 clen = le16_to_cpu(pctx->DataLength);
709 if (clen > len_of_ctxts)
712 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
713 decode_preauth_context(
714 (struct smb2_preauth_neg_context *)pctx);
715 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
716 rc = decode_encrypt_ctx(server,
717 (struct smb2_encryption_neg_context *)pctx);
718 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
719 decode_compress_ctx(server,
720 (struct smb2_compression_capabilities_context *)pctx);
721 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
722 server->posix_ext_supported = true;
724 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
725 le16_to_cpu(pctx->ContextType));
729 /* offsets must be 8 byte aligned */
730 clen = (clen + 7) & ~0x7;
731 offset += clen + sizeof(struct smb2_neg_context);
732 len_of_ctxts -= clen;
737 static struct create_posix *
738 create_posix_buf(umode_t mode)
740 struct create_posix *buf;
742 buf = kzalloc(sizeof(struct create_posix),
747 buf->ccontext.DataOffset =
748 cpu_to_le16(offsetof(struct create_posix, Mode));
749 buf->ccontext.DataLength = cpu_to_le32(4);
750 buf->ccontext.NameOffset =
751 cpu_to_le16(offsetof(struct create_posix, Name));
752 buf->ccontext.NameLength = cpu_to_le16(16);
754 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
765 buf->Name[10] = 0x83;
766 buf->Name[11] = 0xDE;
767 buf->Name[12] = 0x96;
768 buf->Name[13] = 0x8B;
769 buf->Name[14] = 0xCD;
770 buf->Name[15] = 0x7C;
771 buf->Mode = cpu_to_le32(mode);
772 cifs_dbg(FYI, "mode on posix create 0%o", mode);
777 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
779 struct smb2_create_req *req = iov[0].iov_base;
780 unsigned int num = *num_iovec;
782 iov[num].iov_base = create_posix_buf(mode);
783 if (mode == ACL_NO_MODE)
784 cifs_dbg(FYI, "illegal mode\n");
785 if (iov[num].iov_base == NULL)
787 iov[num].iov_len = sizeof(struct create_posix);
788 if (!req->CreateContextsOffset)
789 req->CreateContextsOffset = cpu_to_le32(
790 sizeof(struct smb2_create_req) +
791 iov[num - 1].iov_len);
792 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
793 *num_iovec = num + 1;
800 * SMB2 Worker functions follow:
802 * The general structure of the worker functions is:
803 * 1) Call smb2_init (assembles SMB2 header)
804 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
805 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
806 * 4) Decode SMB2 command specific fields in the fixed length area
807 * 5) Decode variable length data area (if any for this SMB2 command type)
808 * 6) Call free smb buffer
814 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
816 struct smb_rqst rqst;
817 struct smb2_negotiate_req *req;
818 struct smb2_negotiate_rsp *rsp;
823 struct TCP_Server_Info *server = ses->server;
824 int blob_offset, blob_length;
826 int flags = CIFS_NEG_OP;
827 unsigned int total_len;
829 cifs_dbg(FYI, "Negotiate protocol\n");
832 WARN(1, "%s: server is NULL!\n", __func__);
836 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
840 req->sync_hdr.SessionId = 0;
842 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
843 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
845 if (strcmp(ses->server->vals->version_string,
846 SMB3ANY_VERSION_STRING) == 0) {
847 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
848 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
849 req->DialectCount = cpu_to_le16(2);
851 } else if (strcmp(server->vals->version_string,
852 SMBDEFAULT_VERSION_STRING) == 0) {
853 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
854 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
855 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
856 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
857 req->DialectCount = cpu_to_le16(4);
860 /* otherwise send specific dialect */
861 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
862 req->DialectCount = cpu_to_le16(1);
866 /* only one of SMB2 signing flags may be set in SMB2 request */
868 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
869 else if (global_secflags & CIFSSEC_MAY_SIGN)
870 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
872 req->SecurityMode = 0;
874 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
876 /* ClientGUID must be zero for SMB2.02 dialect */
877 if (server->vals->protocol_id == SMB20_PROT_ID)
878 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
880 memcpy(req->ClientGUID, server->client_guid,
881 SMB2_CLIENT_GUID_SIZE);
882 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
883 (strcmp(server->vals->version_string,
884 SMBDEFAULT_VERSION_STRING) == 0))
885 assemble_neg_contexts(req, server, &total_len);
887 iov[0].iov_base = (char *)req;
888 iov[0].iov_len = total_len;
890 memset(&rqst, 0, sizeof(struct smb_rqst));
894 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
895 cifs_small_buf_release(req);
896 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
898 * No tcon so can't do
899 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
901 if (rc == -EOPNOTSUPP) {
902 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
903 "specifying vers=1.0 or vers=2.0 on mount for accessing"
909 if (strcmp(server->vals->version_string,
910 SMB3ANY_VERSION_STRING) == 0) {
911 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
913 "SMB2 dialect returned but not requested\n");
915 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
917 "SMB2.1 dialect returned but not requested\n");
920 } else if (strcmp(server->vals->version_string,
921 SMBDEFAULT_VERSION_STRING) == 0) {
922 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
924 "SMB2 dialect returned but not requested\n");
926 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
927 /* ops set to 3.0 by default for default so update */
928 server->ops = &smb21_operations;
929 server->vals = &smb21_values;
930 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
931 server->ops = &smb311_operations;
932 server->vals = &smb311_values;
934 } else if (le16_to_cpu(rsp->DialectRevision) !=
935 server->vals->protocol_id) {
936 /* if requested single dialect ensure returned dialect matched */
937 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
938 le16_to_cpu(rsp->DialectRevision));
942 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
944 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
945 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
946 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
947 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
948 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
949 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
950 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
951 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
952 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
953 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
955 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
956 le16_to_cpu(rsp->DialectRevision));
960 server->dialect = le16_to_cpu(rsp->DialectRevision);
963 * Keep a copy of the hash after negprot. This hash will be
964 * the starting hash value for all sessions made from this
967 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
968 SMB2_PREAUTH_HASH_SIZE);
970 /* SMB2 only has an extended negflavor */
971 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
972 /* set it to the maximum buffer size value we can send with 1 credit */
973 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
974 SMB2_MAX_BUFFER_SIZE);
975 server->max_read = le32_to_cpu(rsp->MaxReadSize);
976 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
977 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
978 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
979 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
981 server->capabilities = le32_to_cpu(rsp->Capabilities);
983 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
986 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
987 * Set the cipher type manually.
989 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
990 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
992 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
993 (struct smb2_sync_hdr *)rsp);
995 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
997 * ses->sectype = RawNTLMSSP;
998 * but for time being this is our only auth choice so doesn't matter.
999 * We just found a server which sets blob length to zero expecting raw.
1001 if (blob_length == 0) {
1002 cifs_dbg(FYI, "missing security blob on negprot\n");
1003 server->sec_ntlmssp = true;
1006 rc = cifs_enable_signing(server, ses->sign);
1010 rc = decode_negTokenInit(security_blob, blob_length, server);
1017 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1018 if (rsp->NegotiateContextCount)
1019 rc = smb311_decode_neg_context(rsp, server,
1022 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1025 free_rsp_buf(resp_buftype, rsp);
1029 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1032 struct validate_negotiate_info_req *pneg_inbuf;
1033 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1035 u32 inbuflen; /* max of 4 dialects */
1036 struct TCP_Server_Info *server = tcon->ses->server;
1038 cifs_dbg(FYI, "validate negotiate\n");
1040 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1041 if (server->dialect == SMB311_PROT_ID)
1045 * validation ioctl must be signed, so no point sending this if we
1046 * can not sign it (ie are not known user). Even if signing is not
1047 * required (enabled but not negotiated), in those cases we selectively
1048 * sign just this, the first and only signed request on a connection.
1049 * Having validation of negotiate info helps reduce attack vectors.
1051 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1052 return 0; /* validation requires signing */
1054 if (tcon->ses->user_name == NULL) {
1055 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1056 return 0; /* validation requires signing */
1059 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1060 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1062 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1066 pneg_inbuf->Capabilities =
1067 cpu_to_le32(server->vals->req_capabilities);
1068 memcpy(pneg_inbuf->Guid, server->client_guid,
1069 SMB2_CLIENT_GUID_SIZE);
1071 if (tcon->ses->sign)
1072 pneg_inbuf->SecurityMode =
1073 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1074 else if (global_secflags & CIFSSEC_MAY_SIGN)
1075 pneg_inbuf->SecurityMode =
1076 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1078 pneg_inbuf->SecurityMode = 0;
1081 if (strcmp(server->vals->version_string,
1082 SMB3ANY_VERSION_STRING) == 0) {
1083 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1084 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1085 pneg_inbuf->DialectCount = cpu_to_le16(2);
1086 /* structure is big enough for 3 dialects, sending only 2 */
1087 inbuflen = sizeof(*pneg_inbuf) -
1088 (2 * sizeof(pneg_inbuf->Dialects[0]));
1089 } else if (strcmp(server->vals->version_string,
1090 SMBDEFAULT_VERSION_STRING) == 0) {
1091 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1092 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1093 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1094 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1095 pneg_inbuf->DialectCount = cpu_to_le16(4);
1096 /* structure is big enough for 3 dialects */
1097 inbuflen = sizeof(*pneg_inbuf);
1099 /* otherwise specific dialect was requested */
1100 pneg_inbuf->Dialects[0] =
1101 cpu_to_le16(server->vals->protocol_id);
1102 pneg_inbuf->DialectCount = cpu_to_le16(1);
1103 /* structure is big enough for 4 dialects, sending only 1 */
1104 inbuflen = sizeof(*pneg_inbuf) -
1105 sizeof(pneg_inbuf->Dialects[0]) * 3;
1108 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1109 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1110 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1111 (char **)&pneg_rsp, &rsplen);
1112 if (rc == -EOPNOTSUPP) {
1114 * Old Windows versions or Netapp SMB server can return
1115 * not supported error. Client should accept it.
1117 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1119 goto out_free_inbuf;
1120 } else if (rc != 0) {
1121 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1123 goto out_free_inbuf;
1127 if (rsplen != sizeof(*pneg_rsp)) {
1128 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1131 /* relax check since Mac returns max bufsize allowed on ioctl */
1132 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1136 /* check validate negotiate info response matches what we got earlier */
1137 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1140 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1143 /* do not validate server guid because not saved at negprot time yet */
1145 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1146 SMB2_LARGE_FILES) != server->capabilities)
1149 /* validate negotiate successful */
1151 cifs_dbg(FYI, "validate negotiate info successful\n");
1155 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1164 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1166 switch (requested) {
1173 if (server->sec_ntlmssp &&
1174 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1176 if ((server->sec_kerberos || server->sec_mskerberos) &&
1177 (global_secflags & CIFSSEC_MAY_KRB5))
1185 struct SMB2_sess_data {
1187 struct cifs_ses *ses;
1188 struct nls_table *nls_cp;
1189 void (*func)(struct SMB2_sess_data *);
1191 u64 previous_session;
1193 /* we will send the SMB in three pieces:
1194 * a fixed length beginning part, an optional
1195 * SPNEGO blob (which can be zero length), and a
1196 * last part which will include the strings
1197 * and rest of bcc area. This allows us to avoid
1198 * a large buffer 17K allocation
1205 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1208 struct cifs_ses *ses = sess_data->ses;
1209 struct smb2_sess_setup_req *req;
1210 struct TCP_Server_Info *server = ses->server;
1211 unsigned int total_len;
1213 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1218 /* First session, not a reauthenticate */
1219 req->sync_hdr.SessionId = 0;
1221 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1222 req->PreviousSessionId = sess_data->previous_session;
1224 req->Flags = 0; /* MBZ */
1226 /* enough to enable echos and oplocks and one max size write */
1227 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1229 /* only one of SMB2 signing flags may be set in SMB2 request */
1231 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1232 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1233 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1235 req->SecurityMode = 0;
1237 #ifdef CONFIG_CIFS_DFS_UPCALL
1238 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1240 req->Capabilities = 0;
1241 #endif /* DFS_UPCALL */
1243 req->Channel = 0; /* MBZ */
1245 sess_data->iov[0].iov_base = (char *)req;
1247 sess_data->iov[0].iov_len = total_len - 1;
1249 * This variable will be used to clear the buffer
1250 * allocated above in case of any error in the calling function.
1252 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1258 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1260 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1261 sess_data->buf0_type = CIFS_NO_BUFFER;
1265 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1268 struct smb_rqst rqst;
1269 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1270 struct kvec rsp_iov = { NULL, 0 };
1272 /* Testing shows that buffer offset must be at location of Buffer[0] */
1273 req->SecurityBufferOffset =
1274 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1275 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1277 memset(&rqst, 0, sizeof(struct smb_rqst));
1278 rqst.rq_iov = sess_data->iov;
1281 /* BB add code to build os and lm fields */
1282 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1284 &sess_data->buf0_type,
1285 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1286 cifs_small_buf_release(sess_data->iov[0].iov_base);
1287 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1293 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1296 struct cifs_ses *ses = sess_data->ses;
1298 mutex_lock(&ses->server->srv_mutex);
1299 if (ses->server->ops->generate_signingkey) {
1300 rc = ses->server->ops->generate_signingkey(ses);
1303 "SMB3 session key generation failed\n");
1304 mutex_unlock(&ses->server->srv_mutex);
1308 if (!ses->server->session_estab) {
1309 ses->server->sequence_number = 0x2;
1310 ses->server->session_estab = true;
1312 mutex_unlock(&ses->server->srv_mutex);
1314 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1315 spin_lock(&GlobalMid_Lock);
1316 ses->status = CifsGood;
1317 ses->need_reconnect = false;
1318 spin_unlock(&GlobalMid_Lock);
1322 #ifdef CONFIG_CIFS_UPCALL
1324 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1327 struct cifs_ses *ses = sess_data->ses;
1328 struct cifs_spnego_msg *msg;
1329 struct key *spnego_key = NULL;
1330 struct smb2_sess_setup_rsp *rsp = NULL;
1332 rc = SMB2_sess_alloc_buffer(sess_data);
1336 spnego_key = cifs_get_spnego_key(ses);
1337 if (IS_ERR(spnego_key)) {
1338 rc = PTR_ERR(spnego_key);
1340 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1345 msg = spnego_key->payload.data[0];
1347 * check version field to make sure that cifs.upcall is
1348 * sending us a response in an expected form
1350 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1352 "bad cifs.upcall version. Expected %d got %d",
1353 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1355 goto out_put_spnego_key;
1358 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1360 if (!ses->auth_key.response) {
1362 "Kerberos can't allocate (%u bytes) memory",
1365 goto out_put_spnego_key;
1367 ses->auth_key.len = msg->sesskey_len;
1369 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1370 sess_data->iov[1].iov_len = msg->secblob_len;
1372 rc = SMB2_sess_sendreceive(sess_data);
1374 goto out_put_spnego_key;
1376 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1377 ses->Suid = rsp->sync_hdr.SessionId;
1379 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1381 rc = SMB2_sess_establish_session(sess_data);
1383 key_invalidate(spnego_key);
1384 key_put(spnego_key);
1386 sess_data->result = rc;
1387 sess_data->func = NULL;
1388 SMB2_sess_free_buffer(sess_data);
1392 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1394 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1395 sess_data->result = -EOPNOTSUPP;
1396 sess_data->func = NULL;
1401 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1404 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1407 struct cifs_ses *ses = sess_data->ses;
1408 struct smb2_sess_setup_rsp *rsp = NULL;
1409 char *ntlmssp_blob = NULL;
1410 bool use_spnego = false; /* else use raw ntlmssp */
1411 u16 blob_length = 0;
1414 * If memory allocation is successful, caller of this function
1417 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1418 if (!ses->ntlmssp) {
1422 ses->ntlmssp->sesskey_per_smbsess = true;
1424 rc = SMB2_sess_alloc_buffer(sess_data);
1428 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1430 if (ntlmssp_blob == NULL) {
1435 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1437 /* BB eventually need to add this */
1438 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1442 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1443 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1445 sess_data->iov[1].iov_base = ntlmssp_blob;
1446 sess_data->iov[1].iov_len = blob_length;
1448 rc = SMB2_sess_sendreceive(sess_data);
1449 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1451 /* If true, rc here is expected and not an error */
1452 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1453 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1459 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1460 le16_to_cpu(rsp->SecurityBufferOffset)) {
1461 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1462 le16_to_cpu(rsp->SecurityBufferOffset));
1466 rc = decode_ntlmssp_challenge(rsp->Buffer,
1467 le16_to_cpu(rsp->SecurityBufferLength), ses);
1471 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1474 ses->Suid = rsp->sync_hdr.SessionId;
1475 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1478 kfree(ntlmssp_blob);
1479 SMB2_sess_free_buffer(sess_data);
1481 sess_data->result = 0;
1482 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1486 kfree(ses->ntlmssp);
1487 ses->ntlmssp = NULL;
1488 sess_data->result = rc;
1489 sess_data->func = NULL;
1493 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1496 struct cifs_ses *ses = sess_data->ses;
1497 struct smb2_sess_setup_req *req;
1498 struct smb2_sess_setup_rsp *rsp = NULL;
1499 unsigned char *ntlmssp_blob = NULL;
1500 bool use_spnego = false; /* else use raw ntlmssp */
1501 u16 blob_length = 0;
1503 rc = SMB2_sess_alloc_buffer(sess_data);
1507 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1508 req->sync_hdr.SessionId = ses->Suid;
1510 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1513 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1518 /* BB eventually need to add this */
1519 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1523 sess_data->iov[1].iov_base = ntlmssp_blob;
1524 sess_data->iov[1].iov_len = blob_length;
1526 rc = SMB2_sess_sendreceive(sess_data);
1530 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1532 ses->Suid = rsp->sync_hdr.SessionId;
1533 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1535 rc = SMB2_sess_establish_session(sess_data);
1537 kfree(ntlmssp_blob);
1538 SMB2_sess_free_buffer(sess_data);
1539 kfree(ses->ntlmssp);
1540 ses->ntlmssp = NULL;
1541 sess_data->result = rc;
1542 sess_data->func = NULL;
1546 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1550 type = smb2_select_sectype(ses->server, ses->sectype);
1551 cifs_dbg(FYI, "sess setup type %d\n", type);
1552 if (type == Unspecified) {
1554 "Unable to select appropriate authentication method!");
1560 sess_data->func = SMB2_auth_kerberos;
1563 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1566 cifs_dbg(VFS, "secType %d not supported!\n", type);
1574 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1575 const struct nls_table *nls_cp)
1578 struct TCP_Server_Info *server = ses->server;
1579 struct SMB2_sess_data *sess_data;
1581 cifs_dbg(FYI, "Session Setup\n");
1584 WARN(1, "%s: server is NULL!\n", __func__);
1588 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1592 rc = SMB2_select_sec(ses, sess_data);
1595 sess_data->xid = xid;
1596 sess_data->ses = ses;
1597 sess_data->buf0_type = CIFS_NO_BUFFER;
1598 sess_data->nls_cp = (struct nls_table *) nls_cp;
1599 sess_data->previous_session = ses->Suid;
1602 * Initialize the session hash with the server one.
1604 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1605 SMB2_PREAUTH_HASH_SIZE);
1607 while (sess_data->func)
1608 sess_data->func(sess_data);
1610 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1611 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1612 rc = sess_data->result;
1619 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1621 struct smb_rqst rqst;
1622 struct smb2_logoff_req *req; /* response is also trivial struct */
1624 struct TCP_Server_Info *server;
1626 unsigned int total_len;
1628 struct kvec rsp_iov;
1631 cifs_dbg(FYI, "disconnect session %p\n", ses);
1633 if (ses && (ses->server))
1634 server = ses->server;
1638 /* no need to send SMB logoff if uid already closed due to reconnect */
1639 if (ses->need_reconnect)
1640 goto smb2_session_already_dead;
1642 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1646 /* since no tcon, smb2_init can not do this, so do here */
1647 req->sync_hdr.SessionId = ses->Suid;
1649 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1650 flags |= CIFS_TRANSFORM_REQ;
1651 else if (server->sign)
1652 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1654 flags |= CIFS_NO_RSP_BUF;
1656 iov[0].iov_base = (char *)req;
1657 iov[0].iov_len = total_len;
1659 memset(&rqst, 0, sizeof(struct smb_rqst));
1663 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1664 cifs_small_buf_release(req);
1666 * No tcon so can't do
1667 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1670 smb2_session_already_dead:
1674 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1676 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1679 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1681 /* These are similar values to what Windows uses */
1682 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1684 tcon->max_chunks = 256;
1685 tcon->max_bytes_chunk = 1048576;
1686 tcon->max_bytes_copy = 16777216;
1690 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1691 struct cifs_tcon *tcon, const struct nls_table *cp)
1693 struct smb_rqst rqst;
1694 struct smb2_tree_connect_req *req;
1695 struct smb2_tree_connect_rsp *rsp = NULL;
1697 struct kvec rsp_iov = { NULL, 0 };
1701 __le16 *unc_path = NULL;
1703 unsigned int total_len;
1704 struct TCP_Server_Info *server = ses->server;
1706 cifs_dbg(FYI, "TCON\n");
1708 if (!server || !tree)
1711 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1712 if (unc_path == NULL)
1715 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1717 if (unc_path_len < 2) {
1722 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1724 atomic_set(&tcon->num_remote_opens, 0);
1725 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1732 if (smb3_encryption_required(tcon))
1733 flags |= CIFS_TRANSFORM_REQ;
1735 iov[0].iov_base = (char *)req;
1737 iov[0].iov_len = total_len - 1;
1739 /* Testing shows that buffer offset must be at location of Buffer[0] */
1740 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1742 req->PathLength = cpu_to_le16(unc_path_len - 2);
1743 iov[1].iov_base = unc_path;
1744 iov[1].iov_len = unc_path_len;
1747 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1748 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1749 * (Samba servers don't always set the flag so also check if null user)
1751 if ((server->dialect == SMB311_PROT_ID) &&
1752 !smb3_encryption_required(tcon) &&
1753 !(ses->session_flags &
1754 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1755 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1756 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1758 memset(&rqst, 0, sizeof(struct smb_rqst));
1762 /* Need 64 for max size write so ask for more in case not there yet */
1763 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1765 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1766 cifs_small_buf_release(req);
1767 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1768 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1771 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1772 tcon->need_reconnect = true;
1774 goto tcon_error_exit;
1777 switch (rsp->ShareType) {
1778 case SMB2_SHARE_TYPE_DISK:
1779 cifs_dbg(FYI, "connection to disk share\n");
1781 case SMB2_SHARE_TYPE_PIPE:
1783 cifs_dbg(FYI, "connection to pipe share\n");
1785 case SMB2_SHARE_TYPE_PRINT:
1787 cifs_dbg(FYI, "connection to printer\n");
1790 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1792 goto tcon_error_exit;
1795 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1796 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1797 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1798 tcon->tidStatus = CifsGood;
1799 tcon->need_reconnect = false;
1800 tcon->tid = rsp->sync_hdr.TreeId;
1801 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1803 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1804 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1805 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1808 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1809 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1811 init_copy_chunk_defaults(tcon);
1812 if (server->ops->validate_negotiate)
1813 rc = server->ops->validate_negotiate(xid, tcon);
1816 free_rsp_buf(resp_buftype, rsp);
1821 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1822 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1828 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1830 struct smb_rqst rqst;
1831 struct smb2_tree_disconnect_req *req; /* response is trivial */
1833 struct cifs_ses *ses = tcon->ses;
1835 unsigned int total_len;
1837 struct kvec rsp_iov;
1840 cifs_dbg(FYI, "Tree Disconnect\n");
1842 if (!ses || !(ses->server))
1845 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1848 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1853 if (smb3_encryption_required(tcon))
1854 flags |= CIFS_TRANSFORM_REQ;
1856 flags |= CIFS_NO_RSP_BUF;
1858 iov[0].iov_base = (char *)req;
1859 iov[0].iov_len = total_len;
1861 memset(&rqst, 0, sizeof(struct smb_rqst));
1865 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1866 cifs_small_buf_release(req);
1868 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1874 static struct create_durable *
1875 create_durable_buf(void)
1877 struct create_durable *buf;
1879 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1883 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1884 (struct create_durable, Data));
1885 buf->ccontext.DataLength = cpu_to_le32(16);
1886 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1887 (struct create_durable, Name));
1888 buf->ccontext.NameLength = cpu_to_le16(4);
1889 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1897 static struct create_durable *
1898 create_reconnect_durable_buf(struct cifs_fid *fid)
1900 struct create_durable *buf;
1902 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1906 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1907 (struct create_durable, Data));
1908 buf->ccontext.DataLength = cpu_to_le32(16);
1909 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1910 (struct create_durable, Name));
1911 buf->ccontext.NameLength = cpu_to_le16(4);
1912 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1913 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1914 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1923 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1925 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1927 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1928 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1929 buf->IndexNumber = pdisk_id->DiskFileId;
1933 smb2_parse_contexts(struct TCP_Server_Info *server,
1934 struct smb2_create_rsp *rsp,
1935 unsigned int *epoch, char *lease_key, __u8 *oplock,
1936 struct smb2_file_all_info *buf)
1939 struct create_context *cc;
1941 unsigned int remaining;
1945 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1946 remaining = le32_to_cpu(rsp->CreateContextsLength);
1947 cc = (struct create_context *)data_offset;
1949 /* Initialize inode number to 0 in case no valid data in qfid context */
1951 buf->IndexNumber = 0;
1953 while (remaining >= sizeof(struct create_context)) {
1954 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1955 if (le16_to_cpu(cc->NameLength) == 4 &&
1956 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1957 *oplock = server->ops->parse_lease_buf(cc, epoch,
1959 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1960 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1961 parse_query_id_ctxt(cc, buf);
1963 next = le32_to_cpu(cc->Next);
1967 cc = (struct create_context *)((char *)cc + next);
1970 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1971 *oplock = rsp->OplockLevel;
1977 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1978 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1980 struct smb2_create_req *req = iov[0].iov_base;
1981 unsigned int num = *num_iovec;
1983 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1984 if (iov[num].iov_base == NULL)
1986 iov[num].iov_len = server->vals->create_lease_size;
1987 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1988 if (!req->CreateContextsOffset)
1989 req->CreateContextsOffset = cpu_to_le32(
1990 sizeof(struct smb2_create_req) +
1991 iov[num - 1].iov_len);
1992 le32_add_cpu(&req->CreateContextsLength,
1993 server->vals->create_lease_size);
1994 *num_iovec = num + 1;
1998 static struct create_durable_v2 *
1999 create_durable_v2_buf(struct cifs_open_parms *oparms)
2001 struct cifs_fid *pfid = oparms->fid;
2002 struct create_durable_v2 *buf;
2004 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2008 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2009 (struct create_durable_v2, dcontext));
2010 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2011 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2012 (struct create_durable_v2, Name));
2013 buf->ccontext.NameLength = cpu_to_le16(4);
2016 * NB: Handle timeout defaults to 0, which allows server to choose
2017 * (most servers default to 120 seconds) and most clients default to 0.
2018 * This can be overridden at mount ("handletimeout=") if the user wants
2019 * a different persistent (or resilient) handle timeout for all opens
2020 * opens on a particular SMB3 mount.
2022 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2023 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2024 generate_random_uuid(buf->dcontext.CreateGuid);
2025 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2027 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2035 static struct create_durable_handle_reconnect_v2 *
2036 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2038 struct create_durable_handle_reconnect_v2 *buf;
2040 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2045 buf->ccontext.DataOffset =
2046 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2048 buf->ccontext.DataLength =
2049 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2050 buf->ccontext.NameOffset =
2051 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2053 buf->ccontext.NameLength = cpu_to_le16(4);
2055 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2056 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2057 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2058 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2060 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2069 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2070 struct cifs_open_parms *oparms)
2072 struct smb2_create_req *req = iov[0].iov_base;
2073 unsigned int num = *num_iovec;
2075 iov[num].iov_base = create_durable_v2_buf(oparms);
2076 if (iov[num].iov_base == NULL)
2078 iov[num].iov_len = sizeof(struct create_durable_v2);
2079 if (!req->CreateContextsOffset)
2080 req->CreateContextsOffset =
2081 cpu_to_le32(sizeof(struct smb2_create_req) +
2083 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2084 *num_iovec = num + 1;
2089 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2090 struct cifs_open_parms *oparms)
2092 struct smb2_create_req *req = iov[0].iov_base;
2093 unsigned int num = *num_iovec;
2095 /* indicate that we don't need to relock the file */
2096 oparms->reconnect = false;
2098 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2099 if (iov[num].iov_base == NULL)
2101 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2102 if (!req->CreateContextsOffset)
2103 req->CreateContextsOffset =
2104 cpu_to_le32(sizeof(struct smb2_create_req) +
2106 le32_add_cpu(&req->CreateContextsLength,
2107 sizeof(struct create_durable_handle_reconnect_v2));
2108 *num_iovec = num + 1;
2113 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2114 struct cifs_open_parms *oparms, bool use_persistent)
2116 struct smb2_create_req *req = iov[0].iov_base;
2117 unsigned int num = *num_iovec;
2119 if (use_persistent) {
2120 if (oparms->reconnect)
2121 return add_durable_reconnect_v2_context(iov, num_iovec,
2124 return add_durable_v2_context(iov, num_iovec, oparms);
2127 if (oparms->reconnect) {
2128 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2129 /* indicate that we don't need to relock the file */
2130 oparms->reconnect = false;
2132 iov[num].iov_base = create_durable_buf();
2133 if (iov[num].iov_base == NULL)
2135 iov[num].iov_len = sizeof(struct create_durable);
2136 if (!req->CreateContextsOffset)
2137 req->CreateContextsOffset =
2138 cpu_to_le32(sizeof(struct smb2_create_req) +
2140 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2141 *num_iovec = num + 1;
2145 /* See MS-SMB2 2.2.13.2.7 */
2146 static struct crt_twarp_ctxt *
2147 create_twarp_buf(__u64 timewarp)
2149 struct crt_twarp_ctxt *buf;
2151 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2155 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2156 (struct crt_twarp_ctxt, Timestamp));
2157 buf->ccontext.DataLength = cpu_to_le32(8);
2158 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2159 (struct crt_twarp_ctxt, Name));
2160 buf->ccontext.NameLength = cpu_to_le16(4);
2161 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2166 buf->Timestamp = cpu_to_le64(timewarp);
2170 /* See MS-SMB2 2.2.13.2.7 */
2172 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2174 struct smb2_create_req *req = iov[0].iov_base;
2175 unsigned int num = *num_iovec;
2177 iov[num].iov_base = create_twarp_buf(timewarp);
2178 if (iov[num].iov_base == NULL)
2180 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2181 if (!req->CreateContextsOffset)
2182 req->CreateContextsOffset = cpu_to_le32(
2183 sizeof(struct smb2_create_req) +
2184 iov[num - 1].iov_len);
2185 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2186 *num_iovec = num + 1;
2190 static struct crt_query_id_ctxt *
2191 create_query_id_buf(void)
2193 struct crt_query_id_ctxt *buf;
2195 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2199 buf->ccontext.DataOffset = cpu_to_le16(0);
2200 buf->ccontext.DataLength = cpu_to_le32(0);
2201 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2202 (struct crt_query_id_ctxt, Name));
2203 buf->ccontext.NameLength = cpu_to_le16(4);
2204 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2212 /* See MS-SMB2 2.2.13.2.9 */
2214 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2216 struct smb2_create_req *req = iov[0].iov_base;
2217 unsigned int num = *num_iovec;
2219 iov[num].iov_base = create_query_id_buf();
2220 if (iov[num].iov_base == NULL)
2222 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2223 if (!req->CreateContextsOffset)
2224 req->CreateContextsOffset = cpu_to_le32(
2225 sizeof(struct smb2_create_req) +
2226 iov[num - 1].iov_len);
2227 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2228 *num_iovec = num + 1;
2233 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2234 const char *treename, const __le16 *path)
2236 int treename_len, path_len;
2237 struct nls_table *cp;
2238 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2243 treename_len = strlen(treename);
2244 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2250 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2253 * make room for one path separator between the treename and
2256 *out_len = treename_len + 1 + path_len;
2259 * final path needs to be null-terminated UTF16 with a
2263 *out_size = roundup((*out_len+1)*2, 8);
2264 *out_path = kzalloc(*out_size, GFP_KERNEL);
2268 cp = load_nls_default();
2269 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2270 UniStrcat(*out_path, sep);
2271 UniStrcat(*out_path, path);
2277 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2278 umode_t mode, struct cifs_tcon *tcon,
2279 const char *full_path,
2280 struct cifs_sb_info *cifs_sb)
2282 struct smb_rqst rqst;
2283 struct smb2_create_req *req;
2284 struct smb2_create_rsp *rsp = NULL;
2285 struct cifs_ses *ses = tcon->ses;
2286 struct kvec iov[3]; /* make sure at least one for each open context */
2287 struct kvec rsp_iov = {NULL, 0};
2290 __le16 *copy_path = NULL;
2293 unsigned int n_iov = 2;
2294 __u32 file_attributes = 0;
2295 char *pc_buf = NULL;
2297 unsigned int total_len;
2298 __le16 *utf16_path = NULL;
2300 cifs_dbg(FYI, "mkdir\n");
2302 /* resource #1: path allocation */
2303 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2307 if (!ses || !(ses->server)) {
2312 /* resource #2: request */
2313 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2318 if (smb3_encryption_required(tcon))
2319 flags |= CIFS_TRANSFORM_REQ;
2321 req->ImpersonationLevel = IL_IMPERSONATION;
2322 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2323 /* File attributes ignored on open (used in create though) */
2324 req->FileAttributes = cpu_to_le32(file_attributes);
2325 req->ShareAccess = FILE_SHARE_ALL_LE;
2326 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2327 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2329 iov[0].iov_base = (char *)req;
2330 /* -1 since last byte is buf[0] which is sent below (path) */
2331 iov[0].iov_len = total_len - 1;
2333 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2335 /* [MS-SMB2] 2.2.13 NameOffset:
2336 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2337 * the SMB2 header, the file name includes a prefix that will
2338 * be processed during DFS name normalization as specified in
2339 * section 3.3.5.9. Otherwise, the file name is relative to
2340 * the share that is identified by the TreeId in the SMB2
2343 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2346 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2347 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2349 tcon->treeName, utf16_path);
2353 req->NameLength = cpu_to_le16(name_len * 2);
2354 uni_path_len = copy_size;
2355 /* free before overwriting resource */
2357 utf16_path = copy_path;
2359 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2360 /* MUST set path len (NameLength) to 0 opening root of share */
2361 req->NameLength = cpu_to_le16(uni_path_len - 2);
2362 if (uni_path_len % 8 != 0) {
2363 copy_size = roundup(uni_path_len, 8);
2364 copy_path = kzalloc(copy_size, GFP_KERNEL);
2369 memcpy((char *)copy_path, (const char *)utf16_path,
2371 uni_path_len = copy_size;
2372 /* free before overwriting resource */
2374 utf16_path = copy_path;
2378 iov[1].iov_len = uni_path_len;
2379 iov[1].iov_base = utf16_path;
2380 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2382 if (tcon->posix_extensions) {
2383 /* resource #3: posix buf */
2384 rc = add_posix_context(iov, &n_iov, mode);
2387 pc_buf = iov[n_iov-1].iov_base;
2391 memset(&rqst, 0, sizeof(struct smb_rqst));
2393 rqst.rq_nvec = n_iov;
2395 /* no need to inc num_remote_opens because we close it just below */
2396 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2397 FILE_WRITE_ATTRIBUTES);
2398 /* resource #4: response buffer */
2399 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2401 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2402 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2404 FILE_WRITE_ATTRIBUTES, rc);
2405 goto err_free_rsp_buf;
2408 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2409 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2410 ses->Suid, CREATE_NOT_FILE,
2411 FILE_WRITE_ATTRIBUTES);
2413 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2415 /* Eventually save off posix specific response info and timestaps */
2418 free_rsp_buf(resp_buftype, rsp);
2421 cifs_small_buf_release(req);
2428 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2429 struct cifs_open_parms *oparms, __le16 *path)
2431 struct TCP_Server_Info *server = tcon->ses->server;
2432 struct smb2_create_req *req;
2433 unsigned int n_iov = 2;
2434 __u32 file_attributes = 0;
2437 unsigned int total_len;
2438 struct kvec *iov = rqst->rq_iov;
2442 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2446 iov[0].iov_base = (char *)req;
2447 /* -1 since last byte is buf[0] which is sent below (path) */
2448 iov[0].iov_len = total_len - 1;
2450 if (oparms->create_options & CREATE_OPTION_READONLY)
2451 file_attributes |= ATTR_READONLY;
2452 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2453 file_attributes |= ATTR_SYSTEM;
2455 req->ImpersonationLevel = IL_IMPERSONATION;
2456 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2457 /* File attributes ignored on open (used in create though) */
2458 req->FileAttributes = cpu_to_le32(file_attributes);
2459 req->ShareAccess = FILE_SHARE_ALL_LE;
2461 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2462 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2463 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2465 /* [MS-SMB2] 2.2.13 NameOffset:
2466 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2467 * the SMB2 header, the file name includes a prefix that will
2468 * be processed during DFS name normalization as specified in
2469 * section 3.3.5.9. Otherwise, the file name is relative to
2470 * the share that is identified by the TreeId in the SMB2
2473 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2476 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2477 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2479 tcon->treeName, path);
2482 req->NameLength = cpu_to_le16(name_len * 2);
2483 uni_path_len = copy_size;
2486 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2487 /* MUST set path len (NameLength) to 0 opening root of share */
2488 req->NameLength = cpu_to_le16(uni_path_len - 2);
2489 copy_size = uni_path_len;
2490 if (copy_size % 8 != 0)
2491 copy_size = roundup(copy_size, 8);
2492 copy_path = kzalloc(copy_size, GFP_KERNEL);
2495 memcpy((char *)copy_path, (const char *)path,
2497 uni_path_len = copy_size;
2501 iov[1].iov_len = uni_path_len;
2502 iov[1].iov_base = path;
2504 if ((!server->oplocks) || (tcon->no_lease))
2505 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2507 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2508 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2509 req->RequestedOplockLevel = *oplock;
2510 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2511 (oparms->create_options & CREATE_NOT_FILE))
2512 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2514 rc = add_lease_context(server, iov, &n_iov,
2515 oparms->fid->lease_key, oplock);
2520 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2521 /* need to set Next field of lease context if we request it */
2522 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2523 struct create_context *ccontext =
2524 (struct create_context *)iov[n_iov-1].iov_base;
2526 cpu_to_le32(server->vals->create_lease_size);
2529 rc = add_durable_context(iov, &n_iov, oparms,
2530 tcon->use_persistent);
2535 if (tcon->posix_extensions) {
2537 struct create_context *ccontext =
2538 (struct create_context *)iov[n_iov-1].iov_base;
2540 cpu_to_le32(iov[n_iov-1].iov_len);
2543 rc = add_posix_context(iov, &n_iov, oparms->mode);
2548 if (tcon->snapshot_time) {
2549 cifs_dbg(FYI, "adding snapshot context\n");
2551 struct create_context *ccontext =
2552 (struct create_context *)iov[n_iov-1].iov_base;
2554 cpu_to_le32(iov[n_iov-1].iov_len);
2557 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2562 if ((oparms->disposition == FILE_CREATE) &&
2563 (oparms->mode != ACL_NO_MODE)) {
2565 struct create_context *ccontext =
2566 (struct create_context *)iov[n_iov-1].iov_base;
2568 cpu_to_le32(iov[n_iov-1].iov_len);
2571 /* rc = add_sd_context(iov, &n_iov, oparms->mode); */
2577 struct create_context *ccontext =
2578 (struct create_context *)iov[n_iov-1].iov_base;
2579 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2581 add_query_id_context(iov, &n_iov);
2583 rqst->rq_nvec = n_iov;
2587 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2588 * All other vectors are freed by kfree().
2591 SMB2_open_free(struct smb_rqst *rqst)
2595 if (rqst && rqst->rq_iov) {
2596 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2597 for (i = 1; i < rqst->rq_nvec; i++)
2598 if (rqst->rq_iov[i].iov_base != smb2_padding)
2599 kfree(rqst->rq_iov[i].iov_base);
2604 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2605 __u8 *oplock, struct smb2_file_all_info *buf,
2606 struct kvec *err_iov, int *buftype)
2608 struct smb_rqst rqst;
2609 struct smb2_create_rsp *rsp = NULL;
2610 struct TCP_Server_Info *server;
2611 struct cifs_tcon *tcon = oparms->tcon;
2612 struct cifs_ses *ses = tcon->ses;
2613 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2614 struct kvec rsp_iov = {NULL, 0};
2615 int resp_buftype = CIFS_NO_BUFFER;
2619 cifs_dbg(FYI, "create/open\n");
2620 if (ses && (ses->server))
2621 server = ses->server;
2625 if (smb3_encryption_required(tcon))
2626 flags |= CIFS_TRANSFORM_REQ;
2628 memset(&rqst, 0, sizeof(struct smb_rqst));
2629 memset(&iov, 0, sizeof(iov));
2631 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2633 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2637 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2638 oparms->create_options, oparms->desired_access);
2640 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2642 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2645 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2646 if (err_iov && rsp) {
2648 *buftype = resp_buftype;
2649 resp_buftype = CIFS_NO_BUFFER;
2652 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2653 oparms->create_options, oparms->desired_access, rc);
2654 if (rc == -EREMCHG) {
2655 printk_once(KERN_WARNING "server share %s deleted\n",
2657 tcon->need_reconnect = true;
2661 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2662 ses->Suid, oparms->create_options,
2663 oparms->desired_access);
2665 atomic_inc(&tcon->num_remote_opens);
2666 oparms->fid->persistent_fid = rsp->PersistentFileId;
2667 oparms->fid->volatile_fid = rsp->VolatileFileId;
2668 oparms->fid->access = oparms->desired_access;
2669 #ifdef CONFIG_CIFS_DEBUG2
2670 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2671 #endif /* CIFS_DEBUG2 */
2674 memcpy(buf, &rsp->CreationTime, 32);
2675 buf->AllocationSize = rsp->AllocationSize;
2676 buf->EndOfFile = rsp->EndofFile;
2677 buf->Attributes = rsp->FileAttributes;
2678 buf->NumberOfLinks = cpu_to_le32(1);
2679 buf->DeletePending = 0;
2683 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2684 oparms->fid->lease_key, oplock, buf);
2686 SMB2_open_free(&rqst);
2687 free_rsp_buf(resp_buftype, rsp);
2692 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2693 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2694 bool is_fsctl, char *in_data, u32 indatalen,
2695 __u32 max_response_size)
2697 struct smb2_ioctl_req *req;
2698 struct kvec *iov = rqst->rq_iov;
2699 unsigned int total_len;
2703 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2709 * indatalen is usually small at a couple of bytes max, so
2710 * just allocate through generic pool
2712 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2714 cifs_small_buf_release(req);
2719 req->CtlCode = cpu_to_le32(opcode);
2720 req->PersistentFileId = persistent_fid;
2721 req->VolatileFileId = volatile_fid;
2723 iov[0].iov_base = (char *)req;
2725 * If no input data, the size of ioctl struct in
2726 * protocol spec still includes a 1 byte data buffer,
2727 * but if input data passed to ioctl, we do not
2728 * want to double count this, so we do not send
2729 * the dummy one byte of data in iovec[0] if sending
2730 * input data (in iovec[1]).
2733 req->InputCount = cpu_to_le32(indatalen);
2734 /* do not set InputOffset if no input data */
2736 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2738 iov[0].iov_len = total_len - 1;
2739 iov[1].iov_base = in_data_buf;
2740 iov[1].iov_len = indatalen;
2743 iov[0].iov_len = total_len;
2746 req->OutputOffset = 0;
2747 req->OutputCount = 0; /* MBZ */
2750 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2751 * We Could increase default MaxOutputResponse, but that could require
2752 * more credits. Windows typically sets this smaller, but for some
2753 * ioctls it may be useful to allow server to send more. No point
2754 * limiting what the server can send as long as fits in one credit
2755 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2756 * to increase this limit up in the future.
2757 * Note that for snapshot queries that servers like Azure expect that
2758 * the first query be minimal size (and just used to get the number/size
2759 * of previous versions) so response size must be specified as EXACTLY
2760 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2761 * of eight bytes. Currently that is the only case where we set max
2762 * response size smaller.
2764 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2765 req->sync_hdr.CreditCharge =
2766 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2767 SMB2_MAX_BUFFER_SIZE));
2769 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2773 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2774 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2775 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2781 SMB2_ioctl_free(struct smb_rqst *rqst)
2784 if (rqst && rqst->rq_iov) {
2785 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2786 for (i = 1; i < rqst->rq_nvec; i++)
2787 if (rqst->rq_iov[i].iov_base != smb2_padding)
2788 kfree(rqst->rq_iov[i].iov_base);
2794 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2797 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2798 u64 volatile_fid, u32 opcode, bool is_fsctl,
2799 char *in_data, u32 indatalen, u32 max_out_data_len,
2800 char **out_data, u32 *plen /* returned data len */)
2802 struct smb_rqst rqst;
2803 struct smb2_ioctl_rsp *rsp = NULL;
2804 struct cifs_ses *ses;
2805 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2806 struct kvec rsp_iov = {NULL, 0};
2807 int resp_buftype = CIFS_NO_BUFFER;
2810 struct TCP_Server_Info *server;
2812 cifs_dbg(FYI, "SMB2 IOCTL\n");
2814 if (out_data != NULL)
2817 /* zero out returned data len, in case of error */
2828 server = ses->server;
2832 if (smb3_encryption_required(tcon))
2833 flags |= CIFS_TRANSFORM_REQ;
2835 memset(&rqst, 0, sizeof(struct smb_rqst));
2836 memset(&iov, 0, sizeof(iov));
2838 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2840 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2841 is_fsctl, in_data, indatalen, max_out_data_len);
2845 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2847 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2850 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2851 ses->Suid, 0, opcode, rc);
2853 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2854 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2856 } else if (rc == -EINVAL) {
2857 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2858 (opcode != FSCTL_SRV_COPYCHUNK)) {
2859 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2862 } else if (rc == -E2BIG) {
2863 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2864 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2869 /* check if caller wants to look at return data or just return rc */
2870 if ((plen == NULL) || (out_data == NULL))
2873 *plen = le32_to_cpu(rsp->OutputCount);
2875 /* We check for obvious errors in the output buffer length and offset */
2877 goto ioctl_exit; /* server returned no data */
2878 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2879 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2885 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2886 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2887 le32_to_cpu(rsp->OutputOffset));
2893 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2895 if (*out_data == NULL) {
2901 SMB2_ioctl_free(&rqst);
2902 free_rsp_buf(resp_buftype, rsp);
2907 * Individual callers to ioctl worker function follow
2911 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2912 u64 persistent_fid, u64 volatile_fid)
2915 struct compress_ioctl fsctl_input;
2916 char *ret_data = NULL;
2918 fsctl_input.CompressionState =
2919 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2921 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2922 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2923 (char *)&fsctl_input /* data input */,
2924 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2925 &ret_data /* out data */, NULL);
2927 cifs_dbg(FYI, "set compression rc %d\n", rc);
2933 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2934 u64 persistent_fid, u64 volatile_fid)
2936 struct smb2_close_req *req;
2937 struct kvec *iov = rqst->rq_iov;
2938 unsigned int total_len;
2941 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2945 req->PersistentFileId = persistent_fid;
2946 req->VolatileFileId = volatile_fid;
2947 iov[0].iov_base = (char *)req;
2948 iov[0].iov_len = total_len;
2954 SMB2_close_free(struct smb_rqst *rqst)
2956 if (rqst && rqst->rq_iov)
2957 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2961 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2962 u64 persistent_fid, u64 volatile_fid)
2964 struct smb_rqst rqst;
2965 struct smb2_close_rsp *rsp = NULL;
2966 struct cifs_ses *ses = tcon->ses;
2968 struct kvec rsp_iov;
2969 int resp_buftype = CIFS_NO_BUFFER;
2973 cifs_dbg(FYI, "Close\n");
2975 if (!ses || !(ses->server))
2978 if (smb3_encryption_required(tcon))
2979 flags |= CIFS_TRANSFORM_REQ;
2981 memset(&rqst, 0, sizeof(struct smb_rqst));
2982 memset(&iov, 0, sizeof(iov));
2986 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
2987 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2991 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2992 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2995 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2996 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3000 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3003 atomic_dec(&tcon->num_remote_opens);
3005 /* BB FIXME - decode close response, update inode for caching */
3008 SMB2_close_free(&rqst);
3009 free_rsp_buf(resp_buftype, rsp);
3011 /* retry close in a worker thread if this one is interrupted */
3012 if (is_interrupt_error(rc)) {
3015 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3018 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3019 persistent_fid, tmp_rc);
3025 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3026 struct kvec *iov, unsigned int min_buf_size)
3028 unsigned int smb_len = iov->iov_len;
3029 char *end_of_smb = smb_len + (char *)iov->iov_base;
3030 char *begin_of_buf = offset + (char *)iov->iov_base;
3031 char *end_of_buf = begin_of_buf + buffer_length;
3034 if (buffer_length < min_buf_size) {
3035 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3036 buffer_length, min_buf_size);
3040 /* check if beyond RFC1001 maximum length */
3041 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3042 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3043 buffer_length, smb_len);
3047 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3048 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3056 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3057 * Caller must free buffer.
3060 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3061 struct kvec *iov, unsigned int minbufsize,
3064 char *begin_of_buf = offset + (char *)iov->iov_base;
3070 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3074 memcpy(data, begin_of_buf, buffer_length);
3080 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3081 u64 persistent_fid, u64 volatile_fid,
3082 u8 info_class, u8 info_type, u32 additional_info,
3083 size_t output_len, size_t input_len, void *input)
3085 struct smb2_query_info_req *req;
3086 struct kvec *iov = rqst->rq_iov;
3087 unsigned int total_len;
3090 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3095 req->InfoType = info_type;
3096 req->FileInfoClass = info_class;
3097 req->PersistentFileId = persistent_fid;
3098 req->VolatileFileId = volatile_fid;
3099 req->AdditionalInformation = cpu_to_le32(additional_info);
3101 req->OutputBufferLength = cpu_to_le32(output_len);
3103 req->InputBufferLength = cpu_to_le32(input_len);
3104 /* total_len for smb query request never close to le16 max */
3105 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3106 memcpy(req->Buffer, input, input_len);
3109 iov[0].iov_base = (char *)req;
3111 iov[0].iov_len = total_len - 1 + input_len;
3116 SMB2_query_info_free(struct smb_rqst *rqst)
3118 if (rqst && rqst->rq_iov)
3119 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3123 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3124 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3125 u32 additional_info, size_t output_len, size_t min_len, void **data,
3128 struct smb_rqst rqst;
3129 struct smb2_query_info_rsp *rsp = NULL;
3131 struct kvec rsp_iov;
3133 int resp_buftype = CIFS_NO_BUFFER;
3134 struct cifs_ses *ses = tcon->ses;
3135 struct TCP_Server_Info *server;
3137 bool allocated = false;
3139 cifs_dbg(FYI, "Query Info\n");
3143 server = ses->server;
3147 if (smb3_encryption_required(tcon))
3148 flags |= CIFS_TRANSFORM_REQ;
3150 memset(&rqst, 0, sizeof(struct smb_rqst));
3151 memset(&iov, 0, sizeof(iov));
3155 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3156 info_class, info_type, additional_info,
3157 output_len, 0, NULL);
3161 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3162 ses->Suid, info_class, (__u32)info_type);
3164 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3165 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3168 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3169 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3170 ses->Suid, info_class, (__u32)info_type, rc);
3174 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3175 ses->Suid, info_class, (__u32)info_type);
3178 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3180 *data = kmalloc(*dlen, GFP_KERNEL);
3183 "Error %d allocating memory for acl\n",
3193 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3194 le32_to_cpu(rsp->OutputBufferLength),
3195 &rsp_iov, min_len, *data);
3196 if (rc && allocated) {
3203 SMB2_query_info_free(&rqst);
3204 free_rsp_buf(resp_buftype, rsp);
3208 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3209 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3211 return query_info(xid, tcon, persistent_fid, volatile_fid,
3212 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3213 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3214 sizeof(struct smb2_file_all_info), (void **)&data,
3219 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3220 u64 persistent_fid, u64 volatile_fid,
3221 void **data, u32 *plen)
3223 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3226 return query_info(xid, tcon, persistent_fid, volatile_fid,
3227 0, SMB2_O_INFO_SECURITY, additional_info,
3228 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3232 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3233 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3235 return query_info(xid, tcon, persistent_fid, volatile_fid,
3236 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3237 sizeof(struct smb2_file_internal_info),
3238 sizeof(struct smb2_file_internal_info),
3239 (void **)&uniqueid, NULL);
3243 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3244 * See MS-SMB2 2.2.35 and 2.2.36
3248 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3249 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3250 u32 completion_filter, bool watch_tree)
3252 struct smb2_change_notify_req *req;
3253 struct kvec *iov = rqst->rq_iov;
3254 unsigned int total_len;
3257 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3261 req->PersistentFileId = persistent_fid;
3262 req->VolatileFileId = volatile_fid;
3263 req->OutputBufferLength =
3264 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3265 req->CompletionFilter = cpu_to_le32(completion_filter);
3267 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3271 iov[0].iov_base = (char *)req;
3272 iov[0].iov_len = total_len;
3278 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3279 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3280 u32 completion_filter)
3282 struct cifs_ses *ses = tcon->ses;
3283 struct smb_rqst rqst;
3285 struct kvec rsp_iov = {NULL, 0};
3286 int resp_buftype = CIFS_NO_BUFFER;
3290 cifs_dbg(FYI, "change notify\n");
3291 if (!ses || !(ses->server))
3294 if (smb3_encryption_required(tcon))
3295 flags |= CIFS_TRANSFORM_REQ;
3297 memset(&rqst, 0, sizeof(struct smb_rqst));
3298 memset(&iov, 0, sizeof(iov));
3302 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3303 completion_filter, watch_tree);
3307 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3308 (u8)watch_tree, completion_filter);
3309 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3312 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3313 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3314 (u8)watch_tree, completion_filter, rc);
3316 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3317 ses->Suid, (u8)watch_tree, completion_filter);
3321 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3322 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3329 * This is a no-op for now. We're not really interested in the reply, but
3330 * rather in the fact that the server sent one and that server->lstrp
3333 * FIXME: maybe we should consider checking that the reply matches request?
3336 smb2_echo_callback(struct mid_q_entry *mid)
3338 struct TCP_Server_Info *server = mid->callback_data;
3339 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3340 struct cifs_credits credits = { .value = 0, .instance = 0 };
3342 if (mid->mid_state == MID_RESPONSE_RECEIVED
3343 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3344 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3345 credits.instance = server->reconnect_instance;
3348 DeleteMidQEntry(mid);
3349 add_credits(server, &credits, CIFS_ECHO_OP);
3352 void smb2_reconnect_server(struct work_struct *work)
3354 struct TCP_Server_Info *server = container_of(work,
3355 struct TCP_Server_Info, reconnect.work);
3356 struct cifs_ses *ses;
3357 struct cifs_tcon *tcon, *tcon2;
3358 struct list_head tmp_list;
3359 int tcon_exist = false;
3361 int resched = false;
3364 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3365 mutex_lock(&server->reconnect_mutex);
3367 INIT_LIST_HEAD(&tmp_list);
3368 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3370 spin_lock(&cifs_tcp_ses_lock);
3371 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3372 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3373 if (tcon->need_reconnect || tcon->need_reopen_files) {
3375 list_add_tail(&tcon->rlist, &tmp_list);
3380 * IPC has the same lifetime as its session and uses its
3383 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3384 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3390 * Get the reference to server struct to be sure that the last call of
3391 * cifs_put_tcon() in the loop below won't release the server pointer.
3394 server->srv_count++;
3396 spin_unlock(&cifs_tcp_ses_lock);
3398 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3399 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3401 cifs_reopen_persistent_handles(tcon);
3404 list_del_init(&tcon->rlist);
3406 cifs_put_smb_ses(tcon->ses);
3408 cifs_put_tcon(tcon);
3411 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3413 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3414 mutex_unlock(&server->reconnect_mutex);
3416 /* now we can safely release srv struct */
3418 cifs_put_tcp_session(server, 1);
3422 SMB2_echo(struct TCP_Server_Info *server)
3424 struct smb2_echo_req *req;
3427 struct smb_rqst rqst = { .rq_iov = iov,
3429 unsigned int total_len;
3431 cifs_dbg(FYI, "In echo request\n");
3433 if (server->tcpStatus == CifsNeedNegotiate) {
3434 /* No need to send echo on newly established connections */
3435 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3439 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3443 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3445 iov[0].iov_len = total_len;
3446 iov[0].iov_base = (char *)req;
3448 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3449 server, CIFS_ECHO_OP, NULL);
3451 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3453 cifs_small_buf_release(req);
3458 SMB2_flush_free(struct smb_rqst *rqst)
3460 if (rqst && rqst->rq_iov)
3461 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3465 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3466 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3468 struct smb2_flush_req *req;
3469 struct kvec *iov = rqst->rq_iov;
3470 unsigned int total_len;
3473 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3477 req->PersistentFileId = persistent_fid;
3478 req->VolatileFileId = volatile_fid;
3480 iov[0].iov_base = (char *)req;
3481 iov[0].iov_len = total_len;
3487 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3490 struct cifs_ses *ses = tcon->ses;
3491 struct smb_rqst rqst;
3493 struct kvec rsp_iov = {NULL, 0};
3494 int resp_buftype = CIFS_NO_BUFFER;
3498 cifs_dbg(FYI, "flush\n");
3499 if (!ses || !(ses->server))
3502 if (smb3_encryption_required(tcon))
3503 flags |= CIFS_TRANSFORM_REQ;
3505 memset(&rqst, 0, sizeof(struct smb_rqst));
3506 memset(&iov, 0, sizeof(iov));
3510 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3514 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3515 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3518 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3519 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3522 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3526 SMB2_flush_free(&rqst);
3527 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3532 * To form a chain of read requests, any read requests after the first should
3533 * have the end_of_chain boolean set to true.
3536 smb2_new_read_req(void **buf, unsigned int *total_len,
3537 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3538 unsigned int remaining_bytes, int request_type)
3541 struct smb2_read_plain_req *req = NULL;
3542 struct smb2_sync_hdr *shdr;
3543 struct TCP_Server_Info *server;
3545 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3550 server = io_parms->tcon->ses->server;
3552 return -ECONNABORTED;
3554 shdr = &req->sync_hdr;
3555 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3557 req->PersistentFileId = io_parms->persistent_fid;
3558 req->VolatileFileId = io_parms->volatile_fid;
3559 req->ReadChannelInfoOffset = 0; /* reserved */
3560 req->ReadChannelInfoLength = 0; /* reserved */
3561 req->Channel = 0; /* reserved */
3562 req->MinimumCount = 0;
3563 req->Length = cpu_to_le32(io_parms->length);
3564 req->Offset = cpu_to_le64(io_parms->offset);
3566 trace_smb3_read_enter(0 /* xid */,
3567 io_parms->persistent_fid,
3568 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3569 io_parms->offset, io_parms->length);
3570 #ifdef CONFIG_CIFS_SMB_DIRECT
3572 * If we want to do a RDMA write, fill in and append
3573 * smbd_buffer_descriptor_v1 to the end of read request
3575 if (server->rdma && rdata && !server->sign &&
3576 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3578 struct smbd_buffer_descriptor_v1 *v1;
3579 bool need_invalidate =
3580 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3582 rdata->mr = smbd_register_mr(
3583 server->smbd_conn, rdata->pages,
3584 rdata->nr_pages, rdata->page_offset,
3585 rdata->tailsz, true, need_invalidate);
3589 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3590 if (need_invalidate)
3591 req->Channel = SMB2_CHANNEL_RDMA_V1;
3592 req->ReadChannelInfoOffset =
3593 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3594 req->ReadChannelInfoLength =
3595 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3596 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3597 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3598 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3599 v1->length = cpu_to_le32(rdata->mr->mr->length);
3601 *total_len += sizeof(*v1) - 1;
3604 if (request_type & CHAINED_REQUEST) {
3605 if (!(request_type & END_OF_CHAIN)) {
3606 /* next 8-byte aligned request */
3607 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3608 shdr->NextCommand = cpu_to_le32(*total_len);
3609 } else /* END_OF_CHAIN */
3610 shdr->NextCommand = 0;
3611 if (request_type & RELATED_REQUEST) {
3612 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3614 * Related requests use info from previous read request
3617 shdr->SessionId = 0xFFFFFFFFFFFFFFFF;
3618 shdr->TreeId = 0xFFFFFFFF;
3619 req->PersistentFileId = 0xFFFFFFFFFFFFFFFF;
3620 req->VolatileFileId = 0xFFFFFFFFFFFFFFFF;
3623 if (remaining_bytes > io_parms->length)
3624 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3626 req->RemainingBytes = 0;
3633 smb2_readv_callback(struct mid_q_entry *mid)
3635 struct cifs_readdata *rdata = mid->callback_data;
3636 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3637 struct TCP_Server_Info *server = tcon->ses->server;
3638 struct smb2_sync_hdr *shdr =
3639 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3640 struct cifs_credits credits = { .value = 0, .instance = 0 };
3641 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3644 if (rdata->got_bytes) {
3645 rqst.rq_pages = rdata->pages;
3646 rqst.rq_offset = rdata->page_offset;
3647 rqst.rq_npages = rdata->nr_pages;
3648 rqst.rq_pagesz = rdata->pagesz;
3649 rqst.rq_tailsz = rdata->tailsz;
3652 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3653 __func__, mid->mid, mid->mid_state, rdata->result,
3656 switch (mid->mid_state) {
3657 case MID_RESPONSE_RECEIVED:
3658 credits.value = le16_to_cpu(shdr->CreditRequest);
3659 credits.instance = server->reconnect_instance;
3660 /* result already set, check signature */
3661 if (server->sign && !mid->decrypted) {
3664 rc = smb2_verify_signature(&rqst, server);
3666 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3669 /* FIXME: should this be counted toward the initiating task? */
3670 task_io_account_read(rdata->got_bytes);
3671 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3673 case MID_REQUEST_SUBMITTED:
3674 case MID_RETRY_NEEDED:
3675 rdata->result = -EAGAIN;
3676 if (server->sign && rdata->got_bytes)
3677 /* reset bytes number since we can not check a sign */
3678 rdata->got_bytes = 0;
3679 /* FIXME: should this be counted toward the initiating task? */
3680 task_io_account_read(rdata->got_bytes);
3681 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3683 case MID_RESPONSE_MALFORMED:
3684 credits.value = le16_to_cpu(shdr->CreditRequest);
3685 credits.instance = server->reconnect_instance;
3688 rdata->result = -EIO;
3690 #ifdef CONFIG_CIFS_SMB_DIRECT
3692 * If this rdata has a memmory registered, the MR can be freed
3693 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3694 * because they have limited number and are used for future I/Os
3697 smbd_deregister_mr(rdata->mr);
3701 if (rdata->result && rdata->result != -ENODATA) {
3702 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3703 trace_smb3_read_err(0 /* xid */,
3704 rdata->cfile->fid.persistent_fid,
3705 tcon->tid, tcon->ses->Suid, rdata->offset,
3706 rdata->bytes, rdata->result);
3708 trace_smb3_read_done(0 /* xid */,
3709 rdata->cfile->fid.persistent_fid,
3710 tcon->tid, tcon->ses->Suid,
3711 rdata->offset, rdata->got_bytes);
3713 queue_work(cifsiod_wq, &rdata->work);
3714 DeleteMidQEntry(mid);
3715 add_credits(server, &credits, 0);
3718 /* smb2_async_readv - send an async read, and set up mid to handle result */
3720 smb2_async_readv(struct cifs_readdata *rdata)
3724 struct smb2_sync_hdr *shdr;
3725 struct cifs_io_parms io_parms;
3726 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3728 struct TCP_Server_Info *server;
3729 unsigned int total_len;
3731 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3732 __func__, rdata->offset, rdata->bytes);
3734 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3735 io_parms.offset = rdata->offset;
3736 io_parms.length = rdata->bytes;
3737 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3738 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3739 io_parms.pid = rdata->pid;
3741 server = io_parms.tcon->ses->server;
3743 rc = smb2_new_read_req(
3744 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3748 if (smb3_encryption_required(io_parms.tcon))
3749 flags |= CIFS_TRANSFORM_REQ;
3751 rdata->iov[0].iov_base = buf;
3752 rdata->iov[0].iov_len = total_len;
3754 shdr = (struct smb2_sync_hdr *)buf;
3756 if (rdata->credits.value > 0) {
3757 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3758 SMB2_MAX_BUFFER_SIZE));
3759 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
3761 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3763 goto async_readv_out;
3765 flags |= CIFS_HAS_CREDITS;
3768 kref_get(&rdata->refcount);
3769 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3770 cifs_readv_receive, smb2_readv_callback,
3771 smb3_handle_read_data, rdata, flags,
3774 kref_put(&rdata->refcount, cifs_readdata_release);
3775 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3776 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3778 io_parms.tcon->ses->Suid,
3779 io_parms.offset, io_parms.length, rc);
3783 cifs_small_buf_release(buf);
3788 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3789 unsigned int *nbytes, char **buf, int *buf_type)
3791 struct smb_rqst rqst;
3792 int resp_buftype, rc;
3793 struct smb2_read_plain_req *req = NULL;
3794 struct smb2_read_rsp *rsp = NULL;
3796 struct kvec rsp_iov;
3797 unsigned int total_len;
3798 int flags = CIFS_LOG_ERROR;
3799 struct cifs_ses *ses = io_parms->tcon->ses;
3802 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3806 if (smb3_encryption_required(io_parms->tcon))
3807 flags |= CIFS_TRANSFORM_REQ;
3809 iov[0].iov_base = (char *)req;
3810 iov[0].iov_len = total_len;
3812 memset(&rqst, 0, sizeof(struct smb_rqst));
3816 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3817 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3820 if (rc != -ENODATA) {
3821 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3822 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3823 trace_smb3_read_err(xid, req->PersistentFileId,
3824 io_parms->tcon->tid, ses->Suid,
3825 io_parms->offset, io_parms->length,
3828 trace_smb3_read_done(xid, req->PersistentFileId,
3829 io_parms->tcon->tid, ses->Suid,
3830 io_parms->offset, 0);
3831 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3832 cifs_small_buf_release(req);
3833 return rc == -ENODATA ? 0 : rc;
3835 trace_smb3_read_done(xid, req->PersistentFileId,
3836 io_parms->tcon->tid, ses->Suid,
3837 io_parms->offset, io_parms->length);
3839 cifs_small_buf_release(req);
3841 *nbytes = le32_to_cpu(rsp->DataLength);
3842 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3843 (*nbytes > io_parms->length)) {
3844 cifs_dbg(FYI, "bad length %d for count %d\n",
3845 *nbytes, io_parms->length);
3851 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3852 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3853 } else if (resp_buftype != CIFS_NO_BUFFER) {
3854 *buf = rsp_iov.iov_base;
3855 if (resp_buftype == CIFS_SMALL_BUFFER)
3856 *buf_type = CIFS_SMALL_BUFFER;
3857 else if (resp_buftype == CIFS_LARGE_BUFFER)
3858 *buf_type = CIFS_LARGE_BUFFER;
3864 * Check the mid_state and signature on received buffer (if any), and queue the
3865 * workqueue completion task.
3868 smb2_writev_callback(struct mid_q_entry *mid)
3870 struct cifs_writedata *wdata = mid->callback_data;
3871 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3872 struct TCP_Server_Info *server = tcon->ses->server;
3873 unsigned int written;
3874 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3875 struct cifs_credits credits = { .value = 0, .instance = 0 };
3877 switch (mid->mid_state) {
3878 case MID_RESPONSE_RECEIVED:
3879 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3880 credits.instance = server->reconnect_instance;
3881 wdata->result = smb2_check_receive(mid, server, 0);
3882 if (wdata->result != 0)
3885 written = le32_to_cpu(rsp->DataLength);
3887 * Mask off high 16 bits when bytes written as returned
3888 * by the server is greater than bytes requested by the
3889 * client. OS/2 servers are known to set incorrect
3892 if (written > wdata->bytes)
3895 if (written < wdata->bytes)
3896 wdata->result = -ENOSPC;
3898 wdata->bytes = written;
3900 case MID_REQUEST_SUBMITTED:
3901 case MID_RETRY_NEEDED:
3902 wdata->result = -EAGAIN;
3904 case MID_RESPONSE_MALFORMED:
3905 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3906 credits.instance = server->reconnect_instance;
3909 wdata->result = -EIO;
3912 #ifdef CONFIG_CIFS_SMB_DIRECT
3914 * If this wdata has a memory registered, the MR can be freed
3915 * The number of MRs available is limited, it's important to recover
3916 * used MR as soon as I/O is finished. Hold MR longer in the later
3917 * I/O process can possibly result in I/O deadlock due to lack of MR
3918 * to send request on I/O retry
3921 smbd_deregister_mr(wdata->mr);
3925 if (wdata->result) {
3926 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3927 trace_smb3_write_err(0 /* no xid */,
3928 wdata->cfile->fid.persistent_fid,
3929 tcon->tid, tcon->ses->Suid, wdata->offset,
3930 wdata->bytes, wdata->result);
3931 if (wdata->result == -ENOSPC)
3932 printk_once(KERN_WARNING "Out of space writing to %s\n",
3935 trace_smb3_write_done(0 /* no xid */,
3936 wdata->cfile->fid.persistent_fid,
3937 tcon->tid, tcon->ses->Suid,
3938 wdata->offset, wdata->bytes);
3940 queue_work(cifsiod_wq, &wdata->work);
3941 DeleteMidQEntry(mid);
3942 add_credits(server, &credits, 0);
3945 /* smb2_async_writev - send an async write, and set up mid to handle result */
3947 smb2_async_writev(struct cifs_writedata *wdata,
3948 void (*release)(struct kref *kref))
3950 int rc = -EACCES, flags = 0;
3951 struct smb2_write_req *req = NULL;
3952 struct smb2_sync_hdr *shdr;
3953 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3954 struct TCP_Server_Info *server = tcon->ses->server;
3956 struct smb_rqst rqst = { };
3957 unsigned int total_len;
3959 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3963 if (smb3_encryption_required(tcon))
3964 flags |= CIFS_TRANSFORM_REQ;
3966 shdr = (struct smb2_sync_hdr *)req;
3967 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3969 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3970 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3971 req->WriteChannelInfoOffset = 0;
3972 req->WriteChannelInfoLength = 0;
3974 req->Offset = cpu_to_le64(wdata->offset);
3975 req->DataOffset = cpu_to_le16(
3976 offsetof(struct smb2_write_req, Buffer));
3977 req->RemainingBytes = 0;
3979 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3980 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3981 #ifdef CONFIG_CIFS_SMB_DIRECT
3983 * If we want to do a server RDMA read, fill in and append
3984 * smbd_buffer_descriptor_v1 to the end of write request
3986 if (server->rdma && !server->sign && wdata->bytes >=
3987 server->smbd_conn->rdma_readwrite_threshold) {
3989 struct smbd_buffer_descriptor_v1 *v1;
3990 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3992 wdata->mr = smbd_register_mr(
3993 server->smbd_conn, wdata->pages,
3994 wdata->nr_pages, wdata->page_offset,
3995 wdata->tailsz, false, need_invalidate);
3998 goto async_writev_out;
4001 req->DataOffset = 0;
4002 if (wdata->nr_pages > 1)
4003 req->RemainingBytes =
4005 (wdata->nr_pages - 1) * wdata->pagesz -
4006 wdata->page_offset + wdata->tailsz
4009 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4010 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4011 if (need_invalidate)
4012 req->Channel = SMB2_CHANNEL_RDMA_V1;
4013 req->WriteChannelInfoOffset =
4014 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4015 req->WriteChannelInfoLength =
4016 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4017 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4018 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4019 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4020 v1->length = cpu_to_le32(wdata->mr->mr->length);
4023 iov[0].iov_len = total_len - 1;
4024 iov[0].iov_base = (char *)req;
4028 rqst.rq_pages = wdata->pages;
4029 rqst.rq_offset = wdata->page_offset;
4030 rqst.rq_npages = wdata->nr_pages;
4031 rqst.rq_pagesz = wdata->pagesz;
4032 rqst.rq_tailsz = wdata->tailsz;
4033 #ifdef CONFIG_CIFS_SMB_DIRECT
4035 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4039 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4040 wdata->offset, wdata->bytes);
4042 #ifdef CONFIG_CIFS_SMB_DIRECT
4043 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4045 req->Length = cpu_to_le32(wdata->bytes);
4047 req->Length = cpu_to_le32(wdata->bytes);
4050 if (wdata->credits.value > 0) {
4051 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4052 SMB2_MAX_BUFFER_SIZE));
4053 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4055 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4057 goto async_writev_out;
4059 flags |= CIFS_HAS_CREDITS;
4062 kref_get(&wdata->refcount);
4063 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4064 wdata, flags, &wdata->credits);
4067 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4068 tcon->tid, tcon->ses->Suid, wdata->offset,
4070 kref_put(&wdata->refcount, release);
4071 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4075 cifs_small_buf_release(req);
4080 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4081 * The length field from io_parms must be at least 1 and indicates a number of
4082 * elements with data to write that begins with position 1 in iov array. All
4083 * data length is specified by count.
4086 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4087 unsigned int *nbytes, struct kvec *iov, int n_vec)
4089 struct smb_rqst rqst;
4091 struct smb2_write_req *req = NULL;
4092 struct smb2_write_rsp *rsp = NULL;
4094 struct kvec rsp_iov;
4096 unsigned int total_len;
4103 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4108 if (io_parms->tcon->ses->server == NULL)
4109 return -ECONNABORTED;
4111 if (smb3_encryption_required(io_parms->tcon))
4112 flags |= CIFS_TRANSFORM_REQ;
4114 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4116 req->PersistentFileId = io_parms->persistent_fid;
4117 req->VolatileFileId = io_parms->volatile_fid;
4118 req->WriteChannelInfoOffset = 0;
4119 req->WriteChannelInfoLength = 0;
4121 req->Length = cpu_to_le32(io_parms->length);
4122 req->Offset = cpu_to_le64(io_parms->offset);
4123 req->DataOffset = cpu_to_le16(
4124 offsetof(struct smb2_write_req, Buffer));
4125 req->RemainingBytes = 0;
4127 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4128 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4129 io_parms->offset, io_parms->length);
4131 iov[0].iov_base = (char *)req;
4133 iov[0].iov_len = total_len - 1;
4135 memset(&rqst, 0, sizeof(struct smb_rqst));
4137 rqst.rq_nvec = n_vec + 1;
4139 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4140 &resp_buftype, flags, &rsp_iov);
4141 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4144 trace_smb3_write_err(xid, req->PersistentFileId,
4145 io_parms->tcon->tid,
4146 io_parms->tcon->ses->Suid,
4147 io_parms->offset, io_parms->length, rc);
4148 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4149 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4151 *nbytes = le32_to_cpu(rsp->DataLength);
4152 trace_smb3_write_done(xid, req->PersistentFileId,
4153 io_parms->tcon->tid,
4154 io_parms->tcon->ses->Suid,
4155 io_parms->offset, *nbytes);
4158 cifs_small_buf_release(req);
4159 free_rsp_buf(resp_buftype, rsp);
4164 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4167 unsigned int entrycount = 0;
4168 unsigned int next_offset = 0;
4170 FILE_DIRECTORY_INFO *dir_info;
4172 if (bufstart == NULL)
4175 entryptr = bufstart;
4178 if (entryptr + next_offset < entryptr ||
4179 entryptr + next_offset > end_of_buf ||
4180 entryptr + next_offset + size > end_of_buf) {
4181 cifs_dbg(VFS, "malformed search entry would overflow\n");
4185 entryptr = entryptr + next_offset;
4186 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4188 len = le32_to_cpu(dir_info->FileNameLength);
4189 if (entryptr + len < entryptr ||
4190 entryptr + len > end_of_buf ||
4191 entryptr + len + size > end_of_buf) {
4192 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4197 *lastentry = entryptr;
4200 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4212 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4213 u64 persistent_fid, u64 volatile_fid, int index,
4214 struct cifs_search_info *srch_inf)
4216 struct smb_rqst rqst;
4217 struct smb2_query_directory_req *req;
4218 struct smb2_query_directory_rsp *rsp = NULL;
4220 struct kvec rsp_iov;
4223 int resp_buftype = CIFS_NO_BUFFER;
4224 unsigned char *bufptr;
4225 struct TCP_Server_Info *server;
4226 struct cifs_ses *ses = tcon->ses;
4227 __le16 asteriks = cpu_to_le16('*');
4229 unsigned int output_size = CIFSMaxBufSize;
4230 size_t info_buf_size;
4232 unsigned int total_len;
4234 if (ses && (ses->server))
4235 server = ses->server;
4239 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4244 if (smb3_encryption_required(tcon))
4245 flags |= CIFS_TRANSFORM_REQ;
4247 switch (srch_inf->info_level) {
4248 case SMB_FIND_FILE_DIRECTORY_INFO:
4249 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4250 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4252 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4253 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4254 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4257 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4258 srch_inf->info_level);
4263 req->FileIndex = cpu_to_le32(index);
4264 req->PersistentFileId = persistent_fid;
4265 req->VolatileFileId = volatile_fid;
4268 bufptr = req->Buffer;
4269 memcpy(bufptr, &asteriks, len);
4271 req->FileNameOffset =
4272 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4273 req->FileNameLength = cpu_to_le16(len);
4275 * BB could be 30 bytes or so longer if we used SMB2 specific
4276 * buffer lengths, but this is safe and close enough.
4278 output_size = min_t(unsigned int, output_size, server->maxBuf);
4279 output_size = min_t(unsigned int, output_size, 2 << 15);
4280 req->OutputBufferLength = cpu_to_le32(output_size);
4282 iov[0].iov_base = (char *)req;
4284 iov[0].iov_len = total_len - 1;
4286 iov[1].iov_base = (char *)(req->Buffer);
4287 iov[1].iov_len = len;
4289 memset(&rqst, 0, sizeof(struct smb_rqst));
4293 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4294 tcon->ses->Suid, index, output_size);
4296 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4297 cifs_small_buf_release(req);
4298 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4301 if (rc == -ENODATA &&
4302 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4303 trace_smb3_query_dir_done(xid, persistent_fid,
4304 tcon->tid, tcon->ses->Suid, index, 0);
4305 srch_inf->endOfSearch = true;
4308 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4309 tcon->ses->Suid, index, 0, rc);
4310 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4315 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4316 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4319 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4320 tcon->ses->Suid, index, 0, rc);
4324 srch_inf->unicode = true;
4326 if (srch_inf->ntwrk_buf_start) {
4327 if (srch_inf->smallBuf)
4328 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4330 cifs_buf_release(srch_inf->ntwrk_buf_start);
4332 srch_inf->ntwrk_buf_start = (char *)rsp;
4333 srch_inf->srch_entries_start = srch_inf->last_entry =
4334 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4335 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4336 srch_inf->entries_in_buffer =
4337 num_entries(srch_inf->srch_entries_start, end_of_smb,
4338 &srch_inf->last_entry, info_buf_size);
4339 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4340 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4341 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4342 srch_inf->srch_entries_start, srch_inf->last_entry);
4343 if (resp_buftype == CIFS_LARGE_BUFFER)
4344 srch_inf->smallBuf = false;
4345 else if (resp_buftype == CIFS_SMALL_BUFFER)
4346 srch_inf->smallBuf = true;
4348 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4350 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4351 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4355 free_rsp_buf(resp_buftype, rsp);
4360 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4361 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4362 u8 info_type, u32 additional_info,
4363 void **data, unsigned int *size)
4365 struct smb2_set_info_req *req;
4366 struct kvec *iov = rqst->rq_iov;
4367 unsigned int i, total_len;
4370 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4374 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4375 req->InfoType = info_type;
4376 req->FileInfoClass = info_class;
4377 req->PersistentFileId = persistent_fid;
4378 req->VolatileFileId = volatile_fid;
4379 req->AdditionalInformation = cpu_to_le32(additional_info);
4382 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4383 req->BufferLength = cpu_to_le32(*size);
4385 memcpy(req->Buffer, *data, *size);
4388 iov[0].iov_base = (char *)req;
4390 iov[0].iov_len = total_len - 1;
4392 for (i = 1; i < rqst->rq_nvec; i++) {
4393 le32_add_cpu(&req->BufferLength, size[i]);
4394 iov[i].iov_base = (char *)data[i];
4395 iov[i].iov_len = size[i];
4402 SMB2_set_info_free(struct smb_rqst *rqst)
4404 if (rqst && rqst->rq_iov)
4405 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4409 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4410 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4411 u8 info_type, u32 additional_info, unsigned int num,
4412 void **data, unsigned int *size)
4414 struct smb_rqst rqst;
4415 struct smb2_set_info_rsp *rsp = NULL;
4417 struct kvec rsp_iov;
4420 struct cifs_ses *ses = tcon->ses;
4423 if (!ses || !(ses->server))
4429 if (smb3_encryption_required(tcon))
4430 flags |= CIFS_TRANSFORM_REQ;
4432 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4436 memset(&rqst, 0, sizeof(struct smb_rqst));
4440 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4441 info_class, info_type, additional_info,
4449 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4451 SMB2_set_info_free(&rqst);
4452 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4455 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4456 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4457 ses->Suid, info_class, (__u32)info_type, rc);
4460 free_rsp_buf(resp_buftype, rsp);
4466 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4467 u64 volatile_fid, u32 pid, __le64 *eof)
4469 struct smb2_file_eof_info info;
4473 info.EndOfFile = *eof;
4476 size = sizeof(struct smb2_file_eof_info);
4478 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4479 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4480 0, 1, &data, &size);
4484 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4485 u64 persistent_fid, u64 volatile_fid,
4486 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4488 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4489 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4490 1, (void **)&pnntsd, &pacllen);
4494 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4495 u64 persistent_fid, u64 volatile_fid,
4496 struct smb2_file_full_ea_info *buf, int len)
4498 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4499 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4500 0, 1, (void **)&buf, &len);
4504 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4505 const u64 persistent_fid, const u64 volatile_fid,
4508 struct smb_rqst rqst;
4510 struct smb2_oplock_break *req = NULL;
4511 struct cifs_ses *ses = tcon->ses;
4512 int flags = CIFS_OBREAK_OP;
4513 unsigned int total_len;
4515 struct kvec rsp_iov;
4518 cifs_dbg(FYI, "SMB2_oplock_break\n");
4519 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4524 if (smb3_encryption_required(tcon))
4525 flags |= CIFS_TRANSFORM_REQ;
4527 req->VolatileFid = volatile_fid;
4528 req->PersistentFid = persistent_fid;
4529 req->OplockLevel = oplock_level;
4530 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4532 flags |= CIFS_NO_RSP_BUF;
4534 iov[0].iov_base = (char *)req;
4535 iov[0].iov_len = total_len;
4537 memset(&rqst, 0, sizeof(struct smb_rqst));
4541 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4542 cifs_small_buf_release(req);
4545 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4546 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4553 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4554 struct kstatfs *kst)
4556 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4557 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4558 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4559 kst->f_bfree = kst->f_bavail =
4560 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4565 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4566 struct kstatfs *kst)
4568 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4569 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4570 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4571 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4572 kst->f_bavail = kst->f_bfree;
4574 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4575 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4576 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4577 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4578 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4584 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4585 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4588 struct smb2_query_info_req *req;
4589 unsigned int total_len;
4591 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4593 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4596 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4601 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4602 req->FileInfoClass = level;
4603 req->PersistentFileId = persistent_fid;
4604 req->VolatileFileId = volatile_fid;
4606 req->InputBufferOffset =
4607 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4608 req->OutputBufferLength = cpu_to_le32(
4609 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4611 iov->iov_base = (char *)req;
4612 iov->iov_len = total_len;
4617 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4618 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4620 struct smb_rqst rqst;
4621 struct smb2_query_info_rsp *rsp = NULL;
4623 struct kvec rsp_iov;
4626 struct cifs_ses *ses = tcon->ses;
4627 FILE_SYSTEM_POSIX_INFO *info = NULL;
4630 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4631 sizeof(FILE_SYSTEM_POSIX_INFO),
4632 persistent_fid, volatile_fid);
4636 if (smb3_encryption_required(tcon))
4637 flags |= CIFS_TRANSFORM_REQ;
4639 memset(&rqst, 0, sizeof(struct smb_rqst));
4643 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4644 cifs_small_buf_release(iov.iov_base);
4646 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4647 goto posix_qfsinf_exit;
4649 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4651 info = (FILE_SYSTEM_POSIX_INFO *)(
4652 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4653 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4654 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4655 sizeof(FILE_SYSTEM_POSIX_INFO));
4657 copy_posix_fs_info_to_kstatfs(info, fsdata);
4660 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4665 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4666 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4668 struct smb_rqst rqst;
4669 struct smb2_query_info_rsp *rsp = NULL;
4671 struct kvec rsp_iov;
4674 struct cifs_ses *ses = tcon->ses;
4675 struct smb2_fs_full_size_info *info = NULL;
4678 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4679 sizeof(struct smb2_fs_full_size_info),
4680 persistent_fid, volatile_fid);
4684 if (smb3_encryption_required(tcon))
4685 flags |= CIFS_TRANSFORM_REQ;
4687 memset(&rqst, 0, sizeof(struct smb_rqst));
4691 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4692 cifs_small_buf_release(iov.iov_base);
4694 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4697 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4699 info = (struct smb2_fs_full_size_info *)(
4700 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4701 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4702 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4703 sizeof(struct smb2_fs_full_size_info));
4705 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4708 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4713 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4714 u64 persistent_fid, u64 volatile_fid, int level)
4716 struct smb_rqst rqst;
4717 struct smb2_query_info_rsp *rsp = NULL;
4719 struct kvec rsp_iov;
4721 int resp_buftype, max_len, min_len;
4722 struct cifs_ses *ses = tcon->ses;
4723 unsigned int rsp_len, offset;
4726 if (level == FS_DEVICE_INFORMATION) {
4727 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4728 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4729 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4730 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4731 min_len = MIN_FS_ATTR_INFO_SIZE;
4732 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4733 max_len = sizeof(struct smb3_fs_ss_info);
4734 min_len = sizeof(struct smb3_fs_ss_info);
4735 } else if (level == FS_VOLUME_INFORMATION) {
4736 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4737 min_len = sizeof(struct smb3_fs_vol_info);
4739 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4743 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4744 persistent_fid, volatile_fid);
4748 if (smb3_encryption_required(tcon))
4749 flags |= CIFS_TRANSFORM_REQ;
4751 memset(&rqst, 0, sizeof(struct smb_rqst));
4755 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4756 cifs_small_buf_release(iov.iov_base);
4758 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4761 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4763 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4764 offset = le16_to_cpu(rsp->OutputBufferOffset);
4765 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4769 if (level == FS_ATTRIBUTE_INFORMATION)
4770 memcpy(&tcon->fsAttrInfo, offset
4771 + (char *)rsp, min_t(unsigned int,
4773 else if (level == FS_DEVICE_INFORMATION)
4774 memcpy(&tcon->fsDevInfo, offset
4775 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4776 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4777 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4778 (offset + (char *)rsp);
4779 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4780 tcon->perf_sector_size =
4781 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4782 } else if (level == FS_VOLUME_INFORMATION) {
4783 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4784 (offset + (char *)rsp);
4785 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4786 tcon->vol_create_time = vol_info->VolumeCreationTime;
4790 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4795 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4796 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4797 const __u32 num_lock, struct smb2_lock_element *buf)
4799 struct smb_rqst rqst;
4801 struct smb2_lock_req *req = NULL;
4803 struct kvec rsp_iov;
4806 int flags = CIFS_NO_RSP_BUF;
4807 unsigned int total_len;
4809 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4811 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4815 if (smb3_encryption_required(tcon))
4816 flags |= CIFS_TRANSFORM_REQ;
4818 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4819 req->LockCount = cpu_to_le16(num_lock);
4821 req->PersistentFileId = persist_fid;
4822 req->VolatileFileId = volatile_fid;
4824 count = num_lock * sizeof(struct smb2_lock_element);
4826 iov[0].iov_base = (char *)req;
4827 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4828 iov[1].iov_base = (char *)buf;
4829 iov[1].iov_len = count;
4831 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4833 memset(&rqst, 0, sizeof(struct smb_rqst));
4837 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4839 cifs_small_buf_release(req);
4841 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4842 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4843 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4844 tcon->ses->Suid, rc);
4851 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4852 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4853 const __u64 length, const __u64 offset, const __u32 lock_flags,
4856 struct smb2_lock_element lock;
4858 lock.Offset = cpu_to_le64(offset);
4859 lock.Length = cpu_to_le64(length);
4860 lock.Flags = cpu_to_le32(lock_flags);
4861 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4862 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4864 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4868 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4869 __u8 *lease_key, const __le32 lease_state)
4871 struct smb_rqst rqst;
4873 struct smb2_lease_ack *req = NULL;
4874 struct cifs_ses *ses = tcon->ses;
4875 int flags = CIFS_OBREAK_OP;
4876 unsigned int total_len;
4878 struct kvec rsp_iov;
4880 __u64 *please_key_high;
4881 __u64 *please_key_low;
4883 cifs_dbg(FYI, "SMB2_lease_break\n");
4884 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4889 if (smb3_encryption_required(tcon))
4890 flags |= CIFS_TRANSFORM_REQ;
4892 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4893 req->StructureSize = cpu_to_le16(36);
4896 memcpy(req->LeaseKey, lease_key, 16);
4897 req->LeaseState = lease_state;
4899 flags |= CIFS_NO_RSP_BUF;
4901 iov[0].iov_base = (char *)req;
4902 iov[0].iov_len = total_len;
4904 memset(&rqst, 0, sizeof(struct smb_rqst));
4908 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4909 cifs_small_buf_release(req);
4911 please_key_low = (__u64 *)lease_key;
4912 please_key_high = (__u64 *)(lease_key+8);
4914 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4915 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4916 ses->Suid, *please_key_low, *please_key_high, rc);
4917 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4919 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4920 ses->Suid, *please_key_low, *please_key_high);