kaiser: fix perf crashes
[linux/fpc-iii.git] / fs / cifs / smb2pdu.c
blob69b610ad3fdc536c5f8befff05b01e9228850822
1 /*
2 * fs/cifs/smb2pdu.c
4 * Copyright (C) International Business Machines Corp., 2009, 2013
5 * Etersoft, 2012
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 */
31 #include <linux/fs.h>
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/pagemap.h>
37 #include <linux/xattr.h>
38 #include "smb2pdu.h"
39 #include "cifsglob.h"
40 #include "cifsacl.h"
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
45 #include "ntlmssp.h"
46 #include "smb2status.h"
47 #include "smb2glob.h"
48 #include "cifspdu.h"
49 #include "cifs_spnego.h"
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
58 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static void
82 smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
94 memset(temp, 0, 256);
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
100 hdr->ProtocolId = SMB2_PROTO_NUMBER;
101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
108 if (server->credits >= server->max_credits)
109 hdr->CreditRequest = cpu_to_le16(0);
110 else
111 hdr->CreditRequest = cpu_to_le16(
112 min_t(int, server->max_credits -
113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 hdr->CreditRequest = cpu_to_le16(2);
118 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
120 if (!tcon)
121 goto out;
123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
125 if ((tcon->ses) && (tcon->ses->server) &&
126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
127 hdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
130 hdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 hdr->SessionId = tcon->ses->Suid;
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
145 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
149 hdr->Flags |= SMB2_FLAGS_SIGNED;
150 out:
151 pdu->StructureSize2 = cpu_to_le16(parmsize);
152 return;
155 static int
156 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
158 int rc = 0;
159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
168 if (tcon == NULL)
169 return rc;
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
174 if (tcon->tidStatus == CifsExiting) {
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
185 return -ENODEV;
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
192 ses = tcon->ses;
193 server = ses->server;
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
199 while (server->tcpStatus == CifsNeedReconnect) {
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
204 switch (smb2_command) {
206 * BB Should we keep oplock break and add flush to exceptions?
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
227 if (!tcon->retry) {
228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
229 return -EHOSTDOWN;
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
236 nls_codepage = load_nls_default();
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
242 mutex_lock(&tcon->ses->session_mutex);
243 rc = cifs_negotiate_protocol(0, tcon->ses);
244 if (!rc && tcon->ses->need_reconnect)
245 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
247 if (rc || !tcon->need_reconnect) {
248 mutex_unlock(&tcon->ses->session_mutex);
249 goto out;
252 cifs_mark_open_files_invalid(tcon);
253 if (tcon->use_persistent)
254 tcon->need_reopen_files = true;
256 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
257 mutex_unlock(&tcon->ses->session_mutex);
259 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
260 if (rc)
261 goto out;
263 if (smb2_command != SMB2_INTERNAL_CMD)
264 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
266 atomic_inc(&tconInfoReconnectCount);
267 out:
269 * Check if handle based operation so we know whether we can continue
270 * or not without returning to caller to reset file handle.
273 * BB Is flush done by server on drop of tcp session? Should we special
274 * case it and skip above?
276 switch (smb2_command) {
277 case SMB2_FLUSH:
278 case SMB2_READ:
279 case SMB2_WRITE:
280 case SMB2_LOCK:
281 case SMB2_IOCTL:
282 case SMB2_QUERY_DIRECTORY:
283 case SMB2_CHANGE_NOTIFY:
284 case SMB2_QUERY_INFO:
285 case SMB2_SET_INFO:
286 rc = -EAGAIN;
288 unload_nls(nls_codepage);
289 return rc;
293 * Allocate and return pointer to an SMB request hdr, and set basic
294 * SMB information in the SMB header. If the return code is zero, this
295 * function must have filled in request_buf pointer.
297 static int
298 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
299 void **request_buf)
301 int rc = 0;
303 rc = smb2_reconnect(smb2_command, tcon);
304 if (rc)
305 return rc;
307 /* BB eventually switch this to SMB2 specific small buf size */
308 *request_buf = cifs_small_buf_get();
309 if (*request_buf == NULL) {
310 /* BB should we add a retry in here if not a writepage? */
311 return -ENOMEM;
314 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
316 if (tcon != NULL) {
317 #ifdef CONFIG_CIFS_STATS2
318 uint16_t com_code = le16_to_cpu(smb2_command);
319 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
320 #endif
321 cifs_stats_inc(&tcon->num_smbs_sent);
324 return rc;
327 #ifdef CONFIG_CIFS_SMB311
328 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
329 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
332 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
333 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
335 static void
336 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
338 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
339 pneg_ctxt->DataLength = cpu_to_le16(38);
340 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
341 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
342 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
343 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
346 static void
347 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
349 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
350 pneg_ctxt->DataLength = cpu_to_le16(6);
351 pneg_ctxt->CipherCount = cpu_to_le16(2);
352 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
353 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
356 static void
357 assemble_neg_contexts(struct smb2_negotiate_req *req)
360 /* +4 is to account for the RFC1001 len field */
361 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
363 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
364 /* Add 2 to size to round to 8 byte boundary */
365 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
366 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
367 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
368 req->NegotiateContextCount = cpu_to_le16(2);
369 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
370 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
372 #else
373 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
375 return;
377 #endif /* SMB311 */
382 * SMB2 Worker functions follow:
384 * The general structure of the worker functions is:
385 * 1) Call smb2_init (assembles SMB2 header)
386 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
387 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
388 * 4) Decode SMB2 command specific fields in the fixed length area
389 * 5) Decode variable length data area (if any for this SMB2 command type)
390 * 6) Call free smb buffer
391 * 7) return
396 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
398 struct smb2_negotiate_req *req;
399 struct smb2_negotiate_rsp *rsp;
400 struct kvec iov[1];
401 int rc = 0;
402 int resp_buftype;
403 struct TCP_Server_Info *server = ses->server;
404 int blob_offset, blob_length;
405 char *security_blob;
406 int flags = CIFS_NEG_OP;
408 cifs_dbg(FYI, "Negotiate protocol\n");
410 if (!server) {
411 WARN(1, "%s: server is NULL!\n", __func__);
412 return -EIO;
415 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
416 if (rc)
417 return rc;
419 req->hdr.SessionId = 0;
421 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
423 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
424 inc_rfc1001_len(req, 2);
426 /* only one of SMB2 signing flags may be set in SMB2 request */
427 if (ses->sign)
428 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
429 else if (global_secflags & CIFSSEC_MAY_SIGN)
430 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
431 else
432 req->SecurityMode = 0;
434 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
436 /* ClientGUID must be zero for SMB2.02 dialect */
437 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
438 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
439 else {
440 memcpy(req->ClientGUID, server->client_guid,
441 SMB2_CLIENT_GUID_SIZE);
442 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
443 assemble_neg_contexts(req);
445 iov[0].iov_base = (char *)req;
446 /* 4 for rfc1002 length field */
447 iov[0].iov_len = get_rfc1002_length(req) + 4;
449 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
451 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
453 * No tcon so can't do
454 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
456 if (rc != 0)
457 goto neg_exit;
459 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
461 /* BB we may eventually want to match the negotiated vs. requested
462 dialect, even though we are only requesting one at a time */
463 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
464 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
465 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
466 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
467 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
468 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
469 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
471 #ifdef CONFIG_CIFS_SMB311
472 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
473 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
474 #endif /* SMB311 */
475 else {
476 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
477 le16_to_cpu(rsp->DialectRevision));
478 rc = -EIO;
479 goto neg_exit;
481 server->dialect = le16_to_cpu(rsp->DialectRevision);
483 /* SMB2 only has an extended negflavor */
484 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
485 /* set it to the maximum buffer size value we can send with 1 credit */
486 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
487 SMB2_MAX_BUFFER_SIZE);
488 server->max_read = le32_to_cpu(rsp->MaxReadSize);
489 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
490 /* BB Do we need to validate the SecurityMode? */
491 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
492 server->capabilities = le32_to_cpu(rsp->Capabilities);
493 /* Internal types */
494 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
496 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
497 &rsp->hdr);
499 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
500 * for us will be
501 * ses->sectype = RawNTLMSSP;
502 * but for time being this is our only auth choice so doesn't matter.
503 * We just found a server which sets blob length to zero expecting raw.
505 if (blob_length == 0)
506 cifs_dbg(FYI, "missing security blob on negprot\n");
508 rc = cifs_enable_signing(server, ses->sign);
509 if (rc)
510 goto neg_exit;
511 if (blob_length) {
512 rc = decode_negTokenInit(security_blob, blob_length, server);
513 if (rc == 1)
514 rc = 0;
515 else if (rc == 0)
516 rc = -EIO;
518 neg_exit:
519 free_rsp_buf(resp_buftype, rsp);
520 return rc;
523 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
525 int rc = 0;
526 struct validate_negotiate_info_req vneg_inbuf;
527 struct validate_negotiate_info_rsp *pneg_rsp;
528 u32 rsplen;
530 cifs_dbg(FYI, "validate negotiate\n");
533 * validation ioctl must be signed, so no point sending this if we
534 * can not sign it (ie are not known user). Even if signing is not
535 * required (enabled but not negotiated), in those cases we selectively
536 * sign just this, the first and only signed request on a connection.
537 * Having validation of negotiate info helps reduce attack vectors.
539 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
540 return 0; /* validation requires signing */
542 if (tcon->ses->user_name == NULL) {
543 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
544 return 0; /* validation requires signing */
547 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
548 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
550 vneg_inbuf.Capabilities =
551 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
552 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
553 SMB2_CLIENT_GUID_SIZE);
555 if (tcon->ses->sign)
556 vneg_inbuf.SecurityMode =
557 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
558 else if (global_secflags & CIFSSEC_MAY_SIGN)
559 vneg_inbuf.SecurityMode =
560 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
561 else
562 vneg_inbuf.SecurityMode = 0;
564 vneg_inbuf.DialectCount = cpu_to_le16(1);
565 vneg_inbuf.Dialects[0] =
566 cpu_to_le16(tcon->ses->server->vals->protocol_id);
568 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
569 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
570 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
571 (char **)&pneg_rsp, &rsplen);
573 if (rc != 0) {
574 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
575 return -EIO;
578 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
579 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
580 rsplen);
582 /* relax check since Mac returns max bufsize allowed on ioctl */
583 if (rsplen > CIFSMaxBufSize)
584 return -EIO;
587 /* check validate negotiate info response matches what we got earlier */
588 if (pneg_rsp->Dialect !=
589 cpu_to_le16(tcon->ses->server->vals->protocol_id))
590 goto vneg_out;
592 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
593 goto vneg_out;
595 /* do not validate server guid because not saved at negprot time yet */
597 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
598 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
599 goto vneg_out;
601 /* validate negotiate successful */
602 cifs_dbg(FYI, "validate negotiate info successful\n");
603 return 0;
605 vneg_out:
606 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
607 return -EIO;
610 struct SMB2_sess_data {
611 unsigned int xid;
612 struct cifs_ses *ses;
613 struct nls_table *nls_cp;
614 void (*func)(struct SMB2_sess_data *);
615 int result;
616 u64 previous_session;
618 /* we will send the SMB in three pieces:
619 * a fixed length beginning part, an optional
620 * SPNEGO blob (which can be zero length), and a
621 * last part which will include the strings
622 * and rest of bcc area. This allows us to avoid
623 * a large buffer 17K allocation
625 int buf0_type;
626 struct kvec iov[2];
629 static int
630 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
632 int rc;
633 struct cifs_ses *ses = sess_data->ses;
634 struct smb2_sess_setup_req *req;
635 struct TCP_Server_Info *server = ses->server;
637 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
638 if (rc)
639 return rc;
641 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
643 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
644 req->PreviousSessionId = sess_data->previous_session;
646 req->Flags = 0; /* MBZ */
647 /* to enable echos and oplocks */
648 req->hdr.CreditRequest = cpu_to_le16(3);
650 /* only one of SMB2 signing flags may be set in SMB2 request */
651 if (server->sign)
652 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
653 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
654 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
655 else
656 req->SecurityMode = 0;
658 req->Capabilities = 0;
659 req->Channel = 0; /* MBZ */
661 sess_data->iov[0].iov_base = (char *)req;
662 /* 4 for rfc1002 length field and 1 for pad */
663 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
665 * This variable will be used to clear the buffer
666 * allocated above in case of any error in the calling function.
668 sess_data->buf0_type = CIFS_SMALL_BUFFER;
670 return 0;
673 static void
674 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
676 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
677 sess_data->buf0_type = CIFS_NO_BUFFER;
680 static int
681 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
683 int rc;
684 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
686 /* Testing shows that buffer offset must be at location of Buffer[0] */
687 req->SecurityBufferOffset =
688 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
689 1 /* pad */ - 4 /* rfc1001 len */);
690 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
692 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
694 /* BB add code to build os and lm fields */
696 rc = SendReceive2(sess_data->xid, sess_data->ses,
697 sess_data->iov, 2,
698 &sess_data->buf0_type,
699 CIFS_LOG_ERROR | CIFS_NEG_OP);
701 return rc;
704 static int
705 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
707 int rc = 0;
708 struct cifs_ses *ses = sess_data->ses;
710 mutex_lock(&ses->server->srv_mutex);
711 if (ses->server->sign && ses->server->ops->generate_signingkey) {
712 rc = ses->server->ops->generate_signingkey(ses);
713 kfree(ses->auth_key.response);
714 ses->auth_key.response = NULL;
715 if (rc) {
716 cifs_dbg(FYI,
717 "SMB3 session key generation failed\n");
718 mutex_unlock(&ses->server->srv_mutex);
719 goto keygen_exit;
722 if (!ses->server->session_estab) {
723 ses->server->sequence_number = 0x2;
724 ses->server->session_estab = true;
726 mutex_unlock(&ses->server->srv_mutex);
728 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
729 spin_lock(&GlobalMid_Lock);
730 ses->status = CifsGood;
731 ses->need_reconnect = false;
732 spin_unlock(&GlobalMid_Lock);
734 keygen_exit:
735 if (!ses->server->sign) {
736 kfree(ses->auth_key.response);
737 ses->auth_key.response = NULL;
739 return rc;
742 #ifdef CONFIG_CIFS_UPCALL
743 static void
744 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
746 int rc;
747 struct cifs_ses *ses = sess_data->ses;
748 struct cifs_spnego_msg *msg;
749 struct key *spnego_key = NULL;
750 struct smb2_sess_setup_rsp *rsp = NULL;
752 rc = SMB2_sess_alloc_buffer(sess_data);
753 if (rc)
754 goto out;
756 spnego_key = cifs_get_spnego_key(ses);
757 if (IS_ERR(spnego_key)) {
758 rc = PTR_ERR(spnego_key);
759 spnego_key = NULL;
760 goto out;
763 msg = spnego_key->payload.data[0];
765 * check version field to make sure that cifs.upcall is
766 * sending us a response in an expected form
768 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
769 cifs_dbg(VFS,
770 "bad cifs.upcall version. Expected %d got %d",
771 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
772 rc = -EKEYREJECTED;
773 goto out_put_spnego_key;
776 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
777 GFP_KERNEL);
778 if (!ses->auth_key.response) {
779 cifs_dbg(VFS,
780 "Kerberos can't allocate (%u bytes) memory",
781 msg->sesskey_len);
782 rc = -ENOMEM;
783 goto out_put_spnego_key;
785 ses->auth_key.len = msg->sesskey_len;
787 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
788 sess_data->iov[1].iov_len = msg->secblob_len;
790 rc = SMB2_sess_sendreceive(sess_data);
791 if (rc)
792 goto out_put_spnego_key;
794 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
795 ses->Suid = rsp->hdr.SessionId;
797 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
798 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
799 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
801 rc = SMB2_sess_establish_session(sess_data);
802 out_put_spnego_key:
803 key_invalidate(spnego_key);
804 key_put(spnego_key);
805 out:
806 sess_data->result = rc;
807 sess_data->func = NULL;
808 SMB2_sess_free_buffer(sess_data);
810 #else
811 static void
812 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
814 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
815 sess_data->result = -EOPNOTSUPP;
816 sess_data->func = NULL;
818 #endif
820 static void
821 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
823 static void
824 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
826 int rc;
827 struct cifs_ses *ses = sess_data->ses;
828 struct smb2_sess_setup_rsp *rsp = NULL;
829 char *ntlmssp_blob = NULL;
830 bool use_spnego = false; /* else use raw ntlmssp */
831 u16 blob_length = 0;
834 * If memory allocation is successful, caller of this function
835 * frees it.
837 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
838 if (!ses->ntlmssp) {
839 rc = -ENOMEM;
840 goto out_err;
842 ses->ntlmssp->sesskey_per_smbsess = true;
844 rc = SMB2_sess_alloc_buffer(sess_data);
845 if (rc)
846 goto out_err;
848 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
849 GFP_KERNEL);
850 if (ntlmssp_blob == NULL) {
851 rc = -ENOMEM;
852 goto out;
855 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
856 if (use_spnego) {
857 /* BB eventually need to add this */
858 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
859 rc = -EOPNOTSUPP;
860 goto out;
861 } else {
862 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
863 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
865 sess_data->iov[1].iov_base = ntlmssp_blob;
866 sess_data->iov[1].iov_len = blob_length;
868 rc = SMB2_sess_sendreceive(sess_data);
869 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
871 /* If true, rc here is expected and not an error */
872 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
873 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
874 rc = 0;
876 if (rc)
877 goto out;
879 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
880 le16_to_cpu(rsp->SecurityBufferOffset)) {
881 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
882 le16_to_cpu(rsp->SecurityBufferOffset));
883 rc = -EIO;
884 goto out;
886 rc = decode_ntlmssp_challenge(rsp->Buffer,
887 le16_to_cpu(rsp->SecurityBufferLength), ses);
888 if (rc)
889 goto out;
891 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
894 ses->Suid = rsp->hdr.SessionId;
895 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
896 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
897 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
899 out:
900 kfree(ntlmssp_blob);
901 SMB2_sess_free_buffer(sess_data);
902 if (!rc) {
903 sess_data->result = 0;
904 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
905 return;
907 out_err:
908 kfree(ses->ntlmssp);
909 ses->ntlmssp = NULL;
910 sess_data->result = rc;
911 sess_data->func = NULL;
914 static void
915 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
917 int rc;
918 struct cifs_ses *ses = sess_data->ses;
919 struct smb2_sess_setup_req *req;
920 struct smb2_sess_setup_rsp *rsp = NULL;
921 unsigned char *ntlmssp_blob = NULL;
922 bool use_spnego = false; /* else use raw ntlmssp */
923 u16 blob_length = 0;
925 rc = SMB2_sess_alloc_buffer(sess_data);
926 if (rc)
927 goto out;
929 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
930 req->hdr.SessionId = ses->Suid;
932 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
933 sess_data->nls_cp);
934 if (rc) {
935 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
936 goto out;
939 if (use_spnego) {
940 /* BB eventually need to add this */
941 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
942 rc = -EOPNOTSUPP;
943 goto out;
945 sess_data->iov[1].iov_base = ntlmssp_blob;
946 sess_data->iov[1].iov_len = blob_length;
948 rc = SMB2_sess_sendreceive(sess_data);
949 if (rc)
950 goto out;
952 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
954 ses->Suid = rsp->hdr.SessionId;
955 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
956 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
957 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
959 rc = SMB2_sess_establish_session(sess_data);
960 out:
961 kfree(ntlmssp_blob);
962 SMB2_sess_free_buffer(sess_data);
963 kfree(ses->ntlmssp);
964 ses->ntlmssp = NULL;
965 sess_data->result = rc;
966 sess_data->func = NULL;
969 static int
970 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
972 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
973 ses->sectype = RawNTLMSSP;
975 switch (ses->sectype) {
976 case Kerberos:
977 sess_data->func = SMB2_auth_kerberos;
978 break;
979 case RawNTLMSSP:
980 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
981 break;
982 default:
983 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
984 return -EOPNOTSUPP;
987 return 0;
991 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
992 const struct nls_table *nls_cp)
994 int rc = 0;
995 struct TCP_Server_Info *server = ses->server;
996 struct SMB2_sess_data *sess_data;
998 cifs_dbg(FYI, "Session Setup\n");
1000 if (!server) {
1001 WARN(1, "%s: server is NULL!\n", __func__);
1002 return -EIO;
1005 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1006 if (!sess_data)
1007 return -ENOMEM;
1009 rc = SMB2_select_sec(ses, sess_data);
1010 if (rc)
1011 goto out;
1012 sess_data->xid = xid;
1013 sess_data->ses = ses;
1014 sess_data->buf0_type = CIFS_NO_BUFFER;
1015 sess_data->nls_cp = (struct nls_table *) nls_cp;
1017 while (sess_data->func)
1018 sess_data->func(sess_data);
1020 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1021 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1022 rc = sess_data->result;
1023 out:
1024 kfree(sess_data);
1025 return rc;
1029 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1031 struct smb2_logoff_req *req; /* response is also trivial struct */
1032 int rc = 0;
1033 struct TCP_Server_Info *server;
1035 cifs_dbg(FYI, "disconnect session %p\n", ses);
1037 if (ses && (ses->server))
1038 server = ses->server;
1039 else
1040 return -EIO;
1042 /* no need to send SMB logoff if uid already closed due to reconnect */
1043 if (ses->need_reconnect)
1044 goto smb2_session_already_dead;
1046 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1047 if (rc)
1048 return rc;
1050 /* since no tcon, smb2_init can not do this, so do here */
1051 req->hdr.SessionId = ses->Suid;
1052 if (server->sign)
1053 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1055 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1057 * No tcon so can't do
1058 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1061 smb2_session_already_dead:
1062 return rc;
1065 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1067 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1070 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1072 /* These are similar values to what Windows uses */
1073 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1075 tcon->max_chunks = 256;
1076 tcon->max_bytes_chunk = 1048576;
1077 tcon->max_bytes_copy = 16777216;
1081 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1082 struct cifs_tcon *tcon, const struct nls_table *cp)
1084 struct smb2_tree_connect_req *req;
1085 struct smb2_tree_connect_rsp *rsp = NULL;
1086 struct kvec iov[2];
1087 int rc = 0;
1088 int resp_buftype;
1089 int unc_path_len;
1090 struct TCP_Server_Info *server;
1091 __le16 *unc_path = NULL;
1093 cifs_dbg(FYI, "TCON\n");
1095 if ((ses->server) && tree)
1096 server = ses->server;
1097 else
1098 return -EIO;
1100 if ((tcon && tcon->seal) &&
1101 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1102 cifs_dbg(VFS, "encryption requested but no server support");
1103 return -EOPNOTSUPP;
1106 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1107 if (unc_path == NULL)
1108 return -ENOMEM;
1110 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1111 unc_path_len *= 2;
1112 if (unc_path_len < 2) {
1113 kfree(unc_path);
1114 return -EINVAL;
1117 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1118 if (tcon)
1119 tcon->tid = 0;
1121 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1122 if (rc) {
1123 kfree(unc_path);
1124 return rc;
1127 if (tcon == NULL) {
1128 /* since no tcon, smb2_init can not do this, so do here */
1129 req->hdr.SessionId = ses->Suid;
1130 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1131 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1134 iov[0].iov_base = (char *)req;
1135 /* 4 for rfc1002 length field and 1 for pad */
1136 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1138 /* Testing shows that buffer offset must be at location of Buffer[0] */
1139 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1140 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1141 req->PathLength = cpu_to_le16(unc_path_len - 2);
1142 iov[1].iov_base = unc_path;
1143 iov[1].iov_len = unc_path_len;
1145 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1147 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1148 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1150 if (rc != 0) {
1151 if (tcon) {
1152 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1153 tcon->need_reconnect = true;
1155 goto tcon_error_exit;
1158 if (tcon == NULL) {
1159 ses->ipc_tid = rsp->hdr.TreeId;
1160 goto tcon_exit;
1163 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1164 cifs_dbg(FYI, "connection to disk share\n");
1165 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1166 tcon->ipc = true;
1167 cifs_dbg(FYI, "connection to pipe share\n");
1168 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1169 tcon->print = true;
1170 cifs_dbg(FYI, "connection to printer\n");
1171 } else {
1172 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1173 rc = -EOPNOTSUPP;
1174 goto tcon_error_exit;
1177 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1178 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1179 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1180 tcon->tidStatus = CifsGood;
1181 tcon->need_reconnect = false;
1182 tcon->tid = rsp->hdr.TreeId;
1183 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1185 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1186 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1187 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1188 init_copy_chunk_defaults(tcon);
1189 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1190 cifs_dbg(VFS, "Encrypted shares not supported");
1191 if (tcon->ses->server->ops->validate_negotiate)
1192 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1193 tcon_exit:
1194 free_rsp_buf(resp_buftype, rsp);
1195 kfree(unc_path);
1196 return rc;
1198 tcon_error_exit:
1199 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1200 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1202 goto tcon_exit;
1206 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1208 struct smb2_tree_disconnect_req *req; /* response is trivial */
1209 int rc = 0;
1210 struct TCP_Server_Info *server;
1211 struct cifs_ses *ses = tcon->ses;
1213 cifs_dbg(FYI, "Tree Disconnect\n");
1215 if (ses && (ses->server))
1216 server = ses->server;
1217 else
1218 return -EIO;
1220 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1221 return 0;
1223 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1224 if (rc)
1225 return rc;
1227 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1228 if (rc)
1229 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1231 return rc;
1235 static struct create_durable *
1236 create_durable_buf(void)
1238 struct create_durable *buf;
1240 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1241 if (!buf)
1242 return NULL;
1244 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1245 (struct create_durable, Data));
1246 buf->ccontext.DataLength = cpu_to_le32(16);
1247 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1248 (struct create_durable, Name));
1249 buf->ccontext.NameLength = cpu_to_le16(4);
1250 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1251 buf->Name[0] = 'D';
1252 buf->Name[1] = 'H';
1253 buf->Name[2] = 'n';
1254 buf->Name[3] = 'Q';
1255 return buf;
1258 static struct create_durable *
1259 create_reconnect_durable_buf(struct cifs_fid *fid)
1261 struct create_durable *buf;
1263 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1264 if (!buf)
1265 return NULL;
1267 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1268 (struct create_durable, Data));
1269 buf->ccontext.DataLength = cpu_to_le32(16);
1270 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1271 (struct create_durable, Name));
1272 buf->ccontext.NameLength = cpu_to_le16(4);
1273 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1274 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1275 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1276 buf->Name[0] = 'D';
1277 buf->Name[1] = 'H';
1278 buf->Name[2] = 'n';
1279 buf->Name[3] = 'C';
1280 return buf;
1283 static __u8
1284 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1285 unsigned int *epoch)
1287 char *data_offset;
1288 struct create_context *cc;
1289 unsigned int next;
1290 unsigned int remaining;
1291 char *name;
1293 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1294 remaining = le32_to_cpu(rsp->CreateContextsLength);
1295 cc = (struct create_context *)data_offset;
1296 while (remaining >= sizeof(struct create_context)) {
1297 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1298 if (le16_to_cpu(cc->NameLength) == 4 &&
1299 strncmp(name, "RqLs", 4) == 0)
1300 return server->ops->parse_lease_buf(cc, epoch);
1302 next = le32_to_cpu(cc->Next);
1303 if (!next)
1304 break;
1305 remaining -= next;
1306 cc = (struct create_context *)((char *)cc + next);
1309 return 0;
1312 static int
1313 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1314 unsigned int *num_iovec, __u8 *oplock)
1316 struct smb2_create_req *req = iov[0].iov_base;
1317 unsigned int num = *num_iovec;
1319 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1320 if (iov[num].iov_base == NULL)
1321 return -ENOMEM;
1322 iov[num].iov_len = server->vals->create_lease_size;
1323 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1324 if (!req->CreateContextsOffset)
1325 req->CreateContextsOffset = cpu_to_le32(
1326 sizeof(struct smb2_create_req) - 4 +
1327 iov[num - 1].iov_len);
1328 le32_add_cpu(&req->CreateContextsLength,
1329 server->vals->create_lease_size);
1330 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1331 *num_iovec = num + 1;
1332 return 0;
1335 static struct create_durable_v2 *
1336 create_durable_v2_buf(struct cifs_fid *pfid)
1338 struct create_durable_v2 *buf;
1340 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1341 if (!buf)
1342 return NULL;
1344 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1345 (struct create_durable_v2, dcontext));
1346 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1347 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1348 (struct create_durable_v2, Name));
1349 buf->ccontext.NameLength = cpu_to_le16(4);
1351 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1352 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1353 generate_random_uuid(buf->dcontext.CreateGuid);
1354 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1356 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1357 buf->Name[0] = 'D';
1358 buf->Name[1] = 'H';
1359 buf->Name[2] = '2';
1360 buf->Name[3] = 'Q';
1361 return buf;
1364 static struct create_durable_handle_reconnect_v2 *
1365 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1367 struct create_durable_handle_reconnect_v2 *buf;
1369 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1370 GFP_KERNEL);
1371 if (!buf)
1372 return NULL;
1374 buf->ccontext.DataOffset =
1375 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1376 dcontext));
1377 buf->ccontext.DataLength =
1378 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1379 buf->ccontext.NameOffset =
1380 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1381 Name));
1382 buf->ccontext.NameLength = cpu_to_le16(4);
1384 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1385 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1386 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1387 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1389 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1390 buf->Name[0] = 'D';
1391 buf->Name[1] = 'H';
1392 buf->Name[2] = '2';
1393 buf->Name[3] = 'C';
1394 return buf;
1397 static int
1398 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1399 struct cifs_open_parms *oparms)
1401 struct smb2_create_req *req = iov[0].iov_base;
1402 unsigned int num = *num_iovec;
1404 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1405 if (iov[num].iov_base == NULL)
1406 return -ENOMEM;
1407 iov[num].iov_len = sizeof(struct create_durable_v2);
1408 if (!req->CreateContextsOffset)
1409 req->CreateContextsOffset =
1410 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1411 iov[1].iov_len);
1412 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1413 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1414 *num_iovec = num + 1;
1415 return 0;
1418 static int
1419 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1420 struct cifs_open_parms *oparms)
1422 struct smb2_create_req *req = iov[0].iov_base;
1423 unsigned int num = *num_iovec;
1425 /* indicate that we don't need to relock the file */
1426 oparms->reconnect = false;
1428 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1429 if (iov[num].iov_base == NULL)
1430 return -ENOMEM;
1431 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1432 if (!req->CreateContextsOffset)
1433 req->CreateContextsOffset =
1434 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1435 iov[1].iov_len);
1436 le32_add_cpu(&req->CreateContextsLength,
1437 sizeof(struct create_durable_handle_reconnect_v2));
1438 inc_rfc1001_len(&req->hdr,
1439 sizeof(struct create_durable_handle_reconnect_v2));
1440 *num_iovec = num + 1;
1441 return 0;
1444 static int
1445 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1446 struct cifs_open_parms *oparms, bool use_persistent)
1448 struct smb2_create_req *req = iov[0].iov_base;
1449 unsigned int num = *num_iovec;
1451 if (use_persistent) {
1452 if (oparms->reconnect)
1453 return add_durable_reconnect_v2_context(iov, num_iovec,
1454 oparms);
1455 else
1456 return add_durable_v2_context(iov, num_iovec, oparms);
1459 if (oparms->reconnect) {
1460 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1461 /* indicate that we don't need to relock the file */
1462 oparms->reconnect = false;
1463 } else
1464 iov[num].iov_base = create_durable_buf();
1465 if (iov[num].iov_base == NULL)
1466 return -ENOMEM;
1467 iov[num].iov_len = sizeof(struct create_durable);
1468 if (!req->CreateContextsOffset)
1469 req->CreateContextsOffset =
1470 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1471 iov[1].iov_len);
1472 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1473 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1474 *num_iovec = num + 1;
1475 return 0;
1479 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1480 __u8 *oplock, struct smb2_file_all_info *buf,
1481 struct smb2_err_rsp **err_buf)
1483 struct smb2_create_req *req;
1484 struct smb2_create_rsp *rsp;
1485 struct TCP_Server_Info *server;
1486 struct cifs_tcon *tcon = oparms->tcon;
1487 struct cifs_ses *ses = tcon->ses;
1488 struct kvec iov[4];
1489 int resp_buftype;
1490 int uni_path_len;
1491 __le16 *copy_path = NULL;
1492 int copy_size;
1493 int rc = 0;
1494 unsigned int num_iovecs = 2;
1495 __u32 file_attributes = 0;
1496 char *dhc_buf = NULL, *lc_buf = NULL;
1498 cifs_dbg(FYI, "create/open\n");
1500 if (ses && (ses->server))
1501 server = ses->server;
1502 else
1503 return -EIO;
1505 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1506 if (rc)
1507 return rc;
1509 if (oparms->create_options & CREATE_OPTION_READONLY)
1510 file_attributes |= ATTR_READONLY;
1511 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1512 file_attributes |= ATTR_SYSTEM;
1514 req->ImpersonationLevel = IL_IMPERSONATION;
1515 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1516 /* File attributes ignored on open (used in create though) */
1517 req->FileAttributes = cpu_to_le32(file_attributes);
1518 req->ShareAccess = FILE_SHARE_ALL_LE;
1519 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1520 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1521 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1522 /* do not count rfc1001 len field */
1523 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1525 iov[0].iov_base = (char *)req;
1526 /* 4 for rfc1002 length field */
1527 iov[0].iov_len = get_rfc1002_length(req) + 4;
1529 /* MUST set path len (NameLength) to 0 opening root of share */
1530 req->NameLength = cpu_to_le16(uni_path_len - 2);
1531 /* -1 since last byte is buf[0] which is sent below (path) */
1532 iov[0].iov_len--;
1533 if (uni_path_len % 8 != 0) {
1534 copy_size = uni_path_len / 8 * 8;
1535 if (copy_size < uni_path_len)
1536 copy_size += 8;
1538 copy_path = kzalloc(copy_size, GFP_KERNEL);
1539 if (!copy_path)
1540 return -ENOMEM;
1541 memcpy((char *)copy_path, (const char *)path,
1542 uni_path_len);
1543 uni_path_len = copy_size;
1544 path = copy_path;
1547 iov[1].iov_len = uni_path_len;
1548 iov[1].iov_base = path;
1549 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1550 inc_rfc1001_len(req, uni_path_len - 1);
1552 if (!server->oplocks)
1553 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1555 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1556 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1557 req->RequestedOplockLevel = *oplock;
1558 else {
1559 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1560 if (rc) {
1561 cifs_small_buf_release(req);
1562 kfree(copy_path);
1563 return rc;
1565 lc_buf = iov[num_iovecs-1].iov_base;
1568 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1569 /* need to set Next field of lease context if we request it */
1570 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1571 struct create_context *ccontext =
1572 (struct create_context *)iov[num_iovecs-1].iov_base;
1573 ccontext->Next =
1574 cpu_to_le32(server->vals->create_lease_size);
1577 rc = add_durable_context(iov, &num_iovecs, oparms,
1578 tcon->use_persistent);
1579 if (rc) {
1580 cifs_small_buf_release(req);
1581 kfree(copy_path);
1582 kfree(lc_buf);
1583 return rc;
1585 dhc_buf = iov[num_iovecs-1].iov_base;
1588 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1589 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1591 if (rc != 0) {
1592 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1593 if (err_buf)
1594 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1595 GFP_KERNEL);
1596 goto creat_exit;
1599 oparms->fid->persistent_fid = rsp->PersistentFileId;
1600 oparms->fid->volatile_fid = rsp->VolatileFileId;
1602 if (buf) {
1603 memcpy(buf, &rsp->CreationTime, 32);
1604 buf->AllocationSize = rsp->AllocationSize;
1605 buf->EndOfFile = rsp->EndofFile;
1606 buf->Attributes = rsp->FileAttributes;
1607 buf->NumberOfLinks = cpu_to_le32(1);
1608 buf->DeletePending = 0;
1611 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1612 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1613 else
1614 *oplock = rsp->OplockLevel;
1615 creat_exit:
1616 kfree(copy_path);
1617 kfree(lc_buf);
1618 kfree(dhc_buf);
1619 free_rsp_buf(resp_buftype, rsp);
1620 return rc;
1624 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1627 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1628 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1629 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1631 struct smb2_ioctl_req *req;
1632 struct smb2_ioctl_rsp *rsp;
1633 struct TCP_Server_Info *server;
1634 struct cifs_ses *ses;
1635 struct kvec iov[2];
1636 int resp_buftype;
1637 int num_iovecs;
1638 int rc = 0;
1640 cifs_dbg(FYI, "SMB2 IOCTL\n");
1642 if (out_data != NULL)
1643 *out_data = NULL;
1645 /* zero out returned data len, in case of error */
1646 if (plen)
1647 *plen = 0;
1649 if (tcon)
1650 ses = tcon->ses;
1651 else
1652 return -EIO;
1654 if (ses && (ses->server))
1655 server = ses->server;
1656 else
1657 return -EIO;
1659 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1660 if (rc)
1661 return rc;
1663 req->CtlCode = cpu_to_le32(opcode);
1664 req->PersistentFileId = persistent_fid;
1665 req->VolatileFileId = volatile_fid;
1667 if (indatalen) {
1668 req->InputCount = cpu_to_le32(indatalen);
1669 /* do not set InputOffset if no input data */
1670 req->InputOffset =
1671 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1672 iov[1].iov_base = in_data;
1673 iov[1].iov_len = indatalen;
1674 num_iovecs = 2;
1675 } else
1676 num_iovecs = 1;
1678 req->OutputOffset = 0;
1679 req->OutputCount = 0; /* MBZ */
1682 * Could increase MaxOutputResponse, but that would require more
1683 * than one credit. Windows typically sets this smaller, but for some
1684 * ioctls it may be useful to allow server to send more. No point
1685 * limiting what the server can send as long as fits in one credit
1686 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1687 * (by default, note that it can be overridden to make max larger)
1688 * in responses (except for read responses which can be bigger.
1689 * We may want to bump this limit up
1691 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1693 if (is_fsctl)
1694 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1695 else
1696 req->Flags = 0;
1698 iov[0].iov_base = (char *)req;
1701 * If no input data, the size of ioctl struct in
1702 * protocol spec still includes a 1 byte data buffer,
1703 * but if input data passed to ioctl, we do not
1704 * want to double count this, so we do not send
1705 * the dummy one byte of data in iovec[0] if sending
1706 * input data (in iovec[1]). We also must add 4 bytes
1707 * in first iovec to allow for rfc1002 length field.
1710 if (indatalen) {
1711 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1712 inc_rfc1001_len(req, indatalen - 1);
1713 } else
1714 iov[0].iov_len = get_rfc1002_length(req) + 4;
1717 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1718 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1720 if ((rc != 0) && (rc != -EINVAL)) {
1721 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1722 goto ioctl_exit;
1723 } else if (rc == -EINVAL) {
1724 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1725 (opcode != FSCTL_SRV_COPYCHUNK)) {
1726 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1727 goto ioctl_exit;
1731 /* check if caller wants to look at return data or just return rc */
1732 if ((plen == NULL) || (out_data == NULL))
1733 goto ioctl_exit;
1735 *plen = le32_to_cpu(rsp->OutputCount);
1737 /* We check for obvious errors in the output buffer length and offset */
1738 if (*plen == 0)
1739 goto ioctl_exit; /* server returned no data */
1740 else if (*plen > 0xFF00) {
1741 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1742 *plen = 0;
1743 rc = -EIO;
1744 goto ioctl_exit;
1747 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1748 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1749 le32_to_cpu(rsp->OutputOffset));
1750 *plen = 0;
1751 rc = -EIO;
1752 goto ioctl_exit;
1755 *out_data = kmalloc(*plen, GFP_KERNEL);
1756 if (*out_data == NULL) {
1757 rc = -ENOMEM;
1758 goto ioctl_exit;
1761 memcpy(*out_data,
1762 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1763 *plen);
1764 ioctl_exit:
1765 free_rsp_buf(resp_buftype, rsp);
1766 return rc;
1770 * Individual callers to ioctl worker function follow
1774 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1775 u64 persistent_fid, u64 volatile_fid)
1777 int rc;
1778 struct compress_ioctl fsctl_input;
1779 char *ret_data = NULL;
1781 fsctl_input.CompressionState =
1782 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1784 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1785 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1786 (char *)&fsctl_input /* data input */,
1787 2 /* in data len */, &ret_data /* out data */, NULL);
1789 cifs_dbg(FYI, "set compression rc %d\n", rc);
1791 return rc;
1795 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1796 u64 persistent_fid, u64 volatile_fid)
1798 struct smb2_close_req *req;
1799 struct smb2_close_rsp *rsp;
1800 struct TCP_Server_Info *server;
1801 struct cifs_ses *ses = tcon->ses;
1802 struct kvec iov[1];
1803 int resp_buftype;
1804 int rc = 0;
1806 cifs_dbg(FYI, "Close\n");
1808 if (ses && (ses->server))
1809 server = ses->server;
1810 else
1811 return -EIO;
1813 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1814 if (rc)
1815 return rc;
1817 req->PersistentFileId = persistent_fid;
1818 req->VolatileFileId = volatile_fid;
1820 iov[0].iov_base = (char *)req;
1821 /* 4 for rfc1002 length field */
1822 iov[0].iov_len = get_rfc1002_length(req) + 4;
1824 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1825 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1827 if (rc != 0) {
1828 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1829 goto close_exit;
1832 /* BB FIXME - decode close response, update inode for caching */
1834 close_exit:
1835 free_rsp_buf(resp_buftype, rsp);
1836 return rc;
1839 static int
1840 validate_buf(unsigned int offset, unsigned int buffer_length,
1841 struct smb2_hdr *hdr, unsigned int min_buf_size)
1844 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1845 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1846 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1847 char *end_of_buf = begin_of_buf + buffer_length;
1850 if (buffer_length < min_buf_size) {
1851 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1852 buffer_length, min_buf_size);
1853 return -EINVAL;
1856 /* check if beyond RFC1001 maximum length */
1857 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1858 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1859 buffer_length, smb_len);
1860 return -EINVAL;
1863 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1864 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1865 return -EINVAL;
1868 return 0;
1872 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1873 * Caller must free buffer.
1875 static int
1876 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1877 struct smb2_hdr *hdr, unsigned int minbufsize,
1878 char *data)
1881 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1882 int rc;
1884 if (!data)
1885 return -EINVAL;
1887 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1888 if (rc)
1889 return rc;
1891 memcpy(data, begin_of_buf, buffer_length);
1893 return 0;
1896 static int
1897 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1898 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1899 size_t output_len, size_t min_len, void *data)
1901 struct smb2_query_info_req *req;
1902 struct smb2_query_info_rsp *rsp = NULL;
1903 struct kvec iov[2];
1904 int rc = 0;
1905 int resp_buftype;
1906 struct TCP_Server_Info *server;
1907 struct cifs_ses *ses = tcon->ses;
1909 cifs_dbg(FYI, "Query Info\n");
1911 if (ses && (ses->server))
1912 server = ses->server;
1913 else
1914 return -EIO;
1916 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1917 if (rc)
1918 return rc;
1920 req->InfoType = SMB2_O_INFO_FILE;
1921 req->FileInfoClass = info_class;
1922 req->PersistentFileId = persistent_fid;
1923 req->VolatileFileId = volatile_fid;
1924 /* 4 for rfc1002 length field and 1 for Buffer */
1925 req->InputBufferOffset =
1926 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1927 req->OutputBufferLength = cpu_to_le32(output_len);
1929 iov[0].iov_base = (char *)req;
1930 /* 4 for rfc1002 length field */
1931 iov[0].iov_len = get_rfc1002_length(req) + 4;
1933 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1934 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1936 if (rc) {
1937 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1938 goto qinf_exit;
1941 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1942 le32_to_cpu(rsp->OutputBufferLength),
1943 &rsp->hdr, min_len, data);
1945 qinf_exit:
1946 free_rsp_buf(resp_buftype, rsp);
1947 return rc;
1951 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1952 u64 persistent_fid, u64 volatile_fid,
1953 struct smb2_file_all_info *data)
1955 return query_info(xid, tcon, persistent_fid, volatile_fid,
1956 FILE_ALL_INFORMATION,
1957 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1958 sizeof(struct smb2_file_all_info), data);
1962 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1963 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1965 return query_info(xid, tcon, persistent_fid, volatile_fid,
1966 FILE_INTERNAL_INFORMATION,
1967 sizeof(struct smb2_file_internal_info),
1968 sizeof(struct smb2_file_internal_info), uniqueid);
1972 * This is a no-op for now. We're not really interested in the reply, but
1973 * rather in the fact that the server sent one and that server->lstrp
1974 * gets updated.
1976 * FIXME: maybe we should consider checking that the reply matches request?
1978 static void
1979 smb2_echo_callback(struct mid_q_entry *mid)
1981 struct TCP_Server_Info *server = mid->callback_data;
1982 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1983 unsigned int credits_received = 1;
1985 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1986 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1988 mutex_lock(&server->srv_mutex);
1989 DeleteMidQEntry(mid);
1990 mutex_unlock(&server->srv_mutex);
1991 add_credits(server, credits_received, CIFS_ECHO_OP);
1994 void smb2_reconnect_server(struct work_struct *work)
1996 struct TCP_Server_Info *server = container_of(work,
1997 struct TCP_Server_Info, reconnect.work);
1998 struct cifs_ses *ses;
1999 struct cifs_tcon *tcon, *tcon2;
2000 struct list_head tmp_list;
2001 int tcon_exist = false;
2002 int rc;
2003 int resched = false;
2006 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2007 mutex_lock(&server->reconnect_mutex);
2009 INIT_LIST_HEAD(&tmp_list);
2010 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2012 spin_lock(&cifs_tcp_ses_lock);
2013 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2014 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2015 if (tcon->need_reconnect || tcon->need_reopen_files) {
2016 tcon->tc_count++;
2017 list_add_tail(&tcon->rlist, &tmp_list);
2018 tcon_exist = true;
2023 * Get the reference to server struct to be sure that the last call of
2024 * cifs_put_tcon() in the loop below won't release the server pointer.
2026 if (tcon_exist)
2027 server->srv_count++;
2029 spin_unlock(&cifs_tcp_ses_lock);
2031 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2032 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2033 if (!rc)
2034 cifs_reopen_persistent_handles(tcon);
2035 else
2036 resched = true;
2037 list_del_init(&tcon->rlist);
2038 cifs_put_tcon(tcon);
2041 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2042 if (resched)
2043 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2044 mutex_unlock(&server->reconnect_mutex);
2046 /* now we can safely release srv struct */
2047 if (tcon_exist)
2048 cifs_put_tcp_session(server, 1);
2052 SMB2_echo(struct TCP_Server_Info *server)
2054 struct smb2_echo_req *req;
2055 int rc = 0;
2056 struct kvec iov;
2057 struct smb_rqst rqst = { .rq_iov = &iov,
2058 .rq_nvec = 1 };
2060 cifs_dbg(FYI, "In echo request\n");
2062 if (server->tcpStatus == CifsNeedNegotiate) {
2063 /* No need to send echo on newly established connections */
2064 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2065 return rc;
2068 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2069 if (rc)
2070 return rc;
2072 req->hdr.CreditRequest = cpu_to_le16(1);
2074 iov.iov_base = (char *)req;
2075 /* 4 for rfc1002 length field */
2076 iov.iov_len = get_rfc1002_length(req) + 4;
2078 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
2079 CIFS_ECHO_OP);
2080 if (rc)
2081 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2083 cifs_small_buf_release(req);
2084 return rc;
2088 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2089 u64 volatile_fid)
2091 struct smb2_flush_req *req;
2092 struct TCP_Server_Info *server;
2093 struct cifs_ses *ses = tcon->ses;
2094 struct kvec iov[1];
2095 int resp_buftype;
2096 int rc = 0;
2098 cifs_dbg(FYI, "Flush\n");
2100 if (ses && (ses->server))
2101 server = ses->server;
2102 else
2103 return -EIO;
2105 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2106 if (rc)
2107 return rc;
2109 req->PersistentFileId = persistent_fid;
2110 req->VolatileFileId = volatile_fid;
2112 iov[0].iov_base = (char *)req;
2113 /* 4 for rfc1002 length field */
2114 iov[0].iov_len = get_rfc1002_length(req) + 4;
2116 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2118 if (rc != 0)
2119 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2121 free_rsp_buf(resp_buftype, iov[0].iov_base);
2122 return rc;
2126 * To form a chain of read requests, any read requests after the first should
2127 * have the end_of_chain boolean set to true.
2129 static int
2130 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2131 unsigned int remaining_bytes, int request_type)
2133 int rc = -EACCES;
2134 struct smb2_read_req *req = NULL;
2136 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2137 if (rc)
2138 return rc;
2139 if (io_parms->tcon->ses->server == NULL)
2140 return -ECONNABORTED;
2142 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2144 req->PersistentFileId = io_parms->persistent_fid;
2145 req->VolatileFileId = io_parms->volatile_fid;
2146 req->ReadChannelInfoOffset = 0; /* reserved */
2147 req->ReadChannelInfoLength = 0; /* reserved */
2148 req->Channel = 0; /* reserved */
2149 req->MinimumCount = 0;
2150 req->Length = cpu_to_le32(io_parms->length);
2151 req->Offset = cpu_to_le64(io_parms->offset);
2153 if (request_type & CHAINED_REQUEST) {
2154 if (!(request_type & END_OF_CHAIN)) {
2155 /* 4 for rfc1002 length field */
2156 req->hdr.NextCommand =
2157 cpu_to_le32(get_rfc1002_length(req) + 4);
2158 } else /* END_OF_CHAIN */
2159 req->hdr.NextCommand = 0;
2160 if (request_type & RELATED_REQUEST) {
2161 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2163 * Related requests use info from previous read request
2164 * in chain.
2166 req->hdr.SessionId = 0xFFFFFFFF;
2167 req->hdr.TreeId = 0xFFFFFFFF;
2168 req->PersistentFileId = 0xFFFFFFFF;
2169 req->VolatileFileId = 0xFFFFFFFF;
2172 if (remaining_bytes > io_parms->length)
2173 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2174 else
2175 req->RemainingBytes = 0;
2177 iov[0].iov_base = (char *)req;
2178 /* 4 for rfc1002 length field */
2179 iov[0].iov_len = get_rfc1002_length(req) + 4;
2180 return rc;
2183 static void
2184 smb2_readv_callback(struct mid_q_entry *mid)
2186 struct cifs_readdata *rdata = mid->callback_data;
2187 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2188 struct TCP_Server_Info *server = tcon->ses->server;
2189 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
2190 unsigned int credits_received = 1;
2191 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2192 .rq_nvec = 1,
2193 .rq_pages = rdata->pages,
2194 .rq_npages = rdata->nr_pages,
2195 .rq_pagesz = rdata->pagesz,
2196 .rq_tailsz = rdata->tailsz };
2198 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2199 __func__, mid->mid, mid->mid_state, rdata->result,
2200 rdata->bytes);
2202 switch (mid->mid_state) {
2203 case MID_RESPONSE_RECEIVED:
2204 credits_received = le16_to_cpu(buf->CreditRequest);
2205 /* result already set, check signature */
2206 if (server->sign) {
2207 int rc;
2209 rc = smb2_verify_signature(&rqst, server);
2210 if (rc)
2211 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2212 rc);
2214 /* FIXME: should this be counted toward the initiating task? */
2215 task_io_account_read(rdata->got_bytes);
2216 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2217 break;
2218 case MID_REQUEST_SUBMITTED:
2219 case MID_RETRY_NEEDED:
2220 rdata->result = -EAGAIN;
2221 if (server->sign && rdata->got_bytes)
2222 /* reset bytes number since we can not check a sign */
2223 rdata->got_bytes = 0;
2224 /* FIXME: should this be counted toward the initiating task? */
2225 task_io_account_read(rdata->got_bytes);
2226 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2227 break;
2228 default:
2229 if (rdata->result != -ENODATA)
2230 rdata->result = -EIO;
2233 if (rdata->result)
2234 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2236 queue_work(cifsiod_wq, &rdata->work);
2237 mutex_lock(&server->srv_mutex);
2238 DeleteMidQEntry(mid);
2239 mutex_unlock(&server->srv_mutex);
2240 add_credits(server, credits_received, 0);
2243 /* smb2_async_readv - send an async write, and set up mid to handle result */
2245 smb2_async_readv(struct cifs_readdata *rdata)
2247 int rc, flags = 0;
2248 struct smb2_hdr *buf;
2249 struct cifs_io_parms io_parms;
2250 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2251 .rq_nvec = 1 };
2252 struct TCP_Server_Info *server;
2254 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2255 __func__, rdata->offset, rdata->bytes);
2257 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2258 io_parms.offset = rdata->offset;
2259 io_parms.length = rdata->bytes;
2260 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2261 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2262 io_parms.pid = rdata->pid;
2264 server = io_parms.tcon->ses->server;
2266 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2267 if (rc) {
2268 if (rc == -EAGAIN && rdata->credits) {
2269 /* credits was reset by reconnect */
2270 rdata->credits = 0;
2271 /* reduce in_flight value since we won't send the req */
2272 spin_lock(&server->req_lock);
2273 server->in_flight--;
2274 spin_unlock(&server->req_lock);
2276 return rc;
2279 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2280 /* 4 for rfc1002 length field */
2281 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2283 if (rdata->credits) {
2284 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2285 SMB2_MAX_BUFFER_SIZE));
2286 buf->CreditRequest = buf->CreditCharge;
2287 spin_lock(&server->req_lock);
2288 server->credits += rdata->credits -
2289 le16_to_cpu(buf->CreditCharge);
2290 spin_unlock(&server->req_lock);
2291 wake_up(&server->request_q);
2292 flags = CIFS_HAS_CREDITS;
2295 kref_get(&rdata->refcount);
2296 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2297 cifs_readv_receive, smb2_readv_callback,
2298 rdata, flags);
2299 if (rc) {
2300 kref_put(&rdata->refcount, cifs_readdata_release);
2301 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2304 cifs_small_buf_release(buf);
2305 return rc;
2309 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2310 unsigned int *nbytes, char **buf, int *buf_type)
2312 int resp_buftype, rc = -EACCES;
2313 struct smb2_read_rsp *rsp = NULL;
2314 struct kvec iov[1];
2316 *nbytes = 0;
2317 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2318 if (rc)
2319 return rc;
2321 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2322 &resp_buftype, CIFS_LOG_ERROR);
2324 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2326 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2327 free_rsp_buf(resp_buftype, iov[0].iov_base);
2328 return 0;
2331 if (rc) {
2332 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2333 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2334 } else {
2335 *nbytes = le32_to_cpu(rsp->DataLength);
2336 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2337 (*nbytes > io_parms->length)) {
2338 cifs_dbg(FYI, "bad length %d for count %d\n",
2339 *nbytes, io_parms->length);
2340 rc = -EIO;
2341 *nbytes = 0;
2345 if (*buf) {
2346 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
2347 *nbytes);
2348 free_rsp_buf(resp_buftype, iov[0].iov_base);
2349 } else if (resp_buftype != CIFS_NO_BUFFER) {
2350 *buf = iov[0].iov_base;
2351 if (resp_buftype == CIFS_SMALL_BUFFER)
2352 *buf_type = CIFS_SMALL_BUFFER;
2353 else if (resp_buftype == CIFS_LARGE_BUFFER)
2354 *buf_type = CIFS_LARGE_BUFFER;
2356 return rc;
2360 * Check the mid_state and signature on received buffer (if any), and queue the
2361 * workqueue completion task.
2363 static void
2364 smb2_writev_callback(struct mid_q_entry *mid)
2366 struct cifs_writedata *wdata = mid->callback_data;
2367 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2368 struct TCP_Server_Info *server = tcon->ses->server;
2369 unsigned int written;
2370 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2371 unsigned int credits_received = 1;
2373 switch (mid->mid_state) {
2374 case MID_RESPONSE_RECEIVED:
2375 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2376 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2377 if (wdata->result != 0)
2378 break;
2380 written = le32_to_cpu(rsp->DataLength);
2382 * Mask off high 16 bits when bytes written as returned
2383 * by the server is greater than bytes requested by the
2384 * client. OS/2 servers are known to set incorrect
2385 * CountHigh values.
2387 if (written > wdata->bytes)
2388 written &= 0xFFFF;
2390 if (written < wdata->bytes)
2391 wdata->result = -ENOSPC;
2392 else
2393 wdata->bytes = written;
2394 break;
2395 case MID_REQUEST_SUBMITTED:
2396 case MID_RETRY_NEEDED:
2397 wdata->result = -EAGAIN;
2398 break;
2399 default:
2400 wdata->result = -EIO;
2401 break;
2404 if (wdata->result)
2405 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2407 queue_work(cifsiod_wq, &wdata->work);
2408 mutex_lock(&server->srv_mutex);
2409 DeleteMidQEntry(mid);
2410 mutex_unlock(&server->srv_mutex);
2411 add_credits(tcon->ses->server, credits_received, 0);
2414 /* smb2_async_writev - send an async write, and set up mid to handle result */
2416 smb2_async_writev(struct cifs_writedata *wdata,
2417 void (*release)(struct kref *kref))
2419 int rc = -EACCES, flags = 0;
2420 struct smb2_write_req *req = NULL;
2421 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2422 struct TCP_Server_Info *server = tcon->ses->server;
2423 struct kvec iov;
2424 struct smb_rqst rqst;
2426 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2427 if (rc) {
2428 if (rc == -EAGAIN && wdata->credits) {
2429 /* credits was reset by reconnect */
2430 wdata->credits = 0;
2431 /* reduce in_flight value since we won't send the req */
2432 spin_lock(&server->req_lock);
2433 server->in_flight--;
2434 spin_unlock(&server->req_lock);
2436 goto async_writev_out;
2439 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2441 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2442 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2443 req->WriteChannelInfoOffset = 0;
2444 req->WriteChannelInfoLength = 0;
2445 req->Channel = 0;
2446 req->Offset = cpu_to_le64(wdata->offset);
2447 /* 4 for rfc1002 length field */
2448 req->DataOffset = cpu_to_le16(
2449 offsetof(struct smb2_write_req, Buffer) - 4);
2450 req->RemainingBytes = 0;
2452 /* 4 for rfc1002 length field and 1 for Buffer */
2453 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2454 iov.iov_base = req;
2456 rqst.rq_iov = &iov;
2457 rqst.rq_nvec = 1;
2458 rqst.rq_pages = wdata->pages;
2459 rqst.rq_npages = wdata->nr_pages;
2460 rqst.rq_pagesz = wdata->pagesz;
2461 rqst.rq_tailsz = wdata->tailsz;
2463 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2464 wdata->offset, wdata->bytes);
2466 req->Length = cpu_to_le32(wdata->bytes);
2468 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2470 if (wdata->credits) {
2471 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2472 SMB2_MAX_BUFFER_SIZE));
2473 req->hdr.CreditRequest = req->hdr.CreditCharge;
2474 spin_lock(&server->req_lock);
2475 server->credits += wdata->credits -
2476 le16_to_cpu(req->hdr.CreditCharge);
2477 spin_unlock(&server->req_lock);
2478 wake_up(&server->request_q);
2479 flags = CIFS_HAS_CREDITS;
2482 kref_get(&wdata->refcount);
2483 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2484 flags);
2486 if (rc) {
2487 kref_put(&wdata->refcount, release);
2488 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2491 async_writev_out:
2492 cifs_small_buf_release(req);
2493 return rc;
2497 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2498 * The length field from io_parms must be at least 1 and indicates a number of
2499 * elements with data to write that begins with position 1 in iov array. All
2500 * data length is specified by count.
2503 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2504 unsigned int *nbytes, struct kvec *iov, int n_vec)
2506 int rc = 0;
2507 struct smb2_write_req *req = NULL;
2508 struct smb2_write_rsp *rsp = NULL;
2509 int resp_buftype;
2510 *nbytes = 0;
2512 if (n_vec < 1)
2513 return rc;
2515 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2516 if (rc)
2517 return rc;
2519 if (io_parms->tcon->ses->server == NULL)
2520 return -ECONNABORTED;
2522 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2524 req->PersistentFileId = io_parms->persistent_fid;
2525 req->VolatileFileId = io_parms->volatile_fid;
2526 req->WriteChannelInfoOffset = 0;
2527 req->WriteChannelInfoLength = 0;
2528 req->Channel = 0;
2529 req->Length = cpu_to_le32(io_parms->length);
2530 req->Offset = cpu_to_le64(io_parms->offset);
2531 /* 4 for rfc1002 length field */
2532 req->DataOffset = cpu_to_le16(
2533 offsetof(struct smb2_write_req, Buffer) - 4);
2534 req->RemainingBytes = 0;
2536 iov[0].iov_base = (char *)req;
2537 /* 4 for rfc1002 length field and 1 for Buffer */
2538 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2540 /* length of entire message including data to be written */
2541 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2543 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2544 &resp_buftype, 0);
2545 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2547 if (rc) {
2548 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2549 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2550 } else
2551 *nbytes = le32_to_cpu(rsp->DataLength);
2553 free_rsp_buf(resp_buftype, rsp);
2554 return rc;
2557 static unsigned int
2558 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2560 int len;
2561 unsigned int entrycount = 0;
2562 unsigned int next_offset = 0;
2563 FILE_DIRECTORY_INFO *entryptr;
2565 if (bufstart == NULL)
2566 return 0;
2568 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2570 while (1) {
2571 entryptr = (FILE_DIRECTORY_INFO *)
2572 ((char *)entryptr + next_offset);
2574 if ((char *)entryptr + size > end_of_buf) {
2575 cifs_dbg(VFS, "malformed search entry would overflow\n");
2576 break;
2579 len = le32_to_cpu(entryptr->FileNameLength);
2580 if ((char *)entryptr + len + size > end_of_buf) {
2581 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2582 end_of_buf);
2583 break;
2586 *lastentry = (char *)entryptr;
2587 entrycount++;
2589 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2590 if (!next_offset)
2591 break;
2594 return entrycount;
2598 * Readdir/FindFirst
2601 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2602 u64 persistent_fid, u64 volatile_fid, int index,
2603 struct cifs_search_info *srch_inf)
2605 struct smb2_query_directory_req *req;
2606 struct smb2_query_directory_rsp *rsp = NULL;
2607 struct kvec iov[2];
2608 int rc = 0;
2609 int len;
2610 int resp_buftype = CIFS_NO_BUFFER;
2611 unsigned char *bufptr;
2612 struct TCP_Server_Info *server;
2613 struct cifs_ses *ses = tcon->ses;
2614 __le16 asteriks = cpu_to_le16('*');
2615 char *end_of_smb;
2616 unsigned int output_size = CIFSMaxBufSize;
2617 size_t info_buf_size;
2619 if (ses && (ses->server))
2620 server = ses->server;
2621 else
2622 return -EIO;
2624 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2625 if (rc)
2626 return rc;
2628 switch (srch_inf->info_level) {
2629 case SMB_FIND_FILE_DIRECTORY_INFO:
2630 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2631 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2632 break;
2633 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2634 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2635 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2636 break;
2637 default:
2638 cifs_dbg(VFS, "info level %u isn't supported\n",
2639 srch_inf->info_level);
2640 rc = -EINVAL;
2641 goto qdir_exit;
2644 req->FileIndex = cpu_to_le32(index);
2645 req->PersistentFileId = persistent_fid;
2646 req->VolatileFileId = volatile_fid;
2648 len = 0x2;
2649 bufptr = req->Buffer;
2650 memcpy(bufptr, &asteriks, len);
2652 req->FileNameOffset =
2653 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2654 req->FileNameLength = cpu_to_le16(len);
2656 * BB could be 30 bytes or so longer if we used SMB2 specific
2657 * buffer lengths, but this is safe and close enough.
2659 output_size = min_t(unsigned int, output_size, server->maxBuf);
2660 output_size = min_t(unsigned int, output_size, 2 << 15);
2661 req->OutputBufferLength = cpu_to_le32(output_size);
2663 iov[0].iov_base = (char *)req;
2664 /* 4 for RFC1001 length and 1 for Buffer */
2665 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2667 iov[1].iov_base = (char *)(req->Buffer);
2668 iov[1].iov_len = len;
2670 inc_rfc1001_len(req, len - 1 /* Buffer */);
2672 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2673 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2675 if (rc) {
2676 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2677 srch_inf->endOfSearch = true;
2678 rc = 0;
2680 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2681 goto qdir_exit;
2684 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2685 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2686 info_buf_size);
2687 if (rc)
2688 goto qdir_exit;
2690 srch_inf->unicode = true;
2692 if (srch_inf->ntwrk_buf_start) {
2693 if (srch_inf->smallBuf)
2694 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2695 else
2696 cifs_buf_release(srch_inf->ntwrk_buf_start);
2698 srch_inf->ntwrk_buf_start = (char *)rsp;
2699 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2700 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2701 /* 4 for rfc1002 length field */
2702 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2703 srch_inf->entries_in_buffer =
2704 num_entries(srch_inf->srch_entries_start, end_of_smb,
2705 &srch_inf->last_entry, info_buf_size);
2706 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2707 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2708 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2709 srch_inf->srch_entries_start, srch_inf->last_entry);
2710 if (resp_buftype == CIFS_LARGE_BUFFER)
2711 srch_inf->smallBuf = false;
2712 else if (resp_buftype == CIFS_SMALL_BUFFER)
2713 srch_inf->smallBuf = true;
2714 else
2715 cifs_dbg(VFS, "illegal search buffer type\n");
2717 return rc;
2719 qdir_exit:
2720 free_rsp_buf(resp_buftype, rsp);
2721 return rc;
2724 static int
2725 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2726 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2727 unsigned int num, void **data, unsigned int *size)
2729 struct smb2_set_info_req *req;
2730 struct smb2_set_info_rsp *rsp = NULL;
2731 struct kvec *iov;
2732 int rc = 0;
2733 int resp_buftype;
2734 unsigned int i;
2735 struct TCP_Server_Info *server;
2736 struct cifs_ses *ses = tcon->ses;
2738 if (ses && (ses->server))
2739 server = ses->server;
2740 else
2741 return -EIO;
2743 if (!num)
2744 return -EINVAL;
2746 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2747 if (!iov)
2748 return -ENOMEM;
2750 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2751 if (rc) {
2752 kfree(iov);
2753 return rc;
2756 req->hdr.ProcessId = cpu_to_le32(pid);
2758 req->InfoType = SMB2_O_INFO_FILE;
2759 req->FileInfoClass = info_class;
2760 req->PersistentFileId = persistent_fid;
2761 req->VolatileFileId = volatile_fid;
2763 /* 4 for RFC1001 length and 1 for Buffer */
2764 req->BufferOffset =
2765 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2766 req->BufferLength = cpu_to_le32(*size);
2768 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2770 memcpy(req->Buffer, *data, *size);
2772 iov[0].iov_base = (char *)req;
2773 /* 4 for RFC1001 length */
2774 iov[0].iov_len = get_rfc1002_length(req) + 4;
2776 for (i = 1; i < num; i++) {
2777 inc_rfc1001_len(req, size[i]);
2778 le32_add_cpu(&req->BufferLength, size[i]);
2779 iov[i].iov_base = (char *)data[i];
2780 iov[i].iov_len = size[i];
2783 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2784 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2786 if (rc != 0)
2787 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2789 free_rsp_buf(resp_buftype, rsp);
2790 kfree(iov);
2791 return rc;
2795 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2796 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2798 struct smb2_file_rename_info info;
2799 void **data;
2800 unsigned int size[2];
2801 int rc;
2802 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2804 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2805 if (!data)
2806 return -ENOMEM;
2808 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2809 /* 0 = fail if target already exists */
2810 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2811 info.FileNameLength = cpu_to_le32(len);
2813 data[0] = &info;
2814 size[0] = sizeof(struct smb2_file_rename_info);
2816 data[1] = target_file;
2817 size[1] = len + 2 /* null */;
2819 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2820 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2821 size);
2822 kfree(data);
2823 return rc;
2827 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2828 u64 persistent_fid, u64 volatile_fid)
2830 __u8 delete_pending = 1;
2831 void *data;
2832 unsigned int size;
2834 data = &delete_pending;
2835 size = 1; /* sizeof __u8 */
2837 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2838 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2839 &size);
2843 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2846 struct smb2_file_link_info info;
2847 void **data;
2848 unsigned int size[2];
2849 int rc;
2850 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2852 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2853 if (!data)
2854 return -ENOMEM;
2856 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2857 /* 0 = fail if link already exists */
2858 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2859 info.FileNameLength = cpu_to_le32(len);
2861 data[0] = &info;
2862 size[0] = sizeof(struct smb2_file_link_info);
2864 data[1] = target_file;
2865 size[1] = len + 2 /* null */;
2867 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2868 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2869 kfree(data);
2870 return rc;
2874 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2875 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2877 struct smb2_file_eof_info info;
2878 void *data;
2879 unsigned int size;
2881 info.EndOfFile = *eof;
2883 data = &info;
2884 size = sizeof(struct smb2_file_eof_info);
2886 if (is_falloc)
2887 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2888 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2889 else
2890 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2891 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2895 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2896 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2898 unsigned int size;
2899 size = sizeof(FILE_BASIC_INFO);
2900 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2901 current->tgid, FILE_BASIC_INFORMATION, 1,
2902 (void **)&buf, &size);
2906 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2907 const u64 persistent_fid, const u64 volatile_fid,
2908 __u8 oplock_level)
2910 int rc;
2911 struct smb2_oplock_break *req = NULL;
2913 cifs_dbg(FYI, "SMB2_oplock_break\n");
2914 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2916 if (rc)
2917 return rc;
2919 req->VolatileFid = volatile_fid;
2920 req->PersistentFid = persistent_fid;
2921 req->OplockLevel = oplock_level;
2922 req->hdr.CreditRequest = cpu_to_le16(1);
2924 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2925 /* SMB2 buffer freed by function above */
2927 if (rc) {
2928 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2929 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2932 return rc;
2935 static void
2936 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2937 struct kstatfs *kst)
2939 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2940 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2941 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2942 kst->f_bfree = kst->f_bavail =
2943 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2944 return;
2947 static int
2948 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2949 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2951 int rc;
2952 struct smb2_query_info_req *req;
2954 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2956 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2957 return -EIO;
2959 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2960 if (rc)
2961 return rc;
2963 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2964 req->FileInfoClass = level;
2965 req->PersistentFileId = persistent_fid;
2966 req->VolatileFileId = volatile_fid;
2967 /* 4 for rfc1002 length field and 1 for pad */
2968 req->InputBufferOffset =
2969 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2970 req->OutputBufferLength = cpu_to_le32(
2971 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2973 iov->iov_base = (char *)req;
2974 /* 4 for rfc1002 length field */
2975 iov->iov_len = get_rfc1002_length(req) + 4;
2976 return 0;
2980 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2981 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2983 struct smb2_query_info_rsp *rsp = NULL;
2984 struct kvec iov;
2985 int rc = 0;
2986 int resp_buftype;
2987 struct cifs_ses *ses = tcon->ses;
2988 struct smb2_fs_full_size_info *info = NULL;
2990 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2991 sizeof(struct smb2_fs_full_size_info),
2992 persistent_fid, volatile_fid);
2993 if (rc)
2994 return rc;
2996 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2997 if (rc) {
2998 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2999 goto qfsinf_exit;
3001 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3003 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3004 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3005 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3006 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3007 sizeof(struct smb2_fs_full_size_info));
3008 if (!rc)
3009 copy_fs_info_to_kstatfs(info, fsdata);
3011 qfsinf_exit:
3012 free_rsp_buf(resp_buftype, iov.iov_base);
3013 return rc;
3017 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3018 u64 persistent_fid, u64 volatile_fid, int level)
3020 struct smb2_query_info_rsp *rsp = NULL;
3021 struct kvec iov;
3022 int rc = 0;
3023 int resp_buftype, max_len, min_len;
3024 struct cifs_ses *ses = tcon->ses;
3025 unsigned int rsp_len, offset;
3027 if (level == FS_DEVICE_INFORMATION) {
3028 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3029 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3030 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3031 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3032 min_len = MIN_FS_ATTR_INFO_SIZE;
3033 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3034 max_len = sizeof(struct smb3_fs_ss_info);
3035 min_len = sizeof(struct smb3_fs_ss_info);
3036 } else {
3037 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3038 return -EINVAL;
3041 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3042 persistent_fid, volatile_fid);
3043 if (rc)
3044 return rc;
3046 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3047 if (rc) {
3048 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3049 goto qfsattr_exit;
3051 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3053 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3054 offset = le16_to_cpu(rsp->OutputBufferOffset);
3055 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3056 if (rc)
3057 goto qfsattr_exit;
3059 if (level == FS_ATTRIBUTE_INFORMATION)
3060 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3061 + (char *)&rsp->hdr, min_t(unsigned int,
3062 rsp_len, max_len));
3063 else if (level == FS_DEVICE_INFORMATION)
3064 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3065 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3066 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3067 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3068 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3069 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3070 tcon->perf_sector_size =
3071 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3074 qfsattr_exit:
3075 free_rsp_buf(resp_buftype, iov.iov_base);
3076 return rc;
3080 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3081 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3082 const __u32 num_lock, struct smb2_lock_element *buf)
3084 int rc = 0;
3085 struct smb2_lock_req *req = NULL;
3086 struct kvec iov[2];
3087 int resp_buf_type;
3088 unsigned int count;
3090 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3092 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3093 if (rc)
3094 return rc;
3096 req->hdr.ProcessId = cpu_to_le32(pid);
3097 req->LockCount = cpu_to_le16(num_lock);
3099 req->PersistentFileId = persist_fid;
3100 req->VolatileFileId = volatile_fid;
3102 count = num_lock * sizeof(struct smb2_lock_element);
3103 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3105 iov[0].iov_base = (char *)req;
3106 /* 4 for rfc1002 length field and count for all locks */
3107 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3108 iov[1].iov_base = (char *)buf;
3109 iov[1].iov_len = count;
3111 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3112 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3113 if (rc) {
3114 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3115 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3118 return rc;
3122 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3123 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3124 const __u64 length, const __u64 offset, const __u32 lock_flags,
3125 const bool wait)
3127 struct smb2_lock_element lock;
3129 lock.Offset = cpu_to_le64(offset);
3130 lock.Length = cpu_to_le64(length);
3131 lock.Flags = cpu_to_le32(lock_flags);
3132 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3133 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3135 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3139 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3140 __u8 *lease_key, const __le32 lease_state)
3142 int rc;
3143 struct smb2_lease_ack *req = NULL;
3145 cifs_dbg(FYI, "SMB2_lease_break\n");
3146 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3148 if (rc)
3149 return rc;
3151 req->hdr.CreditRequest = cpu_to_le16(1);
3152 req->StructureSize = cpu_to_le16(36);
3153 inc_rfc1001_len(req, 12);
3155 memcpy(req->LeaseKey, lease_key, 16);
3156 req->LeaseState = lease_state;
3158 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3159 /* SMB2 buffer freed by function above */
3161 if (rc) {
3162 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3163 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3166 return rc;