1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2009, 2013
9 * Contains the routines for constructing the SMB2 PDUs themselves
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/task_io_accounting_ops.h>
22 #include <linux/uaccess.h>
23 #include <linux/uuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/xattr.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
42 #include "cached_dir.h"
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
51 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
73 int smb3_encryption_required(const struct cifs_tcon *tcon)
75 if (!tcon || !tcon->ses)
77 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
87 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
91 shdr->ProtocolId = SMB2_PROTO_NUMBER;
92 shdr->StructureSize = cpu_to_le16(64);
93 shdr->Command = smb2_cmd;
95 spin_lock(&server->req_lock);
96 /* Request up to 10 credits but don't go over the limit. */
97 if (server->credits >= server->max_credits)
98 shdr->CreditRequest = cpu_to_le16(0);
100 shdr->CreditRequest = cpu_to_le16(
101 min_t(int, server->max_credits -
102 server->credits, 10));
103 spin_unlock(&server->req_lock);
105 shdr->CreditRequest = cpu_to_le16(2);
107 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
114 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
115 shdr->CreditCharge = cpu_to_le16(1);
116 /* else CreditCharge MBZ */
118 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
119 /* Uid is not converted */
121 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
124 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
125 * to pass the path on the Open SMB prefixed by \\server\share.
126 * Not sure when we would need to do the augmented path (if ever) and
127 * setting this flag breaks the SMB2 open operation since it is
128 * illegal to send an empty path name (without \\server\share prefix)
129 * when the DFS flag is set in the SMB open header. We could
130 * consider setting the flag on all operations other than open
131 * but it is safer to net set it for now.
133 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
134 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136 if (server && server->sign && !smb3_encryption_required(tcon))
137 shdr->Flags |= SMB2_FLAGS_SIGNED;
143 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
144 struct TCP_Server_Info *server)
147 struct nls_table *nls_codepage = NULL;
148 struct cifs_ses *ses;
151 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
152 * check for tcp and smb session status done differently
153 * for those three - in the calling routine.
159 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
160 * cifs_tree_connect().
162 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
165 spin_lock(&tcon->tc_lock);
166 if (tcon->status == TID_EXITING) {
168 * only tree disconnect allowed when disconnecting ...
170 if (smb2_command != SMB2_TREE_DISCONNECT) {
171 spin_unlock(&tcon->tc_lock);
172 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
177 spin_unlock(&tcon->tc_lock);
182 spin_lock(&ses->ses_lock);
183 if (ses->ses_status == SES_EXITING) {
184 spin_unlock(&ses->ses_lock);
187 spin_unlock(&ses->ses_lock);
188 if (!ses->server || !server)
191 spin_lock(&server->srv_lock);
192 if (server->tcpStatus == CifsNeedReconnect) {
194 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
195 * here since they are implicitly done when session drops.
197 switch (smb2_command) {
199 * BB Should we keep oplock break and add flush to exceptions?
201 case SMB2_TREE_DISCONNECT:
204 case SMB2_OPLOCK_BREAK:
205 spin_unlock(&server->srv_lock);
209 spin_unlock(&server->srv_lock);
212 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
216 spin_lock(&ses->chan_lock);
217 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
218 spin_unlock(&ses->chan_lock);
221 spin_unlock(&ses->chan_lock);
222 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
223 tcon->ses->chans_need_reconnect,
224 tcon->need_reconnect);
226 mutex_lock(&ses->session_mutex);
228 * Recheck after acquire mutex. If another thread is negotiating
229 * and the server never sends an answer the socket will be closed
230 * and tcpStatus set to reconnect.
232 spin_lock(&server->srv_lock);
233 if (server->tcpStatus == CifsNeedReconnect) {
234 spin_unlock(&server->srv_lock);
235 mutex_unlock(&ses->session_mutex);
243 spin_unlock(&server->srv_lock);
245 nls_codepage = load_nls_default();
248 * need to prevent multiple threads trying to simultaneously
249 * reconnect the same SMB session
251 spin_lock(&ses->ses_lock);
252 spin_lock(&ses->chan_lock);
253 if (!cifs_chan_needs_reconnect(ses, server) &&
254 ses->ses_status == SES_GOOD) {
255 spin_unlock(&ses->chan_lock);
256 spin_unlock(&ses->ses_lock);
257 /* this means that we only need to tree connect */
258 if (tcon->need_reconnect)
259 goto skip_sess_setup;
261 mutex_unlock(&ses->session_mutex);
264 spin_unlock(&ses->chan_lock);
265 spin_unlock(&ses->ses_lock);
267 rc = cifs_negotiate_protocol(0, ses, server);
269 rc = cifs_setup_session(0, ses, server, nls_codepage);
270 if ((rc == -EACCES) && !tcon->retry) {
271 mutex_unlock(&ses->session_mutex);
275 mutex_unlock(&ses->session_mutex);
279 mutex_unlock(&ses->session_mutex);
284 if (!tcon->need_reconnect) {
285 mutex_unlock(&ses->session_mutex);
288 cifs_mark_open_files_invalid(tcon);
289 if (tcon->use_persistent)
290 tcon->need_reopen_files = true;
292 rc = cifs_tree_connect(0, tcon, nls_codepage);
293 mutex_unlock(&ses->session_mutex);
295 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
297 /* If sess reconnected but tcon didn't, something strange ... */
298 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
302 if (smb2_command != SMB2_INTERNAL_CMD)
303 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
305 atomic_inc(&tconInfoReconnectCount);
308 * Check if handle based operation so we know whether we can continue
309 * or not without returning to caller to reset file handle.
312 * BB Is flush done by server on drop of tcp session? Should we special
313 * case it and skip above?
315 switch (smb2_command) {
320 case SMB2_QUERY_DIRECTORY:
321 case SMB2_CHANGE_NOTIFY:
322 case SMB2_QUERY_INFO:
327 unload_nls(nls_codepage);
332 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
333 struct TCP_Server_Info *server,
335 unsigned int *total_len)
337 struct smb2_pdu *spdu = buf;
338 /* lookup word count ie StructureSize from table */
339 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
342 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
343 * largest operations (Create)
347 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
348 spdu->StructureSize2 = cpu_to_le16(parmsize);
350 *total_len = parmsize + sizeof(struct smb2_hdr);
354 * Allocate and return pointer to an SMB request hdr, and set basic
355 * SMB information in the SMB header. If the return code is zero, this
356 * function must have filled in request_buf pointer.
358 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
359 struct TCP_Server_Info *server,
360 void **request_buf, unsigned int *total_len)
362 /* BB eventually switch this to SMB2 specific small buf size */
363 if (smb2_command == SMB2_SET_INFO)
364 *request_buf = cifs_buf_get();
366 *request_buf = cifs_small_buf_get();
367 if (*request_buf == NULL) {
368 /* BB should we add a retry in here if not a writepage? */
372 fill_small_buf(smb2_command, tcon, server,
373 (struct smb2_hdr *)(*request_buf),
377 uint16_t com_code = le16_to_cpu(smb2_command);
378 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
379 cifs_stats_inc(&tcon->num_smbs_sent);
385 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
386 struct TCP_Server_Info *server,
387 void **request_buf, unsigned int *total_len)
391 rc = smb2_reconnect(smb2_command, tcon, server);
395 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
399 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
400 struct TCP_Server_Info *server,
401 void **request_buf, unsigned int *total_len)
403 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
404 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
405 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
406 request_buf, total_len);
408 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
409 request_buf, total_len);
412 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
415 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
417 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
418 pneg_ctxt->DataLength = cpu_to_le16(38);
419 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
420 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
421 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
422 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
426 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
428 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
429 pneg_ctxt->DataLength =
430 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
431 - sizeof(struct smb2_neg_context));
432 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
433 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
434 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
435 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
439 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
441 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
442 unsigned short num_algs = 1; /* number of signing algorithms sent */
444 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
446 * Context Data length must be rounded to multiple of 8 for some servers
448 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
449 sizeof(struct smb2_neg_context) +
450 (num_algs * sizeof(u16)), 8));
451 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
452 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
454 ctxt_len += sizeof(__le16) * num_algs;
455 ctxt_len = ALIGN(ctxt_len, 8);
457 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
461 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
463 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
464 if (require_gcm_256) {
465 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
466 pneg_ctxt->CipherCount = cpu_to_le16(1);
467 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
468 } else if (enable_gcm_256) {
469 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
470 pneg_ctxt->CipherCount = cpu_to_le16(3);
471 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
472 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
473 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
475 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
476 pneg_ctxt->CipherCount = cpu_to_le16(2);
477 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
478 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
483 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
485 struct nls_table *cp = load_nls_default();
487 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
489 /* copy up to max of first 100 bytes of server name to NetName field */
490 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
491 /* context size is DataLength + minimal smb2_neg_context */
492 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
496 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
498 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
499 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
500 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
501 pneg_ctxt->Name[0] = 0x93;
502 pneg_ctxt->Name[1] = 0xAD;
503 pneg_ctxt->Name[2] = 0x25;
504 pneg_ctxt->Name[3] = 0x50;
505 pneg_ctxt->Name[4] = 0x9C;
506 pneg_ctxt->Name[5] = 0xB4;
507 pneg_ctxt->Name[6] = 0x11;
508 pneg_ctxt->Name[7] = 0xE7;
509 pneg_ctxt->Name[8] = 0xB4;
510 pneg_ctxt->Name[9] = 0x23;
511 pneg_ctxt->Name[10] = 0x83;
512 pneg_ctxt->Name[11] = 0xDE;
513 pneg_ctxt->Name[12] = 0x96;
514 pneg_ctxt->Name[13] = 0x8B;
515 pneg_ctxt->Name[14] = 0xCD;
516 pneg_ctxt->Name[15] = 0x7C;
520 assemble_neg_contexts(struct smb2_negotiate_req *req,
521 struct TCP_Server_Info *server, unsigned int *total_len)
523 unsigned int ctxt_len, neg_context_count;
524 struct TCP_Server_Info *pserver;
528 if (*total_len > 200) {
529 /* In case length corrupted don't want to overrun smb buffer */
530 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
535 * round up total_len of fixed part of SMB3 negotiate request to 8
536 * byte boundary before adding negotiate contexts
538 *total_len = ALIGN(*total_len, 8);
540 pneg_ctxt = (*total_len) + (char *)req;
541 req->NegotiateContextOffset = cpu_to_le32(*total_len);
543 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
544 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
545 *total_len += ctxt_len;
546 pneg_ctxt += ctxt_len;
548 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
549 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
550 *total_len += ctxt_len;
551 pneg_ctxt += ctxt_len;
554 * secondary channels don't have the hostname field populated
555 * use the hostname field in the primary channel instead
557 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
558 cifs_server_lock(pserver);
559 hostname = pserver->hostname;
560 if (hostname && (hostname[0] != 0)) {
561 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
563 *total_len += ctxt_len;
564 pneg_ctxt += ctxt_len;
565 neg_context_count = 3;
567 neg_context_count = 2;
568 cifs_server_unlock(pserver);
570 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
571 *total_len += sizeof(struct smb2_posix_neg_context);
572 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
575 if (server->compress_algorithm) {
576 build_compression_ctxt((struct smb2_compression_capabilities_context *)
578 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
579 *total_len += ctxt_len;
580 pneg_ctxt += ctxt_len;
584 if (enable_negotiate_signing) {
585 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
587 *total_len += ctxt_len;
588 pneg_ctxt += ctxt_len;
592 /* check for and add transport_capabilities and signing capabilities */
593 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
597 /* If invalid preauth context warn but use what we requested, SHA-512 */
598 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
600 unsigned int len = le16_to_cpu(ctxt->DataLength);
603 * Caller checked that DataLength remains within SMB boundary. We still
604 * need to confirm that one HashAlgorithms member is accounted for.
606 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
607 pr_warn_once("server sent bad preauth context\n");
609 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
610 pr_warn_once("server sent invalid SaltLength\n");
613 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
614 pr_warn_once("Invalid SMB3 hash algorithm count\n");
615 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
616 pr_warn_once("unknown SMB3 hash algorithm\n");
619 static void decode_compress_ctx(struct TCP_Server_Info *server,
620 struct smb2_compression_capabilities_context *ctxt)
622 unsigned int len = le16_to_cpu(ctxt->DataLength);
625 * Caller checked that DataLength remains within SMB boundary. We still
626 * need to confirm that one CompressionAlgorithms member is accounted
630 pr_warn_once("server sent bad compression cntxt\n");
633 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
634 pr_warn_once("Invalid SMB3 compress algorithm count\n");
637 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
638 pr_warn_once("unknown compression algorithm\n");
641 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
644 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
645 struct smb2_encryption_neg_context *ctxt)
647 unsigned int len = le16_to_cpu(ctxt->DataLength);
649 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
651 * Caller checked that DataLength remains within SMB boundary. We still
652 * need to confirm that one Cipher flexible array member is accounted
655 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
656 pr_warn_once("server sent bad crypto ctxt len\n");
660 if (le16_to_cpu(ctxt->CipherCount) != 1) {
661 pr_warn_once("Invalid SMB3.11 cipher count\n");
664 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
665 if (require_gcm_256) {
666 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
667 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
670 } else if (ctxt->Ciphers[0] == 0) {
672 * e.g. if server only supported AES256_CCM (very unlikely)
673 * or server supported no encryption types or had all disabled.
674 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
675 * in which mount requested encryption ("seal") checks later
676 * on during tree connection will return proper rc, but if
677 * seal not requested by client, since server is allowed to
678 * return 0 to indicate no supported cipher, we can't fail here
680 server->cipher_type = 0;
681 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
682 pr_warn_once("Server does not support requested encryption types\n");
684 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
685 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
686 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
687 /* server returned a cipher we didn't ask for */
688 pr_warn_once("Invalid SMB3.11 cipher returned\n");
691 server->cipher_type = ctxt->Ciphers[0];
692 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
696 static void decode_signing_ctx(struct TCP_Server_Info *server,
697 struct smb2_signing_capabilities *pctxt)
699 unsigned int len = le16_to_cpu(pctxt->DataLength);
702 * Caller checked that DataLength remains within SMB boundary. We still
703 * need to confirm that one SigningAlgorithms flexible array member is
706 if ((len < 4) || (len > 16)) {
707 pr_warn_once("server sent bad signing negcontext\n");
710 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
711 pr_warn_once("Invalid signing algorithm count\n");
714 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
715 pr_warn_once("unknown signing algorithm\n");
719 server->signing_negotiated = true;
720 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
721 cifs_dbg(FYI, "signing algorithm %d chosen\n",
722 server->signing_algorithm);
726 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
727 struct TCP_Server_Info *server,
728 unsigned int len_of_smb)
730 struct smb2_neg_context *pctx;
731 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
732 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
733 unsigned int len_of_ctxts, i;
736 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
737 if (len_of_smb <= offset) {
738 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
742 len_of_ctxts = len_of_smb - offset;
744 for (i = 0; i < ctxt_cnt; i++) {
746 /* check that offset is not beyond end of SMB */
747 if (len_of_ctxts < sizeof(struct smb2_neg_context))
750 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
751 clen = sizeof(struct smb2_neg_context)
752 + le16_to_cpu(pctx->DataLength);
754 * 2.2.4 SMB2 NEGOTIATE Response
755 * Subsequent negotiate contexts MUST appear at the first 8-byte
756 * aligned offset following the previous negotiate context.
758 if (i + 1 != ctxt_cnt)
759 clen = ALIGN(clen, 8);
760 if (clen > len_of_ctxts)
763 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
764 decode_preauth_context(
765 (struct smb2_preauth_neg_context *)pctx);
766 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
767 rc = decode_encrypt_ctx(server,
768 (struct smb2_encryption_neg_context *)pctx);
769 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
770 decode_compress_ctx(server,
771 (struct smb2_compression_capabilities_context *)pctx);
772 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
773 server->posix_ext_supported = true;
774 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
775 decode_signing_ctx(server,
776 (struct smb2_signing_capabilities *)pctx);
778 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
779 le16_to_cpu(pctx->ContextType));
784 len_of_ctxts -= clen;
789 static struct create_posix *
790 create_posix_buf(umode_t mode)
792 struct create_posix *buf;
794 buf = kzalloc(sizeof(struct create_posix),
799 buf->ccontext.DataOffset =
800 cpu_to_le16(offsetof(struct create_posix, Mode));
801 buf->ccontext.DataLength = cpu_to_le32(4);
802 buf->ccontext.NameOffset =
803 cpu_to_le16(offsetof(struct create_posix, Name));
804 buf->ccontext.NameLength = cpu_to_le16(16);
806 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
817 buf->Name[10] = 0x83;
818 buf->Name[11] = 0xDE;
819 buf->Name[12] = 0x96;
820 buf->Name[13] = 0x8B;
821 buf->Name[14] = 0xCD;
822 buf->Name[15] = 0x7C;
823 buf->Mode = cpu_to_le32(mode);
824 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
829 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
831 unsigned int num = *num_iovec;
833 iov[num].iov_base = create_posix_buf(mode);
834 if (mode == ACL_NO_MODE)
835 cifs_dbg(FYI, "Invalid mode\n");
836 if (iov[num].iov_base == NULL)
838 iov[num].iov_len = sizeof(struct create_posix);
839 *num_iovec = num + 1;
846 * SMB2 Worker functions follow:
848 * The general structure of the worker functions is:
849 * 1) Call smb2_init (assembles SMB2 header)
850 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
851 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
852 * 4) Decode SMB2 command specific fields in the fixed length area
853 * 5) Decode variable length data area (if any for this SMB2 command type)
854 * 6) Call free smb buffer
860 SMB2_negotiate(const unsigned int xid,
861 struct cifs_ses *ses,
862 struct TCP_Server_Info *server)
864 struct smb_rqst rqst;
865 struct smb2_negotiate_req *req;
866 struct smb2_negotiate_rsp *rsp;
871 int blob_offset, blob_length;
873 int flags = CIFS_NEG_OP;
874 unsigned int total_len;
876 cifs_dbg(FYI, "Negotiate protocol\n");
879 WARN(1, "%s: server is NULL!\n", __func__);
883 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
884 (void **) &req, &total_len);
888 req->hdr.SessionId = 0;
890 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
891 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
893 if (strcmp(server->vals->version_string,
894 SMB3ANY_VERSION_STRING) == 0) {
895 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
896 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
897 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
898 req->DialectCount = cpu_to_le16(3);
900 } else if (strcmp(server->vals->version_string,
901 SMBDEFAULT_VERSION_STRING) == 0) {
902 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
903 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
904 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
905 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
906 req->DialectCount = cpu_to_le16(4);
909 /* otherwise send specific dialect */
910 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
911 req->DialectCount = cpu_to_le16(1);
915 /* only one of SMB2 signing flags may be set in SMB2 request */
917 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
918 else if (global_secflags & CIFSSEC_MAY_SIGN)
919 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
921 req->SecurityMode = 0;
923 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
924 if (ses->chan_max > 1)
925 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
927 /* ClientGUID must be zero for SMB2.02 dialect */
928 if (server->vals->protocol_id == SMB20_PROT_ID)
929 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
931 memcpy(req->ClientGUID, server->client_guid,
932 SMB2_CLIENT_GUID_SIZE);
933 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
934 (strcmp(server->vals->version_string,
935 SMB3ANY_VERSION_STRING) == 0) ||
936 (strcmp(server->vals->version_string,
937 SMBDEFAULT_VERSION_STRING) == 0))
938 assemble_neg_contexts(req, server, &total_len);
940 iov[0].iov_base = (char *)req;
941 iov[0].iov_len = total_len;
943 memset(&rqst, 0, sizeof(struct smb_rqst));
947 rc = cifs_send_recv(xid, ses, server,
948 &rqst, &resp_buftype, flags, &rsp_iov);
949 cifs_small_buf_release(req);
950 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
952 * No tcon so can't do
953 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
955 if (rc == -EOPNOTSUPP) {
956 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");
962 if (strcmp(server->vals->version_string,
963 SMB3ANY_VERSION_STRING) == 0) {
964 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
966 "SMB2 dialect returned but not requested\n");
968 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
970 "SMB2.1 dialect returned but not requested\n");
972 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
973 /* ops set to 3.0 by default for default so update */
974 server->ops = &smb311_operations;
975 server->vals = &smb311_values;
977 } else if (strcmp(server->vals->version_string,
978 SMBDEFAULT_VERSION_STRING) == 0) {
979 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
981 "SMB2 dialect returned but not requested\n");
983 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
984 /* ops set to 3.0 by default for default so update */
985 server->ops = &smb21_operations;
986 server->vals = &smb21_values;
987 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
988 server->ops = &smb311_operations;
989 server->vals = &smb311_values;
991 } else if (le16_to_cpu(rsp->DialectRevision) !=
992 server->vals->protocol_id) {
993 /* if requested single dialect ensure returned dialect matched */
994 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
995 le16_to_cpu(rsp->DialectRevision));
999 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1001 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1002 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1003 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1004 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1005 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1006 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1007 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1008 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1009 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1010 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1012 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1013 le16_to_cpu(rsp->DialectRevision));
1018 server->dialect = le16_to_cpu(rsp->DialectRevision);
1021 * Keep a copy of the hash after negprot. This hash will be
1022 * the starting hash value for all sessions made from this
1025 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1026 SMB2_PREAUTH_HASH_SIZE);
1028 /* SMB2 only has an extended negflavor */
1029 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1030 /* set it to the maximum buffer size value we can send with 1 credit */
1031 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1032 SMB2_MAX_BUFFER_SIZE);
1033 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1034 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1035 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1036 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1037 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1039 server->capabilities = le32_to_cpu(rsp->Capabilities);
1040 /* Internal types */
1041 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1044 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1045 * Set the cipher type manually.
1047 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1048 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1050 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1051 (struct smb2_hdr *)rsp);
1053 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1055 * ses->sectype = RawNTLMSSP;
1056 * but for time being this is our only auth choice so doesn't matter.
1057 * We just found a server which sets blob length to zero expecting raw.
1059 if (blob_length == 0) {
1060 cifs_dbg(FYI, "missing security blob on negprot\n");
1061 server->sec_ntlmssp = true;
1064 rc = cifs_enable_signing(server, ses->sign);
1068 rc = decode_negTokenInit(security_blob, blob_length, server);
1075 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1076 if (rsp->NegotiateContextCount)
1077 rc = smb311_decode_neg_context(rsp, server,
1080 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1083 free_rsp_buf(resp_buftype, rsp);
1087 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1090 struct validate_negotiate_info_req *pneg_inbuf;
1091 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1093 u32 inbuflen; /* max of 4 dialects */
1094 struct TCP_Server_Info *server = tcon->ses->server;
1096 cifs_dbg(FYI, "validate negotiate\n");
1098 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1099 if (server->dialect == SMB311_PROT_ID)
1103 * validation ioctl must be signed, so no point sending this if we
1104 * can not sign it (ie are not known user). Even if signing is not
1105 * required (enabled but not negotiated), in those cases we selectively
1106 * sign just this, the first and only signed request on a connection.
1107 * Having validation of negotiate info helps reduce attack vectors.
1109 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1110 return 0; /* validation requires signing */
1112 if (tcon->ses->user_name == NULL) {
1113 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1114 return 0; /* validation requires signing */
1117 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1118 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1120 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1124 pneg_inbuf->Capabilities =
1125 cpu_to_le32(server->vals->req_capabilities);
1126 if (tcon->ses->chan_max > 1)
1127 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1129 memcpy(pneg_inbuf->Guid, server->client_guid,
1130 SMB2_CLIENT_GUID_SIZE);
1132 if (tcon->ses->sign)
1133 pneg_inbuf->SecurityMode =
1134 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1135 else if (global_secflags & CIFSSEC_MAY_SIGN)
1136 pneg_inbuf->SecurityMode =
1137 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1139 pneg_inbuf->SecurityMode = 0;
1142 if (strcmp(server->vals->version_string,
1143 SMB3ANY_VERSION_STRING) == 0) {
1144 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1145 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1146 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1147 pneg_inbuf->DialectCount = cpu_to_le16(3);
1148 /* SMB 2.1 not included so subtract one dialect from len */
1149 inbuflen = sizeof(*pneg_inbuf) -
1150 (sizeof(pneg_inbuf->Dialects[0]));
1151 } else if (strcmp(server->vals->version_string,
1152 SMBDEFAULT_VERSION_STRING) == 0) {
1153 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1154 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1155 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1156 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1157 pneg_inbuf->DialectCount = cpu_to_le16(4);
1158 /* structure is big enough for 4 dialects */
1159 inbuflen = sizeof(*pneg_inbuf);
1161 /* otherwise specific dialect was requested */
1162 pneg_inbuf->Dialects[0] =
1163 cpu_to_le16(server->vals->protocol_id);
1164 pneg_inbuf->DialectCount = cpu_to_le16(1);
1165 /* structure is big enough for 4 dialects, sending only 1 */
1166 inbuflen = sizeof(*pneg_inbuf) -
1167 sizeof(pneg_inbuf->Dialects[0]) * 3;
1170 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1171 FSCTL_VALIDATE_NEGOTIATE_INFO,
1172 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1173 (char **)&pneg_rsp, &rsplen);
1174 if (rc == -EOPNOTSUPP) {
1176 * Old Windows versions or Netapp SMB server can return
1177 * not supported error. Client should accept it.
1179 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1181 goto out_free_inbuf;
1182 } else if (rc != 0) {
1183 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1186 goto out_free_inbuf;
1190 if (rsplen != sizeof(*pneg_rsp)) {
1191 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1194 /* relax check since Mac returns max bufsize allowed on ioctl */
1195 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1199 /* check validate negotiate info response matches what we got earlier */
1200 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1203 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1206 /* do not validate server guid because not saved at negprot time yet */
1208 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1209 SMB2_LARGE_FILES) != server->capabilities)
1212 /* validate negotiate successful */
1214 cifs_dbg(FYI, "validate negotiate info successful\n");
1218 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1227 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1229 switch (requested) {
1236 if (server->sec_ntlmssp &&
1237 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1239 if ((server->sec_kerberos || server->sec_mskerberos) &&
1240 (global_secflags & CIFSSEC_MAY_KRB5))
1248 struct SMB2_sess_data {
1250 struct cifs_ses *ses;
1251 struct TCP_Server_Info *server;
1252 struct nls_table *nls_cp;
1253 void (*func)(struct SMB2_sess_data *);
1255 u64 previous_session;
1257 /* we will send the SMB in three pieces:
1258 * a fixed length beginning part, an optional
1259 * SPNEGO blob (which can be zero length), and a
1260 * last part which will include the strings
1261 * and rest of bcc area. This allows us to avoid
1262 * a large buffer 17K allocation
1269 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1272 struct cifs_ses *ses = sess_data->ses;
1273 struct TCP_Server_Info *server = sess_data->server;
1274 struct smb2_sess_setup_req *req;
1275 unsigned int total_len;
1276 bool is_binding = false;
1278 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1284 spin_lock(&ses->ses_lock);
1285 is_binding = (ses->ses_status == SES_GOOD);
1286 spin_unlock(&ses->ses_lock);
1289 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1290 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1291 req->PreviousSessionId = 0;
1292 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1293 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1295 /* First session, not a reauthenticate */
1296 req->hdr.SessionId = 0;
1298 * if reconnect, we need to send previous sess id
1301 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1302 req->Flags = 0; /* MBZ */
1303 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1304 sess_data->previous_session);
1307 /* enough to enable echos and oplocks and one max size write */
1308 req->hdr.CreditRequest = cpu_to_le16(130);
1310 /* only one of SMB2 signing flags may be set in SMB2 request */
1312 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1313 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1314 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1316 req->SecurityMode = 0;
1318 #ifdef CONFIG_CIFS_DFS_UPCALL
1319 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1321 req->Capabilities = 0;
1322 #endif /* DFS_UPCALL */
1324 req->Channel = 0; /* MBZ */
1326 sess_data->iov[0].iov_base = (char *)req;
1328 sess_data->iov[0].iov_len = total_len - 1;
1330 * This variable will be used to clear the buffer
1331 * allocated above in case of any error in the calling function.
1333 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1339 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1341 struct kvec *iov = sess_data->iov;
1343 /* iov[1] is already freed by caller */
1344 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1345 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1347 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1348 sess_data->buf0_type = CIFS_NO_BUFFER;
1352 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1355 struct smb_rqst rqst;
1356 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1357 struct kvec rsp_iov = { NULL, 0 };
1359 /* Testing shows that buffer offset must be at location of Buffer[0] */
1360 req->SecurityBufferOffset =
1361 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1362 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1364 memset(&rqst, 0, sizeof(struct smb_rqst));
1365 rqst.rq_iov = sess_data->iov;
1368 /* BB add code to build os and lm fields */
1369 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1372 &sess_data->buf0_type,
1373 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1374 cifs_small_buf_release(sess_data->iov[0].iov_base);
1375 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1381 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1384 struct cifs_ses *ses = sess_data->ses;
1385 struct TCP_Server_Info *server = sess_data->server;
1387 cifs_server_lock(server);
1388 if (server->ops->generate_signingkey) {
1389 rc = server->ops->generate_signingkey(ses, server);
1392 "SMB3 session key generation failed\n");
1393 cifs_server_unlock(server);
1397 if (!server->session_estab) {
1398 server->sequence_number = 0x2;
1399 server->session_estab = true;
1401 cifs_server_unlock(server);
1403 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1407 #ifdef CONFIG_CIFS_UPCALL
1409 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1412 struct cifs_ses *ses = sess_data->ses;
1413 struct TCP_Server_Info *server = sess_data->server;
1414 struct cifs_spnego_msg *msg;
1415 struct key *spnego_key = NULL;
1416 struct smb2_sess_setup_rsp *rsp = NULL;
1417 bool is_binding = false;
1419 rc = SMB2_sess_alloc_buffer(sess_data);
1423 spnego_key = cifs_get_spnego_key(ses, server);
1424 if (IS_ERR(spnego_key)) {
1425 rc = PTR_ERR(spnego_key);
1427 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1432 msg = spnego_key->payload.data[0];
1434 * check version field to make sure that cifs.upcall is
1435 * sending us a response in an expected form
1437 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1438 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1439 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1441 goto out_put_spnego_key;
1444 spin_lock(&ses->ses_lock);
1445 is_binding = (ses->ses_status == SES_GOOD);
1446 spin_unlock(&ses->ses_lock);
1448 /* keep session key if binding */
1450 kfree_sensitive(ses->auth_key.response);
1451 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1453 if (!ses->auth_key.response) {
1454 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1457 goto out_put_spnego_key;
1459 ses->auth_key.len = msg->sesskey_len;
1462 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1463 sess_data->iov[1].iov_len = msg->secblob_len;
1465 rc = SMB2_sess_sendreceive(sess_data);
1467 goto out_put_spnego_key;
1469 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1470 /* keep session id and flags if binding */
1472 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1473 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1476 rc = SMB2_sess_establish_session(sess_data);
1478 key_invalidate(spnego_key);
1479 key_put(spnego_key);
1481 kfree_sensitive(ses->auth_key.response);
1482 ses->auth_key.response = NULL;
1483 ses->auth_key.len = 0;
1486 sess_data->result = rc;
1487 sess_data->func = NULL;
1488 SMB2_sess_free_buffer(sess_data);
1492 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1494 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1495 sess_data->result = -EOPNOTSUPP;
1496 sess_data->func = NULL;
1501 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1504 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1507 struct cifs_ses *ses = sess_data->ses;
1508 struct TCP_Server_Info *server = sess_data->server;
1509 struct smb2_sess_setup_rsp *rsp = NULL;
1510 unsigned char *ntlmssp_blob = NULL;
1511 bool use_spnego = false; /* else use raw ntlmssp */
1512 u16 blob_length = 0;
1513 bool is_binding = false;
1516 * If memory allocation is successful, caller of this function
1519 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1520 if (!ses->ntlmssp) {
1524 ses->ntlmssp->sesskey_per_smbsess = true;
1526 rc = SMB2_sess_alloc_buffer(sess_data);
1530 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1531 &blob_length, ses, server,
1537 /* BB eventually need to add this */
1538 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1542 sess_data->iov[1].iov_base = ntlmssp_blob;
1543 sess_data->iov[1].iov_len = blob_length;
1545 rc = SMB2_sess_sendreceive(sess_data);
1546 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1548 /* If true, rc here is expected and not an error */
1549 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1550 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1556 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1557 le16_to_cpu(rsp->SecurityBufferOffset)) {
1558 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1559 le16_to_cpu(rsp->SecurityBufferOffset));
1563 rc = decode_ntlmssp_challenge(rsp->Buffer,
1564 le16_to_cpu(rsp->SecurityBufferLength), ses);
1568 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1570 spin_lock(&ses->ses_lock);
1571 is_binding = (ses->ses_status == SES_GOOD);
1572 spin_unlock(&ses->ses_lock);
1574 /* keep existing ses id and flags if binding */
1576 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1577 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1581 kfree_sensitive(ntlmssp_blob);
1582 SMB2_sess_free_buffer(sess_data);
1584 sess_data->result = 0;
1585 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1589 kfree_sensitive(ses->ntlmssp);
1590 ses->ntlmssp = NULL;
1591 sess_data->result = rc;
1592 sess_data->func = NULL;
1596 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1599 struct cifs_ses *ses = sess_data->ses;
1600 struct TCP_Server_Info *server = sess_data->server;
1601 struct smb2_sess_setup_req *req;
1602 struct smb2_sess_setup_rsp *rsp = NULL;
1603 unsigned char *ntlmssp_blob = NULL;
1604 bool use_spnego = false; /* else use raw ntlmssp */
1605 u16 blob_length = 0;
1606 bool is_binding = false;
1608 rc = SMB2_sess_alloc_buffer(sess_data);
1612 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1613 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1615 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1619 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1624 /* BB eventually need to add this */
1625 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1629 sess_data->iov[1].iov_base = ntlmssp_blob;
1630 sess_data->iov[1].iov_len = blob_length;
1632 rc = SMB2_sess_sendreceive(sess_data);
1636 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1638 spin_lock(&ses->ses_lock);
1639 is_binding = (ses->ses_status == SES_GOOD);
1640 spin_unlock(&ses->ses_lock);
1642 /* keep existing ses id and flags if binding */
1644 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1645 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1648 rc = SMB2_sess_establish_session(sess_data);
1649 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1650 if (ses->server->dialect < SMB30_PROT_ID) {
1651 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1653 * The session id is opaque in terms of endianness, so we can't
1654 * print it as a long long. we dump it as we got it on the wire
1656 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1658 cifs_dbg(VFS, "Session Key %*ph\n",
1659 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1660 cifs_dbg(VFS, "Signing Key %*ph\n",
1661 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1665 kfree_sensitive(ntlmssp_blob);
1666 SMB2_sess_free_buffer(sess_data);
1667 kfree_sensitive(ses->ntlmssp);
1668 ses->ntlmssp = NULL;
1669 sess_data->result = rc;
1670 sess_data->func = NULL;
1674 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1677 struct cifs_ses *ses = sess_data->ses;
1678 struct TCP_Server_Info *server = sess_data->server;
1680 type = smb2_select_sectype(server, ses->sectype);
1681 cifs_dbg(FYI, "sess setup type %d\n", type);
1682 if (type == Unspecified) {
1683 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1689 sess_data->func = SMB2_auth_kerberos;
1692 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1695 cifs_dbg(VFS, "secType %d not supported!\n", type);
1703 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1704 struct TCP_Server_Info *server,
1705 const struct nls_table *nls_cp)
1708 struct SMB2_sess_data *sess_data;
1710 cifs_dbg(FYI, "Session Setup\n");
1713 WARN(1, "%s: server is NULL!\n", __func__);
1717 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1721 sess_data->xid = xid;
1722 sess_data->ses = ses;
1723 sess_data->server = server;
1724 sess_data->buf0_type = CIFS_NO_BUFFER;
1725 sess_data->nls_cp = (struct nls_table *) nls_cp;
1726 sess_data->previous_session = ses->Suid;
1728 rc = SMB2_select_sec(sess_data);
1733 * Initialize the session hash with the server one.
1735 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1736 SMB2_PREAUTH_HASH_SIZE);
1738 while (sess_data->func)
1739 sess_data->func(sess_data);
1741 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1742 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1743 rc = sess_data->result;
1745 kfree_sensitive(sess_data);
1750 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1752 struct smb_rqst rqst;
1753 struct smb2_logoff_req *req; /* response is also trivial struct */
1755 struct TCP_Server_Info *server;
1757 unsigned int total_len;
1759 struct kvec rsp_iov;
1762 cifs_dbg(FYI, "disconnect session %p\n", ses);
1764 if (ses && (ses->server))
1765 server = ses->server;
1769 /* no need to send SMB logoff if uid already closed due to reconnect */
1770 spin_lock(&ses->chan_lock);
1771 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1772 spin_unlock(&ses->chan_lock);
1773 goto smb2_session_already_dead;
1775 spin_unlock(&ses->chan_lock);
1777 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1778 (void **) &req, &total_len);
1782 /* since no tcon, smb2_init can not do this, so do here */
1783 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1785 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1786 flags |= CIFS_TRANSFORM_REQ;
1787 else if (server->sign)
1788 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1790 flags |= CIFS_NO_RSP_BUF;
1792 iov[0].iov_base = (char *)req;
1793 iov[0].iov_len = total_len;
1795 memset(&rqst, 0, sizeof(struct smb_rqst));
1799 rc = cifs_send_recv(xid, ses, ses->server,
1800 &rqst, &resp_buf_type, flags, &rsp_iov);
1801 cifs_small_buf_release(req);
1803 * No tcon so can't do
1804 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1807 smb2_session_already_dead:
1811 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1813 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1816 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1818 /* These are similar values to what Windows uses */
1819 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1821 tcon->max_chunks = 256;
1822 tcon->max_bytes_chunk = 1048576;
1823 tcon->max_bytes_copy = 16777216;
1827 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1828 struct cifs_tcon *tcon, const struct nls_table *cp)
1830 struct smb_rqst rqst;
1831 struct smb2_tree_connect_req *req;
1832 struct smb2_tree_connect_rsp *rsp = NULL;
1834 struct kvec rsp_iov = { NULL, 0 };
1838 __le16 *unc_path = NULL;
1840 unsigned int total_len;
1841 struct TCP_Server_Info *server;
1843 /* always use master channel */
1844 server = ses->server;
1846 cifs_dbg(FYI, "TCON\n");
1848 if (!server || !tree)
1851 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1852 if (unc_path == NULL)
1855 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
1856 if (unc_path_len <= 0) {
1862 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1864 atomic_set(&tcon->num_remote_opens, 0);
1865 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1866 (void **) &req, &total_len);
1872 if (smb3_encryption_required(tcon))
1873 flags |= CIFS_TRANSFORM_REQ;
1875 iov[0].iov_base = (char *)req;
1877 iov[0].iov_len = total_len - 1;
1879 /* Testing shows that buffer offset must be at location of Buffer[0] */
1880 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
1881 req->PathLength = cpu_to_le16(unc_path_len);
1882 iov[1].iov_base = unc_path;
1883 iov[1].iov_len = unc_path_len;
1886 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1887 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1888 * (Samba servers don't always set the flag so also check if null user)
1890 if ((server->dialect == SMB311_PROT_ID) &&
1891 !smb3_encryption_required(tcon) &&
1892 !(ses->session_flags &
1893 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1894 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1895 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1897 memset(&rqst, 0, sizeof(struct smb_rqst));
1901 /* Need 64 for max size write so ask for more in case not there yet */
1902 req->hdr.CreditRequest = cpu_to_le16(64);
1904 rc = cifs_send_recv(xid, ses, server,
1905 &rqst, &resp_buftype, flags, &rsp_iov);
1906 cifs_small_buf_release(req);
1907 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1908 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1909 if ((rc != 0) || (rsp == NULL)) {
1910 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1911 tcon->need_reconnect = true;
1912 goto tcon_error_exit;
1915 switch (rsp->ShareType) {
1916 case SMB2_SHARE_TYPE_DISK:
1917 cifs_dbg(FYI, "connection to disk share\n");
1919 case SMB2_SHARE_TYPE_PIPE:
1921 cifs_dbg(FYI, "connection to pipe share\n");
1923 case SMB2_SHARE_TYPE_PRINT:
1925 cifs_dbg(FYI, "connection to printer\n");
1928 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1930 goto tcon_error_exit;
1933 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1934 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1935 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1936 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
1937 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
1939 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1940 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1941 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1944 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1945 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1947 init_copy_chunk_defaults(tcon);
1948 if (server->ops->validate_negotiate)
1949 rc = server->ops->validate_negotiate(xid, tcon);
1950 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
1951 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
1952 server->nosharesock = true;
1955 free_rsp_buf(resp_buftype, rsp);
1960 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
1961 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1966 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1968 struct smb_rqst rqst;
1969 struct smb2_tree_disconnect_req *req; /* response is trivial */
1971 struct cifs_ses *ses = tcon->ses;
1973 unsigned int total_len;
1975 struct kvec rsp_iov;
1978 cifs_dbg(FYI, "Tree Disconnect\n");
1980 if (!ses || !(ses->server))
1983 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
1984 spin_lock(&ses->chan_lock);
1985 if ((tcon->need_reconnect) ||
1986 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
1987 spin_unlock(&ses->chan_lock);
1990 spin_unlock(&ses->chan_lock);
1992 invalidate_all_cached_dirs(tcon);
1994 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
2000 if (smb3_encryption_required(tcon))
2001 flags |= CIFS_TRANSFORM_REQ;
2003 flags |= CIFS_NO_RSP_BUF;
2005 iov[0].iov_base = (char *)req;
2006 iov[0].iov_len = total_len;
2008 memset(&rqst, 0, sizeof(struct smb_rqst));
2012 rc = cifs_send_recv(xid, ses, ses->server,
2013 &rqst, &resp_buf_type, flags, &rsp_iov);
2014 cifs_small_buf_release(req);
2016 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2017 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2019 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2025 static struct create_durable *
2026 create_durable_buf(void)
2028 struct create_durable *buf;
2030 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2034 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2035 (struct create_durable, Data));
2036 buf->ccontext.DataLength = cpu_to_le32(16);
2037 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2038 (struct create_durable, Name));
2039 buf->ccontext.NameLength = cpu_to_le16(4);
2040 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2048 static struct create_durable *
2049 create_reconnect_durable_buf(struct cifs_fid *fid)
2051 struct create_durable *buf;
2053 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2057 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2058 (struct create_durable, Data));
2059 buf->ccontext.DataLength = cpu_to_le32(16);
2060 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2061 (struct create_durable, Name));
2062 buf->ccontext.NameLength = cpu_to_le16(4);
2063 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2064 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2065 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2074 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2076 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2078 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2079 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2080 buf->IndexNumber = pdisk_id->DiskFileId;
2084 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2085 struct create_posix_rsp *posix)
2088 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2089 u8 *end = beg + le32_to_cpu(cc->DataLength);
2092 memset(posix, 0, sizeof(*posix));
2094 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2095 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2096 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2099 sid_len = posix_info_sid_size(sid, end);
2101 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2104 memcpy(&posix->owner, sid, sid_len);
2106 sid = sid + sid_len;
2107 sid_len = posix_info_sid_size(sid, end);
2109 cifs_dbg(VFS, "bad group sid in posix create response\n");
2112 memcpy(&posix->group, sid, sid_len);
2114 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2115 posix->nlink, posix->mode, posix->reparse_tag);
2119 smb2_parse_contexts(struct TCP_Server_Info *server,
2120 struct smb2_create_rsp *rsp,
2121 unsigned int *epoch, char *lease_key, __u8 *oplock,
2122 struct smb2_file_all_info *buf,
2123 struct create_posix_rsp *posix)
2126 struct create_context *cc;
2128 unsigned int remaining;
2130 static const char smb3_create_tag_posix[] = {
2131 0x93, 0xAD, 0x25, 0x50, 0x9C,
2132 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2133 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2137 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2138 remaining = le32_to_cpu(rsp->CreateContextsLength);
2139 cc = (struct create_context *)data_offset;
2141 /* Initialize inode number to 0 in case no valid data in qfid context */
2143 buf->IndexNumber = 0;
2145 while (remaining >= sizeof(struct create_context)) {
2146 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2147 if (le16_to_cpu(cc->NameLength) == 4 &&
2148 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2149 *oplock = server->ops->parse_lease_buf(cc, epoch,
2151 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2152 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2153 parse_query_id_ctxt(cc, buf);
2154 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2156 memcmp(name, smb3_create_tag_posix, 16) == 0)
2157 parse_posix_ctxt(cc, buf, posix);
2160 cifs_dbg(FYI, "Context not matched with len %d\n",
2161 le16_to_cpu(cc->NameLength));
2162 cifs_dump_mem("Cctxt name: ", name, 4);
2165 next = le32_to_cpu(cc->Next);
2169 cc = (struct create_context *)((char *)cc + next);
2172 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2173 *oplock = rsp->OplockLevel;
2179 add_lease_context(struct TCP_Server_Info *server,
2180 struct smb2_create_req *req,
2182 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2184 unsigned int num = *num_iovec;
2186 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2187 if (iov[num].iov_base == NULL)
2189 iov[num].iov_len = server->vals->create_lease_size;
2190 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2191 *num_iovec = num + 1;
2195 static struct create_durable_v2 *
2196 create_durable_v2_buf(struct cifs_open_parms *oparms)
2198 struct cifs_fid *pfid = oparms->fid;
2199 struct create_durable_v2 *buf;
2201 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2205 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2206 (struct create_durable_v2, dcontext));
2207 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2208 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2209 (struct create_durable_v2, Name));
2210 buf->ccontext.NameLength = cpu_to_le16(4);
2213 * NB: Handle timeout defaults to 0, which allows server to choose
2214 * (most servers default to 120 seconds) and most clients default to 0.
2215 * This can be overridden at mount ("handletimeout=") if the user wants
2216 * a different persistent (or resilient) handle timeout for all opens
2217 * opens on a particular SMB3 mount.
2219 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2220 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2221 generate_random_uuid(buf->dcontext.CreateGuid);
2222 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2224 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2232 static struct create_durable_handle_reconnect_v2 *
2233 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2235 struct create_durable_handle_reconnect_v2 *buf;
2237 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2242 buf->ccontext.DataOffset =
2243 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2245 buf->ccontext.DataLength =
2246 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2247 buf->ccontext.NameOffset =
2248 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2250 buf->ccontext.NameLength = cpu_to_le16(4);
2252 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2253 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2254 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2255 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2257 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2266 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2267 struct cifs_open_parms *oparms)
2269 unsigned int num = *num_iovec;
2271 iov[num].iov_base = create_durable_v2_buf(oparms);
2272 if (iov[num].iov_base == NULL)
2274 iov[num].iov_len = sizeof(struct create_durable_v2);
2275 *num_iovec = num + 1;
2280 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2281 struct cifs_open_parms *oparms)
2283 unsigned int num = *num_iovec;
2285 /* indicate that we don't need to relock the file */
2286 oparms->reconnect = false;
2288 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2289 if (iov[num].iov_base == NULL)
2291 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2292 *num_iovec = num + 1;
2297 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2298 struct cifs_open_parms *oparms, bool use_persistent)
2300 unsigned int num = *num_iovec;
2302 if (use_persistent) {
2303 if (oparms->reconnect)
2304 return add_durable_reconnect_v2_context(iov, num_iovec,
2307 return add_durable_v2_context(iov, num_iovec, oparms);
2310 if (oparms->reconnect) {
2311 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2312 /* indicate that we don't need to relock the file */
2313 oparms->reconnect = false;
2315 iov[num].iov_base = create_durable_buf();
2316 if (iov[num].iov_base == NULL)
2318 iov[num].iov_len = sizeof(struct create_durable);
2319 *num_iovec = num + 1;
2323 /* See MS-SMB2 2.2.13.2.7 */
2324 static struct crt_twarp_ctxt *
2325 create_twarp_buf(__u64 timewarp)
2327 struct crt_twarp_ctxt *buf;
2329 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2333 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2334 (struct crt_twarp_ctxt, Timestamp));
2335 buf->ccontext.DataLength = cpu_to_le32(8);
2336 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2337 (struct crt_twarp_ctxt, Name));
2338 buf->ccontext.NameLength = cpu_to_le16(4);
2339 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2344 buf->Timestamp = cpu_to_le64(timewarp);
2348 /* See MS-SMB2 2.2.13.2.7 */
2350 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2352 unsigned int num = *num_iovec;
2354 iov[num].iov_base = create_twarp_buf(timewarp);
2355 if (iov[num].iov_base == NULL)
2357 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2358 *num_iovec = num + 1;
2362 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2363 static void setup_owner_group_sids(char *buf)
2365 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2367 /* Populate the user ownership fields S-1-5-88-1 */
2368 sids->owner.Revision = 1;
2369 sids->owner.NumAuth = 3;
2370 sids->owner.Authority[5] = 5;
2371 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2372 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2373 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2375 /* Populate the group ownership fields S-1-5-88-2 */
2376 sids->group.Revision = 1;
2377 sids->group.NumAuth = 3;
2378 sids->group.Authority[5] = 5;
2379 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2380 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2381 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2383 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2386 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2387 static struct crt_sd_ctxt *
2388 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2390 struct crt_sd_ctxt *buf;
2392 unsigned int acelen, acl_size, ace_count;
2393 unsigned int owner_offset = 0;
2394 unsigned int group_offset = 0;
2395 struct smb3_acl acl = {};
2397 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2400 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2401 *len += sizeof(struct owner_group_sids);
2404 buf = kzalloc(*len, GFP_KERNEL);
2408 ptr = (__u8 *)&buf[1];
2410 /* offset fields are from beginning of security descriptor not of create context */
2411 owner_offset = ptr - (__u8 *)&buf->sd;
2412 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2413 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2414 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2416 setup_owner_group_sids(ptr);
2417 ptr += sizeof(struct owner_group_sids);
2419 buf->sd.OffsetOwner = 0;
2420 buf->sd.OffsetGroup = 0;
2423 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2424 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2425 buf->ccontext.NameLength = cpu_to_le16(4);
2426 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2431 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2434 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2435 * and "DP" ie the DACL is present
2437 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2439 /* offset owner, group and Sbz1 and SACL are all zero */
2440 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2441 /* Ship the ACL for now. we will copy it into buf later. */
2443 ptr += sizeof(struct smb3_acl);
2445 /* create one ACE to hold the mode embedded in reserved special SID */
2446 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2448 acl_size = acelen + sizeof(struct smb3_acl);
2452 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2453 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2459 /* and one more ACE to allow access for authenticated users */
2460 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2465 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2466 acl.AclSize = cpu_to_le16(acl_size);
2467 acl.AceCount = cpu_to_le16(ace_count);
2468 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2469 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2471 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2472 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2478 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2480 unsigned int num = *num_iovec;
2481 unsigned int len = 0;
2483 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2484 if (iov[num].iov_base == NULL)
2486 iov[num].iov_len = len;
2487 *num_iovec = num + 1;
2491 static struct crt_query_id_ctxt *
2492 create_query_id_buf(void)
2494 struct crt_query_id_ctxt *buf;
2496 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2500 buf->ccontext.DataOffset = cpu_to_le16(0);
2501 buf->ccontext.DataLength = cpu_to_le32(0);
2502 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2503 (struct crt_query_id_ctxt, Name));
2504 buf->ccontext.NameLength = cpu_to_le16(4);
2505 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2513 /* See MS-SMB2 2.2.13.2.9 */
2515 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2517 unsigned int num = *num_iovec;
2519 iov[num].iov_base = create_query_id_buf();
2520 if (iov[num].iov_base == NULL)
2522 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2523 *num_iovec = num + 1;
2528 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2529 const char *treename, const __le16 *path)
2531 int treename_len, path_len;
2532 struct nls_table *cp;
2533 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2538 treename_len = strlen(treename);
2539 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2545 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2547 /* make room for one path separator only if @path isn't empty */
2548 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2551 * final path needs to be 8-byte aligned as specified in
2552 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2554 *out_size = round_up(*out_len * sizeof(__le16), 8);
2555 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2559 cp = load_nls_default();
2560 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2562 /* Do not append the separator if the path is empty */
2563 if (path[0] != cpu_to_le16(0x0000)) {
2564 UniStrcat(*out_path, sep);
2565 UniStrcat(*out_path, path);
2573 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2574 umode_t mode, struct cifs_tcon *tcon,
2575 const char *full_path,
2576 struct cifs_sb_info *cifs_sb)
2578 struct smb_rqst rqst;
2579 struct smb2_create_req *req;
2580 struct smb2_create_rsp *rsp = NULL;
2581 struct cifs_ses *ses = tcon->ses;
2582 struct kvec iov[3]; /* make sure at least one for each open context */
2583 struct kvec rsp_iov = {NULL, 0};
2586 __le16 *copy_path = NULL;
2589 unsigned int n_iov = 2;
2590 __u32 file_attributes = 0;
2591 char *pc_buf = NULL;
2593 unsigned int total_len;
2594 __le16 *utf16_path = NULL;
2595 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2597 cifs_dbg(FYI, "mkdir\n");
2599 /* resource #1: path allocation */
2600 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2604 if (!ses || !server) {
2609 /* resource #2: request */
2610 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2611 (void **) &req, &total_len);
2616 if (smb3_encryption_required(tcon))
2617 flags |= CIFS_TRANSFORM_REQ;
2619 req->ImpersonationLevel = IL_IMPERSONATION;
2620 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2621 /* File attributes ignored on open (used in create though) */
2622 req->FileAttributes = cpu_to_le32(file_attributes);
2623 req->ShareAccess = FILE_SHARE_ALL_LE;
2624 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2625 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2627 iov[0].iov_base = (char *)req;
2628 /* -1 since last byte is buf[0] which is sent below (path) */
2629 iov[0].iov_len = total_len - 1;
2631 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2633 /* [MS-SMB2] 2.2.13 NameOffset:
2634 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2635 * the SMB2 header, the file name includes a prefix that will
2636 * be processed during DFS name normalization as specified in
2637 * section 3.3.5.9. Otherwise, the file name is relative to
2638 * the share that is identified by the TreeId in the SMB2
2641 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2644 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2645 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2647 tcon->tree_name, utf16_path);
2651 req->NameLength = cpu_to_le16(name_len * 2);
2652 uni_path_len = copy_size;
2653 /* free before overwriting resource */
2655 utf16_path = copy_path;
2657 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2658 /* MUST set path len (NameLength) to 0 opening root of share */
2659 req->NameLength = cpu_to_le16(uni_path_len - 2);
2660 if (uni_path_len % 8 != 0) {
2661 copy_size = roundup(uni_path_len, 8);
2662 copy_path = kzalloc(copy_size, GFP_KERNEL);
2667 memcpy((char *)copy_path, (const char *)utf16_path,
2669 uni_path_len = copy_size;
2670 /* free before overwriting resource */
2672 utf16_path = copy_path;
2676 iov[1].iov_len = uni_path_len;
2677 iov[1].iov_base = utf16_path;
2678 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2680 if (tcon->posix_extensions) {
2681 /* resource #3: posix buf */
2682 rc = add_posix_context(iov, &n_iov, mode);
2685 req->CreateContextsOffset = cpu_to_le32(
2686 sizeof(struct smb2_create_req) +
2688 pc_buf = iov[n_iov-1].iov_base;
2692 memset(&rqst, 0, sizeof(struct smb_rqst));
2694 rqst.rq_nvec = n_iov;
2696 /* no need to inc num_remote_opens because we close it just below */
2697 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2698 FILE_WRITE_ATTRIBUTES);
2699 /* resource #4: response buffer */
2700 rc = cifs_send_recv(xid, ses, server,
2701 &rqst, &resp_buftype, flags, &rsp_iov);
2703 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2704 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2706 FILE_WRITE_ATTRIBUTES, rc);
2707 goto err_free_rsp_buf;
2711 * Although unlikely to be possible for rsp to be null and rc not set,
2712 * adding check below is slightly safer long term (and quiets Coverity
2715 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2722 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2723 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2725 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2727 /* Eventually save off posix specific response info and timestaps */
2730 free_rsp_buf(resp_buftype, rsp);
2733 cifs_small_buf_release(req);
2740 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2741 struct smb_rqst *rqst, __u8 *oplock,
2742 struct cifs_open_parms *oparms, __le16 *path)
2744 struct smb2_create_req *req;
2745 unsigned int n_iov = 2;
2746 __u32 file_attributes = 0;
2749 unsigned int total_len;
2750 struct kvec *iov = rqst->rq_iov;
2754 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2755 (void **) &req, &total_len);
2759 iov[0].iov_base = (char *)req;
2760 /* -1 since last byte is buf[0] which is sent below (path) */
2761 iov[0].iov_len = total_len - 1;
2763 if (oparms->create_options & CREATE_OPTION_READONLY)
2764 file_attributes |= ATTR_READONLY;
2765 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2766 file_attributes |= ATTR_SYSTEM;
2768 req->ImpersonationLevel = IL_IMPERSONATION;
2769 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2770 /* File attributes ignored on open (used in create though) */
2771 req->FileAttributes = cpu_to_le32(file_attributes);
2772 req->ShareAccess = FILE_SHARE_ALL_LE;
2774 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2775 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2776 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2778 /* [MS-SMB2] 2.2.13 NameOffset:
2779 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2780 * the SMB2 header, the file name includes a prefix that will
2781 * be processed during DFS name normalization as specified in
2782 * section 3.3.5.9. Otherwise, the file name is relative to
2783 * the share that is identified by the TreeId in the SMB2
2786 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2789 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2790 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2792 tcon->tree_name, path);
2795 req->NameLength = cpu_to_le16(name_len * 2);
2796 uni_path_len = copy_size;
2799 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2800 /* MUST set path len (NameLength) to 0 opening root of share */
2801 req->NameLength = cpu_to_le16(uni_path_len - 2);
2802 copy_size = round_up(uni_path_len, 8);
2803 copy_path = kzalloc(copy_size, GFP_KERNEL);
2806 memcpy((char *)copy_path, (const char *)path,
2808 uni_path_len = copy_size;
2812 iov[1].iov_len = uni_path_len;
2813 iov[1].iov_base = path;
2815 if ((!server->oplocks) || (tcon->no_lease))
2816 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2818 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2819 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2820 req->RequestedOplockLevel = *oplock;
2821 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2822 (oparms->create_options & CREATE_NOT_FILE))
2823 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2825 rc = add_lease_context(server, req, iov, &n_iov,
2826 oparms->fid->lease_key, oplock);
2831 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2832 rc = add_durable_context(iov, &n_iov, oparms,
2833 tcon->use_persistent);
2838 if (tcon->posix_extensions) {
2839 rc = add_posix_context(iov, &n_iov, oparms->mode);
2844 if (tcon->snapshot_time) {
2845 cifs_dbg(FYI, "adding snapshot context\n");
2846 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2851 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2855 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2856 (oparms->mode != ACL_NO_MODE))
2860 oparms->mode = ACL_NO_MODE;
2863 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2868 if (set_owner | set_mode) {
2869 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2870 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2876 add_query_id_context(iov, &n_iov);
2880 * We have create contexts behind iov[1] (the file
2881 * name), point at them from the main create request
2883 req->CreateContextsOffset = cpu_to_le32(
2884 sizeof(struct smb2_create_req) +
2886 req->CreateContextsLength = 0;
2888 for (unsigned int i = 2; i < (n_iov-1); i++) {
2889 struct kvec *v = &iov[i];
2890 size_t len = v->iov_len;
2891 struct create_context *cctx =
2892 (struct create_context *)v->iov_base;
2894 cctx->Next = cpu_to_le32(len);
2895 le32_add_cpu(&req->CreateContextsLength, len);
2897 le32_add_cpu(&req->CreateContextsLength,
2898 iov[n_iov-1].iov_len);
2901 rqst->rq_nvec = n_iov;
2905 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2906 * All other vectors are freed by kfree().
2909 SMB2_open_free(struct smb_rqst *rqst)
2913 if (rqst && rqst->rq_iov) {
2914 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2915 for (i = 1; i < rqst->rq_nvec; i++)
2916 if (rqst->rq_iov[i].iov_base != smb2_padding)
2917 kfree(rqst->rq_iov[i].iov_base);
2922 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2923 __u8 *oplock, struct smb2_file_all_info *buf,
2924 struct create_posix_rsp *posix,
2925 struct kvec *err_iov, int *buftype)
2927 struct smb_rqst rqst;
2928 struct smb2_create_rsp *rsp = NULL;
2929 struct cifs_tcon *tcon = oparms->tcon;
2930 struct cifs_ses *ses = tcon->ses;
2931 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2932 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2933 struct kvec rsp_iov = {NULL, 0};
2934 int resp_buftype = CIFS_NO_BUFFER;
2938 cifs_dbg(FYI, "create/open\n");
2939 if (!ses || !server)
2942 if (smb3_encryption_required(tcon))
2943 flags |= CIFS_TRANSFORM_REQ;
2945 memset(&rqst, 0, sizeof(struct smb_rqst));
2946 memset(&iov, 0, sizeof(iov));
2948 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2950 rc = SMB2_open_init(tcon, server,
2951 &rqst, oplock, oparms, path);
2955 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
2956 oparms->create_options, oparms->desired_access);
2958 rc = cifs_send_recv(xid, ses, server,
2959 &rqst, &resp_buftype, flags,
2961 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2964 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2965 if (err_iov && rsp) {
2967 *buftype = resp_buftype;
2968 resp_buftype = CIFS_NO_BUFFER;
2971 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2972 oparms->create_options, oparms->desired_access, rc);
2973 if (rc == -EREMCHG) {
2974 pr_warn_once("server share %s deleted\n",
2976 tcon->need_reconnect = true;
2979 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
2982 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2983 oparms->create_options, oparms->desired_access);
2985 atomic_inc(&tcon->num_remote_opens);
2986 oparms->fid->persistent_fid = rsp->PersistentFileId;
2987 oparms->fid->volatile_fid = rsp->VolatileFileId;
2988 oparms->fid->access = oparms->desired_access;
2989 #ifdef CONFIG_CIFS_DEBUG2
2990 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
2991 #endif /* CIFS_DEBUG2 */
2994 buf->CreationTime = rsp->CreationTime;
2995 buf->LastAccessTime = rsp->LastAccessTime;
2996 buf->LastWriteTime = rsp->LastWriteTime;
2997 buf->ChangeTime = rsp->ChangeTime;
2998 buf->AllocationSize = rsp->AllocationSize;
2999 buf->EndOfFile = rsp->EndofFile;
3000 buf->Attributes = rsp->FileAttributes;
3001 buf->NumberOfLinks = cpu_to_le32(1);
3002 buf->DeletePending = 0;
3006 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
3007 oparms->fid->lease_key, oplock, buf, posix);
3009 SMB2_open_free(&rqst);
3010 free_rsp_buf(resp_buftype, rsp);
3015 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3016 struct smb_rqst *rqst,
3017 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3018 char *in_data, u32 indatalen,
3019 __u32 max_response_size)
3021 struct smb2_ioctl_req *req;
3022 struct kvec *iov = rqst->rq_iov;
3023 unsigned int total_len;
3027 rc = smb2_ioctl_req_init(opcode, tcon, server,
3028 (void **) &req, &total_len);
3034 * indatalen is usually small at a couple of bytes max, so
3035 * just allocate through generic pool
3037 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3039 cifs_small_buf_release(req);
3044 req->CtlCode = cpu_to_le32(opcode);
3045 req->PersistentFileId = persistent_fid;
3046 req->VolatileFileId = volatile_fid;
3048 iov[0].iov_base = (char *)req;
3050 * If no input data, the size of ioctl struct in
3051 * protocol spec still includes a 1 byte data buffer,
3052 * but if input data passed to ioctl, we do not
3053 * want to double count this, so we do not send
3054 * the dummy one byte of data in iovec[0] if sending
3055 * input data (in iovec[1]).
3058 req->InputCount = cpu_to_le32(indatalen);
3059 /* do not set InputOffset if no input data */
3061 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3063 iov[0].iov_len = total_len - 1;
3064 iov[1].iov_base = in_data_buf;
3065 iov[1].iov_len = indatalen;
3068 iov[0].iov_len = total_len;
3071 req->OutputOffset = 0;
3072 req->OutputCount = 0; /* MBZ */
3075 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3076 * We Could increase default MaxOutputResponse, but that could require
3077 * more credits. Windows typically sets this smaller, but for some
3078 * ioctls it may be useful to allow server to send more. No point
3079 * limiting what the server can send as long as fits in one credit
3080 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3081 * to increase this limit up in the future.
3082 * Note that for snapshot queries that servers like Azure expect that
3083 * the first query be minimal size (and just used to get the number/size
3084 * of previous versions) so response size must be specified as EXACTLY
3085 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3086 * of eight bytes. Currently that is the only case where we set max
3087 * response size smaller.
3089 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3090 req->hdr.CreditCharge =
3091 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3092 SMB2_MAX_BUFFER_SIZE));
3093 /* always an FSCTL (for now) */
3094 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3096 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3097 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3098 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3104 SMB2_ioctl_free(struct smb_rqst *rqst)
3107 if (rqst && rqst->rq_iov) {
3108 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3109 for (i = 1; i < rqst->rq_nvec; i++)
3110 if (rqst->rq_iov[i].iov_base != smb2_padding)
3111 kfree(rqst->rq_iov[i].iov_base);
3117 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3120 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3121 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3122 u32 max_out_data_len, char **out_data,
3123 u32 *plen /* returned data len */)
3125 struct smb_rqst rqst;
3126 struct smb2_ioctl_rsp *rsp = NULL;
3127 struct cifs_ses *ses;
3128 struct TCP_Server_Info *server;
3129 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3130 struct kvec rsp_iov = {NULL, 0};
3131 int resp_buftype = CIFS_NO_BUFFER;
3135 cifs_dbg(FYI, "SMB2 IOCTL\n");
3137 if (out_data != NULL)
3140 /* zero out returned data len, in case of error */
3151 server = cifs_pick_channel(ses);
3155 if (smb3_encryption_required(tcon))
3156 flags |= CIFS_TRANSFORM_REQ;
3158 memset(&rqst, 0, sizeof(struct smb_rqst));
3159 memset(&iov, 0, sizeof(iov));
3161 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3163 rc = SMB2_ioctl_init(tcon, server,
3164 &rqst, persistent_fid, volatile_fid, opcode,
3165 in_data, indatalen, max_out_data_len);
3169 rc = cifs_send_recv(xid, ses, server,
3170 &rqst, &resp_buftype, flags,
3172 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3175 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3176 ses->Suid, 0, opcode, rc);
3178 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3179 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3181 } else if (rc == -EINVAL) {
3182 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3183 (opcode != FSCTL_SRV_COPYCHUNK)) {
3184 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3187 } else if (rc == -E2BIG) {
3188 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3189 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3194 /* check if caller wants to look at return data or just return rc */
3195 if ((plen == NULL) || (out_data == NULL))
3199 * Although unlikely to be possible for rsp to be null and rc not set,
3200 * adding check below is slightly safer long term (and quiets Coverity
3208 *plen = le32_to_cpu(rsp->OutputCount);
3210 /* We check for obvious errors in the output buffer length and offset */
3212 goto ioctl_exit; /* server returned no data */
3213 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3214 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3220 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3221 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3222 le32_to_cpu(rsp->OutputOffset));
3228 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3230 if (*out_data == NULL) {
3236 SMB2_ioctl_free(&rqst);
3237 free_rsp_buf(resp_buftype, rsp);
3242 * Individual callers to ioctl worker function follow
3246 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3247 u64 persistent_fid, u64 volatile_fid)
3250 struct compress_ioctl fsctl_input;
3251 char *ret_data = NULL;
3253 fsctl_input.CompressionState =
3254 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3256 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3257 FSCTL_SET_COMPRESSION,
3258 (char *)&fsctl_input /* data input */,
3259 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3260 &ret_data /* out data */, NULL);
3262 cifs_dbg(FYI, "set compression rc %d\n", rc);
3268 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3269 struct smb_rqst *rqst,
3270 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3272 struct smb2_close_req *req;
3273 struct kvec *iov = rqst->rq_iov;
3274 unsigned int total_len;
3277 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3278 (void **) &req, &total_len);
3282 req->PersistentFileId = persistent_fid;
3283 req->VolatileFileId = volatile_fid;
3285 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3288 iov[0].iov_base = (char *)req;
3289 iov[0].iov_len = total_len;
3295 SMB2_close_free(struct smb_rqst *rqst)
3297 if (rqst && rqst->rq_iov)
3298 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3302 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3303 u64 persistent_fid, u64 volatile_fid,
3304 struct smb2_file_network_open_info *pbuf)
3306 struct smb_rqst rqst;
3307 struct smb2_close_rsp *rsp = NULL;
3308 struct cifs_ses *ses = tcon->ses;
3309 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3311 struct kvec rsp_iov;
3312 int resp_buftype = CIFS_NO_BUFFER;
3315 bool query_attrs = false;
3317 cifs_dbg(FYI, "Close\n");
3319 if (!ses || !server)
3322 if (smb3_encryption_required(tcon))
3323 flags |= CIFS_TRANSFORM_REQ;
3325 memset(&rqst, 0, sizeof(struct smb_rqst));
3326 memset(&iov, 0, sizeof(iov));
3330 /* check if need to ask server to return timestamps in close response */
3334 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3335 rc = SMB2_close_init(tcon, server,
3336 &rqst, persistent_fid, volatile_fid,
3341 rc = cifs_send_recv(xid, ses, server,
3342 &rqst, &resp_buftype, flags, &rsp_iov);
3343 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3346 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3347 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3351 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3354 * Note that have to subtract 4 since struct network_open_info
3355 * has a final 4 byte pad that close response does not have
3358 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3361 atomic_dec(&tcon->num_remote_opens);
3363 SMB2_close_free(&rqst);
3364 free_rsp_buf(resp_buftype, rsp);
3366 /* retry close in a worker thread if this one is interrupted */
3367 if (is_interrupt_error(rc)) {
3370 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3373 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3374 persistent_fid, tmp_rc);
3380 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3381 u64 persistent_fid, u64 volatile_fid)
3383 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3387 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3388 struct kvec *iov, unsigned int min_buf_size)
3390 unsigned int smb_len = iov->iov_len;
3391 char *end_of_smb = smb_len + (char *)iov->iov_base;
3392 char *begin_of_buf = offset + (char *)iov->iov_base;
3393 char *end_of_buf = begin_of_buf + buffer_length;
3396 if (buffer_length < min_buf_size) {
3397 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3398 buffer_length, min_buf_size);
3402 /* check if beyond RFC1001 maximum length */
3403 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3404 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3405 buffer_length, smb_len);
3409 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3410 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3418 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3419 * Caller must free buffer.
3422 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3423 struct kvec *iov, unsigned int minbufsize,
3426 char *begin_of_buf = offset + (char *)iov->iov_base;
3432 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3436 memcpy(data, begin_of_buf, minbufsize);
3442 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3443 struct smb_rqst *rqst,
3444 u64 persistent_fid, u64 volatile_fid,
3445 u8 info_class, u8 info_type, u32 additional_info,
3446 size_t output_len, size_t input_len, void *input)
3448 struct smb2_query_info_req *req;
3449 struct kvec *iov = rqst->rq_iov;
3450 unsigned int total_len;
3453 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3454 (void **) &req, &total_len);
3458 req->InfoType = info_type;
3459 req->FileInfoClass = info_class;
3460 req->PersistentFileId = persistent_fid;
3461 req->VolatileFileId = volatile_fid;
3462 req->AdditionalInformation = cpu_to_le32(additional_info);
3464 req->OutputBufferLength = cpu_to_le32(output_len);
3466 req->InputBufferLength = cpu_to_le32(input_len);
3467 /* total_len for smb query request never close to le16 max */
3468 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3469 memcpy(req->Buffer, input, input_len);
3472 iov[0].iov_base = (char *)req;
3474 iov[0].iov_len = total_len - 1 + input_len;
3479 SMB2_query_info_free(struct smb_rqst *rqst)
3481 if (rqst && rqst->rq_iov)
3482 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3486 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3487 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3488 u32 additional_info, size_t output_len, size_t min_len, void **data,
3491 struct smb_rqst rqst;
3492 struct smb2_query_info_rsp *rsp = NULL;
3494 struct kvec rsp_iov;
3496 int resp_buftype = CIFS_NO_BUFFER;
3497 struct cifs_ses *ses = tcon->ses;
3498 struct TCP_Server_Info *server;
3500 bool allocated = false;
3502 cifs_dbg(FYI, "Query Info\n");
3506 server = cifs_pick_channel(ses);
3510 if (smb3_encryption_required(tcon))
3511 flags |= CIFS_TRANSFORM_REQ;
3513 memset(&rqst, 0, sizeof(struct smb_rqst));
3514 memset(&iov, 0, sizeof(iov));
3518 rc = SMB2_query_info_init(tcon, server,
3519 &rqst, persistent_fid, volatile_fid,
3520 info_class, info_type, additional_info,
3521 output_len, 0, NULL);
3525 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3526 ses->Suid, info_class, (__u32)info_type);
3528 rc = cifs_send_recv(xid, ses, server,
3529 &rqst, &resp_buftype, flags, &rsp_iov);
3530 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3533 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3534 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3535 ses->Suid, info_class, (__u32)info_type, rc);
3539 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3540 ses->Suid, info_class, (__u32)info_type);
3543 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3545 *data = kmalloc(*dlen, GFP_KERNEL);
3548 "Error %d allocating memory for acl\n",
3558 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3559 le32_to_cpu(rsp->OutputBufferLength),
3560 &rsp_iov, dlen ? *dlen : min_len, *data);
3561 if (rc && allocated) {
3568 SMB2_query_info_free(&rqst);
3569 free_rsp_buf(resp_buftype, rsp);
3573 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3574 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3576 return query_info(xid, tcon, persistent_fid, volatile_fid,
3577 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3578 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3579 sizeof(struct smb2_file_all_info), (void **)&data,
3584 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3586 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3587 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3589 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3590 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3593 return query_info(xid, tcon, persistent_fid, volatile_fid,
3594 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3595 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3596 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3601 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3602 u64 persistent_fid, u64 volatile_fid,
3603 void **data, u32 *plen, u32 extra_info)
3605 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3609 return query_info(xid, tcon, persistent_fid, volatile_fid,
3610 0, SMB2_O_INFO_SECURITY, additional_info,
3611 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3615 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3616 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3618 return query_info(xid, tcon, persistent_fid, volatile_fid,
3619 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3620 sizeof(struct smb2_file_internal_info),
3621 sizeof(struct smb2_file_internal_info),
3622 (void **)&uniqueid, NULL);
3626 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3627 * See MS-SMB2 2.2.35 and 2.2.36
3631 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3632 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3633 u64 persistent_fid, u64 volatile_fid,
3634 u32 completion_filter, bool watch_tree)
3636 struct smb2_change_notify_req *req;
3637 struct kvec *iov = rqst->rq_iov;
3638 unsigned int total_len;
3641 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3642 (void **) &req, &total_len);
3646 req->PersistentFileId = persistent_fid;
3647 req->VolatileFileId = volatile_fid;
3648 /* See note 354 of MS-SMB2, 64K max */
3649 req->OutputBufferLength =
3650 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3651 req->CompletionFilter = cpu_to_le32(completion_filter);
3653 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3657 iov[0].iov_base = (char *)req;
3658 iov[0].iov_len = total_len;
3664 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3665 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3666 u32 completion_filter, u32 max_out_data_len, char **out_data,
3667 u32 *plen /* returned data len */)
3669 struct cifs_ses *ses = tcon->ses;
3670 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3671 struct smb_rqst rqst;
3672 struct smb2_change_notify_rsp *smb_rsp;
3674 struct kvec rsp_iov = {NULL, 0};
3675 int resp_buftype = CIFS_NO_BUFFER;
3679 cifs_dbg(FYI, "change notify\n");
3680 if (!ses || !server)
3683 if (smb3_encryption_required(tcon))
3684 flags |= CIFS_TRANSFORM_REQ;
3686 memset(&rqst, 0, sizeof(struct smb_rqst));
3687 memset(&iov, 0, sizeof(iov));
3694 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3695 persistent_fid, volatile_fid,
3696 completion_filter, watch_tree);
3700 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3701 (u8)watch_tree, completion_filter);
3702 rc = cifs_send_recv(xid, ses, server,
3703 &rqst, &resp_buftype, flags, &rsp_iov);
3706 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3707 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3708 (u8)watch_tree, completion_filter, rc);
3710 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3711 ses->Suid, (u8)watch_tree, completion_filter);
3712 /* validate that notify information is plausible */
3713 if ((rsp_iov.iov_base == NULL) ||
3714 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
3717 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3719 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3720 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3721 sizeof(struct file_notify_information));
3723 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3724 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3725 if (*out_data == NULL) {
3729 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
3734 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3735 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3742 * This is a no-op for now. We're not really interested in the reply, but
3743 * rather in the fact that the server sent one and that server->lstrp
3746 * FIXME: maybe we should consider checking that the reply matches request?
3749 smb2_echo_callback(struct mid_q_entry *mid)
3751 struct TCP_Server_Info *server = mid->callback_data;
3752 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3753 struct cifs_credits credits = { .value = 0, .instance = 0 };
3755 if (mid->mid_state == MID_RESPONSE_RECEIVED
3756 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3757 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3758 credits.instance = server->reconnect_instance;
3762 add_credits(server, &credits, CIFS_ECHO_OP);
3765 void smb2_reconnect_server(struct work_struct *work)
3767 struct TCP_Server_Info *server = container_of(work,
3768 struct TCP_Server_Info, reconnect.work);
3769 struct TCP_Server_Info *pserver;
3770 struct cifs_ses *ses, *ses2;
3771 struct cifs_tcon *tcon, *tcon2;
3772 struct list_head tmp_list, tmp_ses_list;
3773 bool tcon_exist = false, ses_exist = false;
3774 bool tcon_selected = false;
3776 bool resched = false;
3778 /* If server is a channel, select the primary channel */
3779 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3781 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3782 mutex_lock(&pserver->reconnect_mutex);
3784 INIT_LIST_HEAD(&tmp_list);
3785 INIT_LIST_HEAD(&tmp_ses_list);
3786 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3788 spin_lock(&cifs_tcp_ses_lock);
3789 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3791 tcon_selected = false;
3793 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3794 if (tcon->need_reconnect || tcon->need_reopen_files) {
3796 list_add_tail(&tcon->rlist, &tmp_list);
3797 tcon_selected = tcon_exist = true;
3801 * IPC has the same lifetime as its session and uses its
3804 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3805 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3806 tcon_selected = tcon_exist = true;
3807 cifs_smb_ses_inc_refcount(ses);
3810 * handle the case where channel needs to reconnect
3811 * binding session, but tcon is healthy (some other channel
3814 spin_lock(&ses->chan_lock);
3815 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3816 list_add_tail(&ses->rlist, &tmp_ses_list);
3818 cifs_smb_ses_inc_refcount(ses);
3820 spin_unlock(&ses->chan_lock);
3823 * Get the reference to server struct to be sure that the last call of
3824 * cifs_put_tcon() in the loop below won't release the server pointer.
3826 if (tcon_exist || ses_exist)
3827 server->srv_count++;
3829 spin_unlock(&cifs_tcp_ses_lock);
3831 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3832 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3834 cifs_reopen_persistent_handles(tcon);
3837 list_del_init(&tcon->rlist);
3839 cifs_put_smb_ses(tcon->ses);
3841 cifs_put_tcon(tcon);
3847 /* allocate a dummy tcon struct used for reconnect */
3848 tcon = tconInfoAlloc();
3851 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3852 list_del_init(&ses->rlist);
3853 cifs_put_smb_ses(ses);
3858 tcon->status = TID_GOOD;
3859 tcon->retry = false;
3860 tcon->need_reconnect = false;
3862 /* now reconnect sessions for necessary channels */
3863 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3865 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3868 list_del_init(&ses->rlist);
3869 cifs_put_smb_ses(ses);
3874 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
3876 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3877 mutex_unlock(&pserver->reconnect_mutex);
3879 /* now we can safely release srv struct */
3880 if (tcon_exist || ses_exist)
3881 cifs_put_tcp_session(server, 1);
3885 SMB2_echo(struct TCP_Server_Info *server)
3887 struct smb2_echo_req *req;
3890 struct smb_rqst rqst = { .rq_iov = iov,
3892 unsigned int total_len;
3894 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
3896 spin_lock(&server->srv_lock);
3897 if (server->ops->need_neg &&
3898 server->ops->need_neg(server)) {
3899 spin_unlock(&server->srv_lock);
3900 /* No need to send echo on newly established connections */
3901 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3904 spin_unlock(&server->srv_lock);
3906 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3907 (void **)&req, &total_len);
3911 req->hdr.CreditRequest = cpu_to_le16(1);
3913 iov[0].iov_len = total_len;
3914 iov[0].iov_base = (char *)req;
3916 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3917 server, CIFS_ECHO_OP, NULL);
3919 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3921 cifs_small_buf_release(req);
3926 SMB2_flush_free(struct smb_rqst *rqst)
3928 if (rqst && rqst->rq_iov)
3929 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3933 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3934 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3935 u64 persistent_fid, u64 volatile_fid)
3937 struct smb2_flush_req *req;
3938 struct kvec *iov = rqst->rq_iov;
3939 unsigned int total_len;
3942 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3943 (void **) &req, &total_len);
3947 req->PersistentFileId = persistent_fid;
3948 req->VolatileFileId = volatile_fid;
3950 iov[0].iov_base = (char *)req;
3951 iov[0].iov_len = total_len;
3957 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3960 struct cifs_ses *ses = tcon->ses;
3961 struct smb_rqst rqst;
3963 struct kvec rsp_iov = {NULL, 0};
3964 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3965 int resp_buftype = CIFS_NO_BUFFER;
3969 cifs_dbg(FYI, "flush\n");
3970 if (!ses || !(ses->server))
3973 if (smb3_encryption_required(tcon))
3974 flags |= CIFS_TRANSFORM_REQ;
3976 memset(&rqst, 0, sizeof(struct smb_rqst));
3977 memset(&iov, 0, sizeof(iov));
3981 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3982 persistent_fid, volatile_fid);
3986 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3987 rc = cifs_send_recv(xid, ses, server,
3988 &rqst, &resp_buftype, flags, &rsp_iov);
3991 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3992 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3995 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3999 SMB2_flush_free(&rqst);
4000 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4004 #ifdef CONFIG_CIFS_SMB_DIRECT
4005 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4007 struct TCP_Server_Info *server = io_parms->server;
4008 struct cifs_tcon *tcon = io_parms->tcon;
4010 /* we can only offload if we're connected */
4011 if (!server || !tcon)
4014 /* we can only offload on an rdma connection */
4015 if (!server->rdma || !server->smbd_conn)
4018 /* we don't support signed offload yet */
4022 /* we don't support encrypted offload yet */
4023 if (smb3_encryption_required(tcon))
4026 /* offload also has its overhead, so only do it if desired */
4027 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4032 #endif /* CONFIG_CIFS_SMB_DIRECT */
4035 * To form a chain of read requests, any read requests after the first should
4036 * have the end_of_chain boolean set to true.
4039 smb2_new_read_req(void **buf, unsigned int *total_len,
4040 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4041 unsigned int remaining_bytes, int request_type)
4044 struct smb2_read_req *req = NULL;
4045 struct smb2_hdr *shdr;
4046 struct TCP_Server_Info *server = io_parms->server;
4048 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4049 (void **) &req, total_len);
4054 return -ECONNABORTED;
4057 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4059 req->PersistentFileId = io_parms->persistent_fid;
4060 req->VolatileFileId = io_parms->volatile_fid;
4061 req->ReadChannelInfoOffset = 0; /* reserved */
4062 req->ReadChannelInfoLength = 0; /* reserved */
4063 req->Channel = 0; /* reserved */
4064 req->MinimumCount = 0;
4065 req->Length = cpu_to_le32(io_parms->length);
4066 req->Offset = cpu_to_le64(io_parms->offset);
4068 trace_smb3_read_enter(0 /* xid */,
4069 io_parms->persistent_fid,
4070 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4071 io_parms->offset, io_parms->length);
4072 #ifdef CONFIG_CIFS_SMB_DIRECT
4074 * If we want to do a RDMA write, fill in and append
4075 * smbd_buffer_descriptor_v1 to the end of read request
4077 if (smb3_use_rdma_offload(io_parms)) {
4078 struct smbd_buffer_descriptor_v1 *v1;
4079 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4081 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4082 true, need_invalidate);
4086 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4087 if (need_invalidate)
4088 req->Channel = SMB2_CHANNEL_RDMA_V1;
4089 req->ReadChannelInfoOffset =
4090 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4091 req->ReadChannelInfoLength =
4092 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4093 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4094 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4095 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4096 v1->length = cpu_to_le32(rdata->mr->mr->length);
4098 *total_len += sizeof(*v1) - 1;
4101 if (request_type & CHAINED_REQUEST) {
4102 if (!(request_type & END_OF_CHAIN)) {
4103 /* next 8-byte aligned request */
4104 *total_len = ALIGN(*total_len, 8);
4105 shdr->NextCommand = cpu_to_le32(*total_len);
4106 } else /* END_OF_CHAIN */
4107 shdr->NextCommand = 0;
4108 if (request_type & RELATED_REQUEST) {
4109 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4111 * Related requests use info from previous read request
4114 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4115 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4116 req->PersistentFileId = (u64)-1;
4117 req->VolatileFileId = (u64)-1;
4120 if (remaining_bytes > io_parms->length)
4121 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4123 req->RemainingBytes = 0;
4130 smb2_readv_callback(struct mid_q_entry *mid)
4132 struct cifs_readdata *rdata = mid->callback_data;
4133 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4134 struct TCP_Server_Info *server = rdata->server;
4135 struct smb2_hdr *shdr =
4136 (struct smb2_hdr *)rdata->iov[0].iov_base;
4137 struct cifs_credits credits = { .value = 0, .instance = 0 };
4138 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4140 if (rdata->got_bytes) {
4141 rqst.rq_iter = rdata->iter;
4142 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
4145 WARN_ONCE(rdata->server != mid->server,
4146 "rdata server %p != mid server %p",
4147 rdata->server, mid->server);
4149 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4150 __func__, mid->mid, mid->mid_state, rdata->result,
4153 switch (mid->mid_state) {
4154 case MID_RESPONSE_RECEIVED:
4155 credits.value = le16_to_cpu(shdr->CreditRequest);
4156 credits.instance = server->reconnect_instance;
4157 /* result already set, check signature */
4158 if (server->sign && !mid->decrypted) {
4161 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4162 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4163 rc = smb2_verify_signature(&rqst, server);
4165 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4168 /* FIXME: should this be counted toward the initiating task? */
4169 task_io_account_read(rdata->got_bytes);
4170 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4172 case MID_REQUEST_SUBMITTED:
4173 case MID_RETRY_NEEDED:
4174 rdata->result = -EAGAIN;
4175 if (server->sign && rdata->got_bytes)
4176 /* reset bytes number since we can not check a sign */
4177 rdata->got_bytes = 0;
4178 /* FIXME: should this be counted toward the initiating task? */
4179 task_io_account_read(rdata->got_bytes);
4180 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4182 case MID_RESPONSE_MALFORMED:
4183 credits.value = le16_to_cpu(shdr->CreditRequest);
4184 credits.instance = server->reconnect_instance;
4187 rdata->result = -EIO;
4189 #ifdef CONFIG_CIFS_SMB_DIRECT
4191 * If this rdata has a memmory registered, the MR can be freed
4192 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4193 * because they have limited number and are used for future I/Os
4196 smbd_deregister_mr(rdata->mr);
4200 if (rdata->result && rdata->result != -ENODATA) {
4201 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4202 trace_smb3_read_err(0 /* xid */,
4203 rdata->cfile->fid.persistent_fid,
4204 tcon->tid, tcon->ses->Suid, rdata->offset,
4205 rdata->bytes, rdata->result);
4207 trace_smb3_read_done(0 /* xid */,
4208 rdata->cfile->fid.persistent_fid,
4209 tcon->tid, tcon->ses->Suid,
4210 rdata->offset, rdata->got_bytes);
4212 queue_work(cifsiod_wq, &rdata->work);
4214 add_credits(server, &credits, 0);
4217 /* smb2_async_readv - send an async read, and set up mid to handle result */
4219 smb2_async_readv(struct cifs_readdata *rdata)
4223 struct smb2_hdr *shdr;
4224 struct cifs_io_parms io_parms;
4225 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4227 struct TCP_Server_Info *server;
4228 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4229 unsigned int total_len;
4231 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4232 __func__, rdata->offset, rdata->bytes);
4235 rdata->server = cifs_pick_channel(tcon->ses);
4237 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4238 io_parms.server = server = rdata->server;
4239 io_parms.offset = rdata->offset;
4240 io_parms.length = rdata->bytes;
4241 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4242 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4243 io_parms.pid = rdata->pid;
4245 rc = smb2_new_read_req(
4246 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4250 if (smb3_encryption_required(io_parms.tcon))
4251 flags |= CIFS_TRANSFORM_REQ;
4253 rdata->iov[0].iov_base = buf;
4254 rdata->iov[0].iov_len = total_len;
4256 shdr = (struct smb2_hdr *)buf;
4258 if (rdata->credits.value > 0) {
4259 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4260 SMB2_MAX_BUFFER_SIZE));
4261 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4263 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4265 goto async_readv_out;
4267 flags |= CIFS_HAS_CREDITS;
4270 kref_get(&rdata->refcount);
4271 rc = cifs_call_async(server, &rqst,
4272 cifs_readv_receive, smb2_readv_callback,
4273 smb3_handle_read_data, rdata, flags,
4276 kref_put(&rdata->refcount, cifs_readdata_release);
4277 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4278 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4280 io_parms.tcon->ses->Suid,
4281 io_parms.offset, io_parms.length, rc);
4285 cifs_small_buf_release(buf);
4290 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4291 unsigned int *nbytes, char **buf, int *buf_type)
4293 struct smb_rqst rqst;
4294 int resp_buftype, rc;
4295 struct smb2_read_req *req = NULL;
4296 struct smb2_read_rsp *rsp = NULL;
4298 struct kvec rsp_iov;
4299 unsigned int total_len;
4300 int flags = CIFS_LOG_ERROR;
4301 struct cifs_ses *ses = io_parms->tcon->ses;
4303 if (!io_parms->server)
4304 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4307 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4311 if (smb3_encryption_required(io_parms->tcon))
4312 flags |= CIFS_TRANSFORM_REQ;
4314 iov[0].iov_base = (char *)req;
4315 iov[0].iov_len = total_len;
4317 memset(&rqst, 0, sizeof(struct smb_rqst));
4321 rc = cifs_send_recv(xid, ses, io_parms->server,
4322 &rqst, &resp_buftype, flags, &rsp_iov);
4323 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4326 if (rc != -ENODATA) {
4327 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4328 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4329 trace_smb3_read_err(xid,
4330 req->PersistentFileId,
4331 io_parms->tcon->tid, ses->Suid,
4332 io_parms->offset, io_parms->length,
4335 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4336 ses->Suid, io_parms->offset, 0);
4337 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4338 cifs_small_buf_release(req);
4339 return rc == -ENODATA ? 0 : rc;
4341 trace_smb3_read_done(xid,
4342 req->PersistentFileId,
4343 io_parms->tcon->tid, ses->Suid,
4344 io_parms->offset, io_parms->length);
4346 cifs_small_buf_release(req);
4348 *nbytes = le32_to_cpu(rsp->DataLength);
4349 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4350 (*nbytes > io_parms->length)) {
4351 cifs_dbg(FYI, "bad length %d for count %d\n",
4352 *nbytes, io_parms->length);
4358 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4359 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4360 } else if (resp_buftype != CIFS_NO_BUFFER) {
4361 *buf = rsp_iov.iov_base;
4362 if (resp_buftype == CIFS_SMALL_BUFFER)
4363 *buf_type = CIFS_SMALL_BUFFER;
4364 else if (resp_buftype == CIFS_LARGE_BUFFER)
4365 *buf_type = CIFS_LARGE_BUFFER;
4371 * Check the mid_state and signature on received buffer (if any), and queue the
4372 * workqueue completion task.
4375 smb2_writev_callback(struct mid_q_entry *mid)
4377 struct cifs_writedata *wdata = mid->callback_data;
4378 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4379 struct TCP_Server_Info *server = wdata->server;
4380 unsigned int written;
4381 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4382 struct cifs_credits credits = { .value = 0, .instance = 0 };
4384 WARN_ONCE(wdata->server != mid->server,
4385 "wdata server %p != mid server %p",
4386 wdata->server, mid->server);
4388 switch (mid->mid_state) {
4389 case MID_RESPONSE_RECEIVED:
4390 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4391 credits.instance = server->reconnect_instance;
4392 wdata->result = smb2_check_receive(mid, server, 0);
4393 if (wdata->result != 0)
4396 written = le32_to_cpu(rsp->DataLength);
4398 * Mask off high 16 bits when bytes written as returned
4399 * by the server is greater than bytes requested by the
4400 * client. OS/2 servers are known to set incorrect
4403 if (written > wdata->bytes)
4406 if (written < wdata->bytes)
4407 wdata->result = -ENOSPC;
4409 wdata->bytes = written;
4411 case MID_REQUEST_SUBMITTED:
4412 case MID_RETRY_NEEDED:
4413 wdata->result = -EAGAIN;
4415 case MID_RESPONSE_MALFORMED:
4416 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4417 credits.instance = server->reconnect_instance;
4420 wdata->result = -EIO;
4423 #ifdef CONFIG_CIFS_SMB_DIRECT
4425 * If this wdata has a memory registered, the MR can be freed
4426 * The number of MRs available is limited, it's important to recover
4427 * used MR as soon as I/O is finished. Hold MR longer in the later
4428 * I/O process can possibly result in I/O deadlock due to lack of MR
4429 * to send request on I/O retry
4432 smbd_deregister_mr(wdata->mr);
4436 if (wdata->result) {
4437 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4438 trace_smb3_write_err(0 /* no xid */,
4439 wdata->cfile->fid.persistent_fid,
4440 tcon->tid, tcon->ses->Suid, wdata->offset,
4441 wdata->bytes, wdata->result);
4442 if (wdata->result == -ENOSPC)
4443 pr_warn_once("Out of space writing to %s\n",
4446 trace_smb3_write_done(0 /* no xid */,
4447 wdata->cfile->fid.persistent_fid,
4448 tcon->tid, tcon->ses->Suid,
4449 wdata->offset, wdata->bytes);
4451 queue_work(cifsiod_wq, &wdata->work);
4453 add_credits(server, &credits, 0);
4456 /* smb2_async_writev - send an async write, and set up mid to handle result */
4458 smb2_async_writev(struct cifs_writedata *wdata,
4459 void (*release)(struct kref *kref))
4461 int rc = -EACCES, flags = 0;
4462 struct smb2_write_req *req = NULL;
4463 struct smb2_hdr *shdr;
4464 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4465 struct TCP_Server_Info *server = wdata->server;
4467 struct smb_rqst rqst = { };
4468 unsigned int total_len;
4469 struct cifs_io_parms _io_parms;
4470 struct cifs_io_parms *io_parms = NULL;
4473 server = wdata->server = cifs_pick_channel(tcon->ses);
4476 * in future we may get cifs_io_parms passed in from the caller,
4477 * but for now we construct it here...
4479 _io_parms = (struct cifs_io_parms) {
4482 .offset = wdata->offset,
4483 .length = wdata->bytes,
4484 .persistent_fid = wdata->cfile->fid.persistent_fid,
4485 .volatile_fid = wdata->cfile->fid.volatile_fid,
4488 io_parms = &_io_parms;
4490 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4491 (void **) &req, &total_len);
4495 if (smb3_encryption_required(tcon))
4496 flags |= CIFS_TRANSFORM_REQ;
4498 shdr = (struct smb2_hdr *)req;
4499 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4501 req->PersistentFileId = io_parms->persistent_fid;
4502 req->VolatileFileId = io_parms->volatile_fid;
4503 req->WriteChannelInfoOffset = 0;
4504 req->WriteChannelInfoLength = 0;
4505 req->Channel = SMB2_CHANNEL_NONE;
4506 req->Offset = cpu_to_le64(io_parms->offset);
4507 req->DataOffset = cpu_to_le16(
4508 offsetof(struct smb2_write_req, Buffer));
4509 req->RemainingBytes = 0;
4511 trace_smb3_write_enter(0 /* xid */,
4512 io_parms->persistent_fid,
4513 io_parms->tcon->tid,
4514 io_parms->tcon->ses->Suid,
4518 #ifdef CONFIG_CIFS_SMB_DIRECT
4520 * If we want to do a server RDMA read, fill in and append
4521 * smbd_buffer_descriptor_v1 to the end of write request
4523 if (smb3_use_rdma_offload(io_parms)) {
4524 struct smbd_buffer_descriptor_v1 *v1;
4525 size_t data_size = iov_iter_count(&wdata->iter);
4526 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4528 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4529 false, need_invalidate);
4532 goto async_writev_out;
4535 req->DataOffset = 0;
4536 req->RemainingBytes = cpu_to_le32(data_size);
4537 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4538 if (need_invalidate)
4539 req->Channel = SMB2_CHANNEL_RDMA_V1;
4540 req->WriteChannelInfoOffset =
4541 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4542 req->WriteChannelInfoLength =
4543 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4544 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4545 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4546 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4547 v1->length = cpu_to_le32(wdata->mr->mr->length);
4550 iov[0].iov_len = total_len - 1;
4551 iov[0].iov_base = (char *)req;
4555 rqst.rq_iter = wdata->iter;
4556 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4557 #ifdef CONFIG_CIFS_SMB_DIRECT
4559 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4561 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4562 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4564 #ifdef CONFIG_CIFS_SMB_DIRECT
4565 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4567 req->Length = cpu_to_le32(io_parms->length);
4569 req->Length = cpu_to_le32(io_parms->length);
4572 if (wdata->credits.value > 0) {
4573 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4574 SMB2_MAX_BUFFER_SIZE));
4575 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4577 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4579 goto async_writev_out;
4581 flags |= CIFS_HAS_CREDITS;
4584 kref_get(&wdata->refcount);
4585 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4586 wdata, flags, &wdata->credits);
4589 trace_smb3_write_err(0 /* no xid */,
4590 io_parms->persistent_fid,
4591 io_parms->tcon->tid,
4592 io_parms->tcon->ses->Suid,
4596 kref_put(&wdata->refcount, release);
4597 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4601 cifs_small_buf_release(req);
4606 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4607 * The length field from io_parms must be at least 1 and indicates a number of
4608 * elements with data to write that begins with position 1 in iov array. All
4609 * data length is specified by count.
4612 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4613 unsigned int *nbytes, struct kvec *iov, int n_vec)
4615 struct smb_rqst rqst;
4617 struct smb2_write_req *req = NULL;
4618 struct smb2_write_rsp *rsp = NULL;
4620 struct kvec rsp_iov;
4622 unsigned int total_len;
4623 struct TCP_Server_Info *server;
4630 if (!io_parms->server)
4631 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4632 server = io_parms->server;
4634 return -ECONNABORTED;
4636 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4637 (void **) &req, &total_len);
4641 if (smb3_encryption_required(io_parms->tcon))
4642 flags |= CIFS_TRANSFORM_REQ;
4644 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4646 req->PersistentFileId = io_parms->persistent_fid;
4647 req->VolatileFileId = io_parms->volatile_fid;
4648 req->WriteChannelInfoOffset = 0;
4649 req->WriteChannelInfoLength = 0;
4651 req->Length = cpu_to_le32(io_parms->length);
4652 req->Offset = cpu_to_le64(io_parms->offset);
4653 req->DataOffset = cpu_to_le16(
4654 offsetof(struct smb2_write_req, Buffer));
4655 req->RemainingBytes = 0;
4657 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4658 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4659 io_parms->offset, io_parms->length);
4661 iov[0].iov_base = (char *)req;
4663 iov[0].iov_len = total_len - 1;
4665 memset(&rqst, 0, sizeof(struct smb_rqst));
4667 rqst.rq_nvec = n_vec + 1;
4669 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4671 &resp_buftype, flags, &rsp_iov);
4672 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4675 trace_smb3_write_err(xid,
4676 req->PersistentFileId,
4677 io_parms->tcon->tid,
4678 io_parms->tcon->ses->Suid,
4679 io_parms->offset, io_parms->length, rc);
4680 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4681 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4683 *nbytes = le32_to_cpu(rsp->DataLength);
4684 trace_smb3_write_done(xid,
4685 req->PersistentFileId,
4686 io_parms->tcon->tid,
4687 io_parms->tcon->ses->Suid,
4688 io_parms->offset, *nbytes);
4691 cifs_small_buf_release(req);
4692 free_rsp_buf(resp_buftype, rsp);
4696 int posix_info_sid_size(const void *beg, const void *end)
4704 subauth = *(u8 *)(beg+1);
4705 if (subauth < 1 || subauth > 15)
4708 total = 1 + 1 + 6 + 4*subauth;
4709 if (beg + total > end)
4715 int posix_info_parse(const void *beg, const void *end,
4716 struct smb2_posix_info_parsed *out)
4720 int owner_len, group_len;
4722 const void *owner_sid;
4723 const void *group_sid;
4726 /* if no end bound given, assume payload to be correct */
4728 const struct smb2_posix_info *p = beg;
4730 end = beg + le32_to_cpu(p->NextEntryOffset);
4731 /* last element will have a 0 offset, pick a sensible bound */
4736 /* check base buf */
4737 if (beg + sizeof(struct smb2_posix_info) > end)
4739 total_len = sizeof(struct smb2_posix_info);
4741 /* check owner sid */
4742 owner_sid = beg + total_len;
4743 owner_len = posix_info_sid_size(owner_sid, end);
4746 total_len += owner_len;
4748 /* check group sid */
4749 group_sid = beg + total_len;
4750 group_len = posix_info_sid_size(group_sid, end);
4753 total_len += group_len;
4755 /* check name len */
4756 if (beg + total_len + 4 > end)
4758 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4759 if (name_len < 1 || name_len > 0xFFFF)
4764 name = beg + total_len;
4765 if (name + name_len > end)
4767 total_len += name_len;
4771 out->size = total_len;
4772 out->name_len = name_len;
4774 memcpy(&out->owner, owner_sid, owner_len);
4775 memcpy(&out->group, group_sid, group_len);
4780 static int posix_info_extra_size(const void *beg, const void *end)
4782 int len = posix_info_parse(beg, end, NULL);
4786 return len - sizeof(struct smb2_posix_info);
4790 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4794 unsigned int entrycount = 0;
4795 unsigned int next_offset = 0;
4797 FILE_DIRECTORY_INFO *dir_info;
4799 if (bufstart == NULL)
4802 entryptr = bufstart;
4805 if (entryptr + next_offset < entryptr ||
4806 entryptr + next_offset > end_of_buf ||
4807 entryptr + next_offset + size > end_of_buf) {
4808 cifs_dbg(VFS, "malformed search entry would overflow\n");
4812 entryptr = entryptr + next_offset;
4813 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4815 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4816 len = posix_info_extra_size(entryptr, end_of_buf);
4818 len = le32_to_cpu(dir_info->FileNameLength);
4821 entryptr + len < entryptr ||
4822 entryptr + len > end_of_buf ||
4823 entryptr + len + size > end_of_buf) {
4824 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4829 *lastentry = entryptr;
4832 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4843 int SMB2_query_directory_init(const unsigned int xid,
4844 struct cifs_tcon *tcon,
4845 struct TCP_Server_Info *server,
4846 struct smb_rqst *rqst,
4847 u64 persistent_fid, u64 volatile_fid,
4848 int index, int info_level)
4850 struct smb2_query_directory_req *req;
4851 unsigned char *bufptr;
4852 __le16 asteriks = cpu_to_le16('*');
4853 unsigned int output_size = CIFSMaxBufSize -
4854 MAX_SMB2_CREATE_RESPONSE_SIZE -
4855 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4856 unsigned int total_len;
4857 struct kvec *iov = rqst->rq_iov;
4860 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4861 (void **) &req, &total_len);
4865 switch (info_level) {
4866 case SMB_FIND_FILE_DIRECTORY_INFO:
4867 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4869 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4870 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4872 case SMB_FIND_FILE_POSIX_INFO:
4873 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4876 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4881 req->FileIndex = cpu_to_le32(index);
4882 req->PersistentFileId = persistent_fid;
4883 req->VolatileFileId = volatile_fid;
4886 bufptr = req->Buffer;
4887 memcpy(bufptr, &asteriks, len);
4889 req->FileNameOffset =
4890 cpu_to_le16(sizeof(struct smb2_query_directory_req));
4891 req->FileNameLength = cpu_to_le16(len);
4893 * BB could be 30 bytes or so longer if we used SMB2 specific
4894 * buffer lengths, but this is safe and close enough.
4896 output_size = min_t(unsigned int, output_size, server->maxBuf);
4897 output_size = min_t(unsigned int, output_size, 2 << 15);
4898 req->OutputBufferLength = cpu_to_le32(output_size);
4900 iov[0].iov_base = (char *)req;
4902 iov[0].iov_len = total_len - 1;
4904 iov[1].iov_base = (char *)(req->Buffer);
4905 iov[1].iov_len = len;
4907 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4908 tcon->ses->Suid, index, output_size);
4913 void SMB2_query_directory_free(struct smb_rqst *rqst)
4915 if (rqst && rqst->rq_iov) {
4916 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4921 smb2_parse_query_directory(struct cifs_tcon *tcon,
4922 struct kvec *rsp_iov,
4924 struct cifs_search_info *srch_inf)
4926 struct smb2_query_directory_rsp *rsp;
4927 size_t info_buf_size;
4931 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4933 switch (srch_inf->info_level) {
4934 case SMB_FIND_FILE_DIRECTORY_INFO:
4935 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
4937 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4938 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
4940 case SMB_FIND_FILE_POSIX_INFO:
4941 /* note that posix payload are variable size */
4942 info_buf_size = sizeof(struct smb2_posix_info);
4945 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4946 srch_inf->info_level);
4950 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4951 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4954 cifs_tcon_dbg(VFS, "bad info payload");
4958 srch_inf->unicode = true;
4960 if (srch_inf->ntwrk_buf_start) {
4961 if (srch_inf->smallBuf)
4962 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4964 cifs_buf_release(srch_inf->ntwrk_buf_start);
4966 srch_inf->ntwrk_buf_start = (char *)rsp;
4967 srch_inf->srch_entries_start = srch_inf->last_entry =
4968 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4969 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4971 srch_inf->entries_in_buffer = num_entries(
4972 srch_inf->info_level,
4973 srch_inf->srch_entries_start,
4975 &srch_inf->last_entry,
4978 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4979 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4980 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4981 srch_inf->srch_entries_start, srch_inf->last_entry);
4982 if (resp_buftype == CIFS_LARGE_BUFFER)
4983 srch_inf->smallBuf = false;
4984 else if (resp_buftype == CIFS_SMALL_BUFFER)
4985 srch_inf->smallBuf = true;
4987 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4993 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4994 u64 persistent_fid, u64 volatile_fid, int index,
4995 struct cifs_search_info *srch_inf)
4997 struct smb_rqst rqst;
4998 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4999 struct smb2_query_directory_rsp *rsp = NULL;
5000 int resp_buftype = CIFS_NO_BUFFER;
5001 struct kvec rsp_iov;
5003 struct cifs_ses *ses = tcon->ses;
5004 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5007 if (!ses || !(ses->server))
5010 if (smb3_encryption_required(tcon))
5011 flags |= CIFS_TRANSFORM_REQ;
5013 memset(&rqst, 0, sizeof(struct smb_rqst));
5014 memset(&iov, 0, sizeof(iov));
5016 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5018 rc = SMB2_query_directory_init(xid, tcon, server,
5019 &rqst, persistent_fid,
5020 volatile_fid, index,
5021 srch_inf->info_level);
5025 rc = cifs_send_recv(xid, ses, server,
5026 &rqst, &resp_buftype, flags, &rsp_iov);
5027 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5030 if (rc == -ENODATA &&
5031 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5032 trace_smb3_query_dir_done(xid, persistent_fid,
5033 tcon->tid, tcon->ses->Suid, index, 0);
5034 srch_inf->endOfSearch = true;
5037 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5038 tcon->ses->Suid, index, 0, rc);
5039 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5044 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5047 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5048 tcon->ses->Suid, index, 0, rc);
5051 resp_buftype = CIFS_NO_BUFFER;
5053 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5054 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5057 SMB2_query_directory_free(&rqst);
5058 free_rsp_buf(resp_buftype, rsp);
5063 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5064 struct smb_rqst *rqst,
5065 u64 persistent_fid, u64 volatile_fid, u32 pid,
5066 u8 info_class, u8 info_type, u32 additional_info,
5067 void **data, unsigned int *size)
5069 struct smb2_set_info_req *req;
5070 struct kvec *iov = rqst->rq_iov;
5071 unsigned int i, total_len;
5074 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5075 (void **) &req, &total_len);
5079 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5080 req->InfoType = info_type;
5081 req->FileInfoClass = info_class;
5082 req->PersistentFileId = persistent_fid;
5083 req->VolatileFileId = volatile_fid;
5084 req->AdditionalInformation = cpu_to_le32(additional_info);
5086 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5087 req->BufferLength = cpu_to_le32(*size);
5089 memcpy(req->Buffer, *data, *size);
5092 iov[0].iov_base = (char *)req;
5094 iov[0].iov_len = total_len - 1;
5096 for (i = 1; i < rqst->rq_nvec; i++) {
5097 le32_add_cpu(&req->BufferLength, size[i]);
5098 iov[i].iov_base = (char *)data[i];
5099 iov[i].iov_len = size[i];
5106 SMB2_set_info_free(struct smb_rqst *rqst)
5108 if (rqst && rqst->rq_iov)
5109 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5113 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5114 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5115 u8 info_type, u32 additional_info, unsigned int num,
5116 void **data, unsigned int *size)
5118 struct smb_rqst rqst;
5119 struct smb2_set_info_rsp *rsp = NULL;
5121 struct kvec rsp_iov;
5124 struct cifs_ses *ses = tcon->ses;
5125 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5128 if (!ses || !server)
5134 if (smb3_encryption_required(tcon))
5135 flags |= CIFS_TRANSFORM_REQ;
5137 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5141 memset(&rqst, 0, sizeof(struct smb_rqst));
5145 rc = SMB2_set_info_init(tcon, server,
5146 &rqst, persistent_fid, volatile_fid, pid,
5147 info_class, info_type, additional_info,
5155 rc = cifs_send_recv(xid, ses, server,
5156 &rqst, &resp_buftype, flags,
5158 SMB2_set_info_free(&rqst);
5159 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5162 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5163 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5164 ses->Suid, info_class, (__u32)info_type, rc);
5167 free_rsp_buf(resp_buftype, rsp);
5173 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5174 u64 volatile_fid, u32 pid, __le64 *eof)
5176 struct smb2_file_eof_info info;
5180 info.EndOfFile = *eof;
5183 size = sizeof(struct smb2_file_eof_info);
5185 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5187 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5188 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5189 0, 1, &data, &size);
5193 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5194 u64 persistent_fid, u64 volatile_fid,
5195 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5197 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5198 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5199 1, (void **)&pnntsd, &pacllen);
5203 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5204 u64 persistent_fid, u64 volatile_fid,
5205 struct smb2_file_full_ea_info *buf, int len)
5207 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5208 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5209 0, 1, (void **)&buf, &len);
5213 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5214 const u64 persistent_fid, const u64 volatile_fid,
5217 struct smb_rqst rqst;
5219 struct smb2_oplock_break *req = NULL;
5220 struct cifs_ses *ses = tcon->ses;
5221 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5222 int flags = CIFS_OBREAK_OP;
5223 unsigned int total_len;
5225 struct kvec rsp_iov;
5228 cifs_dbg(FYI, "SMB2_oplock_break\n");
5229 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5230 (void **) &req, &total_len);
5234 if (smb3_encryption_required(tcon))
5235 flags |= CIFS_TRANSFORM_REQ;
5237 req->VolatileFid = volatile_fid;
5238 req->PersistentFid = persistent_fid;
5239 req->OplockLevel = oplock_level;
5240 req->hdr.CreditRequest = cpu_to_le16(1);
5242 flags |= CIFS_NO_RSP_BUF;
5244 iov[0].iov_base = (char *)req;
5245 iov[0].iov_len = total_len;
5247 memset(&rqst, 0, sizeof(struct smb_rqst));
5251 rc = cifs_send_recv(xid, ses, server,
5252 &rqst, &resp_buf_type, flags, &rsp_iov);
5253 cifs_small_buf_release(req);
5256 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5257 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5264 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5265 struct kstatfs *kst)
5267 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5268 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5269 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5270 kst->f_bfree = kst->f_bavail =
5271 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5276 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5277 struct kstatfs *kst)
5279 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5280 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5281 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5282 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5283 kst->f_bavail = kst->f_bfree;
5285 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5286 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5287 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5288 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5289 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5295 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5296 struct TCP_Server_Info *server,
5297 int level, int outbuf_len, u64 persistent_fid,
5301 struct smb2_query_info_req *req;
5302 unsigned int total_len;
5304 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5306 if ((tcon->ses == NULL) || server == NULL)
5309 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5310 (void **) &req, &total_len);
5314 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5315 req->FileInfoClass = level;
5316 req->PersistentFileId = persistent_fid;
5317 req->VolatileFileId = volatile_fid;
5319 req->InputBufferOffset =
5320 cpu_to_le16(sizeof(struct smb2_query_info_req));
5321 req->OutputBufferLength = cpu_to_le32(
5322 outbuf_len + sizeof(struct smb2_query_info_rsp));
5324 iov->iov_base = (char *)req;
5325 iov->iov_len = total_len;
5330 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5331 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5333 struct smb_rqst rqst;
5334 struct smb2_query_info_rsp *rsp = NULL;
5336 struct kvec rsp_iov;
5339 struct cifs_ses *ses = tcon->ses;
5340 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5341 FILE_SYSTEM_POSIX_INFO *info = NULL;
5344 rc = build_qfs_info_req(&iov, tcon, server,
5345 FS_POSIX_INFORMATION,
5346 sizeof(FILE_SYSTEM_POSIX_INFO),
5347 persistent_fid, volatile_fid);
5351 if (smb3_encryption_required(tcon))
5352 flags |= CIFS_TRANSFORM_REQ;
5354 memset(&rqst, 0, sizeof(struct smb_rqst));
5358 rc = cifs_send_recv(xid, ses, server,
5359 &rqst, &resp_buftype, flags, &rsp_iov);
5360 cifs_small_buf_release(iov.iov_base);
5362 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5363 goto posix_qfsinf_exit;
5365 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5367 info = (FILE_SYSTEM_POSIX_INFO *)(
5368 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5369 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5370 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5371 sizeof(FILE_SYSTEM_POSIX_INFO));
5373 copy_posix_fs_info_to_kstatfs(info, fsdata);
5376 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5381 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5382 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5384 struct smb_rqst rqst;
5385 struct smb2_query_info_rsp *rsp = NULL;
5387 struct kvec rsp_iov;
5390 struct cifs_ses *ses = tcon->ses;
5391 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5392 struct smb2_fs_full_size_info *info = NULL;
5395 rc = build_qfs_info_req(&iov, tcon, server,
5396 FS_FULL_SIZE_INFORMATION,
5397 sizeof(struct smb2_fs_full_size_info),
5398 persistent_fid, volatile_fid);
5402 if (smb3_encryption_required(tcon))
5403 flags |= CIFS_TRANSFORM_REQ;
5405 memset(&rqst, 0, sizeof(struct smb_rqst));
5409 rc = cifs_send_recv(xid, ses, server,
5410 &rqst, &resp_buftype, flags, &rsp_iov);
5411 cifs_small_buf_release(iov.iov_base);
5413 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5416 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5418 info = (struct smb2_fs_full_size_info *)(
5419 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5420 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5421 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5422 sizeof(struct smb2_fs_full_size_info));
5424 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5427 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5432 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5433 u64 persistent_fid, u64 volatile_fid, int level)
5435 struct smb_rqst rqst;
5436 struct smb2_query_info_rsp *rsp = NULL;
5438 struct kvec rsp_iov;
5440 int resp_buftype, max_len, min_len;
5441 struct cifs_ses *ses = tcon->ses;
5442 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5443 unsigned int rsp_len, offset;
5446 if (level == FS_DEVICE_INFORMATION) {
5447 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5448 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5449 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5450 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5451 min_len = MIN_FS_ATTR_INFO_SIZE;
5452 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5453 max_len = sizeof(struct smb3_fs_ss_info);
5454 min_len = sizeof(struct smb3_fs_ss_info);
5455 } else if (level == FS_VOLUME_INFORMATION) {
5456 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5457 min_len = sizeof(struct smb3_fs_vol_info);
5459 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5463 rc = build_qfs_info_req(&iov, tcon, server,
5465 persistent_fid, volatile_fid);
5469 if (smb3_encryption_required(tcon))
5470 flags |= CIFS_TRANSFORM_REQ;
5472 memset(&rqst, 0, sizeof(struct smb_rqst));
5476 rc = cifs_send_recv(xid, ses, server,
5477 &rqst, &resp_buftype, flags, &rsp_iov);
5478 cifs_small_buf_release(iov.iov_base);
5480 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5483 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5485 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5486 offset = le16_to_cpu(rsp->OutputBufferOffset);
5487 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5491 if (level == FS_ATTRIBUTE_INFORMATION)
5492 memcpy(&tcon->fsAttrInfo, offset
5493 + (char *)rsp, min_t(unsigned int,
5495 else if (level == FS_DEVICE_INFORMATION)
5496 memcpy(&tcon->fsDevInfo, offset
5497 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5498 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5499 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5500 (offset + (char *)rsp);
5501 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5502 tcon->perf_sector_size =
5503 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5504 } else if (level == FS_VOLUME_INFORMATION) {
5505 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5506 (offset + (char *)rsp);
5507 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5508 tcon->vol_create_time = vol_info->VolumeCreationTime;
5512 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5517 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5518 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5519 const __u32 num_lock, struct smb2_lock_element *buf)
5521 struct smb_rqst rqst;
5523 struct smb2_lock_req *req = NULL;
5525 struct kvec rsp_iov;
5528 int flags = CIFS_NO_RSP_BUF;
5529 unsigned int total_len;
5530 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5532 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5534 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5535 (void **) &req, &total_len);
5539 if (smb3_encryption_required(tcon))
5540 flags |= CIFS_TRANSFORM_REQ;
5542 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5543 req->LockCount = cpu_to_le16(num_lock);
5545 req->PersistentFileId = persist_fid;
5546 req->VolatileFileId = volatile_fid;
5548 count = num_lock * sizeof(struct smb2_lock_element);
5550 iov[0].iov_base = (char *)req;
5551 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5552 iov[1].iov_base = (char *)buf;
5553 iov[1].iov_len = count;
5555 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5557 memset(&rqst, 0, sizeof(struct smb_rqst));
5561 rc = cifs_send_recv(xid, tcon->ses, server,
5562 &rqst, &resp_buf_type, flags,
5564 cifs_small_buf_release(req);
5566 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5567 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5568 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5569 tcon->ses->Suid, rc);
5576 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5577 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5578 const __u64 length, const __u64 offset, const __u32 lock_flags,
5581 struct smb2_lock_element lock;
5583 lock.Offset = cpu_to_le64(offset);
5584 lock.Length = cpu_to_le64(length);
5585 lock.Flags = cpu_to_le32(lock_flags);
5586 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5587 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5589 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5593 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5594 __u8 *lease_key, const __le32 lease_state)
5596 struct smb_rqst rqst;
5598 struct smb2_lease_ack *req = NULL;
5599 struct cifs_ses *ses = tcon->ses;
5600 int flags = CIFS_OBREAK_OP;
5601 unsigned int total_len;
5603 struct kvec rsp_iov;
5605 __u64 *please_key_high;
5606 __u64 *please_key_low;
5607 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5609 cifs_dbg(FYI, "SMB2_lease_break\n");
5610 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5611 (void **) &req, &total_len);
5615 if (smb3_encryption_required(tcon))
5616 flags |= CIFS_TRANSFORM_REQ;
5618 req->hdr.CreditRequest = cpu_to_le16(1);
5619 req->StructureSize = cpu_to_le16(36);
5622 memcpy(req->LeaseKey, lease_key, 16);
5623 req->LeaseState = lease_state;
5625 flags |= CIFS_NO_RSP_BUF;
5627 iov[0].iov_base = (char *)req;
5628 iov[0].iov_len = total_len;
5630 memset(&rqst, 0, sizeof(struct smb_rqst));
5634 rc = cifs_send_recv(xid, ses, server,
5635 &rqst, &resp_buf_type, flags, &rsp_iov);
5636 cifs_small_buf_release(req);
5638 please_key_low = (__u64 *)lease_key;
5639 please_key_high = (__u64 *)(lease_key+8);
5641 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5642 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5643 ses->Suid, *please_key_low, *please_key_high, rc);
5644 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5646 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5647 ses->Suid, *please_key_low, *please_key_high);