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)
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);
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 cifs_mark_open_files_invalid(tcon);
362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
365 rc = __smb2_reconnect(nls_codepage, tcon);
366 mutex_unlock(&tcon->ses->session_mutex);
368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
378 atomic_inc(&tconInfoReconnectCount);
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
388 switch (smb2_command) {
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
400 unload_nls(nls_codepage);
405 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
429 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
430 void **request_buf, unsigned int *total_len)
432 /* BB eventually switch this to SMB2 specific small buf size */
433 if (smb2_command == SMB2_SET_INFO)
434 *request_buf = cifs_buf_get();
436 *request_buf = cifs_small_buf_get();
437 if (*request_buf == NULL) {
438 /* BB should we add a retry in here if not a writepage? */
442 fill_small_buf(smb2_command, tcon,
443 (struct smb2_sync_hdr *)(*request_buf),
447 uint16_t com_code = le16_to_cpu(smb2_command);
448 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
449 cifs_stats_inc(&tcon->num_smbs_sent);
455 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
456 void **request_buf, unsigned int *total_len)
460 rc = smb2_reconnect(smb2_command, tcon);
464 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
468 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
469 void **request_buf, unsigned int *total_len)
471 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
472 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
473 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
476 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
479 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
482 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
484 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
485 pneg_ctxt->DataLength = cpu_to_le16(38);
486 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
487 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
488 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
489 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
493 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
495 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
496 pneg_ctxt->DataLength =
497 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
498 - sizeof(struct smb2_neg_context));
499 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
500 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
501 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
502 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
506 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
508 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
509 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
510 pneg_ctxt->CipherCount = cpu_to_le16(2);
511 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
512 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
516 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
518 struct nls_table *cp = load_nls_default();
520 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
522 /* copy up to max of first 100 bytes of server name to NetName field */
523 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
524 /* context size is DataLength + minimal smb2_neg_context */
525 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
526 sizeof(struct smb2_neg_context), 8) * 8;
530 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
532 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
533 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
534 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
535 pneg_ctxt->Name[0] = 0x93;
536 pneg_ctxt->Name[1] = 0xAD;
537 pneg_ctxt->Name[2] = 0x25;
538 pneg_ctxt->Name[3] = 0x50;
539 pneg_ctxt->Name[4] = 0x9C;
540 pneg_ctxt->Name[5] = 0xB4;
541 pneg_ctxt->Name[6] = 0x11;
542 pneg_ctxt->Name[7] = 0xE7;
543 pneg_ctxt->Name[8] = 0xB4;
544 pneg_ctxt->Name[9] = 0x23;
545 pneg_ctxt->Name[10] = 0x83;
546 pneg_ctxt->Name[11] = 0xDE;
547 pneg_ctxt->Name[12] = 0x96;
548 pneg_ctxt->Name[13] = 0x8B;
549 pneg_ctxt->Name[14] = 0xCD;
550 pneg_ctxt->Name[15] = 0x7C;
554 assemble_neg_contexts(struct smb2_negotiate_req *req,
555 struct TCP_Server_Info *server, unsigned int *total_len)
558 unsigned int ctxt_len;
560 if (*total_len > 200) {
561 /* In case length corrupted don't want to overrun smb buffer */
562 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
567 * round up total_len of fixed part of SMB3 negotiate request to 8
568 * byte boundary before adding negotiate contexts
570 *total_len = roundup(*total_len, 8);
572 pneg_ctxt = (*total_len) + (char *)req;
573 req->NegotiateContextOffset = cpu_to_le32(*total_len);
575 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
576 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
577 *total_len += ctxt_len;
578 pneg_ctxt += ctxt_len;
580 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
581 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
585 if (server->compress_algorithm) {
586 build_compression_ctxt((struct smb2_compression_capabilities_context *)
588 ctxt_len = DIV_ROUND_UP(
589 sizeof(struct smb2_compression_capabilities_context),
591 *total_len += ctxt_len;
592 pneg_ctxt += ctxt_len;
593 req->NegotiateContextCount = cpu_to_le16(5);
595 req->NegotiateContextCount = cpu_to_le16(4);
597 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
599 *total_len += ctxt_len;
600 pneg_ctxt += ctxt_len;
602 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
603 *total_len += sizeof(struct smb2_posix_neg_context);
606 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
608 unsigned int len = le16_to_cpu(ctxt->DataLength);
610 /* If invalid preauth context warn but use what we requested, SHA-512 */
611 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
612 printk_once(KERN_WARNING "server sent bad preauth context\n");
615 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
616 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
617 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
618 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
621 static void decode_compress_ctx(struct TCP_Server_Info *server,
622 struct smb2_compression_capabilities_context *ctxt)
624 unsigned int len = le16_to_cpu(ctxt->DataLength);
626 /* sizeof compress context is a one element compression capbility struct */
628 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
631 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
632 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
635 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
636 printk_once(KERN_WARNING "unknown compression algorithm\n");
639 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
642 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
643 struct smb2_encryption_neg_context *ctxt)
645 unsigned int len = le16_to_cpu(ctxt->DataLength);
647 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
648 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
649 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
653 if (le16_to_cpu(ctxt->CipherCount) != 1) {
654 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
657 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
658 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
659 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
660 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
663 server->cipher_type = ctxt->Ciphers[0];
664 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
668 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
669 struct TCP_Server_Info *server,
670 unsigned int len_of_smb)
672 struct smb2_neg_context *pctx;
673 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
674 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
675 unsigned int len_of_ctxts, i;
678 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
679 if (len_of_smb <= offset) {
680 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
684 len_of_ctxts = len_of_smb - offset;
686 for (i = 0; i < ctxt_cnt; i++) {
688 /* check that offset is not beyond end of SMB */
689 if (len_of_ctxts == 0)
692 if (len_of_ctxts < sizeof(struct smb2_neg_context))
695 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
696 clen = le16_to_cpu(pctx->DataLength);
697 if (clen > len_of_ctxts)
700 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
701 decode_preauth_context(
702 (struct smb2_preauth_neg_context *)pctx);
703 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
704 rc = decode_encrypt_ctx(server,
705 (struct smb2_encryption_neg_context *)pctx);
706 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
707 decode_compress_ctx(server,
708 (struct smb2_compression_capabilities_context *)pctx);
709 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
710 server->posix_ext_supported = true;
712 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
713 le16_to_cpu(pctx->ContextType));
717 /* offsets must be 8 byte aligned */
718 clen = (clen + 7) & ~0x7;
719 offset += clen + sizeof(struct smb2_neg_context);
720 len_of_ctxts -= clen;
725 static struct create_posix *
726 create_posix_buf(umode_t mode)
728 struct create_posix *buf;
730 buf = kzalloc(sizeof(struct create_posix),
735 buf->ccontext.DataOffset =
736 cpu_to_le16(offsetof(struct create_posix, Mode));
737 buf->ccontext.DataLength = cpu_to_le32(4);
738 buf->ccontext.NameOffset =
739 cpu_to_le16(offsetof(struct create_posix, Name));
740 buf->ccontext.NameLength = cpu_to_le16(16);
742 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
753 buf->Name[10] = 0x83;
754 buf->Name[11] = 0xDE;
755 buf->Name[12] = 0x96;
756 buf->Name[13] = 0x8B;
757 buf->Name[14] = 0xCD;
758 buf->Name[15] = 0x7C;
759 buf->Mode = cpu_to_le32(mode);
760 cifs_dbg(FYI, "mode on posix create 0%o", mode);
765 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
767 struct smb2_create_req *req = iov[0].iov_base;
768 unsigned int num = *num_iovec;
770 iov[num].iov_base = create_posix_buf(mode);
771 if (mode == ACL_NO_MODE)
772 cifs_dbg(FYI, "illegal mode\n");
773 if (iov[num].iov_base == NULL)
775 iov[num].iov_len = sizeof(struct create_posix);
776 if (!req->CreateContextsOffset)
777 req->CreateContextsOffset = cpu_to_le32(
778 sizeof(struct smb2_create_req) +
779 iov[num - 1].iov_len);
780 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
781 *num_iovec = num + 1;
788 * SMB2 Worker functions follow:
790 * The general structure of the worker functions is:
791 * 1) Call smb2_init (assembles SMB2 header)
792 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
793 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
794 * 4) Decode SMB2 command specific fields in the fixed length area
795 * 5) Decode variable length data area (if any for this SMB2 command type)
796 * 6) Call free smb buffer
802 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
804 struct smb_rqst rqst;
805 struct smb2_negotiate_req *req;
806 struct smb2_negotiate_rsp *rsp;
811 struct TCP_Server_Info *server = cifs_ses_server(ses);
812 int blob_offset, blob_length;
814 int flags = CIFS_NEG_OP;
815 unsigned int total_len;
817 cifs_dbg(FYI, "Negotiate protocol\n");
820 WARN(1, "%s: server is NULL!\n", __func__);
824 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
828 req->sync_hdr.SessionId = 0;
830 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
831 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
833 if (strcmp(server->vals->version_string,
834 SMB3ANY_VERSION_STRING) == 0) {
835 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
836 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
837 req->DialectCount = cpu_to_le16(2);
839 } else if (strcmp(server->vals->version_string,
840 SMBDEFAULT_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
843 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
844 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
845 req->DialectCount = cpu_to_le16(4);
848 /* otherwise send specific dialect */
849 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
850 req->DialectCount = cpu_to_le16(1);
854 /* only one of SMB2 signing flags may be set in SMB2 request */
856 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
857 else if (global_secflags & CIFSSEC_MAY_SIGN)
858 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
860 req->SecurityMode = 0;
862 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
864 /* ClientGUID must be zero for SMB2.02 dialect */
865 if (server->vals->protocol_id == SMB20_PROT_ID)
866 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
868 memcpy(req->ClientGUID, server->client_guid,
869 SMB2_CLIENT_GUID_SIZE);
870 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
871 (strcmp(server->vals->version_string,
872 SMBDEFAULT_VERSION_STRING) == 0))
873 assemble_neg_contexts(req, server, &total_len);
875 iov[0].iov_base = (char *)req;
876 iov[0].iov_len = total_len;
878 memset(&rqst, 0, sizeof(struct smb_rqst));
882 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
883 cifs_small_buf_release(req);
884 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
886 * No tcon so can't do
887 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
889 if (rc == -EOPNOTSUPP) {
890 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
891 "specifying vers=1.0 or vers=2.0 on mount for accessing"
897 if (strcmp(server->vals->version_string,
898 SMB3ANY_VERSION_STRING) == 0) {
899 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
901 "SMB2 dialect returned but not requested\n");
903 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
905 "SMB2.1 dialect returned but not requested\n");
908 } else if (strcmp(server->vals->version_string,
909 SMBDEFAULT_VERSION_STRING) == 0) {
910 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
912 "SMB2 dialect returned but not requested\n");
914 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
915 /* ops set to 3.0 by default for default so update */
916 server->ops = &smb21_operations;
917 server->vals = &smb21_values;
918 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
919 server->ops = &smb311_operations;
920 server->vals = &smb311_values;
922 } else if (le16_to_cpu(rsp->DialectRevision) !=
923 server->vals->protocol_id) {
924 /* if requested single dialect ensure returned dialect matched */
925 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
926 le16_to_cpu(rsp->DialectRevision));
930 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
932 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
933 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
934 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
935 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
936 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
937 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
938 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
939 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
940 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
941 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
943 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
944 le16_to_cpu(rsp->DialectRevision));
948 server->dialect = le16_to_cpu(rsp->DialectRevision);
951 * Keep a copy of the hash after negprot. This hash will be
952 * the starting hash value for all sessions made from this
955 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
956 SMB2_PREAUTH_HASH_SIZE);
958 /* SMB2 only has an extended negflavor */
959 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
960 /* set it to the maximum buffer size value we can send with 1 credit */
961 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
962 SMB2_MAX_BUFFER_SIZE);
963 server->max_read = le32_to_cpu(rsp->MaxReadSize);
964 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
965 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
966 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
967 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
969 server->capabilities = le32_to_cpu(rsp->Capabilities);
971 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
973 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
974 (struct smb2_sync_hdr *)rsp);
976 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
978 * ses->sectype = RawNTLMSSP;
979 * but for time being this is our only auth choice so doesn't matter.
980 * We just found a server which sets blob length to zero expecting raw.
982 if (blob_length == 0) {
983 cifs_dbg(FYI, "missing security blob on negprot\n");
984 server->sec_ntlmssp = true;
987 rc = cifs_enable_signing(server, ses->sign);
991 rc = decode_negTokenInit(security_blob, blob_length, server);
998 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
999 if (rsp->NegotiateContextCount)
1000 rc = smb311_decode_neg_context(rsp, server,
1003 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1006 free_rsp_buf(resp_buftype, rsp);
1010 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1013 struct validate_negotiate_info_req *pneg_inbuf;
1014 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1016 u32 inbuflen; /* max of 4 dialects */
1017 struct TCP_Server_Info *server = tcon->ses->server;
1019 cifs_dbg(FYI, "validate negotiate\n");
1021 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1022 if (server->dialect == SMB311_PROT_ID)
1026 * validation ioctl must be signed, so no point sending this if we
1027 * can not sign it (ie are not known user). Even if signing is not
1028 * required (enabled but not negotiated), in those cases we selectively
1029 * sign just this, the first and only signed request on a connection.
1030 * Having validation of negotiate info helps reduce attack vectors.
1032 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1033 return 0; /* validation requires signing */
1035 if (tcon->ses->user_name == NULL) {
1036 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1037 return 0; /* validation requires signing */
1040 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1041 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1043 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1047 pneg_inbuf->Capabilities =
1048 cpu_to_le32(server->vals->req_capabilities);
1049 memcpy(pneg_inbuf->Guid, server->client_guid,
1050 SMB2_CLIENT_GUID_SIZE);
1052 if (tcon->ses->sign)
1053 pneg_inbuf->SecurityMode =
1054 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1055 else if (global_secflags & CIFSSEC_MAY_SIGN)
1056 pneg_inbuf->SecurityMode =
1057 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1059 pneg_inbuf->SecurityMode = 0;
1062 if (strcmp(server->vals->version_string,
1063 SMB3ANY_VERSION_STRING) == 0) {
1064 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1065 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1066 pneg_inbuf->DialectCount = cpu_to_le16(2);
1067 /* structure is big enough for 3 dialects, sending only 2 */
1068 inbuflen = sizeof(*pneg_inbuf) -
1069 (2 * sizeof(pneg_inbuf->Dialects[0]));
1070 } else if (strcmp(server->vals->version_string,
1071 SMBDEFAULT_VERSION_STRING) == 0) {
1072 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1073 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1074 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1075 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1076 pneg_inbuf->DialectCount = cpu_to_le16(4);
1077 /* structure is big enough for 3 dialects */
1078 inbuflen = sizeof(*pneg_inbuf);
1080 /* otherwise specific dialect was requested */
1081 pneg_inbuf->Dialects[0] =
1082 cpu_to_le16(server->vals->protocol_id);
1083 pneg_inbuf->DialectCount = cpu_to_le16(1);
1084 /* structure is big enough for 3 dialects, sending only 1 */
1085 inbuflen = sizeof(*pneg_inbuf) -
1086 sizeof(pneg_inbuf->Dialects[0]) * 2;
1089 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1090 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1091 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1092 (char **)&pneg_rsp, &rsplen);
1093 if (rc == -EOPNOTSUPP) {
1095 * Old Windows versions or Netapp SMB server can return
1096 * not supported error. Client should accept it.
1098 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1100 goto out_free_inbuf;
1101 } else if (rc != 0) {
1102 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1104 goto out_free_inbuf;
1108 if (rsplen != sizeof(*pneg_rsp)) {
1109 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1112 /* relax check since Mac returns max bufsize allowed on ioctl */
1113 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1117 /* check validate negotiate info response matches what we got earlier */
1118 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1121 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1124 /* do not validate server guid because not saved at negprot time yet */
1126 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1127 SMB2_LARGE_FILES) != server->capabilities)
1130 /* validate negotiate successful */
1132 cifs_dbg(FYI, "validate negotiate info successful\n");
1136 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1145 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1147 switch (requested) {
1154 if (server->sec_ntlmssp &&
1155 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1157 if ((server->sec_kerberos || server->sec_mskerberos) &&
1158 (global_secflags & CIFSSEC_MAY_KRB5))
1166 struct SMB2_sess_data {
1168 struct cifs_ses *ses;
1169 struct nls_table *nls_cp;
1170 void (*func)(struct SMB2_sess_data *);
1172 u64 previous_session;
1174 /* we will send the SMB in three pieces:
1175 * a fixed length beginning part, an optional
1176 * SPNEGO blob (which can be zero length), and a
1177 * last part which will include the strings
1178 * and rest of bcc area. This allows us to avoid
1179 * a large buffer 17K allocation
1186 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1189 struct cifs_ses *ses = sess_data->ses;
1190 struct smb2_sess_setup_req *req;
1191 struct TCP_Server_Info *server = cifs_ses_server(ses);
1192 unsigned int total_len;
1194 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1199 if (sess_data->ses->binding) {
1200 req->sync_hdr.SessionId = sess_data->ses->Suid;
1201 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1202 req->PreviousSessionId = 0;
1203 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1205 /* First session, not a reauthenticate */
1206 req->sync_hdr.SessionId = 0;
1208 * if reconnect, we need to send previous sess id
1211 req->PreviousSessionId = sess_data->previous_session;
1212 req->Flags = 0; /* MBZ */
1215 /* enough to enable echos and oplocks and one max size write */
1216 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1218 /* only one of SMB2 signing flags may be set in SMB2 request */
1220 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1221 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1222 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1224 req->SecurityMode = 0;
1226 #ifdef CONFIG_CIFS_DFS_UPCALL
1227 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1229 req->Capabilities = 0;
1230 #endif /* DFS_UPCALL */
1232 req->Channel = 0; /* MBZ */
1234 sess_data->iov[0].iov_base = (char *)req;
1236 sess_data->iov[0].iov_len = total_len - 1;
1238 * This variable will be used to clear the buffer
1239 * allocated above in case of any error in the calling function.
1241 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1247 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1249 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1250 sess_data->buf0_type = CIFS_NO_BUFFER;
1254 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1257 struct smb_rqst rqst;
1258 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1259 struct kvec rsp_iov = { NULL, 0 };
1261 /* Testing shows that buffer offset must be at location of Buffer[0] */
1262 req->SecurityBufferOffset =
1263 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1264 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1266 memset(&rqst, 0, sizeof(struct smb_rqst));
1267 rqst.rq_iov = sess_data->iov;
1270 /* BB add code to build os and lm fields */
1271 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1273 &sess_data->buf0_type,
1274 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1275 cifs_small_buf_release(sess_data->iov[0].iov_base);
1276 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1282 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1285 struct cifs_ses *ses = sess_data->ses;
1286 struct TCP_Server_Info *server = cifs_ses_server(ses);
1288 mutex_lock(&server->srv_mutex);
1289 if (server->ops->generate_signingkey) {
1290 rc = server->ops->generate_signingkey(ses);
1293 "SMB3 session key generation failed\n");
1294 mutex_unlock(&server->srv_mutex);
1298 if (!server->session_estab) {
1299 server->sequence_number = 0x2;
1300 server->session_estab = true;
1302 mutex_unlock(&server->srv_mutex);
1304 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1305 /* keep existing ses state if binding */
1306 if (!ses->binding) {
1307 spin_lock(&GlobalMid_Lock);
1308 ses->status = CifsGood;
1309 ses->need_reconnect = false;
1310 spin_unlock(&GlobalMid_Lock);
1316 #ifdef CONFIG_CIFS_UPCALL
1318 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1321 struct cifs_ses *ses = sess_data->ses;
1322 struct cifs_spnego_msg *msg;
1323 struct key *spnego_key = NULL;
1324 struct smb2_sess_setup_rsp *rsp = NULL;
1326 rc = SMB2_sess_alloc_buffer(sess_data);
1330 spnego_key = cifs_get_spnego_key(ses);
1331 if (IS_ERR(spnego_key)) {
1332 rc = PTR_ERR(spnego_key);
1337 msg = spnego_key->payload.data[0];
1339 * check version field to make sure that cifs.upcall is
1340 * sending us a response in an expected form
1342 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1344 "bad cifs.upcall version. Expected %d got %d",
1345 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1347 goto out_put_spnego_key;
1350 /* keep session key if binding */
1351 if (!ses->binding) {
1352 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1354 if (!ses->auth_key.response) {
1356 "Kerberos can't allocate (%u bytes) memory",
1359 goto out_put_spnego_key;
1361 ses->auth_key.len = msg->sesskey_len;
1364 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1365 sess_data->iov[1].iov_len = msg->secblob_len;
1367 rc = SMB2_sess_sendreceive(sess_data);
1369 goto out_put_spnego_key;
1371 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1372 /* keep session id and flags if binding */
1373 if (!ses->binding) {
1374 ses->Suid = rsp->sync_hdr.SessionId;
1375 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1378 rc = SMB2_sess_establish_session(sess_data);
1380 key_invalidate(spnego_key);
1381 key_put(spnego_key);
1383 sess_data->result = rc;
1384 sess_data->func = NULL;
1385 SMB2_sess_free_buffer(sess_data);
1389 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1391 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1392 sess_data->result = -EOPNOTSUPP;
1393 sess_data->func = NULL;
1398 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1401 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1404 struct cifs_ses *ses = sess_data->ses;
1405 struct smb2_sess_setup_rsp *rsp = NULL;
1406 char *ntlmssp_blob = NULL;
1407 bool use_spnego = false; /* else use raw ntlmssp */
1408 u16 blob_length = 0;
1411 * If memory allocation is successful, caller of this function
1414 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1415 if (!ses->ntlmssp) {
1419 ses->ntlmssp->sesskey_per_smbsess = true;
1421 rc = SMB2_sess_alloc_buffer(sess_data);
1425 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1427 if (ntlmssp_blob == NULL) {
1432 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1434 /* BB eventually need to add this */
1435 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1439 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1440 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1442 sess_data->iov[1].iov_base = ntlmssp_blob;
1443 sess_data->iov[1].iov_len = blob_length;
1445 rc = SMB2_sess_sendreceive(sess_data);
1446 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1448 /* If true, rc here is expected and not an error */
1449 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1450 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1456 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1457 le16_to_cpu(rsp->SecurityBufferOffset)) {
1458 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1459 le16_to_cpu(rsp->SecurityBufferOffset));
1463 rc = decode_ntlmssp_challenge(rsp->Buffer,
1464 le16_to_cpu(rsp->SecurityBufferLength), ses);
1468 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1470 /* keep existing ses id and flags if binding */
1471 if (!ses->binding) {
1472 ses->Suid = rsp->sync_hdr.SessionId;
1473 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1477 kfree(ntlmssp_blob);
1478 SMB2_sess_free_buffer(sess_data);
1480 sess_data->result = 0;
1481 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1485 kfree(ses->ntlmssp);
1486 ses->ntlmssp = NULL;
1487 sess_data->result = rc;
1488 sess_data->func = NULL;
1492 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1495 struct cifs_ses *ses = sess_data->ses;
1496 struct smb2_sess_setup_req *req;
1497 struct smb2_sess_setup_rsp *rsp = NULL;
1498 unsigned char *ntlmssp_blob = NULL;
1499 bool use_spnego = false; /* else use raw ntlmssp */
1500 u16 blob_length = 0;
1502 rc = SMB2_sess_alloc_buffer(sess_data);
1506 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1507 req->sync_hdr.SessionId = ses->Suid;
1509 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1512 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1517 /* BB eventually need to add this */
1518 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1522 sess_data->iov[1].iov_base = ntlmssp_blob;
1523 sess_data->iov[1].iov_len = blob_length;
1525 rc = SMB2_sess_sendreceive(sess_data);
1529 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1531 /* keep existing ses id and flags if binding */
1532 if (!ses->binding) {
1533 ses->Suid = rsp->sync_hdr.SessionId;
1534 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1537 rc = SMB2_sess_establish_session(sess_data);
1539 kfree(ntlmssp_blob);
1540 SMB2_sess_free_buffer(sess_data);
1541 kfree(ses->ntlmssp);
1542 ses->ntlmssp = NULL;
1543 sess_data->result = rc;
1544 sess_data->func = NULL;
1548 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1552 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1553 cifs_dbg(FYI, "sess setup type %d\n", type);
1554 if (type == Unspecified) {
1556 "Unable to select appropriate authentication method!");
1562 sess_data->func = SMB2_auth_kerberos;
1565 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1568 cifs_dbg(VFS, "secType %d not supported!\n", type);
1576 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1577 const struct nls_table *nls_cp)
1580 struct TCP_Server_Info *server = cifs_ses_server(ses);
1581 struct SMB2_sess_data *sess_data;
1583 cifs_dbg(FYI, "Session Setup\n");
1586 WARN(1, "%s: server is NULL!\n", __func__);
1590 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1594 rc = SMB2_select_sec(ses, sess_data);
1597 sess_data->xid = xid;
1598 sess_data->ses = ses;
1599 sess_data->buf0_type = CIFS_NO_BUFFER;
1600 sess_data->nls_cp = (struct nls_table *) nls_cp;
1601 sess_data->previous_session = ses->Suid;
1604 * Initialize the session hash with the server one.
1606 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1607 SMB2_PREAUTH_HASH_SIZE);
1609 while (sess_data->func)
1610 sess_data->func(sess_data);
1612 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1613 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1614 rc = sess_data->result;
1621 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1623 struct smb_rqst rqst;
1624 struct smb2_logoff_req *req; /* response is also trivial struct */
1626 struct TCP_Server_Info *server;
1628 unsigned int total_len;
1630 struct kvec rsp_iov;
1633 cifs_dbg(FYI, "disconnect session %p\n", ses);
1635 if (ses && (ses->server))
1636 server = ses->server;
1640 /* no need to send SMB logoff if uid already closed due to reconnect */
1641 if (ses->need_reconnect)
1642 goto smb2_session_already_dead;
1644 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1648 /* since no tcon, smb2_init can not do this, so do here */
1649 req->sync_hdr.SessionId = ses->Suid;
1651 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1652 flags |= CIFS_TRANSFORM_REQ;
1653 else if (server->sign)
1654 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1656 flags |= CIFS_NO_RSP_BUF;
1658 iov[0].iov_base = (char *)req;
1659 iov[0].iov_len = total_len;
1661 memset(&rqst, 0, sizeof(struct smb_rqst));
1665 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1666 cifs_small_buf_release(req);
1668 * No tcon so can't do
1669 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1672 smb2_session_already_dead:
1676 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1678 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1681 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1683 /* These are similar values to what Windows uses */
1684 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1686 tcon->max_chunks = 256;
1687 tcon->max_bytes_chunk = 1048576;
1688 tcon->max_bytes_copy = 16777216;
1692 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1693 struct cifs_tcon *tcon, const struct nls_table *cp)
1695 struct smb_rqst rqst;
1696 struct smb2_tree_connect_req *req;
1697 struct smb2_tree_connect_rsp *rsp = NULL;
1699 struct kvec rsp_iov = { NULL, 0 };
1703 __le16 *unc_path = NULL;
1705 unsigned int total_len;
1706 struct TCP_Server_Info *server = ses->server;
1708 cifs_dbg(FYI, "TCON\n");
1710 if (!server || !tree)
1713 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1714 if (unc_path == NULL)
1717 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1719 if (unc_path_len < 2) {
1724 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1726 atomic_set(&tcon->num_remote_opens, 0);
1727 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1734 if (smb3_encryption_required(tcon))
1735 flags |= CIFS_TRANSFORM_REQ;
1737 iov[0].iov_base = (char *)req;
1739 iov[0].iov_len = total_len - 1;
1741 /* Testing shows that buffer offset must be at location of Buffer[0] */
1742 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1744 req->PathLength = cpu_to_le16(unc_path_len - 2);
1745 iov[1].iov_base = unc_path;
1746 iov[1].iov_len = unc_path_len;
1749 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1750 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1751 * (Samba servers don't always set the flag so also check if null user)
1753 if ((server->dialect == SMB311_PROT_ID) &&
1754 !smb3_encryption_required(tcon) &&
1755 !(ses->session_flags &
1756 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1757 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1758 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1760 memset(&rqst, 0, sizeof(struct smb_rqst));
1764 /* Need 64 for max size write so ask for more in case not there yet */
1765 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1767 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1768 cifs_small_buf_release(req);
1769 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1770 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1773 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1774 tcon->need_reconnect = true;
1776 goto tcon_error_exit;
1779 switch (rsp->ShareType) {
1780 case SMB2_SHARE_TYPE_DISK:
1781 cifs_dbg(FYI, "connection to disk share\n");
1783 case SMB2_SHARE_TYPE_PIPE:
1785 cifs_dbg(FYI, "connection to pipe share\n");
1787 case SMB2_SHARE_TYPE_PRINT:
1789 cifs_dbg(FYI, "connection to printer\n");
1792 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1794 goto tcon_error_exit;
1797 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1798 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1799 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1800 tcon->tidStatus = CifsGood;
1801 tcon->need_reconnect = false;
1802 tcon->tid = rsp->sync_hdr.TreeId;
1803 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1805 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1806 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1807 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1810 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1811 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1813 init_copy_chunk_defaults(tcon);
1814 if (server->ops->validate_negotiate)
1815 rc = server->ops->validate_negotiate(xid, tcon);
1818 free_rsp_buf(resp_buftype, rsp);
1823 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1824 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1830 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1832 struct smb_rqst rqst;
1833 struct smb2_tree_disconnect_req *req; /* response is trivial */
1835 struct cifs_ses *ses = tcon->ses;
1837 unsigned int total_len;
1839 struct kvec rsp_iov;
1842 cifs_dbg(FYI, "Tree Disconnect\n");
1844 if (!ses || !(ses->server))
1847 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1850 close_shroot_lease(&tcon->crfid);
1852 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1857 if (smb3_encryption_required(tcon))
1858 flags |= CIFS_TRANSFORM_REQ;
1860 flags |= CIFS_NO_RSP_BUF;
1862 iov[0].iov_base = (char *)req;
1863 iov[0].iov_len = total_len;
1865 memset(&rqst, 0, sizeof(struct smb_rqst));
1869 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1870 cifs_small_buf_release(req);
1872 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1878 static struct create_durable *
1879 create_durable_buf(void)
1881 struct create_durable *buf;
1883 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1887 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1888 (struct create_durable, Data));
1889 buf->ccontext.DataLength = cpu_to_le32(16);
1890 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1891 (struct create_durable, Name));
1892 buf->ccontext.NameLength = cpu_to_le16(4);
1893 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1901 static struct create_durable *
1902 create_reconnect_durable_buf(struct cifs_fid *fid)
1904 struct create_durable *buf;
1906 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1910 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1911 (struct create_durable, Data));
1912 buf->ccontext.DataLength = cpu_to_le32(16);
1913 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1914 (struct create_durable, Name));
1915 buf->ccontext.NameLength = cpu_to_le16(4);
1916 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1917 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1918 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1927 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1929 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1931 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1932 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1933 buf->IndexNumber = pdisk_id->DiskFileId;
1937 smb2_parse_contexts(struct TCP_Server_Info *server,
1938 struct smb2_create_rsp *rsp,
1939 unsigned int *epoch, char *lease_key, __u8 *oplock,
1940 struct smb2_file_all_info *buf)
1943 struct create_context *cc;
1945 unsigned int remaining;
1949 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1950 remaining = le32_to_cpu(rsp->CreateContextsLength);
1951 cc = (struct create_context *)data_offset;
1953 /* Initialize inode number to 0 in case no valid data in qfid context */
1955 buf->IndexNumber = 0;
1957 while (remaining >= sizeof(struct create_context)) {
1958 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1959 if (le16_to_cpu(cc->NameLength) == 4 &&
1960 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1961 *oplock = server->ops->parse_lease_buf(cc, epoch,
1963 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1964 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1965 parse_query_id_ctxt(cc, buf);
1967 next = le32_to_cpu(cc->Next);
1971 cc = (struct create_context *)((char *)cc + next);
1974 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1975 *oplock = rsp->OplockLevel;
1981 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1982 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1984 struct smb2_create_req *req = iov[0].iov_base;
1985 unsigned int num = *num_iovec;
1987 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1988 if (iov[num].iov_base == NULL)
1990 iov[num].iov_len = server->vals->create_lease_size;
1991 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1992 if (!req->CreateContextsOffset)
1993 req->CreateContextsOffset = cpu_to_le32(
1994 sizeof(struct smb2_create_req) +
1995 iov[num - 1].iov_len);
1996 le32_add_cpu(&req->CreateContextsLength,
1997 server->vals->create_lease_size);
1998 *num_iovec = num + 1;
2002 static struct create_durable_v2 *
2003 create_durable_v2_buf(struct cifs_open_parms *oparms)
2005 struct cifs_fid *pfid = oparms->fid;
2006 struct create_durable_v2 *buf;
2008 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2012 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2013 (struct create_durable_v2, dcontext));
2014 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2015 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2016 (struct create_durable_v2, Name));
2017 buf->ccontext.NameLength = cpu_to_le16(4);
2020 * NB: Handle timeout defaults to 0, which allows server to choose
2021 * (most servers default to 120 seconds) and most clients default to 0.
2022 * This can be overridden at mount ("handletimeout=") if the user wants
2023 * a different persistent (or resilient) handle timeout for all opens
2024 * opens on a particular SMB3 mount.
2026 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2027 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2028 generate_random_uuid(buf->dcontext.CreateGuid);
2029 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2031 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2039 static struct create_durable_handle_reconnect_v2 *
2040 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2042 struct create_durable_handle_reconnect_v2 *buf;
2044 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2049 buf->ccontext.DataOffset =
2050 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2052 buf->ccontext.DataLength =
2053 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2054 buf->ccontext.NameOffset =
2055 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2057 buf->ccontext.NameLength = cpu_to_le16(4);
2059 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2060 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2061 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2062 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2064 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2073 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2074 struct cifs_open_parms *oparms)
2076 struct smb2_create_req *req = iov[0].iov_base;
2077 unsigned int num = *num_iovec;
2079 iov[num].iov_base = create_durable_v2_buf(oparms);
2080 if (iov[num].iov_base == NULL)
2082 iov[num].iov_len = sizeof(struct create_durable_v2);
2083 if (!req->CreateContextsOffset)
2084 req->CreateContextsOffset =
2085 cpu_to_le32(sizeof(struct smb2_create_req) +
2087 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2088 *num_iovec = num + 1;
2093 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2094 struct cifs_open_parms *oparms)
2096 struct smb2_create_req *req = iov[0].iov_base;
2097 unsigned int num = *num_iovec;
2099 /* indicate that we don't need to relock the file */
2100 oparms->reconnect = false;
2102 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2103 if (iov[num].iov_base == NULL)
2105 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2106 if (!req->CreateContextsOffset)
2107 req->CreateContextsOffset =
2108 cpu_to_le32(sizeof(struct smb2_create_req) +
2110 le32_add_cpu(&req->CreateContextsLength,
2111 sizeof(struct create_durable_handle_reconnect_v2));
2112 *num_iovec = num + 1;
2117 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2118 struct cifs_open_parms *oparms, bool use_persistent)
2120 struct smb2_create_req *req = iov[0].iov_base;
2121 unsigned int num = *num_iovec;
2123 if (use_persistent) {
2124 if (oparms->reconnect)
2125 return add_durable_reconnect_v2_context(iov, num_iovec,
2128 return add_durable_v2_context(iov, num_iovec, oparms);
2131 if (oparms->reconnect) {
2132 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2133 /* indicate that we don't need to relock the file */
2134 oparms->reconnect = false;
2136 iov[num].iov_base = create_durable_buf();
2137 if (iov[num].iov_base == NULL)
2139 iov[num].iov_len = sizeof(struct create_durable);
2140 if (!req->CreateContextsOffset)
2141 req->CreateContextsOffset =
2142 cpu_to_le32(sizeof(struct smb2_create_req) +
2144 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2145 *num_iovec = num + 1;
2149 /* See MS-SMB2 2.2.13.2.7 */
2150 static struct crt_twarp_ctxt *
2151 create_twarp_buf(__u64 timewarp)
2153 struct crt_twarp_ctxt *buf;
2155 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2159 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2160 (struct crt_twarp_ctxt, Timestamp));
2161 buf->ccontext.DataLength = cpu_to_le32(8);
2162 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2163 (struct crt_twarp_ctxt, Name));
2164 buf->ccontext.NameLength = cpu_to_le16(4);
2165 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2170 buf->Timestamp = cpu_to_le64(timewarp);
2174 /* See MS-SMB2 2.2.13.2.7 */
2176 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2178 struct smb2_create_req *req = iov[0].iov_base;
2179 unsigned int num = *num_iovec;
2181 iov[num].iov_base = create_twarp_buf(timewarp);
2182 if (iov[num].iov_base == NULL)
2184 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2185 if (!req->CreateContextsOffset)
2186 req->CreateContextsOffset = cpu_to_le32(
2187 sizeof(struct smb2_create_req) +
2188 iov[num - 1].iov_len);
2189 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2190 *num_iovec = num + 1;
2194 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2195 static struct crt_sd_ctxt *
2196 create_sd_buf(umode_t mode, unsigned int *len)
2198 struct crt_sd_ctxt *buf;
2199 struct cifs_ace *pace;
2200 unsigned int sdlen, acelen;
2202 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace), 8);
2203 buf = kzalloc(*len, GFP_KERNEL);
2207 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2208 sizeof(struct cifs_ace);
2210 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2211 (struct crt_sd_ctxt, sd));
2212 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2213 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2214 (struct crt_sd_ctxt, Name));
2215 buf->ccontext.NameLength = cpu_to_le16(4);
2216 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2221 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2223 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2224 * and "DP" ie the DACL is present
2226 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2228 /* offset owner, group and Sbz1 and SACL are all zero */
2229 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2230 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2232 /* create one ACE to hold the mode embedded in reserved special SID */
2233 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2234 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2235 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2236 buf->acl.AceCount = cpu_to_le16(1);
2241 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2243 struct smb2_create_req *req = iov[0].iov_base;
2244 unsigned int num = *num_iovec;
2245 unsigned int len = 0;
2247 iov[num].iov_base = create_sd_buf(mode, &len);
2248 if (iov[num].iov_base == NULL)
2250 iov[num].iov_len = len;
2251 if (!req->CreateContextsOffset)
2252 req->CreateContextsOffset = cpu_to_le32(
2253 sizeof(struct smb2_create_req) +
2254 iov[num - 1].iov_len);
2255 le32_add_cpu(&req->CreateContextsLength, len);
2256 *num_iovec = num + 1;
2260 static struct crt_query_id_ctxt *
2261 create_query_id_buf(void)
2263 struct crt_query_id_ctxt *buf;
2265 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2269 buf->ccontext.DataOffset = cpu_to_le16(0);
2270 buf->ccontext.DataLength = cpu_to_le32(0);
2271 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2272 (struct crt_query_id_ctxt, Name));
2273 buf->ccontext.NameLength = cpu_to_le16(4);
2274 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2282 /* See MS-SMB2 2.2.13.2.9 */
2284 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2286 struct smb2_create_req *req = iov[0].iov_base;
2287 unsigned int num = *num_iovec;
2289 iov[num].iov_base = create_query_id_buf();
2290 if (iov[num].iov_base == NULL)
2292 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2293 if (!req->CreateContextsOffset)
2294 req->CreateContextsOffset = cpu_to_le32(
2295 sizeof(struct smb2_create_req) +
2296 iov[num - 1].iov_len);
2297 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2298 *num_iovec = num + 1;
2303 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2304 const char *treename, const __le16 *path)
2306 int treename_len, path_len;
2307 struct nls_table *cp;
2308 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2313 treename_len = strlen(treename);
2314 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2320 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2323 * make room for one path separator between the treename and
2326 *out_len = treename_len + 1 + path_len;
2329 * final path needs to be null-terminated UTF16 with a
2333 *out_size = roundup((*out_len+1)*2, 8);
2334 *out_path = kzalloc(*out_size, GFP_KERNEL);
2338 cp = load_nls_default();
2339 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2340 UniStrcat(*out_path, sep);
2341 UniStrcat(*out_path, path);
2347 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2348 umode_t mode, struct cifs_tcon *tcon,
2349 const char *full_path,
2350 struct cifs_sb_info *cifs_sb)
2352 struct smb_rqst rqst;
2353 struct smb2_create_req *req;
2354 struct smb2_create_rsp *rsp = NULL;
2355 struct cifs_ses *ses = tcon->ses;
2356 struct kvec iov[3]; /* make sure at least one for each open context */
2357 struct kvec rsp_iov = {NULL, 0};
2360 __le16 *copy_path = NULL;
2363 unsigned int n_iov = 2;
2364 __u32 file_attributes = 0;
2365 char *pc_buf = NULL;
2367 unsigned int total_len;
2368 __le16 *utf16_path = NULL;
2370 cifs_dbg(FYI, "mkdir\n");
2372 /* resource #1: path allocation */
2373 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2377 if (!ses || !(ses->server)) {
2382 /* resource #2: request */
2383 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2388 if (smb3_encryption_required(tcon))
2389 flags |= CIFS_TRANSFORM_REQ;
2391 req->ImpersonationLevel = IL_IMPERSONATION;
2392 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2393 /* File attributes ignored on open (used in create though) */
2394 req->FileAttributes = cpu_to_le32(file_attributes);
2395 req->ShareAccess = FILE_SHARE_ALL_LE;
2396 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2397 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2399 iov[0].iov_base = (char *)req;
2400 /* -1 since last byte is buf[0] which is sent below (path) */
2401 iov[0].iov_len = total_len - 1;
2403 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2405 /* [MS-SMB2] 2.2.13 NameOffset:
2406 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2407 * the SMB2 header, the file name includes a prefix that will
2408 * be processed during DFS name normalization as specified in
2409 * section 3.3.5.9. Otherwise, the file name is relative to
2410 * the share that is identified by the TreeId in the SMB2
2413 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2416 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2417 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2419 tcon->treeName, utf16_path);
2423 req->NameLength = cpu_to_le16(name_len * 2);
2424 uni_path_len = copy_size;
2425 /* free before overwriting resource */
2427 utf16_path = copy_path;
2429 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2430 /* MUST set path len (NameLength) to 0 opening root of share */
2431 req->NameLength = cpu_to_le16(uni_path_len - 2);
2432 if (uni_path_len % 8 != 0) {
2433 copy_size = roundup(uni_path_len, 8);
2434 copy_path = kzalloc(copy_size, GFP_KERNEL);
2439 memcpy((char *)copy_path, (const char *)utf16_path,
2441 uni_path_len = copy_size;
2442 /* free before overwriting resource */
2444 utf16_path = copy_path;
2448 iov[1].iov_len = uni_path_len;
2449 iov[1].iov_base = utf16_path;
2450 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2452 if (tcon->posix_extensions) {
2453 /* resource #3: posix buf */
2454 rc = add_posix_context(iov, &n_iov, mode);
2457 pc_buf = iov[n_iov-1].iov_base;
2461 memset(&rqst, 0, sizeof(struct smb_rqst));
2463 rqst.rq_nvec = n_iov;
2465 /* no need to inc num_remote_opens because we close it just below */
2466 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2467 FILE_WRITE_ATTRIBUTES);
2468 /* resource #4: response buffer */
2469 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2471 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2472 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2474 FILE_WRITE_ATTRIBUTES, rc);
2475 goto err_free_rsp_buf;
2478 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2479 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2480 ses->Suid, CREATE_NOT_FILE,
2481 FILE_WRITE_ATTRIBUTES);
2483 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2485 /* Eventually save off posix specific response info and timestaps */
2488 free_rsp_buf(resp_buftype, rsp);
2491 cifs_small_buf_release(req);
2498 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2499 struct cifs_open_parms *oparms, __le16 *path)
2501 struct TCP_Server_Info *server = tcon->ses->server;
2502 struct smb2_create_req *req;
2503 unsigned int n_iov = 2;
2504 __u32 file_attributes = 0;
2507 unsigned int total_len;
2508 struct kvec *iov = rqst->rq_iov;
2512 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2516 iov[0].iov_base = (char *)req;
2517 /* -1 since last byte is buf[0] which is sent below (path) */
2518 iov[0].iov_len = total_len - 1;
2520 if (oparms->create_options & CREATE_OPTION_READONLY)
2521 file_attributes |= ATTR_READONLY;
2522 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2523 file_attributes |= ATTR_SYSTEM;
2525 req->ImpersonationLevel = IL_IMPERSONATION;
2526 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2527 /* File attributes ignored on open (used in create though) */
2528 req->FileAttributes = cpu_to_le32(file_attributes);
2529 req->ShareAccess = FILE_SHARE_ALL_LE;
2531 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2532 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2533 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2535 /* [MS-SMB2] 2.2.13 NameOffset:
2536 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2537 * the SMB2 header, the file name includes a prefix that will
2538 * be processed during DFS name normalization as specified in
2539 * section 3.3.5.9. Otherwise, the file name is relative to
2540 * the share that is identified by the TreeId in the SMB2
2543 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2546 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2547 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2549 tcon->treeName, path);
2552 req->NameLength = cpu_to_le16(name_len * 2);
2553 uni_path_len = copy_size;
2556 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2557 /* MUST set path len (NameLength) to 0 opening root of share */
2558 req->NameLength = cpu_to_le16(uni_path_len - 2);
2559 copy_size = uni_path_len;
2560 if (copy_size % 8 != 0)
2561 copy_size = roundup(copy_size, 8);
2562 copy_path = kzalloc(copy_size, GFP_KERNEL);
2565 memcpy((char *)copy_path, (const char *)path,
2567 uni_path_len = copy_size;
2571 iov[1].iov_len = uni_path_len;
2572 iov[1].iov_base = path;
2574 if ((!server->oplocks) || (tcon->no_lease))
2575 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2577 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2578 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2579 req->RequestedOplockLevel = *oplock;
2580 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2581 (oparms->create_options & CREATE_NOT_FILE))
2582 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2584 rc = add_lease_context(server, iov, &n_iov,
2585 oparms->fid->lease_key, oplock);
2590 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2591 /* need to set Next field of lease context if we request it */
2592 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2593 struct create_context *ccontext =
2594 (struct create_context *)iov[n_iov-1].iov_base;
2596 cpu_to_le32(server->vals->create_lease_size);
2599 rc = add_durable_context(iov, &n_iov, oparms,
2600 tcon->use_persistent);
2605 if (tcon->posix_extensions) {
2607 struct create_context *ccontext =
2608 (struct create_context *)iov[n_iov-1].iov_base;
2610 cpu_to_le32(iov[n_iov-1].iov_len);
2613 rc = add_posix_context(iov, &n_iov, oparms->mode);
2618 if (tcon->snapshot_time) {
2619 cifs_dbg(FYI, "adding snapshot context\n");
2621 struct create_context *ccontext =
2622 (struct create_context *)iov[n_iov-1].iov_base;
2624 cpu_to_le32(iov[n_iov-1].iov_len);
2627 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2632 if ((oparms->disposition != FILE_OPEN) &&
2633 (oparms->cifs_sb) &&
2634 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2635 (oparms->mode != ACL_NO_MODE)) {
2637 struct create_context *ccontext =
2638 (struct create_context *)iov[n_iov-1].iov_base;
2640 cpu_to_le32(iov[n_iov-1].iov_len);
2643 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2644 rc = add_sd_context(iov, &n_iov, oparms->mode);
2650 struct create_context *ccontext =
2651 (struct create_context *)iov[n_iov-1].iov_base;
2652 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2654 add_query_id_context(iov, &n_iov);
2656 rqst->rq_nvec = n_iov;
2660 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2661 * All other vectors are freed by kfree().
2664 SMB2_open_free(struct smb_rqst *rqst)
2668 if (rqst && rqst->rq_iov) {
2669 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2670 for (i = 1; i < rqst->rq_nvec; i++)
2671 if (rqst->rq_iov[i].iov_base != smb2_padding)
2672 kfree(rqst->rq_iov[i].iov_base);
2677 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2678 __u8 *oplock, struct smb2_file_all_info *buf,
2679 struct kvec *err_iov, int *buftype)
2681 struct smb_rqst rqst;
2682 struct smb2_create_rsp *rsp = NULL;
2683 struct TCP_Server_Info *server;
2684 struct cifs_tcon *tcon = oparms->tcon;
2685 struct cifs_ses *ses = tcon->ses;
2686 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2687 struct kvec rsp_iov = {NULL, 0};
2688 int resp_buftype = CIFS_NO_BUFFER;
2692 cifs_dbg(FYI, "create/open\n");
2693 if (ses && (ses->server))
2694 server = ses->server;
2698 if (smb3_encryption_required(tcon))
2699 flags |= CIFS_TRANSFORM_REQ;
2701 memset(&rqst, 0, sizeof(struct smb_rqst));
2702 memset(&iov, 0, sizeof(iov));
2704 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2706 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2710 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2711 oparms->create_options, oparms->desired_access);
2713 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2715 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2718 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2719 if (err_iov && rsp) {
2721 *buftype = resp_buftype;
2722 resp_buftype = CIFS_NO_BUFFER;
2725 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2726 oparms->create_options, oparms->desired_access, rc);
2727 if (rc == -EREMCHG) {
2728 printk_once(KERN_WARNING "server share %s deleted\n",
2730 tcon->need_reconnect = true;
2734 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2735 ses->Suid, oparms->create_options,
2736 oparms->desired_access);
2738 atomic_inc(&tcon->num_remote_opens);
2739 oparms->fid->persistent_fid = rsp->PersistentFileId;
2740 oparms->fid->volatile_fid = rsp->VolatileFileId;
2741 #ifdef CONFIG_CIFS_DEBUG2
2742 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2743 #endif /* CIFS_DEBUG2 */
2746 memcpy(buf, &rsp->CreationTime, 32);
2747 buf->AllocationSize = rsp->AllocationSize;
2748 buf->EndOfFile = rsp->EndofFile;
2749 buf->Attributes = rsp->FileAttributes;
2750 buf->NumberOfLinks = cpu_to_le32(1);
2751 buf->DeletePending = 0;
2755 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2756 oparms->fid->lease_key, oplock, buf);
2758 SMB2_open_free(&rqst);
2759 free_rsp_buf(resp_buftype, rsp);
2764 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2765 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2766 bool is_fsctl, char *in_data, u32 indatalen,
2767 __u32 max_response_size)
2769 struct smb2_ioctl_req *req;
2770 struct kvec *iov = rqst->rq_iov;
2771 unsigned int total_len;
2775 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2781 * indatalen is usually small at a couple of bytes max, so
2782 * just allocate through generic pool
2784 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2786 cifs_small_buf_release(req);
2791 req->CtlCode = cpu_to_le32(opcode);
2792 req->PersistentFileId = persistent_fid;
2793 req->VolatileFileId = volatile_fid;
2795 iov[0].iov_base = (char *)req;
2797 * If no input data, the size of ioctl struct in
2798 * protocol spec still includes a 1 byte data buffer,
2799 * but if input data passed to ioctl, we do not
2800 * want to double count this, so we do not send
2801 * the dummy one byte of data in iovec[0] if sending
2802 * input data (in iovec[1]).
2805 req->InputCount = cpu_to_le32(indatalen);
2806 /* do not set InputOffset if no input data */
2808 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2810 iov[0].iov_len = total_len - 1;
2811 iov[1].iov_base = in_data_buf;
2812 iov[1].iov_len = indatalen;
2815 iov[0].iov_len = total_len;
2818 req->OutputOffset = 0;
2819 req->OutputCount = 0; /* MBZ */
2822 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2823 * We Could increase default MaxOutputResponse, but that could require
2824 * more credits. Windows typically sets this smaller, but for some
2825 * ioctls it may be useful to allow server to send more. No point
2826 * limiting what the server can send as long as fits in one credit
2827 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2828 * to increase this limit up in the future.
2829 * Note that for snapshot queries that servers like Azure expect that
2830 * the first query be minimal size (and just used to get the number/size
2831 * of previous versions) so response size must be specified as EXACTLY
2832 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2833 * of eight bytes. Currently that is the only case where we set max
2834 * response size smaller.
2836 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2839 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2843 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2844 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2845 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2851 SMB2_ioctl_free(struct smb_rqst *rqst)
2854 if (rqst && rqst->rq_iov) {
2855 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2856 for (i = 1; i < rqst->rq_nvec; i++)
2857 if (rqst->rq_iov[i].iov_base != smb2_padding)
2858 kfree(rqst->rq_iov[i].iov_base);
2864 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2867 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2868 u64 volatile_fid, u32 opcode, bool is_fsctl,
2869 char *in_data, u32 indatalen, u32 max_out_data_len,
2870 char **out_data, u32 *plen /* returned data len */)
2872 struct smb_rqst rqst;
2873 struct smb2_ioctl_rsp *rsp = NULL;
2874 struct cifs_ses *ses;
2875 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2876 struct kvec rsp_iov = {NULL, 0};
2877 int resp_buftype = CIFS_NO_BUFFER;
2880 struct TCP_Server_Info *server;
2882 cifs_dbg(FYI, "SMB2 IOCTL\n");
2884 if (out_data != NULL)
2887 /* zero out returned data len, in case of error */
2898 server = ses->server;
2902 if (smb3_encryption_required(tcon))
2903 flags |= CIFS_TRANSFORM_REQ;
2905 memset(&rqst, 0, sizeof(struct smb_rqst));
2906 memset(&iov, 0, sizeof(iov));
2908 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2910 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2911 is_fsctl, in_data, indatalen, max_out_data_len);
2915 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2917 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2920 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2921 ses->Suid, 0, opcode, rc);
2923 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2924 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2926 } else if (rc == -EINVAL) {
2927 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2928 (opcode != FSCTL_SRV_COPYCHUNK)) {
2929 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2932 } else if (rc == -E2BIG) {
2933 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2934 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2939 /* check if caller wants to look at return data or just return rc */
2940 if ((plen == NULL) || (out_data == NULL))
2943 *plen = le32_to_cpu(rsp->OutputCount);
2945 /* We check for obvious errors in the output buffer length and offset */
2947 goto ioctl_exit; /* server returned no data */
2948 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2949 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2955 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2956 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2957 le32_to_cpu(rsp->OutputOffset));
2963 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2965 if (*out_data == NULL) {
2971 SMB2_ioctl_free(&rqst);
2972 free_rsp_buf(resp_buftype, rsp);
2977 * Individual callers to ioctl worker function follow
2981 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2982 u64 persistent_fid, u64 volatile_fid)
2985 struct compress_ioctl fsctl_input;
2986 char *ret_data = NULL;
2988 fsctl_input.CompressionState =
2989 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2991 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2992 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2993 (char *)&fsctl_input /* data input */,
2994 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2995 &ret_data /* out data */, NULL);
2997 cifs_dbg(FYI, "set compression rc %d\n", rc);
3003 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3004 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3006 struct smb2_close_req *req;
3007 struct kvec *iov = rqst->rq_iov;
3008 unsigned int total_len;
3011 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3015 req->PersistentFileId = persistent_fid;
3016 req->VolatileFileId = volatile_fid;
3018 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3021 iov[0].iov_base = (char *)req;
3022 iov[0].iov_len = total_len;
3028 SMB2_close_free(struct smb_rqst *rqst)
3030 if (rqst && rqst->rq_iov)
3031 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3035 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3036 u64 persistent_fid, u64 volatile_fid,
3037 struct smb2_file_network_open_info *pbuf)
3039 struct smb_rqst rqst;
3040 struct smb2_close_rsp *rsp = NULL;
3041 struct cifs_ses *ses = tcon->ses;
3043 struct kvec rsp_iov;
3044 int resp_buftype = CIFS_NO_BUFFER;
3047 bool query_attrs = false;
3049 cifs_dbg(FYI, "Close\n");
3051 if (!ses || !(ses->server))
3054 if (smb3_encryption_required(tcon))
3055 flags |= CIFS_TRANSFORM_REQ;
3057 memset(&rqst, 0, sizeof(struct smb_rqst));
3058 memset(&iov, 0, sizeof(iov));
3062 /* check if need to ask server to return timestamps in close response */
3066 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3067 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3072 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3073 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3076 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3077 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3081 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3084 * Note that have to subtract 4 since struct network_open_info
3085 * has a final 4 byte pad that close response does not have
3088 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3091 atomic_dec(&tcon->num_remote_opens);
3093 SMB2_close_free(&rqst);
3094 free_rsp_buf(resp_buftype, rsp);
3096 /* retry close in a worker thread if this one is interrupted */
3100 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3103 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3104 persistent_fid, tmp_rc);
3110 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3111 u64 persistent_fid, u64 volatile_fid)
3113 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3117 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3118 struct kvec *iov, unsigned int min_buf_size)
3120 unsigned int smb_len = iov->iov_len;
3121 char *end_of_smb = smb_len + (char *)iov->iov_base;
3122 char *begin_of_buf = offset + (char *)iov->iov_base;
3123 char *end_of_buf = begin_of_buf + buffer_length;
3126 if (buffer_length < min_buf_size) {
3127 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3128 buffer_length, min_buf_size);
3132 /* check if beyond RFC1001 maximum length */
3133 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3134 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3135 buffer_length, smb_len);
3139 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3140 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3148 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3149 * Caller must free buffer.
3152 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3153 struct kvec *iov, unsigned int minbufsize,
3156 char *begin_of_buf = offset + (char *)iov->iov_base;
3162 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3166 memcpy(data, begin_of_buf, buffer_length);
3172 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3173 u64 persistent_fid, u64 volatile_fid,
3174 u8 info_class, u8 info_type, u32 additional_info,
3175 size_t output_len, size_t input_len, void *input)
3177 struct smb2_query_info_req *req;
3178 struct kvec *iov = rqst->rq_iov;
3179 unsigned int total_len;
3182 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3187 req->InfoType = info_type;
3188 req->FileInfoClass = info_class;
3189 req->PersistentFileId = persistent_fid;
3190 req->VolatileFileId = volatile_fid;
3191 req->AdditionalInformation = cpu_to_le32(additional_info);
3193 req->OutputBufferLength = cpu_to_le32(output_len);
3195 req->InputBufferLength = cpu_to_le32(input_len);
3196 /* total_len for smb query request never close to le16 max */
3197 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3198 memcpy(req->Buffer, input, input_len);
3201 iov[0].iov_base = (char *)req;
3203 iov[0].iov_len = total_len - 1 + input_len;
3208 SMB2_query_info_free(struct smb_rqst *rqst)
3210 if (rqst && rqst->rq_iov)
3211 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3215 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3216 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3217 u32 additional_info, size_t output_len, size_t min_len, void **data,
3220 struct smb_rqst rqst;
3221 struct smb2_query_info_rsp *rsp = NULL;
3223 struct kvec rsp_iov;
3225 int resp_buftype = CIFS_NO_BUFFER;
3226 struct cifs_ses *ses = tcon->ses;
3227 struct TCP_Server_Info *server;
3229 bool allocated = false;
3231 cifs_dbg(FYI, "Query Info\n");
3235 server = ses->server;
3239 if (smb3_encryption_required(tcon))
3240 flags |= CIFS_TRANSFORM_REQ;
3242 memset(&rqst, 0, sizeof(struct smb_rqst));
3243 memset(&iov, 0, sizeof(iov));
3247 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3248 info_class, info_type, additional_info,
3249 output_len, 0, NULL);
3253 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3254 ses->Suid, info_class, (__u32)info_type);
3256 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3257 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3260 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3261 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3262 ses->Suid, info_class, (__u32)info_type, rc);
3266 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3267 ses->Suid, info_class, (__u32)info_type);
3270 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3272 *data = kmalloc(*dlen, GFP_KERNEL);
3275 "Error %d allocating memory for acl\n",
3285 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3286 le32_to_cpu(rsp->OutputBufferLength),
3287 &rsp_iov, min_len, *data);
3288 if (rc && allocated) {
3295 SMB2_query_info_free(&rqst);
3296 free_rsp_buf(resp_buftype, rsp);
3300 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3301 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3303 return query_info(xid, tcon, persistent_fid, volatile_fid,
3304 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3305 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3306 sizeof(struct smb2_file_all_info), (void **)&data,
3311 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3312 u64 persistent_fid, u64 volatile_fid,
3313 void **data, u32 *plen)
3315 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3318 return query_info(xid, tcon, persistent_fid, volatile_fid,
3319 0, SMB2_O_INFO_SECURITY, additional_info,
3320 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3324 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3325 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3327 return query_info(xid, tcon, persistent_fid, volatile_fid,
3328 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3329 sizeof(struct smb2_file_internal_info),
3330 sizeof(struct smb2_file_internal_info),
3331 (void **)&uniqueid, NULL);
3335 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3336 * See MS-SMB2 2.2.35 and 2.2.36
3340 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3341 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3342 u32 completion_filter, bool watch_tree)
3344 struct smb2_change_notify_req *req;
3345 struct kvec *iov = rqst->rq_iov;
3346 unsigned int total_len;
3349 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3353 req->PersistentFileId = persistent_fid;
3354 req->VolatileFileId = volatile_fid;
3355 req->OutputBufferLength =
3356 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3357 req->CompletionFilter = cpu_to_le32(completion_filter);
3359 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3363 iov[0].iov_base = (char *)req;
3364 iov[0].iov_len = total_len;
3370 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3371 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3372 u32 completion_filter)
3374 struct cifs_ses *ses = tcon->ses;
3375 struct smb_rqst rqst;
3377 struct kvec rsp_iov = {NULL, 0};
3378 int resp_buftype = CIFS_NO_BUFFER;
3382 cifs_dbg(FYI, "change notify\n");
3383 if (!ses || !(ses->server))
3386 if (smb3_encryption_required(tcon))
3387 flags |= CIFS_TRANSFORM_REQ;
3389 memset(&rqst, 0, sizeof(struct smb_rqst));
3390 memset(&iov, 0, sizeof(iov));
3394 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3395 completion_filter, watch_tree);
3399 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3400 (u8)watch_tree, completion_filter);
3401 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3404 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3405 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3406 (u8)watch_tree, completion_filter, rc);
3408 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3409 ses->Suid, (u8)watch_tree, completion_filter);
3413 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3414 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3421 * This is a no-op for now. We're not really interested in the reply, but
3422 * rather in the fact that the server sent one and that server->lstrp
3425 * FIXME: maybe we should consider checking that the reply matches request?
3428 smb2_echo_callback(struct mid_q_entry *mid)
3430 struct TCP_Server_Info *server = mid->callback_data;
3431 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3432 struct cifs_credits credits = { .value = 0, .instance = 0 };
3434 if (mid->mid_state == MID_RESPONSE_RECEIVED
3435 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3436 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3437 credits.instance = server->reconnect_instance;
3440 DeleteMidQEntry(mid);
3441 add_credits(server, &credits, CIFS_ECHO_OP);
3444 void smb2_reconnect_server(struct work_struct *work)
3446 struct TCP_Server_Info *server = container_of(work,
3447 struct TCP_Server_Info, reconnect.work);
3448 struct cifs_ses *ses;
3449 struct cifs_tcon *tcon, *tcon2;
3450 struct list_head tmp_list;
3451 int tcon_exist = false;
3453 int resched = false;
3456 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3457 mutex_lock(&server->reconnect_mutex);
3459 INIT_LIST_HEAD(&tmp_list);
3460 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3462 spin_lock(&cifs_tcp_ses_lock);
3463 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3464 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3465 if (tcon->need_reconnect || tcon->need_reopen_files) {
3467 list_add_tail(&tcon->rlist, &tmp_list);
3472 * IPC has the same lifetime as its session and uses its
3475 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3476 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3482 * Get the reference to server struct to be sure that the last call of
3483 * cifs_put_tcon() in the loop below won't release the server pointer.
3486 server->srv_count++;
3488 spin_unlock(&cifs_tcp_ses_lock);
3490 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3491 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3493 cifs_reopen_persistent_handles(tcon);
3496 list_del_init(&tcon->rlist);
3498 cifs_put_smb_ses(tcon->ses);
3500 cifs_put_tcon(tcon);
3503 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3505 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3506 mutex_unlock(&server->reconnect_mutex);
3508 /* now we can safely release srv struct */
3510 cifs_put_tcp_session(server, 1);
3514 SMB2_echo(struct TCP_Server_Info *server)
3516 struct smb2_echo_req *req;
3519 struct smb_rqst rqst = { .rq_iov = iov,
3521 unsigned int total_len;
3523 cifs_dbg(FYI, "In echo request\n");
3525 if (server->tcpStatus == CifsNeedNegotiate) {
3526 /* No need to send echo on newly established connections */
3527 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3531 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3535 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3537 iov[0].iov_len = total_len;
3538 iov[0].iov_base = (char *)req;
3540 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3541 server, CIFS_ECHO_OP, NULL);
3543 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3545 cifs_small_buf_release(req);
3550 SMB2_flush_free(struct smb_rqst *rqst)
3552 if (rqst && rqst->rq_iov)
3553 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3557 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3558 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3560 struct smb2_flush_req *req;
3561 struct kvec *iov = rqst->rq_iov;
3562 unsigned int total_len;
3565 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3569 req->PersistentFileId = persistent_fid;
3570 req->VolatileFileId = volatile_fid;
3572 iov[0].iov_base = (char *)req;
3573 iov[0].iov_len = total_len;
3579 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3582 struct cifs_ses *ses = tcon->ses;
3583 struct smb_rqst rqst;
3585 struct kvec rsp_iov = {NULL, 0};
3586 int resp_buftype = CIFS_NO_BUFFER;
3590 cifs_dbg(FYI, "flush\n");
3591 if (!ses || !(ses->server))
3594 if (smb3_encryption_required(tcon))
3595 flags |= CIFS_TRANSFORM_REQ;
3597 memset(&rqst, 0, sizeof(struct smb_rqst));
3598 memset(&iov, 0, sizeof(iov));
3602 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3606 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3607 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3610 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3611 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3614 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3618 SMB2_flush_free(&rqst);
3619 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3624 * To form a chain of read requests, any read requests after the first should
3625 * have the end_of_chain boolean set to true.
3628 smb2_new_read_req(void **buf, unsigned int *total_len,
3629 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3630 unsigned int remaining_bytes, int request_type)
3633 struct smb2_read_plain_req *req = NULL;
3634 struct smb2_sync_hdr *shdr;
3635 struct TCP_Server_Info *server;
3637 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3642 server = io_parms->tcon->ses->server;
3644 return -ECONNABORTED;
3646 shdr = &req->sync_hdr;
3647 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3649 req->PersistentFileId = io_parms->persistent_fid;
3650 req->VolatileFileId = io_parms->volatile_fid;
3651 req->ReadChannelInfoOffset = 0; /* reserved */
3652 req->ReadChannelInfoLength = 0; /* reserved */
3653 req->Channel = 0; /* reserved */
3654 req->MinimumCount = 0;
3655 req->Length = cpu_to_le32(io_parms->length);
3656 req->Offset = cpu_to_le64(io_parms->offset);
3658 trace_smb3_read_enter(0 /* xid */,
3659 io_parms->persistent_fid,
3660 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3661 io_parms->offset, io_parms->length);
3662 #ifdef CONFIG_CIFS_SMB_DIRECT
3664 * If we want to do a RDMA write, fill in and append
3665 * smbd_buffer_descriptor_v1 to the end of read request
3667 if (server->rdma && rdata && !server->sign &&
3668 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3670 struct smbd_buffer_descriptor_v1 *v1;
3671 bool need_invalidate =
3672 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3674 rdata->mr = smbd_register_mr(
3675 server->smbd_conn, rdata->pages,
3676 rdata->nr_pages, rdata->page_offset,
3677 rdata->tailsz, true, need_invalidate);
3681 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3682 if (need_invalidate)
3683 req->Channel = SMB2_CHANNEL_RDMA_V1;
3684 req->ReadChannelInfoOffset =
3685 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3686 req->ReadChannelInfoLength =
3687 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3688 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3689 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3690 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3691 v1->length = cpu_to_le32(rdata->mr->mr->length);
3693 *total_len += sizeof(*v1) - 1;
3696 if (request_type & CHAINED_REQUEST) {
3697 if (!(request_type & END_OF_CHAIN)) {
3698 /* next 8-byte aligned request */
3699 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3700 shdr->NextCommand = cpu_to_le32(*total_len);
3701 } else /* END_OF_CHAIN */
3702 shdr->NextCommand = 0;
3703 if (request_type & RELATED_REQUEST) {
3704 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3706 * Related requests use info from previous read request
3709 shdr->SessionId = 0xFFFFFFFF;
3710 shdr->TreeId = 0xFFFFFFFF;
3711 req->PersistentFileId = 0xFFFFFFFF;
3712 req->VolatileFileId = 0xFFFFFFFF;
3715 if (remaining_bytes > io_parms->length)
3716 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3718 req->RemainingBytes = 0;
3725 smb2_readv_callback(struct mid_q_entry *mid)
3727 struct cifs_readdata *rdata = mid->callback_data;
3728 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3729 struct TCP_Server_Info *server = tcon->ses->server;
3730 struct smb2_sync_hdr *shdr =
3731 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3732 struct cifs_credits credits = { .value = 0, .instance = 0 };
3733 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3735 .rq_pages = rdata->pages,
3736 .rq_offset = rdata->page_offset,
3737 .rq_npages = rdata->nr_pages,
3738 .rq_pagesz = rdata->pagesz,
3739 .rq_tailsz = rdata->tailsz };
3741 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3742 __func__, mid->mid, mid->mid_state, rdata->result,
3745 switch (mid->mid_state) {
3746 case MID_RESPONSE_RECEIVED:
3747 credits.value = le16_to_cpu(shdr->CreditRequest);
3748 credits.instance = server->reconnect_instance;
3749 /* result already set, check signature */
3750 if (server->sign && !mid->decrypted) {
3753 rc = smb2_verify_signature(&rqst, server);
3755 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3758 /* FIXME: should this be counted toward the initiating task? */
3759 task_io_account_read(rdata->got_bytes);
3760 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3762 case MID_REQUEST_SUBMITTED:
3763 case MID_RETRY_NEEDED:
3764 rdata->result = -EAGAIN;
3765 if (server->sign && rdata->got_bytes)
3766 /* reset bytes number since we can not check a sign */
3767 rdata->got_bytes = 0;
3768 /* FIXME: should this be counted toward the initiating task? */
3769 task_io_account_read(rdata->got_bytes);
3770 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3772 case MID_RESPONSE_MALFORMED:
3773 credits.value = le16_to_cpu(shdr->CreditRequest);
3774 credits.instance = server->reconnect_instance;
3777 rdata->result = -EIO;
3779 #ifdef CONFIG_CIFS_SMB_DIRECT
3781 * If this rdata has a memmory registered, the MR can be freed
3782 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3783 * because they have limited number and are used for future I/Os
3786 smbd_deregister_mr(rdata->mr);
3790 if (rdata->result && rdata->result != -ENODATA) {
3791 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3792 trace_smb3_read_err(0 /* xid */,
3793 rdata->cfile->fid.persistent_fid,
3794 tcon->tid, tcon->ses->Suid, rdata->offset,
3795 rdata->bytes, rdata->result);
3797 trace_smb3_read_done(0 /* xid */,
3798 rdata->cfile->fid.persistent_fid,
3799 tcon->tid, tcon->ses->Suid,
3800 rdata->offset, rdata->got_bytes);
3802 queue_work(cifsiod_wq, &rdata->work);
3803 DeleteMidQEntry(mid);
3804 add_credits(server, &credits, 0);
3807 /* smb2_async_readv - send an async read, and set up mid to handle result */
3809 smb2_async_readv(struct cifs_readdata *rdata)
3813 struct smb2_sync_hdr *shdr;
3814 struct cifs_io_parms io_parms;
3815 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3817 struct TCP_Server_Info *server;
3818 unsigned int total_len;
3820 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3821 __func__, rdata->offset, rdata->bytes);
3823 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3824 io_parms.offset = rdata->offset;
3825 io_parms.length = rdata->bytes;
3826 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3827 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3828 io_parms.pid = rdata->pid;
3830 server = io_parms.tcon->ses->server;
3832 rc = smb2_new_read_req(
3833 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3837 if (smb3_encryption_required(io_parms.tcon))
3838 flags |= CIFS_TRANSFORM_REQ;
3840 rdata->iov[0].iov_base = buf;
3841 rdata->iov[0].iov_len = total_len;
3843 shdr = (struct smb2_sync_hdr *)buf;
3845 if (rdata->credits.value > 0) {
3846 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3847 SMB2_MAX_BUFFER_SIZE));
3848 shdr->CreditRequest =
3849 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3851 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3853 goto async_readv_out;
3855 flags |= CIFS_HAS_CREDITS;
3858 kref_get(&rdata->refcount);
3859 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3860 cifs_readv_receive, smb2_readv_callback,
3861 smb3_handle_read_data, rdata, flags,
3864 kref_put(&rdata->refcount, cifs_readdata_release);
3865 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3866 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3868 io_parms.tcon->ses->Suid,
3869 io_parms.offset, io_parms.length, rc);
3873 cifs_small_buf_release(buf);
3878 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3879 unsigned int *nbytes, char **buf, int *buf_type)
3881 struct smb_rqst rqst;
3882 int resp_buftype, rc;
3883 struct smb2_read_plain_req *req = NULL;
3884 struct smb2_read_rsp *rsp = NULL;
3886 struct kvec rsp_iov;
3887 unsigned int total_len;
3888 int flags = CIFS_LOG_ERROR;
3889 struct cifs_ses *ses = io_parms->tcon->ses;
3892 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3896 if (smb3_encryption_required(io_parms->tcon))
3897 flags |= CIFS_TRANSFORM_REQ;
3899 iov[0].iov_base = (char *)req;
3900 iov[0].iov_len = total_len;
3902 memset(&rqst, 0, sizeof(struct smb_rqst));
3906 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3907 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3910 if (rc != -ENODATA) {
3911 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3912 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3913 trace_smb3_read_err(xid, req->PersistentFileId,
3914 io_parms->tcon->tid, ses->Suid,
3915 io_parms->offset, io_parms->length,
3918 trace_smb3_read_done(xid, req->PersistentFileId,
3919 io_parms->tcon->tid, ses->Suid,
3920 io_parms->offset, 0);
3921 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3922 cifs_small_buf_release(req);
3923 return rc == -ENODATA ? 0 : rc;
3925 trace_smb3_read_done(xid, req->PersistentFileId,
3926 io_parms->tcon->tid, ses->Suid,
3927 io_parms->offset, io_parms->length);
3929 cifs_small_buf_release(req);
3931 *nbytes = le32_to_cpu(rsp->DataLength);
3932 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3933 (*nbytes > io_parms->length)) {
3934 cifs_dbg(FYI, "bad length %d for count %d\n",
3935 *nbytes, io_parms->length);
3941 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3942 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3943 } else if (resp_buftype != CIFS_NO_BUFFER) {
3944 *buf = rsp_iov.iov_base;
3945 if (resp_buftype == CIFS_SMALL_BUFFER)
3946 *buf_type = CIFS_SMALL_BUFFER;
3947 else if (resp_buftype == CIFS_LARGE_BUFFER)
3948 *buf_type = CIFS_LARGE_BUFFER;
3954 * Check the mid_state and signature on received buffer (if any), and queue the
3955 * workqueue completion task.
3958 smb2_writev_callback(struct mid_q_entry *mid)
3960 struct cifs_writedata *wdata = mid->callback_data;
3961 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3962 struct TCP_Server_Info *server = tcon->ses->server;
3963 unsigned int written;
3964 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3965 struct cifs_credits credits = { .value = 0, .instance = 0 };
3967 switch (mid->mid_state) {
3968 case MID_RESPONSE_RECEIVED:
3969 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3970 credits.instance = server->reconnect_instance;
3971 wdata->result = smb2_check_receive(mid, server, 0);
3972 if (wdata->result != 0)
3975 written = le32_to_cpu(rsp->DataLength);
3977 * Mask off high 16 bits when bytes written as returned
3978 * by the server is greater than bytes requested by the
3979 * client. OS/2 servers are known to set incorrect
3982 if (written > wdata->bytes)
3985 if (written < wdata->bytes)
3986 wdata->result = -ENOSPC;
3988 wdata->bytes = written;
3990 case MID_REQUEST_SUBMITTED:
3991 case MID_RETRY_NEEDED:
3992 wdata->result = -EAGAIN;
3994 case MID_RESPONSE_MALFORMED:
3995 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3996 credits.instance = server->reconnect_instance;
3999 wdata->result = -EIO;
4002 #ifdef CONFIG_CIFS_SMB_DIRECT
4004 * If this wdata has a memory registered, the MR can be freed
4005 * The number of MRs available is limited, it's important to recover
4006 * used MR as soon as I/O is finished. Hold MR longer in the later
4007 * I/O process can possibly result in I/O deadlock due to lack of MR
4008 * to send request on I/O retry
4011 smbd_deregister_mr(wdata->mr);
4015 if (wdata->result) {
4016 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4017 trace_smb3_write_err(0 /* no xid */,
4018 wdata->cfile->fid.persistent_fid,
4019 tcon->tid, tcon->ses->Suid, wdata->offset,
4020 wdata->bytes, wdata->result);
4022 trace_smb3_write_done(0 /* no xid */,
4023 wdata->cfile->fid.persistent_fid,
4024 tcon->tid, tcon->ses->Suid,
4025 wdata->offset, wdata->bytes);
4027 queue_work(cifsiod_wq, &wdata->work);
4028 DeleteMidQEntry(mid);
4029 add_credits(server, &credits, 0);
4032 /* smb2_async_writev - send an async write, and set up mid to handle result */
4034 smb2_async_writev(struct cifs_writedata *wdata,
4035 void (*release)(struct kref *kref))
4037 int rc = -EACCES, flags = 0;
4038 struct smb2_write_req *req = NULL;
4039 struct smb2_sync_hdr *shdr;
4040 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4041 struct TCP_Server_Info *server = tcon->ses->server;
4043 struct smb_rqst rqst = { };
4044 unsigned int total_len;
4046 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
4050 if (smb3_encryption_required(tcon))
4051 flags |= CIFS_TRANSFORM_REQ;
4053 shdr = (struct smb2_sync_hdr *)req;
4054 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4056 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4057 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4058 req->WriteChannelInfoOffset = 0;
4059 req->WriteChannelInfoLength = 0;
4061 req->Offset = cpu_to_le64(wdata->offset);
4062 req->DataOffset = cpu_to_le16(
4063 offsetof(struct smb2_write_req, Buffer));
4064 req->RemainingBytes = 0;
4066 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4067 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4068 #ifdef CONFIG_CIFS_SMB_DIRECT
4070 * If we want to do a server RDMA read, fill in and append
4071 * smbd_buffer_descriptor_v1 to the end of write request
4073 if (server->rdma && !server->sign && wdata->bytes >=
4074 server->smbd_conn->rdma_readwrite_threshold) {
4076 struct smbd_buffer_descriptor_v1 *v1;
4077 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4079 wdata->mr = smbd_register_mr(
4080 server->smbd_conn, wdata->pages,
4081 wdata->nr_pages, wdata->page_offset,
4082 wdata->tailsz, false, need_invalidate);
4085 goto async_writev_out;
4088 req->DataOffset = 0;
4089 if (wdata->nr_pages > 1)
4090 req->RemainingBytes =
4092 (wdata->nr_pages - 1) * wdata->pagesz -
4093 wdata->page_offset + wdata->tailsz
4096 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4097 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4098 if (need_invalidate)
4099 req->Channel = SMB2_CHANNEL_RDMA_V1;
4100 req->WriteChannelInfoOffset =
4101 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4102 req->WriteChannelInfoLength =
4103 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4104 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4105 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4106 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4107 v1->length = cpu_to_le32(wdata->mr->mr->length);
4110 iov[0].iov_len = total_len - 1;
4111 iov[0].iov_base = (char *)req;
4115 rqst.rq_pages = wdata->pages;
4116 rqst.rq_offset = wdata->page_offset;
4117 rqst.rq_npages = wdata->nr_pages;
4118 rqst.rq_pagesz = wdata->pagesz;
4119 rqst.rq_tailsz = wdata->tailsz;
4120 #ifdef CONFIG_CIFS_SMB_DIRECT
4122 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4126 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4127 wdata->offset, wdata->bytes);
4129 #ifdef CONFIG_CIFS_SMB_DIRECT
4130 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4132 req->Length = cpu_to_le32(wdata->bytes);
4134 req->Length = cpu_to_le32(wdata->bytes);
4137 if (wdata->credits.value > 0) {
4138 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4139 SMB2_MAX_BUFFER_SIZE));
4140 shdr->CreditRequest =
4141 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4143 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4145 goto async_writev_out;
4147 flags |= CIFS_HAS_CREDITS;
4150 kref_get(&wdata->refcount);
4151 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4152 wdata, flags, &wdata->credits);
4155 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4156 tcon->tid, tcon->ses->Suid, wdata->offset,
4158 kref_put(&wdata->refcount, release);
4159 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4163 cifs_small_buf_release(req);
4168 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4169 * The length field from io_parms must be at least 1 and indicates a number of
4170 * elements with data to write that begins with position 1 in iov array. All
4171 * data length is specified by count.
4174 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4175 unsigned int *nbytes, struct kvec *iov, int n_vec)
4177 struct smb_rqst rqst;
4179 struct smb2_write_req *req = NULL;
4180 struct smb2_write_rsp *rsp = NULL;
4182 struct kvec rsp_iov;
4184 unsigned int total_len;
4191 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4196 if (io_parms->tcon->ses->server == NULL)
4197 return -ECONNABORTED;
4199 if (smb3_encryption_required(io_parms->tcon))
4200 flags |= CIFS_TRANSFORM_REQ;
4202 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4204 req->PersistentFileId = io_parms->persistent_fid;
4205 req->VolatileFileId = io_parms->volatile_fid;
4206 req->WriteChannelInfoOffset = 0;
4207 req->WriteChannelInfoLength = 0;
4209 req->Length = cpu_to_le32(io_parms->length);
4210 req->Offset = cpu_to_le64(io_parms->offset);
4211 req->DataOffset = cpu_to_le16(
4212 offsetof(struct smb2_write_req, Buffer));
4213 req->RemainingBytes = 0;
4215 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4216 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4217 io_parms->offset, io_parms->length);
4219 iov[0].iov_base = (char *)req;
4221 iov[0].iov_len = total_len - 1;
4223 memset(&rqst, 0, sizeof(struct smb_rqst));
4225 rqst.rq_nvec = n_vec + 1;
4227 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4228 &resp_buftype, flags, &rsp_iov);
4229 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4232 trace_smb3_write_err(xid, req->PersistentFileId,
4233 io_parms->tcon->tid,
4234 io_parms->tcon->ses->Suid,
4235 io_parms->offset, io_parms->length, rc);
4236 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4237 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4239 *nbytes = le32_to_cpu(rsp->DataLength);
4240 trace_smb3_write_done(xid, req->PersistentFileId,
4241 io_parms->tcon->tid,
4242 io_parms->tcon->ses->Suid,
4243 io_parms->offset, *nbytes);
4246 cifs_small_buf_release(req);
4247 free_rsp_buf(resp_buftype, rsp);
4252 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4255 unsigned int entrycount = 0;
4256 unsigned int next_offset = 0;
4258 FILE_DIRECTORY_INFO *dir_info;
4260 if (bufstart == NULL)
4263 entryptr = bufstart;
4266 if (entryptr + next_offset < entryptr ||
4267 entryptr + next_offset > end_of_buf ||
4268 entryptr + next_offset + size > end_of_buf) {
4269 cifs_dbg(VFS, "malformed search entry would overflow\n");
4273 entryptr = entryptr + next_offset;
4274 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4276 len = le32_to_cpu(dir_info->FileNameLength);
4277 if (entryptr + len < entryptr ||
4278 entryptr + len > end_of_buf ||
4279 entryptr + len + size > end_of_buf) {
4280 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4285 *lastentry = entryptr;
4288 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4300 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4301 u64 persistent_fid, u64 volatile_fid, int index,
4302 struct cifs_search_info *srch_inf)
4304 struct smb_rqst rqst;
4305 struct smb2_query_directory_req *req;
4306 struct smb2_query_directory_rsp *rsp = NULL;
4308 struct kvec rsp_iov;
4311 int resp_buftype = CIFS_NO_BUFFER;
4312 unsigned char *bufptr;
4313 struct TCP_Server_Info *server;
4314 struct cifs_ses *ses = tcon->ses;
4315 __le16 asteriks = cpu_to_le16('*');
4317 unsigned int output_size = CIFSMaxBufSize;
4318 size_t info_buf_size;
4320 unsigned int total_len;
4322 if (ses && (ses->server))
4323 server = ses->server;
4327 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4332 if (smb3_encryption_required(tcon))
4333 flags |= CIFS_TRANSFORM_REQ;
4335 switch (srch_inf->info_level) {
4336 case SMB_FIND_FILE_DIRECTORY_INFO:
4337 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4338 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4340 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4341 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4342 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4345 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4346 srch_inf->info_level);
4351 req->FileIndex = cpu_to_le32(index);
4352 req->PersistentFileId = persistent_fid;
4353 req->VolatileFileId = volatile_fid;
4356 bufptr = req->Buffer;
4357 memcpy(bufptr, &asteriks, len);
4359 req->FileNameOffset =
4360 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4361 req->FileNameLength = cpu_to_le16(len);
4363 * BB could be 30 bytes or so longer if we used SMB2 specific
4364 * buffer lengths, but this is safe and close enough.
4366 output_size = min_t(unsigned int, output_size, server->maxBuf);
4367 output_size = min_t(unsigned int, output_size, 2 << 15);
4368 req->OutputBufferLength = cpu_to_le32(output_size);
4370 iov[0].iov_base = (char *)req;
4372 iov[0].iov_len = total_len - 1;
4374 iov[1].iov_base = (char *)(req->Buffer);
4375 iov[1].iov_len = len;
4377 memset(&rqst, 0, sizeof(struct smb_rqst));
4381 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4382 tcon->ses->Suid, index, output_size);
4384 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4385 cifs_small_buf_release(req);
4386 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4389 if (rc == -ENODATA &&
4390 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4391 trace_smb3_query_dir_done(xid, persistent_fid,
4392 tcon->tid, tcon->ses->Suid, index, 0);
4393 srch_inf->endOfSearch = true;
4396 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4397 tcon->ses->Suid, index, 0, rc);
4398 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4403 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4404 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4407 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4408 tcon->ses->Suid, index, 0, rc);
4412 srch_inf->unicode = true;
4414 if (srch_inf->ntwrk_buf_start) {
4415 if (srch_inf->smallBuf)
4416 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4418 cifs_buf_release(srch_inf->ntwrk_buf_start);
4420 srch_inf->ntwrk_buf_start = (char *)rsp;
4421 srch_inf->srch_entries_start = srch_inf->last_entry =
4422 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4423 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4424 srch_inf->entries_in_buffer =
4425 num_entries(srch_inf->srch_entries_start, end_of_smb,
4426 &srch_inf->last_entry, info_buf_size);
4427 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4428 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4429 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4430 srch_inf->srch_entries_start, srch_inf->last_entry);
4431 if (resp_buftype == CIFS_LARGE_BUFFER)
4432 srch_inf->smallBuf = false;
4433 else if (resp_buftype == CIFS_SMALL_BUFFER)
4434 srch_inf->smallBuf = true;
4436 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4438 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4439 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4443 free_rsp_buf(resp_buftype, rsp);
4448 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4449 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4450 u8 info_type, u32 additional_info,
4451 void **data, unsigned int *size)
4453 struct smb2_set_info_req *req;
4454 struct kvec *iov = rqst->rq_iov;
4455 unsigned int i, total_len;
4458 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4462 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4463 req->InfoType = info_type;
4464 req->FileInfoClass = info_class;
4465 req->PersistentFileId = persistent_fid;
4466 req->VolatileFileId = volatile_fid;
4467 req->AdditionalInformation = cpu_to_le32(additional_info);
4470 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4471 req->BufferLength = cpu_to_le32(*size);
4473 memcpy(req->Buffer, *data, *size);
4476 iov[0].iov_base = (char *)req;
4478 iov[0].iov_len = total_len - 1;
4480 for (i = 1; i < rqst->rq_nvec; i++) {
4481 le32_add_cpu(&req->BufferLength, size[i]);
4482 iov[i].iov_base = (char *)data[i];
4483 iov[i].iov_len = size[i];
4490 SMB2_set_info_free(struct smb_rqst *rqst)
4492 if (rqst && rqst->rq_iov)
4493 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4497 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4498 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4499 u8 info_type, u32 additional_info, unsigned int num,
4500 void **data, unsigned int *size)
4502 struct smb_rqst rqst;
4503 struct smb2_set_info_rsp *rsp = NULL;
4505 struct kvec rsp_iov;
4508 struct cifs_ses *ses = tcon->ses;
4511 if (!ses || !(ses->server))
4517 if (smb3_encryption_required(tcon))
4518 flags |= CIFS_TRANSFORM_REQ;
4520 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4524 memset(&rqst, 0, sizeof(struct smb_rqst));
4528 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4529 info_class, info_type, additional_info,
4537 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4539 SMB2_set_info_free(&rqst);
4540 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4543 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4544 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4545 ses->Suid, info_class, (__u32)info_type, rc);
4548 free_rsp_buf(resp_buftype, rsp);
4554 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4555 u64 volatile_fid, u32 pid, __le64 *eof)
4557 struct smb2_file_eof_info info;
4561 info.EndOfFile = *eof;
4564 size = sizeof(struct smb2_file_eof_info);
4566 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4567 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4568 0, 1, &data, &size);
4572 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4573 u64 persistent_fid, u64 volatile_fid,
4574 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4576 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4577 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4578 1, (void **)&pnntsd, &pacllen);
4582 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4583 u64 persistent_fid, u64 volatile_fid,
4584 struct smb2_file_full_ea_info *buf, int len)
4586 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4587 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4588 0, 1, (void **)&buf, &len);
4592 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4593 const u64 persistent_fid, const u64 volatile_fid,
4596 struct smb_rqst rqst;
4598 struct smb2_oplock_break *req = NULL;
4599 struct cifs_ses *ses = tcon->ses;
4600 int flags = CIFS_OBREAK_OP;
4601 unsigned int total_len;
4603 struct kvec rsp_iov;
4606 cifs_dbg(FYI, "SMB2_oplock_break\n");
4607 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4612 if (smb3_encryption_required(tcon))
4613 flags |= CIFS_TRANSFORM_REQ;
4615 req->VolatileFid = volatile_fid;
4616 req->PersistentFid = persistent_fid;
4617 req->OplockLevel = oplock_level;
4618 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4620 flags |= CIFS_NO_RSP_BUF;
4622 iov[0].iov_base = (char *)req;
4623 iov[0].iov_len = total_len;
4625 memset(&rqst, 0, sizeof(struct smb_rqst));
4629 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4630 cifs_small_buf_release(req);
4633 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4634 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4641 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4642 struct kstatfs *kst)
4644 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4645 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4646 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4647 kst->f_bfree = kst->f_bavail =
4648 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4653 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4654 struct kstatfs *kst)
4656 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4657 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4658 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4659 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4660 kst->f_bavail = kst->f_bfree;
4662 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4663 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4664 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4665 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4666 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4672 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4673 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4676 struct smb2_query_info_req *req;
4677 unsigned int total_len;
4679 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4681 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4684 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4689 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4690 req->FileInfoClass = level;
4691 req->PersistentFileId = persistent_fid;
4692 req->VolatileFileId = volatile_fid;
4694 req->InputBufferOffset =
4695 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4696 req->OutputBufferLength = cpu_to_le32(
4697 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4699 iov->iov_base = (char *)req;
4700 iov->iov_len = total_len;
4705 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4706 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4708 struct smb_rqst rqst;
4709 struct smb2_query_info_rsp *rsp = NULL;
4711 struct kvec rsp_iov;
4714 struct cifs_ses *ses = tcon->ses;
4715 FILE_SYSTEM_POSIX_INFO *info = NULL;
4718 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4719 sizeof(FILE_SYSTEM_POSIX_INFO),
4720 persistent_fid, volatile_fid);
4724 if (smb3_encryption_required(tcon))
4725 flags |= CIFS_TRANSFORM_REQ;
4727 memset(&rqst, 0, sizeof(struct smb_rqst));
4731 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4732 cifs_small_buf_release(iov.iov_base);
4734 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4735 goto posix_qfsinf_exit;
4737 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4739 info = (FILE_SYSTEM_POSIX_INFO *)(
4740 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4741 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4742 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4743 sizeof(FILE_SYSTEM_POSIX_INFO));
4745 copy_posix_fs_info_to_kstatfs(info, fsdata);
4748 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4753 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4754 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4756 struct smb_rqst rqst;
4757 struct smb2_query_info_rsp *rsp = NULL;
4759 struct kvec rsp_iov;
4762 struct cifs_ses *ses = tcon->ses;
4763 struct smb2_fs_full_size_info *info = NULL;
4766 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4767 sizeof(struct smb2_fs_full_size_info),
4768 persistent_fid, volatile_fid);
4772 if (smb3_encryption_required(tcon))
4773 flags |= CIFS_TRANSFORM_REQ;
4775 memset(&rqst, 0, sizeof(struct smb_rqst));
4779 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4780 cifs_small_buf_release(iov.iov_base);
4782 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4785 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4787 info = (struct smb2_fs_full_size_info *)(
4788 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4789 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4790 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4791 sizeof(struct smb2_fs_full_size_info));
4793 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4796 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4801 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4802 u64 persistent_fid, u64 volatile_fid, int level)
4804 struct smb_rqst rqst;
4805 struct smb2_query_info_rsp *rsp = NULL;
4807 struct kvec rsp_iov;
4809 int resp_buftype, max_len, min_len;
4810 struct cifs_ses *ses = tcon->ses;
4811 unsigned int rsp_len, offset;
4814 if (level == FS_DEVICE_INFORMATION) {
4815 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4816 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4817 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4818 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4819 min_len = MIN_FS_ATTR_INFO_SIZE;
4820 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4821 max_len = sizeof(struct smb3_fs_ss_info);
4822 min_len = sizeof(struct smb3_fs_ss_info);
4823 } else if (level == FS_VOLUME_INFORMATION) {
4824 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4825 min_len = sizeof(struct smb3_fs_vol_info);
4827 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4831 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4832 persistent_fid, volatile_fid);
4836 if (smb3_encryption_required(tcon))
4837 flags |= CIFS_TRANSFORM_REQ;
4839 memset(&rqst, 0, sizeof(struct smb_rqst));
4843 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4844 cifs_small_buf_release(iov.iov_base);
4846 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4849 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4851 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4852 offset = le16_to_cpu(rsp->OutputBufferOffset);
4853 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4857 if (level == FS_ATTRIBUTE_INFORMATION)
4858 memcpy(&tcon->fsAttrInfo, offset
4859 + (char *)rsp, min_t(unsigned int,
4861 else if (level == FS_DEVICE_INFORMATION)
4862 memcpy(&tcon->fsDevInfo, offset
4863 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4864 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4865 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4866 (offset + (char *)rsp);
4867 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4868 tcon->perf_sector_size =
4869 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4870 } else if (level == FS_VOLUME_INFORMATION) {
4871 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4872 (offset + (char *)rsp);
4873 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4874 tcon->vol_create_time = vol_info->VolumeCreationTime;
4878 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4883 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4884 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4885 const __u32 num_lock, struct smb2_lock_element *buf)
4887 struct smb_rqst rqst;
4889 struct smb2_lock_req *req = NULL;
4891 struct kvec rsp_iov;
4894 int flags = CIFS_NO_RSP_BUF;
4895 unsigned int total_len;
4897 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4899 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4903 if (smb3_encryption_required(tcon))
4904 flags |= CIFS_TRANSFORM_REQ;
4906 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4907 req->LockCount = cpu_to_le16(num_lock);
4909 req->PersistentFileId = persist_fid;
4910 req->VolatileFileId = volatile_fid;
4912 count = num_lock * sizeof(struct smb2_lock_element);
4914 iov[0].iov_base = (char *)req;
4915 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4916 iov[1].iov_base = (char *)buf;
4917 iov[1].iov_len = count;
4919 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4921 memset(&rqst, 0, sizeof(struct smb_rqst));
4925 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4927 cifs_small_buf_release(req);
4929 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4930 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4931 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4932 tcon->ses->Suid, rc);
4939 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4940 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4941 const __u64 length, const __u64 offset, const __u32 lock_flags,
4944 struct smb2_lock_element lock;
4946 lock.Offset = cpu_to_le64(offset);
4947 lock.Length = cpu_to_le64(length);
4948 lock.Flags = cpu_to_le32(lock_flags);
4949 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4950 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4952 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4956 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4957 __u8 *lease_key, const __le32 lease_state)
4959 struct smb_rqst rqst;
4961 struct smb2_lease_ack *req = NULL;
4962 struct cifs_ses *ses = tcon->ses;
4963 int flags = CIFS_OBREAK_OP;
4964 unsigned int total_len;
4966 struct kvec rsp_iov;
4968 __u64 *please_key_high;
4969 __u64 *please_key_low;
4971 cifs_dbg(FYI, "SMB2_lease_break\n");
4972 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4977 if (smb3_encryption_required(tcon))
4978 flags |= CIFS_TRANSFORM_REQ;
4980 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4981 req->StructureSize = cpu_to_le16(36);
4984 memcpy(req->LeaseKey, lease_key, 16);
4985 req->LeaseState = lease_state;
4987 flags |= CIFS_NO_RSP_BUF;
4989 iov[0].iov_base = (char *)req;
4990 iov[0].iov_len = total_len;
4992 memset(&rqst, 0, sizeof(struct smb_rqst));
4996 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4997 cifs_small_buf_release(req);
4999 please_key_low = (__u64 *)lease_key;
5000 please_key_high = (__u64 *)(lease_key+8);
5002 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5003 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5004 ses->Suid, *please_key_low, *please_key_high, rc);
5005 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5007 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5008 ses->Suid, *please_key_low, *please_key_high);