blob: e8dc28dbe563e88079a7f994d160873c31b1433e [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
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.
15 *
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.
20 *
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
24 */
25
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 */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070036#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040037#include <linux/xattr.h>
38#include "smb2pdu.h"
39#include "cifsglob.h"
40#include "cifsacl.h"
41#include "cifsproto.h"
42#include "smb2proto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
45#include "ntlmssp.h"
46#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070047#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070048#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050049#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040050
51/*
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 *
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
57 */
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78};
79
80
81static void
82smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
84{
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
89
90 /*
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
93 */
94 memset(temp, 0, 256);
95
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
99
Steve French373512e2015-12-18 13:05:30 -0600100 hdr->ProtocolId = SMB2_PROTO_NUMBER;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100109 hdr->CreditRequest = cpu_to_le16(0);
110 else
111 hdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 hdr->CreditRequest = cpu_to_le16(2);
117 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400118 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Steve French2b80d042013-06-23 18:43:37 -0500127 hdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
129
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400130 hdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 hdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147
Jeff Layton38d77c52013-05-26 07:01:00 -0400148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700149 hdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150out:
151 pdu->StructureSize2 = cpu_to_le16(parmsize);
152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
Paulo Alcantara2c4f6b72018-07-05 13:46:34 -0300158 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
Paulo Alcantara2c4f6b72018-07-05 13:46:34 -0300169 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400170
Pavel Shilovsky84bdf5e2019-07-22 11:34:59 -0700171 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
Paulo Alcantara2c4f6b72018-07-05 13:46:34 -0300172 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
Paulo Alcantara2c4f6b72018-07-05 13:46:34 -0300215 rc = wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect),
217 10 * HZ);
218 if (rc < 0) {
219 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
220 " signal by the process\n", __func__);
221 return -ERESTARTSYS;
222 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400223
224 /* are we still trying to reconnect? */
225 if (server->tcpStatus != CifsNeedReconnect)
226 break;
227
228 /*
229 * on "soft" mounts we wait once. Hard mounts keep
230 * retrying until process is killed or server comes
231 * back on-line
232 */
233 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500234 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400235 return -EHOSTDOWN;
236 }
237 }
238
239 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara2c4f6b72018-07-05 13:46:34 -0300240 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400241
242 nls_codepage = load_nls_default();
243
244 /*
245 * need to prevent multiple threads trying to simultaneously reconnect
246 * the same SMB session
247 */
248 mutex_lock(&tcon->ses->session_mutex);
249 rc = cifs_negotiate_protocol(0, tcon->ses);
Ronnie Sahlbergf78ba732020-02-05 11:08:01 +1000250 if (!rc && tcon->ses->need_reconnect) {
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400251 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
Ronnie Sahlbergf78ba732020-02-05 11:08:01 +1000252 if ((rc == -EACCES) && !tcon->retry) {
253 rc = -EHOSTDOWN;
254 mutex_unlock(&tcon->ses->session_mutex);
255 goto failed;
256 }
257 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400258 if (rc || !tcon->need_reconnect) {
259 mutex_unlock(&tcon->ses->session_mutex);
260 goto out;
261 }
262
263 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800264 if (tcon->use_persistent)
265 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500266
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400267 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
268 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500269
Joe Perchesf96637b2013-05-04 22:12:25 -0500270 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400271 if (rc)
272 goto out;
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800273
274 if (smb2_command != SMB2_INTERNAL_CMD)
275 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
276
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400278out:
279 /*
280 * Check if handle based operation so we know whether we can continue
281 * or not without returning to caller to reset file handle.
282 */
283 /*
284 * BB Is flush done by server on drop of tcp session? Should we special
285 * case it and skip above?
286 */
287 switch (smb2_command) {
288 case SMB2_FLUSH:
289 case SMB2_READ:
290 case SMB2_WRITE:
291 case SMB2_LOCK:
292 case SMB2_IOCTL:
293 case SMB2_QUERY_DIRECTORY:
294 case SMB2_CHANGE_NOTIFY:
295 case SMB2_QUERY_INFO:
296 case SMB2_SET_INFO:
Pavel Shilovsky69d13b62016-11-29 11:30:58 -0800297 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400298 }
Ronnie Sahlbergf78ba732020-02-05 11:08:01 +1000299failed:
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400300 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400301 return rc;
302}
303
304/*
305 * Allocate and return pointer to an SMB request hdr, and set basic
306 * SMB information in the SMB header. If the return code is zero, this
307 * function must have filled in request_buf pointer.
308 */
309static int
310small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
311 void **request_buf)
312{
313 int rc = 0;
314
315 rc = smb2_reconnect(smb2_command, tcon);
316 if (rc)
317 return rc;
318
319 /* BB eventually switch this to SMB2 specific small buf size */
320 *request_buf = cifs_small_buf_get();
321 if (*request_buf == NULL) {
322 /* BB should we add a retry in here if not a writepage? */
323 return -ENOMEM;
324 }
325
326 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
327
328 if (tcon != NULL) {
Steve French7aed5a52018-07-23 09:15:18 -0500329#ifdef CONFIG_CIFS_STATS
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400330 uint16_t com_code = le16_to_cpu(smb2_command);
331 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400332#endif
333 cifs_stats_inc(&tcon->num_smbs_sent);
334 }
335
336 return rc;
337}
338
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500339#ifdef CONFIG_CIFS_SMB311
340/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
341#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
342
343
344#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
345#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
346
347static void
348build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
349{
350 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
351 pneg_ctxt->DataLength = cpu_to_le16(38);
352 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
353 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
354 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
355 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
356}
357
358static void
359build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
360{
361 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
362 pneg_ctxt->DataLength = cpu_to_le16(6);
363 pneg_ctxt->CipherCount = cpu_to_le16(2);
364 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
365 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
366}
367
368static void
369assemble_neg_contexts(struct smb2_negotiate_req *req)
370{
371
372 /* +4 is to account for the RFC1001 len field */
373 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
374
375 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
376 /* Add 2 to size to round to 8 byte boundary */
377 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
378 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
379 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
380 req->NegotiateContextCount = cpu_to_le16(2);
Steve Frenchf2d395b2017-09-18 18:18:45 -0500381 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500382 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
383}
384#else
385static void assemble_neg_contexts(struct smb2_negotiate_req *req)
386{
387 return;
388}
389#endif /* SMB311 */
390
391
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400392/*
393 *
394 * SMB2 Worker functions follow:
395 *
396 * The general structure of the worker functions is:
397 * 1) Call smb2_init (assembles SMB2 header)
398 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
399 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
400 * 4) Decode SMB2 command specific fields in the fixed length area
401 * 5) Decode variable length data area (if any for this SMB2 command type)
402 * 6) Call free smb buffer
403 * 7) return
404 *
405 */
406
407int
408SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
409{
410 struct smb2_negotiate_req *req;
411 struct smb2_negotiate_rsp *rsp;
412 struct kvec iov[1];
413 int rc = 0;
414 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400415 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400416 int blob_offset, blob_length;
417 char *security_blob;
418 int flags = CIFS_NEG_OP;
419
Joe Perchesf96637b2013-05-04 22:12:25 -0500420 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400421
Jeff Layton3534b852013-05-24 07:41:01 -0400422 if (!server) {
423 WARN(1, "%s: server is NULL!\n", __func__);
424 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400425 }
426
427 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
428 if (rc)
429 return rc;
430
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400431 req->hdr.SessionId = 0;
432
Steve Frenche4aa25e2012-10-01 12:26:22 -0500433 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400434
Steve Frenche4aa25e2012-10-01 12:26:22 -0500435 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
436 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400437
438 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400439 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500440 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400441 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500442 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400443 else
444 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445
Steve Frenche4aa25e2012-10-01 12:26:22 -0500446 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400447
Steve French3c5f9be12014-05-13 13:37:45 -0700448 /* ClientGUID must be zero for SMB2.02 dialect */
449 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
450 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500451 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700452 memcpy(req->ClientGUID, server->client_guid,
453 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500454 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
455 assemble_neg_contexts(req);
456 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400457 iov[0].iov_base = (char *)req;
458 /* 4 for rfc1002 length field */
459 iov[0].iov_len = get_rfc1002_length(req) + 4;
460
461 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
462
463 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
464 /*
465 * No tcon so can't do
466 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
467 */
468 if (rc != 0)
469 goto neg_exit;
470
Joe Perchesf96637b2013-05-04 22:12:25 -0500471 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400472
Steve Frenche4aa25e2012-10-01 12:26:22 -0500473 /* BB we may eventually want to match the negotiated vs. requested
474 dialect, even though we are only requesting one at a time */
475 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500476 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500477 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500478 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500479 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500480 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500481 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
482 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600483#ifdef CONFIG_CIFS_SMB311
484 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
485 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
486#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400487 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500488 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500489 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400490 rc = -EIO;
491 goto neg_exit;
492 }
493 server->dialect = le16_to_cpu(rsp->DialectRevision);
494
Jeff Laytone598d1d82013-05-26 07:00:59 -0400495 /* SMB2 only has an extended negflavor */
496 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400497 /* set it to the maximum buffer size value we can send with 1 credit */
498 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
499 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400500 server->max_read = le32_to_cpu(rsp->MaxReadSize);
501 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
502 /* BB Do we need to validate the SecurityMode? */
503 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
504 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400505 /* Internal types */
506 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400507
508 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
509 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500510 /*
511 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
512 * for us will be
513 * ses->sectype = RawNTLMSSP;
514 * but for time being this is our only auth choice so doesn't matter.
515 * We just found a server which sets blob length to zero expecting raw.
516 */
517 if (blob_length == 0)
518 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700519
Jeff Layton38d77c52013-05-26 07:01:00 -0400520 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400521 if (rc)
522 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500523 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500524 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500525 if (rc == 1)
526 rc = 0;
527 else if (rc == 0)
528 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400529 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400530neg_exit:
531 free_rsp_buf(resp_buftype, rsp);
532 return rc;
533}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400534
Steve Frenchff1c0382013-11-19 23:44:46 -0600535int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
536{
537 int rc = 0;
538 struct validate_negotiate_info_req vneg_inbuf;
539 struct validate_negotiate_info_rsp *pneg_rsp;
540 u32 rsplen;
541
542 cifs_dbg(FYI, "validate negotiate\n");
543
544 /*
545 * validation ioctl must be signed, so no point sending this if we
Steve French0e1b85a2017-09-20 19:57:18 -0500546 * can not sign it (ie are not known user). Even if signing is not
547 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600548 * sign just this, the first and only signed request on a connection.
Steve French0e1b85a2017-09-20 19:57:18 -0500549 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600550 */
Steve French0e1b85a2017-09-20 19:57:18 -0500551 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600552 return 0; /* validation requires signing */
553
Steve French0e1b85a2017-09-20 19:57:18 -0500554 if (tcon->ses->user_name == NULL) {
555 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
556 return 0; /* validation requires signing */
557 }
558
559 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
560 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
561
Steve Frenchff1c0382013-11-19 23:44:46 -0600562 vneg_inbuf.Capabilities =
563 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100564 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
565 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600566
567 if (tcon->ses->sign)
568 vneg_inbuf.SecurityMode =
569 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
570 else if (global_secflags & CIFSSEC_MAY_SIGN)
571 vneg_inbuf.SecurityMode =
572 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
573 else
574 vneg_inbuf.SecurityMode = 0;
575
576 vneg_inbuf.DialectCount = cpu_to_le16(1);
577 vneg_inbuf.Dialects[0] =
578 cpu_to_le16(tcon->ses->server->vals->protocol_id);
579
580 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
581 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
582 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
583 (char **)&pneg_rsp, &rsplen);
584
585 if (rc != 0) {
586 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
587 return -EIO;
588 }
589
590 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French8dd4e3f2017-05-03 21:12:20 -0500591 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
592 rsplen);
593
594 /* relax check since Mac returns max bufsize allowed on ioctl */
595 if (rsplen > CIFSMaxBufSize)
596 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600597 }
598
599 /* check validate negotiate info response matches what we got earlier */
Daniel N Petterssonf59eda12018-01-11 16:00:12 +0100600 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600601 goto vneg_out;
602
603 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
604 goto vneg_out;
605
606 /* do not validate server guid because not saved at negprot time yet */
607
608 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
609 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
610 goto vneg_out;
611
612 /* validate negotiate successful */
613 cifs_dbg(FYI, "validate negotiate info successful\n");
614 return 0;
615
616vneg_out:
617 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
618 return -EIO;
619}
620
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100621struct SMB2_sess_data {
622 unsigned int xid;
623 struct cifs_ses *ses;
624 struct nls_table *nls_cp;
625 void (*func)(struct SMB2_sess_data *);
626 int result;
627 u64 previous_session;
628
629 /* we will send the SMB in three pieces:
630 * a fixed length beginning part, an optional
631 * SPNEGO blob (which can be zero length), and a
632 * last part which will include the strings
633 * and rest of bcc area. This allows us to avoid
634 * a large buffer 17K allocation
635 */
636 int buf0_type;
637 struct kvec iov[2];
638};
639
640static int
641SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
642{
643 int rc;
644 struct cifs_ses *ses = sess_data->ses;
645 struct smb2_sess_setup_req *req;
646 struct TCP_Server_Info *server = ses->server;
647
648 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
649 if (rc)
650 return rc;
651
652 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
653
654 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
655 req->PreviousSessionId = sess_data->previous_session;
656
657 req->Flags = 0; /* MBZ */
658 /* to enable echos and oplocks */
659 req->hdr.CreditRequest = cpu_to_le16(3);
660
661 /* only one of SMB2 signing flags may be set in SMB2 request */
662 if (server->sign)
663 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
664 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
665 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
666 else
667 req->SecurityMode = 0;
668
Steve French92225e42019-07-25 18:13:10 -0500669#ifdef CONFIG_CIFS_DFS_UPCALL
670 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
671#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100672 req->Capabilities = 0;
Steve French92225e42019-07-25 18:13:10 -0500673#endif /* DFS_UPCALL */
674
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100675 req->Channel = 0; /* MBZ */
676
677 sess_data->iov[0].iov_base = (char *)req;
678 /* 4 for rfc1002 length field and 1 for pad */
679 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
680 /*
681 * This variable will be used to clear the buffer
682 * allocated above in case of any error in the calling function.
683 */
684 sess_data->buf0_type = CIFS_SMALL_BUFFER;
685
686 return 0;
687}
688
689static void
690SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
691{
692 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
693 sess_data->buf0_type = CIFS_NO_BUFFER;
694}
695
696static int
697SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
698{
699 int rc;
700 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
701
702 /* Testing shows that buffer offset must be at location of Buffer[0] */
703 req->SecurityBufferOffset =
704 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
705 1 /* pad */ - 4 /* rfc1001 len */);
706 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
707
708 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
709
710 /* BB add code to build os and lm fields */
711
712 rc = SendReceive2(sess_data->xid, sess_data->ses,
713 sess_data->iov, 2,
714 &sess_data->buf0_type,
715 CIFS_LOG_ERROR | CIFS_NEG_OP);
716
717 return rc;
718}
719
720static int
721SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
722{
723 int rc = 0;
724 struct cifs_ses *ses = sess_data->ses;
725
726 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800727 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100728 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100729 if (rc) {
730 cifs_dbg(FYI,
731 "SMB3 session key generation failed\n");
732 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800733 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100734 }
735 }
736 if (!ses->server->session_estab) {
737 ses->server->sequence_number = 0x2;
738 ses->server->session_estab = true;
739 }
740 mutex_unlock(&ses->server->srv_mutex);
741
742 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
743 spin_lock(&GlobalMid_Lock);
744 ses->status = CifsGood;
745 ses->need_reconnect = false;
746 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100747 return rc;
748}
749
750#ifdef CONFIG_CIFS_UPCALL
751static void
752SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
753{
754 int rc;
755 struct cifs_ses *ses = sess_data->ses;
756 struct cifs_spnego_msg *msg;
757 struct key *spnego_key = NULL;
758 struct smb2_sess_setup_rsp *rsp = NULL;
759
760 rc = SMB2_sess_alloc_buffer(sess_data);
761 if (rc)
762 goto out;
763
764 spnego_key = cifs_get_spnego_key(ses);
765 if (IS_ERR(spnego_key)) {
766 rc = PTR_ERR(spnego_key);
767 spnego_key = NULL;
768 goto out;
769 }
770
771 msg = spnego_key->payload.data[0];
772 /*
773 * check version field to make sure that cifs.upcall is
774 * sending us a response in an expected form
775 */
776 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
777 cifs_dbg(VFS,
778 "bad cifs.upcall version. Expected %d got %d",
779 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
780 rc = -EKEYREJECTED;
781 goto out_put_spnego_key;
782 }
783
784 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
785 GFP_KERNEL);
786 if (!ses->auth_key.response) {
787 cifs_dbg(VFS,
788 "Kerberos can't allocate (%u bytes) memory",
789 msg->sesskey_len);
790 rc = -ENOMEM;
791 goto out_put_spnego_key;
792 }
793 ses->auth_key.len = msg->sesskey_len;
794
795 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
796 sess_data->iov[1].iov_len = msg->secblob_len;
797
798 rc = SMB2_sess_sendreceive(sess_data);
799 if (rc)
800 goto out_put_spnego_key;
801
802 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
803 ses->Suid = rsp->hdr.SessionId;
804
805 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
806 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
807 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
808
809 rc = SMB2_sess_establish_session(sess_data);
810out_put_spnego_key:
811 key_invalidate(spnego_key);
812 key_put(spnego_key);
813out:
814 sess_data->result = rc;
815 sess_data->func = NULL;
816 SMB2_sess_free_buffer(sess_data);
817}
818#else
819static void
820SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
821{
822 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
823 sess_data->result = -EOPNOTSUPP;
824 sess_data->func = NULL;
825}
826#endif
827
Sachin Prabhu166cea42016-10-07 19:11:22 +0100828static void
829SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
830
831static void
832SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
833{
834 int rc;
835 struct cifs_ses *ses = sess_data->ses;
836 struct smb2_sess_setup_rsp *rsp = NULL;
837 char *ntlmssp_blob = NULL;
838 bool use_spnego = false; /* else use raw ntlmssp */
839 u16 blob_length = 0;
840
841 /*
842 * If memory allocation is successful, caller of this function
843 * frees it.
844 */
845 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
846 if (!ses->ntlmssp) {
847 rc = -ENOMEM;
848 goto out_err;
849 }
850 ses->ntlmssp->sesskey_per_smbsess = true;
851
852 rc = SMB2_sess_alloc_buffer(sess_data);
853 if (rc)
854 goto out_err;
855
856 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
857 GFP_KERNEL);
858 if (ntlmssp_blob == NULL) {
859 rc = -ENOMEM;
860 goto out;
861 }
862
863 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
864 if (use_spnego) {
865 /* BB eventually need to add this */
866 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
867 rc = -EOPNOTSUPP;
868 goto out;
869 } else {
870 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
871 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
872 }
873 sess_data->iov[1].iov_base = ntlmssp_blob;
874 sess_data->iov[1].iov_len = blob_length;
875
876 rc = SMB2_sess_sendreceive(sess_data);
877 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
878
879 /* If true, rc here is expected and not an error */
880 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
881 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
882 rc = 0;
883
884 if (rc)
885 goto out;
886
887 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
888 le16_to_cpu(rsp->SecurityBufferOffset)) {
889 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
890 le16_to_cpu(rsp->SecurityBufferOffset));
891 rc = -EIO;
892 goto out;
893 }
894 rc = decode_ntlmssp_challenge(rsp->Buffer,
895 le16_to_cpu(rsp->SecurityBufferLength), ses);
896 if (rc)
897 goto out;
898
899 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
900
901
902 ses->Suid = rsp->hdr.SessionId;
903 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
904 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
905 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
906
907out:
908 kfree(ntlmssp_blob);
909 SMB2_sess_free_buffer(sess_data);
910 if (!rc) {
911 sess_data->result = 0;
912 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
913 return;
914 }
915out_err:
916 kfree(ses->ntlmssp);
917 ses->ntlmssp = NULL;
918 sess_data->result = rc;
919 sess_data->func = NULL;
920}
921
922static void
923SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
924{
925 int rc;
926 struct cifs_ses *ses = sess_data->ses;
927 struct smb2_sess_setup_req *req;
928 struct smb2_sess_setup_rsp *rsp = NULL;
929 unsigned char *ntlmssp_blob = NULL;
930 bool use_spnego = false; /* else use raw ntlmssp */
931 u16 blob_length = 0;
932
933 rc = SMB2_sess_alloc_buffer(sess_data);
934 if (rc)
935 goto out;
936
937 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
938 req->hdr.SessionId = ses->Suid;
939
940 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
941 sess_data->nls_cp);
942 if (rc) {
943 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
944 goto out;
945 }
946
947 if (use_spnego) {
948 /* BB eventually need to add this */
949 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
950 rc = -EOPNOTSUPP;
951 goto out;
952 }
953 sess_data->iov[1].iov_base = ntlmssp_blob;
954 sess_data->iov[1].iov_len = blob_length;
955
956 rc = SMB2_sess_sendreceive(sess_data);
957 if (rc)
958 goto out;
959
960 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
961
962 ses->Suid = rsp->hdr.SessionId;
963 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
964 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
965 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
966
967 rc = SMB2_sess_establish_session(sess_data);
968out:
969 kfree(ntlmssp_blob);
970 SMB2_sess_free_buffer(sess_data);
971 kfree(ses->ntlmssp);
972 ses->ntlmssp = NULL;
973 sess_data->result = rc;
974 sess_data->func = NULL;
975}
976
977static int
978SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
979{
980 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
981 ses->sectype = RawNTLMSSP;
982
983 switch (ses->sectype) {
984 case Kerberos:
985 sess_data->func = SMB2_auth_kerberos;
986 break;
987 case RawNTLMSSP:
988 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
989 break;
990 default:
991 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
992 return -EOPNOTSUPP;
993 }
994
995 return 0;
996}
997
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400998int
999SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1000 const struct nls_table *nls_cp)
1001{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001002 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001003 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001004 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001005
Joe Perchesf96637b2013-05-04 22:12:25 -05001006 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001007
Jeff Layton3534b852013-05-24 07:41:01 -04001008 if (!server) {
1009 WARN(1, "%s: server is NULL!\n", __func__);
1010 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001011 }
1012
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001013 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1014 if (!sess_data)
1015 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001016
1017 rc = SMB2_select_sec(ses, sess_data);
1018 if (rc)
1019 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001020 sess_data->xid = xid;
1021 sess_data->ses = ses;
1022 sess_data->buf0_type = CIFS_NO_BUFFER;
1023 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frencha6c9a622018-05-31 15:19:25 -05001024 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001025
Sachin Prabhu166cea42016-10-07 19:11:22 +01001026 while (sess_data->func)
1027 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001028
Steve Frenchdf1be202017-09-19 18:40:03 -05001029 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1030 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001031 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001032out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001033 kfree(sess_data);
1034 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001035}
1036
1037int
1038SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1039{
1040 struct smb2_logoff_req *req; /* response is also trivial struct */
1041 int rc = 0;
1042 struct TCP_Server_Info *server;
1043
Joe Perchesf96637b2013-05-04 22:12:25 -05001044 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001045
1046 if (ses && (ses->server))
1047 server = ses->server;
1048 else
1049 return -EIO;
1050
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001051 /* no need to send SMB logoff if uid already closed due to reconnect */
1052 if (ses->need_reconnect)
1053 goto smb2_session_already_dead;
1054
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001055 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1056 if (rc)
1057 return rc;
1058
1059 /* since no tcon, smb2_init can not do this, so do here */
1060 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001061 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001062 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001063
1064 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1065 /*
1066 * No tcon so can't do
1067 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1068 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001069
1070smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001071 return rc;
1072}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001073
1074static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1075{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001076 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001077}
1078
1079#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1080
Steve Frenchde9f68d2013-11-15 11:26:24 -06001081/* These are similar values to what Windows uses */
1082static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1083{
1084 tcon->max_chunks = 256;
1085 tcon->max_bytes_chunk = 1048576;
1086 tcon->max_bytes_copy = 16777216;
1087}
1088
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001089int
1090SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1091 struct cifs_tcon *tcon, const struct nls_table *cp)
1092{
1093 struct smb2_tree_connect_req *req;
1094 struct smb2_tree_connect_rsp *rsp = NULL;
1095 struct kvec iov[2];
1096 int rc = 0;
1097 int resp_buftype;
1098 int unc_path_len;
1099 struct TCP_Server_Info *server;
1100 __le16 *unc_path = NULL;
1101
Joe Perchesf96637b2013-05-04 22:12:25 -05001102 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001103
1104 if ((ses->server) && tree)
1105 server = ses->server;
1106 else
1107 return -EIO;
1108
Steve Frenchff9f84b2015-09-26 09:48:58 -05001109 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001110 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1111 cifs_dbg(VFS, "encryption requested but no server support");
1112 return -EOPNOTSUPP;
1113 }
1114
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001115 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1116 if (unc_path == NULL)
1117 return -ENOMEM;
1118
1119 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1120 unc_path_len *= 2;
1121 if (unc_path_len < 2) {
1122 kfree(unc_path);
1123 return -EINVAL;
1124 }
1125
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001126 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1127 if (tcon)
1128 tcon->tid = 0;
1129
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001130 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1131 if (rc) {
1132 kfree(unc_path);
1133 return rc;
1134 }
1135
1136 if (tcon == NULL) {
1137 /* since no tcon, smb2_init can not do this, so do here */
1138 req->hdr.SessionId = ses->Suid;
1139 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1140 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1141 }
1142
1143 iov[0].iov_base = (char *)req;
1144 /* 4 for rfc1002 length field and 1 for pad */
1145 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1146
1147 /* Testing shows that buffer offset must be at location of Buffer[0] */
1148 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1149 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1150 req->PathLength = cpu_to_le16(unc_path_len - 2);
1151 iov[1].iov_base = unc_path;
1152 iov[1].iov_len = unc_path_len;
1153
1154 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1155
1156 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1157 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1158
1159 if (rc != 0) {
1160 if (tcon) {
1161 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1162 tcon->need_reconnect = true;
1163 }
1164 goto tcon_error_exit;
1165 }
1166
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001167 if (tcon == NULL) {
1168 ses->ipc_tid = rsp->hdr.TreeId;
1169 goto tcon_exit;
1170 }
1171
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001172 switch (rsp->ShareType) {
1173 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001174 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001175 break;
1176 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001177 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001178 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001179 break;
1180 case SMB2_SHARE_TYPE_PRINT:
1181 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001182 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001183 break;
1184 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001185 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001186 rc = -EOPNOTSUPP;
1187 goto tcon_error_exit;
1188 }
1189
1190 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001191 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001192 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1193 tcon->tidStatus = CifsGood;
1194 tcon->need_reconnect = false;
1195 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001196 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001197
1198 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1199 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001200 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001201 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001202 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1203 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001204 if (tcon->ses->server->ops->validate_negotiate)
1205 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001206tcon_exit:
1207 free_rsp_buf(resp_buftype, rsp);
1208 kfree(unc_path);
1209 return rc;
1210
1211tcon_error_exit:
1212 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001213 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001214 }
1215 goto tcon_exit;
1216}
1217
1218int
1219SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1220{
1221 struct smb2_tree_disconnect_req *req; /* response is trivial */
1222 int rc = 0;
1223 struct TCP_Server_Info *server;
1224 struct cifs_ses *ses = tcon->ses;
1225
Joe Perchesf96637b2013-05-04 22:12:25 -05001226 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001227
1228 if (ses && (ses->server))
1229 server = ses->server;
1230 else
1231 return -EIO;
1232
1233 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1234 return 0;
1235
1236 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1237 if (rc)
1238 return rc;
1239
1240 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1241 if (rc)
1242 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1243
1244 return rc;
1245}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001246
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001247
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001248static struct create_durable *
1249create_durable_buf(void)
1250{
1251 struct create_durable *buf;
1252
1253 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1254 if (!buf)
1255 return NULL;
1256
1257 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001258 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001259 buf->ccontext.DataLength = cpu_to_le32(16);
1260 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1261 (struct create_durable, Name));
1262 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001263 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001264 buf->Name[0] = 'D';
1265 buf->Name[1] = 'H';
1266 buf->Name[2] = 'n';
1267 buf->Name[3] = 'Q';
1268 return buf;
1269}
1270
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001271static struct create_durable *
1272create_reconnect_durable_buf(struct cifs_fid *fid)
1273{
1274 struct create_durable *buf;
1275
1276 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1277 if (!buf)
1278 return NULL;
1279
1280 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1281 (struct create_durable, Data));
1282 buf->ccontext.DataLength = cpu_to_le32(16);
1283 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1284 (struct create_durable, Name));
1285 buf->ccontext.NameLength = cpu_to_le16(4);
1286 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1287 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001288 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001289 buf->Name[0] = 'D';
1290 buf->Name[1] = 'H';
1291 buf->Name[2] = 'n';
1292 buf->Name[3] = 'C';
1293 return buf;
1294}
1295
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001296static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001297parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1298 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001299{
1300 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001301 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001302 unsigned int next;
1303 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001304 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001305
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001306 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001307 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001308 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001309 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001310 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001311 if (le16_to_cpu(cc->NameLength) == 4 &&
1312 strncmp(name, "RqLs", 4) == 0)
1313 return server->ops->parse_lease_buf(cc, epoch);
1314
1315 next = le32_to_cpu(cc->Next);
1316 if (!next)
1317 break;
1318 remaining -= next;
1319 cc = (struct create_context *)((char *)cc + next);
1320 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001321
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001322 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001323}
1324
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001325static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001326add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1327 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001328{
1329 struct smb2_create_req *req = iov[0].iov_base;
1330 unsigned int num = *num_iovec;
1331
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001332 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001333 if (iov[num].iov_base == NULL)
1334 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001335 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001336 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1337 if (!req->CreateContextsOffset)
1338 req->CreateContextsOffset = cpu_to_le32(
1339 sizeof(struct smb2_create_req) - 4 +
1340 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001341 le32_add_cpu(&req->CreateContextsLength,
1342 server->vals->create_lease_size);
1343 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001344 *num_iovec = num + 1;
1345 return 0;
1346}
1347
Steve Frenchb56eae42015-11-03 09:26:27 -06001348static struct create_durable_v2 *
1349create_durable_v2_buf(struct cifs_fid *pfid)
1350{
1351 struct create_durable_v2 *buf;
1352
1353 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1354 if (!buf)
1355 return NULL;
1356
1357 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1358 (struct create_durable_v2, dcontext));
1359 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1360 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1361 (struct create_durable_v2, Name));
1362 buf->ccontext.NameLength = cpu_to_le16(4);
1363
1364 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1365 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001366 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001367 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1368
1369 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1370 buf->Name[0] = 'D';
1371 buf->Name[1] = 'H';
1372 buf->Name[2] = '2';
1373 buf->Name[3] = 'Q';
1374 return buf;
1375}
1376
1377static struct create_durable_handle_reconnect_v2 *
1378create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1379{
1380 struct create_durable_handle_reconnect_v2 *buf;
1381
1382 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1383 GFP_KERNEL);
1384 if (!buf)
1385 return NULL;
1386
1387 buf->ccontext.DataOffset =
1388 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1389 dcontext));
1390 buf->ccontext.DataLength =
1391 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1392 buf->ccontext.NameOffset =
1393 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1394 Name));
1395 buf->ccontext.NameLength = cpu_to_le16(4);
1396
1397 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1398 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1399 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1400 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1401
1402 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1403 buf->Name[0] = 'D';
1404 buf->Name[1] = 'H';
1405 buf->Name[2] = '2';
1406 buf->Name[3] = 'C';
1407 return buf;
1408}
1409
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001410static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001411add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001412 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001413{
1414 struct smb2_create_req *req = iov[0].iov_base;
1415 unsigned int num = *num_iovec;
1416
Steve Frenchb56eae42015-11-03 09:26:27 -06001417 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1418 if (iov[num].iov_base == NULL)
1419 return -ENOMEM;
1420 iov[num].iov_len = sizeof(struct create_durable_v2);
1421 if (!req->CreateContextsOffset)
1422 req->CreateContextsOffset =
1423 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1424 iov[1].iov_len);
1425 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1426 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1427 *num_iovec = num + 1;
1428 return 0;
1429}
1430
1431static int
1432add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1433 struct cifs_open_parms *oparms)
1434{
1435 struct smb2_create_req *req = iov[0].iov_base;
1436 unsigned int num = *num_iovec;
1437
1438 /* indicate that we don't need to relock the file */
1439 oparms->reconnect = false;
1440
1441 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1442 if (iov[num].iov_base == NULL)
1443 return -ENOMEM;
1444 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1445 if (!req->CreateContextsOffset)
1446 req->CreateContextsOffset =
1447 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1448 iov[1].iov_len);
1449 le32_add_cpu(&req->CreateContextsLength,
1450 sizeof(struct create_durable_handle_reconnect_v2));
1451 inc_rfc1001_len(&req->hdr,
1452 sizeof(struct create_durable_handle_reconnect_v2));
1453 *num_iovec = num + 1;
1454 return 0;
1455}
1456
1457static int
1458add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1459 struct cifs_open_parms *oparms, bool use_persistent)
1460{
1461 struct smb2_create_req *req = iov[0].iov_base;
1462 unsigned int num = *num_iovec;
1463
1464 if (use_persistent) {
1465 if (oparms->reconnect)
1466 return add_durable_reconnect_v2_context(iov, num_iovec,
1467 oparms);
1468 else
1469 return add_durable_v2_context(iov, num_iovec, oparms);
1470 }
1471
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001472 if (oparms->reconnect) {
1473 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1474 /* indicate that we don't need to relock the file */
1475 oparms->reconnect = false;
1476 } else
1477 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001478 if (iov[num].iov_base == NULL)
1479 return -ENOMEM;
1480 iov[num].iov_len = sizeof(struct create_durable);
1481 if (!req->CreateContextsOffset)
1482 req->CreateContextsOffset =
1483 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1484 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001485 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001486 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1487 *num_iovec = num + 1;
1488 return 0;
1489}
1490
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001491int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001492SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001493 __u8 *oplock, struct smb2_file_all_info *buf,
1494 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001495{
1496 struct smb2_create_req *req;
1497 struct smb2_create_rsp *rsp;
1498 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001499 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001500 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001501 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001502 int resp_buftype;
1503 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001504 __le16 *copy_path = NULL;
1505 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001506 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001507 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001508 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001509 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001510
Joe Perchesf96637b2013-05-04 22:12:25 -05001511 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001512
1513 if (ses && (ses->server))
1514 server = ses->server;
1515 else
1516 return -EIO;
1517
1518 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1519 if (rc)
1520 return rc;
1521
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001522 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001523 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001524 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1525 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001526
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001527 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001528 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001529 /* File attributes ignored on open (used in create though) */
1530 req->FileAttributes = cpu_to_le32(file_attributes);
1531 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001532 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1533 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001534 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001535 /* do not count rfc1001 len field */
1536 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001537
1538 iov[0].iov_base = (char *)req;
1539 /* 4 for rfc1002 length field */
1540 iov[0].iov_len = get_rfc1002_length(req) + 4;
1541
1542 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001543 req->NameLength = cpu_to_le16(uni_path_len - 2);
1544 /* -1 since last byte is buf[0] which is sent below (path) */
1545 iov[0].iov_len--;
1546 if (uni_path_len % 8 != 0) {
1547 copy_size = uni_path_len / 8 * 8;
1548 if (copy_size < uni_path_len)
1549 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001550
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001551 copy_path = kzalloc(copy_size, GFP_KERNEL);
1552 if (!copy_path)
1553 return -ENOMEM;
1554 memcpy((char *)copy_path, (const char *)path,
1555 uni_path_len);
1556 uni_path_len = copy_size;
1557 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001558 }
1559
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001560 iov[1].iov_len = uni_path_len;
1561 iov[1].iov_base = path;
1562 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1563 inc_rfc1001_len(req, uni_path_len - 1);
1564
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001565 if (!server->oplocks)
1566 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1567
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001568 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001569 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1570 req->RequestedOplockLevel = *oplock;
1571 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001572 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001573 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001574 cifs_small_buf_release(req);
1575 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001576 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001577 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001578 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001579 }
1580
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001581 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1582 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001583 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001584 struct create_context *ccontext =
1585 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001586 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001587 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001588 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001589
1590 rc = add_durable_context(iov, &num_iovecs, oparms,
1591 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001592 if (rc) {
1593 cifs_small_buf_release(req);
1594 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001595 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001596 return rc;
1597 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001598 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001599 }
1600
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001601 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1602 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1603
1604 if (rc != 0) {
1605 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001606 if (err_buf)
1607 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1608 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001609 goto creat_exit;
1610 }
1611
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001612 oparms->fid->persistent_fid = rsp->PersistentFileId;
1613 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001614
1615 if (buf) {
1616 memcpy(buf, &rsp->CreationTime, 32);
1617 buf->AllocationSize = rsp->AllocationSize;
1618 buf->EndOfFile = rsp->EndofFile;
1619 buf->Attributes = rsp->FileAttributes;
1620 buf->NumberOfLinks = cpu_to_le32(1);
1621 buf->DeletePending = 0;
1622 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001623
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001624 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001625 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001626 else
1627 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001628creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001629 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001630 kfree(lc_buf);
1631 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001632 free_rsp_buf(resp_buftype, rsp);
1633 return rc;
1634}
1635
Steve French4a72daf2013-06-25 00:20:49 -05001636/*
1637 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1638 */
1639int
1640SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1641 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1642 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1643{
1644 struct smb2_ioctl_req *req;
1645 struct smb2_ioctl_rsp *rsp;
1646 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001647 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001648 struct kvec iov[2];
1649 int resp_buftype;
1650 int num_iovecs;
1651 int rc = 0;
1652
1653 cifs_dbg(FYI, "SMB2 IOCTL\n");
1654
Steve French3d1a3742014-08-11 21:05:25 -05001655 if (out_data != NULL)
1656 *out_data = NULL;
1657
Steve French4a72daf2013-06-25 00:20:49 -05001658 /* zero out returned data len, in case of error */
1659 if (plen)
1660 *plen = 0;
1661
Steve French8e353102015-03-26 19:47:02 -05001662 if (tcon)
1663 ses = tcon->ses;
1664 else
1665 return -EIO;
1666
Steve French4a72daf2013-06-25 00:20:49 -05001667 if (ses && (ses->server))
1668 server = ses->server;
1669 else
1670 return -EIO;
1671
1672 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1673 if (rc)
1674 return rc;
1675
1676 req->CtlCode = cpu_to_le32(opcode);
1677 req->PersistentFileId = persistent_fid;
1678 req->VolatileFileId = volatile_fid;
1679
1680 if (indatalen) {
1681 req->InputCount = cpu_to_le32(indatalen);
1682 /* do not set InputOffset if no input data */
1683 req->InputOffset =
1684 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1685 iov[1].iov_base = in_data;
1686 iov[1].iov_len = indatalen;
1687 num_iovecs = 2;
1688 } else
1689 num_iovecs = 1;
1690
1691 req->OutputOffset = 0;
1692 req->OutputCount = 0; /* MBZ */
1693
1694 /*
1695 * Could increase MaxOutputResponse, but that would require more
1696 * than one credit. Windows typically sets this smaller, but for some
1697 * ioctls it may be useful to allow server to send more. No point
1698 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001699 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1700 * (by default, note that it can be overridden to make max larger)
1701 * in responses (except for read responses which can be bigger.
1702 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001703 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001704 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001705
1706 if (is_fsctl)
1707 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1708 else
1709 req->Flags = 0;
1710
1711 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001712
Steve French7ff8d452013-10-14 00:44:19 -05001713 /*
1714 * If no input data, the size of ioctl struct in
1715 * protocol spec still includes a 1 byte data buffer,
1716 * but if input data passed to ioctl, we do not
1717 * want to double count this, so we do not send
1718 * the dummy one byte of data in iovec[0] if sending
1719 * input data (in iovec[1]). We also must add 4 bytes
1720 * in first iovec to allow for rfc1002 length field.
1721 */
1722
1723 if (indatalen) {
1724 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1725 inc_rfc1001_len(req, indatalen - 1);
1726 } else
1727 iov[0].iov_len = get_rfc1002_length(req) + 4;
1728
Steve Frenchfca16f92017-10-25 15:58:31 -05001729 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1730 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1731 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001732
1733 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1734 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1735
Steve French9bf0c9c2013-11-16 18:05:28 -06001736 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001737 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001738 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001739 } else if (rc == -EINVAL) {
1740 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1741 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001742 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001743 goto ioctl_exit;
1744 }
Steve French4a72daf2013-06-25 00:20:49 -05001745 }
1746
1747 /* check if caller wants to look at return data or just return rc */
1748 if ((plen == NULL) || (out_data == NULL))
1749 goto ioctl_exit;
1750
1751 *plen = le32_to_cpu(rsp->OutputCount);
1752
1753 /* We check for obvious errors in the output buffer length and offset */
1754 if (*plen == 0)
1755 goto ioctl_exit; /* server returned no data */
1756 else if (*plen > 0xFF00) {
1757 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1758 *plen = 0;
1759 rc = -EIO;
1760 goto ioctl_exit;
1761 }
1762
1763 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1764 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1765 le32_to_cpu(rsp->OutputOffset));
1766 *plen = 0;
1767 rc = -EIO;
1768 goto ioctl_exit;
1769 }
1770
1771 *out_data = kmalloc(*plen, GFP_KERNEL);
1772 if (*out_data == NULL) {
1773 rc = -ENOMEM;
1774 goto ioctl_exit;
1775 }
1776
Steve French373512e2015-12-18 13:05:30 -06001777 memcpy(*out_data,
1778 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001779 *plen);
1780ioctl_exit:
1781 free_rsp_buf(resp_buftype, rsp);
1782 return rc;
1783}
1784
Steve French64a5cfa2013-10-14 15:31:32 -05001785/*
1786 * Individual callers to ioctl worker function follow
1787 */
1788
1789int
1790SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1791 u64 persistent_fid, u64 volatile_fid)
1792{
1793 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001794 struct compress_ioctl fsctl_input;
1795 char *ret_data = NULL;
1796
1797 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001798 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001799
1800 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1801 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1802 (char *)&fsctl_input /* data input */,
1803 2 /* in data len */, &ret_data /* out data */, NULL);
1804
1805 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001806
1807 return rc;
1808}
1809
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001810int
1811SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1812 u64 persistent_fid, u64 volatile_fid)
1813{
1814 struct smb2_close_req *req;
1815 struct smb2_close_rsp *rsp;
1816 struct TCP_Server_Info *server;
1817 struct cifs_ses *ses = tcon->ses;
1818 struct kvec iov[1];
1819 int resp_buftype;
1820 int rc = 0;
1821
Joe Perchesf96637b2013-05-04 22:12:25 -05001822 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001823
1824 if (ses && (ses->server))
1825 server = ses->server;
1826 else
1827 return -EIO;
1828
1829 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1830 if (rc)
1831 return rc;
1832
1833 req->PersistentFileId = persistent_fid;
1834 req->VolatileFileId = volatile_fid;
1835
1836 iov[0].iov_base = (char *)req;
1837 /* 4 for rfc1002 length field */
1838 iov[0].iov_len = get_rfc1002_length(req) + 4;
1839
1840 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1841 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1842
1843 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001844 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001845 goto close_exit;
1846 }
1847
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001848 /* BB FIXME - decode close response, update inode for caching */
1849
1850close_exit:
1851 free_rsp_buf(resp_buftype, rsp);
1852 return rc;
1853}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001854
1855static int
1856validate_buf(unsigned int offset, unsigned int buffer_length,
1857 struct smb2_hdr *hdr, unsigned int min_buf_size)
1858
1859{
1860 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1861 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1862 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1863 char *end_of_buf = begin_of_buf + buffer_length;
1864
1865
1866 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001867 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1868 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001869 return -EINVAL;
1870 }
1871
1872 /* check if beyond RFC1001 maximum length */
1873 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001874 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1875 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001876 return -EINVAL;
1877 }
1878
1879 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001880 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001881 return -EINVAL;
1882 }
1883
1884 return 0;
1885}
1886
1887/*
1888 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1889 * Caller must free buffer.
1890 */
1891static int
1892validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1893 struct smb2_hdr *hdr, unsigned int minbufsize,
1894 char *data)
1895
1896{
1897 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1898 int rc;
1899
1900 if (!data)
1901 return -EINVAL;
1902
1903 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1904 if (rc)
1905 return rc;
1906
1907 memcpy(data, begin_of_buf, buffer_length);
1908
1909 return 0;
1910}
1911
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001912static int
1913query_info(const unsigned int xid, struct cifs_tcon *tcon,
1914 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1915 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001916{
1917 struct smb2_query_info_req *req;
1918 struct smb2_query_info_rsp *rsp = NULL;
1919 struct kvec iov[2];
1920 int rc = 0;
1921 int resp_buftype;
1922 struct TCP_Server_Info *server;
1923 struct cifs_ses *ses = tcon->ses;
1924
Joe Perchesf96637b2013-05-04 22:12:25 -05001925 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001926
1927 if (ses && (ses->server))
1928 server = ses->server;
1929 else
1930 return -EIO;
1931
1932 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1933 if (rc)
1934 return rc;
1935
1936 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001937 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001938 req->PersistentFileId = persistent_fid;
1939 req->VolatileFileId = volatile_fid;
1940 /* 4 for rfc1002 length field and 1 for Buffer */
1941 req->InputBufferOffset =
1942 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001943 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001944
1945 iov[0].iov_base = (char *)req;
1946 /* 4 for rfc1002 length field */
1947 iov[0].iov_len = get_rfc1002_length(req) + 4;
1948
1949 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001950 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1951
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001952 if (rc) {
1953 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1954 goto qinf_exit;
1955 }
1956
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001957 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1958 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001959 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001960
1961qinf_exit:
1962 free_rsp_buf(resp_buftype, rsp);
1963 return rc;
1964}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001965
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001966int
1967SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1968 u64 persistent_fid, u64 volatile_fid,
1969 struct smb2_file_all_info *data)
1970{
1971 return query_info(xid, tcon, persistent_fid, volatile_fid,
1972 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001973 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001974 sizeof(struct smb2_file_all_info), data);
1975}
1976
1977int
1978SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1979 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1980{
1981 return query_info(xid, tcon, persistent_fid, volatile_fid,
1982 FILE_INTERNAL_INFORMATION,
1983 sizeof(struct smb2_file_internal_info),
1984 sizeof(struct smb2_file_internal_info), uniqueid);
1985}
1986
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001987/*
1988 * This is a no-op for now. We're not really interested in the reply, but
1989 * rather in the fact that the server sent one and that server->lstrp
1990 * gets updated.
1991 *
1992 * FIXME: maybe we should consider checking that the reply matches request?
1993 */
1994static void
1995smb2_echo_callback(struct mid_q_entry *mid)
1996{
1997 struct TCP_Server_Info *server = mid->callback_data;
1998 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1999 unsigned int credits_received = 1;
2000
2001 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2002 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
2003
Christopher Oo5fb4e282015-06-25 16:10:48 -07002004 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002005 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002006 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002007 add_credits(server, credits_received, CIFS_ECHO_OP);
2008}
2009
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002010void smb2_reconnect_server(struct work_struct *work)
2011{
2012 struct TCP_Server_Info *server = container_of(work,
2013 struct TCP_Server_Info, reconnect.work);
2014 struct cifs_ses *ses;
2015 struct cifs_tcon *tcon, *tcon2;
2016 struct list_head tmp_list;
2017 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002018 int rc;
2019 int resched = false;
2020
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002021
2022 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2023 mutex_lock(&server->reconnect_mutex);
2024
2025 INIT_LIST_HEAD(&tmp_list);
2026 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2027
2028 spin_lock(&cifs_tcp_ses_lock);
2029 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2030 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002031 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002032 tcon->tc_count++;
2033 list_add_tail(&tcon->rlist, &tmp_list);
2034 tcon_exist = true;
2035 }
2036 }
2037 }
2038 /*
2039 * Get the reference to server struct to be sure that the last call of
2040 * cifs_put_tcon() in the loop below won't release the server pointer.
2041 */
2042 if (tcon_exist)
2043 server->srv_count++;
2044
2045 spin_unlock(&cifs_tcp_ses_lock);
2046
2047 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002048 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2049 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002050 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002051 else
2052 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002053 list_del_init(&tcon->rlist);
2054 cifs_put_tcon(tcon);
2055 }
2056
2057 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002058 if (resched)
2059 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002060 mutex_unlock(&server->reconnect_mutex);
2061
2062 /* now we can safely release srv struct */
2063 if (tcon_exist)
2064 cifs_put_tcp_session(server, 1);
2065}
2066
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002067int
2068SMB2_echo(struct TCP_Server_Info *server)
2069{
2070 struct smb2_echo_req *req;
2071 int rc = 0;
2072 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002073 struct smb_rqst rqst = { .rq_iov = &iov,
2074 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002075
Joe Perchesf96637b2013-05-04 22:12:25 -05002076 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002077
Steve French4fcd1812016-06-22 20:12:05 -05002078 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002079 /* No need to send echo on newly established connections */
2080 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2081 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002082 }
2083
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002084 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2085 if (rc)
2086 return rc;
2087
2088 req->hdr.CreditRequest = cpu_to_le16(1);
2089
2090 iov.iov_base = (char *)req;
2091 /* 4 for rfc1002 length field */
2092 iov.iov_len = get_rfc1002_length(req) + 4;
2093
Jeff Laytonfec344e2012-09-18 16:20:35 -07002094 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002095 CIFS_ECHO_OP);
2096 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002097 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002098
2099 cifs_small_buf_release(req);
2100 return rc;
2101}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002102
2103int
2104SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2105 u64 volatile_fid)
2106{
2107 struct smb2_flush_req *req;
2108 struct TCP_Server_Info *server;
2109 struct cifs_ses *ses = tcon->ses;
2110 struct kvec iov[1];
2111 int resp_buftype;
2112 int rc = 0;
2113
Joe Perchesf96637b2013-05-04 22:12:25 -05002114 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002115
2116 if (ses && (ses->server))
2117 server = ses->server;
2118 else
2119 return -EIO;
2120
2121 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2122 if (rc)
2123 return rc;
2124
2125 req->PersistentFileId = persistent_fid;
2126 req->VolatileFileId = volatile_fid;
2127
2128 iov[0].iov_base = (char *)req;
2129 /* 4 for rfc1002 length field */
2130 iov[0].iov_len = get_rfc1002_length(req) + 4;
2131
2132 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2133
Steve Frenchdfebe402015-03-27 01:00:06 -05002134 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002135 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2136
2137 free_rsp_buf(resp_buftype, iov[0].iov_base);
2138 return rc;
2139}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002140
2141/*
2142 * To form a chain of read requests, any read requests after the first should
2143 * have the end_of_chain boolean set to true.
2144 */
2145static int
2146smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2147 unsigned int remaining_bytes, int request_type)
2148{
2149 int rc = -EACCES;
2150 struct smb2_read_req *req = NULL;
2151
2152 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2153 if (rc)
2154 return rc;
2155 if (io_parms->tcon->ses->server == NULL)
2156 return -ECONNABORTED;
2157
2158 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2159
2160 req->PersistentFileId = io_parms->persistent_fid;
2161 req->VolatileFileId = io_parms->volatile_fid;
2162 req->ReadChannelInfoOffset = 0; /* reserved */
2163 req->ReadChannelInfoLength = 0; /* reserved */
2164 req->Channel = 0; /* reserved */
2165 req->MinimumCount = 0;
2166 req->Length = cpu_to_le32(io_parms->length);
2167 req->Offset = cpu_to_le64(io_parms->offset);
2168
2169 if (request_type & CHAINED_REQUEST) {
2170 if (!(request_type & END_OF_CHAIN)) {
2171 /* 4 for rfc1002 length field */
2172 req->hdr.NextCommand =
2173 cpu_to_le32(get_rfc1002_length(req) + 4);
2174 } else /* END_OF_CHAIN */
2175 req->hdr.NextCommand = 0;
2176 if (request_type & RELATED_REQUEST) {
2177 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2178 /*
2179 * Related requests use info from previous read request
2180 * in chain.
2181 */
2182 req->hdr.SessionId = 0xFFFFFFFF;
2183 req->hdr.TreeId = 0xFFFFFFFF;
2184 req->PersistentFileId = 0xFFFFFFFF;
2185 req->VolatileFileId = 0xFFFFFFFF;
2186 }
2187 }
2188 if (remaining_bytes > io_parms->length)
2189 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2190 else
2191 req->RemainingBytes = 0;
2192
2193 iov[0].iov_base = (char *)req;
2194 /* 4 for rfc1002 length field */
2195 iov[0].iov_len = get_rfc1002_length(req) + 4;
2196 return rc;
2197}
2198
2199static void
2200smb2_readv_callback(struct mid_q_entry *mid)
2201{
2202 struct cifs_readdata *rdata = mid->callback_data;
2203 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2204 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002205 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002206 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002207 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002208 .rq_nvec = 1,
2209 .rq_pages = rdata->pages,
2210 .rq_npages = rdata->nr_pages,
2211 .rq_pagesz = rdata->pagesz,
2212 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002213
Joe Perchesf96637b2013-05-04 22:12:25 -05002214 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2215 __func__, mid->mid, mid->mid_state, rdata->result,
2216 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002217
2218 switch (mid->mid_state) {
2219 case MID_RESPONSE_RECEIVED:
2220 credits_received = le16_to_cpu(buf->CreditRequest);
2221 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002222 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002223 int rc;
2224
Jeff Layton0b688cf2012-09-18 16:20:34 -07002225 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002226 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002227 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2228 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002229 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002230 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002231 task_io_account_read(rdata->got_bytes);
2232 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002233 break;
2234 case MID_REQUEST_SUBMITTED:
2235 case MID_RETRY_NEEDED:
2236 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002237 if (server->sign && rdata->got_bytes)
2238 /* reset bytes number since we can not check a sign */
2239 rdata->got_bytes = 0;
2240 /* FIXME: should this be counted toward the initiating task? */
2241 task_io_account_read(rdata->got_bytes);
2242 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002243 break;
2244 default:
2245 if (rdata->result != -ENODATA)
2246 rdata->result = -EIO;
2247 }
2248
2249 if (rdata->result)
2250 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2251
2252 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002253 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002254 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002255 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002256 add_credits(server, credits_received, 0);
2257}
2258
2259/* smb2_async_readv - send an async write, and set up mid to handle result */
2260int
2261smb2_async_readv(struct cifs_readdata *rdata)
2262{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002263 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002264 struct smb2_hdr *buf;
2265 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002266 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002267 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002268 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002269
Joe Perchesf96637b2013-05-04 22:12:25 -05002270 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2271 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002272
2273 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2274 io_parms.offset = rdata->offset;
2275 io_parms.length = rdata->bytes;
2276 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2277 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2278 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002279
2280 server = io_parms.tcon->ses->server;
2281
Jeff Layton58195752012-09-19 06:22:34 -07002282 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002283 if (rc) {
2284 if (rc == -EAGAIN && rdata->credits) {
2285 /* credits was reset by reconnect */
2286 rdata->credits = 0;
2287 /* reduce in_flight value since we won't send the req */
2288 spin_lock(&server->req_lock);
2289 server->in_flight--;
2290 spin_unlock(&server->req_lock);
2291 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002292 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002293 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002294
Jeff Layton58195752012-09-19 06:22:34 -07002295 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002296 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002297 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002298
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002299 if (rdata->credits) {
2300 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2301 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002302 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002303 spin_lock(&server->req_lock);
2304 server->credits += rdata->credits -
2305 le16_to_cpu(buf->CreditCharge);
2306 spin_unlock(&server->req_lock);
2307 wake_up(&server->request_q);
2308 flags = CIFS_HAS_CREDITS;
2309 }
2310
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002311 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002312 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002313 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002314 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002315 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002316 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002317 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2318 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002319
2320 cifs_small_buf_release(buf);
2321 return rc;
2322}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002323
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002324int
2325SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2326 unsigned int *nbytes, char **buf, int *buf_type)
2327{
2328 int resp_buftype, rc = -EACCES;
2329 struct smb2_read_rsp *rsp = NULL;
2330 struct kvec iov[1];
2331
2332 *nbytes = 0;
2333 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2334 if (rc)
2335 return rc;
2336
2337 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2338 &resp_buftype, CIFS_LOG_ERROR);
2339
2340 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2341
2342 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2343 free_rsp_buf(resp_buftype, iov[0].iov_base);
2344 return 0;
2345 }
2346
2347 if (rc) {
2348 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002349 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002350 } else {
2351 *nbytes = le32_to_cpu(rsp->DataLength);
2352 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2353 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002354 cifs_dbg(FYI, "bad length %d for count %d\n",
2355 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002356 rc = -EIO;
2357 *nbytes = 0;
2358 }
2359 }
2360
2361 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002362 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002363 *nbytes);
2364 free_rsp_buf(resp_buftype, iov[0].iov_base);
2365 } else if (resp_buftype != CIFS_NO_BUFFER) {
2366 *buf = iov[0].iov_base;
2367 if (resp_buftype == CIFS_SMALL_BUFFER)
2368 *buf_type = CIFS_SMALL_BUFFER;
2369 else if (resp_buftype == CIFS_LARGE_BUFFER)
2370 *buf_type = CIFS_LARGE_BUFFER;
2371 }
2372 return rc;
2373}
2374
Pavel Shilovsky33319142012-09-18 16:20:29 -07002375/*
2376 * Check the mid_state and signature on received buffer (if any), and queue the
2377 * workqueue completion task.
2378 */
2379static void
2380smb2_writev_callback(struct mid_q_entry *mid)
2381{
2382 struct cifs_writedata *wdata = mid->callback_data;
2383 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002384 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002385 unsigned int written;
2386 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2387 unsigned int credits_received = 1;
2388
2389 switch (mid->mid_state) {
2390 case MID_RESPONSE_RECEIVED:
2391 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2392 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2393 if (wdata->result != 0)
2394 break;
2395
2396 written = le32_to_cpu(rsp->DataLength);
2397 /*
2398 * Mask off high 16 bits when bytes written as returned
2399 * by the server is greater than bytes requested by the
2400 * client. OS/2 servers are known to set incorrect
2401 * CountHigh values.
2402 */
2403 if (written > wdata->bytes)
2404 written &= 0xFFFF;
2405
2406 if (written < wdata->bytes)
2407 wdata->result = -ENOSPC;
2408 else
2409 wdata->bytes = written;
2410 break;
2411 case MID_REQUEST_SUBMITTED:
2412 case MID_RETRY_NEEDED:
2413 wdata->result = -EAGAIN;
2414 break;
2415 default:
2416 wdata->result = -EIO;
2417 break;
2418 }
2419
2420 if (wdata->result)
2421 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2422
2423 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002424 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002425 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002426 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002427 add_credits(tcon->ses->server, credits_received, 0);
2428}
2429
2430/* smb2_async_writev - send an async write, and set up mid to handle result */
2431int
Steve French4a5c80d2014-02-07 20:45:12 -06002432smb2_async_writev(struct cifs_writedata *wdata,
2433 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002434{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002435 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002436 struct smb2_write_req *req = NULL;
2437 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002438 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002439 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002440 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002441
2442 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002443 if (rc) {
2444 if (rc == -EAGAIN && wdata->credits) {
2445 /* credits was reset by reconnect */
2446 wdata->credits = 0;
2447 /* reduce in_flight value since we won't send the req */
2448 spin_lock(&server->req_lock);
2449 server->in_flight--;
2450 spin_unlock(&server->req_lock);
2451 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002452 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002453 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002454
Pavel Shilovsky33319142012-09-18 16:20:29 -07002455 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2456
2457 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2458 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2459 req->WriteChannelInfoOffset = 0;
2460 req->WriteChannelInfoLength = 0;
2461 req->Channel = 0;
2462 req->Offset = cpu_to_le64(wdata->offset);
2463 /* 4 for rfc1002 length field */
2464 req->DataOffset = cpu_to_le16(
2465 offsetof(struct smb2_write_req, Buffer) - 4);
2466 req->RemainingBytes = 0;
2467
2468 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002469 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2470 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002471
Jeff Laytoneddb0792012-09-18 16:20:35 -07002472 rqst.rq_iov = &iov;
2473 rqst.rq_nvec = 1;
2474 rqst.rq_pages = wdata->pages;
2475 rqst.rq_npages = wdata->nr_pages;
2476 rqst.rq_pagesz = wdata->pagesz;
2477 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002478
Joe Perchesf96637b2013-05-04 22:12:25 -05002479 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2480 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002481
2482 req->Length = cpu_to_le32(wdata->bytes);
2483
2484 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2485
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002486 if (wdata->credits) {
2487 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2488 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002489 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002490 spin_lock(&server->req_lock);
2491 server->credits += wdata->credits -
2492 le16_to_cpu(req->hdr.CreditCharge);
2493 spin_unlock(&server->req_lock);
2494 wake_up(&server->request_q);
2495 flags = CIFS_HAS_CREDITS;
2496 }
2497
Pavel Shilovsky33319142012-09-18 16:20:29 -07002498 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002499 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2500 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002501
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002502 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002503 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002504 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2505 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002506
Pavel Shilovsky33319142012-09-18 16:20:29 -07002507async_writev_out:
2508 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002509 return rc;
2510}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002511
2512/*
2513 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2514 * The length field from io_parms must be at least 1 and indicates a number of
2515 * elements with data to write that begins with position 1 in iov array. All
2516 * data length is specified by count.
2517 */
2518int
2519SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2520 unsigned int *nbytes, struct kvec *iov, int n_vec)
2521{
2522 int rc = 0;
2523 struct smb2_write_req *req = NULL;
2524 struct smb2_write_rsp *rsp = NULL;
2525 int resp_buftype;
2526 *nbytes = 0;
2527
2528 if (n_vec < 1)
2529 return rc;
2530
2531 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2532 if (rc)
2533 return rc;
2534
2535 if (io_parms->tcon->ses->server == NULL)
2536 return -ECONNABORTED;
2537
2538 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2539
2540 req->PersistentFileId = io_parms->persistent_fid;
2541 req->VolatileFileId = io_parms->volatile_fid;
2542 req->WriteChannelInfoOffset = 0;
2543 req->WriteChannelInfoLength = 0;
2544 req->Channel = 0;
2545 req->Length = cpu_to_le32(io_parms->length);
2546 req->Offset = cpu_to_le64(io_parms->offset);
2547 /* 4 for rfc1002 length field */
2548 req->DataOffset = cpu_to_le16(
2549 offsetof(struct smb2_write_req, Buffer) - 4);
2550 req->RemainingBytes = 0;
2551
2552 iov[0].iov_base = (char *)req;
2553 /* 4 for rfc1002 length field and 1 for Buffer */
2554 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2555
2556 /* length of entire message including data to be written */
2557 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2558
2559 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2560 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002561 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002562
2563 if (rc) {
2564 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002565 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002566 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002567 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002568
2569 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002570 return rc;
2571}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002572
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002573static unsigned int
2574num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2575{
2576 int len;
2577 unsigned int entrycount = 0;
2578 unsigned int next_offset = 0;
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002579 char *entryptr;
2580 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002581
2582 if (bufstart == NULL)
2583 return 0;
2584
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002585 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002586
2587 while (1) {
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002588 if (entryptr + next_offset < entryptr ||
2589 entryptr + next_offset > end_of_buf ||
2590 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002591 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002592 break;
2593 }
2594
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002595 entryptr = entryptr + next_offset;
2596 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
2597
2598 len = le32_to_cpu(dir_info->FileNameLength);
2599 if (entryptr + len < entryptr ||
2600 entryptr + len > end_of_buf ||
2601 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002602 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2603 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002604 break;
2605 }
2606
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002607 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002608 entrycount++;
2609
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002610 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002611 if (!next_offset)
2612 break;
2613 }
2614
2615 return entrycount;
2616}
2617
2618/*
2619 * Readdir/FindFirst
2620 */
2621int
2622SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2623 u64 persistent_fid, u64 volatile_fid, int index,
2624 struct cifs_search_info *srch_inf)
2625{
2626 struct smb2_query_directory_req *req;
2627 struct smb2_query_directory_rsp *rsp = NULL;
2628 struct kvec iov[2];
2629 int rc = 0;
2630 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002631 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002632 unsigned char *bufptr;
2633 struct TCP_Server_Info *server;
2634 struct cifs_ses *ses = tcon->ses;
2635 __le16 asteriks = cpu_to_le16('*');
2636 char *end_of_smb;
2637 unsigned int output_size = CIFSMaxBufSize;
2638 size_t info_buf_size;
2639
2640 if (ses && (ses->server))
2641 server = ses->server;
2642 else
2643 return -EIO;
2644
2645 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2646 if (rc)
2647 return rc;
2648
2649 switch (srch_inf->info_level) {
2650 case SMB_FIND_FILE_DIRECTORY_INFO:
2651 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2652 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2653 break;
2654 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2655 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2656 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2657 break;
2658 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002659 cifs_dbg(VFS, "info level %u isn't supported\n",
2660 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002661 rc = -EINVAL;
2662 goto qdir_exit;
2663 }
2664
2665 req->FileIndex = cpu_to_le32(index);
2666 req->PersistentFileId = persistent_fid;
2667 req->VolatileFileId = volatile_fid;
2668
2669 len = 0x2;
2670 bufptr = req->Buffer;
2671 memcpy(bufptr, &asteriks, len);
2672
2673 req->FileNameOffset =
2674 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2675 req->FileNameLength = cpu_to_le16(len);
2676 /*
2677 * BB could be 30 bytes or so longer if we used SMB2 specific
2678 * buffer lengths, but this is safe and close enough.
2679 */
2680 output_size = min_t(unsigned int, output_size, server->maxBuf);
2681 output_size = min_t(unsigned int, output_size, 2 << 15);
2682 req->OutputBufferLength = cpu_to_le32(output_size);
2683
2684 iov[0].iov_base = (char *)req;
2685 /* 4 for RFC1001 length and 1 for Buffer */
2686 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2687
2688 iov[1].iov_base = (char *)(req->Buffer);
2689 iov[1].iov_len = len;
2690
2691 inc_rfc1001_len(req, len - 1 /* Buffer */);
2692
2693 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002694 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2695
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002696 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002697 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2698 srch_inf->endOfSearch = true;
2699 rc = 0;
Pavel Shilovsky9033b4f2019-01-26 12:21:32 -08002700 } else
2701 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002702 goto qdir_exit;
2703 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002704
2705 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2706 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2707 info_buf_size);
2708 if (rc)
2709 goto qdir_exit;
2710
2711 srch_inf->unicode = true;
2712
2713 if (srch_inf->ntwrk_buf_start) {
2714 if (srch_inf->smallBuf)
2715 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2716 else
2717 cifs_buf_release(srch_inf->ntwrk_buf_start);
2718 }
2719 srch_inf->ntwrk_buf_start = (char *)rsp;
2720 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2721 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2722 /* 4 for rfc1002 length field */
2723 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2724 srch_inf->entries_in_buffer =
2725 num_entries(srch_inf->srch_entries_start, end_of_smb,
2726 &srch_inf->last_entry, info_buf_size);
2727 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2729 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2730 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002731 if (resp_buftype == CIFS_LARGE_BUFFER)
2732 srch_inf->smallBuf = false;
2733 else if (resp_buftype == CIFS_SMALL_BUFFER)
2734 srch_inf->smallBuf = true;
2735 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002736 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002737
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002738 return rc;
2739
2740qdir_exit:
2741 free_rsp_buf(resp_buftype, rsp);
2742 return rc;
2743}
2744
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002745static int
2746send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002747 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002748 unsigned int num, void **data, unsigned int *size)
2749{
2750 struct smb2_set_info_req *req;
2751 struct smb2_set_info_rsp *rsp = NULL;
2752 struct kvec *iov;
2753 int rc = 0;
2754 int resp_buftype;
2755 unsigned int i;
2756 struct TCP_Server_Info *server;
2757 struct cifs_ses *ses = tcon->ses;
2758
2759 if (ses && (ses->server))
2760 server = ses->server;
2761 else
2762 return -EIO;
2763
2764 if (!num)
2765 return -EINVAL;
2766
2767 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2768 if (!iov)
2769 return -ENOMEM;
2770
2771 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2772 if (rc) {
2773 kfree(iov);
2774 return rc;
2775 }
2776
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002777 req->hdr.ProcessId = cpu_to_le32(pid);
2778
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002779 req->InfoType = SMB2_O_INFO_FILE;
2780 req->FileInfoClass = info_class;
2781 req->PersistentFileId = persistent_fid;
2782 req->VolatileFileId = volatile_fid;
2783
2784 /* 4 for RFC1001 length and 1 for Buffer */
2785 req->BufferOffset =
2786 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2787 req->BufferLength = cpu_to_le32(*size);
2788
2789 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2790
2791 memcpy(req->Buffer, *data, *size);
2792
2793 iov[0].iov_base = (char *)req;
2794 /* 4 for RFC1001 length */
2795 iov[0].iov_len = get_rfc1002_length(req) + 4;
2796
2797 for (i = 1; i < num; i++) {
2798 inc_rfc1001_len(req, size[i]);
2799 le32_add_cpu(&req->BufferLength, size[i]);
2800 iov[i].iov_base = (char *)data[i];
2801 iov[i].iov_len = size[i];
2802 }
2803
2804 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2805 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2806
Steve French7d3fb242013-11-18 09:56:28 -06002807 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002808 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002809
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002810 free_rsp_buf(resp_buftype, rsp);
2811 kfree(iov);
2812 return rc;
2813}
2814
2815int
2816SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2817 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2818{
2819 struct smb2_file_rename_info info;
2820 void **data;
2821 unsigned int size[2];
2822 int rc;
2823 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2824
2825 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2826 if (!data)
2827 return -ENOMEM;
2828
2829 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2830 /* 0 = fail if target already exists */
2831 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2832 info.FileNameLength = cpu_to_le32(len);
2833
2834 data[0] = &info;
2835 size[0] = sizeof(struct smb2_file_rename_info);
2836
2837 data[1] = target_file;
2838 size[1] = len + 2 /* null */;
2839
2840 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002841 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2842 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002843 kfree(data);
2844 return rc;
2845}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002846
2847int
Steve French897fba12016-05-12 21:20:36 -05002848SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2849 u64 persistent_fid, u64 volatile_fid)
2850{
2851 __u8 delete_pending = 1;
2852 void *data;
2853 unsigned int size;
2854
2855 data = &delete_pending;
2856 size = 1; /* sizeof __u8 */
2857
2858 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2859 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2860 &size);
2861}
2862
2863int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002864SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2865 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2866{
2867 struct smb2_file_link_info info;
2868 void **data;
2869 unsigned int size[2];
2870 int rc;
2871 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2872
2873 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2874 if (!data)
2875 return -ENOMEM;
2876
2877 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2878 /* 0 = fail if link already exists */
2879 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2880 info.FileNameLength = cpu_to_le32(len);
2881
2882 data[0] = &info;
2883 size[0] = sizeof(struct smb2_file_link_info);
2884
2885 data[1] = target_file;
2886 size[1] = len + 2 /* null */;
2887
2888 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002889 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002890 kfree(data);
2891 return rc;
2892}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002893
2894int
2895SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002896 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002897{
2898 struct smb2_file_eof_info info;
2899 void *data;
2900 unsigned int size;
2901
2902 info.EndOfFile = *eof;
2903
2904 data = &info;
2905 size = sizeof(struct smb2_file_eof_info);
2906
Steve Frenchf29ebb42014-07-19 21:44:58 -05002907 if (is_falloc)
2908 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2909 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2910 else
2911 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2912 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002913}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002914
2915int
2916SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2917 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2918{
2919 unsigned int size;
2920 size = sizeof(FILE_BASIC_INFO);
2921 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2922 current->tgid, FILE_BASIC_INFORMATION, 1,
2923 (void **)&buf, &size);
2924}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002925
2926int
2927SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2928 const u64 persistent_fid, const u64 volatile_fid,
2929 __u8 oplock_level)
2930{
2931 int rc;
2932 struct smb2_oplock_break *req = NULL;
2933
Joe Perchesf96637b2013-05-04 22:12:25 -05002934 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002935 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2936
2937 if (rc)
2938 return rc;
2939
2940 req->VolatileFid = volatile_fid;
2941 req->PersistentFid = persistent_fid;
2942 req->OplockLevel = oplock_level;
2943 req->hdr.CreditRequest = cpu_to_le16(1);
2944
2945 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2946 /* SMB2 buffer freed by function above */
2947
2948 if (rc) {
2949 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002950 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002951 }
2952
2953 return rc;
2954}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002955
2956static void
2957copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2958 struct kstatfs *kst)
2959{
2960 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2961 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2962 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302963 kst->f_bfree = kst->f_bavail =
2964 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002965 return;
2966}
2967
2968static int
2969build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2970 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2971{
2972 int rc;
2973 struct smb2_query_info_req *req;
2974
Joe Perchesf96637b2013-05-04 22:12:25 -05002975 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002976
2977 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2978 return -EIO;
2979
2980 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2981 if (rc)
2982 return rc;
2983
2984 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2985 req->FileInfoClass = level;
2986 req->PersistentFileId = persistent_fid;
2987 req->VolatileFileId = volatile_fid;
2988 /* 4 for rfc1002 length field and 1 for pad */
2989 req->InputBufferOffset =
2990 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2991 req->OutputBufferLength = cpu_to_le32(
2992 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2993
2994 iov->iov_base = (char *)req;
2995 /* 4 for rfc1002 length field */
2996 iov->iov_len = get_rfc1002_length(req) + 4;
2997 return 0;
2998}
2999
3000int
3001SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3002 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3003{
3004 struct smb2_query_info_rsp *rsp = NULL;
3005 struct kvec iov;
3006 int rc = 0;
3007 int resp_buftype;
3008 struct cifs_ses *ses = tcon->ses;
3009 struct smb2_fs_full_size_info *info = NULL;
3010
3011 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3012 sizeof(struct smb2_fs_full_size_info),
3013 persistent_fid, volatile_fid);
3014 if (rc)
3015 return rc;
3016
3017 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3018 if (rc) {
3019 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003020 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003021 }
3022 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3023
3024 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3025 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3026 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3027 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3028 sizeof(struct smb2_fs_full_size_info));
3029 if (!rc)
3030 copy_fs_info_to_kstatfs(info, fsdata);
3031
Steve French34f62642013-10-09 02:07:00 -05003032qfsinf_exit:
3033 free_rsp_buf(resp_buftype, iov.iov_base);
3034 return rc;
3035}
3036
3037int
3038SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003039 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003040{
3041 struct smb2_query_info_rsp *rsp = NULL;
3042 struct kvec iov;
3043 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003044 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003045 struct cifs_ses *ses = tcon->ses;
3046 unsigned int rsp_len, offset;
3047
Steven French21671142013-10-09 13:36:35 -05003048 if (level == FS_DEVICE_INFORMATION) {
3049 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3050 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3051 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3052 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3053 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003054 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3055 max_len = sizeof(struct smb3_fs_ss_info);
3056 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003057 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003058 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003059 return -EINVAL;
3060 }
3061
3062 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003063 persistent_fid, volatile_fid);
3064 if (rc)
3065 return rc;
3066
3067 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3068 if (rc) {
3069 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3070 goto qfsattr_exit;
3071 }
3072 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3073
3074 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3075 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003076 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3077 if (rc)
3078 goto qfsattr_exit;
3079
3080 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003081 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3082 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003083 rsp_len, max_len));
3084 else if (level == FS_DEVICE_INFORMATION)
3085 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3086 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003087 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3088 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3089 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3090 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3091 tcon->perf_sector_size =
3092 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3093 }
Steve French34f62642013-10-09 02:07:00 -05003094
3095qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003096 free_rsp_buf(resp_buftype, iov.iov_base);
3097 return rc;
3098}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003099
3100int
3101smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3102 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3103 const __u32 num_lock, struct smb2_lock_element *buf)
3104{
3105 int rc = 0;
3106 struct smb2_lock_req *req = NULL;
3107 struct kvec iov[2];
3108 int resp_buf_type;
3109 unsigned int count;
3110
Joe Perchesf96637b2013-05-04 22:12:25 -05003111 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003112
3113 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3114 if (rc)
3115 return rc;
3116
3117 req->hdr.ProcessId = cpu_to_le32(pid);
3118 req->LockCount = cpu_to_le16(num_lock);
3119
3120 req->PersistentFileId = persist_fid;
3121 req->VolatileFileId = volatile_fid;
3122
3123 count = num_lock * sizeof(struct smb2_lock_element);
3124 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3125
3126 iov[0].iov_base = (char *)req;
3127 /* 4 for rfc1002 length field and count for all locks */
3128 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3129 iov[1].iov_base = (char *)buf;
3130 iov[1].iov_len = count;
3131
3132 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3133 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3134 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003135 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003136 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3137 }
3138
3139 return rc;
3140}
3141
3142int
3143SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3144 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3145 const __u64 length, const __u64 offset, const __u32 lock_flags,
3146 const bool wait)
3147{
3148 struct smb2_lock_element lock;
3149
3150 lock.Offset = cpu_to_le64(offset);
3151 lock.Length = cpu_to_le64(length);
3152 lock.Flags = cpu_to_le32(lock_flags);
3153 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3154 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3155
3156 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3157}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003158
3159int
3160SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3161 __u8 *lease_key, const __le32 lease_state)
3162{
3163 int rc;
3164 struct smb2_lease_ack *req = NULL;
3165
Joe Perchesf96637b2013-05-04 22:12:25 -05003166 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003167 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3168
3169 if (rc)
3170 return rc;
3171
3172 req->hdr.CreditRequest = cpu_to_le16(1);
3173 req->StructureSize = cpu_to_le16(36);
3174 inc_rfc1001_len(req, 12);
3175
3176 memcpy(req->LeaseKey, lease_key, 16);
3177 req->LeaseState = lease_state;
3178
3179 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3180 /* SMB2 buffer freed by function above */
3181
3182 if (rc) {
3183 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003184 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003185 }
3186
3187 return rc;
3188}