1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/task_io_accounting_ops.h>
22 #include <linux/uaccess.h>
23 #include <linux/uuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/xattr.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
44 * The following table defines the expected "StructureSize" of SMB2 requests
45 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
47 * Note that commands are defined in smb2pdu.h in le16 but the array below is
48 * indexed by command in host byte order.
50 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
51 /* SMB2_NEGOTIATE */ 36,
52 /* SMB2_SESSION_SETUP */ 25,
54 /* SMB2_TREE_CONNECT */ 9,
55 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_QUERY_DIRECTORY */ 33,
66 /* SMB2_CHANGE_NOTIFY */ 32,
67 /* SMB2_QUERY_INFO */ 41,
68 /* SMB2_SET_INFO */ 33,
69 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
72 int smb3_encryption_required(const struct cifs_tcon *tcon)
74 if (!tcon || !tcon->ses)
76 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
77 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
80 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
86 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
87 const struct cifs_tcon *tcon,
88 struct TCP_Server_Info *server)
90 shdr->ProtocolId = SMB2_PROTO_NUMBER;
91 shdr->StructureSize = cpu_to_le16(64);
92 shdr->Command = smb2_cmd;
94 spin_lock(&server->req_lock);
95 /* Request up to 10 credits but don't go over the limit. */
96 if (server->credits >= server->max_credits)
97 shdr->CreditRequest = cpu_to_le16(0);
99 shdr->CreditRequest = cpu_to_le16(
100 min_t(int, server->max_credits -
101 server->credits, 10));
102 spin_unlock(&server->req_lock);
104 shdr->CreditRequest = cpu_to_le16(2);
106 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
111 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
112 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
113 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
114 shdr->CreditCharge = cpu_to_le16(1);
115 /* else CreditCharge MBZ */
117 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
118 /* Uid is not converted */
120 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
123 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
124 * to pass the path on the Open SMB prefixed by \\server\share.
125 * Not sure when we would need to do the augmented path (if ever) and
126 * setting this flag breaks the SMB2 open operation since it is
127 * illegal to send an empty path name (without \\server\share prefix)
128 * when the DFS flag is set in the SMB open header. We could
129 * consider setting the flag on all operations other than open
130 * but it is safer to net set it for now.
132 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
133 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
135 if (server && server->sign && !smb3_encryption_required(tcon))
136 shdr->Flags |= SMB2_FLAGS_SIGNED;
142 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
143 struct TCP_Server_Info *server)
146 struct nls_table *nls_codepage;
147 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(&cifs_tcp_ses_lock);
166 if (tcon->status == TID_EXITING) {
168 * only tree disconnect, open, and write,
169 * (and ulogoff which does not have tcon)
170 * are allowed as we start force umount.
172 if ((smb2_command != SMB2_WRITE) &&
173 (smb2_command != SMB2_CREATE) &&
174 (smb2_command != SMB2_TREE_DISCONNECT)) {
175 spin_unlock(&cifs_tcp_ses_lock);
176 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
181 spin_unlock(&cifs_tcp_ses_lock);
182 if ((!tcon->ses) || (tcon->ses->ses_status == SES_EXITING) ||
183 (!tcon->ses->server) || !server)
187 retries = server->nr_targets;
190 * Give demultiplex thread up to 10 seconds to each target available for
191 * reconnect -- should be greater than cifs socket timeout which is 7
194 while (server->tcpStatus == CifsNeedReconnect) {
196 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
197 * here since they are implicitly done when session drops.
199 switch (smb2_command) {
201 * BB Should we keep oplock break and add flush to exceptions?
203 case SMB2_TREE_DISCONNECT:
206 case SMB2_OPLOCK_BREAK:
210 rc = wait_event_interruptible_timeout(server->response_q,
211 (server->tcpStatus != CifsNeedReconnect),
214 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
219 /* are we still trying to reconnect? */
220 spin_lock(&cifs_tcp_ses_lock);
221 if (server->tcpStatus != CifsNeedReconnect) {
222 spin_unlock(&cifs_tcp_ses_lock);
225 spin_unlock(&cifs_tcp_ses_lock);
227 if (retries && --retries)
231 * on "soft" mounts we wait once. Hard mounts keep
232 * retrying until process is killed or server comes
236 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
239 retries = server->nr_targets;
242 spin_lock(&ses->chan_lock);
243 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
244 spin_unlock(&ses->chan_lock);
247 spin_unlock(&ses->chan_lock);
248 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
249 tcon->ses->chans_need_reconnect,
250 tcon->need_reconnect);
252 nls_codepage = load_nls_default();
255 * Recheck after acquire mutex. If another thread is negotiating
256 * and the server never sends an answer the socket will be closed
257 * and tcpStatus set to reconnect.
259 spin_lock(&cifs_tcp_ses_lock);
260 if (server->tcpStatus == CifsNeedReconnect) {
261 spin_unlock(&cifs_tcp_ses_lock);
265 spin_unlock(&cifs_tcp_ses_lock);
268 * need to prevent multiple threads trying to simultaneously
269 * reconnect the same SMB session
271 spin_lock(&ses->chan_lock);
272 if (!cifs_chan_needs_reconnect(ses, server)) {
273 spin_unlock(&ses->chan_lock);
275 /* this means that we only need to tree connect */
276 if (tcon->need_reconnect)
277 goto skip_sess_setup;
281 spin_unlock(&ses->chan_lock);
283 mutex_lock(&ses->session_mutex);
284 rc = cifs_negotiate_protocol(0, ses, server);
286 rc = cifs_setup_session(0, ses, server, nls_codepage);
287 if ((rc == -EACCES) && !tcon->retry) {
288 mutex_unlock(&ses->session_mutex);
292 mutex_unlock(&ses->session_mutex);
296 mutex_unlock(&ses->session_mutex);
299 mutex_unlock(&ses->session_mutex);
302 mutex_lock(&ses->session_mutex);
303 if (!tcon->need_reconnect) {
304 mutex_unlock(&ses->session_mutex);
307 cifs_mark_open_files_invalid(tcon);
308 if (tcon->use_persistent)
309 tcon->need_reopen_files = true;
311 rc = cifs_tree_connect(0, tcon, nls_codepage);
312 mutex_unlock(&ses->session_mutex);
314 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
316 /* If sess reconnected but tcon didn't, something strange ... */
317 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
321 if (smb2_command != SMB2_INTERNAL_CMD)
322 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
324 atomic_inc(&tconInfoReconnectCount);
327 * Check if handle based operation so we know whether we can continue
328 * or not without returning to caller to reset file handle.
331 * BB Is flush done by server on drop of tcp session? Should we special
332 * case it and skip above?
334 switch (smb2_command) {
340 case SMB2_QUERY_DIRECTORY:
341 case SMB2_CHANGE_NOTIFY:
342 case SMB2_QUERY_INFO:
347 unload_nls(nls_codepage);
352 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
353 struct TCP_Server_Info *server,
355 unsigned int *total_len)
357 struct smb2_pdu *spdu = (struct smb2_pdu *)buf;
358 /* lookup word count ie StructureSize from table */
359 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
362 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
363 * largest operations (Create)
367 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
368 spdu->StructureSize2 = cpu_to_le16(parmsize);
370 *total_len = parmsize + sizeof(struct smb2_hdr);
374 * Allocate and return pointer to an SMB request hdr, and set basic
375 * SMB information in the SMB header. If the return code is zero, this
376 * function must have filled in request_buf pointer.
378 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
379 struct TCP_Server_Info *server,
380 void **request_buf, unsigned int *total_len)
382 /* BB eventually switch this to SMB2 specific small buf size */
383 if (smb2_command == SMB2_SET_INFO)
384 *request_buf = cifs_buf_get();
386 *request_buf = cifs_small_buf_get();
387 if (*request_buf == NULL) {
388 /* BB should we add a retry in here if not a writepage? */
392 fill_small_buf(smb2_command, tcon, server,
393 (struct smb2_hdr *)(*request_buf),
397 uint16_t com_code = le16_to_cpu(smb2_command);
398 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
399 cifs_stats_inc(&tcon->num_smbs_sent);
405 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
406 struct TCP_Server_Info *server,
407 void **request_buf, unsigned int *total_len)
411 rc = smb2_reconnect(smb2_command, tcon, server);
415 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
419 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
420 struct TCP_Server_Info *server,
421 void **request_buf, unsigned int *total_len)
423 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
424 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
425 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
426 request_buf, total_len);
428 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
429 request_buf, total_len);
432 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
435 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
437 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
438 pneg_ctxt->DataLength = cpu_to_le16(38);
439 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
440 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
441 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
442 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
446 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
448 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
449 pneg_ctxt->DataLength =
450 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
451 - sizeof(struct smb2_neg_context));
452 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
453 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
454 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
455 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
459 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
461 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
462 unsigned short num_algs = 1; /* number of signing algorithms sent */
464 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
466 * Context Data length must be rounded to multiple of 8 for some servers
468 pneg_ctxt->DataLength = cpu_to_le16(DIV_ROUND_UP(
469 sizeof(struct smb2_signing_capabilities) -
470 sizeof(struct smb2_neg_context) +
471 (num_algs * 2 /* sizeof u16 */), 8) * 8);
472 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
473 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
475 ctxt_len += 2 /* sizeof le16 */ * num_algs;
476 ctxt_len = DIV_ROUND_UP(ctxt_len, 8) * 8;
478 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
482 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
484 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
485 if (require_gcm_256) {
486 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
487 pneg_ctxt->CipherCount = cpu_to_le16(1);
488 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
489 } else if (enable_gcm_256) {
490 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
491 pneg_ctxt->CipherCount = cpu_to_le16(3);
492 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
493 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
494 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
496 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
497 pneg_ctxt->CipherCount = cpu_to_le16(2);
498 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
499 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
504 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
506 struct nls_table *cp = load_nls_default();
508 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
510 /* copy up to max of first 100 bytes of server name to NetName field */
511 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
512 /* context size is DataLength + minimal smb2_neg_context */
513 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
514 sizeof(struct smb2_neg_context), 8) * 8;
518 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
520 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
521 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
522 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
523 pneg_ctxt->Name[0] = 0x93;
524 pneg_ctxt->Name[1] = 0xAD;
525 pneg_ctxt->Name[2] = 0x25;
526 pneg_ctxt->Name[3] = 0x50;
527 pneg_ctxt->Name[4] = 0x9C;
528 pneg_ctxt->Name[5] = 0xB4;
529 pneg_ctxt->Name[6] = 0x11;
530 pneg_ctxt->Name[7] = 0xE7;
531 pneg_ctxt->Name[8] = 0xB4;
532 pneg_ctxt->Name[9] = 0x23;
533 pneg_ctxt->Name[10] = 0x83;
534 pneg_ctxt->Name[11] = 0xDE;
535 pneg_ctxt->Name[12] = 0x96;
536 pneg_ctxt->Name[13] = 0x8B;
537 pneg_ctxt->Name[14] = 0xCD;
538 pneg_ctxt->Name[15] = 0x7C;
542 assemble_neg_contexts(struct smb2_negotiate_req *req,
543 struct TCP_Server_Info *server, unsigned int *total_len)
546 char *hostname = NULL;
547 unsigned int ctxt_len, neg_context_count;
549 if (*total_len > 200) {
550 /* In case length corrupted don't want to overrun smb buffer */
551 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
556 * round up total_len of fixed part of SMB3 negotiate request to 8
557 * byte boundary before adding negotiate contexts
559 *total_len = roundup(*total_len, 8);
561 pneg_ctxt = (*total_len) + (char *)req;
562 req->NegotiateContextOffset = cpu_to_le32(*total_len);
564 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
565 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
566 *total_len += ctxt_len;
567 pneg_ctxt += ctxt_len;
569 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
570 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
571 *total_len += ctxt_len;
572 pneg_ctxt += ctxt_len;
574 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
575 *total_len += sizeof(struct smb2_posix_neg_context);
576 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
579 * secondary channels don't have the hostname field populated
580 * use the hostname field in the primary channel instead
582 hostname = CIFS_SERVER_IS_CHAN(server) ?
583 server->primary_server->hostname : server->hostname;
584 if (hostname && (hostname[0] != 0)) {
585 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
587 *total_len += ctxt_len;
588 pneg_ctxt += ctxt_len;
589 neg_context_count = 4;
590 } else /* second channels do not have a hostname */
591 neg_context_count = 3;
593 if (server->compress_algorithm) {
594 build_compression_ctxt((struct smb2_compression_capabilities_context *)
596 ctxt_len = DIV_ROUND_UP(
597 sizeof(struct smb2_compression_capabilities_context),
599 *total_len += ctxt_len;
600 pneg_ctxt += ctxt_len;
604 if (enable_negotiate_signing) {
605 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
607 *total_len += ctxt_len;
608 pneg_ctxt += ctxt_len;
612 /* check for and add transport_capabilities and signing capabilities */
613 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
617 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
619 unsigned int len = le16_to_cpu(ctxt->DataLength);
621 /* If invalid preauth context warn but use what we requested, SHA-512 */
622 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
623 pr_warn_once("server sent bad preauth context\n");
625 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
626 pr_warn_once("server sent invalid SaltLength\n");
629 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
630 pr_warn_once("Invalid SMB3 hash algorithm count\n");
631 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
632 pr_warn_once("unknown SMB3 hash algorithm\n");
635 static void decode_compress_ctx(struct TCP_Server_Info *server,
636 struct smb2_compression_capabilities_context *ctxt)
638 unsigned int len = le16_to_cpu(ctxt->DataLength);
640 /* sizeof compress context is a one element compression capbility struct */
642 pr_warn_once("server sent bad compression cntxt\n");
645 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
646 pr_warn_once("Invalid SMB3 compress algorithm count\n");
649 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
650 pr_warn_once("unknown compression algorithm\n");
653 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
656 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
657 struct smb2_encryption_neg_context *ctxt)
659 unsigned int len = le16_to_cpu(ctxt->DataLength);
661 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
662 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
663 pr_warn_once("server sent bad crypto ctxt len\n");
667 if (le16_to_cpu(ctxt->CipherCount) != 1) {
668 pr_warn_once("Invalid SMB3.11 cipher count\n");
671 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
672 if (require_gcm_256) {
673 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
674 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
677 } else if (ctxt->Ciphers[0] == 0) {
679 * e.g. if server only supported AES256_CCM (very unlikely)
680 * or server supported no encryption types or had all disabled.
681 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
682 * in which mount requested encryption ("seal") checks later
683 * on during tree connection will return proper rc, but if
684 * seal not requested by client, since server is allowed to
685 * return 0 to indicate no supported cipher, we can't fail here
687 server->cipher_type = 0;
688 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
689 pr_warn_once("Server does not support requested encryption types\n");
691 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
692 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
693 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
694 /* server returned a cipher we didn't ask for */
695 pr_warn_once("Invalid SMB3.11 cipher returned\n");
698 server->cipher_type = ctxt->Ciphers[0];
699 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
703 static void decode_signing_ctx(struct TCP_Server_Info *server,
704 struct smb2_signing_capabilities *pctxt)
706 unsigned int len = le16_to_cpu(pctxt->DataLength);
708 if ((len < 4) || (len > 16)) {
709 pr_warn_once("server sent bad signing negcontext\n");
712 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
713 pr_warn_once("Invalid signing algorithm count\n");
716 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
717 pr_warn_once("unknown signing algorithm\n");
721 server->signing_negotiated = true;
722 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
723 cifs_dbg(FYI, "signing algorithm %d chosen\n",
724 server->signing_algorithm);
728 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
729 struct TCP_Server_Info *server,
730 unsigned int len_of_smb)
732 struct smb2_neg_context *pctx;
733 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
734 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
735 unsigned int len_of_ctxts, i;
738 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
739 if (len_of_smb <= offset) {
740 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
744 len_of_ctxts = len_of_smb - offset;
746 for (i = 0; i < ctxt_cnt; i++) {
748 /* check that offset is not beyond end of SMB */
749 if (len_of_ctxts == 0)
752 if (len_of_ctxts < sizeof(struct smb2_neg_context))
755 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
756 clen = le16_to_cpu(pctx->DataLength);
757 if (clen > len_of_ctxts)
760 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
761 decode_preauth_context(
762 (struct smb2_preauth_neg_context *)pctx);
763 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
764 rc = decode_encrypt_ctx(server,
765 (struct smb2_encryption_neg_context *)pctx);
766 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
767 decode_compress_ctx(server,
768 (struct smb2_compression_capabilities_context *)pctx);
769 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
770 server->posix_ext_supported = true;
771 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
772 decode_signing_ctx(server,
773 (struct smb2_signing_capabilities *)pctx);
775 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
776 le16_to_cpu(pctx->ContextType));
780 /* offsets must be 8 byte aligned */
781 clen = (clen + 7) & ~0x7;
782 offset += clen + sizeof(struct smb2_neg_context);
783 len_of_ctxts -= clen;
788 static struct create_posix *
789 create_posix_buf(umode_t mode)
791 struct create_posix *buf;
793 buf = kzalloc(sizeof(struct create_posix),
798 buf->ccontext.DataOffset =
799 cpu_to_le16(offsetof(struct create_posix, Mode));
800 buf->ccontext.DataLength = cpu_to_le32(4);
801 buf->ccontext.NameOffset =
802 cpu_to_le16(offsetof(struct create_posix, Name));
803 buf->ccontext.NameLength = cpu_to_le16(16);
805 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
816 buf->Name[10] = 0x83;
817 buf->Name[11] = 0xDE;
818 buf->Name[12] = 0x96;
819 buf->Name[13] = 0x8B;
820 buf->Name[14] = 0xCD;
821 buf->Name[15] = 0x7C;
822 buf->Mode = cpu_to_le32(mode);
823 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
828 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
830 struct smb2_create_req *req = iov[0].iov_base;
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 if (!req->CreateContextsOffset)
840 req->CreateContextsOffset = cpu_to_le32(
841 sizeof(struct smb2_create_req) +
842 iov[num - 1].iov_len);
843 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
844 *num_iovec = num + 1;
851 * SMB2 Worker functions follow:
853 * The general structure of the worker functions is:
854 * 1) Call smb2_init (assembles SMB2 header)
855 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
856 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
857 * 4) Decode SMB2 command specific fields in the fixed length area
858 * 5) Decode variable length data area (if any for this SMB2 command type)
859 * 6) Call free smb buffer
865 SMB2_negotiate(const unsigned int xid,
866 struct cifs_ses *ses,
867 struct TCP_Server_Info *server)
869 struct smb_rqst rqst;
870 struct smb2_negotiate_req *req;
871 struct smb2_negotiate_rsp *rsp;
876 int blob_offset, blob_length;
878 int flags = CIFS_NEG_OP;
879 unsigned int total_len;
881 cifs_dbg(FYI, "Negotiate protocol\n");
884 WARN(1, "%s: server is NULL!\n", __func__);
888 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
889 (void **) &req, &total_len);
893 req->hdr.SessionId = 0;
895 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
896 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
898 if (strcmp(server->vals->version_string,
899 SMB3ANY_VERSION_STRING) == 0) {
900 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
901 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
902 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
903 req->DialectCount = cpu_to_le16(3);
905 } else if (strcmp(server->vals->version_string,
906 SMBDEFAULT_VERSION_STRING) == 0) {
907 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
908 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
909 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
910 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
911 req->DialectCount = cpu_to_le16(4);
914 /* otherwise send specific dialect */
915 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
916 req->DialectCount = cpu_to_le16(1);
920 /* only one of SMB2 signing flags may be set in SMB2 request */
922 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
923 else if (global_secflags & CIFSSEC_MAY_SIGN)
924 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
926 req->SecurityMode = 0;
928 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
929 if (ses->chan_max > 1)
930 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
932 /* ClientGUID must be zero for SMB2.02 dialect */
933 if (server->vals->protocol_id == SMB20_PROT_ID)
934 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
936 memcpy(req->ClientGUID, server->client_guid,
937 SMB2_CLIENT_GUID_SIZE);
938 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
939 (strcmp(server->vals->version_string,
940 SMB3ANY_VERSION_STRING) == 0) ||
941 (strcmp(server->vals->version_string,
942 SMBDEFAULT_VERSION_STRING) == 0))
943 assemble_neg_contexts(req, server, &total_len);
945 iov[0].iov_base = (char *)req;
946 iov[0].iov_len = total_len;
948 memset(&rqst, 0, sizeof(struct smb_rqst));
952 rc = cifs_send_recv(xid, ses, server,
953 &rqst, &resp_buftype, flags, &rsp_iov);
954 cifs_small_buf_release(req);
955 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
957 * No tcon so can't do
958 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
960 if (rc == -EOPNOTSUPP) {
961 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");
966 if (strcmp(server->vals->version_string,
967 SMB3ANY_VERSION_STRING) == 0) {
968 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
970 "SMB2 dialect returned but not requested\n");
972 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
974 "SMB2.1 dialect returned but not requested\n");
976 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
977 /* ops set to 3.0 by default for default so update */
978 server->ops = &smb311_operations;
979 server->vals = &smb311_values;
981 } else if (strcmp(server->vals->version_string,
982 SMBDEFAULT_VERSION_STRING) == 0) {
983 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
985 "SMB2 dialect returned but not requested\n");
987 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
988 /* ops set to 3.0 by default for default so update */
989 server->ops = &smb21_operations;
990 server->vals = &smb21_values;
991 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
992 server->ops = &smb311_operations;
993 server->vals = &smb311_values;
995 } else if (le16_to_cpu(rsp->DialectRevision) !=
996 server->vals->protocol_id) {
997 /* if requested single dialect ensure returned dialect matched */
998 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
999 le16_to_cpu(rsp->DialectRevision));
1003 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1005 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1006 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1007 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1008 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1009 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1010 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1011 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1012 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1013 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1014 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1016 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1017 le16_to_cpu(rsp->DialectRevision));
1021 server->dialect = le16_to_cpu(rsp->DialectRevision);
1024 * Keep a copy of the hash after negprot. This hash will be
1025 * the starting hash value for all sessions made from this
1028 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1029 SMB2_PREAUTH_HASH_SIZE);
1031 /* SMB2 only has an extended negflavor */
1032 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1033 /* set it to the maximum buffer size value we can send with 1 credit */
1034 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1035 SMB2_MAX_BUFFER_SIZE);
1036 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1037 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1038 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1039 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1040 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1042 server->capabilities = le32_to_cpu(rsp->Capabilities);
1043 /* Internal types */
1044 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1047 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1048 * Set the cipher type manually.
1050 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1051 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1053 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1054 (struct smb2_hdr *)rsp);
1056 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1058 * ses->sectype = RawNTLMSSP;
1059 * but for time being this is our only auth choice so doesn't matter.
1060 * We just found a server which sets blob length to zero expecting raw.
1062 if (blob_length == 0) {
1063 cifs_dbg(FYI, "missing security blob on negprot\n");
1064 server->sec_ntlmssp = true;
1067 rc = cifs_enable_signing(server, ses->sign);
1071 rc = decode_negTokenInit(security_blob, blob_length, server);
1078 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1079 if (rsp->NegotiateContextCount)
1080 rc = smb311_decode_neg_context(rsp, server,
1083 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1086 free_rsp_buf(resp_buftype, rsp);
1090 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1093 struct validate_negotiate_info_req *pneg_inbuf;
1094 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1096 u32 inbuflen; /* max of 4 dialects */
1097 struct TCP_Server_Info *server = tcon->ses->server;
1099 cifs_dbg(FYI, "validate negotiate\n");
1101 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1102 if (server->dialect == SMB311_PROT_ID)
1106 * validation ioctl must be signed, so no point sending this if we
1107 * can not sign it (ie are not known user). Even if signing is not
1108 * required (enabled but not negotiated), in those cases we selectively
1109 * sign just this, the first and only signed request on a connection.
1110 * Having validation of negotiate info helps reduce attack vectors.
1112 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1113 return 0; /* validation requires signing */
1115 if (tcon->ses->user_name == NULL) {
1116 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1117 return 0; /* validation requires signing */
1120 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1121 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1123 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1127 pneg_inbuf->Capabilities =
1128 cpu_to_le32(server->vals->req_capabilities);
1129 if (tcon->ses->chan_max > 1)
1130 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1132 memcpy(pneg_inbuf->Guid, server->client_guid,
1133 SMB2_CLIENT_GUID_SIZE);
1135 if (tcon->ses->sign)
1136 pneg_inbuf->SecurityMode =
1137 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1138 else if (global_secflags & CIFSSEC_MAY_SIGN)
1139 pneg_inbuf->SecurityMode =
1140 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1142 pneg_inbuf->SecurityMode = 0;
1145 if (strcmp(server->vals->version_string,
1146 SMB3ANY_VERSION_STRING) == 0) {
1147 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1148 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1149 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1150 pneg_inbuf->DialectCount = cpu_to_le16(3);
1151 /* SMB 2.1 not included so subtract one dialect from len */
1152 inbuflen = sizeof(*pneg_inbuf) -
1153 (sizeof(pneg_inbuf->Dialects[0]));
1154 } else if (strcmp(server->vals->version_string,
1155 SMBDEFAULT_VERSION_STRING) == 0) {
1156 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1157 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1158 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1159 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1160 pneg_inbuf->DialectCount = cpu_to_le16(4);
1161 /* structure is big enough for 4 dialects */
1162 inbuflen = sizeof(*pneg_inbuf);
1164 /* otherwise specific dialect was requested */
1165 pneg_inbuf->Dialects[0] =
1166 cpu_to_le16(server->vals->protocol_id);
1167 pneg_inbuf->DialectCount = cpu_to_le16(1);
1168 /* structure is big enough for 3 dialects, sending only 1 */
1169 inbuflen = sizeof(*pneg_inbuf) -
1170 sizeof(pneg_inbuf->Dialects[0]) * 2;
1173 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1174 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1175 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1176 (char **)&pneg_rsp, &rsplen);
1177 if (rc == -EOPNOTSUPP) {
1179 * Old Windows versions or Netapp SMB server can return
1180 * not supported error. Client should accept it.
1182 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1184 goto out_free_inbuf;
1185 } else if (rc != 0) {
1186 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1189 goto out_free_inbuf;
1193 if (rsplen != sizeof(*pneg_rsp)) {
1194 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1197 /* relax check since Mac returns max bufsize allowed on ioctl */
1198 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1202 /* check validate negotiate info response matches what we got earlier */
1203 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1206 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1209 /* do not validate server guid because not saved at negprot time yet */
1211 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1212 SMB2_LARGE_FILES) != server->capabilities)
1215 /* validate negotiate successful */
1217 cifs_dbg(FYI, "validate negotiate info successful\n");
1221 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1230 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1232 switch (requested) {
1239 if (server->sec_ntlmssp &&
1240 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1242 if ((server->sec_kerberos || server->sec_mskerberos) &&
1243 (global_secflags & CIFSSEC_MAY_KRB5))
1251 struct SMB2_sess_data {
1253 struct cifs_ses *ses;
1254 struct TCP_Server_Info *server;
1255 struct nls_table *nls_cp;
1256 void (*func)(struct SMB2_sess_data *);
1258 u64 previous_session;
1260 /* we will send the SMB in three pieces:
1261 * a fixed length beginning part, an optional
1262 * SPNEGO blob (which can be zero length), and a
1263 * last part which will include the strings
1264 * and rest of bcc area. This allows us to avoid
1265 * a large buffer 17K allocation
1272 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1275 struct cifs_ses *ses = sess_data->ses;
1276 struct TCP_Server_Info *server = sess_data->server;
1277 struct smb2_sess_setup_req *req;
1278 unsigned int total_len;
1279 bool is_binding = false;
1281 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1287 spin_lock(&ses->chan_lock);
1288 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1289 spin_unlock(&ses->chan_lock);
1292 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1293 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1294 req->PreviousSessionId = 0;
1295 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1296 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1298 /* First session, not a reauthenticate */
1299 req->hdr.SessionId = 0;
1301 * if reconnect, we need to send previous sess id
1304 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1305 req->Flags = 0; /* MBZ */
1306 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1307 sess_data->previous_session);
1310 /* enough to enable echos and oplocks and one max size write */
1311 req->hdr.CreditRequest = cpu_to_le16(130);
1313 /* only one of SMB2 signing flags may be set in SMB2 request */
1315 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1316 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1317 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1319 req->SecurityMode = 0;
1321 #ifdef CONFIG_CIFS_DFS_UPCALL
1322 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1324 req->Capabilities = 0;
1325 #endif /* DFS_UPCALL */
1327 req->Channel = 0; /* MBZ */
1329 sess_data->iov[0].iov_base = (char *)req;
1331 sess_data->iov[0].iov_len = total_len - 1;
1333 * This variable will be used to clear the buffer
1334 * allocated above in case of any error in the calling function.
1336 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1342 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1344 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1345 sess_data->buf0_type = CIFS_NO_BUFFER;
1349 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1352 struct smb_rqst rqst;
1353 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1354 struct kvec rsp_iov = { NULL, 0 };
1356 /* Testing shows that buffer offset must be at location of Buffer[0] */
1357 req->SecurityBufferOffset =
1358 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1359 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1361 memset(&rqst, 0, sizeof(struct smb_rqst));
1362 rqst.rq_iov = sess_data->iov;
1365 /* BB add code to build os and lm fields */
1366 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1369 &sess_data->buf0_type,
1370 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1371 cifs_small_buf_release(sess_data->iov[0].iov_base);
1372 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1378 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1381 struct cifs_ses *ses = sess_data->ses;
1382 struct TCP_Server_Info *server = sess_data->server;
1384 cifs_server_lock(server);
1385 if (server->ops->generate_signingkey) {
1386 rc = server->ops->generate_signingkey(ses, server);
1389 "SMB3 session key generation failed\n");
1390 cifs_server_unlock(server);
1394 if (!server->session_estab) {
1395 server->sequence_number = 0x2;
1396 server->session_estab = true;
1398 cifs_server_unlock(server);
1400 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1404 #ifdef CONFIG_CIFS_UPCALL
1406 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1409 struct cifs_ses *ses = sess_data->ses;
1410 struct TCP_Server_Info *server = sess_data->server;
1411 struct cifs_spnego_msg *msg;
1412 struct key *spnego_key = NULL;
1413 struct smb2_sess_setup_rsp *rsp = NULL;
1414 bool is_binding = false;
1416 rc = SMB2_sess_alloc_buffer(sess_data);
1420 spnego_key = cifs_get_spnego_key(ses, server);
1421 if (IS_ERR(spnego_key)) {
1422 rc = PTR_ERR(spnego_key);
1424 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1429 msg = spnego_key->payload.data[0];
1431 * check version field to make sure that cifs.upcall is
1432 * sending us a response in an expected form
1434 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1435 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1436 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1438 goto out_put_spnego_key;
1441 spin_lock(&ses->chan_lock);
1442 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1443 spin_unlock(&ses->chan_lock);
1445 /* keep session key if binding */
1447 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1449 if (!ses->auth_key.response) {
1450 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1453 goto out_put_spnego_key;
1455 ses->auth_key.len = msg->sesskey_len;
1458 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1459 sess_data->iov[1].iov_len = msg->secblob_len;
1461 rc = SMB2_sess_sendreceive(sess_data);
1463 goto out_put_spnego_key;
1465 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1466 /* keep session id and flags if binding */
1468 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1469 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1472 rc = SMB2_sess_establish_session(sess_data);
1474 key_invalidate(spnego_key);
1475 key_put(spnego_key);
1477 sess_data->result = rc;
1478 sess_data->func = NULL;
1479 SMB2_sess_free_buffer(sess_data);
1483 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1485 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1486 sess_data->result = -EOPNOTSUPP;
1487 sess_data->func = NULL;
1492 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1495 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1498 struct cifs_ses *ses = sess_data->ses;
1499 struct TCP_Server_Info *server = sess_data->server;
1500 struct smb2_sess_setup_rsp *rsp = NULL;
1501 unsigned char *ntlmssp_blob = NULL;
1502 bool use_spnego = false; /* else use raw ntlmssp */
1503 u16 blob_length = 0;
1504 bool is_binding = false;
1507 * If memory allocation is successful, caller of this function
1510 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1511 if (!ses->ntlmssp) {
1515 ses->ntlmssp->sesskey_per_smbsess = true;
1517 rc = SMB2_sess_alloc_buffer(sess_data);
1521 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1522 &blob_length, ses, server,
1528 /* BB eventually need to add this */
1529 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1533 sess_data->iov[1].iov_base = ntlmssp_blob;
1534 sess_data->iov[1].iov_len = blob_length;
1536 rc = SMB2_sess_sendreceive(sess_data);
1537 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1539 /* If true, rc here is expected and not an error */
1540 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1541 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1547 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1548 le16_to_cpu(rsp->SecurityBufferOffset)) {
1549 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1550 le16_to_cpu(rsp->SecurityBufferOffset));
1554 rc = decode_ntlmssp_challenge(rsp->Buffer,
1555 le16_to_cpu(rsp->SecurityBufferLength), ses);
1559 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1561 spin_lock(&ses->chan_lock);
1562 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1563 spin_unlock(&ses->chan_lock);
1565 /* keep existing ses id and flags if binding */
1567 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1568 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1572 kfree(ntlmssp_blob);
1573 SMB2_sess_free_buffer(sess_data);
1575 sess_data->result = 0;
1576 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1580 kfree(ses->ntlmssp);
1581 ses->ntlmssp = NULL;
1582 sess_data->result = rc;
1583 sess_data->func = NULL;
1587 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1590 struct cifs_ses *ses = sess_data->ses;
1591 struct TCP_Server_Info *server = sess_data->server;
1592 struct smb2_sess_setup_req *req;
1593 struct smb2_sess_setup_rsp *rsp = NULL;
1594 unsigned char *ntlmssp_blob = NULL;
1595 bool use_spnego = false; /* else use raw ntlmssp */
1596 u16 blob_length = 0;
1597 bool is_binding = false;
1599 rc = SMB2_sess_alloc_buffer(sess_data);
1603 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1604 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1606 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1610 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1615 /* BB eventually need to add this */
1616 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1620 sess_data->iov[1].iov_base = ntlmssp_blob;
1621 sess_data->iov[1].iov_len = blob_length;
1623 rc = SMB2_sess_sendreceive(sess_data);
1627 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1629 spin_lock(&ses->chan_lock);
1630 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
1631 spin_unlock(&ses->chan_lock);
1633 /* keep existing ses id and flags if binding */
1635 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1636 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1639 rc = SMB2_sess_establish_session(sess_data);
1640 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1641 if (ses->server->dialect < SMB30_PROT_ID) {
1642 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1644 * The session id is opaque in terms of endianness, so we can't
1645 * print it as a long long. we dump it as we got it on the wire
1647 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1649 cifs_dbg(VFS, "Session Key %*ph\n",
1650 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1651 cifs_dbg(VFS, "Signing Key %*ph\n",
1652 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1656 kfree(ntlmssp_blob);
1657 SMB2_sess_free_buffer(sess_data);
1658 kfree(ses->ntlmssp);
1659 ses->ntlmssp = NULL;
1660 sess_data->result = rc;
1661 sess_data->func = NULL;
1665 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1668 struct cifs_ses *ses = sess_data->ses;
1669 struct TCP_Server_Info *server = sess_data->server;
1671 type = smb2_select_sectype(server, ses->sectype);
1672 cifs_dbg(FYI, "sess setup type %d\n", type);
1673 if (type == Unspecified) {
1674 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1680 sess_data->func = SMB2_auth_kerberos;
1683 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1686 cifs_dbg(VFS, "secType %d not supported!\n", type);
1694 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1695 struct TCP_Server_Info *server,
1696 const struct nls_table *nls_cp)
1699 struct SMB2_sess_data *sess_data;
1701 cifs_dbg(FYI, "Session Setup\n");
1704 WARN(1, "%s: server is NULL!\n", __func__);
1708 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1712 sess_data->xid = xid;
1713 sess_data->ses = ses;
1714 sess_data->server = server;
1715 sess_data->buf0_type = CIFS_NO_BUFFER;
1716 sess_data->nls_cp = (struct nls_table *) nls_cp;
1717 sess_data->previous_session = ses->Suid;
1719 rc = SMB2_select_sec(sess_data);
1724 * Initialize the session hash with the server one.
1726 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1727 SMB2_PREAUTH_HASH_SIZE);
1729 while (sess_data->func)
1730 sess_data->func(sess_data);
1732 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1733 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1734 rc = sess_data->result;
1741 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1743 struct smb_rqst rqst;
1744 struct smb2_logoff_req *req; /* response is also trivial struct */
1746 struct TCP_Server_Info *server;
1748 unsigned int total_len;
1750 struct kvec rsp_iov;
1753 cifs_dbg(FYI, "disconnect session %p\n", ses);
1755 if (ses && (ses->server))
1756 server = ses->server;
1760 /* no need to send SMB logoff if uid already closed due to reconnect */
1761 spin_lock(&ses->chan_lock);
1762 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1763 spin_unlock(&ses->chan_lock);
1764 goto smb2_session_already_dead;
1766 spin_unlock(&ses->chan_lock);
1768 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1769 (void **) &req, &total_len);
1773 /* since no tcon, smb2_init can not do this, so do here */
1774 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1776 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1777 flags |= CIFS_TRANSFORM_REQ;
1778 else if (server->sign)
1779 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1781 flags |= CIFS_NO_RSP_BUF;
1783 iov[0].iov_base = (char *)req;
1784 iov[0].iov_len = total_len;
1786 memset(&rqst, 0, sizeof(struct smb_rqst));
1790 rc = cifs_send_recv(xid, ses, ses->server,
1791 &rqst, &resp_buf_type, flags, &rsp_iov);
1792 cifs_small_buf_release(req);
1794 * No tcon so can't do
1795 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1798 smb2_session_already_dead:
1802 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1804 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1807 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1809 /* These are similar values to what Windows uses */
1810 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1812 tcon->max_chunks = 256;
1813 tcon->max_bytes_chunk = 1048576;
1814 tcon->max_bytes_copy = 16777216;
1818 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1819 struct cifs_tcon *tcon, const struct nls_table *cp)
1821 struct smb_rqst rqst;
1822 struct smb2_tree_connect_req *req;
1823 struct smb2_tree_connect_rsp *rsp = NULL;
1825 struct kvec rsp_iov = { NULL, 0 };
1829 __le16 *unc_path = NULL;
1831 unsigned int total_len;
1832 struct TCP_Server_Info *server;
1834 /* always use master channel */
1835 server = ses->server;
1837 cifs_dbg(FYI, "TCON\n");
1839 if (!server || !tree)
1842 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1843 if (unc_path == NULL)
1846 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1848 if (unc_path_len < 2) {
1853 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1855 atomic_set(&tcon->num_remote_opens, 0);
1856 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1857 (void **) &req, &total_len);
1863 if (smb3_encryption_required(tcon))
1864 flags |= CIFS_TRANSFORM_REQ;
1866 iov[0].iov_base = (char *)req;
1868 iov[0].iov_len = total_len - 1;
1870 /* Testing shows that buffer offset must be at location of Buffer[0] */
1871 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1873 req->PathLength = cpu_to_le16(unc_path_len - 2);
1874 iov[1].iov_base = unc_path;
1875 iov[1].iov_len = unc_path_len;
1878 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1879 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1880 * (Samba servers don't always set the flag so also check if null user)
1882 if ((server->dialect == SMB311_PROT_ID) &&
1883 !smb3_encryption_required(tcon) &&
1884 !(ses->session_flags &
1885 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1886 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1887 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1889 memset(&rqst, 0, sizeof(struct smb_rqst));
1893 /* Need 64 for max size write so ask for more in case not there yet */
1894 req->hdr.CreditRequest = cpu_to_le16(64);
1896 rc = cifs_send_recv(xid, ses, server,
1897 &rqst, &resp_buftype, flags, &rsp_iov);
1898 cifs_small_buf_release(req);
1899 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1900 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1901 if ((rc != 0) || (rsp == NULL)) {
1902 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1903 tcon->need_reconnect = true;
1904 goto tcon_error_exit;
1907 switch (rsp->ShareType) {
1908 case SMB2_SHARE_TYPE_DISK:
1909 cifs_dbg(FYI, "connection to disk share\n");
1911 case SMB2_SHARE_TYPE_PIPE:
1913 cifs_dbg(FYI, "connection to pipe share\n");
1915 case SMB2_SHARE_TYPE_PRINT:
1917 cifs_dbg(FYI, "connection to printer\n");
1920 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1922 goto tcon_error_exit;
1925 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1926 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1927 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1928 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
1929 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1931 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1932 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1933 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1936 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1937 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1939 init_copy_chunk_defaults(tcon);
1940 if (server->ops->validate_negotiate)
1941 rc = server->ops->validate_negotiate(xid, tcon);
1944 free_rsp_buf(resp_buftype, rsp);
1949 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
1950 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1955 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1957 struct smb_rqst rqst;
1958 struct smb2_tree_disconnect_req *req; /* response is trivial */
1960 struct cifs_ses *ses = tcon->ses;
1962 unsigned int total_len;
1964 struct kvec rsp_iov;
1967 cifs_dbg(FYI, "Tree Disconnect\n");
1969 if (!ses || !(ses->server))
1972 spin_lock(&ses->chan_lock);
1973 if ((tcon->need_reconnect) ||
1974 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
1975 spin_unlock(&ses->chan_lock);
1978 spin_unlock(&ses->chan_lock);
1980 close_cached_dir_lease(&tcon->crfid);
1982 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1988 if (smb3_encryption_required(tcon))
1989 flags |= CIFS_TRANSFORM_REQ;
1991 flags |= CIFS_NO_RSP_BUF;
1993 iov[0].iov_base = (char *)req;
1994 iov[0].iov_len = total_len;
1996 memset(&rqst, 0, sizeof(struct smb_rqst));
2000 rc = cifs_send_recv(xid, ses, ses->server,
2001 &rqst, &resp_buf_type, flags, &rsp_iov);
2002 cifs_small_buf_release(req);
2004 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2010 static struct create_durable *
2011 create_durable_buf(void)
2013 struct create_durable *buf;
2015 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2019 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2020 (struct create_durable, Data));
2021 buf->ccontext.DataLength = cpu_to_le32(16);
2022 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2023 (struct create_durable, Name));
2024 buf->ccontext.NameLength = cpu_to_le16(4);
2025 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2033 static struct create_durable *
2034 create_reconnect_durable_buf(struct cifs_fid *fid)
2036 struct create_durable *buf;
2038 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2042 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2043 (struct create_durable, Data));
2044 buf->ccontext.DataLength = cpu_to_le32(16);
2045 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2046 (struct create_durable, Name));
2047 buf->ccontext.NameLength = cpu_to_le16(4);
2048 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2049 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2050 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2059 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2061 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2063 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2064 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2065 buf->IndexNumber = pdisk_id->DiskFileId;
2069 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2070 struct create_posix_rsp *posix)
2073 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2074 u8 *end = beg + le32_to_cpu(cc->DataLength);
2077 memset(posix, 0, sizeof(*posix));
2079 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2080 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2081 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2084 sid_len = posix_info_sid_size(sid, end);
2086 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2089 memcpy(&posix->owner, sid, sid_len);
2091 sid = sid + sid_len;
2092 sid_len = posix_info_sid_size(sid, end);
2094 cifs_dbg(VFS, "bad group sid in posix create response\n");
2097 memcpy(&posix->group, sid, sid_len);
2099 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2100 posix->nlink, posix->mode, posix->reparse_tag);
2104 smb2_parse_contexts(struct TCP_Server_Info *server,
2105 struct smb2_create_rsp *rsp,
2106 unsigned int *epoch, char *lease_key, __u8 *oplock,
2107 struct smb2_file_all_info *buf,
2108 struct create_posix_rsp *posix)
2111 struct create_context *cc;
2113 unsigned int remaining;
2115 static const char smb3_create_tag_posix[] = {
2116 0x93, 0xAD, 0x25, 0x50, 0x9C,
2117 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2118 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2122 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2123 remaining = le32_to_cpu(rsp->CreateContextsLength);
2124 cc = (struct create_context *)data_offset;
2126 /* Initialize inode number to 0 in case no valid data in qfid context */
2128 buf->IndexNumber = 0;
2130 while (remaining >= sizeof(struct create_context)) {
2131 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2132 if (le16_to_cpu(cc->NameLength) == 4 &&
2133 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2134 *oplock = server->ops->parse_lease_buf(cc, epoch,
2136 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2137 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2138 parse_query_id_ctxt(cc, buf);
2139 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2141 memcmp(name, smb3_create_tag_posix, 16) == 0)
2142 parse_posix_ctxt(cc, buf, posix);
2145 cifs_dbg(FYI, "Context not matched with len %d\n",
2146 le16_to_cpu(cc->NameLength));
2147 cifs_dump_mem("Cctxt name: ", name, 4);
2150 next = le32_to_cpu(cc->Next);
2154 cc = (struct create_context *)((char *)cc + next);
2157 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2158 *oplock = rsp->OplockLevel;
2164 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2165 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2167 struct smb2_create_req *req = iov[0].iov_base;
2168 unsigned int num = *num_iovec;
2170 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2171 if (iov[num].iov_base == NULL)
2173 iov[num].iov_len = server->vals->create_lease_size;
2174 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2175 if (!req->CreateContextsOffset)
2176 req->CreateContextsOffset = cpu_to_le32(
2177 sizeof(struct smb2_create_req) +
2178 iov[num - 1].iov_len);
2179 le32_add_cpu(&req->CreateContextsLength,
2180 server->vals->create_lease_size);
2181 *num_iovec = num + 1;
2185 static struct create_durable_v2 *
2186 create_durable_v2_buf(struct cifs_open_parms *oparms)
2188 struct cifs_fid *pfid = oparms->fid;
2189 struct create_durable_v2 *buf;
2191 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2195 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2196 (struct create_durable_v2, dcontext));
2197 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2198 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2199 (struct create_durable_v2, Name));
2200 buf->ccontext.NameLength = cpu_to_le16(4);
2203 * NB: Handle timeout defaults to 0, which allows server to choose
2204 * (most servers default to 120 seconds) and most clients default to 0.
2205 * This can be overridden at mount ("handletimeout=") if the user wants
2206 * a different persistent (or resilient) handle timeout for all opens
2207 * opens on a particular SMB3 mount.
2209 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2210 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2211 generate_random_uuid(buf->dcontext.CreateGuid);
2212 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2214 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2222 static struct create_durable_handle_reconnect_v2 *
2223 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2225 struct create_durable_handle_reconnect_v2 *buf;
2227 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2232 buf->ccontext.DataOffset =
2233 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2235 buf->ccontext.DataLength =
2236 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2237 buf->ccontext.NameOffset =
2238 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2240 buf->ccontext.NameLength = cpu_to_le16(4);
2242 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2243 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2244 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2245 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2247 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2256 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2257 struct cifs_open_parms *oparms)
2259 struct smb2_create_req *req = iov[0].iov_base;
2260 unsigned int num = *num_iovec;
2262 iov[num].iov_base = create_durable_v2_buf(oparms);
2263 if (iov[num].iov_base == NULL)
2265 iov[num].iov_len = sizeof(struct create_durable_v2);
2266 if (!req->CreateContextsOffset)
2267 req->CreateContextsOffset =
2268 cpu_to_le32(sizeof(struct smb2_create_req) +
2270 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2271 *num_iovec = num + 1;
2276 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2277 struct cifs_open_parms *oparms)
2279 struct smb2_create_req *req = iov[0].iov_base;
2280 unsigned int num = *num_iovec;
2282 /* indicate that we don't need to relock the file */
2283 oparms->reconnect = false;
2285 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2286 if (iov[num].iov_base == NULL)
2288 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2289 if (!req->CreateContextsOffset)
2290 req->CreateContextsOffset =
2291 cpu_to_le32(sizeof(struct smb2_create_req) +
2293 le32_add_cpu(&req->CreateContextsLength,
2294 sizeof(struct create_durable_handle_reconnect_v2));
2295 *num_iovec = num + 1;
2300 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2301 struct cifs_open_parms *oparms, bool use_persistent)
2303 struct smb2_create_req *req = iov[0].iov_base;
2304 unsigned int num = *num_iovec;
2306 if (use_persistent) {
2307 if (oparms->reconnect)
2308 return add_durable_reconnect_v2_context(iov, num_iovec,
2311 return add_durable_v2_context(iov, num_iovec, oparms);
2314 if (oparms->reconnect) {
2315 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2316 /* indicate that we don't need to relock the file */
2317 oparms->reconnect = false;
2319 iov[num].iov_base = create_durable_buf();
2320 if (iov[num].iov_base == NULL)
2322 iov[num].iov_len = sizeof(struct create_durable);
2323 if (!req->CreateContextsOffset)
2324 req->CreateContextsOffset =
2325 cpu_to_le32(sizeof(struct smb2_create_req) +
2327 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2328 *num_iovec = num + 1;
2332 /* See MS-SMB2 2.2.13.2.7 */
2333 static struct crt_twarp_ctxt *
2334 create_twarp_buf(__u64 timewarp)
2336 struct crt_twarp_ctxt *buf;
2338 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2342 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2343 (struct crt_twarp_ctxt, Timestamp));
2344 buf->ccontext.DataLength = cpu_to_le32(8);
2345 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2346 (struct crt_twarp_ctxt, Name));
2347 buf->ccontext.NameLength = cpu_to_le16(4);
2348 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2353 buf->Timestamp = cpu_to_le64(timewarp);
2357 /* See MS-SMB2 2.2.13.2.7 */
2359 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2361 struct smb2_create_req *req = iov[0].iov_base;
2362 unsigned int num = *num_iovec;
2364 iov[num].iov_base = create_twarp_buf(timewarp);
2365 if (iov[num].iov_base == NULL)
2367 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2368 if (!req->CreateContextsOffset)
2369 req->CreateContextsOffset = cpu_to_le32(
2370 sizeof(struct smb2_create_req) +
2371 iov[num - 1].iov_len);
2372 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2373 *num_iovec = num + 1;
2377 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2378 static void setup_owner_group_sids(char *buf)
2380 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2382 /* Populate the user ownership fields S-1-5-88-1 */
2383 sids->owner.Revision = 1;
2384 sids->owner.NumAuth = 3;
2385 sids->owner.Authority[5] = 5;
2386 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2387 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2388 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2390 /* Populate the group ownership fields S-1-5-88-2 */
2391 sids->group.Revision = 1;
2392 sids->group.NumAuth = 3;
2393 sids->group.Authority[5] = 5;
2394 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2395 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2396 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2398 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2401 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2402 static struct crt_sd_ctxt *
2403 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2405 struct crt_sd_ctxt *buf;
2407 unsigned int acelen, acl_size, ace_count;
2408 unsigned int owner_offset = 0;
2409 unsigned int group_offset = 0;
2410 struct smb3_acl acl;
2412 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2415 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2416 *len += sizeof(struct owner_group_sids);
2419 buf = kzalloc(*len, GFP_KERNEL);
2423 ptr = (__u8 *)&buf[1];
2425 /* offset fields are from beginning of security descriptor not of create context */
2426 owner_offset = ptr - (__u8 *)&buf->sd;
2427 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2428 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2429 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2431 setup_owner_group_sids(ptr);
2432 ptr += sizeof(struct owner_group_sids);
2434 buf->sd.OffsetOwner = 0;
2435 buf->sd.OffsetGroup = 0;
2438 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2439 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2440 buf->ccontext.NameLength = cpu_to_le16(4);
2441 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2446 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2449 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2450 * and "DP" ie the DACL is present
2452 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2454 /* offset owner, group and Sbz1 and SACL are all zero */
2455 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2456 /* Ship the ACL for now. we will copy it into buf later. */
2458 ptr += sizeof(struct smb3_acl);
2460 /* create one ACE to hold the mode embedded in reserved special SID */
2461 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2463 acl_size = acelen + sizeof(struct smb3_acl);
2467 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2468 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2474 /* and one more ACE to allow access for authenticated users */
2475 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2480 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2481 acl.AclSize = cpu_to_le16(acl_size);
2482 acl.AceCount = cpu_to_le16(ace_count);
2483 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2485 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2486 *len = roundup(ptr - (__u8 *)buf, 8);
2492 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2494 struct smb2_create_req *req = iov[0].iov_base;
2495 unsigned int num = *num_iovec;
2496 unsigned int len = 0;
2498 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2499 if (iov[num].iov_base == NULL)
2501 iov[num].iov_len = len;
2502 if (!req->CreateContextsOffset)
2503 req->CreateContextsOffset = cpu_to_le32(
2504 sizeof(struct smb2_create_req) +
2505 iov[num - 1].iov_len);
2506 le32_add_cpu(&req->CreateContextsLength, len);
2507 *num_iovec = num + 1;
2511 static struct crt_query_id_ctxt *
2512 create_query_id_buf(void)
2514 struct crt_query_id_ctxt *buf;
2516 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2520 buf->ccontext.DataOffset = cpu_to_le16(0);
2521 buf->ccontext.DataLength = cpu_to_le32(0);
2522 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2523 (struct crt_query_id_ctxt, Name));
2524 buf->ccontext.NameLength = cpu_to_le16(4);
2525 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2533 /* See MS-SMB2 2.2.13.2.9 */
2535 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2537 struct smb2_create_req *req = iov[0].iov_base;
2538 unsigned int num = *num_iovec;
2540 iov[num].iov_base = create_query_id_buf();
2541 if (iov[num].iov_base == NULL)
2543 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2544 if (!req->CreateContextsOffset)
2545 req->CreateContextsOffset = cpu_to_le32(
2546 sizeof(struct smb2_create_req) +
2547 iov[num - 1].iov_len);
2548 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2549 *num_iovec = num + 1;
2554 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2555 const char *treename, const __le16 *path)
2557 int treename_len, path_len;
2558 struct nls_table *cp;
2559 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2564 treename_len = strlen(treename);
2565 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2571 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2574 * make room for one path separator between the treename and
2577 *out_len = treename_len + 1 + path_len;
2580 * final path needs to be null-terminated UTF16 with a
2584 *out_size = roundup((*out_len+1)*2, 8);
2585 *out_path = kzalloc(*out_size, GFP_KERNEL);
2589 cp = load_nls_default();
2590 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2592 /* Do not append the separator if the path is empty */
2593 if (path[0] != cpu_to_le16(0x0000)) {
2594 UniStrcat(*out_path, sep);
2595 UniStrcat(*out_path, path);
2603 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2604 umode_t mode, struct cifs_tcon *tcon,
2605 const char *full_path,
2606 struct cifs_sb_info *cifs_sb)
2608 struct smb_rqst rqst;
2609 struct smb2_create_req *req;
2610 struct smb2_create_rsp *rsp = NULL;
2611 struct cifs_ses *ses = tcon->ses;
2612 struct kvec iov[3]; /* make sure at least one for each open context */
2613 struct kvec rsp_iov = {NULL, 0};
2616 __le16 *copy_path = NULL;
2619 unsigned int n_iov = 2;
2620 __u32 file_attributes = 0;
2621 char *pc_buf = NULL;
2623 unsigned int total_len;
2624 __le16 *utf16_path = NULL;
2625 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2627 cifs_dbg(FYI, "mkdir\n");
2629 /* resource #1: path allocation */
2630 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2634 if (!ses || !server) {
2639 /* resource #2: request */
2640 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2641 (void **) &req, &total_len);
2646 if (smb3_encryption_required(tcon))
2647 flags |= CIFS_TRANSFORM_REQ;
2649 req->ImpersonationLevel = IL_IMPERSONATION;
2650 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2651 /* File attributes ignored on open (used in create though) */
2652 req->FileAttributes = cpu_to_le32(file_attributes);
2653 req->ShareAccess = FILE_SHARE_ALL_LE;
2654 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2655 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2657 iov[0].iov_base = (char *)req;
2658 /* -1 since last byte is buf[0] which is sent below (path) */
2659 iov[0].iov_len = total_len - 1;
2661 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2663 /* [MS-SMB2] 2.2.13 NameOffset:
2664 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2665 * the SMB2 header, the file name includes a prefix that will
2666 * be processed during DFS name normalization as specified in
2667 * section 3.3.5.9. Otherwise, the file name is relative to
2668 * the share that is identified by the TreeId in the SMB2
2671 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2674 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2675 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2677 tcon->treeName, utf16_path);
2681 req->NameLength = cpu_to_le16(name_len * 2);
2682 uni_path_len = copy_size;
2683 /* free before overwriting resource */
2685 utf16_path = copy_path;
2687 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2688 /* MUST set path len (NameLength) to 0 opening root of share */
2689 req->NameLength = cpu_to_le16(uni_path_len - 2);
2690 if (uni_path_len % 8 != 0) {
2691 copy_size = roundup(uni_path_len, 8);
2692 copy_path = kzalloc(copy_size, GFP_KERNEL);
2697 memcpy((char *)copy_path, (const char *)utf16_path,
2699 uni_path_len = copy_size;
2700 /* free before overwriting resource */
2702 utf16_path = copy_path;
2706 iov[1].iov_len = uni_path_len;
2707 iov[1].iov_base = utf16_path;
2708 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2710 if (tcon->posix_extensions) {
2711 /* resource #3: posix buf */
2712 rc = add_posix_context(iov, &n_iov, mode);
2715 pc_buf = iov[n_iov-1].iov_base;
2719 memset(&rqst, 0, sizeof(struct smb_rqst));
2721 rqst.rq_nvec = n_iov;
2723 /* no need to inc num_remote_opens because we close it just below */
2724 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2725 FILE_WRITE_ATTRIBUTES);
2726 /* resource #4: response buffer */
2727 rc = cifs_send_recv(xid, ses, server,
2728 &rqst, &resp_buftype, flags, &rsp_iov);
2730 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2731 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2733 FILE_WRITE_ATTRIBUTES, rc);
2734 goto err_free_rsp_buf;
2738 * Although unlikely to be possible for rsp to be null and rc not set,
2739 * adding check below is slightly safer long term (and quiets Coverity
2742 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2749 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2750 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2752 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2754 /* Eventually save off posix specific response info and timestaps */
2757 free_rsp_buf(resp_buftype, rsp);
2760 cifs_small_buf_release(req);
2767 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2768 struct smb_rqst *rqst, __u8 *oplock,
2769 struct cifs_open_parms *oparms, __le16 *path)
2771 struct smb2_create_req *req;
2772 unsigned int n_iov = 2;
2773 __u32 file_attributes = 0;
2776 unsigned int total_len;
2777 struct kvec *iov = rqst->rq_iov;
2781 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2782 (void **) &req, &total_len);
2786 iov[0].iov_base = (char *)req;
2787 /* -1 since last byte is buf[0] which is sent below (path) */
2788 iov[0].iov_len = total_len - 1;
2790 if (oparms->create_options & CREATE_OPTION_READONLY)
2791 file_attributes |= ATTR_READONLY;
2792 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2793 file_attributes |= ATTR_SYSTEM;
2795 req->ImpersonationLevel = IL_IMPERSONATION;
2796 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2797 /* File attributes ignored on open (used in create though) */
2798 req->FileAttributes = cpu_to_le32(file_attributes);
2799 req->ShareAccess = FILE_SHARE_ALL_LE;
2801 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2802 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2803 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2805 /* [MS-SMB2] 2.2.13 NameOffset:
2806 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2807 * the SMB2 header, the file name includes a prefix that will
2808 * be processed during DFS name normalization as specified in
2809 * section 3.3.5.9. Otherwise, the file name is relative to
2810 * the share that is identified by the TreeId in the SMB2
2813 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2816 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2817 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2819 tcon->treeName, path);
2822 req->NameLength = cpu_to_le16(name_len * 2);
2823 uni_path_len = copy_size;
2826 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2827 /* MUST set path len (NameLength) to 0 opening root of share */
2828 req->NameLength = cpu_to_le16(uni_path_len - 2);
2829 copy_size = uni_path_len;
2830 if (copy_size % 8 != 0)
2831 copy_size = roundup(copy_size, 8);
2832 copy_path = kzalloc(copy_size, GFP_KERNEL);
2835 memcpy((char *)copy_path, (const char *)path,
2837 uni_path_len = copy_size;
2841 iov[1].iov_len = uni_path_len;
2842 iov[1].iov_base = path;
2844 if ((!server->oplocks) || (tcon->no_lease))
2845 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2847 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2848 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2849 req->RequestedOplockLevel = *oplock;
2850 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2851 (oparms->create_options & CREATE_NOT_FILE))
2852 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2854 rc = add_lease_context(server, iov, &n_iov,
2855 oparms->fid->lease_key, oplock);
2860 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2861 /* need to set Next field of lease context if we request it */
2862 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2863 struct create_context *ccontext =
2864 (struct create_context *)iov[n_iov-1].iov_base;
2866 cpu_to_le32(server->vals->create_lease_size);
2869 rc = add_durable_context(iov, &n_iov, oparms,
2870 tcon->use_persistent);
2875 if (tcon->posix_extensions) {
2877 struct create_context *ccontext =
2878 (struct create_context *)iov[n_iov-1].iov_base;
2880 cpu_to_le32(iov[n_iov-1].iov_len);
2883 rc = add_posix_context(iov, &n_iov, oparms->mode);
2888 if (tcon->snapshot_time) {
2889 cifs_dbg(FYI, "adding snapshot context\n");
2891 struct create_context *ccontext =
2892 (struct create_context *)iov[n_iov-1].iov_base;
2894 cpu_to_le32(iov[n_iov-1].iov_len);
2897 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2902 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2906 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2907 (oparms->mode != ACL_NO_MODE))
2911 oparms->mode = ACL_NO_MODE;
2914 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2919 if (set_owner | set_mode) {
2921 struct create_context *ccontext =
2922 (struct create_context *)iov[n_iov-1].iov_base;
2923 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2926 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2927 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2934 struct create_context *ccontext =
2935 (struct create_context *)iov[n_iov-1].iov_base;
2936 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2938 add_query_id_context(iov, &n_iov);
2940 rqst->rq_nvec = n_iov;
2944 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2945 * All other vectors are freed by kfree().
2948 SMB2_open_free(struct smb_rqst *rqst)
2952 if (rqst && rqst->rq_iov) {
2953 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2954 for (i = 1; i < rqst->rq_nvec; i++)
2955 if (rqst->rq_iov[i].iov_base != smb2_padding)
2956 kfree(rqst->rq_iov[i].iov_base);
2961 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2962 __u8 *oplock, struct smb2_file_all_info *buf,
2963 struct create_posix_rsp *posix,
2964 struct kvec *err_iov, int *buftype)
2966 struct smb_rqst rqst;
2967 struct smb2_create_rsp *rsp = NULL;
2968 struct cifs_tcon *tcon = oparms->tcon;
2969 struct cifs_ses *ses = tcon->ses;
2970 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2971 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2972 struct kvec rsp_iov = {NULL, 0};
2973 int resp_buftype = CIFS_NO_BUFFER;
2977 cifs_dbg(FYI, "create/open\n");
2978 if (!ses || !server)
2981 if (smb3_encryption_required(tcon))
2982 flags |= CIFS_TRANSFORM_REQ;
2984 memset(&rqst, 0, sizeof(struct smb_rqst));
2985 memset(&iov, 0, sizeof(iov));
2987 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2989 rc = SMB2_open_init(tcon, server,
2990 &rqst, oplock, oparms, path);
2994 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2995 oparms->create_options, oparms->desired_access);
2997 rc = cifs_send_recv(xid, ses, server,
2998 &rqst, &resp_buftype, flags,
3000 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3003 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3004 if (err_iov && rsp) {
3006 *buftype = resp_buftype;
3007 resp_buftype = CIFS_NO_BUFFER;
3010 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3011 oparms->create_options, oparms->desired_access, rc);
3012 if (rc == -EREMCHG) {
3013 pr_warn_once("server share %s deleted\n",
3015 tcon->need_reconnect = true;
3018 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3021 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3022 oparms->create_options, oparms->desired_access);
3024 atomic_inc(&tcon->num_remote_opens);
3025 oparms->fid->persistent_fid = rsp->PersistentFileId;
3026 oparms->fid->volatile_fid = rsp->VolatileFileId;
3027 oparms->fid->access = oparms->desired_access;
3028 #ifdef CONFIG_CIFS_DEBUG2
3029 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3030 #endif /* CIFS_DEBUG2 */
3033 buf->CreationTime = rsp->CreationTime;
3034 buf->LastAccessTime = rsp->LastAccessTime;
3035 buf->LastWriteTime = rsp->LastWriteTime;
3036 buf->ChangeTime = rsp->ChangeTime;
3037 buf->AllocationSize = rsp->AllocationSize;
3038 buf->EndOfFile = rsp->EndofFile;
3039 buf->Attributes = rsp->FileAttributes;
3040 buf->NumberOfLinks = cpu_to_le32(1);
3041 buf->DeletePending = 0;
3045 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
3046 oparms->fid->lease_key, oplock, buf, posix);
3048 SMB2_open_free(&rqst);
3049 free_rsp_buf(resp_buftype, rsp);
3054 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3055 struct smb_rqst *rqst,
3056 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3057 bool is_fsctl, char *in_data, u32 indatalen,
3058 __u32 max_response_size)
3060 struct smb2_ioctl_req *req;
3061 struct kvec *iov = rqst->rq_iov;
3062 unsigned int total_len;
3066 rc = smb2_ioctl_req_init(opcode, tcon, server,
3067 (void **) &req, &total_len);
3073 * indatalen is usually small at a couple of bytes max, so
3074 * just allocate through generic pool
3076 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3078 cifs_small_buf_release(req);
3083 req->CtlCode = cpu_to_le32(opcode);
3084 req->PersistentFileId = persistent_fid;
3085 req->VolatileFileId = volatile_fid;
3087 iov[0].iov_base = (char *)req;
3089 * If no input data, the size of ioctl struct in
3090 * protocol spec still includes a 1 byte data buffer,
3091 * but if input data passed to ioctl, we do not
3092 * want to double count this, so we do not send
3093 * the dummy one byte of data in iovec[0] if sending
3094 * input data (in iovec[1]).
3097 req->InputCount = cpu_to_le32(indatalen);
3098 /* do not set InputOffset if no input data */
3100 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3102 iov[0].iov_len = total_len - 1;
3103 iov[1].iov_base = in_data_buf;
3104 iov[1].iov_len = indatalen;
3107 iov[0].iov_len = total_len;
3110 req->OutputOffset = 0;
3111 req->OutputCount = 0; /* MBZ */
3114 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3115 * We Could increase default MaxOutputResponse, but that could require
3116 * more credits. Windows typically sets this smaller, but for some
3117 * ioctls it may be useful to allow server to send more. No point
3118 * limiting what the server can send as long as fits in one credit
3119 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3120 * to increase this limit up in the future.
3121 * Note that for snapshot queries that servers like Azure expect that
3122 * the first query be minimal size (and just used to get the number/size
3123 * of previous versions) so response size must be specified as EXACTLY
3124 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3125 * of eight bytes. Currently that is the only case where we set max
3126 * response size smaller.
3128 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3129 req->hdr.CreditCharge =
3130 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3131 SMB2_MAX_BUFFER_SIZE));
3133 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3137 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3138 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3139 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3145 SMB2_ioctl_free(struct smb_rqst *rqst)
3148 if (rqst && rqst->rq_iov) {
3149 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3150 for (i = 1; i < rqst->rq_nvec; i++)
3151 if (rqst->rq_iov[i].iov_base != smb2_padding)
3152 kfree(rqst->rq_iov[i].iov_base);
3158 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3161 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3162 u64 volatile_fid, u32 opcode, bool is_fsctl,
3163 char *in_data, u32 indatalen, u32 max_out_data_len,
3164 char **out_data, u32 *plen /* returned data len */)
3166 struct smb_rqst rqst;
3167 struct smb2_ioctl_rsp *rsp = NULL;
3168 struct cifs_ses *ses;
3169 struct TCP_Server_Info *server;
3170 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3171 struct kvec rsp_iov = {NULL, 0};
3172 int resp_buftype = CIFS_NO_BUFFER;
3176 cifs_dbg(FYI, "SMB2 IOCTL\n");
3178 if (out_data != NULL)
3181 /* zero out returned data len, in case of error */
3192 server = cifs_pick_channel(ses);
3196 if (smb3_encryption_required(tcon))
3197 flags |= CIFS_TRANSFORM_REQ;
3199 memset(&rqst, 0, sizeof(struct smb_rqst));
3200 memset(&iov, 0, sizeof(iov));
3202 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3204 rc = SMB2_ioctl_init(tcon, server,
3205 &rqst, persistent_fid, volatile_fid, opcode,
3206 is_fsctl, in_data, indatalen, max_out_data_len);
3210 rc = cifs_send_recv(xid, ses, server,
3211 &rqst, &resp_buftype, flags,
3213 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3216 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3217 ses->Suid, 0, opcode, rc);
3219 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3220 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3222 } else if (rc == -EINVAL) {
3223 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3224 (opcode != FSCTL_SRV_COPYCHUNK)) {
3225 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3228 } else if (rc == -E2BIG) {
3229 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3230 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3235 /* check if caller wants to look at return data or just return rc */
3236 if ((plen == NULL) || (out_data == NULL))
3240 * Although unlikely to be possible for rsp to be null and rc not set,
3241 * adding check below is slightly safer long term (and quiets Coverity
3249 *plen = le32_to_cpu(rsp->OutputCount);
3251 /* We check for obvious errors in the output buffer length and offset */
3253 goto ioctl_exit; /* server returned no data */
3254 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3255 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3261 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3262 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3263 le32_to_cpu(rsp->OutputOffset));
3269 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3271 if (*out_data == NULL) {
3277 SMB2_ioctl_free(&rqst);
3278 free_rsp_buf(resp_buftype, rsp);
3283 * Individual callers to ioctl worker function follow
3287 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3288 u64 persistent_fid, u64 volatile_fid)
3291 struct compress_ioctl fsctl_input;
3292 char *ret_data = NULL;
3294 fsctl_input.CompressionState =
3295 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3297 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3298 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3299 (char *)&fsctl_input /* data input */,
3300 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3301 &ret_data /* out data */, NULL);
3303 cifs_dbg(FYI, "set compression rc %d\n", rc);
3309 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3310 struct smb_rqst *rqst,
3311 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3313 struct smb2_close_req *req;
3314 struct kvec *iov = rqst->rq_iov;
3315 unsigned int total_len;
3318 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3319 (void **) &req, &total_len);
3323 req->PersistentFileId = persistent_fid;
3324 req->VolatileFileId = volatile_fid;
3326 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3329 iov[0].iov_base = (char *)req;
3330 iov[0].iov_len = total_len;
3336 SMB2_close_free(struct smb_rqst *rqst)
3338 if (rqst && rqst->rq_iov)
3339 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3343 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3344 u64 persistent_fid, u64 volatile_fid,
3345 struct smb2_file_network_open_info *pbuf)
3347 struct smb_rqst rqst;
3348 struct smb2_close_rsp *rsp = NULL;
3349 struct cifs_ses *ses = tcon->ses;
3350 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3352 struct kvec rsp_iov;
3353 int resp_buftype = CIFS_NO_BUFFER;
3356 bool query_attrs = false;
3358 cifs_dbg(FYI, "Close\n");
3360 if (!ses || !server)
3363 if (smb3_encryption_required(tcon))
3364 flags |= CIFS_TRANSFORM_REQ;
3366 memset(&rqst, 0, sizeof(struct smb_rqst));
3367 memset(&iov, 0, sizeof(iov));
3371 /* check if need to ask server to return timestamps in close response */
3375 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3376 rc = SMB2_close_init(tcon, server,
3377 &rqst, persistent_fid, volatile_fid,
3382 rc = cifs_send_recv(xid, ses, server,
3383 &rqst, &resp_buftype, flags, &rsp_iov);
3384 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3387 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3388 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3392 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3395 * Note that have to subtract 4 since struct network_open_info
3396 * has a final 4 byte pad that close response does not have
3399 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3402 atomic_dec(&tcon->num_remote_opens);
3404 SMB2_close_free(&rqst);
3405 free_rsp_buf(resp_buftype, rsp);
3407 /* retry close in a worker thread if this one is interrupted */
3408 if (is_interrupt_error(rc)) {
3411 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3414 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3415 persistent_fid, tmp_rc);
3421 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3422 u64 persistent_fid, u64 volatile_fid)
3424 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3428 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3429 struct kvec *iov, unsigned int min_buf_size)
3431 unsigned int smb_len = iov->iov_len;
3432 char *end_of_smb = smb_len + (char *)iov->iov_base;
3433 char *begin_of_buf = offset + (char *)iov->iov_base;
3434 char *end_of_buf = begin_of_buf + buffer_length;
3437 if (buffer_length < min_buf_size) {
3438 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3439 buffer_length, min_buf_size);
3443 /* check if beyond RFC1001 maximum length */
3444 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3445 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3446 buffer_length, smb_len);
3450 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3451 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3459 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3460 * Caller must free buffer.
3463 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3464 struct kvec *iov, unsigned int minbufsize,
3467 char *begin_of_buf = offset + (char *)iov->iov_base;
3473 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3477 memcpy(data, begin_of_buf, buffer_length);
3483 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3484 struct smb_rqst *rqst,
3485 u64 persistent_fid, u64 volatile_fid,
3486 u8 info_class, u8 info_type, u32 additional_info,
3487 size_t output_len, size_t input_len, void *input)
3489 struct smb2_query_info_req *req;
3490 struct kvec *iov = rqst->rq_iov;
3491 unsigned int total_len;
3494 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3495 (void **) &req, &total_len);
3499 req->InfoType = info_type;
3500 req->FileInfoClass = info_class;
3501 req->PersistentFileId = persistent_fid;
3502 req->VolatileFileId = volatile_fid;
3503 req->AdditionalInformation = cpu_to_le32(additional_info);
3505 req->OutputBufferLength = cpu_to_le32(output_len);
3507 req->InputBufferLength = cpu_to_le32(input_len);
3508 /* total_len for smb query request never close to le16 max */
3509 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3510 memcpy(req->Buffer, input, input_len);
3513 iov[0].iov_base = (char *)req;
3515 iov[0].iov_len = total_len - 1 + input_len;
3520 SMB2_query_info_free(struct smb_rqst *rqst)
3522 if (rqst && rqst->rq_iov)
3523 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3527 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3528 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3529 u32 additional_info, size_t output_len, size_t min_len, void **data,
3532 struct smb_rqst rqst;
3533 struct smb2_query_info_rsp *rsp = NULL;
3535 struct kvec rsp_iov;
3537 int resp_buftype = CIFS_NO_BUFFER;
3538 struct cifs_ses *ses = tcon->ses;
3539 struct TCP_Server_Info *server;
3541 bool allocated = false;
3543 cifs_dbg(FYI, "Query Info\n");
3547 server = cifs_pick_channel(ses);
3551 if (smb3_encryption_required(tcon))
3552 flags |= CIFS_TRANSFORM_REQ;
3554 memset(&rqst, 0, sizeof(struct smb_rqst));
3555 memset(&iov, 0, sizeof(iov));
3559 rc = SMB2_query_info_init(tcon, server,
3560 &rqst, persistent_fid, volatile_fid,
3561 info_class, info_type, additional_info,
3562 output_len, 0, NULL);
3566 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3567 ses->Suid, info_class, (__u32)info_type);
3569 rc = cifs_send_recv(xid, ses, server,
3570 &rqst, &resp_buftype, flags, &rsp_iov);
3571 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3574 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3575 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3576 ses->Suid, info_class, (__u32)info_type, rc);
3580 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3581 ses->Suid, info_class, (__u32)info_type);
3584 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3586 *data = kmalloc(*dlen, GFP_KERNEL);
3589 "Error %d allocating memory for acl\n",
3599 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3600 le32_to_cpu(rsp->OutputBufferLength),
3601 &rsp_iov, min_len, *data);
3602 if (rc && allocated) {
3609 SMB2_query_info_free(&rqst);
3610 free_rsp_buf(resp_buftype, rsp);
3614 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3615 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3617 return query_info(xid, tcon, persistent_fid, volatile_fid,
3618 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3619 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3620 sizeof(struct smb2_file_all_info), (void **)&data,
3625 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3627 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3628 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3630 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3631 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3634 return query_info(xid, tcon, persistent_fid, volatile_fid,
3635 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3636 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3637 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3642 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3643 u64 persistent_fid, u64 volatile_fid,
3644 void **data, u32 *plen, u32 extra_info)
3646 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3650 return query_info(xid, tcon, persistent_fid, volatile_fid,
3651 0, SMB2_O_INFO_SECURITY, additional_info,
3652 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3656 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3657 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3659 return query_info(xid, tcon, persistent_fid, volatile_fid,
3660 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3661 sizeof(struct smb2_file_internal_info),
3662 sizeof(struct smb2_file_internal_info),
3663 (void **)&uniqueid, NULL);
3667 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3668 * See MS-SMB2 2.2.35 and 2.2.36
3672 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3673 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3674 u64 persistent_fid, u64 volatile_fid,
3675 u32 completion_filter, bool watch_tree)
3677 struct smb2_change_notify_req *req;
3678 struct kvec *iov = rqst->rq_iov;
3679 unsigned int total_len;
3682 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3683 (void **) &req, &total_len);
3687 req->PersistentFileId = persistent_fid;
3688 req->VolatileFileId = volatile_fid;
3689 /* See note 354 of MS-SMB2, 64K max */
3690 req->OutputBufferLength =
3691 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3692 req->CompletionFilter = cpu_to_le32(completion_filter);
3694 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3698 iov[0].iov_base = (char *)req;
3699 iov[0].iov_len = total_len;
3705 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3706 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3707 u32 completion_filter)
3709 struct cifs_ses *ses = tcon->ses;
3710 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3711 struct smb_rqst rqst;
3713 struct kvec rsp_iov = {NULL, 0};
3714 int resp_buftype = CIFS_NO_BUFFER;
3718 cifs_dbg(FYI, "change notify\n");
3719 if (!ses || !server)
3722 if (smb3_encryption_required(tcon))
3723 flags |= CIFS_TRANSFORM_REQ;
3725 memset(&rqst, 0, sizeof(struct smb_rqst));
3726 memset(&iov, 0, sizeof(iov));
3730 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3731 persistent_fid, volatile_fid,
3732 completion_filter, watch_tree);
3736 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3737 (u8)watch_tree, completion_filter);
3738 rc = cifs_send_recv(xid, ses, server,
3739 &rqst, &resp_buftype, flags, &rsp_iov);
3742 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3743 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3744 (u8)watch_tree, completion_filter, rc);
3746 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3747 ses->Suid, (u8)watch_tree, completion_filter);
3751 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3752 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3759 * This is a no-op for now. We're not really interested in the reply, but
3760 * rather in the fact that the server sent one and that server->lstrp
3763 * FIXME: maybe we should consider checking that the reply matches request?
3766 smb2_echo_callback(struct mid_q_entry *mid)
3768 struct TCP_Server_Info *server = mid->callback_data;
3769 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3770 struct cifs_credits credits = { .value = 0, .instance = 0 };
3772 if (mid->mid_state == MID_RESPONSE_RECEIVED
3773 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3774 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3775 credits.instance = server->reconnect_instance;
3778 DeleteMidQEntry(mid);
3779 add_credits(server, &credits, CIFS_ECHO_OP);
3782 void smb2_reconnect_server(struct work_struct *work)
3784 struct TCP_Server_Info *server = container_of(work,
3785 struct TCP_Server_Info, reconnect.work);
3786 struct TCP_Server_Info *pserver;
3787 struct cifs_ses *ses, *ses2;
3788 struct cifs_tcon *tcon, *tcon2;
3789 struct list_head tmp_list, tmp_ses_list;
3790 bool tcon_exist = false, ses_exist = false;
3791 bool tcon_selected = false;
3793 bool resched = false;
3795 /* If server is a channel, select the primary channel */
3796 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3798 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3799 mutex_lock(&pserver->reconnect_mutex);
3801 INIT_LIST_HEAD(&tmp_list);
3802 INIT_LIST_HEAD(&tmp_ses_list);
3803 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3805 spin_lock(&cifs_tcp_ses_lock);
3806 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3808 tcon_selected = false;
3810 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3811 if (tcon->need_reconnect || tcon->need_reopen_files) {
3813 list_add_tail(&tcon->rlist, &tmp_list);
3814 tcon_selected = tcon_exist = true;
3818 * IPC has the same lifetime as its session and uses its
3821 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3822 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3823 tcon_selected = tcon_exist = true;
3827 * handle the case where channel needs to reconnect
3828 * binding session, but tcon is healthy (some other channel
3831 spin_lock(&ses->chan_lock);
3832 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3833 list_add_tail(&ses->rlist, &tmp_ses_list);
3837 spin_unlock(&ses->chan_lock);
3840 * Get the reference to server struct to be sure that the last call of
3841 * cifs_put_tcon() in the loop below won't release the server pointer.
3843 if (tcon_exist || ses_exist)
3844 server->srv_count++;
3846 spin_unlock(&cifs_tcp_ses_lock);
3848 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3849 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3851 cifs_reopen_persistent_handles(tcon);
3854 list_del_init(&tcon->rlist);
3856 cifs_put_smb_ses(tcon->ses);
3858 cifs_put_tcon(tcon);
3864 /* allocate a dummy tcon struct used for reconnect */
3865 tcon = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
3868 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3869 list_del_init(&ses->rlist);
3870 cifs_put_smb_ses(ses);
3875 tcon->status = TID_GOOD;
3876 tcon->retry = false;
3877 tcon->need_reconnect = false;
3879 /* now reconnect sessions for necessary channels */
3880 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3882 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3885 list_del_init(&ses->rlist);
3886 cifs_put_smb_ses(ses);
3891 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
3893 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3894 mutex_unlock(&pserver->reconnect_mutex);
3896 /* now we can safely release srv struct */
3897 if (tcon_exist || ses_exist)
3898 cifs_put_tcp_session(server, 1);
3902 SMB2_echo(struct TCP_Server_Info *server)
3904 struct smb2_echo_req *req;
3907 struct smb_rqst rqst = { .rq_iov = iov,
3909 unsigned int total_len;
3911 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
3913 spin_lock(&cifs_tcp_ses_lock);
3914 if (server->ops->need_neg &&
3915 server->ops->need_neg(server)) {
3916 spin_unlock(&cifs_tcp_ses_lock);
3917 /* No need to send echo on newly established connections */
3918 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3921 spin_unlock(&cifs_tcp_ses_lock);
3923 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3924 (void **)&req, &total_len);
3928 req->hdr.CreditRequest = cpu_to_le16(1);
3930 iov[0].iov_len = total_len;
3931 iov[0].iov_base = (char *)req;
3933 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3934 server, CIFS_ECHO_OP, NULL);
3936 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3938 cifs_small_buf_release(req);
3943 SMB2_flush_free(struct smb_rqst *rqst)
3945 if (rqst && rqst->rq_iov)
3946 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3950 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3951 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3952 u64 persistent_fid, u64 volatile_fid)
3954 struct smb2_flush_req *req;
3955 struct kvec *iov = rqst->rq_iov;
3956 unsigned int total_len;
3959 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3960 (void **) &req, &total_len);
3964 req->PersistentFileId = persistent_fid;
3965 req->VolatileFileId = volatile_fid;
3967 iov[0].iov_base = (char *)req;
3968 iov[0].iov_len = total_len;
3974 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3977 struct cifs_ses *ses = tcon->ses;
3978 struct smb_rqst rqst;
3980 struct kvec rsp_iov = {NULL, 0};
3981 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3982 int resp_buftype = CIFS_NO_BUFFER;
3986 cifs_dbg(FYI, "flush\n");
3987 if (!ses || !(ses->server))
3990 if (smb3_encryption_required(tcon))
3991 flags |= CIFS_TRANSFORM_REQ;
3993 memset(&rqst, 0, sizeof(struct smb_rqst));
3994 memset(&iov, 0, sizeof(iov));
3998 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3999 persistent_fid, volatile_fid);
4003 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4004 rc = cifs_send_recv(xid, ses, server,
4005 &rqst, &resp_buftype, flags, &rsp_iov);
4008 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4009 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4012 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4016 SMB2_flush_free(&rqst);
4017 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4022 * To form a chain of read requests, any read requests after the first should
4023 * have the end_of_chain boolean set to true.
4026 smb2_new_read_req(void **buf, unsigned int *total_len,
4027 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4028 unsigned int remaining_bytes, int request_type)
4031 struct smb2_read_req *req = NULL;
4032 struct smb2_hdr *shdr;
4033 struct TCP_Server_Info *server = io_parms->server;
4035 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4036 (void **) &req, total_len);
4041 return -ECONNABORTED;
4044 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4046 req->PersistentFileId = io_parms->persistent_fid;
4047 req->VolatileFileId = io_parms->volatile_fid;
4048 req->ReadChannelInfoOffset = 0; /* reserved */
4049 req->ReadChannelInfoLength = 0; /* reserved */
4050 req->Channel = 0; /* reserved */
4051 req->MinimumCount = 0;
4052 req->Length = cpu_to_le32(io_parms->length);
4053 req->Offset = cpu_to_le64(io_parms->offset);
4055 trace_smb3_read_enter(0 /* xid */,
4056 io_parms->persistent_fid,
4057 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4058 io_parms->offset, io_parms->length);
4059 #ifdef CONFIG_CIFS_SMB_DIRECT
4061 * If we want to do a RDMA write, fill in and append
4062 * smbd_buffer_descriptor_v1 to the end of read request
4064 if (server->rdma && rdata && !server->sign &&
4065 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
4067 struct smbd_buffer_descriptor_v1 *v1;
4068 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4070 rdata->mr = smbd_register_mr(
4071 server->smbd_conn, rdata->pages,
4072 rdata->nr_pages, rdata->page_offset,
4073 rdata->tailsz, true, need_invalidate);
4077 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4078 if (need_invalidate)
4079 req->Channel = SMB2_CHANNEL_RDMA_V1;
4080 req->ReadChannelInfoOffset =
4081 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4082 req->ReadChannelInfoLength =
4083 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4084 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4085 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4086 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4087 v1->length = cpu_to_le32(rdata->mr->mr->length);
4089 *total_len += sizeof(*v1) - 1;
4092 if (request_type & CHAINED_REQUEST) {
4093 if (!(request_type & END_OF_CHAIN)) {
4094 /* next 8-byte aligned request */
4095 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
4096 shdr->NextCommand = cpu_to_le32(*total_len);
4097 } else /* END_OF_CHAIN */
4098 shdr->NextCommand = 0;
4099 if (request_type & RELATED_REQUEST) {
4100 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4102 * Related requests use info from previous read request
4105 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4106 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4107 req->PersistentFileId = (u64)-1;
4108 req->VolatileFileId = (u64)-1;
4111 if (remaining_bytes > io_parms->length)
4112 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4114 req->RemainingBytes = 0;
4121 smb2_readv_callback(struct mid_q_entry *mid)
4123 struct cifs_readdata *rdata = mid->callback_data;
4124 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4125 struct TCP_Server_Info *server = rdata->server;
4126 struct smb2_hdr *shdr =
4127 (struct smb2_hdr *)rdata->iov[0].iov_base;
4128 struct cifs_credits credits = { .value = 0, .instance = 0 };
4129 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
4131 .rq_pages = rdata->pages,
4132 .rq_offset = rdata->page_offset,
4133 .rq_npages = rdata->nr_pages,
4134 .rq_pagesz = rdata->pagesz,
4135 .rq_tailsz = rdata->tailsz };
4137 WARN_ONCE(rdata->server != mid->server,
4138 "rdata server %p != mid server %p",
4139 rdata->server, mid->server);
4141 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4142 __func__, mid->mid, mid->mid_state, rdata->result,
4145 switch (mid->mid_state) {
4146 case MID_RESPONSE_RECEIVED:
4147 credits.value = le16_to_cpu(shdr->CreditRequest);
4148 credits.instance = server->reconnect_instance;
4149 /* result already set, check signature */
4150 if (server->sign && !mid->decrypted) {
4153 rc = smb2_verify_signature(&rqst, server);
4155 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4158 /* FIXME: should this be counted toward the initiating task? */
4159 task_io_account_read(rdata->got_bytes);
4160 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4162 case MID_REQUEST_SUBMITTED:
4163 case MID_RETRY_NEEDED:
4164 rdata->result = -EAGAIN;
4165 if (server->sign && rdata->got_bytes)
4166 /* reset bytes number since we can not check a sign */
4167 rdata->got_bytes = 0;
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_RESPONSE_MALFORMED:
4173 credits.value = le16_to_cpu(shdr->CreditRequest);
4174 credits.instance = server->reconnect_instance;
4177 rdata->result = -EIO;
4179 #ifdef CONFIG_CIFS_SMB_DIRECT
4181 * If this rdata has a memmory registered, the MR can be freed
4182 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4183 * because they have limited number and are used for future I/Os
4186 smbd_deregister_mr(rdata->mr);
4190 if (rdata->result && rdata->result != -ENODATA) {
4191 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4192 trace_smb3_read_err(0 /* xid */,
4193 rdata->cfile->fid.persistent_fid,
4194 tcon->tid, tcon->ses->Suid, rdata->offset,
4195 rdata->bytes, rdata->result);
4197 trace_smb3_read_done(0 /* xid */,
4198 rdata->cfile->fid.persistent_fid,
4199 tcon->tid, tcon->ses->Suid,
4200 rdata->offset, rdata->got_bytes);
4202 queue_work(cifsiod_wq, &rdata->work);
4203 DeleteMidQEntry(mid);
4204 add_credits(server, &credits, 0);
4207 /* smb2_async_readv - send an async read, and set up mid to handle result */
4209 smb2_async_readv(struct cifs_readdata *rdata)
4213 struct smb2_hdr *shdr;
4214 struct cifs_io_parms io_parms;
4215 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4217 struct TCP_Server_Info *server;
4218 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4219 unsigned int total_len;
4221 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4222 __func__, rdata->offset, rdata->bytes);
4225 rdata->server = cifs_pick_channel(tcon->ses);
4227 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4228 io_parms.server = server = rdata->server;
4229 io_parms.offset = rdata->offset;
4230 io_parms.length = rdata->bytes;
4231 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4232 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4233 io_parms.pid = rdata->pid;
4235 rc = smb2_new_read_req(
4236 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4240 if (smb3_encryption_required(io_parms.tcon))
4241 flags |= CIFS_TRANSFORM_REQ;
4243 rdata->iov[0].iov_base = buf;
4244 rdata->iov[0].iov_len = total_len;
4246 shdr = (struct smb2_hdr *)buf;
4248 if (rdata->credits.value > 0) {
4249 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4250 SMB2_MAX_BUFFER_SIZE));
4251 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4253 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4255 goto async_readv_out;
4257 flags |= CIFS_HAS_CREDITS;
4260 kref_get(&rdata->refcount);
4261 rc = cifs_call_async(server, &rqst,
4262 cifs_readv_receive, smb2_readv_callback,
4263 smb3_handle_read_data, rdata, flags,
4266 kref_put(&rdata->refcount, cifs_readdata_release);
4267 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4268 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4270 io_parms.tcon->ses->Suid,
4271 io_parms.offset, io_parms.length, rc);
4275 cifs_small_buf_release(buf);
4280 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4281 unsigned int *nbytes, char **buf, int *buf_type)
4283 struct smb_rqst rqst;
4284 int resp_buftype, rc;
4285 struct smb2_read_req *req = NULL;
4286 struct smb2_read_rsp *rsp = NULL;
4288 struct kvec rsp_iov;
4289 unsigned int total_len;
4290 int flags = CIFS_LOG_ERROR;
4291 struct cifs_ses *ses = io_parms->tcon->ses;
4293 if (!io_parms->server)
4294 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4297 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4301 if (smb3_encryption_required(io_parms->tcon))
4302 flags |= CIFS_TRANSFORM_REQ;
4304 iov[0].iov_base = (char *)req;
4305 iov[0].iov_len = total_len;
4307 memset(&rqst, 0, sizeof(struct smb_rqst));
4311 rc = cifs_send_recv(xid, ses, io_parms->server,
4312 &rqst, &resp_buftype, flags, &rsp_iov);
4313 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4316 if (rc != -ENODATA) {
4317 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4318 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4319 trace_smb3_read_err(xid,
4320 req->PersistentFileId,
4321 io_parms->tcon->tid, ses->Suid,
4322 io_parms->offset, io_parms->length,
4325 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4326 ses->Suid, io_parms->offset, 0);
4327 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4328 cifs_small_buf_release(req);
4329 return rc == -ENODATA ? 0 : rc;
4331 trace_smb3_read_done(xid,
4332 req->PersistentFileId,
4333 io_parms->tcon->tid, ses->Suid,
4334 io_parms->offset, io_parms->length);
4336 cifs_small_buf_release(req);
4338 *nbytes = le32_to_cpu(rsp->DataLength);
4339 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4340 (*nbytes > io_parms->length)) {
4341 cifs_dbg(FYI, "bad length %d for count %d\n",
4342 *nbytes, io_parms->length);
4348 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4349 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4350 } else if (resp_buftype != CIFS_NO_BUFFER) {
4351 *buf = rsp_iov.iov_base;
4352 if (resp_buftype == CIFS_SMALL_BUFFER)
4353 *buf_type = CIFS_SMALL_BUFFER;
4354 else if (resp_buftype == CIFS_LARGE_BUFFER)
4355 *buf_type = CIFS_LARGE_BUFFER;
4361 * Check the mid_state and signature on received buffer (if any), and queue the
4362 * workqueue completion task.
4365 smb2_writev_callback(struct mid_q_entry *mid)
4367 struct cifs_writedata *wdata = mid->callback_data;
4368 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4369 struct TCP_Server_Info *server = wdata->server;
4370 unsigned int written;
4371 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4372 struct cifs_credits credits = { .value = 0, .instance = 0 };
4374 WARN_ONCE(wdata->server != mid->server,
4375 "wdata server %p != mid server %p",
4376 wdata->server, mid->server);
4378 switch (mid->mid_state) {
4379 case MID_RESPONSE_RECEIVED:
4380 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4381 credits.instance = server->reconnect_instance;
4382 wdata->result = smb2_check_receive(mid, server, 0);
4383 if (wdata->result != 0)
4386 written = le32_to_cpu(rsp->DataLength);
4388 * Mask off high 16 bits when bytes written as returned
4389 * by the server is greater than bytes requested by the
4390 * client. OS/2 servers are known to set incorrect
4393 if (written > wdata->bytes)
4396 if (written < wdata->bytes)
4397 wdata->result = -ENOSPC;
4399 wdata->bytes = written;
4401 case MID_REQUEST_SUBMITTED:
4402 case MID_RETRY_NEEDED:
4403 wdata->result = -EAGAIN;
4405 case MID_RESPONSE_MALFORMED:
4406 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4407 credits.instance = server->reconnect_instance;
4410 wdata->result = -EIO;
4413 #ifdef CONFIG_CIFS_SMB_DIRECT
4415 * If this wdata has a memory registered, the MR can be freed
4416 * The number of MRs available is limited, it's important to recover
4417 * used MR as soon as I/O is finished. Hold MR longer in the later
4418 * I/O process can possibly result in I/O deadlock due to lack of MR
4419 * to send request on I/O retry
4422 smbd_deregister_mr(wdata->mr);
4426 if (wdata->result) {
4427 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4428 trace_smb3_write_err(0 /* no xid */,
4429 wdata->cfile->fid.persistent_fid,
4430 tcon->tid, tcon->ses->Suid, wdata->offset,
4431 wdata->bytes, wdata->result);
4432 if (wdata->result == -ENOSPC)
4433 pr_warn_once("Out of space writing to %s\n",
4436 trace_smb3_write_done(0 /* no xid */,
4437 wdata->cfile->fid.persistent_fid,
4438 tcon->tid, tcon->ses->Suid,
4439 wdata->offset, wdata->bytes);
4441 queue_work(cifsiod_wq, &wdata->work);
4442 DeleteMidQEntry(mid);
4443 add_credits(server, &credits, 0);
4446 /* smb2_async_writev - send an async write, and set up mid to handle result */
4448 smb2_async_writev(struct cifs_writedata *wdata,
4449 void (*release)(struct kref *kref))
4451 int rc = -EACCES, flags = 0;
4452 struct smb2_write_req *req = NULL;
4453 struct smb2_hdr *shdr;
4454 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4455 struct TCP_Server_Info *server = wdata->server;
4457 struct smb_rqst rqst = { };
4458 unsigned int total_len;
4461 server = wdata->server = cifs_pick_channel(tcon->ses);
4463 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4464 (void **) &req, &total_len);
4468 if (smb3_encryption_required(tcon))
4469 flags |= CIFS_TRANSFORM_REQ;
4471 shdr = (struct smb2_hdr *)req;
4472 shdr->Id.SyncId.ProcessId = cpu_to_le32(wdata->cfile->pid);
4474 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4475 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4476 req->WriteChannelInfoOffset = 0;
4477 req->WriteChannelInfoLength = 0;
4479 req->Offset = cpu_to_le64(wdata->offset);
4480 req->DataOffset = cpu_to_le16(
4481 offsetof(struct smb2_write_req, Buffer));
4482 req->RemainingBytes = 0;
4484 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4485 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4486 #ifdef CONFIG_CIFS_SMB_DIRECT
4488 * If we want to do a server RDMA read, fill in and append
4489 * smbd_buffer_descriptor_v1 to the end of write request
4491 if (server->rdma && !server->sign && wdata->bytes >=
4492 server->smbd_conn->rdma_readwrite_threshold) {
4494 struct smbd_buffer_descriptor_v1 *v1;
4495 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4497 wdata->mr = smbd_register_mr(
4498 server->smbd_conn, wdata->pages,
4499 wdata->nr_pages, wdata->page_offset,
4500 wdata->tailsz, false, need_invalidate);
4503 goto async_writev_out;
4506 req->DataOffset = 0;
4507 if (wdata->nr_pages > 1)
4508 req->RemainingBytes =
4510 (wdata->nr_pages - 1) * wdata->pagesz -
4511 wdata->page_offset + wdata->tailsz
4514 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4515 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4516 if (need_invalidate)
4517 req->Channel = SMB2_CHANNEL_RDMA_V1;
4518 req->WriteChannelInfoOffset =
4519 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4520 req->WriteChannelInfoLength =
4521 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4522 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4523 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4524 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4525 v1->length = cpu_to_le32(wdata->mr->mr->length);
4528 iov[0].iov_len = total_len - 1;
4529 iov[0].iov_base = (char *)req;
4533 rqst.rq_pages = wdata->pages;
4534 rqst.rq_offset = wdata->page_offset;
4535 rqst.rq_npages = wdata->nr_pages;
4536 rqst.rq_pagesz = wdata->pagesz;
4537 rqst.rq_tailsz = wdata->tailsz;
4538 #ifdef CONFIG_CIFS_SMB_DIRECT
4540 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4544 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4545 wdata->offset, wdata->bytes);
4547 #ifdef CONFIG_CIFS_SMB_DIRECT
4548 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4550 req->Length = cpu_to_le32(wdata->bytes);
4552 req->Length = cpu_to_le32(wdata->bytes);
4555 if (wdata->credits.value > 0) {
4556 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4557 SMB2_MAX_BUFFER_SIZE));
4558 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4560 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4562 goto async_writev_out;
4564 flags |= CIFS_HAS_CREDITS;
4567 kref_get(&wdata->refcount);
4568 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4569 wdata, flags, &wdata->credits);
4572 trace_smb3_write_err(0 /* no xid */,
4573 req->PersistentFileId,
4574 tcon->tid, tcon->ses->Suid, wdata->offset,
4576 kref_put(&wdata->refcount, release);
4577 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4581 cifs_small_buf_release(req);
4586 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4587 * The length field from io_parms must be at least 1 and indicates a number of
4588 * elements with data to write that begins with position 1 in iov array. All
4589 * data length is specified by count.
4592 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4593 unsigned int *nbytes, struct kvec *iov, int n_vec)
4595 struct smb_rqst rqst;
4597 struct smb2_write_req *req = NULL;
4598 struct smb2_write_rsp *rsp = NULL;
4600 struct kvec rsp_iov;
4602 unsigned int total_len;
4603 struct TCP_Server_Info *server;
4610 if (!io_parms->server)
4611 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4612 server = io_parms->server;
4614 return -ECONNABORTED;
4616 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4617 (void **) &req, &total_len);
4621 if (smb3_encryption_required(io_parms->tcon))
4622 flags |= CIFS_TRANSFORM_REQ;
4624 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4626 req->PersistentFileId = io_parms->persistent_fid;
4627 req->VolatileFileId = io_parms->volatile_fid;
4628 req->WriteChannelInfoOffset = 0;
4629 req->WriteChannelInfoLength = 0;
4631 req->Length = cpu_to_le32(io_parms->length);
4632 req->Offset = cpu_to_le64(io_parms->offset);
4633 req->DataOffset = cpu_to_le16(
4634 offsetof(struct smb2_write_req, Buffer));
4635 req->RemainingBytes = 0;
4637 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4638 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4639 io_parms->offset, io_parms->length);
4641 iov[0].iov_base = (char *)req;
4643 iov[0].iov_len = total_len - 1;
4645 memset(&rqst, 0, sizeof(struct smb_rqst));
4647 rqst.rq_nvec = n_vec + 1;
4649 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4651 &resp_buftype, flags, &rsp_iov);
4652 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4655 trace_smb3_write_err(xid,
4656 req->PersistentFileId,
4657 io_parms->tcon->tid,
4658 io_parms->tcon->ses->Suid,
4659 io_parms->offset, io_parms->length, rc);
4660 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4661 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4663 *nbytes = le32_to_cpu(rsp->DataLength);
4664 trace_smb3_write_done(xid,
4665 req->PersistentFileId,
4666 io_parms->tcon->tid,
4667 io_parms->tcon->ses->Suid,
4668 io_parms->offset, *nbytes);
4671 cifs_small_buf_release(req);
4672 free_rsp_buf(resp_buftype, rsp);
4676 int posix_info_sid_size(const void *beg, const void *end)
4684 subauth = *(u8 *)(beg+1);
4685 if (subauth < 1 || subauth > 15)
4688 total = 1 + 1 + 6 + 4*subauth;
4689 if (beg + total > end)
4695 int posix_info_parse(const void *beg, const void *end,
4696 struct smb2_posix_info_parsed *out)
4700 int owner_len, group_len;
4702 const void *owner_sid;
4703 const void *group_sid;
4706 /* if no end bound given, assume payload to be correct */
4708 const struct smb2_posix_info *p = beg;
4710 end = beg + le32_to_cpu(p->NextEntryOffset);
4711 /* last element will have a 0 offset, pick a sensible bound */
4716 /* check base buf */
4717 if (beg + sizeof(struct smb2_posix_info) > end)
4719 total_len = sizeof(struct smb2_posix_info);
4721 /* check owner sid */
4722 owner_sid = beg + total_len;
4723 owner_len = posix_info_sid_size(owner_sid, end);
4726 total_len += owner_len;
4728 /* check group sid */
4729 group_sid = beg + total_len;
4730 group_len = posix_info_sid_size(group_sid, end);
4733 total_len += group_len;
4735 /* check name len */
4736 if (beg + total_len + 4 > end)
4738 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4739 if (name_len < 1 || name_len > 0xFFFF)
4744 name = beg + total_len;
4745 if (name + name_len > end)
4747 total_len += name_len;
4751 out->size = total_len;
4752 out->name_len = name_len;
4754 memcpy(&out->owner, owner_sid, owner_len);
4755 memcpy(&out->group, group_sid, group_len);
4760 static int posix_info_extra_size(const void *beg, const void *end)
4762 int len = posix_info_parse(beg, end, NULL);
4766 return len - sizeof(struct smb2_posix_info);
4770 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4774 unsigned int entrycount = 0;
4775 unsigned int next_offset = 0;
4777 FILE_DIRECTORY_INFO *dir_info;
4779 if (bufstart == NULL)
4782 entryptr = bufstart;
4785 if (entryptr + next_offset < entryptr ||
4786 entryptr + next_offset > end_of_buf ||
4787 entryptr + next_offset + size > end_of_buf) {
4788 cifs_dbg(VFS, "malformed search entry would overflow\n");
4792 entryptr = entryptr + next_offset;
4793 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4795 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4796 len = posix_info_extra_size(entryptr, end_of_buf);
4798 len = le32_to_cpu(dir_info->FileNameLength);
4801 entryptr + len < entryptr ||
4802 entryptr + len > end_of_buf ||
4803 entryptr + len + size > end_of_buf) {
4804 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4809 *lastentry = entryptr;
4812 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4823 int SMB2_query_directory_init(const unsigned int xid,
4824 struct cifs_tcon *tcon,
4825 struct TCP_Server_Info *server,
4826 struct smb_rqst *rqst,
4827 u64 persistent_fid, u64 volatile_fid,
4828 int index, int info_level)
4830 struct smb2_query_directory_req *req;
4831 unsigned char *bufptr;
4832 __le16 asteriks = cpu_to_le16('*');
4833 unsigned int output_size = CIFSMaxBufSize -
4834 MAX_SMB2_CREATE_RESPONSE_SIZE -
4835 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4836 unsigned int total_len;
4837 struct kvec *iov = rqst->rq_iov;
4840 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4841 (void **) &req, &total_len);
4845 switch (info_level) {
4846 case SMB_FIND_FILE_DIRECTORY_INFO:
4847 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4849 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4850 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4852 case SMB_FIND_FILE_POSIX_INFO:
4853 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4856 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4861 req->FileIndex = cpu_to_le32(index);
4862 req->PersistentFileId = persistent_fid;
4863 req->VolatileFileId = volatile_fid;
4866 bufptr = req->Buffer;
4867 memcpy(bufptr, &asteriks, len);
4869 req->FileNameOffset =
4870 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4871 req->FileNameLength = cpu_to_le16(len);
4873 * BB could be 30 bytes or so longer if we used SMB2 specific
4874 * buffer lengths, but this is safe and close enough.
4876 output_size = min_t(unsigned int, output_size, server->maxBuf);
4877 output_size = min_t(unsigned int, output_size, 2 << 15);
4878 req->OutputBufferLength = cpu_to_le32(output_size);
4880 iov[0].iov_base = (char *)req;
4882 iov[0].iov_len = total_len - 1;
4884 iov[1].iov_base = (char *)(req->Buffer);
4885 iov[1].iov_len = len;
4887 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4888 tcon->ses->Suid, index, output_size);
4893 void SMB2_query_directory_free(struct smb_rqst *rqst)
4895 if (rqst && rqst->rq_iov) {
4896 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4901 smb2_parse_query_directory(struct cifs_tcon *tcon,
4902 struct kvec *rsp_iov,
4904 struct cifs_search_info *srch_inf)
4906 struct smb2_query_directory_rsp *rsp;
4907 size_t info_buf_size;
4911 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4913 switch (srch_inf->info_level) {
4914 case SMB_FIND_FILE_DIRECTORY_INFO:
4915 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4917 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4918 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4920 case SMB_FIND_FILE_POSIX_INFO:
4921 /* note that posix payload are variable size */
4922 info_buf_size = sizeof(struct smb2_posix_info);
4925 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4926 srch_inf->info_level);
4930 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4931 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4934 cifs_tcon_dbg(VFS, "bad info payload");
4938 srch_inf->unicode = true;
4940 if (srch_inf->ntwrk_buf_start) {
4941 if (srch_inf->smallBuf)
4942 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4944 cifs_buf_release(srch_inf->ntwrk_buf_start);
4946 srch_inf->ntwrk_buf_start = (char *)rsp;
4947 srch_inf->srch_entries_start = srch_inf->last_entry =
4948 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4949 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4951 srch_inf->entries_in_buffer = num_entries(
4952 srch_inf->info_level,
4953 srch_inf->srch_entries_start,
4955 &srch_inf->last_entry,
4958 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4959 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4960 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4961 srch_inf->srch_entries_start, srch_inf->last_entry);
4962 if (resp_buftype == CIFS_LARGE_BUFFER)
4963 srch_inf->smallBuf = false;
4964 else if (resp_buftype == CIFS_SMALL_BUFFER)
4965 srch_inf->smallBuf = true;
4967 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4973 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4974 u64 persistent_fid, u64 volatile_fid, int index,
4975 struct cifs_search_info *srch_inf)
4977 struct smb_rqst rqst;
4978 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4979 struct smb2_query_directory_rsp *rsp = NULL;
4980 int resp_buftype = CIFS_NO_BUFFER;
4981 struct kvec rsp_iov;
4983 struct cifs_ses *ses = tcon->ses;
4984 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4987 if (!ses || !(ses->server))
4990 if (smb3_encryption_required(tcon))
4991 flags |= CIFS_TRANSFORM_REQ;
4993 memset(&rqst, 0, sizeof(struct smb_rqst));
4994 memset(&iov, 0, sizeof(iov));
4996 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4998 rc = SMB2_query_directory_init(xid, tcon, server,
4999 &rqst, persistent_fid,
5000 volatile_fid, index,
5001 srch_inf->info_level);
5005 rc = cifs_send_recv(xid, ses, server,
5006 &rqst, &resp_buftype, flags, &rsp_iov);
5007 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5010 if (rc == -ENODATA &&
5011 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5012 trace_smb3_query_dir_done(xid, persistent_fid,
5013 tcon->tid, tcon->ses->Suid, index, 0);
5014 srch_inf->endOfSearch = true;
5017 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5018 tcon->ses->Suid, index, 0, rc);
5019 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5024 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5027 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5028 tcon->ses->Suid, index, 0, rc);
5031 resp_buftype = CIFS_NO_BUFFER;
5033 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5034 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5037 SMB2_query_directory_free(&rqst);
5038 free_rsp_buf(resp_buftype, rsp);
5043 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5044 struct smb_rqst *rqst,
5045 u64 persistent_fid, u64 volatile_fid, u32 pid,
5046 u8 info_class, u8 info_type, u32 additional_info,
5047 void **data, unsigned int *size)
5049 struct smb2_set_info_req *req;
5050 struct kvec *iov = rqst->rq_iov;
5051 unsigned int i, total_len;
5054 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5055 (void **) &req, &total_len);
5059 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5060 req->InfoType = info_type;
5061 req->FileInfoClass = info_class;
5062 req->PersistentFileId = persistent_fid;
5063 req->VolatileFileId = volatile_fid;
5064 req->AdditionalInformation = cpu_to_le32(additional_info);
5067 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
5068 req->BufferLength = cpu_to_le32(*size);
5070 memcpy(req->Buffer, *data, *size);
5073 iov[0].iov_base = (char *)req;
5075 iov[0].iov_len = total_len - 1;
5077 for (i = 1; i < rqst->rq_nvec; i++) {
5078 le32_add_cpu(&req->BufferLength, size[i]);
5079 iov[i].iov_base = (char *)data[i];
5080 iov[i].iov_len = size[i];
5087 SMB2_set_info_free(struct smb_rqst *rqst)
5089 if (rqst && rqst->rq_iov)
5090 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5094 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5095 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5096 u8 info_type, u32 additional_info, unsigned int num,
5097 void **data, unsigned int *size)
5099 struct smb_rqst rqst;
5100 struct smb2_set_info_rsp *rsp = NULL;
5102 struct kvec rsp_iov;
5105 struct cifs_ses *ses = tcon->ses;
5106 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5109 if (!ses || !server)
5115 if (smb3_encryption_required(tcon))
5116 flags |= CIFS_TRANSFORM_REQ;
5118 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5122 memset(&rqst, 0, sizeof(struct smb_rqst));
5126 rc = SMB2_set_info_init(tcon, server,
5127 &rqst, persistent_fid, volatile_fid, pid,
5128 info_class, info_type, additional_info,
5136 rc = cifs_send_recv(xid, ses, server,
5137 &rqst, &resp_buftype, flags,
5139 SMB2_set_info_free(&rqst);
5140 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5143 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5144 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5145 ses->Suid, info_class, (__u32)info_type, rc);
5148 free_rsp_buf(resp_buftype, rsp);
5154 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5155 u64 volatile_fid, u32 pid, __le64 *eof)
5157 struct smb2_file_eof_info info;
5161 info.EndOfFile = *eof;
5164 size = sizeof(struct smb2_file_eof_info);
5166 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5168 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5169 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5170 0, 1, &data, &size);
5174 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5175 u64 persistent_fid, u64 volatile_fid,
5176 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5178 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5179 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5180 1, (void **)&pnntsd, &pacllen);
5184 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5185 u64 persistent_fid, u64 volatile_fid,
5186 struct smb2_file_full_ea_info *buf, int len)
5188 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5189 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5190 0, 1, (void **)&buf, &len);
5194 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5195 const u64 persistent_fid, const u64 volatile_fid,
5198 struct smb_rqst rqst;
5200 struct smb2_oplock_break *req = NULL;
5201 struct cifs_ses *ses = tcon->ses;
5202 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5203 int flags = CIFS_OBREAK_OP;
5204 unsigned int total_len;
5206 struct kvec rsp_iov;
5209 cifs_dbg(FYI, "SMB2_oplock_break\n");
5210 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5211 (void **) &req, &total_len);
5215 if (smb3_encryption_required(tcon))
5216 flags |= CIFS_TRANSFORM_REQ;
5218 req->VolatileFid = volatile_fid;
5219 req->PersistentFid = persistent_fid;
5220 req->OplockLevel = oplock_level;
5221 req->hdr.CreditRequest = cpu_to_le16(1);
5223 flags |= CIFS_NO_RSP_BUF;
5225 iov[0].iov_base = (char *)req;
5226 iov[0].iov_len = total_len;
5228 memset(&rqst, 0, sizeof(struct smb_rqst));
5232 rc = cifs_send_recv(xid, ses, server,
5233 &rqst, &resp_buf_type, flags, &rsp_iov);
5234 cifs_small_buf_release(req);
5237 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5238 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5245 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5246 struct kstatfs *kst)
5248 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5249 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5250 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5251 kst->f_bfree = kst->f_bavail =
5252 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5257 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5258 struct kstatfs *kst)
5260 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5261 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5262 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5263 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5264 kst->f_bavail = kst->f_bfree;
5266 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5267 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5268 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5269 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5270 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5276 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5277 struct TCP_Server_Info *server,
5278 int level, int outbuf_len, u64 persistent_fid,
5282 struct smb2_query_info_req *req;
5283 unsigned int total_len;
5285 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5287 if ((tcon->ses == NULL) || server == NULL)
5290 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5291 (void **) &req, &total_len);
5295 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5296 req->FileInfoClass = level;
5297 req->PersistentFileId = persistent_fid;
5298 req->VolatileFileId = volatile_fid;
5300 req->InputBufferOffset =
5301 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5302 req->OutputBufferLength = cpu_to_le32(
5303 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5305 iov->iov_base = (char *)req;
5306 iov->iov_len = total_len;
5311 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5312 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5314 struct smb_rqst rqst;
5315 struct smb2_query_info_rsp *rsp = NULL;
5317 struct kvec rsp_iov;
5320 struct cifs_ses *ses = tcon->ses;
5321 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5322 FILE_SYSTEM_POSIX_INFO *info = NULL;
5325 rc = build_qfs_info_req(&iov, tcon, server,
5326 FS_POSIX_INFORMATION,
5327 sizeof(FILE_SYSTEM_POSIX_INFO),
5328 persistent_fid, volatile_fid);
5332 if (smb3_encryption_required(tcon))
5333 flags |= CIFS_TRANSFORM_REQ;
5335 memset(&rqst, 0, sizeof(struct smb_rqst));
5339 rc = cifs_send_recv(xid, ses, server,
5340 &rqst, &resp_buftype, flags, &rsp_iov);
5341 cifs_small_buf_release(iov.iov_base);
5343 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5344 goto posix_qfsinf_exit;
5346 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5348 info = (FILE_SYSTEM_POSIX_INFO *)(
5349 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5350 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5351 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5352 sizeof(FILE_SYSTEM_POSIX_INFO));
5354 copy_posix_fs_info_to_kstatfs(info, fsdata);
5357 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5362 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5363 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5365 struct smb_rqst rqst;
5366 struct smb2_query_info_rsp *rsp = NULL;
5368 struct kvec rsp_iov;
5371 struct cifs_ses *ses = tcon->ses;
5372 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5373 struct smb2_fs_full_size_info *info = NULL;
5376 rc = build_qfs_info_req(&iov, tcon, server,
5377 FS_FULL_SIZE_INFORMATION,
5378 sizeof(struct smb2_fs_full_size_info),
5379 persistent_fid, volatile_fid);
5383 if (smb3_encryption_required(tcon))
5384 flags |= CIFS_TRANSFORM_REQ;
5386 memset(&rqst, 0, sizeof(struct smb_rqst));
5390 rc = cifs_send_recv(xid, ses, server,
5391 &rqst, &resp_buftype, flags, &rsp_iov);
5392 cifs_small_buf_release(iov.iov_base);
5394 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5397 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5399 info = (struct smb2_fs_full_size_info *)(
5400 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5401 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5402 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5403 sizeof(struct smb2_fs_full_size_info));
5405 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5408 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5413 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5414 u64 persistent_fid, u64 volatile_fid, int level)
5416 struct smb_rqst rqst;
5417 struct smb2_query_info_rsp *rsp = NULL;
5419 struct kvec rsp_iov;
5421 int resp_buftype, max_len, min_len;
5422 struct cifs_ses *ses = tcon->ses;
5423 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5424 unsigned int rsp_len, offset;
5427 if (level == FS_DEVICE_INFORMATION) {
5428 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5429 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5430 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5431 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5432 min_len = MIN_FS_ATTR_INFO_SIZE;
5433 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5434 max_len = sizeof(struct smb3_fs_ss_info);
5435 min_len = sizeof(struct smb3_fs_ss_info);
5436 } else if (level == FS_VOLUME_INFORMATION) {
5437 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5438 min_len = sizeof(struct smb3_fs_vol_info);
5440 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5444 rc = build_qfs_info_req(&iov, tcon, server,
5446 persistent_fid, volatile_fid);
5450 if (smb3_encryption_required(tcon))
5451 flags |= CIFS_TRANSFORM_REQ;
5453 memset(&rqst, 0, sizeof(struct smb_rqst));
5457 rc = cifs_send_recv(xid, ses, server,
5458 &rqst, &resp_buftype, flags, &rsp_iov);
5459 cifs_small_buf_release(iov.iov_base);
5461 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5464 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5466 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5467 offset = le16_to_cpu(rsp->OutputBufferOffset);
5468 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5472 if (level == FS_ATTRIBUTE_INFORMATION)
5473 memcpy(&tcon->fsAttrInfo, offset
5474 + (char *)rsp, min_t(unsigned int,
5476 else if (level == FS_DEVICE_INFORMATION)
5477 memcpy(&tcon->fsDevInfo, offset
5478 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5479 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5480 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5481 (offset + (char *)rsp);
5482 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5483 tcon->perf_sector_size =
5484 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5485 } else if (level == FS_VOLUME_INFORMATION) {
5486 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5487 (offset + (char *)rsp);
5488 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5489 tcon->vol_create_time = vol_info->VolumeCreationTime;
5493 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5498 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5499 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5500 const __u32 num_lock, struct smb2_lock_element *buf)
5502 struct smb_rqst rqst;
5504 struct smb2_lock_req *req = NULL;
5506 struct kvec rsp_iov;
5509 int flags = CIFS_NO_RSP_BUF;
5510 unsigned int total_len;
5511 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5513 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5515 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5516 (void **) &req, &total_len);
5520 if (smb3_encryption_required(tcon))
5521 flags |= CIFS_TRANSFORM_REQ;
5523 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5524 req->LockCount = cpu_to_le16(num_lock);
5526 req->PersistentFileId = persist_fid;
5527 req->VolatileFileId = volatile_fid;
5529 count = num_lock * sizeof(struct smb2_lock_element);
5531 iov[0].iov_base = (char *)req;
5532 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5533 iov[1].iov_base = (char *)buf;
5534 iov[1].iov_len = count;
5536 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5538 memset(&rqst, 0, sizeof(struct smb_rqst));
5542 rc = cifs_send_recv(xid, tcon->ses, server,
5543 &rqst, &resp_buf_type, flags,
5545 cifs_small_buf_release(req);
5547 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5548 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5549 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5550 tcon->ses->Suid, rc);
5557 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5558 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5559 const __u64 length, const __u64 offset, const __u32 lock_flags,
5562 struct smb2_lock_element lock;
5564 lock.Offset = cpu_to_le64(offset);
5565 lock.Length = cpu_to_le64(length);
5566 lock.Flags = cpu_to_le32(lock_flags);
5567 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5568 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5570 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5574 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5575 __u8 *lease_key, const __le32 lease_state)
5577 struct smb_rqst rqst;
5579 struct smb2_lease_ack *req = NULL;
5580 struct cifs_ses *ses = tcon->ses;
5581 int flags = CIFS_OBREAK_OP;
5582 unsigned int total_len;
5584 struct kvec rsp_iov;
5586 __u64 *please_key_high;
5587 __u64 *please_key_low;
5588 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5590 cifs_dbg(FYI, "SMB2_lease_break\n");
5591 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5592 (void **) &req, &total_len);
5596 if (smb3_encryption_required(tcon))
5597 flags |= CIFS_TRANSFORM_REQ;
5599 req->hdr.CreditRequest = cpu_to_le16(1);
5600 req->StructureSize = cpu_to_le16(36);
5603 memcpy(req->LeaseKey, lease_key, 16);
5604 req->LeaseState = lease_state;
5606 flags |= CIFS_NO_RSP_BUF;
5608 iov[0].iov_base = (char *)req;
5609 iov[0].iov_len = total_len;
5611 memset(&rqst, 0, sizeof(struct smb_rqst));
5615 rc = cifs_send_recv(xid, ses, server,
5616 &rqst, &resp_buf_type, flags, &rsp_iov);
5617 cifs_small_buf_release(req);
5619 please_key_low = (__u64 *)lease_key;
5620 please_key_high = (__u64 *)(lease_key+8);
5622 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5623 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5624 ses->Suid, *please_key_low, *please_key_high, rc);
5625 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5627 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5628 ses->Suid, *please_key_low, *please_key_high);