blob: 5255deac86b2e8c5f1444c33c577a4a36fd7e686 [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
Steve French92225e42019-07-25 18:13:10 -0500663#ifdef CONFIG_CIFS_DFS_UPCALL
664 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
665#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100666 req->Capabilities = 0;
Steve French92225e42019-07-25 18:13:10 -0500667#endif /* DFS_UPCALL */
668
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100669 req->Channel = 0; /* MBZ */
670
671 sess_data->iov[0].iov_base = (char *)req;
672 /* 4 for rfc1002 length field and 1 for pad */
673 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
674 /*
675 * This variable will be used to clear the buffer
676 * allocated above in case of any error in the calling function.
677 */
678 sess_data->buf0_type = CIFS_SMALL_BUFFER;
679
680 return 0;
681}
682
683static void
684SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
685{
686 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
687 sess_data->buf0_type = CIFS_NO_BUFFER;
688}
689
690static int
691SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
692{
693 int rc;
694 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
695
696 /* Testing shows that buffer offset must be at location of Buffer[0] */
697 req->SecurityBufferOffset =
698 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
699 1 /* pad */ - 4 /* rfc1001 len */);
700 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
701
702 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
703
704 /* BB add code to build os and lm fields */
705
706 rc = SendReceive2(sess_data->xid, sess_data->ses,
707 sess_data->iov, 2,
708 &sess_data->buf0_type,
709 CIFS_LOG_ERROR | CIFS_NEG_OP);
710
711 return rc;
712}
713
714static int
715SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
716{
717 int rc = 0;
718 struct cifs_ses *ses = sess_data->ses;
719
720 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800721 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100722 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100723 if (rc) {
724 cifs_dbg(FYI,
725 "SMB3 session key generation failed\n");
726 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800727 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100728 }
729 }
730 if (!ses->server->session_estab) {
731 ses->server->sequence_number = 0x2;
732 ses->server->session_estab = true;
733 }
734 mutex_unlock(&ses->server->srv_mutex);
735
736 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
737 spin_lock(&GlobalMid_Lock);
738 ses->status = CifsGood;
739 ses->need_reconnect = false;
740 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100741 return rc;
742}
743
744#ifdef CONFIG_CIFS_UPCALL
745static void
746SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
747{
748 int rc;
749 struct cifs_ses *ses = sess_data->ses;
750 struct cifs_spnego_msg *msg;
751 struct key *spnego_key = NULL;
752 struct smb2_sess_setup_rsp *rsp = NULL;
753
754 rc = SMB2_sess_alloc_buffer(sess_data);
755 if (rc)
756 goto out;
757
758 spnego_key = cifs_get_spnego_key(ses);
759 if (IS_ERR(spnego_key)) {
760 rc = PTR_ERR(spnego_key);
761 spnego_key = NULL;
762 goto out;
763 }
764
765 msg = spnego_key->payload.data[0];
766 /*
767 * check version field to make sure that cifs.upcall is
768 * sending us a response in an expected form
769 */
770 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
771 cifs_dbg(VFS,
772 "bad cifs.upcall version. Expected %d got %d",
773 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
774 rc = -EKEYREJECTED;
775 goto out_put_spnego_key;
776 }
777
778 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
779 GFP_KERNEL);
780 if (!ses->auth_key.response) {
781 cifs_dbg(VFS,
782 "Kerberos can't allocate (%u bytes) memory",
783 msg->sesskey_len);
784 rc = -ENOMEM;
785 goto out_put_spnego_key;
786 }
787 ses->auth_key.len = msg->sesskey_len;
788
789 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
790 sess_data->iov[1].iov_len = msg->secblob_len;
791
792 rc = SMB2_sess_sendreceive(sess_data);
793 if (rc)
794 goto out_put_spnego_key;
795
796 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
797 ses->Suid = rsp->hdr.SessionId;
798
799 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
800 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
801 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
802
803 rc = SMB2_sess_establish_session(sess_data);
804out_put_spnego_key:
805 key_invalidate(spnego_key);
806 key_put(spnego_key);
807out:
808 sess_data->result = rc;
809 sess_data->func = NULL;
810 SMB2_sess_free_buffer(sess_data);
811}
812#else
813static void
814SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
815{
816 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
817 sess_data->result = -EOPNOTSUPP;
818 sess_data->func = NULL;
819}
820#endif
821
Sachin Prabhu166cea42016-10-07 19:11:22 +0100822static void
823SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
824
825static void
826SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
827{
828 int rc;
829 struct cifs_ses *ses = sess_data->ses;
830 struct smb2_sess_setup_rsp *rsp = NULL;
831 char *ntlmssp_blob = NULL;
832 bool use_spnego = false; /* else use raw ntlmssp */
833 u16 blob_length = 0;
834
835 /*
836 * If memory allocation is successful, caller of this function
837 * frees it.
838 */
839 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
840 if (!ses->ntlmssp) {
841 rc = -ENOMEM;
842 goto out_err;
843 }
844 ses->ntlmssp->sesskey_per_smbsess = true;
845
846 rc = SMB2_sess_alloc_buffer(sess_data);
847 if (rc)
848 goto out_err;
849
850 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
851 GFP_KERNEL);
852 if (ntlmssp_blob == NULL) {
853 rc = -ENOMEM;
854 goto out;
855 }
856
857 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
858 if (use_spnego) {
859 /* BB eventually need to add this */
860 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
861 rc = -EOPNOTSUPP;
862 goto out;
863 } else {
864 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
865 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
866 }
867 sess_data->iov[1].iov_base = ntlmssp_blob;
868 sess_data->iov[1].iov_len = blob_length;
869
870 rc = SMB2_sess_sendreceive(sess_data);
871 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
872
873 /* If true, rc here is expected and not an error */
874 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
875 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
876 rc = 0;
877
878 if (rc)
879 goto out;
880
881 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
882 le16_to_cpu(rsp->SecurityBufferOffset)) {
883 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
884 le16_to_cpu(rsp->SecurityBufferOffset));
885 rc = -EIO;
886 goto out;
887 }
888 rc = decode_ntlmssp_challenge(rsp->Buffer,
889 le16_to_cpu(rsp->SecurityBufferLength), ses);
890 if (rc)
891 goto out;
892
893 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
894
895
896 ses->Suid = rsp->hdr.SessionId;
897 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
898 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
899 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
900
901out:
902 kfree(ntlmssp_blob);
903 SMB2_sess_free_buffer(sess_data);
904 if (!rc) {
905 sess_data->result = 0;
906 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
907 return;
908 }
909out_err:
910 kfree(ses->ntlmssp);
911 ses->ntlmssp = NULL;
912 sess_data->result = rc;
913 sess_data->func = NULL;
914}
915
916static void
917SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
918{
919 int rc;
920 struct cifs_ses *ses = sess_data->ses;
921 struct smb2_sess_setup_req *req;
922 struct smb2_sess_setup_rsp *rsp = NULL;
923 unsigned char *ntlmssp_blob = NULL;
924 bool use_spnego = false; /* else use raw ntlmssp */
925 u16 blob_length = 0;
926
927 rc = SMB2_sess_alloc_buffer(sess_data);
928 if (rc)
929 goto out;
930
931 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
932 req->hdr.SessionId = ses->Suid;
933
934 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
935 sess_data->nls_cp);
936 if (rc) {
937 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
938 goto out;
939 }
940
941 if (use_spnego) {
942 /* BB eventually need to add this */
943 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
944 rc = -EOPNOTSUPP;
945 goto out;
946 }
947 sess_data->iov[1].iov_base = ntlmssp_blob;
948 sess_data->iov[1].iov_len = blob_length;
949
950 rc = SMB2_sess_sendreceive(sess_data);
951 if (rc)
952 goto out;
953
954 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
955
956 ses->Suid = rsp->hdr.SessionId;
957 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
958 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
959 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
960
961 rc = SMB2_sess_establish_session(sess_data);
962out:
963 kfree(ntlmssp_blob);
964 SMB2_sess_free_buffer(sess_data);
965 kfree(ses->ntlmssp);
966 ses->ntlmssp = NULL;
967 sess_data->result = rc;
968 sess_data->func = NULL;
969}
970
971static int
972SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
973{
974 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
975 ses->sectype = RawNTLMSSP;
976
977 switch (ses->sectype) {
978 case Kerberos:
979 sess_data->func = SMB2_auth_kerberos;
980 break;
981 case RawNTLMSSP:
982 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
983 break;
984 default:
985 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
986 return -EOPNOTSUPP;
987 }
988
989 return 0;
990}
991
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400992int
993SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
994 const struct nls_table *nls_cp)
995{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400996 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400997 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100998 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400999
Joe Perchesf96637b2013-05-04 22:12:25 -05001000 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001001
Jeff Layton3534b852013-05-24 07:41:01 -04001002 if (!server) {
1003 WARN(1, "%s: server is NULL!\n", __func__);
1004 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001005 }
1006
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001007 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1008 if (!sess_data)
1009 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001010
1011 rc = SMB2_select_sec(ses, sess_data);
1012 if (rc)
1013 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001014 sess_data->xid = xid;
1015 sess_data->ses = ses;
1016 sess_data->buf0_type = CIFS_NO_BUFFER;
1017 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frencha6c9a622018-05-31 15:19:25 -05001018 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001019
Sachin Prabhu166cea42016-10-07 19:11:22 +01001020 while (sess_data->func)
1021 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001022
Steve Frenchdf1be202017-09-19 18:40:03 -05001023 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1024 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001025 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001026out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001027 kfree(sess_data);
1028 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001029}
1030
1031int
1032SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1033{
1034 struct smb2_logoff_req *req; /* response is also trivial struct */
1035 int rc = 0;
1036 struct TCP_Server_Info *server;
1037
Joe Perchesf96637b2013-05-04 22:12:25 -05001038 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001039
1040 if (ses && (ses->server))
1041 server = ses->server;
1042 else
1043 return -EIO;
1044
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001045 /* no need to send SMB logoff if uid already closed due to reconnect */
1046 if (ses->need_reconnect)
1047 goto smb2_session_already_dead;
1048
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001049 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1050 if (rc)
1051 return rc;
1052
1053 /* since no tcon, smb2_init can not do this, so do here */
1054 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001055 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001056 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001057
1058 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1059 /*
1060 * No tcon so can't do
1061 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1062 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001063
1064smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001065 return rc;
1066}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001067
1068static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1069{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001070 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001071}
1072
1073#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1074
Steve Frenchde9f68d2013-11-15 11:26:24 -06001075/* These are similar values to what Windows uses */
1076static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1077{
1078 tcon->max_chunks = 256;
1079 tcon->max_bytes_chunk = 1048576;
1080 tcon->max_bytes_copy = 16777216;
1081}
1082
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001083int
1084SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1085 struct cifs_tcon *tcon, const struct nls_table *cp)
1086{
1087 struct smb2_tree_connect_req *req;
1088 struct smb2_tree_connect_rsp *rsp = NULL;
1089 struct kvec iov[2];
1090 int rc = 0;
1091 int resp_buftype;
1092 int unc_path_len;
1093 struct TCP_Server_Info *server;
1094 __le16 *unc_path = NULL;
1095
Joe Perchesf96637b2013-05-04 22:12:25 -05001096 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001097
1098 if ((ses->server) && tree)
1099 server = ses->server;
1100 else
1101 return -EIO;
1102
Steve Frenchff9f84b2015-09-26 09:48:58 -05001103 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001104 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1105 cifs_dbg(VFS, "encryption requested but no server support");
1106 return -EOPNOTSUPP;
1107 }
1108
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001109 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1110 if (unc_path == NULL)
1111 return -ENOMEM;
1112
1113 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1114 unc_path_len *= 2;
1115 if (unc_path_len < 2) {
1116 kfree(unc_path);
1117 return -EINVAL;
1118 }
1119
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001120 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1121 if (tcon)
1122 tcon->tid = 0;
1123
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001124 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1125 if (rc) {
1126 kfree(unc_path);
1127 return rc;
1128 }
1129
1130 if (tcon == NULL) {
1131 /* since no tcon, smb2_init can not do this, so do here */
1132 req->hdr.SessionId = ses->Suid;
1133 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1134 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1135 }
1136
1137 iov[0].iov_base = (char *)req;
1138 /* 4 for rfc1002 length field and 1 for pad */
1139 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1140
1141 /* Testing shows that buffer offset must be at location of Buffer[0] */
1142 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1143 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1144 req->PathLength = cpu_to_le16(unc_path_len - 2);
1145 iov[1].iov_base = unc_path;
1146 iov[1].iov_len = unc_path_len;
1147
1148 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1149
1150 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1151 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1152
1153 if (rc != 0) {
1154 if (tcon) {
1155 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1156 tcon->need_reconnect = true;
1157 }
1158 goto tcon_error_exit;
1159 }
1160
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001161 if (tcon == NULL) {
1162 ses->ipc_tid = rsp->hdr.TreeId;
1163 goto tcon_exit;
1164 }
1165
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001166 switch (rsp->ShareType) {
1167 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001168 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001169 break;
1170 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001171 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001172 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001173 break;
1174 case SMB2_SHARE_TYPE_PRINT:
1175 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001176 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcfea5632017-05-12 17:59:32 +02001177 break;
1178 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001179 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001180 rc = -EOPNOTSUPP;
1181 goto tcon_error_exit;
1182 }
1183
1184 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001185 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001186 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1187 tcon->tidStatus = CifsGood;
1188 tcon->need_reconnect = false;
1189 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001190 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001191
1192 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1193 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001194 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001195 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001196 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1197 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001198 if (tcon->ses->server->ops->validate_negotiate)
1199 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001200tcon_exit:
1201 free_rsp_buf(resp_buftype, rsp);
1202 kfree(unc_path);
1203 return rc;
1204
1205tcon_error_exit:
1206 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001207 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001208 }
1209 goto tcon_exit;
1210}
1211
1212int
1213SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1214{
1215 struct smb2_tree_disconnect_req *req; /* response is trivial */
1216 int rc = 0;
1217 struct TCP_Server_Info *server;
1218 struct cifs_ses *ses = tcon->ses;
1219
Joe Perchesf96637b2013-05-04 22:12:25 -05001220 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001221
1222 if (ses && (ses->server))
1223 server = ses->server;
1224 else
1225 return -EIO;
1226
1227 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1228 return 0;
1229
1230 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1231 if (rc)
1232 return rc;
1233
1234 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1235 if (rc)
1236 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1237
1238 return rc;
1239}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001240
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001241
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001242static struct create_durable *
1243create_durable_buf(void)
1244{
1245 struct create_durable *buf;
1246
1247 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1248 if (!buf)
1249 return NULL;
1250
1251 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001252 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001253 buf->ccontext.DataLength = cpu_to_le32(16);
1254 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1255 (struct create_durable, Name));
1256 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001257 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001258 buf->Name[0] = 'D';
1259 buf->Name[1] = 'H';
1260 buf->Name[2] = 'n';
1261 buf->Name[3] = 'Q';
1262 return buf;
1263}
1264
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001265static struct create_durable *
1266create_reconnect_durable_buf(struct cifs_fid *fid)
1267{
1268 struct create_durable *buf;
1269
1270 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1271 if (!buf)
1272 return NULL;
1273
1274 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1275 (struct create_durable, Data));
1276 buf->ccontext.DataLength = cpu_to_le32(16);
1277 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1278 (struct create_durable, Name));
1279 buf->ccontext.NameLength = cpu_to_le16(4);
1280 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1281 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001282 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001283 buf->Name[0] = 'D';
1284 buf->Name[1] = 'H';
1285 buf->Name[2] = 'n';
1286 buf->Name[3] = 'C';
1287 return buf;
1288}
1289
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001290static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001291parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1292 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001293{
1294 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001295 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001296 unsigned int next;
1297 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001298 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001299
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001300 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001301 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001302 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001303 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001304 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001305 if (le16_to_cpu(cc->NameLength) == 4 &&
1306 strncmp(name, "RqLs", 4) == 0)
1307 return server->ops->parse_lease_buf(cc, epoch);
1308
1309 next = le32_to_cpu(cc->Next);
1310 if (!next)
1311 break;
1312 remaining -= next;
1313 cc = (struct create_context *)((char *)cc + next);
1314 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001315
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001316 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001317}
1318
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001319static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001320add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1321 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001322{
1323 struct smb2_create_req *req = iov[0].iov_base;
1324 unsigned int num = *num_iovec;
1325
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001326 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001327 if (iov[num].iov_base == NULL)
1328 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001329 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001330 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1331 if (!req->CreateContextsOffset)
1332 req->CreateContextsOffset = cpu_to_le32(
1333 sizeof(struct smb2_create_req) - 4 +
1334 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001335 le32_add_cpu(&req->CreateContextsLength,
1336 server->vals->create_lease_size);
1337 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001338 *num_iovec = num + 1;
1339 return 0;
1340}
1341
Steve Frenchb56eae42015-11-03 09:26:27 -06001342static struct create_durable_v2 *
1343create_durable_v2_buf(struct cifs_fid *pfid)
1344{
1345 struct create_durable_v2 *buf;
1346
1347 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1348 if (!buf)
1349 return NULL;
1350
1351 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1352 (struct create_durable_v2, dcontext));
1353 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1354 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1355 (struct create_durable_v2, Name));
1356 buf->ccontext.NameLength = cpu_to_le16(4);
1357
1358 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1359 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001360 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001361 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1362
1363 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1364 buf->Name[0] = 'D';
1365 buf->Name[1] = 'H';
1366 buf->Name[2] = '2';
1367 buf->Name[3] = 'Q';
1368 return buf;
1369}
1370
1371static struct create_durable_handle_reconnect_v2 *
1372create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1373{
1374 struct create_durable_handle_reconnect_v2 *buf;
1375
1376 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1377 GFP_KERNEL);
1378 if (!buf)
1379 return NULL;
1380
1381 buf->ccontext.DataOffset =
1382 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1383 dcontext));
1384 buf->ccontext.DataLength =
1385 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1386 buf->ccontext.NameOffset =
1387 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1388 Name));
1389 buf->ccontext.NameLength = cpu_to_le16(4);
1390
1391 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1392 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1393 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1394 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1395
1396 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1397 buf->Name[0] = 'D';
1398 buf->Name[1] = 'H';
1399 buf->Name[2] = '2';
1400 buf->Name[3] = 'C';
1401 return buf;
1402}
1403
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001404static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001405add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001406 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001407{
1408 struct smb2_create_req *req = iov[0].iov_base;
1409 unsigned int num = *num_iovec;
1410
Steve Frenchb56eae42015-11-03 09:26:27 -06001411 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1412 if (iov[num].iov_base == NULL)
1413 return -ENOMEM;
1414 iov[num].iov_len = sizeof(struct create_durable_v2);
1415 if (!req->CreateContextsOffset)
1416 req->CreateContextsOffset =
1417 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1418 iov[1].iov_len);
1419 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1420 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1421 *num_iovec = num + 1;
1422 return 0;
1423}
1424
1425static int
1426add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1427 struct cifs_open_parms *oparms)
1428{
1429 struct smb2_create_req *req = iov[0].iov_base;
1430 unsigned int num = *num_iovec;
1431
1432 /* indicate that we don't need to relock the file */
1433 oparms->reconnect = false;
1434
1435 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1436 if (iov[num].iov_base == NULL)
1437 return -ENOMEM;
1438 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1439 if (!req->CreateContextsOffset)
1440 req->CreateContextsOffset =
1441 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1442 iov[1].iov_len);
1443 le32_add_cpu(&req->CreateContextsLength,
1444 sizeof(struct create_durable_handle_reconnect_v2));
1445 inc_rfc1001_len(&req->hdr,
1446 sizeof(struct create_durable_handle_reconnect_v2));
1447 *num_iovec = num + 1;
1448 return 0;
1449}
1450
1451static int
1452add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1453 struct cifs_open_parms *oparms, bool use_persistent)
1454{
1455 struct smb2_create_req *req = iov[0].iov_base;
1456 unsigned int num = *num_iovec;
1457
1458 if (use_persistent) {
1459 if (oparms->reconnect)
1460 return add_durable_reconnect_v2_context(iov, num_iovec,
1461 oparms);
1462 else
1463 return add_durable_v2_context(iov, num_iovec, oparms);
1464 }
1465
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001466 if (oparms->reconnect) {
1467 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1468 /* indicate that we don't need to relock the file */
1469 oparms->reconnect = false;
1470 } else
1471 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001472 if (iov[num].iov_base == NULL)
1473 return -ENOMEM;
1474 iov[num].iov_len = sizeof(struct create_durable);
1475 if (!req->CreateContextsOffset)
1476 req->CreateContextsOffset =
1477 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1478 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001479 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001480 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1481 *num_iovec = num + 1;
1482 return 0;
1483}
1484
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001485int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001486SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001487 __u8 *oplock, struct smb2_file_all_info *buf,
1488 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001489{
1490 struct smb2_create_req *req;
1491 struct smb2_create_rsp *rsp;
1492 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001493 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001494 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001495 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001496 int resp_buftype;
1497 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001498 __le16 *copy_path = NULL;
1499 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001500 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001501 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001502 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001503 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001504
Joe Perchesf96637b2013-05-04 22:12:25 -05001505 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001506
1507 if (ses && (ses->server))
1508 server = ses->server;
1509 else
1510 return -EIO;
1511
1512 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1513 if (rc)
1514 return rc;
1515
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001516 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001517 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001518 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1519 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001520
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001521 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001522 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001523 /* File attributes ignored on open (used in create though) */
1524 req->FileAttributes = cpu_to_le32(file_attributes);
1525 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001526 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1527 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001528 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001529 /* do not count rfc1001 len field */
1530 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001531
1532 iov[0].iov_base = (char *)req;
1533 /* 4 for rfc1002 length field */
1534 iov[0].iov_len = get_rfc1002_length(req) + 4;
1535
1536 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001537 req->NameLength = cpu_to_le16(uni_path_len - 2);
1538 /* -1 since last byte is buf[0] which is sent below (path) */
1539 iov[0].iov_len--;
1540 if (uni_path_len % 8 != 0) {
1541 copy_size = uni_path_len / 8 * 8;
1542 if (copy_size < uni_path_len)
1543 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001544
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001545 copy_path = kzalloc(copy_size, GFP_KERNEL);
1546 if (!copy_path)
1547 return -ENOMEM;
1548 memcpy((char *)copy_path, (const char *)path,
1549 uni_path_len);
1550 uni_path_len = copy_size;
1551 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001552 }
1553
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001554 iov[1].iov_len = uni_path_len;
1555 iov[1].iov_base = path;
1556 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1557 inc_rfc1001_len(req, uni_path_len - 1);
1558
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001559 if (!server->oplocks)
1560 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1561
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001562 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001563 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1564 req->RequestedOplockLevel = *oplock;
1565 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001566 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001567 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001568 cifs_small_buf_release(req);
1569 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001570 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001571 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001572 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001573 }
1574
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001575 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1576 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001577 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001578 struct create_context *ccontext =
1579 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001580 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001581 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001582 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001583
1584 rc = add_durable_context(iov, &num_iovecs, oparms,
1585 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001586 if (rc) {
1587 cifs_small_buf_release(req);
1588 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001589 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001590 return rc;
1591 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001592 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001593 }
1594
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001595 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1596 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1597
1598 if (rc != 0) {
1599 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001600 if (err_buf)
1601 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1602 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001603 goto creat_exit;
1604 }
1605
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001606 oparms->fid->persistent_fid = rsp->PersistentFileId;
1607 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001608
1609 if (buf) {
1610 memcpy(buf, &rsp->CreationTime, 32);
1611 buf->AllocationSize = rsp->AllocationSize;
1612 buf->EndOfFile = rsp->EndofFile;
1613 buf->Attributes = rsp->FileAttributes;
1614 buf->NumberOfLinks = cpu_to_le32(1);
1615 buf->DeletePending = 0;
1616 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001617
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001618 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001619 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001620 else
1621 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001622creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001623 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001624 kfree(lc_buf);
1625 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001626 free_rsp_buf(resp_buftype, rsp);
1627 return rc;
1628}
1629
Steve French4a72daf2013-06-25 00:20:49 -05001630/*
1631 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1632 */
1633int
1634SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1635 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1636 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1637{
1638 struct smb2_ioctl_req *req;
1639 struct smb2_ioctl_rsp *rsp;
1640 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001641 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001642 struct kvec iov[2];
1643 int resp_buftype;
1644 int num_iovecs;
1645 int rc = 0;
1646
1647 cifs_dbg(FYI, "SMB2 IOCTL\n");
1648
Steve French3d1a3742014-08-11 21:05:25 -05001649 if (out_data != NULL)
1650 *out_data = NULL;
1651
Steve French4a72daf2013-06-25 00:20:49 -05001652 /* zero out returned data len, in case of error */
1653 if (plen)
1654 *plen = 0;
1655
Steve French8e353102015-03-26 19:47:02 -05001656 if (tcon)
1657 ses = tcon->ses;
1658 else
1659 return -EIO;
1660
Steve French4a72daf2013-06-25 00:20:49 -05001661 if (ses && (ses->server))
1662 server = ses->server;
1663 else
1664 return -EIO;
1665
1666 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1667 if (rc)
1668 return rc;
1669
1670 req->CtlCode = cpu_to_le32(opcode);
1671 req->PersistentFileId = persistent_fid;
1672 req->VolatileFileId = volatile_fid;
1673
1674 if (indatalen) {
1675 req->InputCount = cpu_to_le32(indatalen);
1676 /* do not set InputOffset if no input data */
1677 req->InputOffset =
1678 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1679 iov[1].iov_base = in_data;
1680 iov[1].iov_len = indatalen;
1681 num_iovecs = 2;
1682 } else
1683 num_iovecs = 1;
1684
1685 req->OutputOffset = 0;
1686 req->OutputCount = 0; /* MBZ */
1687
1688 /*
1689 * Could increase MaxOutputResponse, but that would require more
1690 * than one credit. Windows typically sets this smaller, but for some
1691 * ioctls it may be useful to allow server to send more. No point
1692 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001693 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1694 * (by default, note that it can be overridden to make max larger)
1695 * in responses (except for read responses which can be bigger.
1696 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001697 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001698 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001699
1700 if (is_fsctl)
1701 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1702 else
1703 req->Flags = 0;
1704
1705 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001706
Steve French7ff8d452013-10-14 00:44:19 -05001707 /*
1708 * If no input data, the size of ioctl struct in
1709 * protocol spec still includes a 1 byte data buffer,
1710 * but if input data passed to ioctl, we do not
1711 * want to double count this, so we do not send
1712 * the dummy one byte of data in iovec[0] if sending
1713 * input data (in iovec[1]). We also must add 4 bytes
1714 * in first iovec to allow for rfc1002 length field.
1715 */
1716
1717 if (indatalen) {
1718 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1719 inc_rfc1001_len(req, indatalen - 1);
1720 } else
1721 iov[0].iov_len = get_rfc1002_length(req) + 4;
1722
Steve Frenchfca16f92017-10-25 15:58:31 -05001723 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1724 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1725 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001726
1727 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1728 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1729
Steve French9bf0c9c2013-11-16 18:05:28 -06001730 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001731 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001732 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001733 } else if (rc == -EINVAL) {
1734 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1735 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001736 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001737 goto ioctl_exit;
1738 }
Steve French4a72daf2013-06-25 00:20:49 -05001739 }
1740
1741 /* check if caller wants to look at return data or just return rc */
1742 if ((plen == NULL) || (out_data == NULL))
1743 goto ioctl_exit;
1744
1745 *plen = le32_to_cpu(rsp->OutputCount);
1746
1747 /* We check for obvious errors in the output buffer length and offset */
1748 if (*plen == 0)
1749 goto ioctl_exit; /* server returned no data */
1750 else if (*plen > 0xFF00) {
1751 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1752 *plen = 0;
1753 rc = -EIO;
1754 goto ioctl_exit;
1755 }
1756
1757 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1758 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1759 le32_to_cpu(rsp->OutputOffset));
1760 *plen = 0;
1761 rc = -EIO;
1762 goto ioctl_exit;
1763 }
1764
1765 *out_data = kmalloc(*plen, GFP_KERNEL);
1766 if (*out_data == NULL) {
1767 rc = -ENOMEM;
1768 goto ioctl_exit;
1769 }
1770
Steve French373512e2015-12-18 13:05:30 -06001771 memcpy(*out_data,
1772 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001773 *plen);
1774ioctl_exit:
1775 free_rsp_buf(resp_buftype, rsp);
1776 return rc;
1777}
1778
Steve French64a5cfa2013-10-14 15:31:32 -05001779/*
1780 * Individual callers to ioctl worker function follow
1781 */
1782
1783int
1784SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1785 u64 persistent_fid, u64 volatile_fid)
1786{
1787 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001788 struct compress_ioctl fsctl_input;
1789 char *ret_data = NULL;
1790
1791 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001792 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001793
1794 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1795 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1796 (char *)&fsctl_input /* data input */,
1797 2 /* in data len */, &ret_data /* out data */, NULL);
1798
1799 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001800
1801 return rc;
1802}
1803
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001804int
1805SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1806 u64 persistent_fid, u64 volatile_fid)
1807{
1808 struct smb2_close_req *req;
1809 struct smb2_close_rsp *rsp;
1810 struct TCP_Server_Info *server;
1811 struct cifs_ses *ses = tcon->ses;
1812 struct kvec iov[1];
1813 int resp_buftype;
1814 int rc = 0;
1815
Joe Perchesf96637b2013-05-04 22:12:25 -05001816 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001817
1818 if (ses && (ses->server))
1819 server = ses->server;
1820 else
1821 return -EIO;
1822
1823 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1824 if (rc)
1825 return rc;
1826
1827 req->PersistentFileId = persistent_fid;
1828 req->VolatileFileId = volatile_fid;
1829
1830 iov[0].iov_base = (char *)req;
1831 /* 4 for rfc1002 length field */
1832 iov[0].iov_len = get_rfc1002_length(req) + 4;
1833
1834 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1835 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1836
1837 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001838 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001839 goto close_exit;
1840 }
1841
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001842 /* BB FIXME - decode close response, update inode for caching */
1843
1844close_exit:
1845 free_rsp_buf(resp_buftype, rsp);
1846 return rc;
1847}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001848
1849static int
1850validate_buf(unsigned int offset, unsigned int buffer_length,
1851 struct smb2_hdr *hdr, unsigned int min_buf_size)
1852
1853{
1854 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1855 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1856 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1857 char *end_of_buf = begin_of_buf + buffer_length;
1858
1859
1860 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001861 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1862 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001863 return -EINVAL;
1864 }
1865
1866 /* check if beyond RFC1001 maximum length */
1867 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001868 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1869 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001870 return -EINVAL;
1871 }
1872
1873 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001874 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001875 return -EINVAL;
1876 }
1877
1878 return 0;
1879}
1880
1881/*
1882 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1883 * Caller must free buffer.
1884 */
1885static int
1886validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1887 struct smb2_hdr *hdr, unsigned int minbufsize,
1888 char *data)
1889
1890{
1891 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1892 int rc;
1893
1894 if (!data)
1895 return -EINVAL;
1896
1897 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1898 if (rc)
1899 return rc;
1900
1901 memcpy(data, begin_of_buf, buffer_length);
1902
1903 return 0;
1904}
1905
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001906static int
1907query_info(const unsigned int xid, struct cifs_tcon *tcon,
1908 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1909 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001910{
1911 struct smb2_query_info_req *req;
1912 struct smb2_query_info_rsp *rsp = NULL;
1913 struct kvec iov[2];
1914 int rc = 0;
1915 int resp_buftype;
1916 struct TCP_Server_Info *server;
1917 struct cifs_ses *ses = tcon->ses;
1918
Joe Perchesf96637b2013-05-04 22:12:25 -05001919 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001920
1921 if (ses && (ses->server))
1922 server = ses->server;
1923 else
1924 return -EIO;
1925
1926 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1927 if (rc)
1928 return rc;
1929
1930 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001931 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001932 req->PersistentFileId = persistent_fid;
1933 req->VolatileFileId = volatile_fid;
1934 /* 4 for rfc1002 length field and 1 for Buffer */
1935 req->InputBufferOffset =
1936 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001937 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001938
1939 iov[0].iov_base = (char *)req;
1940 /* 4 for rfc1002 length field */
1941 iov[0].iov_len = get_rfc1002_length(req) + 4;
1942
1943 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001944 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1945
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001946 if (rc) {
1947 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1948 goto qinf_exit;
1949 }
1950
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001951 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1952 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001953 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001954
1955qinf_exit:
1956 free_rsp_buf(resp_buftype, rsp);
1957 return rc;
1958}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001959
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001960int
1961SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1962 u64 persistent_fid, u64 volatile_fid,
1963 struct smb2_file_all_info *data)
1964{
1965 return query_info(xid, tcon, persistent_fid, volatile_fid,
1966 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001967 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001968 sizeof(struct smb2_file_all_info), data);
1969}
1970
1971int
1972SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1973 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1974{
1975 return query_info(xid, tcon, persistent_fid, volatile_fid,
1976 FILE_INTERNAL_INFORMATION,
1977 sizeof(struct smb2_file_internal_info),
1978 sizeof(struct smb2_file_internal_info), uniqueid);
1979}
1980
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001981/*
1982 * This is a no-op for now. We're not really interested in the reply, but
1983 * rather in the fact that the server sent one and that server->lstrp
1984 * gets updated.
1985 *
1986 * FIXME: maybe we should consider checking that the reply matches request?
1987 */
1988static void
1989smb2_echo_callback(struct mid_q_entry *mid)
1990{
1991 struct TCP_Server_Info *server = mid->callback_data;
1992 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1993 unsigned int credits_received = 1;
1994
1995 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1996 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1997
Christopher Oo5fb4e282015-06-25 16:10:48 -07001998 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001999 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002000 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002001 add_credits(server, credits_received, CIFS_ECHO_OP);
2002}
2003
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002004void smb2_reconnect_server(struct work_struct *work)
2005{
2006 struct TCP_Server_Info *server = container_of(work,
2007 struct TCP_Server_Info, reconnect.work);
2008 struct cifs_ses *ses;
2009 struct cifs_tcon *tcon, *tcon2;
2010 struct list_head tmp_list;
2011 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002012 int rc;
2013 int resched = false;
2014
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002015
2016 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2017 mutex_lock(&server->reconnect_mutex);
2018
2019 INIT_LIST_HEAD(&tmp_list);
2020 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2021
2022 spin_lock(&cifs_tcp_ses_lock);
2023 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2024 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002025 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002026 tcon->tc_count++;
2027 list_add_tail(&tcon->rlist, &tmp_list);
2028 tcon_exist = true;
2029 }
2030 }
2031 }
2032 /*
2033 * Get the reference to server struct to be sure that the last call of
2034 * cifs_put_tcon() in the loop below won't release the server pointer.
2035 */
2036 if (tcon_exist)
2037 server->srv_count++;
2038
2039 spin_unlock(&cifs_tcp_ses_lock);
2040
2041 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002042 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2043 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002044 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002045 else
2046 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002047 list_del_init(&tcon->rlist);
2048 cifs_put_tcon(tcon);
2049 }
2050
2051 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002052 if (resched)
2053 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002054 mutex_unlock(&server->reconnect_mutex);
2055
2056 /* now we can safely release srv struct */
2057 if (tcon_exist)
2058 cifs_put_tcp_session(server, 1);
2059}
2060
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002061int
2062SMB2_echo(struct TCP_Server_Info *server)
2063{
2064 struct smb2_echo_req *req;
2065 int rc = 0;
2066 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002067 struct smb_rqst rqst = { .rq_iov = &iov,
2068 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002069
Joe Perchesf96637b2013-05-04 22:12:25 -05002070 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002071
Steve French4fcd1812016-06-22 20:12:05 -05002072 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002073 /* No need to send echo on newly established connections */
2074 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2075 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002076 }
2077
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002078 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2079 if (rc)
2080 return rc;
2081
2082 req->hdr.CreditRequest = cpu_to_le16(1);
2083
2084 iov.iov_base = (char *)req;
2085 /* 4 for rfc1002 length field */
2086 iov.iov_len = get_rfc1002_length(req) + 4;
2087
Jeff Laytonfec344e2012-09-18 16:20:35 -07002088 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002089 CIFS_ECHO_OP);
2090 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002091 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002092
2093 cifs_small_buf_release(req);
2094 return rc;
2095}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002096
2097int
2098SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2099 u64 volatile_fid)
2100{
2101 struct smb2_flush_req *req;
2102 struct TCP_Server_Info *server;
2103 struct cifs_ses *ses = tcon->ses;
2104 struct kvec iov[1];
2105 int resp_buftype;
2106 int rc = 0;
2107
Joe Perchesf96637b2013-05-04 22:12:25 -05002108 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002109
2110 if (ses && (ses->server))
2111 server = ses->server;
2112 else
2113 return -EIO;
2114
2115 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2116 if (rc)
2117 return rc;
2118
2119 req->PersistentFileId = persistent_fid;
2120 req->VolatileFileId = volatile_fid;
2121
2122 iov[0].iov_base = (char *)req;
2123 /* 4 for rfc1002 length field */
2124 iov[0].iov_len = get_rfc1002_length(req) + 4;
2125
2126 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2127
Steve Frenchdfebe402015-03-27 01:00:06 -05002128 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002129 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2130
2131 free_rsp_buf(resp_buftype, iov[0].iov_base);
2132 return rc;
2133}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002134
2135/*
2136 * To form a chain of read requests, any read requests after the first should
2137 * have the end_of_chain boolean set to true.
2138 */
2139static int
2140smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2141 unsigned int remaining_bytes, int request_type)
2142{
2143 int rc = -EACCES;
2144 struct smb2_read_req *req = NULL;
2145
2146 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2147 if (rc)
2148 return rc;
2149 if (io_parms->tcon->ses->server == NULL)
2150 return -ECONNABORTED;
2151
2152 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2153
2154 req->PersistentFileId = io_parms->persistent_fid;
2155 req->VolatileFileId = io_parms->volatile_fid;
2156 req->ReadChannelInfoOffset = 0; /* reserved */
2157 req->ReadChannelInfoLength = 0; /* reserved */
2158 req->Channel = 0; /* reserved */
2159 req->MinimumCount = 0;
2160 req->Length = cpu_to_le32(io_parms->length);
2161 req->Offset = cpu_to_le64(io_parms->offset);
2162
2163 if (request_type & CHAINED_REQUEST) {
2164 if (!(request_type & END_OF_CHAIN)) {
2165 /* 4 for rfc1002 length field */
2166 req->hdr.NextCommand =
2167 cpu_to_le32(get_rfc1002_length(req) + 4);
2168 } else /* END_OF_CHAIN */
2169 req->hdr.NextCommand = 0;
2170 if (request_type & RELATED_REQUEST) {
2171 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2172 /*
2173 * Related requests use info from previous read request
2174 * in chain.
2175 */
2176 req->hdr.SessionId = 0xFFFFFFFF;
2177 req->hdr.TreeId = 0xFFFFFFFF;
2178 req->PersistentFileId = 0xFFFFFFFF;
2179 req->VolatileFileId = 0xFFFFFFFF;
2180 }
2181 }
2182 if (remaining_bytes > io_parms->length)
2183 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2184 else
2185 req->RemainingBytes = 0;
2186
2187 iov[0].iov_base = (char *)req;
2188 /* 4 for rfc1002 length field */
2189 iov[0].iov_len = get_rfc1002_length(req) + 4;
2190 return rc;
2191}
2192
2193static void
2194smb2_readv_callback(struct mid_q_entry *mid)
2195{
2196 struct cifs_readdata *rdata = mid->callback_data;
2197 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2198 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002199 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002200 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002201 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002202 .rq_nvec = 1,
2203 .rq_pages = rdata->pages,
2204 .rq_npages = rdata->nr_pages,
2205 .rq_pagesz = rdata->pagesz,
2206 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002207
Joe Perchesf96637b2013-05-04 22:12:25 -05002208 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2209 __func__, mid->mid, mid->mid_state, rdata->result,
2210 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002211
2212 switch (mid->mid_state) {
2213 case MID_RESPONSE_RECEIVED:
2214 credits_received = le16_to_cpu(buf->CreditRequest);
2215 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002216 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002217 int rc;
2218
Jeff Layton0b688cf2012-09-18 16:20:34 -07002219 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002220 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002221 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2222 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002223 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002224 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002225 task_io_account_read(rdata->got_bytes);
2226 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002227 break;
2228 case MID_REQUEST_SUBMITTED:
2229 case MID_RETRY_NEEDED:
2230 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002231 if (server->sign && rdata->got_bytes)
2232 /* reset bytes number since we can not check a sign */
2233 rdata->got_bytes = 0;
2234 /* FIXME: should this be counted toward the initiating task? */
2235 task_io_account_read(rdata->got_bytes);
2236 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002237 break;
2238 default:
2239 if (rdata->result != -ENODATA)
2240 rdata->result = -EIO;
2241 }
2242
2243 if (rdata->result)
2244 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2245
2246 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002247 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002248 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002249 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002250 add_credits(server, credits_received, 0);
2251}
2252
2253/* smb2_async_readv - send an async write, and set up mid to handle result */
2254int
2255smb2_async_readv(struct cifs_readdata *rdata)
2256{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002257 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002258 struct smb2_hdr *buf;
2259 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002260 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002261 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002262 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002263
Joe Perchesf96637b2013-05-04 22:12:25 -05002264 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2265 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002266
2267 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2268 io_parms.offset = rdata->offset;
2269 io_parms.length = rdata->bytes;
2270 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2271 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2272 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002273
2274 server = io_parms.tcon->ses->server;
2275
Jeff Layton58195752012-09-19 06:22:34 -07002276 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002277 if (rc) {
2278 if (rc == -EAGAIN && rdata->credits) {
2279 /* credits was reset by reconnect */
2280 rdata->credits = 0;
2281 /* reduce in_flight value since we won't send the req */
2282 spin_lock(&server->req_lock);
2283 server->in_flight--;
2284 spin_unlock(&server->req_lock);
2285 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002286 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002287 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002288
Jeff Layton58195752012-09-19 06:22:34 -07002289 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002290 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002291 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002292
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002293 if (rdata->credits) {
2294 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2295 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002296 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002297 spin_lock(&server->req_lock);
2298 server->credits += rdata->credits -
2299 le16_to_cpu(buf->CreditCharge);
2300 spin_unlock(&server->req_lock);
2301 wake_up(&server->request_q);
2302 flags = CIFS_HAS_CREDITS;
2303 }
2304
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002305 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002306 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002307 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002308 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002309 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002310 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002311 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2312 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002313
2314 cifs_small_buf_release(buf);
2315 return rc;
2316}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002317
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002318int
2319SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2320 unsigned int *nbytes, char **buf, int *buf_type)
2321{
2322 int resp_buftype, rc = -EACCES;
2323 struct smb2_read_rsp *rsp = NULL;
2324 struct kvec iov[1];
2325
2326 *nbytes = 0;
2327 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2328 if (rc)
2329 return rc;
2330
2331 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2332 &resp_buftype, CIFS_LOG_ERROR);
2333
2334 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2335
2336 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2337 free_rsp_buf(resp_buftype, iov[0].iov_base);
2338 return 0;
2339 }
2340
2341 if (rc) {
2342 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002343 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002344 } else {
2345 *nbytes = le32_to_cpu(rsp->DataLength);
2346 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2347 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002348 cifs_dbg(FYI, "bad length %d for count %d\n",
2349 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002350 rc = -EIO;
2351 *nbytes = 0;
2352 }
2353 }
2354
2355 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002356 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002357 *nbytes);
2358 free_rsp_buf(resp_buftype, iov[0].iov_base);
2359 } else if (resp_buftype != CIFS_NO_BUFFER) {
2360 *buf = iov[0].iov_base;
2361 if (resp_buftype == CIFS_SMALL_BUFFER)
2362 *buf_type = CIFS_SMALL_BUFFER;
2363 else if (resp_buftype == CIFS_LARGE_BUFFER)
2364 *buf_type = CIFS_LARGE_BUFFER;
2365 }
2366 return rc;
2367}
2368
Pavel Shilovsky33319142012-09-18 16:20:29 -07002369/*
2370 * Check the mid_state and signature on received buffer (if any), and queue the
2371 * workqueue completion task.
2372 */
2373static void
2374smb2_writev_callback(struct mid_q_entry *mid)
2375{
2376 struct cifs_writedata *wdata = mid->callback_data;
2377 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002378 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002379 unsigned int written;
2380 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2381 unsigned int credits_received = 1;
2382
2383 switch (mid->mid_state) {
2384 case MID_RESPONSE_RECEIVED:
2385 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2386 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2387 if (wdata->result != 0)
2388 break;
2389
2390 written = le32_to_cpu(rsp->DataLength);
2391 /*
2392 * Mask off high 16 bits when bytes written as returned
2393 * by the server is greater than bytes requested by the
2394 * client. OS/2 servers are known to set incorrect
2395 * CountHigh values.
2396 */
2397 if (written > wdata->bytes)
2398 written &= 0xFFFF;
2399
2400 if (written < wdata->bytes)
2401 wdata->result = -ENOSPC;
2402 else
2403 wdata->bytes = written;
2404 break;
2405 case MID_REQUEST_SUBMITTED:
2406 case MID_RETRY_NEEDED:
2407 wdata->result = -EAGAIN;
2408 break;
2409 default:
2410 wdata->result = -EIO;
2411 break;
2412 }
2413
2414 if (wdata->result)
2415 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2416
2417 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002418 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002419 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002420 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002421 add_credits(tcon->ses->server, credits_received, 0);
2422}
2423
2424/* smb2_async_writev - send an async write, and set up mid to handle result */
2425int
Steve French4a5c80d2014-02-07 20:45:12 -06002426smb2_async_writev(struct cifs_writedata *wdata,
2427 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002428{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002429 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002430 struct smb2_write_req *req = NULL;
2431 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002432 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002433 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002434 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002435
2436 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002437 if (rc) {
2438 if (rc == -EAGAIN && wdata->credits) {
2439 /* credits was reset by reconnect */
2440 wdata->credits = 0;
2441 /* reduce in_flight value since we won't send the req */
2442 spin_lock(&server->req_lock);
2443 server->in_flight--;
2444 spin_unlock(&server->req_lock);
2445 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002446 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002447 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002448
Pavel Shilovsky33319142012-09-18 16:20:29 -07002449 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2450
2451 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2452 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2453 req->WriteChannelInfoOffset = 0;
2454 req->WriteChannelInfoLength = 0;
2455 req->Channel = 0;
2456 req->Offset = cpu_to_le64(wdata->offset);
2457 /* 4 for rfc1002 length field */
2458 req->DataOffset = cpu_to_le16(
2459 offsetof(struct smb2_write_req, Buffer) - 4);
2460 req->RemainingBytes = 0;
2461
2462 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002463 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2464 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002465
Jeff Laytoneddb0792012-09-18 16:20:35 -07002466 rqst.rq_iov = &iov;
2467 rqst.rq_nvec = 1;
2468 rqst.rq_pages = wdata->pages;
2469 rqst.rq_npages = wdata->nr_pages;
2470 rqst.rq_pagesz = wdata->pagesz;
2471 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002472
Joe Perchesf96637b2013-05-04 22:12:25 -05002473 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2474 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002475
2476 req->Length = cpu_to_le32(wdata->bytes);
2477
2478 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2479
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002480 if (wdata->credits) {
2481 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2482 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002483 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002484 spin_lock(&server->req_lock);
2485 server->credits += wdata->credits -
2486 le16_to_cpu(req->hdr.CreditCharge);
2487 spin_unlock(&server->req_lock);
2488 wake_up(&server->request_q);
2489 flags = CIFS_HAS_CREDITS;
2490 }
2491
Pavel Shilovsky33319142012-09-18 16:20:29 -07002492 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002493 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2494 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002495
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002496 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002497 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002498 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2499 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002500
Pavel Shilovsky33319142012-09-18 16:20:29 -07002501async_writev_out:
2502 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002503 return rc;
2504}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002505
2506/*
2507 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2508 * The length field from io_parms must be at least 1 and indicates a number of
2509 * elements with data to write that begins with position 1 in iov array. All
2510 * data length is specified by count.
2511 */
2512int
2513SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2514 unsigned int *nbytes, struct kvec *iov, int n_vec)
2515{
2516 int rc = 0;
2517 struct smb2_write_req *req = NULL;
2518 struct smb2_write_rsp *rsp = NULL;
2519 int resp_buftype;
2520 *nbytes = 0;
2521
2522 if (n_vec < 1)
2523 return rc;
2524
2525 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2526 if (rc)
2527 return rc;
2528
2529 if (io_parms->tcon->ses->server == NULL)
2530 return -ECONNABORTED;
2531
2532 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2533
2534 req->PersistentFileId = io_parms->persistent_fid;
2535 req->VolatileFileId = io_parms->volatile_fid;
2536 req->WriteChannelInfoOffset = 0;
2537 req->WriteChannelInfoLength = 0;
2538 req->Channel = 0;
2539 req->Length = cpu_to_le32(io_parms->length);
2540 req->Offset = cpu_to_le64(io_parms->offset);
2541 /* 4 for rfc1002 length field */
2542 req->DataOffset = cpu_to_le16(
2543 offsetof(struct smb2_write_req, Buffer) - 4);
2544 req->RemainingBytes = 0;
2545
2546 iov[0].iov_base = (char *)req;
2547 /* 4 for rfc1002 length field and 1 for Buffer */
2548 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2549
2550 /* length of entire message including data to be written */
2551 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2552
2553 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2554 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002555 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002556
2557 if (rc) {
2558 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002559 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002560 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002561 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002562
2563 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002564 return rc;
2565}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002566
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002567static unsigned int
2568num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2569{
2570 int len;
2571 unsigned int entrycount = 0;
2572 unsigned int next_offset = 0;
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002573 char *entryptr;
2574 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002575
2576 if (bufstart == NULL)
2577 return 0;
2578
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002579 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002580
2581 while (1) {
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002582 if (entryptr + next_offset < entryptr ||
2583 entryptr + next_offset > end_of_buf ||
2584 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002585 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002586 break;
2587 }
2588
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002589 entryptr = entryptr + next_offset;
2590 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
2591
2592 len = le32_to_cpu(dir_info->FileNameLength);
2593 if (entryptr + len < entryptr ||
2594 entryptr + len > end_of_buf ||
2595 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002596 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2597 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002598 break;
2599 }
2600
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002601 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002602 entrycount++;
2603
Dan Carpenterfe6198c2018-09-06 12:48:22 +03002604 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002605 if (!next_offset)
2606 break;
2607 }
2608
2609 return entrycount;
2610}
2611
2612/*
2613 * Readdir/FindFirst
2614 */
2615int
2616SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2617 u64 persistent_fid, u64 volatile_fid, int index,
2618 struct cifs_search_info *srch_inf)
2619{
2620 struct smb2_query_directory_req *req;
2621 struct smb2_query_directory_rsp *rsp = NULL;
2622 struct kvec iov[2];
2623 int rc = 0;
2624 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002625 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002626 unsigned char *bufptr;
2627 struct TCP_Server_Info *server;
2628 struct cifs_ses *ses = tcon->ses;
2629 __le16 asteriks = cpu_to_le16('*');
2630 char *end_of_smb;
2631 unsigned int output_size = CIFSMaxBufSize;
2632 size_t info_buf_size;
2633
2634 if (ses && (ses->server))
2635 server = ses->server;
2636 else
2637 return -EIO;
2638
2639 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2640 if (rc)
2641 return rc;
2642
2643 switch (srch_inf->info_level) {
2644 case SMB_FIND_FILE_DIRECTORY_INFO:
2645 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2646 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2647 break;
2648 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2649 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2650 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2651 break;
2652 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002653 cifs_dbg(VFS, "info level %u isn't supported\n",
2654 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002655 rc = -EINVAL;
2656 goto qdir_exit;
2657 }
2658
2659 req->FileIndex = cpu_to_le32(index);
2660 req->PersistentFileId = persistent_fid;
2661 req->VolatileFileId = volatile_fid;
2662
2663 len = 0x2;
2664 bufptr = req->Buffer;
2665 memcpy(bufptr, &asteriks, len);
2666
2667 req->FileNameOffset =
2668 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2669 req->FileNameLength = cpu_to_le16(len);
2670 /*
2671 * BB could be 30 bytes or so longer if we used SMB2 specific
2672 * buffer lengths, but this is safe and close enough.
2673 */
2674 output_size = min_t(unsigned int, output_size, server->maxBuf);
2675 output_size = min_t(unsigned int, output_size, 2 << 15);
2676 req->OutputBufferLength = cpu_to_le32(output_size);
2677
2678 iov[0].iov_base = (char *)req;
2679 /* 4 for RFC1001 length and 1 for Buffer */
2680 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2681
2682 iov[1].iov_base = (char *)(req->Buffer);
2683 iov[1].iov_len = len;
2684
2685 inc_rfc1001_len(req, len - 1 /* Buffer */);
2686
2687 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002688 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2689
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002690 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002691 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2692 srch_inf->endOfSearch = true;
2693 rc = 0;
Pavel Shilovsky9033b4f2019-01-26 12:21:32 -08002694 } else
2695 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002696 goto qdir_exit;
2697 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002698
2699 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2700 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2701 info_buf_size);
2702 if (rc)
2703 goto qdir_exit;
2704
2705 srch_inf->unicode = true;
2706
2707 if (srch_inf->ntwrk_buf_start) {
2708 if (srch_inf->smallBuf)
2709 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2710 else
2711 cifs_buf_release(srch_inf->ntwrk_buf_start);
2712 }
2713 srch_inf->ntwrk_buf_start = (char *)rsp;
2714 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2715 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2716 /* 4 for rfc1002 length field */
2717 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2718 srch_inf->entries_in_buffer =
2719 num_entries(srch_inf->srch_entries_start, end_of_smb,
2720 &srch_inf->last_entry, info_buf_size);
2721 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002722 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2723 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2724 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002725 if (resp_buftype == CIFS_LARGE_BUFFER)
2726 srch_inf->smallBuf = false;
2727 else if (resp_buftype == CIFS_SMALL_BUFFER)
2728 srch_inf->smallBuf = true;
2729 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002730 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002731
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002732 return rc;
2733
2734qdir_exit:
2735 free_rsp_buf(resp_buftype, rsp);
2736 return rc;
2737}
2738
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002739static int
2740send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002741 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002742 unsigned int num, void **data, unsigned int *size)
2743{
2744 struct smb2_set_info_req *req;
2745 struct smb2_set_info_rsp *rsp = NULL;
2746 struct kvec *iov;
2747 int rc = 0;
2748 int resp_buftype;
2749 unsigned int i;
2750 struct TCP_Server_Info *server;
2751 struct cifs_ses *ses = tcon->ses;
2752
2753 if (ses && (ses->server))
2754 server = ses->server;
2755 else
2756 return -EIO;
2757
2758 if (!num)
2759 return -EINVAL;
2760
2761 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2762 if (!iov)
2763 return -ENOMEM;
2764
2765 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2766 if (rc) {
2767 kfree(iov);
2768 return rc;
2769 }
2770
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002771 req->hdr.ProcessId = cpu_to_le32(pid);
2772
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002773 req->InfoType = SMB2_O_INFO_FILE;
2774 req->FileInfoClass = info_class;
2775 req->PersistentFileId = persistent_fid;
2776 req->VolatileFileId = volatile_fid;
2777
2778 /* 4 for RFC1001 length and 1 for Buffer */
2779 req->BufferOffset =
2780 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2781 req->BufferLength = cpu_to_le32(*size);
2782
2783 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2784
2785 memcpy(req->Buffer, *data, *size);
2786
2787 iov[0].iov_base = (char *)req;
2788 /* 4 for RFC1001 length */
2789 iov[0].iov_len = get_rfc1002_length(req) + 4;
2790
2791 for (i = 1; i < num; i++) {
2792 inc_rfc1001_len(req, size[i]);
2793 le32_add_cpu(&req->BufferLength, size[i]);
2794 iov[i].iov_base = (char *)data[i];
2795 iov[i].iov_len = size[i];
2796 }
2797
2798 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2799 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2800
Steve French7d3fb242013-11-18 09:56:28 -06002801 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002802 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002803
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002804 free_rsp_buf(resp_buftype, rsp);
2805 kfree(iov);
2806 return rc;
2807}
2808
2809int
2810SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2811 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2812{
2813 struct smb2_file_rename_info info;
2814 void **data;
2815 unsigned int size[2];
2816 int rc;
2817 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2818
2819 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2820 if (!data)
2821 return -ENOMEM;
2822
2823 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2824 /* 0 = fail if target already exists */
2825 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2826 info.FileNameLength = cpu_to_le32(len);
2827
2828 data[0] = &info;
2829 size[0] = sizeof(struct smb2_file_rename_info);
2830
2831 data[1] = target_file;
2832 size[1] = len + 2 /* null */;
2833
2834 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002835 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2836 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002837 kfree(data);
2838 return rc;
2839}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002840
2841int
Steve French897fba12016-05-12 21:20:36 -05002842SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2843 u64 persistent_fid, u64 volatile_fid)
2844{
2845 __u8 delete_pending = 1;
2846 void *data;
2847 unsigned int size;
2848
2849 data = &delete_pending;
2850 size = 1; /* sizeof __u8 */
2851
2852 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2853 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2854 &size);
2855}
2856
2857int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002858SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2859 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2860{
2861 struct smb2_file_link_info info;
2862 void **data;
2863 unsigned int size[2];
2864 int rc;
2865 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2866
2867 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2868 if (!data)
2869 return -ENOMEM;
2870
2871 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2872 /* 0 = fail if link already exists */
2873 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2874 info.FileNameLength = cpu_to_le32(len);
2875
2876 data[0] = &info;
2877 size[0] = sizeof(struct smb2_file_link_info);
2878
2879 data[1] = target_file;
2880 size[1] = len + 2 /* null */;
2881
2882 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002883 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002884 kfree(data);
2885 return rc;
2886}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002887
2888int
2889SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002890 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002891{
2892 struct smb2_file_eof_info info;
2893 void *data;
2894 unsigned int size;
2895
2896 info.EndOfFile = *eof;
2897
2898 data = &info;
2899 size = sizeof(struct smb2_file_eof_info);
2900
Steve Frenchf29ebb42014-07-19 21:44:58 -05002901 if (is_falloc)
2902 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2903 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2904 else
2905 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2906 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002907}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002908
2909int
2910SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2911 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2912{
2913 unsigned int size;
2914 size = sizeof(FILE_BASIC_INFO);
2915 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2916 current->tgid, FILE_BASIC_INFORMATION, 1,
2917 (void **)&buf, &size);
2918}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002919
2920int
2921SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2922 const u64 persistent_fid, const u64 volatile_fid,
2923 __u8 oplock_level)
2924{
2925 int rc;
2926 struct smb2_oplock_break *req = NULL;
2927
Joe Perchesf96637b2013-05-04 22:12:25 -05002928 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002929 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2930
2931 if (rc)
2932 return rc;
2933
2934 req->VolatileFid = volatile_fid;
2935 req->PersistentFid = persistent_fid;
2936 req->OplockLevel = oplock_level;
2937 req->hdr.CreditRequest = cpu_to_le16(1);
2938
2939 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2940 /* SMB2 buffer freed by function above */
2941
2942 if (rc) {
2943 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002944 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002945 }
2946
2947 return rc;
2948}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002949
2950static void
2951copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2952 struct kstatfs *kst)
2953{
2954 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2955 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2956 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302957 kst->f_bfree = kst->f_bavail =
2958 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002959 return;
2960}
2961
2962static int
2963build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2964 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2965{
2966 int rc;
2967 struct smb2_query_info_req *req;
2968
Joe Perchesf96637b2013-05-04 22:12:25 -05002969 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002970
2971 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2972 return -EIO;
2973
2974 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2975 if (rc)
2976 return rc;
2977
2978 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2979 req->FileInfoClass = level;
2980 req->PersistentFileId = persistent_fid;
2981 req->VolatileFileId = volatile_fid;
2982 /* 4 for rfc1002 length field and 1 for pad */
2983 req->InputBufferOffset =
2984 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2985 req->OutputBufferLength = cpu_to_le32(
2986 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2987
2988 iov->iov_base = (char *)req;
2989 /* 4 for rfc1002 length field */
2990 iov->iov_len = get_rfc1002_length(req) + 4;
2991 return 0;
2992}
2993
2994int
2995SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2996 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2997{
2998 struct smb2_query_info_rsp *rsp = NULL;
2999 struct kvec iov;
3000 int rc = 0;
3001 int resp_buftype;
3002 struct cifs_ses *ses = tcon->ses;
3003 struct smb2_fs_full_size_info *info = NULL;
3004
3005 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3006 sizeof(struct smb2_fs_full_size_info),
3007 persistent_fid, volatile_fid);
3008 if (rc)
3009 return rc;
3010
3011 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3012 if (rc) {
3013 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003014 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003015 }
3016 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3017
3018 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3019 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3020 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3021 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3022 sizeof(struct smb2_fs_full_size_info));
3023 if (!rc)
3024 copy_fs_info_to_kstatfs(info, fsdata);
3025
Steve French34f62642013-10-09 02:07:00 -05003026qfsinf_exit:
3027 free_rsp_buf(resp_buftype, iov.iov_base);
3028 return rc;
3029}
3030
3031int
3032SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003033 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003034{
3035 struct smb2_query_info_rsp *rsp = NULL;
3036 struct kvec iov;
3037 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003038 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003039 struct cifs_ses *ses = tcon->ses;
3040 unsigned int rsp_len, offset;
3041
Steven French21671142013-10-09 13:36:35 -05003042 if (level == FS_DEVICE_INFORMATION) {
3043 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3044 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3045 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3046 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3047 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003048 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3049 max_len = sizeof(struct smb3_fs_ss_info);
3050 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003051 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003052 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003053 return -EINVAL;
3054 }
3055
3056 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003057 persistent_fid, volatile_fid);
3058 if (rc)
3059 return rc;
3060
3061 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3062 if (rc) {
3063 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3064 goto qfsattr_exit;
3065 }
3066 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3067
3068 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3069 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003070 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3071 if (rc)
3072 goto qfsattr_exit;
3073
3074 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003075 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3076 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003077 rsp_len, max_len));
3078 else if (level == FS_DEVICE_INFORMATION)
3079 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3080 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003081 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3082 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3083 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3084 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3085 tcon->perf_sector_size =
3086 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3087 }
Steve French34f62642013-10-09 02:07:00 -05003088
3089qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003090 free_rsp_buf(resp_buftype, iov.iov_base);
3091 return rc;
3092}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003093
3094int
3095smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3096 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3097 const __u32 num_lock, struct smb2_lock_element *buf)
3098{
3099 int rc = 0;
3100 struct smb2_lock_req *req = NULL;
3101 struct kvec iov[2];
3102 int resp_buf_type;
3103 unsigned int count;
3104
Joe Perchesf96637b2013-05-04 22:12:25 -05003105 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003106
3107 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3108 if (rc)
3109 return rc;
3110
3111 req->hdr.ProcessId = cpu_to_le32(pid);
3112 req->LockCount = cpu_to_le16(num_lock);
3113
3114 req->PersistentFileId = persist_fid;
3115 req->VolatileFileId = volatile_fid;
3116
3117 count = num_lock * sizeof(struct smb2_lock_element);
3118 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3119
3120 iov[0].iov_base = (char *)req;
3121 /* 4 for rfc1002 length field and count for all locks */
3122 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3123 iov[1].iov_base = (char *)buf;
3124 iov[1].iov_len = count;
3125
3126 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3127 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3128 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003129 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003130 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3131 }
3132
3133 return rc;
3134}
3135
3136int
3137SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3138 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3139 const __u64 length, const __u64 offset, const __u32 lock_flags,
3140 const bool wait)
3141{
3142 struct smb2_lock_element lock;
3143
3144 lock.Offset = cpu_to_le64(offset);
3145 lock.Length = cpu_to_le64(length);
3146 lock.Flags = cpu_to_le32(lock_flags);
3147 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3148 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3149
3150 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3151}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003152
3153int
3154SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3155 __u8 *lease_key, const __le32 lease_state)
3156{
3157 int rc;
3158 struct smb2_lease_ack *req = NULL;
3159
Joe Perchesf96637b2013-05-04 22:12:25 -05003160 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003161 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3162
3163 if (rc)
3164 return rc;
3165
3166 req->hdr.CreditRequest = cpu_to_le16(1);
3167 req->StructureSize = cpu_to_le16(36);
3168 inc_rfc1001_len(req, 12);
3169
3170 memcpy(req->LeaseKey, lease_key, 16);
3171 req->LeaseState = lease_state;
3172
3173 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3174 /* SMB2 buffer freed by function above */
3175
3176 if (rc) {
3177 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003178 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003179 }
3180
3181 return rc;
3182}