1 // SPDX-License-Identifier: LGPL-2.1
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
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/task_io_accounting_ops.h>
22 #include <linux/uaccess.h>
23 #include <linux/uuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/xattr.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
42 #include "cached_dir.h"
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
51 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
73 int smb3_encryption_required(const struct cifs_tcon *tcon)
75 if (!tcon || !tcon->ses)
77 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
87 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
91 shdr->ProtocolId = SMB2_PROTO_NUMBER;
92 shdr->StructureSize = cpu_to_le16(64);
93 shdr->Command = smb2_cmd;
95 spin_lock(&server->req_lock);
96 /* Request up to 10 credits but don't go over the limit. */
97 if (server->credits >= server->max_credits)
98 shdr->CreditRequest = cpu_to_le16(0);
100 shdr->CreditRequest = cpu_to_le16(
101 min_t(int, server->max_credits -
102 server->credits, 10));
103 spin_unlock(&server->req_lock);
105 shdr->CreditRequest = cpu_to_le16(2);
107 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
114 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
115 shdr->CreditCharge = cpu_to_le16(1);
116 /* else CreditCharge MBZ */
118 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
119 /* Uid is not converted */
121 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
124 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
125 * to pass the path on the Open SMB prefixed by \\server\share.
126 * Not sure when we would need to do the augmented path (if ever) and
127 * setting this flag breaks the SMB2 open operation since it is
128 * illegal to send an empty path name (without \\server\share prefix)
129 * when the DFS flag is set in the SMB open header. We could
130 * consider setting the flag on all operations other than open
131 * but it is safer to net set it for now.
133 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
134 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136 if (server && server->sign && !smb3_encryption_required(tcon))
137 shdr->Flags |= SMB2_FLAGS_SIGNED;
143 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
144 struct TCP_Server_Info *server)
147 struct nls_table *nls_codepage;
148 struct cifs_ses *ses;
152 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
153 * check for tcp and smb session status done differently
154 * for those three - in the calling routine.
160 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
161 * cifs_tree_connect().
163 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
166 spin_lock(&tcon->tc_lock);
167 if (tcon->status == TID_EXITING) {
169 * only tree disconnect, open, and write,
170 * (and ulogoff which does not have tcon)
171 * are allowed as we start force umount.
173 if ((smb2_command != SMB2_WRITE) &&
174 (smb2_command != SMB2_CREATE) &&
175 (smb2_command != SMB2_TREE_DISCONNECT)) {
176 spin_unlock(&tcon->tc_lock);
177 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 spin_unlock(&tcon->tc_lock);
183 if ((!tcon->ses) || (tcon->ses->ses_status == SES_EXITING) ||
184 (!tcon->ses->server) || !server)
188 retries = server->nr_targets;
191 * Give demultiplex thread up to 10 seconds to each target available for
192 * reconnect -- should be greater than cifs socket timeout which is 7
195 while (server->tcpStatus == CifsNeedReconnect) {
197 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
198 * here since they are implicitly done when session drops.
200 switch (smb2_command) {
202 * BB Should we keep oplock break and add flush to exceptions?
204 case SMB2_TREE_DISCONNECT:
207 case SMB2_OPLOCK_BREAK:
211 rc = wait_event_interruptible_timeout(server->response_q,
212 (server->tcpStatus != CifsNeedReconnect),
215 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
220 /* are we still trying to reconnect? */
221 spin_lock(&server->srv_lock);
222 if (server->tcpStatus != CifsNeedReconnect) {
223 spin_unlock(&server->srv_lock);
226 spin_unlock(&server->srv_lock);
228 if (retries && --retries)
232 * on "soft" mounts we wait once. Hard mounts keep
233 * retrying until process is killed or server comes
237 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
240 retries = server->nr_targets;
243 spin_lock(&ses->chan_lock);
244 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
245 spin_unlock(&ses->chan_lock);
248 spin_unlock(&ses->chan_lock);
249 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
250 tcon->ses->chans_need_reconnect,
251 tcon->need_reconnect);
253 nls_codepage = load_nls_default();
256 * Recheck after acquire mutex. If another thread is negotiating
257 * and the server never sends an answer the socket will be closed
258 * and tcpStatus set to reconnect.
260 spin_lock(&server->srv_lock);
261 if (server->tcpStatus == CifsNeedReconnect) {
262 spin_unlock(&server->srv_lock);
266 spin_unlock(&server->srv_lock);
269 * need to prevent multiple threads trying to simultaneously
270 * reconnect the same SMB session
272 spin_lock(&ses->chan_lock);
273 if (!cifs_chan_needs_reconnect(ses, server)) {
274 spin_unlock(&ses->chan_lock);
276 /* this means that we only need to tree connect */
277 if (tcon->need_reconnect)
278 goto skip_sess_setup;
282 spin_unlock(&ses->chan_lock);
284 mutex_lock(&ses->session_mutex);
285 rc = cifs_negotiate_protocol(0, ses, server);
287 rc = cifs_setup_session(0, ses, server, nls_codepage);
288 if ((rc == -EACCES) && !tcon->retry) {
289 mutex_unlock(&ses->session_mutex);
293 mutex_unlock(&ses->session_mutex);
297 mutex_unlock(&ses->session_mutex);
300 mutex_unlock(&ses->session_mutex);
303 mutex_lock(&ses->session_mutex);
304 if (!tcon->need_reconnect) {
305 mutex_unlock(&ses->session_mutex);
308 cifs_mark_open_files_invalid(tcon);
309 if (tcon->use_persistent)
310 tcon->need_reopen_files = true;
312 rc = cifs_tree_connect(0, tcon, nls_codepage);
313 mutex_unlock(&ses->session_mutex);
315 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
317 /* If sess reconnected but tcon didn't, something strange ... */
318 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
322 if (smb2_command != SMB2_INTERNAL_CMD)
323 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
325 atomic_inc(&tconInfoReconnectCount);
328 * Check if handle based operation so we know whether we can continue
329 * or not without returning to caller to reset file handle.
332 * BB Is flush done by server on drop of tcp session? Should we special
333 * case it and skip above?
335 switch (smb2_command) {
341 case SMB2_QUERY_DIRECTORY:
342 case SMB2_CHANGE_NOTIFY:
343 case SMB2_QUERY_INFO:
348 unload_nls(nls_codepage);
353 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
354 struct TCP_Server_Info *server,
356 unsigned int *total_len)
358 struct smb2_pdu *spdu = buf;
359 /* lookup word count ie StructureSize from table */
360 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
363 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
364 * largest operations (Create)
368 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
369 spdu->StructureSize2 = cpu_to_le16(parmsize);
371 *total_len = parmsize + sizeof(struct smb2_hdr);
375 * Allocate and return pointer to an SMB request hdr, and set basic
376 * SMB information in the SMB header. If the return code is zero, this
377 * function must have filled in request_buf pointer.
379 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
380 struct TCP_Server_Info *server,
381 void **request_buf, unsigned int *total_len)
383 /* BB eventually switch this to SMB2 specific small buf size */
384 if (smb2_command == SMB2_SET_INFO)
385 *request_buf = cifs_buf_get();
387 *request_buf = cifs_small_buf_get();
388 if (*request_buf == NULL) {
389 /* BB should we add a retry in here if not a writepage? */
393 fill_small_buf(smb2_command, tcon, server,
394 (struct smb2_hdr *)(*request_buf),
398 uint16_t com_code = le16_to_cpu(smb2_command);
399 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
400 cifs_stats_inc(&tcon->num_smbs_sent);
406 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
407 struct TCP_Server_Info *server,
408 void **request_buf, unsigned int *total_len)
412 rc = smb2_reconnect(smb2_command, tcon, server);
416 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
420 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
421 struct TCP_Server_Info *server,
422 void **request_buf, unsigned int *total_len)
424 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
425 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
426 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
427 request_buf, total_len);
429 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
430 request_buf, total_len);
433 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
436 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
438 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
439 pneg_ctxt->DataLength = cpu_to_le16(38);
440 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
441 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
442 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
443 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
447 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
449 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
450 pneg_ctxt->DataLength =
451 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
452 - sizeof(struct smb2_neg_context));
453 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
454 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
455 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
456 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
460 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
462 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
463 unsigned short num_algs = 1; /* number of signing algorithms sent */
465 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
467 * Context Data length must be rounded to multiple of 8 for some servers
469 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
470 sizeof(struct smb2_neg_context) +
471 (num_algs * sizeof(u16)), 8));
472 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
473 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
475 ctxt_len += sizeof(__le16) * num_algs;
476 ctxt_len = ALIGN(ctxt_len, 8);
478 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
482 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
484 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
485 if (require_gcm_256) {
486 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
487 pneg_ctxt->CipherCount = cpu_to_le16(1);
488 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
489 } else if (enable_gcm_256) {
490 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
491 pneg_ctxt->CipherCount = cpu_to_le16(3);
492 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
493 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
494 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
496 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
497 pneg_ctxt->CipherCount = cpu_to_le16(2);
498 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
499 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
504 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
506 struct nls_table *cp = load_nls_default();
508 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
510 /* copy up to max of first 100 bytes of server name to NetName field */
511 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
512 /* context size is DataLength + minimal smb2_neg_context */
513 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
517 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
519 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
520 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
521 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
522 pneg_ctxt->Name[0] = 0x93;
523 pneg_ctxt->Name[1] = 0xAD;
524 pneg_ctxt->Name[2] = 0x25;
525 pneg_ctxt->Name[3] = 0x50;
526 pneg_ctxt->Name[4] = 0x9C;
527 pneg_ctxt->Name[5] = 0xB4;
528 pneg_ctxt->Name[6] = 0x11;
529 pneg_ctxt->Name[7] = 0xE7;
530 pneg_ctxt->Name[8] = 0xB4;
531 pneg_ctxt->Name[9] = 0x23;
532 pneg_ctxt->Name[10] = 0x83;
533 pneg_ctxt->Name[11] = 0xDE;
534 pneg_ctxt->Name[12] = 0x96;
535 pneg_ctxt->Name[13] = 0x8B;
536 pneg_ctxt->Name[14] = 0xCD;
537 pneg_ctxt->Name[15] = 0x7C;
541 assemble_neg_contexts(struct smb2_negotiate_req *req,
542 struct TCP_Server_Info *server, unsigned int *total_len)
545 char *hostname = NULL;
546 unsigned int ctxt_len, neg_context_count;
548 if (*total_len > 200) {
549 /* In case length corrupted don't want to overrun smb buffer */
550 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
555 * round up total_len of fixed part of SMB3 negotiate request to 8
556 * byte boundary before adding negotiate contexts
558 *total_len = ALIGN(*total_len, 8);
560 pneg_ctxt = (*total_len) + (char *)req;
561 req->NegotiateContextOffset = cpu_to_le32(*total_len);
563 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
564 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
565 *total_len += ctxt_len;
566 pneg_ctxt += ctxt_len;
568 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
569 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
570 *total_len += ctxt_len;
571 pneg_ctxt += ctxt_len;
574 * secondary channels don't have the hostname field populated
575 * use the hostname field in the primary channel instead
577 hostname = CIFS_SERVER_IS_CHAN(server) ?
578 server->primary_server->hostname : server->hostname;
579 if (hostname && (hostname[0] != 0)) {
580 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
584 neg_context_count = 3;
586 neg_context_count = 2;
588 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
589 *total_len += sizeof(struct smb2_posix_neg_context);
590 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
593 if (server->compress_algorithm) {
594 build_compression_ctxt((struct smb2_compression_capabilities_context *)
596 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
597 *total_len += ctxt_len;
598 pneg_ctxt += ctxt_len;
602 if (enable_negotiate_signing) {
603 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
605 *total_len += ctxt_len;
606 pneg_ctxt += ctxt_len;
610 /* check for and add transport_capabilities and signing capabilities */
611 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
615 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
617 unsigned int len = le16_to_cpu(ctxt->DataLength);
619 /* If invalid preauth context warn but use what we requested, SHA-512 */
620 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
621 pr_warn_once("server sent bad preauth context\n");
623 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
624 pr_warn_once("server sent invalid SaltLength\n");
627 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
628 pr_warn_once("Invalid SMB3 hash algorithm count\n");
629 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
630 pr_warn_once("unknown SMB3 hash algorithm\n");
633 static void decode_compress_ctx(struct TCP_Server_Info *server,
634 struct smb2_compression_capabilities_context *ctxt)
636 unsigned int len = le16_to_cpu(ctxt->DataLength);
638 /* sizeof compress context is a one element compression capbility struct */
640 pr_warn_once("server sent bad compression cntxt\n");
643 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
644 pr_warn_once("Invalid SMB3 compress algorithm count\n");
647 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
648 pr_warn_once("unknown compression algorithm\n");
651 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
654 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
655 struct smb2_encryption_neg_context *ctxt)
657 unsigned int len = le16_to_cpu(ctxt->DataLength);
659 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
660 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
661 pr_warn_once("server sent bad crypto ctxt len\n");
665 if (le16_to_cpu(ctxt->CipherCount) != 1) {
666 pr_warn_once("Invalid SMB3.11 cipher count\n");
669 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
670 if (require_gcm_256) {
671 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
672 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
675 } else if (ctxt->Ciphers[0] == 0) {
677 * e.g. if server only supported AES256_CCM (very unlikely)
678 * or server supported no encryption types or had all disabled.
679 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
680 * in which mount requested encryption ("seal") checks later
681 * on during tree connection will return proper rc, but if
682 * seal not requested by client, since server is allowed to
683 * return 0 to indicate no supported cipher, we can't fail here
685 server->cipher_type = 0;
686 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
687 pr_warn_once("Server does not support requested encryption types\n");
689 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
690 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
691 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
692 /* server returned a cipher we didn't ask for */
693 pr_warn_once("Invalid SMB3.11 cipher returned\n");
696 server->cipher_type = ctxt->Ciphers[0];
697 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
701 static void decode_signing_ctx(struct TCP_Server_Info *server,
702 struct smb2_signing_capabilities *pctxt)
704 unsigned int len = le16_to_cpu(pctxt->DataLength);
706 if ((len < 4) || (len > 16)) {
707 pr_warn_once("server sent bad signing negcontext\n");
710 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
711 pr_warn_once("Invalid signing algorithm count\n");
714 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
715 pr_warn_once("unknown signing algorithm\n");
719 server->signing_negotiated = true;
720 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
721 cifs_dbg(FYI, "signing algorithm %d chosen\n",
722 server->signing_algorithm);
726 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
727 struct TCP_Server_Info *server,
728 unsigned int len_of_smb)
730 struct smb2_neg_context *pctx;
731 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
732 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
733 unsigned int len_of_ctxts, i;
736 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
737 if (len_of_smb <= offset) {
738 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
742 len_of_ctxts = len_of_smb - offset;
744 for (i = 0; i < ctxt_cnt; i++) {
746 /* check that offset is not beyond end of SMB */
747 if (len_of_ctxts == 0)
750 if (len_of_ctxts < sizeof(struct smb2_neg_context))
753 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
754 clen = le16_to_cpu(pctx->DataLength);
755 if (clen > len_of_ctxts)
758 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
759 decode_preauth_context(
760 (struct smb2_preauth_neg_context *)pctx);
761 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
762 rc = decode_encrypt_ctx(server,
763 (struct smb2_encryption_neg_context *)pctx);
764 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
765 decode_compress_ctx(server,
766 (struct smb2_compression_capabilities_context *)pctx);
767 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
768 server->posix_ext_supported = true;
769 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
770 decode_signing_ctx(server,
771 (struct smb2_signing_capabilities *)pctx);
773 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
774 le16_to_cpu(pctx->ContextType));
778 /* offsets must be 8 byte aligned */
779 clen = ALIGN(clen, 8);
780 offset += clen + sizeof(struct smb2_neg_context);
781 len_of_ctxts -= clen;
786 static struct create_posix *
787 create_posix_buf(umode_t mode)
789 struct create_posix *buf;
791 buf = kzalloc(sizeof(struct create_posix),
796 buf->ccontext.DataOffset =
797 cpu_to_le16(offsetof(struct create_posix, Mode));
798 buf->ccontext.DataLength = cpu_to_le32(4);
799 buf->ccontext.NameOffset =
800 cpu_to_le16(offsetof(struct create_posix, Name));
801 buf->ccontext.NameLength = cpu_to_le16(16);
803 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
814 buf->Name[10] = 0x83;
815 buf->Name[11] = 0xDE;
816 buf->Name[12] = 0x96;
817 buf->Name[13] = 0x8B;
818 buf->Name[14] = 0xCD;
819 buf->Name[15] = 0x7C;
820 buf->Mode = cpu_to_le32(mode);
821 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
826 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
828 struct smb2_create_req *req = iov[0].iov_base;
829 unsigned int num = *num_iovec;
831 iov[num].iov_base = create_posix_buf(mode);
832 if (mode == ACL_NO_MODE)
833 cifs_dbg(FYI, "Invalid mode\n");
834 if (iov[num].iov_base == NULL)
836 iov[num].iov_len = sizeof(struct create_posix);
837 if (!req->CreateContextsOffset)
838 req->CreateContextsOffset = cpu_to_le32(
839 sizeof(struct smb2_create_req) +
840 iov[num - 1].iov_len);
841 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
842 *num_iovec = num + 1;
849 * SMB2 Worker functions follow:
851 * The general structure of the worker functions is:
852 * 1) Call smb2_init (assembles SMB2 header)
853 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
854 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
855 * 4) Decode SMB2 command specific fields in the fixed length area
856 * 5) Decode variable length data area (if any for this SMB2 command type)
857 * 6) Call free smb buffer
863 SMB2_negotiate(const unsigned int xid,
864 struct cifs_ses *ses,
865 struct TCP_Server_Info *server)
867 struct smb_rqst rqst;
868 struct smb2_negotiate_req *req;
869 struct smb2_negotiate_rsp *rsp;
874 int blob_offset, blob_length;
876 int flags = CIFS_NEG_OP;
877 unsigned int total_len;
879 cifs_dbg(FYI, "Negotiate protocol\n");
882 WARN(1, "%s: server is NULL!\n", __func__);
886 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
887 (void **) &req, &total_len);
891 req->hdr.SessionId = 0;
893 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
894 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
896 if (strcmp(server->vals->version_string,
897 SMB3ANY_VERSION_STRING) == 0) {
898 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
899 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
900 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
901 req->DialectCount = cpu_to_le16(3);
903 } else if (strcmp(server->vals->version_string,
904 SMBDEFAULT_VERSION_STRING) == 0) {
905 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
906 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
907 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
908 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
909 req->DialectCount = cpu_to_le16(4);
912 /* otherwise send specific dialect */
913 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
914 req->DialectCount = cpu_to_le16(1);
918 /* only one of SMB2 signing flags may be set in SMB2 request */
920 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
921 else if (global_secflags & CIFSSEC_MAY_SIGN)
922 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
924 req->SecurityMode = 0;
926 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
927 if (ses->chan_max > 1)
928 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
930 /* ClientGUID must be zero for SMB2.02 dialect */
931 if (server->vals->protocol_id == SMB20_PROT_ID)
932 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
934 memcpy(req->ClientGUID, server->client_guid,
935 SMB2_CLIENT_GUID_SIZE);
936 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
937 (strcmp(server->vals->version_string,
938 SMB3ANY_VERSION_STRING) == 0) ||
939 (strcmp(server->vals->version_string,
940 SMBDEFAULT_VERSION_STRING) == 0))
941 assemble_neg_contexts(req, server, &total_len);
943 iov[0].iov_base = (char *)req;
944 iov[0].iov_len = total_len;
946 memset(&rqst, 0, sizeof(struct smb_rqst));
950 rc = cifs_send_recv(xid, ses, server,
951 &rqst, &resp_buftype, flags, &rsp_iov);
952 cifs_small_buf_release(req);
953 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
955 * No tcon so can't do
956 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
958 if (rc == -EOPNOTSUPP) {
959 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
965 if (strcmp(server->vals->version_string,
966 SMB3ANY_VERSION_STRING) == 0) {
967 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
969 "SMB2 dialect returned but not requested\n");
971 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
973 "SMB2.1 dialect returned but not requested\n");
975 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
976 /* ops set to 3.0 by default for default so update */
977 server->ops = &smb311_operations;
978 server->vals = &smb311_values;
980 } else if (strcmp(server->vals->version_string,
981 SMBDEFAULT_VERSION_STRING) == 0) {
982 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
984 "SMB2 dialect returned but not requested\n");
986 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
987 /* ops set to 3.0 by default for default so update */
988 server->ops = &smb21_operations;
989 server->vals = &smb21_values;
990 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
991 server->ops = &smb311_operations;
992 server->vals = &smb311_values;
994 } else if (le16_to_cpu(rsp->DialectRevision) !=
995 server->vals->protocol_id) {
996 /* if requested single dialect ensure returned dialect matched */
997 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
998 le16_to_cpu(rsp->DialectRevision));
1002 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1004 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1005 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1006 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1007 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1008 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1009 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1010 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1011 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1012 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1013 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1015 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1016 le16_to_cpu(rsp->DialectRevision));
1021 server->dialect = le16_to_cpu(rsp->DialectRevision);
1024 * Keep a copy of the hash after negprot. This hash will be
1025 * the starting hash value for all sessions made from this
1028 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1029 SMB2_PREAUTH_HASH_SIZE);
1031 /* SMB2 only has an extended negflavor */
1032 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1033 /* set it to the maximum buffer size value we can send with 1 credit */
1034 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1035 SMB2_MAX_BUFFER_SIZE);
1036 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1037 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1038 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1039 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1040 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1042 server->capabilities = le32_to_cpu(rsp->Capabilities);
1043 /* Internal types */
1044 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1047 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1048 * Set the cipher type manually.
1050 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1051 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1053 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1054 (struct smb2_hdr *)rsp);
1056 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1058 * ses->sectype = RawNTLMSSP;
1059 * but for time being this is our only auth choice so doesn't matter.
1060 * We just found a server which sets blob length to zero expecting raw.
1062 if (blob_length == 0) {
1063 cifs_dbg(FYI, "missing security blob on negprot\n");
1064 server->sec_ntlmssp = true;
1067 rc = cifs_enable_signing(server, ses->sign);
1071 rc = decode_negTokenInit(security_blob, blob_length, server);
1078 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1079 if (rsp->NegotiateContextCount)
1080 rc = smb311_decode_neg_context(rsp, server,
1083 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1086 free_rsp_buf(resp_buftype, rsp);
1090 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1093 struct validate_negotiate_info_req *pneg_inbuf;
1094 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1096 u32 inbuflen; /* max of 4 dialects */
1097 struct TCP_Server_Info *server = tcon->ses->server;
1099 cifs_dbg(FYI, "validate negotiate\n");
1101 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1102 if (server->dialect == SMB311_PROT_ID)
1106 * validation ioctl must be signed, so no point sending this if we
1107 * can not sign it (ie are not known user). Even if signing is not
1108 * required (enabled but not negotiated), in those cases we selectively
1109 * sign just this, the first and only signed request on a connection.
1110 * Having validation of negotiate info helps reduce attack vectors.
1112 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1113 return 0; /* validation requires signing */
1115 if (tcon->ses->user_name == NULL) {
1116 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1117 return 0; /* validation requires signing */
1120 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1121 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1123 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1127 pneg_inbuf->Capabilities =
1128 cpu_to_le32(server->vals->req_capabilities);
1129 if (tcon->ses->chan_max > 1)
1130 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1132 memcpy(pneg_inbuf->Guid, server->client_guid,
1133 SMB2_CLIENT_GUID_SIZE);
1135 if (tcon->ses->sign)
1136 pneg_inbuf->SecurityMode =
1137 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1138 else if (global_secflags & CIFSSEC_MAY_SIGN)
1139 pneg_inbuf->SecurityMode =
1140 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1142 pneg_inbuf->SecurityMode = 0;
1145 if (strcmp(server->vals->version_string,
1146 SMB3ANY_VERSION_STRING) == 0) {
1147 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1148 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1149 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1150 pneg_inbuf->DialectCount = cpu_to_le16(3);
1151 /* SMB 2.1 not included so subtract one dialect from len */
1152 inbuflen = sizeof(*pneg_inbuf) -
1153 (sizeof(pneg_inbuf->Dialects[0]));
1154 } else if (strcmp(server->vals->version_string,
1155 SMBDEFAULT_VERSION_STRING) == 0) {
1156 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1157 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1158 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1159 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1160 pneg_inbuf->DialectCount = cpu_to_le16(4);
1161 /* structure is big enough for 4 dialects */
1162 inbuflen = sizeof(*pneg_inbuf);
1164 /* otherwise specific dialect was requested */
1165 pneg_inbuf->Dialects[0] =
1166 cpu_to_le16(server->vals->protocol_id);
1167 pneg_inbuf->DialectCount = cpu_to_le16(1);
1168 /* structure is big enough for 4 dialects, sending only 1 */
1169 inbuflen = sizeof(*pneg_inbuf) -
1170 sizeof(pneg_inbuf->Dialects[0]) * 3;
1173 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1174 FSCTL_VALIDATE_NEGOTIATE_INFO,
1175 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1176 (char **)&pneg_rsp, &rsplen);
1177 if (rc == -EOPNOTSUPP) {
1179 * Old Windows versions or Netapp SMB server can return
1180 * not supported error. Client should accept it.
1182 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1184 goto out_free_inbuf;
1185 } else if (rc != 0) {
1186 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1189 goto out_free_inbuf;
1193 if (rsplen != sizeof(*pneg_rsp)) {
1194 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1197 /* relax check since Mac returns max bufsize allowed on ioctl */
1198 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1202 /* check validate negotiate info response matches what we got earlier */
1203 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1206 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1209 /* do not validate server guid because not saved at negprot time yet */
1211 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1212 SMB2_LARGE_FILES) != server->capabilities)
1215 /* validate negotiate successful */
1217 cifs_dbg(FYI, "validate negotiate info successful\n");
1221 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1230 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1232 switch (requested) {
1239 if (server->sec_ntlmssp &&
1240 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1242 if ((server->sec_kerberos || server->sec_mskerberos) &&
1243 (global_secflags & CIFSSEC_MAY_KRB5))
1251 struct SMB2_sess_data {
1253 struct cifs_ses *ses;
1254 struct TCP_Server_Info *server;
1255 struct nls_table *nls_cp;
1256 void (*func)(struct SMB2_sess_data *);
1258 u64 previous_session;
1260 /* we will send the SMB in three pieces:
1261 * a fixed length beginning part, an optional
1262 * SPNEGO blob (which can be zero length), and a
1263 * last part which will include the strings
1264 * and rest of bcc area. This allows us to avoid
1265 * a large buffer 17K allocation
1272 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1275 struct cifs_ses *ses = sess_data->ses;
1276 struct TCP_Server_Info *server = sess_data->server;
1277 struct smb2_sess_setup_req *req;
1278 unsigned int total_len;
1279 bool is_binding = false;
1281 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1287 spin_lock(&ses->chan_lock);
1288 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1289 spin_unlock(&ses->chan_lock);
1292 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1293 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1294 req->PreviousSessionId = 0;
1295 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1296 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1298 /* First session, not a reauthenticate */
1299 req->hdr.SessionId = 0;
1301 * if reconnect, we need to send previous sess id
1304 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1305 req->Flags = 0; /* MBZ */
1306 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1307 sess_data->previous_session);
1310 /* enough to enable echos and oplocks and one max size write */
1311 req->hdr.CreditRequest = cpu_to_le16(130);
1313 /* only one of SMB2 signing flags may be set in SMB2 request */
1315 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1316 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1317 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1319 req->SecurityMode = 0;
1321 #ifdef CONFIG_CIFS_DFS_UPCALL
1322 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1324 req->Capabilities = 0;
1325 #endif /* DFS_UPCALL */
1327 req->Channel = 0; /* MBZ */
1329 sess_data->iov[0].iov_base = (char *)req;
1331 sess_data->iov[0].iov_len = total_len - 1;
1333 * This variable will be used to clear the buffer
1334 * allocated above in case of any error in the calling function.
1336 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1342 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1344 struct kvec *iov = sess_data->iov;
1346 /* iov[1] is already freed by caller */
1347 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1348 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1350 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1351 sess_data->buf0_type = CIFS_NO_BUFFER;
1355 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1358 struct smb_rqst rqst;
1359 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1360 struct kvec rsp_iov = { NULL, 0 };
1362 /* Testing shows that buffer offset must be at location of Buffer[0] */
1363 req->SecurityBufferOffset =
1364 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1365 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1367 memset(&rqst, 0, sizeof(struct smb_rqst));
1368 rqst.rq_iov = sess_data->iov;
1371 /* BB add code to build os and lm fields */
1372 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1375 &sess_data->buf0_type,
1376 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1377 cifs_small_buf_release(sess_data->iov[0].iov_base);
1378 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1384 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1387 struct cifs_ses *ses = sess_data->ses;
1388 struct TCP_Server_Info *server = sess_data->server;
1390 cifs_server_lock(server);
1391 if (server->ops->generate_signingkey) {
1392 rc = server->ops->generate_signingkey(ses, server);
1395 "SMB3 session key generation failed\n");
1396 cifs_server_unlock(server);
1400 if (!server->session_estab) {
1401 server->sequence_number = 0x2;
1402 server->session_estab = true;
1404 cifs_server_unlock(server);
1406 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1410 #ifdef CONFIG_CIFS_UPCALL
1412 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1415 struct cifs_ses *ses = sess_data->ses;
1416 struct TCP_Server_Info *server = sess_data->server;
1417 struct cifs_spnego_msg *msg;
1418 struct key *spnego_key = NULL;
1419 struct smb2_sess_setup_rsp *rsp = NULL;
1420 bool is_binding = false;
1422 rc = SMB2_sess_alloc_buffer(sess_data);
1426 spnego_key = cifs_get_spnego_key(ses, server);
1427 if (IS_ERR(spnego_key)) {
1428 rc = PTR_ERR(spnego_key);
1430 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1435 msg = spnego_key->payload.data[0];
1437 * check version field to make sure that cifs.upcall is
1438 * sending us a response in an expected form
1440 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1441 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1442 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1444 goto out_put_spnego_key;
1447 spin_lock(&ses->chan_lock);
1448 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1449 spin_unlock(&ses->chan_lock);
1451 /* keep session key if binding */
1453 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1455 if (!ses->auth_key.response) {
1456 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1459 goto out_put_spnego_key;
1461 ses->auth_key.len = msg->sesskey_len;
1464 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1465 sess_data->iov[1].iov_len = msg->secblob_len;
1467 rc = SMB2_sess_sendreceive(sess_data);
1469 goto out_put_spnego_key;
1471 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1472 /* keep session id and flags if binding */
1474 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1475 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1478 rc = SMB2_sess_establish_session(sess_data);
1480 key_invalidate(spnego_key);
1481 key_put(spnego_key);
1483 kfree_sensitive(ses->auth_key.response);
1484 ses->auth_key.response = NULL;
1485 ses->auth_key.len = 0;
1488 sess_data->result = rc;
1489 sess_data->func = NULL;
1490 SMB2_sess_free_buffer(sess_data);
1494 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1496 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1497 sess_data->result = -EOPNOTSUPP;
1498 sess_data->func = NULL;
1503 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1506 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1509 struct cifs_ses *ses = sess_data->ses;
1510 struct TCP_Server_Info *server = sess_data->server;
1511 struct smb2_sess_setup_rsp *rsp = NULL;
1512 unsigned char *ntlmssp_blob = NULL;
1513 bool use_spnego = false; /* else use raw ntlmssp */
1514 u16 blob_length = 0;
1515 bool is_binding = false;
1518 * If memory allocation is successful, caller of this function
1521 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1522 if (!ses->ntlmssp) {
1526 ses->ntlmssp->sesskey_per_smbsess = true;
1528 rc = SMB2_sess_alloc_buffer(sess_data);
1532 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1533 &blob_length, ses, server,
1539 /* BB eventually need to add this */
1540 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1544 sess_data->iov[1].iov_base = ntlmssp_blob;
1545 sess_data->iov[1].iov_len = blob_length;
1547 rc = SMB2_sess_sendreceive(sess_data);
1548 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1550 /* If true, rc here is expected and not an error */
1551 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1552 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1558 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1559 le16_to_cpu(rsp->SecurityBufferOffset)) {
1560 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1561 le16_to_cpu(rsp->SecurityBufferOffset));
1565 rc = decode_ntlmssp_challenge(rsp->Buffer,
1566 le16_to_cpu(rsp->SecurityBufferLength), ses);
1570 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1572 spin_lock(&ses->chan_lock);
1573 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1574 spin_unlock(&ses->chan_lock);
1576 /* keep existing ses id and flags if binding */
1578 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1579 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1583 kfree_sensitive(ntlmssp_blob);
1584 SMB2_sess_free_buffer(sess_data);
1586 sess_data->result = 0;
1587 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1591 kfree_sensitive(ses->ntlmssp);
1592 ses->ntlmssp = NULL;
1593 sess_data->result = rc;
1594 sess_data->func = NULL;
1598 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1601 struct cifs_ses *ses = sess_data->ses;
1602 struct TCP_Server_Info *server = sess_data->server;
1603 struct smb2_sess_setup_req *req;
1604 struct smb2_sess_setup_rsp *rsp = NULL;
1605 unsigned char *ntlmssp_blob = NULL;
1606 bool use_spnego = false; /* else use raw ntlmssp */
1607 u16 blob_length = 0;
1608 bool is_binding = false;
1610 rc = SMB2_sess_alloc_buffer(sess_data);
1614 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1615 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1617 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1621 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1626 /* BB eventually need to add this */
1627 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1631 sess_data->iov[1].iov_base = ntlmssp_blob;
1632 sess_data->iov[1].iov_len = blob_length;
1634 rc = SMB2_sess_sendreceive(sess_data);
1638 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1640 spin_lock(&ses->chan_lock);
1641 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1642 spin_unlock(&ses->chan_lock);
1644 /* keep existing ses id and flags if binding */
1646 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1647 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1650 rc = SMB2_sess_establish_session(sess_data);
1651 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1652 if (ses->server->dialect < SMB30_PROT_ID) {
1653 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1655 * The session id is opaque in terms of endianness, so we can't
1656 * print it as a long long. we dump it as we got it on the wire
1658 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1660 cifs_dbg(VFS, "Session Key %*ph\n",
1661 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1662 cifs_dbg(VFS, "Signing Key %*ph\n",
1663 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1667 kfree_sensitive(ntlmssp_blob);
1668 SMB2_sess_free_buffer(sess_data);
1669 kfree_sensitive(ses->ntlmssp);
1670 ses->ntlmssp = NULL;
1671 sess_data->result = rc;
1672 sess_data->func = NULL;
1676 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1679 struct cifs_ses *ses = sess_data->ses;
1680 struct TCP_Server_Info *server = sess_data->server;
1682 type = smb2_select_sectype(server, ses->sectype);
1683 cifs_dbg(FYI, "sess setup type %d\n", type);
1684 if (type == Unspecified) {
1685 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1691 sess_data->func = SMB2_auth_kerberos;
1694 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1697 cifs_dbg(VFS, "secType %d not supported!\n", type);
1705 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1706 struct TCP_Server_Info *server,
1707 const struct nls_table *nls_cp)
1710 struct SMB2_sess_data *sess_data;
1712 cifs_dbg(FYI, "Session Setup\n");
1715 WARN(1, "%s: server is NULL!\n", __func__);
1719 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1723 sess_data->xid = xid;
1724 sess_data->ses = ses;
1725 sess_data->server = server;
1726 sess_data->buf0_type = CIFS_NO_BUFFER;
1727 sess_data->nls_cp = (struct nls_table *) nls_cp;
1728 sess_data->previous_session = ses->Suid;
1730 rc = SMB2_select_sec(sess_data);
1735 * Initialize the session hash with the server one.
1737 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1738 SMB2_PREAUTH_HASH_SIZE);
1740 while (sess_data->func)
1741 sess_data->func(sess_data);
1743 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1744 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1745 rc = sess_data->result;
1747 kfree_sensitive(sess_data);
1752 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1754 struct smb_rqst rqst;
1755 struct smb2_logoff_req *req; /* response is also trivial struct */
1757 struct TCP_Server_Info *server;
1759 unsigned int total_len;
1761 struct kvec rsp_iov;
1764 cifs_dbg(FYI, "disconnect session %p\n", ses);
1766 if (ses && (ses->server))
1767 server = ses->server;
1771 /* no need to send SMB logoff if uid already closed due to reconnect */
1772 spin_lock(&ses->chan_lock);
1773 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1774 spin_unlock(&ses->chan_lock);
1775 goto smb2_session_already_dead;
1777 spin_unlock(&ses->chan_lock);
1779 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1780 (void **) &req, &total_len);
1784 /* since no tcon, smb2_init can not do this, so do here */
1785 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1787 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1788 flags |= CIFS_TRANSFORM_REQ;
1789 else if (server->sign)
1790 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1792 flags |= CIFS_NO_RSP_BUF;
1794 iov[0].iov_base = (char *)req;
1795 iov[0].iov_len = total_len;
1797 memset(&rqst, 0, sizeof(struct smb_rqst));
1801 rc = cifs_send_recv(xid, ses, ses->server,
1802 &rqst, &resp_buf_type, flags, &rsp_iov);
1803 cifs_small_buf_release(req);
1805 * No tcon so can't do
1806 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1809 smb2_session_already_dead:
1813 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1815 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1818 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1820 /* These are similar values to what Windows uses */
1821 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1823 tcon->max_chunks = 256;
1824 tcon->max_bytes_chunk = 1048576;
1825 tcon->max_bytes_copy = 16777216;
1829 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1830 struct cifs_tcon *tcon, const struct nls_table *cp)
1832 struct smb_rqst rqst;
1833 struct smb2_tree_connect_req *req;
1834 struct smb2_tree_connect_rsp *rsp = NULL;
1836 struct kvec rsp_iov = { NULL, 0 };
1840 __le16 *unc_path = NULL;
1842 unsigned int total_len;
1843 struct TCP_Server_Info *server;
1845 /* always use master channel */
1846 server = ses->server;
1848 cifs_dbg(FYI, "TCON\n");
1850 if (!server || !tree)
1853 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1854 if (unc_path == NULL)
1857 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1859 if (unc_path_len < 2) {
1864 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1866 atomic_set(&tcon->num_remote_opens, 0);
1867 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1868 (void **) &req, &total_len);
1874 if (smb3_encryption_required(tcon))
1875 flags |= CIFS_TRANSFORM_REQ;
1877 iov[0].iov_base = (char *)req;
1879 iov[0].iov_len = total_len - 1;
1881 /* Testing shows that buffer offset must be at location of Buffer[0] */
1882 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1884 req->PathLength = cpu_to_le16(unc_path_len - 2);
1885 iov[1].iov_base = unc_path;
1886 iov[1].iov_len = unc_path_len;
1889 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1890 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1891 * (Samba servers don't always set the flag so also check if null user)
1893 if ((server->dialect == SMB311_PROT_ID) &&
1894 !smb3_encryption_required(tcon) &&
1895 !(ses->session_flags &
1896 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1897 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1898 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1900 memset(&rqst, 0, sizeof(struct smb_rqst));
1904 /* Need 64 for max size write so ask for more in case not there yet */
1905 req->hdr.CreditRequest = cpu_to_le16(64);
1907 rc = cifs_send_recv(xid, ses, server,
1908 &rqst, &resp_buftype, flags, &rsp_iov);
1909 cifs_small_buf_release(req);
1910 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1911 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1912 if ((rc != 0) || (rsp == NULL)) {
1913 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1914 tcon->need_reconnect = true;
1915 goto tcon_error_exit;
1918 switch (rsp->ShareType) {
1919 case SMB2_SHARE_TYPE_DISK:
1920 cifs_dbg(FYI, "connection to disk share\n");
1922 case SMB2_SHARE_TYPE_PIPE:
1924 cifs_dbg(FYI, "connection to pipe share\n");
1926 case SMB2_SHARE_TYPE_PRINT:
1928 cifs_dbg(FYI, "connection to printer\n");
1931 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1933 goto tcon_error_exit;
1936 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1937 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1938 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1939 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
1940 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
1942 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1943 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1944 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1947 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1948 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1950 init_copy_chunk_defaults(tcon);
1951 if (server->ops->validate_negotiate)
1952 rc = server->ops->validate_negotiate(xid, tcon);
1955 free_rsp_buf(resp_buftype, rsp);
1960 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
1961 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1966 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1968 struct smb_rqst rqst;
1969 struct smb2_tree_disconnect_req *req; /* response is trivial */
1971 struct cifs_ses *ses = tcon->ses;
1973 unsigned int total_len;
1975 struct kvec rsp_iov;
1978 cifs_dbg(FYI, "Tree Disconnect\n");
1980 if (!ses || !(ses->server))
1983 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
1984 spin_lock(&ses->chan_lock);
1985 if ((tcon->need_reconnect) ||
1986 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
1987 spin_unlock(&ses->chan_lock);
1990 spin_unlock(&ses->chan_lock);
1992 invalidate_all_cached_dirs(tcon);
1994 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
2000 if (smb3_encryption_required(tcon))
2001 flags |= CIFS_TRANSFORM_REQ;
2003 flags |= CIFS_NO_RSP_BUF;
2005 iov[0].iov_base = (char *)req;
2006 iov[0].iov_len = total_len;
2008 memset(&rqst, 0, sizeof(struct smb_rqst));
2012 rc = cifs_send_recv(xid, ses, ses->server,
2013 &rqst, &resp_buf_type, flags, &rsp_iov);
2014 cifs_small_buf_release(req);
2016 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2017 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2019 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2025 static struct create_durable *
2026 create_durable_buf(void)
2028 struct create_durable *buf;
2030 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2034 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2035 (struct create_durable, Data));
2036 buf->ccontext.DataLength = cpu_to_le32(16);
2037 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2038 (struct create_durable, Name));
2039 buf->ccontext.NameLength = cpu_to_le16(4);
2040 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2048 static struct create_durable *
2049 create_reconnect_durable_buf(struct cifs_fid *fid)
2051 struct create_durable *buf;
2053 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2057 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2058 (struct create_durable, Data));
2059 buf->ccontext.DataLength = cpu_to_le32(16);
2060 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2061 (struct create_durable, Name));
2062 buf->ccontext.NameLength = cpu_to_le16(4);
2063 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2064 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2065 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2074 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2076 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2078 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2079 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2080 buf->IndexNumber = pdisk_id->DiskFileId;
2084 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2085 struct create_posix_rsp *posix)
2088 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2089 u8 *end = beg + le32_to_cpu(cc->DataLength);
2092 memset(posix, 0, sizeof(*posix));
2094 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2095 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2096 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2099 sid_len = posix_info_sid_size(sid, end);
2101 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2104 memcpy(&posix->owner, sid, sid_len);
2106 sid = sid + sid_len;
2107 sid_len = posix_info_sid_size(sid, end);
2109 cifs_dbg(VFS, "bad group sid in posix create response\n");
2112 memcpy(&posix->group, sid, sid_len);
2114 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2115 posix->nlink, posix->mode, posix->reparse_tag);
2119 smb2_parse_contexts(struct TCP_Server_Info *server,
2120 struct smb2_create_rsp *rsp,
2121 unsigned int *epoch, char *lease_key, __u8 *oplock,
2122 struct smb2_file_all_info *buf,
2123 struct create_posix_rsp *posix)
2126 struct create_context *cc;
2128 unsigned int remaining;
2130 static const char smb3_create_tag_posix[] = {
2131 0x93, 0xAD, 0x25, 0x50, 0x9C,
2132 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2133 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2137 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2138 remaining = le32_to_cpu(rsp->CreateContextsLength);
2139 cc = (struct create_context *)data_offset;
2141 /* Initialize inode number to 0 in case no valid data in qfid context */
2143 buf->IndexNumber = 0;
2145 while (remaining >= sizeof(struct create_context)) {
2146 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2147 if (le16_to_cpu(cc->NameLength) == 4 &&
2148 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2149 *oplock = server->ops->parse_lease_buf(cc, epoch,
2151 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2152 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2153 parse_query_id_ctxt(cc, buf);
2154 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2156 memcmp(name, smb3_create_tag_posix, 16) == 0)
2157 parse_posix_ctxt(cc, buf, posix);
2160 cifs_dbg(FYI, "Context not matched with len %d\n",
2161 le16_to_cpu(cc->NameLength));
2162 cifs_dump_mem("Cctxt name: ", name, 4);
2165 next = le32_to_cpu(cc->Next);
2169 cc = (struct create_context *)((char *)cc + next);
2172 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2173 *oplock = rsp->OplockLevel;
2179 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2180 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2182 struct smb2_create_req *req = iov[0].iov_base;
2183 unsigned int num = *num_iovec;
2185 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2186 if (iov[num].iov_base == NULL)
2188 iov[num].iov_len = server->vals->create_lease_size;
2189 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2190 if (!req->CreateContextsOffset)
2191 req->CreateContextsOffset = cpu_to_le32(
2192 sizeof(struct smb2_create_req) +
2193 iov[num - 1].iov_len);
2194 le32_add_cpu(&req->CreateContextsLength,
2195 server->vals->create_lease_size);
2196 *num_iovec = num + 1;
2200 static struct create_durable_v2 *
2201 create_durable_v2_buf(struct cifs_open_parms *oparms)
2203 struct cifs_fid *pfid = oparms->fid;
2204 struct create_durable_v2 *buf;
2206 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2210 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2211 (struct create_durable_v2, dcontext));
2212 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2213 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2214 (struct create_durable_v2, Name));
2215 buf->ccontext.NameLength = cpu_to_le16(4);
2218 * NB: Handle timeout defaults to 0, which allows server to choose
2219 * (most servers default to 120 seconds) and most clients default to 0.
2220 * This can be overridden at mount ("handletimeout=") if the user wants
2221 * a different persistent (or resilient) handle timeout for all opens
2222 * opens on a particular SMB3 mount.
2224 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2225 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2226 generate_random_uuid(buf->dcontext.CreateGuid);
2227 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2229 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2237 static struct create_durable_handle_reconnect_v2 *
2238 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2240 struct create_durable_handle_reconnect_v2 *buf;
2242 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2247 buf->ccontext.DataOffset =
2248 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2250 buf->ccontext.DataLength =
2251 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2252 buf->ccontext.NameOffset =
2253 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2255 buf->ccontext.NameLength = cpu_to_le16(4);
2257 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2258 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2259 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2260 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2262 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2271 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2272 struct cifs_open_parms *oparms)
2274 struct smb2_create_req *req = iov[0].iov_base;
2275 unsigned int num = *num_iovec;
2277 iov[num].iov_base = create_durable_v2_buf(oparms);
2278 if (iov[num].iov_base == NULL)
2280 iov[num].iov_len = sizeof(struct create_durable_v2);
2281 if (!req->CreateContextsOffset)
2282 req->CreateContextsOffset =
2283 cpu_to_le32(sizeof(struct smb2_create_req) +
2285 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2286 *num_iovec = num + 1;
2291 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2292 struct cifs_open_parms *oparms)
2294 struct smb2_create_req *req = iov[0].iov_base;
2295 unsigned int num = *num_iovec;
2297 /* indicate that we don't need to relock the file */
2298 oparms->reconnect = false;
2300 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2301 if (iov[num].iov_base == NULL)
2303 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2304 if (!req->CreateContextsOffset)
2305 req->CreateContextsOffset =
2306 cpu_to_le32(sizeof(struct smb2_create_req) +
2308 le32_add_cpu(&req->CreateContextsLength,
2309 sizeof(struct create_durable_handle_reconnect_v2));
2310 *num_iovec = num + 1;
2315 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2316 struct cifs_open_parms *oparms, bool use_persistent)
2318 struct smb2_create_req *req = iov[0].iov_base;
2319 unsigned int num = *num_iovec;
2321 if (use_persistent) {
2322 if (oparms->reconnect)
2323 return add_durable_reconnect_v2_context(iov, num_iovec,
2326 return add_durable_v2_context(iov, num_iovec, oparms);
2329 if (oparms->reconnect) {
2330 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2331 /* indicate that we don't need to relock the file */
2332 oparms->reconnect = false;
2334 iov[num].iov_base = create_durable_buf();
2335 if (iov[num].iov_base == NULL)
2337 iov[num].iov_len = sizeof(struct create_durable);
2338 if (!req->CreateContextsOffset)
2339 req->CreateContextsOffset =
2340 cpu_to_le32(sizeof(struct smb2_create_req) +
2342 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2343 *num_iovec = num + 1;
2347 /* See MS-SMB2 2.2.13.2.7 */
2348 static struct crt_twarp_ctxt *
2349 create_twarp_buf(__u64 timewarp)
2351 struct crt_twarp_ctxt *buf;
2353 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2357 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2358 (struct crt_twarp_ctxt, Timestamp));
2359 buf->ccontext.DataLength = cpu_to_le32(8);
2360 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2361 (struct crt_twarp_ctxt, Name));
2362 buf->ccontext.NameLength = cpu_to_le16(4);
2363 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2368 buf->Timestamp = cpu_to_le64(timewarp);
2372 /* See MS-SMB2 2.2.13.2.7 */
2374 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2376 struct smb2_create_req *req = iov[0].iov_base;
2377 unsigned int num = *num_iovec;
2379 iov[num].iov_base = create_twarp_buf(timewarp);
2380 if (iov[num].iov_base == NULL)
2382 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2383 if (!req->CreateContextsOffset)
2384 req->CreateContextsOffset = cpu_to_le32(
2385 sizeof(struct smb2_create_req) +
2386 iov[num - 1].iov_len);
2387 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2388 *num_iovec = num + 1;
2392 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2393 static void setup_owner_group_sids(char *buf)
2395 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2397 /* Populate the user ownership fields S-1-5-88-1 */
2398 sids->owner.Revision = 1;
2399 sids->owner.NumAuth = 3;
2400 sids->owner.Authority[5] = 5;
2401 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2402 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2403 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2405 /* Populate the group ownership fields S-1-5-88-2 */
2406 sids->group.Revision = 1;
2407 sids->group.NumAuth = 3;
2408 sids->group.Authority[5] = 5;
2409 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2410 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2411 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2413 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2416 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2417 static struct crt_sd_ctxt *
2418 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2420 struct crt_sd_ctxt *buf;
2422 unsigned int acelen, acl_size, ace_count;
2423 unsigned int owner_offset = 0;
2424 unsigned int group_offset = 0;
2425 struct smb3_acl acl = {};
2427 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2430 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2431 *len += sizeof(struct owner_group_sids);
2434 buf = kzalloc(*len, GFP_KERNEL);
2438 ptr = (__u8 *)&buf[1];
2440 /* offset fields are from beginning of security descriptor not of create context */
2441 owner_offset = ptr - (__u8 *)&buf->sd;
2442 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2443 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2444 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2446 setup_owner_group_sids(ptr);
2447 ptr += sizeof(struct owner_group_sids);
2449 buf->sd.OffsetOwner = 0;
2450 buf->sd.OffsetGroup = 0;
2453 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2454 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2455 buf->ccontext.NameLength = cpu_to_le16(4);
2456 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2461 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2464 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2465 * and "DP" ie the DACL is present
2467 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2469 /* offset owner, group and Sbz1 and SACL are all zero */
2470 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2471 /* Ship the ACL for now. we will copy it into buf later. */
2473 ptr += sizeof(struct smb3_acl);
2475 /* create one ACE to hold the mode embedded in reserved special SID */
2476 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2478 acl_size = acelen + sizeof(struct smb3_acl);
2482 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2483 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2489 /* and one more ACE to allow access for authenticated users */
2490 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2495 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2496 acl.AclSize = cpu_to_le16(acl_size);
2497 acl.AceCount = cpu_to_le16(ace_count);
2498 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2499 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2501 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2502 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2508 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2510 struct smb2_create_req *req = iov[0].iov_base;
2511 unsigned int num = *num_iovec;
2512 unsigned int len = 0;
2514 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2515 if (iov[num].iov_base == NULL)
2517 iov[num].iov_len = len;
2518 if (!req->CreateContextsOffset)
2519 req->CreateContextsOffset = cpu_to_le32(
2520 sizeof(struct smb2_create_req) +
2521 iov[num - 1].iov_len);
2522 le32_add_cpu(&req->CreateContextsLength, len);
2523 *num_iovec = num + 1;
2527 static struct crt_query_id_ctxt *
2528 create_query_id_buf(void)
2530 struct crt_query_id_ctxt *buf;
2532 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2536 buf->ccontext.DataOffset = cpu_to_le16(0);
2537 buf->ccontext.DataLength = cpu_to_le32(0);
2538 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2539 (struct crt_query_id_ctxt, Name));
2540 buf->ccontext.NameLength = cpu_to_le16(4);
2541 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2549 /* See MS-SMB2 2.2.13.2.9 */
2551 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2553 struct smb2_create_req *req = iov[0].iov_base;
2554 unsigned int num = *num_iovec;
2556 iov[num].iov_base = create_query_id_buf();
2557 if (iov[num].iov_base == NULL)
2559 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2560 if (!req->CreateContextsOffset)
2561 req->CreateContextsOffset = cpu_to_le32(
2562 sizeof(struct smb2_create_req) +
2563 iov[num - 1].iov_len);
2564 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2565 *num_iovec = num + 1;
2570 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2571 const char *treename, const __le16 *path)
2573 int treename_len, path_len;
2574 struct nls_table *cp;
2575 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2580 treename_len = strlen(treename);
2581 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2587 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2589 /* make room for one path separator only if @path isn't empty */
2590 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2593 * final path needs to be 8-byte aligned as specified in
2594 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2596 *out_size = round_up(*out_len * sizeof(__le16), 8);
2597 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2601 cp = load_nls_default();
2602 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2604 /* Do not append the separator if the path is empty */
2605 if (path[0] != cpu_to_le16(0x0000)) {
2606 UniStrcat(*out_path, sep);
2607 UniStrcat(*out_path, path);
2615 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2616 umode_t mode, struct cifs_tcon *tcon,
2617 const char *full_path,
2618 struct cifs_sb_info *cifs_sb)
2620 struct smb_rqst rqst;
2621 struct smb2_create_req *req;
2622 struct smb2_create_rsp *rsp = NULL;
2623 struct cifs_ses *ses = tcon->ses;
2624 struct kvec iov[3]; /* make sure at least one for each open context */
2625 struct kvec rsp_iov = {NULL, 0};
2628 __le16 *copy_path = NULL;
2631 unsigned int n_iov = 2;
2632 __u32 file_attributes = 0;
2633 char *pc_buf = NULL;
2635 unsigned int total_len;
2636 __le16 *utf16_path = NULL;
2637 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2639 cifs_dbg(FYI, "mkdir\n");
2641 /* resource #1: path allocation */
2642 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2646 if (!ses || !server) {
2651 /* resource #2: request */
2652 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2653 (void **) &req, &total_len);
2658 if (smb3_encryption_required(tcon))
2659 flags |= CIFS_TRANSFORM_REQ;
2661 req->ImpersonationLevel = IL_IMPERSONATION;
2662 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2663 /* File attributes ignored on open (used in create though) */
2664 req->FileAttributes = cpu_to_le32(file_attributes);
2665 req->ShareAccess = FILE_SHARE_ALL_LE;
2666 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2667 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2669 iov[0].iov_base = (char *)req;
2670 /* -1 since last byte is buf[0] which is sent below (path) */
2671 iov[0].iov_len = total_len - 1;
2673 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2675 /* [MS-SMB2] 2.2.13 NameOffset:
2676 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2677 * the SMB2 header, the file name includes a prefix that will
2678 * be processed during DFS name normalization as specified in
2679 * section 3.3.5.9. Otherwise, the file name is relative to
2680 * the share that is identified by the TreeId in the SMB2
2683 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2686 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2687 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2689 tcon->tree_name, utf16_path);
2693 req->NameLength = cpu_to_le16(name_len * 2);
2694 uni_path_len = copy_size;
2695 /* free before overwriting resource */
2697 utf16_path = copy_path;
2699 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2700 /* MUST set path len (NameLength) to 0 opening root of share */
2701 req->NameLength = cpu_to_le16(uni_path_len - 2);
2702 if (uni_path_len % 8 != 0) {
2703 copy_size = roundup(uni_path_len, 8);
2704 copy_path = kzalloc(copy_size, GFP_KERNEL);
2709 memcpy((char *)copy_path, (const char *)utf16_path,
2711 uni_path_len = copy_size;
2712 /* free before overwriting resource */
2714 utf16_path = copy_path;
2718 iov[1].iov_len = uni_path_len;
2719 iov[1].iov_base = utf16_path;
2720 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2722 if (tcon->posix_extensions) {
2723 /* resource #3: posix buf */
2724 rc = add_posix_context(iov, &n_iov, mode);
2727 pc_buf = iov[n_iov-1].iov_base;
2731 memset(&rqst, 0, sizeof(struct smb_rqst));
2733 rqst.rq_nvec = n_iov;
2735 /* no need to inc num_remote_opens because we close it just below */
2736 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2737 FILE_WRITE_ATTRIBUTES);
2738 /* resource #4: response buffer */
2739 rc = cifs_send_recv(xid, ses, server,
2740 &rqst, &resp_buftype, flags, &rsp_iov);
2742 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2743 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2745 FILE_WRITE_ATTRIBUTES, rc);
2746 goto err_free_rsp_buf;
2750 * Although unlikely to be possible for rsp to be null and rc not set,
2751 * adding check below is slightly safer long term (and quiets Coverity
2754 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2761 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2762 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2764 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2766 /* Eventually save off posix specific response info and timestaps */
2769 free_rsp_buf(resp_buftype, rsp);
2772 cifs_small_buf_release(req);
2779 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2780 struct smb_rqst *rqst, __u8 *oplock,
2781 struct cifs_open_parms *oparms, __le16 *path)
2783 struct smb2_create_req *req;
2784 unsigned int n_iov = 2;
2785 __u32 file_attributes = 0;
2788 unsigned int total_len;
2789 struct kvec *iov = rqst->rq_iov;
2793 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2794 (void **) &req, &total_len);
2798 iov[0].iov_base = (char *)req;
2799 /* -1 since last byte is buf[0] which is sent below (path) */
2800 iov[0].iov_len = total_len - 1;
2802 if (oparms->create_options & CREATE_OPTION_READONLY)
2803 file_attributes |= ATTR_READONLY;
2804 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2805 file_attributes |= ATTR_SYSTEM;
2807 req->ImpersonationLevel = IL_IMPERSONATION;
2808 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2809 /* File attributes ignored on open (used in create though) */
2810 req->FileAttributes = cpu_to_le32(file_attributes);
2811 req->ShareAccess = FILE_SHARE_ALL_LE;
2813 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2814 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2815 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2817 /* [MS-SMB2] 2.2.13 NameOffset:
2818 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2819 * the SMB2 header, the file name includes a prefix that will
2820 * be processed during DFS name normalization as specified in
2821 * section 3.3.5.9. Otherwise, the file name is relative to
2822 * the share that is identified by the TreeId in the SMB2
2825 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2828 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2829 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2831 tcon->tree_name, path);
2834 req->NameLength = cpu_to_le16(name_len * 2);
2835 uni_path_len = copy_size;
2838 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2839 /* MUST set path len (NameLength) to 0 opening root of share */
2840 req->NameLength = cpu_to_le16(uni_path_len - 2);
2841 copy_size = round_up(uni_path_len, 8);
2842 copy_path = kzalloc(copy_size, GFP_KERNEL);
2845 memcpy((char *)copy_path, (const char *)path,
2847 uni_path_len = copy_size;
2851 iov[1].iov_len = uni_path_len;
2852 iov[1].iov_base = path;
2854 if ((!server->oplocks) || (tcon->no_lease))
2855 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2857 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2858 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2859 req->RequestedOplockLevel = *oplock;
2860 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2861 (oparms->create_options & CREATE_NOT_FILE))
2862 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2864 rc = add_lease_context(server, iov, &n_iov,
2865 oparms->fid->lease_key, oplock);
2870 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2871 /* need to set Next field of lease context if we request it */
2872 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2873 struct create_context *ccontext =
2874 (struct create_context *)iov[n_iov-1].iov_base;
2876 cpu_to_le32(server->vals->create_lease_size);
2879 rc = add_durable_context(iov, &n_iov, oparms,
2880 tcon->use_persistent);
2885 if (tcon->posix_extensions) {
2887 struct create_context *ccontext =
2888 (struct create_context *)iov[n_iov-1].iov_base;
2890 cpu_to_le32(iov[n_iov-1].iov_len);
2893 rc = add_posix_context(iov, &n_iov, oparms->mode);
2898 if (tcon->snapshot_time) {
2899 cifs_dbg(FYI, "adding snapshot context\n");
2901 struct create_context *ccontext =
2902 (struct create_context *)iov[n_iov-1].iov_base;
2904 cpu_to_le32(iov[n_iov-1].iov_len);
2907 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2912 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2916 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2917 (oparms->mode != ACL_NO_MODE))
2921 oparms->mode = ACL_NO_MODE;
2924 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2929 if (set_owner | set_mode) {
2931 struct create_context *ccontext =
2932 (struct create_context *)iov[n_iov-1].iov_base;
2933 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2936 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2937 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2944 struct create_context *ccontext =
2945 (struct create_context *)iov[n_iov-1].iov_base;
2946 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2948 add_query_id_context(iov, &n_iov);
2950 rqst->rq_nvec = n_iov;
2954 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2955 * All other vectors are freed by kfree().
2958 SMB2_open_free(struct smb_rqst *rqst)
2962 if (rqst && rqst->rq_iov) {
2963 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2964 for (i = 1; i < rqst->rq_nvec; i++)
2965 if (rqst->rq_iov[i].iov_base != smb2_padding)
2966 kfree(rqst->rq_iov[i].iov_base);
2971 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2972 __u8 *oplock, struct smb2_file_all_info *buf,
2973 struct create_posix_rsp *posix,
2974 struct kvec *err_iov, int *buftype)
2976 struct smb_rqst rqst;
2977 struct smb2_create_rsp *rsp = NULL;
2978 struct cifs_tcon *tcon = oparms->tcon;
2979 struct cifs_ses *ses = tcon->ses;
2980 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2981 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2982 struct kvec rsp_iov = {NULL, 0};
2983 int resp_buftype = CIFS_NO_BUFFER;
2987 cifs_dbg(FYI, "create/open\n");
2988 if (!ses || !server)
2991 if (smb3_encryption_required(tcon))
2992 flags |= CIFS_TRANSFORM_REQ;
2994 memset(&rqst, 0, sizeof(struct smb_rqst));
2995 memset(&iov, 0, sizeof(iov));
2997 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2999 rc = SMB2_open_init(tcon, server,
3000 &rqst, oplock, oparms, path);
3004 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
3005 oparms->create_options, oparms->desired_access);
3007 rc = cifs_send_recv(xid, ses, server,
3008 &rqst, &resp_buftype, flags,
3010 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3013 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3014 if (err_iov && rsp) {
3016 *buftype = resp_buftype;
3017 resp_buftype = CIFS_NO_BUFFER;
3020 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3021 oparms->create_options, oparms->desired_access, rc);
3022 if (rc == -EREMCHG) {
3023 pr_warn_once("server share %s deleted\n",
3025 tcon->need_reconnect = true;
3028 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3031 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3032 oparms->create_options, oparms->desired_access);
3034 atomic_inc(&tcon->num_remote_opens);
3035 oparms->fid->persistent_fid = rsp->PersistentFileId;
3036 oparms->fid->volatile_fid = rsp->VolatileFileId;
3037 oparms->fid->access = oparms->desired_access;
3038 #ifdef CONFIG_CIFS_DEBUG2
3039 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3040 #endif /* CIFS_DEBUG2 */
3043 buf->CreationTime = rsp->CreationTime;
3044 buf->LastAccessTime = rsp->LastAccessTime;
3045 buf->LastWriteTime = rsp->LastWriteTime;
3046 buf->ChangeTime = rsp->ChangeTime;
3047 buf->AllocationSize = rsp->AllocationSize;
3048 buf->EndOfFile = rsp->EndofFile;
3049 buf->Attributes = rsp->FileAttributes;
3050 buf->NumberOfLinks = cpu_to_le32(1);
3051 buf->DeletePending = 0;
3055 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
3056 oparms->fid->lease_key, oplock, buf, posix);
3058 SMB2_open_free(&rqst);
3059 free_rsp_buf(resp_buftype, rsp);
3064 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3065 struct smb_rqst *rqst,
3066 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3067 char *in_data, u32 indatalen,
3068 __u32 max_response_size)
3070 struct smb2_ioctl_req *req;
3071 struct kvec *iov = rqst->rq_iov;
3072 unsigned int total_len;
3076 rc = smb2_ioctl_req_init(opcode, tcon, server,
3077 (void **) &req, &total_len);
3083 * indatalen is usually small at a couple of bytes max, so
3084 * just allocate through generic pool
3086 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3088 cifs_small_buf_release(req);
3093 req->CtlCode = cpu_to_le32(opcode);
3094 req->PersistentFileId = persistent_fid;
3095 req->VolatileFileId = volatile_fid;
3097 iov[0].iov_base = (char *)req;
3099 * If no input data, the size of ioctl struct in
3100 * protocol spec still includes a 1 byte data buffer,
3101 * but if input data passed to ioctl, we do not
3102 * want to double count this, so we do not send
3103 * the dummy one byte of data in iovec[0] if sending
3104 * input data (in iovec[1]).
3107 req->InputCount = cpu_to_le32(indatalen);
3108 /* do not set InputOffset if no input data */
3110 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3112 iov[0].iov_len = total_len - 1;
3113 iov[1].iov_base = in_data_buf;
3114 iov[1].iov_len = indatalen;
3117 iov[0].iov_len = total_len;
3120 req->OutputOffset = 0;
3121 req->OutputCount = 0; /* MBZ */
3124 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3125 * We Could increase default MaxOutputResponse, but that could require
3126 * more credits. Windows typically sets this smaller, but for some
3127 * ioctls it may be useful to allow server to send more. No point
3128 * limiting what the server can send as long as fits in one credit
3129 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3130 * to increase this limit up in the future.
3131 * Note that for snapshot queries that servers like Azure expect that
3132 * the first query be minimal size (and just used to get the number/size
3133 * of previous versions) so response size must be specified as EXACTLY
3134 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3135 * of eight bytes. Currently that is the only case where we set max
3136 * response size smaller.
3138 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3139 req->hdr.CreditCharge =
3140 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3141 SMB2_MAX_BUFFER_SIZE));
3142 /* always an FSCTL (for now) */
3143 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3145 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3146 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3147 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3153 SMB2_ioctl_free(struct smb_rqst *rqst)
3156 if (rqst && rqst->rq_iov) {
3157 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3158 for (i = 1; i < rqst->rq_nvec; i++)
3159 if (rqst->rq_iov[i].iov_base != smb2_padding)
3160 kfree(rqst->rq_iov[i].iov_base);
3166 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3169 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3170 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3171 u32 max_out_data_len, char **out_data,
3172 u32 *plen /* returned data len */)
3174 struct smb_rqst rqst;
3175 struct smb2_ioctl_rsp *rsp = NULL;
3176 struct cifs_ses *ses;
3177 struct TCP_Server_Info *server;
3178 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3179 struct kvec rsp_iov = {NULL, 0};
3180 int resp_buftype = CIFS_NO_BUFFER;
3184 cifs_dbg(FYI, "SMB2 IOCTL\n");
3186 if (out_data != NULL)
3189 /* zero out returned data len, in case of error */
3200 server = cifs_pick_channel(ses);
3204 if (smb3_encryption_required(tcon))
3205 flags |= CIFS_TRANSFORM_REQ;
3207 memset(&rqst, 0, sizeof(struct smb_rqst));
3208 memset(&iov, 0, sizeof(iov));
3210 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3212 rc = SMB2_ioctl_init(tcon, server,
3213 &rqst, persistent_fid, volatile_fid, opcode,
3214 in_data, indatalen, max_out_data_len);
3218 rc = cifs_send_recv(xid, ses, server,
3219 &rqst, &resp_buftype, flags,
3221 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3224 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3225 ses->Suid, 0, opcode, rc);
3227 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3228 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3230 } else if (rc == -EINVAL) {
3231 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3232 (opcode != FSCTL_SRV_COPYCHUNK)) {
3233 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3236 } else if (rc == -E2BIG) {
3237 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3238 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3243 /* check if caller wants to look at return data or just return rc */
3244 if ((plen == NULL) || (out_data == NULL))
3248 * Although unlikely to be possible for rsp to be null and rc not set,
3249 * adding check below is slightly safer long term (and quiets Coverity
3257 *plen = le32_to_cpu(rsp->OutputCount);
3259 /* We check for obvious errors in the output buffer length and offset */
3261 goto ioctl_exit; /* server returned no data */
3262 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3263 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3269 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3270 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3271 le32_to_cpu(rsp->OutputOffset));
3277 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3279 if (*out_data == NULL) {
3285 SMB2_ioctl_free(&rqst);
3286 free_rsp_buf(resp_buftype, rsp);
3291 * Individual callers to ioctl worker function follow
3295 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3296 u64 persistent_fid, u64 volatile_fid)
3299 struct compress_ioctl fsctl_input;
3300 char *ret_data = NULL;
3302 fsctl_input.CompressionState =
3303 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3305 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3306 FSCTL_SET_COMPRESSION,
3307 (char *)&fsctl_input /* data input */,
3308 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3309 &ret_data /* out data */, NULL);
3311 cifs_dbg(FYI, "set compression rc %d\n", rc);
3317 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3318 struct smb_rqst *rqst,
3319 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3321 struct smb2_close_req *req;
3322 struct kvec *iov = rqst->rq_iov;
3323 unsigned int total_len;
3326 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3327 (void **) &req, &total_len);
3331 req->PersistentFileId = persistent_fid;
3332 req->VolatileFileId = volatile_fid;
3334 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3337 iov[0].iov_base = (char *)req;
3338 iov[0].iov_len = total_len;
3344 SMB2_close_free(struct smb_rqst *rqst)
3346 if (rqst && rqst->rq_iov)
3347 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3351 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3352 u64 persistent_fid, u64 volatile_fid,
3353 struct smb2_file_network_open_info *pbuf)
3355 struct smb_rqst rqst;
3356 struct smb2_close_rsp *rsp = NULL;
3357 struct cifs_ses *ses = tcon->ses;
3358 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3360 struct kvec rsp_iov;
3361 int resp_buftype = CIFS_NO_BUFFER;
3364 bool query_attrs = false;
3366 cifs_dbg(FYI, "Close\n");
3368 if (!ses || !server)
3371 if (smb3_encryption_required(tcon))
3372 flags |= CIFS_TRANSFORM_REQ;
3374 memset(&rqst, 0, sizeof(struct smb_rqst));
3375 memset(&iov, 0, sizeof(iov));
3379 /* check if need to ask server to return timestamps in close response */
3383 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3384 rc = SMB2_close_init(tcon, server,
3385 &rqst, persistent_fid, volatile_fid,
3390 rc = cifs_send_recv(xid, ses, server,
3391 &rqst, &resp_buftype, flags, &rsp_iov);
3392 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3395 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3396 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3400 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3403 * Note that have to subtract 4 since struct network_open_info
3404 * has a final 4 byte pad that close response does not have
3407 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3410 atomic_dec(&tcon->num_remote_opens);
3412 SMB2_close_free(&rqst);
3413 free_rsp_buf(resp_buftype, rsp);
3415 /* retry close in a worker thread if this one is interrupted */
3416 if (is_interrupt_error(rc)) {
3419 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3422 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3423 persistent_fid, tmp_rc);
3429 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3430 u64 persistent_fid, u64 volatile_fid)
3432 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3436 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3437 struct kvec *iov, unsigned int min_buf_size)
3439 unsigned int smb_len = iov->iov_len;
3440 char *end_of_smb = smb_len + (char *)iov->iov_base;
3441 char *begin_of_buf = offset + (char *)iov->iov_base;
3442 char *end_of_buf = begin_of_buf + buffer_length;
3445 if (buffer_length < min_buf_size) {
3446 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3447 buffer_length, min_buf_size);
3451 /* check if beyond RFC1001 maximum length */
3452 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3453 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3454 buffer_length, smb_len);
3458 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3459 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3467 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3468 * Caller must free buffer.
3471 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3472 struct kvec *iov, unsigned int minbufsize,
3475 char *begin_of_buf = offset + (char *)iov->iov_base;
3481 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3485 memcpy(data, begin_of_buf, minbufsize);
3491 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3492 struct smb_rqst *rqst,
3493 u64 persistent_fid, u64 volatile_fid,
3494 u8 info_class, u8 info_type, u32 additional_info,
3495 size_t output_len, size_t input_len, void *input)
3497 struct smb2_query_info_req *req;
3498 struct kvec *iov = rqst->rq_iov;
3499 unsigned int total_len;
3502 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3503 (void **) &req, &total_len);
3507 req->InfoType = info_type;
3508 req->FileInfoClass = info_class;
3509 req->PersistentFileId = persistent_fid;
3510 req->VolatileFileId = volatile_fid;
3511 req->AdditionalInformation = cpu_to_le32(additional_info);
3513 req->OutputBufferLength = cpu_to_le32(output_len);
3515 req->InputBufferLength = cpu_to_le32(input_len);
3516 /* total_len for smb query request never close to le16 max */
3517 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3518 memcpy(req->Buffer, input, input_len);
3521 iov[0].iov_base = (char *)req;
3523 iov[0].iov_len = total_len - 1 + input_len;
3528 SMB2_query_info_free(struct smb_rqst *rqst)
3530 if (rqst && rqst->rq_iov)
3531 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3535 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3536 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3537 u32 additional_info, size_t output_len, size_t min_len, void **data,
3540 struct smb_rqst rqst;
3541 struct smb2_query_info_rsp *rsp = NULL;
3543 struct kvec rsp_iov;
3545 int resp_buftype = CIFS_NO_BUFFER;
3546 struct cifs_ses *ses = tcon->ses;
3547 struct TCP_Server_Info *server;
3549 bool allocated = false;
3551 cifs_dbg(FYI, "Query Info\n");
3555 server = cifs_pick_channel(ses);
3559 if (smb3_encryption_required(tcon))
3560 flags |= CIFS_TRANSFORM_REQ;
3562 memset(&rqst, 0, sizeof(struct smb_rqst));
3563 memset(&iov, 0, sizeof(iov));
3567 rc = SMB2_query_info_init(tcon, server,
3568 &rqst, persistent_fid, volatile_fid,
3569 info_class, info_type, additional_info,
3570 output_len, 0, NULL);
3574 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3575 ses->Suid, info_class, (__u32)info_type);
3577 rc = cifs_send_recv(xid, ses, server,
3578 &rqst, &resp_buftype, flags, &rsp_iov);
3579 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3582 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3583 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3584 ses->Suid, info_class, (__u32)info_type, rc);
3588 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3589 ses->Suid, info_class, (__u32)info_type);
3592 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3594 *data = kmalloc(*dlen, GFP_KERNEL);
3597 "Error %d allocating memory for acl\n",
3607 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3608 le32_to_cpu(rsp->OutputBufferLength),
3609 &rsp_iov, dlen ? *dlen : min_len, *data);
3610 if (rc && allocated) {
3617 SMB2_query_info_free(&rqst);
3618 free_rsp_buf(resp_buftype, rsp);
3622 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3623 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3625 return query_info(xid, tcon, persistent_fid, volatile_fid,
3626 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3627 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3628 sizeof(struct smb2_file_all_info), (void **)&data,
3633 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3635 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3636 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3638 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3639 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3642 return query_info(xid, tcon, persistent_fid, volatile_fid,
3643 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3644 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3645 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3650 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3651 u64 persistent_fid, u64 volatile_fid,
3652 void **data, u32 *plen, u32 extra_info)
3654 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3658 return query_info(xid, tcon, persistent_fid, volatile_fid,
3659 0, SMB2_O_INFO_SECURITY, additional_info,
3660 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3664 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3665 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3667 return query_info(xid, tcon, persistent_fid, volatile_fid,
3668 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3669 sizeof(struct smb2_file_internal_info),
3670 sizeof(struct smb2_file_internal_info),
3671 (void **)&uniqueid, NULL);
3675 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3676 * See MS-SMB2 2.2.35 and 2.2.36
3680 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3681 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3682 u64 persistent_fid, u64 volatile_fid,
3683 u32 completion_filter, bool watch_tree)
3685 struct smb2_change_notify_req *req;
3686 struct kvec *iov = rqst->rq_iov;
3687 unsigned int total_len;
3690 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3691 (void **) &req, &total_len);
3695 req->PersistentFileId = persistent_fid;
3696 req->VolatileFileId = volatile_fid;
3697 /* See note 354 of MS-SMB2, 64K max */
3698 req->OutputBufferLength =
3699 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3700 req->CompletionFilter = cpu_to_le32(completion_filter);
3702 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3706 iov[0].iov_base = (char *)req;
3707 iov[0].iov_len = total_len;
3713 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3714 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3715 u32 completion_filter, u32 max_out_data_len, char **out_data,
3716 u32 *plen /* returned data len */)
3718 struct cifs_ses *ses = tcon->ses;
3719 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3720 struct smb_rqst rqst;
3721 struct smb2_change_notify_rsp *smb_rsp;
3723 struct kvec rsp_iov = {NULL, 0};
3724 int resp_buftype = CIFS_NO_BUFFER;
3728 cifs_dbg(FYI, "change notify\n");
3729 if (!ses || !server)
3732 if (smb3_encryption_required(tcon))
3733 flags |= CIFS_TRANSFORM_REQ;
3735 memset(&rqst, 0, sizeof(struct smb_rqst));
3736 memset(&iov, 0, sizeof(iov));
3743 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3744 persistent_fid, volatile_fid,
3745 completion_filter, watch_tree);
3749 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3750 (u8)watch_tree, completion_filter);
3751 rc = cifs_send_recv(xid, ses, server,
3752 &rqst, &resp_buftype, flags, &rsp_iov);
3755 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3756 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3757 (u8)watch_tree, completion_filter, rc);
3759 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3760 ses->Suid, (u8)watch_tree, completion_filter);
3761 /* validate that notify information is plausible */
3762 if ((rsp_iov.iov_base == NULL) ||
3763 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp)))
3766 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3768 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3769 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3770 sizeof(struct file_notify_information));
3772 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3773 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3774 if (*out_data == NULL) {
3778 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
3783 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3784 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3791 * This is a no-op for now. We're not really interested in the reply, but
3792 * rather in the fact that the server sent one and that server->lstrp
3795 * FIXME: maybe we should consider checking that the reply matches request?
3798 smb2_echo_callback(struct mid_q_entry *mid)
3800 struct TCP_Server_Info *server = mid->callback_data;
3801 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3802 struct cifs_credits credits = { .value = 0, .instance = 0 };
3804 if (mid->mid_state == MID_RESPONSE_RECEIVED
3805 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3806 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3807 credits.instance = server->reconnect_instance;
3811 add_credits(server, &credits, CIFS_ECHO_OP);
3814 void smb2_reconnect_server(struct work_struct *work)
3816 struct TCP_Server_Info *server = container_of(work,
3817 struct TCP_Server_Info, reconnect.work);
3818 struct TCP_Server_Info *pserver;
3819 struct cifs_ses *ses, *ses2;
3820 struct cifs_tcon *tcon, *tcon2;
3821 struct list_head tmp_list, tmp_ses_list;
3822 bool tcon_exist = false, ses_exist = false;
3823 bool tcon_selected = false;
3825 bool resched = false;
3827 /* If server is a channel, select the primary channel */
3828 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3830 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3831 mutex_lock(&pserver->reconnect_mutex);
3833 INIT_LIST_HEAD(&tmp_list);
3834 INIT_LIST_HEAD(&tmp_ses_list);
3835 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3837 spin_lock(&cifs_tcp_ses_lock);
3838 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3840 tcon_selected = false;
3842 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3843 if (tcon->need_reconnect || tcon->need_reopen_files) {
3845 list_add_tail(&tcon->rlist, &tmp_list);
3846 tcon_selected = tcon_exist = true;
3850 * IPC has the same lifetime as its session and uses its
3853 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3854 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3855 tcon_selected = tcon_exist = true;
3859 * handle the case where channel needs to reconnect
3860 * binding session, but tcon is healthy (some other channel
3863 spin_lock(&ses->chan_lock);
3864 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3865 list_add_tail(&ses->rlist, &tmp_ses_list);
3869 spin_unlock(&ses->chan_lock);
3872 * Get the reference to server struct to be sure that the last call of
3873 * cifs_put_tcon() in the loop below won't release the server pointer.
3875 if (tcon_exist || ses_exist)
3876 server->srv_count++;
3878 spin_unlock(&cifs_tcp_ses_lock);
3880 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3881 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3883 cifs_reopen_persistent_handles(tcon);
3886 list_del_init(&tcon->rlist);
3888 cifs_put_smb_ses(tcon->ses);
3890 cifs_put_tcon(tcon);
3896 /* allocate a dummy tcon struct used for reconnect */
3897 tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
3900 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3901 list_del_init(&ses->rlist);
3902 cifs_put_smb_ses(ses);
3907 tcon->status = TID_GOOD;
3908 tcon->retry = false;
3909 tcon->need_reconnect = false;
3911 /* now reconnect sessions for necessary channels */
3912 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3914 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3917 list_del_init(&ses->rlist);
3918 cifs_put_smb_ses(ses);
3923 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
3925 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3926 mutex_unlock(&pserver->reconnect_mutex);
3928 /* now we can safely release srv struct */
3929 if (tcon_exist || ses_exist)
3930 cifs_put_tcp_session(server, 1);
3934 SMB2_echo(struct TCP_Server_Info *server)
3936 struct smb2_echo_req *req;
3939 struct smb_rqst rqst = { .rq_iov = iov,
3941 unsigned int total_len;
3943 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
3945 spin_lock(&server->srv_lock);
3946 if (server->ops->need_neg &&
3947 server->ops->need_neg(server)) {
3948 spin_unlock(&server->srv_lock);
3949 /* No need to send echo on newly established connections */
3950 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3953 spin_unlock(&server->srv_lock);
3955 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3956 (void **)&req, &total_len);
3960 req->hdr.CreditRequest = cpu_to_le16(1);
3962 iov[0].iov_len = total_len;
3963 iov[0].iov_base = (char *)req;
3965 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3966 server, CIFS_ECHO_OP, NULL);
3968 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3970 cifs_small_buf_release(req);
3975 SMB2_flush_free(struct smb_rqst *rqst)
3977 if (rqst && rqst->rq_iov)
3978 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3982 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3983 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3984 u64 persistent_fid, u64 volatile_fid)
3986 struct smb2_flush_req *req;
3987 struct kvec *iov = rqst->rq_iov;
3988 unsigned int total_len;
3991 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3992 (void **) &req, &total_len);
3996 req->PersistentFileId = persistent_fid;
3997 req->VolatileFileId = volatile_fid;
3999 iov[0].iov_base = (char *)req;
4000 iov[0].iov_len = total_len;
4006 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4009 struct cifs_ses *ses = tcon->ses;
4010 struct smb_rqst rqst;
4012 struct kvec rsp_iov = {NULL, 0};
4013 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4014 int resp_buftype = CIFS_NO_BUFFER;
4018 cifs_dbg(FYI, "flush\n");
4019 if (!ses || !(ses->server))
4022 if (smb3_encryption_required(tcon))
4023 flags |= CIFS_TRANSFORM_REQ;
4025 memset(&rqst, 0, sizeof(struct smb_rqst));
4026 memset(&iov, 0, sizeof(iov));
4030 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4031 persistent_fid, volatile_fid);
4035 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4036 rc = cifs_send_recv(xid, ses, server,
4037 &rqst, &resp_buftype, flags, &rsp_iov);
4040 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4041 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4044 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4048 SMB2_flush_free(&rqst);
4049 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4054 * To form a chain of read requests, any read requests after the first should
4055 * have the end_of_chain boolean set to true.
4058 smb2_new_read_req(void **buf, unsigned int *total_len,
4059 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4060 unsigned int remaining_bytes, int request_type)
4063 struct smb2_read_req *req = NULL;
4064 struct smb2_hdr *shdr;
4065 struct TCP_Server_Info *server = io_parms->server;
4067 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4068 (void **) &req, total_len);
4073 return -ECONNABORTED;
4076 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4078 req->PersistentFileId = io_parms->persistent_fid;
4079 req->VolatileFileId = io_parms->volatile_fid;
4080 req->ReadChannelInfoOffset = 0; /* reserved */
4081 req->ReadChannelInfoLength = 0; /* reserved */
4082 req->Channel = 0; /* reserved */
4083 req->MinimumCount = 0;
4084 req->Length = cpu_to_le32(io_parms->length);
4085 req->Offset = cpu_to_le64(io_parms->offset);
4087 trace_smb3_read_enter(0 /* xid */,
4088 io_parms->persistent_fid,
4089 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4090 io_parms->offset, io_parms->length);
4091 #ifdef CONFIG_CIFS_SMB_DIRECT
4093 * If we want to do a RDMA write, fill in and append
4094 * smbd_buffer_descriptor_v1 to the end of read request
4096 if (server->rdma && rdata && !server->sign &&
4097 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
4099 struct smbd_buffer_descriptor_v1 *v1;
4100 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4102 rdata->mr = smbd_register_mr(
4103 server->smbd_conn, rdata->pages,
4104 rdata->nr_pages, rdata->page_offset,
4105 rdata->tailsz, true, need_invalidate);
4109 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4110 if (need_invalidate)
4111 req->Channel = SMB2_CHANNEL_RDMA_V1;
4112 req->ReadChannelInfoOffset =
4113 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4114 req->ReadChannelInfoLength =
4115 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4116 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4117 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4118 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4119 v1->length = cpu_to_le32(rdata->mr->mr->length);
4121 *total_len += sizeof(*v1) - 1;
4124 if (request_type & CHAINED_REQUEST) {
4125 if (!(request_type & END_OF_CHAIN)) {
4126 /* next 8-byte aligned request */
4127 *total_len = ALIGN(*total_len, 8);
4128 shdr->NextCommand = cpu_to_le32(*total_len);
4129 } else /* END_OF_CHAIN */
4130 shdr->NextCommand = 0;
4131 if (request_type & RELATED_REQUEST) {
4132 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4134 * Related requests use info from previous read request
4137 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4138 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4139 req->PersistentFileId = (u64)-1;
4140 req->VolatileFileId = (u64)-1;
4143 if (remaining_bytes > io_parms->length)
4144 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4146 req->RemainingBytes = 0;
4153 smb2_readv_callback(struct mid_q_entry *mid)
4155 struct cifs_readdata *rdata = mid->callback_data;
4156 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4157 struct TCP_Server_Info *server = rdata->server;
4158 struct smb2_hdr *shdr =
4159 (struct smb2_hdr *)rdata->iov[0].iov_base;
4160 struct cifs_credits credits = { .value = 0, .instance = 0 };
4161 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
4163 .rq_pages = rdata->pages,
4164 .rq_offset = rdata->page_offset,
4165 .rq_npages = rdata->nr_pages,
4166 .rq_pagesz = rdata->pagesz,
4167 .rq_tailsz = rdata->tailsz };
4169 WARN_ONCE(rdata->server != mid->server,
4170 "rdata server %p != mid server %p",
4171 rdata->server, mid->server);
4173 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4174 __func__, mid->mid, mid->mid_state, rdata->result,
4177 switch (mid->mid_state) {
4178 case MID_RESPONSE_RECEIVED:
4179 credits.value = le16_to_cpu(shdr->CreditRequest);
4180 credits.instance = server->reconnect_instance;
4181 /* result already set, check signature */
4182 if (server->sign && !mid->decrypted) {
4185 rc = smb2_verify_signature(&rqst, server);
4187 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4190 /* FIXME: should this be counted toward the initiating task? */
4191 task_io_account_read(rdata->got_bytes);
4192 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4194 case MID_REQUEST_SUBMITTED:
4195 case MID_RETRY_NEEDED:
4196 rdata->result = -EAGAIN;
4197 if (server->sign && rdata->got_bytes)
4198 /* reset bytes number since we can not check a sign */
4199 rdata->got_bytes = 0;
4200 /* FIXME: should this be counted toward the initiating task? */
4201 task_io_account_read(rdata->got_bytes);
4202 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4204 case MID_RESPONSE_MALFORMED:
4205 credits.value = le16_to_cpu(shdr->CreditRequest);
4206 credits.instance = server->reconnect_instance;
4209 rdata->result = -EIO;
4211 #ifdef CONFIG_CIFS_SMB_DIRECT
4213 * If this rdata has a memmory registered, the MR can be freed
4214 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4215 * because they have limited number and are used for future I/Os
4218 smbd_deregister_mr(rdata->mr);
4222 if (rdata->result && rdata->result != -ENODATA) {
4223 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4224 trace_smb3_read_err(0 /* xid */,
4225 rdata->cfile->fid.persistent_fid,
4226 tcon->tid, tcon->ses->Suid, rdata->offset,
4227 rdata->bytes, rdata->result);
4229 trace_smb3_read_done(0 /* xid */,
4230 rdata->cfile->fid.persistent_fid,
4231 tcon->tid, tcon->ses->Suid,
4232 rdata->offset, rdata->got_bytes);
4234 queue_work(cifsiod_wq, &rdata->work);
4236 add_credits(server, &credits, 0);
4239 /* smb2_async_readv - send an async read, and set up mid to handle result */
4241 smb2_async_readv(struct cifs_readdata *rdata)
4245 struct smb2_hdr *shdr;
4246 struct cifs_io_parms io_parms;
4247 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4249 struct TCP_Server_Info *server;
4250 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4251 unsigned int total_len;
4253 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4254 __func__, rdata->offset, rdata->bytes);
4257 rdata->server = cifs_pick_channel(tcon->ses);
4259 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4260 io_parms.server = server = rdata->server;
4261 io_parms.offset = rdata->offset;
4262 io_parms.length = rdata->bytes;
4263 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4264 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4265 io_parms.pid = rdata->pid;
4267 rc = smb2_new_read_req(
4268 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4272 if (smb3_encryption_required(io_parms.tcon))
4273 flags |= CIFS_TRANSFORM_REQ;
4275 rdata->iov[0].iov_base = buf;
4276 rdata->iov[0].iov_len = total_len;
4278 shdr = (struct smb2_hdr *)buf;
4280 if (rdata->credits.value > 0) {
4281 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4282 SMB2_MAX_BUFFER_SIZE));
4283 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4285 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4287 goto async_readv_out;
4289 flags |= CIFS_HAS_CREDITS;
4292 kref_get(&rdata->refcount);
4293 rc = cifs_call_async(server, &rqst,
4294 cifs_readv_receive, smb2_readv_callback,
4295 smb3_handle_read_data, rdata, flags,
4298 kref_put(&rdata->refcount, cifs_readdata_release);
4299 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4300 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4302 io_parms.tcon->ses->Suid,
4303 io_parms.offset, io_parms.length, rc);
4307 cifs_small_buf_release(buf);
4312 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4313 unsigned int *nbytes, char **buf, int *buf_type)
4315 struct smb_rqst rqst;
4316 int resp_buftype, rc;
4317 struct smb2_read_req *req = NULL;
4318 struct smb2_read_rsp *rsp = NULL;
4320 struct kvec rsp_iov;
4321 unsigned int total_len;
4322 int flags = CIFS_LOG_ERROR;
4323 struct cifs_ses *ses = io_parms->tcon->ses;
4325 if (!io_parms->server)
4326 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4329 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4333 if (smb3_encryption_required(io_parms->tcon))
4334 flags |= CIFS_TRANSFORM_REQ;
4336 iov[0].iov_base = (char *)req;
4337 iov[0].iov_len = total_len;
4339 memset(&rqst, 0, sizeof(struct smb_rqst));
4343 rc = cifs_send_recv(xid, ses, io_parms->server,
4344 &rqst, &resp_buftype, flags, &rsp_iov);
4345 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4348 if (rc != -ENODATA) {
4349 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4350 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4351 trace_smb3_read_err(xid,
4352 req->PersistentFileId,
4353 io_parms->tcon->tid, ses->Suid,
4354 io_parms->offset, io_parms->length,
4357 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4358 ses->Suid, io_parms->offset, 0);
4359 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4360 cifs_small_buf_release(req);
4361 return rc == -ENODATA ? 0 : rc;
4363 trace_smb3_read_done(xid,
4364 req->PersistentFileId,
4365 io_parms->tcon->tid, ses->Suid,
4366 io_parms->offset, io_parms->length);
4368 cifs_small_buf_release(req);
4370 *nbytes = le32_to_cpu(rsp->DataLength);
4371 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4372 (*nbytes > io_parms->length)) {
4373 cifs_dbg(FYI, "bad length %d for count %d\n",
4374 *nbytes, io_parms->length);
4380 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4381 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4382 } else if (resp_buftype != CIFS_NO_BUFFER) {
4383 *buf = rsp_iov.iov_base;
4384 if (resp_buftype == CIFS_SMALL_BUFFER)
4385 *buf_type = CIFS_SMALL_BUFFER;
4386 else if (resp_buftype == CIFS_LARGE_BUFFER)
4387 *buf_type = CIFS_LARGE_BUFFER;
4393 * Check the mid_state and signature on received buffer (if any), and queue the
4394 * workqueue completion task.
4397 smb2_writev_callback(struct mid_q_entry *mid)
4399 struct cifs_writedata *wdata = mid->callback_data;
4400 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4401 struct TCP_Server_Info *server = wdata->server;
4402 unsigned int written;
4403 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4404 struct cifs_credits credits = { .value = 0, .instance = 0 };
4406 WARN_ONCE(wdata->server != mid->server,
4407 "wdata server %p != mid server %p",
4408 wdata->server, mid->server);
4410 switch (mid->mid_state) {
4411 case MID_RESPONSE_RECEIVED:
4412 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4413 credits.instance = server->reconnect_instance;
4414 wdata->result = smb2_check_receive(mid, server, 0);
4415 if (wdata->result != 0)
4418 written = le32_to_cpu(rsp->DataLength);
4420 * Mask off high 16 bits when bytes written as returned
4421 * by the server is greater than bytes requested by the
4422 * client. OS/2 servers are known to set incorrect
4425 if (written > wdata->bytes)
4428 if (written < wdata->bytes)
4429 wdata->result = -ENOSPC;
4431 wdata->bytes = written;
4433 case MID_REQUEST_SUBMITTED:
4434 case MID_RETRY_NEEDED:
4435 wdata->result = -EAGAIN;
4437 case MID_RESPONSE_MALFORMED:
4438 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4439 credits.instance = server->reconnect_instance;
4442 wdata->result = -EIO;
4445 #ifdef CONFIG_CIFS_SMB_DIRECT
4447 * If this wdata has a memory registered, the MR can be freed
4448 * The number of MRs available is limited, it's important to recover
4449 * used MR as soon as I/O is finished. Hold MR longer in the later
4450 * I/O process can possibly result in I/O deadlock due to lack of MR
4451 * to send request on I/O retry
4454 smbd_deregister_mr(wdata->mr);
4458 if (wdata->result) {
4459 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4460 trace_smb3_write_err(0 /* no xid */,
4461 wdata->cfile->fid.persistent_fid,
4462 tcon->tid, tcon->ses->Suid, wdata->offset,
4463 wdata->bytes, wdata->result);
4464 if (wdata->result == -ENOSPC)
4465 pr_warn_once("Out of space writing to %s\n",
4468 trace_smb3_write_done(0 /* no xid */,
4469 wdata->cfile->fid.persistent_fid,
4470 tcon->tid, tcon->ses->Suid,
4471 wdata->offset, wdata->bytes);
4473 queue_work(cifsiod_wq, &wdata->work);
4475 add_credits(server, &credits, 0);
4478 /* smb2_async_writev - send an async write, and set up mid to handle result */
4480 smb2_async_writev(struct cifs_writedata *wdata,
4481 void (*release)(struct kref *kref))
4483 int rc = -EACCES, flags = 0;
4484 struct smb2_write_req *req = NULL;
4485 struct smb2_hdr *shdr;
4486 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4487 struct TCP_Server_Info *server = wdata->server;
4489 struct smb_rqst rqst = { };
4490 unsigned int total_len;
4493 server = wdata->server = cifs_pick_channel(tcon->ses);
4495 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4496 (void **) &req, &total_len);
4500 if (smb3_encryption_required(tcon))
4501 flags |= CIFS_TRANSFORM_REQ;
4503 shdr = (struct smb2_hdr *)req;
4504 shdr->Id.SyncId.ProcessId = cpu_to_le32(wdata->cfile->pid);
4506 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4507 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4508 req->WriteChannelInfoOffset = 0;
4509 req->WriteChannelInfoLength = 0;
4511 req->Offset = cpu_to_le64(wdata->offset);
4512 req->DataOffset = cpu_to_le16(
4513 offsetof(struct smb2_write_req, Buffer));
4514 req->RemainingBytes = 0;
4516 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4517 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4518 #ifdef CONFIG_CIFS_SMB_DIRECT
4520 * If we want to do a server RDMA read, fill in and append
4521 * smbd_buffer_descriptor_v1 to the end of write request
4523 if (server->rdma && !server->sign && wdata->bytes >=
4524 server->smbd_conn->rdma_readwrite_threshold) {
4526 struct smbd_buffer_descriptor_v1 *v1;
4527 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4529 wdata->mr = smbd_register_mr(
4530 server->smbd_conn, wdata->pages,
4531 wdata->nr_pages, wdata->page_offset,
4532 wdata->tailsz, false, need_invalidate);
4535 goto async_writev_out;
4538 req->DataOffset = 0;
4539 if (wdata->nr_pages > 1)
4540 req->RemainingBytes =
4542 (wdata->nr_pages - 1) * wdata->pagesz -
4543 wdata->page_offset + wdata->tailsz
4546 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4547 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4548 if (need_invalidate)
4549 req->Channel = SMB2_CHANNEL_RDMA_V1;
4550 req->WriteChannelInfoOffset =
4551 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4552 req->WriteChannelInfoLength =
4553 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4554 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4555 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4556 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4557 v1->length = cpu_to_le32(wdata->mr->mr->length);
4560 iov[0].iov_len = total_len - 1;
4561 iov[0].iov_base = (char *)req;
4565 rqst.rq_pages = wdata->pages;
4566 rqst.rq_offset = wdata->page_offset;
4567 rqst.rq_npages = wdata->nr_pages;
4568 rqst.rq_pagesz = wdata->pagesz;
4569 rqst.rq_tailsz = wdata->tailsz;
4570 #ifdef CONFIG_CIFS_SMB_DIRECT
4572 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4576 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4577 wdata->offset, wdata->bytes);
4579 #ifdef CONFIG_CIFS_SMB_DIRECT
4580 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4582 req->Length = cpu_to_le32(wdata->bytes);
4584 req->Length = cpu_to_le32(wdata->bytes);
4587 if (wdata->credits.value > 0) {
4588 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4589 SMB2_MAX_BUFFER_SIZE));
4590 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4592 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4594 goto async_writev_out;
4596 flags |= CIFS_HAS_CREDITS;
4599 kref_get(&wdata->refcount);
4600 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4601 wdata, flags, &wdata->credits);
4604 trace_smb3_write_err(0 /* no xid */,
4605 req->PersistentFileId,
4606 tcon->tid, tcon->ses->Suid, wdata->offset,
4608 kref_put(&wdata->refcount, release);
4609 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4613 cifs_small_buf_release(req);
4618 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4619 * The length field from io_parms must be at least 1 and indicates a number of
4620 * elements with data to write that begins with position 1 in iov array. All
4621 * data length is specified by count.
4624 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4625 unsigned int *nbytes, struct kvec *iov, int n_vec)
4627 struct smb_rqst rqst;
4629 struct smb2_write_req *req = NULL;
4630 struct smb2_write_rsp *rsp = NULL;
4632 struct kvec rsp_iov;
4634 unsigned int total_len;
4635 struct TCP_Server_Info *server;
4642 if (!io_parms->server)
4643 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4644 server = io_parms->server;
4646 return -ECONNABORTED;
4648 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4649 (void **) &req, &total_len);
4653 if (smb3_encryption_required(io_parms->tcon))
4654 flags |= CIFS_TRANSFORM_REQ;
4656 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4658 req->PersistentFileId = io_parms->persistent_fid;
4659 req->VolatileFileId = io_parms->volatile_fid;
4660 req->WriteChannelInfoOffset = 0;
4661 req->WriteChannelInfoLength = 0;
4663 req->Length = cpu_to_le32(io_parms->length);
4664 req->Offset = cpu_to_le64(io_parms->offset);
4665 req->DataOffset = cpu_to_le16(
4666 offsetof(struct smb2_write_req, Buffer));
4667 req->RemainingBytes = 0;
4669 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4670 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4671 io_parms->offset, io_parms->length);
4673 iov[0].iov_base = (char *)req;
4675 iov[0].iov_len = total_len - 1;
4677 memset(&rqst, 0, sizeof(struct smb_rqst));
4679 rqst.rq_nvec = n_vec + 1;
4681 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4683 &resp_buftype, flags, &rsp_iov);
4684 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4687 trace_smb3_write_err(xid,
4688 req->PersistentFileId,
4689 io_parms->tcon->tid,
4690 io_parms->tcon->ses->Suid,
4691 io_parms->offset, io_parms->length, rc);
4692 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4693 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4695 *nbytes = le32_to_cpu(rsp->DataLength);
4696 trace_smb3_write_done(xid,
4697 req->PersistentFileId,
4698 io_parms->tcon->tid,
4699 io_parms->tcon->ses->Suid,
4700 io_parms->offset, *nbytes);
4703 cifs_small_buf_release(req);
4704 free_rsp_buf(resp_buftype, rsp);
4708 int posix_info_sid_size(const void *beg, const void *end)
4716 subauth = *(u8 *)(beg+1);
4717 if (subauth < 1 || subauth > 15)
4720 total = 1 + 1 + 6 + 4*subauth;
4721 if (beg + total > end)
4727 int posix_info_parse(const void *beg, const void *end,
4728 struct smb2_posix_info_parsed *out)
4732 int owner_len, group_len;
4734 const void *owner_sid;
4735 const void *group_sid;
4738 /* if no end bound given, assume payload to be correct */
4740 const struct smb2_posix_info *p = beg;
4742 end = beg + le32_to_cpu(p->NextEntryOffset);
4743 /* last element will have a 0 offset, pick a sensible bound */
4748 /* check base buf */
4749 if (beg + sizeof(struct smb2_posix_info) > end)
4751 total_len = sizeof(struct smb2_posix_info);
4753 /* check owner sid */
4754 owner_sid = beg + total_len;
4755 owner_len = posix_info_sid_size(owner_sid, end);
4758 total_len += owner_len;
4760 /* check group sid */
4761 group_sid = beg + total_len;
4762 group_len = posix_info_sid_size(group_sid, end);
4765 total_len += group_len;
4767 /* check name len */
4768 if (beg + total_len + 4 > end)
4770 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4771 if (name_len < 1 || name_len > 0xFFFF)
4776 name = beg + total_len;
4777 if (name + name_len > end)
4779 total_len += name_len;
4783 out->size = total_len;
4784 out->name_len = name_len;
4786 memcpy(&out->owner, owner_sid, owner_len);
4787 memcpy(&out->group, group_sid, group_len);
4792 static int posix_info_extra_size(const void *beg, const void *end)
4794 int len = posix_info_parse(beg, end, NULL);
4798 return len - sizeof(struct smb2_posix_info);
4802 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4806 unsigned int entrycount = 0;
4807 unsigned int next_offset = 0;
4809 FILE_DIRECTORY_INFO *dir_info;
4811 if (bufstart == NULL)
4814 entryptr = bufstart;
4817 if (entryptr + next_offset < entryptr ||
4818 entryptr + next_offset > end_of_buf ||
4819 entryptr + next_offset + size > end_of_buf) {
4820 cifs_dbg(VFS, "malformed search entry would overflow\n");
4824 entryptr = entryptr + next_offset;
4825 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4827 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4828 len = posix_info_extra_size(entryptr, end_of_buf);
4830 len = le32_to_cpu(dir_info->FileNameLength);
4833 entryptr + len < entryptr ||
4834 entryptr + len > end_of_buf ||
4835 entryptr + len + size > end_of_buf) {
4836 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4841 *lastentry = entryptr;
4844 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4855 int SMB2_query_directory_init(const unsigned int xid,
4856 struct cifs_tcon *tcon,
4857 struct TCP_Server_Info *server,
4858 struct smb_rqst *rqst,
4859 u64 persistent_fid, u64 volatile_fid,
4860 int index, int info_level)
4862 struct smb2_query_directory_req *req;
4863 unsigned char *bufptr;
4864 __le16 asteriks = cpu_to_le16('*');
4865 unsigned int output_size = CIFSMaxBufSize -
4866 MAX_SMB2_CREATE_RESPONSE_SIZE -
4867 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4868 unsigned int total_len;
4869 struct kvec *iov = rqst->rq_iov;
4872 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4873 (void **) &req, &total_len);
4877 switch (info_level) {
4878 case SMB_FIND_FILE_DIRECTORY_INFO:
4879 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4881 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4882 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4884 case SMB_FIND_FILE_POSIX_INFO:
4885 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4888 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4893 req->FileIndex = cpu_to_le32(index);
4894 req->PersistentFileId = persistent_fid;
4895 req->VolatileFileId = volatile_fid;
4898 bufptr = req->Buffer;
4899 memcpy(bufptr, &asteriks, len);
4901 req->FileNameOffset =
4902 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4903 req->FileNameLength = cpu_to_le16(len);
4905 * BB could be 30 bytes or so longer if we used SMB2 specific
4906 * buffer lengths, but this is safe and close enough.
4908 output_size = min_t(unsigned int, output_size, server->maxBuf);
4909 output_size = min_t(unsigned int, output_size, 2 << 15);
4910 req->OutputBufferLength = cpu_to_le32(output_size);
4912 iov[0].iov_base = (char *)req;
4914 iov[0].iov_len = total_len - 1;
4916 iov[1].iov_base = (char *)(req->Buffer);
4917 iov[1].iov_len = len;
4919 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4920 tcon->ses->Suid, index, output_size);
4925 void SMB2_query_directory_free(struct smb_rqst *rqst)
4927 if (rqst && rqst->rq_iov) {
4928 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4933 smb2_parse_query_directory(struct cifs_tcon *tcon,
4934 struct kvec *rsp_iov,
4936 struct cifs_search_info *srch_inf)
4938 struct smb2_query_directory_rsp *rsp;
4939 size_t info_buf_size;
4943 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4945 switch (srch_inf->info_level) {
4946 case SMB_FIND_FILE_DIRECTORY_INFO:
4947 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4949 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4950 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4952 case SMB_FIND_FILE_POSIX_INFO:
4953 /* note that posix payload are variable size */
4954 info_buf_size = sizeof(struct smb2_posix_info);
4957 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4958 srch_inf->info_level);
4962 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4963 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4966 cifs_tcon_dbg(VFS, "bad info payload");
4970 srch_inf->unicode = true;
4972 if (srch_inf->ntwrk_buf_start) {
4973 if (srch_inf->smallBuf)
4974 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4976 cifs_buf_release(srch_inf->ntwrk_buf_start);
4978 srch_inf->ntwrk_buf_start = (char *)rsp;
4979 srch_inf->srch_entries_start = srch_inf->last_entry =
4980 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4981 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4983 srch_inf->entries_in_buffer = num_entries(
4984 srch_inf->info_level,
4985 srch_inf->srch_entries_start,
4987 &srch_inf->last_entry,
4990 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4991 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4992 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4993 srch_inf->srch_entries_start, srch_inf->last_entry);
4994 if (resp_buftype == CIFS_LARGE_BUFFER)
4995 srch_inf->smallBuf = false;
4996 else if (resp_buftype == CIFS_SMALL_BUFFER)
4997 srch_inf->smallBuf = true;
4999 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5005 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5006 u64 persistent_fid, u64 volatile_fid, int index,
5007 struct cifs_search_info *srch_inf)
5009 struct smb_rqst rqst;
5010 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5011 struct smb2_query_directory_rsp *rsp = NULL;
5012 int resp_buftype = CIFS_NO_BUFFER;
5013 struct kvec rsp_iov;
5015 struct cifs_ses *ses = tcon->ses;
5016 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5019 if (!ses || !(ses->server))
5022 if (smb3_encryption_required(tcon))
5023 flags |= CIFS_TRANSFORM_REQ;
5025 memset(&rqst, 0, sizeof(struct smb_rqst));
5026 memset(&iov, 0, sizeof(iov));
5028 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5030 rc = SMB2_query_directory_init(xid, tcon, server,
5031 &rqst, persistent_fid,
5032 volatile_fid, index,
5033 srch_inf->info_level);
5037 rc = cifs_send_recv(xid, ses, server,
5038 &rqst, &resp_buftype, flags, &rsp_iov);
5039 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5042 if (rc == -ENODATA &&
5043 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5044 trace_smb3_query_dir_done(xid, persistent_fid,
5045 tcon->tid, tcon->ses->Suid, index, 0);
5046 srch_inf->endOfSearch = true;
5049 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5050 tcon->ses->Suid, index, 0, rc);
5051 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5056 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5059 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5060 tcon->ses->Suid, index, 0, rc);
5063 resp_buftype = CIFS_NO_BUFFER;
5065 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5066 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5069 SMB2_query_directory_free(&rqst);
5070 free_rsp_buf(resp_buftype, rsp);
5075 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5076 struct smb_rqst *rqst,
5077 u64 persistent_fid, u64 volatile_fid, u32 pid,
5078 u8 info_class, u8 info_type, u32 additional_info,
5079 void **data, unsigned int *size)
5081 struct smb2_set_info_req *req;
5082 struct kvec *iov = rqst->rq_iov;
5083 unsigned int i, total_len;
5086 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5087 (void **) &req, &total_len);
5091 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5092 req->InfoType = info_type;
5093 req->FileInfoClass = info_class;
5094 req->PersistentFileId = persistent_fid;
5095 req->VolatileFileId = volatile_fid;
5096 req->AdditionalInformation = cpu_to_le32(additional_info);
5099 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
5100 req->BufferLength = cpu_to_le32(*size);
5102 memcpy(req->Buffer, *data, *size);
5105 iov[0].iov_base = (char *)req;
5107 iov[0].iov_len = total_len - 1;
5109 for (i = 1; i < rqst->rq_nvec; i++) {
5110 le32_add_cpu(&req->BufferLength, size[i]);
5111 iov[i].iov_base = (char *)data[i];
5112 iov[i].iov_len = size[i];
5119 SMB2_set_info_free(struct smb_rqst *rqst)
5121 if (rqst && rqst->rq_iov)
5122 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5126 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5127 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5128 u8 info_type, u32 additional_info, unsigned int num,
5129 void **data, unsigned int *size)
5131 struct smb_rqst rqst;
5132 struct smb2_set_info_rsp *rsp = NULL;
5134 struct kvec rsp_iov;
5137 struct cifs_ses *ses = tcon->ses;
5138 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5141 if (!ses || !server)
5147 if (smb3_encryption_required(tcon))
5148 flags |= CIFS_TRANSFORM_REQ;
5150 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5154 memset(&rqst, 0, sizeof(struct smb_rqst));
5158 rc = SMB2_set_info_init(tcon, server,
5159 &rqst, persistent_fid, volatile_fid, pid,
5160 info_class, info_type, additional_info,
5168 rc = cifs_send_recv(xid, ses, server,
5169 &rqst, &resp_buftype, flags,
5171 SMB2_set_info_free(&rqst);
5172 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5175 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5176 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5177 ses->Suid, info_class, (__u32)info_type, rc);
5180 free_rsp_buf(resp_buftype, rsp);
5186 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5187 u64 volatile_fid, u32 pid, __le64 *eof)
5189 struct smb2_file_eof_info info;
5193 info.EndOfFile = *eof;
5196 size = sizeof(struct smb2_file_eof_info);
5198 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5200 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5201 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5202 0, 1, &data, &size);
5206 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5207 u64 persistent_fid, u64 volatile_fid,
5208 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5210 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5211 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5212 1, (void **)&pnntsd, &pacllen);
5216 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5217 u64 persistent_fid, u64 volatile_fid,
5218 struct smb2_file_full_ea_info *buf, int len)
5220 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5221 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5222 0, 1, (void **)&buf, &len);
5226 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5227 const u64 persistent_fid, const u64 volatile_fid,
5230 struct smb_rqst rqst;
5232 struct smb2_oplock_break *req = NULL;
5233 struct cifs_ses *ses = tcon->ses;
5234 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5235 int flags = CIFS_OBREAK_OP;
5236 unsigned int total_len;
5238 struct kvec rsp_iov;
5241 cifs_dbg(FYI, "SMB2_oplock_break\n");
5242 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5243 (void **) &req, &total_len);
5247 if (smb3_encryption_required(tcon))
5248 flags |= CIFS_TRANSFORM_REQ;
5250 req->VolatileFid = volatile_fid;
5251 req->PersistentFid = persistent_fid;
5252 req->OplockLevel = oplock_level;
5253 req->hdr.CreditRequest = cpu_to_le16(1);
5255 flags |= CIFS_NO_RSP_BUF;
5257 iov[0].iov_base = (char *)req;
5258 iov[0].iov_len = total_len;
5260 memset(&rqst, 0, sizeof(struct smb_rqst));
5264 rc = cifs_send_recv(xid, ses, server,
5265 &rqst, &resp_buf_type, flags, &rsp_iov);
5266 cifs_small_buf_release(req);
5269 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5270 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5277 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5278 struct kstatfs *kst)
5280 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5281 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5282 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5283 kst->f_bfree = kst->f_bavail =
5284 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5289 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5290 struct kstatfs *kst)
5292 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5293 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5294 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5295 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5296 kst->f_bavail = kst->f_bfree;
5298 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5299 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5300 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5301 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5302 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5308 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5309 struct TCP_Server_Info *server,
5310 int level, int outbuf_len, u64 persistent_fid,
5314 struct smb2_query_info_req *req;
5315 unsigned int total_len;
5317 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5319 if ((tcon->ses == NULL) || server == NULL)
5322 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5323 (void **) &req, &total_len);
5327 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5328 req->FileInfoClass = level;
5329 req->PersistentFileId = persistent_fid;
5330 req->VolatileFileId = volatile_fid;
5332 req->InputBufferOffset =
5333 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5334 req->OutputBufferLength = cpu_to_le32(
5335 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5337 iov->iov_base = (char *)req;
5338 iov->iov_len = total_len;
5343 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5344 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5346 struct smb_rqst rqst;
5347 struct smb2_query_info_rsp *rsp = NULL;
5349 struct kvec rsp_iov;
5352 struct cifs_ses *ses = tcon->ses;
5353 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5354 FILE_SYSTEM_POSIX_INFO *info = NULL;
5357 rc = build_qfs_info_req(&iov, tcon, server,
5358 FS_POSIX_INFORMATION,
5359 sizeof(FILE_SYSTEM_POSIX_INFO),
5360 persistent_fid, volatile_fid);
5364 if (smb3_encryption_required(tcon))
5365 flags |= CIFS_TRANSFORM_REQ;
5367 memset(&rqst, 0, sizeof(struct smb_rqst));
5371 rc = cifs_send_recv(xid, ses, server,
5372 &rqst, &resp_buftype, flags, &rsp_iov);
5373 cifs_small_buf_release(iov.iov_base);
5375 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5376 goto posix_qfsinf_exit;
5378 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5380 info = (FILE_SYSTEM_POSIX_INFO *)(
5381 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5382 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5383 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5384 sizeof(FILE_SYSTEM_POSIX_INFO));
5386 copy_posix_fs_info_to_kstatfs(info, fsdata);
5389 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5394 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5395 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5397 struct smb_rqst rqst;
5398 struct smb2_query_info_rsp *rsp = NULL;
5400 struct kvec rsp_iov;
5403 struct cifs_ses *ses = tcon->ses;
5404 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5405 struct smb2_fs_full_size_info *info = NULL;
5408 rc = build_qfs_info_req(&iov, tcon, server,
5409 FS_FULL_SIZE_INFORMATION,
5410 sizeof(struct smb2_fs_full_size_info),
5411 persistent_fid, volatile_fid);
5415 if (smb3_encryption_required(tcon))
5416 flags |= CIFS_TRANSFORM_REQ;
5418 memset(&rqst, 0, sizeof(struct smb_rqst));
5422 rc = cifs_send_recv(xid, ses, server,
5423 &rqst, &resp_buftype, flags, &rsp_iov);
5424 cifs_small_buf_release(iov.iov_base);
5426 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5429 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5431 info = (struct smb2_fs_full_size_info *)(
5432 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5433 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5434 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5435 sizeof(struct smb2_fs_full_size_info));
5437 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5440 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5445 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5446 u64 persistent_fid, u64 volatile_fid, int level)
5448 struct smb_rqst rqst;
5449 struct smb2_query_info_rsp *rsp = NULL;
5451 struct kvec rsp_iov;
5453 int resp_buftype, max_len, min_len;
5454 struct cifs_ses *ses = tcon->ses;
5455 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5456 unsigned int rsp_len, offset;
5459 if (level == FS_DEVICE_INFORMATION) {
5460 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5461 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5462 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5463 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5464 min_len = MIN_FS_ATTR_INFO_SIZE;
5465 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5466 max_len = sizeof(struct smb3_fs_ss_info);
5467 min_len = sizeof(struct smb3_fs_ss_info);
5468 } else if (level == FS_VOLUME_INFORMATION) {
5469 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5470 min_len = sizeof(struct smb3_fs_vol_info);
5472 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5476 rc = build_qfs_info_req(&iov, tcon, server,
5478 persistent_fid, volatile_fid);
5482 if (smb3_encryption_required(tcon))
5483 flags |= CIFS_TRANSFORM_REQ;
5485 memset(&rqst, 0, sizeof(struct smb_rqst));
5489 rc = cifs_send_recv(xid, ses, server,
5490 &rqst, &resp_buftype, flags, &rsp_iov);
5491 cifs_small_buf_release(iov.iov_base);
5493 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5496 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5498 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5499 offset = le16_to_cpu(rsp->OutputBufferOffset);
5500 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5504 if (level == FS_ATTRIBUTE_INFORMATION)
5505 memcpy(&tcon->fsAttrInfo, offset
5506 + (char *)rsp, min_t(unsigned int,
5508 else if (level == FS_DEVICE_INFORMATION)
5509 memcpy(&tcon->fsDevInfo, offset
5510 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5511 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5512 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5513 (offset + (char *)rsp);
5514 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5515 tcon->perf_sector_size =
5516 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5517 } else if (level == FS_VOLUME_INFORMATION) {
5518 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5519 (offset + (char *)rsp);
5520 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5521 tcon->vol_create_time = vol_info->VolumeCreationTime;
5525 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5530 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5531 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5532 const __u32 num_lock, struct smb2_lock_element *buf)
5534 struct smb_rqst rqst;
5536 struct smb2_lock_req *req = NULL;
5538 struct kvec rsp_iov;
5541 int flags = CIFS_NO_RSP_BUF;
5542 unsigned int total_len;
5543 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5545 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5547 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5548 (void **) &req, &total_len);
5552 if (smb3_encryption_required(tcon))
5553 flags |= CIFS_TRANSFORM_REQ;
5555 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5556 req->LockCount = cpu_to_le16(num_lock);
5558 req->PersistentFileId = persist_fid;
5559 req->VolatileFileId = volatile_fid;
5561 count = num_lock * sizeof(struct smb2_lock_element);
5563 iov[0].iov_base = (char *)req;
5564 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5565 iov[1].iov_base = (char *)buf;
5566 iov[1].iov_len = count;
5568 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5570 memset(&rqst, 0, sizeof(struct smb_rqst));
5574 rc = cifs_send_recv(xid, tcon->ses, server,
5575 &rqst, &resp_buf_type, flags,
5577 cifs_small_buf_release(req);
5579 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5580 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5581 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5582 tcon->ses->Suid, rc);
5589 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5590 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5591 const __u64 length, const __u64 offset, const __u32 lock_flags,
5594 struct smb2_lock_element lock;
5596 lock.Offset = cpu_to_le64(offset);
5597 lock.Length = cpu_to_le64(length);
5598 lock.Flags = cpu_to_le32(lock_flags);
5599 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5600 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5602 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5606 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5607 __u8 *lease_key, const __le32 lease_state)
5609 struct smb_rqst rqst;
5611 struct smb2_lease_ack *req = NULL;
5612 struct cifs_ses *ses = tcon->ses;
5613 int flags = CIFS_OBREAK_OP;
5614 unsigned int total_len;
5616 struct kvec rsp_iov;
5618 __u64 *please_key_high;
5619 __u64 *please_key_low;
5620 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5622 cifs_dbg(FYI, "SMB2_lease_break\n");
5623 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5624 (void **) &req, &total_len);
5628 if (smb3_encryption_required(tcon))
5629 flags |= CIFS_TRANSFORM_REQ;
5631 req->hdr.CreditRequest = cpu_to_le16(1);
5632 req->StructureSize = cpu_to_le16(36);
5635 memcpy(req->LeaseKey, lease_key, 16);
5636 req->LeaseState = lease_state;
5638 flags |= CIFS_NO_RSP_BUF;
5640 iov[0].iov_base = (char *)req;
5641 iov[0].iov_len = total_len;
5643 memset(&rqst, 0, sizeof(struct smb_rqst));
5647 rc = cifs_send_recv(xid, ses, server,
5648 &rqst, &resp_buf_type, flags, &rsp_iov);
5649 cifs_small_buf_release(req);
5651 please_key_low = (__u64 *)lease_key;
5652 please_key_high = (__u64 *)(lease_key+8);
5654 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5655 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5656 ses->Suid, *please_key_low, *please_key_high, rc);
5657 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5659 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5660 ses->Suid, *please_key_low, *please_key_high);