blob: 69b610ad3fdc536c5f8befff05b01e9228850822 [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 */
588 if (pneg_rsp->Dialect !=
589 cpu_to_le16(tcon->ses->server->vals->protocol_id))
590 goto vneg_out;
591
592 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
593 goto vneg_out;
594
595 /* do not validate server guid because not saved at negprot time yet */
596
597 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
598 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
599 goto vneg_out;
600
601 /* validate negotiate successful */
602 cifs_dbg(FYI, "validate negotiate info successful\n");
603 return 0;
604
605vneg_out:
606 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
607 return -EIO;
608}
609
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100610struct SMB2_sess_data {
611 unsigned int xid;
612 struct cifs_ses *ses;
613 struct nls_table *nls_cp;
614 void (*func)(struct SMB2_sess_data *);
615 int result;
616 u64 previous_session;
617
618 /* we will send the SMB in three pieces:
619 * a fixed length beginning part, an optional
620 * SPNEGO blob (which can be zero length), and a
621 * last part which will include the strings
622 * and rest of bcc area. This allows us to avoid
623 * a large buffer 17K allocation
624 */
625 int buf0_type;
626 struct kvec iov[2];
627};
628
629static int
630SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
631{
632 int rc;
633 struct cifs_ses *ses = sess_data->ses;
634 struct smb2_sess_setup_req *req;
635 struct TCP_Server_Info *server = ses->server;
636
637 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
638 if (rc)
639 return rc;
640
641 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
642
643 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
644 req->PreviousSessionId = sess_data->previous_session;
645
646 req->Flags = 0; /* MBZ */
647 /* to enable echos and oplocks */
648 req->hdr.CreditRequest = cpu_to_le16(3);
649
650 /* only one of SMB2 signing flags may be set in SMB2 request */
651 if (server->sign)
652 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
653 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
654 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
655 else
656 req->SecurityMode = 0;
657
658 req->Capabilities = 0;
659 req->Channel = 0; /* MBZ */
660
661 sess_data->iov[0].iov_base = (char *)req;
662 /* 4 for rfc1002 length field and 1 for pad */
663 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
664 /*
665 * This variable will be used to clear the buffer
666 * allocated above in case of any error in the calling function.
667 */
668 sess_data->buf0_type = CIFS_SMALL_BUFFER;
669
670 return 0;
671}
672
673static void
674SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
675{
676 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
677 sess_data->buf0_type = CIFS_NO_BUFFER;
678}
679
680static int
681SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
682{
683 int rc;
684 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
685
686 /* Testing shows that buffer offset must be at location of Buffer[0] */
687 req->SecurityBufferOffset =
688 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
689 1 /* pad */ - 4 /* rfc1001 len */);
690 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
691
692 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
693
694 /* BB add code to build os and lm fields */
695
696 rc = SendReceive2(sess_data->xid, sess_data->ses,
697 sess_data->iov, 2,
698 &sess_data->buf0_type,
699 CIFS_LOG_ERROR | CIFS_NEG_OP);
700
701 return rc;
702}
703
704static int
705SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
706{
707 int rc = 0;
708 struct cifs_ses *ses = sess_data->ses;
709
710 mutex_lock(&ses->server->srv_mutex);
711 if (ses->server->sign && ses->server->ops->generate_signingkey) {
712 rc = ses->server->ops->generate_signingkey(ses);
713 kfree(ses->auth_key.response);
714 ses->auth_key.response = NULL;
715 if (rc) {
716 cifs_dbg(FYI,
717 "SMB3 session key generation failed\n");
718 mutex_unlock(&ses->server->srv_mutex);
719 goto keygen_exit;
720 }
721 }
722 if (!ses->server->session_estab) {
723 ses->server->sequence_number = 0x2;
724 ses->server->session_estab = true;
725 }
726 mutex_unlock(&ses->server->srv_mutex);
727
728 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
729 spin_lock(&GlobalMid_Lock);
730 ses->status = CifsGood;
731 ses->need_reconnect = false;
732 spin_unlock(&GlobalMid_Lock);
733
734keygen_exit:
735 if (!ses->server->sign) {
736 kfree(ses->auth_key.response);
737 ses->auth_key.response = NULL;
738 }
739 return rc;
740}
741
742#ifdef CONFIG_CIFS_UPCALL
743static void
744SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
745{
746 int rc;
747 struct cifs_ses *ses = sess_data->ses;
748 struct cifs_spnego_msg *msg;
749 struct key *spnego_key = NULL;
750 struct smb2_sess_setup_rsp *rsp = NULL;
751
752 rc = SMB2_sess_alloc_buffer(sess_data);
753 if (rc)
754 goto out;
755
756 spnego_key = cifs_get_spnego_key(ses);
757 if (IS_ERR(spnego_key)) {
758 rc = PTR_ERR(spnego_key);
759 spnego_key = NULL;
760 goto out;
761 }
762
763 msg = spnego_key->payload.data[0];
764 /*
765 * check version field to make sure that cifs.upcall is
766 * sending us a response in an expected form
767 */
768 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
769 cifs_dbg(VFS,
770 "bad cifs.upcall version. Expected %d got %d",
771 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
772 rc = -EKEYREJECTED;
773 goto out_put_spnego_key;
774 }
775
776 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
777 GFP_KERNEL);
778 if (!ses->auth_key.response) {
779 cifs_dbg(VFS,
780 "Kerberos can't allocate (%u bytes) memory",
781 msg->sesskey_len);
782 rc = -ENOMEM;
783 goto out_put_spnego_key;
784 }
785 ses->auth_key.len = msg->sesskey_len;
786
787 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
788 sess_data->iov[1].iov_len = msg->secblob_len;
789
790 rc = SMB2_sess_sendreceive(sess_data);
791 if (rc)
792 goto out_put_spnego_key;
793
794 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
795 ses->Suid = rsp->hdr.SessionId;
796
797 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
798 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
799 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
800
801 rc = SMB2_sess_establish_session(sess_data);
802out_put_spnego_key:
803 key_invalidate(spnego_key);
804 key_put(spnego_key);
805out:
806 sess_data->result = rc;
807 sess_data->func = NULL;
808 SMB2_sess_free_buffer(sess_data);
809}
810#else
811static void
812SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
813{
814 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
815 sess_data->result = -EOPNOTSUPP;
816 sess_data->func = NULL;
817}
818#endif
819
Sachin Prabhu166cea42016-10-07 19:11:22 +0100820static void
821SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
822
823static void
824SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
825{
826 int rc;
827 struct cifs_ses *ses = sess_data->ses;
828 struct smb2_sess_setup_rsp *rsp = NULL;
829 char *ntlmssp_blob = NULL;
830 bool use_spnego = false; /* else use raw ntlmssp */
831 u16 blob_length = 0;
832
833 /*
834 * If memory allocation is successful, caller of this function
835 * frees it.
836 */
837 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
838 if (!ses->ntlmssp) {
839 rc = -ENOMEM;
840 goto out_err;
841 }
842 ses->ntlmssp->sesskey_per_smbsess = true;
843
844 rc = SMB2_sess_alloc_buffer(sess_data);
845 if (rc)
846 goto out_err;
847
848 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
849 GFP_KERNEL);
850 if (ntlmssp_blob == NULL) {
851 rc = -ENOMEM;
852 goto out;
853 }
854
855 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
856 if (use_spnego) {
857 /* BB eventually need to add this */
858 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
859 rc = -EOPNOTSUPP;
860 goto out;
861 } else {
862 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
863 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
864 }
865 sess_data->iov[1].iov_base = ntlmssp_blob;
866 sess_data->iov[1].iov_len = blob_length;
867
868 rc = SMB2_sess_sendreceive(sess_data);
869 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
870
871 /* If true, rc here is expected and not an error */
872 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
873 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
874 rc = 0;
875
876 if (rc)
877 goto out;
878
879 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
880 le16_to_cpu(rsp->SecurityBufferOffset)) {
881 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
882 le16_to_cpu(rsp->SecurityBufferOffset));
883 rc = -EIO;
884 goto out;
885 }
886 rc = decode_ntlmssp_challenge(rsp->Buffer,
887 le16_to_cpu(rsp->SecurityBufferLength), ses);
888 if (rc)
889 goto out;
890
891 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
892
893
894 ses->Suid = rsp->hdr.SessionId;
895 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
896 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
897 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
898
899out:
900 kfree(ntlmssp_blob);
901 SMB2_sess_free_buffer(sess_data);
902 if (!rc) {
903 sess_data->result = 0;
904 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
905 return;
906 }
907out_err:
908 kfree(ses->ntlmssp);
909 ses->ntlmssp = NULL;
910 sess_data->result = rc;
911 sess_data->func = NULL;
912}
913
914static void
915SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
916{
917 int rc;
918 struct cifs_ses *ses = sess_data->ses;
919 struct smb2_sess_setup_req *req;
920 struct smb2_sess_setup_rsp *rsp = NULL;
921 unsigned char *ntlmssp_blob = NULL;
922 bool use_spnego = false; /* else use raw ntlmssp */
923 u16 blob_length = 0;
924
925 rc = SMB2_sess_alloc_buffer(sess_data);
926 if (rc)
927 goto out;
928
929 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
930 req->hdr.SessionId = ses->Suid;
931
932 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
933 sess_data->nls_cp);
934 if (rc) {
935 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
936 goto out;
937 }
938
939 if (use_spnego) {
940 /* BB eventually need to add this */
941 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
942 rc = -EOPNOTSUPP;
943 goto out;
944 }
945 sess_data->iov[1].iov_base = ntlmssp_blob;
946 sess_data->iov[1].iov_len = blob_length;
947
948 rc = SMB2_sess_sendreceive(sess_data);
949 if (rc)
950 goto out;
951
952 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
953
954 ses->Suid = rsp->hdr.SessionId;
955 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
956 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
957 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
958
959 rc = SMB2_sess_establish_session(sess_data);
960out:
961 kfree(ntlmssp_blob);
962 SMB2_sess_free_buffer(sess_data);
963 kfree(ses->ntlmssp);
964 ses->ntlmssp = NULL;
965 sess_data->result = rc;
966 sess_data->func = NULL;
967}
968
969static int
970SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
971{
972 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
973 ses->sectype = RawNTLMSSP;
974
975 switch (ses->sectype) {
976 case Kerberos:
977 sess_data->func = SMB2_auth_kerberos;
978 break;
979 case RawNTLMSSP:
980 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
981 break;
982 default:
983 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
984 return -EOPNOTSUPP;
985 }
986
987 return 0;
988}
989
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400990int
991SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
992 const struct nls_table *nls_cp)
993{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400994 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400995 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100996 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400997
Joe Perchesf96637b2013-05-04 22:12:25 -0500998 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400999
Jeff Layton3534b852013-05-24 07:41:01 -04001000 if (!server) {
1001 WARN(1, "%s: server is NULL!\n", __func__);
1002 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001003 }
1004
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001005 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1006 if (!sess_data)
1007 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001008
1009 rc = SMB2_select_sec(ses, sess_data);
1010 if (rc)
1011 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001012 sess_data->xid = xid;
1013 sess_data->ses = ses;
1014 sess_data->buf0_type = CIFS_NO_BUFFER;
1015 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001016
Sachin Prabhu166cea42016-10-07 19:11:22 +01001017 while (sess_data->func)
1018 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001019
Steve Frenchdf1be202017-09-19 18:40:03 -05001020 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1021 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001022 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001023out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001024 kfree(sess_data);
1025 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001026}
1027
1028int
1029SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1030{
1031 struct smb2_logoff_req *req; /* response is also trivial struct */
1032 int rc = 0;
1033 struct TCP_Server_Info *server;
1034
Joe Perchesf96637b2013-05-04 22:12:25 -05001035 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001036
1037 if (ses && (ses->server))
1038 server = ses->server;
1039 else
1040 return -EIO;
1041
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001042 /* no need to send SMB logoff if uid already closed due to reconnect */
1043 if (ses->need_reconnect)
1044 goto smb2_session_already_dead;
1045
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001046 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1047 if (rc)
1048 return rc;
1049
1050 /* since no tcon, smb2_init can not do this, so do here */
1051 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001052 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001053 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001054
1055 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1056 /*
1057 * No tcon so can't do
1058 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1059 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001060
1061smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001062 return rc;
1063}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001064
1065static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1066{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001067 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001068}
1069
1070#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1071
Steve Frenchde9f68d2013-11-15 11:26:24 -06001072/* These are similar values to what Windows uses */
1073static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1074{
1075 tcon->max_chunks = 256;
1076 tcon->max_bytes_chunk = 1048576;
1077 tcon->max_bytes_copy = 16777216;
1078}
1079
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001080int
1081SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1082 struct cifs_tcon *tcon, const struct nls_table *cp)
1083{
1084 struct smb2_tree_connect_req *req;
1085 struct smb2_tree_connect_rsp *rsp = NULL;
1086 struct kvec iov[2];
1087 int rc = 0;
1088 int resp_buftype;
1089 int unc_path_len;
1090 struct TCP_Server_Info *server;
1091 __le16 *unc_path = NULL;
1092
Joe Perchesf96637b2013-05-04 22:12:25 -05001093 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001094
1095 if ((ses->server) && tree)
1096 server = ses->server;
1097 else
1098 return -EIO;
1099
Steve Frenchff9f84b2015-09-26 09:48:58 -05001100 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001101 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1102 cifs_dbg(VFS, "encryption requested but no server support");
1103 return -EOPNOTSUPP;
1104 }
1105
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001106 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1107 if (unc_path == NULL)
1108 return -ENOMEM;
1109
1110 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1111 unc_path_len *= 2;
1112 if (unc_path_len < 2) {
1113 kfree(unc_path);
1114 return -EINVAL;
1115 }
1116
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001117 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1118 if (tcon)
1119 tcon->tid = 0;
1120
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001121 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1122 if (rc) {
1123 kfree(unc_path);
1124 return rc;
1125 }
1126
1127 if (tcon == NULL) {
1128 /* since no tcon, smb2_init can not do this, so do here */
1129 req->hdr.SessionId = ses->Suid;
1130 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1131 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1132 }
1133
1134 iov[0].iov_base = (char *)req;
1135 /* 4 for rfc1002 length field and 1 for pad */
1136 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1137
1138 /* Testing shows that buffer offset must be at location of Buffer[0] */
1139 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1140 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1141 req->PathLength = cpu_to_le16(unc_path_len - 2);
1142 iov[1].iov_base = unc_path;
1143 iov[1].iov_len = unc_path_len;
1144
1145 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1146
1147 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1148 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1149
1150 if (rc != 0) {
1151 if (tcon) {
1152 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1153 tcon->need_reconnect = true;
1154 }
1155 goto tcon_error_exit;
1156 }
1157
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001158 if (tcon == NULL) {
1159 ses->ipc_tid = rsp->hdr.TreeId;
1160 goto tcon_exit;
1161 }
1162
1163 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001164 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001165 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1166 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001167 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001168 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1169 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001170 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001171 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001172 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001173 rc = -EOPNOTSUPP;
1174 goto tcon_error_exit;
1175 }
1176
1177 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001178 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001179 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1180 tcon->tidStatus = CifsGood;
1181 tcon->need_reconnect = false;
1182 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001183 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001184
1185 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1186 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001187 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001188 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001189 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1190 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001191 if (tcon->ses->server->ops->validate_negotiate)
1192 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001193tcon_exit:
1194 free_rsp_buf(resp_buftype, rsp);
1195 kfree(unc_path);
1196 return rc;
1197
1198tcon_error_exit:
1199 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001200 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001201 }
1202 goto tcon_exit;
1203}
1204
1205int
1206SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1207{
1208 struct smb2_tree_disconnect_req *req; /* response is trivial */
1209 int rc = 0;
1210 struct TCP_Server_Info *server;
1211 struct cifs_ses *ses = tcon->ses;
1212
Joe Perchesf96637b2013-05-04 22:12:25 -05001213 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001214
1215 if (ses && (ses->server))
1216 server = ses->server;
1217 else
1218 return -EIO;
1219
1220 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1221 return 0;
1222
1223 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1224 if (rc)
1225 return rc;
1226
1227 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1228 if (rc)
1229 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1230
1231 return rc;
1232}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001233
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001234
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001235static struct create_durable *
1236create_durable_buf(void)
1237{
1238 struct create_durable *buf;
1239
1240 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1241 if (!buf)
1242 return NULL;
1243
1244 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001245 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001246 buf->ccontext.DataLength = cpu_to_le32(16);
1247 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1248 (struct create_durable, Name));
1249 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001250 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001251 buf->Name[0] = 'D';
1252 buf->Name[1] = 'H';
1253 buf->Name[2] = 'n';
1254 buf->Name[3] = 'Q';
1255 return buf;
1256}
1257
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001258static struct create_durable *
1259create_reconnect_durable_buf(struct cifs_fid *fid)
1260{
1261 struct create_durable *buf;
1262
1263 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1264 if (!buf)
1265 return NULL;
1266
1267 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1268 (struct create_durable, Data));
1269 buf->ccontext.DataLength = cpu_to_le32(16);
1270 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1271 (struct create_durable, Name));
1272 buf->ccontext.NameLength = cpu_to_le16(4);
1273 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1274 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001275 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001276 buf->Name[0] = 'D';
1277 buf->Name[1] = 'H';
1278 buf->Name[2] = 'n';
1279 buf->Name[3] = 'C';
1280 return buf;
1281}
1282
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001283static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001284parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1285 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001286{
1287 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001288 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001289 unsigned int next;
1290 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001291 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001292
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001293 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001294 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001295 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001296 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001297 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001298 if (le16_to_cpu(cc->NameLength) == 4 &&
1299 strncmp(name, "RqLs", 4) == 0)
1300 return server->ops->parse_lease_buf(cc, epoch);
1301
1302 next = le32_to_cpu(cc->Next);
1303 if (!next)
1304 break;
1305 remaining -= next;
1306 cc = (struct create_context *)((char *)cc + next);
1307 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001308
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001309 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001310}
1311
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001312static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001313add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1314 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001315{
1316 struct smb2_create_req *req = iov[0].iov_base;
1317 unsigned int num = *num_iovec;
1318
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001319 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001320 if (iov[num].iov_base == NULL)
1321 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001322 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001323 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1324 if (!req->CreateContextsOffset)
1325 req->CreateContextsOffset = cpu_to_le32(
1326 sizeof(struct smb2_create_req) - 4 +
1327 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001328 le32_add_cpu(&req->CreateContextsLength,
1329 server->vals->create_lease_size);
1330 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001331 *num_iovec = num + 1;
1332 return 0;
1333}
1334
Steve Frenchb56eae42015-11-03 09:26:27 -06001335static struct create_durable_v2 *
1336create_durable_v2_buf(struct cifs_fid *pfid)
1337{
1338 struct create_durable_v2 *buf;
1339
1340 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1341 if (!buf)
1342 return NULL;
1343
1344 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1345 (struct create_durable_v2, dcontext));
1346 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1347 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1348 (struct create_durable_v2, Name));
1349 buf->ccontext.NameLength = cpu_to_le16(4);
1350
1351 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1352 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001353 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001354 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1355
1356 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1357 buf->Name[0] = 'D';
1358 buf->Name[1] = 'H';
1359 buf->Name[2] = '2';
1360 buf->Name[3] = 'Q';
1361 return buf;
1362}
1363
1364static struct create_durable_handle_reconnect_v2 *
1365create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1366{
1367 struct create_durable_handle_reconnect_v2 *buf;
1368
1369 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1370 GFP_KERNEL);
1371 if (!buf)
1372 return NULL;
1373
1374 buf->ccontext.DataOffset =
1375 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1376 dcontext));
1377 buf->ccontext.DataLength =
1378 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1379 buf->ccontext.NameOffset =
1380 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1381 Name));
1382 buf->ccontext.NameLength = cpu_to_le16(4);
1383
1384 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1385 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1386 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1387 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1388
1389 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1390 buf->Name[0] = 'D';
1391 buf->Name[1] = 'H';
1392 buf->Name[2] = '2';
1393 buf->Name[3] = 'C';
1394 return buf;
1395}
1396
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001397static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001398add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001399 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001400{
1401 struct smb2_create_req *req = iov[0].iov_base;
1402 unsigned int num = *num_iovec;
1403
Steve Frenchb56eae42015-11-03 09:26:27 -06001404 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1405 if (iov[num].iov_base == NULL)
1406 return -ENOMEM;
1407 iov[num].iov_len = sizeof(struct create_durable_v2);
1408 if (!req->CreateContextsOffset)
1409 req->CreateContextsOffset =
1410 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1411 iov[1].iov_len);
1412 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1413 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1414 *num_iovec = num + 1;
1415 return 0;
1416}
1417
1418static int
1419add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1420 struct cifs_open_parms *oparms)
1421{
1422 struct smb2_create_req *req = iov[0].iov_base;
1423 unsigned int num = *num_iovec;
1424
1425 /* indicate that we don't need to relock the file */
1426 oparms->reconnect = false;
1427
1428 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1429 if (iov[num].iov_base == NULL)
1430 return -ENOMEM;
1431 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1432 if (!req->CreateContextsOffset)
1433 req->CreateContextsOffset =
1434 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1435 iov[1].iov_len);
1436 le32_add_cpu(&req->CreateContextsLength,
1437 sizeof(struct create_durable_handle_reconnect_v2));
1438 inc_rfc1001_len(&req->hdr,
1439 sizeof(struct create_durable_handle_reconnect_v2));
1440 *num_iovec = num + 1;
1441 return 0;
1442}
1443
1444static int
1445add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1446 struct cifs_open_parms *oparms, bool use_persistent)
1447{
1448 struct smb2_create_req *req = iov[0].iov_base;
1449 unsigned int num = *num_iovec;
1450
1451 if (use_persistent) {
1452 if (oparms->reconnect)
1453 return add_durable_reconnect_v2_context(iov, num_iovec,
1454 oparms);
1455 else
1456 return add_durable_v2_context(iov, num_iovec, oparms);
1457 }
1458
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001459 if (oparms->reconnect) {
1460 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1461 /* indicate that we don't need to relock the file */
1462 oparms->reconnect = false;
1463 } else
1464 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001465 if (iov[num].iov_base == NULL)
1466 return -ENOMEM;
1467 iov[num].iov_len = sizeof(struct create_durable);
1468 if (!req->CreateContextsOffset)
1469 req->CreateContextsOffset =
1470 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1471 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001472 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001473 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1474 *num_iovec = num + 1;
1475 return 0;
1476}
1477
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001478int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001479SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001480 __u8 *oplock, struct smb2_file_all_info *buf,
1481 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001482{
1483 struct smb2_create_req *req;
1484 struct smb2_create_rsp *rsp;
1485 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001486 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001487 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001488 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001489 int resp_buftype;
1490 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001491 __le16 *copy_path = NULL;
1492 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001493 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001494 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001495 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001496 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001497
Joe Perchesf96637b2013-05-04 22:12:25 -05001498 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001499
1500 if (ses && (ses->server))
1501 server = ses->server;
1502 else
1503 return -EIO;
1504
1505 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1506 if (rc)
1507 return rc;
1508
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001509 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001510 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001511 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1512 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001513
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001514 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001515 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001516 /* File attributes ignored on open (used in create though) */
1517 req->FileAttributes = cpu_to_le32(file_attributes);
1518 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001519 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1520 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001521 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001522 /* do not count rfc1001 len field */
1523 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001524
1525 iov[0].iov_base = (char *)req;
1526 /* 4 for rfc1002 length field */
1527 iov[0].iov_len = get_rfc1002_length(req) + 4;
1528
1529 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001530 req->NameLength = cpu_to_le16(uni_path_len - 2);
1531 /* -1 since last byte is buf[0] which is sent below (path) */
1532 iov[0].iov_len--;
1533 if (uni_path_len % 8 != 0) {
1534 copy_size = uni_path_len / 8 * 8;
1535 if (copy_size < uni_path_len)
1536 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001537
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001538 copy_path = kzalloc(copy_size, GFP_KERNEL);
1539 if (!copy_path)
1540 return -ENOMEM;
1541 memcpy((char *)copy_path, (const char *)path,
1542 uni_path_len);
1543 uni_path_len = copy_size;
1544 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001545 }
1546
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001547 iov[1].iov_len = uni_path_len;
1548 iov[1].iov_base = path;
1549 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1550 inc_rfc1001_len(req, uni_path_len - 1);
1551
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001552 if (!server->oplocks)
1553 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1554
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001555 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001556 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1557 req->RequestedOplockLevel = *oplock;
1558 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001559 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001560 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001561 cifs_small_buf_release(req);
1562 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001563 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001564 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001565 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001566 }
1567
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001568 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1569 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001570 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001571 struct create_context *ccontext =
1572 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001573 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001574 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001575 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001576
1577 rc = add_durable_context(iov, &num_iovecs, oparms,
1578 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001579 if (rc) {
1580 cifs_small_buf_release(req);
1581 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001582 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001583 return rc;
1584 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001585 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001586 }
1587
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001588 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1589 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1590
1591 if (rc != 0) {
1592 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001593 if (err_buf)
1594 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1595 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001596 goto creat_exit;
1597 }
1598
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001599 oparms->fid->persistent_fid = rsp->PersistentFileId;
1600 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001601
1602 if (buf) {
1603 memcpy(buf, &rsp->CreationTime, 32);
1604 buf->AllocationSize = rsp->AllocationSize;
1605 buf->EndOfFile = rsp->EndofFile;
1606 buf->Attributes = rsp->FileAttributes;
1607 buf->NumberOfLinks = cpu_to_le32(1);
1608 buf->DeletePending = 0;
1609 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001610
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001611 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001612 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001613 else
1614 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001615creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001616 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001617 kfree(lc_buf);
1618 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001619 free_rsp_buf(resp_buftype, rsp);
1620 return rc;
1621}
1622
Steve French4a72daf2013-06-25 00:20:49 -05001623/*
1624 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1625 */
1626int
1627SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1628 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1629 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1630{
1631 struct smb2_ioctl_req *req;
1632 struct smb2_ioctl_rsp *rsp;
1633 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001634 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001635 struct kvec iov[2];
1636 int resp_buftype;
1637 int num_iovecs;
1638 int rc = 0;
1639
1640 cifs_dbg(FYI, "SMB2 IOCTL\n");
1641
Steve French3d1a3742014-08-11 21:05:25 -05001642 if (out_data != NULL)
1643 *out_data = NULL;
1644
Steve French4a72daf2013-06-25 00:20:49 -05001645 /* zero out returned data len, in case of error */
1646 if (plen)
1647 *plen = 0;
1648
Steve French8e353102015-03-26 19:47:02 -05001649 if (tcon)
1650 ses = tcon->ses;
1651 else
1652 return -EIO;
1653
Steve French4a72daf2013-06-25 00:20:49 -05001654 if (ses && (ses->server))
1655 server = ses->server;
1656 else
1657 return -EIO;
1658
1659 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1660 if (rc)
1661 return rc;
1662
1663 req->CtlCode = cpu_to_le32(opcode);
1664 req->PersistentFileId = persistent_fid;
1665 req->VolatileFileId = volatile_fid;
1666
1667 if (indatalen) {
1668 req->InputCount = cpu_to_le32(indatalen);
1669 /* do not set InputOffset if no input data */
1670 req->InputOffset =
1671 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1672 iov[1].iov_base = in_data;
1673 iov[1].iov_len = indatalen;
1674 num_iovecs = 2;
1675 } else
1676 num_iovecs = 1;
1677
1678 req->OutputOffset = 0;
1679 req->OutputCount = 0; /* MBZ */
1680
1681 /*
1682 * Could increase MaxOutputResponse, but that would require more
1683 * than one credit. Windows typically sets this smaller, but for some
1684 * ioctls it may be useful to allow server to send more. No point
1685 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001686 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1687 * (by default, note that it can be overridden to make max larger)
1688 * in responses (except for read responses which can be bigger.
1689 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001690 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001691 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001692
1693 if (is_fsctl)
1694 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1695 else
1696 req->Flags = 0;
1697
1698 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001699
Steve French7ff8d452013-10-14 00:44:19 -05001700 /*
1701 * If no input data, the size of ioctl struct in
1702 * protocol spec still includes a 1 byte data buffer,
1703 * but if input data passed to ioctl, we do not
1704 * want to double count this, so we do not send
1705 * the dummy one byte of data in iovec[0] if sending
1706 * input data (in iovec[1]). We also must add 4 bytes
1707 * in first iovec to allow for rfc1002 length field.
1708 */
1709
1710 if (indatalen) {
1711 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1712 inc_rfc1001_len(req, indatalen - 1);
1713 } else
1714 iov[0].iov_len = get_rfc1002_length(req) + 4;
1715
Steve French4a72daf2013-06-25 00:20:49 -05001716
1717 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1718 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1719
Steve French9bf0c9c2013-11-16 18:05:28 -06001720 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001721 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001722 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001723 } else if (rc == -EINVAL) {
1724 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1725 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001726 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001727 goto ioctl_exit;
1728 }
Steve French4a72daf2013-06-25 00:20:49 -05001729 }
1730
1731 /* check if caller wants to look at return data or just return rc */
1732 if ((plen == NULL) || (out_data == NULL))
1733 goto ioctl_exit;
1734
1735 *plen = le32_to_cpu(rsp->OutputCount);
1736
1737 /* We check for obvious errors in the output buffer length and offset */
1738 if (*plen == 0)
1739 goto ioctl_exit; /* server returned no data */
1740 else if (*plen > 0xFF00) {
1741 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1742 *plen = 0;
1743 rc = -EIO;
1744 goto ioctl_exit;
1745 }
1746
1747 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1748 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1749 le32_to_cpu(rsp->OutputOffset));
1750 *plen = 0;
1751 rc = -EIO;
1752 goto ioctl_exit;
1753 }
1754
1755 *out_data = kmalloc(*plen, GFP_KERNEL);
1756 if (*out_data == NULL) {
1757 rc = -ENOMEM;
1758 goto ioctl_exit;
1759 }
1760
Steve French373512e2015-12-18 13:05:30 -06001761 memcpy(*out_data,
1762 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001763 *plen);
1764ioctl_exit:
1765 free_rsp_buf(resp_buftype, rsp);
1766 return rc;
1767}
1768
Steve French64a5cfa2013-10-14 15:31:32 -05001769/*
1770 * Individual callers to ioctl worker function follow
1771 */
1772
1773int
1774SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1775 u64 persistent_fid, u64 volatile_fid)
1776{
1777 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001778 struct compress_ioctl fsctl_input;
1779 char *ret_data = NULL;
1780
1781 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001782 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001783
1784 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1785 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1786 (char *)&fsctl_input /* data input */,
1787 2 /* in data len */, &ret_data /* out data */, NULL);
1788
1789 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001790
1791 return rc;
1792}
1793
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001794int
1795SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1796 u64 persistent_fid, u64 volatile_fid)
1797{
1798 struct smb2_close_req *req;
1799 struct smb2_close_rsp *rsp;
1800 struct TCP_Server_Info *server;
1801 struct cifs_ses *ses = tcon->ses;
1802 struct kvec iov[1];
1803 int resp_buftype;
1804 int rc = 0;
1805
Joe Perchesf96637b2013-05-04 22:12:25 -05001806 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001807
1808 if (ses && (ses->server))
1809 server = ses->server;
1810 else
1811 return -EIO;
1812
1813 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1814 if (rc)
1815 return rc;
1816
1817 req->PersistentFileId = persistent_fid;
1818 req->VolatileFileId = volatile_fid;
1819
1820 iov[0].iov_base = (char *)req;
1821 /* 4 for rfc1002 length field */
1822 iov[0].iov_len = get_rfc1002_length(req) + 4;
1823
1824 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1825 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1826
1827 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001828 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001829 goto close_exit;
1830 }
1831
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001832 /* BB FIXME - decode close response, update inode for caching */
1833
1834close_exit:
1835 free_rsp_buf(resp_buftype, rsp);
1836 return rc;
1837}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001838
1839static int
1840validate_buf(unsigned int offset, unsigned int buffer_length,
1841 struct smb2_hdr *hdr, unsigned int min_buf_size)
1842
1843{
1844 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1845 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1846 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1847 char *end_of_buf = begin_of_buf + buffer_length;
1848
1849
1850 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001851 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1852 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001853 return -EINVAL;
1854 }
1855
1856 /* check if beyond RFC1001 maximum length */
1857 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001858 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1859 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001860 return -EINVAL;
1861 }
1862
1863 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001864 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001865 return -EINVAL;
1866 }
1867
1868 return 0;
1869}
1870
1871/*
1872 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1873 * Caller must free buffer.
1874 */
1875static int
1876validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1877 struct smb2_hdr *hdr, unsigned int minbufsize,
1878 char *data)
1879
1880{
1881 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1882 int rc;
1883
1884 if (!data)
1885 return -EINVAL;
1886
1887 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1888 if (rc)
1889 return rc;
1890
1891 memcpy(data, begin_of_buf, buffer_length);
1892
1893 return 0;
1894}
1895
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001896static int
1897query_info(const unsigned int xid, struct cifs_tcon *tcon,
1898 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1899 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001900{
1901 struct smb2_query_info_req *req;
1902 struct smb2_query_info_rsp *rsp = NULL;
1903 struct kvec iov[2];
1904 int rc = 0;
1905 int resp_buftype;
1906 struct TCP_Server_Info *server;
1907 struct cifs_ses *ses = tcon->ses;
1908
Joe Perchesf96637b2013-05-04 22:12:25 -05001909 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001910
1911 if (ses && (ses->server))
1912 server = ses->server;
1913 else
1914 return -EIO;
1915
1916 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1917 if (rc)
1918 return rc;
1919
1920 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001921 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001922 req->PersistentFileId = persistent_fid;
1923 req->VolatileFileId = volatile_fid;
1924 /* 4 for rfc1002 length field and 1 for Buffer */
1925 req->InputBufferOffset =
1926 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001927 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001928
1929 iov[0].iov_base = (char *)req;
1930 /* 4 for rfc1002 length field */
1931 iov[0].iov_len = get_rfc1002_length(req) + 4;
1932
1933 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001934 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1935
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001936 if (rc) {
1937 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1938 goto qinf_exit;
1939 }
1940
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001941 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1942 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001943 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001944
1945qinf_exit:
1946 free_rsp_buf(resp_buftype, rsp);
1947 return rc;
1948}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001949
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001950int
1951SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1952 u64 persistent_fid, u64 volatile_fid,
1953 struct smb2_file_all_info *data)
1954{
1955 return query_info(xid, tcon, persistent_fid, volatile_fid,
1956 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001957 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001958 sizeof(struct smb2_file_all_info), data);
1959}
1960
1961int
1962SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1963 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1964{
1965 return query_info(xid, tcon, persistent_fid, volatile_fid,
1966 FILE_INTERNAL_INFORMATION,
1967 sizeof(struct smb2_file_internal_info),
1968 sizeof(struct smb2_file_internal_info), uniqueid);
1969}
1970
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001971/*
1972 * This is a no-op for now. We're not really interested in the reply, but
1973 * rather in the fact that the server sent one and that server->lstrp
1974 * gets updated.
1975 *
1976 * FIXME: maybe we should consider checking that the reply matches request?
1977 */
1978static void
1979smb2_echo_callback(struct mid_q_entry *mid)
1980{
1981 struct TCP_Server_Info *server = mid->callback_data;
1982 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1983 unsigned int credits_received = 1;
1984
1985 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1986 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1987
Christopher Oo5fb4e282015-06-25 16:10:48 -07001988 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001989 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001990 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001991 add_credits(server, credits_received, CIFS_ECHO_OP);
1992}
1993
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001994void smb2_reconnect_server(struct work_struct *work)
1995{
1996 struct TCP_Server_Info *server = container_of(work,
1997 struct TCP_Server_Info, reconnect.work);
1998 struct cifs_ses *ses;
1999 struct cifs_tcon *tcon, *tcon2;
2000 struct list_head tmp_list;
2001 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002002 int rc;
2003 int resched = false;
2004
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002005
2006 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2007 mutex_lock(&server->reconnect_mutex);
2008
2009 INIT_LIST_HEAD(&tmp_list);
2010 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2011
2012 spin_lock(&cifs_tcp_ses_lock);
2013 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2014 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002015 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002016 tcon->tc_count++;
2017 list_add_tail(&tcon->rlist, &tmp_list);
2018 tcon_exist = true;
2019 }
2020 }
2021 }
2022 /*
2023 * Get the reference to server struct to be sure that the last call of
2024 * cifs_put_tcon() in the loop below won't release the server pointer.
2025 */
2026 if (tcon_exist)
2027 server->srv_count++;
2028
2029 spin_unlock(&cifs_tcp_ses_lock);
2030
2031 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002032 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2033 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002034 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002035 else
2036 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002037 list_del_init(&tcon->rlist);
2038 cifs_put_tcon(tcon);
2039 }
2040
2041 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002042 if (resched)
2043 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002044 mutex_unlock(&server->reconnect_mutex);
2045
2046 /* now we can safely release srv struct */
2047 if (tcon_exist)
2048 cifs_put_tcp_session(server, 1);
2049}
2050
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002051int
2052SMB2_echo(struct TCP_Server_Info *server)
2053{
2054 struct smb2_echo_req *req;
2055 int rc = 0;
2056 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002057 struct smb_rqst rqst = { .rq_iov = &iov,
2058 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002059
Joe Perchesf96637b2013-05-04 22:12:25 -05002060 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002061
Steve French4fcd1812016-06-22 20:12:05 -05002062 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002063 /* No need to send echo on newly established connections */
2064 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2065 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002066 }
2067
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002068 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2069 if (rc)
2070 return rc;
2071
2072 req->hdr.CreditRequest = cpu_to_le16(1);
2073
2074 iov.iov_base = (char *)req;
2075 /* 4 for rfc1002 length field */
2076 iov.iov_len = get_rfc1002_length(req) + 4;
2077
Jeff Laytonfec344e2012-09-18 16:20:35 -07002078 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002079 CIFS_ECHO_OP);
2080 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002081 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002082
2083 cifs_small_buf_release(req);
2084 return rc;
2085}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002086
2087int
2088SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2089 u64 volatile_fid)
2090{
2091 struct smb2_flush_req *req;
2092 struct TCP_Server_Info *server;
2093 struct cifs_ses *ses = tcon->ses;
2094 struct kvec iov[1];
2095 int resp_buftype;
2096 int rc = 0;
2097
Joe Perchesf96637b2013-05-04 22:12:25 -05002098 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002099
2100 if (ses && (ses->server))
2101 server = ses->server;
2102 else
2103 return -EIO;
2104
2105 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2106 if (rc)
2107 return rc;
2108
2109 req->PersistentFileId = persistent_fid;
2110 req->VolatileFileId = volatile_fid;
2111
2112 iov[0].iov_base = (char *)req;
2113 /* 4 for rfc1002 length field */
2114 iov[0].iov_len = get_rfc1002_length(req) + 4;
2115
2116 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2117
Steve Frenchdfebe402015-03-27 01:00:06 -05002118 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002119 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2120
2121 free_rsp_buf(resp_buftype, iov[0].iov_base);
2122 return rc;
2123}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002124
2125/*
2126 * To form a chain of read requests, any read requests after the first should
2127 * have the end_of_chain boolean set to true.
2128 */
2129static int
2130smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2131 unsigned int remaining_bytes, int request_type)
2132{
2133 int rc = -EACCES;
2134 struct smb2_read_req *req = NULL;
2135
2136 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2137 if (rc)
2138 return rc;
2139 if (io_parms->tcon->ses->server == NULL)
2140 return -ECONNABORTED;
2141
2142 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2143
2144 req->PersistentFileId = io_parms->persistent_fid;
2145 req->VolatileFileId = io_parms->volatile_fid;
2146 req->ReadChannelInfoOffset = 0; /* reserved */
2147 req->ReadChannelInfoLength = 0; /* reserved */
2148 req->Channel = 0; /* reserved */
2149 req->MinimumCount = 0;
2150 req->Length = cpu_to_le32(io_parms->length);
2151 req->Offset = cpu_to_le64(io_parms->offset);
2152
2153 if (request_type & CHAINED_REQUEST) {
2154 if (!(request_type & END_OF_CHAIN)) {
2155 /* 4 for rfc1002 length field */
2156 req->hdr.NextCommand =
2157 cpu_to_le32(get_rfc1002_length(req) + 4);
2158 } else /* END_OF_CHAIN */
2159 req->hdr.NextCommand = 0;
2160 if (request_type & RELATED_REQUEST) {
2161 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2162 /*
2163 * Related requests use info from previous read request
2164 * in chain.
2165 */
2166 req->hdr.SessionId = 0xFFFFFFFF;
2167 req->hdr.TreeId = 0xFFFFFFFF;
2168 req->PersistentFileId = 0xFFFFFFFF;
2169 req->VolatileFileId = 0xFFFFFFFF;
2170 }
2171 }
2172 if (remaining_bytes > io_parms->length)
2173 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2174 else
2175 req->RemainingBytes = 0;
2176
2177 iov[0].iov_base = (char *)req;
2178 /* 4 for rfc1002 length field */
2179 iov[0].iov_len = get_rfc1002_length(req) + 4;
2180 return rc;
2181}
2182
2183static void
2184smb2_readv_callback(struct mid_q_entry *mid)
2185{
2186 struct cifs_readdata *rdata = mid->callback_data;
2187 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2188 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002189 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002190 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002191 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002192 .rq_nvec = 1,
2193 .rq_pages = rdata->pages,
2194 .rq_npages = rdata->nr_pages,
2195 .rq_pagesz = rdata->pagesz,
2196 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002197
Joe Perchesf96637b2013-05-04 22:12:25 -05002198 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2199 __func__, mid->mid, mid->mid_state, rdata->result,
2200 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002201
2202 switch (mid->mid_state) {
2203 case MID_RESPONSE_RECEIVED:
2204 credits_received = le16_to_cpu(buf->CreditRequest);
2205 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002206 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002207 int rc;
2208
Jeff Layton0b688cf2012-09-18 16:20:34 -07002209 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002210 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002211 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2212 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002213 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002214 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002215 task_io_account_read(rdata->got_bytes);
2216 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002217 break;
2218 case MID_REQUEST_SUBMITTED:
2219 case MID_RETRY_NEEDED:
2220 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002221 if (server->sign && rdata->got_bytes)
2222 /* reset bytes number since we can not check a sign */
2223 rdata->got_bytes = 0;
2224 /* FIXME: should this be counted toward the initiating task? */
2225 task_io_account_read(rdata->got_bytes);
2226 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002227 break;
2228 default:
2229 if (rdata->result != -ENODATA)
2230 rdata->result = -EIO;
2231 }
2232
2233 if (rdata->result)
2234 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2235
2236 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002237 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002238 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002239 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002240 add_credits(server, credits_received, 0);
2241}
2242
2243/* smb2_async_readv - send an async write, and set up mid to handle result */
2244int
2245smb2_async_readv(struct cifs_readdata *rdata)
2246{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002247 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002248 struct smb2_hdr *buf;
2249 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002250 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002251 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002252 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002253
Joe Perchesf96637b2013-05-04 22:12:25 -05002254 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2255 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002256
2257 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2258 io_parms.offset = rdata->offset;
2259 io_parms.length = rdata->bytes;
2260 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2261 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2262 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002263
2264 server = io_parms.tcon->ses->server;
2265
Jeff Layton58195752012-09-19 06:22:34 -07002266 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002267 if (rc) {
2268 if (rc == -EAGAIN && rdata->credits) {
2269 /* credits was reset by reconnect */
2270 rdata->credits = 0;
2271 /* reduce in_flight value since we won't send the req */
2272 spin_lock(&server->req_lock);
2273 server->in_flight--;
2274 spin_unlock(&server->req_lock);
2275 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002276 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002277 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002278
Jeff Layton58195752012-09-19 06:22:34 -07002279 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002280 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002281 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002282
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002283 if (rdata->credits) {
2284 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2285 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002286 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002287 spin_lock(&server->req_lock);
2288 server->credits += rdata->credits -
2289 le16_to_cpu(buf->CreditCharge);
2290 spin_unlock(&server->req_lock);
2291 wake_up(&server->request_q);
2292 flags = CIFS_HAS_CREDITS;
2293 }
2294
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002295 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002296 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002297 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002298 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002299 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002300 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002301 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2302 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002303
2304 cifs_small_buf_release(buf);
2305 return rc;
2306}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002307
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002308int
2309SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2310 unsigned int *nbytes, char **buf, int *buf_type)
2311{
2312 int resp_buftype, rc = -EACCES;
2313 struct smb2_read_rsp *rsp = NULL;
2314 struct kvec iov[1];
2315
2316 *nbytes = 0;
2317 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2318 if (rc)
2319 return rc;
2320
2321 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2322 &resp_buftype, CIFS_LOG_ERROR);
2323
2324 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2325
2326 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2327 free_rsp_buf(resp_buftype, iov[0].iov_base);
2328 return 0;
2329 }
2330
2331 if (rc) {
2332 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002333 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002334 } else {
2335 *nbytes = le32_to_cpu(rsp->DataLength);
2336 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2337 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002338 cifs_dbg(FYI, "bad length %d for count %d\n",
2339 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002340 rc = -EIO;
2341 *nbytes = 0;
2342 }
2343 }
2344
2345 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002346 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002347 *nbytes);
2348 free_rsp_buf(resp_buftype, iov[0].iov_base);
2349 } else if (resp_buftype != CIFS_NO_BUFFER) {
2350 *buf = iov[0].iov_base;
2351 if (resp_buftype == CIFS_SMALL_BUFFER)
2352 *buf_type = CIFS_SMALL_BUFFER;
2353 else if (resp_buftype == CIFS_LARGE_BUFFER)
2354 *buf_type = CIFS_LARGE_BUFFER;
2355 }
2356 return rc;
2357}
2358
Pavel Shilovsky33319142012-09-18 16:20:29 -07002359/*
2360 * Check the mid_state and signature on received buffer (if any), and queue the
2361 * workqueue completion task.
2362 */
2363static void
2364smb2_writev_callback(struct mid_q_entry *mid)
2365{
2366 struct cifs_writedata *wdata = mid->callback_data;
2367 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002368 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002369 unsigned int written;
2370 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2371 unsigned int credits_received = 1;
2372
2373 switch (mid->mid_state) {
2374 case MID_RESPONSE_RECEIVED:
2375 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2376 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2377 if (wdata->result != 0)
2378 break;
2379
2380 written = le32_to_cpu(rsp->DataLength);
2381 /*
2382 * Mask off high 16 bits when bytes written as returned
2383 * by the server is greater than bytes requested by the
2384 * client. OS/2 servers are known to set incorrect
2385 * CountHigh values.
2386 */
2387 if (written > wdata->bytes)
2388 written &= 0xFFFF;
2389
2390 if (written < wdata->bytes)
2391 wdata->result = -ENOSPC;
2392 else
2393 wdata->bytes = written;
2394 break;
2395 case MID_REQUEST_SUBMITTED:
2396 case MID_RETRY_NEEDED:
2397 wdata->result = -EAGAIN;
2398 break;
2399 default:
2400 wdata->result = -EIO;
2401 break;
2402 }
2403
2404 if (wdata->result)
2405 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2406
2407 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002408 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002409 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002410 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002411 add_credits(tcon->ses->server, credits_received, 0);
2412}
2413
2414/* smb2_async_writev - send an async write, and set up mid to handle result */
2415int
Steve French4a5c80d2014-02-07 20:45:12 -06002416smb2_async_writev(struct cifs_writedata *wdata,
2417 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002418{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002419 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002420 struct smb2_write_req *req = NULL;
2421 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002422 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002423 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002424 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002425
2426 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002427 if (rc) {
2428 if (rc == -EAGAIN && wdata->credits) {
2429 /* credits was reset by reconnect */
2430 wdata->credits = 0;
2431 /* reduce in_flight value since we won't send the req */
2432 spin_lock(&server->req_lock);
2433 server->in_flight--;
2434 spin_unlock(&server->req_lock);
2435 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002436 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002437 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002438
Pavel Shilovsky33319142012-09-18 16:20:29 -07002439 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2440
2441 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2442 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2443 req->WriteChannelInfoOffset = 0;
2444 req->WriteChannelInfoLength = 0;
2445 req->Channel = 0;
2446 req->Offset = cpu_to_le64(wdata->offset);
2447 /* 4 for rfc1002 length field */
2448 req->DataOffset = cpu_to_le16(
2449 offsetof(struct smb2_write_req, Buffer) - 4);
2450 req->RemainingBytes = 0;
2451
2452 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002453 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2454 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002455
Jeff Laytoneddb0792012-09-18 16:20:35 -07002456 rqst.rq_iov = &iov;
2457 rqst.rq_nvec = 1;
2458 rqst.rq_pages = wdata->pages;
2459 rqst.rq_npages = wdata->nr_pages;
2460 rqst.rq_pagesz = wdata->pagesz;
2461 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002462
Joe Perchesf96637b2013-05-04 22:12:25 -05002463 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2464 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002465
2466 req->Length = cpu_to_le32(wdata->bytes);
2467
2468 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2469
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002470 if (wdata->credits) {
2471 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2472 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002473 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002474 spin_lock(&server->req_lock);
2475 server->credits += wdata->credits -
2476 le16_to_cpu(req->hdr.CreditCharge);
2477 spin_unlock(&server->req_lock);
2478 wake_up(&server->request_q);
2479 flags = CIFS_HAS_CREDITS;
2480 }
2481
Pavel Shilovsky33319142012-09-18 16:20:29 -07002482 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002483 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2484 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002485
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002486 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002487 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002488 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2489 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002490
Pavel Shilovsky33319142012-09-18 16:20:29 -07002491async_writev_out:
2492 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002493 return rc;
2494}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002495
2496/*
2497 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2498 * The length field from io_parms must be at least 1 and indicates a number of
2499 * elements with data to write that begins with position 1 in iov array. All
2500 * data length is specified by count.
2501 */
2502int
2503SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2504 unsigned int *nbytes, struct kvec *iov, int n_vec)
2505{
2506 int rc = 0;
2507 struct smb2_write_req *req = NULL;
2508 struct smb2_write_rsp *rsp = NULL;
2509 int resp_buftype;
2510 *nbytes = 0;
2511
2512 if (n_vec < 1)
2513 return rc;
2514
2515 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2516 if (rc)
2517 return rc;
2518
2519 if (io_parms->tcon->ses->server == NULL)
2520 return -ECONNABORTED;
2521
2522 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2523
2524 req->PersistentFileId = io_parms->persistent_fid;
2525 req->VolatileFileId = io_parms->volatile_fid;
2526 req->WriteChannelInfoOffset = 0;
2527 req->WriteChannelInfoLength = 0;
2528 req->Channel = 0;
2529 req->Length = cpu_to_le32(io_parms->length);
2530 req->Offset = cpu_to_le64(io_parms->offset);
2531 /* 4 for rfc1002 length field */
2532 req->DataOffset = cpu_to_le16(
2533 offsetof(struct smb2_write_req, Buffer) - 4);
2534 req->RemainingBytes = 0;
2535
2536 iov[0].iov_base = (char *)req;
2537 /* 4 for rfc1002 length field and 1 for Buffer */
2538 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2539
2540 /* length of entire message including data to be written */
2541 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2542
2543 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2544 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002545 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002546
2547 if (rc) {
2548 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002549 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002550 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002551 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002552
2553 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002554 return rc;
2555}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002556
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002557static unsigned int
2558num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2559{
2560 int len;
2561 unsigned int entrycount = 0;
2562 unsigned int next_offset = 0;
2563 FILE_DIRECTORY_INFO *entryptr;
2564
2565 if (bufstart == NULL)
2566 return 0;
2567
2568 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2569
2570 while (1) {
2571 entryptr = (FILE_DIRECTORY_INFO *)
2572 ((char *)entryptr + next_offset);
2573
2574 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002575 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002576 break;
2577 }
2578
2579 len = le32_to_cpu(entryptr->FileNameLength);
2580 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002581 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2582 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002583 break;
2584 }
2585
2586 *lastentry = (char *)entryptr;
2587 entrycount++;
2588
2589 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2590 if (!next_offset)
2591 break;
2592 }
2593
2594 return entrycount;
2595}
2596
2597/*
2598 * Readdir/FindFirst
2599 */
2600int
2601SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2602 u64 persistent_fid, u64 volatile_fid, int index,
2603 struct cifs_search_info *srch_inf)
2604{
2605 struct smb2_query_directory_req *req;
2606 struct smb2_query_directory_rsp *rsp = NULL;
2607 struct kvec iov[2];
2608 int rc = 0;
2609 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002610 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002611 unsigned char *bufptr;
2612 struct TCP_Server_Info *server;
2613 struct cifs_ses *ses = tcon->ses;
2614 __le16 asteriks = cpu_to_le16('*');
2615 char *end_of_smb;
2616 unsigned int output_size = CIFSMaxBufSize;
2617 size_t info_buf_size;
2618
2619 if (ses && (ses->server))
2620 server = ses->server;
2621 else
2622 return -EIO;
2623
2624 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2625 if (rc)
2626 return rc;
2627
2628 switch (srch_inf->info_level) {
2629 case SMB_FIND_FILE_DIRECTORY_INFO:
2630 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2631 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2632 break;
2633 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2634 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2635 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2636 break;
2637 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002638 cifs_dbg(VFS, "info level %u isn't supported\n",
2639 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002640 rc = -EINVAL;
2641 goto qdir_exit;
2642 }
2643
2644 req->FileIndex = cpu_to_le32(index);
2645 req->PersistentFileId = persistent_fid;
2646 req->VolatileFileId = volatile_fid;
2647
2648 len = 0x2;
2649 bufptr = req->Buffer;
2650 memcpy(bufptr, &asteriks, len);
2651
2652 req->FileNameOffset =
2653 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2654 req->FileNameLength = cpu_to_le16(len);
2655 /*
2656 * BB could be 30 bytes or so longer if we used SMB2 specific
2657 * buffer lengths, but this is safe and close enough.
2658 */
2659 output_size = min_t(unsigned int, output_size, server->maxBuf);
2660 output_size = min_t(unsigned int, output_size, 2 << 15);
2661 req->OutputBufferLength = cpu_to_le32(output_size);
2662
2663 iov[0].iov_base = (char *)req;
2664 /* 4 for RFC1001 length and 1 for Buffer */
2665 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2666
2667 iov[1].iov_base = (char *)(req->Buffer);
2668 iov[1].iov_len = len;
2669
2670 inc_rfc1001_len(req, len - 1 /* Buffer */);
2671
2672 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002673 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2674
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002675 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002676 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2677 srch_inf->endOfSearch = true;
2678 rc = 0;
2679 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002680 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2681 goto qdir_exit;
2682 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002683
2684 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2685 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2686 info_buf_size);
2687 if (rc)
2688 goto qdir_exit;
2689
2690 srch_inf->unicode = true;
2691
2692 if (srch_inf->ntwrk_buf_start) {
2693 if (srch_inf->smallBuf)
2694 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2695 else
2696 cifs_buf_release(srch_inf->ntwrk_buf_start);
2697 }
2698 srch_inf->ntwrk_buf_start = (char *)rsp;
2699 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2700 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2701 /* 4 for rfc1002 length field */
2702 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2703 srch_inf->entries_in_buffer =
2704 num_entries(srch_inf->srch_entries_start, end_of_smb,
2705 &srch_inf->last_entry, info_buf_size);
2706 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002707 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2708 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2709 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002710 if (resp_buftype == CIFS_LARGE_BUFFER)
2711 srch_inf->smallBuf = false;
2712 else if (resp_buftype == CIFS_SMALL_BUFFER)
2713 srch_inf->smallBuf = true;
2714 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002715 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002716
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002717 return rc;
2718
2719qdir_exit:
2720 free_rsp_buf(resp_buftype, rsp);
2721 return rc;
2722}
2723
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002724static int
2725send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002726 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002727 unsigned int num, void **data, unsigned int *size)
2728{
2729 struct smb2_set_info_req *req;
2730 struct smb2_set_info_rsp *rsp = NULL;
2731 struct kvec *iov;
2732 int rc = 0;
2733 int resp_buftype;
2734 unsigned int i;
2735 struct TCP_Server_Info *server;
2736 struct cifs_ses *ses = tcon->ses;
2737
2738 if (ses && (ses->server))
2739 server = ses->server;
2740 else
2741 return -EIO;
2742
2743 if (!num)
2744 return -EINVAL;
2745
2746 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2747 if (!iov)
2748 return -ENOMEM;
2749
2750 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2751 if (rc) {
2752 kfree(iov);
2753 return rc;
2754 }
2755
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002756 req->hdr.ProcessId = cpu_to_le32(pid);
2757
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002758 req->InfoType = SMB2_O_INFO_FILE;
2759 req->FileInfoClass = info_class;
2760 req->PersistentFileId = persistent_fid;
2761 req->VolatileFileId = volatile_fid;
2762
2763 /* 4 for RFC1001 length and 1 for Buffer */
2764 req->BufferOffset =
2765 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2766 req->BufferLength = cpu_to_le32(*size);
2767
2768 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2769
2770 memcpy(req->Buffer, *data, *size);
2771
2772 iov[0].iov_base = (char *)req;
2773 /* 4 for RFC1001 length */
2774 iov[0].iov_len = get_rfc1002_length(req) + 4;
2775
2776 for (i = 1; i < num; i++) {
2777 inc_rfc1001_len(req, size[i]);
2778 le32_add_cpu(&req->BufferLength, size[i]);
2779 iov[i].iov_base = (char *)data[i];
2780 iov[i].iov_len = size[i];
2781 }
2782
2783 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2784 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2785
Steve French7d3fb242013-11-18 09:56:28 -06002786 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002787 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002788
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002789 free_rsp_buf(resp_buftype, rsp);
2790 kfree(iov);
2791 return rc;
2792}
2793
2794int
2795SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2796 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2797{
2798 struct smb2_file_rename_info info;
2799 void **data;
2800 unsigned int size[2];
2801 int rc;
2802 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2803
2804 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2805 if (!data)
2806 return -ENOMEM;
2807
2808 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2809 /* 0 = fail if target already exists */
2810 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2811 info.FileNameLength = cpu_to_le32(len);
2812
2813 data[0] = &info;
2814 size[0] = sizeof(struct smb2_file_rename_info);
2815
2816 data[1] = target_file;
2817 size[1] = len + 2 /* null */;
2818
2819 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002820 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2821 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002822 kfree(data);
2823 return rc;
2824}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002825
2826int
Steve French897fba12016-05-12 21:20:36 -05002827SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2828 u64 persistent_fid, u64 volatile_fid)
2829{
2830 __u8 delete_pending = 1;
2831 void *data;
2832 unsigned int size;
2833
2834 data = &delete_pending;
2835 size = 1; /* sizeof __u8 */
2836
2837 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2838 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2839 &size);
2840}
2841
2842int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002843SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2845{
2846 struct smb2_file_link_info info;
2847 void **data;
2848 unsigned int size[2];
2849 int rc;
2850 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2851
2852 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2853 if (!data)
2854 return -ENOMEM;
2855
2856 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2857 /* 0 = fail if link already exists */
2858 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2859 info.FileNameLength = cpu_to_le32(len);
2860
2861 data[0] = &info;
2862 size[0] = sizeof(struct smb2_file_link_info);
2863
2864 data[1] = target_file;
2865 size[1] = len + 2 /* null */;
2866
2867 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002868 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002869 kfree(data);
2870 return rc;
2871}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002872
2873int
2874SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002875 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002876{
2877 struct smb2_file_eof_info info;
2878 void *data;
2879 unsigned int size;
2880
2881 info.EndOfFile = *eof;
2882
2883 data = &info;
2884 size = sizeof(struct smb2_file_eof_info);
2885
Steve Frenchf29ebb42014-07-19 21:44:58 -05002886 if (is_falloc)
2887 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2888 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2889 else
2890 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2891 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002892}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002893
2894int
2895SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2896 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2897{
2898 unsigned int size;
2899 size = sizeof(FILE_BASIC_INFO);
2900 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2901 current->tgid, FILE_BASIC_INFORMATION, 1,
2902 (void **)&buf, &size);
2903}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002904
2905int
2906SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2907 const u64 persistent_fid, const u64 volatile_fid,
2908 __u8 oplock_level)
2909{
2910 int rc;
2911 struct smb2_oplock_break *req = NULL;
2912
Joe Perchesf96637b2013-05-04 22:12:25 -05002913 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002914 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2915
2916 if (rc)
2917 return rc;
2918
2919 req->VolatileFid = volatile_fid;
2920 req->PersistentFid = persistent_fid;
2921 req->OplockLevel = oplock_level;
2922 req->hdr.CreditRequest = cpu_to_le16(1);
2923
2924 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2925 /* SMB2 buffer freed by function above */
2926
2927 if (rc) {
2928 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002929 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002930 }
2931
2932 return rc;
2933}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002934
2935static void
2936copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2937 struct kstatfs *kst)
2938{
2939 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2940 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2941 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302942 kst->f_bfree = kst->f_bavail =
2943 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002944 return;
2945}
2946
2947static int
2948build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2949 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2950{
2951 int rc;
2952 struct smb2_query_info_req *req;
2953
Joe Perchesf96637b2013-05-04 22:12:25 -05002954 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002955
2956 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2957 return -EIO;
2958
2959 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2960 if (rc)
2961 return rc;
2962
2963 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2964 req->FileInfoClass = level;
2965 req->PersistentFileId = persistent_fid;
2966 req->VolatileFileId = volatile_fid;
2967 /* 4 for rfc1002 length field and 1 for pad */
2968 req->InputBufferOffset =
2969 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2970 req->OutputBufferLength = cpu_to_le32(
2971 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2972
2973 iov->iov_base = (char *)req;
2974 /* 4 for rfc1002 length field */
2975 iov->iov_len = get_rfc1002_length(req) + 4;
2976 return 0;
2977}
2978
2979int
2980SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2981 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2982{
2983 struct smb2_query_info_rsp *rsp = NULL;
2984 struct kvec iov;
2985 int rc = 0;
2986 int resp_buftype;
2987 struct cifs_ses *ses = tcon->ses;
2988 struct smb2_fs_full_size_info *info = NULL;
2989
2990 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2991 sizeof(struct smb2_fs_full_size_info),
2992 persistent_fid, volatile_fid);
2993 if (rc)
2994 return rc;
2995
2996 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2997 if (rc) {
2998 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002999 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003000 }
3001 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3002
3003 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3004 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3005 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3006 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3007 sizeof(struct smb2_fs_full_size_info));
3008 if (!rc)
3009 copy_fs_info_to_kstatfs(info, fsdata);
3010
Steve French34f62642013-10-09 02:07:00 -05003011qfsinf_exit:
3012 free_rsp_buf(resp_buftype, iov.iov_base);
3013 return rc;
3014}
3015
3016int
3017SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003018 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003019{
3020 struct smb2_query_info_rsp *rsp = NULL;
3021 struct kvec iov;
3022 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003023 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003024 struct cifs_ses *ses = tcon->ses;
3025 unsigned int rsp_len, offset;
3026
Steven French21671142013-10-09 13:36:35 -05003027 if (level == FS_DEVICE_INFORMATION) {
3028 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3029 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3030 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3031 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3032 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003033 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3034 max_len = sizeof(struct smb3_fs_ss_info);
3035 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003036 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003037 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003038 return -EINVAL;
3039 }
3040
3041 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003042 persistent_fid, volatile_fid);
3043 if (rc)
3044 return rc;
3045
3046 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3047 if (rc) {
3048 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3049 goto qfsattr_exit;
3050 }
3051 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3052
3053 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3054 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003055 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3056 if (rc)
3057 goto qfsattr_exit;
3058
3059 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003060 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3061 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003062 rsp_len, max_len));
3063 else if (level == FS_DEVICE_INFORMATION)
3064 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3065 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003066 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3067 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3068 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3069 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3070 tcon->perf_sector_size =
3071 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3072 }
Steve French34f62642013-10-09 02:07:00 -05003073
3074qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003075 free_rsp_buf(resp_buftype, iov.iov_base);
3076 return rc;
3077}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003078
3079int
3080smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3081 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3082 const __u32 num_lock, struct smb2_lock_element *buf)
3083{
3084 int rc = 0;
3085 struct smb2_lock_req *req = NULL;
3086 struct kvec iov[2];
3087 int resp_buf_type;
3088 unsigned int count;
3089
Joe Perchesf96637b2013-05-04 22:12:25 -05003090 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003091
3092 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3093 if (rc)
3094 return rc;
3095
3096 req->hdr.ProcessId = cpu_to_le32(pid);
3097 req->LockCount = cpu_to_le16(num_lock);
3098
3099 req->PersistentFileId = persist_fid;
3100 req->VolatileFileId = volatile_fid;
3101
3102 count = num_lock * sizeof(struct smb2_lock_element);
3103 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3104
3105 iov[0].iov_base = (char *)req;
3106 /* 4 for rfc1002 length field and count for all locks */
3107 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3108 iov[1].iov_base = (char *)buf;
3109 iov[1].iov_len = count;
3110
3111 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3112 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3113 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003114 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003115 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3116 }
3117
3118 return rc;
3119}
3120
3121int
3122SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3123 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3124 const __u64 length, const __u64 offset, const __u32 lock_flags,
3125 const bool wait)
3126{
3127 struct smb2_lock_element lock;
3128
3129 lock.Offset = cpu_to_le64(offset);
3130 lock.Length = cpu_to_le64(length);
3131 lock.Flags = cpu_to_le32(lock_flags);
3132 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3133 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3134
3135 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3136}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003137
3138int
3139SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3140 __u8 *lease_key, const __le32 lease_state)
3141{
3142 int rc;
3143 struct smb2_lease_ack *req = NULL;
3144
Joe Perchesf96637b2013-05-04 22:12:25 -05003145 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003146 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3147
3148 if (rc)
3149 return rc;
3150
3151 req->hdr.CreditRequest = cpu_to_le16(1);
3152 req->StructureSize = cpu_to_le16(36);
3153 inc_rfc1001_len(req, 12);
3154
3155 memcpy(req->LeaseKey, lease_key, 16);
3156 req->LeaseState = lease_state;
3157
3158 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3159 /* SMB2 buffer freed by function above */
3160
3161 if (rc) {
3162 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003163 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003164 }
3165
3166 return rc;
3167}