ip6_tunnel: better validate user provided tunnel names
[linux/fpc-iii.git] / fs / cifs / smb2pdu.c
blob44b7ccbe4b082f82f0d800555e8b890f6114cc05
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 != cpu_to_le16(tcon->ses->server->dialect))
589 goto vneg_out;
591 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
592 goto vneg_out;
594 /* do not validate server guid because not saved at negprot time yet */
596 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
597 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
598 goto vneg_out;
600 /* validate negotiate successful */
601 cifs_dbg(FYI, "validate negotiate info successful\n");
602 return 0;
604 vneg_out:
605 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
606 return -EIO;
609 struct SMB2_sess_data {
610 unsigned int xid;
611 struct cifs_ses *ses;
612 struct nls_table *nls_cp;
613 void (*func)(struct SMB2_sess_data *);
614 int result;
615 u64 previous_session;
617 /* we will send the SMB in three pieces:
618 * a fixed length beginning part, an optional
619 * SPNEGO blob (which can be zero length), and a
620 * last part which will include the strings
621 * and rest of bcc area. This allows us to avoid
622 * a large buffer 17K allocation
624 int buf0_type;
625 struct kvec iov[2];
628 static int
629 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
631 int rc;
632 struct cifs_ses *ses = sess_data->ses;
633 struct smb2_sess_setup_req *req;
634 struct TCP_Server_Info *server = ses->server;
636 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
637 if (rc)
638 return rc;
640 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
642 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
643 req->PreviousSessionId = sess_data->previous_session;
645 req->Flags = 0; /* MBZ */
646 /* to enable echos and oplocks */
647 req->hdr.CreditRequest = cpu_to_le16(3);
649 /* only one of SMB2 signing flags may be set in SMB2 request */
650 if (server->sign)
651 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
652 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
653 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
654 else
655 req->SecurityMode = 0;
657 req->Capabilities = 0;
658 req->Channel = 0; /* MBZ */
660 sess_data->iov[0].iov_base = (char *)req;
661 /* 4 for rfc1002 length field and 1 for pad */
662 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
664 * This variable will be used to clear the buffer
665 * allocated above in case of any error in the calling function.
667 sess_data->buf0_type = CIFS_SMALL_BUFFER;
669 return 0;
672 static void
673 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
675 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
676 sess_data->buf0_type = CIFS_NO_BUFFER;
679 static int
680 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
682 int rc;
683 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
685 /* Testing shows that buffer offset must be at location of Buffer[0] */
686 req->SecurityBufferOffset =
687 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
688 1 /* pad */ - 4 /* rfc1001 len */);
689 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
691 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
693 /* BB add code to build os and lm fields */
695 rc = SendReceive2(sess_data->xid, sess_data->ses,
696 sess_data->iov, 2,
697 &sess_data->buf0_type,
698 CIFS_LOG_ERROR | CIFS_NEG_OP);
700 return rc;
703 static int
704 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
706 int rc = 0;
707 struct cifs_ses *ses = sess_data->ses;
709 mutex_lock(&ses->server->srv_mutex);
710 if (ses->server->ops->generate_signingkey) {
711 rc = ses->server->ops->generate_signingkey(ses);
712 if (rc) {
713 cifs_dbg(FYI,
714 "SMB3 session key generation failed\n");
715 mutex_unlock(&ses->server->srv_mutex);
716 return rc;
719 if (!ses->server->session_estab) {
720 ses->server->sequence_number = 0x2;
721 ses->server->session_estab = true;
723 mutex_unlock(&ses->server->srv_mutex);
725 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
726 spin_lock(&GlobalMid_Lock);
727 ses->status = CifsGood;
728 ses->need_reconnect = false;
729 spin_unlock(&GlobalMid_Lock);
730 return rc;
733 #ifdef CONFIG_CIFS_UPCALL
734 static void
735 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
737 int rc;
738 struct cifs_ses *ses = sess_data->ses;
739 struct cifs_spnego_msg *msg;
740 struct key *spnego_key = NULL;
741 struct smb2_sess_setup_rsp *rsp = NULL;
743 rc = SMB2_sess_alloc_buffer(sess_data);
744 if (rc)
745 goto out;
747 spnego_key = cifs_get_spnego_key(ses);
748 if (IS_ERR(spnego_key)) {
749 rc = PTR_ERR(spnego_key);
750 spnego_key = NULL;
751 goto out;
754 msg = spnego_key->payload.data[0];
756 * check version field to make sure that cifs.upcall is
757 * sending us a response in an expected form
759 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
760 cifs_dbg(VFS,
761 "bad cifs.upcall version. Expected %d got %d",
762 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
763 rc = -EKEYREJECTED;
764 goto out_put_spnego_key;
767 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
768 GFP_KERNEL);
769 if (!ses->auth_key.response) {
770 cifs_dbg(VFS,
771 "Kerberos can't allocate (%u bytes) memory",
772 msg->sesskey_len);
773 rc = -ENOMEM;
774 goto out_put_spnego_key;
776 ses->auth_key.len = msg->sesskey_len;
778 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
779 sess_data->iov[1].iov_len = msg->secblob_len;
781 rc = SMB2_sess_sendreceive(sess_data);
782 if (rc)
783 goto out_put_spnego_key;
785 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
786 ses->Suid = rsp->hdr.SessionId;
788 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
789 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
790 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
792 rc = SMB2_sess_establish_session(sess_data);
793 out_put_spnego_key:
794 key_invalidate(spnego_key);
795 key_put(spnego_key);
796 out:
797 sess_data->result = rc;
798 sess_data->func = NULL;
799 SMB2_sess_free_buffer(sess_data);
801 #else
802 static void
803 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
805 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
806 sess_data->result = -EOPNOTSUPP;
807 sess_data->func = NULL;
809 #endif
811 static void
812 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
814 static void
815 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
817 int rc;
818 struct cifs_ses *ses = sess_data->ses;
819 struct smb2_sess_setup_rsp *rsp = NULL;
820 char *ntlmssp_blob = NULL;
821 bool use_spnego = false; /* else use raw ntlmssp */
822 u16 blob_length = 0;
825 * If memory allocation is successful, caller of this function
826 * frees it.
828 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
829 if (!ses->ntlmssp) {
830 rc = -ENOMEM;
831 goto out_err;
833 ses->ntlmssp->sesskey_per_smbsess = true;
835 rc = SMB2_sess_alloc_buffer(sess_data);
836 if (rc)
837 goto out_err;
839 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
840 GFP_KERNEL);
841 if (ntlmssp_blob == NULL) {
842 rc = -ENOMEM;
843 goto out;
846 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
847 if (use_spnego) {
848 /* BB eventually need to add this */
849 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
850 rc = -EOPNOTSUPP;
851 goto out;
852 } else {
853 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
854 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
856 sess_data->iov[1].iov_base = ntlmssp_blob;
857 sess_data->iov[1].iov_len = blob_length;
859 rc = SMB2_sess_sendreceive(sess_data);
860 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
862 /* If true, rc here is expected and not an error */
863 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
864 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
865 rc = 0;
867 if (rc)
868 goto out;
870 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
871 le16_to_cpu(rsp->SecurityBufferOffset)) {
872 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
873 le16_to_cpu(rsp->SecurityBufferOffset));
874 rc = -EIO;
875 goto out;
877 rc = decode_ntlmssp_challenge(rsp->Buffer,
878 le16_to_cpu(rsp->SecurityBufferLength), ses);
879 if (rc)
880 goto out;
882 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
885 ses->Suid = rsp->hdr.SessionId;
886 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
887 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
888 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
890 out:
891 kfree(ntlmssp_blob);
892 SMB2_sess_free_buffer(sess_data);
893 if (!rc) {
894 sess_data->result = 0;
895 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
896 return;
898 out_err:
899 kfree(ses->ntlmssp);
900 ses->ntlmssp = NULL;
901 sess_data->result = rc;
902 sess_data->func = NULL;
905 static void
906 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
908 int rc;
909 struct cifs_ses *ses = sess_data->ses;
910 struct smb2_sess_setup_req *req;
911 struct smb2_sess_setup_rsp *rsp = NULL;
912 unsigned char *ntlmssp_blob = NULL;
913 bool use_spnego = false; /* else use raw ntlmssp */
914 u16 blob_length = 0;
916 rc = SMB2_sess_alloc_buffer(sess_data);
917 if (rc)
918 goto out;
920 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
921 req->hdr.SessionId = ses->Suid;
923 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
924 sess_data->nls_cp);
925 if (rc) {
926 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
927 goto out;
930 if (use_spnego) {
931 /* BB eventually need to add this */
932 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
933 rc = -EOPNOTSUPP;
934 goto out;
936 sess_data->iov[1].iov_base = ntlmssp_blob;
937 sess_data->iov[1].iov_len = blob_length;
939 rc = SMB2_sess_sendreceive(sess_data);
940 if (rc)
941 goto out;
943 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
945 ses->Suid = rsp->hdr.SessionId;
946 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
947 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
948 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
950 rc = SMB2_sess_establish_session(sess_data);
951 out:
952 kfree(ntlmssp_blob);
953 SMB2_sess_free_buffer(sess_data);
954 kfree(ses->ntlmssp);
955 ses->ntlmssp = NULL;
956 sess_data->result = rc;
957 sess_data->func = NULL;
960 static int
961 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
963 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
964 ses->sectype = RawNTLMSSP;
966 switch (ses->sectype) {
967 case Kerberos:
968 sess_data->func = SMB2_auth_kerberos;
969 break;
970 case RawNTLMSSP:
971 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
972 break;
973 default:
974 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
975 return -EOPNOTSUPP;
978 return 0;
982 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
983 const struct nls_table *nls_cp)
985 int rc = 0;
986 struct TCP_Server_Info *server = ses->server;
987 struct SMB2_sess_data *sess_data;
989 cifs_dbg(FYI, "Session Setup\n");
991 if (!server) {
992 WARN(1, "%s: server is NULL!\n", __func__);
993 return -EIO;
996 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
997 if (!sess_data)
998 return -ENOMEM;
1000 rc = SMB2_select_sec(ses, sess_data);
1001 if (rc)
1002 goto out;
1003 sess_data->xid = xid;
1004 sess_data->ses = ses;
1005 sess_data->buf0_type = CIFS_NO_BUFFER;
1006 sess_data->nls_cp = (struct nls_table *) nls_cp;
1008 while (sess_data->func)
1009 sess_data->func(sess_data);
1011 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1012 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1013 rc = sess_data->result;
1014 out:
1015 kfree(sess_data);
1016 return rc;
1020 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1022 struct smb2_logoff_req *req; /* response is also trivial struct */
1023 int rc = 0;
1024 struct TCP_Server_Info *server;
1026 cifs_dbg(FYI, "disconnect session %p\n", ses);
1028 if (ses && (ses->server))
1029 server = ses->server;
1030 else
1031 return -EIO;
1033 /* no need to send SMB logoff if uid already closed due to reconnect */
1034 if (ses->need_reconnect)
1035 goto smb2_session_already_dead;
1037 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1038 if (rc)
1039 return rc;
1041 /* since no tcon, smb2_init can not do this, so do here */
1042 req->hdr.SessionId = ses->Suid;
1043 if (server->sign)
1044 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1046 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1048 * No tcon so can't do
1049 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1052 smb2_session_already_dead:
1053 return rc;
1056 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1058 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1061 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1063 /* These are similar values to what Windows uses */
1064 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1066 tcon->max_chunks = 256;
1067 tcon->max_bytes_chunk = 1048576;
1068 tcon->max_bytes_copy = 16777216;
1072 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1073 struct cifs_tcon *tcon, const struct nls_table *cp)
1075 struct smb2_tree_connect_req *req;
1076 struct smb2_tree_connect_rsp *rsp = NULL;
1077 struct kvec iov[2];
1078 int rc = 0;
1079 int resp_buftype;
1080 int unc_path_len;
1081 struct TCP_Server_Info *server;
1082 __le16 *unc_path = NULL;
1084 cifs_dbg(FYI, "TCON\n");
1086 if ((ses->server) && tree)
1087 server = ses->server;
1088 else
1089 return -EIO;
1091 if ((tcon && tcon->seal) &&
1092 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1093 cifs_dbg(VFS, "encryption requested but no server support");
1094 return -EOPNOTSUPP;
1097 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1098 if (unc_path == NULL)
1099 return -ENOMEM;
1101 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1102 unc_path_len *= 2;
1103 if (unc_path_len < 2) {
1104 kfree(unc_path);
1105 return -EINVAL;
1108 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1109 if (tcon)
1110 tcon->tid = 0;
1112 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1113 if (rc) {
1114 kfree(unc_path);
1115 return rc;
1118 if (tcon == NULL) {
1119 /* since no tcon, smb2_init can not do this, so do here */
1120 req->hdr.SessionId = ses->Suid;
1121 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1122 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1125 iov[0].iov_base = (char *)req;
1126 /* 4 for rfc1002 length field and 1 for pad */
1127 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1129 /* Testing shows that buffer offset must be at location of Buffer[0] */
1130 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1131 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1132 req->PathLength = cpu_to_le16(unc_path_len - 2);
1133 iov[1].iov_base = unc_path;
1134 iov[1].iov_len = unc_path_len;
1136 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1138 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1139 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1141 if (rc != 0) {
1142 if (tcon) {
1143 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1144 tcon->need_reconnect = true;
1146 goto tcon_error_exit;
1149 if (tcon == NULL) {
1150 ses->ipc_tid = rsp->hdr.TreeId;
1151 goto tcon_exit;
1154 switch (rsp->ShareType) {
1155 case SMB2_SHARE_TYPE_DISK:
1156 cifs_dbg(FYI, "connection to disk share\n");
1157 break;
1158 case SMB2_SHARE_TYPE_PIPE:
1159 tcon->ipc = true;
1160 cifs_dbg(FYI, "connection to pipe share\n");
1161 break;
1162 case SMB2_SHARE_TYPE_PRINT:
1163 tcon->ipc = true;
1164 cifs_dbg(FYI, "connection to printer\n");
1165 break;
1166 default:
1167 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1168 rc = -EOPNOTSUPP;
1169 goto tcon_error_exit;
1172 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1173 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1174 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1175 tcon->tidStatus = CifsGood;
1176 tcon->need_reconnect = false;
1177 tcon->tid = rsp->hdr.TreeId;
1178 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1180 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1181 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1182 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1183 init_copy_chunk_defaults(tcon);
1184 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1185 cifs_dbg(VFS, "Encrypted shares not supported");
1186 if (tcon->ses->server->ops->validate_negotiate)
1187 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1188 tcon_exit:
1189 free_rsp_buf(resp_buftype, rsp);
1190 kfree(unc_path);
1191 return rc;
1193 tcon_error_exit:
1194 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1195 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1197 goto tcon_exit;
1201 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1203 struct smb2_tree_disconnect_req *req; /* response is trivial */
1204 int rc = 0;
1205 struct TCP_Server_Info *server;
1206 struct cifs_ses *ses = tcon->ses;
1208 cifs_dbg(FYI, "Tree Disconnect\n");
1210 if (ses && (ses->server))
1211 server = ses->server;
1212 else
1213 return -EIO;
1215 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1216 return 0;
1218 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1219 if (rc)
1220 return rc;
1222 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1223 if (rc)
1224 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1226 return rc;
1230 static struct create_durable *
1231 create_durable_buf(void)
1233 struct create_durable *buf;
1235 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1236 if (!buf)
1237 return NULL;
1239 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1240 (struct create_durable, Data));
1241 buf->ccontext.DataLength = cpu_to_le32(16);
1242 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1243 (struct create_durable, Name));
1244 buf->ccontext.NameLength = cpu_to_le16(4);
1245 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1246 buf->Name[0] = 'D';
1247 buf->Name[1] = 'H';
1248 buf->Name[2] = 'n';
1249 buf->Name[3] = 'Q';
1250 return buf;
1253 static struct create_durable *
1254 create_reconnect_durable_buf(struct cifs_fid *fid)
1256 struct create_durable *buf;
1258 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1259 if (!buf)
1260 return NULL;
1262 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1263 (struct create_durable, Data));
1264 buf->ccontext.DataLength = cpu_to_le32(16);
1265 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1266 (struct create_durable, Name));
1267 buf->ccontext.NameLength = cpu_to_le16(4);
1268 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1269 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1270 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1271 buf->Name[0] = 'D';
1272 buf->Name[1] = 'H';
1273 buf->Name[2] = 'n';
1274 buf->Name[3] = 'C';
1275 return buf;
1278 static __u8
1279 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1280 unsigned int *epoch)
1282 char *data_offset;
1283 struct create_context *cc;
1284 unsigned int next;
1285 unsigned int remaining;
1286 char *name;
1288 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1289 remaining = le32_to_cpu(rsp->CreateContextsLength);
1290 cc = (struct create_context *)data_offset;
1291 while (remaining >= sizeof(struct create_context)) {
1292 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1293 if (le16_to_cpu(cc->NameLength) == 4 &&
1294 strncmp(name, "RqLs", 4) == 0)
1295 return server->ops->parse_lease_buf(cc, epoch);
1297 next = le32_to_cpu(cc->Next);
1298 if (!next)
1299 break;
1300 remaining -= next;
1301 cc = (struct create_context *)((char *)cc + next);
1304 return 0;
1307 static int
1308 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1309 unsigned int *num_iovec, __u8 *oplock)
1311 struct smb2_create_req *req = iov[0].iov_base;
1312 unsigned int num = *num_iovec;
1314 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1315 if (iov[num].iov_base == NULL)
1316 return -ENOMEM;
1317 iov[num].iov_len = server->vals->create_lease_size;
1318 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1319 if (!req->CreateContextsOffset)
1320 req->CreateContextsOffset = cpu_to_le32(
1321 sizeof(struct smb2_create_req) - 4 +
1322 iov[num - 1].iov_len);
1323 le32_add_cpu(&req->CreateContextsLength,
1324 server->vals->create_lease_size);
1325 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1326 *num_iovec = num + 1;
1327 return 0;
1330 static struct create_durable_v2 *
1331 create_durable_v2_buf(struct cifs_fid *pfid)
1333 struct create_durable_v2 *buf;
1335 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1336 if (!buf)
1337 return NULL;
1339 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1340 (struct create_durable_v2, dcontext));
1341 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1342 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1343 (struct create_durable_v2, Name));
1344 buf->ccontext.NameLength = cpu_to_le16(4);
1346 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1347 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1348 generate_random_uuid(buf->dcontext.CreateGuid);
1349 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1351 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1352 buf->Name[0] = 'D';
1353 buf->Name[1] = 'H';
1354 buf->Name[2] = '2';
1355 buf->Name[3] = 'Q';
1356 return buf;
1359 static struct create_durable_handle_reconnect_v2 *
1360 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1362 struct create_durable_handle_reconnect_v2 *buf;
1364 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1365 GFP_KERNEL);
1366 if (!buf)
1367 return NULL;
1369 buf->ccontext.DataOffset =
1370 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1371 dcontext));
1372 buf->ccontext.DataLength =
1373 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1374 buf->ccontext.NameOffset =
1375 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1376 Name));
1377 buf->ccontext.NameLength = cpu_to_le16(4);
1379 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1380 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1381 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1382 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1384 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1385 buf->Name[0] = 'D';
1386 buf->Name[1] = 'H';
1387 buf->Name[2] = '2';
1388 buf->Name[3] = 'C';
1389 return buf;
1392 static int
1393 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1394 struct cifs_open_parms *oparms)
1396 struct smb2_create_req *req = iov[0].iov_base;
1397 unsigned int num = *num_iovec;
1399 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1400 if (iov[num].iov_base == NULL)
1401 return -ENOMEM;
1402 iov[num].iov_len = sizeof(struct create_durable_v2);
1403 if (!req->CreateContextsOffset)
1404 req->CreateContextsOffset =
1405 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1406 iov[1].iov_len);
1407 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1408 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1409 *num_iovec = num + 1;
1410 return 0;
1413 static int
1414 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1415 struct cifs_open_parms *oparms)
1417 struct smb2_create_req *req = iov[0].iov_base;
1418 unsigned int num = *num_iovec;
1420 /* indicate that we don't need to relock the file */
1421 oparms->reconnect = false;
1423 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1424 if (iov[num].iov_base == NULL)
1425 return -ENOMEM;
1426 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1427 if (!req->CreateContextsOffset)
1428 req->CreateContextsOffset =
1429 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1430 iov[1].iov_len);
1431 le32_add_cpu(&req->CreateContextsLength,
1432 sizeof(struct create_durable_handle_reconnect_v2));
1433 inc_rfc1001_len(&req->hdr,
1434 sizeof(struct create_durable_handle_reconnect_v2));
1435 *num_iovec = num + 1;
1436 return 0;
1439 static int
1440 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1441 struct cifs_open_parms *oparms, bool use_persistent)
1443 struct smb2_create_req *req = iov[0].iov_base;
1444 unsigned int num = *num_iovec;
1446 if (use_persistent) {
1447 if (oparms->reconnect)
1448 return add_durable_reconnect_v2_context(iov, num_iovec,
1449 oparms);
1450 else
1451 return add_durable_v2_context(iov, num_iovec, oparms);
1454 if (oparms->reconnect) {
1455 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1456 /* indicate that we don't need to relock the file */
1457 oparms->reconnect = false;
1458 } else
1459 iov[num].iov_base = create_durable_buf();
1460 if (iov[num].iov_base == NULL)
1461 return -ENOMEM;
1462 iov[num].iov_len = sizeof(struct create_durable);
1463 if (!req->CreateContextsOffset)
1464 req->CreateContextsOffset =
1465 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1466 iov[1].iov_len);
1467 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1468 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1469 *num_iovec = num + 1;
1470 return 0;
1474 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1475 __u8 *oplock, struct smb2_file_all_info *buf,
1476 struct smb2_err_rsp **err_buf)
1478 struct smb2_create_req *req;
1479 struct smb2_create_rsp *rsp;
1480 struct TCP_Server_Info *server;
1481 struct cifs_tcon *tcon = oparms->tcon;
1482 struct cifs_ses *ses = tcon->ses;
1483 struct kvec iov[4];
1484 int resp_buftype;
1485 int uni_path_len;
1486 __le16 *copy_path = NULL;
1487 int copy_size;
1488 int rc = 0;
1489 unsigned int num_iovecs = 2;
1490 __u32 file_attributes = 0;
1491 char *dhc_buf = NULL, *lc_buf = NULL;
1493 cifs_dbg(FYI, "create/open\n");
1495 if (ses && (ses->server))
1496 server = ses->server;
1497 else
1498 return -EIO;
1500 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1501 if (rc)
1502 return rc;
1504 if (oparms->create_options & CREATE_OPTION_READONLY)
1505 file_attributes |= ATTR_READONLY;
1506 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1507 file_attributes |= ATTR_SYSTEM;
1509 req->ImpersonationLevel = IL_IMPERSONATION;
1510 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1511 /* File attributes ignored on open (used in create though) */
1512 req->FileAttributes = cpu_to_le32(file_attributes);
1513 req->ShareAccess = FILE_SHARE_ALL_LE;
1514 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1515 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1516 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1517 /* do not count rfc1001 len field */
1518 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1520 iov[0].iov_base = (char *)req;
1521 /* 4 for rfc1002 length field */
1522 iov[0].iov_len = get_rfc1002_length(req) + 4;
1524 /* MUST set path len (NameLength) to 0 opening root of share */
1525 req->NameLength = cpu_to_le16(uni_path_len - 2);
1526 /* -1 since last byte is buf[0] which is sent below (path) */
1527 iov[0].iov_len--;
1528 if (uni_path_len % 8 != 0) {
1529 copy_size = uni_path_len / 8 * 8;
1530 if (copy_size < uni_path_len)
1531 copy_size += 8;
1533 copy_path = kzalloc(copy_size, GFP_KERNEL);
1534 if (!copy_path)
1535 return -ENOMEM;
1536 memcpy((char *)copy_path, (const char *)path,
1537 uni_path_len);
1538 uni_path_len = copy_size;
1539 path = copy_path;
1542 iov[1].iov_len = uni_path_len;
1543 iov[1].iov_base = path;
1544 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1545 inc_rfc1001_len(req, uni_path_len - 1);
1547 if (!server->oplocks)
1548 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1550 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1551 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1552 req->RequestedOplockLevel = *oplock;
1553 else {
1554 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1555 if (rc) {
1556 cifs_small_buf_release(req);
1557 kfree(copy_path);
1558 return rc;
1560 lc_buf = iov[num_iovecs-1].iov_base;
1563 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1564 /* need to set Next field of lease context if we request it */
1565 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1566 struct create_context *ccontext =
1567 (struct create_context *)iov[num_iovecs-1].iov_base;
1568 ccontext->Next =
1569 cpu_to_le32(server->vals->create_lease_size);
1572 rc = add_durable_context(iov, &num_iovecs, oparms,
1573 tcon->use_persistent);
1574 if (rc) {
1575 cifs_small_buf_release(req);
1576 kfree(copy_path);
1577 kfree(lc_buf);
1578 return rc;
1580 dhc_buf = iov[num_iovecs-1].iov_base;
1583 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1584 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1586 if (rc != 0) {
1587 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1588 if (err_buf)
1589 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1590 GFP_KERNEL);
1591 goto creat_exit;
1594 oparms->fid->persistent_fid = rsp->PersistentFileId;
1595 oparms->fid->volatile_fid = rsp->VolatileFileId;
1597 if (buf) {
1598 memcpy(buf, &rsp->CreationTime, 32);
1599 buf->AllocationSize = rsp->AllocationSize;
1600 buf->EndOfFile = rsp->EndofFile;
1601 buf->Attributes = rsp->FileAttributes;
1602 buf->NumberOfLinks = cpu_to_le32(1);
1603 buf->DeletePending = 0;
1606 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1607 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1608 else
1609 *oplock = rsp->OplockLevel;
1610 creat_exit:
1611 kfree(copy_path);
1612 kfree(lc_buf);
1613 kfree(dhc_buf);
1614 free_rsp_buf(resp_buftype, rsp);
1615 return rc;
1619 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1622 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1623 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1624 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1626 struct smb2_ioctl_req *req;
1627 struct smb2_ioctl_rsp *rsp;
1628 struct TCP_Server_Info *server;
1629 struct cifs_ses *ses;
1630 struct kvec iov[2];
1631 int resp_buftype;
1632 int num_iovecs;
1633 int rc = 0;
1635 cifs_dbg(FYI, "SMB2 IOCTL\n");
1637 if (out_data != NULL)
1638 *out_data = NULL;
1640 /* zero out returned data len, in case of error */
1641 if (plen)
1642 *plen = 0;
1644 if (tcon)
1645 ses = tcon->ses;
1646 else
1647 return -EIO;
1649 if (ses && (ses->server))
1650 server = ses->server;
1651 else
1652 return -EIO;
1654 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1655 if (rc)
1656 return rc;
1658 req->CtlCode = cpu_to_le32(opcode);
1659 req->PersistentFileId = persistent_fid;
1660 req->VolatileFileId = volatile_fid;
1662 if (indatalen) {
1663 req->InputCount = cpu_to_le32(indatalen);
1664 /* do not set InputOffset if no input data */
1665 req->InputOffset =
1666 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1667 iov[1].iov_base = in_data;
1668 iov[1].iov_len = indatalen;
1669 num_iovecs = 2;
1670 } else
1671 num_iovecs = 1;
1673 req->OutputOffset = 0;
1674 req->OutputCount = 0; /* MBZ */
1677 * Could increase MaxOutputResponse, but that would require more
1678 * than one credit. Windows typically sets this smaller, but for some
1679 * ioctls it may be useful to allow server to send more. No point
1680 * limiting what the server can send as long as fits in one credit
1681 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1682 * (by default, note that it can be overridden to make max larger)
1683 * in responses (except for read responses which can be bigger.
1684 * We may want to bump this limit up
1686 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1688 if (is_fsctl)
1689 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1690 else
1691 req->Flags = 0;
1693 iov[0].iov_base = (char *)req;
1696 * If no input data, the size of ioctl struct in
1697 * protocol spec still includes a 1 byte data buffer,
1698 * but if input data passed to ioctl, we do not
1699 * want to double count this, so we do not send
1700 * the dummy one byte of data in iovec[0] if sending
1701 * input data (in iovec[1]). We also must add 4 bytes
1702 * in first iovec to allow for rfc1002 length field.
1705 if (indatalen) {
1706 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1707 inc_rfc1001_len(req, indatalen - 1);
1708 } else
1709 iov[0].iov_len = get_rfc1002_length(req) + 4;
1711 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1712 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1713 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1715 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1716 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1718 if ((rc != 0) && (rc != -EINVAL)) {
1719 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1720 goto ioctl_exit;
1721 } else if (rc == -EINVAL) {
1722 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1723 (opcode != FSCTL_SRV_COPYCHUNK)) {
1724 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1725 goto ioctl_exit;
1729 /* check if caller wants to look at return data or just return rc */
1730 if ((plen == NULL) || (out_data == NULL))
1731 goto ioctl_exit;
1733 *plen = le32_to_cpu(rsp->OutputCount);
1735 /* We check for obvious errors in the output buffer length and offset */
1736 if (*plen == 0)
1737 goto ioctl_exit; /* server returned no data */
1738 else if (*plen > 0xFF00) {
1739 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1740 *plen = 0;
1741 rc = -EIO;
1742 goto ioctl_exit;
1745 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1746 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1747 le32_to_cpu(rsp->OutputOffset));
1748 *plen = 0;
1749 rc = -EIO;
1750 goto ioctl_exit;
1753 *out_data = kmalloc(*plen, GFP_KERNEL);
1754 if (*out_data == NULL) {
1755 rc = -ENOMEM;
1756 goto ioctl_exit;
1759 memcpy(*out_data,
1760 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1761 *plen);
1762 ioctl_exit:
1763 free_rsp_buf(resp_buftype, rsp);
1764 return rc;
1768 * Individual callers to ioctl worker function follow
1772 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1773 u64 persistent_fid, u64 volatile_fid)
1775 int rc;
1776 struct compress_ioctl fsctl_input;
1777 char *ret_data = NULL;
1779 fsctl_input.CompressionState =
1780 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1782 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1783 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1784 (char *)&fsctl_input /* data input */,
1785 2 /* in data len */, &ret_data /* out data */, NULL);
1787 cifs_dbg(FYI, "set compression rc %d\n", rc);
1789 return rc;
1793 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1794 u64 persistent_fid, u64 volatile_fid)
1796 struct smb2_close_req *req;
1797 struct smb2_close_rsp *rsp;
1798 struct TCP_Server_Info *server;
1799 struct cifs_ses *ses = tcon->ses;
1800 struct kvec iov[1];
1801 int resp_buftype;
1802 int rc = 0;
1804 cifs_dbg(FYI, "Close\n");
1806 if (ses && (ses->server))
1807 server = ses->server;
1808 else
1809 return -EIO;
1811 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1812 if (rc)
1813 return rc;
1815 req->PersistentFileId = persistent_fid;
1816 req->VolatileFileId = volatile_fid;
1818 iov[0].iov_base = (char *)req;
1819 /* 4 for rfc1002 length field */
1820 iov[0].iov_len = get_rfc1002_length(req) + 4;
1822 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1823 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1825 if (rc != 0) {
1826 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1827 goto close_exit;
1830 /* BB FIXME - decode close response, update inode for caching */
1832 close_exit:
1833 free_rsp_buf(resp_buftype, rsp);
1834 return rc;
1837 static int
1838 validate_buf(unsigned int offset, unsigned int buffer_length,
1839 struct smb2_hdr *hdr, unsigned int min_buf_size)
1842 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1843 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1844 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1845 char *end_of_buf = begin_of_buf + buffer_length;
1848 if (buffer_length < min_buf_size) {
1849 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1850 buffer_length, min_buf_size);
1851 return -EINVAL;
1854 /* check if beyond RFC1001 maximum length */
1855 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1856 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1857 buffer_length, smb_len);
1858 return -EINVAL;
1861 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1862 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1863 return -EINVAL;
1866 return 0;
1870 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1871 * Caller must free buffer.
1873 static int
1874 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1875 struct smb2_hdr *hdr, unsigned int minbufsize,
1876 char *data)
1879 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1880 int rc;
1882 if (!data)
1883 return -EINVAL;
1885 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1886 if (rc)
1887 return rc;
1889 memcpy(data, begin_of_buf, buffer_length);
1891 return 0;
1894 static int
1895 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1896 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1897 size_t output_len, size_t min_len, void *data)
1899 struct smb2_query_info_req *req;
1900 struct smb2_query_info_rsp *rsp = NULL;
1901 struct kvec iov[2];
1902 int rc = 0;
1903 int resp_buftype;
1904 struct TCP_Server_Info *server;
1905 struct cifs_ses *ses = tcon->ses;
1907 cifs_dbg(FYI, "Query Info\n");
1909 if (ses && (ses->server))
1910 server = ses->server;
1911 else
1912 return -EIO;
1914 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1915 if (rc)
1916 return rc;
1918 req->InfoType = SMB2_O_INFO_FILE;
1919 req->FileInfoClass = info_class;
1920 req->PersistentFileId = persistent_fid;
1921 req->VolatileFileId = volatile_fid;
1922 /* 4 for rfc1002 length field and 1 for Buffer */
1923 req->InputBufferOffset =
1924 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1925 req->OutputBufferLength = cpu_to_le32(output_len);
1927 iov[0].iov_base = (char *)req;
1928 /* 4 for rfc1002 length field */
1929 iov[0].iov_len = get_rfc1002_length(req) + 4;
1931 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1932 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1934 if (rc) {
1935 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1936 goto qinf_exit;
1939 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1940 le32_to_cpu(rsp->OutputBufferLength),
1941 &rsp->hdr, min_len, data);
1943 qinf_exit:
1944 free_rsp_buf(resp_buftype, rsp);
1945 return rc;
1949 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1950 u64 persistent_fid, u64 volatile_fid,
1951 struct smb2_file_all_info *data)
1953 return query_info(xid, tcon, persistent_fid, volatile_fid,
1954 FILE_ALL_INFORMATION,
1955 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1956 sizeof(struct smb2_file_all_info), data);
1960 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1961 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1963 return query_info(xid, tcon, persistent_fid, volatile_fid,
1964 FILE_INTERNAL_INFORMATION,
1965 sizeof(struct smb2_file_internal_info),
1966 sizeof(struct smb2_file_internal_info), uniqueid);
1970 * This is a no-op for now. We're not really interested in the reply, but
1971 * rather in the fact that the server sent one and that server->lstrp
1972 * gets updated.
1974 * FIXME: maybe we should consider checking that the reply matches request?
1976 static void
1977 smb2_echo_callback(struct mid_q_entry *mid)
1979 struct TCP_Server_Info *server = mid->callback_data;
1980 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1981 unsigned int credits_received = 1;
1983 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1984 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1986 mutex_lock(&server->srv_mutex);
1987 DeleteMidQEntry(mid);
1988 mutex_unlock(&server->srv_mutex);
1989 add_credits(server, credits_received, CIFS_ECHO_OP);
1992 void smb2_reconnect_server(struct work_struct *work)
1994 struct TCP_Server_Info *server = container_of(work,
1995 struct TCP_Server_Info, reconnect.work);
1996 struct cifs_ses *ses;
1997 struct cifs_tcon *tcon, *tcon2;
1998 struct list_head tmp_list;
1999 int tcon_exist = false;
2000 int rc;
2001 int resched = false;
2004 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2005 mutex_lock(&server->reconnect_mutex);
2007 INIT_LIST_HEAD(&tmp_list);
2008 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2010 spin_lock(&cifs_tcp_ses_lock);
2011 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2012 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2013 if (tcon->need_reconnect || tcon->need_reopen_files) {
2014 tcon->tc_count++;
2015 list_add_tail(&tcon->rlist, &tmp_list);
2016 tcon_exist = true;
2021 * Get the reference to server struct to be sure that the last call of
2022 * cifs_put_tcon() in the loop below won't release the server pointer.
2024 if (tcon_exist)
2025 server->srv_count++;
2027 spin_unlock(&cifs_tcp_ses_lock);
2029 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2030 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2031 if (!rc)
2032 cifs_reopen_persistent_handles(tcon);
2033 else
2034 resched = true;
2035 list_del_init(&tcon->rlist);
2036 cifs_put_tcon(tcon);
2039 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2040 if (resched)
2041 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2042 mutex_unlock(&server->reconnect_mutex);
2044 /* now we can safely release srv struct */
2045 if (tcon_exist)
2046 cifs_put_tcp_session(server, 1);
2050 SMB2_echo(struct TCP_Server_Info *server)
2052 struct smb2_echo_req *req;
2053 int rc = 0;
2054 struct kvec iov;
2055 struct smb_rqst rqst = { .rq_iov = &iov,
2056 .rq_nvec = 1 };
2058 cifs_dbg(FYI, "In echo request\n");
2060 if (server->tcpStatus == CifsNeedNegotiate) {
2061 /* No need to send echo on newly established connections */
2062 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2063 return rc;
2066 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2067 if (rc)
2068 return rc;
2070 req->hdr.CreditRequest = cpu_to_le16(1);
2072 iov.iov_base = (char *)req;
2073 /* 4 for rfc1002 length field */
2074 iov.iov_len = get_rfc1002_length(req) + 4;
2076 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
2077 CIFS_ECHO_OP);
2078 if (rc)
2079 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2081 cifs_small_buf_release(req);
2082 return rc;
2086 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2087 u64 volatile_fid)
2089 struct smb2_flush_req *req;
2090 struct TCP_Server_Info *server;
2091 struct cifs_ses *ses = tcon->ses;
2092 struct kvec iov[1];
2093 int resp_buftype;
2094 int rc = 0;
2096 cifs_dbg(FYI, "Flush\n");
2098 if (ses && (ses->server))
2099 server = ses->server;
2100 else
2101 return -EIO;
2103 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2104 if (rc)
2105 return rc;
2107 req->PersistentFileId = persistent_fid;
2108 req->VolatileFileId = volatile_fid;
2110 iov[0].iov_base = (char *)req;
2111 /* 4 for rfc1002 length field */
2112 iov[0].iov_len = get_rfc1002_length(req) + 4;
2114 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2116 if (rc != 0)
2117 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2119 free_rsp_buf(resp_buftype, iov[0].iov_base);
2120 return rc;
2124 * To form a chain of read requests, any read requests after the first should
2125 * have the end_of_chain boolean set to true.
2127 static int
2128 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2129 unsigned int remaining_bytes, int request_type)
2131 int rc = -EACCES;
2132 struct smb2_read_req *req = NULL;
2134 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2135 if (rc)
2136 return rc;
2137 if (io_parms->tcon->ses->server == NULL)
2138 return -ECONNABORTED;
2140 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2142 req->PersistentFileId = io_parms->persistent_fid;
2143 req->VolatileFileId = io_parms->volatile_fid;
2144 req->ReadChannelInfoOffset = 0; /* reserved */
2145 req->ReadChannelInfoLength = 0; /* reserved */
2146 req->Channel = 0; /* reserved */
2147 req->MinimumCount = 0;
2148 req->Length = cpu_to_le32(io_parms->length);
2149 req->Offset = cpu_to_le64(io_parms->offset);
2151 if (request_type & CHAINED_REQUEST) {
2152 if (!(request_type & END_OF_CHAIN)) {
2153 /* 4 for rfc1002 length field */
2154 req->hdr.NextCommand =
2155 cpu_to_le32(get_rfc1002_length(req) + 4);
2156 } else /* END_OF_CHAIN */
2157 req->hdr.NextCommand = 0;
2158 if (request_type & RELATED_REQUEST) {
2159 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2161 * Related requests use info from previous read request
2162 * in chain.
2164 req->hdr.SessionId = 0xFFFFFFFF;
2165 req->hdr.TreeId = 0xFFFFFFFF;
2166 req->PersistentFileId = 0xFFFFFFFF;
2167 req->VolatileFileId = 0xFFFFFFFF;
2170 if (remaining_bytes > io_parms->length)
2171 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2172 else
2173 req->RemainingBytes = 0;
2175 iov[0].iov_base = (char *)req;
2176 /* 4 for rfc1002 length field */
2177 iov[0].iov_len = get_rfc1002_length(req) + 4;
2178 return rc;
2181 static void
2182 smb2_readv_callback(struct mid_q_entry *mid)
2184 struct cifs_readdata *rdata = mid->callback_data;
2185 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2186 struct TCP_Server_Info *server = tcon->ses->server;
2187 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
2188 unsigned int credits_received = 1;
2189 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2190 .rq_nvec = 1,
2191 .rq_pages = rdata->pages,
2192 .rq_npages = rdata->nr_pages,
2193 .rq_pagesz = rdata->pagesz,
2194 .rq_tailsz = rdata->tailsz };
2196 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2197 __func__, mid->mid, mid->mid_state, rdata->result,
2198 rdata->bytes);
2200 switch (mid->mid_state) {
2201 case MID_RESPONSE_RECEIVED:
2202 credits_received = le16_to_cpu(buf->CreditRequest);
2203 /* result already set, check signature */
2204 if (server->sign) {
2205 int rc;
2207 rc = smb2_verify_signature(&rqst, server);
2208 if (rc)
2209 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2210 rc);
2212 /* FIXME: should this be counted toward the initiating task? */
2213 task_io_account_read(rdata->got_bytes);
2214 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2215 break;
2216 case MID_REQUEST_SUBMITTED:
2217 case MID_RETRY_NEEDED:
2218 rdata->result = -EAGAIN;
2219 if (server->sign && rdata->got_bytes)
2220 /* reset bytes number since we can not check a sign */
2221 rdata->got_bytes = 0;
2222 /* FIXME: should this be counted toward the initiating task? */
2223 task_io_account_read(rdata->got_bytes);
2224 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2225 break;
2226 default:
2227 if (rdata->result != -ENODATA)
2228 rdata->result = -EIO;
2231 if (rdata->result)
2232 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2234 queue_work(cifsiod_wq, &rdata->work);
2235 mutex_lock(&server->srv_mutex);
2236 DeleteMidQEntry(mid);
2237 mutex_unlock(&server->srv_mutex);
2238 add_credits(server, credits_received, 0);
2241 /* smb2_async_readv - send an async write, and set up mid to handle result */
2243 smb2_async_readv(struct cifs_readdata *rdata)
2245 int rc, flags = 0;
2246 struct smb2_hdr *buf;
2247 struct cifs_io_parms io_parms;
2248 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2249 .rq_nvec = 1 };
2250 struct TCP_Server_Info *server;
2252 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2253 __func__, rdata->offset, rdata->bytes);
2255 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2256 io_parms.offset = rdata->offset;
2257 io_parms.length = rdata->bytes;
2258 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2259 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2260 io_parms.pid = rdata->pid;
2262 server = io_parms.tcon->ses->server;
2264 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2265 if (rc) {
2266 if (rc == -EAGAIN && rdata->credits) {
2267 /* credits was reset by reconnect */
2268 rdata->credits = 0;
2269 /* reduce in_flight value since we won't send the req */
2270 spin_lock(&server->req_lock);
2271 server->in_flight--;
2272 spin_unlock(&server->req_lock);
2274 return rc;
2277 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2278 /* 4 for rfc1002 length field */
2279 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2281 if (rdata->credits) {
2282 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2283 SMB2_MAX_BUFFER_SIZE));
2284 buf->CreditRequest = buf->CreditCharge;
2285 spin_lock(&server->req_lock);
2286 server->credits += rdata->credits -
2287 le16_to_cpu(buf->CreditCharge);
2288 spin_unlock(&server->req_lock);
2289 wake_up(&server->request_q);
2290 flags = CIFS_HAS_CREDITS;
2293 kref_get(&rdata->refcount);
2294 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2295 cifs_readv_receive, smb2_readv_callback,
2296 rdata, flags);
2297 if (rc) {
2298 kref_put(&rdata->refcount, cifs_readdata_release);
2299 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2302 cifs_small_buf_release(buf);
2303 return rc;
2307 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2308 unsigned int *nbytes, char **buf, int *buf_type)
2310 int resp_buftype, rc = -EACCES;
2311 struct smb2_read_rsp *rsp = NULL;
2312 struct kvec iov[1];
2314 *nbytes = 0;
2315 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2316 if (rc)
2317 return rc;
2319 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2320 &resp_buftype, CIFS_LOG_ERROR);
2322 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2324 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2325 free_rsp_buf(resp_buftype, iov[0].iov_base);
2326 return 0;
2329 if (rc) {
2330 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2331 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2332 } else {
2333 *nbytes = le32_to_cpu(rsp->DataLength);
2334 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2335 (*nbytes > io_parms->length)) {
2336 cifs_dbg(FYI, "bad length %d for count %d\n",
2337 *nbytes, io_parms->length);
2338 rc = -EIO;
2339 *nbytes = 0;
2343 if (*buf) {
2344 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
2345 *nbytes);
2346 free_rsp_buf(resp_buftype, iov[0].iov_base);
2347 } else if (resp_buftype != CIFS_NO_BUFFER) {
2348 *buf = iov[0].iov_base;
2349 if (resp_buftype == CIFS_SMALL_BUFFER)
2350 *buf_type = CIFS_SMALL_BUFFER;
2351 else if (resp_buftype == CIFS_LARGE_BUFFER)
2352 *buf_type = CIFS_LARGE_BUFFER;
2354 return rc;
2358 * Check the mid_state and signature on received buffer (if any), and queue the
2359 * workqueue completion task.
2361 static void
2362 smb2_writev_callback(struct mid_q_entry *mid)
2364 struct cifs_writedata *wdata = mid->callback_data;
2365 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2366 struct TCP_Server_Info *server = tcon->ses->server;
2367 unsigned int written;
2368 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2369 unsigned int credits_received = 1;
2371 switch (mid->mid_state) {
2372 case MID_RESPONSE_RECEIVED:
2373 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2374 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2375 if (wdata->result != 0)
2376 break;
2378 written = le32_to_cpu(rsp->DataLength);
2380 * Mask off high 16 bits when bytes written as returned
2381 * by the server is greater than bytes requested by the
2382 * client. OS/2 servers are known to set incorrect
2383 * CountHigh values.
2385 if (written > wdata->bytes)
2386 written &= 0xFFFF;
2388 if (written < wdata->bytes)
2389 wdata->result = -ENOSPC;
2390 else
2391 wdata->bytes = written;
2392 break;
2393 case MID_REQUEST_SUBMITTED:
2394 case MID_RETRY_NEEDED:
2395 wdata->result = -EAGAIN;
2396 break;
2397 default:
2398 wdata->result = -EIO;
2399 break;
2402 if (wdata->result)
2403 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2405 queue_work(cifsiod_wq, &wdata->work);
2406 mutex_lock(&server->srv_mutex);
2407 DeleteMidQEntry(mid);
2408 mutex_unlock(&server->srv_mutex);
2409 add_credits(tcon->ses->server, credits_received, 0);
2412 /* smb2_async_writev - send an async write, and set up mid to handle result */
2414 smb2_async_writev(struct cifs_writedata *wdata,
2415 void (*release)(struct kref *kref))
2417 int rc = -EACCES, flags = 0;
2418 struct smb2_write_req *req = NULL;
2419 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2420 struct TCP_Server_Info *server = tcon->ses->server;
2421 struct kvec iov;
2422 struct smb_rqst rqst;
2424 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2425 if (rc) {
2426 if (rc == -EAGAIN && wdata->credits) {
2427 /* credits was reset by reconnect */
2428 wdata->credits = 0;
2429 /* reduce in_flight value since we won't send the req */
2430 spin_lock(&server->req_lock);
2431 server->in_flight--;
2432 spin_unlock(&server->req_lock);
2434 goto async_writev_out;
2437 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2439 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2440 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2441 req->WriteChannelInfoOffset = 0;
2442 req->WriteChannelInfoLength = 0;
2443 req->Channel = 0;
2444 req->Offset = cpu_to_le64(wdata->offset);
2445 /* 4 for rfc1002 length field */
2446 req->DataOffset = cpu_to_le16(
2447 offsetof(struct smb2_write_req, Buffer) - 4);
2448 req->RemainingBytes = 0;
2450 /* 4 for rfc1002 length field and 1 for Buffer */
2451 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2452 iov.iov_base = req;
2454 rqst.rq_iov = &iov;
2455 rqst.rq_nvec = 1;
2456 rqst.rq_pages = wdata->pages;
2457 rqst.rq_npages = wdata->nr_pages;
2458 rqst.rq_pagesz = wdata->pagesz;
2459 rqst.rq_tailsz = wdata->tailsz;
2461 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2462 wdata->offset, wdata->bytes);
2464 req->Length = cpu_to_le32(wdata->bytes);
2466 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2468 if (wdata->credits) {
2469 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2470 SMB2_MAX_BUFFER_SIZE));
2471 req->hdr.CreditRequest = req->hdr.CreditCharge;
2472 spin_lock(&server->req_lock);
2473 server->credits += wdata->credits -
2474 le16_to_cpu(req->hdr.CreditCharge);
2475 spin_unlock(&server->req_lock);
2476 wake_up(&server->request_q);
2477 flags = CIFS_HAS_CREDITS;
2480 kref_get(&wdata->refcount);
2481 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2482 flags);
2484 if (rc) {
2485 kref_put(&wdata->refcount, release);
2486 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2489 async_writev_out:
2490 cifs_small_buf_release(req);
2491 return rc;
2495 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2496 * The length field from io_parms must be at least 1 and indicates a number of
2497 * elements with data to write that begins with position 1 in iov array. All
2498 * data length is specified by count.
2501 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2502 unsigned int *nbytes, struct kvec *iov, int n_vec)
2504 int rc = 0;
2505 struct smb2_write_req *req = NULL;
2506 struct smb2_write_rsp *rsp = NULL;
2507 int resp_buftype;
2508 *nbytes = 0;
2510 if (n_vec < 1)
2511 return rc;
2513 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2514 if (rc)
2515 return rc;
2517 if (io_parms->tcon->ses->server == NULL)
2518 return -ECONNABORTED;
2520 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2522 req->PersistentFileId = io_parms->persistent_fid;
2523 req->VolatileFileId = io_parms->volatile_fid;
2524 req->WriteChannelInfoOffset = 0;
2525 req->WriteChannelInfoLength = 0;
2526 req->Channel = 0;
2527 req->Length = cpu_to_le32(io_parms->length);
2528 req->Offset = cpu_to_le64(io_parms->offset);
2529 /* 4 for rfc1002 length field */
2530 req->DataOffset = cpu_to_le16(
2531 offsetof(struct smb2_write_req, Buffer) - 4);
2532 req->RemainingBytes = 0;
2534 iov[0].iov_base = (char *)req;
2535 /* 4 for rfc1002 length field and 1 for Buffer */
2536 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2538 /* length of entire message including data to be written */
2539 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2541 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2542 &resp_buftype, 0);
2543 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2545 if (rc) {
2546 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2547 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2548 } else
2549 *nbytes = le32_to_cpu(rsp->DataLength);
2551 free_rsp_buf(resp_buftype, rsp);
2552 return rc;
2555 static unsigned int
2556 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2558 int len;
2559 unsigned int entrycount = 0;
2560 unsigned int next_offset = 0;
2561 FILE_DIRECTORY_INFO *entryptr;
2563 if (bufstart == NULL)
2564 return 0;
2566 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2568 while (1) {
2569 entryptr = (FILE_DIRECTORY_INFO *)
2570 ((char *)entryptr + next_offset);
2572 if ((char *)entryptr + size > end_of_buf) {
2573 cifs_dbg(VFS, "malformed search entry would overflow\n");
2574 break;
2577 len = le32_to_cpu(entryptr->FileNameLength);
2578 if ((char *)entryptr + len + size > end_of_buf) {
2579 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2580 end_of_buf);
2581 break;
2584 *lastentry = (char *)entryptr;
2585 entrycount++;
2587 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2588 if (!next_offset)
2589 break;
2592 return entrycount;
2596 * Readdir/FindFirst
2599 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2600 u64 persistent_fid, u64 volatile_fid, int index,
2601 struct cifs_search_info *srch_inf)
2603 struct smb2_query_directory_req *req;
2604 struct smb2_query_directory_rsp *rsp = NULL;
2605 struct kvec iov[2];
2606 int rc = 0;
2607 int len;
2608 int resp_buftype = CIFS_NO_BUFFER;
2609 unsigned char *bufptr;
2610 struct TCP_Server_Info *server;
2611 struct cifs_ses *ses = tcon->ses;
2612 __le16 asteriks = cpu_to_le16('*');
2613 char *end_of_smb;
2614 unsigned int output_size = CIFSMaxBufSize;
2615 size_t info_buf_size;
2617 if (ses && (ses->server))
2618 server = ses->server;
2619 else
2620 return -EIO;
2622 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2623 if (rc)
2624 return rc;
2626 switch (srch_inf->info_level) {
2627 case SMB_FIND_FILE_DIRECTORY_INFO:
2628 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2629 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2630 break;
2631 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2632 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2633 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2634 break;
2635 default:
2636 cifs_dbg(VFS, "info level %u isn't supported\n",
2637 srch_inf->info_level);
2638 rc = -EINVAL;
2639 goto qdir_exit;
2642 req->FileIndex = cpu_to_le32(index);
2643 req->PersistentFileId = persistent_fid;
2644 req->VolatileFileId = volatile_fid;
2646 len = 0x2;
2647 bufptr = req->Buffer;
2648 memcpy(bufptr, &asteriks, len);
2650 req->FileNameOffset =
2651 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2652 req->FileNameLength = cpu_to_le16(len);
2654 * BB could be 30 bytes or so longer if we used SMB2 specific
2655 * buffer lengths, but this is safe and close enough.
2657 output_size = min_t(unsigned int, output_size, server->maxBuf);
2658 output_size = min_t(unsigned int, output_size, 2 << 15);
2659 req->OutputBufferLength = cpu_to_le32(output_size);
2661 iov[0].iov_base = (char *)req;
2662 /* 4 for RFC1001 length and 1 for Buffer */
2663 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2665 iov[1].iov_base = (char *)(req->Buffer);
2666 iov[1].iov_len = len;
2668 inc_rfc1001_len(req, len - 1 /* Buffer */);
2670 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2671 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2673 if (rc) {
2674 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2675 srch_inf->endOfSearch = true;
2676 rc = 0;
2678 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2679 goto qdir_exit;
2682 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2683 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2684 info_buf_size);
2685 if (rc)
2686 goto qdir_exit;
2688 srch_inf->unicode = true;
2690 if (srch_inf->ntwrk_buf_start) {
2691 if (srch_inf->smallBuf)
2692 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2693 else
2694 cifs_buf_release(srch_inf->ntwrk_buf_start);
2696 srch_inf->ntwrk_buf_start = (char *)rsp;
2697 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2698 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2699 /* 4 for rfc1002 length field */
2700 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2701 srch_inf->entries_in_buffer =
2702 num_entries(srch_inf->srch_entries_start, end_of_smb,
2703 &srch_inf->last_entry, info_buf_size);
2704 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2705 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2706 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2707 srch_inf->srch_entries_start, srch_inf->last_entry);
2708 if (resp_buftype == CIFS_LARGE_BUFFER)
2709 srch_inf->smallBuf = false;
2710 else if (resp_buftype == CIFS_SMALL_BUFFER)
2711 srch_inf->smallBuf = true;
2712 else
2713 cifs_dbg(VFS, "illegal search buffer type\n");
2715 return rc;
2717 qdir_exit:
2718 free_rsp_buf(resp_buftype, rsp);
2719 return rc;
2722 static int
2723 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2724 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2725 unsigned int num, void **data, unsigned int *size)
2727 struct smb2_set_info_req *req;
2728 struct smb2_set_info_rsp *rsp = NULL;
2729 struct kvec *iov;
2730 int rc = 0;
2731 int resp_buftype;
2732 unsigned int i;
2733 struct TCP_Server_Info *server;
2734 struct cifs_ses *ses = tcon->ses;
2736 if (ses && (ses->server))
2737 server = ses->server;
2738 else
2739 return -EIO;
2741 if (!num)
2742 return -EINVAL;
2744 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2745 if (!iov)
2746 return -ENOMEM;
2748 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2749 if (rc) {
2750 kfree(iov);
2751 return rc;
2754 req->hdr.ProcessId = cpu_to_le32(pid);
2756 req->InfoType = SMB2_O_INFO_FILE;
2757 req->FileInfoClass = info_class;
2758 req->PersistentFileId = persistent_fid;
2759 req->VolatileFileId = volatile_fid;
2761 /* 4 for RFC1001 length and 1 for Buffer */
2762 req->BufferOffset =
2763 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2764 req->BufferLength = cpu_to_le32(*size);
2766 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2768 memcpy(req->Buffer, *data, *size);
2770 iov[0].iov_base = (char *)req;
2771 /* 4 for RFC1001 length */
2772 iov[0].iov_len = get_rfc1002_length(req) + 4;
2774 for (i = 1; i < num; i++) {
2775 inc_rfc1001_len(req, size[i]);
2776 le32_add_cpu(&req->BufferLength, size[i]);
2777 iov[i].iov_base = (char *)data[i];
2778 iov[i].iov_len = size[i];
2781 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2782 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2784 if (rc != 0)
2785 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2787 free_rsp_buf(resp_buftype, rsp);
2788 kfree(iov);
2789 return rc;
2793 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2794 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2796 struct smb2_file_rename_info info;
2797 void **data;
2798 unsigned int size[2];
2799 int rc;
2800 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2802 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2803 if (!data)
2804 return -ENOMEM;
2806 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2807 /* 0 = fail if target already exists */
2808 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2809 info.FileNameLength = cpu_to_le32(len);
2811 data[0] = &info;
2812 size[0] = sizeof(struct smb2_file_rename_info);
2814 data[1] = target_file;
2815 size[1] = len + 2 /* null */;
2817 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2818 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2819 size);
2820 kfree(data);
2821 return rc;
2825 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2826 u64 persistent_fid, u64 volatile_fid)
2828 __u8 delete_pending = 1;
2829 void *data;
2830 unsigned int size;
2832 data = &delete_pending;
2833 size = 1; /* sizeof __u8 */
2835 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2836 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2837 &size);
2841 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2842 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2844 struct smb2_file_link_info info;
2845 void **data;
2846 unsigned int size[2];
2847 int rc;
2848 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2850 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2851 if (!data)
2852 return -ENOMEM;
2854 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2855 /* 0 = fail if link already exists */
2856 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2857 info.FileNameLength = cpu_to_le32(len);
2859 data[0] = &info;
2860 size[0] = sizeof(struct smb2_file_link_info);
2862 data[1] = target_file;
2863 size[1] = len + 2 /* null */;
2865 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2866 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2867 kfree(data);
2868 return rc;
2872 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2873 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2875 struct smb2_file_eof_info info;
2876 void *data;
2877 unsigned int size;
2879 info.EndOfFile = *eof;
2881 data = &info;
2882 size = sizeof(struct smb2_file_eof_info);
2884 if (is_falloc)
2885 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2886 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2887 else
2888 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2889 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2893 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2894 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2896 unsigned int size;
2897 size = sizeof(FILE_BASIC_INFO);
2898 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2899 current->tgid, FILE_BASIC_INFORMATION, 1,
2900 (void **)&buf, &size);
2904 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2905 const u64 persistent_fid, const u64 volatile_fid,
2906 __u8 oplock_level)
2908 int rc;
2909 struct smb2_oplock_break *req = NULL;
2911 cifs_dbg(FYI, "SMB2_oplock_break\n");
2912 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2914 if (rc)
2915 return rc;
2917 req->VolatileFid = volatile_fid;
2918 req->PersistentFid = persistent_fid;
2919 req->OplockLevel = oplock_level;
2920 req->hdr.CreditRequest = cpu_to_le16(1);
2922 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2923 /* SMB2 buffer freed by function above */
2925 if (rc) {
2926 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2927 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2930 return rc;
2933 static void
2934 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2935 struct kstatfs *kst)
2937 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2938 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2939 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2940 kst->f_bfree = kst->f_bavail =
2941 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2942 return;
2945 static int
2946 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2947 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2949 int rc;
2950 struct smb2_query_info_req *req;
2952 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2954 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2955 return -EIO;
2957 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2958 if (rc)
2959 return rc;
2961 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2962 req->FileInfoClass = level;
2963 req->PersistentFileId = persistent_fid;
2964 req->VolatileFileId = volatile_fid;
2965 /* 4 for rfc1002 length field and 1 for pad */
2966 req->InputBufferOffset =
2967 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2968 req->OutputBufferLength = cpu_to_le32(
2969 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2971 iov->iov_base = (char *)req;
2972 /* 4 for rfc1002 length field */
2973 iov->iov_len = get_rfc1002_length(req) + 4;
2974 return 0;
2978 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2979 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2981 struct smb2_query_info_rsp *rsp = NULL;
2982 struct kvec iov;
2983 int rc = 0;
2984 int resp_buftype;
2985 struct cifs_ses *ses = tcon->ses;
2986 struct smb2_fs_full_size_info *info = NULL;
2988 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2989 sizeof(struct smb2_fs_full_size_info),
2990 persistent_fid, volatile_fid);
2991 if (rc)
2992 return rc;
2994 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2995 if (rc) {
2996 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2997 goto qfsinf_exit;
2999 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3001 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3002 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3003 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3004 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3005 sizeof(struct smb2_fs_full_size_info));
3006 if (!rc)
3007 copy_fs_info_to_kstatfs(info, fsdata);
3009 qfsinf_exit:
3010 free_rsp_buf(resp_buftype, iov.iov_base);
3011 return rc;
3015 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3016 u64 persistent_fid, u64 volatile_fid, int level)
3018 struct smb2_query_info_rsp *rsp = NULL;
3019 struct kvec iov;
3020 int rc = 0;
3021 int resp_buftype, max_len, min_len;
3022 struct cifs_ses *ses = tcon->ses;
3023 unsigned int rsp_len, offset;
3025 if (level == FS_DEVICE_INFORMATION) {
3026 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3027 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3028 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3029 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3030 min_len = MIN_FS_ATTR_INFO_SIZE;
3031 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3032 max_len = sizeof(struct smb3_fs_ss_info);
3033 min_len = sizeof(struct smb3_fs_ss_info);
3034 } else {
3035 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3036 return -EINVAL;
3039 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3040 persistent_fid, volatile_fid);
3041 if (rc)
3042 return rc;
3044 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3045 if (rc) {
3046 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3047 goto qfsattr_exit;
3049 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3051 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3052 offset = le16_to_cpu(rsp->OutputBufferOffset);
3053 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3054 if (rc)
3055 goto qfsattr_exit;
3057 if (level == FS_ATTRIBUTE_INFORMATION)
3058 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3059 + (char *)&rsp->hdr, min_t(unsigned int,
3060 rsp_len, max_len));
3061 else if (level == FS_DEVICE_INFORMATION)
3062 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3063 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3064 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3065 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3066 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3067 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3068 tcon->perf_sector_size =
3069 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3072 qfsattr_exit:
3073 free_rsp_buf(resp_buftype, iov.iov_base);
3074 return rc;
3078 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3079 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3080 const __u32 num_lock, struct smb2_lock_element *buf)
3082 int rc = 0;
3083 struct smb2_lock_req *req = NULL;
3084 struct kvec iov[2];
3085 int resp_buf_type;
3086 unsigned int count;
3088 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3090 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3091 if (rc)
3092 return rc;
3094 req->hdr.ProcessId = cpu_to_le32(pid);
3095 req->LockCount = cpu_to_le16(num_lock);
3097 req->PersistentFileId = persist_fid;
3098 req->VolatileFileId = volatile_fid;
3100 count = num_lock * sizeof(struct smb2_lock_element);
3101 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3103 iov[0].iov_base = (char *)req;
3104 /* 4 for rfc1002 length field and count for all locks */
3105 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3106 iov[1].iov_base = (char *)buf;
3107 iov[1].iov_len = count;
3109 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3110 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3111 if (rc) {
3112 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3113 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3116 return rc;
3120 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3121 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3122 const __u64 length, const __u64 offset, const __u32 lock_flags,
3123 const bool wait)
3125 struct smb2_lock_element lock;
3127 lock.Offset = cpu_to_le64(offset);
3128 lock.Length = cpu_to_le64(length);
3129 lock.Flags = cpu_to_le32(lock_flags);
3130 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3131 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3133 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3137 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3138 __u8 *lease_key, const __le32 lease_state)
3140 int rc;
3141 struct smb2_lease_ack *req = NULL;
3143 cifs_dbg(FYI, "SMB2_lease_break\n");
3144 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3146 if (rc)
3147 return rc;
3149 req->hdr.CreditRequest = cpu_to_le16(1);
3150 req->StructureSize = cpu_to_le16(36);
3151 inc_rfc1001_len(req, 12);
3153 memcpy(req->LeaseKey, lease_key, 16);
3154 req->LeaseState = lease_state;
3156 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3157 /* SMB2 buffer freed by function above */
3159 if (rc) {
3160 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3161 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3164 return rc;