blob: 7c26286a525d5161bd3172f90c0604bd83c87c4c [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{
158 int rc = 0;
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)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
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
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
243 rc = cifs_negotiate_protocol(0, tcon->ses);
244 if (!rc && tcon->ses->need_reconnect)
245 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
246
247 if (rc || !tcon->need_reconnect) {
248 mutex_unlock(&tcon->ses->session_mutex);
249 goto out;
250 }
251
252 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800253 if (tcon->use_persistent)
254 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500255
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400256 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
257 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500258
Joe Perchesf96637b2013-05-04 22:12:25 -0500259 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400260 if (rc)
261 goto out;
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800262
263 if (smb2_command != SMB2_INTERNAL_CMD)
264 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
265
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400266 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400267out:
268 /*
269 * Check if handle based operation so we know whether we can continue
270 * or not without returning to caller to reset file handle.
271 */
272 /*
273 * BB Is flush done by server on drop of tcp session? Should we special
274 * case it and skip above?
275 */
276 switch (smb2_command) {
277 case SMB2_FLUSH:
278 case SMB2_READ:
279 case SMB2_WRITE:
280 case SMB2_LOCK:
281 case SMB2_IOCTL:
282 case SMB2_QUERY_DIRECTORY:
283 case SMB2_CHANGE_NOTIFY:
284 case SMB2_QUERY_INFO:
285 case SMB2_SET_INFO:
Pavel Shilovsky69d13b62016-11-29 11:30:58 -0800286 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400287 }
288 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400289 return rc;
290}
291
292/*
293 * Allocate and return pointer to an SMB request hdr, and set basic
294 * SMB information in the SMB header. If the return code is zero, this
295 * function must have filled in request_buf pointer.
296 */
297static int
298small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
299 void **request_buf)
300{
301 int rc = 0;
302
303 rc = smb2_reconnect(smb2_command, tcon);
304 if (rc)
305 return rc;
306
307 /* BB eventually switch this to SMB2 specific small buf size */
308 *request_buf = cifs_small_buf_get();
309 if (*request_buf == NULL) {
310 /* BB should we add a retry in here if not a writepage? */
311 return -ENOMEM;
312 }
313
314 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
315
316 if (tcon != NULL) {
317#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400318 uint16_t com_code = le16_to_cpu(smb2_command);
319 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400320#endif
321 cifs_stats_inc(&tcon->num_smbs_sent);
322 }
323
324 return rc;
325}
326
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500327#ifdef CONFIG_CIFS_SMB311
328/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
329#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
330
331
332#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
333#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
334
335static void
336build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
337{
338 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
339 pneg_ctxt->DataLength = cpu_to_le16(38);
340 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
341 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
342 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
343 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
344}
345
346static void
347build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
348{
349 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
350 pneg_ctxt->DataLength = cpu_to_le16(6);
351 pneg_ctxt->CipherCount = cpu_to_le16(2);
352 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
353 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
354}
355
356static void
357assemble_neg_contexts(struct smb2_negotiate_req *req)
358{
359
360 /* +4 is to account for the RFC1001 len field */
361 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
362
363 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
364 /* Add 2 to size to round to 8 byte boundary */
365 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
366 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
367 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
368 req->NegotiateContextCount = cpu_to_le16(2);
Steve Frenchf2d395b2017-09-18 18:18:45 -0500369 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500370 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
371}
372#else
373static void assemble_neg_contexts(struct smb2_negotiate_req *req)
374{
375 return;
376}
377#endif /* SMB311 */
378
379
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400380/*
381 *
382 * SMB2 Worker functions follow:
383 *
384 * The general structure of the worker functions is:
385 * 1) Call smb2_init (assembles SMB2 header)
386 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
387 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
388 * 4) Decode SMB2 command specific fields in the fixed length area
389 * 5) Decode variable length data area (if any for this SMB2 command type)
390 * 6) Call free smb buffer
391 * 7) return
392 *
393 */
394
395int
396SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
397{
398 struct smb2_negotiate_req *req;
399 struct smb2_negotiate_rsp *rsp;
400 struct kvec iov[1];
401 int rc = 0;
402 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400403 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400404 int blob_offset, blob_length;
405 char *security_blob;
406 int flags = CIFS_NEG_OP;
407
Joe Perchesf96637b2013-05-04 22:12:25 -0500408 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400409
Jeff Layton3534b852013-05-24 07:41:01 -0400410 if (!server) {
411 WARN(1, "%s: server is NULL!\n", __func__);
412 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413 }
414
415 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
416 if (rc)
417 return rc;
418
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400419 req->hdr.SessionId = 0;
420
Steve Frenche4aa25e2012-10-01 12:26:22 -0500421 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400422
Steve Frenche4aa25e2012-10-01 12:26:22 -0500423 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
424 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400425
426 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400427 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500428 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400429 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500430 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400431 else
432 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400433
Steve Frenche4aa25e2012-10-01 12:26:22 -0500434 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400435
Steve French3c5f9be12014-05-13 13:37:45 -0700436 /* ClientGUID must be zero for SMB2.02 dialect */
437 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
438 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500439 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700440 memcpy(req->ClientGUID, server->client_guid,
441 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500442 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
443 assemble_neg_contexts(req);
444 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445 iov[0].iov_base = (char *)req;
446 /* 4 for rfc1002 length field */
447 iov[0].iov_len = get_rfc1002_length(req) + 4;
448
449 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
450
451 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
452 /*
453 * No tcon so can't do
454 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
455 */
456 if (rc != 0)
457 goto neg_exit;
458
Joe Perchesf96637b2013-05-04 22:12:25 -0500459 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400460
Steve Frenche4aa25e2012-10-01 12:26:22 -0500461 /* BB we may eventually want to match the negotiated vs. requested
462 dialect, even though we are only requesting one at a time */
463 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500464 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500465 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500466 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500467 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500468 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500469 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600471#ifdef CONFIG_CIFS_SMB311
472 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
473 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
474#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400475 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500476 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500477 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400478 rc = -EIO;
479 goto neg_exit;
480 }
481 server->dialect = le16_to_cpu(rsp->DialectRevision);
482
Jeff Laytone598d1d82013-05-26 07:00:59 -0400483 /* SMB2 only has an extended negflavor */
484 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400485 /* set it to the maximum buffer size value we can send with 1 credit */
486 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
487 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400488 server->max_read = le32_to_cpu(rsp->MaxReadSize);
489 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
490 /* BB Do we need to validate the SecurityMode? */
491 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
492 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400493 /* Internal types */
494 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495
496 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
497 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500498 /*
499 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
500 * for us will be
501 * ses->sectype = RawNTLMSSP;
502 * but for time being this is our only auth choice so doesn't matter.
503 * We just found a server which sets blob length to zero expecting raw.
504 */
505 if (blob_length == 0)
506 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700507
Jeff Layton38d77c52013-05-26 07:01:00 -0400508 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400509 if (rc)
510 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500511 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500512 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500513 if (rc == 1)
514 rc = 0;
515 else if (rc == 0)
516 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400517 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400518neg_exit:
519 free_rsp_buf(resp_buftype, rsp);
520 return rc;
521}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400522
Steve Frenchff1c0382013-11-19 23:44:46 -0600523int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
524{
525 int rc = 0;
526 struct validate_negotiate_info_req vneg_inbuf;
527 struct validate_negotiate_info_rsp *pneg_rsp;
528 u32 rsplen;
529
530 cifs_dbg(FYI, "validate negotiate\n");
531
532 /*
533 * validation ioctl must be signed, so no point sending this if we
Steve French0e1b85a2017-09-20 19:57:18 -0500534 * can not sign it (ie are not known user). Even if signing is not
535 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600536 * sign just this, the first and only signed request on a connection.
Steve French0e1b85a2017-09-20 19:57:18 -0500537 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600538 */
Steve French0e1b85a2017-09-20 19:57:18 -0500539 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600540 return 0; /* validation requires signing */
541
Steve French0e1b85a2017-09-20 19:57:18 -0500542 if (tcon->ses->user_name == NULL) {
543 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
544 return 0; /* validation requires signing */
545 }
546
547 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
548 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
549
Steve Frenchff1c0382013-11-19 23:44:46 -0600550 vneg_inbuf.Capabilities =
551 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100552 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
553 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600554
555 if (tcon->ses->sign)
556 vneg_inbuf.SecurityMode =
557 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
558 else if (global_secflags & CIFSSEC_MAY_SIGN)
559 vneg_inbuf.SecurityMode =
560 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
561 else
562 vneg_inbuf.SecurityMode = 0;
563
564 vneg_inbuf.DialectCount = cpu_to_le16(1);
565 vneg_inbuf.Dialects[0] =
566 cpu_to_le16(tcon->ses->server->vals->protocol_id);
567
568 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
569 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
570 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
571 (char **)&pneg_rsp, &rsplen);
572
573 if (rc != 0) {
574 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
575 return -EIO;
576 }
577
578 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French8dd4e3f2017-05-03 21:12:20 -0500579 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
580 rsplen);
581
582 /* relax check since Mac returns max bufsize allowed on ioctl */
583 if (rsplen > CIFSMaxBufSize)
584 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600585 }
586
587 /* check validate negotiate info response matches what we got earlier */
Daniel N Petterssonf59eda12018-01-11 16:00:12 +0100588 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600589 goto vneg_out;
590
591 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
592 goto vneg_out;
593
594 /* do not validate server guid because not saved at negprot time yet */
595
596 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
597 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
598 goto vneg_out;
599
600 /* validate negotiate successful */
601 cifs_dbg(FYI, "validate negotiate info successful\n");
602 return 0;
603
604vneg_out:
605 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
606 return -EIO;
607}
608
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100609struct SMB2_sess_data {
610 unsigned int xid;
611 struct cifs_ses *ses;
612 struct nls_table *nls_cp;
613 void (*func)(struct SMB2_sess_data *);
614 int result;
615 u64 previous_session;
616
617 /* we will send the SMB in three pieces:
618 * a fixed length beginning part, an optional
619 * SPNEGO blob (which can be zero length), and a
620 * last part which will include the strings
621 * and rest of bcc area. This allows us to avoid
622 * a large buffer 17K allocation
623 */
624 int buf0_type;
625 struct kvec iov[2];
626};
627
628static int
629SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
630{
631 int rc;
632 struct cifs_ses *ses = sess_data->ses;
633 struct smb2_sess_setup_req *req;
634 struct TCP_Server_Info *server = ses->server;
635
636 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
637 if (rc)
638 return rc;
639
640 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
641
642 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
643 req->PreviousSessionId = sess_data->previous_session;
644
645 req->Flags = 0; /* MBZ */
646 /* to enable echos and oplocks */
647 req->hdr.CreditRequest = cpu_to_le16(3);
648
649 /* only one of SMB2 signing flags may be set in SMB2 request */
650 if (server->sign)
651 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
652 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
653 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
654 else
655 req->SecurityMode = 0;
656
657 req->Capabilities = 0;
658 req->Channel = 0; /* MBZ */
659
660 sess_data->iov[0].iov_base = (char *)req;
661 /* 4 for rfc1002 length field and 1 for pad */
662 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
663 /*
664 * This variable will be used to clear the buffer
665 * allocated above in case of any error in the calling function.
666 */
667 sess_data->buf0_type = CIFS_SMALL_BUFFER;
668
669 return 0;
670}
671
672static void
673SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
674{
675 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
676 sess_data->buf0_type = CIFS_NO_BUFFER;
677}
678
679static int
680SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
681{
682 int rc;
683 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
684
685 /* Testing shows that buffer offset must be at location of Buffer[0] */
686 req->SecurityBufferOffset =
687 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
688 1 /* pad */ - 4 /* rfc1001 len */);
689 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
690
691 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
692
693 /* BB add code to build os and lm fields */
694
695 rc = SendReceive2(sess_data->xid, sess_data->ses,
696 sess_data->iov, 2,
697 &sess_data->buf0_type,
698 CIFS_LOG_ERROR | CIFS_NEG_OP);
699
700 return rc;
701}
702
703static int
704SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
705{
706 int rc = 0;
707 struct cifs_ses *ses = sess_data->ses;
708
709 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800710 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100711 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100712 if (rc) {
713 cifs_dbg(FYI,
714 "SMB3 session key generation failed\n");
715 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskydf09b6f2016-11-07 18:20:50 -0800716 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100717 }
718 }
719 if (!ses->server->session_estab) {
720 ses->server->sequence_number = 0x2;
721 ses->server->session_estab = true;
722 }
723 mutex_unlock(&ses->server->srv_mutex);
724
725 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
726 spin_lock(&GlobalMid_Lock);
727 ses->status = CifsGood;
728 ses->need_reconnect = false;
729 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100730 return rc;
731}
732
733#ifdef CONFIG_CIFS_UPCALL
734static void
735SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
736{
737 int rc;
738 struct cifs_ses *ses = sess_data->ses;
739 struct cifs_spnego_msg *msg;
740 struct key *spnego_key = NULL;
741 struct smb2_sess_setup_rsp *rsp = NULL;
742
743 rc = SMB2_sess_alloc_buffer(sess_data);
744 if (rc)
745 goto out;
746
747 spnego_key = cifs_get_spnego_key(ses);
748 if (IS_ERR(spnego_key)) {
749 rc = PTR_ERR(spnego_key);
750 spnego_key = NULL;
751 goto out;
752 }
753
754 msg = spnego_key->payload.data[0];
755 /*
756 * check version field to make sure that cifs.upcall is
757 * sending us a response in an expected form
758 */
759 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
760 cifs_dbg(VFS,
761 "bad cifs.upcall version. Expected %d got %d",
762 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
763 rc = -EKEYREJECTED;
764 goto out_put_spnego_key;
765 }
766
767 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
768 GFP_KERNEL);
769 if (!ses->auth_key.response) {
770 cifs_dbg(VFS,
771 "Kerberos can't allocate (%u bytes) memory",
772 msg->sesskey_len);
773 rc = -ENOMEM;
774 goto out_put_spnego_key;
775 }
776 ses->auth_key.len = msg->sesskey_len;
777
778 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
779 sess_data->iov[1].iov_len = msg->secblob_len;
780
781 rc = SMB2_sess_sendreceive(sess_data);
782 if (rc)
783 goto out_put_spnego_key;
784
785 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
786 ses->Suid = rsp->hdr.SessionId;
787
788 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
789 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
790 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
791
792 rc = SMB2_sess_establish_session(sess_data);
793out_put_spnego_key:
794 key_invalidate(spnego_key);
795 key_put(spnego_key);
796out:
797 sess_data->result = rc;
798 sess_data->func = NULL;
799 SMB2_sess_free_buffer(sess_data);
800}
801#else
802static void
803SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
804{
805 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
806 sess_data->result = -EOPNOTSUPP;
807 sess_data->func = NULL;
808}
809#endif
810
Sachin Prabhu166cea42016-10-07 19:11:22 +0100811static void
812SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
813
814static void
815SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
816{
817 int rc;
818 struct cifs_ses *ses = sess_data->ses;
819 struct smb2_sess_setup_rsp *rsp = NULL;
820 char *ntlmssp_blob = NULL;
821 bool use_spnego = false; /* else use raw ntlmssp */
822 u16 blob_length = 0;
823
824 /*
825 * If memory allocation is successful, caller of this function
826 * frees it.
827 */
828 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
829 if (!ses->ntlmssp) {
830 rc = -ENOMEM;
831 goto out_err;
832 }
833 ses->ntlmssp->sesskey_per_smbsess = true;
834
835 rc = SMB2_sess_alloc_buffer(sess_data);
836 if (rc)
837 goto out_err;
838
839 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
840 GFP_KERNEL);
841 if (ntlmssp_blob == NULL) {
842 rc = -ENOMEM;
843 goto out;
844 }
845
846 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
847 if (use_spnego) {
848 /* BB eventually need to add this */
849 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
850 rc = -EOPNOTSUPP;
851 goto out;
852 } else {
853 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
854 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
855 }
856 sess_data->iov[1].iov_base = ntlmssp_blob;
857 sess_data->iov[1].iov_len = blob_length;
858
859 rc = SMB2_sess_sendreceive(sess_data);
860 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
861
862 /* If true, rc here is expected and not an error */
863 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
864 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
865 rc = 0;
866
867 if (rc)
868 goto out;
869
870 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
871 le16_to_cpu(rsp->SecurityBufferOffset)) {
872 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
873 le16_to_cpu(rsp->SecurityBufferOffset));
874 rc = -EIO;
875 goto out;
876 }
877 rc = decode_ntlmssp_challenge(rsp->Buffer,
878 le16_to_cpu(rsp->SecurityBufferLength), ses);
879 if (rc)
880 goto out;
881
882 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
883
884
885 ses->Suid = rsp->hdr.SessionId;
886 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
887 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
888 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
889
890out:
891 kfree(ntlmssp_blob);
892 SMB2_sess_free_buffer(sess_data);
893 if (!rc) {
894 sess_data->result = 0;
895 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
896 return;
897 }
898out_err:
899 kfree(ses->ntlmssp);
900 ses->ntlmssp = NULL;
901 sess_data->result = rc;
902 sess_data->func = NULL;
903}
904
905static void
906SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
907{
908 int rc;
909 struct cifs_ses *ses = sess_data->ses;
910 struct smb2_sess_setup_req *req;
911 struct smb2_sess_setup_rsp *rsp = NULL;
912 unsigned char *ntlmssp_blob = NULL;
913 bool use_spnego = false; /* else use raw ntlmssp */
914 u16 blob_length = 0;
915
916 rc = SMB2_sess_alloc_buffer(sess_data);
917 if (rc)
918 goto out;
919
920 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
921 req->hdr.SessionId = ses->Suid;
922
923 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
924 sess_data->nls_cp);
925 if (rc) {
926 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
927 goto out;
928 }
929
930 if (use_spnego) {
931 /* BB eventually need to add this */
932 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
933 rc = -EOPNOTSUPP;
934 goto out;
935 }
936 sess_data->iov[1].iov_base = ntlmssp_blob;
937 sess_data->iov[1].iov_len = blob_length;
938
939 rc = SMB2_sess_sendreceive(sess_data);
940 if (rc)
941 goto out;
942
943 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
944
945 ses->Suid = rsp->hdr.SessionId;
946 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
947 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
948 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
949
950 rc = SMB2_sess_establish_session(sess_data);
951out:
952 kfree(ntlmssp_blob);
953 SMB2_sess_free_buffer(sess_data);
954 kfree(ses->ntlmssp);
955 ses->ntlmssp = NULL;
956 sess_data->result = rc;
957 sess_data->func = NULL;
958}
959
960static int
961SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
962{
963 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
964 ses->sectype = RawNTLMSSP;
965
966 switch (ses->sectype) {
967 case Kerberos:
968 sess_data->func = SMB2_auth_kerberos;
969 break;
970 case RawNTLMSSP:
971 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
972 break;
973 default:
974 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
975 return -EOPNOTSUPP;
976 }
977
978 return 0;
979}
980
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400981int
982SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
983 const struct nls_table *nls_cp)
984{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400985 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400986 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100987 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400988
Joe Perchesf96637b2013-05-04 22:12:25 -0500989 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400990
Jeff Layton3534b852013-05-24 07:41:01 -0400991 if (!server) {
992 WARN(1, "%s: server is NULL!\n", __func__);
993 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400994 }
995
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100996 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
997 if (!sess_data)
998 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100999
1000 rc = SMB2_select_sec(ses, sess_data);
1001 if (rc)
1002 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001003 sess_data->xid = xid;
1004 sess_data->ses = ses;
1005 sess_data->buf0_type = CIFS_NO_BUFFER;
1006 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001007
Sachin Prabhu166cea42016-10-07 19:11:22 +01001008 while (sess_data->func)
1009 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001010
Steve Frenchdf1be202017-09-19 18:40:03 -05001011 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1012 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001013 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001014out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001015 kfree(sess_data);
1016 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001017}
1018
1019int
1020SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1021{
1022 struct smb2_logoff_req *req; /* response is also trivial struct */
1023 int rc = 0;
1024 struct TCP_Server_Info *server;
1025
Joe Perchesf96637b2013-05-04 22:12:25 -05001026 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001027
1028 if (ses && (ses->server))
1029 server = ses->server;
1030 else
1031 return -EIO;
1032
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001033 /* no need to send SMB logoff if uid already closed due to reconnect */
1034 if (ses->need_reconnect)
1035 goto smb2_session_already_dead;
1036
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001037 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1038 if (rc)
1039 return rc;
1040
1041 /* since no tcon, smb2_init can not do this, so do here */
1042 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001043 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001044 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001045
1046 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1047 /*
1048 * No tcon so can't do
1049 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1050 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001051
1052smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001053 return rc;
1054}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001055
1056static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1057{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001058 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001059}
1060
1061#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1062
Steve Frenchde9f68d2013-11-15 11:26:24 -06001063/* These are similar values to what Windows uses */
1064static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1065{
1066 tcon->max_chunks = 256;
1067 tcon->max_bytes_chunk = 1048576;
1068 tcon->max_bytes_copy = 16777216;
1069}
1070
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001071int
1072SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1073 struct cifs_tcon *tcon, const struct nls_table *cp)
1074{
1075 struct smb2_tree_connect_req *req;
1076 struct smb2_tree_connect_rsp *rsp = NULL;
1077 struct kvec iov[2];
1078 int rc = 0;
1079 int resp_buftype;
1080 int unc_path_len;
1081 struct TCP_Server_Info *server;
1082 __le16 *unc_path = NULL;
1083
Joe Perchesf96637b2013-05-04 22:12:25 -05001084 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001085
1086 if ((ses->server) && tree)
1087 server = ses->server;
1088 else
1089 return -EIO;
1090
Steve Frenchff9f84b2015-09-26 09:48:58 -05001091 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001092 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1093 cifs_dbg(VFS, "encryption requested but no server support");
1094 return -EOPNOTSUPP;
1095 }
1096
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001097 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1098 if (unc_path == NULL)
1099 return -ENOMEM;
1100
1101 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1102 unc_path_len *= 2;
1103 if (unc_path_len < 2) {
1104 kfree(unc_path);
1105 return -EINVAL;
1106 }
1107
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001108 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1109 if (tcon)
1110 tcon->tid = 0;
1111
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001112 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1113 if (rc) {
1114 kfree(unc_path);
1115 return rc;
1116 }
1117
1118 if (tcon == NULL) {
1119 /* since no tcon, smb2_init can not do this, so do here */
1120 req->hdr.SessionId = ses->Suid;
1121 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1122 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1123 }
1124
1125 iov[0].iov_base = (char *)req;
1126 /* 4 for rfc1002 length field and 1 for pad */
1127 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1128
1129 /* Testing shows that buffer offset must be at location of Buffer[0] */
1130 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1131 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1132 req->PathLength = cpu_to_le16(unc_path_len - 2);
1133 iov[1].iov_base = unc_path;
1134 iov[1].iov_len = unc_path_len;
1135
1136 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1137
1138 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1139 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1140
1141 if (rc != 0) {
1142 if (tcon) {
1143 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1144 tcon->need_reconnect = true;
1145 }
1146 goto tcon_error_exit;
1147 }
1148
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001149 if (tcon == NULL) {
1150 ses->ipc_tid = rsp->hdr.TreeId;
1151 goto tcon_exit;
1152 }
1153
1154 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001155 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001156 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1157 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001158 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001159 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1160 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001161 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001162 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001163 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001164 rc = -EOPNOTSUPP;
1165 goto tcon_error_exit;
1166 }
1167
1168 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001169 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001170 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1171 tcon->tidStatus = CifsGood;
1172 tcon->need_reconnect = false;
1173 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001174 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001175
1176 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1177 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001178 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001179 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001180 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1181 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001182 if (tcon->ses->server->ops->validate_negotiate)
1183 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001184tcon_exit:
1185 free_rsp_buf(resp_buftype, rsp);
1186 kfree(unc_path);
1187 return rc;
1188
1189tcon_error_exit:
1190 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001191 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001192 }
1193 goto tcon_exit;
1194}
1195
1196int
1197SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1198{
1199 struct smb2_tree_disconnect_req *req; /* response is trivial */
1200 int rc = 0;
1201 struct TCP_Server_Info *server;
1202 struct cifs_ses *ses = tcon->ses;
1203
Joe Perchesf96637b2013-05-04 22:12:25 -05001204 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001205
1206 if (ses && (ses->server))
1207 server = ses->server;
1208 else
1209 return -EIO;
1210
1211 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1212 return 0;
1213
1214 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1215 if (rc)
1216 return rc;
1217
1218 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1219 if (rc)
1220 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1221
1222 return rc;
1223}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001224
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001225
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001226static struct create_durable *
1227create_durable_buf(void)
1228{
1229 struct create_durable *buf;
1230
1231 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1232 if (!buf)
1233 return NULL;
1234
1235 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001236 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001237 buf->ccontext.DataLength = cpu_to_le32(16);
1238 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1239 (struct create_durable, Name));
1240 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001241 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001242 buf->Name[0] = 'D';
1243 buf->Name[1] = 'H';
1244 buf->Name[2] = 'n';
1245 buf->Name[3] = 'Q';
1246 return buf;
1247}
1248
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001249static struct create_durable *
1250create_reconnect_durable_buf(struct cifs_fid *fid)
1251{
1252 struct create_durable *buf;
1253
1254 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1255 if (!buf)
1256 return NULL;
1257
1258 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1259 (struct create_durable, Data));
1260 buf->ccontext.DataLength = cpu_to_le32(16);
1261 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1262 (struct create_durable, Name));
1263 buf->ccontext.NameLength = cpu_to_le16(4);
1264 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1265 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001266 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001267 buf->Name[0] = 'D';
1268 buf->Name[1] = 'H';
1269 buf->Name[2] = 'n';
1270 buf->Name[3] = 'C';
1271 return buf;
1272}
1273
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001274static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001275parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1276 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001277{
1278 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001279 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001280 unsigned int next;
1281 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001282 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001283
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001284 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001285 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001286 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001287 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001288 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001289 if (le16_to_cpu(cc->NameLength) == 4 &&
1290 strncmp(name, "RqLs", 4) == 0)
1291 return server->ops->parse_lease_buf(cc, epoch);
1292
1293 next = le32_to_cpu(cc->Next);
1294 if (!next)
1295 break;
1296 remaining -= next;
1297 cc = (struct create_context *)((char *)cc + next);
1298 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001299
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001300 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001301}
1302
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001303static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001304add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1305 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001306{
1307 struct smb2_create_req *req = iov[0].iov_base;
1308 unsigned int num = *num_iovec;
1309
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001310 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001311 if (iov[num].iov_base == NULL)
1312 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001313 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001314 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1315 if (!req->CreateContextsOffset)
1316 req->CreateContextsOffset = cpu_to_le32(
1317 sizeof(struct smb2_create_req) - 4 +
1318 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001319 le32_add_cpu(&req->CreateContextsLength,
1320 server->vals->create_lease_size);
1321 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001322 *num_iovec = num + 1;
1323 return 0;
1324}
1325
Steve Frenchb56eae42015-11-03 09:26:27 -06001326static struct create_durable_v2 *
1327create_durable_v2_buf(struct cifs_fid *pfid)
1328{
1329 struct create_durable_v2 *buf;
1330
1331 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1332 if (!buf)
1333 return NULL;
1334
1335 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1336 (struct create_durable_v2, dcontext));
1337 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1338 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1339 (struct create_durable_v2, Name));
1340 buf->ccontext.NameLength = cpu_to_le16(4);
1341
1342 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1343 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001344 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001345 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1346
1347 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1348 buf->Name[0] = 'D';
1349 buf->Name[1] = 'H';
1350 buf->Name[2] = '2';
1351 buf->Name[3] = 'Q';
1352 return buf;
1353}
1354
1355static struct create_durable_handle_reconnect_v2 *
1356create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1357{
1358 struct create_durable_handle_reconnect_v2 *buf;
1359
1360 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1361 GFP_KERNEL);
1362 if (!buf)
1363 return NULL;
1364
1365 buf->ccontext.DataOffset =
1366 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1367 dcontext));
1368 buf->ccontext.DataLength =
1369 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1370 buf->ccontext.NameOffset =
1371 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1372 Name));
1373 buf->ccontext.NameLength = cpu_to_le16(4);
1374
1375 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1376 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1377 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1378 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1379
1380 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1381 buf->Name[0] = 'D';
1382 buf->Name[1] = 'H';
1383 buf->Name[2] = '2';
1384 buf->Name[3] = 'C';
1385 return buf;
1386}
1387
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001388static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001389add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001390 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001391{
1392 struct smb2_create_req *req = iov[0].iov_base;
1393 unsigned int num = *num_iovec;
1394
Steve Frenchb56eae42015-11-03 09:26:27 -06001395 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1396 if (iov[num].iov_base == NULL)
1397 return -ENOMEM;
1398 iov[num].iov_len = sizeof(struct create_durable_v2);
1399 if (!req->CreateContextsOffset)
1400 req->CreateContextsOffset =
1401 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1402 iov[1].iov_len);
1403 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1404 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1405 *num_iovec = num + 1;
1406 return 0;
1407}
1408
1409static int
1410add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1411 struct cifs_open_parms *oparms)
1412{
1413 struct smb2_create_req *req = iov[0].iov_base;
1414 unsigned int num = *num_iovec;
1415
1416 /* indicate that we don't need to relock the file */
1417 oparms->reconnect = false;
1418
1419 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1420 if (iov[num].iov_base == NULL)
1421 return -ENOMEM;
1422 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1423 if (!req->CreateContextsOffset)
1424 req->CreateContextsOffset =
1425 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1426 iov[1].iov_len);
1427 le32_add_cpu(&req->CreateContextsLength,
1428 sizeof(struct create_durable_handle_reconnect_v2));
1429 inc_rfc1001_len(&req->hdr,
1430 sizeof(struct create_durable_handle_reconnect_v2));
1431 *num_iovec = num + 1;
1432 return 0;
1433}
1434
1435static int
1436add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1437 struct cifs_open_parms *oparms, bool use_persistent)
1438{
1439 struct smb2_create_req *req = iov[0].iov_base;
1440 unsigned int num = *num_iovec;
1441
1442 if (use_persistent) {
1443 if (oparms->reconnect)
1444 return add_durable_reconnect_v2_context(iov, num_iovec,
1445 oparms);
1446 else
1447 return add_durable_v2_context(iov, num_iovec, oparms);
1448 }
1449
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001450 if (oparms->reconnect) {
1451 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1452 /* indicate that we don't need to relock the file */
1453 oparms->reconnect = false;
1454 } else
1455 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001456 if (iov[num].iov_base == NULL)
1457 return -ENOMEM;
1458 iov[num].iov_len = sizeof(struct create_durable);
1459 if (!req->CreateContextsOffset)
1460 req->CreateContextsOffset =
1461 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1462 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001463 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001464 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1465 *num_iovec = num + 1;
1466 return 0;
1467}
1468
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001469int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001470SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001471 __u8 *oplock, struct smb2_file_all_info *buf,
1472 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001473{
1474 struct smb2_create_req *req;
1475 struct smb2_create_rsp *rsp;
1476 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001477 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001478 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001479 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001480 int resp_buftype;
1481 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001482 __le16 *copy_path = NULL;
1483 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001484 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001485 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001486 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001487 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001488
Joe Perchesf96637b2013-05-04 22:12:25 -05001489 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001490
1491 if (ses && (ses->server))
1492 server = ses->server;
1493 else
1494 return -EIO;
1495
1496 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1497 if (rc)
1498 return rc;
1499
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001500 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001501 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001502 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1503 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001504
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001505 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001506 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001507 /* File attributes ignored on open (used in create though) */
1508 req->FileAttributes = cpu_to_le32(file_attributes);
1509 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001510 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1511 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001512 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001513 /* do not count rfc1001 len field */
1514 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001515
1516 iov[0].iov_base = (char *)req;
1517 /* 4 for rfc1002 length field */
1518 iov[0].iov_len = get_rfc1002_length(req) + 4;
1519
1520 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001521 req->NameLength = cpu_to_le16(uni_path_len - 2);
1522 /* -1 since last byte is buf[0] which is sent below (path) */
1523 iov[0].iov_len--;
1524 if (uni_path_len % 8 != 0) {
1525 copy_size = uni_path_len / 8 * 8;
1526 if (copy_size < uni_path_len)
1527 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001528
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001529 copy_path = kzalloc(copy_size, GFP_KERNEL);
1530 if (!copy_path)
1531 return -ENOMEM;
1532 memcpy((char *)copy_path, (const char *)path,
1533 uni_path_len);
1534 uni_path_len = copy_size;
1535 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001536 }
1537
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001538 iov[1].iov_len = uni_path_len;
1539 iov[1].iov_base = path;
1540 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1541 inc_rfc1001_len(req, uni_path_len - 1);
1542
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001543 if (!server->oplocks)
1544 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1545
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001546 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001547 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1548 req->RequestedOplockLevel = *oplock;
1549 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001550 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001551 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001552 cifs_small_buf_release(req);
1553 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001554 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001555 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001556 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001557 }
1558
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001559 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1560 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001561 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001562 struct create_context *ccontext =
1563 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001564 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001565 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001566 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001567
1568 rc = add_durable_context(iov, &num_iovecs, oparms,
1569 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001570 if (rc) {
1571 cifs_small_buf_release(req);
1572 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001573 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001574 return rc;
1575 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001576 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001577 }
1578
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001579 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1580 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1581
1582 if (rc != 0) {
1583 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001584 if (err_buf)
1585 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1586 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001587 goto creat_exit;
1588 }
1589
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001590 oparms->fid->persistent_fid = rsp->PersistentFileId;
1591 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001592
1593 if (buf) {
1594 memcpy(buf, &rsp->CreationTime, 32);
1595 buf->AllocationSize = rsp->AllocationSize;
1596 buf->EndOfFile = rsp->EndofFile;
1597 buf->Attributes = rsp->FileAttributes;
1598 buf->NumberOfLinks = cpu_to_le32(1);
1599 buf->DeletePending = 0;
1600 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001601
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001602 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001603 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001604 else
1605 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001606creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001607 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001608 kfree(lc_buf);
1609 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001610 free_rsp_buf(resp_buftype, rsp);
1611 return rc;
1612}
1613
Steve French4a72daf2013-06-25 00:20:49 -05001614/*
1615 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1616 */
1617int
1618SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1619 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1620 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1621{
1622 struct smb2_ioctl_req *req;
1623 struct smb2_ioctl_rsp *rsp;
1624 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001625 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001626 struct kvec iov[2];
1627 int resp_buftype;
1628 int num_iovecs;
1629 int rc = 0;
1630
1631 cifs_dbg(FYI, "SMB2 IOCTL\n");
1632
Steve French3d1a3742014-08-11 21:05:25 -05001633 if (out_data != NULL)
1634 *out_data = NULL;
1635
Steve French4a72daf2013-06-25 00:20:49 -05001636 /* zero out returned data len, in case of error */
1637 if (plen)
1638 *plen = 0;
1639
Steve French8e353102015-03-26 19:47:02 -05001640 if (tcon)
1641 ses = tcon->ses;
1642 else
1643 return -EIO;
1644
Steve French4a72daf2013-06-25 00:20:49 -05001645 if (ses && (ses->server))
1646 server = ses->server;
1647 else
1648 return -EIO;
1649
1650 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1651 if (rc)
1652 return rc;
1653
1654 req->CtlCode = cpu_to_le32(opcode);
1655 req->PersistentFileId = persistent_fid;
1656 req->VolatileFileId = volatile_fid;
1657
1658 if (indatalen) {
1659 req->InputCount = cpu_to_le32(indatalen);
1660 /* do not set InputOffset if no input data */
1661 req->InputOffset =
1662 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1663 iov[1].iov_base = in_data;
1664 iov[1].iov_len = indatalen;
1665 num_iovecs = 2;
1666 } else
1667 num_iovecs = 1;
1668
1669 req->OutputOffset = 0;
1670 req->OutputCount = 0; /* MBZ */
1671
1672 /*
1673 * Could increase MaxOutputResponse, but that would require more
1674 * than one credit. Windows typically sets this smaller, but for some
1675 * ioctls it may be useful to allow server to send more. No point
1676 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001677 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1678 * (by default, note that it can be overridden to make max larger)
1679 * in responses (except for read responses which can be bigger.
1680 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001681 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001682 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001683
1684 if (is_fsctl)
1685 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1686 else
1687 req->Flags = 0;
1688
1689 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001690
Steve French7ff8d452013-10-14 00:44:19 -05001691 /*
1692 * If no input data, the size of ioctl struct in
1693 * protocol spec still includes a 1 byte data buffer,
1694 * but if input data passed to ioctl, we do not
1695 * want to double count this, so we do not send
1696 * the dummy one byte of data in iovec[0] if sending
1697 * input data (in iovec[1]). We also must add 4 bytes
1698 * in first iovec to allow for rfc1002 length field.
1699 */
1700
1701 if (indatalen) {
1702 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1703 inc_rfc1001_len(req, indatalen - 1);
1704 } else
1705 iov[0].iov_len = get_rfc1002_length(req) + 4;
1706
Steve Frenchfca16f92017-10-25 15:58:31 -05001707 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1708 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1709 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001710
1711 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1712 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1713
Steve French9bf0c9c2013-11-16 18:05:28 -06001714 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001715 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001716 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001717 } else if (rc == -EINVAL) {
1718 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1719 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001720 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001721 goto ioctl_exit;
1722 }
Steve French4a72daf2013-06-25 00:20:49 -05001723 }
1724
1725 /* check if caller wants to look at return data or just return rc */
1726 if ((plen == NULL) || (out_data == NULL))
1727 goto ioctl_exit;
1728
1729 *plen = le32_to_cpu(rsp->OutputCount);
1730
1731 /* We check for obvious errors in the output buffer length and offset */
1732 if (*plen == 0)
1733 goto ioctl_exit; /* server returned no data */
1734 else if (*plen > 0xFF00) {
1735 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1736 *plen = 0;
1737 rc = -EIO;
1738 goto ioctl_exit;
1739 }
1740
1741 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1742 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1743 le32_to_cpu(rsp->OutputOffset));
1744 *plen = 0;
1745 rc = -EIO;
1746 goto ioctl_exit;
1747 }
1748
1749 *out_data = kmalloc(*plen, GFP_KERNEL);
1750 if (*out_data == NULL) {
1751 rc = -ENOMEM;
1752 goto ioctl_exit;
1753 }
1754
Steve French373512e2015-12-18 13:05:30 -06001755 memcpy(*out_data,
1756 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001757 *plen);
1758ioctl_exit:
1759 free_rsp_buf(resp_buftype, rsp);
1760 return rc;
1761}
1762
Steve French64a5cfa2013-10-14 15:31:32 -05001763/*
1764 * Individual callers to ioctl worker function follow
1765 */
1766
1767int
1768SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1769 u64 persistent_fid, u64 volatile_fid)
1770{
1771 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001772 struct compress_ioctl fsctl_input;
1773 char *ret_data = NULL;
1774
1775 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001776 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001777
1778 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1779 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1780 (char *)&fsctl_input /* data input */,
1781 2 /* in data len */, &ret_data /* out data */, NULL);
1782
1783 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001784
1785 return rc;
1786}
1787
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001788int
1789SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1790 u64 persistent_fid, u64 volatile_fid)
1791{
1792 struct smb2_close_req *req;
1793 struct smb2_close_rsp *rsp;
1794 struct TCP_Server_Info *server;
1795 struct cifs_ses *ses = tcon->ses;
1796 struct kvec iov[1];
1797 int resp_buftype;
1798 int rc = 0;
1799
Joe Perchesf96637b2013-05-04 22:12:25 -05001800 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001801
1802 if (ses && (ses->server))
1803 server = ses->server;
1804 else
1805 return -EIO;
1806
1807 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1808 if (rc)
1809 return rc;
1810
1811 req->PersistentFileId = persistent_fid;
1812 req->VolatileFileId = volatile_fid;
1813
1814 iov[0].iov_base = (char *)req;
1815 /* 4 for rfc1002 length field */
1816 iov[0].iov_len = get_rfc1002_length(req) + 4;
1817
1818 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1819 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1820
1821 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001822 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001823 goto close_exit;
1824 }
1825
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001826 /* BB FIXME - decode close response, update inode for caching */
1827
1828close_exit:
1829 free_rsp_buf(resp_buftype, rsp);
1830 return rc;
1831}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001832
1833static int
1834validate_buf(unsigned int offset, unsigned int buffer_length,
1835 struct smb2_hdr *hdr, unsigned int min_buf_size)
1836
1837{
1838 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1839 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1840 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1841 char *end_of_buf = begin_of_buf + buffer_length;
1842
1843
1844 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001845 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1846 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001847 return -EINVAL;
1848 }
1849
1850 /* check if beyond RFC1001 maximum length */
1851 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001852 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1853 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001854 return -EINVAL;
1855 }
1856
1857 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001858 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001859 return -EINVAL;
1860 }
1861
1862 return 0;
1863}
1864
1865/*
1866 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1867 * Caller must free buffer.
1868 */
1869static int
1870validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1871 struct smb2_hdr *hdr, unsigned int minbufsize,
1872 char *data)
1873
1874{
1875 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1876 int rc;
1877
1878 if (!data)
1879 return -EINVAL;
1880
1881 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1882 if (rc)
1883 return rc;
1884
1885 memcpy(data, begin_of_buf, buffer_length);
1886
1887 return 0;
1888}
1889
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001890static int
1891query_info(const unsigned int xid, struct cifs_tcon *tcon,
1892 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1893 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001894{
1895 struct smb2_query_info_req *req;
1896 struct smb2_query_info_rsp *rsp = NULL;
1897 struct kvec iov[2];
1898 int rc = 0;
1899 int resp_buftype;
1900 struct TCP_Server_Info *server;
1901 struct cifs_ses *ses = tcon->ses;
1902
Joe Perchesf96637b2013-05-04 22:12:25 -05001903 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001904
1905 if (ses && (ses->server))
1906 server = ses->server;
1907 else
1908 return -EIO;
1909
1910 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1911 if (rc)
1912 return rc;
1913
1914 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001915 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001916 req->PersistentFileId = persistent_fid;
1917 req->VolatileFileId = volatile_fid;
1918 /* 4 for rfc1002 length field and 1 for Buffer */
1919 req->InputBufferOffset =
1920 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001921 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001922
1923 iov[0].iov_base = (char *)req;
1924 /* 4 for rfc1002 length field */
1925 iov[0].iov_len = get_rfc1002_length(req) + 4;
1926
1927 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001928 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1929
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001930 if (rc) {
1931 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1932 goto qinf_exit;
1933 }
1934
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001935 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1936 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001937 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001938
1939qinf_exit:
1940 free_rsp_buf(resp_buftype, rsp);
1941 return rc;
1942}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001943
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001944int
1945SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1946 u64 persistent_fid, u64 volatile_fid,
1947 struct smb2_file_all_info *data)
1948{
1949 return query_info(xid, tcon, persistent_fid, volatile_fid,
1950 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001951 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001952 sizeof(struct smb2_file_all_info), data);
1953}
1954
1955int
1956SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1957 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1958{
1959 return query_info(xid, tcon, persistent_fid, volatile_fid,
1960 FILE_INTERNAL_INFORMATION,
1961 sizeof(struct smb2_file_internal_info),
1962 sizeof(struct smb2_file_internal_info), uniqueid);
1963}
1964
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001965/*
1966 * This is a no-op for now. We're not really interested in the reply, but
1967 * rather in the fact that the server sent one and that server->lstrp
1968 * gets updated.
1969 *
1970 * FIXME: maybe we should consider checking that the reply matches request?
1971 */
1972static void
1973smb2_echo_callback(struct mid_q_entry *mid)
1974{
1975 struct TCP_Server_Info *server = mid->callback_data;
1976 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1977 unsigned int credits_received = 1;
1978
1979 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1980 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1981
Christopher Oo5fb4e282015-06-25 16:10:48 -07001982 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001983 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001984 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001985 add_credits(server, credits_received, CIFS_ECHO_OP);
1986}
1987
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001988void smb2_reconnect_server(struct work_struct *work)
1989{
1990 struct TCP_Server_Info *server = container_of(work,
1991 struct TCP_Server_Info, reconnect.work);
1992 struct cifs_ses *ses;
1993 struct cifs_tcon *tcon, *tcon2;
1994 struct list_head tmp_list;
1995 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01001996 int rc;
1997 int resched = false;
1998
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001999
2000 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2001 mutex_lock(&server->reconnect_mutex);
2002
2003 INIT_LIST_HEAD(&tmp_list);
2004 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2005
2006 spin_lock(&cifs_tcp_ses_lock);
2007 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2008 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002009 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002010 tcon->tc_count++;
2011 list_add_tail(&tcon->rlist, &tmp_list);
2012 tcon_exist = true;
2013 }
2014 }
2015 }
2016 /*
2017 * Get the reference to server struct to be sure that the last call of
2018 * cifs_put_tcon() in the loop below won't release the server pointer.
2019 */
2020 if (tcon_exist)
2021 server->srv_count++;
2022
2023 spin_unlock(&cifs_tcp_ses_lock);
2024
2025 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002026 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2027 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002028 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002029 else
2030 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002031 list_del_init(&tcon->rlist);
2032 cifs_put_tcon(tcon);
2033 }
2034
2035 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002036 if (resched)
2037 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002038 mutex_unlock(&server->reconnect_mutex);
2039
2040 /* now we can safely release srv struct */
2041 if (tcon_exist)
2042 cifs_put_tcp_session(server, 1);
2043}
2044
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002045int
2046SMB2_echo(struct TCP_Server_Info *server)
2047{
2048 struct smb2_echo_req *req;
2049 int rc = 0;
2050 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002051 struct smb_rqst rqst = { .rq_iov = &iov,
2052 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002053
Joe Perchesf96637b2013-05-04 22:12:25 -05002054 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002055
Steve French4fcd1812016-06-22 20:12:05 -05002056 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002057 /* No need to send echo on newly established connections */
2058 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2059 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002060 }
2061
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002062 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2063 if (rc)
2064 return rc;
2065
2066 req->hdr.CreditRequest = cpu_to_le16(1);
2067
2068 iov.iov_base = (char *)req;
2069 /* 4 for rfc1002 length field */
2070 iov.iov_len = get_rfc1002_length(req) + 4;
2071
Jeff Laytonfec344e2012-09-18 16:20:35 -07002072 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002073 CIFS_ECHO_OP);
2074 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002075 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002076
2077 cifs_small_buf_release(req);
2078 return rc;
2079}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002080
2081int
2082SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2083 u64 volatile_fid)
2084{
2085 struct smb2_flush_req *req;
2086 struct TCP_Server_Info *server;
2087 struct cifs_ses *ses = tcon->ses;
2088 struct kvec iov[1];
2089 int resp_buftype;
2090 int rc = 0;
2091
Joe Perchesf96637b2013-05-04 22:12:25 -05002092 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002093
2094 if (ses && (ses->server))
2095 server = ses->server;
2096 else
2097 return -EIO;
2098
2099 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2100 if (rc)
2101 return rc;
2102
2103 req->PersistentFileId = persistent_fid;
2104 req->VolatileFileId = volatile_fid;
2105
2106 iov[0].iov_base = (char *)req;
2107 /* 4 for rfc1002 length field */
2108 iov[0].iov_len = get_rfc1002_length(req) + 4;
2109
2110 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2111
Steve Frenchdfebe402015-03-27 01:00:06 -05002112 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002113 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2114
2115 free_rsp_buf(resp_buftype, iov[0].iov_base);
2116 return rc;
2117}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002118
2119/*
2120 * To form a chain of read requests, any read requests after the first should
2121 * have the end_of_chain boolean set to true.
2122 */
2123static int
2124smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2125 unsigned int remaining_bytes, int request_type)
2126{
2127 int rc = -EACCES;
2128 struct smb2_read_req *req = NULL;
2129
2130 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2131 if (rc)
2132 return rc;
2133 if (io_parms->tcon->ses->server == NULL)
2134 return -ECONNABORTED;
2135
2136 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2137
2138 req->PersistentFileId = io_parms->persistent_fid;
2139 req->VolatileFileId = io_parms->volatile_fid;
2140 req->ReadChannelInfoOffset = 0; /* reserved */
2141 req->ReadChannelInfoLength = 0; /* reserved */
2142 req->Channel = 0; /* reserved */
2143 req->MinimumCount = 0;
2144 req->Length = cpu_to_le32(io_parms->length);
2145 req->Offset = cpu_to_le64(io_parms->offset);
2146
2147 if (request_type & CHAINED_REQUEST) {
2148 if (!(request_type & END_OF_CHAIN)) {
2149 /* 4 for rfc1002 length field */
2150 req->hdr.NextCommand =
2151 cpu_to_le32(get_rfc1002_length(req) + 4);
2152 } else /* END_OF_CHAIN */
2153 req->hdr.NextCommand = 0;
2154 if (request_type & RELATED_REQUEST) {
2155 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2156 /*
2157 * Related requests use info from previous read request
2158 * in chain.
2159 */
2160 req->hdr.SessionId = 0xFFFFFFFF;
2161 req->hdr.TreeId = 0xFFFFFFFF;
2162 req->PersistentFileId = 0xFFFFFFFF;
2163 req->VolatileFileId = 0xFFFFFFFF;
2164 }
2165 }
2166 if (remaining_bytes > io_parms->length)
2167 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2168 else
2169 req->RemainingBytes = 0;
2170
2171 iov[0].iov_base = (char *)req;
2172 /* 4 for rfc1002 length field */
2173 iov[0].iov_len = get_rfc1002_length(req) + 4;
2174 return rc;
2175}
2176
2177static void
2178smb2_readv_callback(struct mid_q_entry *mid)
2179{
2180 struct cifs_readdata *rdata = mid->callback_data;
2181 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2182 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002183 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002184 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002185 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002186 .rq_nvec = 1,
2187 .rq_pages = rdata->pages,
2188 .rq_npages = rdata->nr_pages,
2189 .rq_pagesz = rdata->pagesz,
2190 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002191
Joe Perchesf96637b2013-05-04 22:12:25 -05002192 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2193 __func__, mid->mid, mid->mid_state, rdata->result,
2194 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002195
2196 switch (mid->mid_state) {
2197 case MID_RESPONSE_RECEIVED:
2198 credits_received = le16_to_cpu(buf->CreditRequest);
2199 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002200 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002201 int rc;
2202
Jeff Layton0b688cf2012-09-18 16:20:34 -07002203 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002204 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002205 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2206 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002207 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002208 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002209 task_io_account_read(rdata->got_bytes);
2210 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002211 break;
2212 case MID_REQUEST_SUBMITTED:
2213 case MID_RETRY_NEEDED:
2214 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002215 if (server->sign && rdata->got_bytes)
2216 /* reset bytes number since we can not check a sign */
2217 rdata->got_bytes = 0;
2218 /* FIXME: should this be counted toward the initiating task? */
2219 task_io_account_read(rdata->got_bytes);
2220 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002221 break;
2222 default:
2223 if (rdata->result != -ENODATA)
2224 rdata->result = -EIO;
2225 }
2226
2227 if (rdata->result)
2228 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2229
2230 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002231 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002232 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002233 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002234 add_credits(server, credits_received, 0);
2235}
2236
2237/* smb2_async_readv - send an async write, and set up mid to handle result */
2238int
2239smb2_async_readv(struct cifs_readdata *rdata)
2240{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002241 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002242 struct smb2_hdr *buf;
2243 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002244 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002245 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002246 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002247
Joe Perchesf96637b2013-05-04 22:12:25 -05002248 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2249 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002250
2251 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2252 io_parms.offset = rdata->offset;
2253 io_parms.length = rdata->bytes;
2254 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2255 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2256 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002257
2258 server = io_parms.tcon->ses->server;
2259
Jeff Layton58195752012-09-19 06:22:34 -07002260 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002261 if (rc) {
2262 if (rc == -EAGAIN && rdata->credits) {
2263 /* credits was reset by reconnect */
2264 rdata->credits = 0;
2265 /* reduce in_flight value since we won't send the req */
2266 spin_lock(&server->req_lock);
2267 server->in_flight--;
2268 spin_unlock(&server->req_lock);
2269 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002270 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002271 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002272
Jeff Layton58195752012-09-19 06:22:34 -07002273 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002274 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002275 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002276
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002277 if (rdata->credits) {
2278 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2279 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002280 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002281 spin_lock(&server->req_lock);
2282 server->credits += rdata->credits -
2283 le16_to_cpu(buf->CreditCharge);
2284 spin_unlock(&server->req_lock);
2285 wake_up(&server->request_q);
2286 flags = CIFS_HAS_CREDITS;
2287 }
2288
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002289 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002290 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002291 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002292 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002293 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002294 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002295 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2296 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002297
2298 cifs_small_buf_release(buf);
2299 return rc;
2300}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002301
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002302int
2303SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2304 unsigned int *nbytes, char **buf, int *buf_type)
2305{
2306 int resp_buftype, rc = -EACCES;
2307 struct smb2_read_rsp *rsp = NULL;
2308 struct kvec iov[1];
2309
2310 *nbytes = 0;
2311 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2312 if (rc)
2313 return rc;
2314
2315 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2316 &resp_buftype, CIFS_LOG_ERROR);
2317
2318 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2319
2320 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2321 free_rsp_buf(resp_buftype, iov[0].iov_base);
2322 return 0;
2323 }
2324
2325 if (rc) {
2326 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002327 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002328 } else {
2329 *nbytes = le32_to_cpu(rsp->DataLength);
2330 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2331 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002332 cifs_dbg(FYI, "bad length %d for count %d\n",
2333 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002334 rc = -EIO;
2335 *nbytes = 0;
2336 }
2337 }
2338
2339 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002340 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002341 *nbytes);
2342 free_rsp_buf(resp_buftype, iov[0].iov_base);
2343 } else if (resp_buftype != CIFS_NO_BUFFER) {
2344 *buf = iov[0].iov_base;
2345 if (resp_buftype == CIFS_SMALL_BUFFER)
2346 *buf_type = CIFS_SMALL_BUFFER;
2347 else if (resp_buftype == CIFS_LARGE_BUFFER)
2348 *buf_type = CIFS_LARGE_BUFFER;
2349 }
2350 return rc;
2351}
2352
Pavel Shilovsky33319142012-09-18 16:20:29 -07002353/*
2354 * Check the mid_state and signature on received buffer (if any), and queue the
2355 * workqueue completion task.
2356 */
2357static void
2358smb2_writev_callback(struct mid_q_entry *mid)
2359{
2360 struct cifs_writedata *wdata = mid->callback_data;
2361 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002362 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002363 unsigned int written;
2364 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2365 unsigned int credits_received = 1;
2366
2367 switch (mid->mid_state) {
2368 case MID_RESPONSE_RECEIVED:
2369 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2370 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2371 if (wdata->result != 0)
2372 break;
2373
2374 written = le32_to_cpu(rsp->DataLength);
2375 /*
2376 * Mask off high 16 bits when bytes written as returned
2377 * by the server is greater than bytes requested by the
2378 * client. OS/2 servers are known to set incorrect
2379 * CountHigh values.
2380 */
2381 if (written > wdata->bytes)
2382 written &= 0xFFFF;
2383
2384 if (written < wdata->bytes)
2385 wdata->result = -ENOSPC;
2386 else
2387 wdata->bytes = written;
2388 break;
2389 case MID_REQUEST_SUBMITTED:
2390 case MID_RETRY_NEEDED:
2391 wdata->result = -EAGAIN;
2392 break;
2393 default:
2394 wdata->result = -EIO;
2395 break;
2396 }
2397
2398 if (wdata->result)
2399 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2400
2401 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002402 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002403 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002404 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002405 add_credits(tcon->ses->server, credits_received, 0);
2406}
2407
2408/* smb2_async_writev - send an async write, and set up mid to handle result */
2409int
Steve French4a5c80d2014-02-07 20:45:12 -06002410smb2_async_writev(struct cifs_writedata *wdata,
2411 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002412{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002413 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002414 struct smb2_write_req *req = NULL;
2415 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002416 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002417 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002418 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002419
2420 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002421 if (rc) {
2422 if (rc == -EAGAIN && wdata->credits) {
2423 /* credits was reset by reconnect */
2424 wdata->credits = 0;
2425 /* reduce in_flight value since we won't send the req */
2426 spin_lock(&server->req_lock);
2427 server->in_flight--;
2428 spin_unlock(&server->req_lock);
2429 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002430 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002431 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002432
Pavel Shilovsky33319142012-09-18 16:20:29 -07002433 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2434
2435 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2436 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2437 req->WriteChannelInfoOffset = 0;
2438 req->WriteChannelInfoLength = 0;
2439 req->Channel = 0;
2440 req->Offset = cpu_to_le64(wdata->offset);
2441 /* 4 for rfc1002 length field */
2442 req->DataOffset = cpu_to_le16(
2443 offsetof(struct smb2_write_req, Buffer) - 4);
2444 req->RemainingBytes = 0;
2445
2446 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002447 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2448 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002449
Jeff Laytoneddb0792012-09-18 16:20:35 -07002450 rqst.rq_iov = &iov;
2451 rqst.rq_nvec = 1;
2452 rqst.rq_pages = wdata->pages;
2453 rqst.rq_npages = wdata->nr_pages;
2454 rqst.rq_pagesz = wdata->pagesz;
2455 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002456
Joe Perchesf96637b2013-05-04 22:12:25 -05002457 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2458 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002459
2460 req->Length = cpu_to_le32(wdata->bytes);
2461
2462 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2463
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002464 if (wdata->credits) {
2465 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2466 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002467 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002468 spin_lock(&server->req_lock);
2469 server->credits += wdata->credits -
2470 le16_to_cpu(req->hdr.CreditCharge);
2471 spin_unlock(&server->req_lock);
2472 wake_up(&server->request_q);
2473 flags = CIFS_HAS_CREDITS;
2474 }
2475
Pavel Shilovsky33319142012-09-18 16:20:29 -07002476 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002477 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2478 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002479
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002480 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002481 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002482 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2483 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002484
Pavel Shilovsky33319142012-09-18 16:20:29 -07002485async_writev_out:
2486 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002487 return rc;
2488}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002489
2490/*
2491 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2492 * The length field from io_parms must be at least 1 and indicates a number of
2493 * elements with data to write that begins with position 1 in iov array. All
2494 * data length is specified by count.
2495 */
2496int
2497SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2498 unsigned int *nbytes, struct kvec *iov, int n_vec)
2499{
2500 int rc = 0;
2501 struct smb2_write_req *req = NULL;
2502 struct smb2_write_rsp *rsp = NULL;
2503 int resp_buftype;
2504 *nbytes = 0;
2505
2506 if (n_vec < 1)
2507 return rc;
2508
2509 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2510 if (rc)
2511 return rc;
2512
2513 if (io_parms->tcon->ses->server == NULL)
2514 return -ECONNABORTED;
2515
2516 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2517
2518 req->PersistentFileId = io_parms->persistent_fid;
2519 req->VolatileFileId = io_parms->volatile_fid;
2520 req->WriteChannelInfoOffset = 0;
2521 req->WriteChannelInfoLength = 0;
2522 req->Channel = 0;
2523 req->Length = cpu_to_le32(io_parms->length);
2524 req->Offset = cpu_to_le64(io_parms->offset);
2525 /* 4 for rfc1002 length field */
2526 req->DataOffset = cpu_to_le16(
2527 offsetof(struct smb2_write_req, Buffer) - 4);
2528 req->RemainingBytes = 0;
2529
2530 iov[0].iov_base = (char *)req;
2531 /* 4 for rfc1002 length field and 1 for Buffer */
2532 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2533
2534 /* length of entire message including data to be written */
2535 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2536
2537 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2538 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002539 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002540
2541 if (rc) {
2542 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002543 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002544 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002545 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002546
2547 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002548 return rc;
2549}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002550
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002551static unsigned int
2552num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2553{
2554 int len;
2555 unsigned int entrycount = 0;
2556 unsigned int next_offset = 0;
2557 FILE_DIRECTORY_INFO *entryptr;
2558
2559 if (bufstart == NULL)
2560 return 0;
2561
2562 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2563
2564 while (1) {
2565 entryptr = (FILE_DIRECTORY_INFO *)
2566 ((char *)entryptr + next_offset);
2567
2568 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002569 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002570 break;
2571 }
2572
2573 len = le32_to_cpu(entryptr->FileNameLength);
2574 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002575 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2576 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002577 break;
2578 }
2579
2580 *lastentry = (char *)entryptr;
2581 entrycount++;
2582
2583 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2584 if (!next_offset)
2585 break;
2586 }
2587
2588 return entrycount;
2589}
2590
2591/*
2592 * Readdir/FindFirst
2593 */
2594int
2595SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2596 u64 persistent_fid, u64 volatile_fid, int index,
2597 struct cifs_search_info *srch_inf)
2598{
2599 struct smb2_query_directory_req *req;
2600 struct smb2_query_directory_rsp *rsp = NULL;
2601 struct kvec iov[2];
2602 int rc = 0;
2603 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002604 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002605 unsigned char *bufptr;
2606 struct TCP_Server_Info *server;
2607 struct cifs_ses *ses = tcon->ses;
2608 __le16 asteriks = cpu_to_le16('*');
2609 char *end_of_smb;
2610 unsigned int output_size = CIFSMaxBufSize;
2611 size_t info_buf_size;
2612
2613 if (ses && (ses->server))
2614 server = ses->server;
2615 else
2616 return -EIO;
2617
2618 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2619 if (rc)
2620 return rc;
2621
2622 switch (srch_inf->info_level) {
2623 case SMB_FIND_FILE_DIRECTORY_INFO:
2624 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2625 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2626 break;
2627 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2628 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2629 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2630 break;
2631 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002632 cifs_dbg(VFS, "info level %u isn't supported\n",
2633 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002634 rc = -EINVAL;
2635 goto qdir_exit;
2636 }
2637
2638 req->FileIndex = cpu_to_le32(index);
2639 req->PersistentFileId = persistent_fid;
2640 req->VolatileFileId = volatile_fid;
2641
2642 len = 0x2;
2643 bufptr = req->Buffer;
2644 memcpy(bufptr, &asteriks, len);
2645
2646 req->FileNameOffset =
2647 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2648 req->FileNameLength = cpu_to_le16(len);
2649 /*
2650 * BB could be 30 bytes or so longer if we used SMB2 specific
2651 * buffer lengths, but this is safe and close enough.
2652 */
2653 output_size = min_t(unsigned int, output_size, server->maxBuf);
2654 output_size = min_t(unsigned int, output_size, 2 << 15);
2655 req->OutputBufferLength = cpu_to_le32(output_size);
2656
2657 iov[0].iov_base = (char *)req;
2658 /* 4 for RFC1001 length and 1 for Buffer */
2659 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2660
2661 iov[1].iov_base = (char *)(req->Buffer);
2662 iov[1].iov_len = len;
2663
2664 inc_rfc1001_len(req, len - 1 /* Buffer */);
2665
2666 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002667 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2668
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002669 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002670 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2671 srch_inf->endOfSearch = true;
2672 rc = 0;
2673 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002674 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2675 goto qdir_exit;
2676 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002677
2678 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2679 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2680 info_buf_size);
2681 if (rc)
2682 goto qdir_exit;
2683
2684 srch_inf->unicode = true;
2685
2686 if (srch_inf->ntwrk_buf_start) {
2687 if (srch_inf->smallBuf)
2688 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2689 else
2690 cifs_buf_release(srch_inf->ntwrk_buf_start);
2691 }
2692 srch_inf->ntwrk_buf_start = (char *)rsp;
2693 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2694 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2695 /* 4 for rfc1002 length field */
2696 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2697 srch_inf->entries_in_buffer =
2698 num_entries(srch_inf->srch_entries_start, end_of_smb,
2699 &srch_inf->last_entry, info_buf_size);
2700 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002701 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2702 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2703 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002704 if (resp_buftype == CIFS_LARGE_BUFFER)
2705 srch_inf->smallBuf = false;
2706 else if (resp_buftype == CIFS_SMALL_BUFFER)
2707 srch_inf->smallBuf = true;
2708 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002709 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002710
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002711 return rc;
2712
2713qdir_exit:
2714 free_rsp_buf(resp_buftype, rsp);
2715 return rc;
2716}
2717
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002718static int
2719send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002720 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002721 unsigned int num, void **data, unsigned int *size)
2722{
2723 struct smb2_set_info_req *req;
2724 struct smb2_set_info_rsp *rsp = NULL;
2725 struct kvec *iov;
2726 int rc = 0;
2727 int resp_buftype;
2728 unsigned int i;
2729 struct TCP_Server_Info *server;
2730 struct cifs_ses *ses = tcon->ses;
2731
2732 if (ses && (ses->server))
2733 server = ses->server;
2734 else
2735 return -EIO;
2736
2737 if (!num)
2738 return -EINVAL;
2739
2740 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2741 if (!iov)
2742 return -ENOMEM;
2743
2744 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2745 if (rc) {
2746 kfree(iov);
2747 return rc;
2748 }
2749
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002750 req->hdr.ProcessId = cpu_to_le32(pid);
2751
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002752 req->InfoType = SMB2_O_INFO_FILE;
2753 req->FileInfoClass = info_class;
2754 req->PersistentFileId = persistent_fid;
2755 req->VolatileFileId = volatile_fid;
2756
2757 /* 4 for RFC1001 length and 1 for Buffer */
2758 req->BufferOffset =
2759 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2760 req->BufferLength = cpu_to_le32(*size);
2761
2762 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2763
2764 memcpy(req->Buffer, *data, *size);
2765
2766 iov[0].iov_base = (char *)req;
2767 /* 4 for RFC1001 length */
2768 iov[0].iov_len = get_rfc1002_length(req) + 4;
2769
2770 for (i = 1; i < num; i++) {
2771 inc_rfc1001_len(req, size[i]);
2772 le32_add_cpu(&req->BufferLength, size[i]);
2773 iov[i].iov_base = (char *)data[i];
2774 iov[i].iov_len = size[i];
2775 }
2776
2777 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2778 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2779
Steve French7d3fb242013-11-18 09:56:28 -06002780 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002781 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002782
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002783 free_rsp_buf(resp_buftype, rsp);
2784 kfree(iov);
2785 return rc;
2786}
2787
2788int
2789SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2790 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2791{
2792 struct smb2_file_rename_info info;
2793 void **data;
2794 unsigned int size[2];
2795 int rc;
2796 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2797
2798 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2799 if (!data)
2800 return -ENOMEM;
2801
2802 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2803 /* 0 = fail if target already exists */
2804 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2805 info.FileNameLength = cpu_to_le32(len);
2806
2807 data[0] = &info;
2808 size[0] = sizeof(struct smb2_file_rename_info);
2809
2810 data[1] = target_file;
2811 size[1] = len + 2 /* null */;
2812
2813 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002814 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2815 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002816 kfree(data);
2817 return rc;
2818}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002819
2820int
Steve French897fba12016-05-12 21:20:36 -05002821SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2822 u64 persistent_fid, u64 volatile_fid)
2823{
2824 __u8 delete_pending = 1;
2825 void *data;
2826 unsigned int size;
2827
2828 data = &delete_pending;
2829 size = 1; /* sizeof __u8 */
2830
2831 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2832 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2833 &size);
2834}
2835
2836int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002837SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2838 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2839{
2840 struct smb2_file_link_info info;
2841 void **data;
2842 unsigned int size[2];
2843 int rc;
2844 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2845
2846 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2847 if (!data)
2848 return -ENOMEM;
2849
2850 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2851 /* 0 = fail if link already exists */
2852 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2853 info.FileNameLength = cpu_to_le32(len);
2854
2855 data[0] = &info;
2856 size[0] = sizeof(struct smb2_file_link_info);
2857
2858 data[1] = target_file;
2859 size[1] = len + 2 /* null */;
2860
2861 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002862 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002863 kfree(data);
2864 return rc;
2865}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002866
2867int
2868SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002869 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002870{
2871 struct smb2_file_eof_info info;
2872 void *data;
2873 unsigned int size;
2874
2875 info.EndOfFile = *eof;
2876
2877 data = &info;
2878 size = sizeof(struct smb2_file_eof_info);
2879
Steve Frenchf29ebb42014-07-19 21:44:58 -05002880 if (is_falloc)
2881 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2882 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2883 else
2884 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2885 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002886}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002887
2888int
2889SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2890 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2891{
2892 unsigned int size;
2893 size = sizeof(FILE_BASIC_INFO);
2894 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2895 current->tgid, FILE_BASIC_INFORMATION, 1,
2896 (void **)&buf, &size);
2897}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002898
2899int
2900SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2901 const u64 persistent_fid, const u64 volatile_fid,
2902 __u8 oplock_level)
2903{
2904 int rc;
2905 struct smb2_oplock_break *req = NULL;
2906
Joe Perchesf96637b2013-05-04 22:12:25 -05002907 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002908 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2909
2910 if (rc)
2911 return rc;
2912
2913 req->VolatileFid = volatile_fid;
2914 req->PersistentFid = persistent_fid;
2915 req->OplockLevel = oplock_level;
2916 req->hdr.CreditRequest = cpu_to_le16(1);
2917
2918 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2919 /* SMB2 buffer freed by function above */
2920
2921 if (rc) {
2922 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002923 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002924 }
2925
2926 return rc;
2927}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002928
2929static void
2930copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2931 struct kstatfs *kst)
2932{
2933 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2934 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2935 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302936 kst->f_bfree = kst->f_bavail =
2937 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002938 return;
2939}
2940
2941static int
2942build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2943 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2944{
2945 int rc;
2946 struct smb2_query_info_req *req;
2947
Joe Perchesf96637b2013-05-04 22:12:25 -05002948 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002949
2950 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2951 return -EIO;
2952
2953 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2954 if (rc)
2955 return rc;
2956
2957 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2958 req->FileInfoClass = level;
2959 req->PersistentFileId = persistent_fid;
2960 req->VolatileFileId = volatile_fid;
2961 /* 4 for rfc1002 length field and 1 for pad */
2962 req->InputBufferOffset =
2963 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2964 req->OutputBufferLength = cpu_to_le32(
2965 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2966
2967 iov->iov_base = (char *)req;
2968 /* 4 for rfc1002 length field */
2969 iov->iov_len = get_rfc1002_length(req) + 4;
2970 return 0;
2971}
2972
2973int
2974SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2975 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2976{
2977 struct smb2_query_info_rsp *rsp = NULL;
2978 struct kvec iov;
2979 int rc = 0;
2980 int resp_buftype;
2981 struct cifs_ses *ses = tcon->ses;
2982 struct smb2_fs_full_size_info *info = NULL;
2983
2984 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2985 sizeof(struct smb2_fs_full_size_info),
2986 persistent_fid, volatile_fid);
2987 if (rc)
2988 return rc;
2989
2990 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2991 if (rc) {
2992 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002993 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002994 }
2995 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2996
2997 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2998 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2999 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3000 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3001 sizeof(struct smb2_fs_full_size_info));
3002 if (!rc)
3003 copy_fs_info_to_kstatfs(info, fsdata);
3004
Steve French34f62642013-10-09 02:07:00 -05003005qfsinf_exit:
3006 free_rsp_buf(resp_buftype, iov.iov_base);
3007 return rc;
3008}
3009
3010int
3011SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003012 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003013{
3014 struct smb2_query_info_rsp *rsp = NULL;
3015 struct kvec iov;
3016 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003017 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003018 struct cifs_ses *ses = tcon->ses;
3019 unsigned int rsp_len, offset;
3020
Steven French21671142013-10-09 13:36:35 -05003021 if (level == FS_DEVICE_INFORMATION) {
3022 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3023 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3024 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3025 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3026 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003027 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3028 max_len = sizeof(struct smb3_fs_ss_info);
3029 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003030 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003031 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003032 return -EINVAL;
3033 }
3034
3035 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003036 persistent_fid, volatile_fid);
3037 if (rc)
3038 return rc;
3039
3040 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3041 if (rc) {
3042 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3043 goto qfsattr_exit;
3044 }
3045 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3046
3047 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3048 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003049 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3050 if (rc)
3051 goto qfsattr_exit;
3052
3053 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003054 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3055 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003056 rsp_len, max_len));
3057 else if (level == FS_DEVICE_INFORMATION)
3058 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3059 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003060 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3061 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3062 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3063 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3064 tcon->perf_sector_size =
3065 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3066 }
Steve French34f62642013-10-09 02:07:00 -05003067
3068qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003069 free_rsp_buf(resp_buftype, iov.iov_base);
3070 return rc;
3071}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003072
3073int
3074smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3075 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3076 const __u32 num_lock, struct smb2_lock_element *buf)
3077{
3078 int rc = 0;
3079 struct smb2_lock_req *req = NULL;
3080 struct kvec iov[2];
3081 int resp_buf_type;
3082 unsigned int count;
3083
Joe Perchesf96637b2013-05-04 22:12:25 -05003084 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003085
3086 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3087 if (rc)
3088 return rc;
3089
3090 req->hdr.ProcessId = cpu_to_le32(pid);
3091 req->LockCount = cpu_to_le16(num_lock);
3092
3093 req->PersistentFileId = persist_fid;
3094 req->VolatileFileId = volatile_fid;
3095
3096 count = num_lock * sizeof(struct smb2_lock_element);
3097 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3098
3099 iov[0].iov_base = (char *)req;
3100 /* 4 for rfc1002 length field and count for all locks */
3101 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3102 iov[1].iov_base = (char *)buf;
3103 iov[1].iov_len = count;
3104
3105 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3106 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3107 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003108 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003109 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3110 }
3111
3112 return rc;
3113}
3114
3115int
3116SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3117 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3118 const __u64 length, const __u64 offset, const __u32 lock_flags,
3119 const bool wait)
3120{
3121 struct smb2_lock_element lock;
3122
3123 lock.Offset = cpu_to_le64(offset);
3124 lock.Length = cpu_to_le64(length);
3125 lock.Flags = cpu_to_le32(lock_flags);
3126 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3127 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3128
3129 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3130}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003131
3132int
3133SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3134 __u8 *lease_key, const __le32 lease_state)
3135{
3136 int rc;
3137 struct smb2_lease_ack *req = NULL;
3138
Joe Perchesf96637b2013-05-04 22:12:25 -05003139 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003140 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3141
3142 if (rc)
3143 return rc;
3144
3145 req->hdr.CreditRequest = cpu_to_le16(1);
3146 req->StructureSize = cpu_to_le16(36);
3147 inc_rfc1001_len(req, 12);
3148
3149 memcpy(req->LeaseKey, lease_key, 16);
3150 req->LeaseState = lease_state;
3151
3152 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3153 /* SMB2 buffer freed by function above */
3154
3155 if (rc) {
3156 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003157 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003158 }
3159
3160 return rc;
3161}