crypto: s5p-sss - Fix kernel Oops in AES-ECB mode
[linux/fpc-iii.git] / fs / cifs / smb2pdu.c
blob66af1f8a13cc71cdfda751eed338c59fa233a910
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/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
39 #include "smb2pdu.h"
40 #include "cifsglob.h"
41 #include "cifsacl.h"
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
46 #include "ntlmssp.h"
47 #include "smb2status.h"
48 #include "smb2glob.h"
49 #include "cifspdu.h"
50 #include "cifs_spnego.h"
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
59 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_LOGOFF */ 4,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_CREATE */ 57,
66 /* SMB2_CLOSE */ 24,
67 /* SMB2_FLUSH */ 24,
68 /* SMB2_READ */ 49,
69 /* SMB2_WRITE */ 49,
70 /* SMB2_LOCK */ 48,
71 /* SMB2_IOCTL */ 57,
72 /* SMB2_CANCEL */ 4,
73 /* SMB2_ECHO */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static int encryption_required(const struct cifs_tcon *tcon)
83 if (!tcon)
84 return 0;
85 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
87 return 1;
88 if (tcon->seal &&
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
90 return 1;
91 return 0;
94 static void
95 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
96 const struct cifs_tcon *tcon)
98 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
106 if (server->credits >= server->max_credits)
107 shdr->CreditRequest = cpu_to_le16(0);
108 else
109 shdr->CreditRequest = cpu_to_le16(
110 min_t(int, server->max_credits -
111 server->credits, 2));
112 spin_unlock(&server->req_lock);
113 } else {
114 shdr->CreditRequest = cpu_to_le16(2);
116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
118 if (!tcon)
119 goto out;
121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
123 if ((tcon->ses) && (tcon->ses->server) &&
124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
125 shdr->CreditCharge = cpu_to_le16(1);
126 /* else CreditCharge MBZ */
128 shdr->TreeId = tcon->tid;
129 /* Uid is not converted */
130 if (tcon->ses)
131 shdr->SessionId = tcon->ses->Suid;
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
143 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
148 shdr->Flags |= SMB2_FLAGS_SIGNED;
149 out:
150 return;
153 static int
154 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
156 int rc = 0;
157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
166 if (tcon == NULL)
167 return rc;
169 if (smb2_command == SMB2_TREE_CONNECT)
170 return rc;
172 if (tcon->tidStatus == CifsExiting) {
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 smb2_command);
183 return -ENODEV;
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
188 return -EIO;
190 ses = tcon->ses;
191 server = ses->server;
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
197 while (server->tcpStatus == CifsNeedReconnect) {
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
202 switch (smb2_command) {
204 * BB Should we keep oplock break and add flush to exceptions?
206 case SMB2_TREE_DISCONNECT:
207 case SMB2_CANCEL:
208 case SMB2_CLOSE:
209 case SMB2_OPLOCK_BREAK:
210 return -EAGAIN;
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
218 break;
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
223 * back on-line
225 if (!tcon->retry) {
226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
227 return -EHOSTDOWN;
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
232 return rc;
234 nls_codepage = load_nls_default();
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
240 mutex_lock(&tcon->ses->session_mutex);
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
247 if (server->tcpStatus == CifsNeedReconnect) {
248 rc = -EHOSTDOWN;
249 mutex_unlock(&tcon->ses->session_mutex);
250 goto out;
253 rc = cifs_negotiate_protocol(0, tcon->ses);
254 if (!rc && tcon->ses->need_reconnect)
255 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
257 if (rc || !tcon->need_reconnect) {
258 mutex_unlock(&tcon->ses->session_mutex);
259 goto out;
262 cifs_mark_open_files_invalid(tcon);
263 if (tcon->use_persistent)
264 tcon->need_reopen_files = true;
266 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
267 mutex_unlock(&tcon->ses->session_mutex);
269 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
270 if (rc)
271 goto out;
273 if (smb2_command != SMB2_INTERNAL_CMD)
274 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
276 atomic_inc(&tconInfoReconnectCount);
277 out:
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
286 switch (smb2_command) {
287 case SMB2_FLUSH:
288 case SMB2_READ:
289 case SMB2_WRITE:
290 case SMB2_LOCK:
291 case SMB2_IOCTL:
292 case SMB2_QUERY_DIRECTORY:
293 case SMB2_CHANGE_NOTIFY:
294 case SMB2_QUERY_INFO:
295 case SMB2_SET_INFO:
296 rc = -EAGAIN;
298 unload_nls(nls_codepage);
299 return rc;
302 static void
303 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
304 unsigned int *total_len)
306 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
314 memset(buf, 0, 256);
316 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
317 spdu->StructureSize2 = cpu_to_le16(parmsize);
319 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
322 /* init request without RFC1001 length at the beginning */
323 static int
324 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
325 void **request_buf, unsigned int *total_len)
327 int rc;
328 struct smb2_sync_hdr *shdr;
330 rc = smb2_reconnect(smb2_command, tcon);
331 if (rc)
332 return rc;
334 /* BB eventually switch this to SMB2 specific small buf size */
335 *request_buf = cifs_small_buf_get();
336 if (*request_buf == NULL) {
337 /* BB should we add a retry in here if not a writepage? */
338 return -ENOMEM;
341 shdr = (struct smb2_sync_hdr *)(*request_buf);
343 fill_small_buf(smb2_command, tcon, shdr, total_len);
345 if (tcon != NULL) {
346 #ifdef CONFIG_CIFS_STATS2
347 uint16_t com_code = le16_to_cpu(smb2_command);
349 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
350 #endif
351 cifs_stats_inc(&tcon->num_smbs_sent);
354 return rc;
358 * Allocate and return pointer to an SMB request hdr, and set basic
359 * SMB information in the SMB header. If the return code is zero, this
360 * function must have filled in request_buf pointer. The returned buffer
361 * has RFC1001 length at the beginning.
363 static int
364 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
365 void **request_buf)
367 int rc;
368 unsigned int total_len;
369 struct smb2_pdu *pdu;
371 rc = smb2_reconnect(smb2_command, tcon);
372 if (rc)
373 return rc;
375 /* BB eventually switch this to SMB2 specific small buf size */
376 *request_buf = cifs_small_buf_get();
377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
379 return -ENOMEM;
382 pdu = (struct smb2_pdu *)(*request_buf);
384 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
386 /* Note this is only network field converted to big endian */
387 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
389 if (tcon != NULL) {
390 #ifdef CONFIG_CIFS_STATS2
391 uint16_t com_code = le16_to_cpu(smb2_command);
392 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
393 #endif
394 cifs_stats_inc(&tcon->num_smbs_sent);
397 return rc;
400 #ifdef CONFIG_CIFS_SMB311
401 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
402 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
405 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
406 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
408 static void
409 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
411 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
412 pneg_ctxt->DataLength = cpu_to_le16(38);
413 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
414 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
415 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
416 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
419 static void
420 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
422 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
423 pneg_ctxt->DataLength = cpu_to_le16(6);
424 pneg_ctxt->CipherCount = cpu_to_le16(2);
425 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
426 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
429 static void
430 assemble_neg_contexts(struct smb2_negotiate_req *req)
433 /* +4 is to account for the RFC1001 len field */
434 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
436 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
437 /* Add 2 to size to round to 8 byte boundary */
438 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
439 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
440 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
441 req->NegotiateContextCount = cpu_to_le16(2);
442 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
443 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
445 #else
446 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
448 return;
450 #endif /* SMB311 */
454 * SMB2 Worker functions follow:
456 * The general structure of the worker functions is:
457 * 1) Call smb2_init (assembles SMB2 header)
458 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
459 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
460 * 4) Decode SMB2 command specific fields in the fixed length area
461 * 5) Decode variable length data area (if any for this SMB2 command type)
462 * 6) Call free smb buffer
463 * 7) return
468 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
470 struct smb2_negotiate_req *req;
471 struct smb2_negotiate_rsp *rsp;
472 struct kvec iov[1];
473 struct kvec rsp_iov;
474 int rc = 0;
475 int resp_buftype;
476 struct TCP_Server_Info *server = ses->server;
477 int blob_offset, blob_length;
478 char *security_blob;
479 int flags = CIFS_NEG_OP;
481 cifs_dbg(FYI, "Negotiate protocol\n");
483 if (!server) {
484 WARN(1, "%s: server is NULL!\n", __func__);
485 return -EIO;
488 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
489 if (rc)
490 return rc;
492 req->hdr.sync_hdr.SessionId = 0;
494 if (strcmp(ses->server->vals->version_string,
495 SMB3ANY_VERSION_STRING) == 0) {
496 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
497 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
498 req->DialectCount = cpu_to_le16(2);
499 inc_rfc1001_len(req, 4);
500 } else if (strcmp(ses->server->vals->version_string,
501 SMBDEFAULT_VERSION_STRING) == 0) {
502 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
503 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
504 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
505 req->DialectCount = cpu_to_le16(3);
506 inc_rfc1001_len(req, 6);
507 } else {
508 /* otherwise send specific dialect */
509 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
510 req->DialectCount = cpu_to_le16(1);
511 inc_rfc1001_len(req, 2);
514 /* only one of SMB2 signing flags may be set in SMB2 request */
515 if (ses->sign)
516 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
517 else if (global_secflags & CIFSSEC_MAY_SIGN)
518 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
519 else
520 req->SecurityMode = 0;
522 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
524 /* ClientGUID must be zero for SMB2.02 dialect */
525 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
526 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
527 else {
528 memcpy(req->ClientGUID, server->client_guid,
529 SMB2_CLIENT_GUID_SIZE);
530 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
531 assemble_neg_contexts(req);
533 iov[0].iov_base = (char *)req;
534 /* 4 for rfc1002 length field */
535 iov[0].iov_len = get_rfc1002_length(req) + 4;
537 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
538 cifs_small_buf_release(req);
539 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
541 * No tcon so can't do
542 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
544 if (rc == -EOPNOTSUPP) {
545 cifs_dbg(VFS, "Dialect not supported by server. Consider "
546 "specifying vers=1.0 or vers=2.0 on mount for accessing"
547 " older servers\n");
548 goto neg_exit;
549 } else if (rc != 0)
550 goto neg_exit;
552 if (strcmp(ses->server->vals->version_string,
553 SMB3ANY_VERSION_STRING) == 0) {
554 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
555 cifs_dbg(VFS,
556 "SMB2 dialect returned but not requested\n");
557 return -EIO;
558 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
559 cifs_dbg(VFS,
560 "SMB2.1 dialect returned but not requested\n");
561 return -EIO;
563 } else if (strcmp(ses->server->vals->version_string,
564 SMBDEFAULT_VERSION_STRING) == 0) {
565 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
566 cifs_dbg(VFS,
567 "SMB2 dialect returned but not requested\n");
568 return -EIO;
569 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
570 /* ops set to 3.0 by default for default so update */
571 ses->server->ops = &smb21_operations;
573 } else if (le16_to_cpu(rsp->DialectRevision) !=
574 ses->server->vals->protocol_id) {
575 /* if requested single dialect ensure returned dialect matched */
576 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
577 le16_to_cpu(rsp->DialectRevision));
578 return -EIO;
581 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
583 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
584 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
585 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
586 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
587 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
588 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
589 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
590 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
591 #ifdef CONFIG_CIFS_SMB311
592 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
593 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
594 #endif /* SMB311 */
595 else {
596 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
597 le16_to_cpu(rsp->DialectRevision));
598 rc = -EIO;
599 goto neg_exit;
601 server->dialect = le16_to_cpu(rsp->DialectRevision);
603 /* BB: add check that dialect was valid given dialect(s) we asked for */
605 /* SMB2 only has an extended negflavor */
606 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
607 /* set it to the maximum buffer size value we can send with 1 credit */
608 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
609 SMB2_MAX_BUFFER_SIZE);
610 server->max_read = le32_to_cpu(rsp->MaxReadSize);
611 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
612 /* BB Do we need to validate the SecurityMode? */
613 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
614 server->capabilities = le32_to_cpu(rsp->Capabilities);
615 /* Internal types */
616 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
618 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
619 &rsp->hdr);
621 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
622 * for us will be
623 * ses->sectype = RawNTLMSSP;
624 * but for time being this is our only auth choice so doesn't matter.
625 * We just found a server which sets blob length to zero expecting raw.
627 if (blob_length == 0) {
628 cifs_dbg(FYI, "missing security blob on negprot\n");
629 server->sec_ntlmssp = true;
632 rc = cifs_enable_signing(server, ses->sign);
633 if (rc)
634 goto neg_exit;
635 if (blob_length) {
636 rc = decode_negTokenInit(security_blob, blob_length, server);
637 if (rc == 1)
638 rc = 0;
639 else if (rc == 0)
640 rc = -EIO;
642 neg_exit:
643 free_rsp_buf(resp_buftype, rsp);
644 return rc;
647 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
649 int rc = 0;
650 struct validate_negotiate_info_req vneg_inbuf;
651 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
652 u32 rsplen;
653 u32 inbuflen; /* max of 4 dialects */
655 cifs_dbg(FYI, "validate negotiate\n");
658 * validation ioctl must be signed, so no point sending this if we
659 * can not sign it (ie are not known user). Even if signing is not
660 * required (enabled but not negotiated), in those cases we selectively
661 * sign just this, the first and only signed request on a connection.
662 * Having validation of negotiate info helps reduce attack vectors.
664 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
665 return 0; /* validation requires signing */
667 if (tcon->ses->user_name == NULL) {
668 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
669 return 0; /* validation requires signing */
672 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
673 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
675 vneg_inbuf.Capabilities =
676 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
677 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
678 SMB2_CLIENT_GUID_SIZE);
680 if (tcon->ses->sign)
681 vneg_inbuf.SecurityMode =
682 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
683 else if (global_secflags & CIFSSEC_MAY_SIGN)
684 vneg_inbuf.SecurityMode =
685 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
686 else
687 vneg_inbuf.SecurityMode = 0;
690 if (strcmp(tcon->ses->server->vals->version_string,
691 SMB3ANY_VERSION_STRING) == 0) {
692 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
693 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
694 vneg_inbuf.DialectCount = cpu_to_le16(2);
695 /* structure is big enough for 3 dialects, sending only 2 */
696 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
697 } else if (strcmp(tcon->ses->server->vals->version_string,
698 SMBDEFAULT_VERSION_STRING) == 0) {
699 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
700 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
701 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
702 vneg_inbuf.DialectCount = cpu_to_le16(3);
703 /* structure is big enough for 3 dialects */
704 inbuflen = sizeof(struct validate_negotiate_info_req);
705 } else {
706 /* otherwise specific dialect was requested */
707 vneg_inbuf.Dialects[0] =
708 cpu_to_le16(tcon->ses->server->vals->protocol_id);
709 vneg_inbuf.DialectCount = cpu_to_le16(1);
710 /* structure is big enough for 3 dialects, sending only 1 */
711 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
714 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
715 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
716 false /* use_ipc */,
717 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
718 (char **)&pneg_rsp, &rsplen);
720 if (rc != 0) {
721 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
722 return -EIO;
725 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
726 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
727 rsplen);
729 /* relax check since Mac returns max bufsize allowed on ioctl */
730 if ((rsplen > CIFSMaxBufSize)
731 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
732 goto err_rsp_free;
735 /* check validate negotiate info response matches what we got earlier */
736 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
737 goto vneg_out;
739 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
740 goto vneg_out;
742 /* do not validate server guid because not saved at negprot time yet */
744 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
745 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
746 goto vneg_out;
748 /* validate negotiate successful */
749 cifs_dbg(FYI, "validate negotiate info successful\n");
750 kfree(pneg_rsp);
751 return 0;
753 vneg_out:
754 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
755 err_rsp_free:
756 kfree(pneg_rsp);
757 return -EIO;
760 enum securityEnum
761 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
763 switch (requested) {
764 case Kerberos:
765 case RawNTLMSSP:
766 return requested;
767 case NTLMv2:
768 return RawNTLMSSP;
769 case Unspecified:
770 if (server->sec_ntlmssp &&
771 (global_secflags & CIFSSEC_MAY_NTLMSSP))
772 return RawNTLMSSP;
773 if ((server->sec_kerberos || server->sec_mskerberos) &&
774 (global_secflags & CIFSSEC_MAY_KRB5))
775 return Kerberos;
776 /* Fallthrough */
777 default:
778 return Unspecified;
782 struct SMB2_sess_data {
783 unsigned int xid;
784 struct cifs_ses *ses;
785 struct nls_table *nls_cp;
786 void (*func)(struct SMB2_sess_data *);
787 int result;
788 u64 previous_session;
790 /* we will send the SMB in three pieces:
791 * a fixed length beginning part, an optional
792 * SPNEGO blob (which can be zero length), and a
793 * last part which will include the strings
794 * and rest of bcc area. This allows us to avoid
795 * a large buffer 17K allocation
797 int buf0_type;
798 struct kvec iov[2];
801 static int
802 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
804 int rc;
805 struct cifs_ses *ses = sess_data->ses;
806 struct smb2_sess_setup_req *req;
807 struct TCP_Server_Info *server = ses->server;
809 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
810 if (rc)
811 return rc;
813 /* First session, not a reauthenticate */
814 req->hdr.sync_hdr.SessionId = 0;
816 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
817 req->PreviousSessionId = sess_data->previous_session;
819 req->Flags = 0; /* MBZ */
820 /* to enable echos and oplocks */
821 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
823 /* only one of SMB2 signing flags may be set in SMB2 request */
824 if (server->sign)
825 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
826 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
827 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
828 else
829 req->SecurityMode = 0;
831 req->Capabilities = 0;
832 req->Channel = 0; /* MBZ */
834 sess_data->iov[0].iov_base = (char *)req;
835 /* 4 for rfc1002 length field and 1 for pad */
836 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
838 * This variable will be used to clear the buffer
839 * allocated above in case of any error in the calling function.
841 sess_data->buf0_type = CIFS_SMALL_BUFFER;
843 return 0;
846 static void
847 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
849 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
850 sess_data->buf0_type = CIFS_NO_BUFFER;
853 static int
854 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
856 int rc;
857 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
858 struct kvec rsp_iov = { NULL, 0 };
860 /* Testing shows that buffer offset must be at location of Buffer[0] */
861 req->SecurityBufferOffset =
862 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
863 1 /* pad */ - 4 /* rfc1001 len */);
864 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
866 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
868 /* BB add code to build os and lm fields */
870 rc = SendReceive2(sess_data->xid, sess_data->ses,
871 sess_data->iov, 2,
872 &sess_data->buf0_type,
873 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
874 cifs_small_buf_release(sess_data->iov[0].iov_base);
875 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
877 return rc;
880 static int
881 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
883 int rc = 0;
884 struct cifs_ses *ses = sess_data->ses;
886 mutex_lock(&ses->server->srv_mutex);
887 if (ses->server->ops->generate_signingkey) {
888 rc = ses->server->ops->generate_signingkey(ses);
889 if (rc) {
890 cifs_dbg(FYI,
891 "SMB3 session key generation failed\n");
892 mutex_unlock(&ses->server->srv_mutex);
893 return rc;
896 if (!ses->server->session_estab) {
897 ses->server->sequence_number = 0x2;
898 ses->server->session_estab = true;
900 mutex_unlock(&ses->server->srv_mutex);
902 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
903 spin_lock(&GlobalMid_Lock);
904 ses->status = CifsGood;
905 ses->need_reconnect = false;
906 spin_unlock(&GlobalMid_Lock);
907 return rc;
910 #ifdef CONFIG_CIFS_UPCALL
911 static void
912 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
914 int rc;
915 struct cifs_ses *ses = sess_data->ses;
916 struct cifs_spnego_msg *msg;
917 struct key *spnego_key = NULL;
918 struct smb2_sess_setup_rsp *rsp = NULL;
920 rc = SMB2_sess_alloc_buffer(sess_data);
921 if (rc)
922 goto out;
924 spnego_key = cifs_get_spnego_key(ses);
925 if (IS_ERR(spnego_key)) {
926 rc = PTR_ERR(spnego_key);
927 spnego_key = NULL;
928 goto out;
931 msg = spnego_key->payload.data[0];
933 * check version field to make sure that cifs.upcall is
934 * sending us a response in an expected form
936 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
937 cifs_dbg(VFS,
938 "bad cifs.upcall version. Expected %d got %d",
939 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
940 rc = -EKEYREJECTED;
941 goto out_put_spnego_key;
944 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
945 GFP_KERNEL);
946 if (!ses->auth_key.response) {
947 cifs_dbg(VFS,
948 "Kerberos can't allocate (%u bytes) memory",
949 msg->sesskey_len);
950 rc = -ENOMEM;
951 goto out_put_spnego_key;
953 ses->auth_key.len = msg->sesskey_len;
955 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
956 sess_data->iov[1].iov_len = msg->secblob_len;
958 rc = SMB2_sess_sendreceive(sess_data);
959 if (rc)
960 goto out_put_spnego_key;
962 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
963 ses->Suid = rsp->hdr.sync_hdr.SessionId;
965 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
967 rc = SMB2_sess_establish_session(sess_data);
968 out_put_spnego_key:
969 key_invalidate(spnego_key);
970 key_put(spnego_key);
971 out:
972 sess_data->result = rc;
973 sess_data->func = NULL;
974 SMB2_sess_free_buffer(sess_data);
976 #else
977 static void
978 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
980 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
981 sess_data->result = -EOPNOTSUPP;
982 sess_data->func = NULL;
984 #endif
986 static void
987 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
989 static void
990 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
992 int rc;
993 struct cifs_ses *ses = sess_data->ses;
994 struct smb2_sess_setup_rsp *rsp = NULL;
995 char *ntlmssp_blob = NULL;
996 bool use_spnego = false; /* else use raw ntlmssp */
997 u16 blob_length = 0;
1000 * If memory allocation is successful, caller of this function
1001 * frees it.
1003 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1004 if (!ses->ntlmssp) {
1005 rc = -ENOMEM;
1006 goto out_err;
1008 ses->ntlmssp->sesskey_per_smbsess = true;
1010 rc = SMB2_sess_alloc_buffer(sess_data);
1011 if (rc)
1012 goto out_err;
1014 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1015 GFP_KERNEL);
1016 if (ntlmssp_blob == NULL) {
1017 rc = -ENOMEM;
1018 goto out;
1021 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1022 if (use_spnego) {
1023 /* BB eventually need to add this */
1024 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1025 rc = -EOPNOTSUPP;
1026 goto out;
1027 } else {
1028 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1029 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1031 sess_data->iov[1].iov_base = ntlmssp_blob;
1032 sess_data->iov[1].iov_len = blob_length;
1034 rc = SMB2_sess_sendreceive(sess_data);
1035 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1037 /* If true, rc here is expected and not an error */
1038 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1039 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1040 rc = 0;
1042 if (rc)
1043 goto out;
1045 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1046 le16_to_cpu(rsp->SecurityBufferOffset)) {
1047 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1048 le16_to_cpu(rsp->SecurityBufferOffset));
1049 rc = -EIO;
1050 goto out;
1052 rc = decode_ntlmssp_challenge(rsp->Buffer,
1053 le16_to_cpu(rsp->SecurityBufferLength), ses);
1054 if (rc)
1055 goto out;
1057 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1060 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1061 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1063 out:
1064 kfree(ntlmssp_blob);
1065 SMB2_sess_free_buffer(sess_data);
1066 if (!rc) {
1067 sess_data->result = 0;
1068 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1069 return;
1071 out_err:
1072 kfree(ses->ntlmssp);
1073 ses->ntlmssp = NULL;
1074 sess_data->result = rc;
1075 sess_data->func = NULL;
1078 static void
1079 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1081 int rc;
1082 struct cifs_ses *ses = sess_data->ses;
1083 struct smb2_sess_setup_req *req;
1084 struct smb2_sess_setup_rsp *rsp = NULL;
1085 unsigned char *ntlmssp_blob = NULL;
1086 bool use_spnego = false; /* else use raw ntlmssp */
1087 u16 blob_length = 0;
1089 rc = SMB2_sess_alloc_buffer(sess_data);
1090 if (rc)
1091 goto out;
1093 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1094 req->hdr.sync_hdr.SessionId = ses->Suid;
1096 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1097 sess_data->nls_cp);
1098 if (rc) {
1099 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1100 goto out;
1103 if (use_spnego) {
1104 /* BB eventually need to add this */
1105 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1106 rc = -EOPNOTSUPP;
1107 goto out;
1109 sess_data->iov[1].iov_base = ntlmssp_blob;
1110 sess_data->iov[1].iov_len = blob_length;
1112 rc = SMB2_sess_sendreceive(sess_data);
1113 if (rc)
1114 goto out;
1116 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1118 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1119 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1121 rc = SMB2_sess_establish_session(sess_data);
1122 out:
1123 kfree(ntlmssp_blob);
1124 SMB2_sess_free_buffer(sess_data);
1125 kfree(ses->ntlmssp);
1126 ses->ntlmssp = NULL;
1127 sess_data->result = rc;
1128 sess_data->func = NULL;
1131 static int
1132 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1134 int type;
1136 type = smb2_select_sectype(ses->server, ses->sectype);
1137 cifs_dbg(FYI, "sess setup type %d\n", type);
1138 if (type == Unspecified) {
1139 cifs_dbg(VFS,
1140 "Unable to select appropriate authentication method!");
1141 return -EINVAL;
1144 switch (type) {
1145 case Kerberos:
1146 sess_data->func = SMB2_auth_kerberos;
1147 break;
1148 case RawNTLMSSP:
1149 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1150 break;
1151 default:
1152 cifs_dbg(VFS, "secType %d not supported!\n", type);
1153 return -EOPNOTSUPP;
1156 return 0;
1160 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1161 const struct nls_table *nls_cp)
1163 int rc = 0;
1164 struct TCP_Server_Info *server = ses->server;
1165 struct SMB2_sess_data *sess_data;
1167 cifs_dbg(FYI, "Session Setup\n");
1169 if (!server) {
1170 WARN(1, "%s: server is NULL!\n", __func__);
1171 return -EIO;
1174 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1175 if (!sess_data)
1176 return -ENOMEM;
1178 rc = SMB2_select_sec(ses, sess_data);
1179 if (rc)
1180 goto out;
1181 sess_data->xid = xid;
1182 sess_data->ses = ses;
1183 sess_data->buf0_type = CIFS_NO_BUFFER;
1184 sess_data->nls_cp = (struct nls_table *) nls_cp;
1186 while (sess_data->func)
1187 sess_data->func(sess_data);
1189 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1190 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1191 rc = sess_data->result;
1192 out:
1193 kfree(sess_data);
1194 return rc;
1198 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1200 struct smb2_logoff_req *req; /* response is also trivial struct */
1201 int rc = 0;
1202 struct TCP_Server_Info *server;
1203 int flags = 0;
1205 cifs_dbg(FYI, "disconnect session %p\n", ses);
1207 if (ses && (ses->server))
1208 server = ses->server;
1209 else
1210 return -EIO;
1212 /* no need to send SMB logoff if uid already closed due to reconnect */
1213 if (ses->need_reconnect)
1214 goto smb2_session_already_dead;
1216 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1217 if (rc)
1218 return rc;
1220 /* since no tcon, smb2_init can not do this, so do here */
1221 req->hdr.sync_hdr.SessionId = ses->Suid;
1223 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1224 flags |= CIFS_TRANSFORM_REQ;
1225 else if (server->sign)
1226 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1228 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
1229 cifs_small_buf_release(req);
1231 * No tcon so can't do
1232 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1235 smb2_session_already_dead:
1236 return rc;
1239 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1241 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1244 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1246 /* These are similar values to what Windows uses */
1247 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1249 tcon->max_chunks = 256;
1250 tcon->max_bytes_chunk = 1048576;
1251 tcon->max_bytes_copy = 16777216;
1255 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1256 struct cifs_tcon *tcon, const struct nls_table *cp)
1258 struct smb2_tree_connect_req *req;
1259 struct smb2_tree_connect_rsp *rsp = NULL;
1260 struct kvec iov[2];
1261 struct kvec rsp_iov = { NULL, 0 };
1262 int rc = 0;
1263 int resp_buftype;
1264 int unc_path_len;
1265 __le16 *unc_path = NULL;
1266 int flags = 0;
1268 cifs_dbg(FYI, "TCON\n");
1270 if (!(ses->server) || !tree)
1271 return -EIO;
1273 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1274 if (unc_path == NULL)
1275 return -ENOMEM;
1277 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1278 unc_path_len *= 2;
1279 if (unc_path_len < 2) {
1280 kfree(unc_path);
1281 return -EINVAL;
1284 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1285 if (tcon)
1286 tcon->tid = 0;
1288 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1289 if (rc) {
1290 kfree(unc_path);
1291 return rc;
1294 if (tcon == NULL) {
1295 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1296 flags |= CIFS_TRANSFORM_REQ;
1298 /* since no tcon, smb2_init can not do this, so do here */
1299 req->hdr.sync_hdr.SessionId = ses->Suid;
1300 if (ses->server->sign)
1301 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1302 } else if (encryption_required(tcon))
1303 flags |= CIFS_TRANSFORM_REQ;
1305 iov[0].iov_base = (char *)req;
1306 /* 4 for rfc1002 length field and 1 for pad */
1307 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1309 /* Testing shows that buffer offset must be at location of Buffer[0] */
1310 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1311 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1312 req->PathLength = cpu_to_le16(unc_path_len - 2);
1313 iov[1].iov_base = unc_path;
1314 iov[1].iov_len = unc_path_len;
1316 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1318 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1319 cifs_small_buf_release(req);
1320 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1322 if (rc != 0) {
1323 if (tcon) {
1324 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1325 tcon->need_reconnect = true;
1327 goto tcon_error_exit;
1330 if (tcon == NULL) {
1331 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1332 goto tcon_exit;
1335 switch (rsp->ShareType) {
1336 case SMB2_SHARE_TYPE_DISK:
1337 cifs_dbg(FYI, "connection to disk share\n");
1338 break;
1339 case SMB2_SHARE_TYPE_PIPE:
1340 tcon->ipc = true;
1341 cifs_dbg(FYI, "connection to pipe share\n");
1342 break;
1343 case SMB2_SHARE_TYPE_PRINT:
1344 tcon->ipc = true;
1345 cifs_dbg(FYI, "connection to printer\n");
1346 break;
1347 default:
1348 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1349 rc = -EOPNOTSUPP;
1350 goto tcon_error_exit;
1353 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1354 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1355 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1356 tcon->tidStatus = CifsGood;
1357 tcon->need_reconnect = false;
1358 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1359 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1361 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1362 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1363 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1365 if (tcon->seal &&
1366 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1367 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1369 init_copy_chunk_defaults(tcon);
1370 if (tcon->ses->server->ops->validate_negotiate)
1371 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1372 tcon_exit:
1373 free_rsp_buf(resp_buftype, rsp);
1374 kfree(unc_path);
1375 return rc;
1377 tcon_error_exit:
1378 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1379 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1381 goto tcon_exit;
1385 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1387 struct smb2_tree_disconnect_req *req; /* response is trivial */
1388 int rc = 0;
1389 struct cifs_ses *ses = tcon->ses;
1390 int flags = 0;
1392 cifs_dbg(FYI, "Tree Disconnect\n");
1394 if (!ses || !(ses->server))
1395 return -EIO;
1397 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1398 return 0;
1400 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1401 if (rc)
1402 return rc;
1404 if (encryption_required(tcon))
1405 flags |= CIFS_TRANSFORM_REQ;
1407 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
1408 cifs_small_buf_release(req);
1409 if (rc)
1410 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1412 return rc;
1416 static struct create_durable *
1417 create_durable_buf(void)
1419 struct create_durable *buf;
1421 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1422 if (!buf)
1423 return NULL;
1425 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1426 (struct create_durable, Data));
1427 buf->ccontext.DataLength = cpu_to_le32(16);
1428 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1429 (struct create_durable, Name));
1430 buf->ccontext.NameLength = cpu_to_le16(4);
1431 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1432 buf->Name[0] = 'D';
1433 buf->Name[1] = 'H';
1434 buf->Name[2] = 'n';
1435 buf->Name[3] = 'Q';
1436 return buf;
1439 static struct create_durable *
1440 create_reconnect_durable_buf(struct cifs_fid *fid)
1442 struct create_durable *buf;
1444 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1445 if (!buf)
1446 return NULL;
1448 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1449 (struct create_durable, Data));
1450 buf->ccontext.DataLength = cpu_to_le32(16);
1451 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1452 (struct create_durable, Name));
1453 buf->ccontext.NameLength = cpu_to_le16(4);
1454 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1455 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1456 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1457 buf->Name[0] = 'D';
1458 buf->Name[1] = 'H';
1459 buf->Name[2] = 'n';
1460 buf->Name[3] = 'C';
1461 return buf;
1464 static __u8
1465 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1466 unsigned int *epoch)
1468 char *data_offset;
1469 struct create_context *cc;
1470 unsigned int next;
1471 unsigned int remaining;
1472 char *name;
1474 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1475 remaining = le32_to_cpu(rsp->CreateContextsLength);
1476 cc = (struct create_context *)data_offset;
1477 while (remaining >= sizeof(struct create_context)) {
1478 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1479 if (le16_to_cpu(cc->NameLength) == 4 &&
1480 strncmp(name, "RqLs", 4) == 0)
1481 return server->ops->parse_lease_buf(cc, epoch);
1483 next = le32_to_cpu(cc->Next);
1484 if (!next)
1485 break;
1486 remaining -= next;
1487 cc = (struct create_context *)((char *)cc + next);
1490 return 0;
1493 static int
1494 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1495 unsigned int *num_iovec, __u8 *oplock)
1497 struct smb2_create_req *req = iov[0].iov_base;
1498 unsigned int num = *num_iovec;
1500 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1501 if (iov[num].iov_base == NULL)
1502 return -ENOMEM;
1503 iov[num].iov_len = server->vals->create_lease_size;
1504 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1505 if (!req->CreateContextsOffset)
1506 req->CreateContextsOffset = cpu_to_le32(
1507 sizeof(struct smb2_create_req) - 4 +
1508 iov[num - 1].iov_len);
1509 le32_add_cpu(&req->CreateContextsLength,
1510 server->vals->create_lease_size);
1511 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1512 *num_iovec = num + 1;
1513 return 0;
1516 static struct create_durable_v2 *
1517 create_durable_v2_buf(struct cifs_fid *pfid)
1519 struct create_durable_v2 *buf;
1521 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1522 if (!buf)
1523 return NULL;
1525 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1526 (struct create_durable_v2, dcontext));
1527 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1528 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1529 (struct create_durable_v2, Name));
1530 buf->ccontext.NameLength = cpu_to_le16(4);
1532 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1533 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1534 generate_random_uuid(buf->dcontext.CreateGuid);
1535 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1537 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1538 buf->Name[0] = 'D';
1539 buf->Name[1] = 'H';
1540 buf->Name[2] = '2';
1541 buf->Name[3] = 'Q';
1542 return buf;
1545 static struct create_durable_handle_reconnect_v2 *
1546 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1548 struct create_durable_handle_reconnect_v2 *buf;
1550 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1551 GFP_KERNEL);
1552 if (!buf)
1553 return NULL;
1555 buf->ccontext.DataOffset =
1556 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1557 dcontext));
1558 buf->ccontext.DataLength =
1559 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1560 buf->ccontext.NameOffset =
1561 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1562 Name));
1563 buf->ccontext.NameLength = cpu_to_le16(4);
1565 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1566 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1567 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1568 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1570 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1571 buf->Name[0] = 'D';
1572 buf->Name[1] = 'H';
1573 buf->Name[2] = '2';
1574 buf->Name[3] = 'C';
1575 return buf;
1578 static int
1579 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1580 struct cifs_open_parms *oparms)
1582 struct smb2_create_req *req = iov[0].iov_base;
1583 unsigned int num = *num_iovec;
1585 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1586 if (iov[num].iov_base == NULL)
1587 return -ENOMEM;
1588 iov[num].iov_len = sizeof(struct create_durable_v2);
1589 if (!req->CreateContextsOffset)
1590 req->CreateContextsOffset =
1591 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1592 iov[1].iov_len);
1593 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1594 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1595 *num_iovec = num + 1;
1596 return 0;
1599 static int
1600 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1601 struct cifs_open_parms *oparms)
1603 struct smb2_create_req *req = iov[0].iov_base;
1604 unsigned int num = *num_iovec;
1606 /* indicate that we don't need to relock the file */
1607 oparms->reconnect = false;
1609 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1610 if (iov[num].iov_base == NULL)
1611 return -ENOMEM;
1612 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1613 if (!req->CreateContextsOffset)
1614 req->CreateContextsOffset =
1615 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1616 iov[1].iov_len);
1617 le32_add_cpu(&req->CreateContextsLength,
1618 sizeof(struct create_durable_handle_reconnect_v2));
1619 inc_rfc1001_len(&req->hdr,
1620 sizeof(struct create_durable_handle_reconnect_v2));
1621 *num_iovec = num + 1;
1622 return 0;
1625 static int
1626 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1627 struct cifs_open_parms *oparms, bool use_persistent)
1629 struct smb2_create_req *req = iov[0].iov_base;
1630 unsigned int num = *num_iovec;
1632 if (use_persistent) {
1633 if (oparms->reconnect)
1634 return add_durable_reconnect_v2_context(iov, num_iovec,
1635 oparms);
1636 else
1637 return add_durable_v2_context(iov, num_iovec, oparms);
1640 if (oparms->reconnect) {
1641 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1642 /* indicate that we don't need to relock the file */
1643 oparms->reconnect = false;
1644 } else
1645 iov[num].iov_base = create_durable_buf();
1646 if (iov[num].iov_base == NULL)
1647 return -ENOMEM;
1648 iov[num].iov_len = sizeof(struct create_durable);
1649 if (!req->CreateContextsOffset)
1650 req->CreateContextsOffset =
1651 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1652 iov[1].iov_len);
1653 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1654 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1655 *num_iovec = num + 1;
1656 return 0;
1659 static int
1660 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1661 const char *treename, const __le16 *path)
1663 int treename_len, path_len;
1664 struct nls_table *cp;
1665 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1668 * skip leading "\\"
1670 treename_len = strlen(treename);
1671 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1672 return -EINVAL;
1674 treename += 2;
1675 treename_len -= 2;
1677 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1680 * make room for one path separator between the treename and
1681 * path
1683 *out_len = treename_len + 1 + path_len;
1686 * final path needs to be null-terminated UTF16 with a
1687 * size aligned to 8
1690 *out_size = roundup((*out_len+1)*2, 8);
1691 *out_path = kzalloc(*out_size, GFP_KERNEL);
1692 if (!*out_path)
1693 return -ENOMEM;
1695 cp = load_nls_default();
1696 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1697 UniStrcat(*out_path, sep);
1698 UniStrcat(*out_path, path);
1699 unload_nls(cp);
1701 return 0;
1705 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1706 __u8 *oplock, struct smb2_file_all_info *buf,
1707 struct smb2_err_rsp **err_buf)
1709 struct smb2_create_req *req;
1710 struct smb2_create_rsp *rsp;
1711 struct TCP_Server_Info *server;
1712 struct cifs_tcon *tcon = oparms->tcon;
1713 struct cifs_ses *ses = tcon->ses;
1714 struct kvec iov[4];
1715 struct kvec rsp_iov = {NULL, 0};
1716 int resp_buftype;
1717 int uni_path_len;
1718 __le16 *copy_path = NULL;
1719 int copy_size;
1720 int rc = 0;
1721 unsigned int n_iov = 2;
1722 __u32 file_attributes = 0;
1723 char *dhc_buf = NULL, *lc_buf = NULL;
1724 int flags = 0;
1726 cifs_dbg(FYI, "create/open\n");
1728 if (ses && (ses->server))
1729 server = ses->server;
1730 else
1731 return -EIO;
1733 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1734 if (rc)
1735 return rc;
1737 if (encryption_required(tcon))
1738 flags |= CIFS_TRANSFORM_REQ;
1740 if (oparms->create_options & CREATE_OPTION_READONLY)
1741 file_attributes |= ATTR_READONLY;
1742 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1743 file_attributes |= ATTR_SYSTEM;
1745 req->ImpersonationLevel = IL_IMPERSONATION;
1746 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1747 /* File attributes ignored on open (used in create though) */
1748 req->FileAttributes = cpu_to_le32(file_attributes);
1749 req->ShareAccess = FILE_SHARE_ALL_LE;
1750 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1751 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1753 iov[0].iov_base = (char *)req;
1754 /* 4 for rfc1002 length field */
1755 iov[0].iov_len = get_rfc1002_length(req) + 4;
1756 /* -1 since last byte is buf[0] which is sent below (path) */
1757 iov[0].iov_len--;
1759 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1761 /* [MS-SMB2] 2.2.13 NameOffset:
1762 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1763 * the SMB2 header, the file name includes a prefix that will
1764 * be processed during DFS name normalization as specified in
1765 * section 3.3.5.9. Otherwise, the file name is relative to
1766 * the share that is identified by the TreeId in the SMB2
1767 * header.
1769 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1770 int name_len;
1772 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1773 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1774 &name_len,
1775 tcon->treeName, path);
1776 if (rc)
1777 return rc;
1778 req->NameLength = cpu_to_le16(name_len * 2);
1779 uni_path_len = copy_size;
1780 path = copy_path;
1781 } else {
1782 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1783 /* MUST set path len (NameLength) to 0 opening root of share */
1784 req->NameLength = cpu_to_le16(uni_path_len - 2);
1785 if (uni_path_len % 8 != 0) {
1786 copy_size = roundup(uni_path_len, 8);
1787 copy_path = kzalloc(copy_size, GFP_KERNEL);
1788 if (!copy_path)
1789 return -ENOMEM;
1790 memcpy((char *)copy_path, (const char *)path,
1791 uni_path_len);
1792 uni_path_len = copy_size;
1793 path = copy_path;
1797 iov[1].iov_len = uni_path_len;
1798 iov[1].iov_base = path;
1799 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1800 inc_rfc1001_len(req, uni_path_len - 1);
1802 if (!server->oplocks)
1803 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1805 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1806 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1807 req->RequestedOplockLevel = *oplock;
1808 else {
1809 rc = add_lease_context(server, iov, &n_iov, oplock);
1810 if (rc) {
1811 cifs_small_buf_release(req);
1812 kfree(copy_path);
1813 return rc;
1815 lc_buf = iov[n_iov-1].iov_base;
1818 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1819 /* need to set Next field of lease context if we request it */
1820 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1821 struct create_context *ccontext =
1822 (struct create_context *)iov[n_iov-1].iov_base;
1823 ccontext->Next =
1824 cpu_to_le32(server->vals->create_lease_size);
1827 rc = add_durable_context(iov, &n_iov, oparms,
1828 tcon->use_persistent);
1829 if (rc) {
1830 cifs_small_buf_release(req);
1831 kfree(copy_path);
1832 kfree(lc_buf);
1833 return rc;
1835 dhc_buf = iov[n_iov-1].iov_base;
1838 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1839 cifs_small_buf_release(req);
1840 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1842 if (rc != 0) {
1843 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1844 if (err_buf && rsp)
1845 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1846 GFP_KERNEL);
1847 goto creat_exit;
1850 oparms->fid->persistent_fid = rsp->PersistentFileId;
1851 oparms->fid->volatile_fid = rsp->VolatileFileId;
1853 if (buf) {
1854 memcpy(buf, &rsp->CreationTime, 32);
1855 buf->AllocationSize = rsp->AllocationSize;
1856 buf->EndOfFile = rsp->EndofFile;
1857 buf->Attributes = rsp->FileAttributes;
1858 buf->NumberOfLinks = cpu_to_le32(1);
1859 buf->DeletePending = 0;
1862 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1863 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1864 else
1865 *oplock = rsp->OplockLevel;
1866 creat_exit:
1867 kfree(copy_path);
1868 kfree(lc_buf);
1869 kfree(dhc_buf);
1870 free_rsp_buf(resp_buftype, rsp);
1871 return rc;
1875 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1878 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1879 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1880 char *in_data, u32 indatalen,
1881 char **out_data, u32 *plen /* returned data len */)
1883 struct smb2_ioctl_req *req;
1884 struct smb2_ioctl_rsp *rsp;
1885 struct smb2_sync_hdr *shdr;
1886 struct cifs_ses *ses;
1887 struct kvec iov[2];
1888 struct kvec rsp_iov;
1889 int resp_buftype;
1890 int n_iov;
1891 int rc = 0;
1892 int flags = 0;
1894 cifs_dbg(FYI, "SMB2 IOCTL\n");
1896 if (out_data != NULL)
1897 *out_data = NULL;
1899 /* zero out returned data len, in case of error */
1900 if (plen)
1901 *plen = 0;
1903 if (tcon)
1904 ses = tcon->ses;
1905 else
1906 return -EIO;
1908 if (!ses || !(ses->server))
1909 return -EIO;
1911 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1912 if (rc)
1913 return rc;
1915 if (use_ipc) {
1916 if (ses->ipc_tid == 0) {
1917 cifs_small_buf_release(req);
1918 return -ENOTCONN;
1921 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1922 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1923 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1925 if (encryption_required(tcon))
1926 flags |= CIFS_TRANSFORM_REQ;
1928 req->CtlCode = cpu_to_le32(opcode);
1929 req->PersistentFileId = persistent_fid;
1930 req->VolatileFileId = volatile_fid;
1932 if (indatalen) {
1933 req->InputCount = cpu_to_le32(indatalen);
1934 /* do not set InputOffset if no input data */
1935 req->InputOffset =
1936 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1937 iov[1].iov_base = in_data;
1938 iov[1].iov_len = indatalen;
1939 n_iov = 2;
1940 } else
1941 n_iov = 1;
1943 req->OutputOffset = 0;
1944 req->OutputCount = 0; /* MBZ */
1947 * Could increase MaxOutputResponse, but that would require more
1948 * than one credit. Windows typically sets this smaller, but for some
1949 * ioctls it may be useful to allow server to send more. No point
1950 * limiting what the server can send as long as fits in one credit
1951 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1952 * (by default, note that it can be overridden to make max larger)
1953 * in responses (except for read responses which can be bigger.
1954 * We may want to bump this limit up
1956 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1958 if (is_fsctl)
1959 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1960 else
1961 req->Flags = 0;
1963 iov[0].iov_base = (char *)req;
1966 * If no input data, the size of ioctl struct in
1967 * protocol spec still includes a 1 byte data buffer,
1968 * but if input data passed to ioctl, we do not
1969 * want to double count this, so we do not send
1970 * the dummy one byte of data in iovec[0] if sending
1971 * input data (in iovec[1]). We also must add 4 bytes
1972 * in first iovec to allow for rfc1002 length field.
1975 if (indatalen) {
1976 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1977 inc_rfc1001_len(req, indatalen - 1);
1978 } else
1979 iov[0].iov_len = get_rfc1002_length(req) + 4;
1981 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1982 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1983 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1985 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1986 cifs_small_buf_release(req);
1987 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1989 if ((rc != 0) && (rc != -EINVAL)) {
1990 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1991 goto ioctl_exit;
1992 } else if (rc == -EINVAL) {
1993 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1994 (opcode != FSCTL_SRV_COPYCHUNK)) {
1995 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1996 goto ioctl_exit;
2000 /* check if caller wants to look at return data or just return rc */
2001 if ((plen == NULL) || (out_data == NULL))
2002 goto ioctl_exit;
2004 *plen = le32_to_cpu(rsp->OutputCount);
2006 /* We check for obvious errors in the output buffer length and offset */
2007 if (*plen == 0)
2008 goto ioctl_exit; /* server returned no data */
2009 else if (*plen > 0xFF00) {
2010 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2011 *plen = 0;
2012 rc = -EIO;
2013 goto ioctl_exit;
2016 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2017 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2018 le32_to_cpu(rsp->OutputOffset));
2019 *plen = 0;
2020 rc = -EIO;
2021 goto ioctl_exit;
2024 *out_data = kmalloc(*plen, GFP_KERNEL);
2025 if (*out_data == NULL) {
2026 rc = -ENOMEM;
2027 goto ioctl_exit;
2030 shdr = get_sync_hdr(rsp);
2031 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
2032 ioctl_exit:
2033 free_rsp_buf(resp_buftype, rsp);
2034 return rc;
2038 * Individual callers to ioctl worker function follow
2042 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2043 u64 persistent_fid, u64 volatile_fid)
2045 int rc;
2046 struct compress_ioctl fsctl_input;
2047 char *ret_data = NULL;
2049 fsctl_input.CompressionState =
2050 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2052 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2053 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2054 false /* use_ipc */,
2055 (char *)&fsctl_input /* data input */,
2056 2 /* in data len */, &ret_data /* out data */, NULL);
2058 cifs_dbg(FYI, "set compression rc %d\n", rc);
2060 return rc;
2064 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2065 u64 persistent_fid, u64 volatile_fid)
2067 struct smb2_close_req *req;
2068 struct smb2_close_rsp *rsp;
2069 struct cifs_ses *ses = tcon->ses;
2070 struct kvec iov[1];
2071 struct kvec rsp_iov;
2072 int resp_buftype;
2073 int rc = 0;
2074 int flags = 0;
2076 cifs_dbg(FYI, "Close\n");
2078 if (!ses || !(ses->server))
2079 return -EIO;
2081 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2082 if (rc)
2083 return rc;
2085 if (encryption_required(tcon))
2086 flags |= CIFS_TRANSFORM_REQ;
2088 req->PersistentFileId = persistent_fid;
2089 req->VolatileFileId = volatile_fid;
2091 iov[0].iov_base = (char *)req;
2092 /* 4 for rfc1002 length field */
2093 iov[0].iov_len = get_rfc1002_length(req) + 4;
2095 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2096 cifs_small_buf_release(req);
2097 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2099 if (rc != 0) {
2100 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2101 goto close_exit;
2104 /* BB FIXME - decode close response, update inode for caching */
2106 close_exit:
2107 free_rsp_buf(resp_buftype, rsp);
2108 return rc;
2111 static int
2112 validate_buf(unsigned int offset, unsigned int buffer_length,
2113 struct smb2_hdr *hdr, unsigned int min_buf_size)
2116 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2117 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2118 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2119 char *end_of_buf = begin_of_buf + buffer_length;
2122 if (buffer_length < min_buf_size) {
2123 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2124 buffer_length, min_buf_size);
2125 return -EINVAL;
2128 /* check if beyond RFC1001 maximum length */
2129 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2130 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2131 buffer_length, smb_len);
2132 return -EINVAL;
2135 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2136 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2137 return -EINVAL;
2140 return 0;
2144 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2145 * Caller must free buffer.
2147 static int
2148 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2149 struct smb2_hdr *hdr, unsigned int minbufsize,
2150 char *data)
2153 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2154 int rc;
2156 if (!data)
2157 return -EINVAL;
2159 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2160 if (rc)
2161 return rc;
2163 memcpy(data, begin_of_buf, buffer_length);
2165 return 0;
2168 static int
2169 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2170 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2171 u32 additional_info, size_t output_len, size_t min_len, void **data,
2172 u32 *dlen)
2174 struct smb2_query_info_req *req;
2175 struct smb2_query_info_rsp *rsp = NULL;
2176 struct kvec iov[2];
2177 struct kvec rsp_iov;
2178 int rc = 0;
2179 int resp_buftype;
2180 struct cifs_ses *ses = tcon->ses;
2181 int flags = 0;
2183 cifs_dbg(FYI, "Query Info\n");
2185 if (!ses || !(ses->server))
2186 return -EIO;
2188 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2189 if (rc)
2190 return rc;
2192 if (encryption_required(tcon))
2193 flags |= CIFS_TRANSFORM_REQ;
2195 req->InfoType = info_type;
2196 req->FileInfoClass = info_class;
2197 req->PersistentFileId = persistent_fid;
2198 req->VolatileFileId = volatile_fid;
2199 req->AdditionalInformation = cpu_to_le32(additional_info);
2202 * We do not use the input buffer (do not send extra byte)
2204 req->InputBufferOffset = 0;
2205 inc_rfc1001_len(req, -1);
2207 req->OutputBufferLength = cpu_to_le32(output_len);
2209 iov[0].iov_base = (char *)req;
2210 /* 4 for rfc1002 length field */
2211 iov[0].iov_len = get_rfc1002_length(req) + 4;
2213 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2214 cifs_small_buf_release(req);
2215 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2217 if (rc) {
2218 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2219 goto qinf_exit;
2222 if (dlen) {
2223 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2224 if (!*data) {
2225 *data = kmalloc(*dlen, GFP_KERNEL);
2226 if (!*data) {
2227 cifs_dbg(VFS,
2228 "Error %d allocating memory for acl\n",
2229 rc);
2230 *dlen = 0;
2231 goto qinf_exit;
2236 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2237 le32_to_cpu(rsp->OutputBufferLength),
2238 &rsp->hdr, min_len, *data);
2240 qinf_exit:
2241 free_rsp_buf(resp_buftype, rsp);
2242 return rc;
2245 int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2246 u64 persistent_fid, u64 volatile_fid,
2247 int ea_buf_size, struct smb2_file_full_ea_info *data)
2249 return query_info(xid, tcon, persistent_fid, volatile_fid,
2250 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2251 ea_buf_size,
2252 sizeof(struct smb2_file_full_ea_info),
2253 (void **)&data,
2254 NULL);
2257 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2258 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2260 return query_info(xid, tcon, persistent_fid, volatile_fid,
2261 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2262 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2263 sizeof(struct smb2_file_all_info), (void **)&data,
2264 NULL);
2268 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2269 u64 persistent_fid, u64 volatile_fid,
2270 void **data, u32 *plen)
2272 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2273 *plen = 0;
2275 return query_info(xid, tcon, persistent_fid, volatile_fid,
2276 0, SMB2_O_INFO_SECURITY, additional_info,
2277 SMB2_MAX_BUFFER_SIZE,
2278 sizeof(struct smb2_file_all_info), data, plen);
2282 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2283 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2285 return query_info(xid, tcon, persistent_fid, volatile_fid,
2286 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2287 sizeof(struct smb2_file_internal_info),
2288 sizeof(struct smb2_file_internal_info),
2289 (void **)&uniqueid, NULL);
2293 * This is a no-op for now. We're not really interested in the reply, but
2294 * rather in the fact that the server sent one and that server->lstrp
2295 * gets updated.
2297 * FIXME: maybe we should consider checking that the reply matches request?
2299 static void
2300 smb2_echo_callback(struct mid_q_entry *mid)
2302 struct TCP_Server_Info *server = mid->callback_data;
2303 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2304 unsigned int credits_received = 1;
2306 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2307 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2309 DeleteMidQEntry(mid);
2310 add_credits(server, credits_received, CIFS_ECHO_OP);
2313 void smb2_reconnect_server(struct work_struct *work)
2315 struct TCP_Server_Info *server = container_of(work,
2316 struct TCP_Server_Info, reconnect.work);
2317 struct cifs_ses *ses;
2318 struct cifs_tcon *tcon, *tcon2;
2319 struct list_head tmp_list;
2320 int tcon_exist = false;
2321 int rc;
2322 int resched = false;
2325 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2326 mutex_lock(&server->reconnect_mutex);
2328 INIT_LIST_HEAD(&tmp_list);
2329 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2331 spin_lock(&cifs_tcp_ses_lock);
2332 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2333 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2334 if (tcon->need_reconnect || tcon->need_reopen_files) {
2335 tcon->tc_count++;
2336 list_add_tail(&tcon->rlist, &tmp_list);
2337 tcon_exist = true;
2342 * Get the reference to server struct to be sure that the last call of
2343 * cifs_put_tcon() in the loop below won't release the server pointer.
2345 if (tcon_exist)
2346 server->srv_count++;
2348 spin_unlock(&cifs_tcp_ses_lock);
2350 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2351 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2352 if (!rc)
2353 cifs_reopen_persistent_handles(tcon);
2354 else
2355 resched = true;
2356 list_del_init(&tcon->rlist);
2357 cifs_put_tcon(tcon);
2360 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2361 if (resched)
2362 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2363 mutex_unlock(&server->reconnect_mutex);
2365 /* now we can safely release srv struct */
2366 if (tcon_exist)
2367 cifs_put_tcp_session(server, 1);
2371 SMB2_echo(struct TCP_Server_Info *server)
2373 struct smb2_echo_req *req;
2374 int rc = 0;
2375 struct kvec iov[2];
2376 struct smb_rqst rqst = { .rq_iov = iov,
2377 .rq_nvec = 2 };
2379 cifs_dbg(FYI, "In echo request\n");
2381 if (server->tcpStatus == CifsNeedNegotiate) {
2382 /* No need to send echo on newly established connections */
2383 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2384 return rc;
2387 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2388 if (rc)
2389 return rc;
2391 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2393 /* 4 for rfc1002 length field */
2394 iov[0].iov_len = 4;
2395 iov[0].iov_base = (char *)req;
2396 iov[1].iov_len = get_rfc1002_length(req);
2397 iov[1].iov_base = (char *)req + 4;
2399 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2400 server, CIFS_ECHO_OP);
2401 if (rc)
2402 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2404 cifs_small_buf_release(req);
2405 return rc;
2409 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2410 u64 volatile_fid)
2412 struct smb2_flush_req *req;
2413 struct cifs_ses *ses = tcon->ses;
2414 struct kvec iov[1];
2415 struct kvec rsp_iov;
2416 int resp_buftype;
2417 int rc = 0;
2418 int flags = 0;
2420 cifs_dbg(FYI, "Flush\n");
2422 if (!ses || !(ses->server))
2423 return -EIO;
2425 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2426 if (rc)
2427 return rc;
2429 if (encryption_required(tcon))
2430 flags |= CIFS_TRANSFORM_REQ;
2432 req->PersistentFileId = persistent_fid;
2433 req->VolatileFileId = volatile_fid;
2435 iov[0].iov_base = (char *)req;
2436 /* 4 for rfc1002 length field */
2437 iov[0].iov_len = get_rfc1002_length(req) + 4;
2439 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2440 cifs_small_buf_release(req);
2442 if (rc != 0)
2443 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2445 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2446 return rc;
2450 * To form a chain of read requests, any read requests after the first should
2451 * have the end_of_chain boolean set to true.
2453 static int
2454 smb2_new_read_req(void **buf, unsigned int *total_len,
2455 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2456 int request_type)
2458 int rc = -EACCES;
2459 struct smb2_read_plain_req *req = NULL;
2460 struct smb2_sync_hdr *shdr;
2462 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2463 total_len);
2464 if (rc)
2465 return rc;
2466 if (io_parms->tcon->ses->server == NULL)
2467 return -ECONNABORTED;
2469 shdr = &req->sync_hdr;
2470 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2472 req->PersistentFileId = io_parms->persistent_fid;
2473 req->VolatileFileId = io_parms->volatile_fid;
2474 req->ReadChannelInfoOffset = 0; /* reserved */
2475 req->ReadChannelInfoLength = 0; /* reserved */
2476 req->Channel = 0; /* reserved */
2477 req->MinimumCount = 0;
2478 req->Length = cpu_to_le32(io_parms->length);
2479 req->Offset = cpu_to_le64(io_parms->offset);
2481 if (request_type & CHAINED_REQUEST) {
2482 if (!(request_type & END_OF_CHAIN)) {
2483 /* next 8-byte aligned request */
2484 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2485 shdr->NextCommand = cpu_to_le32(*total_len);
2486 } else /* END_OF_CHAIN */
2487 shdr->NextCommand = 0;
2488 if (request_type & RELATED_REQUEST) {
2489 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2491 * Related requests use info from previous read request
2492 * in chain.
2494 shdr->SessionId = 0xFFFFFFFF;
2495 shdr->TreeId = 0xFFFFFFFF;
2496 req->PersistentFileId = 0xFFFFFFFF;
2497 req->VolatileFileId = 0xFFFFFFFF;
2500 if (remaining_bytes > io_parms->length)
2501 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2502 else
2503 req->RemainingBytes = 0;
2505 *buf = req;
2506 return rc;
2509 static void
2510 smb2_readv_callback(struct mid_q_entry *mid)
2512 struct cifs_readdata *rdata = mid->callback_data;
2513 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2514 struct TCP_Server_Info *server = tcon->ses->server;
2515 struct smb2_sync_hdr *shdr =
2516 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2517 unsigned int credits_received = 1;
2518 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2519 .rq_nvec = 2,
2520 .rq_pages = rdata->pages,
2521 .rq_npages = rdata->nr_pages,
2522 .rq_pagesz = rdata->pagesz,
2523 .rq_tailsz = rdata->tailsz };
2525 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2526 __func__, mid->mid, mid->mid_state, rdata->result,
2527 rdata->bytes);
2529 switch (mid->mid_state) {
2530 case MID_RESPONSE_RECEIVED:
2531 credits_received = le16_to_cpu(shdr->CreditRequest);
2532 /* result already set, check signature */
2533 if (server->sign && !mid->decrypted) {
2534 int rc;
2536 rc = smb2_verify_signature(&rqst, server);
2537 if (rc)
2538 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2539 rc);
2541 /* FIXME: should this be counted toward the initiating task? */
2542 task_io_account_read(rdata->got_bytes);
2543 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2544 break;
2545 case MID_REQUEST_SUBMITTED:
2546 case MID_RETRY_NEEDED:
2547 rdata->result = -EAGAIN;
2548 if (server->sign && rdata->got_bytes)
2549 /* reset bytes number since we can not check a sign */
2550 rdata->got_bytes = 0;
2551 /* FIXME: should this be counted toward the initiating task? */
2552 task_io_account_read(rdata->got_bytes);
2553 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2554 break;
2555 default:
2556 if (rdata->result != -ENODATA)
2557 rdata->result = -EIO;
2560 if (rdata->result)
2561 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2563 queue_work(cifsiod_wq, &rdata->work);
2564 DeleteMidQEntry(mid);
2565 add_credits(server, credits_received, 0);
2568 /* smb2_async_readv - send an async read, and set up mid to handle result */
2570 smb2_async_readv(struct cifs_readdata *rdata)
2572 int rc, flags = 0;
2573 char *buf;
2574 struct smb2_sync_hdr *shdr;
2575 struct cifs_io_parms io_parms;
2576 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2577 .rq_nvec = 2 };
2578 struct TCP_Server_Info *server;
2579 unsigned int total_len;
2580 __be32 req_len;
2582 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2583 __func__, rdata->offset, rdata->bytes);
2585 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2586 io_parms.offset = rdata->offset;
2587 io_parms.length = rdata->bytes;
2588 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2589 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2590 io_parms.pid = rdata->pid;
2592 server = io_parms.tcon->ses->server;
2594 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2595 if (rc) {
2596 if (rc == -EAGAIN && rdata->credits) {
2597 /* credits was reset by reconnect */
2598 rdata->credits = 0;
2599 /* reduce in_flight value since we won't send the req */
2600 spin_lock(&server->req_lock);
2601 server->in_flight--;
2602 spin_unlock(&server->req_lock);
2604 return rc;
2607 if (encryption_required(io_parms.tcon))
2608 flags |= CIFS_TRANSFORM_REQ;
2610 req_len = cpu_to_be32(total_len);
2612 rdata->iov[0].iov_base = &req_len;
2613 rdata->iov[0].iov_len = sizeof(__be32);
2614 rdata->iov[1].iov_base = buf;
2615 rdata->iov[1].iov_len = total_len;
2617 shdr = (struct smb2_sync_hdr *)buf;
2619 if (rdata->credits) {
2620 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2621 SMB2_MAX_BUFFER_SIZE));
2622 shdr->CreditRequest = shdr->CreditCharge;
2623 spin_lock(&server->req_lock);
2624 server->credits += rdata->credits -
2625 le16_to_cpu(shdr->CreditCharge);
2626 spin_unlock(&server->req_lock);
2627 wake_up(&server->request_q);
2628 flags |= CIFS_HAS_CREDITS;
2631 kref_get(&rdata->refcount);
2632 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2633 cifs_readv_receive, smb2_readv_callback,
2634 smb3_handle_read_data, rdata, flags);
2635 if (rc) {
2636 kref_put(&rdata->refcount, cifs_readdata_release);
2637 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2640 cifs_small_buf_release(buf);
2641 return rc;
2645 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2646 unsigned int *nbytes, char **buf, int *buf_type)
2648 int resp_buftype, rc = -EACCES;
2649 struct smb2_read_plain_req *req = NULL;
2650 struct smb2_read_rsp *rsp = NULL;
2651 struct smb2_sync_hdr *shdr;
2652 struct kvec iov[2];
2653 struct kvec rsp_iov;
2654 unsigned int total_len;
2655 __be32 req_len;
2656 struct smb_rqst rqst = { .rq_iov = iov,
2657 .rq_nvec = 2 };
2658 int flags = CIFS_LOG_ERROR;
2659 struct cifs_ses *ses = io_parms->tcon->ses;
2661 *nbytes = 0;
2662 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2663 if (rc)
2664 return rc;
2666 if (encryption_required(io_parms->tcon))
2667 flags |= CIFS_TRANSFORM_REQ;
2669 req_len = cpu_to_be32(total_len);
2671 iov[0].iov_base = &req_len;
2672 iov[0].iov_len = sizeof(__be32);
2673 iov[1].iov_base = req;
2674 iov[1].iov_len = total_len;
2676 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2677 cifs_small_buf_release(req);
2679 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2681 if (rc) {
2682 if (rc != -ENODATA) {
2683 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2684 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2686 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2687 return rc == -ENODATA ? 0 : rc;
2690 *nbytes = le32_to_cpu(rsp->DataLength);
2691 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2692 (*nbytes > io_parms->length)) {
2693 cifs_dbg(FYI, "bad length %d for count %d\n",
2694 *nbytes, io_parms->length);
2695 rc = -EIO;
2696 *nbytes = 0;
2699 shdr = get_sync_hdr(rsp);
2701 if (*buf) {
2702 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2703 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2704 } else if (resp_buftype != CIFS_NO_BUFFER) {
2705 *buf = rsp_iov.iov_base;
2706 if (resp_buftype == CIFS_SMALL_BUFFER)
2707 *buf_type = CIFS_SMALL_BUFFER;
2708 else if (resp_buftype == CIFS_LARGE_BUFFER)
2709 *buf_type = CIFS_LARGE_BUFFER;
2711 return rc;
2715 * Check the mid_state and signature on received buffer (if any), and queue the
2716 * workqueue completion task.
2718 static void
2719 smb2_writev_callback(struct mid_q_entry *mid)
2721 struct cifs_writedata *wdata = mid->callback_data;
2722 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2723 unsigned int written;
2724 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2725 unsigned int credits_received = 1;
2727 switch (mid->mid_state) {
2728 case MID_RESPONSE_RECEIVED:
2729 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2730 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2731 if (wdata->result != 0)
2732 break;
2734 written = le32_to_cpu(rsp->DataLength);
2736 * Mask off high 16 bits when bytes written as returned
2737 * by the server is greater than bytes requested by the
2738 * client. OS/2 servers are known to set incorrect
2739 * CountHigh values.
2741 if (written > wdata->bytes)
2742 written &= 0xFFFF;
2744 if (written < wdata->bytes)
2745 wdata->result = -ENOSPC;
2746 else
2747 wdata->bytes = written;
2748 break;
2749 case MID_REQUEST_SUBMITTED:
2750 case MID_RETRY_NEEDED:
2751 wdata->result = -EAGAIN;
2752 break;
2753 default:
2754 wdata->result = -EIO;
2755 break;
2758 if (wdata->result)
2759 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2761 queue_work(cifsiod_wq, &wdata->work);
2762 DeleteMidQEntry(mid);
2763 add_credits(tcon->ses->server, credits_received, 0);
2766 /* smb2_async_writev - send an async write, and set up mid to handle result */
2768 smb2_async_writev(struct cifs_writedata *wdata,
2769 void (*release)(struct kref *kref))
2771 int rc = -EACCES, flags = 0;
2772 struct smb2_write_req *req = NULL;
2773 struct smb2_sync_hdr *shdr;
2774 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2775 struct TCP_Server_Info *server = tcon->ses->server;
2776 struct kvec iov[2];
2777 struct smb_rqst rqst = { };
2779 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2780 if (rc) {
2781 if (rc == -EAGAIN && wdata->credits) {
2782 /* credits was reset by reconnect */
2783 wdata->credits = 0;
2784 /* reduce in_flight value since we won't send the req */
2785 spin_lock(&server->req_lock);
2786 server->in_flight--;
2787 spin_unlock(&server->req_lock);
2789 goto async_writev_out;
2792 if (encryption_required(tcon))
2793 flags |= CIFS_TRANSFORM_REQ;
2795 shdr = get_sync_hdr(req);
2796 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2798 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2799 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2800 req->WriteChannelInfoOffset = 0;
2801 req->WriteChannelInfoLength = 0;
2802 req->Channel = 0;
2803 req->Offset = cpu_to_le64(wdata->offset);
2804 /* 4 for rfc1002 length field */
2805 req->DataOffset = cpu_to_le16(
2806 offsetof(struct smb2_write_req, Buffer) - 4);
2807 req->RemainingBytes = 0;
2809 /* 4 for rfc1002 length field and 1 for Buffer */
2810 iov[0].iov_len = 4;
2811 iov[0].iov_base = req;
2812 iov[1].iov_len = get_rfc1002_length(req) - 1;
2813 iov[1].iov_base = (char *)req + 4;
2815 rqst.rq_iov = iov;
2816 rqst.rq_nvec = 2;
2817 rqst.rq_pages = wdata->pages;
2818 rqst.rq_npages = wdata->nr_pages;
2819 rqst.rq_pagesz = wdata->pagesz;
2820 rqst.rq_tailsz = wdata->tailsz;
2822 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2823 wdata->offset, wdata->bytes);
2825 req->Length = cpu_to_le32(wdata->bytes);
2827 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2829 if (wdata->credits) {
2830 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2831 SMB2_MAX_BUFFER_SIZE));
2832 shdr->CreditRequest = shdr->CreditCharge;
2833 spin_lock(&server->req_lock);
2834 server->credits += wdata->credits -
2835 le16_to_cpu(shdr->CreditCharge);
2836 spin_unlock(&server->req_lock);
2837 wake_up(&server->request_q);
2838 flags |= CIFS_HAS_CREDITS;
2841 kref_get(&wdata->refcount);
2842 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2843 wdata, flags);
2845 if (rc) {
2846 kref_put(&wdata->refcount, release);
2847 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2850 async_writev_out:
2851 cifs_small_buf_release(req);
2852 return rc;
2856 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2857 * The length field from io_parms must be at least 1 and indicates a number of
2858 * elements with data to write that begins with position 1 in iov array. All
2859 * data length is specified by count.
2862 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2863 unsigned int *nbytes, struct kvec *iov, int n_vec)
2865 int rc = 0;
2866 struct smb2_write_req *req = NULL;
2867 struct smb2_write_rsp *rsp = NULL;
2868 int resp_buftype;
2869 struct kvec rsp_iov;
2870 int flags = 0;
2872 *nbytes = 0;
2874 if (n_vec < 1)
2875 return rc;
2877 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2878 if (rc)
2879 return rc;
2881 if (io_parms->tcon->ses->server == NULL)
2882 return -ECONNABORTED;
2884 if (encryption_required(io_parms->tcon))
2885 flags |= CIFS_TRANSFORM_REQ;
2887 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2889 req->PersistentFileId = io_parms->persistent_fid;
2890 req->VolatileFileId = io_parms->volatile_fid;
2891 req->WriteChannelInfoOffset = 0;
2892 req->WriteChannelInfoLength = 0;
2893 req->Channel = 0;
2894 req->Length = cpu_to_le32(io_parms->length);
2895 req->Offset = cpu_to_le64(io_parms->offset);
2896 /* 4 for rfc1002 length field */
2897 req->DataOffset = cpu_to_le16(
2898 offsetof(struct smb2_write_req, Buffer) - 4);
2899 req->RemainingBytes = 0;
2901 iov[0].iov_base = (char *)req;
2902 /* 4 for rfc1002 length field and 1 for Buffer */
2903 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2905 /* length of entire message including data to be written */
2906 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2908 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2909 &resp_buftype, flags, &rsp_iov);
2910 cifs_small_buf_release(req);
2911 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2913 if (rc) {
2914 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2915 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2916 } else
2917 *nbytes = le32_to_cpu(rsp->DataLength);
2919 free_rsp_buf(resp_buftype, rsp);
2920 return rc;
2923 static unsigned int
2924 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2926 int len;
2927 unsigned int entrycount = 0;
2928 unsigned int next_offset = 0;
2929 FILE_DIRECTORY_INFO *entryptr;
2931 if (bufstart == NULL)
2932 return 0;
2934 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2936 while (1) {
2937 entryptr = (FILE_DIRECTORY_INFO *)
2938 ((char *)entryptr + next_offset);
2940 if ((char *)entryptr + size > end_of_buf) {
2941 cifs_dbg(VFS, "malformed search entry would overflow\n");
2942 break;
2945 len = le32_to_cpu(entryptr->FileNameLength);
2946 if ((char *)entryptr + len + size > end_of_buf) {
2947 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2948 end_of_buf);
2949 break;
2952 *lastentry = (char *)entryptr;
2953 entrycount++;
2955 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2956 if (!next_offset)
2957 break;
2960 return entrycount;
2964 * Readdir/FindFirst
2967 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2968 u64 persistent_fid, u64 volatile_fid, int index,
2969 struct cifs_search_info *srch_inf)
2971 struct smb2_query_directory_req *req;
2972 struct smb2_query_directory_rsp *rsp = NULL;
2973 struct kvec iov[2];
2974 struct kvec rsp_iov;
2975 int rc = 0;
2976 int len;
2977 int resp_buftype = CIFS_NO_BUFFER;
2978 unsigned char *bufptr;
2979 struct TCP_Server_Info *server;
2980 struct cifs_ses *ses = tcon->ses;
2981 __le16 asteriks = cpu_to_le16('*');
2982 char *end_of_smb;
2983 unsigned int output_size = CIFSMaxBufSize;
2984 size_t info_buf_size;
2985 int flags = 0;
2987 if (ses && (ses->server))
2988 server = ses->server;
2989 else
2990 return -EIO;
2992 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2993 if (rc)
2994 return rc;
2996 if (encryption_required(tcon))
2997 flags |= CIFS_TRANSFORM_REQ;
2999 switch (srch_inf->info_level) {
3000 case SMB_FIND_FILE_DIRECTORY_INFO:
3001 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3002 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3003 break;
3004 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3005 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3006 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3007 break;
3008 default:
3009 cifs_dbg(VFS, "info level %u isn't supported\n",
3010 srch_inf->info_level);
3011 rc = -EINVAL;
3012 goto qdir_exit;
3015 req->FileIndex = cpu_to_le32(index);
3016 req->PersistentFileId = persistent_fid;
3017 req->VolatileFileId = volatile_fid;
3019 len = 0x2;
3020 bufptr = req->Buffer;
3021 memcpy(bufptr, &asteriks, len);
3023 req->FileNameOffset =
3024 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3025 req->FileNameLength = cpu_to_le16(len);
3027 * BB could be 30 bytes or so longer if we used SMB2 specific
3028 * buffer lengths, but this is safe and close enough.
3030 output_size = min_t(unsigned int, output_size, server->maxBuf);
3031 output_size = min_t(unsigned int, output_size, 2 << 15);
3032 req->OutputBufferLength = cpu_to_le32(output_size);
3034 iov[0].iov_base = (char *)req;
3035 /* 4 for RFC1001 length and 1 for Buffer */
3036 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3038 iov[1].iov_base = (char *)(req->Buffer);
3039 iov[1].iov_len = len;
3041 inc_rfc1001_len(req, len - 1 /* Buffer */);
3043 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3044 cifs_small_buf_release(req);
3045 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3047 if (rc) {
3048 if (rc == -ENODATA &&
3049 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
3050 srch_inf->endOfSearch = true;
3051 rc = 0;
3053 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3054 goto qdir_exit;
3057 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3058 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3059 info_buf_size);
3060 if (rc)
3061 goto qdir_exit;
3063 srch_inf->unicode = true;
3065 if (srch_inf->ntwrk_buf_start) {
3066 if (srch_inf->smallBuf)
3067 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3068 else
3069 cifs_buf_release(srch_inf->ntwrk_buf_start);
3071 srch_inf->ntwrk_buf_start = (char *)rsp;
3072 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3073 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3074 /* 4 for rfc1002 length field */
3075 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3076 srch_inf->entries_in_buffer =
3077 num_entries(srch_inf->srch_entries_start, end_of_smb,
3078 &srch_inf->last_entry, info_buf_size);
3079 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3080 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3081 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3082 srch_inf->srch_entries_start, srch_inf->last_entry);
3083 if (resp_buftype == CIFS_LARGE_BUFFER)
3084 srch_inf->smallBuf = false;
3085 else if (resp_buftype == CIFS_SMALL_BUFFER)
3086 srch_inf->smallBuf = true;
3087 else
3088 cifs_dbg(VFS, "illegal search buffer type\n");
3090 return rc;
3092 qdir_exit:
3093 free_rsp_buf(resp_buftype, rsp);
3094 return rc;
3097 static int
3098 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3099 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3100 u8 info_type, u32 additional_info, unsigned int num,
3101 void **data, unsigned int *size)
3103 struct smb2_set_info_req *req;
3104 struct smb2_set_info_rsp *rsp = NULL;
3105 struct kvec *iov;
3106 struct kvec rsp_iov;
3107 int rc = 0;
3108 int resp_buftype;
3109 unsigned int i;
3110 struct cifs_ses *ses = tcon->ses;
3111 int flags = 0;
3113 if (!ses || !(ses->server))
3114 return -EIO;
3116 if (!num)
3117 return -EINVAL;
3119 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3120 if (!iov)
3121 return -ENOMEM;
3123 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3124 if (rc) {
3125 kfree(iov);
3126 return rc;
3129 if (encryption_required(tcon))
3130 flags |= CIFS_TRANSFORM_REQ;
3132 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3134 req->InfoType = info_type;
3135 req->FileInfoClass = info_class;
3136 req->PersistentFileId = persistent_fid;
3137 req->VolatileFileId = volatile_fid;
3138 req->AdditionalInformation = cpu_to_le32(additional_info);
3140 /* 4 for RFC1001 length and 1 for Buffer */
3141 req->BufferOffset =
3142 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3143 req->BufferLength = cpu_to_le32(*size);
3145 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3147 memcpy(req->Buffer, *data, *size);
3149 iov[0].iov_base = (char *)req;
3150 /* 4 for RFC1001 length */
3151 iov[0].iov_len = get_rfc1002_length(req) + 4;
3153 for (i = 1; i < num; i++) {
3154 inc_rfc1001_len(req, size[i]);
3155 le32_add_cpu(&req->BufferLength, size[i]);
3156 iov[i].iov_base = (char *)data[i];
3157 iov[i].iov_len = size[i];
3160 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
3161 cifs_small_buf_release(req);
3162 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3164 if (rc != 0)
3165 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3167 free_rsp_buf(resp_buftype, rsp);
3168 kfree(iov);
3169 return rc;
3173 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3174 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3176 struct smb2_file_rename_info info;
3177 void **data;
3178 unsigned int size[2];
3179 int rc;
3180 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3182 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3183 if (!data)
3184 return -ENOMEM;
3186 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3187 /* 0 = fail if target already exists */
3188 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3189 info.FileNameLength = cpu_to_le32(len);
3191 data[0] = &info;
3192 size[0] = sizeof(struct smb2_file_rename_info);
3194 data[1] = target_file;
3195 size[1] = len + 2 /* null */;
3197 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3198 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3199 0, 2, data, size);
3200 kfree(data);
3201 return rc;
3205 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3206 u64 persistent_fid, u64 volatile_fid)
3208 __u8 delete_pending = 1;
3209 void *data;
3210 unsigned int size;
3212 data = &delete_pending;
3213 size = 1; /* sizeof __u8 */
3215 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3216 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3217 0, 1, &data, &size);
3221 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3222 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3224 struct smb2_file_link_info info;
3225 void **data;
3226 unsigned int size[2];
3227 int rc;
3228 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3230 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3231 if (!data)
3232 return -ENOMEM;
3234 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3235 /* 0 = fail if link already exists */
3236 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3237 info.FileNameLength = cpu_to_le32(len);
3239 data[0] = &info;
3240 size[0] = sizeof(struct smb2_file_link_info);
3242 data[1] = target_file;
3243 size[1] = len + 2 /* null */;
3245 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3246 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3247 0, 2, data, size);
3248 kfree(data);
3249 return rc;
3253 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3254 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3256 struct smb2_file_eof_info info;
3257 void *data;
3258 unsigned int size;
3260 info.EndOfFile = *eof;
3262 data = &info;
3263 size = sizeof(struct smb2_file_eof_info);
3265 if (is_falloc)
3266 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3267 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3268 0, 1, &data, &size);
3269 else
3270 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3271 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3272 0, 1, &data, &size);
3276 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3277 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3279 unsigned int size;
3280 size = sizeof(FILE_BASIC_INFO);
3281 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3282 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3283 0, 1, (void **)&buf, &size);
3287 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3288 u64 persistent_fid, u64 volatile_fid,
3289 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3291 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3292 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3293 1, (void **)&pnntsd, &pacllen);
3297 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3298 u64 persistent_fid, u64 volatile_fid,
3299 struct smb2_file_full_ea_info *buf, int len)
3301 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3302 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3303 0, 1, (void **)&buf, &len);
3307 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3308 const u64 persistent_fid, const u64 volatile_fid,
3309 __u8 oplock_level)
3311 int rc;
3312 struct smb2_oplock_break *req = NULL;
3313 int flags = CIFS_OBREAK_OP;
3315 cifs_dbg(FYI, "SMB2_oplock_break\n");
3316 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3317 if (rc)
3318 return rc;
3320 if (encryption_required(tcon))
3321 flags |= CIFS_TRANSFORM_REQ;
3323 req->VolatileFid = volatile_fid;
3324 req->PersistentFid = persistent_fid;
3325 req->OplockLevel = oplock_level;
3326 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3328 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3329 cifs_small_buf_release(req);
3331 if (rc) {
3332 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3333 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3336 return rc;
3339 static void
3340 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3341 struct kstatfs *kst)
3343 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3344 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3345 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3346 kst->f_bfree = kst->f_bavail =
3347 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3348 return;
3351 static int
3352 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3353 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3355 int rc;
3356 struct smb2_query_info_req *req;
3358 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3360 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3361 return -EIO;
3363 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3364 if (rc)
3365 return rc;
3367 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3368 req->FileInfoClass = level;
3369 req->PersistentFileId = persistent_fid;
3370 req->VolatileFileId = volatile_fid;
3371 /* 4 for rfc1002 length field and 1 for pad */
3372 req->InputBufferOffset =
3373 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3374 req->OutputBufferLength = cpu_to_le32(
3375 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3377 iov->iov_base = (char *)req;
3378 /* 4 for rfc1002 length field */
3379 iov->iov_len = get_rfc1002_length(req) + 4;
3380 return 0;
3384 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3385 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3387 struct smb2_query_info_rsp *rsp = NULL;
3388 struct kvec iov;
3389 struct kvec rsp_iov;
3390 int rc = 0;
3391 int resp_buftype;
3392 struct cifs_ses *ses = tcon->ses;
3393 struct smb2_fs_full_size_info *info = NULL;
3394 int flags = 0;
3396 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3397 sizeof(struct smb2_fs_full_size_info),
3398 persistent_fid, volatile_fid);
3399 if (rc)
3400 return rc;
3402 if (encryption_required(tcon))
3403 flags |= CIFS_TRANSFORM_REQ;
3405 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3406 cifs_small_buf_release(iov.iov_base);
3407 if (rc) {
3408 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3409 goto qfsinf_exit;
3411 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3413 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3414 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3415 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3416 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3417 sizeof(struct smb2_fs_full_size_info));
3418 if (!rc)
3419 copy_fs_info_to_kstatfs(info, fsdata);
3421 qfsinf_exit:
3422 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3423 return rc;
3427 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3428 u64 persistent_fid, u64 volatile_fid, int level)
3430 struct smb2_query_info_rsp *rsp = NULL;
3431 struct kvec iov;
3432 struct kvec rsp_iov;
3433 int rc = 0;
3434 int resp_buftype, max_len, min_len;
3435 struct cifs_ses *ses = tcon->ses;
3436 unsigned int rsp_len, offset;
3437 int flags = 0;
3439 if (level == FS_DEVICE_INFORMATION) {
3440 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3441 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3442 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3443 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3444 min_len = MIN_FS_ATTR_INFO_SIZE;
3445 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3446 max_len = sizeof(struct smb3_fs_ss_info);
3447 min_len = sizeof(struct smb3_fs_ss_info);
3448 } else {
3449 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3450 return -EINVAL;
3453 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3454 persistent_fid, volatile_fid);
3455 if (rc)
3456 return rc;
3458 if (encryption_required(tcon))
3459 flags |= CIFS_TRANSFORM_REQ;
3461 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3462 cifs_small_buf_release(iov.iov_base);
3463 if (rc) {
3464 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3465 goto qfsattr_exit;
3467 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3469 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3470 offset = le16_to_cpu(rsp->OutputBufferOffset);
3471 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3472 if (rc)
3473 goto qfsattr_exit;
3475 if (level == FS_ATTRIBUTE_INFORMATION)
3476 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3477 + (char *)&rsp->hdr, min_t(unsigned int,
3478 rsp_len, max_len));
3479 else if (level == FS_DEVICE_INFORMATION)
3480 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3481 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3482 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3483 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3484 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3485 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3486 tcon->perf_sector_size =
3487 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3490 qfsattr_exit:
3491 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3492 return rc;
3496 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3497 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3498 const __u32 num_lock, struct smb2_lock_element *buf)
3500 int rc = 0;
3501 struct smb2_lock_req *req = NULL;
3502 struct kvec iov[2];
3503 struct kvec rsp_iov;
3504 int resp_buf_type;
3505 unsigned int count;
3506 int flags = CIFS_NO_RESP;
3508 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3510 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3511 if (rc)
3512 return rc;
3514 if (encryption_required(tcon))
3515 flags |= CIFS_TRANSFORM_REQ;
3517 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3518 req->LockCount = cpu_to_le16(num_lock);
3520 req->PersistentFileId = persist_fid;
3521 req->VolatileFileId = volatile_fid;
3523 count = num_lock * sizeof(struct smb2_lock_element);
3524 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3526 iov[0].iov_base = (char *)req;
3527 /* 4 for rfc1002 length field and count for all locks */
3528 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3529 iov[1].iov_base = (char *)buf;
3530 iov[1].iov_len = count;
3532 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3533 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3534 &rsp_iov);
3535 cifs_small_buf_release(req);
3536 if (rc) {
3537 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3538 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3541 return rc;
3545 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3546 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3547 const __u64 length, const __u64 offset, const __u32 lock_flags,
3548 const bool wait)
3550 struct smb2_lock_element lock;
3552 lock.Offset = cpu_to_le64(offset);
3553 lock.Length = cpu_to_le64(length);
3554 lock.Flags = cpu_to_le32(lock_flags);
3555 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3556 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3558 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3562 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3563 __u8 *lease_key, const __le32 lease_state)
3565 int rc;
3566 struct smb2_lease_ack *req = NULL;
3567 int flags = CIFS_OBREAK_OP;
3569 cifs_dbg(FYI, "SMB2_lease_break\n");
3570 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3571 if (rc)
3572 return rc;
3574 if (encryption_required(tcon))
3575 flags |= CIFS_TRANSFORM_REQ;
3577 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3578 req->StructureSize = cpu_to_le16(36);
3579 inc_rfc1001_len(req, 12);
3581 memcpy(req->LeaseKey, lease_key, 16);
3582 req->LeaseState = lease_state;
3584 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3585 cifs_small_buf_release(req);
3587 if (rc) {
3588 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3589 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3592 return rc;