4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
59 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static int encryption_required(const struct cifs_tcon *tcon)
85 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
96 const struct cifs_tcon *tcon)
98 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
106 if (server->credits >= server->max_credits)
107 shdr->CreditRequest = cpu_to_le16(0);
109 shdr->CreditRequest = cpu_to_le16(
110 min_t(int, server->max_credits -
111 server->credits, 2));
112 spin_unlock(&server->req_lock);
114 shdr->CreditRequest = cpu_to_le16(2);
116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
123 if ((tcon->ses) && (tcon->ses->server) &&
124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
125 shdr->CreditCharge = cpu_to_le16(1);
126 /* else CreditCharge MBZ */
128 shdr->TreeId = tcon->tid;
129 /* Uid is not converted */
131 shdr->SessionId = tcon->ses->Suid;
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
143 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
148 shdr->Flags |= SMB2_FLAGS_SIGNED;
154 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
169 if (smb2_command == SMB2_TREE_CONNECT)
172 if (tcon->tidStatus == CifsExiting) {
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
191 server = ses->server;
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
197 while (server->tcpStatus == CifsNeedReconnect) {
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
202 switch (smb2_command) {
204 * BB Should we keep oplock break and add flush to exceptions?
206 case SMB2_TREE_DISCONNECT:
209 case SMB2_OPLOCK_BREAK:
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 nls_codepage = load_nls_default();
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
240 mutex_lock(&tcon->ses->session_mutex);
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
247 if (server->tcpStatus == CifsNeedReconnect) {
249 mutex_unlock(&tcon->ses->session_mutex);
253 rc = cifs_negotiate_protocol(0, tcon->ses);
254 if (!rc && tcon->ses->need_reconnect)
255 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
257 if (rc || !tcon->need_reconnect) {
258 mutex_unlock(&tcon->ses->session_mutex);
262 cifs_mark_open_files_invalid(tcon);
263 if (tcon->use_persistent)
264 tcon->need_reopen_files = true;
266 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
267 mutex_unlock(&tcon->ses->session_mutex);
269 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
273 if (smb2_command != SMB2_INTERNAL_CMD)
274 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
276 atomic_inc(&tconInfoReconnectCount);
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
286 switch (smb2_command) {
292 case SMB2_QUERY_DIRECTORY:
293 case SMB2_CHANGE_NOTIFY:
294 case SMB2_QUERY_INFO:
298 unload_nls(nls_codepage);
303 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
304 unsigned int *total_len)
306 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
316 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
317 spdu->StructureSize2 = cpu_to_le16(parmsize);
319 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
322 /* init request without RFC1001 length at the beginning */
324 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
325 void **request_buf, unsigned int *total_len)
328 struct smb2_sync_hdr *shdr;
330 rc = smb2_reconnect(smb2_command, tcon);
334 /* BB eventually switch this to SMB2 specific small buf size */
335 *request_buf = cifs_small_buf_get();
336 if (*request_buf == NULL) {
337 /* BB should we add a retry in here if not a writepage? */
341 shdr = (struct smb2_sync_hdr *)(*request_buf);
343 fill_small_buf(smb2_command, tcon, shdr, total_len);
346 #ifdef CONFIG_CIFS_STATS2
347 uint16_t com_code = le16_to_cpu(smb2_command);
349 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
351 cifs_stats_inc(&tcon->num_smbs_sent);
358 * Allocate and return pointer to an SMB request hdr, and set basic
359 * SMB information in the SMB header. If the return code is zero, this
360 * function must have filled in request_buf pointer. The returned buffer
361 * has RFC1001 length at the beginning.
364 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
368 unsigned int total_len;
369 struct smb2_pdu *pdu;
371 rc = smb2_reconnect(smb2_command, tcon);
375 /* BB eventually switch this to SMB2 specific small buf size */
376 *request_buf = cifs_small_buf_get();
377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
382 pdu = (struct smb2_pdu *)(*request_buf);
384 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
386 /* Note this is only network field converted to big endian */
387 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
390 #ifdef CONFIG_CIFS_STATS2
391 uint16_t com_code = le16_to_cpu(smb2_command);
392 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
394 cifs_stats_inc(&tcon->num_smbs_sent);
400 #ifdef CONFIG_CIFS_SMB311
401 /* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
402 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
405 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
406 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
409 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
411 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
412 pneg_ctxt->DataLength = cpu_to_le16(38);
413 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
414 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
415 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
416 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
420 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
422 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
423 pneg_ctxt->DataLength = cpu_to_le16(6);
424 pneg_ctxt->CipherCount = cpu_to_le16(2);
425 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
426 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
430 assemble_neg_contexts(struct smb2_negotiate_req *req,
431 unsigned int *total_len)
433 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
435 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
436 /* Add 2 to size to round to 8 byte boundary */
438 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
439 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
440 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
441 req->NegotiateContextCount = cpu_to_le16(2);
443 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
444 + sizeof(struct smb2_encryption_neg_context);
447 static void assemble_neg_contexts(struct smb2_negotiate_req *req,
448 unsigned int *total_len)
456 * SMB2 Worker functions follow:
458 * The general structure of the worker functions is:
459 * 1) Call smb2_init (assembles SMB2 header)
460 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
461 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
462 * 4) Decode SMB2 command specific fields in the fixed length area
463 * 5) Decode variable length data area (if any for this SMB2 command type)
464 * 6) Call free smb buffer
470 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
472 struct smb2_negotiate_req *req;
473 struct smb2_negotiate_rsp *rsp;
478 struct TCP_Server_Info *server = ses->server;
479 int blob_offset, blob_length;
481 int flags = CIFS_NEG_OP;
482 unsigned int total_len;
484 cifs_dbg(FYI, "Negotiate protocol\n");
487 WARN(1, "%s: server is NULL!\n", __func__);
491 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
495 req->sync_hdr.SessionId = 0;
497 if (strcmp(ses->server->vals->version_string,
498 SMB3ANY_VERSION_STRING) == 0) {
499 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
500 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
501 req->DialectCount = cpu_to_le16(2);
503 } else if (strcmp(ses->server->vals->version_string,
504 SMBDEFAULT_VERSION_STRING) == 0) {
505 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
506 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
507 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
508 req->DialectCount = cpu_to_le16(3);
511 /* otherwise send specific dialect */
512 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
513 req->DialectCount = cpu_to_le16(1);
517 /* only one of SMB2 signing flags may be set in SMB2 request */
519 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
520 else if (global_secflags & CIFSSEC_MAY_SIGN)
521 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
523 req->SecurityMode = 0;
525 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
527 /* ClientGUID must be zero for SMB2.02 dialect */
528 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
529 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
531 memcpy(req->ClientGUID, server->client_guid,
532 SMB2_CLIENT_GUID_SIZE);
533 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
534 assemble_neg_contexts(req, &total_len);
536 iov[0].iov_base = (char *)req;
537 iov[0].iov_len = total_len;
539 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
540 cifs_small_buf_release(req);
541 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
543 * No tcon so can't do
544 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
546 if (rc == -EOPNOTSUPP) {
547 cifs_dbg(VFS, "Dialect not supported by server. Consider "
548 "specifying vers=1.0 or vers=2.0 on mount for accessing"
554 if (strcmp(ses->server->vals->version_string,
555 SMB3ANY_VERSION_STRING) == 0) {
556 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
558 "SMB2 dialect returned but not requested\n");
560 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
562 "SMB2.1 dialect returned but not requested\n");
565 } else if (strcmp(ses->server->vals->version_string,
566 SMBDEFAULT_VERSION_STRING) == 0) {
567 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
569 "SMB2 dialect returned but not requested\n");
571 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
572 /* ops set to 3.0 by default for default so update */
573 ses->server->ops = &smb21_operations;
575 } else if (le16_to_cpu(rsp->DialectRevision) !=
576 ses->server->vals->protocol_id) {
577 /* if requested single dialect ensure returned dialect matched */
578 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
579 le16_to_cpu(rsp->DialectRevision));
583 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
585 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
586 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
587 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
588 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
589 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
590 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
591 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
592 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
593 #ifdef CONFIG_CIFS_SMB311
594 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
595 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
598 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
599 le16_to_cpu(rsp->DialectRevision));
603 server->dialect = le16_to_cpu(rsp->DialectRevision);
605 /* BB: add check that dialect was valid given dialect(s) we asked for */
607 /* SMB2 only has an extended negflavor */
608 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
609 /* set it to the maximum buffer size value we can send with 1 credit */
610 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
611 SMB2_MAX_BUFFER_SIZE);
612 server->max_read = le32_to_cpu(rsp->MaxReadSize);
613 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
614 /* BB Do we need to validate the SecurityMode? */
615 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
616 server->capabilities = le32_to_cpu(rsp->Capabilities);
618 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
620 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
623 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
625 * ses->sectype = RawNTLMSSP;
626 * but for time being this is our only auth choice so doesn't matter.
627 * We just found a server which sets blob length to zero expecting raw.
629 if (blob_length == 0) {
630 cifs_dbg(FYI, "missing security blob on negprot\n");
631 server->sec_ntlmssp = true;
634 rc = cifs_enable_signing(server, ses->sign);
638 rc = decode_negTokenInit(security_blob, blob_length, server);
645 free_rsp_buf(resp_buftype, rsp);
649 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
652 struct validate_negotiate_info_req vneg_inbuf;
653 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
655 u32 inbuflen; /* max of 4 dialects */
657 cifs_dbg(FYI, "validate negotiate\n");
660 * validation ioctl must be signed, so no point sending this if we
661 * can not sign it (ie are not known user). Even if signing is not
662 * required (enabled but not negotiated), in those cases we selectively
663 * sign just this, the first and only signed request on a connection.
664 * Having validation of negotiate info helps reduce attack vectors.
666 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
667 return 0; /* validation requires signing */
669 if (tcon->ses->user_name == NULL) {
670 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
671 return 0; /* validation requires signing */
674 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
675 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
677 vneg_inbuf.Capabilities =
678 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
679 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
680 SMB2_CLIENT_GUID_SIZE);
683 vneg_inbuf.SecurityMode =
684 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
685 else if (global_secflags & CIFSSEC_MAY_SIGN)
686 vneg_inbuf.SecurityMode =
687 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
689 vneg_inbuf.SecurityMode = 0;
692 if (strcmp(tcon->ses->server->vals->version_string,
693 SMB3ANY_VERSION_STRING) == 0) {
694 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
695 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
696 vneg_inbuf.DialectCount = cpu_to_le16(2);
697 /* structure is big enough for 3 dialects, sending only 2 */
698 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
699 } else if (strcmp(tcon->ses->server->vals->version_string,
700 SMBDEFAULT_VERSION_STRING) == 0) {
701 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
702 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
703 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
704 vneg_inbuf.DialectCount = cpu_to_le16(3);
705 /* structure is big enough for 3 dialects */
706 inbuflen = sizeof(struct validate_negotiate_info_req);
708 /* otherwise specific dialect was requested */
709 vneg_inbuf.Dialects[0] =
710 cpu_to_le16(tcon->ses->server->vals->protocol_id);
711 vneg_inbuf.DialectCount = cpu_to_le16(1);
712 /* structure is big enough for 3 dialects, sending only 1 */
713 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
716 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
717 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
719 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
720 (char **)&pneg_rsp, &rsplen);
723 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
727 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
728 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
731 /* relax check since Mac returns max bufsize allowed on ioctl */
732 if ((rsplen > CIFSMaxBufSize)
733 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
737 /* check validate negotiate info response matches what we got earlier */
738 if (pneg_rsp->Dialect !=
739 cpu_to_le16(tcon->ses->server->vals->protocol_id))
742 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
745 /* do not validate server guid because not saved at negprot time yet */
747 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
748 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
751 /* validate negotiate successful */
752 cifs_dbg(FYI, "validate negotiate info successful\n");
757 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
764 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
773 if (server->sec_ntlmssp &&
774 (global_secflags & CIFSSEC_MAY_NTLMSSP))
776 if ((server->sec_kerberos || server->sec_mskerberos) &&
777 (global_secflags & CIFSSEC_MAY_KRB5))
785 struct SMB2_sess_data {
787 struct cifs_ses *ses;
788 struct nls_table *nls_cp;
789 void (*func)(struct SMB2_sess_data *);
791 u64 previous_session;
793 /* we will send the SMB in three pieces:
794 * a fixed length beginning part, an optional
795 * SPNEGO blob (which can be zero length), and a
796 * last part which will include the strings
797 * and rest of bcc area. This allows us to avoid
798 * a large buffer 17K allocation
805 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
808 struct cifs_ses *ses = sess_data->ses;
809 struct smb2_sess_setup_req *req;
810 struct TCP_Server_Info *server = ses->server;
811 unsigned int total_len;
813 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
818 /* First session, not a reauthenticate */
819 req->sync_hdr.SessionId = 0;
821 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
822 req->PreviousSessionId = sess_data->previous_session;
824 req->Flags = 0; /* MBZ */
825 /* to enable echos and oplocks */
826 req->sync_hdr.CreditRequest = cpu_to_le16(3);
828 /* only one of SMB2 signing flags may be set in SMB2 request */
830 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
831 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
832 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
834 req->SecurityMode = 0;
836 req->Capabilities = 0;
837 req->Channel = 0; /* MBZ */
839 sess_data->iov[0].iov_base = (char *)req;
841 sess_data->iov[0].iov_len = total_len - 1;
843 * This variable will be used to clear the buffer
844 * allocated above in case of any error in the calling function.
846 sess_data->buf0_type = CIFS_SMALL_BUFFER;
852 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
854 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
855 sess_data->buf0_type = CIFS_NO_BUFFER;
859 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
862 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
863 struct kvec rsp_iov = { NULL, 0 };
865 /* Testing shows that buffer offset must be at location of Buffer[0] */
866 req->SecurityBufferOffset =
867 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
868 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
870 /* BB add code to build os and lm fields */
872 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
874 &sess_data->buf0_type,
875 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
876 cifs_small_buf_release(sess_data->iov[0].iov_base);
877 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
883 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
886 struct cifs_ses *ses = sess_data->ses;
888 mutex_lock(&ses->server->srv_mutex);
889 if (ses->server->ops->generate_signingkey) {
890 rc = ses->server->ops->generate_signingkey(ses);
893 "SMB3 session key generation failed\n");
894 mutex_unlock(&ses->server->srv_mutex);
898 if (!ses->server->session_estab) {
899 ses->server->sequence_number = 0x2;
900 ses->server->session_estab = true;
902 mutex_unlock(&ses->server->srv_mutex);
904 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
905 spin_lock(&GlobalMid_Lock);
906 ses->status = CifsGood;
907 ses->need_reconnect = false;
908 spin_unlock(&GlobalMid_Lock);
912 #ifdef CONFIG_CIFS_UPCALL
914 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
917 struct cifs_ses *ses = sess_data->ses;
918 struct cifs_spnego_msg *msg;
919 struct key *spnego_key = NULL;
920 struct smb2_sess_setup_rsp *rsp = NULL;
922 rc = SMB2_sess_alloc_buffer(sess_data);
926 spnego_key = cifs_get_spnego_key(ses);
927 if (IS_ERR(spnego_key)) {
928 rc = PTR_ERR(spnego_key);
933 msg = spnego_key->payload.data[0];
935 * check version field to make sure that cifs.upcall is
936 * sending us a response in an expected form
938 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
940 "bad cifs.upcall version. Expected %d got %d",
941 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
943 goto out_put_spnego_key;
946 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
948 if (!ses->auth_key.response) {
950 "Kerberos can't allocate (%u bytes) memory",
953 goto out_put_spnego_key;
955 ses->auth_key.len = msg->sesskey_len;
957 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
958 sess_data->iov[1].iov_len = msg->secblob_len;
960 rc = SMB2_sess_sendreceive(sess_data);
962 goto out_put_spnego_key;
964 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
965 ses->Suid = rsp->hdr.sync_hdr.SessionId;
967 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
969 rc = SMB2_sess_establish_session(sess_data);
971 key_invalidate(spnego_key);
974 sess_data->result = rc;
975 sess_data->func = NULL;
976 SMB2_sess_free_buffer(sess_data);
980 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
982 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
983 sess_data->result = -EOPNOTSUPP;
984 sess_data->func = NULL;
989 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
992 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
995 struct cifs_ses *ses = sess_data->ses;
996 struct smb2_sess_setup_rsp *rsp = NULL;
997 char *ntlmssp_blob = NULL;
998 bool use_spnego = false; /* else use raw ntlmssp */
1002 * If memory allocation is successful, caller of this function
1005 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1006 if (!ses->ntlmssp) {
1010 ses->ntlmssp->sesskey_per_smbsess = true;
1012 rc = SMB2_sess_alloc_buffer(sess_data);
1016 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1018 if (ntlmssp_blob == NULL) {
1023 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1025 /* BB eventually need to add this */
1026 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1030 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1031 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1033 sess_data->iov[1].iov_base = ntlmssp_blob;
1034 sess_data->iov[1].iov_len = blob_length;
1036 rc = SMB2_sess_sendreceive(sess_data);
1037 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1039 /* If true, rc here is expected and not an error */
1040 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1041 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1047 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1048 le16_to_cpu(rsp->SecurityBufferOffset)) {
1049 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1050 le16_to_cpu(rsp->SecurityBufferOffset));
1054 rc = decode_ntlmssp_challenge(rsp->Buffer,
1055 le16_to_cpu(rsp->SecurityBufferLength), ses);
1059 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1062 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1063 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1066 kfree(ntlmssp_blob);
1067 SMB2_sess_free_buffer(sess_data);
1069 sess_data->result = 0;
1070 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1074 kfree(ses->ntlmssp);
1075 ses->ntlmssp = NULL;
1076 sess_data->result = rc;
1077 sess_data->func = NULL;
1081 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1084 struct cifs_ses *ses = sess_data->ses;
1085 struct smb2_sess_setup_req *req;
1086 struct smb2_sess_setup_rsp *rsp = NULL;
1087 unsigned char *ntlmssp_blob = NULL;
1088 bool use_spnego = false; /* else use raw ntlmssp */
1089 u16 blob_length = 0;
1091 rc = SMB2_sess_alloc_buffer(sess_data);
1095 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1096 req->sync_hdr.SessionId = ses->Suid;
1098 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1101 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1106 /* BB eventually need to add this */
1107 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1111 sess_data->iov[1].iov_base = ntlmssp_blob;
1112 sess_data->iov[1].iov_len = blob_length;
1114 rc = SMB2_sess_sendreceive(sess_data);
1118 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1120 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1121 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1123 rc = SMB2_sess_establish_session(sess_data);
1125 kfree(ntlmssp_blob);
1126 SMB2_sess_free_buffer(sess_data);
1127 kfree(ses->ntlmssp);
1128 ses->ntlmssp = NULL;
1129 sess_data->result = rc;
1130 sess_data->func = NULL;
1134 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1138 type = smb2_select_sectype(ses->server, ses->sectype);
1139 cifs_dbg(FYI, "sess setup type %d\n", type);
1140 if (type == Unspecified) {
1142 "Unable to select appropriate authentication method!");
1148 sess_data->func = SMB2_auth_kerberos;
1151 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1154 cifs_dbg(VFS, "secType %d not supported!\n", type);
1162 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1163 const struct nls_table *nls_cp)
1166 struct TCP_Server_Info *server = ses->server;
1167 struct SMB2_sess_data *sess_data;
1169 cifs_dbg(FYI, "Session Setup\n");
1172 WARN(1, "%s: server is NULL!\n", __func__);
1176 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1180 rc = SMB2_select_sec(ses, sess_data);
1183 sess_data->xid = xid;
1184 sess_data->ses = ses;
1185 sess_data->buf0_type = CIFS_NO_BUFFER;
1186 sess_data->nls_cp = (struct nls_table *) nls_cp;
1188 while (sess_data->func)
1189 sess_data->func(sess_data);
1191 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1192 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1193 rc = sess_data->result;
1200 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1202 struct smb2_logoff_req *req; /* response is also trivial struct */
1204 struct TCP_Server_Info *server;
1206 unsigned int total_len;
1208 struct kvec rsp_iov;
1211 cifs_dbg(FYI, "disconnect session %p\n", ses);
1213 if (ses && (ses->server))
1214 server = ses->server;
1218 /* no need to send SMB logoff if uid already closed due to reconnect */
1219 if (ses->need_reconnect)
1220 goto smb2_session_already_dead;
1222 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1226 /* since no tcon, smb2_init can not do this, so do here */
1227 req->sync_hdr.SessionId = ses->Suid;
1229 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1230 flags |= CIFS_TRANSFORM_REQ;
1231 else if (server->sign)
1232 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1234 flags |= CIFS_NO_RESP;
1236 iov[0].iov_base = (char *)req;
1237 iov[0].iov_len = total_len;
1239 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1240 cifs_small_buf_release(req);
1242 * No tcon so can't do
1243 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1246 smb2_session_already_dead:
1250 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1252 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1255 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1257 /* These are similar values to what Windows uses */
1258 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1260 tcon->max_chunks = 256;
1261 tcon->max_bytes_chunk = 1048576;
1262 tcon->max_bytes_copy = 16777216;
1266 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1267 struct cifs_tcon *tcon, const struct nls_table *cp)
1269 struct smb2_tree_connect_req *req;
1270 struct smb2_tree_connect_rsp *rsp = NULL;
1272 struct kvec rsp_iov = { NULL, 0 };
1276 __le16 *unc_path = NULL;
1278 unsigned int total_len;
1280 cifs_dbg(FYI, "TCON\n");
1282 if (!(ses->server) || !tree)
1285 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1286 if (unc_path == NULL)
1289 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1291 if (unc_path_len < 2) {
1296 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1300 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1308 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1309 flags |= CIFS_TRANSFORM_REQ;
1311 /* since no tcon, smb2_init can not do this, so do here */
1312 req->sync_hdr.SessionId = ses->Suid;
1313 if (ses->server->sign)
1314 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1315 } else if (encryption_required(tcon))
1316 flags |= CIFS_TRANSFORM_REQ;
1318 iov[0].iov_base = (char *)req;
1320 iov[0].iov_len = total_len - 1;
1322 /* Testing shows that buffer offset must be at location of Buffer[0] */
1323 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1325 req->PathLength = cpu_to_le16(unc_path_len - 2);
1326 iov[1].iov_base = unc_path;
1327 iov[1].iov_len = unc_path_len;
1329 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1330 cifs_small_buf_release(req);
1331 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1335 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1336 tcon->need_reconnect = true;
1338 goto tcon_error_exit;
1342 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1346 switch (rsp->ShareType) {
1347 case SMB2_SHARE_TYPE_DISK:
1348 cifs_dbg(FYI, "connection to disk share\n");
1350 case SMB2_SHARE_TYPE_PIPE:
1352 cifs_dbg(FYI, "connection to pipe share\n");
1354 case SMB2_SHARE_TYPE_PRINT:
1356 cifs_dbg(FYI, "connection to printer\n");
1359 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1361 goto tcon_error_exit;
1364 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1365 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1366 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1367 tcon->tidStatus = CifsGood;
1368 tcon->need_reconnect = false;
1369 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1370 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1372 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1373 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1374 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1377 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1378 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1380 init_copy_chunk_defaults(tcon);
1381 if (tcon->ses->server->ops->validate_negotiate)
1382 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1384 free_rsp_buf(resp_buftype, rsp);
1389 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1390 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1396 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1398 struct smb2_tree_disconnect_req *req; /* response is trivial */
1400 struct cifs_ses *ses = tcon->ses;
1402 unsigned int total_len;
1404 struct kvec rsp_iov;
1407 cifs_dbg(FYI, "Tree Disconnect\n");
1409 if (!ses || !(ses->server))
1412 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1415 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1420 if (encryption_required(tcon))
1421 flags |= CIFS_TRANSFORM_REQ;
1423 flags |= CIFS_NO_RESP;
1425 iov[0].iov_base = (char *)req;
1426 iov[0].iov_len = total_len;
1428 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1429 cifs_small_buf_release(req);
1431 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1437 static struct create_durable *
1438 create_durable_buf(void)
1440 struct create_durable *buf;
1442 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1446 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1447 (struct create_durable, Data));
1448 buf->ccontext.DataLength = cpu_to_le32(16);
1449 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1450 (struct create_durable, Name));
1451 buf->ccontext.NameLength = cpu_to_le16(4);
1452 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1460 static struct create_durable *
1461 create_reconnect_durable_buf(struct cifs_fid *fid)
1463 struct create_durable *buf;
1465 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1469 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1470 (struct create_durable, Data));
1471 buf->ccontext.DataLength = cpu_to_le32(16);
1472 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1473 (struct create_durable, Name));
1474 buf->ccontext.NameLength = cpu_to_le16(4);
1475 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1476 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1477 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1486 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1487 unsigned int *epoch)
1490 struct create_context *cc;
1492 unsigned int remaining;
1495 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1496 remaining = le32_to_cpu(rsp->CreateContextsLength);
1497 cc = (struct create_context *)data_offset;
1498 while (remaining >= sizeof(struct create_context)) {
1499 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1500 if (le16_to_cpu(cc->NameLength) == 4 &&
1501 strncmp(name, "RqLs", 4) == 0)
1502 return server->ops->parse_lease_buf(cc, epoch);
1504 next = le32_to_cpu(cc->Next);
1508 cc = (struct create_context *)((char *)cc + next);
1515 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1516 unsigned int *num_iovec, __u8 *oplock)
1518 struct smb2_create_req *req = iov[0].iov_base;
1519 unsigned int num = *num_iovec;
1521 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1522 if (iov[num].iov_base == NULL)
1524 iov[num].iov_len = server->vals->create_lease_size;
1525 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1526 if (!req->CreateContextsOffset)
1527 req->CreateContextsOffset = cpu_to_le32(
1528 sizeof(struct smb2_create_req) +
1529 iov[num - 1].iov_len);
1530 le32_add_cpu(&req->CreateContextsLength,
1531 server->vals->create_lease_size);
1532 *num_iovec = num + 1;
1536 static struct create_durable_v2 *
1537 create_durable_v2_buf(struct cifs_fid *pfid)
1539 struct create_durable_v2 *buf;
1541 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1545 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1546 (struct create_durable_v2, dcontext));
1547 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1548 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1549 (struct create_durable_v2, Name));
1550 buf->ccontext.NameLength = cpu_to_le16(4);
1552 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1553 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1554 generate_random_uuid(buf->dcontext.CreateGuid);
1555 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1557 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1565 static struct create_durable_handle_reconnect_v2 *
1566 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1568 struct create_durable_handle_reconnect_v2 *buf;
1570 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1575 buf->ccontext.DataOffset =
1576 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1578 buf->ccontext.DataLength =
1579 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1580 buf->ccontext.NameOffset =
1581 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1583 buf->ccontext.NameLength = cpu_to_le16(4);
1585 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1586 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1587 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1588 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1590 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1599 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1600 struct cifs_open_parms *oparms)
1602 struct smb2_create_req *req = iov[0].iov_base;
1603 unsigned int num = *num_iovec;
1605 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1606 if (iov[num].iov_base == NULL)
1608 iov[num].iov_len = sizeof(struct create_durable_v2);
1609 if (!req->CreateContextsOffset)
1610 req->CreateContextsOffset =
1611 cpu_to_le32(sizeof(struct smb2_create_req) +
1613 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1614 *num_iovec = num + 1;
1619 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1620 struct cifs_open_parms *oparms)
1622 struct smb2_create_req *req = iov[0].iov_base;
1623 unsigned int num = *num_iovec;
1625 /* indicate that we don't need to relock the file */
1626 oparms->reconnect = false;
1628 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1629 if (iov[num].iov_base == NULL)
1631 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1632 if (!req->CreateContextsOffset)
1633 req->CreateContextsOffset =
1634 cpu_to_le32(sizeof(struct smb2_create_req) +
1636 le32_add_cpu(&req->CreateContextsLength,
1637 sizeof(struct create_durable_handle_reconnect_v2));
1638 *num_iovec = num + 1;
1643 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1644 struct cifs_open_parms *oparms, bool use_persistent)
1646 struct smb2_create_req *req = iov[0].iov_base;
1647 unsigned int num = *num_iovec;
1649 if (use_persistent) {
1650 if (oparms->reconnect)
1651 return add_durable_reconnect_v2_context(iov, num_iovec,
1654 return add_durable_v2_context(iov, num_iovec, oparms);
1657 if (oparms->reconnect) {
1658 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1659 /* indicate that we don't need to relock the file */
1660 oparms->reconnect = false;
1662 iov[num].iov_base = create_durable_buf();
1663 if (iov[num].iov_base == NULL)
1665 iov[num].iov_len = sizeof(struct create_durable);
1666 if (!req->CreateContextsOffset)
1667 req->CreateContextsOffset =
1668 cpu_to_le32(sizeof(struct smb2_create_req) +
1670 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1671 *num_iovec = num + 1;
1676 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1677 const char *treename, const __le16 *path)
1679 int treename_len, path_len;
1680 struct nls_table *cp;
1681 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1686 treename_len = strlen(treename);
1687 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1693 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1696 * make room for one path separator between the treename and
1699 *out_len = treename_len + 1 + path_len;
1702 * final path needs to be null-terminated UTF16 with a
1706 *out_size = roundup((*out_len+1)*2, 8);
1707 *out_path = kzalloc(*out_size, GFP_KERNEL);
1711 cp = load_nls_default();
1712 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1713 UniStrcat(*out_path, sep);
1714 UniStrcat(*out_path, path);
1721 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1722 __u8 *oplock, struct smb2_file_all_info *buf,
1723 struct smb2_err_rsp **err_buf)
1725 struct smb2_create_req *req;
1726 struct smb2_create_rsp *rsp;
1727 struct TCP_Server_Info *server;
1728 struct cifs_tcon *tcon = oparms->tcon;
1729 struct cifs_ses *ses = tcon->ses;
1731 struct kvec rsp_iov = {NULL, 0};
1734 __le16 *copy_path = NULL;
1737 unsigned int n_iov = 2;
1738 __u32 file_attributes = 0;
1739 char *dhc_buf = NULL, *lc_buf = NULL;
1741 unsigned int total_len;
1743 cifs_dbg(FYI, "create/open\n");
1745 if (ses && (ses->server))
1746 server = ses->server;
1750 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1755 if (encryption_required(tcon))
1756 flags |= CIFS_TRANSFORM_REQ;
1758 if (oparms->create_options & CREATE_OPTION_READONLY)
1759 file_attributes |= ATTR_READONLY;
1760 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1761 file_attributes |= ATTR_SYSTEM;
1763 req->ImpersonationLevel = IL_IMPERSONATION;
1764 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1765 /* File attributes ignored on open (used in create though) */
1766 req->FileAttributes = cpu_to_le32(file_attributes);
1767 req->ShareAccess = FILE_SHARE_ALL_LE;
1768 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1769 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1771 iov[0].iov_base = (char *)req;
1772 /* -1 since last byte is buf[0] which is sent below (path) */
1773 iov[0].iov_len = total_len - 1;
1775 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
1777 /* [MS-SMB2] 2.2.13 NameOffset:
1778 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1779 * the SMB2 header, the file name includes a prefix that will
1780 * be processed during DFS name normalization as specified in
1781 * section 3.3.5.9. Otherwise, the file name is relative to
1782 * the share that is identified by the TreeId in the SMB2
1785 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1788 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1789 rc = alloc_path_with_tree_prefix(©_path, ©_size,
1791 tcon->treeName, path);
1794 req->NameLength = cpu_to_le16(name_len * 2);
1795 uni_path_len = copy_size;
1798 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1799 /* MUST set path len (NameLength) to 0 opening root of share */
1800 req->NameLength = cpu_to_le16(uni_path_len - 2);
1801 if (uni_path_len % 8 != 0) {
1802 copy_size = roundup(uni_path_len, 8);
1803 copy_path = kzalloc(copy_size, GFP_KERNEL);
1806 memcpy((char *)copy_path, (const char *)path,
1808 uni_path_len = copy_size;
1813 iov[1].iov_len = uni_path_len;
1814 iov[1].iov_base = path;
1816 if (!server->oplocks)
1817 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1819 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1820 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1821 req->RequestedOplockLevel = *oplock;
1823 rc = add_lease_context(server, iov, &n_iov, oplock);
1825 cifs_small_buf_release(req);
1829 lc_buf = iov[n_iov-1].iov_base;
1832 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1833 /* need to set Next field of lease context if we request it */
1834 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1835 struct create_context *ccontext =
1836 (struct create_context *)iov[n_iov-1].iov_base;
1838 cpu_to_le32(server->vals->create_lease_size);
1841 rc = add_durable_context(iov, &n_iov, oparms,
1842 tcon->use_persistent);
1844 cifs_small_buf_release(req);
1849 dhc_buf = iov[n_iov-1].iov_base;
1852 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1854 cifs_small_buf_release(req);
1855 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1858 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1860 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1865 oparms->fid->persistent_fid = rsp->PersistentFileId;
1866 oparms->fid->volatile_fid = rsp->VolatileFileId;
1869 memcpy(buf, &rsp->CreationTime, 32);
1870 buf->AllocationSize = rsp->AllocationSize;
1871 buf->EndOfFile = rsp->EndofFile;
1872 buf->Attributes = rsp->FileAttributes;
1873 buf->NumberOfLinks = cpu_to_le32(1);
1874 buf->DeletePending = 0;
1877 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1878 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1880 *oplock = rsp->OplockLevel;
1885 free_rsp_buf(resp_buftype, rsp);
1890 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1893 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1894 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1895 char *in_data, u32 indatalen,
1896 char **out_data, u32 *plen /* returned data len */)
1898 struct smb2_ioctl_req *req;
1899 struct smb2_ioctl_rsp *rsp;
1900 struct smb2_sync_hdr *shdr;
1901 struct cifs_ses *ses;
1903 struct kvec rsp_iov;
1908 unsigned int total_len;
1910 cifs_dbg(FYI, "SMB2 IOCTL\n");
1912 if (out_data != NULL)
1915 /* zero out returned data len, in case of error */
1924 if (!ses || !(ses->server))
1927 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
1932 if (ses->ipc_tid == 0) {
1933 cifs_small_buf_release(req);
1937 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1938 req->sync_hdr.TreeId, ses->ipc_tid);
1939 req->sync_hdr.TreeId = ses->ipc_tid;
1941 if (encryption_required(tcon))
1942 flags |= CIFS_TRANSFORM_REQ;
1944 req->CtlCode = cpu_to_le32(opcode);
1945 req->PersistentFileId = persistent_fid;
1946 req->VolatileFileId = volatile_fid;
1949 req->InputCount = cpu_to_le32(indatalen);
1950 /* do not set InputOffset if no input data */
1952 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
1953 iov[1].iov_base = in_data;
1954 iov[1].iov_len = indatalen;
1959 req->OutputOffset = 0;
1960 req->OutputCount = 0; /* MBZ */
1963 * Could increase MaxOutputResponse, but that would require more
1964 * than one credit. Windows typically sets this smaller, but for some
1965 * ioctls it may be useful to allow server to send more. No point
1966 * limiting what the server can send as long as fits in one credit
1967 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1968 * (by default, note that it can be overridden to make max larger)
1969 * in responses (except for read responses which can be bigger.
1970 * We may want to bump this limit up
1972 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1975 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1979 iov[0].iov_base = (char *)req;
1982 * If no input data, the size of ioctl struct in
1983 * protocol spec still includes a 1 byte data buffer,
1984 * but if input data passed to ioctl, we do not
1985 * want to double count this, so we do not send
1986 * the dummy one byte of data in iovec[0] if sending
1987 * input data (in iovec[1]).
1991 iov[0].iov_len = total_len - 1;
1993 iov[0].iov_len = total_len;
1995 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1996 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1997 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1999 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2001 cifs_small_buf_release(req);
2002 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2004 if ((rc != 0) && (rc != -EINVAL)) {
2005 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2007 } else if (rc == -EINVAL) {
2008 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2009 (opcode != FSCTL_SRV_COPYCHUNK)) {
2010 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2015 /* check if caller wants to look at return data or just return rc */
2016 if ((plen == NULL) || (out_data == NULL))
2019 *plen = le32_to_cpu(rsp->OutputCount);
2021 /* We check for obvious errors in the output buffer length and offset */
2023 goto ioctl_exit; /* server returned no data */
2024 else if (*plen > 0xFF00) {
2025 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2031 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2032 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2033 le32_to_cpu(rsp->OutputOffset));
2039 *out_data = kmalloc(*plen, GFP_KERNEL);
2040 if (*out_data == NULL) {
2045 shdr = get_sync_hdr(rsp);
2046 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
2048 free_rsp_buf(resp_buftype, rsp);
2053 * Individual callers to ioctl worker function follow
2057 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2058 u64 persistent_fid, u64 volatile_fid)
2061 struct compress_ioctl fsctl_input;
2062 char *ret_data = NULL;
2064 fsctl_input.CompressionState =
2065 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2067 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2068 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2069 false /* use_ipc */,
2070 (char *)&fsctl_input /* data input */,
2071 2 /* in data len */, &ret_data /* out data */, NULL);
2073 cifs_dbg(FYI, "set compression rc %d\n", rc);
2079 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2080 u64 persistent_fid, u64 volatile_fid)
2082 struct smb2_close_req *req;
2083 struct smb2_close_rsp *rsp;
2084 struct cifs_ses *ses = tcon->ses;
2086 struct kvec rsp_iov;
2090 unsigned int total_len;
2092 cifs_dbg(FYI, "Close\n");
2094 if (!ses || !(ses->server))
2097 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2101 if (encryption_required(tcon))
2102 flags |= CIFS_TRANSFORM_REQ;
2104 req->PersistentFileId = persistent_fid;
2105 req->VolatileFileId = volatile_fid;
2107 iov[0].iov_base = (char *)req;
2108 iov[0].iov_len = total_len;
2110 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2111 cifs_small_buf_release(req);
2112 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2115 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2119 /* BB FIXME - decode close response, update inode for caching */
2122 free_rsp_buf(resp_buftype, rsp);
2127 validate_buf(unsigned int offset, unsigned int buffer_length,
2128 struct smb2_hdr *hdr, unsigned int min_buf_size)
2131 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2132 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2133 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2134 char *end_of_buf = begin_of_buf + buffer_length;
2137 if (buffer_length < min_buf_size) {
2138 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2139 buffer_length, min_buf_size);
2143 /* check if beyond RFC1001 maximum length */
2144 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2145 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2146 buffer_length, smb_len);
2150 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2151 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2159 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2160 * Caller must free buffer.
2163 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2164 struct smb2_hdr *hdr, unsigned int minbufsize,
2168 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2174 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2178 memcpy(data, begin_of_buf, buffer_length);
2184 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2185 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2186 u32 additional_info, size_t output_len, size_t min_len, void **data,
2189 struct smb2_query_info_req *req;
2190 struct smb2_query_info_rsp *rsp = NULL;
2192 struct kvec rsp_iov;
2195 struct cifs_ses *ses = tcon->ses;
2197 unsigned int total_len;
2199 cifs_dbg(FYI, "Query Info\n");
2201 if (!ses || !(ses->server))
2204 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2209 if (encryption_required(tcon))
2210 flags |= CIFS_TRANSFORM_REQ;
2212 req->InfoType = info_type;
2213 req->FileInfoClass = info_class;
2214 req->PersistentFileId = persistent_fid;
2215 req->VolatileFileId = volatile_fid;
2216 req->AdditionalInformation = cpu_to_le32(additional_info);
2219 * We do not use the input buffer (do not send extra byte)
2221 req->InputBufferOffset = 0;
2223 req->OutputBufferLength = cpu_to_le32(output_len);
2225 iov[0].iov_base = (char *)req;
2227 iov[0].iov_len = total_len - 1;
2229 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2230 cifs_small_buf_release(req);
2231 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2234 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2239 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2241 *data = kmalloc(*dlen, GFP_KERNEL);
2244 "Error %d allocating memory for acl\n",
2252 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2253 le32_to_cpu(rsp->OutputBufferLength),
2254 &rsp->hdr, min_len, *data);
2257 free_rsp_buf(resp_buftype, rsp);
2261 int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2262 u64 persistent_fid, u64 volatile_fid,
2263 int ea_buf_size, struct smb2_file_full_ea_info *data)
2265 return query_info(xid, tcon, persistent_fid, volatile_fid,
2266 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2268 sizeof(struct smb2_file_full_ea_info),
2273 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2274 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2276 return query_info(xid, tcon, persistent_fid, volatile_fid,
2277 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2278 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2279 sizeof(struct smb2_file_all_info), (void **)&data,
2284 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2285 u64 persistent_fid, u64 volatile_fid,
2286 void **data, u32 *plen)
2288 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2291 return query_info(xid, tcon, persistent_fid, volatile_fid,
2292 0, SMB2_O_INFO_SECURITY, additional_info,
2293 SMB2_MAX_BUFFER_SIZE,
2294 sizeof(struct smb2_file_all_info), data, plen);
2298 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2299 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2301 return query_info(xid, tcon, persistent_fid, volatile_fid,
2302 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2303 sizeof(struct smb2_file_internal_info),
2304 sizeof(struct smb2_file_internal_info),
2305 (void **)&uniqueid, NULL);
2309 * This is a no-op for now. We're not really interested in the reply, but
2310 * rather in the fact that the server sent one and that server->lstrp
2313 * FIXME: maybe we should consider checking that the reply matches request?
2316 smb2_echo_callback(struct mid_q_entry *mid)
2318 struct TCP_Server_Info *server = mid->callback_data;
2319 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2320 unsigned int credits_received = 1;
2322 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2323 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2325 DeleteMidQEntry(mid);
2326 add_credits(server, credits_received, CIFS_ECHO_OP);
2329 void smb2_reconnect_server(struct work_struct *work)
2331 struct TCP_Server_Info *server = container_of(work,
2332 struct TCP_Server_Info, reconnect.work);
2333 struct cifs_ses *ses;
2334 struct cifs_tcon *tcon, *tcon2;
2335 struct list_head tmp_list;
2336 int tcon_exist = false;
2338 int resched = false;
2341 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2342 mutex_lock(&server->reconnect_mutex);
2344 INIT_LIST_HEAD(&tmp_list);
2345 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2347 spin_lock(&cifs_tcp_ses_lock);
2348 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2349 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2350 if (tcon->need_reconnect || tcon->need_reopen_files) {
2352 list_add_tail(&tcon->rlist, &tmp_list);
2358 * Get the reference to server struct to be sure that the last call of
2359 * cifs_put_tcon() in the loop below won't release the server pointer.
2362 server->srv_count++;
2364 spin_unlock(&cifs_tcp_ses_lock);
2366 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2367 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2369 cifs_reopen_persistent_handles(tcon);
2372 list_del_init(&tcon->rlist);
2373 cifs_put_tcon(tcon);
2376 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2378 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2379 mutex_unlock(&server->reconnect_mutex);
2381 /* now we can safely release srv struct */
2383 cifs_put_tcp_session(server, 1);
2387 SMB2_echo(struct TCP_Server_Info *server)
2389 struct smb2_echo_req *req;
2392 struct smb_rqst rqst = { .rq_iov = iov,
2394 unsigned int total_len;
2395 __be32 rfc1002_marker;
2397 cifs_dbg(FYI, "In echo request\n");
2399 if (server->tcpStatus == CifsNeedNegotiate) {
2400 /* No need to send echo on newly established connections */
2401 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2405 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
2409 req->sync_hdr.CreditRequest = cpu_to_le16(1);
2412 rfc1002_marker = cpu_to_be32(total_len);
2413 iov[0].iov_base = &rfc1002_marker;
2414 iov[1].iov_len = total_len;
2415 iov[1].iov_base = (char *)req;
2417 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2418 server, CIFS_ECHO_OP);
2420 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2422 cifs_small_buf_release(req);
2427 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2430 struct smb2_flush_req *req;
2431 struct cifs_ses *ses = tcon->ses;
2433 struct kvec rsp_iov;
2437 unsigned int total_len;
2439 cifs_dbg(FYI, "Flush\n");
2441 if (!ses || !(ses->server))
2444 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
2448 if (encryption_required(tcon))
2449 flags |= CIFS_TRANSFORM_REQ;
2451 req->PersistentFileId = persistent_fid;
2452 req->VolatileFileId = volatile_fid;
2454 iov[0].iov_base = (char *)req;
2455 iov[0].iov_len = total_len;
2457 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2458 cifs_small_buf_release(req);
2461 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2463 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2468 * To form a chain of read requests, any read requests after the first should
2469 * have the end_of_chain boolean set to true.
2472 smb2_new_read_req(void **buf, unsigned int *total_len,
2473 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2477 struct smb2_read_plain_req *req = NULL;
2478 struct smb2_sync_hdr *shdr;
2480 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2484 if (io_parms->tcon->ses->server == NULL)
2485 return -ECONNABORTED;
2487 shdr = &req->sync_hdr;
2488 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2490 req->PersistentFileId = io_parms->persistent_fid;
2491 req->VolatileFileId = io_parms->volatile_fid;
2492 req->ReadChannelInfoOffset = 0; /* reserved */
2493 req->ReadChannelInfoLength = 0; /* reserved */
2494 req->Channel = 0; /* reserved */
2495 req->MinimumCount = 0;
2496 req->Length = cpu_to_le32(io_parms->length);
2497 req->Offset = cpu_to_le64(io_parms->offset);
2499 if (request_type & CHAINED_REQUEST) {
2500 if (!(request_type & END_OF_CHAIN)) {
2501 /* next 8-byte aligned request */
2502 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2503 shdr->NextCommand = cpu_to_le32(*total_len);
2504 } else /* END_OF_CHAIN */
2505 shdr->NextCommand = 0;
2506 if (request_type & RELATED_REQUEST) {
2507 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2509 * Related requests use info from previous read request
2512 shdr->SessionId = 0xFFFFFFFF;
2513 shdr->TreeId = 0xFFFFFFFF;
2514 req->PersistentFileId = 0xFFFFFFFF;
2515 req->VolatileFileId = 0xFFFFFFFF;
2518 if (remaining_bytes > io_parms->length)
2519 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2521 req->RemainingBytes = 0;
2528 smb2_readv_callback(struct mid_q_entry *mid)
2530 struct cifs_readdata *rdata = mid->callback_data;
2531 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2532 struct TCP_Server_Info *server = tcon->ses->server;
2533 struct smb2_sync_hdr *shdr =
2534 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2535 unsigned int credits_received = 1;
2536 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2538 .rq_pages = rdata->pages,
2539 .rq_npages = rdata->nr_pages,
2540 .rq_pagesz = rdata->pagesz,
2541 .rq_tailsz = rdata->tailsz };
2543 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2544 __func__, mid->mid, mid->mid_state, rdata->result,
2547 switch (mid->mid_state) {
2548 case MID_RESPONSE_RECEIVED:
2549 credits_received = le16_to_cpu(shdr->CreditRequest);
2550 /* result already set, check signature */
2551 if (server->sign && !mid->decrypted) {
2554 rc = smb2_verify_signature(&rqst, server);
2556 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2559 /* FIXME: should this be counted toward the initiating task? */
2560 task_io_account_read(rdata->got_bytes);
2561 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2563 case MID_REQUEST_SUBMITTED:
2564 case MID_RETRY_NEEDED:
2565 rdata->result = -EAGAIN;
2566 if (server->sign && rdata->got_bytes)
2567 /* reset bytes number since we can not check a sign */
2568 rdata->got_bytes = 0;
2569 /* FIXME: should this be counted toward the initiating task? */
2570 task_io_account_read(rdata->got_bytes);
2571 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2574 if (rdata->result != -ENODATA)
2575 rdata->result = -EIO;
2579 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2581 queue_work(cifsiod_wq, &rdata->work);
2582 DeleteMidQEntry(mid);
2583 add_credits(server, credits_received, 0);
2586 /* smb2_async_readv - send an async read, and set up mid to handle result */
2588 smb2_async_readv(struct cifs_readdata *rdata)
2592 struct smb2_sync_hdr *shdr;
2593 struct cifs_io_parms io_parms;
2594 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2596 struct TCP_Server_Info *server;
2597 unsigned int total_len;
2600 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2601 __func__, rdata->offset, rdata->bytes);
2603 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2604 io_parms.offset = rdata->offset;
2605 io_parms.length = rdata->bytes;
2606 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2607 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2608 io_parms.pid = rdata->pid;
2610 server = io_parms.tcon->ses->server;
2612 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2614 if (rc == -EAGAIN && rdata->credits) {
2615 /* credits was reset by reconnect */
2617 /* reduce in_flight value since we won't send the req */
2618 spin_lock(&server->req_lock);
2619 server->in_flight--;
2620 spin_unlock(&server->req_lock);
2625 if (encryption_required(io_parms.tcon))
2626 flags |= CIFS_TRANSFORM_REQ;
2628 req_len = cpu_to_be32(total_len);
2630 rdata->iov[0].iov_base = &req_len;
2631 rdata->iov[0].iov_len = sizeof(__be32);
2632 rdata->iov[1].iov_base = buf;
2633 rdata->iov[1].iov_len = total_len;
2635 shdr = (struct smb2_sync_hdr *)buf;
2637 if (rdata->credits) {
2638 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2639 SMB2_MAX_BUFFER_SIZE));
2640 shdr->CreditRequest = shdr->CreditCharge;
2641 spin_lock(&server->req_lock);
2642 server->credits += rdata->credits -
2643 le16_to_cpu(shdr->CreditCharge);
2644 spin_unlock(&server->req_lock);
2645 wake_up(&server->request_q);
2646 flags |= CIFS_HAS_CREDITS;
2649 kref_get(&rdata->refcount);
2650 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2651 cifs_readv_receive, smb2_readv_callback,
2652 smb3_handle_read_data, rdata, flags);
2654 kref_put(&rdata->refcount, cifs_readdata_release);
2655 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2658 cifs_small_buf_release(buf);
2663 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2664 unsigned int *nbytes, char **buf, int *buf_type)
2666 int resp_buftype, rc = -EACCES;
2667 struct smb2_read_plain_req *req = NULL;
2668 struct smb2_read_rsp *rsp = NULL;
2669 struct smb2_sync_hdr *shdr;
2671 struct kvec rsp_iov;
2672 unsigned int total_len;
2673 struct smb_rqst rqst = { .rq_iov = iov,
2675 int flags = CIFS_LOG_ERROR;
2676 struct cifs_ses *ses = io_parms->tcon->ses;
2679 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2683 if (encryption_required(io_parms->tcon))
2684 flags |= CIFS_TRANSFORM_REQ;
2686 iov[0].iov_base = (char *)req;
2687 iov[0].iov_len = total_len;
2689 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2690 cifs_small_buf_release(req);
2692 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2695 if (rc != -ENODATA) {
2696 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2697 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2699 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2700 return rc == -ENODATA ? 0 : rc;
2703 *nbytes = le32_to_cpu(rsp->DataLength);
2704 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2705 (*nbytes > io_parms->length)) {
2706 cifs_dbg(FYI, "bad length %d for count %d\n",
2707 *nbytes, io_parms->length);
2712 shdr = get_sync_hdr(rsp);
2715 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2716 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2717 } else if (resp_buftype != CIFS_NO_BUFFER) {
2718 *buf = rsp_iov.iov_base;
2719 if (resp_buftype == CIFS_SMALL_BUFFER)
2720 *buf_type = CIFS_SMALL_BUFFER;
2721 else if (resp_buftype == CIFS_LARGE_BUFFER)
2722 *buf_type = CIFS_LARGE_BUFFER;
2728 * Check the mid_state and signature on received buffer (if any), and queue the
2729 * workqueue completion task.
2732 smb2_writev_callback(struct mid_q_entry *mid)
2734 struct cifs_writedata *wdata = mid->callback_data;
2735 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2736 unsigned int written;
2737 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2738 unsigned int credits_received = 1;
2740 switch (mid->mid_state) {
2741 case MID_RESPONSE_RECEIVED:
2742 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2743 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2744 if (wdata->result != 0)
2747 written = le32_to_cpu(rsp->DataLength);
2749 * Mask off high 16 bits when bytes written as returned
2750 * by the server is greater than bytes requested by the
2751 * client. OS/2 servers are known to set incorrect
2754 if (written > wdata->bytes)
2757 if (written < wdata->bytes)
2758 wdata->result = -ENOSPC;
2760 wdata->bytes = written;
2762 case MID_REQUEST_SUBMITTED:
2763 case MID_RETRY_NEEDED:
2764 wdata->result = -EAGAIN;
2767 wdata->result = -EIO;
2772 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2774 queue_work(cifsiod_wq, &wdata->work);
2775 DeleteMidQEntry(mid);
2776 add_credits(tcon->ses->server, credits_received, 0);
2779 /* smb2_async_writev - send an async write, and set up mid to handle result */
2781 smb2_async_writev(struct cifs_writedata *wdata,
2782 void (*release)(struct kref *kref))
2784 int rc = -EACCES, flags = 0;
2785 struct smb2_write_req *req = NULL;
2786 struct smb2_sync_hdr *shdr;
2787 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2788 struct TCP_Server_Info *server = tcon->ses->server;
2790 struct smb_rqst rqst = { };
2791 unsigned int total_len;
2792 __be32 rfc1002_marker;
2794 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
2796 if (rc == -EAGAIN && wdata->credits) {
2797 /* credits was reset by reconnect */
2799 /* reduce in_flight value since we won't send the req */
2800 spin_lock(&server->req_lock);
2801 server->in_flight--;
2802 spin_unlock(&server->req_lock);
2804 goto async_writev_out;
2807 if (encryption_required(tcon))
2808 flags |= CIFS_TRANSFORM_REQ;
2810 shdr = (struct smb2_sync_hdr *)req;
2811 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2813 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2814 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2815 req->WriteChannelInfoOffset = 0;
2816 req->WriteChannelInfoLength = 0;
2818 req->Offset = cpu_to_le64(wdata->offset);
2819 req->DataOffset = cpu_to_le16(
2820 offsetof(struct smb2_write_req, Buffer));
2821 req->RemainingBytes = 0;
2823 /* 4 for rfc1002 length field and 1 for Buffer */
2825 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2826 iov[0].iov_base = &rfc1002_marker;
2827 iov[1].iov_len = total_len - 1;
2828 iov[1].iov_base = (char *)req;
2832 rqst.rq_pages = wdata->pages;
2833 rqst.rq_npages = wdata->nr_pages;
2834 rqst.rq_pagesz = wdata->pagesz;
2835 rqst.rq_tailsz = wdata->tailsz;
2837 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2838 wdata->offset, wdata->bytes);
2840 req->Length = cpu_to_le32(wdata->bytes);
2842 if (wdata->credits) {
2843 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2844 SMB2_MAX_BUFFER_SIZE));
2845 shdr->CreditRequest = shdr->CreditCharge;
2846 spin_lock(&server->req_lock);
2847 server->credits += wdata->credits -
2848 le16_to_cpu(shdr->CreditCharge);
2849 spin_unlock(&server->req_lock);
2850 wake_up(&server->request_q);
2851 flags |= CIFS_HAS_CREDITS;
2854 kref_get(&wdata->refcount);
2855 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2859 kref_put(&wdata->refcount, release);
2860 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2864 cifs_small_buf_release(req);
2869 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2870 * The length field from io_parms must be at least 1 and indicates a number of
2871 * elements with data to write that begins with position 1 in iov array. All
2872 * data length is specified by count.
2875 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2876 unsigned int *nbytes, struct kvec *iov, int n_vec)
2879 struct smb2_write_req *req = NULL;
2880 struct smb2_write_rsp *rsp = NULL;
2882 struct kvec rsp_iov;
2884 unsigned int total_len;
2891 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2896 if (io_parms->tcon->ses->server == NULL)
2897 return -ECONNABORTED;
2899 if (encryption_required(io_parms->tcon))
2900 flags |= CIFS_TRANSFORM_REQ;
2902 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2904 req->PersistentFileId = io_parms->persistent_fid;
2905 req->VolatileFileId = io_parms->volatile_fid;
2906 req->WriteChannelInfoOffset = 0;
2907 req->WriteChannelInfoLength = 0;
2909 req->Length = cpu_to_le32(io_parms->length);
2910 req->Offset = cpu_to_le64(io_parms->offset);
2911 req->DataOffset = cpu_to_le16(
2912 offsetof(struct smb2_write_req, Buffer));
2913 req->RemainingBytes = 0;
2915 iov[0].iov_base = (char *)req;
2917 iov[0].iov_len = total_len - 1;
2919 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
2920 &resp_buftype, flags, &rsp_iov);
2921 cifs_small_buf_release(req);
2922 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2925 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2926 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2928 *nbytes = le32_to_cpu(rsp->DataLength);
2930 free_rsp_buf(resp_buftype, rsp);
2935 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2938 unsigned int entrycount = 0;
2939 unsigned int next_offset = 0;
2940 FILE_DIRECTORY_INFO *entryptr;
2942 if (bufstart == NULL)
2945 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2948 entryptr = (FILE_DIRECTORY_INFO *)
2949 ((char *)entryptr + next_offset);
2951 if ((char *)entryptr + size > end_of_buf) {
2952 cifs_dbg(VFS, "malformed search entry would overflow\n");
2956 len = le32_to_cpu(entryptr->FileNameLength);
2957 if ((char *)entryptr + len + size > end_of_buf) {
2958 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2963 *lastentry = (char *)entryptr;
2966 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2978 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2979 u64 persistent_fid, u64 volatile_fid, int index,
2980 struct cifs_search_info *srch_inf)
2982 struct smb2_query_directory_req *req;
2983 struct smb2_query_directory_rsp *rsp = NULL;
2985 struct kvec rsp_iov;
2988 int resp_buftype = CIFS_NO_BUFFER;
2989 unsigned char *bufptr;
2990 struct TCP_Server_Info *server;
2991 struct cifs_ses *ses = tcon->ses;
2992 __le16 asteriks = cpu_to_le16('*');
2994 unsigned int output_size = CIFSMaxBufSize;
2995 size_t info_buf_size;
2997 unsigned int total_len;
2999 if (ses && (ses->server))
3000 server = ses->server;
3004 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3009 if (encryption_required(tcon))
3010 flags |= CIFS_TRANSFORM_REQ;
3012 switch (srch_inf->info_level) {
3013 case SMB_FIND_FILE_DIRECTORY_INFO:
3014 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3015 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3017 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3018 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3019 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3022 cifs_dbg(VFS, "info level %u isn't supported\n",
3023 srch_inf->info_level);
3028 req->FileIndex = cpu_to_le32(index);
3029 req->PersistentFileId = persistent_fid;
3030 req->VolatileFileId = volatile_fid;
3033 bufptr = req->Buffer;
3034 memcpy(bufptr, &asteriks, len);
3036 req->FileNameOffset =
3037 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3038 req->FileNameLength = cpu_to_le16(len);
3040 * BB could be 30 bytes or so longer if we used SMB2 specific
3041 * buffer lengths, but this is safe and close enough.
3043 output_size = min_t(unsigned int, output_size, server->maxBuf);
3044 output_size = min_t(unsigned int, output_size, 2 << 15);
3045 req->OutputBufferLength = cpu_to_le32(output_size);
3047 iov[0].iov_base = (char *)req;
3049 iov[0].iov_len = total_len - 1;
3051 iov[1].iov_base = (char *)(req->Buffer);
3052 iov[1].iov_len = len;
3054 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3055 cifs_small_buf_release(req);
3056 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3059 if (rc == -ENODATA &&
3060 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
3061 srch_inf->endOfSearch = true;
3064 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3068 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3069 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3074 srch_inf->unicode = true;
3076 if (srch_inf->ntwrk_buf_start) {
3077 if (srch_inf->smallBuf)
3078 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3080 cifs_buf_release(srch_inf->ntwrk_buf_start);
3082 srch_inf->ntwrk_buf_start = (char *)rsp;
3083 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3084 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3085 /* 4 for rfc1002 length field */
3086 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3087 srch_inf->entries_in_buffer =
3088 num_entries(srch_inf->srch_entries_start, end_of_smb,
3089 &srch_inf->last_entry, info_buf_size);
3090 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3091 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3092 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3093 srch_inf->srch_entries_start, srch_inf->last_entry);
3094 if (resp_buftype == CIFS_LARGE_BUFFER)
3095 srch_inf->smallBuf = false;
3096 else if (resp_buftype == CIFS_SMALL_BUFFER)
3097 srch_inf->smallBuf = true;
3099 cifs_dbg(VFS, "illegal search buffer type\n");
3104 free_rsp_buf(resp_buftype, rsp);
3109 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3110 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3111 u8 info_type, u32 additional_info, unsigned int num,
3112 void **data, unsigned int *size)
3114 struct smb2_set_info_req *req;
3115 struct smb2_set_info_rsp *rsp = NULL;
3117 struct kvec rsp_iov;
3121 struct cifs_ses *ses = tcon->ses;
3123 unsigned int total_len;
3125 if (!ses || !(ses->server))
3131 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3135 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3141 if (encryption_required(tcon))
3142 flags |= CIFS_TRANSFORM_REQ;
3144 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3146 req->InfoType = info_type;
3147 req->FileInfoClass = info_class;
3148 req->PersistentFileId = persistent_fid;
3149 req->VolatileFileId = volatile_fid;
3150 req->AdditionalInformation = cpu_to_le32(additional_info);
3153 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3154 req->BufferLength = cpu_to_le32(*size);
3156 memcpy(req->Buffer, *data, *size);
3159 iov[0].iov_base = (char *)req;
3161 iov[0].iov_len = total_len - 1;
3163 for (i = 1; i < num; i++) {
3164 le32_add_cpu(&req->BufferLength, size[i]);
3165 iov[i].iov_base = (char *)data[i];
3166 iov[i].iov_len = size[i];
3169 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3171 cifs_small_buf_release(req);
3172 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3175 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3177 free_rsp_buf(resp_buftype, rsp);
3183 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3184 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3186 struct smb2_file_rename_info info;
3188 unsigned int size[2];
3190 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3192 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3196 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3197 /* 0 = fail if target already exists */
3198 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3199 info.FileNameLength = cpu_to_le32(len);
3202 size[0] = sizeof(struct smb2_file_rename_info);
3204 data[1] = target_file;
3205 size[1] = len + 2 /* null */;
3207 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3208 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3215 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3216 u64 persistent_fid, u64 volatile_fid)
3218 __u8 delete_pending = 1;
3222 data = &delete_pending;
3223 size = 1; /* sizeof __u8 */
3225 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3226 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3227 0, 1, &data, &size);
3231 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3232 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3234 struct smb2_file_link_info info;
3236 unsigned int size[2];
3238 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3240 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3244 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3245 /* 0 = fail if link already exists */
3246 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3247 info.FileNameLength = cpu_to_le32(len);
3250 size[0] = sizeof(struct smb2_file_link_info);
3252 data[1] = target_file;
3253 size[1] = len + 2 /* null */;
3255 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3256 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3263 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3264 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3266 struct smb2_file_eof_info info;
3270 info.EndOfFile = *eof;
3273 size = sizeof(struct smb2_file_eof_info);
3276 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3277 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3278 0, 1, &data, &size);
3280 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3281 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3282 0, 1, &data, &size);
3286 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3287 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3290 size = sizeof(FILE_BASIC_INFO);
3291 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3292 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3293 0, 1, (void **)&buf, &size);
3297 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3298 u64 persistent_fid, u64 volatile_fid,
3299 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3301 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3302 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3303 1, (void **)&pnntsd, &pacllen);
3307 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3308 u64 persistent_fid, u64 volatile_fid,
3309 struct smb2_file_full_ea_info *buf, int len)
3311 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3312 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3313 0, 1, (void **)&buf, &len);
3317 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3318 const u64 persistent_fid, const u64 volatile_fid,
3322 struct smb2_oplock_break *req = NULL;
3323 int flags = CIFS_OBREAK_OP;
3325 cifs_dbg(FYI, "SMB2_oplock_break\n");
3326 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3330 if (encryption_required(tcon))
3331 flags |= CIFS_TRANSFORM_REQ;
3333 req->VolatileFid = volatile_fid;
3334 req->PersistentFid = persistent_fid;
3335 req->OplockLevel = oplock_level;
3336 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3338 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3339 cifs_small_buf_release(req);
3342 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3343 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3350 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3351 struct kstatfs *kst)
3353 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3354 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3355 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3356 kst->f_bfree = kst->f_bavail =
3357 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3362 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3363 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3366 struct smb2_query_info_req *req;
3367 unsigned int total_len;
3369 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3371 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3374 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3379 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3380 req->FileInfoClass = level;
3381 req->PersistentFileId = persistent_fid;
3382 req->VolatileFileId = volatile_fid;
3384 req->InputBufferOffset =
3385 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
3386 req->OutputBufferLength = cpu_to_le32(
3387 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3389 iov->iov_base = (char *)req;
3390 iov->iov_len = total_len;
3395 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3396 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3398 struct smb2_query_info_rsp *rsp = NULL;
3400 struct kvec rsp_iov;
3403 struct cifs_ses *ses = tcon->ses;
3404 struct smb2_fs_full_size_info *info = NULL;
3407 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3408 sizeof(struct smb2_fs_full_size_info),
3409 persistent_fid, volatile_fid);
3413 if (encryption_required(tcon))
3414 flags |= CIFS_TRANSFORM_REQ;
3416 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3417 cifs_small_buf_release(iov.iov_base);
3419 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3422 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3424 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3425 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3426 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3427 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3428 sizeof(struct smb2_fs_full_size_info));
3430 copy_fs_info_to_kstatfs(info, fsdata);
3433 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3438 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3439 u64 persistent_fid, u64 volatile_fid, int level)
3441 struct smb2_query_info_rsp *rsp = NULL;
3443 struct kvec rsp_iov;
3445 int resp_buftype, max_len, min_len;
3446 struct cifs_ses *ses = tcon->ses;
3447 unsigned int rsp_len, offset;
3450 if (level == FS_DEVICE_INFORMATION) {
3451 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3452 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3453 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3454 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3455 min_len = MIN_FS_ATTR_INFO_SIZE;
3456 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3457 max_len = sizeof(struct smb3_fs_ss_info);
3458 min_len = sizeof(struct smb3_fs_ss_info);
3460 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3464 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3465 persistent_fid, volatile_fid);
3469 if (encryption_required(tcon))
3470 flags |= CIFS_TRANSFORM_REQ;
3472 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3473 cifs_small_buf_release(iov.iov_base);
3475 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3478 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3480 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3481 offset = le16_to_cpu(rsp->OutputBufferOffset);
3482 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3486 if (level == FS_ATTRIBUTE_INFORMATION)
3487 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3488 + (char *)&rsp->hdr, min_t(unsigned int,
3490 else if (level == FS_DEVICE_INFORMATION)
3491 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3492 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3493 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3494 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3495 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3496 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3497 tcon->perf_sector_size =
3498 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3502 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3507 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3508 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3509 const __u32 num_lock, struct smb2_lock_element *buf)
3512 struct smb2_lock_req *req = NULL;
3514 struct kvec rsp_iov;
3517 int flags = CIFS_NO_RESP;
3518 unsigned int total_len;
3520 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3522 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
3526 if (encryption_required(tcon))
3527 flags |= CIFS_TRANSFORM_REQ;
3529 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3530 req->LockCount = cpu_to_le16(num_lock);
3532 req->PersistentFileId = persist_fid;
3533 req->VolatileFileId = volatile_fid;
3535 count = num_lock * sizeof(struct smb2_lock_element);
3537 iov[0].iov_base = (char *)req;
3538 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
3539 iov[1].iov_base = (char *)buf;
3540 iov[1].iov_len = count;
3542 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3543 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3545 cifs_small_buf_release(req);
3547 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3548 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3555 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3556 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3557 const __u64 length, const __u64 offset, const __u32 lock_flags,
3560 struct smb2_lock_element lock;
3562 lock.Offset = cpu_to_le64(offset);
3563 lock.Length = cpu_to_le64(length);
3564 lock.Flags = cpu_to_le32(lock_flags);
3565 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3566 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3568 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3572 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3573 __u8 *lease_key, const __le32 lease_state)
3576 struct smb2_lease_ack *req = NULL;
3577 int flags = CIFS_OBREAK_OP;
3579 cifs_dbg(FYI, "SMB2_lease_break\n");
3580 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3584 if (encryption_required(tcon))
3585 flags |= CIFS_TRANSFORM_REQ;
3587 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3588 req->StructureSize = cpu_to_le16(36);
3589 inc_rfc1001_len(req, 12);
3591 memcpy(req->LeaseKey, lease_key, 16);
3592 req->LeaseState = lease_state;
3594 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3595 cifs_small_buf_release(req);
3598 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3599 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);