blob: 1a47b952e9bd6fbd6c6714d5591e061822aa58f4 [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);
250 if (!rc && tcon->ses->need_reconnect)
251 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
252
253 if (rc || !tcon->need_reconnect) {
254 mutex_unlock(&tcon->ses->session_mutex);
255 goto out;
256 }
257
258 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800259 if (tcon->use_persistent)
260 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500261
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400262 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
263 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500264
Joe Perchesf96637b2013-05-04 22:12:25 -0500265 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400266 if (rc)
267 goto out;
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800268
269 if (smb2_command != SMB2_INTERNAL_CMD)
270 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
271
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400272 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400273out:
274 /*
275 * Check if handle based operation so we know whether we can continue
276 * or not without returning to caller to reset file handle.
277 */
278 /*
279 * BB Is flush done by server on drop of tcp session? Should we special
280 * case it and skip above?
281 */
282 switch (smb2_command) {
283 case SMB2_FLUSH:
284 case SMB2_READ:
285 case SMB2_WRITE:
286 case SMB2_LOCK:
287 case SMB2_IOCTL:
288 case SMB2_QUERY_DIRECTORY:
289 case SMB2_CHANGE_NOTIFY:
290 case SMB2_QUERY_INFO:
291 case SMB2_SET_INFO:
Pavel Shilovsky69d13b62016-11-29 11:30:58 -0800292 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400293 }
294 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400295 return rc;
296}
297
298/*
299 * Allocate and return pointer to an SMB request hdr, and set basic
300 * SMB information in the SMB header. If the return code is zero, this
301 * function must have filled in request_buf pointer.
302 */
303static int
304small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
305 void **request_buf)
306{
307 int rc = 0;
308
309 rc = smb2_reconnect(smb2_command, tcon);
310 if (rc)
311 return rc;
312
313 /* BB eventually switch this to SMB2 specific small buf size */
314 *request_buf = cifs_small_buf_get();
315 if (*request_buf == NULL) {
316 /* BB should we add a retry in here if not a writepage? */
317 return -ENOMEM;
318 }
319
320 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
321
322 if (tcon != NULL) {
Steve French7aed5a52018-07-23 09:15:18 -0500323#ifdef CONFIG_CIFS_STATS
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400324 uint16_t com_code = le16_to_cpu(smb2_command);
325 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400326#endif
327 cifs_stats_inc(&tcon->num_smbs_sent);
328 }
329
330 return rc;
331}
332
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500333#ifdef CONFIG_CIFS_SMB311
334/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
335#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
336
337
338#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
339#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
340
341static void
342build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
343{
344 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
345 pneg_ctxt->DataLength = cpu_to_le16(38);
346 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
347 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
348 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
349 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
350}
351
352static void
353build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
354{
355 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
356 pneg_ctxt->DataLength = cpu_to_le16(6);
357 pneg_ctxt->CipherCount = cpu_to_le16(2);
358 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
359 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
360}
361
362static void
363assemble_neg_contexts(struct smb2_negotiate_req *req)
364{
365
366 /* +4 is to account for the RFC1001 len field */
367 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
368
369 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
370 /* Add 2 to size to round to 8 byte boundary */
371 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
372 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
373 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
374 req->NegotiateContextCount = cpu_to_le16(2);
Steve Frenchf2d395b2017-09-18 18:18:45 -0500375 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500376 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
377}
378#else
379static void assemble_neg_contexts(struct smb2_negotiate_req *req)
380{
381 return;
382}
383#endif /* SMB311 */
384
385
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400386/*
387 *
388 * SMB2 Worker functions follow:
389 *
390 * The general structure of the worker functions is:
391 * 1) Call smb2_init (assembles SMB2 header)
392 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
393 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
394 * 4) Decode SMB2 command specific fields in the fixed length area
395 * 5) Decode variable length data area (if any for this SMB2 command type)
396 * 6) Call free smb buffer
397 * 7) return
398 *
399 */
400
401int
402SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
403{
404 struct smb2_negotiate_req *req;
405 struct smb2_negotiate_rsp *rsp;
406 struct kvec iov[1];
407 int rc = 0;
408 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400409 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400410 int blob_offset, blob_length;
411 char *security_blob;
412 int flags = CIFS_NEG_OP;
413
Joe Perchesf96637b2013-05-04 22:12:25 -0500414 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400415
Jeff Layton3534b852013-05-24 07:41:01 -0400416 if (!server) {
417 WARN(1, "%s: server is NULL!\n", __func__);
418 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400419 }
420
421 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
422 if (rc)
423 return rc;
424
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400425 req->hdr.SessionId = 0;
426
Steve Frenche4aa25e2012-10-01 12:26:22 -0500427 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400428
Steve Frenche4aa25e2012-10-01 12:26:22 -0500429 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
430 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400431
432 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400433 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500434 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400435 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500436 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400437 else
438 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400439
Steve Frenche4aa25e2012-10-01 12:26:22 -0500440 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400441
Steve French3c5f9be12014-05-13 13:37:45 -0700442 /* ClientGUID must be zero for SMB2.02 dialect */
443 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
444 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500445 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700446 memcpy(req->ClientGUID, server->client_guid,
447 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500448 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
449 assemble_neg_contexts(req);
450 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400451 iov[0].iov_base = (char *)req;
452 /* 4 for rfc1002 length field */
453 iov[0].iov_len = get_rfc1002_length(req) + 4;
454
455 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
456
457 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
458 /*
459 * No tcon so can't do
460 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
461 */
462 if (rc != 0)
463 goto neg_exit;
464
Joe Perchesf96637b2013-05-04 22:12:25 -0500465 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400466
Steve Frenche4aa25e2012-10-01 12:26:22 -0500467 /* BB we may eventually want to match the negotiated vs. requested
468 dialect, even though we are only requesting one at a time */
469 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500470 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500471 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500472 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500473 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500474 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500475 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
476 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600477#ifdef CONFIG_CIFS_SMB311
478 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
479 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
480#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400481 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500482 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500483 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400484 rc = -EIO;
485 goto neg_exit;
486 }
487 server->dialect = le16_to_cpu(rsp->DialectRevision);
488
Jeff Laytone598d1d82013-05-26 07:00:59 -0400489 /* SMB2 only has an extended negflavor */
490 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400491 /* set it to the maximum buffer size value we can send with 1 credit */
492 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
493 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400494 server->max_read = le32_to_cpu(rsp->MaxReadSize);
495 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
496 /* BB Do we need to validate the SecurityMode? */
497 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
498 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400499 /* Internal types */
500 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400501
502 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
503 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500504 /*
505 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
506 * for us will be
507 * ses->sectype = RawNTLMSSP;
508 * but for time being this is our only auth choice so doesn't matter.
509 * We just found a server which sets blob length to zero expecting raw.
510 */
511 if (blob_length == 0)
512 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700513
Jeff Layton38d77c52013-05-26 07:01:00 -0400514 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400515 if (rc)
516 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500517 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500518 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500519 if (rc == 1)
520 rc = 0;
521 else if (rc == 0)
522 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400523 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400524neg_exit:
525 free_rsp_buf(resp_buftype, rsp);
526 return rc;
527}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400528
Steve Frenchff1c0382013-11-19 23:44:46 -0600529int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
530{
531 int rc = 0;
532 struct validate_negotiate_info_req vneg_inbuf;
533 struct validate_negotiate_info_rsp *pneg_rsp;
534 u32 rsplen;
535
536 cifs_dbg(FYI, "validate negotiate\n");
537
538 /*
539 * validation ioctl must be signed, so no point sending this if we
Steve French0e1b85a2017-09-20 19:57:18 -0500540 * can not sign it (ie are not known user). Even if signing is not
541 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600542 * sign just this, the first and only signed request on a connection.
Steve French0e1b85a2017-09-20 19:57:18 -0500543 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600544 */
Steve French0e1b85a2017-09-20 19:57:18 -0500545 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600546 return 0; /* validation requires signing */
547
Steve French0e1b85a2017-09-20 19:57:18 -0500548 if (tcon->ses->user_name == NULL) {
549 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
550 return 0; /* validation requires signing */
551 }
552
553 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
554 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
555
Steve Frenchff1c0382013-11-19 23:44:46 -0600556 vneg_inbuf.Capabilities =
557 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100558 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
559 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600560
561 if (tcon->ses->sign)
562 vneg_inbuf.SecurityMode =
563 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
564 else if (global_secflags & CIFSSEC_MAY_SIGN)
565 vneg_inbuf.SecurityMode =
566 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
567 else
568 vneg_inbuf.SecurityMode = 0;
569
570 vneg_inbuf.DialectCount = cpu_to_le16(1);
571 vneg_inbuf.Dialects[0] =
572 cpu_to_le16(tcon->ses->server->vals->protocol_id);
573
574 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
575 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
576 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
577 (char **)&pneg_rsp, &rsplen);
578
579 if (rc != 0) {
580 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
581 return -EIO;
582 }
583
584 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French8dd4e3f2017-05-03 21:12:20 -0500585 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
586 rsplen);
587
588 /* relax check since Mac returns max bufsize allowed on ioctl */
589 if (rsplen > CIFSMaxBufSize)
590 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600591 }
592
593 /* check validate negotiate info response matches what we got earlier */
Daniel N Petterssonf59eda12018-01-11 16:00:12 +0100594 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600595 goto vneg_out;
596
597 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
598 goto vneg_out;
599
600 /* do not validate server guid because not saved at negprot time yet */
601
602 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
603 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
604 goto vneg_out;
605
606 /* validate negotiate successful */
607 cifs_dbg(FYI, "validate negotiate info successful\n");
608 return 0;
609
610vneg_out:
611 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
612 return -EIO;
613}
614
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100615struct SMB2_sess_data {
616 unsigned int xid;
617 struct cifs_ses *ses;
618 struct nls_table *nls_cp;
619 void (*func)(struct SMB2_sess_data *);
620 int result;
621 u64 previous_session;
622
623 /* we will send the SMB in three pieces:
624 * a fixed length beginning part, an optional
625 * SPNEGO blob (which can be zero length), and a
626 * last part which will include the strings
627 * and rest of bcc area. This allows us to avoid
628 * a large buffer 17K allocation
629 */
630 int buf0_type;
631 struct kvec iov[2];
632};
633
634static int
635SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
636{
637 int rc;
638 struct cifs_ses *ses = sess_data->ses;
639 struct smb2_sess_setup_req *req;
640 struct TCP_Server_Info *server = ses->server;
641
642 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
643 if (rc)
644 return rc;
645
646 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
647
648 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
649 req->PreviousSessionId = sess_data->previous_session;
650
651 req->Flags = 0; /* MBZ */
652 /* to enable echos and oplocks */
653 req->hdr.CreditRequest = cpu_to_le16(3);
654
655 /* only one of SMB2 signing flags may be set in SMB2 request */
656 if (server->sign)
657 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
658 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
659 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
660 else
661 req->SecurityMode = 0;
662
663 req->Capabilities = 0;
664 req->Channel = 0; /* MBZ */
665
666 sess_data->iov[0].iov_base = (char *)req;
667 /* 4 for rfc1002 length field and 1 for pad */
668 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
669 /*
670 * This variable will be used to clear the buffer
671 * allocated above in case of any error in the calling function.
672 */
673 sess_data->buf0_type = CIFS_SMALL_BUFFER;
674
675 return 0;
676}
677
678static void
679SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
680{
681 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
682 sess_data->buf0_type = CIFS_NO_BUFFER;
683}
684
685static int
686SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
687{
688 int rc;
689 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
690
691 /* Testing shows that buffer offset must be at location of Buffer[0] */
692 req->SecurityBufferOffset =
693 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
694 1 /* pad */ - 4 /* rfc1001 len */);
695 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
696
697 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
698
699 /* BB add code to build os and lm fields */
700
701 rc = SendReceive2(sess_data->xid, sess_data->ses,
702 sess_data->iov, 2,
703 &sess_data->buf0_type,
704 CIFS_LOG_ERROR | CIFS_NEG_OP);
705
706 return rc;
707}
708
709static int
710SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
711{
712 int rc = 0;
713 struct cifs_ses *ses = sess_data->ses;
714
715 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800716 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100717 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100718 if (rc) {
719 cifs_dbg(FYI,
720 "SMB3 session key generation failed\n");
721 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800722 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100723 }
724 }
725 if (!ses->server->session_estab) {
726 ses->server->sequence_number = 0x2;
727 ses->server->session_estab = true;
728 }
729 mutex_unlock(&ses->server->srv_mutex);
730
731 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
732 spin_lock(&GlobalMid_Lock);
733 ses->status = CifsGood;
734 ses->need_reconnect = false;
735 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100736 return rc;
737}
738
739#ifdef CONFIG_CIFS_UPCALL
740static void
741SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
742{
743 int rc;
744 struct cifs_ses *ses = sess_data->ses;
745 struct cifs_spnego_msg *msg;
746 struct key *spnego_key = NULL;
747 struct smb2_sess_setup_rsp *rsp = NULL;
748
749 rc = SMB2_sess_alloc_buffer(sess_data);
750 if (rc)
751 goto out;
752
753 spnego_key = cifs_get_spnego_key(ses);
754 if (IS_ERR(spnego_key)) {
755 rc = PTR_ERR(spnego_key);
756 spnego_key = NULL;
757 goto out;
758 }
759
760 msg = spnego_key->payload.data[0];
761 /*
762 * check version field to make sure that cifs.upcall is
763 * sending us a response in an expected form
764 */
765 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
766 cifs_dbg(VFS,
767 "bad cifs.upcall version. Expected %d got %d",
768 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
769 rc = -EKEYREJECTED;
770 goto out_put_spnego_key;
771 }
772
773 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
774 GFP_KERNEL);
775 if (!ses->auth_key.response) {
776 cifs_dbg(VFS,
777 "Kerberos can't allocate (%u bytes) memory",
778 msg->sesskey_len);
779 rc = -ENOMEM;
780 goto out_put_spnego_key;
781 }
782 ses->auth_key.len = msg->sesskey_len;
783
784 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
785 sess_data->iov[1].iov_len = msg->secblob_len;
786
787 rc = SMB2_sess_sendreceive(sess_data);
788 if (rc)
789 goto out_put_spnego_key;
790
791 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
792 ses->Suid = rsp->hdr.SessionId;
793
794 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
795 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
796 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
797
798 rc = SMB2_sess_establish_session(sess_data);
799out_put_spnego_key:
800 key_invalidate(spnego_key);
801 key_put(spnego_key);
802out:
803 sess_data->result = rc;
804 sess_data->func = NULL;
805 SMB2_sess_free_buffer(sess_data);
806}
807#else
808static void
809SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
810{
811 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
812 sess_data->result = -EOPNOTSUPP;
813 sess_data->func = NULL;
814}
815#endif
816
Sachin Prabhu166cea42016-10-07 19:11:22 +0100817static void
818SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
819
820static void
821SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
822{
823 int rc;
824 struct cifs_ses *ses = sess_data->ses;
825 struct smb2_sess_setup_rsp *rsp = NULL;
826 char *ntlmssp_blob = NULL;
827 bool use_spnego = false; /* else use raw ntlmssp */
828 u16 blob_length = 0;
829
830 /*
831 * If memory allocation is successful, caller of this function
832 * frees it.
833 */
834 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
835 if (!ses->ntlmssp) {
836 rc = -ENOMEM;
837 goto out_err;
838 }
839 ses->ntlmssp->sesskey_per_smbsess = true;
840
841 rc = SMB2_sess_alloc_buffer(sess_data);
842 if (rc)
843 goto out_err;
844
845 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
846 GFP_KERNEL);
847 if (ntlmssp_blob == NULL) {
848 rc = -ENOMEM;
849 goto out;
850 }
851
852 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
853 if (use_spnego) {
854 /* BB eventually need to add this */
855 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
856 rc = -EOPNOTSUPP;
857 goto out;
858 } else {
859 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
860 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
861 }
862 sess_data->iov[1].iov_base = ntlmssp_blob;
863 sess_data->iov[1].iov_len = blob_length;
864
865 rc = SMB2_sess_sendreceive(sess_data);
866 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
867
868 /* If true, rc here is expected and not an error */
869 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
870 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
871 rc = 0;
872
873 if (rc)
874 goto out;
875
876 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
877 le16_to_cpu(rsp->SecurityBufferOffset)) {
878 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
879 le16_to_cpu(rsp->SecurityBufferOffset));
880 rc = -EIO;
881 goto out;
882 }
883 rc = decode_ntlmssp_challenge(rsp->Buffer,
884 le16_to_cpu(rsp->SecurityBufferLength), ses);
885 if (rc)
886 goto out;
887
888 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
889
890
891 ses->Suid = rsp->hdr.SessionId;
892 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
893 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
894 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
895
896out:
897 kfree(ntlmssp_blob);
898 SMB2_sess_free_buffer(sess_data);
899 if (!rc) {
900 sess_data->result = 0;
901 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
902 return;
903 }
904out_err:
905 kfree(ses->ntlmssp);
906 ses->ntlmssp = NULL;
907 sess_data->result = rc;
908 sess_data->func = NULL;
909}
910
911static void
912SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
913{
914 int rc;
915 struct cifs_ses *ses = sess_data->ses;
916 struct smb2_sess_setup_req *req;
917 struct smb2_sess_setup_rsp *rsp = NULL;
918 unsigned char *ntlmssp_blob = NULL;
919 bool use_spnego = false; /* else use raw ntlmssp */
920 u16 blob_length = 0;
921
922 rc = SMB2_sess_alloc_buffer(sess_data);
923 if (rc)
924 goto out;
925
926 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
927 req->hdr.SessionId = ses->Suid;
928
929 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
930 sess_data->nls_cp);
931 if (rc) {
932 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
933 goto out;
934 }
935
936 if (use_spnego) {
937 /* BB eventually need to add this */
938 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
939 rc = -EOPNOTSUPP;
940 goto out;
941 }
942 sess_data->iov[1].iov_base = ntlmssp_blob;
943 sess_data->iov[1].iov_len = blob_length;
944
945 rc = SMB2_sess_sendreceive(sess_data);
946 if (rc)
947 goto out;
948
949 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
950
951 ses->Suid = rsp->hdr.SessionId;
952 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
953 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
954 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
955
956 rc = SMB2_sess_establish_session(sess_data);
957out:
958 kfree(ntlmssp_blob);
959 SMB2_sess_free_buffer(sess_data);
960 kfree(ses->ntlmssp);
961 ses->ntlmssp = NULL;
962 sess_data->result = rc;
963 sess_data->func = NULL;
964}
965
966static int
967SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
968{
969 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
970 ses->sectype = RawNTLMSSP;
971
972 switch (ses->sectype) {
973 case Kerberos:
974 sess_data->func = SMB2_auth_kerberos;
975 break;
976 case RawNTLMSSP:
977 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
978 break;
979 default:
980 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
981 return -EOPNOTSUPP;
982 }
983
984 return 0;
985}
986
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400987int
988SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
989 const struct nls_table *nls_cp)
990{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400991 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400992 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100993 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400994
Joe Perchesf96637b2013-05-04 22:12:25 -0500995 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400996
Jeff Layton3534b852013-05-24 07:41:01 -0400997 if (!server) {
998 WARN(1, "%s: server is NULL!\n", __func__);
999 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001000 }
1001
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001002 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1003 if (!sess_data)
1004 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001005
1006 rc = SMB2_select_sec(ses, sess_data);
1007 if (rc)
1008 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001009 sess_data->xid = xid;
1010 sess_data->ses = ses;
1011 sess_data->buf0_type = CIFS_NO_BUFFER;
1012 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frencha6c9a622018-05-31 15:19:25 -05001013 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001014
Sachin Prabhu166cea42016-10-07 19:11:22 +01001015 while (sess_data->func)
1016 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001017
Steve Frenchdf1be202017-09-19 18:40:03 -05001018 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1019 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001020 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001021out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001022 kfree(sess_data);
1023 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001024}
1025
1026int
1027SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1028{
1029 struct smb2_logoff_req *req; /* response is also trivial struct */
1030 int rc = 0;
1031 struct TCP_Server_Info *server;
1032
Joe Perchesf96637b2013-05-04 22:12:25 -05001033 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001034
1035 if (ses && (ses->server))
1036 server = ses->server;
1037 else
1038 return -EIO;
1039
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001040 /* no need to send SMB logoff if uid already closed due to reconnect */
1041 if (ses->need_reconnect)
1042 goto smb2_session_already_dead;
1043
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001044 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1045 if (rc)
1046 return rc;
1047
1048 /* since no tcon, smb2_init can not do this, so do here */
1049 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001050 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001051 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001052
1053 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1054 /*
1055 * No tcon so can't do
1056 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1057 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001058
1059smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001060 return rc;
1061}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001062
1063static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1064{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001065 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001066}
1067
1068#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1069
Steve Frenchde9f68d2013-11-15 11:26:24 -06001070/* These are similar values to what Windows uses */
1071static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1072{
1073 tcon->max_chunks = 256;
1074 tcon->max_bytes_chunk = 1048576;
1075 tcon->max_bytes_copy = 16777216;
1076}
1077
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001078int
1079SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1080 struct cifs_tcon *tcon, const struct nls_table *cp)
1081{
1082 struct smb2_tree_connect_req *req;
1083 struct smb2_tree_connect_rsp *rsp = NULL;
1084 struct kvec iov[2];
1085 int rc = 0;
1086 int resp_buftype;
1087 int unc_path_len;
1088 struct TCP_Server_Info *server;
1089 __le16 *unc_path = NULL;
1090
Joe Perchesf96637b2013-05-04 22:12:25 -05001091 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001092
1093 if ((ses->server) && tree)
1094 server = ses->server;
1095 else
1096 return -EIO;
1097
Steve Frenchff9f84b2015-09-26 09:48:58 -05001098 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001099 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1100 cifs_dbg(VFS, "encryption requested but no server support");
1101 return -EOPNOTSUPP;
1102 }
1103
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001104 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1105 if (unc_path == NULL)
1106 return -ENOMEM;
1107
1108 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1109 unc_path_len *= 2;
1110 if (unc_path_len < 2) {
1111 kfree(unc_path);
1112 return -EINVAL;
1113 }
1114
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001115 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1116 if (tcon)
1117 tcon->tid = 0;
1118
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001119 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1120 if (rc) {
1121 kfree(unc_path);
1122 return rc;
1123 }
1124
1125 if (tcon == NULL) {
1126 /* since no tcon, smb2_init can not do this, so do here */
1127 req->hdr.SessionId = ses->Suid;
1128 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1129 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1130 }
1131
1132 iov[0].iov_base = (char *)req;
1133 /* 4 for rfc1002 length field and 1 for pad */
1134 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1135
1136 /* Testing shows that buffer offset must be at location of Buffer[0] */
1137 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1138 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1139 req->PathLength = cpu_to_le16(unc_path_len - 2);
1140 iov[1].iov_base = unc_path;
1141 iov[1].iov_len = unc_path_len;
1142
1143 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1144
1145 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1146 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1147
1148 if (rc != 0) {
1149 if (tcon) {
1150 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1151 tcon->need_reconnect = true;
1152 }
1153 goto tcon_error_exit;
1154 }
1155
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001156 if (tcon == NULL) {
1157 ses->ipc_tid = rsp->hdr.TreeId;
1158 goto tcon_exit;
1159 }
1160
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001161 switch (rsp->ShareType) {
1162 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001163 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001164 break;
1165 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001166 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001167 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001168 break;
1169 case SMB2_SHARE_TYPE_PRINT:
1170 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001171 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001172 break;
1173 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001174 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001175 rc = -EOPNOTSUPP;
1176 goto tcon_error_exit;
1177 }
1178
1179 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001180 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001181 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1182 tcon->tidStatus = CifsGood;
1183 tcon->need_reconnect = false;
1184 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001185 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001186
1187 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1188 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001189 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001190 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001191 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1192 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001193 if (tcon->ses->server->ops->validate_negotiate)
1194 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001195tcon_exit:
1196 free_rsp_buf(resp_buftype, rsp);
1197 kfree(unc_path);
1198 return rc;
1199
1200tcon_error_exit:
1201 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001202 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001203 }
1204 goto tcon_exit;
1205}
1206
1207int
1208SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1209{
1210 struct smb2_tree_disconnect_req *req; /* response is trivial */
1211 int rc = 0;
1212 struct TCP_Server_Info *server;
1213 struct cifs_ses *ses = tcon->ses;
1214
Joe Perchesf96637b2013-05-04 22:12:25 -05001215 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001216
1217 if (ses && (ses->server))
1218 server = ses->server;
1219 else
1220 return -EIO;
1221
1222 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1223 return 0;
1224
1225 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1226 if (rc)
1227 return rc;
1228
1229 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1230 if (rc)
1231 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1232
1233 return rc;
1234}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001235
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001236
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001237static struct create_durable *
1238create_durable_buf(void)
1239{
1240 struct create_durable *buf;
1241
1242 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1243 if (!buf)
1244 return NULL;
1245
1246 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001247 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001248 buf->ccontext.DataLength = cpu_to_le32(16);
1249 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1250 (struct create_durable, Name));
1251 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001252 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001253 buf->Name[0] = 'D';
1254 buf->Name[1] = 'H';
1255 buf->Name[2] = 'n';
1256 buf->Name[3] = 'Q';
1257 return buf;
1258}
1259
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001260static struct create_durable *
1261create_reconnect_durable_buf(struct cifs_fid *fid)
1262{
1263 struct create_durable *buf;
1264
1265 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1266 if (!buf)
1267 return NULL;
1268
1269 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1270 (struct create_durable, Data));
1271 buf->ccontext.DataLength = cpu_to_le32(16);
1272 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1273 (struct create_durable, Name));
1274 buf->ccontext.NameLength = cpu_to_le16(4);
1275 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1276 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001277 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001278 buf->Name[0] = 'D';
1279 buf->Name[1] = 'H';
1280 buf->Name[2] = 'n';
1281 buf->Name[3] = 'C';
1282 return buf;
1283}
1284
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001285static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001286parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1287 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001288{
1289 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001290 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001291 unsigned int next;
1292 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001293 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001294
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001295 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001296 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001297 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001298 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001299 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001300 if (le16_to_cpu(cc->NameLength) == 4 &&
1301 strncmp(name, "RqLs", 4) == 0)
1302 return server->ops->parse_lease_buf(cc, epoch);
1303
1304 next = le32_to_cpu(cc->Next);
1305 if (!next)
1306 break;
1307 remaining -= next;
1308 cc = (struct create_context *)((char *)cc + next);
1309 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001310
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001311 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001312}
1313
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001314static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001315add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1316 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001317{
1318 struct smb2_create_req *req = iov[0].iov_base;
1319 unsigned int num = *num_iovec;
1320
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001321 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001322 if (iov[num].iov_base == NULL)
1323 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001324 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001325 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1326 if (!req->CreateContextsOffset)
1327 req->CreateContextsOffset = cpu_to_le32(
1328 sizeof(struct smb2_create_req) - 4 +
1329 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001330 le32_add_cpu(&req->CreateContextsLength,
1331 server->vals->create_lease_size);
1332 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001333 *num_iovec = num + 1;
1334 return 0;
1335}
1336
Steve Frenchb56eae42015-11-03 09:26:27 -06001337static struct create_durable_v2 *
1338create_durable_v2_buf(struct cifs_fid *pfid)
1339{
1340 struct create_durable_v2 *buf;
1341
1342 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1343 if (!buf)
1344 return NULL;
1345
1346 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1347 (struct create_durable_v2, dcontext));
1348 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1349 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1350 (struct create_durable_v2, Name));
1351 buf->ccontext.NameLength = cpu_to_le16(4);
1352
1353 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1354 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001355 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001356 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1357
1358 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1359 buf->Name[0] = 'D';
1360 buf->Name[1] = 'H';
1361 buf->Name[2] = '2';
1362 buf->Name[3] = 'Q';
1363 return buf;
1364}
1365
1366static struct create_durable_handle_reconnect_v2 *
1367create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1368{
1369 struct create_durable_handle_reconnect_v2 *buf;
1370
1371 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1372 GFP_KERNEL);
1373 if (!buf)
1374 return NULL;
1375
1376 buf->ccontext.DataOffset =
1377 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1378 dcontext));
1379 buf->ccontext.DataLength =
1380 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1381 buf->ccontext.NameOffset =
1382 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1383 Name));
1384 buf->ccontext.NameLength = cpu_to_le16(4);
1385
1386 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1387 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1388 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1389 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1390
1391 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1392 buf->Name[0] = 'D';
1393 buf->Name[1] = 'H';
1394 buf->Name[2] = '2';
1395 buf->Name[3] = 'C';
1396 return buf;
1397}
1398
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001399static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001400add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001401 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001402{
1403 struct smb2_create_req *req = iov[0].iov_base;
1404 unsigned int num = *num_iovec;
1405
Steve Frenchb56eae42015-11-03 09:26:27 -06001406 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1407 if (iov[num].iov_base == NULL)
1408 return -ENOMEM;
1409 iov[num].iov_len = sizeof(struct create_durable_v2);
1410 if (!req->CreateContextsOffset)
1411 req->CreateContextsOffset =
1412 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1413 iov[1].iov_len);
1414 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1415 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1416 *num_iovec = num + 1;
1417 return 0;
1418}
1419
1420static int
1421add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1422 struct cifs_open_parms *oparms)
1423{
1424 struct smb2_create_req *req = iov[0].iov_base;
1425 unsigned int num = *num_iovec;
1426
1427 /* indicate that we don't need to relock the file */
1428 oparms->reconnect = false;
1429
1430 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1431 if (iov[num].iov_base == NULL)
1432 return -ENOMEM;
1433 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1434 if (!req->CreateContextsOffset)
1435 req->CreateContextsOffset =
1436 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1437 iov[1].iov_len);
1438 le32_add_cpu(&req->CreateContextsLength,
1439 sizeof(struct create_durable_handle_reconnect_v2));
1440 inc_rfc1001_len(&req->hdr,
1441 sizeof(struct create_durable_handle_reconnect_v2));
1442 *num_iovec = num + 1;
1443 return 0;
1444}
1445
1446static int
1447add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1448 struct cifs_open_parms *oparms, bool use_persistent)
1449{
1450 struct smb2_create_req *req = iov[0].iov_base;
1451 unsigned int num = *num_iovec;
1452
1453 if (use_persistent) {
1454 if (oparms->reconnect)
1455 return add_durable_reconnect_v2_context(iov, num_iovec,
1456 oparms);
1457 else
1458 return add_durable_v2_context(iov, num_iovec, oparms);
1459 }
1460
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001461 if (oparms->reconnect) {
1462 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1463 /* indicate that we don't need to relock the file */
1464 oparms->reconnect = false;
1465 } else
1466 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001467 if (iov[num].iov_base == NULL)
1468 return -ENOMEM;
1469 iov[num].iov_len = sizeof(struct create_durable);
1470 if (!req->CreateContextsOffset)
1471 req->CreateContextsOffset =
1472 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1473 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001474 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001475 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1476 *num_iovec = num + 1;
1477 return 0;
1478}
1479
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001480int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001481SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001482 __u8 *oplock, struct smb2_file_all_info *buf,
1483 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001484{
1485 struct smb2_create_req *req;
1486 struct smb2_create_rsp *rsp;
1487 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001488 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001489 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001490 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001491 int resp_buftype;
1492 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001493 __le16 *copy_path = NULL;
1494 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001495 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001496 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001497 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001498 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001499
Joe Perchesf96637b2013-05-04 22:12:25 -05001500 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001501
1502 if (ses && (ses->server))
1503 server = ses->server;
1504 else
1505 return -EIO;
1506
1507 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1508 if (rc)
1509 return rc;
1510
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001511 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001512 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001513 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1514 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001515
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001516 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001517 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001518 /* File attributes ignored on open (used in create though) */
1519 req->FileAttributes = cpu_to_le32(file_attributes);
1520 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001521 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1522 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001523 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001524 /* do not count rfc1001 len field */
1525 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001526
1527 iov[0].iov_base = (char *)req;
1528 /* 4 for rfc1002 length field */
1529 iov[0].iov_len = get_rfc1002_length(req) + 4;
1530
1531 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001532 req->NameLength = cpu_to_le16(uni_path_len - 2);
1533 /* -1 since last byte is buf[0] which is sent below (path) */
1534 iov[0].iov_len--;
1535 if (uni_path_len % 8 != 0) {
1536 copy_size = uni_path_len / 8 * 8;
1537 if (copy_size < uni_path_len)
1538 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001539
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001540 copy_path = kzalloc(copy_size, GFP_KERNEL);
1541 if (!copy_path)
1542 return -ENOMEM;
1543 memcpy((char *)copy_path, (const char *)path,
1544 uni_path_len);
1545 uni_path_len = copy_size;
1546 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001547 }
1548
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001549 iov[1].iov_len = uni_path_len;
1550 iov[1].iov_base = path;
1551 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1552 inc_rfc1001_len(req, uni_path_len - 1);
1553
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001554 if (!server->oplocks)
1555 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1556
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001557 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001558 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1559 req->RequestedOplockLevel = *oplock;
1560 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001561 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001562 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001563 cifs_small_buf_release(req);
1564 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001565 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001566 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001567 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001568 }
1569
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001570 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1571 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001572 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001573 struct create_context *ccontext =
1574 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001575 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001576 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001577 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001578
1579 rc = add_durable_context(iov, &num_iovecs, oparms,
1580 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001581 if (rc) {
1582 cifs_small_buf_release(req);
1583 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001584 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001585 return rc;
1586 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001587 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001588 }
1589
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001590 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1591 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1592
1593 if (rc != 0) {
1594 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001595 if (err_buf)
1596 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1597 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001598 goto creat_exit;
1599 }
1600
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001601 oparms->fid->persistent_fid = rsp->PersistentFileId;
1602 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001603
1604 if (buf) {
1605 memcpy(buf, &rsp->CreationTime, 32);
1606 buf->AllocationSize = rsp->AllocationSize;
1607 buf->EndOfFile = rsp->EndofFile;
1608 buf->Attributes = rsp->FileAttributes;
1609 buf->NumberOfLinks = cpu_to_le32(1);
1610 buf->DeletePending = 0;
1611 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001612
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001613 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001614 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001615 else
1616 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001617creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001618 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001619 kfree(lc_buf);
1620 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001621 free_rsp_buf(resp_buftype, rsp);
1622 return rc;
1623}
1624
Steve French4a72daf2013-06-25 00:20:49 -05001625/*
1626 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1627 */
1628int
1629SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1630 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1631 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1632{
1633 struct smb2_ioctl_req *req;
1634 struct smb2_ioctl_rsp *rsp;
1635 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001636 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001637 struct kvec iov[2];
1638 int resp_buftype;
1639 int num_iovecs;
1640 int rc = 0;
1641
1642 cifs_dbg(FYI, "SMB2 IOCTL\n");
1643
Steve French3d1a3742014-08-11 21:05:25 -05001644 if (out_data != NULL)
1645 *out_data = NULL;
1646
Steve French4a72daf2013-06-25 00:20:49 -05001647 /* zero out returned data len, in case of error */
1648 if (plen)
1649 *plen = 0;
1650
Steve French8e353102015-03-26 19:47:02 -05001651 if (tcon)
1652 ses = tcon->ses;
1653 else
1654 return -EIO;
1655
Steve French4a72daf2013-06-25 00:20:49 -05001656 if (ses && (ses->server))
1657 server = ses->server;
1658 else
1659 return -EIO;
1660
1661 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1662 if (rc)
1663 return rc;
1664
1665 req->CtlCode = cpu_to_le32(opcode);
1666 req->PersistentFileId = persistent_fid;
1667 req->VolatileFileId = volatile_fid;
1668
1669 if (indatalen) {
1670 req->InputCount = cpu_to_le32(indatalen);
1671 /* do not set InputOffset if no input data */
1672 req->InputOffset =
1673 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1674 iov[1].iov_base = in_data;
1675 iov[1].iov_len = indatalen;
1676 num_iovecs = 2;
1677 } else
1678 num_iovecs = 1;
1679
1680 req->OutputOffset = 0;
1681 req->OutputCount = 0; /* MBZ */
1682
1683 /*
1684 * Could increase MaxOutputResponse, but that would require more
1685 * than one credit. Windows typically sets this smaller, but for some
1686 * ioctls it may be useful to allow server to send more. No point
1687 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001688 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1689 * (by default, note that it can be overridden to make max larger)
1690 * in responses (except for read responses which can be bigger.
1691 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001692 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001693 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001694
1695 if (is_fsctl)
1696 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1697 else
1698 req->Flags = 0;
1699
1700 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001701
Steve French7ff8d452013-10-14 00:44:19 -05001702 /*
1703 * If no input data, the size of ioctl struct in
1704 * protocol spec still includes a 1 byte data buffer,
1705 * but if input data passed to ioctl, we do not
1706 * want to double count this, so we do not send
1707 * the dummy one byte of data in iovec[0] if sending
1708 * input data (in iovec[1]). We also must add 4 bytes
1709 * in first iovec to allow for rfc1002 length field.
1710 */
1711
1712 if (indatalen) {
1713 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1714 inc_rfc1001_len(req, indatalen - 1);
1715 } else
1716 iov[0].iov_len = get_rfc1002_length(req) + 4;
1717
Steve Frenchfca16f92017-10-25 15:58:31 -05001718 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1719 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1720 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001721
1722 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1723 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1724
Steve French9bf0c9c2013-11-16 18:05:28 -06001725 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001726 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001727 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001728 } else if (rc == -EINVAL) {
1729 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1730 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001731 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001732 goto ioctl_exit;
1733 }
Steve French4a72daf2013-06-25 00:20:49 -05001734 }
1735
1736 /* check if caller wants to look at return data or just return rc */
1737 if ((plen == NULL) || (out_data == NULL))
1738 goto ioctl_exit;
1739
1740 *plen = le32_to_cpu(rsp->OutputCount);
1741
1742 /* We check for obvious errors in the output buffer length and offset */
1743 if (*plen == 0)
1744 goto ioctl_exit; /* server returned no data */
1745 else if (*plen > 0xFF00) {
1746 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1747 *plen = 0;
1748 rc = -EIO;
1749 goto ioctl_exit;
1750 }
1751
1752 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1753 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1754 le32_to_cpu(rsp->OutputOffset));
1755 *plen = 0;
1756 rc = -EIO;
1757 goto ioctl_exit;
1758 }
1759
1760 *out_data = kmalloc(*plen, GFP_KERNEL);
1761 if (*out_data == NULL) {
1762 rc = -ENOMEM;
1763 goto ioctl_exit;
1764 }
1765
Steve French373512e2015-12-18 13:05:30 -06001766 memcpy(*out_data,
1767 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001768 *plen);
1769ioctl_exit:
1770 free_rsp_buf(resp_buftype, rsp);
1771 return rc;
1772}
1773
Steve French64a5cfa2013-10-14 15:31:32 -05001774/*
1775 * Individual callers to ioctl worker function follow
1776 */
1777
1778int
1779SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1780 u64 persistent_fid, u64 volatile_fid)
1781{
1782 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001783 struct compress_ioctl fsctl_input;
1784 char *ret_data = NULL;
1785
1786 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001787 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001788
1789 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1790 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1791 (char *)&fsctl_input /* data input */,
1792 2 /* in data len */, &ret_data /* out data */, NULL);
1793
1794 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001795
1796 return rc;
1797}
1798
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001799int
1800SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1801 u64 persistent_fid, u64 volatile_fid)
1802{
1803 struct smb2_close_req *req;
1804 struct smb2_close_rsp *rsp;
1805 struct TCP_Server_Info *server;
1806 struct cifs_ses *ses = tcon->ses;
1807 struct kvec iov[1];
1808 int resp_buftype;
1809 int rc = 0;
1810
Joe Perchesf96637b2013-05-04 22:12:25 -05001811 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001812
1813 if (ses && (ses->server))
1814 server = ses->server;
1815 else
1816 return -EIO;
1817
1818 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1819 if (rc)
1820 return rc;
1821
1822 req->PersistentFileId = persistent_fid;
1823 req->VolatileFileId = volatile_fid;
1824
1825 iov[0].iov_base = (char *)req;
1826 /* 4 for rfc1002 length field */
1827 iov[0].iov_len = get_rfc1002_length(req) + 4;
1828
1829 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1830 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1831
1832 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001833 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001834 goto close_exit;
1835 }
1836
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001837 /* BB FIXME - decode close response, update inode for caching */
1838
1839close_exit:
1840 free_rsp_buf(resp_buftype, rsp);
1841 return rc;
1842}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001843
1844static int
1845validate_buf(unsigned int offset, unsigned int buffer_length,
1846 struct smb2_hdr *hdr, unsigned int min_buf_size)
1847
1848{
1849 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1850 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1851 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1852 char *end_of_buf = begin_of_buf + buffer_length;
1853
1854
1855 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001856 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1857 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001858 return -EINVAL;
1859 }
1860
1861 /* check if beyond RFC1001 maximum length */
1862 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001863 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1864 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001865 return -EINVAL;
1866 }
1867
1868 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001869 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001870 return -EINVAL;
1871 }
1872
1873 return 0;
1874}
1875
1876/*
1877 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1878 * Caller must free buffer.
1879 */
1880static int
1881validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1882 struct smb2_hdr *hdr, unsigned int minbufsize,
1883 char *data)
1884
1885{
1886 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1887 int rc;
1888
1889 if (!data)
1890 return -EINVAL;
1891
1892 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1893 if (rc)
1894 return rc;
1895
1896 memcpy(data, begin_of_buf, buffer_length);
1897
1898 return 0;
1899}
1900
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001901static int
1902query_info(const unsigned int xid, struct cifs_tcon *tcon,
1903 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1904 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001905{
1906 struct smb2_query_info_req *req;
1907 struct smb2_query_info_rsp *rsp = NULL;
1908 struct kvec iov[2];
1909 int rc = 0;
1910 int resp_buftype;
1911 struct TCP_Server_Info *server;
1912 struct cifs_ses *ses = tcon->ses;
1913
Joe Perchesf96637b2013-05-04 22:12:25 -05001914 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001915
1916 if (ses && (ses->server))
1917 server = ses->server;
1918 else
1919 return -EIO;
1920
1921 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1922 if (rc)
1923 return rc;
1924
1925 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001926 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001927 req->PersistentFileId = persistent_fid;
1928 req->VolatileFileId = volatile_fid;
1929 /* 4 for rfc1002 length field and 1 for Buffer */
1930 req->InputBufferOffset =
1931 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001932 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001933
1934 iov[0].iov_base = (char *)req;
1935 /* 4 for rfc1002 length field */
1936 iov[0].iov_len = get_rfc1002_length(req) + 4;
1937
1938 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001939 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1940
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001941 if (rc) {
1942 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1943 goto qinf_exit;
1944 }
1945
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001946 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1947 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001948 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001949
1950qinf_exit:
1951 free_rsp_buf(resp_buftype, rsp);
1952 return rc;
1953}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001954
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001955int
1956SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1957 u64 persistent_fid, u64 volatile_fid,
1958 struct smb2_file_all_info *data)
1959{
1960 return query_info(xid, tcon, persistent_fid, volatile_fid,
1961 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001962 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001963 sizeof(struct smb2_file_all_info), data);
1964}
1965
1966int
1967SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1968 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1969{
1970 return query_info(xid, tcon, persistent_fid, volatile_fid,
1971 FILE_INTERNAL_INFORMATION,
1972 sizeof(struct smb2_file_internal_info),
1973 sizeof(struct smb2_file_internal_info), uniqueid);
1974}
1975
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001976/*
1977 * This is a no-op for now. We're not really interested in the reply, but
1978 * rather in the fact that the server sent one and that server->lstrp
1979 * gets updated.
1980 *
1981 * FIXME: maybe we should consider checking that the reply matches request?
1982 */
1983static void
1984smb2_echo_callback(struct mid_q_entry *mid)
1985{
1986 struct TCP_Server_Info *server = mid->callback_data;
1987 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1988 unsigned int credits_received = 1;
1989
1990 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1991 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1992
Christopher Oo5fb4e282015-06-25 16:10:48 -07001993 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001994 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001995 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001996 add_credits(server, credits_received, CIFS_ECHO_OP);
1997}
1998
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001999void smb2_reconnect_server(struct work_struct *work)
2000{
2001 struct TCP_Server_Info *server = container_of(work,
2002 struct TCP_Server_Info, reconnect.work);
2003 struct cifs_ses *ses;
2004 struct cifs_tcon *tcon, *tcon2;
2005 struct list_head tmp_list;
2006 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002007 int rc;
2008 int resched = false;
2009
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002010
2011 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2012 mutex_lock(&server->reconnect_mutex);
2013
2014 INIT_LIST_HEAD(&tmp_list);
2015 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2016
2017 spin_lock(&cifs_tcp_ses_lock);
2018 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2019 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002020 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002021 tcon->tc_count++;
2022 list_add_tail(&tcon->rlist, &tmp_list);
2023 tcon_exist = true;
2024 }
2025 }
2026 }
2027 /*
2028 * Get the reference to server struct to be sure that the last call of
2029 * cifs_put_tcon() in the loop below won't release the server pointer.
2030 */
2031 if (tcon_exist)
2032 server->srv_count++;
2033
2034 spin_unlock(&cifs_tcp_ses_lock);
2035
2036 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002037 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2038 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002039 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002040 else
2041 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002042 list_del_init(&tcon->rlist);
2043 cifs_put_tcon(tcon);
2044 }
2045
2046 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002047 if (resched)
2048 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002049 mutex_unlock(&server->reconnect_mutex);
2050
2051 /* now we can safely release srv struct */
2052 if (tcon_exist)
2053 cifs_put_tcp_session(server, 1);
2054}
2055
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002056int
2057SMB2_echo(struct TCP_Server_Info *server)
2058{
2059 struct smb2_echo_req *req;
2060 int rc = 0;
2061 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002062 struct smb_rqst rqst = { .rq_iov = &iov,
2063 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002064
Joe Perchesf96637b2013-05-04 22:12:25 -05002065 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002066
Steve French4fcd1812016-06-22 20:12:05 -05002067 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002068 /* No need to send echo on newly established connections */
2069 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2070 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002071 }
2072
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002073 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2074 if (rc)
2075 return rc;
2076
2077 req->hdr.CreditRequest = cpu_to_le16(1);
2078
2079 iov.iov_base = (char *)req;
2080 /* 4 for rfc1002 length field */
2081 iov.iov_len = get_rfc1002_length(req) + 4;
2082
Jeff Laytonfec344e2012-09-18 16:20:35 -07002083 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002084 CIFS_ECHO_OP);
2085 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002086 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002087
2088 cifs_small_buf_release(req);
2089 return rc;
2090}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002091
2092int
2093SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2094 u64 volatile_fid)
2095{
2096 struct smb2_flush_req *req;
2097 struct TCP_Server_Info *server;
2098 struct cifs_ses *ses = tcon->ses;
2099 struct kvec iov[1];
2100 int resp_buftype;
2101 int rc = 0;
2102
Joe Perchesf96637b2013-05-04 22:12:25 -05002103 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002104
2105 if (ses && (ses->server))
2106 server = ses->server;
2107 else
2108 return -EIO;
2109
2110 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2111 if (rc)
2112 return rc;
2113
2114 req->PersistentFileId = persistent_fid;
2115 req->VolatileFileId = volatile_fid;
2116
2117 iov[0].iov_base = (char *)req;
2118 /* 4 for rfc1002 length field */
2119 iov[0].iov_len = get_rfc1002_length(req) + 4;
2120
2121 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2122
Steve Frenchdfebe402015-03-27 01:00:06 -05002123 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002124 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2125
2126 free_rsp_buf(resp_buftype, iov[0].iov_base);
2127 return rc;
2128}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002129
2130/*
2131 * To form a chain of read requests, any read requests after the first should
2132 * have the end_of_chain boolean set to true.
2133 */
2134static int
2135smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2136 unsigned int remaining_bytes, int request_type)
2137{
2138 int rc = -EACCES;
2139 struct smb2_read_req *req = NULL;
2140
2141 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2142 if (rc)
2143 return rc;
2144 if (io_parms->tcon->ses->server == NULL)
2145 return -ECONNABORTED;
2146
2147 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2148
2149 req->PersistentFileId = io_parms->persistent_fid;
2150 req->VolatileFileId = io_parms->volatile_fid;
2151 req->ReadChannelInfoOffset = 0; /* reserved */
2152 req->ReadChannelInfoLength = 0; /* reserved */
2153 req->Channel = 0; /* reserved */
2154 req->MinimumCount = 0;
2155 req->Length = cpu_to_le32(io_parms->length);
2156 req->Offset = cpu_to_le64(io_parms->offset);
2157
2158 if (request_type & CHAINED_REQUEST) {
2159 if (!(request_type & END_OF_CHAIN)) {
2160 /* 4 for rfc1002 length field */
2161 req->hdr.NextCommand =
2162 cpu_to_le32(get_rfc1002_length(req) + 4);
2163 } else /* END_OF_CHAIN */
2164 req->hdr.NextCommand = 0;
2165 if (request_type & RELATED_REQUEST) {
2166 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2167 /*
2168 * Related requests use info from previous read request
2169 * in chain.
2170 */
2171 req->hdr.SessionId = 0xFFFFFFFF;
2172 req->hdr.TreeId = 0xFFFFFFFF;
2173 req->PersistentFileId = 0xFFFFFFFF;
2174 req->VolatileFileId = 0xFFFFFFFF;
2175 }
2176 }
2177 if (remaining_bytes > io_parms->length)
2178 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2179 else
2180 req->RemainingBytes = 0;
2181
2182 iov[0].iov_base = (char *)req;
2183 /* 4 for rfc1002 length field */
2184 iov[0].iov_len = get_rfc1002_length(req) + 4;
2185 return rc;
2186}
2187
2188static void
2189smb2_readv_callback(struct mid_q_entry *mid)
2190{
2191 struct cifs_readdata *rdata = mid->callback_data;
2192 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2193 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002194 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002195 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002196 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002197 .rq_nvec = 1,
2198 .rq_pages = rdata->pages,
2199 .rq_npages = rdata->nr_pages,
2200 .rq_pagesz = rdata->pagesz,
2201 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002202
Joe Perchesf96637b2013-05-04 22:12:25 -05002203 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2204 __func__, mid->mid, mid->mid_state, rdata->result,
2205 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002206
2207 switch (mid->mid_state) {
2208 case MID_RESPONSE_RECEIVED:
2209 credits_received = le16_to_cpu(buf->CreditRequest);
2210 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002211 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002212 int rc;
2213
Jeff Layton0b688cf2012-09-18 16:20:34 -07002214 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002215 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002216 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2217 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002218 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002219 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002220 task_io_account_read(rdata->got_bytes);
2221 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002222 break;
2223 case MID_REQUEST_SUBMITTED:
2224 case MID_RETRY_NEEDED:
2225 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002226 if (server->sign && rdata->got_bytes)
2227 /* reset bytes number since we can not check a sign */
2228 rdata->got_bytes = 0;
2229 /* FIXME: should this be counted toward the initiating task? */
2230 task_io_account_read(rdata->got_bytes);
2231 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002232 break;
2233 default:
2234 if (rdata->result != -ENODATA)
2235 rdata->result = -EIO;
2236 }
2237
2238 if (rdata->result)
2239 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2240
2241 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002242 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002243 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002244 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002245 add_credits(server, credits_received, 0);
2246}
2247
2248/* smb2_async_readv - send an async write, and set up mid to handle result */
2249int
2250smb2_async_readv(struct cifs_readdata *rdata)
2251{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002252 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002253 struct smb2_hdr *buf;
2254 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002255 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002256 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002257 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002258
Joe Perchesf96637b2013-05-04 22:12:25 -05002259 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2260 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002261
2262 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2263 io_parms.offset = rdata->offset;
2264 io_parms.length = rdata->bytes;
2265 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2266 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2267 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002268
2269 server = io_parms.tcon->ses->server;
2270
Jeff Layton58195752012-09-19 06:22:34 -07002271 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002272 if (rc) {
2273 if (rc == -EAGAIN && rdata->credits) {
2274 /* credits was reset by reconnect */
2275 rdata->credits = 0;
2276 /* reduce in_flight value since we won't send the req */
2277 spin_lock(&server->req_lock);
2278 server->in_flight--;
2279 spin_unlock(&server->req_lock);
2280 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002281 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002282 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002283
Jeff Layton58195752012-09-19 06:22:34 -07002284 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002285 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002286 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002287
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002288 if (rdata->credits) {
2289 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2290 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002291 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002292 spin_lock(&server->req_lock);
2293 server->credits += rdata->credits -
2294 le16_to_cpu(buf->CreditCharge);
2295 spin_unlock(&server->req_lock);
2296 wake_up(&server->request_q);
2297 flags = CIFS_HAS_CREDITS;
2298 }
2299
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002300 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002301 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002302 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002303 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002304 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002305 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002306 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2307 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002308
2309 cifs_small_buf_release(buf);
2310 return rc;
2311}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002312
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002313int
2314SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2315 unsigned int *nbytes, char **buf, int *buf_type)
2316{
2317 int resp_buftype, rc = -EACCES;
2318 struct smb2_read_rsp *rsp = NULL;
2319 struct kvec iov[1];
2320
2321 *nbytes = 0;
2322 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2323 if (rc)
2324 return rc;
2325
2326 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2327 &resp_buftype, CIFS_LOG_ERROR);
2328
2329 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2330
2331 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2332 free_rsp_buf(resp_buftype, iov[0].iov_base);
2333 return 0;
2334 }
2335
2336 if (rc) {
2337 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002338 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002339 } else {
2340 *nbytes = le32_to_cpu(rsp->DataLength);
2341 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2342 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002343 cifs_dbg(FYI, "bad length %d for count %d\n",
2344 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002345 rc = -EIO;
2346 *nbytes = 0;
2347 }
2348 }
2349
2350 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002351 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002352 *nbytes);
2353 free_rsp_buf(resp_buftype, iov[0].iov_base);
2354 } else if (resp_buftype != CIFS_NO_BUFFER) {
2355 *buf = iov[0].iov_base;
2356 if (resp_buftype == CIFS_SMALL_BUFFER)
2357 *buf_type = CIFS_SMALL_BUFFER;
2358 else if (resp_buftype == CIFS_LARGE_BUFFER)
2359 *buf_type = CIFS_LARGE_BUFFER;
2360 }
2361 return rc;
2362}
2363
Pavel Shilovsky33319142012-09-18 16:20:29 -07002364/*
2365 * Check the mid_state and signature on received buffer (if any), and queue the
2366 * workqueue completion task.
2367 */
2368static void
2369smb2_writev_callback(struct mid_q_entry *mid)
2370{
2371 struct cifs_writedata *wdata = mid->callback_data;
2372 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002373 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002374 unsigned int written;
2375 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2376 unsigned int credits_received = 1;
2377
2378 switch (mid->mid_state) {
2379 case MID_RESPONSE_RECEIVED:
2380 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2381 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2382 if (wdata->result != 0)
2383 break;
2384
2385 written = le32_to_cpu(rsp->DataLength);
2386 /*
2387 * Mask off high 16 bits when bytes written as returned
2388 * by the server is greater than bytes requested by the
2389 * client. OS/2 servers are known to set incorrect
2390 * CountHigh values.
2391 */
2392 if (written > wdata->bytes)
2393 written &= 0xFFFF;
2394
2395 if (written < wdata->bytes)
2396 wdata->result = -ENOSPC;
2397 else
2398 wdata->bytes = written;
2399 break;
2400 case MID_REQUEST_SUBMITTED:
2401 case MID_RETRY_NEEDED:
2402 wdata->result = -EAGAIN;
2403 break;
2404 default:
2405 wdata->result = -EIO;
2406 break;
2407 }
2408
2409 if (wdata->result)
2410 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2411
2412 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002413 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002414 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002415 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002416 add_credits(tcon->ses->server, credits_received, 0);
2417}
2418
2419/* smb2_async_writev - send an async write, and set up mid to handle result */
2420int
Steve French4a5c80d2014-02-07 20:45:12 -06002421smb2_async_writev(struct cifs_writedata *wdata,
2422 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002423{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002424 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002425 struct smb2_write_req *req = NULL;
2426 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002427 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002428 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002429 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002430
2431 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002432 if (rc) {
2433 if (rc == -EAGAIN && wdata->credits) {
2434 /* credits was reset by reconnect */
2435 wdata->credits = 0;
2436 /* reduce in_flight value since we won't send the req */
2437 spin_lock(&server->req_lock);
2438 server->in_flight--;
2439 spin_unlock(&server->req_lock);
2440 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002441 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002442 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002443
Pavel Shilovsky33319142012-09-18 16:20:29 -07002444 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2445
2446 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2447 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2448 req->WriteChannelInfoOffset = 0;
2449 req->WriteChannelInfoLength = 0;
2450 req->Channel = 0;
2451 req->Offset = cpu_to_le64(wdata->offset);
2452 /* 4 for rfc1002 length field */
2453 req->DataOffset = cpu_to_le16(
2454 offsetof(struct smb2_write_req, Buffer) - 4);
2455 req->RemainingBytes = 0;
2456
2457 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002458 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2459 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002460
Jeff Laytoneddb0792012-09-18 16:20:35 -07002461 rqst.rq_iov = &iov;
2462 rqst.rq_nvec = 1;
2463 rqst.rq_pages = wdata->pages;
2464 rqst.rq_npages = wdata->nr_pages;
2465 rqst.rq_pagesz = wdata->pagesz;
2466 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002467
Joe Perchesf96637b2013-05-04 22:12:25 -05002468 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2469 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002470
2471 req->Length = cpu_to_le32(wdata->bytes);
2472
2473 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2474
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002475 if (wdata->credits) {
2476 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2477 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002478 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002479 spin_lock(&server->req_lock);
2480 server->credits += wdata->credits -
2481 le16_to_cpu(req->hdr.CreditCharge);
2482 spin_unlock(&server->req_lock);
2483 wake_up(&server->request_q);
2484 flags = CIFS_HAS_CREDITS;
2485 }
2486
Pavel Shilovsky33319142012-09-18 16:20:29 -07002487 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002488 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2489 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002490
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002491 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002492 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002493 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2494 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002495
Pavel Shilovsky33319142012-09-18 16:20:29 -07002496async_writev_out:
2497 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002498 return rc;
2499}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002500
2501/*
2502 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2503 * The length field from io_parms must be at least 1 and indicates a number of
2504 * elements with data to write that begins with position 1 in iov array. All
2505 * data length is specified by count.
2506 */
2507int
2508SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2509 unsigned int *nbytes, struct kvec *iov, int n_vec)
2510{
2511 int rc = 0;
2512 struct smb2_write_req *req = NULL;
2513 struct smb2_write_rsp *rsp = NULL;
2514 int resp_buftype;
2515 *nbytes = 0;
2516
2517 if (n_vec < 1)
2518 return rc;
2519
2520 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2521 if (rc)
2522 return rc;
2523
2524 if (io_parms->tcon->ses->server == NULL)
2525 return -ECONNABORTED;
2526
2527 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2528
2529 req->PersistentFileId = io_parms->persistent_fid;
2530 req->VolatileFileId = io_parms->volatile_fid;
2531 req->WriteChannelInfoOffset = 0;
2532 req->WriteChannelInfoLength = 0;
2533 req->Channel = 0;
2534 req->Length = cpu_to_le32(io_parms->length);
2535 req->Offset = cpu_to_le64(io_parms->offset);
2536 /* 4 for rfc1002 length field */
2537 req->DataOffset = cpu_to_le16(
2538 offsetof(struct smb2_write_req, Buffer) - 4);
2539 req->RemainingBytes = 0;
2540
2541 iov[0].iov_base = (char *)req;
2542 /* 4 for rfc1002 length field and 1 for Buffer */
2543 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2544
2545 /* length of entire message including data to be written */
2546 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2547
2548 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2549 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002550 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002551
2552 if (rc) {
2553 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002554 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002555 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002556 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002557
2558 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002559 return rc;
2560}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002561
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002562static unsigned int
2563num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2564{
2565 int len;
2566 unsigned int entrycount = 0;
2567 unsigned int next_offset = 0;
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002568 char *entryptr;
2569 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002570
2571 if (bufstart == NULL)
2572 return 0;
2573
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002574 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002575
2576 while (1) {
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002577 if (entryptr + next_offset < entryptr ||
2578 entryptr + next_offset > end_of_buf ||
2579 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002580 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002581 break;
2582 }
2583
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002584 entryptr = entryptr + next_offset;
2585 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
2586
2587 len = le32_to_cpu(dir_info->FileNameLength);
2588 if (entryptr + len < entryptr ||
2589 entryptr + len > end_of_buf ||
2590 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002591 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2592 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002593 break;
2594 }
2595
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002596 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002597 entrycount++;
2598
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002599 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002600 if (!next_offset)
2601 break;
2602 }
2603
2604 return entrycount;
2605}
2606
2607/*
2608 * Readdir/FindFirst
2609 */
2610int
2611SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2612 u64 persistent_fid, u64 volatile_fid, int index,
2613 struct cifs_search_info *srch_inf)
2614{
2615 struct smb2_query_directory_req *req;
2616 struct smb2_query_directory_rsp *rsp = NULL;
2617 struct kvec iov[2];
2618 int rc = 0;
2619 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002620 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002621 unsigned char *bufptr;
2622 struct TCP_Server_Info *server;
2623 struct cifs_ses *ses = tcon->ses;
2624 __le16 asteriks = cpu_to_le16('*');
2625 char *end_of_smb;
2626 unsigned int output_size = CIFSMaxBufSize;
2627 size_t info_buf_size;
2628
2629 if (ses && (ses->server))
2630 server = ses->server;
2631 else
2632 return -EIO;
2633
2634 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2635 if (rc)
2636 return rc;
2637
2638 switch (srch_inf->info_level) {
2639 case SMB_FIND_FILE_DIRECTORY_INFO:
2640 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2641 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2642 break;
2643 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2644 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2645 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2646 break;
2647 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002648 cifs_dbg(VFS, "info level %u isn't supported\n",
2649 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002650 rc = -EINVAL;
2651 goto qdir_exit;
2652 }
2653
2654 req->FileIndex = cpu_to_le32(index);
2655 req->PersistentFileId = persistent_fid;
2656 req->VolatileFileId = volatile_fid;
2657
2658 len = 0x2;
2659 bufptr = req->Buffer;
2660 memcpy(bufptr, &asteriks, len);
2661
2662 req->FileNameOffset =
2663 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2664 req->FileNameLength = cpu_to_le16(len);
2665 /*
2666 * BB could be 30 bytes or so longer if we used SMB2 specific
2667 * buffer lengths, but this is safe and close enough.
2668 */
2669 output_size = min_t(unsigned int, output_size, server->maxBuf);
2670 output_size = min_t(unsigned int, output_size, 2 << 15);
2671 req->OutputBufferLength = cpu_to_le32(output_size);
2672
2673 iov[0].iov_base = (char *)req;
2674 /* 4 for RFC1001 length and 1 for Buffer */
2675 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2676
2677 iov[1].iov_base = (char *)(req->Buffer);
2678 iov[1].iov_len = len;
2679
2680 inc_rfc1001_len(req, len - 1 /* Buffer */);
2681
2682 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002683 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2684
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002685 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002686 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2687 srch_inf->endOfSearch = true;
2688 rc = 0;
Pavel Shilovsky9033b4f2019-01-26 12:21:32 -08002689 } else
2690 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002691 goto qdir_exit;
2692 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002693
2694 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2695 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2696 info_buf_size);
2697 if (rc)
2698 goto qdir_exit;
2699
2700 srch_inf->unicode = true;
2701
2702 if (srch_inf->ntwrk_buf_start) {
2703 if (srch_inf->smallBuf)
2704 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2705 else
2706 cifs_buf_release(srch_inf->ntwrk_buf_start);
2707 }
2708 srch_inf->ntwrk_buf_start = (char *)rsp;
2709 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2710 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2711 /* 4 for rfc1002 length field */
2712 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2713 srch_inf->entries_in_buffer =
2714 num_entries(srch_inf->srch_entries_start, end_of_smb,
2715 &srch_inf->last_entry, info_buf_size);
2716 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002717 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2718 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2719 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002720 if (resp_buftype == CIFS_LARGE_BUFFER)
2721 srch_inf->smallBuf = false;
2722 else if (resp_buftype == CIFS_SMALL_BUFFER)
2723 srch_inf->smallBuf = true;
2724 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002725 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002726
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002727 return rc;
2728
2729qdir_exit:
2730 free_rsp_buf(resp_buftype, rsp);
2731 return rc;
2732}
2733
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002734static int
2735send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002736 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002737 unsigned int num, void **data, unsigned int *size)
2738{
2739 struct smb2_set_info_req *req;
2740 struct smb2_set_info_rsp *rsp = NULL;
2741 struct kvec *iov;
2742 int rc = 0;
2743 int resp_buftype;
2744 unsigned int i;
2745 struct TCP_Server_Info *server;
2746 struct cifs_ses *ses = tcon->ses;
2747
2748 if (ses && (ses->server))
2749 server = ses->server;
2750 else
2751 return -EIO;
2752
2753 if (!num)
2754 return -EINVAL;
2755
2756 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2757 if (!iov)
2758 return -ENOMEM;
2759
2760 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2761 if (rc) {
2762 kfree(iov);
2763 return rc;
2764 }
2765
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002766 req->hdr.ProcessId = cpu_to_le32(pid);
2767
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002768 req->InfoType = SMB2_O_INFO_FILE;
2769 req->FileInfoClass = info_class;
2770 req->PersistentFileId = persistent_fid;
2771 req->VolatileFileId = volatile_fid;
2772
2773 /* 4 for RFC1001 length and 1 for Buffer */
2774 req->BufferOffset =
2775 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2776 req->BufferLength = cpu_to_le32(*size);
2777
2778 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2779
2780 memcpy(req->Buffer, *data, *size);
2781
2782 iov[0].iov_base = (char *)req;
2783 /* 4 for RFC1001 length */
2784 iov[0].iov_len = get_rfc1002_length(req) + 4;
2785
2786 for (i = 1; i < num; i++) {
2787 inc_rfc1001_len(req, size[i]);
2788 le32_add_cpu(&req->BufferLength, size[i]);
2789 iov[i].iov_base = (char *)data[i];
2790 iov[i].iov_len = size[i];
2791 }
2792
2793 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2794 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2795
Steve French7d3fb242013-11-18 09:56:28 -06002796 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002797 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002798
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002799 free_rsp_buf(resp_buftype, rsp);
2800 kfree(iov);
2801 return rc;
2802}
2803
2804int
2805SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2806 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2807{
2808 struct smb2_file_rename_info info;
2809 void **data;
2810 unsigned int size[2];
2811 int rc;
2812 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2813
2814 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2815 if (!data)
2816 return -ENOMEM;
2817
2818 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2819 /* 0 = fail if target already exists */
2820 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2821 info.FileNameLength = cpu_to_le32(len);
2822
2823 data[0] = &info;
2824 size[0] = sizeof(struct smb2_file_rename_info);
2825
2826 data[1] = target_file;
2827 size[1] = len + 2 /* null */;
2828
2829 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002830 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2831 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002832 kfree(data);
2833 return rc;
2834}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002835
2836int
Steve French897fba12016-05-12 21:20:36 -05002837SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2838 u64 persistent_fid, u64 volatile_fid)
2839{
2840 __u8 delete_pending = 1;
2841 void *data;
2842 unsigned int size;
2843
2844 data = &delete_pending;
2845 size = 1; /* sizeof __u8 */
2846
2847 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2848 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2849 &size);
2850}
2851
2852int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002853SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2854 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2855{
2856 struct smb2_file_link_info info;
2857 void **data;
2858 unsigned int size[2];
2859 int rc;
2860 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2861
2862 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2863 if (!data)
2864 return -ENOMEM;
2865
2866 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2867 /* 0 = fail if link already exists */
2868 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2869 info.FileNameLength = cpu_to_le32(len);
2870
2871 data[0] = &info;
2872 size[0] = sizeof(struct smb2_file_link_info);
2873
2874 data[1] = target_file;
2875 size[1] = len + 2 /* null */;
2876
2877 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002878 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002879 kfree(data);
2880 return rc;
2881}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002882
2883int
2884SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002885 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002886{
2887 struct smb2_file_eof_info info;
2888 void *data;
2889 unsigned int size;
2890
2891 info.EndOfFile = *eof;
2892
2893 data = &info;
2894 size = sizeof(struct smb2_file_eof_info);
2895
Steve Frenchf29ebb42014-07-19 21:44:58 -05002896 if (is_falloc)
2897 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2898 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2899 else
2900 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2901 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002902}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002903
2904int
2905SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2906 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2907{
2908 unsigned int size;
2909 size = sizeof(FILE_BASIC_INFO);
2910 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2911 current->tgid, FILE_BASIC_INFORMATION, 1,
2912 (void **)&buf, &size);
2913}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002914
2915int
2916SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2917 const u64 persistent_fid, const u64 volatile_fid,
2918 __u8 oplock_level)
2919{
2920 int rc;
2921 struct smb2_oplock_break *req = NULL;
2922
Joe Perchesf96637b2013-05-04 22:12:25 -05002923 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002924 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2925
2926 if (rc)
2927 return rc;
2928
2929 req->VolatileFid = volatile_fid;
2930 req->PersistentFid = persistent_fid;
2931 req->OplockLevel = oplock_level;
2932 req->hdr.CreditRequest = cpu_to_le16(1);
2933
2934 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2935 /* SMB2 buffer freed by function above */
2936
2937 if (rc) {
2938 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002940 }
2941
2942 return rc;
2943}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002944
2945static void
2946copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2947 struct kstatfs *kst)
2948{
2949 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2950 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2951 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302952 kst->f_bfree = kst->f_bavail =
2953 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002954 return;
2955}
2956
2957static int
2958build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2959 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2960{
2961 int rc;
2962 struct smb2_query_info_req *req;
2963
Joe Perchesf96637b2013-05-04 22:12:25 -05002964 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002965
2966 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2967 return -EIO;
2968
2969 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2970 if (rc)
2971 return rc;
2972
2973 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2974 req->FileInfoClass = level;
2975 req->PersistentFileId = persistent_fid;
2976 req->VolatileFileId = volatile_fid;
2977 /* 4 for rfc1002 length field and 1 for pad */
2978 req->InputBufferOffset =
2979 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2980 req->OutputBufferLength = cpu_to_le32(
2981 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2982
2983 iov->iov_base = (char *)req;
2984 /* 4 for rfc1002 length field */
2985 iov->iov_len = get_rfc1002_length(req) + 4;
2986 return 0;
2987}
2988
2989int
2990SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2991 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2992{
2993 struct smb2_query_info_rsp *rsp = NULL;
2994 struct kvec iov;
2995 int rc = 0;
2996 int resp_buftype;
2997 struct cifs_ses *ses = tcon->ses;
2998 struct smb2_fs_full_size_info *info = NULL;
2999
3000 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3001 sizeof(struct smb2_fs_full_size_info),
3002 persistent_fid, volatile_fid);
3003 if (rc)
3004 return rc;
3005
3006 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3007 if (rc) {
3008 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003009 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003010 }
3011 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3012
3013 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3014 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3015 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3016 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3017 sizeof(struct smb2_fs_full_size_info));
3018 if (!rc)
3019 copy_fs_info_to_kstatfs(info, fsdata);
3020
Steve French34f62642013-10-09 02:07:00 -05003021qfsinf_exit:
3022 free_rsp_buf(resp_buftype, iov.iov_base);
3023 return rc;
3024}
3025
3026int
3027SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003028 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003029{
3030 struct smb2_query_info_rsp *rsp = NULL;
3031 struct kvec iov;
3032 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003033 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003034 struct cifs_ses *ses = tcon->ses;
3035 unsigned int rsp_len, offset;
3036
Steven French21671142013-10-09 13:36:35 -05003037 if (level == FS_DEVICE_INFORMATION) {
3038 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3039 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3040 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3041 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3042 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003043 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3044 max_len = sizeof(struct smb3_fs_ss_info);
3045 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003046 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003047 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003048 return -EINVAL;
3049 }
3050
3051 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003052 persistent_fid, volatile_fid);
3053 if (rc)
3054 return rc;
3055
3056 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3057 if (rc) {
3058 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3059 goto qfsattr_exit;
3060 }
3061 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3062
3063 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3064 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003065 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3066 if (rc)
3067 goto qfsattr_exit;
3068
3069 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003070 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3071 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003072 rsp_len, max_len));
3073 else if (level == FS_DEVICE_INFORMATION)
3074 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3075 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003076 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3077 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3078 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3079 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3080 tcon->perf_sector_size =
3081 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3082 }
Steve French34f62642013-10-09 02:07:00 -05003083
3084qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003085 free_rsp_buf(resp_buftype, iov.iov_base);
3086 return rc;
3087}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003088
3089int
3090smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3091 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3092 const __u32 num_lock, struct smb2_lock_element *buf)
3093{
3094 int rc = 0;
3095 struct smb2_lock_req *req = NULL;
3096 struct kvec iov[2];
3097 int resp_buf_type;
3098 unsigned int count;
3099
Joe Perchesf96637b2013-05-04 22:12:25 -05003100 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003101
3102 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3103 if (rc)
3104 return rc;
3105
3106 req->hdr.ProcessId = cpu_to_le32(pid);
3107 req->LockCount = cpu_to_le16(num_lock);
3108
3109 req->PersistentFileId = persist_fid;
3110 req->VolatileFileId = volatile_fid;
3111
3112 count = num_lock * sizeof(struct smb2_lock_element);
3113 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3114
3115 iov[0].iov_base = (char *)req;
3116 /* 4 for rfc1002 length field and count for all locks */
3117 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3118 iov[1].iov_base = (char *)buf;
3119 iov[1].iov_len = count;
3120
3121 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3122 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3123 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003124 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003125 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3126 }
3127
3128 return rc;
3129}
3130
3131int
3132SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3133 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3134 const __u64 length, const __u64 offset, const __u32 lock_flags,
3135 const bool wait)
3136{
3137 struct smb2_lock_element lock;
3138
3139 lock.Offset = cpu_to_le64(offset);
3140 lock.Length = cpu_to_le64(length);
3141 lock.Flags = cpu_to_le32(lock_flags);
3142 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3143 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3144
3145 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3146}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003147
3148int
3149SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3150 __u8 *lease_key, const __le32 lease_state)
3151{
3152 int rc;
3153 struct smb2_lease_ack *req = NULL;
3154
Joe Perchesf96637b2013-05-04 22:12:25 -05003155 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003156 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3157
3158 if (rc)
3159 return rc;
3160
3161 req->hdr.CreditRequest = cpu_to_le16(1);
3162 req->StructureSize = cpu_to_le16(36);
3163 inc_rfc1001_len(req, 12);
3164
3165 memcpy(req->LeaseKey, lease_key, 16);
3166 req->LeaseState = lease_state;
3167
3168 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3169 /* SMB2 buffer freed by function above */
3170
3171 if (rc) {
3172 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003173 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003174 }
3175
3176 return rc;
3177}