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 struct smb3_hdr_req *smb3_hdr;
93 shdr->ProtocolId = SMB2_PROTO_NUMBER;
94 shdr->StructureSize = cpu_to_le16(64);
95 shdr->Command = smb2_cmd;
98 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
99 if (server->dialect >= SMB30_PROT_ID) {
100 smb3_hdr = (struct smb3_hdr_req *)shdr;
102 * if primary channel is not set yet, use default
103 * channel for chan sequence num
105 if (SERVER_IS_CHAN(server))
106 smb3_hdr->ChannelSequence =
107 cpu_to_le16(server->primary_server->channel_sequence_num);
109 smb3_hdr->ChannelSequence =
110 cpu_to_le16(server->channel_sequence_num);
112 spin_lock(&server->req_lock);
113 /* Request up to 10 credits but don't go over the limit. */
114 if (server->credits >= server->max_credits)
115 shdr->CreditRequest = cpu_to_le16(0);
117 shdr->CreditRequest = cpu_to_le16(
118 min_t(int, server->max_credits -
119 server->credits, 10));
120 spin_unlock(&server->req_lock);
122 shdr->CreditRequest = cpu_to_le16(2);
124 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
129 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
130 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
131 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
132 shdr->CreditCharge = cpu_to_le16(1);
133 /* else CreditCharge MBZ */
135 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
136 /* Uid is not converted */
138 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
141 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
142 * to pass the path on the Open SMB prefixed by \\server\share.
143 * Not sure when we would need to do the augmented path (if ever) and
144 * setting this flag breaks the SMB2 open operation since it is
145 * illegal to send an empty path name (without \\server\share prefix)
146 * when the DFS flag is set in the SMB open header. We could
147 * consider setting the flag on all operations other than open
148 * but it is safer to net set it for now.
150 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
151 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
153 if (server && server->sign && !smb3_encryption_required(tcon))
154 shdr->Flags |= SMB2_FLAGS_SIGNED;
160 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
161 struct TCP_Server_Info *server)
164 struct nls_table *nls_codepage = NULL;
165 struct cifs_ses *ses;
168 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
169 * check for tcp and smb session status done differently
170 * for those three - in the calling routine.
176 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
177 * cifs_tree_connect().
179 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
182 spin_lock(&tcon->tc_lock);
183 if (tcon->status == TID_EXITING) {
185 * only tree disconnect allowed when disconnecting ...
187 if (smb2_command != SMB2_TREE_DISCONNECT) {
188 spin_unlock(&tcon->tc_lock);
189 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
194 spin_unlock(&tcon->tc_lock);
199 spin_lock(&ses->ses_lock);
200 if (ses->ses_status == SES_EXITING) {
201 spin_unlock(&ses->ses_lock);
204 spin_unlock(&ses->ses_lock);
205 if (!ses->server || !server)
208 spin_lock(&server->srv_lock);
209 if (server->tcpStatus == CifsNeedReconnect) {
211 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
212 * here since they are implicitly done when session drops.
214 switch (smb2_command) {
216 * BB Should we keep oplock break and add flush to exceptions?
218 case SMB2_TREE_DISCONNECT:
221 case SMB2_OPLOCK_BREAK:
222 spin_unlock(&server->srv_lock);
226 spin_unlock(&server->srv_lock);
229 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
233 spin_lock(&ses->chan_lock);
234 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
235 spin_unlock(&ses->chan_lock);
238 spin_unlock(&ses->chan_lock);
239 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
240 tcon->ses->chans_need_reconnect,
241 tcon->need_reconnect);
243 mutex_lock(&ses->session_mutex);
245 * Recheck after acquire mutex. If another thread is negotiating
246 * and the server never sends an answer the socket will be closed
247 * and tcpStatus set to reconnect.
249 spin_lock(&server->srv_lock);
250 if (server->tcpStatus == CifsNeedReconnect) {
251 spin_unlock(&server->srv_lock);
252 mutex_unlock(&ses->session_mutex);
260 spin_unlock(&server->srv_lock);
262 nls_codepage = ses->local_nls;
265 * need to prevent multiple threads trying to simultaneously
266 * reconnect the same SMB session
268 spin_lock(&ses->ses_lock);
269 spin_lock(&ses->chan_lock);
270 if (!cifs_chan_needs_reconnect(ses, server) &&
271 ses->ses_status == SES_GOOD) {
272 spin_unlock(&ses->chan_lock);
273 spin_unlock(&ses->ses_lock);
274 /* this means that we only need to tree connect */
275 if (tcon->need_reconnect)
276 goto skip_sess_setup;
278 mutex_unlock(&ses->session_mutex);
281 spin_unlock(&ses->chan_lock);
282 spin_unlock(&ses->ses_lock);
284 rc = cifs_negotiate_protocol(0, ses, server);
286 rc = cifs_setup_session(0, ses, server, nls_codepage);
287 if ((rc == -EACCES) && !tcon->retry) {
288 mutex_unlock(&ses->session_mutex);
292 mutex_unlock(&ses->session_mutex);
296 mutex_unlock(&ses->session_mutex);
301 if (!tcon->need_reconnect) {
302 mutex_unlock(&ses->session_mutex);
305 cifs_mark_open_files_invalid(tcon);
306 if (tcon->use_persistent)
307 tcon->need_reopen_files = true;
309 rc = cifs_tree_connect(0, tcon, nls_codepage);
310 mutex_unlock(&ses->session_mutex);
312 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
314 /* If sess reconnected but tcon didn't, something strange ... */
315 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
319 if (smb2_command != SMB2_INTERNAL_CMD)
320 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
322 atomic_inc(&tconInfoReconnectCount);
325 * Check if handle based operation so we know whether we can continue
326 * or not without returning to caller to reset file handle.
329 * BB Is flush done by server on drop of tcp session? Should we special
330 * case it and skip above?
332 switch (smb2_command) {
337 case SMB2_QUERY_DIRECTORY:
338 case SMB2_CHANGE_NOTIFY:
339 case SMB2_QUERY_INFO:
348 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
349 struct TCP_Server_Info *server,
351 unsigned int *total_len)
353 struct smb2_pdu *spdu = buf;
354 /* lookup word count ie StructureSize from table */
355 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
358 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
359 * largest operations (Create)
363 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
364 spdu->StructureSize2 = cpu_to_le16(parmsize);
366 *total_len = parmsize + sizeof(struct smb2_hdr);
370 * Allocate and return pointer to an SMB request hdr, and set basic
371 * SMB information in the SMB header. If the return code is zero, this
372 * function must have filled in request_buf pointer.
374 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
375 struct TCP_Server_Info *server,
376 void **request_buf, unsigned int *total_len)
378 /* BB eventually switch this to SMB2 specific small buf size */
379 switch (smb2_command) {
381 case SMB2_QUERY_INFO:
382 *request_buf = cifs_buf_get();
385 *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)
544 unsigned int ctxt_len, neg_context_count;
545 struct TCP_Server_Info *pserver;
549 if (*total_len > 200) {
550 /* In case length corrupted don't want to overrun smb buffer */
551 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
556 * round up total_len of fixed part of SMB3 negotiate request to 8
557 * byte boundary before adding negotiate contexts
559 *total_len = ALIGN(*total_len, 8);
561 pneg_ctxt = (*total_len) + (char *)req;
562 req->NegotiateContextOffset = cpu_to_le32(*total_len);
564 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
565 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
566 *total_len += ctxt_len;
567 pneg_ctxt += ctxt_len;
569 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
570 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
571 *total_len += ctxt_len;
572 pneg_ctxt += ctxt_len;
575 * secondary channels don't have the hostname field populated
576 * use the hostname field in the primary channel instead
578 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
579 cifs_server_lock(pserver);
580 hostname = pserver->hostname;
581 if (hostname && (hostname[0] != 0)) {
582 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
584 *total_len += ctxt_len;
585 pneg_ctxt += ctxt_len;
586 neg_context_count = 3;
588 neg_context_count = 2;
589 cifs_server_unlock(pserver);
591 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
592 *total_len += sizeof(struct smb2_posix_neg_context);
593 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
596 if (server->compress_algorithm) {
597 build_compression_ctxt((struct smb2_compression_capabilities_context *)
599 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
600 *total_len += ctxt_len;
601 pneg_ctxt += ctxt_len;
605 if (enable_negotiate_signing) {
606 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
608 *total_len += ctxt_len;
609 pneg_ctxt += ctxt_len;
613 /* check for and add transport_capabilities and signing capabilities */
614 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
618 /* If invalid preauth context warn but use what we requested, SHA-512 */
619 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
621 unsigned int len = le16_to_cpu(ctxt->DataLength);
624 * Caller checked that DataLength remains within SMB boundary. We still
625 * need to confirm that one HashAlgorithms member is accounted for.
627 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
628 pr_warn_once("server sent bad preauth context\n");
630 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
631 pr_warn_once("server sent invalid SaltLength\n");
634 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
635 pr_warn_once("Invalid SMB3 hash algorithm count\n");
636 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
637 pr_warn_once("unknown SMB3 hash algorithm\n");
640 static void decode_compress_ctx(struct TCP_Server_Info *server,
641 struct smb2_compression_capabilities_context *ctxt)
643 unsigned int len = le16_to_cpu(ctxt->DataLength);
646 * Caller checked that DataLength remains within SMB boundary. We still
647 * need to confirm that one CompressionAlgorithms member is accounted
651 pr_warn_once("server sent bad compression cntxt\n");
654 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
655 pr_warn_once("Invalid SMB3 compress algorithm count\n");
658 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
659 pr_warn_once("unknown compression algorithm\n");
662 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
665 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
666 struct smb2_encryption_neg_context *ctxt)
668 unsigned int len = le16_to_cpu(ctxt->DataLength);
670 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
672 * Caller checked that DataLength remains within SMB boundary. We still
673 * need to confirm that one Cipher flexible array member is accounted
676 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
677 pr_warn_once("server sent bad crypto ctxt len\n");
681 if (le16_to_cpu(ctxt->CipherCount) != 1) {
682 pr_warn_once("Invalid SMB3.11 cipher count\n");
685 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
686 if (require_gcm_256) {
687 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
688 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
691 } else if (ctxt->Ciphers[0] == 0) {
693 * e.g. if server only supported AES256_CCM (very unlikely)
694 * or server supported no encryption types or had all disabled.
695 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
696 * in which mount requested encryption ("seal") checks later
697 * on during tree connection will return proper rc, but if
698 * seal not requested by client, since server is allowed to
699 * return 0 to indicate no supported cipher, we can't fail here
701 server->cipher_type = 0;
702 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
703 pr_warn_once("Server does not support requested encryption types\n");
705 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
706 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
707 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
708 /* server returned a cipher we didn't ask for */
709 pr_warn_once("Invalid SMB3.11 cipher returned\n");
712 server->cipher_type = ctxt->Ciphers[0];
713 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
717 static void decode_signing_ctx(struct TCP_Server_Info *server,
718 struct smb2_signing_capabilities *pctxt)
720 unsigned int len = le16_to_cpu(pctxt->DataLength);
723 * Caller checked that DataLength remains within SMB boundary. We still
724 * need to confirm that one SigningAlgorithms flexible array member is
727 if ((len < 4) || (len > 16)) {
728 pr_warn_once("server sent bad signing negcontext\n");
731 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
732 pr_warn_once("Invalid signing algorithm count\n");
735 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
736 pr_warn_once("unknown signing algorithm\n");
740 server->signing_negotiated = true;
741 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
742 cifs_dbg(FYI, "signing algorithm %d chosen\n",
743 server->signing_algorithm);
747 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
748 struct TCP_Server_Info *server,
749 unsigned int len_of_smb)
751 struct smb2_neg_context *pctx;
752 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
753 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
754 unsigned int len_of_ctxts, i;
757 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
758 if (len_of_smb <= offset) {
759 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
763 len_of_ctxts = len_of_smb - offset;
765 for (i = 0; i < ctxt_cnt; i++) {
767 /* check that offset is not beyond end of SMB */
768 if (len_of_ctxts < sizeof(struct smb2_neg_context))
771 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
772 clen = sizeof(struct smb2_neg_context)
773 + le16_to_cpu(pctx->DataLength);
775 * 2.2.4 SMB2 NEGOTIATE Response
776 * Subsequent negotiate contexts MUST appear at the first 8-byte
777 * aligned offset following the previous negotiate context.
779 if (i + 1 != ctxt_cnt)
780 clen = ALIGN(clen, 8);
781 if (clen > len_of_ctxts)
784 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
785 decode_preauth_context(
786 (struct smb2_preauth_neg_context *)pctx);
787 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
788 rc = decode_encrypt_ctx(server,
789 (struct smb2_encryption_neg_context *)pctx);
790 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
791 decode_compress_ctx(server,
792 (struct smb2_compression_capabilities_context *)pctx);
793 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
794 server->posix_ext_supported = true;
795 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
796 decode_signing_ctx(server,
797 (struct smb2_signing_capabilities *)pctx);
799 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
800 le16_to_cpu(pctx->ContextType));
805 len_of_ctxts -= clen;
810 static struct create_posix *
811 create_posix_buf(umode_t mode)
813 struct create_posix *buf;
815 buf = kzalloc(sizeof(struct create_posix),
820 buf->ccontext.DataOffset =
821 cpu_to_le16(offsetof(struct create_posix, Mode));
822 buf->ccontext.DataLength = cpu_to_le32(4);
823 buf->ccontext.NameOffset =
824 cpu_to_le16(offsetof(struct create_posix, Name));
825 buf->ccontext.NameLength = cpu_to_le16(16);
827 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
838 buf->Name[10] = 0x83;
839 buf->Name[11] = 0xDE;
840 buf->Name[12] = 0x96;
841 buf->Name[13] = 0x8B;
842 buf->Name[14] = 0xCD;
843 buf->Name[15] = 0x7C;
844 buf->Mode = cpu_to_le32(mode);
845 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
850 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
852 unsigned int num = *num_iovec;
854 iov[num].iov_base = create_posix_buf(mode);
855 if (mode == ACL_NO_MODE)
856 cifs_dbg(FYI, "%s: no mode\n", __func__);
857 if (iov[num].iov_base == NULL)
859 iov[num].iov_len = sizeof(struct create_posix);
860 *num_iovec = num + 1;
867 * SMB2 Worker functions follow:
869 * The general structure of the worker functions is:
870 * 1) Call smb2_init (assembles SMB2 header)
871 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
872 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
873 * 4) Decode SMB2 command specific fields in the fixed length area
874 * 5) Decode variable length data area (if any for this SMB2 command type)
875 * 6) Call free smb buffer
881 SMB2_negotiate(const unsigned int xid,
882 struct cifs_ses *ses,
883 struct TCP_Server_Info *server)
885 struct smb_rqst rqst;
886 struct smb2_negotiate_req *req;
887 struct smb2_negotiate_rsp *rsp;
892 int blob_offset, blob_length;
894 int flags = CIFS_NEG_OP;
895 unsigned int total_len;
897 cifs_dbg(FYI, "Negotiate protocol\n");
900 WARN(1, "%s: server is NULL!\n", __func__);
904 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
905 (void **) &req, &total_len);
909 req->hdr.SessionId = 0;
911 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
912 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
914 if (strcmp(server->vals->version_string,
915 SMB3ANY_VERSION_STRING) == 0) {
916 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
917 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
918 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
919 req->DialectCount = cpu_to_le16(3);
921 } else if (strcmp(server->vals->version_string,
922 SMBDEFAULT_VERSION_STRING) == 0) {
923 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
924 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
925 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
926 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
927 req->DialectCount = cpu_to_le16(4);
930 /* otherwise send specific dialect */
931 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
932 req->DialectCount = cpu_to_le16(1);
936 /* only one of SMB2 signing flags may be set in SMB2 request */
938 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
939 else if (global_secflags & CIFSSEC_MAY_SIGN)
940 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
942 req->SecurityMode = 0;
944 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
945 if (ses->chan_max > 1)
946 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
948 /* ClientGUID must be zero for SMB2.02 dialect */
949 if (server->vals->protocol_id == SMB20_PROT_ID)
950 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
952 memcpy(req->ClientGUID, server->client_guid,
953 SMB2_CLIENT_GUID_SIZE);
954 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
955 (strcmp(server->vals->version_string,
956 SMB3ANY_VERSION_STRING) == 0) ||
957 (strcmp(server->vals->version_string,
958 SMBDEFAULT_VERSION_STRING) == 0))
959 assemble_neg_contexts(req, server, &total_len);
961 iov[0].iov_base = (char *)req;
962 iov[0].iov_len = total_len;
964 memset(&rqst, 0, sizeof(struct smb_rqst));
968 rc = cifs_send_recv(xid, ses, server,
969 &rqst, &resp_buftype, flags, &rsp_iov);
970 cifs_small_buf_release(req);
971 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
973 * No tcon so can't do
974 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
976 if (rc == -EOPNOTSUPP) {
977 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");
983 if (strcmp(server->vals->version_string,
984 SMB3ANY_VERSION_STRING) == 0) {
985 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
987 "SMB2 dialect returned but not requested\n");
989 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
991 "SMB2.1 dialect returned but not requested\n");
993 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
994 /* ops set to 3.0 by default for default so update */
995 server->ops = &smb311_operations;
996 server->vals = &smb311_values;
998 } else if (strcmp(server->vals->version_string,
999 SMBDEFAULT_VERSION_STRING) == 0) {
1000 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1001 cifs_server_dbg(VFS,
1002 "SMB2 dialect returned but not requested\n");
1004 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1005 /* ops set to 3.0 by default for default so update */
1006 server->ops = &smb21_operations;
1007 server->vals = &smb21_values;
1008 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1009 server->ops = &smb311_operations;
1010 server->vals = &smb311_values;
1012 } else if (le16_to_cpu(rsp->DialectRevision) !=
1013 server->vals->protocol_id) {
1014 /* if requested single dialect ensure returned dialect matched */
1015 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1016 le16_to_cpu(rsp->DialectRevision));
1020 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1022 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1023 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1024 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1025 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1026 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1027 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1028 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1029 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1030 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1031 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1033 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1034 le16_to_cpu(rsp->DialectRevision));
1039 server->dialect = le16_to_cpu(rsp->DialectRevision);
1042 * Keep a copy of the hash after negprot. This hash will be
1043 * the starting hash value for all sessions made from this
1046 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1047 SMB2_PREAUTH_HASH_SIZE);
1049 /* SMB2 only has an extended negflavor */
1050 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1051 /* set it to the maximum buffer size value we can send with 1 credit */
1052 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1053 SMB2_MAX_BUFFER_SIZE);
1054 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1055 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1056 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1057 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1058 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1060 server->capabilities = le32_to_cpu(rsp->Capabilities);
1061 /* Internal types */
1062 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1065 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1066 * Set the cipher type manually.
1068 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1069 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1071 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1072 (struct smb2_hdr *)rsp);
1074 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1076 * ses->sectype = RawNTLMSSP;
1077 * but for time being this is our only auth choice so doesn't matter.
1078 * We just found a server which sets blob length to zero expecting raw.
1080 if (blob_length == 0) {
1081 cifs_dbg(FYI, "missing security blob on negprot\n");
1082 server->sec_ntlmssp = true;
1085 rc = cifs_enable_signing(server, ses->sign);
1089 rc = decode_negTokenInit(security_blob, blob_length, server);
1096 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1097 if (rsp->NegotiateContextCount)
1098 rc = smb311_decode_neg_context(rsp, server,
1101 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1104 free_rsp_buf(resp_buftype, rsp);
1108 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1111 struct validate_negotiate_info_req *pneg_inbuf;
1112 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1114 u32 inbuflen; /* max of 4 dialects */
1115 struct TCP_Server_Info *server = tcon->ses->server;
1117 cifs_dbg(FYI, "validate negotiate\n");
1119 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1120 if (server->dialect == SMB311_PROT_ID)
1124 * validation ioctl must be signed, so no point sending this if we
1125 * can not sign it (ie are not known user). Even if signing is not
1126 * required (enabled but not negotiated), in those cases we selectively
1127 * sign just this, the first and only signed request on a connection.
1128 * Having validation of negotiate info helps reduce attack vectors.
1130 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1131 return 0; /* validation requires signing */
1133 if (tcon->ses->user_name == NULL) {
1134 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1135 return 0; /* validation requires signing */
1138 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1139 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1141 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1145 pneg_inbuf->Capabilities =
1146 cpu_to_le32(server->vals->req_capabilities);
1147 if (tcon->ses->chan_max > 1)
1148 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1150 memcpy(pneg_inbuf->Guid, server->client_guid,
1151 SMB2_CLIENT_GUID_SIZE);
1153 if (tcon->ses->sign)
1154 pneg_inbuf->SecurityMode =
1155 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1156 else if (global_secflags & CIFSSEC_MAY_SIGN)
1157 pneg_inbuf->SecurityMode =
1158 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1160 pneg_inbuf->SecurityMode = 0;
1163 if (strcmp(server->vals->version_string,
1164 SMB3ANY_VERSION_STRING) == 0) {
1165 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1166 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1167 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1168 pneg_inbuf->DialectCount = cpu_to_le16(3);
1169 /* SMB 2.1 not included so subtract one dialect from len */
1170 inbuflen = sizeof(*pneg_inbuf) -
1171 (sizeof(pneg_inbuf->Dialects[0]));
1172 } else if (strcmp(server->vals->version_string,
1173 SMBDEFAULT_VERSION_STRING) == 0) {
1174 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1175 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1176 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1177 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1178 pneg_inbuf->DialectCount = cpu_to_le16(4);
1179 /* structure is big enough for 4 dialects */
1180 inbuflen = sizeof(*pneg_inbuf);
1182 /* otherwise specific dialect was requested */
1183 pneg_inbuf->Dialects[0] =
1184 cpu_to_le16(server->vals->protocol_id);
1185 pneg_inbuf->DialectCount = cpu_to_le16(1);
1186 /* structure is big enough for 4 dialects, sending only 1 */
1187 inbuflen = sizeof(*pneg_inbuf) -
1188 sizeof(pneg_inbuf->Dialects[0]) * 3;
1191 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1192 FSCTL_VALIDATE_NEGOTIATE_INFO,
1193 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1194 (char **)&pneg_rsp, &rsplen);
1195 if (rc == -EOPNOTSUPP) {
1197 * Old Windows versions or Netapp SMB server can return
1198 * not supported error. Client should accept it.
1200 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1202 goto out_free_inbuf;
1203 } else if (rc != 0) {
1204 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1207 goto out_free_inbuf;
1211 if (rsplen != sizeof(*pneg_rsp)) {
1212 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1215 /* relax check since Mac returns max bufsize allowed on ioctl */
1216 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1220 /* check validate negotiate info response matches what we got earlier */
1221 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1224 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1227 /* do not validate server guid because not saved at negprot time yet */
1229 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1230 SMB2_LARGE_FILES) != server->capabilities)
1233 /* validate negotiate successful */
1235 cifs_dbg(FYI, "validate negotiate info successful\n");
1239 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1248 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1250 switch (requested) {
1257 if (server->sec_ntlmssp &&
1258 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1260 if ((server->sec_kerberos || server->sec_mskerberos) &&
1261 (global_secflags & CIFSSEC_MAY_KRB5))
1269 struct SMB2_sess_data {
1271 struct cifs_ses *ses;
1272 struct TCP_Server_Info *server;
1273 struct nls_table *nls_cp;
1274 void (*func)(struct SMB2_sess_data *);
1276 u64 previous_session;
1278 /* we will send the SMB in three pieces:
1279 * a fixed length beginning part, an optional
1280 * SPNEGO blob (which can be zero length), and a
1281 * last part which will include the strings
1282 * and rest of bcc area. This allows us to avoid
1283 * a large buffer 17K allocation
1290 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1293 struct cifs_ses *ses = sess_data->ses;
1294 struct TCP_Server_Info *server = sess_data->server;
1295 struct smb2_sess_setup_req *req;
1296 unsigned int total_len;
1297 bool is_binding = false;
1299 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1305 spin_lock(&ses->ses_lock);
1306 is_binding = (ses->ses_status == SES_GOOD);
1307 spin_unlock(&ses->ses_lock);
1310 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1311 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1312 req->PreviousSessionId = 0;
1313 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1314 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1316 /* First session, not a reauthenticate */
1317 req->hdr.SessionId = 0;
1319 * if reconnect, we need to send previous sess id
1322 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1323 req->Flags = 0; /* MBZ */
1324 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1325 sess_data->previous_session);
1328 /* enough to enable echos and oplocks and one max size write */
1329 if (server->credits >= server->max_credits)
1330 req->hdr.CreditRequest = cpu_to_le16(0);
1332 req->hdr.CreditRequest = cpu_to_le16(
1333 min_t(int, server->max_credits -
1334 server->credits, 130));
1336 /* only one of SMB2 signing flags may be set in SMB2 request */
1338 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1339 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1340 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1342 req->SecurityMode = 0;
1344 #ifdef CONFIG_CIFS_DFS_UPCALL
1345 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1347 req->Capabilities = 0;
1348 #endif /* DFS_UPCALL */
1350 req->Channel = 0; /* MBZ */
1352 sess_data->iov[0].iov_base = (char *)req;
1354 sess_data->iov[0].iov_len = total_len - 1;
1356 * This variable will be used to clear the buffer
1357 * allocated above in case of any error in the calling function.
1359 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1365 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1367 struct kvec *iov = sess_data->iov;
1369 /* iov[1] is already freed by caller */
1370 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1371 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1373 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1374 sess_data->buf0_type = CIFS_NO_BUFFER;
1378 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1381 struct smb_rqst rqst;
1382 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1383 struct kvec rsp_iov = { NULL, 0 };
1385 /* Testing shows that buffer offset must be at location of Buffer[0] */
1386 req->SecurityBufferOffset =
1387 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1388 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1390 memset(&rqst, 0, sizeof(struct smb_rqst));
1391 rqst.rq_iov = sess_data->iov;
1394 /* BB add code to build os and lm fields */
1395 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1398 &sess_data->buf0_type,
1399 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1400 cifs_small_buf_release(sess_data->iov[0].iov_base);
1401 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1407 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1410 struct cifs_ses *ses = sess_data->ses;
1411 struct TCP_Server_Info *server = sess_data->server;
1413 cifs_server_lock(server);
1414 if (server->ops->generate_signingkey) {
1415 rc = server->ops->generate_signingkey(ses, server);
1418 "SMB3 session key generation failed\n");
1419 cifs_server_unlock(server);
1423 if (!server->session_estab) {
1424 server->sequence_number = 0x2;
1425 server->session_estab = true;
1427 cifs_server_unlock(server);
1429 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1433 #ifdef CONFIG_CIFS_UPCALL
1435 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1438 struct cifs_ses *ses = sess_data->ses;
1439 struct TCP_Server_Info *server = sess_data->server;
1440 struct cifs_spnego_msg *msg;
1441 struct key *spnego_key = NULL;
1442 struct smb2_sess_setup_rsp *rsp = NULL;
1443 bool is_binding = false;
1445 rc = SMB2_sess_alloc_buffer(sess_data);
1449 spnego_key = cifs_get_spnego_key(ses, server);
1450 if (IS_ERR(spnego_key)) {
1451 rc = PTR_ERR(spnego_key);
1453 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1458 msg = spnego_key->payload.data[0];
1460 * check version field to make sure that cifs.upcall is
1461 * sending us a response in an expected form
1463 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1464 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1465 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1467 goto out_put_spnego_key;
1470 spin_lock(&ses->ses_lock);
1471 is_binding = (ses->ses_status == SES_GOOD);
1472 spin_unlock(&ses->ses_lock);
1474 /* keep session key if binding */
1476 kfree_sensitive(ses->auth_key.response);
1477 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1479 if (!ses->auth_key.response) {
1480 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1483 goto out_put_spnego_key;
1485 ses->auth_key.len = msg->sesskey_len;
1488 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1489 sess_data->iov[1].iov_len = msg->secblob_len;
1491 rc = SMB2_sess_sendreceive(sess_data);
1493 goto out_put_spnego_key;
1495 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1496 /* keep session id and flags if binding */
1498 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1499 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1502 rc = SMB2_sess_establish_session(sess_data);
1504 key_invalidate(spnego_key);
1505 key_put(spnego_key);
1507 kfree_sensitive(ses->auth_key.response);
1508 ses->auth_key.response = NULL;
1509 ses->auth_key.len = 0;
1512 sess_data->result = rc;
1513 sess_data->func = NULL;
1514 SMB2_sess_free_buffer(sess_data);
1518 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1520 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1521 sess_data->result = -EOPNOTSUPP;
1522 sess_data->func = NULL;
1527 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1530 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1533 struct cifs_ses *ses = sess_data->ses;
1534 struct TCP_Server_Info *server = sess_data->server;
1535 struct smb2_sess_setup_rsp *rsp = NULL;
1536 unsigned char *ntlmssp_blob = NULL;
1537 bool use_spnego = false; /* else use raw ntlmssp */
1538 u16 blob_length = 0;
1539 bool is_binding = false;
1542 * If memory allocation is successful, caller of this function
1545 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1546 if (!ses->ntlmssp) {
1550 ses->ntlmssp->sesskey_per_smbsess = true;
1552 rc = SMB2_sess_alloc_buffer(sess_data);
1556 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1557 &blob_length, ses, server,
1563 /* BB eventually need to add this */
1564 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1568 sess_data->iov[1].iov_base = ntlmssp_blob;
1569 sess_data->iov[1].iov_len = blob_length;
1571 rc = SMB2_sess_sendreceive(sess_data);
1572 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1574 /* If true, rc here is expected and not an error */
1575 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1576 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1582 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1583 le16_to_cpu(rsp->SecurityBufferOffset)) {
1584 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1585 le16_to_cpu(rsp->SecurityBufferOffset));
1589 rc = decode_ntlmssp_challenge(rsp->Buffer,
1590 le16_to_cpu(rsp->SecurityBufferLength), ses);
1594 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1596 spin_lock(&ses->ses_lock);
1597 is_binding = (ses->ses_status == SES_GOOD);
1598 spin_unlock(&ses->ses_lock);
1600 /* keep existing ses id and flags if binding */
1602 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1603 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1607 kfree_sensitive(ntlmssp_blob);
1608 SMB2_sess_free_buffer(sess_data);
1610 sess_data->result = 0;
1611 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1615 kfree_sensitive(ses->ntlmssp);
1616 ses->ntlmssp = NULL;
1617 sess_data->result = rc;
1618 sess_data->func = NULL;
1622 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1625 struct cifs_ses *ses = sess_data->ses;
1626 struct TCP_Server_Info *server = sess_data->server;
1627 struct smb2_sess_setup_req *req;
1628 struct smb2_sess_setup_rsp *rsp = NULL;
1629 unsigned char *ntlmssp_blob = NULL;
1630 bool use_spnego = false; /* else use raw ntlmssp */
1631 u16 blob_length = 0;
1632 bool is_binding = false;
1634 rc = SMB2_sess_alloc_buffer(sess_data);
1638 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1639 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1641 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1645 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1650 /* BB eventually need to add this */
1651 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1655 sess_data->iov[1].iov_base = ntlmssp_blob;
1656 sess_data->iov[1].iov_len = blob_length;
1658 rc = SMB2_sess_sendreceive(sess_data);
1662 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1664 spin_lock(&ses->ses_lock);
1665 is_binding = (ses->ses_status == SES_GOOD);
1666 spin_unlock(&ses->ses_lock);
1668 /* keep existing ses id and flags if binding */
1670 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1671 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1674 rc = SMB2_sess_establish_session(sess_data);
1675 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1676 if (ses->server->dialect < SMB30_PROT_ID) {
1677 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1679 * The session id is opaque in terms of endianness, so we can't
1680 * print it as a long long. we dump it as we got it on the wire
1682 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1684 cifs_dbg(VFS, "Session Key %*ph\n",
1685 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1686 cifs_dbg(VFS, "Signing Key %*ph\n",
1687 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1691 kfree_sensitive(ntlmssp_blob);
1692 SMB2_sess_free_buffer(sess_data);
1693 kfree_sensitive(ses->ntlmssp);
1694 ses->ntlmssp = NULL;
1695 sess_data->result = rc;
1696 sess_data->func = NULL;
1700 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1703 struct cifs_ses *ses = sess_data->ses;
1704 struct TCP_Server_Info *server = sess_data->server;
1706 type = smb2_select_sectype(server, ses->sectype);
1707 cifs_dbg(FYI, "sess setup type %d\n", type);
1708 if (type == Unspecified) {
1709 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1715 sess_data->func = SMB2_auth_kerberos;
1718 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1721 cifs_dbg(VFS, "secType %d not supported!\n", type);
1729 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1730 struct TCP_Server_Info *server,
1731 const struct nls_table *nls_cp)
1734 struct SMB2_sess_data *sess_data;
1736 cifs_dbg(FYI, "Session Setup\n");
1739 WARN(1, "%s: server is NULL!\n", __func__);
1743 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1747 sess_data->xid = xid;
1748 sess_data->ses = ses;
1749 sess_data->server = server;
1750 sess_data->buf0_type = CIFS_NO_BUFFER;
1751 sess_data->nls_cp = (struct nls_table *) nls_cp;
1752 sess_data->previous_session = ses->Suid;
1754 rc = SMB2_select_sec(sess_data);
1759 * Initialize the session hash with the server one.
1761 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1762 SMB2_PREAUTH_HASH_SIZE);
1764 while (sess_data->func)
1765 sess_data->func(sess_data);
1767 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1768 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1769 rc = sess_data->result;
1771 kfree_sensitive(sess_data);
1776 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1778 struct smb_rqst rqst;
1779 struct smb2_logoff_req *req; /* response is also trivial struct */
1781 struct TCP_Server_Info *server;
1783 unsigned int total_len;
1785 struct kvec rsp_iov;
1788 cifs_dbg(FYI, "disconnect session %p\n", ses);
1790 if (ses && (ses->server))
1791 server = ses->server;
1795 /* no need to send SMB logoff if uid already closed due to reconnect */
1796 spin_lock(&ses->chan_lock);
1797 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1798 spin_unlock(&ses->chan_lock);
1799 goto smb2_session_already_dead;
1801 spin_unlock(&ses->chan_lock);
1803 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1804 (void **) &req, &total_len);
1808 /* since no tcon, smb2_init can not do this, so do here */
1809 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1811 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1812 flags |= CIFS_TRANSFORM_REQ;
1813 else if (server->sign)
1814 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1816 flags |= CIFS_NO_RSP_BUF;
1818 iov[0].iov_base = (char *)req;
1819 iov[0].iov_len = total_len;
1821 memset(&rqst, 0, sizeof(struct smb_rqst));
1825 rc = cifs_send_recv(xid, ses, ses->server,
1826 &rqst, &resp_buf_type, flags, &rsp_iov);
1827 cifs_small_buf_release(req);
1829 * No tcon so can't do
1830 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1833 smb2_session_already_dead:
1837 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1839 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1842 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1844 /* These are similar values to what Windows uses */
1845 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1847 tcon->max_chunks = 256;
1848 tcon->max_bytes_chunk = 1048576;
1849 tcon->max_bytes_copy = 16777216;
1853 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1854 struct cifs_tcon *tcon, const struct nls_table *cp)
1856 struct smb_rqst rqst;
1857 struct smb2_tree_connect_req *req;
1858 struct smb2_tree_connect_rsp *rsp = NULL;
1860 struct kvec rsp_iov = { NULL, 0 };
1864 __le16 *unc_path = NULL;
1866 unsigned int total_len;
1867 struct TCP_Server_Info *server;
1869 /* always use master channel */
1870 server = ses->server;
1872 cifs_dbg(FYI, "TCON\n");
1874 if (!server || !tree)
1877 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1878 if (unc_path == NULL)
1881 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
1882 if (unc_path_len <= 0) {
1888 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1890 atomic_set(&tcon->num_remote_opens, 0);
1891 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1892 (void **) &req, &total_len);
1898 if (smb3_encryption_required(tcon))
1899 flags |= CIFS_TRANSFORM_REQ;
1901 iov[0].iov_base = (char *)req;
1903 iov[0].iov_len = total_len - 1;
1905 /* Testing shows that buffer offset must be at location of Buffer[0] */
1906 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
1907 req->PathLength = cpu_to_le16(unc_path_len);
1908 iov[1].iov_base = unc_path;
1909 iov[1].iov_len = unc_path_len;
1912 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1913 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1914 * (Samba servers don't always set the flag so also check if null user)
1916 if ((server->dialect == SMB311_PROT_ID) &&
1917 !smb3_encryption_required(tcon) &&
1918 !(ses->session_flags &
1919 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1920 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1921 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1923 memset(&rqst, 0, sizeof(struct smb_rqst));
1927 /* Need 64 for max size write so ask for more in case not there yet */
1928 if (server->credits >= server->max_credits)
1929 req->hdr.CreditRequest = cpu_to_le16(0);
1931 req->hdr.CreditRequest = cpu_to_le16(
1932 min_t(int, server->max_credits -
1933 server->credits, 64));
1935 rc = cifs_send_recv(xid, ses, server,
1936 &rqst, &resp_buftype, flags, &rsp_iov);
1937 cifs_small_buf_release(req);
1938 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1939 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1940 if ((rc != 0) || (rsp == NULL)) {
1941 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1942 tcon->need_reconnect = true;
1943 goto tcon_error_exit;
1946 switch (rsp->ShareType) {
1947 case SMB2_SHARE_TYPE_DISK:
1948 cifs_dbg(FYI, "connection to disk share\n");
1950 case SMB2_SHARE_TYPE_PIPE:
1952 cifs_dbg(FYI, "connection to pipe share\n");
1954 case SMB2_SHARE_TYPE_PRINT:
1956 cifs_dbg(FYI, "connection to printer\n");
1959 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1961 goto tcon_error_exit;
1964 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1965 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1966 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1967 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
1968 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
1970 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1971 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1972 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1975 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1976 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1978 init_copy_chunk_defaults(tcon);
1979 if (server->ops->validate_negotiate)
1980 rc = server->ops->validate_negotiate(xid, tcon);
1981 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
1982 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
1983 server->nosharesock = true;
1986 free_rsp_buf(resp_buftype, rsp);
1991 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
1992 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1997 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1999 struct smb_rqst rqst;
2000 struct smb2_tree_disconnect_req *req; /* response is trivial */
2002 struct cifs_ses *ses = tcon->ses;
2004 unsigned int total_len;
2006 struct kvec rsp_iov;
2009 cifs_dbg(FYI, "Tree Disconnect\n");
2011 if (!ses || !(ses->server))
2014 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2015 spin_lock(&ses->chan_lock);
2016 if ((tcon->need_reconnect) ||
2017 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2018 spin_unlock(&ses->chan_lock);
2021 spin_unlock(&ses->chan_lock);
2023 invalidate_all_cached_dirs(tcon);
2025 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
2031 if (smb3_encryption_required(tcon))
2032 flags |= CIFS_TRANSFORM_REQ;
2034 flags |= CIFS_NO_RSP_BUF;
2036 iov[0].iov_base = (char *)req;
2037 iov[0].iov_len = total_len;
2039 memset(&rqst, 0, sizeof(struct smb_rqst));
2043 rc = cifs_send_recv(xid, ses, ses->server,
2044 &rqst, &resp_buf_type, flags, &rsp_iov);
2045 cifs_small_buf_release(req);
2047 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2048 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2050 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2056 static struct create_durable *
2057 create_durable_buf(void)
2059 struct create_durable *buf;
2061 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2065 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2066 (struct create_durable, Data));
2067 buf->ccontext.DataLength = cpu_to_le32(16);
2068 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2069 (struct create_durable, Name));
2070 buf->ccontext.NameLength = cpu_to_le16(4);
2071 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2079 static struct create_durable *
2080 create_reconnect_durable_buf(struct cifs_fid *fid)
2082 struct create_durable *buf;
2084 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2088 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2089 (struct create_durable, Data));
2090 buf->ccontext.DataLength = cpu_to_le32(16);
2091 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2092 (struct create_durable, Name));
2093 buf->ccontext.NameLength = cpu_to_le16(4);
2094 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2095 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2096 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2105 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2107 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2109 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2110 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2111 buf->IndexNumber = pdisk_id->DiskFileId;
2115 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2116 struct create_posix_rsp *posix)
2119 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2120 u8 *end = beg + le32_to_cpu(cc->DataLength);
2123 memset(posix, 0, sizeof(*posix));
2125 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2126 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2127 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2130 sid_len = posix_info_sid_size(sid, end);
2132 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2135 memcpy(&posix->owner, sid, sid_len);
2137 sid = sid + sid_len;
2138 sid_len = posix_info_sid_size(sid, end);
2140 cifs_dbg(VFS, "bad group sid in posix create response\n");
2143 memcpy(&posix->group, sid, sid_len);
2145 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2146 posix->nlink, posix->mode, posix->reparse_tag);
2149 int smb2_parse_contexts(struct TCP_Server_Info *server,
2150 struct kvec *rsp_iov,
2151 unsigned int *epoch,
2152 char *lease_key, __u8 *oplock,
2153 struct smb2_file_all_info *buf,
2154 struct create_posix_rsp *posix)
2156 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2157 struct create_context *cc;
2158 size_t rem, off, len;
2162 static const char smb3_create_tag_posix[] = {
2163 0x93, 0xAD, 0x25, 0x50, 0x9C,
2164 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2165 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2170 off = le32_to_cpu(rsp->CreateContextsOffset);
2171 rem = le32_to_cpu(rsp->CreateContextsLength);
2172 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2174 cc = (struct create_context *)((u8 *)rsp + off);
2176 /* Initialize inode number to 0 in case no valid data in qfid context */
2178 buf->IndexNumber = 0;
2180 while (rem >= sizeof(*cc)) {
2181 doff = le16_to_cpu(cc->DataOffset);
2182 dlen = le32_to_cpu(cc->DataLength);
2183 if (check_add_overflow(doff, dlen, &len) || len > rem)
2186 noff = le16_to_cpu(cc->NameOffset);
2187 nlen = le16_to_cpu(cc->NameLength);
2188 if (noff + nlen > doff)
2191 name = (char *)cc + noff;
2194 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2195 *oplock = server->ops->parse_lease_buf(cc, epoch,
2198 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2199 parse_query_id_ctxt(cc, buf);
2203 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2204 parse_posix_ctxt(cc, buf, posix);
2207 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2208 __func__, nlen, dlen);
2209 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2210 cifs_dump_mem("context data: ", cc, dlen);
2214 off = le32_to_cpu(cc->Next);
2217 if (check_sub_overflow(rem, off, &rem))
2219 cc = (struct create_context *)((u8 *)cc + off);
2222 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2223 *oplock = rsp->OplockLevel;
2229 add_lease_context(struct TCP_Server_Info *server,
2230 struct smb2_create_req *req,
2232 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2234 unsigned int num = *num_iovec;
2236 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2237 if (iov[num].iov_base == NULL)
2239 iov[num].iov_len = server->vals->create_lease_size;
2240 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2241 *num_iovec = num + 1;
2245 static struct create_durable_v2 *
2246 create_durable_v2_buf(struct cifs_open_parms *oparms)
2248 struct cifs_fid *pfid = oparms->fid;
2249 struct create_durable_v2 *buf;
2251 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2255 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2256 (struct create_durable_v2, dcontext));
2257 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2258 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2259 (struct create_durable_v2, Name));
2260 buf->ccontext.NameLength = cpu_to_le16(4);
2263 * NB: Handle timeout defaults to 0, which allows server to choose
2264 * (most servers default to 120 seconds) and most clients default to 0.
2265 * This can be overridden at mount ("handletimeout=") if the user wants
2266 * a different persistent (or resilient) handle timeout for all opens
2267 * on a particular SMB3 mount.
2269 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2270 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2271 generate_random_uuid(buf->dcontext.CreateGuid);
2272 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2274 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2282 static struct create_durable_handle_reconnect_v2 *
2283 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2285 struct create_durable_handle_reconnect_v2 *buf;
2287 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2292 buf->ccontext.DataOffset =
2293 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2295 buf->ccontext.DataLength =
2296 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2297 buf->ccontext.NameOffset =
2298 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2300 buf->ccontext.NameLength = cpu_to_le16(4);
2302 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2303 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2304 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2305 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2307 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2316 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2317 struct cifs_open_parms *oparms)
2319 unsigned int num = *num_iovec;
2321 iov[num].iov_base = create_durable_v2_buf(oparms);
2322 if (iov[num].iov_base == NULL)
2324 iov[num].iov_len = sizeof(struct create_durable_v2);
2325 *num_iovec = num + 1;
2330 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2331 struct cifs_open_parms *oparms)
2333 unsigned int num = *num_iovec;
2335 /* indicate that we don't need to relock the file */
2336 oparms->reconnect = false;
2338 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2339 if (iov[num].iov_base == NULL)
2341 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2342 *num_iovec = num + 1;
2347 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2348 struct cifs_open_parms *oparms, bool use_persistent)
2350 unsigned int num = *num_iovec;
2352 if (use_persistent) {
2353 if (oparms->reconnect)
2354 return add_durable_reconnect_v2_context(iov, num_iovec,
2357 return add_durable_v2_context(iov, num_iovec, oparms);
2360 if (oparms->reconnect) {
2361 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2362 /* indicate that we don't need to relock the file */
2363 oparms->reconnect = false;
2365 iov[num].iov_base = create_durable_buf();
2366 if (iov[num].iov_base == NULL)
2368 iov[num].iov_len = sizeof(struct create_durable);
2369 *num_iovec = num + 1;
2373 /* See MS-SMB2 2.2.13.2.7 */
2374 static struct crt_twarp_ctxt *
2375 create_twarp_buf(__u64 timewarp)
2377 struct crt_twarp_ctxt *buf;
2379 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2383 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2384 (struct crt_twarp_ctxt, Timestamp));
2385 buf->ccontext.DataLength = cpu_to_le32(8);
2386 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2387 (struct crt_twarp_ctxt, Name));
2388 buf->ccontext.NameLength = cpu_to_le16(4);
2389 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2394 buf->Timestamp = cpu_to_le64(timewarp);
2398 /* See MS-SMB2 2.2.13.2.7 */
2400 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2402 unsigned int num = *num_iovec;
2404 iov[num].iov_base = create_twarp_buf(timewarp);
2405 if (iov[num].iov_base == NULL)
2407 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2408 *num_iovec = num + 1;
2412 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2413 static void setup_owner_group_sids(char *buf)
2415 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2417 /* Populate the user ownership fields S-1-5-88-1 */
2418 sids->owner.Revision = 1;
2419 sids->owner.NumAuth = 3;
2420 sids->owner.Authority[5] = 5;
2421 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2422 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2423 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2425 /* Populate the group ownership fields S-1-5-88-2 */
2426 sids->group.Revision = 1;
2427 sids->group.NumAuth = 3;
2428 sids->group.Authority[5] = 5;
2429 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2430 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2431 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2433 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2436 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2437 static struct crt_sd_ctxt *
2438 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2440 struct crt_sd_ctxt *buf;
2442 unsigned int acelen, acl_size, ace_count;
2443 unsigned int owner_offset = 0;
2444 unsigned int group_offset = 0;
2445 struct smb3_acl acl = {};
2447 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2450 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2451 *len += sizeof(struct owner_group_sids);
2454 buf = kzalloc(*len, GFP_KERNEL);
2458 ptr = (__u8 *)&buf[1];
2460 /* offset fields are from beginning of security descriptor not of create context */
2461 owner_offset = ptr - (__u8 *)&buf->sd;
2462 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2463 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2464 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2466 setup_owner_group_sids(ptr);
2467 ptr += sizeof(struct owner_group_sids);
2469 buf->sd.OffsetOwner = 0;
2470 buf->sd.OffsetGroup = 0;
2473 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2474 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2475 buf->ccontext.NameLength = cpu_to_le16(4);
2476 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2481 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2484 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2485 * and "DP" ie the DACL is present
2487 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2489 /* offset owner, group and Sbz1 and SACL are all zero */
2490 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2491 /* Ship the ACL for now. we will copy it into buf later. */
2493 ptr += sizeof(struct smb3_acl);
2495 /* create one ACE to hold the mode embedded in reserved special SID */
2496 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2498 acl_size = acelen + sizeof(struct smb3_acl);
2502 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2503 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2509 /* and one more ACE to allow access for authenticated users */
2510 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2515 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2516 acl.AclSize = cpu_to_le16(acl_size);
2517 acl.AceCount = cpu_to_le16(ace_count);
2518 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2519 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2521 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2522 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2528 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2530 unsigned int num = *num_iovec;
2531 unsigned int len = 0;
2533 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2534 if (iov[num].iov_base == NULL)
2536 iov[num].iov_len = len;
2537 *num_iovec = num + 1;
2541 static struct crt_query_id_ctxt *
2542 create_query_id_buf(void)
2544 struct crt_query_id_ctxt *buf;
2546 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2550 buf->ccontext.DataOffset = cpu_to_le16(0);
2551 buf->ccontext.DataLength = cpu_to_le32(0);
2552 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2553 (struct crt_query_id_ctxt, Name));
2554 buf->ccontext.NameLength = cpu_to_le16(4);
2555 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2563 /* See MS-SMB2 2.2.13.2.9 */
2565 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2567 unsigned int num = *num_iovec;
2569 iov[num].iov_base = create_query_id_buf();
2570 if (iov[num].iov_base == NULL)
2572 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2573 *num_iovec = num + 1;
2578 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2579 const char *treename, const __le16 *path)
2581 int treename_len, path_len;
2582 struct nls_table *cp;
2583 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2588 treename_len = strlen(treename);
2589 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2595 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2597 /* make room for one path separator only if @path isn't empty */
2598 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2601 * final path needs to be 8-byte aligned as specified in
2602 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2604 *out_size = round_up(*out_len * sizeof(__le16), 8);
2605 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2609 cp = load_nls_default();
2610 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2612 /* Do not append the separator if the path is empty */
2613 if (path[0] != cpu_to_le16(0x0000)) {
2614 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2615 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2623 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2624 umode_t mode, struct cifs_tcon *tcon,
2625 const char *full_path,
2626 struct cifs_sb_info *cifs_sb)
2628 struct smb_rqst rqst;
2629 struct smb2_create_req *req;
2630 struct smb2_create_rsp *rsp = NULL;
2631 struct cifs_ses *ses = tcon->ses;
2632 struct kvec iov[3]; /* make sure at least one for each open context */
2633 struct kvec rsp_iov = {NULL, 0};
2636 __le16 *copy_path = NULL;
2639 unsigned int n_iov = 2;
2640 __u32 file_attributes = 0;
2641 char *pc_buf = NULL;
2643 unsigned int total_len;
2644 __le16 *utf16_path = NULL;
2645 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2647 cifs_dbg(FYI, "mkdir\n");
2649 /* resource #1: path allocation */
2650 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2654 if (!ses || !server) {
2659 /* resource #2: request */
2660 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2661 (void **) &req, &total_len);
2666 if (smb3_encryption_required(tcon))
2667 flags |= CIFS_TRANSFORM_REQ;
2669 req->ImpersonationLevel = IL_IMPERSONATION;
2670 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2671 /* File attributes ignored on open (used in create though) */
2672 req->FileAttributes = cpu_to_le32(file_attributes);
2673 req->ShareAccess = FILE_SHARE_ALL_LE;
2674 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2675 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2677 iov[0].iov_base = (char *)req;
2678 /* -1 since last byte is buf[0] which is sent below (path) */
2679 iov[0].iov_len = total_len - 1;
2681 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2683 /* [MS-SMB2] 2.2.13 NameOffset:
2684 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2685 * the SMB2 header, the file name includes a prefix that will
2686 * be processed during DFS name normalization as specified in
2687 * section 3.3.5.9. Otherwise, the file name is relative to
2688 * the share that is identified by the TreeId in the SMB2
2691 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2694 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2695 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2697 tcon->tree_name, utf16_path);
2701 req->NameLength = cpu_to_le16(name_len * 2);
2702 uni_path_len = copy_size;
2703 /* free before overwriting resource */
2705 utf16_path = copy_path;
2707 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2708 /* MUST set path len (NameLength) to 0 opening root of share */
2709 req->NameLength = cpu_to_le16(uni_path_len - 2);
2710 if (uni_path_len % 8 != 0) {
2711 copy_size = roundup(uni_path_len, 8);
2712 copy_path = kzalloc(copy_size, GFP_KERNEL);
2717 memcpy((char *)copy_path, (const char *)utf16_path,
2719 uni_path_len = copy_size;
2720 /* free before overwriting resource */
2722 utf16_path = copy_path;
2726 iov[1].iov_len = uni_path_len;
2727 iov[1].iov_base = utf16_path;
2728 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2730 if (tcon->posix_extensions) {
2731 /* resource #3: posix buf */
2732 rc = add_posix_context(iov, &n_iov, mode);
2735 req->CreateContextsOffset = cpu_to_le32(
2736 sizeof(struct smb2_create_req) +
2738 pc_buf = iov[n_iov-1].iov_base;
2742 memset(&rqst, 0, sizeof(struct smb_rqst));
2744 rqst.rq_nvec = n_iov;
2746 /* no need to inc num_remote_opens because we close it just below */
2747 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2748 FILE_WRITE_ATTRIBUTES);
2749 /* resource #4: response buffer */
2750 rc = cifs_send_recv(xid, ses, server,
2751 &rqst, &resp_buftype, flags, &rsp_iov);
2753 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2754 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2756 FILE_WRITE_ATTRIBUTES, rc);
2757 goto err_free_rsp_buf;
2761 * Although unlikely to be possible for rsp to be null and rc not set,
2762 * adding check below is slightly safer long term (and quiets Coverity
2765 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2772 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2773 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2775 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2777 /* Eventually save off posix specific response info and timestaps */
2780 free_rsp_buf(resp_buftype, rsp);
2783 cifs_small_buf_release(req);
2790 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2791 struct smb_rqst *rqst, __u8 *oplock,
2792 struct cifs_open_parms *oparms, __le16 *path)
2794 struct smb2_create_req *req;
2795 unsigned int n_iov = 2;
2796 __u32 file_attributes = 0;
2799 unsigned int total_len;
2800 struct kvec *iov = rqst->rq_iov;
2804 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2805 (void **) &req, &total_len);
2809 iov[0].iov_base = (char *)req;
2810 /* -1 since last byte is buf[0] which is sent below (path) */
2811 iov[0].iov_len = total_len - 1;
2813 if (oparms->create_options & CREATE_OPTION_READONLY)
2814 file_attributes |= ATTR_READONLY;
2815 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2816 file_attributes |= ATTR_SYSTEM;
2818 req->ImpersonationLevel = IL_IMPERSONATION;
2819 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2820 /* File attributes ignored on open (used in create though) */
2821 req->FileAttributes = cpu_to_le32(file_attributes);
2822 req->ShareAccess = FILE_SHARE_ALL_LE;
2824 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2825 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2826 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2828 /* [MS-SMB2] 2.2.13 NameOffset:
2829 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2830 * the SMB2 header, the file name includes a prefix that will
2831 * be processed during DFS name normalization as specified in
2832 * section 3.3.5.9. Otherwise, the file name is relative to
2833 * the share that is identified by the TreeId in the SMB2
2836 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2839 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2840 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2842 tcon->tree_name, path);
2845 req->NameLength = cpu_to_le16(name_len * 2);
2846 uni_path_len = copy_size;
2849 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2850 /* MUST set path len (NameLength) to 0 opening root of share */
2851 req->NameLength = cpu_to_le16(uni_path_len - 2);
2852 copy_size = round_up(uni_path_len, 8);
2853 copy_path = kzalloc(copy_size, GFP_KERNEL);
2856 memcpy((char *)copy_path, (const char *)path,
2858 uni_path_len = copy_size;
2862 iov[1].iov_len = uni_path_len;
2863 iov[1].iov_base = path;
2865 if ((!server->oplocks) || (tcon->no_lease))
2866 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2868 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2869 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2870 req->RequestedOplockLevel = *oplock;
2871 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2872 (oparms->create_options & CREATE_NOT_FILE))
2873 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2875 rc = add_lease_context(server, req, iov, &n_iov,
2876 oparms->fid->lease_key, oplock);
2881 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2882 rc = add_durable_context(iov, &n_iov, oparms,
2883 tcon->use_persistent);
2888 if (tcon->posix_extensions) {
2889 rc = add_posix_context(iov, &n_iov, oparms->mode);
2894 if (tcon->snapshot_time) {
2895 cifs_dbg(FYI, "adding snapshot context\n");
2896 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2901 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2905 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2906 (oparms->mode != ACL_NO_MODE))
2910 oparms->mode = ACL_NO_MODE;
2913 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2918 if (set_owner | set_mode) {
2919 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2920 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2926 add_query_id_context(iov, &n_iov);
2930 * We have create contexts behind iov[1] (the file
2931 * name), point at them from the main create request
2933 req->CreateContextsOffset = cpu_to_le32(
2934 sizeof(struct smb2_create_req) +
2936 req->CreateContextsLength = 0;
2938 for (unsigned int i = 2; i < (n_iov-1); i++) {
2939 struct kvec *v = &iov[i];
2940 size_t len = v->iov_len;
2941 struct create_context *cctx =
2942 (struct create_context *)v->iov_base;
2944 cctx->Next = cpu_to_le32(len);
2945 le32_add_cpu(&req->CreateContextsLength, len);
2947 le32_add_cpu(&req->CreateContextsLength,
2948 iov[n_iov-1].iov_len);
2951 rqst->rq_nvec = n_iov;
2955 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2956 * All other vectors are freed by kfree().
2959 SMB2_open_free(struct smb_rqst *rqst)
2963 if (rqst && rqst->rq_iov) {
2964 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2965 for (i = 1; i < rqst->rq_nvec; i++)
2966 if (rqst->rq_iov[i].iov_base != smb2_padding)
2967 kfree(rqst->rq_iov[i].iov_base);
2972 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2973 __u8 *oplock, struct smb2_file_all_info *buf,
2974 struct create_posix_rsp *posix,
2975 struct kvec *err_iov, int *buftype)
2977 struct smb_rqst rqst;
2978 struct smb2_create_rsp *rsp = NULL;
2979 struct cifs_tcon *tcon = oparms->tcon;
2980 struct cifs_ses *ses = tcon->ses;
2981 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2982 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2983 struct kvec rsp_iov = {NULL, 0};
2984 int resp_buftype = CIFS_NO_BUFFER;
2988 cifs_dbg(FYI, "create/open\n");
2989 if (!ses || !server)
2992 if (smb3_encryption_required(tcon))
2993 flags |= CIFS_TRANSFORM_REQ;
2995 memset(&rqst, 0, sizeof(struct smb_rqst));
2996 memset(&iov, 0, sizeof(iov));
2998 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3000 rc = SMB2_open_init(tcon, server,
3001 &rqst, oplock, oparms, path);
3005 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3006 oparms->create_options, oparms->desired_access);
3008 rc = cifs_send_recv(xid, ses, server,
3009 &rqst, &resp_buftype, flags,
3011 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3014 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3015 if (err_iov && rsp) {
3017 *buftype = resp_buftype;
3018 resp_buftype = CIFS_NO_BUFFER;
3021 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3022 oparms->create_options, oparms->desired_access, rc);
3023 if (rc == -EREMCHG) {
3024 pr_warn_once("server share %s deleted\n",
3026 tcon->need_reconnect = true;
3029 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3032 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3033 oparms->create_options, oparms->desired_access);
3035 atomic_inc(&tcon->num_remote_opens);
3036 oparms->fid->persistent_fid = rsp->PersistentFileId;
3037 oparms->fid->volatile_fid = rsp->VolatileFileId;
3038 oparms->fid->access = oparms->desired_access;
3039 #ifdef CONFIG_CIFS_DEBUG2
3040 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3041 #endif /* CIFS_DEBUG2 */
3044 buf->CreationTime = rsp->CreationTime;
3045 buf->LastAccessTime = rsp->LastAccessTime;
3046 buf->LastWriteTime = rsp->LastWriteTime;
3047 buf->ChangeTime = rsp->ChangeTime;
3048 buf->AllocationSize = rsp->AllocationSize;
3049 buf->EndOfFile = rsp->EndofFile;
3050 buf->Attributes = rsp->FileAttributes;
3051 buf->NumberOfLinks = cpu_to_le32(1);
3052 buf->DeletePending = 0;
3056 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3057 oparms->fid->lease_key, oplock, buf, posix);
3059 SMB2_open_free(&rqst);
3060 free_rsp_buf(resp_buftype, rsp);
3065 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3066 struct smb_rqst *rqst,
3067 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3068 char *in_data, u32 indatalen,
3069 __u32 max_response_size)
3071 struct smb2_ioctl_req *req;
3072 struct kvec *iov = rqst->rq_iov;
3073 unsigned int total_len;
3077 rc = smb2_ioctl_req_init(opcode, tcon, server,
3078 (void **) &req, &total_len);
3084 * indatalen is usually small at a couple of bytes max, so
3085 * just allocate through generic pool
3087 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3089 cifs_small_buf_release(req);
3094 req->CtlCode = cpu_to_le32(opcode);
3095 req->PersistentFileId = persistent_fid;
3096 req->VolatileFileId = volatile_fid;
3098 iov[0].iov_base = (char *)req;
3100 * If no input data, the size of ioctl struct in
3101 * protocol spec still includes a 1 byte data buffer,
3102 * but if input data passed to ioctl, we do not
3103 * want to double count this, so we do not send
3104 * the dummy one byte of data in iovec[0] if sending
3105 * input data (in iovec[1]).
3108 req->InputCount = cpu_to_le32(indatalen);
3109 /* do not set InputOffset if no input data */
3111 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3113 iov[0].iov_len = total_len - 1;
3114 iov[1].iov_base = in_data_buf;
3115 iov[1].iov_len = indatalen;
3118 iov[0].iov_len = total_len;
3121 req->OutputOffset = 0;
3122 req->OutputCount = 0; /* MBZ */
3125 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3126 * We Could increase default MaxOutputResponse, but that could require
3127 * more credits. Windows typically sets this smaller, but for some
3128 * ioctls it may be useful to allow server to send more. No point
3129 * limiting what the server can send as long as fits in one credit
3130 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3131 * to increase this limit up in the future.
3132 * Note that for snapshot queries that servers like Azure expect that
3133 * the first query be minimal size (and just used to get the number/size
3134 * of previous versions) so response size must be specified as EXACTLY
3135 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3136 * of eight bytes. Currently that is the only case where we set max
3137 * response size smaller.
3139 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3140 req->hdr.CreditCharge =
3141 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3142 SMB2_MAX_BUFFER_SIZE));
3143 /* always an FSCTL (for now) */
3144 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3146 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3147 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3148 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3154 SMB2_ioctl_free(struct smb_rqst *rqst)
3158 if (rqst && rqst->rq_iov) {
3159 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3160 for (i = 1; i < rqst->rq_nvec; i++)
3161 if (rqst->rq_iov[i].iov_base != smb2_padding)
3162 kfree(rqst->rq_iov[i].iov_base);
3168 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3171 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3172 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3173 u32 max_out_data_len, char **out_data,
3174 u32 *plen /* returned data len */)
3176 struct smb_rqst rqst;
3177 struct smb2_ioctl_rsp *rsp = NULL;
3178 struct cifs_ses *ses;
3179 struct TCP_Server_Info *server;
3180 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3181 struct kvec rsp_iov = {NULL, 0};
3182 int resp_buftype = CIFS_NO_BUFFER;
3186 cifs_dbg(FYI, "SMB2 IOCTL\n");
3188 if (out_data != NULL)
3191 /* zero out returned data len, in case of error */
3202 server = cifs_pick_channel(ses);
3206 if (smb3_encryption_required(tcon))
3207 flags |= CIFS_TRANSFORM_REQ;
3209 memset(&rqst, 0, sizeof(struct smb_rqst));
3210 memset(&iov, 0, sizeof(iov));
3212 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3214 rc = SMB2_ioctl_init(tcon, server,
3215 &rqst, persistent_fid, volatile_fid, opcode,
3216 in_data, indatalen, max_out_data_len);
3220 rc = cifs_send_recv(xid, ses, server,
3221 &rqst, &resp_buftype, flags,
3223 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3226 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3227 ses->Suid, 0, opcode, rc);
3229 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3230 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3232 } else if (rc == -EINVAL) {
3233 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3234 (opcode != FSCTL_SRV_COPYCHUNK)) {
3235 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3238 } else if (rc == -E2BIG) {
3239 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3240 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3245 /* check if caller wants to look at return data or just return rc */
3246 if ((plen == NULL) || (out_data == NULL))
3250 * Although unlikely to be possible for rsp to be null and rc not set,
3251 * adding check below is slightly safer long term (and quiets Coverity
3259 *plen = le32_to_cpu(rsp->OutputCount);
3261 /* We check for obvious errors in the output buffer length and offset */
3263 goto ioctl_exit; /* server returned no data */
3264 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3265 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3271 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3272 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3273 le32_to_cpu(rsp->OutputOffset));
3279 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3281 if (*out_data == NULL) {
3287 SMB2_ioctl_free(&rqst);
3288 free_rsp_buf(resp_buftype, rsp);
3293 * Individual callers to ioctl worker function follow
3297 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3298 u64 persistent_fid, u64 volatile_fid)
3301 struct compress_ioctl fsctl_input;
3302 char *ret_data = NULL;
3304 fsctl_input.CompressionState =
3305 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3307 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3308 FSCTL_SET_COMPRESSION,
3309 (char *)&fsctl_input /* data input */,
3310 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3311 &ret_data /* out data */, NULL);
3313 cifs_dbg(FYI, "set compression rc %d\n", rc);
3319 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3320 struct smb_rqst *rqst,
3321 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3323 struct smb2_close_req *req;
3324 struct kvec *iov = rqst->rq_iov;
3325 unsigned int total_len;
3328 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3329 (void **) &req, &total_len);
3333 req->PersistentFileId = persistent_fid;
3334 req->VolatileFileId = volatile_fid;
3336 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3339 iov[0].iov_base = (char *)req;
3340 iov[0].iov_len = total_len;
3346 SMB2_close_free(struct smb_rqst *rqst)
3348 if (rqst && rqst->rq_iov)
3349 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3353 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3354 u64 persistent_fid, u64 volatile_fid,
3355 struct smb2_file_network_open_info *pbuf)
3357 struct smb_rqst rqst;
3358 struct smb2_close_rsp *rsp = NULL;
3359 struct cifs_ses *ses = tcon->ses;
3360 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3362 struct kvec rsp_iov;
3363 int resp_buftype = CIFS_NO_BUFFER;
3366 bool query_attrs = false;
3368 cifs_dbg(FYI, "Close\n");
3370 if (!ses || !server)
3373 if (smb3_encryption_required(tcon))
3374 flags |= CIFS_TRANSFORM_REQ;
3376 memset(&rqst, 0, sizeof(struct smb_rqst));
3377 memset(&iov, 0, sizeof(iov));
3381 /* check if need to ask server to return timestamps in close response */
3385 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3386 rc = SMB2_close_init(tcon, server,
3387 &rqst, persistent_fid, volatile_fid,
3392 rc = cifs_send_recv(xid, ses, server,
3393 &rqst, &resp_buftype, flags, &rsp_iov);
3394 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3397 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3398 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3402 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3405 memcpy(&pbuf->network_open_info,
3406 &rsp->network_open_info,
3407 sizeof(pbuf->network_open_info));
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;
3503 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3504 len > CIFSMaxBufSize))
3507 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3508 (void **) &req, &total_len);
3512 req->InfoType = info_type;
3513 req->FileInfoClass = info_class;
3514 req->PersistentFileId = persistent_fid;
3515 req->VolatileFileId = volatile_fid;
3516 req->AdditionalInformation = cpu_to_le32(additional_info);
3518 req->OutputBufferLength = cpu_to_le32(output_len);
3520 req->InputBufferLength = cpu_to_le32(input_len);
3521 /* total_len for smb query request never close to le16 max */
3522 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3523 memcpy(req->Buffer, input, input_len);
3526 iov[0].iov_base = (char *)req;
3528 iov[0].iov_len = len;
3533 SMB2_query_info_free(struct smb_rqst *rqst)
3535 if (rqst && rqst->rq_iov)
3536 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3540 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3541 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3542 u32 additional_info, size_t output_len, size_t min_len, void **data,
3545 struct smb_rqst rqst;
3546 struct smb2_query_info_rsp *rsp = NULL;
3548 struct kvec rsp_iov;
3550 int resp_buftype = CIFS_NO_BUFFER;
3551 struct cifs_ses *ses = tcon->ses;
3552 struct TCP_Server_Info *server;
3554 bool allocated = false;
3556 cifs_dbg(FYI, "Query Info\n");
3560 server = cifs_pick_channel(ses);
3564 if (smb3_encryption_required(tcon))
3565 flags |= CIFS_TRANSFORM_REQ;
3567 memset(&rqst, 0, sizeof(struct smb_rqst));
3568 memset(&iov, 0, sizeof(iov));
3572 rc = SMB2_query_info_init(tcon, server,
3573 &rqst, persistent_fid, volatile_fid,
3574 info_class, info_type, additional_info,
3575 output_len, 0, NULL);
3579 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3580 ses->Suid, info_class, (__u32)info_type);
3582 rc = cifs_send_recv(xid, ses, server,
3583 &rqst, &resp_buftype, flags, &rsp_iov);
3584 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3587 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3588 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3589 ses->Suid, info_class, (__u32)info_type, rc);
3593 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3594 ses->Suid, info_class, (__u32)info_type);
3597 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3599 *data = kmalloc(*dlen, GFP_KERNEL);
3602 "Error %d allocating memory for acl\n",
3612 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3613 le32_to_cpu(rsp->OutputBufferLength),
3614 &rsp_iov, dlen ? *dlen : min_len, *data);
3615 if (rc && allocated) {
3622 SMB2_query_info_free(&rqst);
3623 free_rsp_buf(resp_buftype, rsp);
3627 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3628 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3630 return query_info(xid, tcon, persistent_fid, volatile_fid,
3631 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3632 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3633 sizeof(struct smb2_file_all_info), (void **)&data,
3638 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3640 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3641 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3643 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3644 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3647 return query_info(xid, tcon, persistent_fid, volatile_fid,
3648 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3649 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3650 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3655 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3656 u64 persistent_fid, u64 volatile_fid,
3657 void **data, u32 *plen, u32 extra_info)
3659 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3663 return query_info(xid, tcon, persistent_fid, volatile_fid,
3664 0, SMB2_O_INFO_SECURITY, additional_info,
3665 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3669 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3670 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3672 return query_info(xid, tcon, persistent_fid, volatile_fid,
3673 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3674 sizeof(struct smb2_file_internal_info),
3675 sizeof(struct smb2_file_internal_info),
3676 (void **)&uniqueid, NULL);
3680 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3681 * See MS-SMB2 2.2.35 and 2.2.36
3685 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3686 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3687 u64 persistent_fid, u64 volatile_fid,
3688 u32 completion_filter, bool watch_tree)
3690 struct smb2_change_notify_req *req;
3691 struct kvec *iov = rqst->rq_iov;
3692 unsigned int total_len;
3695 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3696 (void **) &req, &total_len);
3700 req->PersistentFileId = persistent_fid;
3701 req->VolatileFileId = volatile_fid;
3702 /* See note 354 of MS-SMB2, 64K max */
3703 req->OutputBufferLength =
3704 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3705 req->CompletionFilter = cpu_to_le32(completion_filter);
3707 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3711 iov[0].iov_base = (char *)req;
3712 iov[0].iov_len = total_len;
3718 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3719 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3720 u32 completion_filter, u32 max_out_data_len, char **out_data,
3721 u32 *plen /* returned data len */)
3723 struct cifs_ses *ses = tcon->ses;
3724 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3725 struct smb_rqst rqst;
3726 struct smb2_change_notify_rsp *smb_rsp;
3728 struct kvec rsp_iov = {NULL, 0};
3729 int resp_buftype = CIFS_NO_BUFFER;
3733 cifs_dbg(FYI, "change notify\n");
3734 if (!ses || !server)
3737 if (smb3_encryption_required(tcon))
3738 flags |= CIFS_TRANSFORM_REQ;
3740 memset(&rqst, 0, sizeof(struct smb_rqst));
3741 memset(&iov, 0, sizeof(iov));
3748 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3749 persistent_fid, volatile_fid,
3750 completion_filter, watch_tree);
3754 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3755 (u8)watch_tree, completion_filter);
3756 rc = cifs_send_recv(xid, ses, server,
3757 &rqst, &resp_buftype, flags, &rsp_iov);
3760 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3761 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3762 (u8)watch_tree, completion_filter, rc);
3764 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3765 ses->Suid, (u8)watch_tree, completion_filter);
3766 /* validate that notify information is plausible */
3767 if ((rsp_iov.iov_base == NULL) ||
3768 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
3771 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3773 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3774 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3775 sizeof(struct file_notify_information));
3777 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3778 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3779 if (*out_data == NULL) {
3783 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
3788 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3789 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3796 * This is a no-op for now. We're not really interested in the reply, but
3797 * rather in the fact that the server sent one and that server->lstrp
3800 * FIXME: maybe we should consider checking that the reply matches request?
3803 smb2_echo_callback(struct mid_q_entry *mid)
3805 struct TCP_Server_Info *server = mid->callback_data;
3806 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3807 struct cifs_credits credits = { .value = 0, .instance = 0 };
3809 if (mid->mid_state == MID_RESPONSE_RECEIVED
3810 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3811 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3812 credits.instance = server->reconnect_instance;
3816 add_credits(server, &credits, CIFS_ECHO_OP);
3819 void smb2_reconnect_server(struct work_struct *work)
3821 struct TCP_Server_Info *server = container_of(work,
3822 struct TCP_Server_Info, reconnect.work);
3823 struct TCP_Server_Info *pserver;
3824 struct cifs_ses *ses, *ses2;
3825 struct cifs_tcon *tcon, *tcon2;
3826 struct list_head tmp_list, tmp_ses_list;
3827 bool tcon_exist = false, ses_exist = false;
3828 bool tcon_selected = false;
3830 bool resched = false;
3832 /* If server is a channel, select the primary channel */
3833 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
3835 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3836 mutex_lock(&pserver->reconnect_mutex);
3838 INIT_LIST_HEAD(&tmp_list);
3839 INIT_LIST_HEAD(&tmp_ses_list);
3840 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3842 spin_lock(&cifs_tcp_ses_lock);
3843 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3844 spin_lock(&ses->ses_lock);
3845 if (ses->ses_status == SES_EXITING) {
3846 spin_unlock(&ses->ses_lock);
3849 spin_unlock(&ses->ses_lock);
3851 tcon_selected = false;
3853 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3854 if (tcon->need_reconnect || tcon->need_reopen_files) {
3856 list_add_tail(&tcon->rlist, &tmp_list);
3857 tcon_selected = tcon_exist = true;
3861 * IPC has the same lifetime as its session and uses its
3864 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3865 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3866 tcon_selected = tcon_exist = true;
3867 cifs_smb_ses_inc_refcount(ses);
3870 * handle the case where channel needs to reconnect
3871 * binding session, but tcon is healthy (some other channel
3874 spin_lock(&ses->chan_lock);
3875 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3876 list_add_tail(&ses->rlist, &tmp_ses_list);
3878 cifs_smb_ses_inc_refcount(ses);
3880 spin_unlock(&ses->chan_lock);
3883 * Get the reference to server struct to be sure that the last call of
3884 * cifs_put_tcon() in the loop below won't release the server pointer.
3886 if (tcon_exist || ses_exist)
3887 server->srv_count++;
3889 spin_unlock(&cifs_tcp_ses_lock);
3891 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3892 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3894 cifs_reopen_persistent_handles(tcon);
3897 list_del_init(&tcon->rlist);
3899 cifs_put_smb_ses(tcon->ses);
3901 cifs_put_tcon(tcon);
3907 /* allocate a dummy tcon struct used for reconnect */
3908 tcon = tcon_info_alloc(false);
3911 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3912 list_del_init(&ses->rlist);
3913 cifs_put_smb_ses(ses);
3918 tcon->status = TID_GOOD;
3919 tcon->retry = false;
3920 tcon->need_reconnect = false;
3922 /* now reconnect sessions for necessary channels */
3923 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3925 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3928 list_del_init(&ses->rlist);
3929 cifs_put_smb_ses(ses);
3934 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
3936 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3937 mutex_unlock(&pserver->reconnect_mutex);
3939 /* now we can safely release srv struct */
3940 if (tcon_exist || ses_exist)
3941 cifs_put_tcp_session(server, 1);
3945 SMB2_echo(struct TCP_Server_Info *server)
3947 struct smb2_echo_req *req;
3950 struct smb_rqst rqst = { .rq_iov = iov,
3952 unsigned int total_len;
3954 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
3956 spin_lock(&server->srv_lock);
3957 if (server->ops->need_neg &&
3958 server->ops->need_neg(server)) {
3959 spin_unlock(&server->srv_lock);
3960 /* No need to send echo on newly established connections */
3961 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3964 spin_unlock(&server->srv_lock);
3966 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3967 (void **)&req, &total_len);
3971 req->hdr.CreditRequest = cpu_to_le16(1);
3973 iov[0].iov_len = total_len;
3974 iov[0].iov_base = (char *)req;
3976 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3977 server, CIFS_ECHO_OP, NULL);
3979 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3981 cifs_small_buf_release(req);
3986 SMB2_flush_free(struct smb_rqst *rqst)
3988 if (rqst && rqst->rq_iov)
3989 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3993 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3994 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3995 u64 persistent_fid, u64 volatile_fid)
3997 struct smb2_flush_req *req;
3998 struct kvec *iov = rqst->rq_iov;
3999 unsigned int total_len;
4002 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4003 (void **) &req, &total_len);
4007 req->PersistentFileId = persistent_fid;
4008 req->VolatileFileId = volatile_fid;
4010 iov[0].iov_base = (char *)req;
4011 iov[0].iov_len = total_len;
4017 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4020 struct cifs_ses *ses = tcon->ses;
4021 struct smb_rqst rqst;
4023 struct kvec rsp_iov = {NULL, 0};
4024 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4025 int resp_buftype = CIFS_NO_BUFFER;
4029 cifs_dbg(FYI, "flush\n");
4030 if (!ses || !(ses->server))
4033 if (smb3_encryption_required(tcon))
4034 flags |= CIFS_TRANSFORM_REQ;
4036 memset(&rqst, 0, sizeof(struct smb_rqst));
4037 memset(&iov, 0, sizeof(iov));
4041 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4042 persistent_fid, volatile_fid);
4046 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4047 rc = cifs_send_recv(xid, ses, server,
4048 &rqst, &resp_buftype, flags, &rsp_iov);
4051 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4052 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4055 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4059 SMB2_flush_free(&rqst);
4060 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4064 #ifdef CONFIG_CIFS_SMB_DIRECT
4065 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4067 struct TCP_Server_Info *server = io_parms->server;
4068 struct cifs_tcon *tcon = io_parms->tcon;
4070 /* we can only offload if we're connected */
4071 if (!server || !tcon)
4074 /* we can only offload on an rdma connection */
4075 if (!server->rdma || !server->smbd_conn)
4078 /* we don't support signed offload yet */
4082 /* we don't support encrypted offload yet */
4083 if (smb3_encryption_required(tcon))
4086 /* offload also has its overhead, so only do it if desired */
4087 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4092 #endif /* CONFIG_CIFS_SMB_DIRECT */
4095 * To form a chain of read requests, any read requests after the first should
4096 * have the end_of_chain boolean set to true.
4099 smb2_new_read_req(void **buf, unsigned int *total_len,
4100 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4101 unsigned int remaining_bytes, int request_type)
4104 struct smb2_read_req *req = NULL;
4105 struct smb2_hdr *shdr;
4106 struct TCP_Server_Info *server = io_parms->server;
4108 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4109 (void **) &req, total_len);
4114 return -ECONNABORTED;
4117 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4119 req->PersistentFileId = io_parms->persistent_fid;
4120 req->VolatileFileId = io_parms->volatile_fid;
4121 req->ReadChannelInfoOffset = 0; /* reserved */
4122 req->ReadChannelInfoLength = 0; /* reserved */
4123 req->Channel = 0; /* reserved */
4124 req->MinimumCount = 0;
4125 req->Length = cpu_to_le32(io_parms->length);
4126 req->Offset = cpu_to_le64(io_parms->offset);
4128 trace_smb3_read_enter(0 /* xid */,
4129 io_parms->persistent_fid,
4130 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4131 io_parms->offset, io_parms->length);
4132 #ifdef CONFIG_CIFS_SMB_DIRECT
4134 * If we want to do a RDMA write, fill in and append
4135 * smbd_buffer_descriptor_v1 to the end of read request
4137 if (smb3_use_rdma_offload(io_parms)) {
4138 struct smbd_buffer_descriptor_v1 *v1;
4139 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4141 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4142 true, need_invalidate);
4146 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4147 if (need_invalidate)
4148 req->Channel = SMB2_CHANNEL_RDMA_V1;
4149 req->ReadChannelInfoOffset =
4150 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4151 req->ReadChannelInfoLength =
4152 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4153 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4154 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4155 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4156 v1->length = cpu_to_le32(rdata->mr->mr->length);
4158 *total_len += sizeof(*v1) - 1;
4161 if (request_type & CHAINED_REQUEST) {
4162 if (!(request_type & END_OF_CHAIN)) {
4163 /* next 8-byte aligned request */
4164 *total_len = ALIGN(*total_len, 8);
4165 shdr->NextCommand = cpu_to_le32(*total_len);
4166 } else /* END_OF_CHAIN */
4167 shdr->NextCommand = 0;
4168 if (request_type & RELATED_REQUEST) {
4169 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4171 * Related requests use info from previous read request
4174 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4175 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4176 req->PersistentFileId = (u64)-1;
4177 req->VolatileFileId = (u64)-1;
4180 if (remaining_bytes > io_parms->length)
4181 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4183 req->RemainingBytes = 0;
4190 smb2_readv_callback(struct mid_q_entry *mid)
4192 struct cifs_readdata *rdata = mid->callback_data;
4193 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4194 struct TCP_Server_Info *server = rdata->server;
4195 struct smb2_hdr *shdr =
4196 (struct smb2_hdr *)rdata->iov[0].iov_base;
4197 struct cifs_credits credits = { .value = 0, .instance = 0 };
4198 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4200 if (rdata->got_bytes) {
4201 rqst.rq_iter = rdata->iter;
4202 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
4205 WARN_ONCE(rdata->server != mid->server,
4206 "rdata server %p != mid server %p",
4207 rdata->server, mid->server);
4209 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4210 __func__, mid->mid, mid->mid_state, rdata->result,
4213 switch (mid->mid_state) {
4214 case MID_RESPONSE_RECEIVED:
4215 credits.value = le16_to_cpu(shdr->CreditRequest);
4216 credits.instance = server->reconnect_instance;
4217 /* result already set, check signature */
4218 if (server->sign && !mid->decrypted) {
4221 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4222 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4223 rc = smb2_verify_signature(&rqst, server);
4225 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4228 /* FIXME: should this be counted toward the initiating task? */
4229 task_io_account_read(rdata->got_bytes);
4230 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4232 case MID_REQUEST_SUBMITTED:
4233 case MID_RETRY_NEEDED:
4234 rdata->result = -EAGAIN;
4235 if (server->sign && rdata->got_bytes)
4236 /* reset bytes number since we can not check a sign */
4237 rdata->got_bytes = 0;
4238 /* FIXME: should this be counted toward the initiating task? */
4239 task_io_account_read(rdata->got_bytes);
4240 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4242 case MID_RESPONSE_MALFORMED:
4243 credits.value = le16_to_cpu(shdr->CreditRequest);
4244 credits.instance = server->reconnect_instance;
4247 rdata->result = -EIO;
4249 #ifdef CONFIG_CIFS_SMB_DIRECT
4251 * If this rdata has a memmory registered, the MR can be freed
4252 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4253 * because they have limited number and are used for future I/Os
4256 smbd_deregister_mr(rdata->mr);
4260 if (rdata->result && rdata->result != -ENODATA) {
4261 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4262 trace_smb3_read_err(0 /* xid */,
4263 rdata->cfile->fid.persistent_fid,
4264 tcon->tid, tcon->ses->Suid, rdata->offset,
4265 rdata->bytes, rdata->result);
4267 trace_smb3_read_done(0 /* xid */,
4268 rdata->cfile->fid.persistent_fid,
4269 tcon->tid, tcon->ses->Suid,
4270 rdata->offset, rdata->got_bytes);
4272 queue_work(cifsiod_wq, &rdata->work);
4274 add_credits(server, &credits, 0);
4277 /* smb2_async_readv - send an async read, and set up mid to handle result */
4279 smb2_async_readv(struct cifs_readdata *rdata)
4283 struct smb2_hdr *shdr;
4284 struct cifs_io_parms io_parms;
4285 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4287 struct TCP_Server_Info *server;
4288 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4289 unsigned int total_len;
4292 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4293 __func__, rdata->offset, rdata->bytes);
4296 rdata->server = cifs_pick_channel(tcon->ses);
4298 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4299 io_parms.server = server = rdata->server;
4300 io_parms.offset = rdata->offset;
4301 io_parms.length = rdata->bytes;
4302 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4303 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4304 io_parms.pid = rdata->pid;
4306 rc = smb2_new_read_req(
4307 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4311 if (smb3_encryption_required(io_parms.tcon))
4312 flags |= CIFS_TRANSFORM_REQ;
4314 rdata->iov[0].iov_base = buf;
4315 rdata->iov[0].iov_len = total_len;
4317 shdr = (struct smb2_hdr *)buf;
4319 if (rdata->credits.value > 0) {
4320 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4321 SMB2_MAX_BUFFER_SIZE));
4322 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4323 if (server->credits >= server->max_credits)
4324 shdr->CreditRequest = cpu_to_le16(0);
4326 shdr->CreditRequest = cpu_to_le16(
4327 min_t(int, server->max_credits -
4328 server->credits, credit_request));
4330 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4332 goto async_readv_out;
4334 flags |= CIFS_HAS_CREDITS;
4337 kref_get(&rdata->refcount);
4338 rc = cifs_call_async(server, &rqst,
4339 cifs_readv_receive, smb2_readv_callback,
4340 smb3_handle_read_data, rdata, flags,
4343 kref_put(&rdata->refcount, cifs_readdata_release);
4344 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4345 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4347 io_parms.tcon->ses->Suid,
4348 io_parms.offset, io_parms.length, rc);
4352 cifs_small_buf_release(buf);
4357 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4358 unsigned int *nbytes, char **buf, int *buf_type)
4360 struct smb_rqst rqst;
4361 int resp_buftype, rc;
4362 struct smb2_read_req *req = NULL;
4363 struct smb2_read_rsp *rsp = NULL;
4365 struct kvec rsp_iov;
4366 unsigned int total_len;
4367 int flags = CIFS_LOG_ERROR;
4368 struct cifs_ses *ses = io_parms->tcon->ses;
4370 if (!io_parms->server)
4371 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4374 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4378 if (smb3_encryption_required(io_parms->tcon))
4379 flags |= CIFS_TRANSFORM_REQ;
4381 iov[0].iov_base = (char *)req;
4382 iov[0].iov_len = total_len;
4384 memset(&rqst, 0, sizeof(struct smb_rqst));
4388 rc = cifs_send_recv(xid, ses, io_parms->server,
4389 &rqst, &resp_buftype, flags, &rsp_iov);
4390 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4393 if (rc != -ENODATA) {
4394 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4395 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4396 trace_smb3_read_err(xid,
4397 req->PersistentFileId,
4398 io_parms->tcon->tid, ses->Suid,
4399 io_parms->offset, io_parms->length,
4402 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4403 ses->Suid, io_parms->offset, 0);
4404 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4405 cifs_small_buf_release(req);
4406 return rc == -ENODATA ? 0 : rc;
4408 trace_smb3_read_done(xid,
4409 req->PersistentFileId,
4410 io_parms->tcon->tid, ses->Suid,
4411 io_parms->offset, io_parms->length);
4413 cifs_small_buf_release(req);
4415 *nbytes = le32_to_cpu(rsp->DataLength);
4416 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4417 (*nbytes > io_parms->length)) {
4418 cifs_dbg(FYI, "bad length %d for count %d\n",
4419 *nbytes, io_parms->length);
4425 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4426 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4427 } else if (resp_buftype != CIFS_NO_BUFFER) {
4428 *buf = rsp_iov.iov_base;
4429 if (resp_buftype == CIFS_SMALL_BUFFER)
4430 *buf_type = CIFS_SMALL_BUFFER;
4431 else if (resp_buftype == CIFS_LARGE_BUFFER)
4432 *buf_type = CIFS_LARGE_BUFFER;
4438 * Check the mid_state and signature on received buffer (if any), and queue the
4439 * workqueue completion task.
4442 smb2_writev_callback(struct mid_q_entry *mid)
4444 struct cifs_writedata *wdata = mid->callback_data;
4445 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4446 struct TCP_Server_Info *server = wdata->server;
4447 unsigned int written;
4448 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4449 struct cifs_credits credits = { .value = 0, .instance = 0 };
4451 WARN_ONCE(wdata->server != mid->server,
4452 "wdata server %p != mid server %p",
4453 wdata->server, mid->server);
4455 switch (mid->mid_state) {
4456 case MID_RESPONSE_RECEIVED:
4457 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4458 credits.instance = server->reconnect_instance;
4459 wdata->result = smb2_check_receive(mid, server, 0);
4460 if (wdata->result != 0)
4463 written = le32_to_cpu(rsp->DataLength);
4465 * Mask off high 16 bits when bytes written as returned
4466 * by the server is greater than bytes requested by the
4467 * client. OS/2 servers are known to set incorrect
4470 if (written > wdata->bytes)
4473 if (written < wdata->bytes)
4474 wdata->result = -ENOSPC;
4476 wdata->bytes = written;
4478 case MID_REQUEST_SUBMITTED:
4479 case MID_RETRY_NEEDED:
4480 wdata->result = -EAGAIN;
4482 case MID_RESPONSE_MALFORMED:
4483 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4484 credits.instance = server->reconnect_instance;
4487 wdata->result = -EIO;
4490 #ifdef CONFIG_CIFS_SMB_DIRECT
4492 * If this wdata has a memory registered, the MR can be freed
4493 * The number of MRs available is limited, it's important to recover
4494 * used MR as soon as I/O is finished. Hold MR longer in the later
4495 * I/O process can possibly result in I/O deadlock due to lack of MR
4496 * to send request on I/O retry
4499 smbd_deregister_mr(wdata->mr);
4503 if (wdata->result) {
4504 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4505 trace_smb3_write_err(0 /* no xid */,
4506 wdata->cfile->fid.persistent_fid,
4507 tcon->tid, tcon->ses->Suid, wdata->offset,
4508 wdata->bytes, wdata->result);
4509 if (wdata->result == -ENOSPC)
4510 pr_warn_once("Out of space writing to %s\n",
4513 trace_smb3_write_done(0 /* no xid */,
4514 wdata->cfile->fid.persistent_fid,
4515 tcon->tid, tcon->ses->Suid,
4516 wdata->offset, wdata->bytes);
4518 queue_work(cifsiod_wq, &wdata->work);
4520 add_credits(server, &credits, 0);
4523 /* smb2_async_writev - send an async write, and set up mid to handle result */
4525 smb2_async_writev(struct cifs_writedata *wdata,
4526 void (*release)(struct kref *kref))
4528 int rc = -EACCES, flags = 0;
4529 struct smb2_write_req *req = NULL;
4530 struct smb2_hdr *shdr;
4531 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4532 struct TCP_Server_Info *server = wdata->server;
4534 struct smb_rqst rqst = { };
4535 unsigned int total_len;
4536 struct cifs_io_parms _io_parms;
4537 struct cifs_io_parms *io_parms = NULL;
4541 server = wdata->server = cifs_pick_channel(tcon->ses);
4544 * in future we may get cifs_io_parms passed in from the caller,
4545 * but for now we construct it here...
4547 _io_parms = (struct cifs_io_parms) {
4550 .offset = wdata->offset,
4551 .length = wdata->bytes,
4552 .persistent_fid = wdata->cfile->fid.persistent_fid,
4553 .volatile_fid = wdata->cfile->fid.volatile_fid,
4556 io_parms = &_io_parms;
4558 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4559 (void **) &req, &total_len);
4563 if (smb3_encryption_required(tcon))
4564 flags |= CIFS_TRANSFORM_REQ;
4566 shdr = (struct smb2_hdr *)req;
4567 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4569 req->PersistentFileId = io_parms->persistent_fid;
4570 req->VolatileFileId = io_parms->volatile_fid;
4571 req->WriteChannelInfoOffset = 0;
4572 req->WriteChannelInfoLength = 0;
4573 req->Channel = SMB2_CHANNEL_NONE;
4574 req->Offset = cpu_to_le64(io_parms->offset);
4575 req->DataOffset = cpu_to_le16(
4576 offsetof(struct smb2_write_req, Buffer));
4577 req->RemainingBytes = 0;
4579 trace_smb3_write_enter(0 /* xid */,
4580 io_parms->persistent_fid,
4581 io_parms->tcon->tid,
4582 io_parms->tcon->ses->Suid,
4586 #ifdef CONFIG_CIFS_SMB_DIRECT
4588 * If we want to do a server RDMA read, fill in and append
4589 * smbd_buffer_descriptor_v1 to the end of write request
4591 if (smb3_use_rdma_offload(io_parms)) {
4592 struct smbd_buffer_descriptor_v1 *v1;
4593 size_t data_size = iov_iter_count(&wdata->iter);
4594 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4596 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4597 false, need_invalidate);
4600 goto async_writev_out;
4603 req->DataOffset = 0;
4604 req->RemainingBytes = cpu_to_le32(data_size);
4605 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4606 if (need_invalidate)
4607 req->Channel = SMB2_CHANNEL_RDMA_V1;
4608 req->WriteChannelInfoOffset =
4609 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4610 req->WriteChannelInfoLength =
4611 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4612 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4613 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4614 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4615 v1->length = cpu_to_le32(wdata->mr->mr->length);
4618 iov[0].iov_len = total_len - 1;
4619 iov[0].iov_base = (char *)req;
4623 rqst.rq_iter = wdata->iter;
4624 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4625 #ifdef CONFIG_CIFS_SMB_DIRECT
4627 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4629 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4630 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4632 #ifdef CONFIG_CIFS_SMB_DIRECT
4633 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4635 req->Length = cpu_to_le32(io_parms->length);
4637 req->Length = cpu_to_le32(io_parms->length);
4640 if (wdata->credits.value > 0) {
4641 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4642 SMB2_MAX_BUFFER_SIZE));
4643 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4644 if (server->credits >= server->max_credits)
4645 shdr->CreditRequest = cpu_to_le16(0);
4647 shdr->CreditRequest = cpu_to_le16(
4648 min_t(int, server->max_credits -
4649 server->credits, credit_request));
4651 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4653 goto async_writev_out;
4655 flags |= CIFS_HAS_CREDITS;
4658 kref_get(&wdata->refcount);
4659 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4660 wdata, flags, &wdata->credits);
4663 trace_smb3_write_err(0 /* no xid */,
4664 io_parms->persistent_fid,
4665 io_parms->tcon->tid,
4666 io_parms->tcon->ses->Suid,
4670 kref_put(&wdata->refcount, release);
4671 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4675 cifs_small_buf_release(req);
4680 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4681 * The length field from io_parms must be at least 1 and indicates a number of
4682 * elements with data to write that begins with position 1 in iov array. All
4683 * data length is specified by count.
4686 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4687 unsigned int *nbytes, struct kvec *iov, int n_vec)
4689 struct smb_rqst rqst;
4691 struct smb2_write_req *req = NULL;
4692 struct smb2_write_rsp *rsp = NULL;
4694 struct kvec rsp_iov;
4696 unsigned int total_len;
4697 struct TCP_Server_Info *server;
4704 if (!io_parms->server)
4705 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4706 server = io_parms->server;
4708 return -ECONNABORTED;
4710 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4711 (void **) &req, &total_len);
4715 if (smb3_encryption_required(io_parms->tcon))
4716 flags |= CIFS_TRANSFORM_REQ;
4718 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4720 req->PersistentFileId = io_parms->persistent_fid;
4721 req->VolatileFileId = io_parms->volatile_fid;
4722 req->WriteChannelInfoOffset = 0;
4723 req->WriteChannelInfoLength = 0;
4725 req->Length = cpu_to_le32(io_parms->length);
4726 req->Offset = cpu_to_le64(io_parms->offset);
4727 req->DataOffset = cpu_to_le16(
4728 offsetof(struct smb2_write_req, Buffer));
4729 req->RemainingBytes = 0;
4731 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4732 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4733 io_parms->offset, io_parms->length);
4735 iov[0].iov_base = (char *)req;
4737 iov[0].iov_len = total_len - 1;
4739 memset(&rqst, 0, sizeof(struct smb_rqst));
4741 rqst.rq_nvec = n_vec + 1;
4743 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4745 &resp_buftype, flags, &rsp_iov);
4746 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4749 trace_smb3_write_err(xid,
4750 req->PersistentFileId,
4751 io_parms->tcon->tid,
4752 io_parms->tcon->ses->Suid,
4753 io_parms->offset, io_parms->length, rc);
4754 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4755 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4757 *nbytes = le32_to_cpu(rsp->DataLength);
4758 trace_smb3_write_done(xid,
4759 req->PersistentFileId,
4760 io_parms->tcon->tid,
4761 io_parms->tcon->ses->Suid,
4762 io_parms->offset, *nbytes);
4765 cifs_small_buf_release(req);
4766 free_rsp_buf(resp_buftype, rsp);
4770 int posix_info_sid_size(const void *beg, const void *end)
4778 subauth = *(u8 *)(beg+1);
4779 if (subauth < 1 || subauth > 15)
4782 total = 1 + 1 + 6 + 4*subauth;
4783 if (beg + total > end)
4789 int posix_info_parse(const void *beg, const void *end,
4790 struct smb2_posix_info_parsed *out)
4794 int owner_len, group_len;
4796 const void *owner_sid;
4797 const void *group_sid;
4800 /* if no end bound given, assume payload to be correct */
4802 const struct smb2_posix_info *p = beg;
4804 end = beg + le32_to_cpu(p->NextEntryOffset);
4805 /* last element will have a 0 offset, pick a sensible bound */
4810 /* check base buf */
4811 if (beg + sizeof(struct smb2_posix_info) > end)
4813 total_len = sizeof(struct smb2_posix_info);
4815 /* check owner sid */
4816 owner_sid = beg + total_len;
4817 owner_len = posix_info_sid_size(owner_sid, end);
4820 total_len += owner_len;
4822 /* check group sid */
4823 group_sid = beg + total_len;
4824 group_len = posix_info_sid_size(group_sid, end);
4827 total_len += group_len;
4829 /* check name len */
4830 if (beg + total_len + 4 > end)
4832 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4833 if (name_len < 1 || name_len > 0xFFFF)
4838 name = beg + total_len;
4839 if (name + name_len > end)
4841 total_len += name_len;
4845 out->size = total_len;
4846 out->name_len = name_len;
4848 memcpy(&out->owner, owner_sid, owner_len);
4849 memcpy(&out->group, group_sid, group_len);
4854 static int posix_info_extra_size(const void *beg, const void *end)
4856 int len = posix_info_parse(beg, end, NULL);
4860 return len - sizeof(struct smb2_posix_info);
4864 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4868 unsigned int entrycount = 0;
4869 unsigned int next_offset = 0;
4871 FILE_DIRECTORY_INFO *dir_info;
4873 if (bufstart == NULL)
4876 entryptr = bufstart;
4879 if (entryptr + next_offset < entryptr ||
4880 entryptr + next_offset > end_of_buf ||
4881 entryptr + next_offset + size > end_of_buf) {
4882 cifs_dbg(VFS, "malformed search entry would overflow\n");
4886 entryptr = entryptr + next_offset;
4887 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4889 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4890 len = posix_info_extra_size(entryptr, end_of_buf);
4892 len = le32_to_cpu(dir_info->FileNameLength);
4895 entryptr + len < entryptr ||
4896 entryptr + len > end_of_buf ||
4897 entryptr + len + size > end_of_buf) {
4898 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4903 *lastentry = entryptr;
4906 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4917 int SMB2_query_directory_init(const unsigned int xid,
4918 struct cifs_tcon *tcon,
4919 struct TCP_Server_Info *server,
4920 struct smb_rqst *rqst,
4921 u64 persistent_fid, u64 volatile_fid,
4922 int index, int info_level)
4924 struct smb2_query_directory_req *req;
4925 unsigned char *bufptr;
4926 __le16 asteriks = cpu_to_le16('*');
4927 unsigned int output_size = CIFSMaxBufSize -
4928 MAX_SMB2_CREATE_RESPONSE_SIZE -
4929 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4930 unsigned int total_len;
4931 struct kvec *iov = rqst->rq_iov;
4934 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4935 (void **) &req, &total_len);
4939 switch (info_level) {
4940 case SMB_FIND_FILE_DIRECTORY_INFO:
4941 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4943 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4944 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4946 case SMB_FIND_FILE_POSIX_INFO:
4947 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4950 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4955 req->FileIndex = cpu_to_le32(index);
4956 req->PersistentFileId = persistent_fid;
4957 req->VolatileFileId = volatile_fid;
4960 bufptr = req->Buffer;
4961 memcpy(bufptr, &asteriks, len);
4963 req->FileNameOffset =
4964 cpu_to_le16(sizeof(struct smb2_query_directory_req));
4965 req->FileNameLength = cpu_to_le16(len);
4967 * BB could be 30 bytes or so longer if we used SMB2 specific
4968 * buffer lengths, but this is safe and close enough.
4970 output_size = min_t(unsigned int, output_size, server->maxBuf);
4971 output_size = min_t(unsigned int, output_size, 2 << 15);
4972 req->OutputBufferLength = cpu_to_le32(output_size);
4974 iov[0].iov_base = (char *)req;
4976 iov[0].iov_len = total_len - 1;
4978 iov[1].iov_base = (char *)(req->Buffer);
4979 iov[1].iov_len = len;
4981 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4982 tcon->ses->Suid, index, output_size);
4987 void SMB2_query_directory_free(struct smb_rqst *rqst)
4989 if (rqst && rqst->rq_iov) {
4990 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4995 smb2_parse_query_directory(struct cifs_tcon *tcon,
4996 struct kvec *rsp_iov,
4998 struct cifs_search_info *srch_inf)
5000 struct smb2_query_directory_rsp *rsp;
5001 size_t info_buf_size;
5005 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5007 switch (srch_inf->info_level) {
5008 case SMB_FIND_FILE_DIRECTORY_INFO:
5009 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5011 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5012 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5014 case SMB_FIND_FILE_POSIX_INFO:
5015 /* note that posix payload are variable size */
5016 info_buf_size = sizeof(struct smb2_posix_info);
5019 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5020 srch_inf->info_level);
5024 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5025 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5028 cifs_tcon_dbg(VFS, "bad info payload");
5032 srch_inf->unicode = true;
5034 if (srch_inf->ntwrk_buf_start) {
5035 if (srch_inf->smallBuf)
5036 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5038 cifs_buf_release(srch_inf->ntwrk_buf_start);
5040 srch_inf->ntwrk_buf_start = (char *)rsp;
5041 srch_inf->srch_entries_start = srch_inf->last_entry =
5042 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5043 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5045 srch_inf->entries_in_buffer = num_entries(
5046 srch_inf->info_level,
5047 srch_inf->srch_entries_start,
5049 &srch_inf->last_entry,
5052 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5053 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5054 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5055 srch_inf->srch_entries_start, srch_inf->last_entry);
5056 if (resp_buftype == CIFS_LARGE_BUFFER)
5057 srch_inf->smallBuf = false;
5058 else if (resp_buftype == CIFS_SMALL_BUFFER)
5059 srch_inf->smallBuf = true;
5061 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5067 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5068 u64 persistent_fid, u64 volatile_fid, int index,
5069 struct cifs_search_info *srch_inf)
5071 struct smb_rqst rqst;
5072 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5073 struct smb2_query_directory_rsp *rsp = NULL;
5074 int resp_buftype = CIFS_NO_BUFFER;
5075 struct kvec rsp_iov;
5077 struct cifs_ses *ses = tcon->ses;
5078 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5081 if (!ses || !(ses->server))
5084 if (smb3_encryption_required(tcon))
5085 flags |= CIFS_TRANSFORM_REQ;
5087 memset(&rqst, 0, sizeof(struct smb_rqst));
5088 memset(&iov, 0, sizeof(iov));
5090 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5092 rc = SMB2_query_directory_init(xid, tcon, server,
5093 &rqst, persistent_fid,
5094 volatile_fid, index,
5095 srch_inf->info_level);
5099 rc = cifs_send_recv(xid, ses, server,
5100 &rqst, &resp_buftype, flags, &rsp_iov);
5101 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5104 if (rc == -ENODATA &&
5105 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5106 trace_smb3_query_dir_done(xid, persistent_fid,
5107 tcon->tid, tcon->ses->Suid, index, 0);
5108 srch_inf->endOfSearch = true;
5111 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5112 tcon->ses->Suid, index, 0, rc);
5113 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5118 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5121 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5122 tcon->ses->Suid, index, 0, rc);
5125 resp_buftype = CIFS_NO_BUFFER;
5127 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5128 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5131 SMB2_query_directory_free(&rqst);
5132 free_rsp_buf(resp_buftype, rsp);
5137 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5138 struct smb_rqst *rqst,
5139 u64 persistent_fid, u64 volatile_fid, u32 pid,
5140 u8 info_class, u8 info_type, u32 additional_info,
5141 void **data, unsigned int *size)
5143 struct smb2_set_info_req *req;
5144 struct kvec *iov = rqst->rq_iov;
5145 unsigned int i, total_len;
5148 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5149 (void **) &req, &total_len);
5153 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5154 req->InfoType = info_type;
5155 req->FileInfoClass = info_class;
5156 req->PersistentFileId = persistent_fid;
5157 req->VolatileFileId = volatile_fid;
5158 req->AdditionalInformation = cpu_to_le32(additional_info);
5160 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5161 req->BufferLength = cpu_to_le32(*size);
5163 memcpy(req->Buffer, *data, *size);
5166 iov[0].iov_base = (char *)req;
5168 iov[0].iov_len = total_len - 1;
5170 for (i = 1; i < rqst->rq_nvec; i++) {
5171 le32_add_cpu(&req->BufferLength, size[i]);
5172 iov[i].iov_base = (char *)data[i];
5173 iov[i].iov_len = size[i];
5180 SMB2_set_info_free(struct smb_rqst *rqst)
5182 if (rqst && rqst->rq_iov)
5183 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5187 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5188 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5189 u8 info_type, u32 additional_info, unsigned int num,
5190 void **data, unsigned int *size)
5192 struct smb_rqst rqst;
5193 struct smb2_set_info_rsp *rsp = NULL;
5195 struct kvec rsp_iov;
5198 struct cifs_ses *ses = tcon->ses;
5199 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5202 if (!ses || !server)
5208 if (smb3_encryption_required(tcon))
5209 flags |= CIFS_TRANSFORM_REQ;
5211 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5215 memset(&rqst, 0, sizeof(struct smb_rqst));
5219 rc = SMB2_set_info_init(tcon, server,
5220 &rqst, persistent_fid, volatile_fid, pid,
5221 info_class, info_type, additional_info,
5229 rc = cifs_send_recv(xid, ses, server,
5230 &rqst, &resp_buftype, flags,
5232 SMB2_set_info_free(&rqst);
5233 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5236 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5237 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5238 ses->Suid, info_class, (__u32)info_type, rc);
5241 free_rsp_buf(resp_buftype, rsp);
5247 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5248 u64 volatile_fid, u32 pid, __le64 *eof)
5250 struct smb2_file_eof_info info;
5254 info.EndOfFile = *eof;
5257 size = sizeof(struct smb2_file_eof_info);
5259 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5261 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5262 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5263 0, 1, &data, &size);
5267 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5268 u64 persistent_fid, u64 volatile_fid,
5269 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5271 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5272 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5273 1, (void **)&pnntsd, &pacllen);
5277 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5278 u64 persistent_fid, u64 volatile_fid,
5279 struct smb2_file_full_ea_info *buf, int len)
5281 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5282 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5283 0, 1, (void **)&buf, &len);
5287 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5288 const u64 persistent_fid, const u64 volatile_fid,
5291 struct smb_rqst rqst;
5293 struct smb2_oplock_break *req = NULL;
5294 struct cifs_ses *ses = tcon->ses;
5295 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5296 int flags = CIFS_OBREAK_OP;
5297 unsigned int total_len;
5299 struct kvec rsp_iov;
5302 cifs_dbg(FYI, "SMB2_oplock_break\n");
5303 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5304 (void **) &req, &total_len);
5308 if (smb3_encryption_required(tcon))
5309 flags |= CIFS_TRANSFORM_REQ;
5311 req->VolatileFid = volatile_fid;
5312 req->PersistentFid = persistent_fid;
5313 req->OplockLevel = oplock_level;
5314 req->hdr.CreditRequest = cpu_to_le16(1);
5316 flags |= CIFS_NO_RSP_BUF;
5318 iov[0].iov_base = (char *)req;
5319 iov[0].iov_len = total_len;
5321 memset(&rqst, 0, sizeof(struct smb_rqst));
5325 rc = cifs_send_recv(xid, ses, server,
5326 &rqst, &resp_buf_type, flags, &rsp_iov);
5327 cifs_small_buf_release(req);
5330 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5331 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5338 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5339 struct kstatfs *kst)
5341 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5342 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5343 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5344 kst->f_bfree = kst->f_bavail =
5345 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5350 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5351 struct kstatfs *kst)
5353 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5354 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5355 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5356 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5357 kst->f_bavail = kst->f_bfree;
5359 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5360 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5361 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5362 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5363 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5369 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5370 struct TCP_Server_Info *server,
5371 int level, int outbuf_len, u64 persistent_fid,
5375 struct smb2_query_info_req *req;
5376 unsigned int total_len;
5378 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5380 if ((tcon->ses == NULL) || server == NULL)
5383 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5384 (void **) &req, &total_len);
5388 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5389 req->FileInfoClass = level;
5390 req->PersistentFileId = persistent_fid;
5391 req->VolatileFileId = volatile_fid;
5393 req->InputBufferOffset =
5394 cpu_to_le16(sizeof(struct smb2_query_info_req));
5395 req->OutputBufferLength = cpu_to_le32(
5396 outbuf_len + sizeof(struct smb2_query_info_rsp));
5398 iov->iov_base = (char *)req;
5399 iov->iov_len = total_len;
5403 static inline void free_qfs_info_req(struct kvec *iov)
5405 cifs_buf_release(iov->iov_base);
5409 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5410 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5412 struct smb_rqst rqst;
5413 struct smb2_query_info_rsp *rsp = NULL;
5415 struct kvec rsp_iov;
5418 struct cifs_ses *ses = tcon->ses;
5419 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5420 FILE_SYSTEM_POSIX_INFO *info = NULL;
5423 rc = build_qfs_info_req(&iov, tcon, server,
5424 FS_POSIX_INFORMATION,
5425 sizeof(FILE_SYSTEM_POSIX_INFO),
5426 persistent_fid, volatile_fid);
5430 if (smb3_encryption_required(tcon))
5431 flags |= CIFS_TRANSFORM_REQ;
5433 memset(&rqst, 0, sizeof(struct smb_rqst));
5437 rc = cifs_send_recv(xid, ses, server,
5438 &rqst, &resp_buftype, flags, &rsp_iov);
5439 free_qfs_info_req(&iov);
5441 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5442 goto posix_qfsinf_exit;
5444 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5446 info = (FILE_SYSTEM_POSIX_INFO *)(
5447 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5448 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5449 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5450 sizeof(FILE_SYSTEM_POSIX_INFO));
5452 copy_posix_fs_info_to_kstatfs(info, fsdata);
5455 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5460 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5461 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5463 struct smb_rqst rqst;
5464 struct smb2_query_info_rsp *rsp = NULL;
5466 struct kvec rsp_iov;
5469 struct cifs_ses *ses = tcon->ses;
5470 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5471 struct smb2_fs_full_size_info *info = NULL;
5474 rc = build_qfs_info_req(&iov, tcon, server,
5475 FS_FULL_SIZE_INFORMATION,
5476 sizeof(struct smb2_fs_full_size_info),
5477 persistent_fid, volatile_fid);
5481 if (smb3_encryption_required(tcon))
5482 flags |= CIFS_TRANSFORM_REQ;
5484 memset(&rqst, 0, sizeof(struct smb_rqst));
5488 rc = cifs_send_recv(xid, ses, server,
5489 &rqst, &resp_buftype, flags, &rsp_iov);
5490 free_qfs_info_req(&iov);
5492 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5495 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5497 info = (struct smb2_fs_full_size_info *)(
5498 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5499 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5500 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5501 sizeof(struct smb2_fs_full_size_info));
5503 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5506 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5511 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5512 u64 persistent_fid, u64 volatile_fid, int level)
5514 struct smb_rqst rqst;
5515 struct smb2_query_info_rsp *rsp = NULL;
5517 struct kvec rsp_iov;
5519 int resp_buftype, max_len, min_len;
5520 struct cifs_ses *ses = tcon->ses;
5521 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5522 unsigned int rsp_len, offset;
5525 if (level == FS_DEVICE_INFORMATION) {
5526 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5527 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5528 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5529 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5530 min_len = MIN_FS_ATTR_INFO_SIZE;
5531 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5532 max_len = sizeof(struct smb3_fs_ss_info);
5533 min_len = sizeof(struct smb3_fs_ss_info);
5534 } else if (level == FS_VOLUME_INFORMATION) {
5535 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5536 min_len = sizeof(struct smb3_fs_vol_info);
5538 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5542 rc = build_qfs_info_req(&iov, tcon, server,
5544 persistent_fid, volatile_fid);
5548 if (smb3_encryption_required(tcon))
5549 flags |= CIFS_TRANSFORM_REQ;
5551 memset(&rqst, 0, sizeof(struct smb_rqst));
5555 rc = cifs_send_recv(xid, ses, server,
5556 &rqst, &resp_buftype, flags, &rsp_iov);
5557 free_qfs_info_req(&iov);
5559 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5562 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5564 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5565 offset = le16_to_cpu(rsp->OutputBufferOffset);
5566 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5570 if (level == FS_ATTRIBUTE_INFORMATION)
5571 memcpy(&tcon->fsAttrInfo, offset
5572 + (char *)rsp, min_t(unsigned int,
5574 else if (level == FS_DEVICE_INFORMATION)
5575 memcpy(&tcon->fsDevInfo, offset
5576 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5577 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5578 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5579 (offset + (char *)rsp);
5580 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5581 tcon->perf_sector_size =
5582 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5583 } else if (level == FS_VOLUME_INFORMATION) {
5584 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5585 (offset + (char *)rsp);
5586 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5587 tcon->vol_create_time = vol_info->VolumeCreationTime;
5591 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5596 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5597 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5598 const __u32 num_lock, struct smb2_lock_element *buf)
5600 struct smb_rqst rqst;
5602 struct smb2_lock_req *req = NULL;
5604 struct kvec rsp_iov;
5607 int flags = CIFS_NO_RSP_BUF;
5608 unsigned int total_len;
5609 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5611 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5613 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5614 (void **) &req, &total_len);
5618 if (smb3_encryption_required(tcon))
5619 flags |= CIFS_TRANSFORM_REQ;
5621 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5622 req->LockCount = cpu_to_le16(num_lock);
5624 req->PersistentFileId = persist_fid;
5625 req->VolatileFileId = volatile_fid;
5627 count = num_lock * sizeof(struct smb2_lock_element);
5629 iov[0].iov_base = (char *)req;
5630 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5631 iov[1].iov_base = (char *)buf;
5632 iov[1].iov_len = count;
5634 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5636 memset(&rqst, 0, sizeof(struct smb_rqst));
5640 rc = cifs_send_recv(xid, tcon->ses, server,
5641 &rqst, &resp_buf_type, flags,
5643 cifs_small_buf_release(req);
5645 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5646 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5647 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5648 tcon->ses->Suid, rc);
5655 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5656 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5657 const __u64 length, const __u64 offset, const __u32 lock_flags,
5660 struct smb2_lock_element lock;
5662 lock.Offset = cpu_to_le64(offset);
5663 lock.Length = cpu_to_le64(length);
5664 lock.Flags = cpu_to_le32(lock_flags);
5665 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5666 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5668 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5672 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5673 __u8 *lease_key, const __le32 lease_state)
5675 struct smb_rqst rqst;
5677 struct smb2_lease_ack *req = NULL;
5678 struct cifs_ses *ses = tcon->ses;
5679 int flags = CIFS_OBREAK_OP;
5680 unsigned int total_len;
5682 struct kvec rsp_iov;
5684 __u64 *please_key_high;
5685 __u64 *please_key_low;
5686 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5688 cifs_dbg(FYI, "SMB2_lease_break\n");
5689 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5690 (void **) &req, &total_len);
5694 if (smb3_encryption_required(tcon))
5695 flags |= CIFS_TRANSFORM_REQ;
5697 req->hdr.CreditRequest = cpu_to_le16(1);
5698 req->StructureSize = cpu_to_le16(36);
5701 memcpy(req->LeaseKey, lease_key, 16);
5702 req->LeaseState = lease_state;
5704 flags |= CIFS_NO_RSP_BUF;
5706 iov[0].iov_base = (char *)req;
5707 iov[0].iov_len = total_len;
5709 memset(&rqst, 0, sizeof(struct smb_rqst));
5713 rc = cifs_send_recv(xid, ses, server,
5714 &rqst, &resp_buf_type, flags, &rsp_iov);
5715 cifs_small_buf_release(req);
5717 please_key_low = (__u64 *)lease_key;
5718 please_key_high = (__u64 *)(lease_key+8);
5720 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5721 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5722 ses->Suid, *please_key_low, *please_key_high, rc);
5723 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5725 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5726 ses->Suid, *please_key_low, *please_key_high);